]> git.sesse.net Git - vlc/blob - modules/access/v4l.c
Fix parsing of float-typed variables
[vlc] / modules / access / v4l.c
1 /*****************************************************************************
2  * v4l.c : Video4Linux input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002-2004 the VideoLAN team
5  * $Id$
6  *
7  * Author: Laurent Aimar <fenrir@via.ecp.fr>
8  *         Paul Forgey <paulf at aphrodite dot com>
9  *         Gildas Bazin <gbazin@videolan.org>
10  *         Benjamin Pracht <bigben at videolan dot org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_input.h>
38 #include <vlc_demux.h>
39 #include <vlc_access.h>
40 #include <vlc_picture.h>
41 #include <vlc_charset.h>
42
43 #include <sys/ioctl.h>
44 #include <sys/mman.h>
45 #include <fcntl.h>
46 #include <arpa/inet.h>
47 #include <errno.h>
48
49 /* From GStreamer's v4l plugin:
50  * Because of some really cool feature in video4linux1, also known as
51  * 'not including sys/types.h and sys/time.h', we had to include it
52  * ourselves. In all their intelligence, these people decided to fix
53  * this in the next version (video4linux2) in such a cool way that it
54  * breaks all compilations of old stuff...
55  * The real problem is actually that linux/time.h doesn't use proper
56  * macro checks before defining types like struct timeval. The proper
57  * fix here is to either fuck the kernel header (which is what we do
58  * by defining _LINUX_TIME_H, an innocent little hack) or by fixing it
59  * upstream, which I'll consider doing later on. If you get compiler
60  * errors here, check your linux/time.h && sys/time.h header setup.
61 */
62 #define _LINUX_TIME_H
63
64 #include <linux/videodev.h>
65 #include "videodev_mjpeg.h"
66
67 /*****************************************************************************
68  * Module descriptior
69  *****************************************************************************/
70 static int  Open ( vlc_object_t * );
71 static void Close( vlc_object_t * );
72
73 #define CACHING_TEXT N_("Caching value in ms")
74 #define CACHING_LONGTEXT N_( \
75     "Caching value for V4L captures. This " \
76     "value should be set in milliseconds." )
77 #define VDEV_TEXT N_("Video device name")
78 #define VDEV_LONGTEXT N_( \
79     "Name of the video device to use. " \
80     "If you don't specify anything, no video device will be used.")
81 #define CHROMA_TEXT N_("Video input chroma format")
82 #define CHROMA_LONGTEXT N_( \
83     "Force the Video4Linux video device to use a specific chroma format " \
84     "(eg. I420 (default), RV24, etc.)")
85 #define FREQUENCY_TEXT N_( "Frequency" )
86 #define FREQUENCY_LONGTEXT N_( \
87     "Frequency to capture (in kHz), if applicable." )
88 #define CHANNEL_TEXT N_( "Channel" )
89 #define CHANNEL_LONGTEXT N_( \
90     "Channel of the card to use (Usually, 0 = tuner, " \
91     "1 = composite, 2 = svideo)." )
92 #define NORM_TEXT N_( "Norm" )
93 #define NORM_LONGTEXT N_( \
94     "Norm of the stream (Automatic, SECAM, PAL, or NTSC)." )
95 #define AUDIO_TEXT N_( "Audio Channel" )
96 #define AUDIO_LONGTEXT N_( \
97     "Audio Channel to use, if there are several audio inputs." )
98 #define WIDTH_TEXT N_( "Width" )
99 #define WIDTH_LONGTEXT N_( "Width of the stream to capture " \
100     "(-1 for autodetect)." )
101 #define HEIGHT_TEXT N_( "Height" )
102 #define HEIGHT_LONGTEXT N_( "Height of the stream to capture " \
103     "(-1 for autodetect)." )
104 #define BRIGHTNESS_TEXT N_( "Brightness" )
105 #define BRIGHTNESS_LONGTEXT N_( \
106     "Brightness of the video input." )
107 #define HUE_TEXT N_( "Hue" )
108 #define HUE_LONGTEXT N_( \
109     "Hue of the video input." )
110 #define COLOUR_TEXT N_( "Color" )
111 #define COLOUR_LONGTEXT N_( \
112     "Color of the video input." )
113 #define CONTRAST_TEXT N_( "Contrast" )
114 #define CONTRAST_LONGTEXT N_( \
115     "Contrast of the video input." )
116 #define TUNER_TEXT N_( "Tuner" )
117 #define TUNER_LONGTEXT N_( "Tuner to use, if there are several ones." )
118 #define MJPEG_TEXT N_( "MJPEG" )
119 #define MJPEG_LONGTEXT N_(  \
120     "Set this option if the capture device outputs MJPEG" )
121 #define DECIMATION_TEXT N_( "Decimation" )
122 #define DECIMATION_LONGTEXT N_( \
123     "Decimation level for MJPEG streams" )
124 #define QUALITY_TEXT N_( "Quality" )
125 #define QUALITY_LONGTEXT N_( "Quality of the stream." )
126 #define FPS_TEXT N_( "Framerate" )
127 #define FPS_LONGTEXT N_( "Framerate to capture, if applicable " \
128     "(-1 for autodetect)." )
129
130 #define AUDIO_DEPRECATED_ERROR N_( \
131     "Alsa or OSS audio capture in the v4l access is deprecated. " \
132     "please use 'v4l:/""/ :input-slave=alsa:/""/' or " \
133     "'v4l:/""/ :input-slave=oss:/""/' instead." )
134
135 static const int i_norm_list[] =
136     { VIDEO_MODE_AUTO, VIDEO_MODE_SECAM, VIDEO_MODE_PAL, VIDEO_MODE_NTSC };
137 static const char *const psz_norm_list_text[] =
138     { N_("Automatic"), N_("SECAM"), N_("PAL"),  N_("NTSC") };
139
140 #define V4L_DEFAULT "/dev/video"
141
142 vlc_module_begin ()
143     set_shortname( N_("Video4Linux") )
144     set_description( N_("Video4Linux input") )
145     set_category( CAT_INPUT )
146     set_subcategory( SUBCAT_INPUT_ACCESS )
147
148     add_integer( "v4l-caching", DEFAULT_PTS_DELAY / 1000, NULL,
149                  CACHING_TEXT, CACHING_LONGTEXT, true )
150     add_obsolete_string( "v4l-vdev" );
151     add_obsolete_string( "v4l-adev" );
152     add_string( "v4l-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
153                 true )
154     add_float( "v4l-fps", -1.0, NULL, FPS_TEXT, FPS_LONGTEXT, true )
155     add_obsolete_integer( "v4l-samplerate" );
156     add_integer( "v4l-channel", 0, NULL, CHANNEL_TEXT, CHANNEL_LONGTEXT,
157                 true )
158     add_integer( "v4l-tuner", -1, NULL, TUNER_TEXT, TUNER_LONGTEXT, true )
159     add_integer( "v4l-norm", VIDEO_MODE_AUTO, NULL, NORM_TEXT, NORM_LONGTEXT,
160                 false )
161         change_integer_list( i_norm_list, psz_norm_list_text, NULL );
162     add_integer( "v4l-frequency", -1, NULL, FREQUENCY_TEXT, FREQUENCY_LONGTEXT,
163                 false )
164     add_integer( "v4l-audio", -1, NULL, AUDIO_TEXT, AUDIO_LONGTEXT, true )
165     add_obsolete_bool( "v4l-stereo" );
166     add_integer( "v4l-width", 0, NULL, WIDTH_TEXT, WIDTH_LONGTEXT, true )
167     add_integer( "v4l-height", 0, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT,
168                 true )
169     add_integer( "v4l-brightness", -1, NULL, BRIGHTNESS_TEXT,
170                 BRIGHTNESS_LONGTEXT, true )
171     add_integer( "v4l-colour", -1, NULL, COLOUR_TEXT, COLOUR_LONGTEXT,
172                 true )
173     add_integer( "v4l-hue", -1, NULL, HUE_TEXT, HUE_LONGTEXT, true )
174     add_integer( "v4l-contrast", -1, NULL, CONTRAST_TEXT, CONTRAST_LONGTEXT,
175                 true )
176     add_bool( "v4l-mjpeg", false, NULL, MJPEG_TEXT, MJPEG_LONGTEXT,
177             true )
178     add_integer( "v4l-decimation", 1, NULL, DECIMATION_TEXT,
179             DECIMATION_LONGTEXT, true )
180     add_integer( "v4l-quality", 100, NULL, QUALITY_TEXT, QUALITY_LONGTEXT,
181             true )
182
183     add_shortcut( "v4l" )
184     set_capability( "access_demux", 10 )
185     set_callbacks( Open, Close )
186 vlc_module_end ()
187
188 /*****************************************************************************
189  * Access: local prototypes
190  *****************************************************************************/
191 static int Demux  ( demux_t * );
192 static int Control( demux_t *, int, va_list );
193
194 static void ParseMRL    ( demux_t * );
195 static int  OpenVideoDev( demux_t *, char * );
196 static block_t *GrabVideo( demux_t * );
197
198 #define MJPEG_BUFFER_SIZE (256*1024)
199
200 struct quicktime_mjpeg_app1
201 {
202     uint32_t    i_reserved;             /* set to 0 */
203     uint32_t    i_tag;                  /* 'mjpg' */
204     uint32_t    i_field_size;           /* offset following EOI */
205     uint32_t    i_padded_field_size;    /* offset following EOI+pad */
206     uint32_t    i_next_field;           /* offset to next field */
207     uint32_t    i_DQT_offset;
208     uint32_t    i_DHT_offset;
209     uint32_t    i_SOF_offset;
210     uint32_t    i_SOS_offset;
211     uint32_t    i_data_offset;          /* following SOS marker data */
212 };
213
214 static const struct
215 {
216     int i_v4l;
217     vlc_fourcc_t i_fourcc;
218
219 } v4lchroma_to_fourcc[] =
220 {
221     { VIDEO_PALETTE_GREY, VLC_CODEC_GREY },
222     { VIDEO_PALETTE_HI240, VLC_FOURCC( 'I', '2', '4', '0' ) },
223     { VIDEO_PALETTE_RGB565, VLC_CODEC_RGB16 },
224     { VIDEO_PALETTE_RGB555, VLC_CODEC_RGB15 },
225     { VIDEO_PALETTE_RGB24, VLC_CODEC_RGB24 },
226     { VIDEO_PALETTE_RGB32, VLC_CODEC_RGB32 },
227     { VIDEO_PALETTE_YUV422, VLC_CODEC_YUYV },
228     { VIDEO_PALETTE_YUYV, VLC_CODEC_YUYV },
229     { VIDEO_PALETTE_UYVY, VLC_CODEC_UYVY },
230     { VIDEO_PALETTE_YUV420, VLC_FOURCC( 'I', '4', '2', 'N' ) },
231     { VIDEO_PALETTE_YUV411, VLC_FOURCC( 'I', '4', '1', 'N' ) },
232     { VIDEO_PALETTE_RAW, VLC_FOURCC( 'G', 'R', 'A', 'W' ) },
233     { VIDEO_PALETTE_YUV422P, VLC_CODEC_I422 },
234     { VIDEO_PALETTE_YUV420P, VLC_CODEC_I420 },
235     { VIDEO_PALETTE_YUV411P, VLC_CODEC_I411 },
236     { 0, 0 }
237 };
238
239 struct demux_sys_t
240 {
241     /* Devices */
242     char *psz_device;         /* Main device from MRL */
243     int  i_fd;
244
245     /* Video properties */
246     picture_t pic;
247
248     int i_fourcc;
249     int i_channel;
250     int i_audio;
251     int i_norm;
252     int i_tuner;
253     int i_frequency;
254     int i_width;
255     int i_height;
256
257     int i_brightness;
258     int i_hue;
259     int i_colour;
260     int i_contrast;
261
262     float f_fps;            /* <= 0.0 mean to grab at full rate */
263     mtime_t i_video_pts;    /* only used when f_fps > 0 */
264
265     bool b_mjpeg;
266     int i_decimation;
267     int i_quality;
268
269     struct video_capability vid_cap;
270     struct video_mbuf       vid_mbuf;
271     struct mjpeg_requestbuffers mjpeg_buffers;
272
273     uint8_t *p_video_mmap;
274     int     i_frame_pos;
275
276     struct video_mmap   vid_mmap;
277     struct video_picture vid_picture;
278
279     int          i_video_frame_size;
280     es_out_id_t  *p_es;
281 };
282
283 /*****************************************************************************
284  * Open: opens v4l device
285  *****************************************************************************
286  *
287  * url: <video device>::::
288  *
289  *****************************************************************************/
290 static int Open( vlc_object_t *p_this )
291 {
292     demux_t     *p_demux = (demux_t*)p_this;
293     demux_sys_t *p_sys;
294
295     /* Only when selected */
296     if( *p_demux->psz_access == '\0' )
297         return VLC_EGENERIC;
298
299     /* Set up p_demux */
300     p_demux->pf_demux = Demux;
301     p_demux->pf_control = Control;
302     p_demux->info.i_update = 0;
303     p_demux->info.i_title = 0;
304     p_demux->info.i_seekpoint = 0;
305     p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
306     if( !p_sys )
307         return VLC_ENOMEM;
308
309     p_sys->i_audio      = var_CreateGetInteger( p_demux, "v4l-audio" );
310     p_sys->i_channel    = var_CreateGetInteger( p_demux, "v4l-channel" );
311     p_sys->i_norm       = var_CreateGetInteger( p_demux, "v4l-norm" );
312     p_sys->i_tuner      = var_CreateGetInteger( p_demux, "v4l-tuner" );
313     p_sys->i_frequency  = var_CreateGetInteger( p_demux, "v4l-frequency" );
314
315     p_sys->f_fps        = var_CreateGetFloat( p_demux, "v4l-fps" );
316     p_sys->i_width      = var_CreateGetInteger( p_demux, "v4l-width" );
317     p_sys->i_height     = var_CreateGetInteger( p_demux, "v4l-height" );
318     p_sys->i_video_pts  = -1;
319     p_sys->i_brightness = var_CreateGetInteger( p_demux, "v4l-brightness" );
320
321     p_sys->i_hue        = var_CreateGetInteger( p_demux, "v4l-hue" );
322     p_sys->i_colour     = var_CreateGetInteger( p_demux, "v4l-colour" );
323     p_sys->i_contrast   = var_CreateGetInteger( p_demux, "v4l-contrast" );
324
325     p_sys->b_mjpeg      = var_CreateGetBool( p_demux, "v4l-mjpeg" );
326     p_sys->i_decimation = var_CreateGetInteger( p_demux, "v4l-decimation" );
327     p_sys->i_quality    = var_CreateGetInteger( p_demux, "v4l-quality" );
328
329     p_sys->psz_device = NULL;
330     p_sys->i_fd = -1;
331
332     p_sys->p_es = NULL;
333
334     ParseMRL( p_demux );
335
336     msg_Dbg( p_this, "opening device '%s'", p_sys->psz_device );
337     p_sys->i_fd = OpenVideoDev( p_demux, p_sys->psz_device );
338     if( p_sys->i_fd < 0 )
339     {
340         Close( p_this );
341         return VLC_EGENERIC;
342     }
343
344
345     msg_Dbg( p_demux, "v4l grabbing started" );
346
347     /* Declare elementary streams */
348     es_format_t fmt;
349     es_format_Init( &fmt, VIDEO_ES, p_sys->i_fourcc );
350     fmt.video.i_width  = p_sys->i_width;
351     fmt.video.i_height = p_sys->i_height;
352     fmt.video.i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
353
354     /* Setup rgb mask for RGB formats */
355     switch( p_sys->i_fourcc )
356     {
357         case VLC_CODEC_RGB15:
358             fmt.video.i_rmask = 0x001f;
359             fmt.video.i_gmask = 0x03e0;
360             fmt.video.i_bmask = 0x7c00;
361             break;
362         case VLC_CODEC_RGB16:
363             fmt.video.i_rmask = 0x001f;
364             fmt.video.i_gmask = 0x07e0;
365             fmt.video.i_bmask = 0xf800;
366             break;
367         case VLC_CODEC_RGB24:
368         case VLC_CODEC_RGB32:
369             fmt.video.i_rmask = 0x00ff0000;
370             fmt.video.i_gmask = 0x0000ff00;
371             fmt.video.i_bmask = 0x000000ff;
372             break;
373     }
374
375     msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
376              (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
377     p_sys->p_es = es_out_Add( p_demux->out, &fmt );
378
379     /* Update default_pts to a suitable value for access */
380     var_Create( p_demux, "v4l-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
381
382     return VLC_SUCCESS;
383 }
384
385 /*****************************************************************************
386  * Close: close device, free resources
387  *****************************************************************************/
388 static void Close( vlc_object_t *p_this )
389 {
390     demux_t     *p_demux = (demux_t *)p_this;
391     demux_sys_t *p_sys   = p_demux->p_sys;
392
393     free( p_sys->psz_device );
394     if( p_sys->i_fd >= 0 ) close( p_sys->i_fd );
395
396     if( p_sys->b_mjpeg )
397     {
398         int i_noframe = -1;
399         ioctl( p_sys->i_fd, MJPIOC_QBUF_CAPT, &i_noframe );
400     }
401
402     if( p_sys->p_video_mmap && p_sys->p_video_mmap != MAP_FAILED )
403     {
404         if( p_sys->b_mjpeg )
405             munmap( p_sys->p_video_mmap, p_sys->mjpeg_buffers.size *
406                     p_sys->mjpeg_buffers.count );
407         else
408             munmap( p_sys->p_video_mmap, p_sys->vid_mbuf.size );
409     }
410
411     free( p_sys );
412 }
413
414 /*****************************************************************************
415  * Control:
416  *****************************************************************************/
417 static int Control( demux_t *p_demux, int i_query, va_list args )
418 {
419     bool *pb;
420     int64_t    *pi64;
421
422     switch( i_query )
423     {
424         /* Special for access_demux */
425         case DEMUX_CAN_PAUSE:
426         case DEMUX_CAN_SEEK:
427         case DEMUX_CAN_CONTROL_PACE:
428             pb = (bool*)va_arg( args, bool * );
429             *pb = false;
430             return VLC_SUCCESS;
431
432         case DEMUX_GET_PTS_DELAY:
433             pi64 = (int64_t*)va_arg( args, int64_t * );
434             *pi64 = (int64_t)var_GetInteger( p_demux, "v4l-caching" ) * 1000;
435             return VLC_SUCCESS;
436
437         case DEMUX_GET_TIME:
438             pi64 = (int64_t*)va_arg( args, int64_t * );
439             *pi64 = mdate();
440             return VLC_SUCCESS;
441
442         /* TODO implement others */
443         default:
444             return VLC_EGENERIC;
445     }
446
447     return VLC_EGENERIC;
448 }
449
450 /*****************************************************************************
451  * Demux:
452  *****************************************************************************/
453 static int Demux( demux_t *p_demux )
454 {
455     demux_sys_t *p_sys = p_demux->p_sys;
456
457     block_t *p_block = GrabVideo( p_demux );
458
459     if( !p_block )
460     {
461         msleep( 10000 ); /* Unfortunately v4l doesn't allow polling */
462         return 1;
463     }
464
465     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
466     es_out_Send( p_demux->out, p_sys->p_es, p_block );
467
468     return 1;
469 }
470
471 /*****************************************************************************
472  * ParseMRL: parse the options contained in the MRL
473  *****************************************************************************/
474 static void ParseMRL( demux_t *p_demux )
475 {
476     demux_sys_t *p_sys = p_demux->p_sys;
477
478     char *psz_dup = strdup( p_demux->psz_path );
479     char *psz_parser = psz_dup;
480
481     while( *psz_parser && *psz_parser != ':' )
482     {
483         psz_parser++;
484     }
485
486     if( *psz_parser == ':' )
487     {
488         /* read options */
489         for( ;; )
490         {
491             *psz_parser++ = '\0';
492             if( !strncmp( psz_parser, "channel=", strlen( "channel=" ) ) )
493             {
494                 p_sys->i_channel = strtol( psz_parser + strlen( "channel=" ),
495                                            &psz_parser, 0 );
496             }
497             else if( !strncmp( psz_parser, "norm=", strlen( "norm=" ) ) )
498             {
499                 psz_parser += strlen( "norm=" );
500                 if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
501                 {
502                     p_sys->i_norm = VIDEO_MODE_PAL;
503                     psz_parser += strlen( "pal" );
504                 }
505                 else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
506                 {
507                     p_sys->i_norm = VIDEO_MODE_NTSC;
508                     psz_parser += strlen( "ntsc" );
509                 }
510                 else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
511                 {
512                     p_sys->i_norm = VIDEO_MODE_SECAM;
513                     psz_parser += strlen( "secam" );
514                 }
515                 else if( !strncmp( psz_parser, "auto", strlen( "auto" ) ) )
516                 {
517                     p_sys->i_norm = VIDEO_MODE_AUTO;
518                     psz_parser += strlen( "auto" );
519                 }
520                 else
521                 {
522                     p_sys->i_norm = strtol( psz_parser, &psz_parser, 0 );
523                 }
524             }
525             else if( !strncmp( psz_parser, "frequency=",
526                                strlen( "frequency=" ) ) )
527             {
528                 p_sys->i_frequency =
529                     strtol( psz_parser + strlen( "frequency=" ),
530                             &psz_parser, 0 );
531                 if( p_sys->i_frequency < 30000 )
532                 {
533                     msg_Warn( p_demux, "v4l syntax has changed : "
534                               "'frequency' is now channel frequency in kHz");
535                 }
536             }
537             else if( !strncmp( psz_parser, "audio=", strlen( "audio=" ) ) )
538             {
539                 p_sys->i_audio = strtol( psz_parser + strlen( "audio=" ),
540                                          &psz_parser, 0 );
541             }
542             else if( !strncmp( psz_parser, "size=", strlen( "size=" ) ) )
543             {
544                 psz_parser += strlen( "size=" );
545                 if( !strncmp( psz_parser, "subqcif", strlen( "subqcif" ) ) )
546                 {
547                     p_sys->i_width  = 128;
548                     p_sys->i_height = 96;
549                 }
550                 else if( !strncmp( psz_parser, "qsif", strlen( "qsif" ) ) )
551                 {
552                     p_sys->i_width  = 160;
553                     p_sys->i_height = 120;
554                 }
555                 else if( !strncmp( psz_parser, "qcif", strlen( "qcif" ) ) )
556                 {
557                     p_sys->i_width  = 176;
558                     p_sys->i_height = 144;
559                 }
560                 else if( !strncmp( psz_parser, "sif", strlen( "sif" ) ) )
561                 {
562                     p_sys->i_width  = 320;
563                     p_sys->i_height = 244;
564                 }
565                 else if( !strncmp( psz_parser, "cif", strlen( "cif" ) ) )
566                 {
567                     p_sys->i_width  = 352;
568                     p_sys->i_height = 288;
569                 }
570                 else if( !strncmp( psz_parser, "vga", strlen( "vga" ) ) )
571                 {
572                     p_sys->i_width  = 640;
573                     p_sys->i_height = 480;
574                 }
575                 else
576                 {
577                     /* widthxheight */
578                     p_sys->i_width = strtol( psz_parser, &psz_parser, 0 );
579                     if( *psz_parser == 'x' || *psz_parser == 'X')
580                     {
581                         p_sys->i_height = strtol( psz_parser + 1,
582                                                   &psz_parser, 0 );
583                     }
584                     msg_Dbg( p_demux, "WxH %dx%d", p_sys->i_width,
585                              p_sys->i_height );
586                 }
587             }
588             else if( !strncmp( psz_parser, "brightness=", strlen( "brightness=" ) ) )
589             {
590                 p_sys->i_brightness = strtol( psz_parser + strlen( "brightness=" ),
591                                               &psz_parser, 0 );
592             }
593             else if( !strncmp( psz_parser, "colour=", strlen( "colour=" ) ) )
594             {
595                 p_sys->i_colour = strtol( psz_parser + strlen( "colour=" ),
596                                           &psz_parser, 0 );
597             }
598             else if( !strncmp( psz_parser, "hue=", strlen( "hue=" ) ) )
599             {
600                 p_sys->i_hue = strtol( psz_parser + strlen( "hue=" ),
601                                        &psz_parser, 0 );
602             }
603             else if( !strncmp( psz_parser, "contrast=", strlen( "contrast=" ) ) )
604             {
605                 p_sys->i_contrast = strtol( psz_parser + strlen( "contrast=" ),
606                                             &psz_parser, 0 );
607             }
608             else if( !strncmp( psz_parser, "tuner=", strlen( "tuner=" ) ) )
609             {
610                 p_sys->i_tuner = strtol( psz_parser + strlen( "tuner=" ),
611                                          &psz_parser, 0 );
612             }
613             else if( !strncmp( psz_parser, "mjpeg", strlen( "mjpeg" ) ) )
614             {
615                 psz_parser += strlen( "mjpeg" );
616
617                 p_sys->b_mjpeg = true;
618             }
619             else if( !strncmp( psz_parser, "decimation=",
620                         strlen( "decimation=" ) ) )
621             {
622                 p_sys->i_decimation =
623                     strtol( psz_parser + strlen( "decimation=" ),
624                             &psz_parser, 0 );
625             }
626             else if( !strncmp( psz_parser, "quality=",
627                         strlen( "quality=" ) ) )
628             {
629                 p_sys->i_quality =
630                     strtol( psz_parser + strlen( "quality=" ),
631                             &psz_parser, 0 );
632             }
633             else if( !strncmp( psz_parser, "fps=", strlen( "fps=" ) ) )
634             {
635                 p_sys->f_fps = us_strtof( psz_parser + strlen( "fps=" ),
636                                           &psz_parser );
637             }
638             else if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) )
639              || !strncmp( psz_parser, "samplerate=", strlen( "samplerate=" ) )
640              || !strncmp( psz_parser, "stereo", strlen( "stereo" ) )
641              || !strncmp( psz_parser, "mono", strlen( "mono" ) ) )
642             {
643                 if( strchr( psz_parser, ':' ) )
644                     psz_parser = strchr( psz_parser, ':' );
645                 else
646                     psz_parser += strlen( psz_parser );
647                 msg_Err( p_demux, AUDIO_DEPRECATED_ERROR );
648             }
649             else
650             {
651                 msg_Warn( p_demux, "unknown option" );
652             }
653
654             while( *psz_parser && *psz_parser != ':' )
655             {
656                 psz_parser++;
657             }
658
659             if( *psz_parser == '\0' )
660             {
661                 break;
662             }
663         }
664     }
665
666     if( *psz_dup )
667         p_sys->psz_device = strdup( psz_dup );
668     else
669         p_sys->psz_device = strdup( V4L_DEFAULT );
670     free( psz_dup );
671 }
672
673 /*****************************************************************************
674  * OpenVideoDev:
675  *****************************************************************************/
676 static int OpenVideoDev( demux_t *p_demux, char *psz_device )
677 {
678     demux_sys_t *p_sys = p_demux->p_sys;
679     int i_fd;
680
681     struct video_channel vid_channel;
682     struct mjpeg_params mjpeg;
683     int i;
684
685     if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
686     {
687         msg_Err( p_demux, "cannot open device (%m)" );
688         goto vdev_failed;
689     }
690
691     if( ioctl( i_fd, VIDIOCGCAP, &p_sys->vid_cap ) < 0 )
692     {
693         msg_Err( p_demux, "cannot get capabilities (%m)" );
694         goto vdev_failed;
695     }
696
697     msg_Dbg( p_demux,
698              "V4L device %s %d channels %d audios %d < w < %d %d < h < %d",
699              p_sys->vid_cap.name,
700              p_sys->vid_cap.channels,
701              p_sys->vid_cap.audios,
702              p_sys->vid_cap.minwidth,  p_sys->vid_cap.maxwidth,
703              p_sys->vid_cap.minheight, p_sys->vid_cap.maxheight );
704
705     if( p_sys->i_channel < 0 || p_sys->i_channel >= p_sys->vid_cap.channels )
706     {
707         msg_Dbg( p_demux, "invalid channel, falling back on channel 0" );
708         p_sys->i_channel = 0;
709     }
710     if( p_sys->vid_cap.audios && p_sys->i_audio >= p_sys->vid_cap.audios )
711     {
712         msg_Dbg( p_demux, "invalid audio, falling back with no audio" );
713         p_sys->i_audio = -1;
714     }
715
716     if( p_sys->i_width < p_sys->vid_cap.minwidth ||
717         p_sys->i_width > p_sys->vid_cap.maxwidth )
718     {
719         msg_Dbg( p_demux, "invalid width %i", p_sys->i_width );
720         p_sys->i_width = 0;
721     }
722     if( p_sys->i_height < p_sys->vid_cap.minheight ||
723         p_sys->i_height > p_sys->vid_cap.maxheight )
724     {
725         msg_Dbg( p_demux, "invalid height %i", p_sys->i_height );
726         p_sys->i_height = 0;
727     }
728
729     if( !( p_sys->vid_cap.type & VID_TYPE_CAPTURE ) )
730     {
731         msg_Err( p_demux, "cannot grab" );
732         goto vdev_failed;
733     }
734
735     vid_channel.channel = p_sys->i_channel;
736     if( ioctl( i_fd, VIDIOCGCHAN, &vid_channel ) < 0 )
737     {
738         msg_Err( p_demux, "cannot get channel infos (%m)" );
739         goto vdev_failed;
740     }
741     msg_Dbg( p_demux,
742              "setting channel %s(%d) %d tuners flags=0x%x type=0x%x norm=0x%x",
743              vid_channel.name, vid_channel.channel, vid_channel.tuners,
744              vid_channel.flags, vid_channel.type, vid_channel.norm );
745
746     if( p_sys->i_tuner >= vid_channel.tuners )
747     {
748         msg_Dbg( p_demux, "invalid tuner, falling back on tuner 0" );
749         p_sys->i_tuner = 0;
750     }
751
752     vid_channel.norm = p_sys->i_norm;
753     if( ioctl( i_fd, VIDIOCSCHAN, &vid_channel ) < 0 )
754     {
755         msg_Err( p_demux, "cannot set channel (%m)" );
756         goto vdev_failed;
757     }
758
759     if( vid_channel.flags & VIDEO_VC_TUNER )
760     {
761
762         /* set tuner */
763 #if 0
764         struct video_tuner vid_tuner;
765         if( p_sys->i_tuner >= 0 )
766         {
767             vid_tuner.tuner = p_sys->i_tuner;
768             if( ioctl( i_fd, VIDIOCGTUNER, &vid_tuner ) < 0 )
769             {
770                 msg_Err( p_demux, "cannot get tuner (%m)" );
771                 goto vdev_failed;
772             }
773             msg_Dbg( p_demux, "tuner %s low=%d high=%d, flags=0x%x "
774                      "mode=0x%x signal=0x%x",
775                      vid_tuner.name, vid_tuner.rangelow, vid_tuner.rangehigh,
776                      vid_tuner.flags, vid_tuner.mode, vid_tuner.signal );
777
778             msg_Dbg( p_demux, "setting tuner %s (%d)",
779                      vid_tuner.name, vid_tuner.tuner );
780
781             /* FIXME FIXME to be checked FIXME FIXME */
782             //vid_tuner.mode = p_sys->i_norm;
783             if( ioctl( i_fd, VIDIOCSTUNER, &vid_tuner ) < 0 )
784             {
785                 msg_Err( p_demux, "cannot set tuner (%m)" );
786                 goto vdev_failed;
787             }
788         }
789 #endif
790
791         /* Show a warning if frequency is < than 30000.
792          * User is certainly usint old syntax. */
793
794
795         /* set frequency */
796         if( p_sys->i_frequency >= 0 )
797         {
798             int driver_frequency = p_sys->i_frequency * 16 /1000;
799             if( ioctl( i_fd, VIDIOCSFREQ, &driver_frequency ) < 0 )
800             {
801                 msg_Err( p_demux, "cannot set frequency (%m)" );
802                 goto vdev_failed;
803             }
804             msg_Dbg( p_demux, "frequency %d (%d)", p_sys->i_frequency,
805                                                    driver_frequency );
806         }
807     }
808
809     /* set audio */
810     if( vid_channel.flags & VIDEO_VC_AUDIO )
811     {
812         struct video_audio      vid_audio;
813
814         /* XXX TODO volume, balance, ... */
815         if( p_sys->i_audio >= 0 )
816         {
817             vid_audio.audio = p_sys->i_audio;
818             if( ioctl( i_fd, VIDIOCGAUDIO, &vid_audio ) < 0 )
819             {
820                 msg_Err( p_demux, "cannot get audio (%m)" );
821                 goto vdev_failed;
822             }
823
824             /* unmute audio */
825             vid_audio.flags &= ~VIDEO_AUDIO_MUTE;
826
827             if( ioctl( i_fd, VIDIOCSAUDIO, &vid_audio ) < 0 )
828             {
829                 msg_Err( p_demux, "cannot set audio (%m)" );
830                 goto vdev_failed;
831             }
832         }
833
834     }
835
836     /* establish basic params with input and norm before feeling width
837      * or height */
838     if( p_sys->b_mjpeg )
839     {
840         struct quicktime_mjpeg_app1 *p_app1;
841         int32_t i_offset;
842
843         if( ioctl( i_fd, MJPIOC_G_PARAMS, &mjpeg ) < 0 )
844         {
845             msg_Err( p_demux, "cannot get mjpeg params (%m)" );
846             goto vdev_failed;
847         }
848         mjpeg.input = p_sys->i_channel;
849         mjpeg.norm  = p_sys->i_norm;
850         mjpeg.decimation = p_sys->i_decimation;
851
852         if( p_sys->i_width )
853             mjpeg.img_width = p_sys->i_width / p_sys->i_decimation;
854         if( p_sys->i_height )
855             mjpeg.img_height = p_sys->i_height / p_sys->i_decimation;
856
857         /* establish Quicktime APP1 marker while we are here */
858         mjpeg.APPn = 1;
859         mjpeg.APP_len = 40;
860
861         /* aligned */
862         p_app1 = (struct quicktime_mjpeg_app1 *)mjpeg.APP_data;
863         p_app1->i_reserved = 0;
864         p_app1->i_tag = VLC_FOURCC( 'm','j','p','g' );
865         p_app1->i_field_size = 0;
866         p_app1->i_padded_field_size = 0;
867         p_app1->i_next_field = 0;
868         /* XXX WARNING XXX */
869         /* these's nothing magic about these values.  We are dangerously
870          * assuming the encoder card is encoding mjpeg-a and is not throwing
871          * in marker tags we aren't expecting.  It's bad enough we have to
872          * search through the jpeg output for every frame we grab just to
873          * find the first field's end marker, so we take this risk to boost
874          * performance.
875          * This is really something the driver could do for us because this
876          * does conform to standards outside of Apple Quicktime.
877          */
878         i_offset = 0x2e;
879         p_app1->i_DQT_offset = hton32( i_offset );
880         i_offset = 0xb4;
881         p_app1->i_DHT_offset = hton32( i_offset );
882         i_offset = 0x258;
883         p_app1->i_SOF_offset = hton32( i_offset );
884         i_offset = 0x26b;
885         p_app1->i_SOS_offset = hton32( i_offset );
886         i_offset = 0x279;
887         p_app1->i_data_offset = hton32( i_offset );
888
889         /* SOF and SOS aren't specified by the mjpeg API because they aren't
890          * optional.  They will be present in the output. */
891         mjpeg.jpeg_markers = JPEG_MARKER_DHT | JPEG_MARKER_DQT;
892
893         if( ioctl( i_fd, MJPIOC_S_PARAMS, &mjpeg ) < 0 )
894         {
895             msg_Err( p_demux, "cannot set mjpeg params (%m)" );
896             goto vdev_failed;
897         }
898
899         p_sys->i_width = mjpeg.img_width * mjpeg.HorDcm;
900         p_sys->i_height = mjpeg.img_height * mjpeg.VerDcm *
901             mjpeg.field_per_buff;
902     }
903
904     /* fix width/height */
905     if( !p_sys->b_mjpeg && ( p_sys->i_width == 0 || p_sys->i_height == 0 ) )
906     {
907         struct video_window vid_win;
908
909         if( ioctl( i_fd, VIDIOCGWIN, &vid_win ) < 0 )
910         {
911             msg_Err( p_demux, "cannot get win (%m)" );
912             goto vdev_failed;
913         }
914         p_sys->i_width  = vid_win.width;
915         p_sys->i_height = vid_win.height;
916
917         if( !p_sys->i_width || !p_sys->i_height )
918         {
919             p_sys->i_width = p_sys->vid_cap.maxwidth;
920             p_sys->i_height = p_sys->vid_cap.maxheight;
921         }
922
923         if( !p_sys->i_width || !p_sys->i_height )
924         {
925             msg_Err( p_demux, "invalid video size (%ix%i)",
926                      p_sys->i_width, p_sys->i_height );
927             goto vdev_failed;
928         }
929
930         msg_Dbg( p_demux, "will use %dx%d", p_sys->i_width, p_sys->i_height );
931     }
932
933     if( !p_sys->b_mjpeg )
934     {
935         /* set hue/color/.. */
936         if( ioctl( i_fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 )
937         {
938             struct video_picture vid_picture = p_sys->vid_picture;
939
940             if( p_sys->i_brightness >= 0 && p_sys->i_brightness < 65536 )
941             {
942                 vid_picture.brightness = p_sys->i_brightness;
943             }
944             if( p_sys->i_colour >= 0 && p_sys->i_colour < 65536 )
945             {
946                 vid_picture.colour = p_sys->i_colour;
947             }
948             if( p_sys->i_hue >= 0 && p_sys->i_hue < 65536 )
949             {
950                 vid_picture.hue = p_sys->i_hue;
951             }
952             if( p_sys->i_contrast  >= 0 && p_sys->i_contrast < 65536 )
953             {
954                 vid_picture.contrast = p_sys->i_contrast;
955             }
956             if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
957             {
958                 msg_Dbg( p_demux, "v4l device uses brightness: %d",
959                          vid_picture.brightness );
960                 msg_Dbg( p_demux, "v4l device uses colour: %d",
961                          vid_picture.colour );
962                 msg_Dbg( p_demux, "v4l device uses hue: %d", vid_picture.hue );
963                 msg_Dbg( p_demux, "v4l device uses contrast: %d",
964                          vid_picture.contrast );
965                 p_sys->vid_picture = vid_picture;
966             }
967         }
968
969         /* Find out video format used by device */
970         if( ioctl( i_fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 )
971         {
972             struct video_picture vid_picture = p_sys->vid_picture;
973             char *psz;
974             int i;
975
976             p_sys->i_fourcc = 0;
977
978             psz = var_CreateGetString( p_demux, "v4l-chroma" );
979
980             const vlc_fourcc_t i_chroma =
981                 vlc_fourcc_GetCodecFromString( VIDEO_ES, psz );
982             if( i_chroma )
983             {
984                 vid_picture.palette = 0;
985
986                 /* Find out v4l chroma code */
987                 for( i = 0; v4lchroma_to_fourcc[i].i_v4l != 0; i++ )
988                 {
989                     if( v4lchroma_to_fourcc[i].i_fourcc == i_chroma )
990                     {
991                         vid_picture.palette = v4lchroma_to_fourcc[i].i_v4l;
992                         break;
993                     }
994                 }
995             }
996             free( psz );
997
998             if( vid_picture.palette &&
999                 !ioctl( i_fd, VIDIOCSPICT, &vid_picture ) )
1000             {
1001                 p_sys->vid_picture = vid_picture;
1002             }
1003             else
1004             {
1005                 /* Try to set the format to something easy to encode */
1006                 vid_picture.palette = VIDEO_PALETTE_YUV420P;
1007                 if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
1008                 {
1009                     p_sys->vid_picture = vid_picture;
1010                 }
1011                 else
1012                 {
1013                     vid_picture.palette = VIDEO_PALETTE_YUV422P;
1014                     if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
1015                     {
1016                         p_sys->vid_picture = vid_picture;
1017                     }
1018                 }
1019             }
1020
1021             /* Find out final format */
1022             for( i = 0; v4lchroma_to_fourcc[i].i_v4l != 0; i++ )
1023             {
1024                 if( v4lchroma_to_fourcc[i].i_v4l == p_sys->vid_picture.palette)
1025                 {
1026                     p_sys->i_fourcc = v4lchroma_to_fourcc[i].i_fourcc;
1027                     break;
1028                 }
1029             }
1030         }
1031         else
1032         {
1033             msg_Err( p_demux, "ioctl VIDIOCGPICT failed" );
1034             goto vdev_failed;
1035         }
1036     }
1037
1038     if( p_sys->b_mjpeg )
1039     {
1040         int i;
1041
1042         p_sys->mjpeg_buffers.count = 8;
1043         p_sys->mjpeg_buffers.size = MJPEG_BUFFER_SIZE;
1044
1045         if( ioctl( i_fd, MJPIOC_REQBUFS, &p_sys->mjpeg_buffers ) < 0 )
1046         {
1047             msg_Err( p_demux, "mmap unsupported" );
1048             goto vdev_failed;
1049         }
1050
1051         p_sys->p_video_mmap = mmap( 0,
1052                 p_sys->mjpeg_buffers.size * p_sys->mjpeg_buffers.count,
1053                 PROT_READ | PROT_WRITE, MAP_SHARED, i_fd, 0 );
1054         if( p_sys->p_video_mmap == MAP_FAILED )
1055         {
1056             msg_Err( p_demux, "mmap failed" );
1057             goto vdev_failed;
1058         }
1059
1060         p_sys->i_fourcc  = VLC_CODEC_MJPG;
1061         p_sys->i_frame_pos = -1;
1062
1063         /* queue up all the frames */
1064         for( i = 0; i < (int)p_sys->mjpeg_buffers.count; i++ )
1065         {
1066             if( ioctl( i_fd, MJPIOC_QBUF_CAPT, &i ) < 0 )
1067             {
1068                 msg_Err( p_demux, "unable to queue frame" );
1069                 goto vdev_failed;
1070             }
1071         }
1072     }
1073     else
1074     {
1075         /* Fill in picture_t fields */
1076         if( picture_Setup( &p_sys->pic, p_sys->i_fourcc,
1077                            p_sys->i_width, p_sys->i_height, p_sys->i_width *
1078                            VOUT_ASPECT_FACTOR / p_sys->i_height ) )
1079         {
1080             msg_Err( p_demux, "unsupported chroma" );
1081             goto vdev_failed;
1082         }
1083         p_sys->i_video_frame_size = 0;
1084         for( i = 0; i < p_sys->pic.i_planes; i++ )
1085         {
1086             p_sys->i_video_frame_size += p_sys->pic.p[i].i_visible_lines *
1087               p_sys->pic.p[i].i_visible_pitch;
1088         }
1089
1090         msg_Dbg( p_demux, "v4l device uses frame size: %i",
1091                  p_sys->i_video_frame_size );
1092         msg_Dbg( p_demux, "v4l device uses chroma: %4.4s",
1093                 (char*)&p_sys->i_fourcc );
1094
1095         /* Allocate mmap buffer */
1096         if( ioctl( i_fd, VIDIOCGMBUF, &p_sys->vid_mbuf ) < 0 )
1097         {
1098             msg_Err( p_demux, "mmap unsupported" );
1099             goto vdev_failed;
1100         }
1101
1102         p_sys->p_video_mmap = mmap( 0, p_sys->vid_mbuf.size,
1103                                     PROT_READ|PROT_WRITE, MAP_SHARED,
1104                                     i_fd, 0 );
1105         if( p_sys->p_video_mmap == MAP_FAILED )
1106         {
1107             /* FIXME -> normal read */
1108             msg_Err( p_demux, "mmap failed" );
1109             goto vdev_failed;
1110         }
1111
1112         /* init grabbing */
1113         p_sys->vid_mmap.frame  = 0;
1114         p_sys->vid_mmap.width  = p_sys->i_width;
1115         p_sys->vid_mmap.height = p_sys->i_height;
1116         p_sys->vid_mmap.format = p_sys->vid_picture.palette;
1117         if( ioctl( i_fd, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 )
1118         {
1119             msg_Warn( p_demux, "%4.4s refused", (char*)&p_sys->i_fourcc );
1120             msg_Err( p_demux, "chroma selection failed" );
1121             goto vdev_failed;
1122         }
1123     }
1124     return i_fd;
1125
1126 vdev_failed:
1127
1128     if( i_fd >= 0 ) close( i_fd );
1129     return -1;
1130 }
1131
1132 /*****************************************************************************
1133  * GrabVideo:
1134  *****************************************************************************/
1135 static uint8_t *GrabCapture( demux_t *p_demux )
1136 {
1137     demux_sys_t *p_sys = p_demux->p_sys;
1138     int i_captured_frame = p_sys->i_frame_pos;
1139
1140     p_sys->vid_mmap.frame = (p_sys->i_frame_pos + 1) % p_sys->vid_mbuf.frames;
1141
1142     while( ioctl( p_sys->i_fd, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 )
1143     {
1144         if( errno != EAGAIN )
1145         {
1146             msg_Err( p_demux, "failed capturing new frame" );
1147             return NULL;
1148         }
1149
1150         if( !vlc_object_alive (p_demux) )
1151         {
1152             return NULL;
1153         }
1154
1155         msg_Dbg( p_demux, "grab failed, trying again" );
1156     }
1157
1158     while( ioctl(p_sys->i_fd, VIDIOCSYNC, &p_sys->i_frame_pos) < 0 )
1159     {
1160         if( errno != EAGAIN && errno != EINTR )
1161         {
1162             msg_Err( p_demux, "failed syncing new frame" );
1163             return NULL;
1164         }
1165     }
1166
1167     p_sys->i_frame_pos = p_sys->vid_mmap.frame;
1168     /* leave i_video_frame_size alone */
1169     return p_sys->p_video_mmap + p_sys->vid_mbuf.offsets[i_captured_frame];
1170 }
1171
1172 static uint8_t *GrabMJPEG( demux_t *p_demux )
1173 {
1174     demux_sys_t *p_sys = p_demux->p_sys;
1175     struct mjpeg_sync sync;
1176     uint8_t *p_frame, *p_field, *p;
1177     uint16_t tag;
1178     uint32_t i_size;
1179     struct quicktime_mjpeg_app1 *p_app1 = NULL;
1180
1181     /* re-queue the last frame we sync'd */
1182     if( p_sys->i_frame_pos != -1 )
1183     {
1184         while( ioctl( p_sys->i_fd, MJPIOC_QBUF_CAPT,
1185                                        &p_sys->i_frame_pos ) < 0 )
1186         {
1187             if( errno != EAGAIN && errno != EINTR )
1188             {
1189                 msg_Err( p_demux, "failed capturing new frame" );
1190                 return NULL;
1191             }
1192         }
1193     }
1194
1195     /* sync on the next frame */
1196     while( ioctl( p_sys->i_fd, MJPIOC_SYNC, &sync ) < 0 )
1197     {
1198         if( errno != EAGAIN && errno != EINTR )
1199         {
1200             msg_Err( p_demux, "failed syncing new frame" );
1201             return NULL;
1202         }
1203     }
1204
1205     p_sys->i_frame_pos = sync.frame;
1206     p_frame = p_sys->p_video_mmap + p_sys->mjpeg_buffers.size * sync.frame;
1207
1208     /* p_frame now points to the data.  fix up the Quicktime APP1 marker */
1209     tag = 0xffd9;
1210     tag = hton16( tag );
1211     p_field = p_frame;
1212
1213     /* look for EOI */
1214     p = memmem( p_field, sync.length, &tag, 2 );
1215
1216     if( p )
1217     {
1218         p += 2; /* data immediately following EOI */
1219         /* UNALIGNED! */
1220         p_app1 = (struct quicktime_mjpeg_app1 *)(p_field + 6);
1221
1222         i_size = ((uint32_t)(p - p_field));
1223         i_size = hton32( i_size );
1224         memcpy( &p_app1->i_field_size, &i_size, 4 );
1225
1226         while( *p == 0xff && *(p+1) == 0xff )
1227             p++;
1228
1229         i_size = ((uint32_t)(p - p_field));
1230         i_size = hton32( i_size );
1231         memcpy( &p_app1->i_padded_field_size, &i_size, 4 );
1232     }
1233
1234     tag = 0xffd8;
1235     tag = hton16( tag );
1236     p_field = memmem( p, sync.length - (size_t)(p - p_frame), &tag, 2 );
1237
1238     if( p_field )
1239     {
1240         i_size = (uint32_t)(p_field - p_frame);
1241         i_size = hton32( i_size );
1242         memcpy( &p_app1->i_next_field, &i_size, 4 );
1243
1244         /* UNALIGNED! */
1245         p_app1 = (struct quicktime_mjpeg_app1 *)(p_field + 6);
1246         tag = 0xffd9;
1247         tag = hton16( tag );
1248         p = memmem( p_field, sync.length - (size_t)(p_field - p_frame),
1249                 &tag, 2 );
1250
1251         if( !p )
1252         {
1253             /* sometimes the second field doesn't have the EOI.  just put it
1254              * there
1255              */
1256             p = p_frame + sync.length;
1257             memcpy( p, &tag, 2 );
1258             sync.length += 2;
1259         }
1260
1261         p += 2;
1262         i_size = (uint32_t)(p - p_field);
1263         i_size = hton32( i_size );
1264         memcpy( &p_app1->i_field_size, &i_size, 4 );
1265         i_size = (uint32_t)(sync.length - (uint32_t)(p_field - p_frame));
1266         i_size = hton32( i_size );
1267         memcpy( &p_app1->i_padded_field_size, &i_size, 4 );
1268     }
1269
1270     p_sys->i_video_frame_size = sync.length;
1271     return p_frame;
1272 }
1273
1274 static block_t *GrabVideo( demux_t *p_demux )
1275 {
1276     demux_sys_t *p_sys = p_demux->p_sys;
1277     uint8_t     *p_frame;
1278     block_t     *p_block;
1279
1280     if( p_sys->f_fps >= 0.1 && p_sys->i_video_pts > 0 )
1281     {
1282         mtime_t i_dur = (mtime_t)((double)1000000 / (double)p_sys->f_fps);
1283
1284         /* Did we wait long enough ? (frame rate reduction) */
1285         if( p_sys->i_video_pts + i_dur > mdate() ) return 0;
1286     }
1287
1288     if( p_sys->b_mjpeg ) p_frame = GrabMJPEG( p_demux );
1289     else p_frame = GrabCapture( p_demux );
1290
1291     if( !p_frame ) return 0;
1292
1293     if( !( p_block = block_New( p_demux, p_sys->i_video_frame_size ) ) )
1294     {
1295         msg_Warn( p_demux, "cannot get block" );
1296         return 0;
1297     }
1298
1299     memcpy( p_block->p_buffer, p_frame, p_sys->i_video_frame_size );
1300     p_sys->i_video_pts = p_block->i_pts = p_block->i_dts = mdate();
1301
1302     return p_block;
1303 }