]> git.sesse.net Git - vlc/blob - modules/access/v4l/v4l.c
* all : set pf_demux_control.
[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.24 2003/09/07 22:49:05 fenrir 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,
862                           p_sys->i_width, p_sys->i_height, p_sys->i_fourcc );
863         if( !p_sys->pic.i_planes )
864         {
865             msg_Err( p_input, "unsupported chroma" );
866             goto failed;
867         }
868         p_sys->i_video_frame_size = 0;
869         for( i = 0; i < p_sys->pic.i_planes; i++ )
870         {
871             p_sys->i_video_frame_size += p_sys->pic.p[i].i_lines *
872               p_sys->pic.p[i].i_visible_pitch;
873         }
874
875         msg_Dbg( p_input, "v4l device uses frame size: %i",
876                  p_sys->i_video_frame_size );
877         msg_Dbg( p_input, "v4l device uses chroma: %4.4s",
878                 (char*)&p_sys->i_fourcc );
879
880         /* Allocate mmap buffer */
881         if( ioctl( p_sys->fd, VIDIOCGMBUF, &p_sys->vid_mbuf ) < 0 )
882         {
883             msg_Err( p_input, "mmap unsupported" );
884             goto failed;
885         }
886
887         p_sys->p_video_mmap = mmap( 0, p_sys->vid_mbuf.size,
888                                     PROT_READ|PROT_WRITE, MAP_SHARED,
889                                     p_sys->fd, 0 );
890         if( p_sys->p_video_mmap == MAP_FAILED )
891         {
892             /* FIXME -> normal read */
893             msg_Err( p_input, "mmap failed" );
894             goto failed;
895         }
896
897         /* init grabbing */
898         p_sys->vid_mmap.frame  = 0;
899         p_sys->vid_mmap.width  = p_sys->i_width;
900         p_sys->vid_mmap.height = p_sys->i_height;
901         p_sys->vid_mmap.format = p_sys->vid_picture.palette;
902         if( ioctl( p_sys->fd, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 )
903         {
904             msg_Warn( p_input, "%4.4s refused", (char*)&p_sys->i_fourcc );
905             msg_Err( p_input, "chroma selection failed" );
906             goto failed;
907         }
908     }
909
910     p_input->pf_read        = Read;
911     p_input->pf_seek        = NULL;
912     p_input->pf_set_area    = NULL;
913     p_input->pf_set_program = NULL;
914
915     p_input->p_access_data  = p_sys;
916
917     vlc_mutex_lock( &p_input->stream.stream_lock );
918     p_input->stream.b_pace_control = 0;
919     p_input->stream.b_seekable = 0;
920     p_input->stream.p_selected_area->i_size = 0;
921     p_input->stream.p_selected_area->i_tell = 0;
922     p_input->stream.i_method = INPUT_METHOD_FILE;
923     vlc_mutex_unlock( &p_input->stream.stream_lock );
924
925     /* Update default_pts to a suitable value for access */
926     p_input->i_pts_delay = config_GetInt( p_input, "v4l-caching" ) * 1000;
927
928     msg_Info( p_input, "v4l grabbing started" );
929
930     /* create header */
931     p_sys->i_header_size = 8 + 20;
932     p_sys->i_header_pos  = 0;
933     p_sys->p_header      = malloc( p_sys->i_header_size );
934
935     memcpy(  &p_sys->p_header[0], ".v4l", 4 );
936     SetDWBE( &p_sys->p_header[4], 1 );
937
938     memcpy(  &p_sys->p_header[ 8], "vids", 4 );
939     memcpy(  &p_sys->p_header[12], &p_sys->i_fourcc, 4 );
940     SetDWBE( &p_sys->p_header[16], p_sys->i_width );
941     SetDWBE( &p_sys->p_header[20], p_sys->i_height );
942     SetDWBE( &p_sys->p_header[24], 0 );
943
944     if( p_sys->fd_audio > 0 )
945     {
946         p_sys->i_header_size += 20;
947         p_sys->p_header = realloc( p_sys->p_header, p_sys->i_header_size );
948
949         SetDWBE( &p_sys->p_header[4], 2 );
950
951         memcpy(  &p_sys->p_header[28], "auds", 4 );
952         memcpy(  &p_sys->p_header[32], "araw", 4 );
953         SetDWBE( &p_sys->p_header[36], p_sys->b_stereo ? 2 : 1 );
954         SetDWBE( &p_sys->p_header[40], p_sys->i_sample_rate );
955         SetDWBE( &p_sys->p_header[44], 16 );
956     }
957     return VLC_SUCCESS;
958
959 failed:
960     free( p_sys->psz_video_device );
961     if( p_sys->fd >= 0 )
962     {
963         close( p_sys->fd );
964     }
965     free( p_sys );
966     return VLC_EGENERIC;
967 }
968
969 /*****************************************************************************
970  * V4lClose: close device
971  *****************************************************************************/
972 static void AccessClose( vlc_object_t *p_this )
973 {
974     input_thread_t  *p_input = (input_thread_t *)p_this;
975     access_sys_t    *p_sys   = p_input->p_access_data;
976
977     msg_Info( p_input, "v4l grabbing stoped" );
978
979     if( p_sys->b_mjpeg )
980     {
981         int i_noframe = -1;
982         ioctl( p_sys->fd, MJPIOC_QBUF_CAPT, &i_noframe );
983     }
984
985     free( p_sys->psz_video_device );
986     close( p_sys->fd );
987     if( p_sys->p_video_mmap && p_sys->p_video_mmap != MAP_FAILED )
988     {
989         if( p_sys->b_mjpeg )
990             munmap( p_sys->p_video_mmap, p_sys->mjpeg_buffers.size *
991                     p_sys->mjpeg_buffers.count );
992         else
993             munmap( p_sys->p_video_mmap, p_sys->vid_mbuf.size );
994     }
995     if( p_sys->fd_audio >= 0 )
996     {
997         close( p_sys->fd_audio );
998     }
999
1000     free( p_sys );
1001 }
1002
1003 static int GrabAudio( input_thread_t * p_input,
1004                       uint8_t **pp_data,
1005                       int      *pi_data,
1006                       mtime_t  *pi_pts )
1007 {
1008     access_sys_t    *p_sys   = p_input->p_access_data;
1009     struct audio_buf_info buf_info;
1010     int i_read;
1011     int i_correct;
1012
1013     i_read = read( p_sys->fd_audio, p_sys->p_audio_frame,
1014                    p_sys->i_audio_frame_size_allocated );
1015
1016     if( i_read <= 0 )
1017     {
1018         return VLC_EGENERIC;
1019     }
1020
1021     p_sys->i_audio_frame_size = i_read;
1022
1023     /* from vls : correct the date because of kernel buffering */
1024     i_correct = i_read;
1025     if( ioctl( p_sys->fd_audio, SNDCTL_DSP_GETISPACE, &buf_info ) == 0 )
1026     {
1027         i_correct += buf_info.bytes;
1028     }
1029
1030
1031     *pp_data = p_sys->p_audio_frame;
1032     *pi_data = p_sys->i_audio_frame_size;
1033     *pi_pts  = mdate() - (mtime_t)1000000 * (mtime_t)i_correct /
1034                          2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate;
1035     return VLC_SUCCESS;
1036 }
1037
1038 static uint8_t *GrabCapture( input_thread_t *p_input )
1039 {
1040     access_sys_t *p_sys = p_input->p_access_data;
1041     p_sys->vid_mmap.frame = ( p_sys->i_frame_pos + 1 ) %
1042                             p_sys->vid_mbuf.frames;
1043     for( ;; )
1044     {
1045         if( ioctl( p_sys->fd, VIDIOCMCAPTURE, &p_sys->vid_mmap ) >= 0 )
1046         {
1047             break;
1048         }
1049
1050         if( errno != EAGAIN )
1051         {
1052             msg_Err( p_input, "failed while grabbing new frame" );
1053             return( NULL );
1054         }
1055         msg_Dbg( p_input, "another try ?" );
1056     }
1057
1058     //msg_Warn( p_input, "grab a new frame" );
1059
1060     while( ioctl(p_sys->fd, VIDIOCSYNC, &p_sys->i_frame_pos) < 0 &&
1061            ( errno == EAGAIN || errno == EINTR ) );
1062
1063     p_sys->i_frame_pos = p_sys->vid_mmap.frame;
1064     /* leave i_video_frame_size alone */
1065     return p_sys->p_video_mmap + p_sys->vid_mbuf.offsets[p_sys->i_frame_pos];
1066 }
1067
1068 static uint8_t *GrabMJPEG( input_thread_t *p_input )
1069 {
1070     access_sys_t *p_sys = p_input->p_access_data;
1071     struct mjpeg_sync sync;
1072     uint8_t *p_frame, *p_field, *p;
1073     uint16_t tag;
1074     uint32_t i_size;
1075     struct quicktime_mjpeg_app1 *p_app1 = NULL;
1076
1077     /* re-queue the last frame we sync'd */
1078     if( p_sys->i_frame_pos != -1 )
1079         while( ioctl( p_sys->fd, MJPIOC_QBUF_CAPT, &p_sys->i_frame_pos ) < 0 &&
1080                 ( errno == EAGAIN || errno == EINTR ) );
1081
1082     /* sync on the next frame */
1083     while( ioctl( p_sys->fd, MJPIOC_SYNC, &sync ) < 0 &&
1084             ( errno == EAGAIN || errno == EINTR ) );
1085
1086     p_sys->i_frame_pos = sync.frame;
1087     p_frame = p_sys->p_video_mmap + p_sys->mjpeg_buffers.size * sync.frame;
1088
1089     /* p_frame now points to the data.  fix up the Quicktime APP1 marker */
1090     tag = 0xffd9;
1091     tag = hton16( tag );
1092     p_field = p_frame;
1093
1094     /* look for EOI */
1095     p = memmem( p_field, sync.length, &tag, 2 );
1096
1097     if( p )
1098     {
1099         p += 2; /* data immediately following EOI */
1100         /* UNALIGNED! */
1101         p_app1 = (struct quicktime_mjpeg_app1 *)(p_field + 6);
1102
1103         i_size = ((uint32_t)(p - p_field));
1104         i_size = hton32( i_size );
1105         memcpy( &p_app1->i_field_size, &i_size, 4 );
1106
1107         while( *p == 0xff && *(p+1) == 0xff )
1108             p++;
1109
1110         i_size = ((uint32_t)(p - p_field));
1111         i_size = hton32( i_size );
1112         memcpy( &p_app1->i_padded_field_size, &i_size, 4 );
1113     }
1114
1115     tag = 0xffd8;
1116     tag = hton16( tag );
1117     p_field = memmem( p, sync.length - (size_t)(p - p_frame), &tag, 2 );
1118
1119     if( p_field )
1120     {
1121         i_size = (uint32_t)(p_field - p_frame);
1122         i_size = hton32( i_size );
1123         memcpy( &p_app1->i_next_field, &i_size, 4 );
1124
1125         /* UNALIGNED! */
1126         p_app1 = (struct quicktime_mjpeg_app1 *)(p_field + 6);
1127         tag = 0xffd9;
1128         tag = hton16( tag );
1129         p = memmem( p_field, sync.length - (size_t)(p_field - p_frame), 
1130                 &tag, 2 );
1131
1132         if( !p )
1133         {
1134             /* sometimes the second field doesn't have the EOI.  just put it
1135              * there
1136              */
1137             p = p_frame + sync.length;
1138             memcpy( p, &tag, 2 );
1139             sync.length += 2;
1140         }
1141
1142         p += 2;
1143         i_size = (uint32_t)(p - p_field);
1144         i_size = hton32( i_size );
1145         memcpy( &p_app1->i_field_size, &i_size, 4 );
1146         i_size = (uint32_t)(sync.length - (uint32_t)(p_field - p_frame));
1147         i_size = hton32( i_size );
1148         memcpy( &p_app1->i_padded_field_size, &i_size, 4 );
1149     }
1150
1151     p_sys->i_video_frame_size = sync.length;
1152     return p_frame;
1153 }
1154
1155 static int GrabVideo( input_thread_t * p_input,
1156                       uint8_t **pp_data,
1157                       int *pi_data,
1158                       mtime_t  *pi_pts )
1159 {
1160     access_sys_t *p_sys   = p_input->p_access_data;
1161     uint8_t *p_frame;
1162
1163     if( p_sys->b_mjpeg )
1164         p_frame = GrabMJPEG( p_input );
1165     else
1166         p_frame = GrabCapture( p_input );
1167
1168     if( !p_frame )
1169         return -1;
1170
1171     p_sys->p_video_frame = p_frame;
1172
1173     *pp_data = p_sys->p_video_frame;
1174     *pi_data = p_sys->i_video_frame_size;
1175     *pi_pts  = mdate();
1176     return VLC_SUCCESS;
1177 }
1178
1179 /*****************************************************************************
1180  * Read: reads from the device into PES packets.
1181  *****************************************************************************
1182  * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
1183  * bytes.
1184  *****************************************************************************/
1185 static int Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
1186 {
1187     access_sys_t *p_sys = p_input->p_access_data;
1188     int          i_data = 0;
1189     int          i_stream;
1190     mtime_t      i_pts;
1191
1192     //msg_Info( p_input, "access read data_size %i, data_pos %i",
1193                 //p_sys->i_data_size, p_sys->i_data_pos );
1194     while( i_len > 0 )
1195     {
1196         /* First copy header if any */
1197         if( i_len > 0 && p_sys->i_header_pos < p_sys->i_header_size )
1198         {
1199             int i_copy;
1200
1201             i_copy = __MIN( p_sys->i_header_size - p_sys->i_header_pos,
1202                             (int)i_len );
1203             memcpy( p_buffer, &p_sys->p_header[p_sys->i_header_pos], i_copy );
1204             p_sys->i_header_pos += i_copy;
1205
1206             p_buffer += i_copy;
1207             i_len -= i_copy;
1208             i_data += i_copy;
1209         }
1210
1211         /* then data */
1212         if( i_len > 0 && p_sys->i_data_pos < p_sys->i_data_size )
1213         {
1214             int i_copy;
1215
1216             i_copy = __MIN( p_sys->i_data_size - p_sys->i_data_pos,
1217                             (int)i_len );
1218
1219             memcpy( p_buffer, &p_sys->p_data[p_sys->i_data_pos], i_copy );
1220             p_sys->i_data_pos += i_copy;
1221
1222             p_buffer += i_copy;
1223             i_len -= i_copy;
1224             i_data += i_copy;
1225         }
1226
1227         /* The caller got what he wanted */
1228         if( i_len <= 0 )
1229         {
1230             return( i_data );
1231         }
1232
1233         /* Read no more than one frame at a time.
1234          * That kills latency, especially for encoded v4l streams */
1235         if( p_sys->i_data_size && p_sys->i_data_pos == p_sys->i_data_size )
1236         {
1237             p_sys->i_data_pos = 0; p_sys->i_data_size = 0;
1238             return( i_data );
1239         }
1240
1241         /* re fill data by grabbing audio/video */
1242         p_sys->i_data_pos = 0;
1243
1244         /* try grabbing audio frames */
1245         i_stream = 1;
1246         if( p_sys->fd_audio < 0 ||
1247             GrabAudio( p_input, &p_sys->p_data, &p_sys->i_data_size, &i_pts ) )
1248         {
1249             /* and then get video frame if no audio */
1250             i_stream = 0;
1251             if( GrabVideo( p_input, &p_sys->p_data, &p_sys->i_data_size, &i_pts ) )
1252             {
1253                 return -1;
1254             }
1255         }
1256
1257         /* create pseudo header */
1258         p_sys->i_header_size = 16;
1259         p_sys->i_header_pos  = 0;
1260         SetDWBE( &p_sys->p_header[0], i_stream );
1261         SetDWBE( &p_sys->p_header[4], p_sys->i_data_size );
1262         SetQWBE( &p_sys->p_header[8], i_pts );
1263     }
1264
1265     return i_data;
1266 }
1267
1268 /****************************************************************************
1269  * I. Demux Part
1270  ****************************************************************************/
1271 #define MAX_PACKETS_IN_FIFO 3
1272
1273 static int DemuxOpen( vlc_object_t *p_this )
1274 {
1275     input_thread_t *p_input = (input_thread_t *)p_this;
1276     //demux_sys_t    *p_sys;
1277
1278     uint8_t        *p_peek;
1279     int            i_streams;
1280     int            i;
1281
1282     data_packet_t  *p_pk;
1283
1284     /* Initialize access plug-in structures. */
1285     if( p_input->i_mtu == 0 )
1286     {
1287         /* Improve speed. */
1288         p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE ;
1289     }
1290
1291     /* a little test to see if it's a v4l stream */
1292     if( input_Peek( p_input, &p_peek, 8 ) < 8 )
1293     {
1294         msg_Warn( p_input, "v4l plugin discarded (cannot peek)" );
1295         return( VLC_EGENERIC );
1296     }
1297
1298     if( strncmp( p_peek, ".v4l", 4 ) || GetDWBE( &p_peek[4] ) <= 0 )
1299     {
1300         msg_Warn( p_input, "v4l plugin discarded (not a valid stream)" );
1301         return VLC_EGENERIC;
1302     }
1303
1304     /*  create one program */
1305     vlc_mutex_lock( &p_input->stream.stream_lock );
1306     if( input_InitStream( p_input, 0 ) == -1)
1307     {
1308         vlc_mutex_unlock( &p_input->stream.stream_lock );
1309         msg_Err( p_input, "cannot init stream" );
1310         return( VLC_EGENERIC );
1311     }
1312     if( input_AddProgram( p_input, 0, 0) == NULL )
1313     {
1314         vlc_mutex_unlock( &p_input->stream.stream_lock );
1315         msg_Err( p_input, "cannot add program" );
1316         return( VLC_EGENERIC );
1317     }
1318
1319     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
1320     p_input->stream.i_mux_rate =  0;
1321
1322     i_streams = GetDWBE( &p_peek[4] );
1323     if( input_Peek( p_input, &p_peek, 8 + 20 * i_streams )
1324         < 8 + 20 * i_streams )
1325     {
1326         msg_Err( p_input, "v4l plugin discarded (cannot peek)" );
1327         return( VLC_EGENERIC );
1328     }
1329     p_peek += 8;
1330
1331     for( i = 0; i < i_streams; i++ )
1332     {
1333         es_descriptor_t *p_es;
1334
1335         if( !strncmp( p_peek, "auds", 4 ) )
1336         {
1337 #define wf ((WAVEFORMATEX*)p_es->p_waveformatex)
1338             p_es = input_AddES( p_input, p_input->stream.pp_programs[0],
1339                                 i + 1, AUDIO_ES, NULL, 0 );
1340             p_es->i_stream_id   = i + 1;
1341             p_es->i_fourcc      =
1342                 VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] );
1343
1344             p_es->p_waveformatex= malloc( sizeof( WAVEFORMATEX ) );
1345
1346             wf->wFormatTag      = WAVE_FORMAT_UNKNOWN;
1347             wf->nChannels       = GetDWBE( &p_peek[8] );
1348             wf->nSamplesPerSec  = GetDWBE( &p_peek[12] );
1349             wf->wBitsPerSample  = GetDWBE( &p_peek[16] );
1350             wf->nBlockAlign     = wf->wBitsPerSample * wf->nChannels / 8;
1351             wf->nAvgBytesPerSec = wf->nBlockAlign * wf->nSamplesPerSec;
1352             wf->cbSize          = 0;
1353
1354             msg_Dbg( p_input, "added new audio es %d channels %dHz",
1355                      wf->nChannels, wf->nSamplesPerSec );
1356
1357             input_SelectES( p_input, p_es );
1358 #undef wf
1359         }
1360         else if( !strncmp( p_peek, "vids", 4 ) )
1361         {
1362 #define bih ((BITMAPINFOHEADER*)p_es->p_bitmapinfoheader)
1363             p_es = input_AddES( p_input, p_input->stream.pp_programs[0],
1364                                 i + 1, VIDEO_ES, NULL, 0 );
1365             p_es->i_stream_id   = i + 1;
1366             p_es->i_fourcc  =
1367                 VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] );
1368
1369             p_es->p_bitmapinfoheader = malloc( sizeof( BITMAPINFOHEADER ) );
1370
1371             bih->biSize     = sizeof( BITMAPINFOHEADER );
1372             bih->biWidth    = GetDWBE( &p_peek[8] );
1373             bih->biHeight   = GetDWBE( &p_peek[12] );
1374             bih->biPlanes   = 0;
1375             bih->biBitCount = 0;
1376             bih->biCompression      = 0;
1377             bih->biSizeImage= 0;
1378             bih->biXPelsPerMeter    = 0;
1379             bih->biYPelsPerMeter    = 0;
1380             bih->biClrUsed  = 0;
1381             bih->biClrImportant     = 0;
1382
1383             msg_Dbg( p_input, "added new video es %4.4s %dx%d",
1384                      (char*)&p_es->i_fourcc, bih->biWidth, bih->biHeight );
1385
1386             input_SelectES( p_input, p_es );
1387 #undef bih
1388         }
1389
1390         p_peek += 20;
1391     }
1392
1393     p_input->stream.p_selected_program->b_is_ok = 1;
1394     vlc_mutex_unlock( &p_input->stream.stream_lock );
1395
1396     if( input_SplitBuffer( p_input, &p_pk, 8 + i_streams * 20 ) > 0 )
1397     {
1398         input_DeletePacket( p_input->p_method_data, p_pk );
1399     }
1400
1401     p_input->pf_demux = Demux;
1402     p_input->pf_demux_control = demux_vaControlDefault;
1403     return VLC_SUCCESS;
1404 }
1405
1406 static void DemuxClose( vlc_object_t *p_this )
1407 {
1408     return;
1409 }
1410
1411 static int Demux( input_thread_t *p_input )
1412 {
1413     es_descriptor_t *p_es;
1414     pes_packet_t    *p_pes;
1415
1416     int i_stream;
1417     int i_size;
1418     uint8_t *p_peek;
1419     mtime_t        i_pts;
1420
1421     if( input_Peek( p_input, &p_peek, 16 ) < 16 )
1422     {
1423         msg_Warn( p_input, "cannot peek (EOF ?)" );
1424         return( 0 );
1425     }
1426
1427     i_stream = GetDWBE( &p_peek[0] );
1428     i_size   = GetDWBE( &p_peek[4] );
1429     i_pts    = GetQWBE( &p_peek[8] );
1430
1431     //msg_Dbg( p_input, "stream=%d size=%d", i_stream, i_size );
1432 //    p_es = input_FindES( p_input, i_stream );
1433     p_es = p_input->stream.p_selected_program->pp_es[i_stream];
1434     if( !p_es )
1435     {
1436         msg_Err( p_input, "cannot find ES" );
1437     }
1438
1439     p_pes = input_NewPES( p_input->p_method_data );
1440     if( p_pes == NULL )
1441     {
1442         msg_Warn( p_input, "cannot allocate PES" );
1443         msleep( 1000 );
1444         return( 1 );
1445     }
1446     i_size += 16;
1447     while( i_size > 0 )
1448     {
1449         data_packet_t   *p_data;
1450         int i_read;
1451
1452         if( (i_read = input_SplitBuffer( p_input, &p_data,
1453                                          __MIN( i_size, 10000 ) ) ) <= 0 )
1454         {
1455             input_DeletePES( p_input->p_method_data, p_pes );
1456             return( 0 );
1457         }
1458         if( !p_pes->p_first )
1459         {
1460             p_pes->p_first = p_data;
1461             p_pes->i_nb_data = 1;
1462             p_pes->i_pes_size = i_read;
1463         }
1464         else
1465         {
1466             p_pes->p_last->p_next  = p_data;
1467             p_pes->i_nb_data++;
1468             p_pes->i_pes_size += i_read;
1469         }
1470         p_pes->p_last  = p_data;
1471         i_size -= i_read;
1472     }
1473 //    input_SplitBuffer( p_input, &p_pk, i_size + 8 );
1474     p_pes->p_first->p_payload_start += 16;
1475     p_pes->i_pes_size               -= 16;
1476     if( p_es && p_es->p_decoder_fifo )
1477     {
1478         vlc_mutex_lock( &p_es->p_decoder_fifo->data_lock );
1479         if( p_es->p_decoder_fifo->i_depth >= MAX_PACKETS_IN_FIFO )
1480         {
1481             /* Wait for the decoder. */
1482             vlc_cond_wait( &p_es->p_decoder_fifo->data_wait,
1483                            &p_es->p_decoder_fifo->data_lock );
1484         }
1485         vlc_mutex_unlock( &p_es->p_decoder_fifo->data_lock );
1486         //p_pes->i_pts = mdate() + p_input->i_pts_delay;
1487         p_pes->i_pts = p_pes->i_dts = i_pts + p_input->i_pts_delay;
1488         input_DecodePES( p_es->p_decoder_fifo, p_pes );
1489     }
1490     else
1491     {
1492         input_DeletePES( p_input->p_method_data, p_pes );
1493     }
1494
1495     return 1;
1496 }