]> git.sesse.net Git - vlc/blob - modules/access/dc1394.c
dc1394: cleanings (no need to set p_sys to NULL and some other stuffs).
[vlc] / modules / access / dc1394.c
1 /*****************************************************************************
2  * dc1394.c: firewire input module
3  *****************************************************************************
4  * Copyright (C) 2006-2009 VideoLAN
5  *
6  * Authors: Xant Majere <xant@xant.net>
7  *          Rob Shortt <rob@tvcentric.com> - libdc1394 V2 API updates
8  *          Frederic Benoist <fridorik@gmail.com> - updates from Rob's work
9  *
10  *****************************************************************************
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation;
14  * version 2 of the License.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  *
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_input.h>
38 #include <vlc_demux.h>
39 #include <vlc_charset.h>
40 #include <vlc_picture.h>
41
42 #ifdef HAVE_FCNTL_H
43 #   include <fcntl.h>
44 #endif
45 #ifdef HAVE_UNISTD_H
46 #   include <unistd.h>
47 #elif defined( WIN32 ) && !defined( UNDER_CE )
48 #   include <io.h>
49 #endif
50
51 #include <sys/ioctl.h>
52 #include <sys/soundcard.h>
53
54 #include <libraw1394/raw1394.h>
55 #include <dc1394/dc1394.h>
56
57 #define MAX_IEEE1394_HOSTS 32
58 #define MAX_CAMERA_NODES 32
59
60 /*****************************************************************************
61  * Module descriptor
62  *****************************************************************************/
63 static int  Open ( vlc_object_t * );
64 static void Close( vlc_object_t * );
65 static void OpenAudioDev( demux_t *p_demux );
66 static inline void CloseAudioDev( demux_t *p_demux );
67
68 vlc_module_begin()
69     set_description( N_("dc1394 input") )
70     set_capability( "access_demux", 10 )
71     add_shortcut( "dc1394" )
72     set_callbacks( Open, Close )
73 vlc_module_end()
74
75 struct demux_sys_t
76 {
77     /* camera info */
78     dc1394_t            *p_dccontext;
79     uint32_t            num_cameras;
80     dc1394camera_t      *camera;
81     int                 selected_camera;
82     uint64_t            selected_uid;
83     picture_t           pic;
84     uint32_t            dma_buffers;
85     dc1394featureset_t  features;
86     quadlet_t           supported_framerates;
87     bool                reset_bus;
88
89     /* video info */
90     char                *video_device;
91     dc1394video_mode_t  video_mode;
92     int                 width;
93     int                 height;
94     int                 frame_size;
95     int                 frame_rate;
96     unsigned int        brightness;
97     unsigned int        focus;
98     es_out_id_t         *p_es_video;
99     dc1394video_frame_t *frame;
100
101     /* audio stuff */
102     int                 i_sample_rate;
103     int                 channels;
104     int                 i_audio_max_frame_size;
105     int                 fd_audio;
106     char                *audio_device;
107     es_out_id_t         *p_es_audio;
108 };
109
110 /*****************************************************************************
111  * Local prototypes
112  *****************************************************************************/
113 static int Demux( demux_t *p_demux );
114 static int Control( demux_t *, int, va_list );
115 static block_t *GrabVideo( demux_t *p_demux );
116 static block_t *GrabAudio( demux_t *p_demux );
117 static int process_options( demux_t *p_demux);
118
119 /*****************************************************************************
120  * FindCameras
121  *****************************************************************************/
122 static int FindCamera( demux_sys_t *sys, demux_t *p_demux )
123 {
124     dc1394camera_list_t *list;
125
126     msg_Dbg( p_demux, "Scanning for ieee1394 ports ..." );
127
128     if( dc1394_camera_enumerate (sys->p_dccontext, &list) != DC1394_SUCCESS )
129     {
130         msg_Err(p_demux, "Can not ennumerate cameras");
131         dc1394_camera_free_list (list);
132         return VLC_EGENERIC;
133     }
134
135     if( list->num == 0 )
136     {
137         msg_Err(p_demux, "Can not find cameras");
138         dc1394_camera_free_list (list);
139         return VLC_EGENERIC;
140     }
141
142     sys->num_cameras = list->num;
143     msg_Dbg( p_demux, "Found %d dc1394 cameras.", list->num);
144
145     if( sys->selected_uid )
146     {
147         int found = 0;
148         for( unsigned i = 0; i < sys->num_cameras; i++ )
149         {
150             if( list->ids[i].guid == sys->selected_uid )
151             {
152                 sys->camera = dc1394_camera_new(sys->p_dccontext,
153                                                 list->ids[i].guid);
154                 found++;
155                 break;
156             }
157         }
158         if( !found )
159         {
160             msg_Err( p_demux, "Can't find camera with uid : 0x%llx.",
161                      sys->selected_uid );
162             dc1394_camera_free_list (list);
163             return VLC_EGENERIC;
164         }
165     }
166     else if( sys->selected_camera >= (int)list->num )
167     {
168         msg_Err( p_demux, "There are not this many cameras. (%d/%d)",
169                  sys->selected_camera, sys->num_cameras );
170         dc1394_camera_free_list (list);
171         return VLC_EGENERIC;
172     }
173     else if( sys->selected_camera >= 0 )
174     {
175         sys->camera = dc1394_camera_new(sys->p_dccontext,
176                     list->ids[sys->selected_camera].guid);
177     }
178     else
179     {
180         sys->camera = dc1394_camera_new(sys->p_dccontext,
181                                           list->ids[0].guid);
182     }
183
184     dc1394_camera_free_list (list);
185     return VLC_SUCCESS;
186 }
187
188 /*****************************************************************************
189  * Open:
190  *****************************************************************************/
191 static int Open( vlc_object_t *p_this )
192 {
193     demux_t      *p_demux = (demux_t*)p_this;
194     demux_sys_t  *p_sys;
195     es_format_t   fmt;
196     dc1394error_t res;
197     int           i_width;
198     int           i_height;
199
200     if( strncmp(p_demux->psz_access, "dc1394", 6) != 0 )
201         return VLC_EGENERIC;
202
203     /* Set up p_demux */
204     p_demux->pf_demux = Demux;
205     p_demux->pf_control = Control;
206     p_demux->info.i_update = 0;
207     p_demux->info.i_title = 0;
208     p_demux->info.i_seekpoint = 0;
209
210     p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
211     if( !p_sys )
212         return VLC_ENOMEM;
213
214     memset( &fmt, 0, sizeof( es_format_t ) );
215
216     /* DEFAULTS */
217     p_sys->video_mode      = DC1394_VIDEO_MODE_640x480_YUV422;
218     p_sys->width           = 640;
219     p_sys->height          = 480;
220     p_sys->frame_rate      = DC1394_FRAMERATE_15;
221     p_sys->brightness      = 200;
222     p_sys->focus           = 0;
223     p_sys->fd_audio        = -1;
224     p_sys->p_dccontext     = NULL;
225     p_sys->camera          = NULL;
226     p_sys->selected_camera = -1;
227     p_sys->dma_buffers     = 1;
228     p_sys->reset_bus       = 0;
229
230     /* PROCESS INPUT OPTIONS */
231     if( process_options(p_demux) != VLC_SUCCESS )
232     {
233         msg_Err( p_demux, "Bad MRL, please check the option line "
234                           "(MRL was: %s)",
235                           p_demux->psz_path );
236         free( p_sys );
237         return VLC_EGENERIC;
238     }
239
240     p_sys->p_dccontext = dc1394_new();
241     if( !p_sys->p_dccontext )
242     {
243         msg_Err( p_demux, "Failed to initialise libdc1394");
244         free( p_sys );
245         return VLC_EGENERIC;
246     }
247
248     if( FindCamera( p_sys, p_demux ) != VLC_SUCCESS )
249     {
250         dc1394_free( p_sys->p_dccontext );
251         free( p_sys );
252         return VLC_EGENERIC;
253     }
254
255     if( !p_sys->camera )
256     {
257         msg_Err( p_demux, "No camera found !!" );
258         dc1394_free( p_sys->p_dccontext );
259         free( p_sys );
260         return VLC_EGENERIC;
261     }
262
263     if( p_sys->reset_bus )
264     {
265         if( dc1394_reset_bus( p_sys->camera ) != DC1394_SUCCESS )
266         {
267             msg_Err( p_demux, "Unable to reset IEEE 1394 bus");
268             Close( p_this );
269             return VLC_EGENERIC;
270         }
271         else msg_Dbg( p_demux, "Successfully reset IEEE 1394 bus");
272     }
273
274     if( dc1394_camera_reset( p_sys->camera ) != DC1394_SUCCESS )
275     {
276         msg_Err( p_demux, "Unable to reset camera");
277         Close( p_this );
278         return VLC_EGENERIC;
279     }
280
281     if( dc1394_camera_print_info( p_sys->camera,
282                   stderr ) != DC1394_SUCCESS )
283     {
284         msg_Err( p_demux, "Unable to print camera info");
285         Close( p_this );
286         return VLC_EGENERIC;
287     }
288
289     if( dc1394_feature_get_all( p_sys->camera,
290                 &p_sys->features ) != DC1394_SUCCESS )
291     {
292         msg_Err( p_demux, "Unable to get feature set");
293         Close( p_this );
294         return VLC_EGENERIC;
295     }
296     // TODO: only print features if verbosity increased
297     dc1394_feature_print_all(&p_sys->features, stderr);
298
299 #if 0 //"Note that you need to execute this function only if you use exotic video1394 device names. /dev/video1394, /dev/video1394/* and /dev/video1394-* are automatically recognized." http://damien.douxchamps.net/ieee1394/libdc1394/v2.x/api/capture/
300     if( p_sys->video_device )
301     {
302         if( dc1394_capture_set_device_filename( p_sys->camera,
303                         p_sys->video_device ) != DC1394_SUCCESS )
304         {
305             msg_Err( p_demux, "Unable to set video device");
306             Close( p_this );
307             return VLC_EGENERIC;
308         }
309     }
310 #endif
311
312     if( p_sys->focus )
313     {
314         if( dc1394_feature_set_value( p_sys->camera,
315                       DC1394_FEATURE_FOCUS,
316                       p_sys->focus ) != DC1394_SUCCESS )
317         {
318             msg_Err( p_demux, "Unable to set initial focus to %u",
319                      p_sys->focus );
320         }
321         msg_Dbg( p_demux, "Initial focus set to %u", p_sys->focus );
322     }
323
324     if( dc1394_feature_set_value( p_sys->camera,
325                   DC1394_FEATURE_FOCUS,
326                   p_sys->brightness ) != DC1394_SUCCESS )
327     {
328         msg_Err( p_demux, "Unable to set initial brightness to %u",
329                  p_sys->brightness );
330     }
331     msg_Dbg( p_demux, "Initial brightness set to %u", p_sys->brightness );
332
333     if( dc1394_video_set_framerate( p_sys->camera,
334                     p_sys->frame_rate ) != DC1394_SUCCESS )
335     {
336         msg_Err( p_demux, "Unable to set framerate");
337         Close( p_this );
338         return VLC_EGENERIC;
339     }
340
341     if( dc1394_video_set_mode( p_sys->camera,
342                    p_sys->video_mode ) != DC1394_SUCCESS )
343     {
344         msg_Err( p_demux, "Unable to set video mode");
345         Close( p_this );
346         return VLC_EGENERIC;
347     }
348
349     if( dc1394_video_set_iso_speed( p_sys->camera,
350                     DC1394_ISO_SPEED_400 ) != DC1394_SUCCESS )
351     {
352         msg_Err( p_demux, "Unable to set iso speed");
353         Close( p_this );
354         return VLC_EGENERIC;
355     }
356
357     /* and setup capture */
358     res = dc1394_capture_setup( p_sys->camera,
359                     p_sys->dma_buffers,
360                 DC1394_CAPTURE_FLAGS_DEFAULT);
361     if( res != DC1394_SUCCESS )
362     {
363         if( res == DC1394_NO_BANDWIDTH )
364         {
365             msg_Err( p_demux ,"No bandwidth: try adding the "
366              "\"resetbus\" option" );
367         }
368         else
369         {
370             msg_Err( p_demux ,"Unable to setup capture" );
371         }
372         Close( p_this );
373         return VLC_EGENERIC;
374     }
375
376     /* TODO - UYV444 chroma converter is missing, when it will be available
377      * fourcc will become variable (and not just a fixed value for UYVY)
378      */
379     i_width = p_sys->width;
380     i_height = p_sys->height;
381
382     if( picture_Setup( &p_sys->pic, VLC_CODEC_UYVY,
383                        i_width, i_height,
384                        i_width * VOUT_ASPECT_FACTOR / i_height ) )
385     {
386         msg_Err( p_demux ,"unknown chroma" );
387         Close( p_this );
388         return VLC_EGENERIC;
389     }
390
391     es_format_Init( &fmt, VIDEO_ES, VLC_CODEC_UYVY );
392
393     fmt.video.i_width = i_width;
394     fmt.video.i_height = i_height;
395
396     msg_Dbg( p_demux, "Added new video es %4.4s %dx%d",
397              (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
398
399     p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );
400
401     if( p_sys->audio_device )
402     {
403         OpenAudioDev( p_demux );
404         if( p_sys->fd_audio >= 0 )
405         {
406             es_format_t fmt;
407             es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_S16L ); /* FIXME: hmm, ?? */
408
409             fmt.audio.i_channels = p_sys->channels ? p_sys->channels : 1;
410             fmt.audio.i_rate = p_sys->i_sample_rate;
411             fmt.audio.i_bitspersample = 16;
412             fmt.audio.i_blockalign = fmt.audio.i_channels *
413                                      fmt.audio.i_bitspersample / 8;
414             fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate *
415                             fmt.audio.i_bitspersample;
416
417             msg_Dbg( p_demux, "New audio es %d channels %dHz",
418             fmt.audio.i_channels, fmt.audio.i_rate );
419
420             p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
421         }
422     }
423
424     /* have the camera start sending us data */
425     if( dc1394_video_set_transmission( p_sys->camera,
426                        DC1394_ON ) != DC1394_SUCCESS )
427     {
428         msg_Err( p_demux, "Unable to start camera iso transmission" );
429         dc1394_capture_stop( p_sys->camera );
430         Close( p_this );
431         return VLC_EGENERIC;
432     }
433     msg_Dbg( p_demux, "Set iso transmission" );
434     // TODO: reread camera
435     return VLC_SUCCESS;
436 }
437
438 static void OpenAudioDev( demux_t *p_demux )
439 {
440     demux_sys_t *p_sys = p_demux->p_sys;
441     char *psz_device = p_sys->audio_device;
442     int i_format = AFMT_S16_LE;
443     int result;
444
445     p_sys->fd_audio = utf8_open( psz_device, O_RDONLY | O_NONBLOCK );
446     if( p_sys->fd_audio  < 0 )
447     {
448         msg_Err( p_demux, "Cannot open audio device (%s)", psz_device );
449         CloseAudioDev( p_demux );
450     }
451
452     if( !p_sys->i_sample_rate )
453         p_sys->i_sample_rate = 44100;
454
455     result = ioctl( p_sys->fd_audio, SNDCTL_DSP_SETFMT, &i_format );
456     if( (result  < 0) || (i_format != AFMT_S16_LE) )
457     {
458         msg_Err( p_demux, "Cannot set audio format (16b little endian) "
459                           "(%d)", i_format );
460         CloseAudioDev( p_demux );
461     }
462
463     result = ioctl( p_sys->fd_audio, SNDCTL_DSP_CHANNELS, &p_sys->channels );
464     if( result < 0 )
465     {
466         msg_Err( p_demux, "Cannot set audio channels count (%d)",
467                  p_sys->channels );
468         CloseAudioDev( p_demux );
469     }
470
471     result = ioctl( p_sys->fd_audio, SNDCTL_DSP_SPEED, &p_sys->i_sample_rate );
472     if( result < 0 )
473     {
474         msg_Err( p_demux, "Cannot set audio sample rate (%d)",
475          p_sys->i_sample_rate );
476         CloseAudioDev( p_demux );
477     }
478
479     msg_Dbg( p_demux, "Opened adev=`%s' %s %dHz",
480              psz_device,
481              (p_sys->channels > 1) ? "stereo" : "mono",
482              p_sys->i_sample_rate );
483
484     p_sys->i_audio_max_frame_size = 32 * 1024;
485 }
486
487 static inline void CloseAudioDev( demux_t *p_demux )
488 {
489     demux_sys_t *p_sys = p_demux->p_sys;
490
491     if( p_sys->fd_audio >= 0 )
492         close( p_sys->fd_audio );
493 }
494
495 /*****************************************************************************
496  * Close:
497  *****************************************************************************/
498 static void Close( vlc_object_t *p_this )
499 {
500     demux_t     *p_demux = (demux_t*)p_this;
501     demux_sys_t *p_sys = p_demux->p_sys;
502
503     /* Stop data transmission */
504     if( dc1394_video_set_transmission( p_sys->camera,
505                        DC1394_OFF ) != DC1394_SUCCESS )
506         msg_Err( p_demux, "Unable to stop camera iso transmission" );
507
508     /* Close camera */
509     dc1394_capture_stop( p_sys->camera );
510
511     CloseAudioDev( p_demux );
512
513     dc1394_camera_free(p_sys->camera);
514     dc1394_free(p_sys->p_dccontext);
515
516     free( p_sys->audio_device );
517     free( p_sys );
518 }
519
520 #if 0
521 static void MovePixelUYVY( void *src, int spos, void *dst, int dpos )
522 {
523     char u,v,y;
524     u_char  *sc;
525     u_char  *dc;
526
527     sc = (u_char *)src + (spos*2);
528     if( spos % 2 )
529     {
530         v = sc[0];
531         y = sc[1];
532         u = *(sc -2);
533     }
534     else
535     {
536         u = sc[0];
537         y = sc[1];
538         v = sc[2];
539     }
540     dc = (u_char *)dst+(dpos*2);
541     if( dpos % 2 )
542     {
543         dc[0] = v;
544         dc[1] = y;
545     }
546     else
547     {
548         dc[0] = u;
549         dc[1] = y;
550     }
551 }
552 #endif
553
554 /*****************************************************************************
555  * Demux:
556  *****************************************************************************/
557 static block_t *GrabVideo( demux_t *p_demux )
558 {
559     demux_sys_t *p_sys = p_demux->p_sys;
560     block_t     *p_block = NULL;
561
562     if( dc1394_capture_dequeue( p_sys->camera,
563                 DC1394_CAPTURE_POLICY_WAIT,
564                 &p_sys->frame ) != DC1394_SUCCESS )
565     {
566         msg_Err( p_demux, "Unable to capture a frame" );
567         return NULL;
568     }
569
570     p_block = block_New( p_demux, p_sys->frame->size[0] *
571                                   p_sys->frame->size[1] * 2 );
572     if( !p_block )
573     {
574         msg_Err( p_demux, "Can not get block" );
575         return NULL;
576     }
577
578     if( !p_sys->frame->image )
579     {
580         msg_Err (p_demux, "Capture buffer empty");
581         block_Release( p_block );
582         return NULL;
583     }
584
585     memcpy( p_block->p_buffer, (const char *)p_sys->frame->image,
586             p_sys->width * p_sys->height * 2 );
587
588     p_block->i_pts = p_block->i_dts = mdate();
589     dc1394_capture_enqueue( p_sys->camera, p_sys->frame );
590     return p_block;
591 }
592
593 static block_t *GrabAudio( demux_t *p_demux )
594 {
595     demux_sys_t *p_sys = p_demux->p_sys;
596     struct audio_buf_info buf_info;
597     block_t *p_block = NULL;
598     int i_read = 0;
599     int i_correct = 0;
600     int result = 0;
601
602     p_block = block_New( p_demux, p_sys->i_audio_max_frame_size );
603     if( !p_block )
604     {
605         msg_Warn( p_demux, "Cannot get buffer" );
606         return NULL;
607     }
608
609     i_read = read( p_sys->fd_audio, p_block->p_buffer,
610                    p_sys->i_audio_max_frame_size );
611
612     if( i_read <= 0 )
613         return NULL;
614
615     p_block->i_buffer = i_read;
616
617     /* Correct the date because of kernel buffering */
618     i_correct = i_read;
619     result = ioctl( p_sys->fd_audio, SNDCTL_DSP_GETISPACE, &buf_info );
620     if( result == 0 )
621         i_correct += buf_info.bytes;
622
623     p_block->i_pts = p_block->i_dts =
624                         mdate() - INT64_C(1000000) * (mtime_t)i_correct /
625                         2 / p_sys->channels / p_sys->i_sample_rate;
626     return p_block;
627 }
628
629 static int Demux( demux_t *p_demux )
630 {
631     demux_sys_t *p_sys = p_demux->p_sys;
632     block_t *p_blocka = NULL;
633     block_t *p_blockv = NULL;
634
635     /* Try grabbing audio frames first */
636     if( p_sys->fd_audio > 0 )
637         p_blocka = GrabAudio( p_demux );
638
639     /* Try grabbing video frame */
640     p_blockv = GrabVideo( p_demux );
641
642     if( !p_blocka && !p_blockv )
643     {
644         /* Sleep so we do not consume all the cpu, 10ms seems
645          * like a good value (100fps)
646          */
647         msleep( 10000 );
648         return 1;
649     }
650
651     if( p_blocka )
652     {
653         es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_blocka->i_pts );
654         es_out_Send( p_demux->out, p_sys->p_es_audio, p_blocka );
655     }
656
657     if( p_blockv )
658     {
659         es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_blockv->i_pts );
660         es_out_Send( p_demux->out, p_sys->p_es_video, p_blockv );
661     }
662     return 1;
663 }
664
665 /*****************************************************************************
666  * Control:
667  *****************************************************************************/
668 static int Control( demux_t *p_demux, int i_query, va_list args )
669 {
670     VLC_UNUSED( p_demux );
671     switch( i_query )
672     {
673         /* Special for access_demux */
674         case DEMUX_CAN_PAUSE:
675         case DEMUX_CAN_SEEK:
676         case DEMUX_SET_PAUSE_STATE:
677         case DEMUX_CAN_CONTROL_PACE:
678             *va_arg( args, bool * ) = false;
679             return VLC_SUCCESS;
680
681         case DEMUX_GET_PTS_DELAY:
682             *va_arg( args, int64_t * ) = (int64_t)DEFAULT_PTS_DELAY;
683             return VLC_SUCCESS;
684
685         case DEMUX_GET_TIME:
686             *va_arg( args, int64_t * ) = mdate();
687             return VLC_SUCCESS;
688
689         /* TODO implement others */
690         default:
691             return VLC_EGENERIC;
692     }
693     return VLC_EGENERIC;
694 }
695
696 static int process_options( demux_t *p_demux )
697 {
698     demux_sys_t *p_sys = p_demux->p_sys;
699     char *psz_dup;
700     char *psz_parser;
701     char *token = NULL;
702     char *state = NULL;
703     const char *in_size = NULL;
704     const char *in_fmt = NULL;
705     float rate_f;
706
707     psz_dup = strdup( p_demux->psz_path );
708     psz_parser = psz_dup;
709     for( token = strtok_r( psz_parser,":",&state); token;
710          token = strtok_r( NULL, ":", &state ) )
711     {
712         if( strncmp( token, "size=", strlen("size=") ) == 0 )
713         {
714             token += strlen("size=");
715             if( strncmp( token, "160x120", 7 ) == 0 )
716             {
717                 /* TODO - UYV444 chroma converter is needed ...
718                     * video size of 160x120 is temporarily disabled
719                     */
720                 msg_Err( p_demux,
721                     "video size of 160x120 is actually disabled for lack of"
722                     "chroma support. It will relased ASAP, until then try "
723                     "an higher size (320x240 and 640x480 are fully supported)" );
724                 free(psz_dup);
725                 return VLC_EGENERIC;
726 #if 0
727                 in_size = "160x120";
728                 p_sys->width = 160;
729                 p_sys->height = 120;
730 #endif
731             }
732             else if( strncmp( token, "320x240", 7 ) == 0 )
733             {
734                 in_size = "320x240";
735                 p_sys->width = 320;
736                 p_sys->height = 240;
737             }
738             else if( strncmp( token, "640x480", 7 ) == 0 )
739             {
740                 in_size = "640x480";
741                 p_sys->width = 640;
742                 p_sys->height = 480;
743             }
744             else
745             {
746                 msg_Err( p_demux,
747                     "This program currently suppots frame sizes of"
748                     " 160x120, 320x240, and 640x480. "
749                     "Please specify one of them. You have specified %s.",
750                     token );
751                 free(psz_dup);
752                 return VLC_EGENERIC;
753             }
754             msg_Dbg( p_demux, "Requested video size : %s",token );
755         }
756         if( strncmp( token, "format=", strlen("format=") ) == 0 )
757         {
758             token += strlen("format=");
759             if( strncmp( token, "YUV411", 6 ) == 0 )
760             {
761                 in_fmt = "YUV411";
762             }
763             else if( strncmp( token, "YUV422", 6 ) == 0 )
764             {
765                 in_fmt = "YUV422";
766             }
767             else if( strncmp( token, "YUV444", 6 ) == 0 )
768             {
769                 in_fmt = "YUV444";
770             }
771             else if( strncmp( token, "RGB8", 4 ) == 0 )
772             {
773                 in_fmt = "RGB8";
774             }
775             else if( strncmp( token, "MONO8", 5 ) == 0 )
776             {
777                 in_fmt = "MONO8";
778             }
779             else if( strncmp( token, "MONO16", 6 ) == 0 )
780             {
781                 in_fmt = "MONO16";
782             }
783             else
784             {
785                 msg_Err( p_demux, "Invalid format %s.", token );
786                 free(psz_dup);
787                 return VLC_EGENERIC;
788             }
789             msg_Dbg( p_demux, "Requested video format : %s", token );
790         }
791         else if( strncmp( token, "fps=", strlen( "fps=" ) ) == 0 )
792         {
793             token += strlen("fps=");
794             sscanf( token, "%g", &rate_f );
795             if( rate_f == 1.875 )
796                 p_sys->frame_rate = DC1394_FRAMERATE_1_875;
797             else if( rate_f == 3.75 )
798                 p_sys->frame_rate = DC1394_FRAMERATE_3_75;
799             else if( rate_f == 7.5 )
800                 p_sys->frame_rate = DC1394_FRAMERATE_7_5;
801             else if( rate_f == 15 )
802                 p_sys->frame_rate = DC1394_FRAMERATE_15;
803             else if( rate_f == 30 )
804                 p_sys->frame_rate = DC1394_FRAMERATE_30;
805             else if( rate_f == 60 )
806                 p_sys->frame_rate = DC1394_FRAMERATE_60;
807             else
808             {
809                 msg_Err( p_demux ,
810                     "This program supports framerates of"
811                     " 1.875, 3.75, 7.5, 15, 30, 60. "
812                     "Please specify one of them. You have specified %s.",
813                     token);
814                 free(psz_dup);
815                 return VLC_EGENERIC;
816             }
817             msg_Dbg( p_demux, "Requested frame rate : %s",token );
818         }
819         else if( strncmp( token, "resetbus", strlen( "resetbus" ) ) == 0 )
820         {
821             token += strlen("resetbus");
822             p_sys->reset_bus = 1;
823         }
824         else if( strncmp( token, "brightness=", strlen( "brightness=" ) ) == 0 )
825         {
826             int nr = 0;
827             token += strlen("brightness=");
828             nr = sscanf( token, "%u", &p_sys->brightness);
829             if( nr != 1 )
830             {
831                 msg_Err( p_demux, "Bad brightness value '%s', "
832                                   "must be an unsigned integer.",
833                                   token );
834                 free(psz_dup);
835                 return VLC_EGENERIC;
836             }
837         }
838         else if( strncmp( token, "buffers=", strlen( "buffers=" ) ) == 0 )
839         {
840             int nr = 0;
841             int in_buf = 0;
842             token += strlen("buffers=");
843             nr = sscanf( token, "%d", &in_buf);
844             if( nr != 1 || in_buf < 1 )
845             {
846                 msg_Err( p_demux, "DMA buffers must be 1 or greater." );
847                 free(psz_dup);
848                 return VLC_EGENERIC;
849             }
850             else p_sys->dma_buffers = in_buf;
851         }
852 #if 0
853         // NOTE: If controller support is added back, more logic will needed to be added
854         //       after the cameras are scanned.
855         else if( strncmp( token, "controller=", strlen( "controller=" ) ) == 0 )
856         {
857             int nr = 0;
858             token += strlen("controller=");
859             nr = sscanf( token, "%u", &p_sys->controller );
860             if( nr != 1)
861             {
862                 msg_Err(p_demux, "Bad controller value '%s', "
863                                  "must be an unsigned integer.",
864                                  token );
865                 return VLC_EGENERIC;
866             }
867         }
868 #endif
869         else if( strncmp( token, "camera=", strlen( "camera=" ) ) == 0 )
870         {
871             int nr = 0;
872             token += strlen("camera=");
873             nr = sscanf(token,"%u",&p_sys->selected_camera);
874             if( nr != 1)
875             {
876                 msg_Err( p_demux, "Bad camera number '%s', "
877                                   "must be an unsigned integer.",
878                                   token );
879                 free(psz_dup);
880                 return VLC_EGENERIC;
881             }
882         }
883         else if( strncmp( token, "vdev=", strlen( "vdev=" ) ) == 0)
884         {
885             token += strlen("vdev=");
886             p_sys->video_device = strdup(token);
887             msg_Dbg( p_demux, "Using video device '%s'.", token );
888         }
889         else if( strncmp( token, "adev=", strlen( "adev=" ) ) == 0 )
890         {
891             token += strlen("adev=");
892             p_sys->audio_device = strdup(token);
893             msg_Dbg( p_demux, "Using audio device '%s'.", token );
894         }
895         else if( strncmp( token, "samplerate=", strlen( "samplerate=" ) ) == 0 )
896         {
897             token += strlen("samplerate=");
898             sscanf( token, "%d", &p_sys->i_sample_rate );
899         }
900         else if( strncmp( token, "channels=", strlen("channels=" ) ) == 0 )
901         {
902             token += strlen("channels=");
903             sscanf( token, "%d", &p_sys->channels );
904         }
905         else if( strncmp( token, "focus=", strlen("focus=" ) ) == 0)
906         {
907             int nr = 0;
908             token += strlen("focus=");
909             nr = sscanf( token, "%u", &p_sys->focus );
910             if( nr != 1 )
911             {
912                 msg_Err( p_demux, "Bad focus value '%s', "
913                                   "must be an unsigned integer.",
914                                   token );
915                 free(psz_dup);
916                 return VLC_EGENERIC;
917             }
918         }
919         else if( strncmp( token, "uid=", strlen("uid=") ) == 0)
920         {
921             token += strlen("uid=");
922             sscanf( token, "0x%llx", &p_sys->selected_uid );
923         }
924     }
925
926     // The mode is a combination of size and format and not every format
927     // is supported by every size.
928     if( in_size)
929     {
930         if( strcmp( in_size, "160x120") == 0)
931         {
932             if( in_fmt && (strcmp( in_fmt, "YUV444") != 0) )
933                 msg_Err(p_demux, "160x120 only supports YUV444 - forcing");
934             p_sys->video_mode = DC1394_VIDEO_MODE_160x120_YUV444;
935         }
936         else if( strcmp( in_size, "320x240") == 0)
937         {
938             if( in_fmt && (strcmp( in_fmt, "YUV422") != 0) )
939                 msg_Err(p_demux, "320x240 only supports YUV422 - forcing");
940             p_sys->video_mode = DC1394_VIDEO_MODE_320x240_YUV422;
941         }
942     }
943     else
944     { // 640x480 default
945         if( in_fmt )
946         {
947             if( strcmp( in_fmt, "RGB8") == 0)
948                 p_sys->video_mode = DC1394_VIDEO_MODE_640x480_RGB8;
949             else if( strcmp( in_fmt, "MONO8") == 0)
950                 p_sys->video_mode = DC1394_VIDEO_MODE_640x480_MONO8;
951             else if( strcmp( in_fmt, "MONO16") == 0)
952                 p_sys->video_mode = DC1394_VIDEO_MODE_640x480_MONO16;
953             else if( strcmp( in_fmt, "YUV411") == 0)
954                 p_sys->video_mode = DC1394_VIDEO_MODE_640x480_YUV411;
955             else // YUV422 default
956                 p_sys->video_mode = DC1394_VIDEO_MODE_640x480_YUV422;
957         }
958         else // YUV422 default
959             p_sys->video_mode = DC1394_VIDEO_MODE_640x480_YUV422;
960     }
961     return VLC_SUCCESS;
962 }