[demux/avi] Enable dirac support (Set PTS on first output block)
[vlc/davidf-public.git] / modules / access / dv.c
bloba0b004b127336b1fe17d9301296f5cdd87efc057
1 /*****************************************************************************
2 * dv.c: Digital video/Firewire input (file: access plug-in)
3 *****************************************************************************
4 * Copyright (C) 2005 M2X
5 * $Id$
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 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_access.h>
35 #include <errno.h>
36 #ifdef HAVE_SYS_TYPES_H
37 # include <sys/types.h>
38 #endif
39 #ifdef HAVE_SYS_TIME_H
40 # include <sys/time.h>
41 #endif
42 #ifdef HAVE_SYS_STAT_H
43 # include <sys/stat.h>
44 #endif
45 #ifdef HAVE_FCNTL_H
46 # include <fcntl.h>
47 #endif
49 #ifdef HAVE_UNISTD_H
50 # include <unistd.h>
51 #elif defined( WIN32 ) && !defined( UNDER_CE )
52 # include <io.h>
53 #endif
55 #include <sys/poll.h>
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 /*****************************************************************************
64 * Module descriptor
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." )
76 vlc_module_begin();
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 );
83 add_shortcut( "dv" );
84 add_shortcut( "dv1394" );
85 add_shortcut( "raw1394" );
86 set_callbacks( Open, Close );
87 vlc_module_end();
89 typedef struct
91 VLC_COMMON_MEMBERS
93 access_t *p_access;
94 vlc_mutex_t lock;
95 block_t *p_frame;
96 block_t **pp_last;
98 } event_thread_t;
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 );
115 struct access_sys_t
117 raw1394handle_t p_avc1394;
118 raw1394handle_t p_raw1394;
119 struct pollfd raw1394_poll;
121 int i_cards;
122 int i_node;
123 int i_port;
124 int i_channel;
125 uint64_t i_guid;
127 /* event */
128 event_thread_t *p_ev;
129 vlc_mutex_t lock;
130 block_t *p_frame;
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;
139 access_sys_t *p_sys;
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 );
150 p_access->info.b_prebuffered = false;
152 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
153 if( !p_sys )
155 free( psz_name );
156 return VLC_EGENERIC;
159 p_sys->i_cards = 0;
160 p_sys->i_node = 0;
161 p_sys->i_port = 0;
162 p_sys->i_guid = 0;
163 p_sys->i_channel = 63;
164 p_sys->p_raw1394 = NULL;
165 p_sys->p_avc1394 = NULL;
166 p_sys->p_frame = NULL;
167 p_sys->p_ev = NULL;
169 vlc_mutex_init( &p_sys->lock );
171 p_sys->i_node = DiscoverAVC( p_access, &p_sys->i_port, p_sys->i_guid );
172 if( p_sys->i_node < 0 )
174 msg_Err( p_access, "failed to open a Firewire (IEEE1394) connection" );
175 Close( p_this );
176 free( psz_name );
177 return VLC_EGENERIC;
180 p_sys->p_avc1394 = AVCOpen( p_access, p_sys->i_port );
181 if( !p_sys->p_avc1394 )
183 msg_Err( p_access, "no Digital Video Control device found on %s", psz_name );
184 Close( p_this );
185 free( psz_name );
186 return VLC_EGENERIC;
189 p_sys->p_raw1394 = raw1394_new_handle();
190 if( !p_sys->p_raw1394 )
192 msg_Err( p_access, "no Digital Video device found on %s", psz_name );
193 Close( p_this );
194 free( psz_name );
195 return VLC_EGENERIC;
198 p_sys->i_cards = raw1394_get_port_info( p_sys->p_raw1394, port_inf, 16 );
199 if( p_sys->i_cards < 0 )
201 msg_Err( p_access, "failed to get port info for %s", psz_name );
202 Close( p_this );
203 free( psz_name );
204 return VLC_EGENERIC;
207 if( raw1394_set_port( p_sys->p_raw1394, p_sys->i_port ) < 0 )
209 msg_Err( p_access, "failed to set port info for %s", psz_name );
210 Close( p_this );
211 free( psz_name );
212 return VLC_EGENERIC;
215 oldhandler = raw1394_set_iso_handler( p_sys->p_raw1394,
216 p_sys->i_channel, Raw1394Handler );
217 raw1394_set_userdata( p_sys->p_raw1394, p_access );
218 raw1394_start_iso_rcv( p_sys->p_raw1394, p_sys->i_channel );
220 p_sys->raw1394_poll.fd = raw1394_get_fd( p_sys->p_raw1394 );
221 p_sys->raw1394_poll.events = POLLIN | POLLPRI;
223 /* Update default_pts to a suitable value for udp access */
224 var_Create( p_access, "dv-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
226 /* Now create our event thread catcher */
227 p_sys->p_ev = vlc_object_create( p_access, sizeof( event_thread_t ) );
228 if( !p_sys->p_ev )
230 msg_Err( p_access, "failed to create event thread for %s", psz_name );
231 Close( p_this );
232 free( psz_name );
233 return VLC_EGENERIC;
236 p_sys->p_ev->p_frame = NULL;
237 p_sys->p_ev->pp_last = &p_sys->p_ev->p_frame;
238 p_sys->p_ev->p_access = p_access;
239 vlc_mutex_init( &p_sys->p_ev->lock );
240 vlc_thread_create( p_sys->p_ev, "dv event thread handler", Raw1394EventThread,
241 VLC_THREAD_PRIORITY_OUTPUT, false );
243 free( psz_name );
244 return VLC_SUCCESS;
247 /*****************************************************************************
248 * Close: free unused data structures
249 *****************************************************************************/
250 static void Close( vlc_object_t *p_this )
252 access_t *p_access = (access_t*)p_this;
253 access_sys_t *p_sys = p_access->p_sys;
255 if( p_sys->p_ev )
257 /* stop the event handler */
258 vlc_object_kill( p_sys->p_ev );
260 if( p_sys->p_raw1394 )
261 raw1394_stop_iso_rcv( p_sys->p_raw1394, p_sys->i_channel );
263 vlc_mutex_destroy( &p_sys->p_ev->lock );
264 vlc_thread_join( p_sys->p_ev );
266 /* Cleanup frame data */
267 if( p_sys->p_ev->p_frame )
269 vlc_mutex_lock( &p_sys->p_ev->lock );
270 block_ChainRelease( p_sys->p_ev->p_frame );
271 p_sys->p_ev->p_frame = NULL;
272 p_sys->p_ev->pp_last = &p_sys->p_frame;
273 vlc_mutex_unlock( &p_sys->p_ev->lock );
275 vlc_object_release( p_sys->p_ev );
278 if( p_sys->p_frame )
279 block_ChainRelease( p_sys->p_frame );
280 if( p_sys->p_raw1394 )
281 raw1394_destroy_handle( p_sys->p_raw1394 );
283 AVCClose( p_access );
285 vlc_mutex_destroy( &p_sys->lock );
286 free( p_sys );
289 /*****************************************************************************
290 * Control:
291 *****************************************************************************/
292 static int Control( access_t *p_access, int i_query, va_list args )
294 access_sys_t *p_sys = p_access->p_sys;
295 bool *pb_bool;
296 int64_t *pi_64;
298 switch( i_query )
300 /* */
301 case ACCESS_CAN_PAUSE:
302 pb_bool = (bool*)va_arg( args, bool* );
303 *pb_bool = true;
304 break;
306 case ACCESS_CAN_SEEK:
307 case ACCESS_CAN_FASTSEEK:
308 case ACCESS_CAN_CONTROL_PACE:
309 pb_bool = (bool*)va_arg( args, bool* );
310 *pb_bool = false;
311 break;
313 case ACCESS_GET_PTS_DELAY:
314 pi_64 = (int64_t*)va_arg( args, int64_t * );
315 *pi_64 = var_GetInteger( p_access, "dv-caching" ) * 1000;
316 break;
318 /* */
319 case ACCESS_SET_PAUSE_STATE:
320 AVCPause( p_access, p_sys->i_node );
321 break;
323 case ACCESS_GET_TITLE_INFO:
324 case ACCESS_SET_TITLE:
325 case ACCESS_SET_SEEKPOINT:
326 case ACCESS_SET_PRIVATE_ID_STATE:
327 case ACCESS_GET_CONTENT_TYPE:
328 return VLC_EGENERIC;
330 default:
331 msg_Warn( p_access, "unimplemented query in control" );
332 return VLC_EGENERIC;
335 return VLC_SUCCESS;
338 static block_t *Block( access_t *p_access )
340 access_sys_t *p_sys = p_access->p_sys;
341 block_t *p_block = NULL;
343 #if 0
344 if( !p_access->psz_demux )
346 free( p_access->psz_demux );
347 p_access->psz_demux = strdup( "rawdv" );
349 #endif
351 vlc_mutex_lock( &p_sys->lock );
352 p_block = p_sys->p_frame;
353 //msg_Dbg( p_access, "sending frame %p",p_block );
354 p_sys->p_frame = NULL;
355 vlc_mutex_unlock( &p_sys->lock );
357 return p_block;
360 static void* Raw1394EventThread( vlc_object_t *p_this )
362 event_thread_t *p_ev = (event_thread_t *) p_this;
363 access_t *p_access = (access_t *) p_ev->p_access;
364 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
365 int result = 0;
366 int canc = vlc_savecancel ();
368 AVCPlay( p_access, p_sys->i_node );
370 while( vlc_object_alive (p_sys->p_ev) )
372 while( ( result = poll( &(p_sys->raw1394_poll), 1, 200 ) ) < 0 )
374 if( !( errno == EAGAIN || errno == EINTR ) )
376 perror( "error: raw1394 poll" );
377 msg_Err( p_access, "retrying device raw1394" );
380 if( !vlc_object_alive (p_sys->p_ev) )
381 break;
382 if( result > 0 && ( ( p_sys->raw1394_poll.revents & POLLIN )
383 || ( p_sys->raw1394_poll.revents & POLLPRI ) ) )
384 result = raw1394_loop_iterate( p_sys->p_raw1394 );
387 AVCStop( p_access, p_sys->i_node );
388 vlc_restorecancel (canc);
389 return NULL;
392 static int Raw1394Handler( raw1394handle_t handle, int channel, size_t length, quadlet_t *data )
394 access_t *p_access = NULL;
395 access_sys_t *p_sys = NULL;
396 block_t *p_block = NULL;
398 p_access = (access_t *) raw1394_get_userdata( handle );
399 if( !p_access ) return 0;
401 p_sys = p_access->p_sys;
403 /* skip empty packets */
404 if ( length > 16 )
406 unsigned char * p = ( unsigned char* ) &data[ 3 ];
407 int section_type = p[ 0 ] >> 5; /* section type is in bits 5 - 7 */
408 int dif_sequence = p[ 1 ] >> 4; /* dif sequence number is in bits 4 - 7 */
409 int dif_block = p[ 2 ];
411 vlc_mutex_lock( &p_sys->p_ev->lock );
413 /* if we are at the beginning of a frame, we put the previous
414 frame in our output_queue. */
415 if( (section_type == 0) && (dif_sequence == 0) )
417 vlc_mutex_lock( &p_sys->lock );
418 if( p_sys->p_ev->p_frame )
420 /* Push current frame to p_access thread. */
421 //p_sys->p_ev->p_frame->i_pts = mdate();
422 block_ChainAppend( &p_sys->p_frame, p_sys->p_ev->p_frame );
424 /* reset list */
425 p_sys->p_ev->p_frame = block_New( p_access, 144000 );
426 p_sys->p_ev->pp_last = &p_sys->p_frame;
427 vlc_mutex_unlock( &p_sys->lock );
430 p_block = p_sys->p_ev->p_frame;
431 if( p_block )
433 switch ( section_type )
435 case 0: /* 1 Header block */
436 /* p[3] |= 0x80; // hack to force PAL data */
437 memcpy( p_block->p_buffer + dif_sequence * 150 * 80, p, 480 );
438 break;
440 case 1: /* 2 Subcode blocks */
441 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 1 + dif_block ) * 80, p, 480 );
442 break;
444 case 2: /* 3 VAUX blocks */
445 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 3 + dif_block ) * 80, p, 480 );
446 break;
448 case 3: /* 9 Audio blocks interleaved with video */
449 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 6 + dif_block * 16 ) * 80, p, 480 );
450 break;
452 case 4: /* 135 Video blocks interleaved with audio */
453 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 7 + ( dif_block / 15 ) + dif_block ) * 80, p, 480 );
454 break;
456 default: /* we canĀ“t handle any other data */
457 block_Release( p_block );
458 p_block = NULL;
459 break;
462 vlc_mutex_unlock( &p_sys->p_ev->lock );
464 return 0;
468 * Routing borrowed from dvgrab-1.8
469 * Copyright by Arne Schirmacher <dvgrab@schirmacher.de>
470 * Dan Dennedy <dan@dennedy.org> and others
472 static int Raw1394GetNumPorts( access_t *p_access )
474 int n_ports;
475 struct raw1394_portinfo pinf[ 16 ];
476 raw1394handle_t handle;
478 /* get a raw1394 handle */
479 if ( !( handle = raw1394_new_handle() ) )
481 msg_Err( p_access, "raw1394 - failed to get handle: %m." );
482 return VLC_EGENERIC;
485 if ( ( n_ports = raw1394_get_port_info( handle, pinf, 16 ) ) < 0 )
487 msg_Err( p_access, "raw1394 - failed to get port info: %m.\n" );
488 raw1394_destroy_handle( handle );
489 return VLC_EGENERIC;
491 raw1394_destroy_handle( handle );
493 return n_ports;
496 static raw1394handle_t Raw1394Open( access_t *p_access, int port )
498 int n_ports;
499 struct raw1394_portinfo pinf[ 16 ];
500 raw1394handle_t handle;
502 /* get a raw1394 handle */
503 #ifdef RAW1394_V_0_8
505 handle = raw1394_get_handle();
506 #else
508 handle = raw1394_new_handle();
509 #endif
511 if ( !handle )
513 msg_Err( p_access, "raw1394 - failed to get handle: %m." );
514 return NULL;
517 if ( ( n_ports = raw1394_get_port_info( handle, pinf, 16 ) ) < 0 )
519 msg_Err( p_access, "raw1394 - failed to get port info: %m." );
520 raw1394_destroy_handle( handle );
521 return NULL;
524 /* tell raw1394 which host adapter to use */
525 if ( raw1394_set_port( handle, port ) < 0 )
527 msg_Err( p_access, "raw1394 - failed to set set port: %m." );
528 return NULL;
531 return handle;
534 static void Raw1394Close( raw1394handle_t handle )
536 raw1394_destroy_handle( handle );
539 static int DiscoverAVC( access_t *p_access, int* port, uint64_t guid )
541 rom1394_directory rom_dir;
542 raw1394handle_t handle = NULL;
543 int device = -1;
544 int i, j = 0;
545 int m = Raw1394GetNumPorts( p_access );
547 if( *port >= 0 )
549 /* search on explicit port */
550 j = *port;
551 m = *port + 1;
554 for( ; j < m && device == -1; j++ )
556 handle = Raw1394Open( p_access, j );
557 if( !handle )
558 return -1;
560 for( i = 0; i < raw1394_get_nodecount( handle ); ++i )
562 if( guid != 0 )
564 /* select explicitly by GUID */
565 if( guid == rom1394_get_guid( handle, i ) )
567 device = i;
568 *port = j;
569 break;
572 else
574 /* select first AV/C Tape Reccorder Player node */
575 if( rom1394_get_directory( handle, i, &rom_dir ) < 0 )
577 msg_Err( p_access, "error reading config rom directory for node %d\n", i );
578 continue;
580 if( ( rom1394_get_node_type( &rom_dir ) == ROM1394_NODE_TYPE_AVC ) &&
581 avc1394_check_subunit_type( handle, i, AVC1394_SUBUNIT_TYPE_VCR ) )
583 device = i;
584 *port = j;
585 break;
589 Raw1394Close( handle );
592 return device;
596 * Handle AVC commands
598 static raw1394handle_t AVCOpen( access_t *p_access, int port )
600 access_sys_t *p_sys = p_access->p_sys;
601 int numcards;
602 struct raw1394_portinfo pinf[ 16 ];
604 p_sys->p_avc1394 = raw1394_new_handle();
605 if( !p_sys->p_avc1394 )
606 return NULL;
608 numcards = raw1394_get_port_info( p_sys->p_avc1394, pinf, 16 );
609 if( numcards < -1 )
610 return NULL;
611 if( raw1394_set_port( p_sys->p_avc1394, port ) < 0 )
612 return NULL;
614 raw1394_set_bus_reset_handler( p_sys->p_avc1394, AVCResetHandler );
616 return p_sys->p_avc1394;
619 static void AVCClose( access_t *p_access )
621 access_sys_t *p_sys = p_access->p_sys;
623 if( p_sys->p_avc1394 )
625 raw1394_destroy_handle( p_sys->p_avc1394 );
626 p_sys->p_avc1394 = NULL;
631 static int AVCResetHandler( raw1394handle_t handle, unsigned int generation )
633 raw1394_update_generation( handle, generation );
634 return 0;
637 static int AVCPlay( access_t *p_access, int phyID )
639 access_sys_t *p_sys = p_access->p_sys;
641 msg_Dbg( p_access, "send play command over Digital Video control channel" );
642 if( !p_sys->p_avc1394 )
643 return 0;
645 if( phyID >= 0 )
647 if( !avc1394_vcr_is_recording( p_sys->p_avc1394, phyID ) &&
648 avc1394_vcr_is_playing( p_sys->p_avc1394, phyID ) != AVC1394_VCR_OPERAND_PLAY_FORWARD )
649 avc1394_vcr_play( p_sys->p_avc1394, phyID );
651 return 0;
654 static int AVCPause( access_t *p_access, int phyID )
656 access_sys_t *p_sys = p_access->p_sys;
658 if( !p_sys->p_avc1394 )
659 return 0;
661 if( phyID >= 0 )
663 if( !avc1394_vcr_is_recording( p_sys->p_avc1394, phyID ) &&
664 ( avc1394_vcr_is_playing( p_sys->p_avc1394, phyID ) != AVC1394_VCR_OPERAND_PLAY_FORWARD_PAUSE ) )
665 avc1394_vcr_pause( p_sys->p_avc1394, phyID );
667 return 0;
671 static int AVCStop( access_t *p_access, int phyID )
673 access_sys_t *p_sys = p_access->p_sys;
675 msg_Dbg( p_access, "closing Digital Video control channel" );
676 if( !p_sys->p_avc1394 )
677 return 0;
679 if ( phyID >= 0 )
680 avc1394_vcr_stop( p_sys->p_avc1394, phyID );
682 return 0;