codec/hxxx_helper: removing redundant new-line from call to msg_Dbg
[vlc.git] / modules / access / dv.c
blobd81d2b6659b11491344136cc47b007b0fec511ae
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 <assert.h>
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_access.h>
37 #include <errno.h>
38 #include <sys/types.h>
39 #include <unistd.h>
40 #include <sys/poll.h>
42 #include <libraw1394/raw1394.h>
43 #include <libraw1394/csr.h>
44 #include <libavc1394/avc1394.h>
45 #include <libavc1394/avc1394_vcr.h>
46 #include <libavc1394/rom1394.h>
48 /*****************************************************************************
49 * Module descriptor
50 *****************************************************************************/
51 static int Open ( vlc_object_t * );
52 static void Close( vlc_object_t * );
53 static block_t *Block( access_t *, bool * );
54 static int Control( access_t *, int, va_list );
56 vlc_module_begin ()
57 set_description( N_("Digital Video (Firewire/ieee1394) input") )
58 set_shortname( N_("DV") )
59 set_category( CAT_INPUT )
60 set_subcategory( SUBCAT_INPUT_ACCESS )
61 set_capability( "access", 0 )
62 add_shortcut( "dv", "raw1394" )
63 set_callbacks( Open, Close )
64 vlc_module_end ()
66 typedef struct
68 vlc_thread_t thread;
69 access_t *p_access;
70 vlc_mutex_t lock;
71 block_t *p_frame;
72 block_t **pp_last;
74 } event_thread_t;
76 static void* Raw1394EventThread( void * );
77 static enum raw1394_iso_disposition
78 Raw1394Handler(raw1394handle_t, unsigned char *,
79 unsigned int, unsigned char,
80 unsigned char, unsigned char, unsigned int,
81 unsigned int);
83 static int Raw1394GetNumPorts( access_t *p_access );
84 static raw1394handle_t Raw1394Open( access_t *, int );
85 static void Raw1394Close( raw1394handle_t );
87 static int DiscoverAVC( access_t *, int *, uint64_t );
88 static raw1394handle_t AVCOpen( access_t *, int );
89 static void AVCClose( access_t * );
90 static int AVCResetHandler( raw1394handle_t, unsigned int );
91 static int AVCPlay( access_t *, int );
92 static int AVCPause( access_t *, int );
93 static int AVCStop( access_t *, int );
95 struct access_sys_t
97 raw1394handle_t p_avc1394;
98 raw1394handle_t p_raw1394;
99 struct pollfd raw1394_poll;
101 int i_cards;
102 int i_node;
103 int i_port;
104 int i_channel;
105 uint64_t i_guid;
107 /* event */
108 event_thread_t *p_ev;
109 vlc_mutex_t lock;
110 block_t *p_frame;
113 #define ISOCHRONOUS_QUEUE_LENGTH 1000
114 #define ISOCHRONOUS_MAX_PACKET_SIZE 4096
116 /*****************************************************************************
117 * Open: open the file
118 *****************************************************************************/
119 static int Open( vlc_object_t *p_this )
121 access_t *p_access = (access_t*)p_this;
122 access_sys_t *p_sys;
124 struct raw1394_portinfo port_inf[ 16 ];
126 msg_Dbg( p_access, "opening device" );
128 /* Set up p_access */
129 ACCESS_SET_CALLBACKS( NULL, Block, Control, NULL );
131 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
132 if( !p_sys )
133 return VLC_EGENERIC;
135 p_sys->i_cards = 0;
136 p_sys->i_node = 0;
137 p_sys->i_port = 0;
138 p_sys->i_guid = 0;
139 p_sys->i_channel = 63;
140 p_sys->p_raw1394 = NULL;
141 p_sys->p_avc1394 = NULL;
142 p_sys->p_frame = NULL;
143 p_sys->p_ev = NULL;
145 vlc_mutex_init( &p_sys->lock );
147 p_sys->i_node = DiscoverAVC( p_access, &p_sys->i_port, p_sys->i_guid );
148 if( p_sys->i_node < 0 )
150 msg_Err( p_access, "failed to open a Firewire (IEEE1394) connection" );
151 Close( p_this );
152 return VLC_EGENERIC;
155 p_sys->p_avc1394 = AVCOpen( p_access, p_sys->i_port );
156 if( !p_sys->p_avc1394 )
158 msg_Err( p_access, "no Digital Video Control device found" );
159 Close( p_this );
160 return VLC_EGENERIC;
163 p_sys->p_raw1394 = raw1394_new_handle();
164 if( !p_sys->p_raw1394 )
166 msg_Err( p_access, "no Digital Video device found" );
167 Close( p_this );
168 return VLC_EGENERIC;
171 p_sys->i_cards = raw1394_get_port_info( p_sys->p_raw1394, port_inf, 16 );
172 if( p_sys->i_cards < 0 )
174 msg_Err( p_access, "failed to get port info" );
175 Close( p_this );
176 return VLC_EGENERIC;
179 if( raw1394_set_port( p_sys->p_raw1394, p_sys->i_port ) < 0 )
181 msg_Err( p_access, "failed to set port info" );
182 Close( p_this );
183 return VLC_EGENERIC;
186 if ( raw1394_iso_recv_init( p_sys->p_raw1394, Raw1394Handler,
187 ISOCHRONOUS_QUEUE_LENGTH, ISOCHRONOUS_MAX_PACKET_SIZE,
188 p_sys->i_channel, RAW1394_DMA_PACKET_PER_BUFFER, -1 ) < 0 )
190 msg_Err( p_access, "failed to init isochronous recv" );
191 Close( p_this );
192 return VLC_EGENERIC;
195 raw1394_set_userdata( p_sys->p_raw1394, p_access );
196 raw1394_iso_recv_start( p_sys->p_raw1394, -1, -1, 0 );
198 p_sys->raw1394_poll.fd = raw1394_get_fd( p_sys->p_raw1394 );
199 p_sys->raw1394_poll.events = POLLIN | POLLPRI;
201 /* Now create our event thread catcher */
202 p_sys->p_ev = calloc( 1, sizeof( *p_sys->p_ev ) );
203 if( !p_sys->p_ev )
205 msg_Err( p_access, "failed to create event thread struct" );
206 Close( p_this );
207 return VLC_ENOMEM;
210 p_sys->p_ev->p_frame = NULL;
211 p_sys->p_ev->pp_last = &p_sys->p_ev->p_frame;
212 p_sys->p_ev->p_access = p_access;
213 vlc_mutex_init( &p_sys->p_ev->lock );
214 if( vlc_clone( &p_sys->p_ev->thread, Raw1394EventThread,
215 p_sys->p_ev, VLC_THREAD_PRIORITY_OUTPUT ) )
217 msg_Err( p_access, "failed to clone event thread" );
218 Close( p_this );
219 return VLC_EGENERIC;
222 return VLC_SUCCESS;
225 /*****************************************************************************
226 * Close: free unused data structures
227 *****************************************************************************/
228 static void Close( vlc_object_t *p_this )
230 access_t *p_access = (access_t*)p_this;
231 access_sys_t *p_sys = p_access->p_sys;
233 if( p_sys->p_ev )
235 /* stop the event handler */
236 vlc_cancel( p_sys->p_ev->thread );
238 if( p_sys->p_raw1394 )
239 raw1394_iso_shutdown( p_sys->p_raw1394 );
241 vlc_join( p_sys->p_ev->thread, NULL );
242 vlc_mutex_destroy( &p_sys->p_ev->lock );
244 /* Cleanup frame data */
245 if( p_sys->p_ev->p_frame )
247 block_ChainRelease( p_sys->p_ev->p_frame );
248 p_sys->p_ev->p_frame = NULL;
249 p_sys->p_ev->pp_last = &p_sys->p_frame;
251 free( p_sys->p_ev );
254 if( p_sys->p_frame )
255 block_ChainRelease( p_sys->p_frame );
256 if( p_sys->p_raw1394 )
257 raw1394_destroy_handle( p_sys->p_raw1394 );
259 AVCClose( p_access );
261 vlc_mutex_destroy( &p_sys->lock );
262 free( p_sys );
265 /*****************************************************************************
266 * Control:
267 *****************************************************************************/
268 static int Control( access_t *p_access, int i_query, va_list args )
270 access_sys_t *sys = p_access->p_sys;
272 switch( i_query )
274 /* */
275 case STREAM_CAN_PAUSE:
276 *va_arg( args, bool* ) = true;
277 break;
279 case STREAM_CAN_SEEK:
280 case STREAM_CAN_FASTSEEK:
281 case STREAM_CAN_CONTROL_PACE:
282 *va_arg( args, bool* ) = false;
283 break;
285 case STREAM_GET_PTS_DELAY:
286 *va_arg( args, int64_t * ) =
287 INT64_C(1000) * var_InheritInteger( p_access, "live-caching" );
288 break;
290 /* */
291 case STREAM_SET_PAUSE_STATE:
292 AVCPause( p_access, sys->i_node );
293 break;
295 default:
296 return VLC_EGENERIC;
299 return VLC_SUCCESS;
302 static block_t *Block( access_t *p_access, bool *restrict eof )
304 access_sys_t *p_sys = p_access->p_sys;
305 block_t *p_block = NULL;
307 vlc_mutex_lock( &p_sys->lock );
308 p_block = p_sys->p_frame;
309 //msg_Dbg( p_access, "sending frame %p", (void *)p_block );
310 p_sys->p_frame = NULL;
311 vlc_mutex_unlock( &p_sys->lock );
313 (void) eof;
314 return p_block;
317 static void Raw1394EventThreadCleanup( void *obj )
319 event_thread_t *p_ev = (event_thread_t *)obj;
320 access_sys_t *sys = p_ev->p_access->p_sys;
322 AVCStop( p_ev->p_access, sys->i_node );
325 static void* Raw1394EventThread( void *obj )
327 event_thread_t *p_ev = (event_thread_t *)obj;
328 access_t *p_access = (access_t *) p_ev->p_access;
329 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
330 int result = 0;
331 int canc = vlc_savecancel();
333 AVCPlay( p_access, p_sys->i_node );
334 vlc_cleanup_push( Raw1394EventThreadCleanup, p_ev );
335 vlc_restorecancel( canc );
337 for( ;; )
339 while( ( result = poll( &p_sys->raw1394_poll, 1, -1 ) ) < 0 )
341 if( errno != EINTR )
342 msg_Err( p_access, "poll error: %s", vlc_strerror_c(errno) );
345 if( result > 0 && ( ( p_sys->raw1394_poll.revents & POLLIN )
346 || ( p_sys->raw1394_poll.revents & POLLPRI ) ) )
348 canc = vlc_savecancel();
349 result = raw1394_loop_iterate( p_sys->p_raw1394 );
350 vlc_restorecancel( canc );
354 vlc_cleanup_pop();
355 vlc_assert_unreachable();
358 static enum raw1394_iso_disposition
359 Raw1394Handler(raw1394handle_t handle, unsigned char *data,
360 unsigned int length, unsigned char channel,
361 unsigned char tag, unsigned char sy, unsigned int cycle,
362 unsigned int dropped)
364 access_t *p_access = NULL;
365 access_sys_t *p_sys = NULL;
366 block_t *p_block = NULL;
367 VLC_UNUSED(channel); VLC_UNUSED(tag);
368 VLC_UNUSED(sy); VLC_UNUSED(cycle); VLC_UNUSED(dropped);
370 p_access = (access_t *) raw1394_get_userdata( handle );
371 if( !p_access ) return 0;
373 p_sys = p_access->p_sys;
375 /* skip empty packets */
376 if( length > 16 )
378 unsigned char * p = data + 8;
379 int section_type = p[ 0 ] >> 5; /* section type is in bits 5 - 7 */
380 int dif_sequence = p[ 1 ] >> 4; /* dif sequence number is in bits 4 - 7 */
381 int dif_block = p[ 2 ];
383 vlc_mutex_lock( &p_sys->p_ev->lock );
385 /* if we are at the beginning of a frame, we put the previous
386 frame in our output_queue. */
387 if( (section_type == 0) && (dif_sequence == 0) )
389 vlc_mutex_lock( &p_sys->lock );
390 if( p_sys->p_ev->p_frame )
392 /* Push current frame to p_access thread. */
393 //p_sys->p_ev->p_frame->i_pts = mdate();
394 block_ChainAppend( &p_sys->p_frame, p_sys->p_ev->p_frame );
396 /* reset list */
397 p_sys->p_ev->p_frame = block_Alloc( 144000 );
398 p_sys->p_ev->pp_last = &p_sys->p_frame;
399 vlc_mutex_unlock( &p_sys->lock );
402 p_block = p_sys->p_ev->p_frame;
403 if( p_block )
405 switch ( section_type )
407 case 0: /* 1 Header block */
408 /* p[3] |= 0x80; // hack to force PAL data */
409 memcpy( p_block->p_buffer + dif_sequence * 150 * 80, p, 480 );
410 break;
412 case 1: /* 2 Subcode blocks */
413 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 1 + dif_block ) * 80, p, 480 );
414 break;
416 case 2: /* 3 VAUX blocks */
417 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 3 + dif_block ) * 80, p, 480 );
418 break;
420 case 3: /* 9 Audio blocks interleaved with video */
421 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 6 + dif_block * 16 ) * 80, p, 480 );
422 break;
424 case 4: /* 135 Video blocks interleaved with audio */
425 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 7 + ( dif_block / 15 ) + dif_block ) * 80, p, 480 );
426 break;
428 default: /* we canĀ“t handle any other data */
429 block_Release( p_block );
430 p_block = NULL;
431 break;
435 vlc_mutex_unlock( &p_sys->p_ev->lock );
437 return 0;
441 * Routing borrowed from dvgrab-1.8
442 * Copyright by Arne Schirmacher <dvgrab@schirmacher.de>
443 * Dan Dennedy <dan@dennedy.org> and others
445 static int Raw1394GetNumPorts( access_t *p_access )
447 int n_ports;
448 struct raw1394_portinfo pinf[ 16 ];
449 raw1394handle_t handle;
451 /* get a raw1394 handle */
452 if( !( handle = raw1394_new_handle() ) )
454 msg_Err( p_access, "raw1394 - failed to get handle: %s",
455 vlc_strerror_c(errno) );
456 return VLC_EGENERIC;
459 if( ( n_ports = raw1394_get_port_info( handle, pinf, 16 ) ) < 0 )
461 msg_Err( p_access, "raw1394 - failed to get port info: %s",
462 vlc_strerror_c(errno) );
463 raw1394_destroy_handle( handle );
464 return VLC_EGENERIC;
466 raw1394_destroy_handle( handle );
468 return n_ports;
471 static raw1394handle_t Raw1394Open( access_t *p_access, int port )
473 int n_ports;
474 struct raw1394_portinfo pinf[ 16 ];
475 raw1394handle_t handle;
477 /* get a raw1394 handle */
478 handle = raw1394_new_handle();
479 if( !handle )
481 msg_Err( p_access, "raw1394 - failed to get handle: %s",
482 vlc_strerror_c(errno) );
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: %s",
489 vlc_strerror_c(errno) );
490 raw1394_destroy_handle( handle );
491 return NULL;
494 /* tell raw1394 which host adapter to use */
495 if( raw1394_set_port( handle, port ) < 0 )
497 msg_Err( p_access, "raw1394 - failed to set set port: %s",
498 vlc_strerror_c(errno) );
499 return NULL;
502 return handle;
505 static void Raw1394Close( raw1394handle_t handle )
507 raw1394_destroy_handle( handle );
510 static int DiscoverAVC( access_t *p_access, int* port, uint64_t guid )
512 rom1394_directory rom_dir;
513 raw1394handle_t handle = NULL;
514 int device = -1;
515 int i, j = 0;
516 int m = Raw1394GetNumPorts( p_access );
518 if( *port >= 0 )
520 /* search on explicit port */
521 j = *port;
522 m = *port + 1;
525 for( ; j < m && device == -1; j++ )
527 handle = Raw1394Open( p_access, j );
528 if( !handle )
529 return -1;
531 for( i = 0; i < raw1394_get_nodecount( handle ); ++i )
533 if( guid != 0 )
535 /* select explicitly by GUID */
536 if( guid == rom1394_get_guid( handle, i ) )
538 device = i;
539 *port = j;
540 break;
543 else
545 /* select first AV/C Tape Reccorder Player node */
546 if( rom1394_get_directory( handle, i, &rom_dir ) < 0 )
548 msg_Err( p_access, "error reading config rom directory for node %d", i );
549 continue;
551 if( ( rom1394_get_node_type( &rom_dir ) == ROM1394_NODE_TYPE_AVC ) &&
552 avc1394_check_subunit_type( handle, i, AVC1394_SUBUNIT_TYPE_VCR ) )
554 device = i;
555 *port = j;
556 break;
560 Raw1394Close( handle );
563 return device;
567 * Handle AVC commands
569 static raw1394handle_t AVCOpen( access_t *p_access, int port )
571 access_sys_t *p_sys = p_access->p_sys;
572 struct raw1394_portinfo pinf[ 16 ];
573 int numcards;
575 p_sys->p_avc1394 = raw1394_new_handle();
576 if( !p_sys->p_avc1394 )
577 return NULL;
579 numcards = raw1394_get_port_info( p_sys->p_avc1394, pinf, 16 );
580 if( numcards < -1 )
581 return NULL;
582 if( raw1394_set_port( p_sys->p_avc1394, port ) < 0 )
583 return NULL;
585 raw1394_set_bus_reset_handler( p_sys->p_avc1394, AVCResetHandler );
587 return p_sys->p_avc1394;
590 static void AVCClose( access_t *p_access )
592 access_sys_t *p_sys = p_access->p_sys;
594 if( p_sys->p_avc1394 )
596 raw1394_destroy_handle( p_sys->p_avc1394 );
597 p_sys->p_avc1394 = NULL;
601 static int AVCResetHandler( raw1394handle_t handle, unsigned int generation )
603 raw1394_update_generation( handle, generation );
604 return 0;
607 static int AVCPlay( access_t *p_access, int phyID )
609 access_sys_t *p_sys = p_access->p_sys;
611 msg_Dbg( p_access, "send play command over Digital Video control channel" );
613 if( p_sys->p_avc1394 && phyID >= 0 )
615 if( !avc1394_vcr_is_recording( p_sys->p_avc1394, phyID ) &&
616 avc1394_vcr_is_playing( p_sys->p_avc1394, phyID ) != AVC1394_VCR_OPERAND_PLAY_FORWARD )
617 avc1394_vcr_play( p_sys->p_avc1394, phyID );
619 return 0;
622 static int AVCPause( access_t *p_access, int phyID )
624 access_sys_t *p_sys = p_access->p_sys;
626 if( p_sys->p_avc1394 && phyID >= 0 )
628 if( !avc1394_vcr_is_recording( p_sys->p_avc1394, phyID ) &&
629 ( avc1394_vcr_is_playing( p_sys->p_avc1394, phyID ) != AVC1394_VCR_OPERAND_PLAY_FORWARD_PAUSE ) )
630 avc1394_vcr_pause( p_sys->p_avc1394, phyID );
632 return 0;
636 static int AVCStop( access_t *p_access, int phyID )
638 access_sys_t *p_sys = p_access->p_sys;
640 msg_Dbg( p_access, "closing Digital Video control channel" );
642 if ( p_sys->p_avc1394 && phyID >= 0 )
643 avc1394_vcr_stop( p_sys->p_avc1394, phyID );
645 return 0;