1 /*****************************************************************************
2  * dv.c: Digital video/Firewire input (file: access plug-in)
3  *****************************************************************************
4  * Copyright (C) 2005 M2X
5  * $Id: b15b22f49080fecd841e153b6cf13cb8080170dc $
6  *
7  * Authors: Jean-Paul Saman <jpsaman at m2x dot nl>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23 
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30 
31 #include <assert.h>
32 
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_access.h>
36 
37 #include <errno.h>
38 #include <sys/types.h>
39 #include <unistd.h>
40 #include <sys/poll.h>
41 
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>
47 
48 /*****************************************************************************
49  * Module descriptor
50  *****************************************************************************/
51 static int  Open ( vlc_object_t * );
52 static void Close( vlc_object_t * );
53 static block_t *Block( stream_t *, bool * );
54 static int Control( stream_t *, int, va_list );
55 
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 ()
65 
66 typedef struct
67 {
68     vlc_thread_t    thread;
69     stream_t        *p_access;
70     vlc_mutex_t     lock;
71     block_t         *p_frame;
72     block_t         **pp_last;
73 
74 } event_thread_t;
75 
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);
82 
83 static int Raw1394GetNumPorts( stream_t *p_access );
84 static raw1394handle_t Raw1394Open( stream_t *, int );
85 static void Raw1394Close( raw1394handle_t );
86 
87 static int DiscoverAVC( stream_t *, int *, uint64_t );
88 static raw1394handle_t AVCOpen( stream_t *, int );
89 static void AVCClose( stream_t * );
90 static int AVCResetHandler( raw1394handle_t, unsigned int );
91 static int AVCPlay( stream_t *, int );
92 static int AVCPause( stream_t *, int );
93 static int AVCStop( stream_t *, int );
94 
95 struct access_sys_t
96 {
97     raw1394handle_t p_avc1394;
98     raw1394handle_t p_raw1394;
99     struct pollfd   raw1394_poll;
100 
101     int i_cards;
102     int i_node;
103     int i_port;
104     int i_channel;
105     uint64_t i_guid;
106 
107     /* event */
108     event_thread_t *p_ev;
109     vlc_mutex_t lock;
110     block_t *p_frame;
111 };
112 
113 #define ISOCHRONOUS_QUEUE_LENGTH 1000
114 #define ISOCHRONOUS_MAX_PACKET_SIZE 4096
115 
116 /*****************************************************************************
117  * Open: open the file
118  *****************************************************************************/
Open(vlc_object_t * p_this)119 static int Open( vlc_object_t *p_this )
120 {
121     stream_t     *p_access = (stream_t*)p_this;
122     access_sys_t *p_sys;
123 
124     struct raw1394_portinfo port_inf[ 16 ];
125 
126     msg_Dbg( p_access, "opening device" );
127 
128     /* Set up p_access */
129     ACCESS_SET_CALLBACKS( NULL, Block, Control, NULL );
130 
131     p_access->p_sys = p_sys = vlc_obj_malloc( p_this, sizeof( *p_sys ) );
132     if( !p_sys )
133         return VLC_EGENERIC;
134 
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;
144 
145     vlc_mutex_init( &p_sys->lock );
146 
147     p_sys->i_node = DiscoverAVC( p_access, &p_sys->i_port, p_sys->i_guid );
148     if( p_sys->i_node < 0 )
149     {
150         msg_Err( p_access, "failed to open a Firewire (IEEE1394) connection" );
151         Close( p_this );
152         return VLC_EGENERIC;
153     }
154 
155     p_sys->p_avc1394 = AVCOpen( p_access, p_sys->i_port );
156     if( !p_sys->p_avc1394 )
157     {
158         msg_Err( p_access, "no Digital Video Control device found" );
159         Close( p_this );
160         return VLC_EGENERIC;
161     }
162 
163     p_sys->p_raw1394 = raw1394_new_handle();
164     if( !p_sys->p_raw1394 )
165     {
166         msg_Err( p_access, "no Digital Video device found" );
167         Close( p_this );
168         return VLC_EGENERIC;
169     }
170 
171     p_sys->i_cards = raw1394_get_port_info( p_sys->p_raw1394, port_inf, 16 );
172     if( p_sys->i_cards < 0 )
173     {
174         msg_Err( p_access, "failed to get port info" );
175         Close( p_this );
176         return VLC_EGENERIC;
177     }
178 
179     if( raw1394_set_port( p_sys->p_raw1394, p_sys->i_port ) < 0 )
180     {
181         msg_Err( p_access, "failed to set port info" );
182         Close( p_this );
183         return VLC_EGENERIC;
184     }
185 
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 )
189     {
190         msg_Err( p_access, "failed to init isochronous recv" );
191         Close( p_this );
192         return VLC_EGENERIC;
193     }
194 
195     raw1394_set_userdata( p_sys->p_raw1394, p_access );
196     raw1394_iso_recv_start( p_sys->p_raw1394, -1, -1, 0 );
197 
198     p_sys->raw1394_poll.fd = raw1394_get_fd( p_sys->p_raw1394 );
199     p_sys->raw1394_poll.events = POLLIN | POLLPRI;
200 
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 )
204     {
205         msg_Err( p_access, "failed to create event thread struct" );
206         Close( p_this );
207         return VLC_ENOMEM;
208     }
209 
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 ) )
216     {
217         msg_Err( p_access, "failed to clone event thread" );
218         Close( p_this );
219         return VLC_EGENERIC;
220     }
221 
222     return VLC_SUCCESS;
223 }
224 
225 /*****************************************************************************
226  * Close: free unused data structures
227  *****************************************************************************/
Close(vlc_object_t * p_this)228 static void Close( vlc_object_t *p_this )
229 {
230     stream_t     *p_access = (stream_t*)p_this;
231     access_sys_t *p_sys = p_access->p_sys;
232 
233     if( p_sys->p_ev )
234     {
235         /* stop the event handler */
236         vlc_cancel( p_sys->p_ev->thread );
237 
238         if( p_sys->p_raw1394 )
239             raw1394_iso_shutdown( p_sys->p_raw1394 );
240 
241         vlc_join( p_sys->p_ev->thread, NULL );
242         vlc_mutex_destroy( &p_sys->p_ev->lock );
243 
244         /* Cleanup frame data */
245         if( p_sys->p_ev->p_frame )
246         {
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;
250         }
251         free( p_sys->p_ev );
252     }
253 
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 );
258 
259     AVCClose( p_access );
260 
261     vlc_mutex_destroy( &p_sys->lock );
262 }
263 
264 /*****************************************************************************
265  * Control:
266  *****************************************************************************/
Control(stream_t * p_access,int i_query,va_list args)267 static int Control( stream_t *p_access, int i_query, va_list args )
268 {
269     access_sys_t *sys = p_access->p_sys;
270 
271     switch( i_query )
272     {
273         /* */
274         case STREAM_CAN_PAUSE:
275             *va_arg( args, bool* ) = true;
276             break;
277 
278        case STREAM_CAN_SEEK:
279        case STREAM_CAN_FASTSEEK:
280        case STREAM_CAN_CONTROL_PACE:
281             *va_arg( args, bool* ) = false;
282             break;
283 
284         case STREAM_GET_PTS_DELAY:
285             *va_arg( args, int64_t * ) =
286                 INT64_C(1000) * var_InheritInteger( p_access, "live-caching" );
287             break;
288 
289         /* */
290         case STREAM_SET_PAUSE_STATE:
291             AVCPause( p_access, sys->i_node );
292             break;
293 
294         default:
295             return VLC_EGENERIC;
296 
297     }
298     return VLC_SUCCESS;
299 }
300 
Block(stream_t * p_access,bool * restrict eof)301 static block_t *Block( stream_t *p_access, bool *restrict eof )
302 {
303     access_sys_t *p_sys = p_access->p_sys;
304     block_t *p_block = NULL;
305 
306     vlc_mutex_lock( &p_sys->lock );
307     p_block = p_sys->p_frame;
308     //msg_Dbg( p_access, "sending frame %p", (void *)p_block );
309     p_sys->p_frame = NULL;
310     vlc_mutex_unlock( &p_sys->lock );
311 
312     (void) eof;
313     return p_block;
314 }
315 
Raw1394EventThreadCleanup(void * obj)316 static void Raw1394EventThreadCleanup( void *obj )
317 {
318     event_thread_t *p_ev = (event_thread_t *)obj;
319     access_sys_t *sys = p_ev->p_access->p_sys;
320 
321     AVCStop( p_ev->p_access, sys->i_node );
322 }
323 
Raw1394EventThread(void * obj)324 static void* Raw1394EventThread( void *obj )
325 {
326     event_thread_t *p_ev = (event_thread_t *)obj;
327     stream_t *p_access = (stream_t *) p_ev->p_access;
328     access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
329     int result = 0;
330     int canc = vlc_savecancel();
331 
332     AVCPlay( p_access, p_sys->i_node );
333     vlc_cleanup_push( Raw1394EventThreadCleanup, p_ev );
334     vlc_restorecancel( canc );
335 
336     for( ;; )
337     {
338         while( ( result = poll( &p_sys->raw1394_poll, 1, -1 ) ) < 0 )
339         {
340             if( errno != EINTR )
341                 msg_Err( p_access, "poll error: %s", vlc_strerror_c(errno) );
342         }
343 
344         if( result > 0 && ( ( p_sys->raw1394_poll.revents & POLLIN )
345                          || ( p_sys->raw1394_poll.revents & POLLPRI ) ) )
346         {
347             canc = vlc_savecancel();
348             result = raw1394_loop_iterate( p_sys->p_raw1394 );
349             vlc_restorecancel( canc );
350         }
351     }
352 
353     vlc_cleanup_pop();
354     vlc_assert_unreachable();
355 }
356 
357 static enum raw1394_iso_disposition
Raw1394Handler(raw1394handle_t handle,unsigned char * data,unsigned int length,unsigned char channel,unsigned char tag,unsigned char sy,unsigned int cycle,unsigned int dropped)358 Raw1394Handler(raw1394handle_t handle, unsigned char *data,
359         unsigned int length, unsigned char channel,
360         unsigned char tag, unsigned char sy, unsigned int cycle,
361         unsigned int dropped)
362 {
363     stream_t *p_access = NULL;
364     access_sys_t *p_sys = NULL;
365     block_t *p_block = NULL;
366     VLC_UNUSED(channel); VLC_UNUSED(tag);
367     VLC_UNUSED(sy); VLC_UNUSED(cycle); VLC_UNUSED(dropped);
368 
369     p_access = (stream_t *) raw1394_get_userdata( handle );
370     if( !p_access ) return 0;
371 
372     p_sys = p_access->p_sys;
373 
374     /* skip empty packets */
375     if( length > 16 )
376     {
377         unsigned char * p = data + 8;
378         int section_type = p[ 0 ] >> 5;           /* section type is in bits 5 - 7 */
379         int dif_sequence = p[ 1 ] >> 4;           /* dif sequence number is in bits 4 - 7 */
380         int dif_block = p[ 2 ];
381 
382         vlc_mutex_lock( &p_sys->p_ev->lock );
383 
384         /* if we are at the beginning of a frame, we put the previous
385            frame in our output_queue. */
386         if( (section_type == 0) && (dif_sequence == 0) )
387         {
388             vlc_mutex_lock( &p_sys->lock );
389             if( p_sys->p_ev->p_frame )
390             {
391                 /* Push current frame to p_access thread. */
392                 //p_sys->p_ev->p_frame->i_pts = mdate();
393                 block_ChainAppend( &p_sys->p_frame, p_sys->p_ev->p_frame );
394             }
395             /* reset list */
396             p_sys->p_ev->p_frame = block_Alloc( 144000 );
397             p_sys->p_ev->pp_last = &p_sys->p_frame;
398             vlc_mutex_unlock( &p_sys->lock );
399         }
400 
401         p_block = p_sys->p_ev->p_frame;
402         if( p_block )
403         {
404             switch ( section_type )
405             {
406             case 0:    /* 1 Header block */
407                 /* p[3] |= 0x80; // hack to force PAL data */
408                 memcpy( p_block->p_buffer + dif_sequence * 150 * 80, p, 480 );
409                 break;
410 
411             case 1:    /* 2 Subcode blocks */
412                 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 1 + dif_block ) * 80, p, 480 );
413                 break;
414 
415             case 2:    /* 3 VAUX blocks */
416                 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 3 + dif_block ) * 80, p, 480 );
417                 break;
418 
419             case 3:    /* 9 Audio blocks interleaved with video */
420                 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 6 + dif_block * 16 ) * 80, p, 480 );
421                 break;
422 
423             case 4:    /* 135 Video blocks interleaved with audio */
424                 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 7 + ( dif_block / 15 ) + dif_block ) * 80, p, 480 );
425                 break;
426 
427             default:    /* we can´t handle any other data */
428                 block_Release( p_block );
429                 p_block = NULL;
430                 break;
431             }
432         }
433 
434         vlc_mutex_unlock( &p_sys->p_ev->lock );
435     }
436     return 0;
437 }
438 
439 /*
440  * Routing borrowed from dvgrab-1.8
441  * Copyright by Arne Schirmacher <dvgrab@schirmacher.de>
442  * Dan Dennedy <dan@dennedy.org> and others
443  */
Raw1394GetNumPorts(stream_t * p_access)444 static int Raw1394GetNumPorts( stream_t *p_access )
445 {
446     int n_ports;
447     struct raw1394_portinfo pinf[ 16 ];
448     raw1394handle_t handle;
449 
450     /* get a raw1394 handle */
451     if( !( handle = raw1394_new_handle() ) )
452     {
453         msg_Err( p_access, "raw1394 - failed to get handle: %s",
454                  vlc_strerror_c(errno) );
455         return VLC_EGENERIC;
456     }
457 
458     if( ( n_ports = raw1394_get_port_info( handle, pinf, 16 ) ) < 0 )
459     {
460         msg_Err( p_access, "raw1394 - failed to get port info: %s",
461                  vlc_strerror_c(errno) );
462         raw1394_destroy_handle( handle );
463         return VLC_EGENERIC;
464     }
465     raw1394_destroy_handle( handle );
466 
467     return n_ports;
468 }
469 
Raw1394Open(stream_t * p_access,int port)470 static raw1394handle_t Raw1394Open( stream_t *p_access, int port )
471 {
472     int n_ports;
473     struct raw1394_portinfo pinf[ 16 ];
474     raw1394handle_t handle;
475 
476     /* get a raw1394 handle */
477     handle = raw1394_new_handle();
478     if( !handle )
479     {
480         msg_Err( p_access, "raw1394 - failed to get handle: %s",
481                  vlc_strerror_c(errno) );
482         return NULL;
483     }
484 
485     if( ( n_ports = raw1394_get_port_info( handle, pinf, 16 ) ) < 0 )
486     {
487         msg_Err( p_access, "raw1394 - failed to get port info: %s",
488                  vlc_strerror_c(errno) );
489         raw1394_destroy_handle( handle );
490         return NULL;
491     }
492 
493     /* tell raw1394 which host adapter to use */
494     if( raw1394_set_port( handle, port ) < 0 )
495     {
496         msg_Err( p_access, "raw1394 - failed to set set port: %s",
497                  vlc_strerror_c(errno) );
498         return NULL;
499     }
500 
501     return handle;
502 }
503 
Raw1394Close(raw1394handle_t handle)504 static void Raw1394Close( raw1394handle_t handle )
505 {
506     raw1394_destroy_handle( handle );
507 }
508 
DiscoverAVC(stream_t * p_access,int * port,uint64_t guid)509 static int DiscoverAVC( stream_t *p_access, int* port, uint64_t guid )
510 {
511     rom1394_directory rom_dir;
512     raw1394handle_t handle = NULL;
513     int device = -1;
514     int i, j = 0;
515     int m = Raw1394GetNumPorts( p_access );
516 
517     if( *port >= 0 )
518     {
519         /* search on explicit port */
520         j = *port;
521         m = *port + 1;
522     }
523 
524     for( ; j < m && device == -1; j++ )
525     {
526         handle = Raw1394Open( p_access, j );
527         if( !handle )
528             return -1;
529 
530         for( i = 0; i < raw1394_get_nodecount( handle ); ++i )
531         {
532             if( guid != 0 )
533             {
534                 /* select explicitly by GUID */
535                 if( guid == rom1394_get_guid( handle, i ) )
536                 {
537                     device = i;
538                     *port = j;
539                     break;
540                 }
541             }
542             else
543             {
544                 /* select first AV/C Tape Reccorder Player node */
545                 if( rom1394_get_directory( handle, i, &rom_dir ) < 0 )
546                 {
547                     msg_Err( p_access, "error reading config rom directory for node %d", i );
548                     continue;
549                 }
550                 if( ( rom1394_get_node_type( &rom_dir ) == ROM1394_NODE_TYPE_AVC ) &&
551                         avc1394_check_subunit_type( handle, i, AVC1394_SUBUNIT_TYPE_VCR ) )
552                 {
553                     device = i;
554                     *port = j;
555                     break;
556                 }
557             }
558         }
559         Raw1394Close( handle );
560     }
561 
562     return device;
563 }
564 
565 /*
566  * Handle AVC commands
567  */
AVCOpen(stream_t * p_access,int port)568 static raw1394handle_t AVCOpen( stream_t *p_access, int port )
569 {
570     access_sys_t *p_sys = p_access->p_sys;
571     struct raw1394_portinfo pinf[ 16 ];
572     int numcards;
573 
574     p_sys->p_avc1394 = raw1394_new_handle();
575     if( !p_sys->p_avc1394 )
576         return NULL;
577 
578     numcards = raw1394_get_port_info( p_sys->p_avc1394, pinf, 16 );
579     if( numcards < -1 )
580         return NULL;
581     if( raw1394_set_port( p_sys->p_avc1394, port ) < 0 )
582         return NULL;
583 
584     raw1394_set_bus_reset_handler( p_sys->p_avc1394, AVCResetHandler );
585 
586     return  p_sys->p_avc1394;
587 }
588 
AVCClose(stream_t * p_access)589 static void AVCClose( stream_t *p_access )
590 {
591     access_sys_t *p_sys = p_access->p_sys;
592 
593     if( p_sys->p_avc1394 )
594     {
595         raw1394_destroy_handle( p_sys->p_avc1394 );
596         p_sys->p_avc1394 = NULL;
597     }
598 }
599 
AVCResetHandler(raw1394handle_t handle,unsigned int generation)600 static int AVCResetHandler( raw1394handle_t handle, unsigned int generation )
601 {
602     raw1394_update_generation( handle, generation );
603     return 0;
604 }
605 
AVCPlay(stream_t * p_access,int phyID)606 static int AVCPlay( stream_t *p_access, int phyID )
607 {
608     access_sys_t *p_sys = p_access->p_sys;
609 
610     msg_Dbg( p_access, "send play command over Digital Video control channel" );
611 
612     if( p_sys->p_avc1394 && phyID >= 0 )
613     {
614         if( !avc1394_vcr_is_recording( p_sys->p_avc1394, phyID ) &&
615             avc1394_vcr_is_playing( p_sys->p_avc1394, phyID ) != AVC1394_VCR_OPERAND_PLAY_FORWARD )
616                 avc1394_vcr_play( p_sys->p_avc1394, phyID );
617     }
618     return 0;
619 }
620 
AVCPause(stream_t * p_access,int phyID)621 static int AVCPause( stream_t *p_access, int phyID )
622 {
623     access_sys_t *p_sys = p_access->p_sys;
624 
625     if( p_sys->p_avc1394 && phyID >= 0 )
626     {
627         if( !avc1394_vcr_is_recording( p_sys->p_avc1394, phyID ) &&
628             ( avc1394_vcr_is_playing( p_sys->p_avc1394, phyID ) != AVC1394_VCR_OPERAND_PLAY_FORWARD_PAUSE ) )
629                 avc1394_vcr_pause( p_sys->p_avc1394, phyID );
630     }
631     return 0;
632 }
633 
634 
AVCStop(stream_t * p_access,int phyID)635 static int AVCStop( stream_t *p_access, int phyID )
636 {
637     access_sys_t *p_sys = p_access->p_sys;
638 
639     msg_Dbg( p_access, "closing Digital Video control channel" );
640 
641     if ( p_sys->p_avc1394 && phyID >= 0 )
642         avc1394_vcr_stop( p_sys->p_avc1394, phyID );
643 
644     return 0;
645 }
646