A little more detail regarding using my github copies of the code with where it's...
[vlc/adversarial.git] / modules / access / dv.c
blobf7792b880561b80bc26d30a00983df1f92733b38
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 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 /*****************************************************************************
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 #include <sys/types.h>
37 #include <unistd.h>
38 #include <sys/poll.h>
40 #include <libraw1394/raw1394.h>
41 #include <libraw1394/csr.h>
42 #include <libavc1394/avc1394.h>
43 #include <libavc1394/avc1394_vcr.h>
44 #include <libavc1394/rom1394.h>
46 /*****************************************************************************
47 * Module descriptor
48 *****************************************************************************/
49 static int Open ( vlc_object_t * );
50 static void Close( vlc_object_t * );
51 static block_t *Block( access_t * );
52 static int Control( access_t *, int, va_list );
54 vlc_module_begin ()
55 set_description( N_("Digital Video (Firewire/ieee1394) input") )
56 set_shortname( N_("DV") )
57 set_category( CAT_INPUT )
58 set_subcategory( SUBCAT_INPUT_ACCESS )
59 set_capability( "access", 0 )
60 add_shortcut( "dv", "raw1394" )
61 set_callbacks( Open, Close )
62 vlc_module_end ()
64 typedef struct
66 vlc_thread_t thread;
67 access_t *p_access;
68 vlc_mutex_t lock;
69 block_t *p_frame;
70 block_t **pp_last;
72 } event_thread_t;
74 static void* Raw1394EventThread( void * );
75 static enum raw1394_iso_disposition
76 Raw1394Handler(raw1394handle_t, unsigned char *,
77 unsigned int, unsigned char,
78 unsigned char, unsigned char, unsigned int,
79 unsigned int);
81 static int Raw1394GetNumPorts( access_t *p_access );
82 static raw1394handle_t Raw1394Open( access_t *, int );
83 static void Raw1394Close( raw1394handle_t );
85 static int DiscoverAVC( access_t *, int *, uint64_t );
86 static raw1394handle_t AVCOpen( access_t *, int );
87 static void AVCClose( access_t * );
88 static int AVCResetHandler( raw1394handle_t, unsigned int );
89 static int AVCPlay( access_t *, int );
90 static int AVCPause( access_t *, int );
91 static int AVCStop( access_t *, int );
93 struct access_sys_t
95 raw1394handle_t p_avc1394;
96 raw1394handle_t p_raw1394;
97 struct pollfd raw1394_poll;
99 int i_cards;
100 int i_node;
101 int i_port;
102 int i_channel;
103 uint64_t i_guid;
105 /* event */
106 event_thread_t *p_ev;
107 vlc_mutex_t lock;
108 block_t *p_frame;
111 #define ISOCHRONOUS_QUEUE_LENGTH 1000
112 #define ISOCHRONOUS_MAX_PACKET_SIZE 4096
114 /*****************************************************************************
115 * Open: open the file
116 *****************************************************************************/
117 static int Open( vlc_object_t *p_this )
119 access_t *p_access = (access_t*)p_this;
120 access_sys_t *p_sys;
122 struct raw1394_portinfo port_inf[ 16 ];
124 msg_Dbg( p_access, "opening device" );
126 /* Set up p_access */
127 access_InitFields( p_access );
128 ACCESS_SET_CALLBACKS( NULL, Block, Control, NULL );
130 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
131 if( !p_sys )
132 return VLC_EGENERIC;
134 p_sys->i_cards = 0;
135 p_sys->i_node = 0;
136 p_sys->i_port = 0;
137 p_sys->i_guid = 0;
138 p_sys->i_channel = 63;
139 p_sys->p_raw1394 = NULL;
140 p_sys->p_avc1394 = NULL;
141 p_sys->p_frame = NULL;
142 p_sys->p_ev = NULL;
144 vlc_mutex_init( &p_sys->lock );
146 p_sys->i_node = DiscoverAVC( p_access, &p_sys->i_port, p_sys->i_guid );
147 if( p_sys->i_node < 0 )
149 msg_Err( p_access, "failed to open a Firewire (IEEE1394) connection" );
150 Close( p_this );
151 return VLC_EGENERIC;
154 p_sys->p_avc1394 = AVCOpen( p_access, p_sys->i_port );
155 if( !p_sys->p_avc1394 )
157 msg_Err( p_access, "no Digital Video Control device found" );
158 Close( p_this );
159 return VLC_EGENERIC;
162 p_sys->p_raw1394 = raw1394_new_handle();
163 if( !p_sys->p_raw1394 )
165 msg_Err( p_access, "no Digital Video device found" );
166 Close( p_this );
167 return VLC_EGENERIC;
170 p_sys->i_cards = raw1394_get_port_info( p_sys->p_raw1394, port_inf, 16 );
171 if( p_sys->i_cards < 0 )
173 msg_Err( p_access, "failed to get port info" );
174 Close( p_this );
175 return VLC_EGENERIC;
178 if( raw1394_set_port( p_sys->p_raw1394, p_sys->i_port ) < 0 )
180 msg_Err( p_access, "failed to set port info" );
181 Close( p_this );
182 return VLC_EGENERIC;
185 if ( raw1394_iso_recv_init( p_sys->p_raw1394, Raw1394Handler,
186 ISOCHRONOUS_QUEUE_LENGTH, ISOCHRONOUS_MAX_PACKET_SIZE,
187 p_sys->i_channel, RAW1394_DMA_PACKET_PER_BUFFER, -1 ) < 0 )
189 msg_Err( p_access, "failed to init isochronous recv" );
190 Close( p_this );
191 return VLC_EGENERIC;
194 raw1394_set_userdata( p_sys->p_raw1394, p_access );
195 raw1394_iso_recv_start( p_sys->p_raw1394, -1, -1, 0 );
197 p_sys->raw1394_poll.fd = raw1394_get_fd( p_sys->p_raw1394 );
198 p_sys->raw1394_poll.events = POLLIN | POLLPRI;
200 /* Now create our event thread catcher */
201 p_sys->p_ev = calloc( 1, sizeof( *p_sys->p_ev ) );
202 if( !p_sys->p_ev )
204 msg_Err( p_access, "failed to create event thread struct" );
205 Close( p_this );
206 return VLC_ENOMEM;
209 p_sys->p_ev->p_frame = NULL;
210 p_sys->p_ev->pp_last = &p_sys->p_ev->p_frame;
211 p_sys->p_ev->p_access = p_access;
212 vlc_mutex_init( &p_sys->p_ev->lock );
213 if( vlc_clone( &p_sys->p_ev->thread, Raw1394EventThread,
214 p_sys->p_ev, VLC_THREAD_PRIORITY_OUTPUT ) )
216 msg_Err( p_access, "failed to clone event thread" );
217 Close( p_this );
218 return VLC_EGENERIC;
221 return VLC_SUCCESS;
224 /*****************************************************************************
225 * Close: free unused data structures
226 *****************************************************************************/
227 static void Close( vlc_object_t *p_this )
229 access_t *p_access = (access_t*)p_this;
230 access_sys_t *p_sys = p_access->p_sys;
232 if( p_sys->p_ev )
234 /* stop the event handler */
235 vlc_cancel( p_sys->p_ev->thread );
237 if( p_sys->p_raw1394 )
238 raw1394_iso_shutdown( p_sys->p_raw1394 );
240 vlc_join( p_sys->p_ev->thread, NULL );
241 vlc_mutex_destroy( &p_sys->p_ev->lock );
243 /* Cleanup frame data */
244 if( p_sys->p_ev->p_frame )
246 block_ChainRelease( p_sys->p_ev->p_frame );
247 p_sys->p_ev->p_frame = NULL;
248 p_sys->p_ev->pp_last = &p_sys->p_frame;
250 free( p_sys->p_ev );
253 if( p_sys->p_frame )
254 block_ChainRelease( p_sys->p_frame );
255 if( p_sys->p_raw1394 )
256 raw1394_destroy_handle( p_sys->p_raw1394 );
258 AVCClose( p_access );
260 vlc_mutex_destroy( &p_sys->lock );
261 free( p_sys );
264 /*****************************************************************************
265 * Control:
266 *****************************************************************************/
267 static int Control( access_t *p_access, int i_query, va_list args )
269 switch( i_query )
271 /* */
272 case ACCESS_CAN_PAUSE:
273 *va_arg( args, bool* ) = true;
274 break;
276 case ACCESS_CAN_SEEK:
277 case ACCESS_CAN_FASTSEEK:
278 case ACCESS_CAN_CONTROL_PACE:
279 *va_arg( args, bool* ) = false;
280 break;
282 case ACCESS_GET_PTS_DELAY:
283 *va_arg( args, int64_t * ) =
284 INT64_C(1000) * var_InheritInteger( p_access, "live-caching" );
285 break;
287 /* */
288 case ACCESS_SET_PAUSE_STATE:
289 AVCPause( p_access, p_access->p_sys->i_node );
290 break;
292 default:
293 return VLC_EGENERIC;
296 return VLC_SUCCESS;
299 static block_t *Block( access_t *p_access )
301 access_sys_t *p_sys = p_access->p_sys;
302 block_t *p_block = NULL;
304 vlc_mutex_lock( &p_sys->lock );
305 p_block = p_sys->p_frame;
306 //msg_Dbg( p_access, "sending frame %p",p_block );
307 p_sys->p_frame = NULL;
308 vlc_mutex_unlock( &p_sys->lock );
310 return p_block;
313 static void Raw1394EventThreadCleanup( void *obj )
315 event_thread_t *p_ev = (event_thread_t *)obj;
317 AVCStop( p_ev->p_access, p_ev->p_access->p_sys->i_node );
320 static void* Raw1394EventThread( void *obj )
322 event_thread_t *p_ev = (event_thread_t *)obj;
323 access_t *p_access = (access_t *) p_ev->p_access;
324 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
325 int result = 0;
326 int canc = vlc_savecancel();
328 AVCPlay( p_access, p_sys->i_node );
329 vlc_cleanup_push( Raw1394EventThreadCleanup, p_ev );
330 vlc_restorecancel( canc );
332 for( ;; )
334 while( ( result = poll( &p_sys->raw1394_poll, 1, -1 ) ) < 0 )
336 if( errno != EINTR )
337 msg_Err( p_access, "poll error: %s", vlc_strerror_c(errno) );
340 if( result > 0 && ( ( p_sys->raw1394_poll.revents & POLLIN )
341 || ( p_sys->raw1394_poll.revents & POLLPRI ) ) )
343 canc = vlc_savecancel();
344 result = raw1394_loop_iterate( p_sys->p_raw1394 );
345 vlc_restorecancel( canc );
349 vlc_cleanup_run();
350 return NULL;
353 static enum raw1394_iso_disposition
354 Raw1394Handler(raw1394handle_t handle, unsigned char *data,
355 unsigned int length, unsigned char channel,
356 unsigned char tag, unsigned char sy, unsigned int cycle,
357 unsigned int dropped)
359 access_t *p_access = NULL;
360 access_sys_t *p_sys = NULL;
361 block_t *p_block = NULL;
362 VLC_UNUSED(channel); VLC_UNUSED(tag);
363 VLC_UNUSED(sy); VLC_UNUSED(cycle); VLC_UNUSED(dropped);
365 p_access = (access_t *) raw1394_get_userdata( handle );
366 if( !p_access ) return 0;
368 p_sys = p_access->p_sys;
370 /* skip empty packets */
371 if( length > 16 )
373 unsigned char * p = data + 8;
374 int section_type = p[ 0 ] >> 5; /* section type is in bits 5 - 7 */
375 int dif_sequence = p[ 1 ] >> 4; /* dif sequence number is in bits 4 - 7 */
376 int dif_block = p[ 2 ];
378 vlc_mutex_lock( &p_sys->p_ev->lock );
380 /* if we are at the beginning of a frame, we put the previous
381 frame in our output_queue. */
382 if( (section_type == 0) && (dif_sequence == 0) )
384 vlc_mutex_lock( &p_sys->lock );
385 if( p_sys->p_ev->p_frame )
387 /* Push current frame to p_access thread. */
388 //p_sys->p_ev->p_frame->i_pts = mdate();
389 block_ChainAppend( &p_sys->p_frame, p_sys->p_ev->p_frame );
391 /* reset list */
392 p_sys->p_ev->p_frame = block_Alloc( 144000 );
393 p_sys->p_ev->pp_last = &p_sys->p_frame;
394 vlc_mutex_unlock( &p_sys->lock );
397 p_block = p_sys->p_ev->p_frame;
398 if( p_block )
400 switch ( section_type )
402 case 0: /* 1 Header block */
403 /* p[3] |= 0x80; // hack to force PAL data */
404 memcpy( p_block->p_buffer + dif_sequence * 150 * 80, p, 480 );
405 break;
407 case 1: /* 2 Subcode blocks */
408 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 1 + dif_block ) * 80, p, 480 );
409 break;
411 case 2: /* 3 VAUX blocks */
412 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 3 + dif_block ) * 80, p, 480 );
413 break;
415 case 3: /* 9 Audio blocks interleaved with video */
416 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 6 + dif_block * 16 ) * 80, p, 480 );
417 break;
419 case 4: /* 135 Video blocks interleaved with audio */
420 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 7 + ( dif_block / 15 ) + dif_block ) * 80, p, 480 );
421 break;
423 default: /* we canĀ“t handle any other data */
424 block_Release( p_block );
425 p_block = NULL;
426 break;
430 vlc_mutex_unlock( &p_sys->p_ev->lock );
432 return 0;
436 * Routing borrowed from dvgrab-1.8
437 * Copyright by Arne Schirmacher <dvgrab@schirmacher.de>
438 * Dan Dennedy <dan@dennedy.org> and others
440 static int Raw1394GetNumPorts( access_t *p_access )
442 int n_ports;
443 struct raw1394_portinfo pinf[ 16 ];
444 raw1394handle_t handle;
446 /* get a raw1394 handle */
447 if( !( handle = raw1394_new_handle() ) )
449 msg_Err( p_access, "raw1394 - failed to get handle: %s",
450 vlc_strerror_c(errno) );
451 return VLC_EGENERIC;
454 if( ( n_ports = raw1394_get_port_info( handle, pinf, 16 ) ) < 0 )
456 msg_Err( p_access, "raw1394 - failed to get port info: %s",
457 vlc_strerror_c(errno) );
458 raw1394_destroy_handle( handle );
459 return VLC_EGENERIC;
461 raw1394_destroy_handle( handle );
463 return n_ports;
466 static raw1394handle_t Raw1394Open( access_t *p_access, int port )
468 int n_ports;
469 struct raw1394_portinfo pinf[ 16 ];
470 raw1394handle_t handle;
472 /* get a raw1394 handle */
473 handle = raw1394_new_handle();
474 if( !handle )
476 msg_Err( p_access, "raw1394 - failed to get handle: %s",
477 vlc_strerror_c(errno) );
478 return NULL;
481 if( ( n_ports = raw1394_get_port_info( handle, pinf, 16 ) ) < 0 )
483 msg_Err( p_access, "raw1394 - failed to get port info: %s",
484 vlc_strerror_c(errno) );
485 raw1394_destroy_handle( handle );
486 return NULL;
489 /* tell raw1394 which host adapter to use */
490 if( raw1394_set_port( handle, port ) < 0 )
492 msg_Err( p_access, "raw1394 - failed to set set port: %s",
493 vlc_strerror_c(errno) );
494 return NULL;
497 return handle;
500 static void Raw1394Close( raw1394handle_t handle )
502 raw1394_destroy_handle( handle );
505 static int DiscoverAVC( access_t *p_access, int* port, uint64_t guid )
507 rom1394_directory rom_dir;
508 raw1394handle_t handle = NULL;
509 int device = -1;
510 int i, j = 0;
511 int m = Raw1394GetNumPorts( p_access );
513 if( *port >= 0 )
515 /* search on explicit port */
516 j = *port;
517 m = *port + 1;
520 for( ; j < m && device == -1; j++ )
522 handle = Raw1394Open( p_access, j );
523 if( !handle )
524 return -1;
526 for( i = 0; i < raw1394_get_nodecount( handle ); ++i )
528 if( guid != 0 )
530 /* select explicitly by GUID */
531 if( guid == rom1394_get_guid( handle, i ) )
533 device = i;
534 *port = j;
535 break;
538 else
540 /* select first AV/C Tape Reccorder Player node */
541 if( rom1394_get_directory( handle, i, &rom_dir ) < 0 )
543 msg_Err( p_access, "error reading config rom directory for node %d", i );
544 continue;
546 if( ( rom1394_get_node_type( &rom_dir ) == ROM1394_NODE_TYPE_AVC ) &&
547 avc1394_check_subunit_type( handle, i, AVC1394_SUBUNIT_TYPE_VCR ) )
549 device = i;
550 *port = j;
551 break;
555 Raw1394Close( handle );
558 return device;
562 * Handle AVC commands
564 static raw1394handle_t AVCOpen( access_t *p_access, int port )
566 access_sys_t *p_sys = p_access->p_sys;
567 struct raw1394_portinfo pinf[ 16 ];
568 int numcards;
570 p_sys->p_avc1394 = raw1394_new_handle();
571 if( !p_sys->p_avc1394 )
572 return NULL;
574 numcards = raw1394_get_port_info( p_sys->p_avc1394, pinf, 16 );
575 if( numcards < -1 )
576 return NULL;
577 if( raw1394_set_port( p_sys->p_avc1394, port ) < 0 )
578 return NULL;
580 raw1394_set_bus_reset_handler( p_sys->p_avc1394, AVCResetHandler );
582 return p_sys->p_avc1394;
585 static void AVCClose( access_t *p_access )
587 access_sys_t *p_sys = p_access->p_sys;
589 if( p_sys->p_avc1394 )
591 raw1394_destroy_handle( p_sys->p_avc1394 );
592 p_sys->p_avc1394 = NULL;
596 static int AVCResetHandler( raw1394handle_t handle, unsigned int generation )
598 raw1394_update_generation( handle, generation );
599 return 0;
602 static int AVCPlay( access_t *p_access, int phyID )
604 access_sys_t *p_sys = p_access->p_sys;
606 msg_Dbg( p_access, "send play command over Digital Video control channel" );
608 if( p_sys->p_avc1394 && phyID >= 0 )
610 if( !avc1394_vcr_is_recording( p_sys->p_avc1394, phyID ) &&
611 avc1394_vcr_is_playing( p_sys->p_avc1394, phyID ) != AVC1394_VCR_OPERAND_PLAY_FORWARD )
612 avc1394_vcr_play( p_sys->p_avc1394, phyID );
614 return 0;
617 static int AVCPause( access_t *p_access, int phyID )
619 access_sys_t *p_sys = p_access->p_sys;
621 if( p_sys->p_avc1394 && phyID >= 0 )
623 if( !avc1394_vcr_is_recording( p_sys->p_avc1394, phyID ) &&
624 ( avc1394_vcr_is_playing( p_sys->p_avc1394, phyID ) != AVC1394_VCR_OPERAND_PLAY_FORWARD_PAUSE ) )
625 avc1394_vcr_pause( p_sys->p_avc1394, phyID );
627 return 0;
631 static int AVCStop( access_t *p_access, int phyID )
633 access_sys_t *p_sys = p_access->p_sys;
635 msg_Dbg( p_access, "closing Digital Video control channel" );
637 if ( p_sys->p_avc1394 && phyID >= 0 )
638 avc1394_vcr_stop( p_sys->p_avc1394, phyID );
640 return 0;