]> git.sesse.net Git - vlc/blob - modules/access/dc1394.c
bda: fix bandwidth labels
[vlc] / modules / access / dc1394.c
1 /*****************************************************************************
2  * dc1394.c: firewire input module
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  *
6  * Authors: Xant Majere <xant@xant.net>
7  *
8  *****************************************************************************
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; 
12  * version 2 of the License.
13  *
14  * This library 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 GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #include <vlc/vlc.h>
30 #include <vlc_input.h>
31 #include <vlc_vout.h>
32 #include <vlc_demux.h>
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #ifdef HAVE_FCNTL_H
39 #   include <fcntl.h>
40 #endif
41 #ifdef HAVE_UNISTD_H
42 #   include <unistd.h>
43 #elif defined( WIN32 ) && !defined( UNDER_CE )
44 #   include <io.h>
45 #endif
46
47 #include <sys/ioctl.h>
48 #include <sys/soundcard.h>
49
50 #include <libraw1394/raw1394.h>
51 #include <libdc1394/dc1394_control.h>
52
53 #define MAX_IEEE1394_HOSTS 32
54 #define MAX_CAMERA_NODES 32
55
56 /*****************************************************************************
57  * Module descriptor
58  *****************************************************************************/
59 static int  Open ( vlc_object_t * );
60 static void Close( vlc_object_t * );
61 static void OpenAudioDev( demux_t *p_demux );
62 static inline void CloseAudioDev( demux_t *p_demux );
63
64 vlc_module_begin();
65     set_description( _("dc1394 input") );
66     set_capability( "access_demux", 10 );
67     add_shortcut( "dc1394" );
68     set_callbacks( Open, Close );
69 vlc_module_end();
70
71 typedef struct __dc_camera
72 {
73     int port;
74     nodeid_t node;
75     u_int64_t uid;
76 } dc_camera;
77
78 typedef struct demux_sys_t
79 {
80     dc1394_cameracapture camera;
81     picture_t            pic;
82     int                  dma_capture;
83 #define DMA_OFF 0
84 #define DMA_ON 1
85     int                 num_ports;
86     int                 num_cameras;
87     int                 selected_camera;
88     u_int64_t           selected_uid;
89
90     dc_camera           *camera_nodes;
91     dc1394_camerainfo   camera_info;
92     dc1394_miscinfo     misc_info;
93     raw1394handle_t     fd_video;
94     quadlet_t           supported_framerates;
95
96     int                 width;
97     int                 height;
98     int                 frame_size;
99     int                 frame_rate;
100     unsigned int        brightness;
101     unsigned int        focus;
102     char                *dma_device;
103     es_out_id_t         *p_es_video;
104
105     /* audio stuff */
106     int                 i_sample_rate;
107     int                 channels;
108     int                 i_audio_max_frame_size;
109     int                 fd_audio;
110     char                *audio_device;
111 #define NO_ROTATION 0
112 #define ROTATION_LEFT 1
113 #define ROTATION_RIGHT 2
114     es_out_id_t         *p_es_audio;
115 } dc1394_sys;
116
117 /*****************************************************************************
118  * Local prototypes
119  *****************************************************************************/
120 static int Demux( demux_t *p_demux );
121 static int Control( demux_t *, int, va_list );
122 static block_t *GrabVideo( demux_t *p_demux );
123 static block_t *GrabAudio( demux_t *p_demux );
124 static int process_options( demux_t *p_demux);
125
126 /*****************************************************************************
127  * ScanCameras
128  *****************************************************************************/
129 static void ScanCameras( dc1394_sys *sys, demux_t *p_demux )
130 {
131     struct raw1394_portinfo portinfo[MAX_IEEE1394_HOSTS];
132     raw1394handle_t tempFd;
133     dc1394_camerainfo  info;
134     dc_camera *node_list = NULL;
135     nodeid_t  *nodes = NULL;
136     int num_ports = 0;
137     int num_cameras = 0;
138     int nodecount;
139     int i, n;
140
141     memset( &portinfo, 0, sizeof(portinfo) );
142
143     msg_Dbg( p_demux, "Scanning for ieee1394 ports ..." );
144
145     tempFd = raw1394_new_handle();
146     if( !tempFd )
147         return VLC_EGENERIC;
148     raw1394_get_port_info( tempFd, portinfo, MAX_IEEE1394_HOSTS);
149     raw1394_destroy_handle( tempFd );
150
151     for( i=0; i < MAX_IEEE1394_HOSTS; i++ )
152     {
153         /* check if port exists and has at least one node*/
154         if( !portinfo[i].nodes )
155             continue;
156         nodes = NULL;
157         nodecount = 0;
158         tempFd = dc1394_create_handle( i );
159
160         /* skip this port if we can't obtain a valid handle */
161         if( !tempFd )
162             continue;
163         msg_Dbg( p_demux, "Found ieee1394 port %d (%s) ... "
164                           "checking for camera nodes",
165                           i, portinfo[i].name );
166         num_ports++;
167
168         nodes = dc1394_get_camera_nodes( tempFd, &nodecount, 0 );
169         if( nodecount )
170         {
171             msg_Dbg( p_demux, "Found %d dc1394 cameras on port %d (%s)",
172                      nodecount, i, portinfo[i].name );
173
174             if( node_list )
175                 node_list = realloc( node_list, sizeof(dc_camera) * (num_cameras+nodecount) );
176             else
177                 node_list = malloc( sizeof(dc_camera) * nodecount);
178
179             for( n = 0; n < nodecount; n++ )
180             {
181                 int result = 0;
182
183                 result = dc1394_get_camera_info( tempFd, nodes[n], &info );
184                 if( result == DC1394_SUCCESS )
185                 {
186                     node_list[num_cameras+n].uid = info.euid_64;
187                 }
188                 node_list[num_cameras+n].node = nodes[n];
189                 node_list[num_cameras+n].port = i;
190             }
191             num_cameras += nodecount;
192         }
193         else
194             msg_Dbg( p_demux, "no cameras found  on port %d (%s)",
195                      i, portinfo[i].name );
196
197         if( tempFd )
198             dc1394_destroy_handle( tempFd );
199     }
200     sys->num_ports = num_ports;
201     sys->num_cameras = num_cameras;
202     sys->camera_nodes = node_list;
203 }
204
205 /*****************************************************************************
206  * Open:
207  *****************************************************************************/
208 static int Open( vlc_object_t *p_this )
209 {
210     demux_t     *p_demux = (demux_t*)p_this;
211     demux_sys_t *p_sys;
212     es_format_t fmt;
213     int i;
214     int i_width;
215     int i_height;
216     int i_aspect;
217     int result = 0;
218
219     /* Set up p_demux */
220     p_demux->pf_demux = Demux;
221     p_demux->pf_control = Control;
222     p_demux->info.i_update = 0;
223     p_demux->info.i_title = 0;
224     p_demux->info.i_seekpoint = 0;
225
226     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
227     if( !p_sys )
228     {
229         msg_Err( p_demux, "not enough memory available" );
230         return VLC_ENOMEM;
231     }
232     memset( p_sys, 0, sizeof( demux_sys_t ) );
233     memset( &fmt, 0, sizeof( es_format_t ) );
234
235     /* DEFAULTS */
236     p_sys->frame_size = MODE_640x480_YUV422;
237     p_sys->width      = 640;
238     p_sys->height     = 480;
239     p_sys->frame_rate = FRAMERATE_30;
240     p_sys->brightness = 200;
241     p_sys->focus      = 0;
242     p_sys->dma_capture = DMA_ON; /* defaults to VIDEO1394 capture mode */
243     p_sys->fd_audio   = -1;
244     p_sys->fd_video   = NULL;
245     p_sys->camera_nodes = NULL;
246     p_sys->selected_camera = 0;
247     p_sys->dma_device = NULL;
248     p_sys->selected_uid = 0;
249
250     /* PROCESS INPUT OPTIONS */
251     if( process_options(p_demux) != VLC_SUCCESS )
252     {
253         msg_Err( p_demux, "Bad MRL, please check the option line "
254                           "(MRL was: %s)",
255                           p_demux->psz_path );
256         free( p_sys );
257         p_demux->p_sys = NULL;
258         return VLC_EGENERIC;
259     }
260
261     msg_Dbg( p_demux, "Selected camera %d", p_sys->selected_camera );
262     msg_Dbg( p_demux, "Selected uid 0x%llx", p_sys->selected_uid );
263
264     ScanCameras( p_sys, p_demux );
265     if( !p_sys->camera_nodes )
266     {
267         msg_Err( p_demux, "No camera found !!" );
268         free( p_sys );
269         p_demux->p_sys = NULL;
270         return VLC_EGENERIC;
271     }
272
273     if( p_sys->selected_uid )
274     {
275         int found = 0;
276         for( i=0; i < p_sys->num_cameras; i++ )
277         {
278             if( p_sys->camera_nodes[i].uid == p_sys->selected_uid )
279             {
280                 p_sys->selected_camera = i;
281                 found++;
282                 break;
283             }
284         }
285         if( !found )
286         {
287             msg_Err( p_demux, "Can't find camera with uid : 0x%llx.",
288                      p_sys->selected_uid );
289             Close( p_this );
290             return VLC_EGENERIC;
291         }
292     }
293     else if( p_sys->selected_camera >= p_sys->num_cameras )
294     {
295         msg_Err( p_demux, "there are not this many cameras. (%d/%d)",
296                           p_sys->selected_camera, p_sys->num_cameras );
297         Close( p_this );
298         return VLC_EGENERIC;
299     }
300
301     p_sys->fd_video = dc1394_create_handle(
302                         p_sys->camera_nodes[p_sys->selected_camera].port );
303     if( (int)p_sys->fd_video < 0 )
304     {
305         msg_Err( p_demux, "Can't init dc1394 handle" );
306         Close( p_this );
307         return VLC_EGENERIC;
308     }
309
310     /* get camera info */
311     result = dc1394_get_camera_info( p_sys->fd_video,
312                         p_sys->camera_nodes[p_sys->selected_camera].node,
313                         &p_sys->camera_info );
314     if( result != DC1394_SUCCESS )
315     {
316         msg_Err( p_demux ,"unable to get camera info" );
317         Close( p_this );
318         return VLC_EGENERIC;
319     }
320
321     dc1394_print_camera_info( &p_sys->camera_info );
322     result = dc1394_get_camera_misc_info( p_sys->fd_video,
323                         p_sys->camera_nodes[p_sys->selected_camera].node,
324                         &p_sys->misc_info );
325     if( result != DC1394_SUCCESS )
326     {
327         msg_Err( p_demux, "unable to get camera misc info" );
328         Close( p_this );
329         return VLC_EGENERIC;
330     }
331
332     /* init camera and set some video options  */
333     result = dc1394_init_camera( p_sys->camera_info.handle,
334                                  p_sys->camera_info.id );
335     if( result != DC1394_SUCCESS )
336     {
337         msg_Err( p_demux, "unable to get init dc1394 camera" );
338         Close( p_this );
339         return VLC_EGENERIC;
340     }
341
342     if( p_sys->focus )
343     {
344         result = dc1394_set_focus( p_sys->camera_info.handle,
345                         p_sys->camera_nodes[p_sys->selected_camera].node,
346                         p_sys->focus );
347         if( result != DC1394_SUCCESS )
348         {
349             msg_Err( p_demux, "unable to set initial focus to %u",
350                      p_sys->focus );
351         }
352         msg_Dbg( p_demux, "Initial focus set to %u", p_sys->focus );
353     }
354
355     result = dc1394_set_brightness( p_sys->camera_info.handle,
356                         p_sys->camera_nodes[p_sys->selected_camera].node,
357                         p_sys->brightness );
358     if( result != DC1394_SUCCESS )
359     {
360         msg_Err( p_demux, "unable to set init brightness to %d",
361                  p_sys->brightness);
362         Close( p_this );
363         return VLC_EGENERIC;
364     }
365
366     result = dc1394_set_video_framerate( p_sys->camera_info.handle,
367                         p_sys->camera_nodes[p_sys->selected_camera].node,
368                         p_sys->frame_rate );
369     if( result != DC1394_SUCCESS )
370     {
371         msg_Err( p_demux, "unable to set framerate to %d",
372                  p_sys->frame_rate );
373         Close( p_this );
374         return VLC_EGENERIC;
375     }
376     p_sys->misc_info.framerate = p_sys->frame_rate;
377
378     result = dc1394_set_video_format( p_sys->camera_info.handle,
379                         p_sys->camera_nodes[p_sys->selected_camera].node,
380                         FORMAT_VGA_NONCOMPRESSED );
381     if( result != DC1394_SUCCESS )
382     {
383         msg_Err( p_demux, "unable to set video format to VGA_NONCOMPRESSED" );
384         Close( p_this );
385         return VLC_EGENERIC;
386     }
387     p_sys->misc_info.format = FORMAT_VGA_NONCOMPRESSED;
388
389     result = dc1394_set_video_mode( p_sys->camera_info.handle,
390                         p_sys->camera_nodes[p_sys->selected_camera].node,
391                         p_sys->frame_size );
392     if( result != DC1394_SUCCESS )
393     {
394         msg_Err( p_demux, "unable to set video mode" );
395         Close( p_this );
396         return VLC_EGENERIC;
397     }
398     p_sys->misc_info.mode = p_sys->frame_size;
399
400     /* reprobe everything */
401     result = dc1394_get_camera_info( p_sys->camera_info.handle,
402                                      p_sys->camera_info.id,
403                                      &p_sys->camera_info );
404     if( result != DC1394_SUCCESS )
405     {
406         msg_Err( p_demux, "Could not get camera basic information!" );
407         Close( p_this );
408         return VLC_EGENERIC;
409     }
410
411     result = dc1394_get_camera_misc_info( p_sys->camera_info.handle,
412                                           p_sys->camera_info.id,
413                                           &p_sys->misc_info );
414     if( result != DC1394_SUCCESS )
415     {
416         msg_Err( p_demux, "Could not get camera misc information!" );
417         Close( p_this );
418         return VLC_EGENERIC;
419     }
420
421     /* set iso_channel */
422     result = dc1394_set_iso_channel_and_speed( p_sys->camera_info.handle,
423                                                p_sys->camera_info.id,
424                                                p_sys->selected_camera,
425                                                SPEED_400 );
426     if( result != DC1394_SUCCESS )
427     {
428         msg_Err( p_demux, "Could not set iso channel!" );
429         Close( p_this );
430         return VLC_EGENERIC;
431     }
432     msg_Dbg( p_demux, "Using ISO channel %d", p_sys->misc_info.iso_channel );
433     p_sys->misc_info.iso_channel = p_sys->selected_camera;
434
435     /* and setup capture */
436     if( p_sys->dma_capture )
437     {
438         result = dc1394_dma_setup_capture( p_sys->camera_info.handle,
439                         p_sys->camera_info.id,
440                         p_sys->misc_info.iso_channel,
441                         p_sys->misc_info.format,
442                         p_sys->misc_info.mode,
443                         SPEED_400,
444                         p_sys->misc_info.framerate,
445                         10, 0,
446                         p_sys->dma_device,
447                         &p_sys->camera );
448         if( result != DC1394_SUCCESS )
449         {
450             msg_Err( p_demux ,"unable to setup camera" );
451             Close( p_this );
452             return VLC_EGENERIC;
453         }
454     }
455     else
456     {
457         result = dc1394_setup_capture( p_sys->camera_info.handle,
458                     p_sys->camera_info.id,
459                     p_sys->misc_info.iso_channel,
460                     p_sys->misc_info.format,
461                     p_sys->misc_info.mode,
462                     SPEED_400,
463                     p_sys->misc_info.framerate,
464                     &p_sys->camera );
465         if( result != DC1394_SUCCESS)
466         {
467             msg_Err( p_demux ,"unable to setup camera" );
468             Close( p_this );
469             return VLC_EGENERIC;
470         }
471     }
472
473     /* TODO - UYV444 chroma converter is missing, when it will be available
474      * fourcc will become variable (and not just a fixed value for UYVY)
475      */
476     i_width = p_sys->camera.frame_width;
477     i_height = p_sys->camera.frame_height;
478
479     i_aspect = vout_InitPicture( VLC_OBJECT(p_demux), &p_sys->pic,
480                     VLC_FOURCC('U', 'Y', 'V', 'Y'),
481                     i_width, i_height,
482                     i_width * VOUT_ASPECT_FACTOR / i_height );
483
484     es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC('U', 'Y', 'V', 'Y') );
485
486     fmt.video.i_width = i_width;
487     fmt.video.i_height = i_height;
488
489     msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
490              (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
491
492     p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );
493
494     if( p_sys->audio_device )
495     {
496         OpenAudioDev( p_demux );
497         if( p_sys->fd_audio >= 0 )
498         {
499             es_format_t fmt;
500             es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC('a','r','a','w') );
501
502             fmt.audio.i_channels = p_sys->channels ? p_sys->channels : 1;
503             fmt.audio.i_rate = p_sys->i_sample_rate;
504             fmt.audio.i_bitspersample = 16; /* FIXME: hmm, ?? */
505             fmt.audio.i_blockalign = fmt.audio.i_channels *
506                                      fmt.audio.i_bitspersample / 8;
507             fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate *
508                             fmt.audio.i_bitspersample;
509
510             msg_Dbg( p_demux, "new audio es %d channels %dHz",
511             fmt.audio.i_channels, fmt.audio.i_rate );
512
513             p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
514         }
515     }
516
517     /* have the camera start sending us data */
518     result = dc1394_start_iso_transmission( p_sys->camera_info.handle,
519                                             p_sys->camera_info.id );
520     if( result != DC1394_SUCCESS )
521     {
522         msg_Err( p_demux, "unable to start camera iso transmission" );
523         if( p_sys->dma_capture )
524         {
525             dc1394_dma_release_camera( p_sys->fd_video, &p_sys->camera );
526         }
527         else
528         {
529             dc1394_release_camera( p_sys->fd_video, &p_sys->camera );
530         }
531         Close( p_this );
532         return VLC_EGENERIC;
533     }
534     p_sys->misc_info.is_iso_on = DC1394_TRUE;
535     return VLC_SUCCESS;
536 }
537
538 static void OpenAudioDev( demux_t *p_demux )
539 {
540     demux_sys_t *p_sys = p_demux->p_sys;
541     char *psz_device = p_sys->audio_device;
542     int i_format = AFMT_S16_LE;
543     int result;
544
545     p_sys->fd_audio = open( psz_device, O_RDONLY | O_NONBLOCK );
546     if( p_sys->fd_audio  < 0 )
547     {
548         msg_Err( p_demux, "cannot open audio device (%s)", psz_device );
549         CloseAudioDev( p_demux );
550     }
551
552     if( !p_sys->i_sample_rate )
553         p_sys->i_sample_rate = 44100;
554
555     result = ioctl( p_sys->fd_audio, SNDCTL_DSP_SETFMT, &i_format );
556     if( (result  < 0) || (i_format != AFMT_S16_LE) )
557     {
558         msg_Err( p_demux, "cannot set audio format (16b little endian) "
559                           "(%d)", i_format );
560         CloseAudioDev( p_demux );
561     }
562
563     result = ioctl( p_sys->fd_audio, SNDCTL_DSP_CHANNELS, &p_sys->channels );
564     if( result < 0 )
565     {
566         msg_Err( p_demux, "cannot set audio channels count (%d)",
567                  p_sys->channels );
568         CloseAudioDev( p_demux );
569     }
570
571     result = ioctl( p_sys->fd_audio, SNDCTL_DSP_SPEED, &p_sys->i_sample_rate );
572     if( result < 0 )
573     {
574         msg_Err( p_demux, "cannot set audio sample rate (%s)", p_sys->i_sample_rate );
575         CloseAudioDev( p_demux );
576     }
577
578     msg_Dbg( p_demux, "openened adev=`%s' %s %dHz",
579              psz_device,
580              (p_sys->channels > 1) ? "stereo" : "mono",
581              p_sys->i_sample_rate );
582
583     p_sys->i_audio_max_frame_size = 32 * 1024;
584 }
585
586 static inline void CloseAudioDev( demux_t *p_demux )
587 {
588     demux_sys_t *p_sys = NULL;
589
590     if( p_demux )
591     {
592         p_sys = p_demux->p_sys;
593         if( p_sys->fd_audio >= 0 )
594             close( p_sys->fd_audio );
595     }
596 }
597
598 /*****************************************************************************
599  * Close:
600  *****************************************************************************/
601 static void Close( vlc_object_t *p_this )
602 {
603     demux_t     *p_demux = (demux_t*)p_this;
604     demux_sys_t *p_sys = p_demux->p_sys;
605     int result = 0;
606
607     /* Stop data transmission */
608     result = dc1394_stop_iso_transmission( p_sys->fd_video,
609                                            p_sys->camera.node );
610     if( result != DC1394_SUCCESS )
611     {
612         msg_Err( p_demux, "couldn't stop the camera" );
613     }
614
615     /* Close camera */
616     if( p_sys->dma_capture )
617     {
618         dc1394_dma_unlisten( p_sys->fd_video, &p_sys->camera );
619         dc1394_dma_release_camera( p_sys->fd_video, &p_sys->camera );
620     }
621     else
622     {
623         dc1394_release_camera( p_sys->fd_video, &p_sys->camera );
624     }
625
626     if( p_sys->fd_video )
627         dc1394_destroy_handle( p_sys->fd_video );
628     CloseAudioDev( p_demux );
629
630     if( p_sys->camera_nodes )
631         free( p_sys->camera_nodes );
632     if( p_sys->audio_device )
633         free( p_sys->audio_device );
634
635     free( p_sys );
636 }
637
638 static void MovePixelUYVY( void *src, int spos, void *dst, int dpos )
639 {
640     char u,v,y;
641     u_char  *sc;
642     u_char  *dc;
643
644     sc = (u_char *)src + (spos*2);
645     if( spos % 2 )
646     {
647         v = sc[0];
648         y = sc[1];
649         u = *(sc -2);
650     }
651     else
652     {
653         u = sc[0];
654         y = sc[1];
655         v = sc[2];
656     }
657     dc = (u_char *)dst+(dpos*2);
658     if( dpos % 2 )
659     {
660         dc[0] = v;
661         dc[1] = y;
662     }
663     else
664     {
665         dc[0] = u;
666         dc[1] = y;
667     }
668 }
669
670 /*****************************************************************************
671  * Demux:
672  *****************************************************************************/
673 static block_t *GrabVideo( demux_t *p_demux )
674 {
675     demux_sys_t *p_sys = p_demux->p_sys;
676     block_t     *p_block = NULL;
677     int result = 0;
678
679     if( p_sys->dma_capture )
680     {
681         result = dc1394_dma_single_capture( &p_sys->camera );
682         if( result != DC1394_SUCCESS )
683         {
684             msg_Err( p_demux, "unable to capture a frame" );
685             return NULL;
686         }
687     }
688     else
689     {
690         result = dc1394_single_capture( p_sys->camera_info.handle,
691                                         &p_sys->camera );
692         if( result != DC1394_SUCCESS )
693         {
694             msg_Err( p_demux, "unable to capture a frame" );
695             return NULL;
696         }
697     }
698
699     p_block = block_New( p_demux, p_sys->camera.frame_width *
700                                   p_sys->camera.frame_height * 2 );
701     if( !p_block )
702     {
703         msg_Err( p_demux, "cannot get block" );
704         return NULL;
705     }
706
707     if( !p_sys->camera.capture_buffer )
708     {
709         msg_Err (p_demux, "caputer buffer empty");
710         block_Release( p_block );
711         return NULL;
712     }
713
714     memcpy( p_block->p_buffer, (const char *)p_sys->camera.capture_buffer,
715             p_sys->camera.frame_width * p_sys->camera.frame_height * 2 );
716
717     p_block->i_pts = p_block->i_dts = mdate();
718     if( p_sys->dma_capture )
719         dc1394_dma_done_with_buffer( &p_sys->camera );
720     return p_block;
721 }
722
723 static block_t *GrabAudio( demux_t *p_demux )
724 {
725     demux_sys_t *p_sys = p_demux->p_sys;
726     struct audio_buf_info buf_info;
727     block_t *p_block = NULL;
728     int i_read = 0;
729     int i_correct = 0;
730     int result = 0;
731
732     p_block = block_New( p_demux, p_sys->i_audio_max_frame_size );
733     if( !p_block )
734     {
735         msg_Warn( p_demux, "cannot get buffer" );
736         return NULL;
737     }
738
739     i_read = read( p_sys->fd_audio, p_block->p_buffer,
740                    p_sys->i_audio_max_frame_size );
741
742     if( i_read <= 0 )
743         return NULL;
744
745     p_block->i_buffer = i_read;
746
747     /* Correct the date because of kernel buffering */
748     i_correct = i_read;
749     result = ioctl( p_sys->fd_audio, SNDCTL_DSP_GETISPACE, &buf_info );
750     if( result == 0 )
751         i_correct += buf_info.bytes;
752
753     p_block->i_pts = p_block->i_dts =
754                         mdate() - I64C(1000000) * (mtime_t)i_correct /
755                         2 / p_sys->channels / p_sys->i_sample_rate;
756     return p_block;
757 }
758
759 static int Demux( demux_t *p_demux )
760 {
761     demux_sys_t *p_sys = p_demux->p_sys;
762     block_t *p_blocka = NULL;
763     block_t *p_blockv = NULL;
764
765     /* Try grabbing audio frames first */
766     if( p_sys->fd_audio > 0 )
767         p_blocka = GrabAudio( p_demux );
768
769     /* Try grabbing video frame */
770     if( (int)p_sys->fd_video > 0 )
771         p_blockv = GrabVideo( p_demux );
772
773     if( !p_blocka && !p_blockv )
774     {
775         /* Sleep so we do not consume all the cpu, 10ms seems
776          * like a good value (100fps)
777          */
778         msleep( 10000 );
779         return 1;
780     }
781
782     if( p_blocka )
783     {
784         es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_blocka->i_pts );
785         es_out_Send( p_demux->out, p_sys->p_es_audio, p_blocka );
786     }
787
788     if( p_blockv )
789     {
790         es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_blockv->i_pts );
791         es_out_Send( p_demux->out, p_sys->p_es_video, p_blockv );
792     }
793     return 1;
794 }
795
796 /*****************************************************************************
797  * Control:
798  *****************************************************************************/
799 static int Control( demux_t *p_demux, int i_query, va_list args )
800 {
801     vlc_bool_t *pb;
802     int64_t    *pi64;
803
804     switch( i_query )
805     {
806         /* Special for access_demux */
807         case DEMUX_CAN_PAUSE:
808         case DEMUX_SET_PAUSE_STATE:
809         case DEMUX_CAN_CONTROL_PACE:
810             pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
811             *pb = VLC_FALSE;
812             return VLC_SUCCESS;
813
814         case DEMUX_GET_PTS_DELAY:
815             pi64 = (int64_t*)va_arg( args, int64_t * );
816             *pi64 = (int64_t)DEFAULT_PTS_DELAY;
817             return VLC_SUCCESS;
818
819         case DEMUX_GET_TIME:
820             pi64 = (int64_t*)va_arg( args, int64_t * );
821             *pi64 = mdate();
822             return VLC_SUCCESS;
823
824         /* TODO implement others */
825         default:
826             return VLC_EGENERIC;
827     }
828     return VLC_EGENERIC;
829 }
830
831 static int process_options( demux_t *p_demux )
832 {
833     demux_sys_t *p_sys = p_demux->p_sys;
834     char *psz_dup; 
835     char *psz_parser;
836     char *token = NULL;
837     char *state = NULL;
838     float rate_f;
839
840     if( strncmp(p_demux->psz_access, "dc1394", 6) != 0 )
841         return VLC_EGENERIC;
842
843     psz_dup = strdup( p_demux->psz_path );
844     psz_parser = psz_dup;
845     for( token = strtok_r( psz_parser,":",&state); token;
846          token = strtok_r( NULL, ":", &state ) )
847     {
848         if( strncmp( token, "size=", strlen("size=") ) == 0 )
849         {
850             token += strlen("size=");
851             if( strncmp( token, "160x120", 7 ) == 0 )
852             {
853                 /* TODO - UYV444 chroma converter is needed ...
854                     * video size of 160x120 is temporarily disabled
855                     */
856                 msg_Err( p_demux,
857                     "video size of 160x120 is actually disabled for lack of chroma "
858                     "support. It will relased ASAP, until then try an higher size "
859                     "(320x240 and 640x480 are fully supported)" );
860                 free(psz_dup);
861                 return VLC_EGENERIC;
862 #if 0
863                 p_sys->frame_size = MODE_160x120_YUV444;
864                 p_sys->width = 160;
865                 p_sys->height = 120;
866 #endif
867             }
868             else if( strncmp( token, "320x240", 7 ) == 0 )
869             {
870                 p_sys->frame_size = MODE_320x240_YUV422;
871                 p_sys->width = 320;
872                 p_sys->height = 240;
873             }
874             else if( strncmp( token, "640x480", 7 ) == 0 )
875             {
876                 p_sys->frame_size = MODE_640x480_YUV422;
877                 p_sys->width = 640;
878                 p_sys->height = 480;
879             }
880             else
881             {
882                 msg_Err( p_demux,
883                     "This program currently suppots frame sizes of"
884                     " 160x120, 320x240, and 640x480. "
885                     "Please specify one of them. You have specified %s.",
886                     token );
887                 free(psz_dup);
888                 return VLC_EGENERIC;
889             }
890             msg_Dbg( p_demux, "Requested video size : %s",token );
891         }
892         else if( strncmp( token, "fps=", strlen( "fps=" ) ) == 0 )
893         {
894             token += strlen("fps=");
895             sscanf( token, "%g", &rate_f );
896             if( rate_f == 1.875 )
897                 p_sys->frame_rate = FRAMERATE_1_875;
898             else if( rate_f == 3.75 )
899                 p_sys->frame_rate = FRAMERATE_3_75;
900             else if( rate_f == 7.5 )
901                 p_sys->frame_rate = FRAMERATE_7_5;
902             else if( rate_f == 15 )
903                 p_sys->frame_rate = FRAMERATE_15;
904             else if( rate_f == 30 )
905                 p_sys->frame_rate = FRAMERATE_30;
906             else if( rate_f == 60 )
907                 p_sys->frame_rate = FRAMERATE_60;
908             else
909             {
910                 msg_Err( p_demux ,
911                     "This program supports framerates of"
912                     " 1.875, 3.75, 7.5, 15, 30, 60. "
913                     "Please specify one of them. You have specified %s.",
914                     token);
915                 free(psz_dup);
916                 return VLC_EGENERIC;
917             }
918             msg_Dbg( p_demux, "Requested frame rate : %s",token );
919         }
920         else if( strncmp( token, "brightness=", strlen( "brightness=" ) ) == 0 )
921         {
922             int nr = 0;
923             token += strlen("brightness=");
924             nr = sscanf( token, "%u", &p_sys->brightness);
925             if( nr != 1 )
926             {
927                 msg_Err( p_demux, "Bad brightness value '%s', "
928                                   "must be an unsigned integer.",
929                                   token );
930                 free(psz_dup);
931                 return VLC_EGENERIC;
932             }
933         }
934 #if 0
935         else if( strncmp( token, "controller=", strlen( "controller=" ) ) == 0 )
936         {
937             int nr = 0;
938             token += strlen("controller=");
939             nr = sscanf( token, "%u", &p_sys->controller );
940             if( nr != 1)
941             {
942                 msg_Err(p_demux, "Bad controller value '%s', "
943                                  "must be an unsigned integer.",
944                                  token );
945                 return VLC_EGENERIC;
946             }
947         }
948 #endif
949         else if( strncmp( token, "camera=", strlen( "camera=" ) ) == 0 )
950         {
951             int nr = 0;
952             token += strlen("camera=");
953             nr = sscanf(token,"%u",&p_sys->selected_camera);
954             if( nr != 1)
955             {
956                 msg_Err( p_demux, "Bad camera number '%s', "
957                                   "must be an unsigned integer.",
958                                   token );
959                 free(psz_dup);
960                 return VLC_EGENERIC;
961             }
962         }
963         else if( strncmp( token, "capture=", strlen( "capture=" ) ) == 0)
964         {
965             token += strlen("capture=");
966             if( strncmp(token, "raw1394",7) == 0 )
967             {
968                 msg_Dbg( p_demux, "DMA capture disabled!" );
969                 p_sys->dma_capture = DMA_OFF;
970             }
971             else if( strncmp(token,"video1394",9) == 0 )
972             {
973                 msg_Dbg( p_demux, "DMA capture enabled!" );
974                 p_sys->dma_capture = DMA_ON;
975             }
976             else
977             {
978                 msg_Err(p_demux, "Bad capture method value '%s', "
979                                  "it can be 'raw1394' or 'video1394'.",
980                                 token );
981                 free(psz_dup);
982                 return VLC_EGENERIC;
983             }
984         }
985         else if( strncmp( token, "adev=", strlen( "adev=" ) ) == 0 )
986         {
987             token += strlen("adev=");
988             p_sys->audio_device = strdup(token);
989             msg_Dbg( p_demux, "Using audio device '%s'.", token );
990         }
991         else if( strncmp( token, "samplerate=", strlen( "samplerate=" ) ) == 0 )
992         {
993             token += strlen("samplerate=");
994             sscanf( token, "%d", &p_sys->i_sample_rate );
995         }
996         else if( strncmp( token, "channels=", strlen("channels=" ) ) == 0 )
997         {
998             token += strlen("channels=");
999             sscanf( token, "%d", &p_sys->channels );
1000         }
1001         else if( strncmp( token, "focus=", strlen("focus=" ) ) == 0)
1002         {
1003             token += strlen("focus=");
1004             sscanf( token, "%u", &p_sys->focus );
1005         }
1006         else if( strncmp( token, "uid=", strlen("uid=") ) == 0)
1007         {
1008             token += strlen("uid=");
1009             sscanf( token, "0x%llx", &p_sys->selected_uid );
1010         }
1011     }
1012     if( psz_dup ) free( psz_dup );
1013     return VLC_SUCCESS;
1014 }
1015