Qt: add asserts around p_input access
[vlc.git] / modules / access / dv.c
blobd43d476311a2ba5d0eb4f904ff8511f204d62fb3
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 along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 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 #ifdef HAVE_UNISTD_H
38 # include <unistd.h>
39 #elif defined( WIN32 ) && !defined( UNDER_CE )
40 # include <io.h>
41 #endif
43 #include <sys/poll.h>
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 /*****************************************************************************
52 * Module descriptor
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 );
59 vlc_module_begin ()
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", "dv1394", "raw1394" )
66 set_callbacks( Open, Close )
67 vlc_module_end ()
69 typedef struct
71 vlc_thread_t thread;
72 access_t *p_access;
73 vlc_mutex_t lock;
74 block_t *p_frame;
75 block_t **pp_last;
77 } event_thread_t;
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,
84 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 );
98 struct access_sys_t
100 raw1394handle_t p_avc1394;
101 raw1394handle_t p_raw1394;
102 struct pollfd raw1394_poll;
104 int i_cards;
105 int i_node;
106 int i_port;
107 int i_channel;
108 uint64_t i_guid;
110 /* event */
111 event_thread_t *p_ev;
112 vlc_mutex_t lock;
113 block_t *p_frame;
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;
125 access_sys_t *p_sys;
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 ) );
136 if( !p_sys )
137 return VLC_EGENERIC;
139 p_sys->i_cards = 0;
140 p_sys->i_node = 0;
141 p_sys->i_port = 0;
142 p_sys->i_guid = 0;
143 p_sys->i_channel = 63;
144 p_sys->p_raw1394 = NULL;
145 p_sys->p_avc1394 = NULL;
146 p_sys->p_frame = NULL;
147 p_sys->p_ev = 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" );
155 Close( p_this );
156 return VLC_EGENERIC;
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" );
163 Close( p_this );
164 return VLC_EGENERIC;
167 p_sys->p_raw1394 = raw1394_new_handle();
168 if( !p_sys->p_raw1394 )
170 msg_Err( p_access, "no Digital Video device found" );
171 Close( p_this );
172 return VLC_EGENERIC;
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" );
179 Close( p_this );
180 return VLC_EGENERIC;
183 if( raw1394_set_port( p_sys->p_raw1394, p_sys->i_port ) < 0 )
185 msg_Err( p_access, "failed to set port info" );
186 Close( p_this );
187 return VLC_EGENERIC;
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" );
195 Close( p_this );
196 return VLC_EGENERIC;
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 ) );
207 if( !p_sys->p_ev )
209 msg_Err( p_access, "failed to create event thread" );
210 Close( p_this );
211 return VLC_EGENERIC;
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 vlc_clone( &p_sys->p_ev->thread, Raw1394EventThread,
219 p_sys->p_ev, VLC_THREAD_PRIORITY_OUTPUT );
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 case ACCESS_GET_TITLE_INFO:
293 case ACCESS_SET_TITLE:
294 case ACCESS_SET_SEEKPOINT:
295 case ACCESS_SET_PRIVATE_ID_STATE:
296 case ACCESS_GET_CONTENT_TYPE:
297 return VLC_EGENERIC;
299 default:
300 msg_Warn( p_access, "unimplemented query in control" );
301 return VLC_EGENERIC;
304 return VLC_SUCCESS;
307 static block_t *Block( access_t *p_access )
309 access_sys_t *p_sys = p_access->p_sys;
310 block_t *p_block = NULL;
312 vlc_mutex_lock( &p_sys->lock );
313 p_block = p_sys->p_frame;
314 //msg_Dbg( p_access, "sending frame %p",p_block );
315 p_sys->p_frame = NULL;
316 vlc_mutex_unlock( &p_sys->lock );
318 return p_block;
321 static void Raw1394EventThreadCleanup( void *obj )
323 event_thread_t *p_ev = (event_thread_t *)obj;
325 AVCStop( p_ev->p_access, p_ev->p_access->p_sys->i_node );
328 static void* Raw1394EventThread( void *obj )
330 event_thread_t *p_ev = (event_thread_t *)obj;
331 access_t *p_access = (access_t *) p_ev->p_access;
332 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
333 int result = 0;
334 int canc = vlc_savecancel();
336 AVCPlay( p_access, p_sys->i_node );
337 vlc_cleanup_push( Raw1394EventThreadCleanup, p_ev );
338 vlc_restorecancel( canc );
340 for( ;; )
342 while( ( result = poll( &p_sys->raw1394_poll, 1, -1 ) ) < 0 )
344 if( errno != EINTR )
345 msg_Err( p_access, "poll error: %m" );
348 if( result > 0 && ( ( p_sys->raw1394_poll.revents & POLLIN )
349 || ( p_sys->raw1394_poll.revents & POLLPRI ) ) )
351 canc = vlc_savecancel();
352 result = raw1394_loop_iterate( p_sys->p_raw1394 );
353 vlc_restorecancel( canc );
357 vlc_cleanup_run();
358 return NULL;
361 static enum raw1394_iso_disposition
362 Raw1394Handler(raw1394handle_t handle, unsigned char *data,
363 unsigned int length, unsigned char channel,
364 unsigned char tag, unsigned char sy, unsigned int cycle,
365 unsigned int dropped)
367 access_t *p_access = NULL;
368 access_sys_t *p_sys = NULL;
369 block_t *p_block = NULL;
370 VLC_UNUSED(channel); VLC_UNUSED(tag);
371 VLC_UNUSED(sy); VLC_UNUSED(cycle); VLC_UNUSED(dropped);
373 p_access = (access_t *) raw1394_get_userdata( handle );
374 if( !p_access ) return 0;
376 p_sys = p_access->p_sys;
378 /* skip empty packets */
379 if( length > 16 )
381 unsigned char * p = data + 8;
382 int section_type = p[ 0 ] >> 5; /* section type is in bits 5 - 7 */
383 int dif_sequence = p[ 1 ] >> 4; /* dif sequence number is in bits 4 - 7 */
384 int dif_block = p[ 2 ];
386 vlc_mutex_lock( &p_sys->p_ev->lock );
388 /* if we are at the beginning of a frame, we put the previous
389 frame in our output_queue. */
390 if( (section_type == 0) && (dif_sequence == 0) )
392 vlc_mutex_lock( &p_sys->lock );
393 if( p_sys->p_ev->p_frame )
395 /* Push current frame to p_access thread. */
396 //p_sys->p_ev->p_frame->i_pts = mdate();
397 block_ChainAppend( &p_sys->p_frame, p_sys->p_ev->p_frame );
399 /* reset list */
400 p_sys->p_ev->p_frame = block_New( p_access, 144000 );
401 p_sys->p_ev->pp_last = &p_sys->p_frame;
402 vlc_mutex_unlock( &p_sys->lock );
405 p_block = p_sys->p_ev->p_frame;
406 if( p_block )
408 switch ( section_type )
410 case 0: /* 1 Header block */
411 /* p[3] |= 0x80; // hack to force PAL data */
412 memcpy( p_block->p_buffer + dif_sequence * 150 * 80, p, 480 );
413 break;
415 case 1: /* 2 Subcode blocks */
416 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 1 + dif_block ) * 80, p, 480 );
417 break;
419 case 2: /* 3 VAUX blocks */
420 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 3 + dif_block ) * 80, p, 480 );
421 break;
423 case 3: /* 9 Audio blocks interleaved with video */
424 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 6 + dif_block * 16 ) * 80, p, 480 );
425 break;
427 case 4: /* 135 Video blocks interleaved with audio */
428 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 7 + ( dif_block / 15 ) + dif_block ) * 80, p, 480 );
429 break;
431 default: /* we canĀ“t handle any other data */
432 block_Release( p_block );
433 p_block = NULL;
434 break;
438 vlc_mutex_unlock( &p_sys->p_ev->lock );
440 return 0;
444 * Routing borrowed from dvgrab-1.8
445 * Copyright by Arne Schirmacher <dvgrab@schirmacher.de>
446 * Dan Dennedy <dan@dennedy.org> and others
448 static int Raw1394GetNumPorts( access_t *p_access )
450 int n_ports;
451 struct raw1394_portinfo pinf[ 16 ];
452 raw1394handle_t handle;
454 /* get a raw1394 handle */
455 if( !( handle = raw1394_new_handle() ) )
457 msg_Err( p_access, "raw1394 - failed to get handle: %m." );
458 return VLC_EGENERIC;
461 if( ( n_ports = raw1394_get_port_info( handle, pinf, 16 ) ) < 0 )
463 msg_Err( p_access, "raw1394 - failed to get port info: %m." );
464 raw1394_destroy_handle( handle );
465 return VLC_EGENERIC;
467 raw1394_destroy_handle( handle );
469 return n_ports;
472 static raw1394handle_t Raw1394Open( access_t *p_access, int port )
474 int n_ports;
475 struct raw1394_portinfo pinf[ 16 ];
476 raw1394handle_t handle;
478 /* get a raw1394 handle */
479 handle = raw1394_new_handle();
480 if( !handle )
482 msg_Err( p_access, "raw1394 - failed to get handle: %m." );
483 return NULL;
486 if( ( n_ports = raw1394_get_port_info( handle, pinf, 16 ) ) < 0 )
488 msg_Err( p_access, "raw1394 - failed to get port info: %m." );
489 raw1394_destroy_handle( handle );
490 return NULL;
493 /* tell raw1394 which host adapter to use */
494 if( raw1394_set_port( handle, port ) < 0 )
496 msg_Err( p_access, "raw1394 - failed to set set port: %m." );
497 return NULL;
500 return handle;
503 static void Raw1394Close( raw1394handle_t handle )
505 raw1394_destroy_handle( handle );
508 static int DiscoverAVC( access_t *p_access, int* port, uint64_t guid )
510 rom1394_directory rom_dir;
511 raw1394handle_t handle = NULL;
512 int device = -1;
513 int i, j = 0;
514 int m = Raw1394GetNumPorts( p_access );
516 if( *port >= 0 )
518 /* search on explicit port */
519 j = *port;
520 m = *port + 1;
523 for( ; j < m && device == -1; j++ )
525 handle = Raw1394Open( p_access, j );
526 if( !handle )
527 return -1;
529 for( i = 0; i < raw1394_get_nodecount( handle ); ++i )
531 if( guid != 0 )
533 /* select explicitly by GUID */
534 if( guid == rom1394_get_guid( handle, i ) )
536 device = i;
537 *port = j;
538 break;
541 else
543 /* select first AV/C Tape Reccorder Player node */
544 if( rom1394_get_directory( handle, i, &rom_dir ) < 0 )
546 msg_Err( p_access, "error reading config rom directory for node %d", i );
547 continue;
549 if( ( rom1394_get_node_type( &rom_dir ) == ROM1394_NODE_TYPE_AVC ) &&
550 avc1394_check_subunit_type( handle, i, AVC1394_SUBUNIT_TYPE_VCR ) )
552 device = i;
553 *port = j;
554 break;
558 Raw1394Close( handle );
561 return device;
565 * Handle AVC commands
567 static raw1394handle_t AVCOpen( access_t *p_access, int port )
569 access_sys_t *p_sys = p_access->p_sys;
570 struct raw1394_portinfo pinf[ 16 ];
571 int numcards;
573 p_sys->p_avc1394 = raw1394_new_handle();
574 if( !p_sys->p_avc1394 )
575 return NULL;
577 numcards = raw1394_get_port_info( p_sys->p_avc1394, pinf, 16 );
578 if( numcards < -1 )
579 return NULL;
580 if( raw1394_set_port( p_sys->p_avc1394, port ) < 0 )
581 return NULL;
583 raw1394_set_bus_reset_handler( p_sys->p_avc1394, AVCResetHandler );
585 return p_sys->p_avc1394;
588 static void AVCClose( access_t *p_access )
590 access_sys_t *p_sys = p_access->p_sys;
592 if( p_sys->p_avc1394 )
594 raw1394_destroy_handle( p_sys->p_avc1394 );
595 p_sys->p_avc1394 = NULL;
599 static int AVCResetHandler( raw1394handle_t handle, unsigned int generation )
601 raw1394_update_generation( handle, generation );
602 return 0;
605 static int AVCPlay( access_t *p_access, int phyID )
607 access_sys_t *p_sys = p_access->p_sys;
609 msg_Dbg( p_access, "send play command over Digital Video control channel" );
611 if( p_sys->p_avc1394 && phyID >= 0 )
613 if( !avc1394_vcr_is_recording( p_sys->p_avc1394, phyID ) &&
614 avc1394_vcr_is_playing( p_sys->p_avc1394, phyID ) != AVC1394_VCR_OPERAND_PLAY_FORWARD )
615 avc1394_vcr_play( p_sys->p_avc1394, phyID );
617 return 0;
620 static int AVCPause( access_t *p_access, int phyID )
622 access_sys_t *p_sys = p_access->p_sys;
624 if( p_sys->p_avc1394 && phyID >= 0 )
626 if( !avc1394_vcr_is_recording( p_sys->p_avc1394, phyID ) &&
627 ( avc1394_vcr_is_playing( p_sys->p_avc1394, phyID ) != AVC1394_VCR_OPERAND_PLAY_FORWARD_PAUSE ) )
628 avc1394_vcr_pause( p_sys->p_avc1394, phyID );
630 return 0;
634 static int AVCStop( access_t *p_access, int phyID )
636 access_sys_t *p_sys = p_access->p_sys;
638 msg_Dbg( p_access, "closing Digital Video control channel" );
640 if ( p_sys->p_avc1394 && phyID >= 0 )
641 avc1394_vcr_stop( p_sys->p_avc1394, phyID );
643 return 0;