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