]> git.sesse.net Git - vlc/blob - modules/access/v4l/v4l.c
* include/vlc_common.h:
[vlc] / modules / access / v4l / v4l.c
1 /*****************************************************************************
2  * v4l.c : Video4Linux input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: v4l.c,v 1.26 2003/10/25 00:49:13 sam Exp $
6  *
7  * Author: Laurent Aimar <fenrir@via.ecp.fr>
8  *         Paul Forgey <paulf at aphrodite dot com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31
32 #include <vlc/vlc.h>
33 #include <vlc/input.h>
34 #include <vlc/vout.h>
35 #include <codecs.h>
36
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <sys/ioctl.h>
40 #include <unistd.h>
41 #include <sys/mman.h>
42 #include <errno.h>
43
44 #include <fcntl.h>
45 #include <linux/videodev.h>
46 #include "videodev_mjpeg.h"
47
48 #include <sys/soundcard.h>
49
50 /*****************************************************************************
51  * Local prototypes
52  *****************************************************************************/
53 static int  AccessOpen  ( vlc_object_t * );
54 static void AccessClose ( vlc_object_t * );
55 static int  Read        ( input_thread_t *, byte_t *, size_t );
56
57 static int  DemuxOpen  ( vlc_object_t * );
58 static void DemuxClose ( vlc_object_t * );
59 static int  Demux      ( input_thread_t * );
60
61
62 /*****************************************************************************
63  * Module descriptior
64  *****************************************************************************/
65 #define CACHING_TEXT N_("Caching value in ms")
66 #define CACHING_LONGTEXT N_( \
67     "Allows you to modify the default caching value for v4l streams. This " \
68     "value should be set in miliseconds units." )
69
70 vlc_module_begin();
71     set_description( _("Video4Linux input") );
72     add_category_hint( N_("v4l"), NULL, VLC_TRUE );
73     add_integer( "v4l-caching", DEFAULT_PTS_DELAY / 1000, NULL,
74                  CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
75     add_shortcut( "v4l" );
76     set_capability( "access", 10 );
77     set_callbacks( AccessOpen, AccessClose );
78
79     add_submodule();
80         set_description( _("Video4Linux demuxer") );
81         add_shortcut( "v4l" );
82         set_capability( "demux", 200 );
83         set_callbacks( DemuxOpen, DemuxClose );
84 vlc_module_end();
85
86
87 /****************************************************************************
88  * I. Access Part
89  ****************************************************************************/
90 #define MJPEG_BUFFER_SIZE (256*1024)
91
92 struct quicktime_mjpeg_app1
93 {
94     uint32_t    i_reserved;             /* set to 0 */
95     uint32_t    i_tag;                  /* 'mjpg' */
96     uint32_t    i_field_size;           /* offset following EOI */
97     uint32_t    i_padded_field_size;    /* offset following EOI+pad */
98     uint32_t    i_next_field;           /* offset to next field */
99     uint32_t    i_DQT_offset;
100     uint32_t    i_DHT_offset;
101     uint32_t    i_SOF_offset;
102     uint32_t    i_SOS_offset;
103     uint32_t    i_data_offset;          /* following SOS marker data */
104 };
105
106 struct access_sys_t
107 {
108     char    *psz_video_device;
109     int     fd;
110
111     picture_t       pic;
112
113     int i_fourcc;
114     int i_channel;
115     int i_audio;
116     int i_norm;
117     int i_tuner;
118     int i_frequency;
119     int i_width;
120     int i_height;
121
122     vlc_bool_t b_mjpeg;
123     int i_decimation;
124     int i_quality;
125
126     struct video_capability vid_cap;
127     struct video_mbuf       vid_mbuf;
128     struct mjpeg_requestbuffers mjpeg_buffers;
129
130     uint8_t *p_video_mmap;
131     int     i_frame_pos;
132
133     struct video_mmap   vid_mmap;
134     struct video_picture vid_picture;
135
136     uint8_t *p_video_frame;
137     int     i_video_frame_size;
138     int     i_video_frame_size_allocated;
139
140     char         *psz_adev;
141     int          fd_audio;
142     vlc_fourcc_t i_acodec_raw;
143     int          i_sample_rate;
144     vlc_bool_t   b_stereo;
145
146     uint8_t *p_audio_frame;
147     int     i_audio_frame_size;
148     int     i_audio_frame_size_allocated;
149
150     /* header */
151     int     i_header_size;
152     int     i_header_pos;
153     uint8_t *p_header;      // at lest 8 bytes allocated
154
155     /* data */
156     int     i_data_size;
157     int     i_data_pos;
158     uint8_t *p_data;        // never allocated
159
160 };
161
162 /*
163  * header:
164  *  fcc  ".v4l"
165  *  u32    stream count
166  *      fcc "auds"|"vids"       0
167  *      fcc codec               4
168  *      if vids
169  *          u32 width           8
170  *          u32 height          12
171  *          u32 padding         16
172  *      if auds
173  *          u32 channels        12
174  *          u32 samplerate      8
175  *          u32 samplesize      16
176  *
177  * data:
178  *  u32     stream number
179  *  u32     data size
180  *  u8      data
181  */
182
183 static void    SetDWBE( uint8_t *p, uint32_t dw )
184 {
185     p[0] = (dw >> 24)&0xff;
186     p[1] = (dw >> 16)&0xff;
187     p[2] = (dw >>  8)&0xff;
188     p[3] = (dw      )&0xff;
189 }
190
191 static void    SetQWBE( uint8_t *p, uint64_t qw )
192 {
193     SetDWBE( p,     (qw >> 32)&0xffffffff );
194     SetDWBE( &p[4], qw&0xffffffff);
195 }
196
197 /*****************************************************************************
198  * Open: open device:
199  *****************************************************************************
200  *
201  * url: <video device>::::
202  *
203  *****************************************************************************/
204 static int AccessOpen( vlc_object_t *p_this )
205 {
206     input_thread_t *p_input = (input_thread_t *)p_this;
207     access_sys_t   *p_sys;
208     char           *psz_dup, *psz_parser;
209     struct mjpeg_params mjpeg;
210     int i;
211
212     struct video_channel    vid_channel;
213
214     /* create access private data */
215     p_sys = malloc( sizeof( access_sys_t ) );
216     memset( p_sys, 0, sizeof( access_sys_t ) );
217
218     p_sys->psz_video_device = NULL;
219     p_sys->fd               = -1;
220     p_sys->i_channel        = -1;
221     p_sys->i_audio          = -1;
222     p_sys->i_norm           = VIDEO_MODE_AUTO;    // auto
223     p_sys->i_tuner          = -1;
224     p_sys->i_frequency      = -1;
225     p_sys->i_width          = 0;
226     p_sys->i_height         = 0;
227
228     p_sys->b_mjpeg     = VLC_FALSE;
229     p_sys->i_decimation = 1;
230     p_sys->i_quality = 100;
231
232     p_sys->i_frame_pos = 0;
233
234     p_sys->i_video_frame_size_allocated = 0;
235     p_sys->psz_adev         = NULL;
236     p_sys->fd_audio         = -1;
237     p_sys->i_sample_rate    = 44100;
238     p_sys->b_stereo         = VLC_TRUE;
239
240     p_sys->i_data_size = 0;
241     p_sys->i_data_pos  = 0;
242     p_sys->p_data      = NULL;
243
244     /* parse url and open device(s) */
245     psz_dup = strdup( p_input->psz_name );
246     psz_parser = psz_dup;
247
248     while( *psz_parser && *psz_parser != ':' )
249     {
250         psz_parser++;
251     }
252
253     if( *psz_parser == ':' )
254     {
255         /* read options */
256         for( ;; )
257         {
258             *psz_parser++ = '\0';
259             if( !strncmp( psz_parser, "channel=", strlen( "channel=" ) ) )
260             {
261                 p_sys->i_channel = strtol( psz_parser + strlen( "channel=" ),
262                                            &psz_parser, 0 );
263             }
264             else if( !strncmp( psz_parser, "norm=", strlen( "norm=" ) ) )
265             {
266                 psz_parser += strlen( "norm=" );
267                 if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
268                 {
269                     p_sys->i_norm = VIDEO_MODE_PAL;
270                     psz_parser += strlen( "pal" );
271                 }
272                 else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
273                 {
274                     p_sys->i_norm = VIDEO_MODE_NTSC;
275                     psz_parser += strlen( "ntsc" );
276                 }
277                 else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
278                 {
279                     p_sys->i_norm = VIDEO_MODE_SECAM;
280                     psz_parser += strlen( "secam" );
281                 }
282                 else if( !strncmp( psz_parser, "auto", strlen( "auto" ) ) )
283                 {
284                     p_sys->i_norm = VIDEO_MODE_AUTO;
285                     psz_parser += strlen( "auto" );
286                 }
287                 else
288                 {
289                     p_sys->i_norm = strtol( psz_parser, &psz_parser, 0 );
290                 }
291             }
292             else if( !strncmp( psz_parser, "frequency=",
293                                strlen( "frequency=" ) ) )
294             {
295                 p_sys->i_frequency =
296                     strtol( psz_parser + strlen( "frequency=" ),
297                             &psz_parser, 0 );
298                 if( p_sys->i_frequency < 30000 )
299                 {
300                 msg_Warn( p_input, "v4l syntax has changed : 'frequency' is now channel frequency in kHz");
301                 }
302             }
303             else if( !strncmp( psz_parser, "audio=", strlen( "audio=" ) ) )
304             {
305                 p_sys->i_audio = strtol( psz_parser + strlen( "audio=" ),
306                                          &psz_parser, 0 );
307             }
308             else if( !strncmp( psz_parser, "size=", strlen( "size=" ) ) )
309             {
310                 psz_parser += strlen( "size=" );
311                 if( !strncmp( psz_parser, "subqcif", strlen( "subqcif" ) ) )
312                 {
313                     p_sys->i_width  = 128;
314                     p_sys->i_height = 96;
315                 }
316                 else if( !strncmp( psz_parser, "qsif", strlen( "qsif" ) ) )
317                 {
318                     p_sys->i_width  = 160;
319                     p_sys->i_height = 120;
320                 }
321                 else if( !strncmp( psz_parser, "qcif", strlen( "qcif" ) ) )
322                 {
323                     p_sys->i_width  = 176;
324                     p_sys->i_height = 144;
325                 }
326                 else if( !strncmp( psz_parser, "sif", strlen( "sif" ) ) )
327                 {
328                     p_sys->i_width  = 320;
329                     p_sys->i_height = 244;
330                 }
331                 else if( !strncmp( psz_parser, "cif", strlen( "cif" ) ) )
332                 {
333                     p_sys->i_width  = 352;
334                     p_sys->i_height = 288;
335                 }
336                 else if( !strncmp( psz_parser, "vga", strlen( "vga" ) ) )
337                 {
338                     p_sys->i_width  = 640;
339                     p_sys->i_height = 480;
340                 }
341                 else
342                 {
343                     /* widthxheight */
344                     p_sys->i_width = strtol( psz_parser, &psz_parser, 0 );
345                     if( *psz_parser == 'x' || *psz_parser == 'X')
346                     {
347                         p_sys->i_height = strtol( psz_parser + 1,
348                                                   &psz_parser, 0 );
349                     }
350                     msg_Dbg( p_input, "WxH %dx%d", p_sys->i_width,
351                              p_sys->i_height );
352                 }
353             }
354             else if( !strncmp( psz_parser, "tuner=", strlen( "tuner=" ) ) )
355             {
356                 p_sys->i_tuner = strtol( psz_parser + strlen( "tuner=" ),
357                                          &psz_parser, 0 );
358             }
359             else if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) ) )
360             {
361                 int  i_len;
362
363                 psz_parser += strlen( "adev=" );
364                 if( strchr( psz_parser, ':' ) )
365                 {
366                     i_len = strchr( psz_parser, ':' ) - psz_parser;
367                 }
368                 else
369                 {
370                     i_len = strlen( psz_parser );
371                 }
372
373                 p_sys->psz_adev = strndup( psz_parser, i_len );
374
375                 psz_parser += i_len;
376             }
377             else if( !strncmp( psz_parser, "samplerate=",
378                                strlen( "samplerate=" ) ) )
379             {
380                 p_sys->i_sample_rate =
381                     strtol( psz_parser + strlen( "samplerate=" ),
382                             &psz_parser, 0 );
383             }
384             else if( !strncmp( psz_parser, "stereo", strlen( "stereo" ) ) )
385             {
386                 psz_parser += strlen( "stereo" );
387
388                 p_sys->b_stereo = VLC_TRUE;
389             }
390             else if( !strncmp( psz_parser, "mono", strlen( "mono" ) ) )
391             {
392                 psz_parser += strlen( "mono" );
393
394                 p_sys->b_stereo = VLC_FALSE;
395             }
396             else if( !strncmp( psz_parser, "mjpeg", strlen( "mjpeg" ) ) )
397             {
398                 psz_parser += strlen( "mjpeg" );
399
400                 p_sys->b_mjpeg = VLC_TRUE;
401             }
402             else if( !strncmp( psz_parser, "decimation=",
403                         strlen( "decimation=" ) ) )
404             {
405                 p_sys->i_decimation =
406                     strtol( psz_parser + strlen( "decimation=" ),
407                             &psz_parser, 0 );
408             }
409             else if( !strncmp( psz_parser, "quality=",
410                         strlen( "quality=" ) ) )
411             {
412                 p_sys->i_quality =
413                     strtol( psz_parser + strlen( "quality=" ),
414                             &psz_parser, 0 );
415             }
416             else
417             {
418                 msg_Warn( p_input, "unknown option" );
419             }
420
421             while( *psz_parser && *psz_parser != ':' )
422             {
423                 psz_parser++;
424             }
425
426             if( *psz_parser == '\0' )
427             {
428                 break;
429             }
430         }
431     }
432
433     if( *psz_dup )
434     {
435         p_sys->psz_video_device = strdup( psz_dup );
436     }
437     else
438     {
439         p_sys->psz_video_device = strdup( "/dev/video" );
440     }
441     msg_Dbg( p_input, "video device=`%s'", p_sys->psz_video_device );
442
443     if( p_sys->psz_adev && *p_sys->psz_adev == '\0' )
444     {
445         p_sys->psz_adev = strdup( "/dev/dsp" );
446     }
447     msg_Dbg( p_input, "audio device=`%s'", p_sys->psz_adev );
448
449
450
451     if( ( p_sys->fd = open( p_sys->psz_video_device, O_RDWR ) ) < 0 )
452     {
453         msg_Err( p_input, "cannot open device (%s)", strerror( errno ) );
454         goto failed;
455     }
456
457     if( ioctl( p_sys->fd, VIDIOCGCAP, &p_sys->vid_cap ) < 0 )
458     {
459         msg_Err( p_input, "cannot get capabilities (%s)", strerror( errno ) );
460         goto failed;
461     }
462
463     msg_Dbg( p_input,
464              "V4L device %s %d channels %d audios %d < w < %d %d < h < %d",
465              p_sys->vid_cap.name,
466              p_sys->vid_cap.channels,
467              p_sys->vid_cap.audios,
468              p_sys->vid_cap.minwidth,  p_sys->vid_cap.maxwidth,
469              p_sys->vid_cap.minheight, p_sys->vid_cap.maxheight );
470
471     if( p_sys->i_channel < 0 || p_sys->i_channel >= p_sys->vid_cap.channels )
472     {
473         msg_Dbg( p_input, "invalid channel, falling back on channel 0" );
474         p_sys->i_channel = 0;
475     }
476     if( p_sys->i_audio >= p_sys->vid_cap.audios )
477     {
478         msg_Dbg( p_input, "invalid audio, falling back with no audio" );
479         p_sys->i_audio = -1;
480     }
481
482     if( p_sys->i_width < p_sys->vid_cap.minwidth ||
483         p_sys->i_width > p_sys->vid_cap.maxwidth )
484     {
485         msg_Dbg( p_input, "invalid width %i", p_sys->i_width );
486         p_sys->i_width = 0;
487     }
488     if( p_sys->i_height < p_sys->vid_cap.minheight ||
489         p_sys->i_height > p_sys->vid_cap.maxheight )
490     {
491         msg_Dbg( p_input, "invalid height %i", p_sys->i_height );
492         p_sys->i_height = 0;
493     }
494
495     if( !( p_sys->vid_cap.type & VID_TYPE_CAPTURE ) )
496     {
497         msg_Err( p_input, "cannot grab" );
498         goto failed;
499     }
500
501     vid_channel.channel = p_sys->i_channel;
502     if( ioctl( p_sys->fd, VIDIOCGCHAN, &vid_channel ) < 0 )
503     {
504         msg_Err( p_input, "cannot get channel infos (%s)",
505                           strerror( errno ) );
506         goto failed;
507     }
508     msg_Dbg( p_input,
509              "setting channel %s(%d) %d tuners flags=0x%x type=0x%x norm=0x%x",
510              vid_channel.name,
511              vid_channel.channel,
512              vid_channel.tuners,
513              vid_channel.flags,
514              vid_channel.type,
515              vid_channel.norm );
516
517     if( p_sys->i_tuner >= vid_channel.tuners )
518     {
519         msg_Dbg( p_input, "invalid tuner, falling back on tuner 0" );
520         p_sys->i_tuner = 0;
521     }
522
523     vid_channel.norm = p_sys->i_norm;
524     if( ioctl( p_sys->fd, VIDIOCSCHAN, &vid_channel ) < 0 )
525     {
526         msg_Err( p_input, "cannot set channel (%s)", strerror( errno ) );
527         goto failed;
528     }
529
530     if( vid_channel.flags & VIDEO_VC_TUNER )
531     {
532
533         /* set tuner */
534 #if 0
535         struct video_tuner vid_tuner;
536         if( p_sys->i_tuner >= 0 )
537         {
538             vid_tuner.tuner = p_sys->i_tuner;
539             if( ioctl( p_sys->fd, VIDIOCGTUNER, &vid_tuner ) < 0 )
540             {
541                 msg_Err( p_input, "cannot get tuner (%s)", strerror( errno ) );
542                 goto failed;
543             }
544             msg_Dbg( p_input, "tuner %s low=%d high=%d, flags=0x%x "
545                      "mode=0x%x signal=0x%x",
546                      vid_tuner.name, vid_tuner.rangelow, vid_tuner.rangehigh,
547                      vid_tuner.flags, vid_tuner.mode, vid_tuner.signal );
548
549             msg_Dbg( p_input, "setting tuner %s (%d)",
550                      vid_tuner.name, vid_tuner.tuner );
551
552             //vid_tuner.mode = p_sys->i_norm; /* FIXME FIXME to be checked FIXME FIXME */
553             if( ioctl( p_sys->fd, VIDIOCSTUNER, &vid_tuner ) < 0 )
554             {
555                 msg_Err( p_input, "cannot set tuner (%s)", strerror( errno ) );
556                 goto failed;
557             }
558         }
559 #endif
560
561         // show a warning if frequency is < than 30000. User is certainly usint old syntax.
562
563
564         /* set frequency */
565         if( p_sys->i_frequency >= 0 )
566         {
567             int driver_frequency = p_sys->i_frequency * 16 /1000;
568             if( ioctl( p_sys->fd, VIDIOCSFREQ, &driver_frequency ) < 0 )
569             {
570                 msg_Err( p_input, "cannot set frequency (%s)",
571                                   strerror( errno ) );
572                 goto failed;
573             }
574             msg_Dbg( p_input, "frequency %d (%d)", p_sys->i_frequency,
575                                                    driver_frequency );
576         }
577     }
578
579     /* set audio */
580     if( vid_channel.flags & VIDEO_VC_AUDIO )
581     {
582         struct video_audio      vid_audio;
583
584         /* XXX TODO volume, balance, ... */
585         if( p_sys->i_audio >= 0 )
586         {
587             vid_audio.audio = p_sys->i_audio;
588             if( ioctl( p_sys->fd, VIDIOCGAUDIO, &vid_audio ) < 0 )
589             {
590                 msg_Err( p_input, "cannot get audio (%s)", strerror( errno ) );
591                 goto failed;
592             }
593
594             /* unmute audio */
595             vid_audio.flags &= ~VIDEO_AUDIO_MUTE;
596
597             if( ioctl( p_sys->fd, VIDIOCSAUDIO, &vid_audio ) < 0 )
598             {
599                 msg_Err( p_input, "cannot set audio (%s)", strerror( errno ) );
600                 goto failed;
601             }
602         }
603
604     }
605
606     if( p_sys->psz_adev )
607     {
608         int    i_format;
609         if( ( p_sys->fd_audio = open( p_sys->psz_adev, O_RDONLY|O_NONBLOCK ) ) < 0 )
610         {
611             msg_Err( p_input, "cannot open audio device (%s)",
612                               strerror( errno ) );
613             goto failed;
614         }
615
616         i_format = AFMT_S16_LE;
617         if( ioctl( p_sys->fd_audio, SNDCTL_DSP_SETFMT, &i_format ) < 0
618             || i_format != AFMT_S16_LE )
619         {
620             msg_Err( p_input, "cannot set audio format (16b little endian) "
621                               "(%s)", strerror( errno ) );
622             goto failed;
623         }
624
625         if( ioctl( p_sys->fd_audio, SNDCTL_DSP_STEREO,
626                    &p_sys->b_stereo ) < 0 )
627         {
628             msg_Err( p_input, "cannot set audio channels count (%s)",
629                               strerror( errno ) );
630             goto failed;
631         }
632
633         if( ioctl( p_sys->fd_audio, SNDCTL_DSP_SPEED,
634                    &p_sys->i_sample_rate ) < 0 )
635         {
636             msg_Err( p_input, "cannot set audio sample rate (%s)",
637                               strerror( errno ) );
638             goto failed;
639         }
640
641         msg_Dbg( p_input,
642                  "adev=`%s' %s %dHz",
643                  p_sys->psz_adev,
644                  p_sys->b_stereo ? "stereo" : "mono",
645                  p_sys->i_sample_rate );
646
647         p_sys->i_audio_frame_size = 0;
648         p_sys->i_audio_frame_size_allocated = 6*1024;
649         p_sys->p_audio_frame =
650             malloc( p_sys->i_audio_frame_size_allocated );
651     }
652
653     /* establish basic params with input and norm before feeling width
654      * or height */
655     if( p_sys->b_mjpeg )
656     {
657         struct quicktime_mjpeg_app1 *p_app1;
658         int32_t i_offset;
659
660         if( ioctl( p_sys->fd, MJPIOC_G_PARAMS, &mjpeg ) < 0 )
661         {
662             msg_Err( p_input, "cannot get mjpeg params (%s)",
663                               strerror( errno ) );
664             goto failed;
665         }
666         mjpeg.input = p_sys->i_channel;
667         mjpeg.norm  = p_sys->i_norm;
668         mjpeg.decimation = p_sys->i_decimation;
669
670         if( p_sys->i_width )
671             mjpeg.img_width = p_sys->i_width / p_sys->i_decimation;
672         if( p_sys->i_height )
673             mjpeg.img_height = p_sys->i_height / p_sys->i_decimation;
674
675         /* establish Quicktime APP1 marker while we are here */
676         mjpeg.APPn = 1;
677         mjpeg.APP_len = 40;
678
679         /* aligned */
680         p_app1 = (struct quicktime_mjpeg_app1 *)mjpeg.APP_data;
681         p_app1->i_reserved = 0;
682         p_app1->i_tag = VLC_FOURCC( 'm','j','p','g' );
683         p_app1->i_field_size = 0;
684         p_app1->i_padded_field_size = 0;
685         p_app1->i_next_field = 0;
686         /* XXX WARNING XXX */
687         /* these's nothing magic about these values.  We are dangerously
688          * assuming the encoder card is encoding mjpeg-a and is not throwing
689          * in marker tags we aren't expecting.  It's bad enough we have to
690          * search through the jpeg output for every frame we grab just to
691          * find the first field's end marker, so we take this risk to boost
692          * performance.
693          * This is really something the driver could do for us because this
694          * does conform to standards outside of Apple Quicktime.
695          */
696         i_offset = 0x2e;
697         p_app1->i_DQT_offset = hton32( i_offset );
698         i_offset = 0xb4;
699         p_app1->i_DHT_offset = hton32( i_offset );
700         i_offset = 0x258;
701         p_app1->i_SOF_offset = hton32( i_offset );
702         i_offset = 0x26b;
703         p_app1->i_SOS_offset = hton32( i_offset );
704         i_offset = 0x279;
705         p_app1->i_data_offset = hton32( i_offset );
706
707         /* SOF and SOS aren't specified by the mjpeg API because they aren't
708          * optional.  They will be present in the output. */
709         mjpeg.jpeg_markers = JPEG_MARKER_DHT | JPEG_MARKER_DQT;
710
711         if( ioctl( p_sys->fd, MJPIOC_S_PARAMS, &mjpeg ) < 0 )
712         {
713             msg_Err( p_input, "cannot set mjpeg params (%s)",
714                               strerror( errno ) );
715             goto failed;
716         }
717
718         p_sys->i_width = mjpeg.img_width * mjpeg.HorDcm;
719         p_sys->i_height = mjpeg.img_height * mjpeg.VerDcm *
720             mjpeg.field_per_buff;
721     }
722
723     /* fix width/height */
724     if( !p_sys->b_mjpeg && ( p_sys->i_width == 0 || p_sys->i_height == 0 ) )
725     {
726         struct video_window vid_win;
727
728         if( ioctl( p_sys->fd, VIDIOCGWIN, &vid_win ) < 0 )
729         {
730             msg_Err( p_input, "cannot get win (%s)", strerror( errno ) );
731             goto failed;
732         }
733         p_sys->i_width  = vid_win.width;
734         p_sys->i_height = vid_win.height;
735
736         msg_Dbg( p_input, "will use %dx%d", p_sys->i_width, p_sys->i_height );
737     }
738
739     p_sys->p_video_frame = NULL;
740
741     if( !p_sys->b_mjpeg )
742     {
743         /* Find out video format used by device */
744         if( ioctl( p_sys->fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 )
745         {
746             int i_chroma = VLC_FOURCC( ' ', ' ', ' ', ' ' );
747             struct video_picture vid_picture = p_sys->vid_picture;
748
749             /* Try to set the format to something easy to encode */
750             vid_picture.palette = VIDEO_PALETTE_YUV420P;
751             if( ioctl( p_sys->fd, VIDIOCSPICT, &vid_picture ) == 0 )
752             {
753                 p_sys->vid_picture = vid_picture;
754             }
755             else
756             {
757                 vid_picture.palette = VIDEO_PALETTE_YUV422P;
758                 if( ioctl( p_sys->fd, VIDIOCSPICT, &vid_picture ) == 0 )
759                 {
760                     p_sys->vid_picture = vid_picture;
761                 }
762             }
763
764             /* Find out final format */
765             switch( p_sys->vid_picture.palette )
766             {
767             case VIDEO_PALETTE_GREY:
768                 i_chroma = VLC_FOURCC( 'G', 'R', 'E', 'Y' );
769                 break;
770             case VIDEO_PALETTE_HI240:
771                 i_chroma = VLC_FOURCC( 'I', '2', '4', '0' );
772                 break;
773             case VIDEO_PALETTE_RGB565:
774                 i_chroma = VLC_FOURCC( 'R', 'V', '1', '6' );
775                 break;
776             case VIDEO_PALETTE_RGB555:
777                 i_chroma = VLC_FOURCC( 'R', 'V', '1', '5' );
778                 break;
779             case VIDEO_PALETTE_RGB24:
780                 i_chroma = VLC_FOURCC( 'R', 'V', '2', '4' );
781                 break;
782             case VIDEO_PALETTE_RGB32:
783                 i_chroma = VLC_FOURCC( 'R', 'V', '3', '2' );
784                 break;
785             case VIDEO_PALETTE_YUV422:
786                 i_chroma = VLC_FOURCC( 'I', '4', '2', '2' );
787                 break;
788             case VIDEO_PALETTE_YUYV:
789                 i_chroma = VLC_FOURCC( 'Y', 'U', 'Y', 'V' );
790                 break;
791             case VIDEO_PALETTE_UYVY:
792                 i_chroma = VLC_FOURCC( 'U', 'Y', 'V', 'Y' );
793                 break;
794             case VIDEO_PALETTE_YUV420:
795                 i_chroma = VLC_FOURCC( 'I', '4', '2', 'N' );
796                 break;
797             case VIDEO_PALETTE_YUV411:
798                 i_chroma = VLC_FOURCC( 'I', '4', '1', 'N' );
799                 break;
800             case VIDEO_PALETTE_RAW:
801                 i_chroma = VLC_FOURCC( 'G', 'R', 'A', 'W' );
802                 break;
803             case VIDEO_PALETTE_YUV422P:
804                 i_chroma = VLC_FOURCC( 'I', '4', '2', '2' );
805                 break;
806             case VIDEO_PALETTE_YUV420P:
807                 i_chroma = VLC_FOURCC( 'I', '4', '2', '0' );
808                 break;
809             case VIDEO_PALETTE_YUV411P:
810                 i_chroma = VLC_FOURCC( 'I', '4', '1', '1' );
811                 break;
812             }
813             p_sys->i_fourcc = i_chroma;
814         }
815         else
816         {
817             msg_Err( p_input, "ioctl VIDIOCGPICT failed" );
818             goto failed;
819         }
820     }
821
822
823     if( p_sys->b_mjpeg )
824     {
825         int i;
826
827         p_sys->mjpeg_buffers.count = 8;
828         p_sys->mjpeg_buffers.size = MJPEG_BUFFER_SIZE;
829
830         if( ioctl( p_sys->fd, MJPIOC_REQBUFS, &p_sys->mjpeg_buffers ) < 0 )
831         {
832             msg_Err( p_input, "mmap unsupported" );
833             goto failed;
834         }
835
836         p_sys->p_video_mmap = mmap( 0,
837                 p_sys->mjpeg_buffers.size * p_sys->mjpeg_buffers.count,
838                 PROT_READ | PROT_WRITE, MAP_SHARED, p_sys->fd, 0 );
839         if( p_sys->p_video_mmap == MAP_FAILED )
840         {
841             msg_Err( p_input, "mmap failed" );
842             goto failed;
843         }
844
845         p_sys->i_fourcc  = VLC_FOURCC( 'm','j','p','g' );
846         p_sys->i_frame_pos = -1;
847
848         /* queue up all the frames */
849         for( i = 0; i < (int)p_sys->mjpeg_buffers.count; i++ )
850         {
851             if( ioctl( p_sys->fd, MJPIOC_QBUF_CAPT, &i ) < 0 )
852             {
853                 msg_Err( p_input, "unable to queue frame" );
854                 goto failed;
855             }
856         }
857     }
858     else
859     {
860         /* Fill in picture_t fields */
861         vout_InitPicture( VLC_OBJECT(p_input), &p_sys->pic, p_sys->i_fourcc,
862                           p_sys->i_width, p_sys->i_height, p_sys->i_width *
863                           VOUT_ASPECT_FACTOR / p_sys->i_height );
864         if( !p_sys->pic.i_planes )
865         {
866             msg_Err( p_input, "unsupported chroma" );
867             goto failed;
868         }
869         p_sys->i_video_frame_size = 0;
870         for( i = 0; i < p_sys->pic.i_planes; i++ )
871         {
872             p_sys->i_video_frame_size += p_sys->pic.p[i].i_lines *
873               p_sys->pic.p[i].i_visible_pitch;
874         }
875
876         msg_Dbg( p_input, "v4l device uses frame size: %i",
877                  p_sys->i_video_frame_size );
878         msg_Dbg( p_input, "v4l device uses chroma: %4.4s",
879                 (char*)&p_sys->i_fourcc );
880
881         /* Allocate mmap buffer */
882         if( ioctl( p_sys->fd, VIDIOCGMBUF, &p_sys->vid_mbuf ) < 0 )
883         {
884             msg_Err( p_input, "mmap unsupported" );
885             goto failed;
886         }
887
888         p_sys->p_video_mmap = mmap( 0, p_sys->vid_mbuf.size,
889                                     PROT_READ|PROT_WRITE, MAP_SHARED,
890                                     p_sys->fd, 0 );
891         if( p_sys->p_video_mmap == MAP_FAILED )
892         {
893             /* FIXME -> normal read */
894             msg_Err( p_input, "mmap failed" );
895             goto failed;
896         }
897
898         /* init grabbing */
899         p_sys->vid_mmap.frame  = 0;
900         p_sys->vid_mmap.width  = p_sys->i_width;
901         p_sys->vid_mmap.height = p_sys->i_height;
902         p_sys->vid_mmap.format = p_sys->vid_picture.palette;
903         if( ioctl( p_sys->fd, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 )
904         {
905             msg_Warn( p_input, "%4.4s refused", (char*)&p_sys->i_fourcc );
906             msg_Err( p_input, "chroma selection failed" );
907             goto failed;
908         }
909     }
910
911     p_input->pf_read        = Read;
912     p_input->pf_seek        = NULL;
913     p_input->pf_set_area    = NULL;
914     p_input->pf_set_program = NULL;
915
916     p_input->p_access_data  = p_sys;
917
918     vlc_mutex_lock( &p_input->stream.stream_lock );
919     p_input->stream.b_pace_control = 0;
920     p_input->stream.b_seekable = 0;
921     p_input->stream.p_selected_area->i_size = 0;
922     p_input->stream.p_selected_area->i_tell = 0;
923     p_input->stream.i_method = INPUT_METHOD_FILE;
924     vlc_mutex_unlock( &p_input->stream.stream_lock );
925
926     /* Update default_pts to a suitable value for access */
927     p_input->i_pts_delay = config_GetInt( p_input, "v4l-caching" ) * 1000;
928
929     msg_Info( p_input, "v4l grabbing started" );
930
931     /* create header */
932     p_sys->i_header_size = 8 + 20;
933     p_sys->i_header_pos  = 0;
934     p_sys->p_header      = malloc( p_sys->i_header_size );
935
936     memcpy(  &p_sys->p_header[0], ".v4l", 4 );
937     SetDWBE( &p_sys->p_header[4], 1 );
938
939     memcpy(  &p_sys->p_header[ 8], "vids", 4 );
940     memcpy(  &p_sys->p_header[12], &p_sys->i_fourcc, 4 );
941     SetDWBE( &p_sys->p_header[16], p_sys->i_width );
942     SetDWBE( &p_sys->p_header[20], p_sys->i_height );
943     SetDWBE( &p_sys->p_header[24], 0 );
944
945     if( p_sys->fd_audio > 0 )
946     {
947         p_sys->i_header_size += 20;
948         p_sys->p_header = realloc( p_sys->p_header, p_sys->i_header_size );
949
950         SetDWBE( &p_sys->p_header[4], 2 );
951
952         memcpy(  &p_sys->p_header[28], "auds", 4 );
953         memcpy(  &p_sys->p_header[32], "araw", 4 );
954         SetDWBE( &p_sys->p_header[36], p_sys->b_stereo ? 2 : 1 );
955         SetDWBE( &p_sys->p_header[40], p_sys->i_sample_rate );
956         SetDWBE( &p_sys->p_header[44], 16 );
957     }
958     return VLC_SUCCESS;
959
960 failed:
961     free( p_sys->psz_video_device );
962     if( p_sys->fd >= 0 )
963     {
964         close( p_sys->fd );
965     }
966     free( p_sys );
967     return VLC_EGENERIC;
968 }
969
970 /*****************************************************************************
971  * V4lClose: close device
972  *****************************************************************************/
973 static void AccessClose( vlc_object_t *p_this )
974 {
975     input_thread_t  *p_input = (input_thread_t *)p_this;
976     access_sys_t    *p_sys   = p_input->p_access_data;
977
978     msg_Info( p_input, "v4l grabbing stoped" );
979
980     if( p_sys->b_mjpeg )
981     {
982         int i_noframe = -1;
983         ioctl( p_sys->fd, MJPIOC_QBUF_CAPT, &i_noframe );
984     }
985
986     free( p_sys->psz_video_device );
987     close( p_sys->fd );
988     if( p_sys->p_video_mmap && p_sys->p_video_mmap != MAP_FAILED )
989     {
990         if( p_sys->b_mjpeg )
991             munmap( p_sys->p_video_mmap, p_sys->mjpeg_buffers.size *
992                     p_sys->mjpeg_buffers.count );
993         else
994             munmap( p_sys->p_video_mmap, p_sys->vid_mbuf.size );
995     }
996     if( p_sys->fd_audio >= 0 )
997     {
998         close( p_sys->fd_audio );
999     }
1000
1001     free( p_sys );
1002 }
1003
1004 static int GrabAudio( input_thread_t * p_input,
1005                       uint8_t **pp_data,
1006                       int      *pi_data,
1007                       mtime_t  *pi_pts )
1008 {
1009     access_sys_t    *p_sys   = p_input->p_access_data;
1010     struct audio_buf_info buf_info;
1011     int i_read;
1012     int i_correct;
1013
1014     i_read = read( p_sys->fd_audio, p_sys->p_audio_frame,
1015                    p_sys->i_audio_frame_size_allocated );
1016
1017     if( i_read <= 0 )
1018     {
1019         return VLC_EGENERIC;
1020     }
1021
1022     p_sys->i_audio_frame_size = i_read;
1023
1024     /* from vls : correct the date because of kernel buffering */
1025     i_correct = i_read;
1026     if( ioctl( p_sys->fd_audio, SNDCTL_DSP_GETISPACE, &buf_info ) == 0 )
1027     {
1028         i_correct += buf_info.bytes;
1029     }
1030
1031
1032     *pp_data = p_sys->p_audio_frame;
1033     *pi_data = p_sys->i_audio_frame_size;
1034     *pi_pts  = mdate() - (mtime_t)1000000 * (mtime_t)i_correct /
1035                          2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate;
1036     return VLC_SUCCESS;
1037 }
1038
1039 static uint8_t *GrabCapture( input_thread_t *p_input )
1040 {
1041     access_sys_t *p_sys = p_input->p_access_data;
1042     p_sys->vid_mmap.frame = ( p_sys->i_frame_pos + 1 ) %
1043                             p_sys->vid_mbuf.frames;
1044     for( ;; )
1045     {
1046         if( ioctl( p_sys->fd, VIDIOCMCAPTURE, &p_sys->vid_mmap ) >= 0 )
1047         {
1048             break;
1049         }
1050
1051         if( errno != EAGAIN )
1052         {
1053             msg_Err( p_input, "failed while grabbing new frame" );
1054             return( NULL );
1055         }
1056         msg_Dbg( p_input, "another try ?" );
1057     }
1058
1059     //msg_Warn( p_input, "grab a new frame" );
1060
1061     while( ioctl(p_sys->fd, VIDIOCSYNC, &p_sys->i_frame_pos) < 0 &&
1062            ( errno == EAGAIN || errno == EINTR ) );
1063
1064     p_sys->i_frame_pos = p_sys->vid_mmap.frame;
1065     /* leave i_video_frame_size alone */
1066     return p_sys->p_video_mmap + p_sys->vid_mbuf.offsets[p_sys->i_frame_pos];
1067 }
1068
1069 static uint8_t *GrabMJPEG( input_thread_t *p_input )
1070 {
1071     access_sys_t *p_sys = p_input->p_access_data;
1072     struct mjpeg_sync sync;
1073     uint8_t *p_frame, *p_field, *p;
1074     uint16_t tag;
1075     uint32_t i_size;
1076     struct quicktime_mjpeg_app1 *p_app1 = NULL;
1077
1078     /* re-queue the last frame we sync'd */
1079     if( p_sys->i_frame_pos != -1 )
1080         while( ioctl( p_sys->fd, MJPIOC_QBUF_CAPT, &p_sys->i_frame_pos ) < 0 &&
1081                 ( errno == EAGAIN || errno == EINTR ) );
1082
1083     /* sync on the next frame */
1084     while( ioctl( p_sys->fd, MJPIOC_SYNC, &sync ) < 0 &&
1085             ( errno == EAGAIN || errno == EINTR ) );
1086
1087     p_sys->i_frame_pos = sync.frame;
1088     p_frame = p_sys->p_video_mmap + p_sys->mjpeg_buffers.size * sync.frame;
1089
1090     /* p_frame now points to the data.  fix up the Quicktime APP1 marker */
1091     tag = 0xffd9;
1092     tag = hton16( tag );
1093     p_field = p_frame;
1094
1095     /* look for EOI */
1096     p = memmem( p_field, sync.length, &tag, 2 );
1097
1098     if( p )
1099     {
1100         p += 2; /* data immediately following EOI */
1101         /* UNALIGNED! */
1102         p_app1 = (struct quicktime_mjpeg_app1 *)(p_field + 6);
1103
1104         i_size = ((uint32_t)(p - p_field));
1105         i_size = hton32( i_size );
1106         memcpy( &p_app1->i_field_size, &i_size, 4 );
1107
1108         while( *p == 0xff && *(p+1) == 0xff )
1109             p++;
1110
1111         i_size = ((uint32_t)(p - p_field));
1112         i_size = hton32( i_size );
1113         memcpy( &p_app1->i_padded_field_size, &i_size, 4 );
1114     }
1115
1116     tag = 0xffd8;
1117     tag = hton16( tag );
1118     p_field = memmem( p, sync.length - (size_t)(p - p_frame), &tag, 2 );
1119
1120     if( p_field )
1121     {
1122         i_size = (uint32_t)(p_field - p_frame);
1123         i_size = hton32( i_size );
1124         memcpy( &p_app1->i_next_field, &i_size, 4 );
1125
1126         /* UNALIGNED! */
1127         p_app1 = (struct quicktime_mjpeg_app1 *)(p_field + 6);
1128         tag = 0xffd9;
1129         tag = hton16( tag );
1130         p = memmem( p_field, sync.length - (size_t)(p_field - p_frame),
1131                 &tag, 2 );
1132
1133         if( !p )
1134         {
1135             /* sometimes the second field doesn't have the EOI.  just put it
1136              * there
1137              */
1138             p = p_frame + sync.length;
1139             memcpy( p, &tag, 2 );
1140             sync.length += 2;
1141         }
1142
1143         p += 2;
1144         i_size = (uint32_t)(p - p_field);
1145         i_size = hton32( i_size );
1146         memcpy( &p_app1->i_field_size, &i_size, 4 );
1147         i_size = (uint32_t)(sync.length - (uint32_t)(p_field - p_frame));
1148         i_size = hton32( i_size );
1149         memcpy( &p_app1->i_padded_field_size, &i_size, 4 );
1150     }
1151
1152     p_sys->i_video_frame_size = sync.length;
1153     return p_frame;
1154 }
1155
1156 static int GrabVideo( input_thread_t * p_input,
1157                       uint8_t **pp_data,
1158                       int *pi_data,
1159                       mtime_t  *pi_pts )
1160 {
1161     access_sys_t *p_sys   = p_input->p_access_data;
1162     uint8_t *p_frame;
1163
1164     if( p_sys->b_mjpeg )
1165         p_frame = GrabMJPEG( p_input );
1166     else
1167         p_frame = GrabCapture( p_input );
1168
1169     if( !p_frame )
1170         return -1;
1171
1172     p_sys->p_video_frame = p_frame;
1173
1174     *pp_data = p_sys->p_video_frame;
1175     *pi_data = p_sys->i_video_frame_size;
1176     *pi_pts  = mdate();
1177     return VLC_SUCCESS;
1178 }
1179
1180 /*****************************************************************************
1181  * Read: reads from the device into PES packets.
1182  *****************************************************************************
1183  * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
1184  * bytes.
1185  *****************************************************************************/
1186 static int Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
1187 {
1188     access_sys_t *p_sys = p_input->p_access_data;
1189     int          i_data = 0;
1190     int          i_stream;
1191     mtime_t      i_pts;
1192
1193     //msg_Info( p_input, "access read data_size %i, data_pos %i",
1194                 //p_sys->i_data_size, p_sys->i_data_pos );
1195     while( i_len > 0 )
1196     {
1197         /* First copy header if any */
1198         if( i_len > 0 && p_sys->i_header_pos < p_sys->i_header_size )
1199         {
1200             int i_copy;
1201
1202             i_copy = __MIN( p_sys->i_header_size - p_sys->i_header_pos,
1203                             (int)i_len );
1204             memcpy( p_buffer, &p_sys->p_header[p_sys->i_header_pos], i_copy );
1205             p_sys->i_header_pos += i_copy;
1206
1207             p_buffer += i_copy;
1208             i_len -= i_copy;
1209             i_data += i_copy;
1210         }
1211
1212         /* then data */
1213         if( i_len > 0 && p_sys->i_data_pos < p_sys->i_data_size )
1214         {
1215             int i_copy;
1216
1217             i_copy = __MIN( p_sys->i_data_size - p_sys->i_data_pos,
1218                             (int)i_len );
1219
1220             memcpy( p_buffer, &p_sys->p_data[p_sys->i_data_pos], i_copy );
1221             p_sys->i_data_pos += i_copy;
1222
1223             p_buffer += i_copy;
1224             i_len -= i_copy;
1225             i_data += i_copy;
1226         }
1227
1228         /* The caller got what he wanted */
1229         if( i_len <= 0 )
1230         {
1231             return( i_data );
1232         }
1233
1234         /* Read no more than one frame at a time.
1235          * That kills latency, especially for encoded v4l streams */
1236         if( p_sys->i_data_size && p_sys->i_data_pos == p_sys->i_data_size )
1237         {
1238             p_sys->i_data_pos = 0; p_sys->i_data_size = 0;
1239             return( i_data );
1240         }
1241
1242         /* re fill data by grabbing audio/video */
1243         p_sys->i_data_pos = 0;
1244
1245         /* try grabbing audio frames */
1246         i_stream = 1;
1247         if( p_sys->fd_audio < 0 ||
1248             GrabAudio( p_input, &p_sys->p_data, &p_sys->i_data_size, &i_pts ) )
1249         {
1250             /* and then get video frame if no audio */
1251             i_stream = 0;
1252             if( GrabVideo( p_input, &p_sys->p_data, &p_sys->i_data_size, &i_pts ) )
1253             {
1254                 return -1;
1255             }
1256         }
1257
1258         /* create pseudo header */
1259         p_sys->i_header_size = 16;
1260         p_sys->i_header_pos  = 0;
1261         SetDWBE( &p_sys->p_header[0], i_stream );
1262         SetDWBE( &p_sys->p_header[4], p_sys->i_data_size );
1263         SetQWBE( &p_sys->p_header[8], i_pts );
1264     }
1265
1266     return i_data;
1267 }
1268
1269 /****************************************************************************
1270  * I. Demux Part
1271  ****************************************************************************/
1272 #define MAX_PACKETS_IN_FIFO 3
1273
1274 static int DemuxOpen( vlc_object_t *p_this )
1275 {
1276     input_thread_t *p_input = (input_thread_t *)p_this;
1277     //demux_sys_t    *p_sys;
1278
1279     uint8_t        *p_peek;
1280     int            i_streams;
1281     int            i;
1282
1283     data_packet_t  *p_pk;
1284
1285     /* Initialize access plug-in structures. */
1286     if( p_input->i_mtu == 0 )
1287     {
1288         /* Improve speed. */
1289         p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE ;
1290     }
1291
1292     /* a little test to see if it's a v4l stream */
1293     if( input_Peek( p_input, &p_peek, 8 ) < 8 )
1294     {
1295         msg_Warn( p_input, "v4l plugin discarded (cannot peek)" );
1296         return( VLC_EGENERIC );
1297     }
1298
1299     if( strncmp( p_peek, ".v4l", 4 ) || GetDWBE( &p_peek[4] ) <= 0 )
1300     {
1301         msg_Warn( p_input, "v4l plugin discarded (not a valid stream)" );
1302         return VLC_EGENERIC;
1303     }
1304
1305     /*  create one program */
1306     vlc_mutex_lock( &p_input->stream.stream_lock );
1307     if( input_InitStream( p_input, 0 ) == -1)
1308     {
1309         vlc_mutex_unlock( &p_input->stream.stream_lock );
1310         msg_Err( p_input, "cannot init stream" );
1311         return( VLC_EGENERIC );
1312     }
1313     if( input_AddProgram( p_input, 0, 0) == NULL )
1314     {
1315         vlc_mutex_unlock( &p_input->stream.stream_lock );
1316         msg_Err( p_input, "cannot add program" );
1317         return( VLC_EGENERIC );
1318     }
1319
1320     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
1321     p_input->stream.i_mux_rate =  0;
1322
1323     i_streams = GetDWBE( &p_peek[4] );
1324     if( input_Peek( p_input, &p_peek, 8 + 20 * i_streams )
1325         < 8 + 20 * i_streams )
1326     {
1327         msg_Err( p_input, "v4l plugin discarded (cannot peek)" );
1328         return( VLC_EGENERIC );
1329     }
1330     p_peek += 8;
1331
1332     for( i = 0; i < i_streams; i++ )
1333     {
1334         es_descriptor_t *p_es;
1335
1336         if( !strncmp( p_peek, "auds", 4 ) )
1337         {
1338 #define wf ((WAVEFORMATEX*)p_es->p_waveformatex)
1339             p_es = input_AddES( p_input, p_input->stream.pp_programs[0],
1340                                 i + 1, AUDIO_ES, NULL, 0 );
1341             p_es->i_stream_id   = i + 1;
1342             p_es->i_fourcc      =
1343                 VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] );
1344
1345             p_es->p_waveformatex= malloc( sizeof( WAVEFORMATEX ) );
1346
1347             wf->wFormatTag      = WAVE_FORMAT_UNKNOWN;
1348             wf->nChannels       = GetDWBE( &p_peek[8] );
1349             wf->nSamplesPerSec  = GetDWBE( &p_peek[12] );
1350             wf->wBitsPerSample  = GetDWBE( &p_peek[16] );
1351             wf->nBlockAlign     = wf->wBitsPerSample * wf->nChannels / 8;
1352             wf->nAvgBytesPerSec = wf->nBlockAlign * wf->nSamplesPerSec;
1353             wf->cbSize          = 0;
1354
1355             msg_Dbg( p_input, "added new audio es %d channels %dHz",
1356                      wf->nChannels, wf->nSamplesPerSec );
1357
1358             input_SelectES( p_input, p_es );
1359 #undef wf
1360         }
1361         else if( !strncmp( p_peek, "vids", 4 ) )
1362         {
1363 #define bih ((BITMAPINFOHEADER*)p_es->p_bitmapinfoheader)
1364             p_es = input_AddES( p_input, p_input->stream.pp_programs[0],
1365                                 i + 1, VIDEO_ES, NULL, 0 );
1366             p_es->i_stream_id   = i + 1;
1367             p_es->i_fourcc  =
1368                 VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] );
1369
1370             p_es->p_bitmapinfoheader = malloc( sizeof( BITMAPINFOHEADER ) );
1371
1372             bih->biSize     = sizeof( BITMAPINFOHEADER );
1373             bih->biWidth    = GetDWBE( &p_peek[8] );
1374             bih->biHeight   = GetDWBE( &p_peek[12] );
1375             bih->biPlanes   = 0;
1376             bih->biBitCount = 0;
1377             bih->biCompression      = 0;
1378             bih->biSizeImage= 0;
1379             bih->biXPelsPerMeter    = 0;
1380             bih->biYPelsPerMeter    = 0;
1381             bih->biClrUsed  = 0;
1382             bih->biClrImportant     = 0;
1383
1384             msg_Dbg( p_input, "added new video es %4.4s %dx%d",
1385                      (char*)&p_es->i_fourcc, bih->biWidth, bih->biHeight );
1386
1387             input_SelectES( p_input, p_es );
1388 #undef bih
1389         }
1390
1391         p_peek += 20;
1392     }
1393
1394     p_input->stream.p_selected_program->b_is_ok = 1;
1395     vlc_mutex_unlock( &p_input->stream.stream_lock );
1396
1397     if( input_SplitBuffer( p_input, &p_pk, 8 + i_streams * 20 ) > 0 )
1398     {
1399         input_DeletePacket( p_input->p_method_data, p_pk );
1400     }
1401
1402     p_input->pf_demux = Demux;
1403     p_input->pf_demux_control = demux_vaControlDefault;
1404     return VLC_SUCCESS;
1405 }
1406
1407 static void DemuxClose( vlc_object_t *p_this )
1408 {
1409     return;
1410 }
1411
1412 static int Demux( input_thread_t *p_input )
1413 {
1414     es_descriptor_t *p_es;
1415     pes_packet_t    *p_pes;
1416
1417     int i_stream;
1418     int i_size;
1419     uint8_t *p_peek;
1420     mtime_t        i_pts;
1421
1422     if( input_Peek( p_input, &p_peek, 16 ) < 16 )
1423     {
1424         msg_Warn( p_input, "cannot peek (EOF ?)" );
1425         return( 0 );
1426     }
1427
1428     i_stream = GetDWBE( &p_peek[0] );
1429     i_size   = GetDWBE( &p_peek[4] );
1430     i_pts    = GetQWBE( &p_peek[8] );
1431
1432     //msg_Dbg( p_input, "stream=%d size=%d", i_stream, i_size );
1433 //    p_es = input_FindES( p_input, i_stream );
1434     p_es = p_input->stream.p_selected_program->pp_es[i_stream];
1435     if( !p_es )
1436     {
1437         msg_Err( p_input, "cannot find ES" );
1438     }
1439
1440     p_pes = input_NewPES( p_input->p_method_data );
1441     if( p_pes == NULL )
1442     {
1443         msg_Warn( p_input, "cannot allocate PES" );
1444         msleep( 1000 );
1445         return( 1 );
1446     }
1447     i_size += 16;
1448     while( i_size > 0 )
1449     {
1450         data_packet_t   *p_data;
1451         int i_read;
1452
1453         if( (i_read = input_SplitBuffer( p_input, &p_data,
1454                                          __MIN( i_size, 10000 ) ) ) <= 0 )
1455         {
1456             input_DeletePES( p_input->p_method_data, p_pes );
1457             return( 0 );
1458         }
1459         if( !p_pes->p_first )
1460         {
1461             p_pes->p_first = p_data;
1462             p_pes->i_nb_data = 1;
1463             p_pes->i_pes_size = i_read;
1464         }
1465         else
1466         {
1467             p_pes->p_last->p_next  = p_data;
1468             p_pes->i_nb_data++;
1469             p_pes->i_pes_size += i_read;
1470         }
1471         p_pes->p_last  = p_data;
1472         i_size -= i_read;
1473     }
1474 //    input_SplitBuffer( p_input, &p_pk, i_size + 8 );
1475     p_pes->p_first->p_payload_start += 16;
1476     p_pes->i_pes_size               -= 16;
1477     if( p_es && p_es->p_decoder_fifo )
1478     {
1479         vlc_mutex_lock( &p_es->p_decoder_fifo->data_lock );
1480         if( p_es->p_decoder_fifo->i_depth >= MAX_PACKETS_IN_FIFO )
1481         {
1482             /* Wait for the decoder. */
1483             vlc_cond_wait( &p_es->p_decoder_fifo->data_wait,
1484                            &p_es->p_decoder_fifo->data_lock );
1485         }
1486         vlc_mutex_unlock( &p_es->p_decoder_fifo->data_lock );
1487         //p_pes->i_pts = mdate() + p_input->i_pts_delay;
1488         p_pes->i_pts = p_pes->i_dts = i_pts + p_input->i_pts_delay;
1489         input_DecodePES( p_es->p_decoder_fifo, p_pes );
1490     }
1491     else
1492     {
1493         input_DeletePES( p_input->p_method_data, p_pes );
1494     }
1495
1496     return 1;
1497 }