]> git.sesse.net Git - vlc/blob - modules/access/v4l.c
macosx: fixed menubar appearance in fullscreen mode by partially reverting [46c93c9cc...
[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_vout.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     int i_fourcc;
216
217 } v4lchroma_to_fourcc[] =
218 {
219     { VIDEO_PALETTE_GREY, VLC_FOURCC( 'G', 'R', 'E', 'Y' ) },
220     { VIDEO_PALETTE_HI240, VLC_FOURCC( 'I', '2', '4', '0' ) },
221     { VIDEO_PALETTE_RGB565, VLC_FOURCC( 'R', 'V', '1', '6' ) },
222     { VIDEO_PALETTE_RGB555, VLC_FOURCC( 'R', 'V', '1', '5' ) },
223     { VIDEO_PALETTE_RGB24, VLC_FOURCC( 'R', 'V', '2', '4' ) },
224     { VIDEO_PALETTE_RGB32, VLC_FOURCC( 'R', 'V', '3', '2' ) },
225     { VIDEO_PALETTE_YUV422, VLC_FOURCC( 'Y', 'U', 'Y', '2' ) },
226     { VIDEO_PALETTE_YUV422, VLC_FOURCC( 'Y', 'U', 'Y', 'V' ) },
227     { VIDEO_PALETTE_YUYV, VLC_FOURCC( 'Y', 'U', 'Y', '2' ) },
228     { VIDEO_PALETTE_YUYV, VLC_FOURCC( 'Y', 'U', 'Y', 'V' ) },
229     { VIDEO_PALETTE_UYVY, VLC_FOURCC( 'U', 'Y', 'V', 'Y' ) },
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_FOURCC( 'I', '4', '2', '2' ) },
234     { VIDEO_PALETTE_YUV420P, VLC_FOURCC( 'I', '4', '2', '0' ) },
235     { VIDEO_PALETTE_YUV411P, VLC_FOURCC( 'I', '4', '1', '1' ) },
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_FOURCC('R','V','1','5'):
358             fmt.video.i_rmask = 0x001f;
359             fmt.video.i_gmask = 0x03e0;
360             fmt.video.i_bmask = 0x7c00;
361             break;
362         case VLC_FOURCC('R','V','1','6'):
363             fmt.video.i_rmask = 0x001f;
364             fmt.video.i_gmask = 0x07e0;
365             fmt.video.i_bmask = 0xf800;
366             break;
367         case VLC_FOURCC('R','V','2','4'):
368         case VLC_FOURCC('R','V','3','2'):
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_SET_PAUSE_STATE:
428         case DEMUX_CAN_CONTROL_PACE:
429             pb = (bool*)va_arg( args, bool * );
430             *pb = false;
431             return VLC_SUCCESS;
432
433         case DEMUX_GET_PTS_DELAY:
434             pi64 = (int64_t*)va_arg( args, int64_t * );
435             *pi64 = (int64_t)var_GetInteger( p_demux, "v4l-caching" ) * 1000;
436             return VLC_SUCCESS;
437
438         case DEMUX_GET_TIME:
439             pi64 = (int64_t*)va_arg( args, int64_t * );
440             *pi64 = mdate();
441             return VLC_SUCCESS;
442
443         /* TODO implement others */
444         default:
445             return VLC_EGENERIC;
446     }
447
448     return VLC_EGENERIC;
449 }
450
451 /*****************************************************************************
452  * Demux:
453  *****************************************************************************/
454 static int Demux( demux_t *p_demux )
455 {
456     demux_sys_t *p_sys = p_demux->p_sys;
457
458     block_t *p_block = GrabVideo( p_demux );
459
460     if( !p_block )
461     {
462         msleep( 10000 ); /* Unfortunately v4l doesn't allow polling */
463         return 1;
464     }
465
466     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
467     es_out_Send( p_demux->out, p_sys->p_es, p_block );
468
469     return 1;
470 }
471
472 /*****************************************************************************
473  * ParseMRL: parse the options contained in the MRL
474  *****************************************************************************/
475 static void ParseMRL( demux_t *p_demux )
476 {
477     demux_sys_t *p_sys = p_demux->p_sys;
478
479     char *psz_dup = strdup( p_demux->psz_path );
480     char *psz_parser = psz_dup;
481
482     while( *psz_parser && *psz_parser != ':' )
483     {
484         psz_parser++;
485     }
486
487     if( *psz_parser == ':' )
488     {
489         /* read options */
490         for( ;; )
491         {
492             *psz_parser++ = '\0';
493             if( !strncmp( psz_parser, "channel=", strlen( "channel=" ) ) )
494             {
495                 p_sys->i_channel = strtol( psz_parser + strlen( "channel=" ),
496                                            &psz_parser, 0 );
497             }
498             else if( !strncmp( psz_parser, "norm=", strlen( "norm=" ) ) )
499             {
500                 psz_parser += strlen( "norm=" );
501                 if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
502                 {
503                     p_sys->i_norm = VIDEO_MODE_PAL;
504                     psz_parser += strlen( "pal" );
505                 }
506                 else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
507                 {
508                     p_sys->i_norm = VIDEO_MODE_NTSC;
509                     psz_parser += strlen( "ntsc" );
510                 }
511                 else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
512                 {
513                     p_sys->i_norm = VIDEO_MODE_SECAM;
514                     psz_parser += strlen( "secam" );
515                 }
516                 else if( !strncmp( psz_parser, "auto", strlen( "auto" ) ) )
517                 {
518                     p_sys->i_norm = VIDEO_MODE_AUTO;
519                     psz_parser += strlen( "auto" );
520                 }
521                 else
522                 {
523                     p_sys->i_norm = strtol( psz_parser, &psz_parser, 0 );
524                 }
525             }
526             else if( !strncmp( psz_parser, "frequency=",
527                                strlen( "frequency=" ) ) )
528             {
529                 p_sys->i_frequency =
530                     strtol( psz_parser + strlen( "frequency=" ),
531                             &psz_parser, 0 );
532                 if( p_sys->i_frequency < 30000 )
533                 {
534                     msg_Warn( p_demux, "v4l syntax has changed : "
535                               "'frequency' is now channel frequency in kHz");
536                 }
537             }
538             else if( !strncmp( psz_parser, "audio=", strlen( "audio=" ) ) )
539             {
540                 p_sys->i_audio = strtol( psz_parser + strlen( "audio=" ),
541                                          &psz_parser, 0 );
542             }
543             else if( !strncmp( psz_parser, "size=", strlen( "size=" ) ) )
544             {
545                 psz_parser += strlen( "size=" );
546                 if( !strncmp( psz_parser, "subqcif", strlen( "subqcif" ) ) )
547                 {
548                     p_sys->i_width  = 128;
549                     p_sys->i_height = 96;
550                 }
551                 else if( !strncmp( psz_parser, "qsif", strlen( "qsif" ) ) )
552                 {
553                     p_sys->i_width  = 160;
554                     p_sys->i_height = 120;
555                 }
556                 else if( !strncmp( psz_parser, "qcif", strlen( "qcif" ) ) )
557                 {
558                     p_sys->i_width  = 176;
559                     p_sys->i_height = 144;
560                 }
561                 else if( !strncmp( psz_parser, "sif", strlen( "sif" ) ) )
562                 {
563                     p_sys->i_width  = 320;
564                     p_sys->i_height = 244;
565                 }
566                 else if( !strncmp( psz_parser, "cif", strlen( "cif" ) ) )
567                 {
568                     p_sys->i_width  = 352;
569                     p_sys->i_height = 288;
570                 }
571                 else if( !strncmp( psz_parser, "vga", strlen( "vga" ) ) )
572                 {
573                     p_sys->i_width  = 640;
574                     p_sys->i_height = 480;
575                 }
576                 else
577                 {
578                     /* widthxheight */
579                     p_sys->i_width = strtol( psz_parser, &psz_parser, 0 );
580                     if( *psz_parser == 'x' || *psz_parser == 'X')
581                     {
582                         p_sys->i_height = strtol( psz_parser + 1,
583                                                   &psz_parser, 0 );
584                     }
585                     msg_Dbg( p_demux, "WxH %dx%d", p_sys->i_width,
586                              p_sys->i_height );
587                 }
588             }
589             else if( !strncmp( psz_parser, "brightness=", strlen( "brightness=" ) ) )
590             {
591                 p_sys->i_brightness = strtol( psz_parser + strlen( "brightness=" ),
592                                               &psz_parser, 0 );
593             }
594             else if( !strncmp( psz_parser, "colour=", strlen( "colour=" ) ) )
595             {
596                 p_sys->i_colour = strtol( psz_parser + strlen( "colour=" ),
597                                           &psz_parser, 0 );
598             }
599             else if( !strncmp( psz_parser, "hue=", strlen( "hue=" ) ) )
600             {
601                 p_sys->i_hue = strtol( psz_parser + strlen( "hue=" ),
602                                        &psz_parser, 0 );
603             }
604             else if( !strncmp( psz_parser, "contrast=", strlen( "contrast=" ) ) )
605             {
606                 p_sys->i_contrast = strtol( psz_parser + strlen( "contrast=" ),
607                                             &psz_parser, 0 );
608             }
609             else if( !strncmp( psz_parser, "tuner=", strlen( "tuner=" ) ) )
610             {
611                 p_sys->i_tuner = strtol( psz_parser + strlen( "tuner=" ),
612                                          &psz_parser, 0 );
613             }
614             else if( !strncmp( psz_parser, "mjpeg", strlen( "mjpeg" ) ) )
615             {
616                 psz_parser += strlen( "mjpeg" );
617
618                 p_sys->b_mjpeg = true;
619             }
620             else if( !strncmp( psz_parser, "decimation=",
621                         strlen( "decimation=" ) ) )
622             {
623                 p_sys->i_decimation =
624                     strtol( psz_parser + strlen( "decimation=" ),
625                             &psz_parser, 0 );
626             }
627             else if( !strncmp( psz_parser, "quality=",
628                         strlen( "quality=" ) ) )
629             {
630                 p_sys->i_quality =
631                     strtol( psz_parser + strlen( "quality=" ),
632                             &psz_parser, 0 );
633             }
634             else if( !strncmp( psz_parser, "fps=", strlen( "fps=" ) ) )
635             {
636                 p_sys->f_fps = strtof( psz_parser + strlen( "fps=" ),
637                                        &psz_parser );
638             }
639             else if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) )
640              || !strncmp( psz_parser, "samplerate=", strlen( "samplerate=" ) )
641              || !strncmp( psz_parser, "stereo", strlen( "stereo" ) )
642              || !strncmp( psz_parser, "mono", strlen( "mono" ) ) )
643             {
644                 if( strchr( psz_parser, ':' ) )
645                     psz_parser = strchr( psz_parser, ':' );
646                 else
647                     psz_parser += strlen( psz_parser );
648                 msg_Err( p_demux, AUDIO_DEPRECATED_ERROR );
649             }
650             else
651             {
652                 msg_Warn( p_demux, "unknown option" );
653             }
654
655             while( *psz_parser && *psz_parser != ':' )
656             {
657                 psz_parser++;
658             }
659
660             if( *psz_parser == '\0' )
661             {
662                 break;
663             }
664         }
665     }
666
667     if( *psz_dup )
668         p_sys->psz_device = strdup( psz_dup );
669     else
670         p_sys->psz_device = strdup( V4L_DEFAULT );
671     free( psz_dup );
672 }
673
674 /*****************************************************************************
675  * OpenVideoDev:
676  *****************************************************************************/
677 static int OpenVideoDev( demux_t *p_demux, char *psz_device )
678 {
679     demux_sys_t *p_sys = p_demux->p_sys;
680     int i_fd;
681
682     struct video_channel vid_channel;
683     struct mjpeg_params mjpeg;
684     int i;
685
686     if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
687     {
688         msg_Err( p_demux, "cannot open device (%m)" );
689         goto vdev_failed;
690     }
691
692     if( ioctl( i_fd, VIDIOCGCAP, &p_sys->vid_cap ) < 0 )
693     {
694         msg_Err( p_demux, "cannot get capabilities (%m)" );
695         goto vdev_failed;
696     }
697
698     msg_Dbg( p_demux,
699              "V4L device %s %d channels %d audios %d < w < %d %d < h < %d",
700              p_sys->vid_cap.name,
701              p_sys->vid_cap.channels,
702              p_sys->vid_cap.audios,
703              p_sys->vid_cap.minwidth,  p_sys->vid_cap.maxwidth,
704              p_sys->vid_cap.minheight, p_sys->vid_cap.maxheight );
705
706     if( p_sys->i_channel < 0 || p_sys->i_channel >= p_sys->vid_cap.channels )
707     {
708         msg_Dbg( p_demux, "invalid channel, falling back on channel 0" );
709         p_sys->i_channel = 0;
710     }
711     if( p_sys->vid_cap.audios && p_sys->i_audio >= p_sys->vid_cap.audios )
712     {
713         msg_Dbg( p_demux, "invalid audio, falling back with no audio" );
714         p_sys->i_audio = -1;
715     }
716
717     if( p_sys->i_width < p_sys->vid_cap.minwidth ||
718         p_sys->i_width > p_sys->vid_cap.maxwidth )
719     {
720         msg_Dbg( p_demux, "invalid width %i", p_sys->i_width );
721         p_sys->i_width = 0;
722     }
723     if( p_sys->i_height < p_sys->vid_cap.minheight ||
724         p_sys->i_height > p_sys->vid_cap.maxheight )
725     {
726         msg_Dbg( p_demux, "invalid height %i", p_sys->i_height );
727         p_sys->i_height = 0;
728     }
729
730     if( !( p_sys->vid_cap.type & VID_TYPE_CAPTURE ) )
731     {
732         msg_Err( p_demux, "cannot grab" );
733         goto vdev_failed;
734     }
735
736     vid_channel.channel = p_sys->i_channel;
737     if( ioctl( i_fd, VIDIOCGCHAN, &vid_channel ) < 0 )
738     {
739         msg_Err( p_demux, "cannot get channel infos (%m)" );
740         goto vdev_failed;
741     }
742     msg_Dbg( p_demux,
743              "setting channel %s(%d) %d tuners flags=0x%x type=0x%x norm=0x%x",
744              vid_channel.name, vid_channel.channel, vid_channel.tuners,
745              vid_channel.flags, vid_channel.type, vid_channel.norm );
746
747     if( p_sys->i_tuner >= vid_channel.tuners )
748     {
749         msg_Dbg( p_demux, "invalid tuner, falling back on tuner 0" );
750         p_sys->i_tuner = 0;
751     }
752
753     vid_channel.norm = p_sys->i_norm;
754     if( ioctl( i_fd, VIDIOCSCHAN, &vid_channel ) < 0 )
755     {
756         msg_Err( p_demux, "cannot set channel (%m)" );
757         goto vdev_failed;
758     }
759
760     if( vid_channel.flags & VIDEO_VC_TUNER )
761     {
762
763         /* set tuner */
764 #if 0
765         struct video_tuner vid_tuner;
766         if( p_sys->i_tuner >= 0 )
767         {
768             vid_tuner.tuner = p_sys->i_tuner;
769             if( ioctl( i_fd, VIDIOCGTUNER, &vid_tuner ) < 0 )
770             {
771                 msg_Err( p_demux, "cannot get tuner (%m)" );
772                 goto vdev_failed;
773             }
774             msg_Dbg( p_demux, "tuner %s low=%d high=%d, flags=0x%x "
775                      "mode=0x%x signal=0x%x",
776                      vid_tuner.name, vid_tuner.rangelow, vid_tuner.rangehigh,
777                      vid_tuner.flags, vid_tuner.mode, vid_tuner.signal );
778
779             msg_Dbg( p_demux, "setting tuner %s (%d)",
780                      vid_tuner.name, vid_tuner.tuner );
781
782             /* FIXME FIXME to be checked FIXME FIXME */
783             //vid_tuner.mode = p_sys->i_norm;
784             if( ioctl( i_fd, VIDIOCSTUNER, &vid_tuner ) < 0 )
785             {
786                 msg_Err( p_demux, "cannot set tuner (%m)" );
787                 goto vdev_failed;
788             }
789         }
790 #endif
791
792         /* Show a warning if frequency is < than 30000.
793          * User is certainly usint old syntax. */
794
795
796         /* set frequency */
797         if( p_sys->i_frequency >= 0 )
798         {
799             int driver_frequency = p_sys->i_frequency * 16 /1000;
800             if( ioctl( i_fd, VIDIOCSFREQ, &driver_frequency ) < 0 )
801             {
802                 msg_Err( p_demux, "cannot set frequency (%m)" );
803                 goto vdev_failed;
804             }
805             msg_Dbg( p_demux, "frequency %d (%d)", p_sys->i_frequency,
806                                                    driver_frequency );
807         }
808     }
809
810     /* set audio */
811     if( vid_channel.flags & VIDEO_VC_AUDIO )
812     {
813         struct video_audio      vid_audio;
814
815         /* XXX TODO volume, balance, ... */
816         if( p_sys->i_audio >= 0 )
817         {
818             vid_audio.audio = p_sys->i_audio;
819             if( ioctl( i_fd, VIDIOCGAUDIO, &vid_audio ) < 0 )
820             {
821                 msg_Err( p_demux, "cannot get audio (%m)" );
822                 goto vdev_failed;
823             }
824
825             /* unmute audio */
826             vid_audio.flags &= ~VIDEO_AUDIO_MUTE;
827
828             if( ioctl( i_fd, VIDIOCSAUDIO, &vid_audio ) < 0 )
829             {
830                 msg_Err( p_demux, "cannot set audio (%m)" );
831                 goto vdev_failed;
832             }
833         }
834
835     }
836
837     /* establish basic params with input and norm before feeling width
838      * or height */
839     if( p_sys->b_mjpeg )
840     {
841         struct quicktime_mjpeg_app1 *p_app1;
842         int32_t i_offset;
843
844         if( ioctl( i_fd, MJPIOC_G_PARAMS, &mjpeg ) < 0 )
845         {
846             msg_Err( p_demux, "cannot get mjpeg params (%m)" );
847             goto vdev_failed;
848         }
849         mjpeg.input = p_sys->i_channel;
850         mjpeg.norm  = p_sys->i_norm;
851         mjpeg.decimation = p_sys->i_decimation;
852
853         if( p_sys->i_width )
854             mjpeg.img_width = p_sys->i_width / p_sys->i_decimation;
855         if( p_sys->i_height )
856             mjpeg.img_height = p_sys->i_height / p_sys->i_decimation;
857
858         /* establish Quicktime APP1 marker while we are here */
859         mjpeg.APPn = 1;
860         mjpeg.APP_len = 40;
861
862         /* aligned */
863         p_app1 = (struct quicktime_mjpeg_app1 *)mjpeg.APP_data;
864         p_app1->i_reserved = 0;
865         p_app1->i_tag = VLC_FOURCC( 'm','j','p','g' );
866         p_app1->i_field_size = 0;
867         p_app1->i_padded_field_size = 0;
868         p_app1->i_next_field = 0;
869         /* XXX WARNING XXX */
870         /* these's nothing magic about these values.  We are dangerously
871          * assuming the encoder card is encoding mjpeg-a and is not throwing
872          * in marker tags we aren't expecting.  It's bad enough we have to
873          * search through the jpeg output for every frame we grab just to
874          * find the first field's end marker, so we take this risk to boost
875          * performance.
876          * This is really something the driver could do for us because this
877          * does conform to standards outside of Apple Quicktime.
878          */
879         i_offset = 0x2e;
880         p_app1->i_DQT_offset = hton32( i_offset );
881         i_offset = 0xb4;
882         p_app1->i_DHT_offset = hton32( i_offset );
883         i_offset = 0x258;
884         p_app1->i_SOF_offset = hton32( i_offset );
885         i_offset = 0x26b;
886         p_app1->i_SOS_offset = hton32( i_offset );
887         i_offset = 0x279;
888         p_app1->i_data_offset = hton32( i_offset );
889
890         /* SOF and SOS aren't specified by the mjpeg API because they aren't
891          * optional.  They will be present in the output. */
892         mjpeg.jpeg_markers = JPEG_MARKER_DHT | JPEG_MARKER_DQT;
893
894         if( ioctl( i_fd, MJPIOC_S_PARAMS, &mjpeg ) < 0 )
895         {
896             msg_Err( p_demux, "cannot set mjpeg params (%m)" );
897             goto vdev_failed;
898         }
899
900         p_sys->i_width = mjpeg.img_width * mjpeg.HorDcm;
901         p_sys->i_height = mjpeg.img_height * mjpeg.VerDcm *
902             mjpeg.field_per_buff;
903     }
904
905     /* fix width/height */
906     if( !p_sys->b_mjpeg && ( p_sys->i_width == 0 || p_sys->i_height == 0 ) )
907     {
908         struct video_window vid_win;
909
910         if( ioctl( i_fd, VIDIOCGWIN, &vid_win ) < 0 )
911         {
912             msg_Err( p_demux, "cannot get win (%m)" );
913             goto vdev_failed;
914         }
915         p_sys->i_width  = vid_win.width;
916         p_sys->i_height = vid_win.height;
917
918         if( !p_sys->i_width || !p_sys->i_height )
919         {
920             p_sys->i_width = p_sys->vid_cap.maxwidth;
921             p_sys->i_height = p_sys->vid_cap.maxheight;
922         }
923
924         if( !p_sys->i_width || !p_sys->i_height )
925         {
926             msg_Err( p_demux, "invalid video size (%ix%i)",
927                      p_sys->i_width, p_sys->i_height );
928             goto vdev_failed;
929         }
930
931         msg_Dbg( p_demux, "will use %dx%d", p_sys->i_width, p_sys->i_height );
932     }
933
934     if( !p_sys->b_mjpeg )
935     {
936         /* set hue/color/.. */
937         if( ioctl( i_fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 )
938         {
939             struct video_picture vid_picture = p_sys->vid_picture;
940
941             if( p_sys->i_brightness >= 0 && p_sys->i_brightness < 65536 )
942             {
943                 vid_picture.brightness = p_sys->i_brightness;
944             }
945             if( p_sys->i_colour >= 0 && p_sys->i_colour < 65536 )
946             {
947                 vid_picture.colour = p_sys->i_colour;
948             }
949             if( p_sys->i_hue >= 0 && p_sys->i_hue < 65536 )
950             {
951                 vid_picture.hue = p_sys->i_hue;
952             }
953             if( p_sys->i_contrast  >= 0 && p_sys->i_contrast < 65536 )
954             {
955                 vid_picture.contrast = p_sys->i_contrast;
956             }
957             if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
958             {
959                 msg_Dbg( p_demux, "v4l device uses brightness: %d",
960                          vid_picture.brightness );
961                 msg_Dbg( p_demux, "v4l device uses colour: %d",
962                          vid_picture.colour );
963                 msg_Dbg( p_demux, "v4l device uses hue: %d", vid_picture.hue );
964                 msg_Dbg( p_demux, "v4l device uses contrast: %d",
965                          vid_picture.contrast );
966                 p_sys->vid_picture = vid_picture;
967             }
968         }
969
970         /* Find out video format used by device */
971         if( ioctl( i_fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 )
972         {
973             struct video_picture vid_picture = p_sys->vid_picture;
974             char *psz;
975             int i;
976
977             p_sys->i_fourcc = 0;
978
979             psz = var_CreateGetString( p_demux, "v4l-chroma" );
980             if( strlen( psz ) >= 4 )
981             {
982                 vid_picture.palette = 0;
983                 int i_chroma = VLC_FOURCC( psz[0], psz[1], psz[2], psz[3] );
984
985                 /* Find out v4l chroma code */
986                 for( i = 0; v4lchroma_to_fourcc[i].i_v4l != 0; i++ )
987                 {
988                     if( v4lchroma_to_fourcc[i].i_fourcc == i_chroma )
989                     {
990                         vid_picture.palette = v4lchroma_to_fourcc[i].i_v4l;
991                         break;
992                     }
993                 }
994             }
995             free( psz );
996
997             if( vid_picture.palette &&
998                 !ioctl( i_fd, VIDIOCSPICT, &vid_picture ) )
999             {
1000                 p_sys->vid_picture = vid_picture;
1001             }
1002             else
1003             {
1004                 /* Try to set the format to something easy to encode */
1005                 vid_picture.palette = VIDEO_PALETTE_YUV420P;
1006                 if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
1007                 {
1008                     p_sys->vid_picture = vid_picture;
1009                 }
1010                 else
1011                 {
1012                     vid_picture.palette = VIDEO_PALETTE_YUV422P;
1013                     if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
1014                     {
1015                         p_sys->vid_picture = vid_picture;
1016                     }
1017                 }
1018             }
1019
1020             /* Find out final format */
1021             for( i = 0; v4lchroma_to_fourcc[i].i_v4l != 0; i++ )
1022             {
1023                 if( v4lchroma_to_fourcc[i].i_v4l == p_sys->vid_picture.palette)
1024                 {
1025                     p_sys->i_fourcc = v4lchroma_to_fourcc[i].i_fourcc;
1026                     break;
1027                 }
1028             }
1029         }
1030         else
1031         {
1032             msg_Err( p_demux, "ioctl VIDIOCGPICT failed" );
1033             goto vdev_failed;
1034         }
1035     }
1036
1037     if( p_sys->b_mjpeg )
1038     {
1039         int i;
1040
1041         p_sys->mjpeg_buffers.count = 8;
1042         p_sys->mjpeg_buffers.size = MJPEG_BUFFER_SIZE;
1043
1044         if( ioctl( i_fd, MJPIOC_REQBUFS, &p_sys->mjpeg_buffers ) < 0 )
1045         {
1046             msg_Err( p_demux, "mmap unsupported" );
1047             goto vdev_failed;
1048         }
1049
1050         p_sys->p_video_mmap = mmap( 0,
1051                 p_sys->mjpeg_buffers.size * p_sys->mjpeg_buffers.count,
1052                 PROT_READ | PROT_WRITE, MAP_SHARED, i_fd, 0 );
1053         if( p_sys->p_video_mmap == MAP_FAILED )
1054         {
1055             msg_Err( p_demux, "mmap failed" );
1056             goto vdev_failed;
1057         }
1058
1059         p_sys->i_fourcc  = VLC_FOURCC( 'm','j','p','g' );
1060         p_sys->i_frame_pos = -1;
1061
1062         /* queue up all the frames */
1063         for( i = 0; i < (int)p_sys->mjpeg_buffers.count; i++ )
1064         {
1065             if( ioctl( i_fd, MJPIOC_QBUF_CAPT, &i ) < 0 )
1066             {
1067                 msg_Err( p_demux, "unable to queue frame" );
1068                 goto vdev_failed;
1069             }
1070         }
1071     }
1072     else
1073     {
1074         /* Fill in picture_t fields */
1075         vout_InitPicture( VLC_OBJECT(p_demux), &p_sys->pic, p_sys->i_fourcc,
1076                           p_sys->i_width, p_sys->i_height, p_sys->i_width *
1077                           VOUT_ASPECT_FACTOR / p_sys->i_height );
1078         if( !p_sys->pic.i_planes )
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 }