dv.c 18.5 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * dv.c: Digital video/Firewire input (file: access plug-in)
 *****************************************************************************
 * Copyright (C) 2005 M2X
5
 * $Id$
6 7 8
 *
 * Authors: Jean-Paul Saman <jpsaman at m2x dot nl>
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
9 10 11
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
12 13 14 15
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
18
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
19 20 21
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23 24 25 26
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
27 28 29 30
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

31 32
#include <assert.h>

33
#include <vlc_common.h>
34
#include <vlc_plugin.h>
35
#include <vlc_access.h>
36 37

#include <errno.h>
38
#include <sys/types.h>
39
#include <unistd.h>
40 41 42 43 44 45 46 47 48 49 50 51 52
#include <sys/poll.h>

#include <libraw1394/raw1394.h>
#include <libraw1394/csr.h>
#include <libavc1394/avc1394.h>
#include <libavc1394/avc1394_vcr.h>
#include <libavc1394/rom1394.h>

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );
53
static block_t *Block( access_t *, bool * );
54 55
static int Control( access_t *, int, va_list );

56
vlc_module_begin ()
57
    set_description( N_("Digital Video (Firewire/ieee1394) input") )
58
    set_shortname( N_("DV") )
59 60 61
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_ACCESS )
    set_capability( "access", 0 )
62
    add_shortcut( "dv", "raw1394" )
63 64
    set_callbacks( Open, Close )
vlc_module_end ()
65 66 67

typedef struct
{
68
    vlc_thread_t    thread;
69 70 71 72 73 74 75
    access_t        *p_access;
    vlc_mutex_t     lock;
    block_t         *p_frame;
    block_t         **pp_last;

} event_thread_t;

76
static void* Raw1394EventThread( void * );
77 78 79 80 81
static enum raw1394_iso_disposition
Raw1394Handler(raw1394handle_t, unsigned char *,
        unsigned int, unsigned char,
        unsigned char, unsigned char, unsigned int,
        unsigned int);
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

static int Raw1394GetNumPorts( access_t *p_access );
static raw1394handle_t Raw1394Open( access_t *, int );
static void Raw1394Close( raw1394handle_t );

static int DiscoverAVC( access_t *, int *, uint64_t );
static raw1394handle_t AVCOpen( access_t *, int );
static void AVCClose( access_t * );
static int AVCResetHandler( raw1394handle_t, unsigned int );
static int AVCPlay( access_t *, int );
static int AVCPause( access_t *, int );
static int AVCStop( access_t *, int );

struct access_sys_t
{
    raw1394handle_t p_avc1394;
    raw1394handle_t p_raw1394;
    struct pollfd   raw1394_poll;

    int i_cards;
    int i_node;
    int i_port;
    int i_channel;
    uint64_t i_guid;

    /* event */
    event_thread_t *p_ev;
    vlc_mutex_t lock;
    block_t *p_frame;
};

113 114 115
#define ISOCHRONOUS_QUEUE_LENGTH 1000
#define ISOCHRONOUS_MAX_PACKET_SIZE 4096

116 117 118 119 120 121 122 123 124 125
/*****************************************************************************
 * Open: open the file
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys;

    struct raw1394_portinfo port_inf[ 16 ];

126
    msg_Dbg( p_access, "opening device" );
127 128

    /* Set up p_access */
129
    ACCESS_SET_CALLBACKS( NULL, Block, Control, NULL );
130

131
    p_access->p_sys = p_sys = vlc_malloc( p_this, sizeof( access_sys_t ) );
132 133 134 135 136 137 138 139 140 141 142
    if( !p_sys )
        return VLC_EGENERIC;

    p_sys->i_cards = 0;
    p_sys->i_node = 0;
    p_sys->i_port = 0;
    p_sys->i_guid = 0;
    p_sys->i_channel = 63;
    p_sys->p_raw1394 = NULL;
    p_sys->p_avc1394 = NULL;
    p_sys->p_frame = NULL;
143
    p_sys->p_ev = NULL;
144

145
    vlc_mutex_init( &p_sys->lock );
146 147

    p_sys->i_node = DiscoverAVC( p_access, &p_sys->i_port, p_sys->i_guid );
148 149 150 151 152 153 154
    if( p_sys->i_node < 0 )
    {
        msg_Err( p_access, "failed to open a Firewire (IEEE1394) connection" );
        Close( p_this );
        return VLC_EGENERIC;
    }

155 156 157
    p_sys->p_avc1394 = AVCOpen( p_access, p_sys->i_port );
    if( !p_sys->p_avc1394 )
    {
158
        msg_Err( p_access, "no Digital Video Control device found" );
159 160 161 162 163 164 165
        Close( p_this );
        return VLC_EGENERIC;
    }

    p_sys->p_raw1394 = raw1394_new_handle();
    if( !p_sys->p_raw1394 )
    {
166
        msg_Err( p_access, "no Digital Video device found" );
167 168 169 170 171 172 173
        Close( p_this );
        return VLC_EGENERIC;
    }

    p_sys->i_cards = raw1394_get_port_info( p_sys->p_raw1394, port_inf, 16 );
    if( p_sys->i_cards < 0 )
    {
174
        msg_Err( p_access, "failed to get port info" );
175 176 177 178 179 180
        Close( p_this );
        return VLC_EGENERIC;
    }

    if( raw1394_set_port( p_sys->p_raw1394, p_sys->i_port ) < 0 )
    {
181
        msg_Err( p_access, "failed to set port info" );
182 183 184 185
        Close( p_this );
        return VLC_EGENERIC;
    }

186 187 188 189
    if ( raw1394_iso_recv_init( p_sys->p_raw1394, Raw1394Handler,
                ISOCHRONOUS_QUEUE_LENGTH, ISOCHRONOUS_MAX_PACKET_SIZE,
                p_sys->i_channel, RAW1394_DMA_PACKET_PER_BUFFER, -1 ) < 0 )
    {
190
        msg_Err( p_access, "failed to init isochronous recv" );
191 192 193 194
        Close( p_this );
        return VLC_EGENERIC;
    }

195
    raw1394_set_userdata( p_sys->p_raw1394, p_access );
196
    raw1394_iso_recv_start( p_sys->p_raw1394, -1, -1, 0 );
197 198

    p_sys->raw1394_poll.fd = raw1394_get_fd( p_sys->p_raw1394 );
199
    p_sys->raw1394_poll.events = POLLIN | POLLPRI;
200 201

    /* Now create our event thread catcher */
202
    p_sys->p_ev = calloc( 1, sizeof( *p_sys->p_ev ) );
203 204
    if( !p_sys->p_ev )
    {
205
        msg_Err( p_access, "failed to create event thread struct" );
206
        Close( p_this );
207
        return VLC_ENOMEM;
208
    }
Jean-Paul Saman's avatar
Jean-Paul Saman committed
209

210 211 212
    p_sys->p_ev->p_frame = NULL;
    p_sys->p_ev->pp_last = &p_sys->p_ev->p_frame;
    p_sys->p_ev->p_access = p_access;
213
    vlc_mutex_init( &p_sys->p_ev->lock );
214 215 216 217 218 219 220
    if( vlc_clone( &p_sys->p_ev->thread, Raw1394EventThread,
               p_sys->p_ev, VLC_THREAD_PRIORITY_OUTPUT ) )
    {
        msg_Err( p_access, "failed to clone event thread" );
        Close( p_this );
        return VLC_EGENERIC;
    }
221 222 223 224 225 226 227 228 229 230 231 232

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close: free unused data structures
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys = p_access->p_sys;

233 234 235
    if( p_sys->p_ev )
    {
        /* stop the event handler */
236
        vlc_cancel( p_sys->p_ev->thread );
237

238
        if( p_sys->p_raw1394 )
239
            raw1394_iso_shutdown( p_sys->p_raw1394 );
240

241
        vlc_join( p_sys->p_ev->thread, NULL );
242
        vlc_mutex_destroy( &p_sys->p_ev->lock );
243

244 245 246 247 248 249 250
        /* Cleanup frame data */
        if( p_sys->p_ev->p_frame )
        {
            block_ChainRelease( p_sys->p_ev->p_frame );
            p_sys->p_ev->p_frame = NULL;
            p_sys->p_ev->pp_last = &p_sys->p_frame;
        }
251
        free( p_sys->p_ev );
252 253 254 255
    }

    if( p_sys->p_frame )
        block_ChainRelease( p_sys->p_frame );
256
    if( p_sys->p_raw1394 )
257 258 259 260 261 262 263 264 265 266 267 268
        raw1394_destroy_handle( p_sys->p_raw1394 );

    AVCClose( p_access );

    vlc_mutex_destroy( &p_sys->lock );
}

/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( access_t *p_access, int i_query, va_list args )
{
269 270
    access_sys_t *sys = p_access->p_sys;

271 272 273
    switch( i_query )
    {
        /* */
274
        case STREAM_CAN_PAUSE:
275
            *va_arg( args, bool* ) = true;
276 277
            break;

278 279 280
       case STREAM_CAN_SEEK:
       case STREAM_CAN_FASTSEEK:
       case STREAM_CAN_CONTROL_PACE:
281
            *va_arg( args, bool* ) = false;
282 283
            break;

284
        case STREAM_GET_PTS_DELAY:
285 286
            *va_arg( args, int64_t * ) =
                INT64_C(1000) * var_InheritInteger( p_access, "live-caching" );
287 288 289
            break;

        /* */
290
        case STREAM_SET_PAUSE_STATE:
291
            AVCPause( p_access, sys->i_node );
292 293 294 295 296 297 298 299 300
            break;

        default:
            return VLC_EGENERIC;

    }
    return VLC_SUCCESS;
}

301
static block_t *Block( access_t *p_access, bool *restrict eof )
302 303 304 305 306 307
{
    access_sys_t *p_sys = p_access->p_sys;
    block_t *p_block = NULL;

    vlc_mutex_lock( &p_sys->lock );
    p_block = p_sys->p_frame;
308
    //msg_Dbg( p_access, "sending frame %p", (void *)p_block );
309 310 311
    p_sys->p_frame = NULL;
    vlc_mutex_unlock( &p_sys->lock );

312
    (void) eof;
313 314 315
    return p_block;
}

316 317 318
static void Raw1394EventThreadCleanup( void *obj )
{
    event_thread_t *p_ev = (event_thread_t *)obj;
319
    access_sys_t *sys = p_ev->p_access->p_sys;
320

321
    AVCStop( p_ev->p_access, sys->i_node );
322 323 324
}

static void* Raw1394EventThread( void *obj )
325
{
326
    event_thread_t *p_ev = (event_thread_t *)obj;
327 328 329
    access_t *p_access = (access_t *) p_ev->p_access;
    access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
    int result = 0;
330
    int canc = vlc_savecancel();
331 332

    AVCPlay( p_access, p_sys->i_node );
333 334
    vlc_cleanup_push( Raw1394EventThreadCleanup, p_ev );
    vlc_restorecancel( canc );
335

336
    for( ;; )
337
    {
338
        while( ( result = poll( &p_sys->raw1394_poll, 1, -1 ) ) < 0 )
339
        {
340
            if( errno != EINTR )
341
                msg_Err( p_access, "poll error: %s", vlc_strerror_c(errno) );
342
        }
343

344
        if( result > 0 && ( ( p_sys->raw1394_poll.revents & POLLIN )
345 346 347
                         || ( p_sys->raw1394_poll.revents & POLLPRI ) ) )
        {
            canc = vlc_savecancel();
348
            result = raw1394_loop_iterate( p_sys->p_raw1394 );
349 350
            vlc_restorecancel( canc );
        }
351 352
    }

353 354
    vlc_cleanup_pop();
    vlc_assert_unreachable();
355 356
}

357 358 359 360 361
static enum raw1394_iso_disposition
Raw1394Handler(raw1394handle_t handle, unsigned char *data,
        unsigned int length, unsigned char channel,
        unsigned char tag, unsigned char sy, unsigned int cycle,
        unsigned int dropped)
362 363 364 365
{
    access_t *p_access = NULL;
    access_sys_t *p_sys = NULL;
    block_t *p_block = NULL;
366 367
    VLC_UNUSED(channel); VLC_UNUSED(tag);
    VLC_UNUSED(sy); VLC_UNUSED(cycle); VLC_UNUSED(dropped);
368 369 370 371 372 373 374

    p_access = (access_t *) raw1394_get_userdata( handle );
    if( !p_access ) return 0;

    p_sys = p_access->p_sys;

    /* skip empty packets */
Jean-Paul Saman's avatar
Jean-Paul Saman committed
375
    if( length > 16 )
376
    {
377
        unsigned char * p = data + 8;
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
        int section_type = p[ 0 ] >> 5;           /* section type is in bits 5 - 7 */
        int dif_sequence = p[ 1 ] >> 4;           /* dif sequence number is in bits 4 - 7 */
        int dif_block = p[ 2 ];

        vlc_mutex_lock( &p_sys->p_ev->lock );

        /* if we are at the beginning of a frame, we put the previous
           frame in our output_queue. */
        if( (section_type == 0) && (dif_sequence == 0) )
        {
            vlc_mutex_lock( &p_sys->lock );
            if( p_sys->p_ev->p_frame )
            {
                /* Push current frame to p_access thread. */
                //p_sys->p_ev->p_frame->i_pts = mdate();
                block_ChainAppend( &p_sys->p_frame, p_sys->p_ev->p_frame );
            }
            /* reset list */
396
            p_sys->p_ev->p_frame = block_Alloc( 144000 );
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
            p_sys->p_ev->pp_last = &p_sys->p_frame;
            vlc_mutex_unlock( &p_sys->lock );
        }

        p_block = p_sys->p_ev->p_frame;
        if( p_block )
        {
            switch ( section_type )
            {
            case 0:    /* 1 Header block */
                /* p[3] |= 0x80; // hack to force PAL data */
                memcpy( p_block->p_buffer + dif_sequence * 150 * 80, p, 480 );
                break;

            case 1:    /* 2 Subcode blocks */
                memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 1 + dif_block ) * 80, p, 480 );
                break;

            case 2:    /* 3 VAUX blocks */
                memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 3 + dif_block ) * 80, p, 480 );
                break;

            case 3:    /* 9 Audio blocks interleaved with video */
                memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 6 + dif_block * 16 ) * 80, p, 480 );
                break;

            case 4:    /* 135 Video blocks interleaved with audio */
                memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 7 + ( dif_block / 15 ) + dif_block ) * 80, p, 480 );
                break;

            default:    /* we can´t handle any other data */
                block_Release( p_block );
                p_block = NULL;
                break;
            }
        }
433

434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
        vlc_mutex_unlock( &p_sys->p_ev->lock );
    }
    return 0;
}

/*
 * Routing borrowed from dvgrab-1.8
 * Copyright by Arne Schirmacher <dvgrab@schirmacher.de>
 * Dan Dennedy <dan@dennedy.org> and others
 */
static int Raw1394GetNumPorts( access_t *p_access )
{
    int n_ports;
    struct raw1394_portinfo pinf[ 16 ];
    raw1394handle_t handle;

    /* get a raw1394 handle */
Jean-Paul Saman's avatar
Jean-Paul Saman committed
451
    if( !( handle = raw1394_new_handle() ) )
452
    {
453 454
        msg_Err( p_access, "raw1394 - failed to get handle: %s",
                 vlc_strerror_c(errno) );
455 456 457
        return VLC_EGENERIC;
    }

Jean-Paul Saman's avatar
Jean-Paul Saman committed
458
    if( ( n_ports = raw1394_get_port_info( handle, pinf, 16 ) ) < 0 )
459
    {
460 461
        msg_Err( p_access, "raw1394 - failed to get port info: %s",
                 vlc_strerror_c(errno) );
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
        raw1394_destroy_handle( handle );
        return VLC_EGENERIC;
    }
    raw1394_destroy_handle( handle );

    return n_ports;
}

static raw1394handle_t Raw1394Open( access_t *p_access, int port )
{
    int n_ports;
    struct raw1394_portinfo pinf[ 16 ];
    raw1394handle_t handle;

    /* get a raw1394 handle */
    handle = raw1394_new_handle();
Jean-Paul Saman's avatar
Jean-Paul Saman committed
478
    if( !handle )
479
    {
480 481
        msg_Err( p_access, "raw1394 - failed to get handle: %s",
                 vlc_strerror_c(errno) );
482 483 484
        return NULL;
    }

Jean-Paul Saman's avatar
Jean-Paul Saman committed
485
    if( ( n_ports = raw1394_get_port_info( handle, pinf, 16 ) ) < 0 )
486
    {
487 488
        msg_Err( p_access, "raw1394 - failed to get port info: %s",
                 vlc_strerror_c(errno) );
489 490 491 492 493
        raw1394_destroy_handle( handle );
        return NULL;
    }

    /* tell raw1394 which host adapter to use */
Jean-Paul Saman's avatar
Jean-Paul Saman committed
494
    if( raw1394_set_port( handle, port ) < 0 )
495
    {
496 497
        msg_Err( p_access, "raw1394 - failed to set set port: %s",
                 vlc_strerror_c(errno) );
498 499 500 501 502 503 504 505 506 507 508 509 510 511
        return NULL;
    }

    return handle;
}

static void Raw1394Close( raw1394handle_t handle )
{
    raw1394_destroy_handle( handle );
}

static int DiscoverAVC( access_t *p_access, int* port, uint64_t guid )
{
    rom1394_directory rom_dir;
512
    raw1394handle_t handle = NULL;
513 514 515 516 517 518 519 520 521 522 523 524 525 526
    int device = -1;
    int i, j = 0;
    int m = Raw1394GetNumPorts( p_access );

    if( *port >= 0 )
    {
        /* search on explicit port */
        j = *port;
        m = *port + 1;
    }

    for( ; j < m && device == -1; j++ )
    {
        handle = Raw1394Open( p_access, j );
527 528 529
        if( !handle )
            return -1;

530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
        for( i = 0; i < raw1394_get_nodecount( handle ); ++i )
        {
            if( guid != 0 )
            {
                /* select explicitly by GUID */
                if( guid == rom1394_get_guid( handle, i ) )
                {
                    device = i;
                    *port = j;
                    break;
                }
            }
            else
            {
                /* select first AV/C Tape Reccorder Player node */
                if( rom1394_get_directory( handle, i, &rom_dir ) < 0 )
                {
547
                    msg_Err( p_access, "error reading config rom directory for node %d", i );
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
                    continue;
                }
                if( ( rom1394_get_node_type( &rom_dir ) == ROM1394_NODE_TYPE_AVC ) &&
                        avc1394_check_subunit_type( handle, i, AVC1394_SUBUNIT_TYPE_VCR ) )
                {
                    device = i;
                    *port = j;
                    break;
                }
            }
        }
        Raw1394Close( handle );
    }

    return device;
}

/*
 * Handle AVC commands
 */
static raw1394handle_t AVCOpen( access_t *p_access, int port )
{
    access_sys_t *p_sys = p_access->p_sys;
    struct raw1394_portinfo pinf[ 16 ];
Jean-Paul Saman's avatar
Jean-Paul Saman committed
572
    int numcards;
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611

    p_sys->p_avc1394 = raw1394_new_handle();
    if( !p_sys->p_avc1394 )
        return NULL;

    numcards = raw1394_get_port_info( p_sys->p_avc1394, pinf, 16 );
    if( numcards < -1 )
        return NULL;
    if( raw1394_set_port( p_sys->p_avc1394, port ) < 0 )
        return NULL;

    raw1394_set_bus_reset_handler( p_sys->p_avc1394, AVCResetHandler );

    return  p_sys->p_avc1394;
}

static void AVCClose( access_t *p_access )
{
    access_sys_t *p_sys = p_access->p_sys;

    if( p_sys->p_avc1394 )
    {
        raw1394_destroy_handle( p_sys->p_avc1394 );
        p_sys->p_avc1394 = NULL;
    }
}

static int AVCResetHandler( raw1394handle_t handle, unsigned int generation )
{
    raw1394_update_generation( handle, generation );
    return 0;
}

static int AVCPlay( access_t *p_access, int phyID )
{
    access_sys_t *p_sys = p_access->p_sys;

    msg_Dbg( p_access, "send play command over Digital Video control channel" );

612
    if( p_sys->p_avc1394 && phyID >= 0 )
613 614 615 616 617 618 619 620 621 622 623 624
    {
        if( !avc1394_vcr_is_recording( p_sys->p_avc1394, phyID ) &&
            avc1394_vcr_is_playing( p_sys->p_avc1394, phyID ) != AVC1394_VCR_OPERAND_PLAY_FORWARD )
                avc1394_vcr_play( p_sys->p_avc1394, phyID );
    }
    return 0;
}

static int AVCPause( access_t *p_access, int phyID )
{
    access_sys_t *p_sys = p_access->p_sys;

625
    if( p_sys->p_avc1394 && phyID >= 0 )
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
    {
        if( !avc1394_vcr_is_recording( p_sys->p_avc1394, phyID ) &&
            ( avc1394_vcr_is_playing( p_sys->p_avc1394, phyID ) != AVC1394_VCR_OPERAND_PLAY_FORWARD_PAUSE ) )
                avc1394_vcr_pause( p_sys->p_avc1394, phyID );
    }
    return 0;
}


static int AVCStop( access_t *p_access, int phyID )
{
    access_sys_t *p_sys = p_access->p_sys;

    msg_Dbg( p_access, "closing Digital Video control channel" );

641
    if ( p_sys->p_avc1394 && phyID >= 0 )
642 643 644 645
        avc1394_vcr_stop( p_sys->p_avc1394, phyID );

    return 0;
}