1 /*****************************************************************************
2 * dv.c: Digital video/Firewire input (file: access plug-in)
3 *****************************************************************************
4 * Copyright (C) 2005 M2X
7 * Authors: Jean-Paul Saman <jpsaman at m2x dot nl>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_access.h>
36 #include <sys/types.h>
39 #elif defined( _WIN32 )
45 #include <libraw1394/raw1394.h>
46 #include <libraw1394/csr.h>
47 #include <libavc1394/avc1394.h>
48 #include <libavc1394/avc1394_vcr.h>
49 #include <libavc1394/rom1394.h>
51 /*****************************************************************************
53 *****************************************************************************/
54 static int Open ( vlc_object_t * );
55 static void Close( vlc_object_t * );
56 static block_t *Block( access_t * );
57 static int Control( access_t *, int, va_list );
60 set_description( N_("Digital Video (Firewire/ieee1394) input") )
61 set_shortname( N_("DV") )
62 set_category( CAT_INPUT )
63 set_subcategory( SUBCAT_INPUT_ACCESS )
64 set_capability( "access", 0 )
65 add_shortcut( "dv", "raw1394" )
66 set_callbacks( Open, Close )
79 static void* Raw1394EventThread( void * );
80 static enum raw1394_iso_disposition
81 Raw1394Handler(raw1394handle_t, unsigned char *,
82 unsigned int, unsigned char,
83 unsigned char, unsigned char, unsigned int,
86 static int Raw1394GetNumPorts( access_t *p_access );
87 static raw1394handle_t Raw1394Open( access_t *, int );
88 static void Raw1394Close( raw1394handle_t );
90 static int DiscoverAVC( access_t *, int *, uint64_t );
91 static raw1394handle_t AVCOpen( access_t *, int );
92 static void AVCClose( access_t * );
93 static int AVCResetHandler( raw1394handle_t, unsigned int );
94 static int AVCPlay( access_t *, int );
95 static int AVCPause( access_t *, int );
96 static int AVCStop( access_t *, int );
100 raw1394handle_t p_avc1394;
101 raw1394handle_t p_raw1394;
102 struct pollfd raw1394_poll;
111 event_thread_t *p_ev;
116 #define ISOCHRONOUS_QUEUE_LENGTH 1000
117 #define ISOCHRONOUS_MAX_PACKET_SIZE 4096
119 /*****************************************************************************
120 * Open: open the file
121 *****************************************************************************/
122 static int Open( vlc_object_t *p_this )
124 access_t *p_access = (access_t*)p_this;
127 struct raw1394_portinfo port_inf[ 16 ];
129 msg_Dbg( p_access, "opening device" );
131 /* Set up p_access */
132 access_InitFields( p_access );
133 ACCESS_SET_CALLBACKS( NULL, Block, Control, NULL );
135 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
143 p_sys->i_channel = 63;
144 p_sys->p_raw1394 = NULL;
145 p_sys->p_avc1394 = NULL;
146 p_sys->p_frame = NULL;
149 vlc_mutex_init( &p_sys->lock );
151 p_sys->i_node = DiscoverAVC( p_access, &p_sys->i_port, p_sys->i_guid );
152 if( p_sys->i_node < 0 )
154 msg_Err( p_access, "failed to open a Firewire (IEEE1394) connection" );
159 p_sys->p_avc1394 = AVCOpen( p_access, p_sys->i_port );
160 if( !p_sys->p_avc1394 )
162 msg_Err( p_access, "no Digital Video Control device found" );
167 p_sys->p_raw1394 = raw1394_new_handle();
168 if( !p_sys->p_raw1394 )
170 msg_Err( p_access, "no Digital Video device found" );
175 p_sys->i_cards = raw1394_get_port_info( p_sys->p_raw1394, port_inf, 16 );
176 if( p_sys->i_cards < 0 )
178 msg_Err( p_access, "failed to get port info" );
183 if( raw1394_set_port( p_sys->p_raw1394, p_sys->i_port ) < 0 )
185 msg_Err( p_access, "failed to set port info" );
190 if ( raw1394_iso_recv_init( p_sys->p_raw1394, Raw1394Handler,
191 ISOCHRONOUS_QUEUE_LENGTH, ISOCHRONOUS_MAX_PACKET_SIZE,
192 p_sys->i_channel, RAW1394_DMA_PACKET_PER_BUFFER, -1 ) < 0 )
194 msg_Err( p_access, "failed to init isochronous recv" );
199 raw1394_set_userdata( p_sys->p_raw1394, p_access );
200 raw1394_iso_recv_start( p_sys->p_raw1394, -1, -1, 0 );
202 p_sys->raw1394_poll.fd = raw1394_get_fd( p_sys->p_raw1394 );
203 p_sys->raw1394_poll.events = POLLIN | POLLPRI;
205 /* Now create our event thread catcher */
206 p_sys->p_ev = calloc( 1, sizeof( *p_sys->p_ev ) );
209 msg_Err( p_access, "failed to create event thread struct" );
214 p_sys->p_ev->p_frame = NULL;
215 p_sys->p_ev->pp_last = &p_sys->p_ev->p_frame;
216 p_sys->p_ev->p_access = p_access;
217 vlc_mutex_init( &p_sys->p_ev->lock );
218 if( vlc_clone( &p_sys->p_ev->thread, Raw1394EventThread,
219 p_sys->p_ev, VLC_THREAD_PRIORITY_OUTPUT ) )
221 msg_Err( p_access, "failed to clone event thread" );
229 /*****************************************************************************
230 * Close: free unused data structures
231 *****************************************************************************/
232 static void Close( vlc_object_t *p_this )
234 access_t *p_access = (access_t*)p_this;
235 access_sys_t *p_sys = p_access->p_sys;
239 /* stop the event handler */
240 vlc_cancel( p_sys->p_ev->thread );
242 if( p_sys->p_raw1394 )
243 raw1394_iso_shutdown( p_sys->p_raw1394 );
245 vlc_join( p_sys->p_ev->thread, NULL );
246 vlc_mutex_destroy( &p_sys->p_ev->lock );
248 /* Cleanup frame data */
249 if( p_sys->p_ev->p_frame )
251 block_ChainRelease( p_sys->p_ev->p_frame );
252 p_sys->p_ev->p_frame = NULL;
253 p_sys->p_ev->pp_last = &p_sys->p_frame;
259 block_ChainRelease( p_sys->p_frame );
260 if( p_sys->p_raw1394 )
261 raw1394_destroy_handle( p_sys->p_raw1394 );
263 AVCClose( p_access );
265 vlc_mutex_destroy( &p_sys->lock );
269 /*****************************************************************************
271 *****************************************************************************/
272 static int Control( access_t *p_access, int i_query, va_list args )
277 case ACCESS_CAN_PAUSE:
278 *va_arg( args, bool* ) = true;
281 case ACCESS_CAN_SEEK:
282 case ACCESS_CAN_FASTSEEK:
283 case ACCESS_CAN_CONTROL_PACE:
284 *va_arg( args, bool* ) = false;
287 case ACCESS_GET_PTS_DELAY:
288 *va_arg( args, int64_t * ) =
289 INT64_C(1000) * var_InheritInteger( p_access, "live-caching" );
293 case ACCESS_SET_PAUSE_STATE:
294 AVCPause( p_access, p_access->p_sys->i_node );
297 case ACCESS_GET_TITLE_INFO:
298 case ACCESS_SET_TITLE:
299 case ACCESS_SET_SEEKPOINT:
300 case ACCESS_SET_PRIVATE_ID_STATE:
301 case ACCESS_GET_CONTENT_TYPE:
305 msg_Warn( p_access, "unimplemented query in control" );
312 static block_t *Block( access_t *p_access )
314 access_sys_t *p_sys = p_access->p_sys;
315 block_t *p_block = NULL;
317 vlc_mutex_lock( &p_sys->lock );
318 p_block = p_sys->p_frame;
319 //msg_Dbg( p_access, "sending frame %p",p_block );
320 p_sys->p_frame = NULL;
321 vlc_mutex_unlock( &p_sys->lock );
326 static void Raw1394EventThreadCleanup( void *obj )
328 event_thread_t *p_ev = (event_thread_t *)obj;
330 AVCStop( p_ev->p_access, p_ev->p_access->p_sys->i_node );
333 static void* Raw1394EventThread( void *obj )
335 event_thread_t *p_ev = (event_thread_t *)obj;
336 access_t *p_access = (access_t *) p_ev->p_access;
337 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
339 int canc = vlc_savecancel();
341 AVCPlay( p_access, p_sys->i_node );
342 vlc_cleanup_push( Raw1394EventThreadCleanup, p_ev );
343 vlc_restorecancel( canc );
347 while( ( result = poll( &p_sys->raw1394_poll, 1, -1 ) ) < 0 )
350 msg_Err( p_access, "poll error: %m" );
353 if( result > 0 && ( ( p_sys->raw1394_poll.revents & POLLIN )
354 || ( p_sys->raw1394_poll.revents & POLLPRI ) ) )
356 canc = vlc_savecancel();
357 result = raw1394_loop_iterate( p_sys->p_raw1394 );
358 vlc_restorecancel( canc );
366 static enum raw1394_iso_disposition
367 Raw1394Handler(raw1394handle_t handle, unsigned char *data,
368 unsigned int length, unsigned char channel,
369 unsigned char tag, unsigned char sy, unsigned int cycle,
370 unsigned int dropped)
372 access_t *p_access = NULL;
373 access_sys_t *p_sys = NULL;
374 block_t *p_block = NULL;
375 VLC_UNUSED(channel); VLC_UNUSED(tag);
376 VLC_UNUSED(sy); VLC_UNUSED(cycle); VLC_UNUSED(dropped);
378 p_access = (access_t *) raw1394_get_userdata( handle );
379 if( !p_access ) return 0;
381 p_sys = p_access->p_sys;
383 /* skip empty packets */
386 unsigned char * p = data + 8;
387 int section_type = p[ 0 ] >> 5; /* section type is in bits 5 - 7 */
388 int dif_sequence = p[ 1 ] >> 4; /* dif sequence number is in bits 4 - 7 */
389 int dif_block = p[ 2 ];
391 vlc_mutex_lock( &p_sys->p_ev->lock );
393 /* if we are at the beginning of a frame, we put the previous
394 frame in our output_queue. */
395 if( (section_type == 0) && (dif_sequence == 0) )
397 vlc_mutex_lock( &p_sys->lock );
398 if( p_sys->p_ev->p_frame )
400 /* Push current frame to p_access thread. */
401 //p_sys->p_ev->p_frame->i_pts = mdate();
402 block_ChainAppend( &p_sys->p_frame, p_sys->p_ev->p_frame );
405 p_sys->p_ev->p_frame = block_Alloc( 144000 );
406 p_sys->p_ev->pp_last = &p_sys->p_frame;
407 vlc_mutex_unlock( &p_sys->lock );
410 p_block = p_sys->p_ev->p_frame;
413 switch ( section_type )
415 case 0: /* 1 Header block */
416 /* p[3] |= 0x80; // hack to force PAL data */
417 memcpy( p_block->p_buffer + dif_sequence * 150 * 80, p, 480 );
420 case 1: /* 2 Subcode blocks */
421 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 1 + dif_block ) * 80, p, 480 );
424 case 2: /* 3 VAUX blocks */
425 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 3 + dif_block ) * 80, p, 480 );
428 case 3: /* 9 Audio blocks interleaved with video */
429 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 6 + dif_block * 16 ) * 80, p, 480 );
432 case 4: /* 135 Video blocks interleaved with audio */
433 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 7 + ( dif_block / 15 ) + dif_block ) * 80, p, 480 );
436 default: /* we canĀ“t handle any other data */
437 block_Release( p_block );
443 vlc_mutex_unlock( &p_sys->p_ev->lock );
449 * Routing borrowed from dvgrab-1.8
450 * Copyright by Arne Schirmacher <dvgrab@schirmacher.de>
451 * Dan Dennedy <dan@dennedy.org> and others
453 static int Raw1394GetNumPorts( access_t *p_access )
456 struct raw1394_portinfo pinf[ 16 ];
457 raw1394handle_t handle;
459 /* get a raw1394 handle */
460 if( !( handle = raw1394_new_handle() ) )
462 msg_Err( p_access, "raw1394 - failed to get handle: %m." );
466 if( ( n_ports = raw1394_get_port_info( handle, pinf, 16 ) ) < 0 )
468 msg_Err( p_access, "raw1394 - failed to get port info: %m." );
469 raw1394_destroy_handle( handle );
472 raw1394_destroy_handle( handle );
477 static raw1394handle_t Raw1394Open( access_t *p_access, int port )
480 struct raw1394_portinfo pinf[ 16 ];
481 raw1394handle_t handle;
483 /* get a raw1394 handle */
484 handle = raw1394_new_handle();
487 msg_Err( p_access, "raw1394 - failed to get handle: %m." );
491 if( ( n_ports = raw1394_get_port_info( handle, pinf, 16 ) ) < 0 )
493 msg_Err( p_access, "raw1394 - failed to get port info: %m." );
494 raw1394_destroy_handle( handle );
498 /* tell raw1394 which host adapter to use */
499 if( raw1394_set_port( handle, port ) < 0 )
501 msg_Err( p_access, "raw1394 - failed to set set port: %m." );
508 static void Raw1394Close( raw1394handle_t handle )
510 raw1394_destroy_handle( handle );
513 static int DiscoverAVC( access_t *p_access, int* port, uint64_t guid )
515 rom1394_directory rom_dir;
516 raw1394handle_t handle = NULL;
519 int m = Raw1394GetNumPorts( p_access );
523 /* search on explicit port */
528 for( ; j < m && device == -1; j++ )
530 handle = Raw1394Open( p_access, j );
534 for( i = 0; i < raw1394_get_nodecount( handle ); ++i )
538 /* select explicitly by GUID */
539 if( guid == rom1394_get_guid( handle, i ) )
548 /* select first AV/C Tape Reccorder Player node */
549 if( rom1394_get_directory( handle, i, &rom_dir ) < 0 )
551 msg_Err( p_access, "error reading config rom directory for node %d", i );
554 if( ( rom1394_get_node_type( &rom_dir ) == ROM1394_NODE_TYPE_AVC ) &&
555 avc1394_check_subunit_type( handle, i, AVC1394_SUBUNIT_TYPE_VCR ) )
563 Raw1394Close( handle );
570 * Handle AVC commands
572 static raw1394handle_t AVCOpen( access_t *p_access, int port )
574 access_sys_t *p_sys = p_access->p_sys;
575 struct raw1394_portinfo pinf[ 16 ];
578 p_sys->p_avc1394 = raw1394_new_handle();
579 if( !p_sys->p_avc1394 )
582 numcards = raw1394_get_port_info( p_sys->p_avc1394, pinf, 16 );
585 if( raw1394_set_port( p_sys->p_avc1394, port ) < 0 )
588 raw1394_set_bus_reset_handler( p_sys->p_avc1394, AVCResetHandler );
590 return p_sys->p_avc1394;
593 static void AVCClose( access_t *p_access )
595 access_sys_t *p_sys = p_access->p_sys;
597 if( p_sys->p_avc1394 )
599 raw1394_destroy_handle( p_sys->p_avc1394 );
600 p_sys->p_avc1394 = NULL;
604 static int AVCResetHandler( raw1394handle_t handle, unsigned int generation )
606 raw1394_update_generation( handle, generation );
610 static int AVCPlay( access_t *p_access, int phyID )
612 access_sys_t *p_sys = p_access->p_sys;
614 msg_Dbg( p_access, "send play command over Digital Video control channel" );
616 if( p_sys->p_avc1394 && phyID >= 0 )
618 if( !avc1394_vcr_is_recording( p_sys->p_avc1394, phyID ) &&
619 avc1394_vcr_is_playing( p_sys->p_avc1394, phyID ) != AVC1394_VCR_OPERAND_PLAY_FORWARD )
620 avc1394_vcr_play( p_sys->p_avc1394, phyID );
625 static int AVCPause( access_t *p_access, int phyID )
627 access_sys_t *p_sys = p_access->p_sys;
629 if( p_sys->p_avc1394 && phyID >= 0 )
631 if( !avc1394_vcr_is_recording( p_sys->p_avc1394, phyID ) &&
632 ( avc1394_vcr_is_playing( p_sys->p_avc1394, phyID ) != AVC1394_VCR_OPERAND_PLAY_FORWARD_PAUSE ) )
633 avc1394_vcr_pause( p_sys->p_avc1394, phyID );
639 static int AVCStop( access_t *p_access, int phyID )
641 access_sys_t *p_sys = p_access->p_sys;
643 msg_Dbg( p_access, "closing Digital Video control channel" );
645 if ( p_sys->p_avc1394 && phyID >= 0 )
646 avc1394_vcr_stop( p_sys->p_avc1394, phyID );