]> git.sesse.net Git - vlc/blob - modules/access/v4l.c
Merge branch 1.0-bugfix
[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_SET_PAUSE_STATE:
426         case DEMUX_CAN_CONTROL_PACE:
427             pb = (bool*)va_arg( args, bool * );
428             *pb = false;
429             return VLC_SUCCESS;
430
431         case DEMUX_GET_PTS_DELAY:
432             pi64 = (int64_t*)va_arg( args, int64_t * );
433             *pi64 = (int64_t)var_GetInteger( p_demux, "v4l-caching" ) * 1000;
434             return VLC_SUCCESS;
435
436         case DEMUX_GET_TIME:
437             pi64 = (int64_t*)va_arg( args, int64_t * );
438             *pi64 = mdate();
439             return VLC_SUCCESS;
440
441         /* TODO implement others */
442         default:
443             return VLC_EGENERIC;
444     }
445
446     return VLC_EGENERIC;
447 }
448
449 /*****************************************************************************
450  * Demux:
451  *****************************************************************************/
452 static int Demux( demux_t *p_demux )
453 {
454     demux_sys_t *p_sys = p_demux->p_sys;
455
456     block_t *p_block = GrabVideo( p_demux );
457
458     if( !p_block )
459     {
460         msleep( 10000 ); /* Unfortunately v4l doesn't allow polling */
461         return 1;
462     }
463
464     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
465     es_out_Send( p_demux->out, p_sys->p_es, p_block );
466
467     return 1;
468 }
469
470 /*****************************************************************************
471  * ParseMRL: parse the options contained in the MRL
472  *****************************************************************************/
473 static void ParseMRL( demux_t *p_demux )
474 {
475     demux_sys_t *p_sys = p_demux->p_sys;
476
477     char *psz_dup = strdup( p_demux->psz_path );
478     char *psz_parser = psz_dup;
479
480     while( *psz_parser && *psz_parser != ':' )
481     {
482         psz_parser++;
483     }
484
485     if( *psz_parser == ':' )
486     {
487         /* read options */
488         for( ;; )
489         {
490             *psz_parser++ = '\0';
491             if( !strncmp( psz_parser, "channel=", strlen( "channel=" ) ) )
492             {
493                 p_sys->i_channel = strtol( psz_parser + strlen( "channel=" ),
494                                            &psz_parser, 0 );
495             }
496             else if( !strncmp( psz_parser, "norm=", strlen( "norm=" ) ) )
497             {
498                 psz_parser += strlen( "norm=" );
499                 if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
500                 {
501                     p_sys->i_norm = VIDEO_MODE_PAL;
502                     psz_parser += strlen( "pal" );
503                 }
504                 else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
505                 {
506                     p_sys->i_norm = VIDEO_MODE_NTSC;
507                     psz_parser += strlen( "ntsc" );
508                 }
509                 else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
510                 {
511                     p_sys->i_norm = VIDEO_MODE_SECAM;
512                     psz_parser += strlen( "secam" );
513                 }
514                 else if( !strncmp( psz_parser, "auto", strlen( "auto" ) ) )
515                 {
516                     p_sys->i_norm = VIDEO_MODE_AUTO;
517                     psz_parser += strlen( "auto" );
518                 }
519                 else
520                 {
521                     p_sys->i_norm = strtol( psz_parser, &psz_parser, 0 );
522                 }
523             }
524             else if( !strncmp( psz_parser, "frequency=",
525                                strlen( "frequency=" ) ) )
526             {
527                 p_sys->i_frequency =
528                     strtol( psz_parser + strlen( "frequency=" ),
529                             &psz_parser, 0 );
530                 if( p_sys->i_frequency < 30000 )
531                 {
532                     msg_Warn( p_demux, "v4l syntax has changed : "
533                               "'frequency' is now channel frequency in kHz");
534                 }
535             }
536             else if( !strncmp( psz_parser, "audio=", strlen( "audio=" ) ) )
537             {
538                 p_sys->i_audio = strtol( psz_parser + strlen( "audio=" ),
539                                          &psz_parser, 0 );
540             }
541             else if( !strncmp( psz_parser, "size=", strlen( "size=" ) ) )
542             {
543                 psz_parser += strlen( "size=" );
544                 if( !strncmp( psz_parser, "subqcif", strlen( "subqcif" ) ) )
545                 {
546                     p_sys->i_width  = 128;
547                     p_sys->i_height = 96;
548                 }
549                 else if( !strncmp( psz_parser, "qsif", strlen( "qsif" ) ) )
550                 {
551                     p_sys->i_width  = 160;
552                     p_sys->i_height = 120;
553                 }
554                 else if( !strncmp( psz_parser, "qcif", strlen( "qcif" ) ) )
555                 {
556                     p_sys->i_width  = 176;
557                     p_sys->i_height = 144;
558                 }
559                 else if( !strncmp( psz_parser, "sif", strlen( "sif" ) ) )
560                 {
561                     p_sys->i_width  = 320;
562                     p_sys->i_height = 244;
563                 }
564                 else if( !strncmp( psz_parser, "cif", strlen( "cif" ) ) )
565                 {
566                     p_sys->i_width  = 352;
567                     p_sys->i_height = 288;
568                 }
569                 else if( !strncmp( psz_parser, "vga", strlen( "vga" ) ) )
570                 {
571                     p_sys->i_width  = 640;
572                     p_sys->i_height = 480;
573                 }
574                 else
575                 {
576                     /* widthxheight */
577                     p_sys->i_width = strtol( psz_parser, &psz_parser, 0 );
578                     if( *psz_parser == 'x' || *psz_parser == 'X')
579                     {
580                         p_sys->i_height = strtol( psz_parser + 1,
581                                                   &psz_parser, 0 );
582                     }
583                     msg_Dbg( p_demux, "WxH %dx%d", p_sys->i_width,
584                              p_sys->i_height );
585                 }
586             }
587             else if( !strncmp( psz_parser, "brightness=", strlen( "brightness=" ) ) )
588             {
589                 p_sys->i_brightness = strtol( psz_parser + strlen( "brightness=" ),
590                                               &psz_parser, 0 );
591             }
592             else if( !strncmp( psz_parser, "colour=", strlen( "colour=" ) ) )
593             {
594                 p_sys->i_colour = strtol( psz_parser + strlen( "colour=" ),
595                                           &psz_parser, 0 );
596             }
597             else if( !strncmp( psz_parser, "hue=", strlen( "hue=" ) ) )
598             {
599                 p_sys->i_hue = strtol( psz_parser + strlen( "hue=" ),
600                                        &psz_parser, 0 );
601             }
602             else if( !strncmp( psz_parser, "contrast=", strlen( "contrast=" ) ) )
603             {
604                 p_sys->i_contrast = strtol( psz_parser + strlen( "contrast=" ),
605                                             &psz_parser, 0 );
606             }
607             else if( !strncmp( psz_parser, "tuner=", strlen( "tuner=" ) ) )
608             {
609                 p_sys->i_tuner = strtol( psz_parser + strlen( "tuner=" ),
610                                          &psz_parser, 0 );
611             }
612             else if( !strncmp( psz_parser, "mjpeg", strlen( "mjpeg" ) ) )
613             {
614                 psz_parser += strlen( "mjpeg" );
615
616                 p_sys->b_mjpeg = true;
617             }
618             else if( !strncmp( psz_parser, "decimation=",
619                         strlen( "decimation=" ) ) )
620             {
621                 p_sys->i_decimation =
622                     strtol( psz_parser + strlen( "decimation=" ),
623                             &psz_parser, 0 );
624             }
625             else if( !strncmp( psz_parser, "quality=",
626                         strlen( "quality=" ) ) )
627             {
628                 p_sys->i_quality =
629                     strtol( psz_parser + strlen( "quality=" ),
630                             &psz_parser, 0 );
631             }
632             else if( !strncmp( psz_parser, "fps=", strlen( "fps=" ) ) )
633             {
634                 p_sys->f_fps = strtof( psz_parser + strlen( "fps=" ),
635                                        &psz_parser );
636             }
637             else if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) )
638              || !strncmp( psz_parser, "samplerate=", strlen( "samplerate=" ) )
639              || !strncmp( psz_parser, "stereo", strlen( "stereo" ) )
640              || !strncmp( psz_parser, "mono", strlen( "mono" ) ) )
641             {
642                 if( strchr( psz_parser, ':' ) )
643                     psz_parser = strchr( psz_parser, ':' );
644                 else
645                     psz_parser += strlen( psz_parser );
646                 msg_Err( p_demux, AUDIO_DEPRECATED_ERROR );
647             }
648             else
649             {
650                 msg_Warn( p_demux, "unknown option" );
651             }
652
653             while( *psz_parser && *psz_parser != ':' )
654             {
655                 psz_parser++;
656             }
657
658             if( *psz_parser == '\0' )
659             {
660                 break;
661             }
662         }
663     }
664
665     if( *psz_dup )
666         p_sys->psz_device = strdup( psz_dup );
667     else
668         p_sys->psz_device = strdup( V4L_DEFAULT );
669     free( psz_dup );
670 }
671
672 /*****************************************************************************
673  * OpenVideoDev:
674  *****************************************************************************/
675 static int OpenVideoDev( demux_t *p_demux, char *psz_device )
676 {
677     demux_sys_t *p_sys = p_demux->p_sys;
678     int i_fd;
679
680     struct video_channel vid_channel;
681     struct mjpeg_params mjpeg;
682     int i;
683
684     if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
685     {
686         msg_Err( p_demux, "cannot open device (%m)" );
687         goto vdev_failed;
688     }
689
690     if( ioctl( i_fd, VIDIOCGCAP, &p_sys->vid_cap ) < 0 )
691     {
692         msg_Err( p_demux, "cannot get capabilities (%m)" );
693         goto vdev_failed;
694     }
695
696     msg_Dbg( p_demux,
697              "V4L device %s %d channels %d audios %d < w < %d %d < h < %d",
698              p_sys->vid_cap.name,
699              p_sys->vid_cap.channels,
700              p_sys->vid_cap.audios,
701              p_sys->vid_cap.minwidth,  p_sys->vid_cap.maxwidth,
702              p_sys->vid_cap.minheight, p_sys->vid_cap.maxheight );
703
704     if( p_sys->i_channel < 0 || p_sys->i_channel >= p_sys->vid_cap.channels )
705     {
706         msg_Dbg( p_demux, "invalid channel, falling back on channel 0" );
707         p_sys->i_channel = 0;
708     }
709     if( p_sys->vid_cap.audios && p_sys->i_audio >= p_sys->vid_cap.audios )
710     {
711         msg_Dbg( p_demux, "invalid audio, falling back with no audio" );
712         p_sys->i_audio = -1;
713     }
714
715     if( p_sys->i_width < p_sys->vid_cap.minwidth ||
716         p_sys->i_width > p_sys->vid_cap.maxwidth )
717     {
718         msg_Dbg( p_demux, "invalid width %i", p_sys->i_width );
719         p_sys->i_width = 0;
720     }
721     if( p_sys->i_height < p_sys->vid_cap.minheight ||
722         p_sys->i_height > p_sys->vid_cap.maxheight )
723     {
724         msg_Dbg( p_demux, "invalid height %i", p_sys->i_height );
725         p_sys->i_height = 0;
726     }
727
728     if( !( p_sys->vid_cap.type & VID_TYPE_CAPTURE ) )
729     {
730         msg_Err( p_demux, "cannot grab" );
731         goto vdev_failed;
732     }
733
734     vid_channel.channel = p_sys->i_channel;
735     if( ioctl( i_fd, VIDIOCGCHAN, &vid_channel ) < 0 )
736     {
737         msg_Err( p_demux, "cannot get channel infos (%m)" );
738         goto vdev_failed;
739     }
740     msg_Dbg( p_demux,
741              "setting channel %s(%d) %d tuners flags=0x%x type=0x%x norm=0x%x",
742              vid_channel.name, vid_channel.channel, vid_channel.tuners,
743              vid_channel.flags, vid_channel.type, vid_channel.norm );
744
745     if( p_sys->i_tuner >= vid_channel.tuners )
746     {
747         msg_Dbg( p_demux, "invalid tuner, falling back on tuner 0" );
748         p_sys->i_tuner = 0;
749     }
750
751     vid_channel.norm = p_sys->i_norm;
752     if( ioctl( i_fd, VIDIOCSCHAN, &vid_channel ) < 0 )
753     {
754         msg_Err( p_demux, "cannot set channel (%m)" );
755         goto vdev_failed;
756     }
757
758     if( vid_channel.flags & VIDEO_VC_TUNER )
759     {
760
761         /* set tuner */
762 #if 0
763         struct video_tuner vid_tuner;
764         if( p_sys->i_tuner >= 0 )
765         {
766             vid_tuner.tuner = p_sys->i_tuner;
767             if( ioctl( i_fd, VIDIOCGTUNER, &vid_tuner ) < 0 )
768             {
769                 msg_Err( p_demux, "cannot get tuner (%m)" );
770                 goto vdev_failed;
771             }
772             msg_Dbg( p_demux, "tuner %s low=%d high=%d, flags=0x%x "
773                      "mode=0x%x signal=0x%x",
774                      vid_tuner.name, vid_tuner.rangelow, vid_tuner.rangehigh,
775                      vid_tuner.flags, vid_tuner.mode, vid_tuner.signal );
776
777             msg_Dbg( p_demux, "setting tuner %s (%d)",
778                      vid_tuner.name, vid_tuner.tuner );
779
780             /* FIXME FIXME to be checked FIXME FIXME */
781             //vid_tuner.mode = p_sys->i_norm;
782             if( ioctl( i_fd, VIDIOCSTUNER, &vid_tuner ) < 0 )
783             {
784                 msg_Err( p_demux, "cannot set tuner (%m)" );
785                 goto vdev_failed;
786             }
787         }
788 #endif
789
790         /* Show a warning if frequency is < than 30000.
791          * User is certainly usint old syntax. */
792
793
794         /* set frequency */
795         if( p_sys->i_frequency >= 0 )
796         {
797             int driver_frequency = p_sys->i_frequency * 16 /1000;
798             if( ioctl( i_fd, VIDIOCSFREQ, &driver_frequency ) < 0 )
799             {
800                 msg_Err( p_demux, "cannot set frequency (%m)" );
801                 goto vdev_failed;
802             }
803             msg_Dbg( p_demux, "frequency %d (%d)", p_sys->i_frequency,
804                                                    driver_frequency );
805         }
806     }
807
808     /* set audio */
809     if( vid_channel.flags & VIDEO_VC_AUDIO )
810     {
811         struct video_audio      vid_audio;
812
813         /* XXX TODO volume, balance, ... */
814         if( p_sys->i_audio >= 0 )
815         {
816             vid_audio.audio = p_sys->i_audio;
817             if( ioctl( i_fd, VIDIOCGAUDIO, &vid_audio ) < 0 )
818             {
819                 msg_Err( p_demux, "cannot get audio (%m)" );
820                 goto vdev_failed;
821             }
822
823             /* unmute audio */
824             vid_audio.flags &= ~VIDEO_AUDIO_MUTE;
825
826             if( ioctl( i_fd, VIDIOCSAUDIO, &vid_audio ) < 0 )
827             {
828                 msg_Err( p_demux, "cannot set audio (%m)" );
829                 goto vdev_failed;
830             }
831         }
832
833     }
834
835     /* establish basic params with input and norm before feeling width
836      * or height */
837     if( p_sys->b_mjpeg )
838     {
839         struct quicktime_mjpeg_app1 *p_app1;
840         int32_t i_offset;
841
842         if( ioctl( i_fd, MJPIOC_G_PARAMS, &mjpeg ) < 0 )
843         {
844             msg_Err( p_demux, "cannot get mjpeg params (%m)" );
845             goto vdev_failed;
846         }
847         mjpeg.input = p_sys->i_channel;
848         mjpeg.norm  = p_sys->i_norm;
849         mjpeg.decimation = p_sys->i_decimation;
850
851         if( p_sys->i_width )
852             mjpeg.img_width = p_sys->i_width / p_sys->i_decimation;
853         if( p_sys->i_height )
854             mjpeg.img_height = p_sys->i_height / p_sys->i_decimation;
855
856         /* establish Quicktime APP1 marker while we are here */
857         mjpeg.APPn = 1;
858         mjpeg.APP_len = 40;
859
860         /* aligned */
861         p_app1 = (struct quicktime_mjpeg_app1 *)mjpeg.APP_data;
862         p_app1->i_reserved = 0;
863         p_app1->i_tag = VLC_FOURCC( 'm','j','p','g' );
864         p_app1->i_field_size = 0;
865         p_app1->i_padded_field_size = 0;
866         p_app1->i_next_field = 0;
867         /* XXX WARNING XXX */
868         /* these's nothing magic about these values.  We are dangerously
869          * assuming the encoder card is encoding mjpeg-a and is not throwing
870          * in marker tags we aren't expecting.  It's bad enough we have to
871          * search through the jpeg output for every frame we grab just to
872          * find the first field's end marker, so we take this risk to boost
873          * performance.
874          * This is really something the driver could do for us because this
875          * does conform to standards outside of Apple Quicktime.
876          */
877         i_offset = 0x2e;
878         p_app1->i_DQT_offset = hton32( i_offset );
879         i_offset = 0xb4;
880         p_app1->i_DHT_offset = hton32( i_offset );
881         i_offset = 0x258;
882         p_app1->i_SOF_offset = hton32( i_offset );
883         i_offset = 0x26b;
884         p_app1->i_SOS_offset = hton32( i_offset );
885         i_offset = 0x279;
886         p_app1->i_data_offset = hton32( i_offset );
887
888         /* SOF and SOS aren't specified by the mjpeg API because they aren't
889          * optional.  They will be present in the output. */
890         mjpeg.jpeg_markers = JPEG_MARKER_DHT | JPEG_MARKER_DQT;
891
892         if( ioctl( i_fd, MJPIOC_S_PARAMS, &mjpeg ) < 0 )
893         {
894             msg_Err( p_demux, "cannot set mjpeg params (%m)" );
895             goto vdev_failed;
896         }
897
898         p_sys->i_width = mjpeg.img_width * mjpeg.HorDcm;
899         p_sys->i_height = mjpeg.img_height * mjpeg.VerDcm *
900             mjpeg.field_per_buff;
901     }
902
903     /* fix width/height */
904     if( !p_sys->b_mjpeg && ( p_sys->i_width == 0 || p_sys->i_height == 0 ) )
905     {
906         struct video_window vid_win;
907
908         if( ioctl( i_fd, VIDIOCGWIN, &vid_win ) < 0 )
909         {
910             msg_Err( p_demux, "cannot get win (%m)" );
911             goto vdev_failed;
912         }
913         p_sys->i_width  = vid_win.width;
914         p_sys->i_height = vid_win.height;
915
916         if( !p_sys->i_width || !p_sys->i_height )
917         {
918             p_sys->i_width = p_sys->vid_cap.maxwidth;
919             p_sys->i_height = p_sys->vid_cap.maxheight;
920         }
921
922         if( !p_sys->i_width || !p_sys->i_height )
923         {
924             msg_Err( p_demux, "invalid video size (%ix%i)",
925                      p_sys->i_width, p_sys->i_height );
926             goto vdev_failed;
927         }
928
929         msg_Dbg( p_demux, "will use %dx%d", p_sys->i_width, p_sys->i_height );
930     }
931
932     if( !p_sys->b_mjpeg )
933     {
934         /* set hue/color/.. */
935         if( ioctl( i_fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 )
936         {
937             struct video_picture vid_picture = p_sys->vid_picture;
938
939             if( p_sys->i_brightness >= 0 && p_sys->i_brightness < 65536 )
940             {
941                 vid_picture.brightness = p_sys->i_brightness;
942             }
943             if( p_sys->i_colour >= 0 && p_sys->i_colour < 65536 )
944             {
945                 vid_picture.colour = p_sys->i_colour;
946             }
947             if( p_sys->i_hue >= 0 && p_sys->i_hue < 65536 )
948             {
949                 vid_picture.hue = p_sys->i_hue;
950             }
951             if( p_sys->i_contrast  >= 0 && p_sys->i_contrast < 65536 )
952             {
953                 vid_picture.contrast = p_sys->i_contrast;
954             }
955             if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
956             {
957                 msg_Dbg( p_demux, "v4l device uses brightness: %d",
958                          vid_picture.brightness );
959                 msg_Dbg( p_demux, "v4l device uses colour: %d",
960                          vid_picture.colour );
961                 msg_Dbg( p_demux, "v4l device uses hue: %d", vid_picture.hue );
962                 msg_Dbg( p_demux, "v4l device uses contrast: %d",
963                          vid_picture.contrast );
964                 p_sys->vid_picture = vid_picture;
965             }
966         }
967
968         /* Find out video format used by device */
969         if( ioctl( i_fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 )
970         {
971             struct video_picture vid_picture = p_sys->vid_picture;
972             char *psz;
973             int i;
974
975             p_sys->i_fourcc = 0;
976
977             psz = var_CreateGetString( p_demux, "v4l-chroma" );
978
979             const vlc_fourcc_t i_chroma =
980                 vlc_fourcc_GetCodecFromString( VIDEO_ES, psz );
981             if( i_chroma )
982             {
983                 vid_picture.palette = 0;
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_CODEC_MJPG;
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         if( picture_Setup( &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         {
1079             msg_Err( p_demux, "unsupported chroma" );
1080             goto vdev_failed;
1081         }
1082         p_sys->i_video_frame_size = 0;
1083         for( i = 0; i < p_sys->pic.i_planes; i++ )
1084         {
1085             p_sys->i_video_frame_size += p_sys->pic.p[i].i_visible_lines *
1086               p_sys->pic.p[i].i_visible_pitch;
1087         }
1088
1089         msg_Dbg( p_demux, "v4l device uses frame size: %i",
1090                  p_sys->i_video_frame_size );
1091         msg_Dbg( p_demux, "v4l device uses chroma: %4.4s",
1092                 (char*)&p_sys->i_fourcc );
1093
1094         /* Allocate mmap buffer */
1095         if( ioctl( i_fd, VIDIOCGMBUF, &p_sys->vid_mbuf ) < 0 )
1096         {
1097             msg_Err( p_demux, "mmap unsupported" );
1098             goto vdev_failed;
1099         }
1100
1101         p_sys->p_video_mmap = mmap( 0, p_sys->vid_mbuf.size,
1102                                     PROT_READ|PROT_WRITE, MAP_SHARED,
1103                                     i_fd, 0 );
1104         if( p_sys->p_video_mmap == MAP_FAILED )
1105         {
1106             /* FIXME -> normal read */
1107             msg_Err( p_demux, "mmap failed" );
1108             goto vdev_failed;
1109         }
1110
1111         /* init grabbing */
1112         p_sys->vid_mmap.frame  = 0;
1113         p_sys->vid_mmap.width  = p_sys->i_width;
1114         p_sys->vid_mmap.height = p_sys->i_height;
1115         p_sys->vid_mmap.format = p_sys->vid_picture.palette;
1116         if( ioctl( i_fd, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 )
1117         {
1118             msg_Warn( p_demux, "%4.4s refused", (char*)&p_sys->i_fourcc );
1119             msg_Err( p_demux, "chroma selection failed" );
1120             goto vdev_failed;
1121         }
1122     }
1123     return i_fd;
1124
1125 vdev_failed:
1126
1127     if( i_fd >= 0 ) close( i_fd );
1128     return -1;
1129 }
1130
1131 /*****************************************************************************
1132  * GrabVideo:
1133  *****************************************************************************/
1134 static uint8_t *GrabCapture( demux_t *p_demux )
1135 {
1136     demux_sys_t *p_sys = p_demux->p_sys;
1137     int i_captured_frame = p_sys->i_frame_pos;
1138
1139     p_sys->vid_mmap.frame = (p_sys->i_frame_pos + 1) % p_sys->vid_mbuf.frames;
1140
1141     while( ioctl( p_sys->i_fd, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 )
1142     {
1143         if( errno != EAGAIN )
1144         {
1145             msg_Err( p_demux, "failed capturing new frame" );
1146             return NULL;
1147         }
1148
1149         if( !vlc_object_alive (p_demux) )
1150         {
1151             return NULL;
1152         }
1153
1154         msg_Dbg( p_demux, "grab failed, trying again" );
1155     }
1156
1157     while( ioctl(p_sys->i_fd, VIDIOCSYNC, &p_sys->i_frame_pos) < 0 )
1158     {
1159         if( errno != EAGAIN && errno != EINTR )
1160         {
1161             msg_Err( p_demux, "failed syncing new frame" );
1162             return NULL;
1163         }
1164     }
1165
1166     p_sys->i_frame_pos = p_sys->vid_mmap.frame;
1167     /* leave i_video_frame_size alone */
1168     return p_sys->p_video_mmap + p_sys->vid_mbuf.offsets[i_captured_frame];
1169 }
1170
1171 static uint8_t *GrabMJPEG( demux_t *p_demux )
1172 {
1173     demux_sys_t *p_sys = p_demux->p_sys;
1174     struct mjpeg_sync sync;
1175     uint8_t *p_frame, *p_field, *p;
1176     uint16_t tag;
1177     uint32_t i_size;
1178     struct quicktime_mjpeg_app1 *p_app1 = NULL;
1179
1180     /* re-queue the last frame we sync'd */
1181     if( p_sys->i_frame_pos != -1 )
1182     {
1183         while( ioctl( p_sys->i_fd, MJPIOC_QBUF_CAPT,
1184                                        &p_sys->i_frame_pos ) < 0 )
1185         {
1186             if( errno != EAGAIN && errno != EINTR )
1187             {
1188                 msg_Err( p_demux, "failed capturing new frame" );
1189                 return NULL;
1190             }
1191         }
1192     }
1193
1194     /* sync on the next frame */
1195     while( ioctl( p_sys->i_fd, MJPIOC_SYNC, &sync ) < 0 )
1196     {
1197         if( errno != EAGAIN && errno != EINTR )
1198         {
1199             msg_Err( p_demux, "failed syncing new frame" );
1200             return NULL;
1201         }
1202     }
1203
1204     p_sys->i_frame_pos = sync.frame;
1205     p_frame = p_sys->p_video_mmap + p_sys->mjpeg_buffers.size * sync.frame;
1206
1207     /* p_frame now points to the data.  fix up the Quicktime APP1 marker */
1208     tag = 0xffd9;
1209     tag = hton16( tag );
1210     p_field = p_frame;
1211
1212     /* look for EOI */
1213     p = memmem( p_field, sync.length, &tag, 2 );
1214
1215     if( p )
1216     {
1217         p += 2; /* data immediately following EOI */
1218         /* UNALIGNED! */
1219         p_app1 = (struct quicktime_mjpeg_app1 *)(p_field + 6);
1220
1221         i_size = ((uint32_t)(p - p_field));
1222         i_size = hton32( i_size );
1223         memcpy( &p_app1->i_field_size, &i_size, 4 );
1224
1225         while( *p == 0xff && *(p+1) == 0xff )
1226             p++;
1227
1228         i_size = ((uint32_t)(p - p_field));
1229         i_size = hton32( i_size );
1230         memcpy( &p_app1->i_padded_field_size, &i_size, 4 );
1231     }
1232
1233     tag = 0xffd8;
1234     tag = hton16( tag );
1235     p_field = memmem( p, sync.length - (size_t)(p - p_frame), &tag, 2 );
1236
1237     if( p_field )
1238     {
1239         i_size = (uint32_t)(p_field - p_frame);
1240         i_size = hton32( i_size );
1241         memcpy( &p_app1->i_next_field, &i_size, 4 );
1242
1243         /* UNALIGNED! */
1244         p_app1 = (struct quicktime_mjpeg_app1 *)(p_field + 6);
1245         tag = 0xffd9;
1246         tag = hton16( tag );
1247         p = memmem( p_field, sync.length - (size_t)(p_field - p_frame),
1248                 &tag, 2 );
1249
1250         if( !p )
1251         {
1252             /* sometimes the second field doesn't have the EOI.  just put it
1253              * there
1254              */
1255             p = p_frame + sync.length;
1256             memcpy( p, &tag, 2 );
1257             sync.length += 2;
1258         }
1259
1260         p += 2;
1261         i_size = (uint32_t)(p - p_field);
1262         i_size = hton32( i_size );
1263         memcpy( &p_app1->i_field_size, &i_size, 4 );
1264         i_size = (uint32_t)(sync.length - (uint32_t)(p_field - p_frame));
1265         i_size = hton32( i_size );
1266         memcpy( &p_app1->i_padded_field_size, &i_size, 4 );
1267     }
1268
1269     p_sys->i_video_frame_size = sync.length;
1270     return p_frame;
1271 }
1272
1273 static block_t *GrabVideo( demux_t *p_demux )
1274 {
1275     demux_sys_t *p_sys = p_demux->p_sys;
1276     uint8_t     *p_frame;
1277     block_t     *p_block;
1278
1279     if( p_sys->f_fps >= 0.1 && p_sys->i_video_pts > 0 )
1280     {
1281         mtime_t i_dur = (mtime_t)((double)1000000 / (double)p_sys->f_fps);
1282
1283         /* Did we wait long enough ? (frame rate reduction) */
1284         if( p_sys->i_video_pts + i_dur > mdate() ) return 0;
1285     }
1286
1287     if( p_sys->b_mjpeg ) p_frame = GrabMJPEG( p_demux );
1288     else p_frame = GrabCapture( p_demux );
1289
1290     if( !p_frame ) return 0;
1291
1292     if( !( p_block = block_New( p_demux, p_sys->i_video_frame_size ) ) )
1293     {
1294         msg_Warn( p_demux, "cannot get block" );
1295         return 0;
1296     }
1297
1298     memcpy( p_block->p_buffer, p_frame, p_sys->i_video_frame_size );
1299     p_sys->i_video_pts = p_block->i_pts = p_block->i_dts = mdate();
1300
1301     return p_block;
1302 }