]> git.sesse.net Git - vlc/blob - modules/access/v4l/v4l.c
f154a73c7a3a377c434cd9bc2e267713fe3aaac0
[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.21 2003/08/28 21:11:55 gbazin 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 #include "encoder.h"
37
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <sys/ioctl.h>
41 #include <unistd.h>
42 #include <sys/mman.h>
43 #include <errno.h>
44
45 #include <fcntl.h>
46 #include <linux/videodev.h>
47 #include "videodev_mjpeg.h"
48
49 #include <sys/soundcard.h>
50
51 /*****************************************************************************
52  * Local prototypes
53  *****************************************************************************/
54 static int  AccessOpen  ( vlc_object_t * );
55 static void AccessClose ( vlc_object_t * );
56 static int  Read        ( input_thread_t *, byte_t *, size_t );
57
58 static int  DemuxOpen  ( vlc_object_t * );
59 static void DemuxClose ( vlc_object_t * );
60 static int  Demux      ( input_thread_t * );
61
62
63 /*****************************************************************************
64  * Module descriptior
65  *****************************************************************************/
66 #define CACHING_TEXT N_("Caching value in ms")
67 #define CACHING_LONGTEXT N_( \
68     "Allows you to modify the default caching value for v4l streams. This " \
69     "value should be set in miliseconds units." )
70
71 vlc_module_begin();
72     set_description( _("Video4Linux input") );
73     add_category_hint( N_("v4l"), NULL, VLC_TRUE );
74     add_integer( "v4l-caching", DEFAULT_PTS_DELAY / 1000, NULL,
75                  CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
76     add_shortcut( "v4l" );
77     set_capability( "access", 10 );
78     set_callbacks( AccessOpen, AccessClose );
79
80     add_submodule();
81         set_description( _("Video4Linux demuxer") );
82         add_shortcut( "v4l" );
83         set_capability( "demux", 200 );
84         set_callbacks( DemuxOpen, DemuxClose );
85 vlc_module_end();
86
87
88 /****************************************************************************
89  * I. Access Part
90  ****************************************************************************/
91 #define MJPEG_BUFFER_SIZE (256*1024)
92
93 struct quicktime_mjpeg_app1
94 {
95     uint32_t    i_reserved;             /* set to 0 */
96     uint32_t    i_tag;                  /* 'mjpg' */
97     uint32_t    i_field_size;           /* offset following EOI */
98     uint32_t    i_padded_field_size;    /* offset following EOI+pad */
99     uint32_t    i_next_field;           /* offset to next field */
100     uint32_t    i_DQT_offset;
101     uint32_t    i_DHT_offset;
102     uint32_t    i_SOF_offset;
103     uint32_t    i_SOS_offset;
104     uint32_t    i_data_offset;          /* following SOS marker data */
105 };
106
107 struct access_sys_t
108 {
109     char    *psz_video_device;
110     int     fd;
111
112     picture_t       pic;
113
114     int i_fourcc;
115     int i_channel;
116     int i_audio;
117     int i_norm;
118     int i_tuner;
119     int i_frequency;
120     int i_width;
121     int i_height;
122
123     vlc_bool_t b_mjpeg;
124     int i_decimation;
125     int i_quality;
126
127     struct video_capability vid_cap;
128     struct video_mbuf       vid_mbuf;
129     struct mjpeg_requestbuffers mjpeg_buffers;
130
131     uint8_t *p_video_mmap;
132     int     i_frame_pos;
133
134     struct video_mmap   vid_mmap;
135     struct video_picture vid_picture;
136
137     uint8_t *p_video_frame;
138     int     i_video_frame_size;
139     int     i_video_frame_size_allocated;
140
141     char         *psz_adev;
142     int          fd_audio;
143     vlc_fourcc_t i_acodec_raw;
144     int          i_sample_rate;
145     vlc_bool_t   b_stereo;
146
147     uint8_t *p_audio_frame;
148     int     i_audio_frame_size;
149     int     i_audio_frame_size_allocated;
150
151     /* header */
152     int     i_header_size;
153     int     i_header_pos;
154     uint8_t *p_header;      // at lest 8 bytes allocated
155
156     /* data */
157     int     i_data_size;
158     int     i_data_pos;
159     uint8_t *p_data;        // never allocated
160
161 };
162
163 /*
164  * header:
165  *  fcc  ".v4l"
166  *  u32    stream count
167  *      fcc "auds"|"vids"       0
168  *      fcc codec               4
169  *      if vids
170  *          u32 width           8
171  *          u32 height          12
172  *          u32 padding         16
173  *      if auds
174  *          u32 channels        12
175  *          u32 samplerate      8
176  *          u32 samplesize      16
177  *
178  * data:
179  *  u32     stream number
180  *  u32     data size
181  *  u8      data
182  */
183
184 static void    SetDWBE( uint8_t *p, uint32_t dw )
185 {
186     p[0] = (dw >> 24)&0xff;
187     p[1] = (dw >> 16)&0xff;
188     p[2] = (dw >>  8)&0xff;
189     p[3] = (dw      )&0xff;
190 }
191
192 static void    SetQWBE( uint8_t *p, uint64_t qw )
193 {
194     SetDWBE( p,     (qw >> 32)&0xffffffff );
195     SetDWBE( &p[4], qw&0xffffffff);
196 }
197
198 /*****************************************************************************
199  * Open: open device:
200  *****************************************************************************
201  *
202  * url: <video device>::::
203  *
204  *****************************************************************************/
205 static int AccessOpen( vlc_object_t *p_this )
206 {
207     input_thread_t *p_input = (input_thread_t *)p_this;
208     access_sys_t   *p_sys;
209     char           *psz_dup, *psz_parser;
210     struct mjpeg_params mjpeg;
211     int i;
212
213     struct video_channel    vid_channel;
214
215     /* create access private data */
216     p_sys = malloc( sizeof( access_sys_t ) );
217     memset( p_sys, 0, sizeof( access_sys_t ) );
218
219     p_sys->psz_video_device = NULL;
220     p_sys->fd               = -1;
221     p_sys->i_channel        = -1;
222     p_sys->i_audio          = -1;
223     p_sys->i_norm           = VIDEO_MODE_AUTO;    // auto
224     p_sys->i_tuner          = -1;
225     p_sys->i_frequency      = -1;
226     p_sys->i_width          = 0;
227     p_sys->i_height         = 0;
228
229     p_sys->b_mjpeg     = VLC_FALSE;
230     p_sys->i_decimation = 1;
231     p_sys->i_quality = 100;
232
233     p_sys->i_frame_pos = 0;
234
235     p_sys->i_video_frame_size_allocated = 0;
236     p_sys->psz_adev         = NULL;
237     p_sys->fd_audio         = -1;
238     p_sys->i_sample_rate    = 44100;
239     p_sys->b_stereo         = VLC_TRUE;
240
241     p_sys->i_data_size = 0;
242     p_sys->i_data_pos  = 0;
243     p_sys->p_data      = NULL;
244
245     /* parse url and open device(s) */
246     psz_dup = strdup( p_input->psz_name );
247     psz_parser = psz_dup;
248
249     while( *psz_parser && *psz_parser != ':' )
250     {
251         psz_parser++;
252     }
253
254     if( *psz_parser == ':' )
255     {
256         /* read options */
257         for( ;; )
258         {
259             *psz_parser++ = '\0';
260             if( !strncmp( psz_parser, "channel=", strlen( "channel=" ) ) )
261             {
262                 p_sys->i_channel = strtol( psz_parser + strlen( "channel=" ),
263                                            &psz_parser, 0 );
264             }
265             else if( !strncmp( psz_parser, "norm=", strlen( "norm=" ) ) )
266             {
267                 psz_parser += strlen( "norm=" );
268                 if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
269                 {
270                     p_sys->i_norm = VIDEO_MODE_PAL;
271                     psz_parser += strlen( "pal" );
272                 }
273                 else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
274                 {
275                     p_sys->i_norm = VIDEO_MODE_NTSC;
276                     psz_parser += strlen( "ntsc" );
277                 }
278                 else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
279                 {
280                     p_sys->i_norm = VIDEO_MODE_SECAM;
281                     psz_parser += strlen( "secam" );
282                 }
283                 else if( !strncmp( psz_parser, "auto", strlen( "auto" ) ) )
284                 {
285                     p_sys->i_norm = VIDEO_MODE_AUTO;
286                     psz_parser += strlen( "auto" );
287                 }
288                 else
289                 {
290                     p_sys->i_norm = strtol( psz_parser, &psz_parser, 0 );
291                 }
292             }
293             else if( !strncmp( psz_parser, "frequency=",
294                                strlen( "frequency=" ) ) )
295             {
296                 p_sys->i_frequency =
297                     strtol( psz_parser + strlen( "frequency=" ),
298                             &psz_parser, 0 );
299                 if( p_sys->i_frequency < 30000 )
300                 {
301                 msg_Warn( p_input, "v4l syntax has changed : 'frequency' is now channel frequency in kHz");
302                 }
303             }
304             else if( !strncmp( psz_parser, "audio=", strlen( "audio=" ) ) )
305             {
306                 p_sys->i_audio = strtol( psz_parser + strlen( "audio=" ),
307                                          &psz_parser, 0 );
308             }
309             else if( !strncmp( psz_parser, "size=", strlen( "size=" ) ) )
310             {
311                 psz_parser += strlen( "size=" );
312                 if( !strncmp( psz_parser, "subqcif", strlen( "subqcif" ) ) )
313                 {
314                     p_sys->i_width  = 128;
315                     p_sys->i_height = 96;
316                 }
317                 else if( !strncmp( psz_parser, "qsif", strlen( "qsif" ) ) )
318                 {
319                     p_sys->i_width  = 160;
320                     p_sys->i_height = 120;
321                 }
322                 else if( !strncmp( psz_parser, "qcif", strlen( "qcif" ) ) )
323                 {
324                     p_sys->i_width  = 176;
325                     p_sys->i_height = 144;
326                 }
327                 else if( !strncmp( psz_parser, "sif", strlen( "sif" ) ) )
328                 {
329                     p_sys->i_width  = 320;
330                     p_sys->i_height = 244;
331                 }
332                 else if( !strncmp( psz_parser, "cif", strlen( "cif" ) ) )
333                 {
334                     p_sys->i_width  = 352;
335                     p_sys->i_height = 288;
336                 }
337                 else if( !strncmp( psz_parser, "vga", strlen( "vga" ) ) )
338                 {
339                     p_sys->i_width  = 640;
340                     p_sys->i_height = 480;
341                 }
342                 else
343                 {
344                     /* widthxheight */
345                     p_sys->i_width = strtol( psz_parser, &psz_parser, 0 );
346                     if( *psz_parser == 'x' || *psz_parser == 'X')
347                     {
348                         p_sys->i_height = strtol( psz_parser + 1,
349                                                   &psz_parser, 0 );
350                     }
351                     msg_Dbg( p_input, "WxH %dx%d", p_sys->i_width,
352                              p_sys->i_height );
353                 }
354             }
355             else if( !strncmp( psz_parser, "tuner=", strlen( "tuner=" ) ) )
356             {
357                 p_sys->i_tuner = strtol( psz_parser + strlen( "tuner=" ),
358                                          &psz_parser, 0 );
359             }
360             else if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) ) )
361             {
362                 int  i_len;
363
364                 psz_parser += strlen( "adev=" );
365                 if( strchr( psz_parser, ':' ) )
366                 {
367                     i_len = strchr( psz_parser, ':' ) - psz_parser;
368                 }
369                 else
370                 {
371                     i_len = strlen( psz_parser );
372                 }
373
374                 p_sys->psz_adev = strndup( psz_parser, i_len );
375
376                 psz_parser += i_len;
377             }
378             else if( !strncmp( psz_parser, "samplerate=",
379                                strlen( "samplerate=" ) ) )
380             {
381                 p_sys->i_sample_rate =
382                     strtol( psz_parser + strlen( "samplerate=" ),
383                             &psz_parser, 0 );
384             }
385             else if( !strncmp( psz_parser, "stereo", strlen( "stereo" ) ) )
386             {
387                 psz_parser += strlen( "stereo" );
388
389                 p_sys->b_stereo = VLC_TRUE;
390             }
391             else if( !strncmp( psz_parser, "mono", strlen( "mono" ) ) )
392             {
393                 psz_parser += strlen( "mono" );
394
395                 p_sys->b_stereo = VLC_FALSE;
396             }
397             else if( !strncmp( psz_parser, "mjpeg", strlen( "mjpeg" ) ) )
398             {
399                 psz_parser += strlen( "mjpeg" );
400
401                 p_sys->b_mjpeg = VLC_TRUE;
402             }
403             else if( !strncmp( psz_parser, "decimation=", 
404                         strlen( "decimation=" ) ) )
405             {
406                 p_sys->i_decimation = 
407                     strtol( psz_parser + strlen( "decimation=" ),
408                             &psz_parser, 0 );
409             }
410             else if( !strncmp( psz_parser, "quality=",
411                         strlen( "quality=" ) ) )
412             {
413                 p_sys->i_quality =
414                     strtol( psz_parser + strlen( "quality=" ),
415                             &psz_parser, 0 );
416             }
417             else
418             {
419                 msg_Warn( p_input, "unknown option" );
420             }
421
422             while( *psz_parser && *psz_parser != ':' )
423             {
424                 psz_parser++;
425             }
426
427             if( *psz_parser == '\0' )
428             {
429                 break;
430             }
431         }
432     }
433
434     if( *psz_dup )
435     {
436         p_sys->psz_video_device = strdup( psz_dup );
437     }
438     else
439     {
440         p_sys->psz_video_device = strdup( "/dev/video" );
441     }
442     msg_Dbg( p_input, "video device=`%s'", p_sys->psz_video_device );
443
444     if( p_sys->psz_adev && *p_sys->psz_adev == '\0' )
445     {
446         p_sys->psz_adev = strdup( "/dev/dsp" );
447     }
448     msg_Dbg( p_input, "audio device=`%s'", p_sys->psz_adev );
449
450
451
452     if( ( p_sys->fd = open( p_sys->psz_video_device, O_RDWR ) ) < 0 )
453     {
454         msg_Err( p_input, "cannot open device (%s)", strerror( errno ) );
455         goto failed;
456     }
457
458     if( ioctl( p_sys->fd, VIDIOCGCAP, &p_sys->vid_cap ) < 0 )
459     {
460         msg_Err( p_input, "cannot get capabilities (%s)", strerror( errno ) );
461         goto failed;
462     }
463
464     msg_Dbg( p_input,
465              "V4L device %s %d channels %d audios %d < w < %d %d < h < %d",
466              p_sys->vid_cap.name,
467              p_sys->vid_cap.channels,
468              p_sys->vid_cap.audios,
469              p_sys->vid_cap.minwidth,  p_sys->vid_cap.maxwidth,
470              p_sys->vid_cap.minheight, p_sys->vid_cap.maxheight );
471
472     if( p_sys->i_channel < 0 || p_sys->i_channel >= p_sys->vid_cap.channels )
473     {
474         msg_Dbg( p_input, "invalid channel, falling back on channel 0" );
475         p_sys->i_channel = 0;
476     }
477     if( p_sys->i_audio >= p_sys->vid_cap.audios )
478     {
479         msg_Dbg( p_input, "invalid audio, falling back with no audio" );
480         p_sys->i_audio = -1;
481     }
482
483     if( p_sys->i_width < p_sys->vid_cap.minwidth ||
484         p_sys->i_width > p_sys->vid_cap.maxwidth )
485     {
486         msg_Dbg( p_input, "invalid width %i", p_sys->i_width );
487         p_sys->i_width = 0;
488     }
489     if( p_sys->i_height < p_sys->vid_cap.minheight ||
490         p_sys->i_height > p_sys->vid_cap.maxheight )
491     {
492         msg_Dbg( p_input, "invalid height %i", p_sys->i_height );
493         p_sys->i_height = 0;
494     }
495
496     if( !( p_sys->vid_cap.type & VID_TYPE_CAPTURE ) )
497     {
498         msg_Err( p_input, "cannot grab" );
499         goto failed;
500     }
501
502     vid_channel.channel = p_sys->i_channel;
503     if( ioctl( p_sys->fd, VIDIOCGCHAN, &vid_channel ) < 0 )
504     {
505         msg_Err( p_input, "cannot get channel infos (%s)",
506                           strerror( errno ) );
507         goto failed;
508     }
509     msg_Dbg( p_input,
510              "setting channel %s(%d) %d tuners flags=0x%x type=0x%x norm=0x%x",
511              vid_channel.name,
512              vid_channel.channel,
513              vid_channel.tuners,
514              vid_channel.flags,
515              vid_channel.type,
516              vid_channel.norm );
517
518     if( p_sys->i_tuner >= vid_channel.tuners )
519     {
520         msg_Dbg( p_input, "invalid tuner, falling back on tuner 0" );
521         p_sys->i_tuner = 0;
522     }
523
524     vid_channel.norm = p_sys->i_norm;
525     if( ioctl( p_sys->fd, VIDIOCSCHAN, &vid_channel ) < 0 )
526     {
527         msg_Err( p_input, "cannot set channel (%s)", strerror( errno ) );
528         goto failed;
529     }
530
531     if( vid_channel.flags & VIDEO_VC_TUNER )
532     {
533
534         /* set tuner */
535 #if 0
536         struct video_tuner vid_tuner;
537         if( p_sys->i_tuner >= 0 )
538         {
539             vid_tuner.tuner = p_sys->i_tuner;
540             if( ioctl( p_sys->fd, VIDIOCGTUNER, &vid_tuner ) < 0 )
541             {
542                 msg_Err( p_input, "cannot get tuner (%s)", strerror( errno ) );
543                 goto failed;
544             }
545             msg_Dbg( p_input, "tuner %s low=%d high=%d, flags=0x%x "
546                      "mode=0x%x signal=0x%x",
547                      vid_tuner.name, vid_tuner.rangelow, vid_tuner.rangehigh,
548                      vid_tuner.flags, vid_tuner.mode, vid_tuner.signal );
549
550             msg_Dbg( p_input, "setting tuner %s (%d)",
551                      vid_tuner.name, vid_tuner.tuner );
552
553             //vid_tuner.mode = p_sys->i_norm; /* FIXME FIXME to be checked FIXME FIXME */
554             if( ioctl( p_sys->fd, VIDIOCSTUNER, &vid_tuner ) < 0 )
555             {
556                 msg_Err( p_input, "cannot set tuner (%s)", strerror( errno ) );
557                 goto failed;
558             }
559         }
560 #endif
561
562         // show a warning if frequency is < than 30000. User is certainly usint old syntax.
563         
564
565         /* set frequency */
566         if( p_sys->i_frequency >= 0 )
567         {
568             int driver_frequency = p_sys->i_frequency * 16 /1000;
569             if( ioctl( p_sys->fd, VIDIOCSFREQ, &driver_frequency ) < 0 )
570             {
571                 msg_Err( p_input, "cannot set frequency (%s)",
572                                   strerror( errno ) );
573                 goto failed;
574             }
575             msg_Dbg( p_input, "frequency %d (%d)", p_sys->i_frequency,
576                                                    driver_frequency );
577         }
578     }
579
580     /* set audio */
581     if( vid_channel.flags & VIDEO_VC_AUDIO )
582     {
583         struct video_audio      vid_audio;
584
585         /* XXX TODO volume, balance, ... */
586         if( p_sys->i_audio >= 0 )
587         {
588             vid_audio.audio = p_sys->i_audio;
589             if( ioctl( p_sys->fd, VIDIOCGAUDIO, &vid_audio ) < 0 )
590             {
591                 msg_Err( p_input, "cannot get audio (%s)", strerror( errno ) );
592                 goto failed;
593             }
594
595             /* unmute audio */
596             vid_audio.flags &= ~VIDEO_AUDIO_MUTE;
597
598             if( ioctl( p_sys->fd, VIDIOCSAUDIO, &vid_audio ) < 0 )
599             {
600                 msg_Err( p_input, "cannot set audio (%s)", strerror( errno ) );
601                 goto failed;
602             }
603         }
604
605     }
606
607     if( p_sys->psz_adev )
608     {
609         int    i_format;
610         if( ( p_sys->fd_audio = open( p_sys->psz_adev, O_RDONLY|O_NONBLOCK ) ) < 0 )
611         {
612             msg_Err( p_input, "cannot open audio device (%s)",
613                               strerror( errno ) );
614             goto failed;
615         }
616
617         i_format = AFMT_S16_LE;
618         if( ioctl( p_sys->fd_audio, SNDCTL_DSP_SETFMT, &i_format ) < 0
619             || i_format != AFMT_S16_LE )
620         {
621             msg_Err( p_input, "cannot set audio format (16b little endian) "
622                               "(%s)", strerror( errno ) );
623             goto failed;
624         }
625
626         if( ioctl( p_sys->fd_audio, SNDCTL_DSP_STEREO,
627                    &p_sys->b_stereo ) < 0 )
628         {
629             msg_Err( p_input, "cannot set audio channels count (%s)",
630                               strerror( errno ) );
631             goto failed;
632         }
633
634         if( ioctl( p_sys->fd_audio, SNDCTL_DSP_SPEED,
635                    &p_sys->i_sample_rate ) < 0 )
636         {
637             msg_Err( p_input, "cannot set audio sample rate (%s)",
638                               strerror( errno ) );
639             goto failed;
640         }
641
642         msg_Dbg( p_input,
643                  "adev=`%s' %s %dHz",
644                  p_sys->psz_adev,
645                  p_sys->b_stereo ? "stereo" : "mono",
646                  p_sys->i_sample_rate );
647
648         p_sys->i_audio_frame_size = 0;
649         p_sys->i_audio_frame_size_allocated = 6*1024;
650         p_sys->p_audio_frame =
651             malloc( p_sys->i_audio_frame_size_allocated );
652     }
653
654     /* establish basic params with input and norm before feeling width
655      * or height */
656     if( p_sys->b_mjpeg )
657     {
658         struct quicktime_mjpeg_app1 *p_app1;
659         int32_t i_offset;
660
661         if( ioctl( p_sys->fd, MJPIOC_G_PARAMS, &mjpeg ) < 0 )
662         {
663             msg_Err( p_input, "cannot get mjpeg params (%s)",
664                               strerror( errno ) );
665             goto failed;
666         }
667         mjpeg.input = p_sys->i_channel;
668         mjpeg.norm  = p_sys->i_norm;
669         mjpeg.decimation = p_sys->i_decimation;
670
671         if( p_sys->i_width )
672             mjpeg.img_width = p_sys->i_width / p_sys->i_decimation;
673         if( p_sys->i_height )
674             mjpeg.img_height = p_sys->i_height / p_sys->i_decimation;
675
676         /* establish Quicktime APP1 marker while we are here */
677         mjpeg.APPn = 1;
678         mjpeg.APP_len = 40;
679
680         /* aligned */
681         p_app1 = (struct quicktime_mjpeg_app1 *)mjpeg.APP_data;
682         p_app1->i_reserved = 0;
683         p_app1->i_tag = VLC_FOURCC( 'm','j','p','g' );
684         p_app1->i_field_size = 0;
685         p_app1->i_padded_field_size = 0;
686         p_app1->i_next_field = 0;
687         /* XXX WARNING XXX */
688         /* these's nothing magic about these values.  We are dangerously
689          * assuming the encoder card is encoding mjpeg-a and is not throwing
690          * in marker tags we aren't expecting.  It's bad enough we have to
691          * search through the jpeg output for every frame we grab just to
692          * find the first field's end marker, so we take this risk to boost
693          * performance.
694          * This is really something the driver could do for us because this
695          * does conform to standards outside of Apple Quicktime.
696          */
697         i_offset = 0x2e;
698         p_app1->i_DQT_offset = hton32( i_offset );
699         i_offset = 0xb4;
700         p_app1->i_DHT_offset = hton32( i_offset );
701         i_offset = 0x258;
702         p_app1->i_SOF_offset = hton32( i_offset );
703         i_offset = 0x26b;
704         p_app1->i_SOS_offset = hton32( i_offset );
705         i_offset = 0x279;
706         p_app1->i_data_offset = hton32( i_offset );
707
708         /* SOF and SOS aren't specified by the mjpeg API because they aren't
709          * optional.  They will be present in the output. */
710         mjpeg.jpeg_markers = JPEG_MARKER_DHT | JPEG_MARKER_DQT;
711
712         if( ioctl( p_sys->fd, MJPIOC_S_PARAMS, &mjpeg ) < 0 )
713         {
714             msg_Err( p_input, "cannot set mjpeg params (%s)",
715                               strerror( errno ) );
716             goto failed;
717         }
718
719         p_sys->i_width = mjpeg.img_width * mjpeg.HorDcm;
720         p_sys->i_height = mjpeg.img_height * mjpeg.VerDcm *
721             mjpeg.field_per_buff;
722     }
723
724     /* fix width/height */
725     if( !p_sys->b_mjpeg && ( p_sys->i_width == 0 || p_sys->i_height == 0 ) )
726     {
727         struct video_window vid_win;
728
729         if( ioctl( p_sys->fd, VIDIOCGWIN, &vid_win ) < 0 )
730         {
731             msg_Err( p_input, "cannot get win (%s)", strerror( errno ) );
732             goto failed;
733         }
734         p_sys->i_width  = vid_win.width;
735         p_sys->i_height = vid_win.height;
736
737         msg_Dbg( p_input, "will use %dx%d", p_sys->i_width, p_sys->i_height );
738     }
739
740     p_sys->p_video_frame = NULL;
741
742     if( !p_sys->b_mjpeg )
743     {
744         /* Find out video format used by device */
745         if( ioctl( p_sys->fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 )
746         {
747             int i_chroma;
748             struct video_picture vid_picture = p_sys->vid_picture;
749
750             /* Try to set the format to something easy to encode */
751             vid_picture.palette = VIDEO_PALETTE_YUV420P;
752             if( ioctl( p_sys->fd, VIDIOCSPICT, &vid_picture ) == 0 )
753             {
754                 p_sys->vid_picture = vid_picture;
755             }
756             else
757             {
758                 vid_picture.palette = VIDEO_PALETTE_YUV422P;
759                 if( ioctl( p_sys->fd, VIDIOCSPICT, &vid_picture ) == 0 )
760                 {
761                     p_sys->vid_picture = vid_picture;
762                 }
763             }
764
765             /* Find out final format */
766             switch( p_sys->vid_picture.palette )
767             {
768             case VIDEO_PALETTE_GREY:
769                 i_chroma = VLC_FOURCC( 'G', 'R', 'E', 'Y' );
770                 break;
771             case VIDEO_PALETTE_HI240:
772                 i_chroma = VLC_FOURCC( 'I', '2', '4', '0' );
773                 break;
774             case VIDEO_PALETTE_RGB565:
775                 i_chroma = VLC_FOURCC( 'R', 'V', '1', '6' );
776                 break;
777             case VIDEO_PALETTE_RGB555:
778                 i_chroma = VLC_FOURCC( 'R', 'V', '1', '5' );
779                 break;
780             case VIDEO_PALETTE_RGB24:
781                 i_chroma = VLC_FOURCC( 'R', 'V', '2', '4' );
782                 break;
783             case VIDEO_PALETTE_RGB32:
784                 i_chroma = VLC_FOURCC( 'R', 'V', '3', '2' );
785                 break;
786             case VIDEO_PALETTE_YUV422:
787                 i_chroma = VLC_FOURCC( 'I', '4', '2', '2' );
788                 break;
789             case VIDEO_PALETTE_YUYV:
790                 i_chroma = VLC_FOURCC( 'Y', 'U', 'Y', 'V' );
791                 break;
792             case VIDEO_PALETTE_UYVY:
793                 i_chroma = VLC_FOURCC( 'U', 'Y', 'V', 'Y' );
794                 break;
795             case VIDEO_PALETTE_YUV420:
796                 i_chroma = VLC_FOURCC( 'I', '4', '2', 'N' );
797                 break;
798             case VIDEO_PALETTE_YUV411:
799                 i_chroma = VLC_FOURCC( 'I', '4', '1', 'N' );
800                 break;
801             case VIDEO_PALETTE_RAW:
802                 i_chroma = VLC_FOURCC( 'G', 'R', 'A', 'W' );
803                 break;
804             case VIDEO_PALETTE_YUV422P:
805                 i_chroma = VLC_FOURCC( 'I', '4', '2', '2' );
806                 break;
807             case VIDEO_PALETTE_YUV420P:
808                 i_chroma = VLC_FOURCC( 'I', '4', '2', '0' );
809                 break;
810             case VIDEO_PALETTE_YUV411P:
811                 i_chroma = VLC_FOURCC( 'I', '4', '1', '1' );
812                 break;
813             }
814             p_sys->i_fourcc = i_chroma;
815         }
816         else
817         {
818             msg_Err( p_input, "ioctl VIDIOCGPICT failed" );
819             goto failed;
820         }
821     }
822
823
824     if( p_sys->b_mjpeg )
825     {
826         int i;
827
828         p_sys->mjpeg_buffers.count = 8;
829         p_sys->mjpeg_buffers.size = MJPEG_BUFFER_SIZE;
830
831         if( ioctl( p_sys->fd, MJPIOC_REQBUFS, &p_sys->mjpeg_buffers ) < 0 )
832         {
833             msg_Err( p_input, "mmap unsupported" );
834             goto failed;
835         }
836
837         p_sys->p_video_mmap = mmap( 0,
838                 p_sys->mjpeg_buffers.size * p_sys->mjpeg_buffers.count,
839                 PROT_READ | PROT_WRITE, MAP_SHARED, p_sys->fd, 0 );
840         if( p_sys->p_video_mmap == MAP_FAILED )
841         {
842             msg_Err( p_input, "mmap failed" );
843             goto failed;
844         }
845
846         p_sys->i_fourcc  = VLC_FOURCC( 'm','j','p','g' );
847         p_sys->i_frame_pos = -1;
848
849         /* queue up all the frames */
850         for( i = 0; i < (int)p_sys->mjpeg_buffers.count; i++ )
851         {
852             if( ioctl( p_sys->fd, MJPIOC_QBUF_CAPT, &i ) < 0 )
853             {
854                 msg_Err( p_input, "unable to queue frame" );
855                 goto failed;
856             }
857         }
858     }
859     else
860     {
861         /* Fill in picture_t fields */
862         vout_InitPicture( VLC_OBJECT(p_input), &p_sys->pic,
863                           p_sys->i_width, p_sys->i_height, p_sys->i_fourcc );
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     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 }