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 *****************************************************************************/
28 #include <vlc_access.h>
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
34 #ifdef HAVE_SYS_TIME_H
35 # include <sys/time.h>
37 #ifdef HAVE_SYS_STAT_H
38 # include <sys/stat.h>
46 #elif defined( WIN32 ) && !defined( UNDER_CE )
52 #include <libraw1394/raw1394.h>
53 #include <libraw1394/csr.h>
54 #include <libavc1394/avc1394.h>
55 #include <libavc1394/avc1394_vcr.h>
56 #include <libavc1394/rom1394.h>
58 /*****************************************************************************
60 *****************************************************************************/
61 static int Open ( vlc_object_t * );
62 static void Close( vlc_object_t * );
63 static block_t *Block( access_t * );
64 static int Control( access_t *, int, va_list );
66 #define CACHING_TEXT N_("Caching value in ms")
67 #define CACHING_LONGTEXT N_( \
68 "Caching value for DV streams. This" \
69 "value should be set in milliseconds." )
72 set_description( _("Digital Video (Firewire/ieee1394) input") );
73 set_shortname( _("dv") );
74 set_category( CAT_INPUT );
75 set_subcategory( SUBCAT_INPUT_ACCESS );
76 add_integer( "dv-caching", 60000 / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
78 set_capability( "access2", 0 );
80 add_shortcut( "dv1394" );
81 add_shortcut( "raw1394" );
82 set_callbacks( Open, Close );
96 static int Raw1394EventThread( vlc_object_t * );
97 static int Raw1394Handler( raw1394handle_t, int, size_t, quadlet_t * );
99 static int Raw1394GetNumPorts( access_t *p_access );
100 static raw1394handle_t Raw1394Open( access_t *, int );
101 static void Raw1394Close( raw1394handle_t );
103 static int DiscoverAVC( access_t *, int *, uint64_t );
104 static raw1394handle_t AVCOpen( access_t *, int );
105 static void AVCClose( access_t * );
106 static int AVCResetHandler( raw1394handle_t, unsigned int );
107 static int AVCPlay( access_t *, int );
108 static int AVCPause( access_t *, int );
109 static int AVCStop( access_t *, int );
113 raw1394handle_t p_avc1394;
114 raw1394handle_t p_raw1394;
115 struct pollfd raw1394_poll;
124 event_thread_t *p_ev;
129 /*****************************************************************************
130 * Open: open the file
131 *****************************************************************************/
132 static int Open( vlc_object_t *p_this )
134 access_t *p_access = (access_t*)p_this;
136 char *psz_name = strdup( p_access->psz_path );
138 struct raw1394_portinfo port_inf[ 16 ];
139 iso_handler_t oldhandler;
141 msg_Dbg( p_access, "opening device %s", psz_name );
143 /* Set up p_access */
144 access_InitFields( p_access );
145 ACCESS_SET_CALLBACKS( NULL, Block, Control, NULL );
146 p_access->info.b_prebuffered = VLC_FALSE;
148 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
159 p_sys->i_channel = 63;
160 p_sys->p_raw1394 = NULL;
161 p_sys->p_avc1394 = NULL;
162 p_sys->p_frame = NULL;
165 vlc_mutex_init( p_access, &p_sys->lock );
167 p_sys->i_node = DiscoverAVC( p_access, &p_sys->i_port, p_sys->i_guid );
168 if( p_sys->i_node < 0 )
170 msg_Err( p_access, "failed to open a Firewire (IEEE1394) connection" );
176 p_sys->p_avc1394 = AVCOpen( p_access, p_sys->i_port );
177 if( !p_sys->p_avc1394 )
179 msg_Err( p_access, "no Digital Video Control device found on %s", psz_name );
185 p_sys->p_raw1394 = raw1394_new_handle();
186 if( !p_sys->p_raw1394 )
188 msg_Err( p_access, "no Digital Video device found on %s", psz_name );
194 p_sys->i_cards = raw1394_get_port_info( p_sys->p_raw1394, port_inf, 16 );
195 if( p_sys->i_cards < 0 )
197 msg_Err( p_access, "failed to get port info for %s", psz_name );
203 if( raw1394_set_port( p_sys->p_raw1394, p_sys->i_port ) < 0 )
205 msg_Err( p_access, "failed to set port info for %s", psz_name );
211 oldhandler = raw1394_set_iso_handler( p_sys->p_raw1394,
212 p_sys->i_channel, Raw1394Handler );
213 raw1394_set_userdata( p_sys->p_raw1394, p_access );
214 raw1394_start_iso_rcv( p_sys->p_raw1394, p_sys->i_channel );
216 p_sys->raw1394_poll.fd = raw1394_get_fd( p_sys->p_raw1394 );
217 p_sys->raw1394_poll.events = POLLIN | POLLERR | POLLHUP | POLLPRI;
219 /* Update default_pts to a suitable value for udp access */
220 var_Create( p_access, "dv-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
222 /* Now create our event thread catcher */
223 p_sys->p_ev = vlc_object_create( p_access, sizeof( event_thread_t ) );
226 msg_Err( p_access, "failed to create event thread for %s", psz_name );
232 p_sys->p_ev->p_frame = NULL;
233 p_sys->p_ev->pp_last = &p_sys->p_ev->p_frame;
234 p_sys->p_ev->p_access = p_access;
235 vlc_mutex_init( p_access, &p_sys->p_ev->lock );
236 vlc_thread_create( p_sys->p_ev, "dv event thread handler", Raw1394EventThread,
237 VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE );
243 /*****************************************************************************
244 * Close: free unused data structures
245 *****************************************************************************/
246 static void Close( vlc_object_t *p_this )
248 access_t *p_access = (access_t*)p_this;
249 access_sys_t *p_sys = p_access->p_sys;
253 /* stop the event handler */
254 vlc_object_kill( p_sys->p_ev );
256 if( p_sys->p_raw1394 )
257 raw1394_stop_iso_rcv( p_sys->p_raw1394, p_sys->i_channel );
259 vlc_mutex_destroy( &p_sys->p_ev->lock );
260 vlc_thread_join( p_sys->p_ev );
262 /* Cleanup frame data */
263 if( p_sys->p_ev->p_frame )
265 vlc_mutex_lock( &p_sys->p_ev->lock );
266 block_ChainRelease( p_sys->p_ev->p_frame );
267 p_sys->p_ev->p_frame = NULL;
268 p_sys->p_ev->pp_last = &p_sys->p_frame;
269 vlc_mutex_unlock( &p_sys->p_ev->lock );
271 vlc_object_destroy( p_sys->p_ev );
275 block_ChainRelease( p_sys->p_frame );
276 if( p_sys->p_raw1394 )
277 raw1394_destroy_handle( p_sys->p_raw1394 );
279 AVCClose( p_access );
281 vlc_mutex_destroy( &p_sys->lock );
285 /*****************************************************************************
287 *****************************************************************************/
288 static int Control( access_t *p_access, int i_query, va_list args )
290 access_sys_t *p_sys = p_access->p_sys;
297 case ACCESS_CAN_PAUSE:
298 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
302 case ACCESS_CAN_SEEK:
303 case ACCESS_CAN_FASTSEEK:
304 case ACCESS_CAN_CONTROL_PACE:
305 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
306 *pb_bool = VLC_FALSE;
309 case ACCESS_GET_PTS_DELAY:
310 pi_64 = (int64_t*)va_arg( args, int64_t * );
311 *pi_64 = var_GetInteger( p_access, "dv-caching" ) * 1000;
315 case ACCESS_SET_PAUSE_STATE:
316 AVCPause( p_access, p_sys->i_node );
319 case ACCESS_GET_TITLE_INFO:
320 case ACCESS_SET_TITLE:
321 case ACCESS_SET_SEEKPOINT:
322 case ACCESS_SET_PRIVATE_ID_STATE:
323 case ACCESS_GET_CONTENT_TYPE:
327 msg_Warn( p_access, "unimplemented query in control" );
334 static block_t *Block( access_t *p_access )
336 access_sys_t *p_sys = p_access->p_sys;
337 block_t *p_block = NULL;
339 // if( !p_access->psz_demux )
340 // p_access->psz_demux = strdup( "rawdv" );
342 vlc_mutex_lock( &p_sys->lock );
343 p_block = p_sys->p_frame;
344 //msg_Dbg( p_access, "sending frame %p",p_block );
345 p_sys->p_frame = NULL;
346 vlc_mutex_unlock( &p_sys->lock );
351 static int Raw1394EventThread( vlc_object_t *p_this )
353 event_thread_t *p_ev = (event_thread_t *) p_this;
354 access_t *p_access = (access_t *) p_ev->p_access;
355 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
358 AVCPlay( p_access, p_sys->i_node );
360 vlc_thread_ready( p_this );
362 while( !p_sys->p_ev->b_die )
364 while( ( result = poll( &(p_sys->raw1394_poll), 1, 200 ) ) < 0 )
366 if( !( errno == EAGAIN || errno == EINTR ) )
368 perror( "error: raw1394 poll" );
369 msg_Err( p_access, "retrying device raw1394" );
371 if( p_sys->p_ev->b_die )
374 if( p_sys->p_ev->b_die )
376 if( result > 0 && ( ( p_sys->raw1394_poll.revents & POLLIN )
377 || ( p_sys->raw1394_poll.revents & POLLPRI ) ) )
378 result = raw1394_loop_iterate( p_sys->p_raw1394 );
381 AVCStop( p_access, p_sys->i_node );
385 static int Raw1394Handler( raw1394handle_t handle, int channel, size_t length, quadlet_t *data )
387 access_t *p_access = NULL;
388 access_sys_t *p_sys = NULL;
389 block_t *p_block = NULL;
391 p_access = (access_t *) raw1394_get_userdata( handle );
392 if( !p_access ) return 0;
394 p_sys = p_access->p_sys;
396 /* skip empty packets */
399 unsigned char * p = ( unsigned char* ) &data[ 3 ];
400 int section_type = p[ 0 ] >> 5; /* section type is in bits 5 - 7 */
401 int dif_sequence = p[ 1 ] >> 4; /* dif sequence number is in bits 4 - 7 */
402 int dif_block = p[ 2 ];
404 vlc_mutex_lock( &p_sys->p_ev->lock );
406 /* if we are at the beginning of a frame, we put the previous
407 frame in our output_queue. */
408 if( (section_type == 0) && (dif_sequence == 0) )
410 vlc_mutex_lock( &p_sys->lock );
411 if( p_sys->p_ev->p_frame )
413 /* Push current frame to p_access thread. */
414 //p_sys->p_ev->p_frame->i_pts = mdate();
415 block_ChainAppend( &p_sys->p_frame, p_sys->p_ev->p_frame );
418 p_sys->p_ev->p_frame = block_New( p_access, 144000 );
419 p_sys->p_ev->pp_last = &p_sys->p_frame;
420 vlc_mutex_unlock( &p_sys->lock );
423 p_block = p_sys->p_ev->p_frame;
426 switch ( section_type )
428 case 0: /* 1 Header block */
429 /* p[3] |= 0x80; // hack to force PAL data */
430 memcpy( p_block->p_buffer + dif_sequence * 150 * 80, p, 480 );
433 case 1: /* 2 Subcode blocks */
434 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 1 + dif_block ) * 80, p, 480 );
437 case 2: /* 3 VAUX blocks */
438 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 3 + dif_block ) * 80, p, 480 );
441 case 3: /* 9 Audio blocks interleaved with video */
442 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 6 + dif_block * 16 ) * 80, p, 480 );
445 case 4: /* 135 Video blocks interleaved with audio */
446 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 7 + ( dif_block / 15 ) + dif_block ) * 80, p, 480 );
449 default: /* we canĀ“t handle any other data */
450 block_Release( p_block );
455 vlc_mutex_unlock( &p_sys->p_ev->lock );
461 * Routing borrowed from dvgrab-1.8
462 * Copyright by Arne Schirmacher <dvgrab@schirmacher.de>
463 * Dan Dennedy <dan@dennedy.org> and others
465 static int Raw1394GetNumPorts( access_t *p_access )
468 struct raw1394_portinfo pinf[ 16 ];
469 raw1394handle_t handle;
471 /* get a raw1394 handle */
472 if ( !( handle = raw1394_new_handle() ) )
474 msg_Err( p_access, "raw1394 - failed to get handle: %m." );
478 if ( ( n_ports = raw1394_get_port_info( handle, pinf, 16 ) ) < 0 )
480 msg_Err( p_access, "raw1394 - failed to get port info: %m.\n" );
481 raw1394_destroy_handle( handle );
484 raw1394_destroy_handle( handle );
489 static raw1394handle_t Raw1394Open( access_t *p_access, int port )
492 struct raw1394_portinfo pinf[ 16 ];
493 raw1394handle_t handle;
495 /* get a raw1394 handle */
498 handle = raw1394_get_handle();
501 handle = raw1394_new_handle();
506 msg_Err( p_access, "raw1394 - failed to get handle: %m." );
510 if ( ( n_ports = raw1394_get_port_info( handle, pinf, 16 ) ) < 0 )
512 msg_Err( p_access, "raw1394 - failed to get port info: %m." );
513 raw1394_destroy_handle( handle );
517 /* tell raw1394 which host adapter to use */
518 if ( raw1394_set_port( handle, port ) < 0 )
520 msg_Err( p_access, "raw1394 - failed to set set port: %m." );
527 static void Raw1394Close( raw1394handle_t handle )
529 raw1394_destroy_handle( handle );
532 static int DiscoverAVC( access_t *p_access, int* port, uint64_t guid )
534 rom1394_directory rom_dir;
535 raw1394handle_t handle = NULL;
538 int m = Raw1394GetNumPorts( p_access );
542 /* search on explicit port */
547 for( ; j < m && device == -1; j++ )
549 handle = Raw1394Open( p_access, j );
553 for( i = 0; i < raw1394_get_nodecount( handle ); ++i )
557 /* select explicitly by GUID */
558 if( guid == rom1394_get_guid( handle, i ) )
567 /* select first AV/C Tape Reccorder Player node */
568 if( rom1394_get_directory( handle, i, &rom_dir ) < 0 )
570 msg_Err( p_access, "error reading config rom directory for node %d\n", i );
573 if( ( rom1394_get_node_type( &rom_dir ) == ROM1394_NODE_TYPE_AVC ) &&
574 avc1394_check_subunit_type( handle, i, AVC1394_SUBUNIT_TYPE_VCR ) )
582 Raw1394Close( handle );
589 * Handle AVC commands
591 static raw1394handle_t AVCOpen( access_t *p_access, int port )
593 access_sys_t *p_sys = p_access->p_sys;
595 struct raw1394_portinfo pinf[ 16 ];
597 p_sys->p_avc1394 = raw1394_new_handle();
598 if( !p_sys->p_avc1394 )
601 numcards = raw1394_get_port_info( p_sys->p_avc1394, pinf, 16 );
604 if( raw1394_set_port( p_sys->p_avc1394, port ) < 0 )
607 raw1394_set_bus_reset_handler( p_sys->p_avc1394, AVCResetHandler );
609 return p_sys->p_avc1394;
612 static void AVCClose( access_t *p_access )
614 access_sys_t *p_sys = p_access->p_sys;
616 if( p_sys->p_avc1394 )
618 raw1394_destroy_handle( p_sys->p_avc1394 );
619 p_sys->p_avc1394 = NULL;
624 static int AVCResetHandler( raw1394handle_t handle, unsigned int generation )
626 raw1394_update_generation( handle, generation );
630 static int AVCPlay( access_t *p_access, int phyID )
632 access_sys_t *p_sys = p_access->p_sys;
634 msg_Dbg( p_access, "send play command over Digital Video control channel" );
635 if( !p_sys->p_avc1394 )
640 if( !avc1394_vcr_is_recording( p_sys->p_avc1394, phyID ) &&
641 avc1394_vcr_is_playing( p_sys->p_avc1394, phyID ) != AVC1394_VCR_OPERAND_PLAY_FORWARD )
642 avc1394_vcr_play( p_sys->p_avc1394, phyID );
647 static int AVCPause( access_t *p_access, int phyID )
649 access_sys_t *p_sys = p_access->p_sys;
651 if( !p_sys->p_avc1394 )
656 if( !avc1394_vcr_is_recording( p_sys->p_avc1394, phyID ) &&
657 ( avc1394_vcr_is_playing( p_sys->p_avc1394, phyID ) != AVC1394_VCR_OPERAND_PLAY_FORWARD_PAUSE ) )
658 avc1394_vcr_pause( p_sys->p_avc1394, phyID );
664 static int AVCStop( access_t *p_access, int phyID )
666 access_sys_t *p_sys = p_access->p_sys;
668 msg_Dbg( p_access, "closing Digital Video control channel" );
669 if( !p_sys->p_avc1394 )
673 avc1394_vcr_stop( p_sys->p_avc1394, phyID );