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
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 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 General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, 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 #ifdef HAVE_SYS_TYPES_H
37 # include <sys/types.h>
39 #ifdef HAVE_SYS_TIME_H
40 # include <sys/time.h>
42 #ifdef HAVE_SYS_STAT_H
43 # include <sys/stat.h>
51 #elif defined( WIN32 ) && !defined( UNDER_CE )
57 #include <libraw1394/raw1394.h>
58 #include <libraw1394/csr.h>
59 #include <libavc1394/avc1394.h>
60 #include <libavc1394/avc1394_vcr.h>
61 #include <libavc1394/rom1394.h>
63 /*****************************************************************************
65 *****************************************************************************/
66 static int Open ( vlc_object_t * );
67 static void Close( vlc_object_t * );
68 static block_t *Block( access_t * );
69 static int Control( access_t *, int, va_list );
71 #define CACHING_TEXT N_("Caching value in ms")
72 #define CACHING_LONGTEXT N_( \
73 "Caching value for DV streams. This " \
74 "value should be set in milliseconds." )
77 set_description( N_("Digital Video (Firewire/ieee1394) input") )
78 set_shortname( N_("dv") )
79 set_category( CAT_INPUT )
80 set_subcategory( SUBCAT_INPUT_ACCESS )
81 add_integer( "dv-caching", 60000 / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, true )
82 set_capability( "access", 0 )
84 add_shortcut( "dv1394" )
85 add_shortcut( "raw1394" )
86 set_callbacks( Open, Close )
100 static void* Raw1394EventThread( vlc_object_t * );
101 static int Raw1394Handler( raw1394handle_t, int, size_t, quadlet_t * );
103 static int Raw1394GetNumPorts( access_t *p_access );
104 static raw1394handle_t Raw1394Open( access_t *, int );
105 static void Raw1394Close( raw1394handle_t );
107 static int DiscoverAVC( access_t *, int *, uint64_t );
108 static raw1394handle_t AVCOpen( access_t *, int );
109 static void AVCClose( access_t * );
110 static int AVCResetHandler( raw1394handle_t, unsigned int );
111 static int AVCPlay( access_t *, int );
112 static int AVCPause( access_t *, int );
113 static int AVCStop( access_t *, int );
117 raw1394handle_t p_avc1394;
118 raw1394handle_t p_raw1394;
119 struct pollfd raw1394_poll;
128 event_thread_t *p_ev;
133 /*****************************************************************************
134 * Open: open the file
135 *****************************************************************************/
136 static int Open( vlc_object_t *p_this )
138 access_t *p_access = (access_t*)p_this;
140 char *psz_name = strdup( p_access->psz_path );
142 struct raw1394_portinfo port_inf[ 16 ];
143 iso_handler_t oldhandler;
145 msg_Dbg( p_access, "opening device %s", psz_name );
147 /* Set up p_access */
148 access_InitFields( p_access );
149 ACCESS_SET_CALLBACKS( NULL, Block, Control, NULL );
151 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
162 p_sys->i_channel = 63;
163 p_sys->p_raw1394 = NULL;
164 p_sys->p_avc1394 = NULL;
165 p_sys->p_frame = NULL;
168 vlc_mutex_init( &p_sys->lock );
170 p_sys->i_node = DiscoverAVC( p_access, &p_sys->i_port, p_sys->i_guid );
171 if( p_sys->i_node < 0 )
173 msg_Err( p_access, "failed to open a Firewire (IEEE1394) connection" );
179 p_sys->p_avc1394 = AVCOpen( p_access, p_sys->i_port );
180 if( !p_sys->p_avc1394 )
182 msg_Err( p_access, "no Digital Video Control device found on %s", psz_name );
188 p_sys->p_raw1394 = raw1394_new_handle();
189 if( !p_sys->p_raw1394 )
191 msg_Err( p_access, "no Digital Video device found on %s", psz_name );
197 p_sys->i_cards = raw1394_get_port_info( p_sys->p_raw1394, port_inf, 16 );
198 if( p_sys->i_cards < 0 )
200 msg_Err( p_access, "failed to get port info for %s", psz_name );
206 if( raw1394_set_port( p_sys->p_raw1394, p_sys->i_port ) < 0 )
208 msg_Err( p_access, "failed to set port info for %s", psz_name );
214 oldhandler = raw1394_set_iso_handler( p_sys->p_raw1394,
215 p_sys->i_channel, Raw1394Handler );
216 raw1394_set_userdata( p_sys->p_raw1394, p_access );
217 raw1394_start_iso_rcv( p_sys->p_raw1394, p_sys->i_channel );
219 p_sys->raw1394_poll.fd = raw1394_get_fd( p_sys->p_raw1394 );
220 p_sys->raw1394_poll.events = POLLIN | POLLPRI;
222 /* Update default_pts to a suitable value for udp access */
223 var_Create( p_access, "dv-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
225 /* Now create our event thread catcher */
226 p_sys->p_ev = vlc_object_create( p_access, sizeof( event_thread_t ) );
229 msg_Err( p_access, "failed to create event thread for %s", psz_name );
235 p_sys->p_ev->p_frame = NULL;
236 p_sys->p_ev->pp_last = &p_sys->p_ev->p_frame;
237 p_sys->p_ev->p_access = p_access;
238 vlc_mutex_init( &p_sys->p_ev->lock );
239 vlc_thread_create( p_sys->p_ev, "dv event thread handler", Raw1394EventThread,
240 VLC_THREAD_PRIORITY_OUTPUT, false );
246 /*****************************************************************************
247 * Close: free unused data structures
248 *****************************************************************************/
249 static void Close( vlc_object_t *p_this )
251 access_t *p_access = (access_t*)p_this;
252 access_sys_t *p_sys = p_access->p_sys;
256 /* stop the event handler */
257 vlc_object_kill( p_sys->p_ev );
259 if( p_sys->p_raw1394 )
260 raw1394_stop_iso_rcv( p_sys->p_raw1394, p_sys->i_channel );
262 vlc_mutex_destroy( &p_sys->p_ev->lock );
263 vlc_thread_join( p_sys->p_ev );
265 /* Cleanup frame data */
266 if( p_sys->p_ev->p_frame )
268 vlc_mutex_lock( &p_sys->p_ev->lock );
269 block_ChainRelease( p_sys->p_ev->p_frame );
270 p_sys->p_ev->p_frame = NULL;
271 p_sys->p_ev->pp_last = &p_sys->p_frame;
272 vlc_mutex_unlock( &p_sys->p_ev->lock );
274 vlc_object_release( p_sys->p_ev );
278 block_ChainRelease( p_sys->p_frame );
279 if( p_sys->p_raw1394 )
280 raw1394_destroy_handle( p_sys->p_raw1394 );
282 AVCClose( p_access );
284 vlc_mutex_destroy( &p_sys->lock );
288 /*****************************************************************************
290 *****************************************************************************/
291 static int Control( access_t *p_access, int i_query, va_list args )
293 access_sys_t *p_sys = p_access->p_sys;
300 case ACCESS_CAN_PAUSE:
301 pb_bool = (bool*)va_arg( args, bool* );
305 case ACCESS_CAN_SEEK:
306 case ACCESS_CAN_FASTSEEK:
307 case ACCESS_CAN_CONTROL_PACE:
308 pb_bool = (bool*)va_arg( args, bool* );
312 case ACCESS_GET_PTS_DELAY:
313 pi_64 = (int64_t*)va_arg( args, int64_t * );
314 *pi_64 = var_GetInteger( p_access, "dv-caching" ) * 1000;
318 case ACCESS_SET_PAUSE_STATE:
319 AVCPause( p_access, p_sys->i_node );
322 case ACCESS_GET_TITLE_INFO:
323 case ACCESS_SET_TITLE:
324 case ACCESS_SET_SEEKPOINT:
325 case ACCESS_SET_PRIVATE_ID_STATE:
326 case ACCESS_GET_CONTENT_TYPE:
330 msg_Warn( p_access, "unimplemented query in control" );
337 static block_t *Block( access_t *p_access )
339 access_sys_t *p_sys = p_access->p_sys;
340 block_t *p_block = NULL;
343 if( !p_access->psz_demux )
345 free( p_access->psz_demux );
346 p_access->psz_demux = strdup( "rawdv" );
350 vlc_mutex_lock( &p_sys->lock );
351 p_block = p_sys->p_frame;
352 //msg_Dbg( p_access, "sending frame %p",p_block );
353 p_sys->p_frame = NULL;
354 vlc_mutex_unlock( &p_sys->lock );
359 static void* Raw1394EventThread( vlc_object_t *p_this )
361 event_thread_t *p_ev = (event_thread_t *) p_this;
362 access_t *p_access = (access_t *) p_ev->p_access;
363 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
365 int canc = vlc_savecancel ();
367 AVCPlay( p_access, p_sys->i_node );
369 while( vlc_object_alive (p_sys->p_ev) )
371 while( ( result = poll( &(p_sys->raw1394_poll), 1, 200 ) ) < 0 )
373 if( !( errno == EAGAIN || errno == EINTR ) )
375 perror( "error: raw1394 poll" );
376 msg_Err( p_access, "retrying device raw1394" );
379 if( !vlc_object_alive (p_sys->p_ev) )
381 if( result > 0 && ( ( p_sys->raw1394_poll.revents & POLLIN )
382 || ( p_sys->raw1394_poll.revents & POLLPRI ) ) )
383 result = raw1394_loop_iterate( p_sys->p_raw1394 );
386 AVCStop( p_access, p_sys->i_node );
387 vlc_restorecancel (canc);
391 static int Raw1394Handler( raw1394handle_t handle, int channel, size_t length, quadlet_t *data )
393 access_t *p_access = NULL;
394 access_sys_t *p_sys = NULL;
395 block_t *p_block = NULL;
397 p_access = (access_t *) raw1394_get_userdata( handle );
398 if( !p_access ) return 0;
400 p_sys = p_access->p_sys;
402 /* skip empty packets */
405 unsigned char * p = ( unsigned char* ) &data[ 3 ];
406 int section_type = p[ 0 ] >> 5; /* section type is in bits 5 - 7 */
407 int dif_sequence = p[ 1 ] >> 4; /* dif sequence number is in bits 4 - 7 */
408 int dif_block = p[ 2 ];
410 vlc_mutex_lock( &p_sys->p_ev->lock );
412 /* if we are at the beginning of a frame, we put the previous
413 frame in our output_queue. */
414 if( (section_type == 0) && (dif_sequence == 0) )
416 vlc_mutex_lock( &p_sys->lock );
417 if( p_sys->p_ev->p_frame )
419 /* Push current frame to p_access thread. */
420 //p_sys->p_ev->p_frame->i_pts = mdate();
421 block_ChainAppend( &p_sys->p_frame, p_sys->p_ev->p_frame );
424 p_sys->p_ev->p_frame = block_New( p_access, 144000 );
425 p_sys->p_ev->pp_last = &p_sys->p_frame;
426 vlc_mutex_unlock( &p_sys->lock );
429 p_block = p_sys->p_ev->p_frame;
432 switch ( section_type )
434 case 0: /* 1 Header block */
435 /* p[3] |= 0x80; // hack to force PAL data */
436 memcpy( p_block->p_buffer + dif_sequence * 150 * 80, p, 480 );
439 case 1: /* 2 Subcode blocks */
440 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 1 + dif_block ) * 80, p, 480 );
443 case 2: /* 3 VAUX blocks */
444 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 3 + dif_block ) * 80, p, 480 );
447 case 3: /* 9 Audio blocks interleaved with video */
448 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 6 + dif_block * 16 ) * 80, p, 480 );
451 case 4: /* 135 Video blocks interleaved with audio */
452 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 7 + ( dif_block / 15 ) + dif_block ) * 80, p, 480 );
455 default: /* we canĀ“t handle any other data */
456 block_Release( p_block );
461 vlc_mutex_unlock( &p_sys->p_ev->lock );
467 * Routing borrowed from dvgrab-1.8
468 * Copyright by Arne Schirmacher <dvgrab@schirmacher.de>
469 * Dan Dennedy <dan@dennedy.org> and others
471 static int Raw1394GetNumPorts( access_t *p_access )
474 struct raw1394_portinfo pinf[ 16 ];
475 raw1394handle_t handle;
477 /* get a raw1394 handle */
478 if ( !( handle = raw1394_new_handle() ) )
480 msg_Err( p_access, "raw1394 - failed to get handle: %m." );
484 if ( ( n_ports = raw1394_get_port_info( handle, pinf, 16 ) ) < 0 )
486 msg_Err( p_access, "raw1394 - failed to get port info: %m.\n" );
487 raw1394_destroy_handle( handle );
490 raw1394_destroy_handle( handle );
495 static raw1394handle_t Raw1394Open( access_t *p_access, int port )
498 struct raw1394_portinfo pinf[ 16 ];
499 raw1394handle_t handle;
501 /* get a raw1394 handle */
504 handle = raw1394_get_handle();
507 handle = raw1394_new_handle();
512 msg_Err( p_access, "raw1394 - failed to get handle: %m." );
516 if ( ( n_ports = raw1394_get_port_info( handle, pinf, 16 ) ) < 0 )
518 msg_Err( p_access, "raw1394 - failed to get port info: %m." );
519 raw1394_destroy_handle( handle );
523 /* tell raw1394 which host adapter to use */
524 if ( raw1394_set_port( handle, port ) < 0 )
526 msg_Err( p_access, "raw1394 - failed to set set port: %m." );
533 static void Raw1394Close( raw1394handle_t handle )
535 raw1394_destroy_handle( handle );
538 static int DiscoverAVC( access_t *p_access, int* port, uint64_t guid )
540 rom1394_directory rom_dir;
541 raw1394handle_t handle = NULL;
544 int m = Raw1394GetNumPorts( p_access );
548 /* search on explicit port */
553 for( ; j < m && device == -1; j++ )
555 handle = Raw1394Open( p_access, j );
559 for( i = 0; i < raw1394_get_nodecount( handle ); ++i )
563 /* select explicitly by GUID */
564 if( guid == rom1394_get_guid( handle, i ) )
573 /* select first AV/C Tape Reccorder Player node */
574 if( rom1394_get_directory( handle, i, &rom_dir ) < 0 )
576 msg_Err( p_access, "error reading config rom directory for node %d\n", i );
579 if( ( rom1394_get_node_type( &rom_dir ) == ROM1394_NODE_TYPE_AVC ) &&
580 avc1394_check_subunit_type( handle, i, AVC1394_SUBUNIT_TYPE_VCR ) )
588 Raw1394Close( handle );
595 * Handle AVC commands
597 static raw1394handle_t AVCOpen( access_t *p_access, int port )
599 access_sys_t *p_sys = p_access->p_sys;
601 struct raw1394_portinfo pinf[ 16 ];
603 p_sys->p_avc1394 = raw1394_new_handle();
604 if( !p_sys->p_avc1394 )
607 numcards = raw1394_get_port_info( p_sys->p_avc1394, pinf, 16 );
610 if( raw1394_set_port( p_sys->p_avc1394, port ) < 0 )
613 raw1394_set_bus_reset_handler( p_sys->p_avc1394, AVCResetHandler );
615 return p_sys->p_avc1394;
618 static void AVCClose( access_t *p_access )
620 access_sys_t *p_sys = p_access->p_sys;
622 if( p_sys->p_avc1394 )
624 raw1394_destroy_handle( p_sys->p_avc1394 );
625 p_sys->p_avc1394 = NULL;
630 static int AVCResetHandler( raw1394handle_t handle, unsigned int generation )
632 raw1394_update_generation( handle, generation );
636 static int AVCPlay( access_t *p_access, int phyID )
638 access_sys_t *p_sys = p_access->p_sys;
640 msg_Dbg( p_access, "send play command over Digital Video control channel" );
641 if( !p_sys->p_avc1394 )
646 if( !avc1394_vcr_is_recording( p_sys->p_avc1394, phyID ) &&
647 avc1394_vcr_is_playing( p_sys->p_avc1394, phyID ) != AVC1394_VCR_OPERAND_PLAY_FORWARD )
648 avc1394_vcr_play( p_sys->p_avc1394, phyID );
653 static int AVCPause( access_t *p_access, int phyID )
655 access_sys_t *p_sys = p_access->p_sys;
657 if( !p_sys->p_avc1394 )
662 if( !avc1394_vcr_is_recording( p_sys->p_avc1394, phyID ) &&
663 ( avc1394_vcr_is_playing( p_sys->p_avc1394, phyID ) != AVC1394_VCR_OPERAND_PLAY_FORWARD_PAUSE ) )
664 avc1394_vcr_pause( p_sys->p_avc1394, phyID );
670 static int AVCStop( access_t *p_access, int phyID )
672 access_sys_t *p_sys = p_access->p_sys;
674 msg_Dbg( p_access, "closing Digital Video control channel" );
675 if( !p_sys->p_avc1394 )
679 avc1394_vcr_stop( p_sys->p_avc1394, phyID );