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