1 /*****************************************************************************
2 * v4l.c : Video4Linux input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2004 the VideoLAN team
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>
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.
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.
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 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
32 #include <vlc_input.h>
33 #include <vlc_demux.h>
34 #include <vlc_access.h>
36 #include <vlc_codecs.h>
38 #include <sys/types.h>
40 #include <sys/ioctl.h>
46 /* From GStreamer's v4l plugin:
47 * Because of some really cool feature in video4linux1, also known as
48 * 'not including sys/types.h and sys/time.h', we had to include it
49 * ourselves. In all their intelligence, these people decided to fix
50 * this in the next version (video4linux2) in such a cool way that it
51 * breaks all compilations of old stuff...
52 * The real problem is actually that linux/time.h doesn't use proper
53 * macro checks before defining types like struct timeval. The proper
54 * fix here is to either fuck the kernel header (which is what we do
55 * by defining _LINUX_TIME_H, an innocent little hack) or by fixing it
56 * upstream, which I'll consider doing later on. If you get compiler
57 * errors here, check your linux/time.h && sys/time.h header setup.
61 #include <linux/videodev.h>
62 #include "videodev_mjpeg.h"
64 #include <sys/soundcard.h>
66 /*****************************************************************************
68 *****************************************************************************/
69 static int Open ( vlc_object_t * );
70 static void Close( vlc_object_t * );
72 #define CACHING_TEXT N_("Caching value in ms")
73 #define CACHING_LONGTEXT N_( \
74 "Caching value for V4L captures. This " \
75 "value should be set in milliseconds." )
76 #define VDEV_TEXT N_("Video device name")
77 #define VDEV_LONGTEXT N_( \
78 "Name of the video device to use. " \
79 "If you don't specify anything, no video device will be used.")
80 #define ADEV_TEXT N_("Audio device name")
81 #define ADEV_LONGTEXT N_( \
82 "Name of the audio device to use. " \
83 "If you don't specify anything, no audio device will be used.")
84 #define CHROMA_TEXT N_("Video input chroma format")
85 #define CHROMA_LONGTEXT N_( \
86 "Force the Video4Linux video device to use a specific chroma format " \
87 "(eg. I420 (default), RV24, etc.)")
88 #define FREQUENCY_TEXT N_( "Frequency" )
89 #define FREQUENCY_LONGTEXT N_( \
90 "Frequency to capture (in kHz), if applicable." )
91 #define CHANNEL_TEXT N_( "Channel" )
92 #define CHANNEL_LONGTEXT N_( \
93 "Channel of the card to use (Usually, 0 = tuner, " \
94 "1 = composite, 2 = svideo)." )
95 #define NORM_TEXT N_( "Norm" )
96 #define NORM_LONGTEXT N_( \
97 "Norm of the stream (Automatic, SECAM, PAL, or NTSC)." )
98 #define AUDIO_TEXT N_( "Audio Channel" )
99 #define AUDIO_LONGTEXT N_( \
100 "Audio Channel to use, if there are several audio inputs." )
101 #define WIDTH_TEXT N_( "Width" )
102 #define WIDTH_LONGTEXT N_( "Width of the stream to capture " \
103 "(-1 for autodetect)." )
104 #define HEIGHT_TEXT N_( "Height" )
105 #define HEIGHT_LONGTEXT N_( "Height of the stream to capture " \
106 "(-1 for autodetect)." )
107 #define BRIGHTNESS_TEXT N_( "Brightness" )
108 #define BRIGHTNESS_LONGTEXT N_( \
109 "Brightness of the video input." )
110 #define HUE_TEXT N_( "Hue" )
111 #define HUE_LONGTEXT N_( \
112 "Hue of the video input." )
113 #define COLOUR_TEXT N_( "Color" )
114 #define COLOUR_LONGTEXT N_( \
115 "Color of the video input." )
116 #define CONTRAST_TEXT N_( "Contrast" )
117 #define CONTRAST_LONGTEXT N_( \
118 "Contrast of the video input." )
119 #define TUNER_TEXT N_( "Tuner" )
120 #define TUNER_LONGTEXT N_( "Tuner to use, if there are several ones." )
121 #define SAMPLERATE_TEXT N_( "Samplerate" )
122 #define SAMPLERATE_LONGTEXT N_( \
123 "Samplerate of the captured audio stream, in Hz (eg: 11025, 22050, 44100)" )
124 #define STEREO_TEXT N_( "Stereo" )
125 #define STEREO_LONGTEXT N_( \
126 "Capture the audio stream in stereo." )
127 #define MJPEG_TEXT N_( "MJPEG" )
128 #define MJPEG_LONGTEXT N_( \
129 "Set this option if the capture device outputs MJPEG" )
130 #define DECIMATION_TEXT N_( "Decimation" )
131 #define DECIMATION_LONGTEXT N_( \
132 "Decimation level for MJPEG streams" )
133 #define QUALITY_TEXT N_( "Quality" )
134 #define QUALITY_LONGTEXT N_( "Quality of the stream." )
135 #define FPS_TEXT N_( "Framerate" )
136 #define FPS_LONGTEXT N_( "Framerate to capture, if applicable " \
137 "(-1 for autodetect)." )
139 static int i_norm_list[] =
140 { VIDEO_MODE_AUTO, VIDEO_MODE_SECAM, VIDEO_MODE_PAL, VIDEO_MODE_NTSC };
141 static const char *psz_norm_list_text[] =
142 { N_("Automatic"), N_("SECAM"), N_("PAL"), N_("NTSC") };
145 set_shortname( _("Video4Linux") );
146 set_description( _("Video4Linux input") );
147 set_category( CAT_INPUT );
148 set_subcategory( SUBCAT_INPUT_ACCESS );
150 add_integer( "v4l-caching", DEFAULT_PTS_DELAY / 1000, NULL,
151 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
152 add_string( "v4l-vdev", "/dev/video", 0, VDEV_TEXT, VDEV_LONGTEXT,
154 add_string( "v4l-adev", "/dev/dsp", 0, ADEV_TEXT, ADEV_LONGTEXT,
156 add_string( "v4l-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
158 add_float( "v4l-fps", -1.0, NULL, FPS_TEXT, FPS_LONGTEXT, VLC_TRUE );
159 add_integer( "v4l-samplerate", 44100, NULL, SAMPLERATE_TEXT,
160 SAMPLERATE_LONGTEXT, VLC_TRUE );
161 add_integer( "v4l-channel", 0, NULL, CHANNEL_TEXT, CHANNEL_LONGTEXT,
163 add_integer( "v4l-tuner", -1, NULL, TUNER_TEXT, TUNER_LONGTEXT, VLC_TRUE );
164 add_integer( "v4l-norm", VIDEO_MODE_AUTO, NULL, NORM_TEXT, NORM_LONGTEXT,
166 change_integer_list( i_norm_list, psz_norm_list_text, 0 );
167 add_integer( "v4l-frequency", -1, NULL, FREQUENCY_TEXT, FREQUENCY_LONGTEXT,
169 add_integer( "v4l-audio", -1, NULL, AUDIO_TEXT, AUDIO_LONGTEXT, VLC_TRUE );
170 add_bool( "v4l-stereo", VLC_TRUE, NULL, STEREO_TEXT, STEREO_LONGTEXT,
172 add_integer( "v4l-width", 0, NULL, WIDTH_TEXT, WIDTH_LONGTEXT, VLC_TRUE );
173 add_integer( "v4l-height", 0, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT,
175 add_integer( "v4l-brightness", -1, NULL, BRIGHTNESS_TEXT,
176 BRIGHTNESS_LONGTEXT, VLC_TRUE );
177 add_integer( "v4l-colour", -1, NULL, COLOUR_TEXT, COLOUR_LONGTEXT,
179 add_integer( "v4l-hue", -1, NULL, HUE_TEXT, HUE_LONGTEXT, VLC_TRUE );
180 add_integer( "v4l-contrast", -1, NULL, CONTRAST_TEXT, CONTRAST_LONGTEXT,
182 add_bool( "v4l-mjpeg", VLC_FALSE, NULL, MJPEG_TEXT, MJPEG_LONGTEXT,
184 add_integer( "v4l-decimation", 1, NULL, DECIMATION_TEXT,
185 DECIMATION_LONGTEXT, VLC_TRUE );
186 add_integer( "v4l-quality", 100, NULL, QUALITY_TEXT, QUALITY_LONGTEXT,
189 add_shortcut( "v4l" );
190 set_capability( "access_demux", 10 );
191 set_callbacks( Open, Close );
194 /*****************************************************************************
195 * Access: local prototypes
196 *****************************************************************************/
197 static int Demux ( demux_t * );
198 static int Control( demux_t *, int, va_list );
200 static void ParseMRL ( demux_t * );
201 static int OpenVideoDev( demux_t *, char * );
202 static int OpenAudioDev( demux_t *, char * );
204 static block_t *GrabAudio( demux_t * );
205 static block_t *GrabVideo( demux_t * );
207 #define MJPEG_BUFFER_SIZE (256*1024)
209 struct quicktime_mjpeg_app1
211 uint32_t i_reserved; /* set to 0 */
212 uint32_t i_tag; /* 'mjpg' */
213 uint32_t i_field_size; /* offset following EOI */
214 uint32_t i_padded_field_size; /* offset following EOI+pad */
215 uint32_t i_next_field; /* offset to next field */
216 uint32_t i_DQT_offset;
217 uint32_t i_DHT_offset;
218 uint32_t i_SOF_offset;
219 uint32_t i_SOS_offset;
220 uint32_t i_data_offset; /* following SOS marker data */
228 } v4lchroma_to_fourcc[] =
230 { VIDEO_PALETTE_GREY, VLC_FOURCC( 'G', 'R', 'E', 'Y' ) },
231 { VIDEO_PALETTE_HI240, VLC_FOURCC( 'I', '2', '4', '0' ) },
232 { VIDEO_PALETTE_RGB565, VLC_FOURCC( 'R', 'V', '1', '6' ) },
233 { VIDEO_PALETTE_RGB555, VLC_FOURCC( 'R', 'V', '1', '5' ) },
234 { VIDEO_PALETTE_RGB24, VLC_FOURCC( 'R', 'V', '2', '4' ) },
235 { VIDEO_PALETTE_RGB32, VLC_FOURCC( 'R', 'V', '3', '2' ) },
236 { VIDEO_PALETTE_YUV422, VLC_FOURCC( 'Y', 'U', 'Y', '2' ) },
237 { VIDEO_PALETTE_YUV422, VLC_FOURCC( 'Y', 'U', 'Y', 'V' ) },
238 { VIDEO_PALETTE_YUYV, VLC_FOURCC( 'Y', 'U', 'Y', '2' ) },
239 { VIDEO_PALETTE_YUYV, VLC_FOURCC( 'Y', 'U', 'Y', 'V' ) },
240 { VIDEO_PALETTE_UYVY, VLC_FOURCC( 'U', 'Y', 'V', 'Y' ) },
241 { VIDEO_PALETTE_YUV420, VLC_FOURCC( 'I', '4', '2', 'N' ) },
242 { VIDEO_PALETTE_YUV411, VLC_FOURCC( 'I', '4', '1', 'N' ) },
243 { VIDEO_PALETTE_RAW, VLC_FOURCC( 'G', 'R', 'A', 'W' ) },
244 { VIDEO_PALETTE_YUV422P, VLC_FOURCC( 'I', '4', '2', '2' ) },
245 { VIDEO_PALETTE_YUV420P, VLC_FOURCC( 'I', '4', '2', '0' ) },
246 { VIDEO_PALETTE_YUV411P, VLC_FOURCC( 'I', '4', '1', '1' ) },
253 char *psz_device; /* Main device from MRL, can be video or audio */
261 /* Video properties */
278 float f_fps; /* <= 0.0 mean to grab at full rate */
279 mtime_t i_video_pts; /* only used when f_fps > 0 */
285 struct video_capability vid_cap;
286 struct video_mbuf vid_mbuf;
287 struct mjpeg_requestbuffers mjpeg_buffers;
289 uint8_t *p_video_mmap;
292 struct video_mmap vid_mmap;
293 struct video_picture vid_picture;
295 int i_video_frame_size;
296 es_out_id_t *p_es_video;
298 /* Audio properties */
299 vlc_fourcc_t i_acodec_raw;
302 int i_audio_max_frame_size;
303 block_t *p_block_audio;
304 es_out_id_t *p_es_audio;
307 /*****************************************************************************
308 * Open: opens v4l device
309 *****************************************************************************
311 * url: <video device>::::
313 *****************************************************************************/
314 static int Open( vlc_object_t *p_this )
316 demux_t *p_demux = (demux_t*)p_this;
320 /* Only when selected */
321 if( *p_demux->psz_access == '\0' )
325 p_demux->pf_demux = Demux;
326 p_demux->pf_control = Control;
327 p_demux->info.i_update = 0;
328 p_demux->info.i_title = 0;
329 p_demux->info.i_seekpoint = 0;
330 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
331 memset( p_sys, 0, sizeof( demux_sys_t ) );
333 var_Create( p_demux, "v4l-audio", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
334 var_Get( p_demux, "v4l-audio", &val );
335 p_sys->i_audio = val.i_int;
337 var_Create( p_demux, "v4l-channel", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
338 var_Get( p_demux, "v4l-channel", &val );
339 p_sys->i_channel = val.i_int;
341 var_Create( p_demux, "v4l-norm", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
342 var_Get( p_demux, "v4l-norm", &val );
343 p_sys->i_norm = val.i_int;
345 var_Create( p_demux, "v4l-tuner", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
346 var_Get( p_demux, "v4l-tuner", &val );
347 p_sys->i_tuner = val.i_int;
349 var_Create( p_demux, "v4l-frequency",
350 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
351 var_Get( p_demux, "v4l-frequency", &val );
352 p_sys->i_frequency = val.i_int;
354 var_Create( p_demux, "v4l-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
355 var_Get( p_demux, "v4l-fps", &val );
356 p_sys->f_fps = val.f_float;
358 var_Create( p_demux, "v4l-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
359 var_Get( p_demux, "v4l-width", &val );
360 p_sys->i_width = val.i_int;
362 var_Create( p_demux, "v4l-height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
363 var_Get( p_demux, "v4l-height", &val );
364 p_sys->i_height = val.i_int;
366 p_sys->i_video_pts = -1;
368 var_Create( p_demux, "v4l-brightness", VLC_VAR_INTEGER |
370 var_Get( p_demux, "v4l-brightness", &val );
371 p_sys->i_brightness = val.i_int;
373 var_Create( p_demux, "v4l-hue", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
374 var_Get( p_demux, "v4l-hue", &val );
377 var_Create( p_demux, "v4l-colour", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
378 var_Get( p_demux, "v4l-colour", &val );
379 p_sys->i_colour = val.i_int;
381 var_Create( p_demux, "v4l-contrast", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
382 var_Get( p_demux, "v4l-contrast", &val );
383 p_sys->i_contrast = val.i_int;
385 var_Create( p_demux, "v4l-mjpeg", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
386 var_Get( p_demux, "v4l-mjpeg", &val );
387 p_sys->b_mjpeg = val.b_bool;
389 var_Create( p_demux, "v4l-decimation", VLC_VAR_INTEGER |
391 var_Get( p_demux, "v4l-decimation", &val );
392 p_sys->i_decimation = val.i_int;
394 var_Create( p_demux, "v4l-quality", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
395 var_Get( p_demux, "v4l-quality", &val );
396 p_sys->i_quality = val.i_int;
398 var_Create( p_demux, "v4l-samplerate",
399 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
400 var_Get( p_demux, "v4l-samplerate", &val );
401 p_sys->i_sample_rate = val.i_int;
403 var_Create( p_demux, "v4l-stereo", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
404 var_Get( p_demux, "v4l-stereo", &val );
405 p_sys->b_stereo = val.b_bool;
407 p_sys->psz_device = p_sys->psz_vdev = p_sys->psz_adev = NULL;
408 p_sys->fd_video = -1;
409 p_sys->fd_audio = -1;
411 p_sys->p_es_video = p_sys->p_es_audio = 0;
412 p_sys->p_block_audio = 0;
416 /* Find main device (video or audio) */
417 if( p_sys->psz_device && *p_sys->psz_device )
419 msg_Dbg( p_demux, "main device=`%s'", p_sys->psz_device );
421 /* Try to open as video device */
422 p_sys->fd_video = OpenVideoDev( p_demux, p_sys->psz_device );
424 if( p_sys->fd_video < 0 )
426 /* Try to open as audio device */
427 p_sys->fd_audio = OpenAudioDev( p_demux, p_sys->psz_device );
428 if( p_sys->fd_audio >= 0 )
430 if( p_sys->psz_adev ) free( p_sys->psz_adev );
431 p_sys->psz_adev = p_sys->psz_device;
432 p_sys->psz_device = NULL;
437 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
438 p_sys->psz_vdev = p_sys->psz_device;
439 p_sys->psz_device = NULL;
443 /* If no device opened, only continue if the access was forced */
444 if( p_sys->fd_video < 0 && p_sys->fd_audio < 0 )
446 if( strcmp( p_demux->psz_access, "v4l" ) )
453 /* Find video device */
454 if( p_sys->fd_video < 0 )
456 if( !p_sys->psz_vdev || !*p_sys->psz_vdev )
458 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
459 p_sys->psz_vdev = var_CreateGetString( p_demux, "v4l-vdev" );;
462 if( p_sys->psz_vdev && *p_sys->psz_vdev )
464 p_sys->fd_video = OpenVideoDev( p_demux, p_sys->psz_vdev );
468 /* Find audio device */
469 if( p_sys->fd_audio < 0 )
471 if( !p_sys->psz_adev || !*p_sys->psz_adev )
473 if( p_sys->psz_adev ) free( p_sys->psz_adev );
474 p_sys->psz_adev = var_CreateGetString( p_demux, "v4l-adev" );;
477 if( p_sys->psz_adev && *p_sys->psz_adev )
479 p_sys->fd_audio = OpenAudioDev( p_demux, p_sys->psz_adev );
483 if( p_sys->fd_video < 0 && p_sys->fd_audio < 0 )
489 msg_Dbg( p_demux, "v4l grabbing started" );
491 /* Declare elementary streams */
492 if( p_sys->fd_video >= 0 )
495 es_format_Init( &fmt, VIDEO_ES, p_sys->i_fourcc );
496 fmt.video.i_width = p_sys->i_width;
497 fmt.video.i_height = p_sys->i_height;
498 fmt.video.i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
500 /* Setup rgb mask for RGB formats */
501 if( p_sys->i_fourcc == VLC_FOURCC('R','V','2','4') )
503 /* This is in BGR format */
504 fmt.video.i_bmask = 0x00ff0000;
505 fmt.video.i_gmask = 0x0000ff00;
506 fmt.video.i_rmask = 0x000000ff;
509 msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
510 (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
511 p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );
514 if( p_sys->fd_audio >= 0 )
517 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC('a','r','a','w') );
519 fmt.audio.i_channels = p_sys->b_stereo ? 2 : 1;
520 fmt.audio.i_rate = p_sys->i_sample_rate;
521 fmt.audio.i_bitspersample = 16; // FIXME ?
522 fmt.audio.i_blockalign = fmt.audio.i_channels *
523 fmt.audio.i_bitspersample / 8;
524 fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate *
525 fmt.audio.i_bitspersample;
527 msg_Dbg( p_demux, "new audio es %d channels %dHz",
528 fmt.audio.i_channels, fmt.audio.i_rate );
530 p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
533 /* Update default_pts to a suitable value for access */
534 var_Create( p_demux, "v4l-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
539 /*****************************************************************************
540 * Close: close device, free resources
541 *****************************************************************************/
542 static void Close( vlc_object_t *p_this )
544 demux_t *p_demux = (demux_t *)p_this;
545 demux_sys_t *p_sys = p_demux->p_sys;
547 if( p_sys->psz_device ) free( p_sys->psz_device );
548 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
549 if( p_sys->psz_adev ) free( p_sys->psz_adev );
550 if( p_sys->fd_video >= 0 ) close( p_sys->fd_video );
551 if( p_sys->fd_audio >= 0 ) close( p_sys->fd_audio );
552 if( p_sys->p_block_audio ) block_Release( p_sys->p_block_audio );
557 ioctl( p_sys->fd_video, MJPIOC_QBUF_CAPT, &i_noframe );
560 if( p_sys->p_video_mmap && p_sys->p_video_mmap != MAP_FAILED )
563 munmap( p_sys->p_video_mmap, p_sys->mjpeg_buffers.size *
564 p_sys->mjpeg_buffers.count );
566 munmap( p_sys->p_video_mmap, p_sys->vid_mbuf.size );
572 /*****************************************************************************
574 *****************************************************************************/
575 static int Control( demux_t *p_demux, int i_query, va_list args )
582 /* Special for access_demux */
583 case DEMUX_CAN_PAUSE:
585 case DEMUX_SET_PAUSE_STATE:
586 case DEMUX_CAN_CONTROL_PACE:
587 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
591 case DEMUX_GET_PTS_DELAY:
592 pi64 = (int64_t*)va_arg( args, int64_t * );
593 *pi64 = (int64_t)var_GetInteger( p_demux, "v4l-caching" ) * 1000;
597 pi64 = (int64_t*)va_arg( args, int64_t * );
601 /* TODO implement others */
609 /*****************************************************************************
611 *****************************************************************************/
612 static int Demux( demux_t *p_demux )
614 demux_sys_t *p_sys = p_demux->p_sys;
615 es_out_id_t *p_es = p_sys->p_es_audio;
616 block_t *p_block = NULL;
618 /* Try grabbing audio frames first */
619 if( p_sys->fd_audio < 0 || !( p_block = GrabAudio( p_demux ) ) )
621 /* Try grabbing video frame */
622 p_es = p_sys->p_es_video;
623 if( p_sys->fd_video > 0 ) p_block = GrabVideo( p_demux );
628 /* Sleep so we do not consume all the cpu, 10ms seems
629 * like a good value (100fps) */
634 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
635 es_out_Send( p_demux->out, p_es, p_block );
640 /*****************************************************************************
641 * ParseMRL: parse the options contained in the MRL
642 *****************************************************************************/
643 static void ParseMRL( demux_t *p_demux )
645 demux_sys_t *p_sys = p_demux->p_sys;
647 char *psz_dup = strdup( p_demux->psz_path );
648 char *psz_parser = psz_dup;
650 while( *psz_parser && *psz_parser != ':' )
655 if( *psz_parser == ':' )
660 *psz_parser++ = '\0';
661 if( !strncmp( psz_parser, "channel=", strlen( "channel=" ) ) )
663 p_sys->i_channel = strtol( psz_parser + strlen( "channel=" ),
666 else if( !strncmp( psz_parser, "norm=", strlen( "norm=" ) ) )
668 psz_parser += strlen( "norm=" );
669 if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
671 p_sys->i_norm = VIDEO_MODE_PAL;
672 psz_parser += strlen( "pal" );
674 else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
676 p_sys->i_norm = VIDEO_MODE_NTSC;
677 psz_parser += strlen( "ntsc" );
679 else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
681 p_sys->i_norm = VIDEO_MODE_SECAM;
682 psz_parser += strlen( "secam" );
684 else if( !strncmp( psz_parser, "auto", strlen( "auto" ) ) )
686 p_sys->i_norm = VIDEO_MODE_AUTO;
687 psz_parser += strlen( "auto" );
691 p_sys->i_norm = strtol( psz_parser, &psz_parser, 0 );
694 else if( !strncmp( psz_parser, "frequency=",
695 strlen( "frequency=" ) ) )
698 strtol( psz_parser + strlen( "frequency=" ),
700 if( p_sys->i_frequency < 30000 )
702 msg_Warn( p_demux, "v4l syntax has changed : "
703 "'frequency' is now channel frequency in kHz");
706 else if( !strncmp( psz_parser, "audio=", strlen( "audio=" ) ) )
708 p_sys->i_audio = strtol( psz_parser + strlen( "audio=" ),
711 else if( !strncmp( psz_parser, "size=", strlen( "size=" ) ) )
713 psz_parser += strlen( "size=" );
714 if( !strncmp( psz_parser, "subqcif", strlen( "subqcif" ) ) )
716 p_sys->i_width = 128;
717 p_sys->i_height = 96;
719 else if( !strncmp( psz_parser, "qsif", strlen( "qsif" ) ) )
721 p_sys->i_width = 160;
722 p_sys->i_height = 120;
724 else if( !strncmp( psz_parser, "qcif", strlen( "qcif" ) ) )
726 p_sys->i_width = 176;
727 p_sys->i_height = 144;
729 else if( !strncmp( psz_parser, "sif", strlen( "sif" ) ) )
731 p_sys->i_width = 320;
732 p_sys->i_height = 244;
734 else if( !strncmp( psz_parser, "cif", strlen( "cif" ) ) )
736 p_sys->i_width = 352;
737 p_sys->i_height = 288;
739 else if( !strncmp( psz_parser, "vga", strlen( "vga" ) ) )
741 p_sys->i_width = 640;
742 p_sys->i_height = 480;
747 p_sys->i_width = strtol( psz_parser, &psz_parser, 0 );
748 if( *psz_parser == 'x' || *psz_parser == 'X')
750 p_sys->i_height = strtol( psz_parser + 1,
753 msg_Dbg( p_demux, "WxH %dx%d", p_sys->i_width,
757 else if( !strncmp( psz_parser, "brightness=", strlen( "brightness=" ) ) )
759 p_sys->i_brightness = strtol( psz_parser + strlen( "brightness=" ),
762 else if( !strncmp( psz_parser, "colour=", strlen( "colour=" ) ) )
764 p_sys->i_colour = strtol( psz_parser + strlen( "colour=" ),
767 else if( !strncmp( psz_parser, "hue=", strlen( "hue=" ) ) )
769 p_sys->i_hue = strtol( psz_parser + strlen( "hue=" ),
772 else if( !strncmp( psz_parser, "contrast=", strlen( "contrast=" ) ) )
774 p_sys->i_contrast = strtol( psz_parser + strlen( "contrast=" ),
777 else if( !strncmp( psz_parser, "tuner=", strlen( "tuner=" ) ) )
779 p_sys->i_tuner = strtol( psz_parser + strlen( "tuner=" ),
782 else if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) ) )
786 psz_parser += strlen( "adev=" );
787 if( strchr( psz_parser, ':' ) )
789 i_len = strchr( psz_parser, ':' ) - psz_parser;
793 i_len = strlen( psz_parser );
796 p_sys->psz_adev = strndup( psz_parser, i_len );
800 else if( !strncmp( psz_parser, "samplerate=",
801 strlen( "samplerate=" ) ) )
803 p_sys->i_sample_rate =
804 strtol( psz_parser + strlen( "samplerate=" ),
807 else if( !strncmp( psz_parser, "stereo", strlen( "stereo" ) ) )
809 psz_parser += strlen( "stereo" );
811 p_sys->b_stereo = VLC_TRUE;
813 else if( !strncmp( psz_parser, "mono", strlen( "mono" ) ) )
815 psz_parser += strlen( "mono" );
817 p_sys->b_stereo = VLC_FALSE;
819 else if( !strncmp( psz_parser, "mjpeg", strlen( "mjpeg" ) ) )
821 psz_parser += strlen( "mjpeg" );
823 p_sys->b_mjpeg = VLC_TRUE;
825 else if( !strncmp( psz_parser, "decimation=",
826 strlen( "decimation=" ) ) )
828 p_sys->i_decimation =
829 strtol( psz_parser + strlen( "decimation=" ),
832 else if( !strncmp( psz_parser, "quality=",
833 strlen( "quality=" ) ) )
836 strtol( psz_parser + strlen( "quality=" ),
839 else if( !strncmp( psz_parser, "fps=", strlen( "fps=" ) ) )
841 p_sys->f_fps = strtof( psz_parser + strlen( "fps=" ),
846 msg_Warn( p_demux, "unknown option" );
849 while( *psz_parser && *psz_parser != ':' )
854 if( *psz_parser == '\0' )
863 p_sys->psz_device = strdup( psz_dup );
865 if( psz_dup ) free( psz_dup );
868 /*****************************************************************************
870 *****************************************************************************/
871 static int OpenVideoDev( demux_t *p_demux, char *psz_device )
873 demux_sys_t *p_sys = p_demux->p_sys;
876 struct video_channel vid_channel;
877 struct mjpeg_params mjpeg;
880 if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
882 msg_Err( p_demux, "cannot open device (%m)" );
886 if( ioctl( i_fd, VIDIOCGCAP, &p_sys->vid_cap ) < 0 )
888 msg_Err( p_demux, "cannot get capabilities (%m)" );
893 "V4L device %s %d channels %d audios %d < w < %d %d < h < %d",
895 p_sys->vid_cap.channels,
896 p_sys->vid_cap.audios,
897 p_sys->vid_cap.minwidth, p_sys->vid_cap.maxwidth,
898 p_sys->vid_cap.minheight, p_sys->vid_cap.maxheight );
900 if( p_sys->i_channel < 0 || p_sys->i_channel >= p_sys->vid_cap.channels )
902 msg_Dbg( p_demux, "invalid channel, falling back on channel 0" );
903 p_sys->i_channel = 0;
905 if( p_sys->vid_cap.audios && p_sys->i_audio >= p_sys->vid_cap.audios )
907 msg_Dbg( p_demux, "invalid audio, falling back with no audio" );
911 if( p_sys->i_width < p_sys->vid_cap.minwidth ||
912 p_sys->i_width > p_sys->vid_cap.maxwidth )
914 msg_Dbg( p_demux, "invalid width %i", p_sys->i_width );
917 if( p_sys->i_height < p_sys->vid_cap.minheight ||
918 p_sys->i_height > p_sys->vid_cap.maxheight )
920 msg_Dbg( p_demux, "invalid height %i", p_sys->i_height );
924 if( !( p_sys->vid_cap.type & VID_TYPE_CAPTURE ) )
926 msg_Err( p_demux, "cannot grab" );
930 vid_channel.channel = p_sys->i_channel;
931 if( ioctl( i_fd, VIDIOCGCHAN, &vid_channel ) < 0 )
933 msg_Err( p_demux, "cannot get channel infos (%m)" );
937 "setting channel %s(%d) %d tuners flags=0x%x type=0x%x norm=0x%x",
938 vid_channel.name, vid_channel.channel, vid_channel.tuners,
939 vid_channel.flags, vid_channel.type, vid_channel.norm );
941 if( p_sys->i_tuner >= vid_channel.tuners )
943 msg_Dbg( p_demux, "invalid tuner, falling back on tuner 0" );
947 vid_channel.norm = p_sys->i_norm;
948 if( ioctl( i_fd, VIDIOCSCHAN, &vid_channel ) < 0 )
950 msg_Err( p_demux, "cannot set channel (%m)" );
954 if( vid_channel.flags & VIDEO_VC_TUNER )
959 struct video_tuner vid_tuner;
960 if( p_sys->i_tuner >= 0 )
962 vid_tuner.tuner = p_sys->i_tuner;
963 if( ioctl( i_fd, VIDIOCGTUNER, &vid_tuner ) < 0 )
965 msg_Err( p_demux, "cannot get tuner (%m)" );
968 msg_Dbg( p_demux, "tuner %s low=%d high=%d, flags=0x%x "
969 "mode=0x%x signal=0x%x",
970 vid_tuner.name, vid_tuner.rangelow, vid_tuner.rangehigh,
971 vid_tuner.flags, vid_tuner.mode, vid_tuner.signal );
973 msg_Dbg( p_demux, "setting tuner %s (%d)",
974 vid_tuner.name, vid_tuner.tuner );
976 /* FIXME FIXME to be checked FIXME FIXME */
977 //vid_tuner.mode = p_sys->i_norm;
978 if( ioctl( i_fd, VIDIOCSTUNER, &vid_tuner ) < 0 )
980 msg_Err( p_demux, "cannot set tuner (%m)" );
986 /* Show a warning if frequency is < than 30000.
987 * User is certainly usint old syntax. */
991 if( p_sys->i_frequency >= 0 )
993 int driver_frequency = p_sys->i_frequency * 16 /1000;
994 if( ioctl( i_fd, VIDIOCSFREQ, &driver_frequency ) < 0 )
996 msg_Err( p_demux, "cannot set frequency (%m)" );
999 msg_Dbg( p_demux, "frequency %d (%d)", p_sys->i_frequency,
1005 if( vid_channel.flags & VIDEO_VC_AUDIO )
1007 struct video_audio vid_audio;
1009 /* XXX TODO volume, balance, ... */
1010 if( p_sys->i_audio >= 0 )
1012 vid_audio.audio = p_sys->i_audio;
1013 if( ioctl( i_fd, VIDIOCGAUDIO, &vid_audio ) < 0 )
1015 msg_Err( p_demux, "cannot get audio (%m)" );
1020 vid_audio.flags &= ~VIDEO_AUDIO_MUTE;
1022 if( ioctl( i_fd, VIDIOCSAUDIO, &vid_audio ) < 0 )
1024 msg_Err( p_demux, "cannot set audio (%m)" );
1031 /* establish basic params with input and norm before feeling width
1033 if( p_sys->b_mjpeg )
1035 struct quicktime_mjpeg_app1 *p_app1;
1038 if( ioctl( i_fd, MJPIOC_G_PARAMS, &mjpeg ) < 0 )
1040 msg_Err( p_demux, "cannot get mjpeg params (%m)" );
1043 mjpeg.input = p_sys->i_channel;
1044 mjpeg.norm = p_sys->i_norm;
1045 mjpeg.decimation = p_sys->i_decimation;
1047 if( p_sys->i_width )
1048 mjpeg.img_width = p_sys->i_width / p_sys->i_decimation;
1049 if( p_sys->i_height )
1050 mjpeg.img_height = p_sys->i_height / p_sys->i_decimation;
1052 /* establish Quicktime APP1 marker while we are here */
1057 p_app1 = (struct quicktime_mjpeg_app1 *)mjpeg.APP_data;
1058 p_app1->i_reserved = 0;
1059 p_app1->i_tag = VLC_FOURCC( 'm','j','p','g' );
1060 p_app1->i_field_size = 0;
1061 p_app1->i_padded_field_size = 0;
1062 p_app1->i_next_field = 0;
1063 /* XXX WARNING XXX */
1064 /* these's nothing magic about these values. We are dangerously
1065 * assuming the encoder card is encoding mjpeg-a and is not throwing
1066 * in marker tags we aren't expecting. It's bad enough we have to
1067 * search through the jpeg output for every frame we grab just to
1068 * find the first field's end marker, so we take this risk to boost
1070 * This is really something the driver could do for us because this
1071 * does conform to standards outside of Apple Quicktime.
1074 p_app1->i_DQT_offset = hton32( i_offset );
1076 p_app1->i_DHT_offset = hton32( i_offset );
1078 p_app1->i_SOF_offset = hton32( i_offset );
1080 p_app1->i_SOS_offset = hton32( i_offset );
1082 p_app1->i_data_offset = hton32( i_offset );
1084 /* SOF and SOS aren't specified by the mjpeg API because they aren't
1085 * optional. They will be present in the output. */
1086 mjpeg.jpeg_markers = JPEG_MARKER_DHT | JPEG_MARKER_DQT;
1088 if( ioctl( i_fd, MJPIOC_S_PARAMS, &mjpeg ) < 0 )
1090 msg_Err( p_demux, "cannot set mjpeg params (%m)" );
1094 p_sys->i_width = mjpeg.img_width * mjpeg.HorDcm;
1095 p_sys->i_height = mjpeg.img_height * mjpeg.VerDcm *
1096 mjpeg.field_per_buff;
1099 /* fix width/height */
1100 if( !p_sys->b_mjpeg && ( p_sys->i_width == 0 || p_sys->i_height == 0 ) )
1102 struct video_window vid_win;
1104 if( ioctl( i_fd, VIDIOCGWIN, &vid_win ) < 0 )
1106 msg_Err( p_demux, "cannot get win (%m)" );
1109 p_sys->i_width = vid_win.width;
1110 p_sys->i_height = vid_win.height;
1112 if( !p_sys->i_width || !p_sys->i_height )
1114 p_sys->i_width = p_sys->vid_cap.maxwidth;
1115 p_sys->i_height = p_sys->vid_cap.maxheight;
1118 if( !p_sys->i_width || !p_sys->i_height )
1120 msg_Err( p_demux, "invalid video size (%ix%i)",
1121 p_sys->i_width, p_sys->i_height );
1125 msg_Dbg( p_demux, "will use %dx%d", p_sys->i_width, p_sys->i_height );
1128 if( !p_sys->b_mjpeg )
1130 /* set hue/color/.. */
1131 if( ioctl( i_fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 )
1133 struct video_picture vid_picture = p_sys->vid_picture;
1135 if( p_sys->i_brightness >= 0 && p_sys->i_brightness < 65536 )
1137 vid_picture.brightness = p_sys->i_brightness;
1139 if( p_sys->i_colour >= 0 && p_sys->i_colour < 65536 )
1141 vid_picture.colour = p_sys->i_colour;
1143 if( p_sys->i_hue >= 0 && p_sys->i_hue < 65536 )
1145 vid_picture.hue = p_sys->i_hue;
1147 if( p_sys->i_contrast >= 0 && p_sys->i_contrast < 65536 )
1149 vid_picture.contrast = p_sys->i_contrast;
1151 if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
1153 msg_Dbg( p_demux, "v4l device uses brightness: %d",
1154 vid_picture.brightness );
1155 msg_Dbg( p_demux, "v4l device uses colour: %d",
1156 vid_picture.colour );
1157 msg_Dbg( p_demux, "v4l device uses hue: %d", vid_picture.hue );
1158 msg_Dbg( p_demux, "v4l device uses contrast: %d",
1159 vid_picture.contrast );
1160 p_sys->vid_picture = vid_picture;
1164 /* Find out video format used by device */
1165 if( ioctl( i_fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 )
1167 struct video_picture vid_picture = p_sys->vid_picture;
1171 p_sys->i_fourcc = 0;
1173 psz = var_CreateGetString( p_demux, "v4l-chroma" );
1174 if( strlen( psz ) >= 4 )
1176 vid_picture.palette = 0;
1177 int i_chroma = VLC_FOURCC( psz[0], psz[1], psz[2], psz[3] );
1179 /* Find out v4l chroma code */
1180 for( i = 0; v4lchroma_to_fourcc[i].i_v4l != 0; i++ )
1182 if( v4lchroma_to_fourcc[i].i_fourcc == i_chroma )
1184 vid_picture.palette = v4lchroma_to_fourcc[i].i_v4l;
1191 if( vid_picture.palette &&
1192 !ioctl( i_fd, VIDIOCSPICT, &vid_picture ) )
1194 p_sys->vid_picture = vid_picture;
1198 /* Try to set the format to something easy to encode */
1199 vid_picture.palette = VIDEO_PALETTE_YUV420P;
1200 if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
1202 p_sys->vid_picture = vid_picture;
1206 vid_picture.palette = VIDEO_PALETTE_YUV422P;
1207 if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
1209 p_sys->vid_picture = vid_picture;
1214 /* Find out final format */
1215 for( i = 0; v4lchroma_to_fourcc[i].i_v4l != 0; i++ )
1217 if( v4lchroma_to_fourcc[i].i_v4l == p_sys->vid_picture.palette)
1219 p_sys->i_fourcc = v4lchroma_to_fourcc[i].i_fourcc;
1226 msg_Err( p_demux, "ioctl VIDIOCGPICT failed" );
1231 if( p_sys->b_mjpeg )
1235 p_sys->mjpeg_buffers.count = 8;
1236 p_sys->mjpeg_buffers.size = MJPEG_BUFFER_SIZE;
1238 if( ioctl( i_fd, MJPIOC_REQBUFS, &p_sys->mjpeg_buffers ) < 0 )
1240 msg_Err( p_demux, "mmap unsupported" );
1244 p_sys->p_video_mmap = mmap( 0,
1245 p_sys->mjpeg_buffers.size * p_sys->mjpeg_buffers.count,
1246 PROT_READ | PROT_WRITE, MAP_SHARED, i_fd, 0 );
1247 if( p_sys->p_video_mmap == MAP_FAILED )
1249 msg_Err( p_demux, "mmap failed" );
1253 p_sys->i_fourcc = VLC_FOURCC( 'm','j','p','g' );
1254 p_sys->i_frame_pos = -1;
1256 /* queue up all the frames */
1257 for( i = 0; i < (int)p_sys->mjpeg_buffers.count; i++ )
1259 if( ioctl( i_fd, MJPIOC_QBUF_CAPT, &i ) < 0 )
1261 msg_Err( p_demux, "unable to queue frame" );
1268 /* Fill in picture_t fields */
1269 vout_InitPicture( VLC_OBJECT(p_demux), &p_sys->pic, p_sys->i_fourcc,
1270 p_sys->i_width, p_sys->i_height, p_sys->i_width *
1271 VOUT_ASPECT_FACTOR / p_sys->i_height );
1272 if( !p_sys->pic.i_planes )
1274 msg_Err( p_demux, "unsupported chroma" );
1277 p_sys->i_video_frame_size = 0;
1278 for( i = 0; i < p_sys->pic.i_planes; i++ )
1280 p_sys->i_video_frame_size += p_sys->pic.p[i].i_visible_lines *
1281 p_sys->pic.p[i].i_visible_pitch;
1284 msg_Dbg( p_demux, "v4l device uses frame size: %i",
1285 p_sys->i_video_frame_size );
1286 msg_Dbg( p_demux, "v4l device uses chroma: %4.4s",
1287 (char*)&p_sys->i_fourcc );
1289 /* Allocate mmap buffer */
1290 if( ioctl( i_fd, VIDIOCGMBUF, &p_sys->vid_mbuf ) < 0 )
1292 msg_Err( p_demux, "mmap unsupported" );
1296 p_sys->p_video_mmap = mmap( 0, p_sys->vid_mbuf.size,
1297 PROT_READ|PROT_WRITE, MAP_SHARED,
1299 if( p_sys->p_video_mmap == MAP_FAILED )
1301 /* FIXME -> normal read */
1302 msg_Err( p_demux, "mmap failed" );
1307 p_sys->vid_mmap.frame = 0;
1308 p_sys->vid_mmap.width = p_sys->i_width;
1309 p_sys->vid_mmap.height = p_sys->i_height;
1310 p_sys->vid_mmap.format = p_sys->vid_picture.palette;
1311 if( ioctl( i_fd, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 )
1313 msg_Warn( p_demux, "%4.4s refused", (char*)&p_sys->i_fourcc );
1314 msg_Err( p_demux, "chroma selection failed" );
1322 if( i_fd >= 0 ) close( i_fd );
1326 /*****************************************************************************
1328 *****************************************************************************/
1329 static int OpenAudioDev( demux_t *p_demux, char *psz_device )
1331 demux_sys_t *p_sys = p_demux->p_sys;
1334 if( (i_fd = open( psz_device, O_RDONLY | O_NONBLOCK )) < 0 )
1336 msg_Err( p_demux, "cannot open audio device (%m)" );
1340 i_format = AFMT_S16_LE;
1341 if( ioctl( i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
1342 || i_format != AFMT_S16_LE )
1344 msg_Err( p_demux, "cannot set audio format (16b little endian) "
1349 if( ioctl( i_fd, SNDCTL_DSP_STEREO,
1350 &p_sys->b_stereo ) < 0 )
1352 msg_Err( p_demux, "cannot set audio channels count (%m)" );
1356 if( ioctl( i_fd, SNDCTL_DSP_SPEED,
1357 &p_sys->i_sample_rate ) < 0 )
1359 msg_Err( p_demux, "cannot set audio sample rate (%m)" );
1363 msg_Dbg( p_demux, "opened adev=`%s' %s %dHz",
1364 psz_device, p_sys->b_stereo ? "stereo" : "mono",
1365 p_sys->i_sample_rate );
1367 p_sys->i_audio_max_frame_size = 6 * 1024;
1373 if( i_fd >= 0 ) close( i_fd );
1377 /*****************************************************************************
1378 * GrabAudio: grab audio
1379 *****************************************************************************/
1380 static block_t *GrabAudio( demux_t *p_demux )
1382 demux_sys_t *p_sys = p_demux->p_sys;
1383 struct audio_buf_info buf_info;
1384 int i_read, i_correct;
1387 if( p_sys->p_block_audio ) p_block = p_sys->p_block_audio;
1388 else p_block = block_New( p_demux, p_sys->i_audio_max_frame_size );
1392 msg_Warn( p_demux, "cannot get block" );
1396 p_sys->p_block_audio = p_block;
1398 i_read = read( p_sys->fd_audio, p_block->p_buffer,
1399 p_sys->i_audio_max_frame_size );
1401 if( i_read <= 0 ) return 0;
1403 p_block->i_buffer = i_read;
1404 p_sys->p_block_audio = 0;
1406 /* Correct the date because of kernel buffering */
1408 if( ioctl( p_sys->fd_audio, SNDCTL_DSP_GETISPACE, &buf_info ) == 0 )
1410 i_correct += buf_info.bytes;
1413 p_block->i_pts = p_block->i_dts =
1414 mdate() - I64C(1000000) * (mtime_t)i_correct /
1415 2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate;
1420 /*****************************************************************************
1422 *****************************************************************************/
1423 static uint8_t *GrabCapture( demux_t *p_demux )
1425 demux_sys_t *p_sys = p_demux->p_sys;
1426 int i_captured_frame = p_sys->i_frame_pos;
1428 p_sys->vid_mmap.frame = (p_sys->i_frame_pos + 1) % p_sys->vid_mbuf.frames;
1430 while( ioctl( p_sys->fd_video, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 )
1432 if( errno != EAGAIN )
1434 msg_Err( p_demux, "failed capturing new frame" );
1438 if( p_demux->b_die )
1443 msg_Dbg( p_demux, "grab failed, trying again" );
1446 while( ioctl(p_sys->fd_video, VIDIOCSYNC, &p_sys->i_frame_pos) < 0 )
1448 if( errno != EAGAIN && errno != EINTR )
1450 msg_Err( p_demux, "failed syncing new frame" );
1455 p_sys->i_frame_pos = p_sys->vid_mmap.frame;
1456 /* leave i_video_frame_size alone */
1457 return p_sys->p_video_mmap + p_sys->vid_mbuf.offsets[i_captured_frame];
1460 static uint8_t *GrabMJPEG( demux_t *p_demux )
1462 demux_sys_t *p_sys = p_demux->p_sys;
1463 struct mjpeg_sync sync;
1464 uint8_t *p_frame, *p_field, *p;
1467 struct quicktime_mjpeg_app1 *p_app1 = NULL;
1469 /* re-queue the last frame we sync'd */
1470 if( p_sys->i_frame_pos != -1 )
1472 while( ioctl( p_sys->fd_video, MJPIOC_QBUF_CAPT,
1473 &p_sys->i_frame_pos ) < 0 )
1475 if( errno != EAGAIN && errno != EINTR )
1477 msg_Err( p_demux, "failed capturing new frame" );
1483 /* sync on the next frame */
1484 while( ioctl( p_sys->fd_video, MJPIOC_SYNC, &sync ) < 0 )
1486 if( errno != EAGAIN && errno != EINTR )
1488 msg_Err( p_demux, "failed syncing new frame" );
1493 p_sys->i_frame_pos = sync.frame;
1494 p_frame = p_sys->p_video_mmap + p_sys->mjpeg_buffers.size * sync.frame;
1496 /* p_frame now points to the data. fix up the Quicktime APP1 marker */
1498 tag = hton16( tag );
1502 p = memmem( p_field, sync.length, &tag, 2 );
1506 p += 2; /* data immediately following EOI */
1508 p_app1 = (struct quicktime_mjpeg_app1 *)(p_field + 6);
1510 i_size = ((uint32_t)(p - p_field));
1511 i_size = hton32( i_size );
1512 memcpy( &p_app1->i_field_size, &i_size, 4 );
1514 while( *p == 0xff && *(p+1) == 0xff )
1517 i_size = ((uint32_t)(p - p_field));
1518 i_size = hton32( i_size );
1519 memcpy( &p_app1->i_padded_field_size, &i_size, 4 );
1523 tag = hton16( tag );
1524 p_field = memmem( p, sync.length - (size_t)(p - p_frame), &tag, 2 );
1528 i_size = (uint32_t)(p_field - p_frame);
1529 i_size = hton32( i_size );
1530 memcpy( &p_app1->i_next_field, &i_size, 4 );
1533 p_app1 = (struct quicktime_mjpeg_app1 *)(p_field + 6);
1535 tag = hton16( tag );
1536 p = memmem( p_field, sync.length - (size_t)(p_field - p_frame),
1541 /* sometimes the second field doesn't have the EOI. just put it
1544 p = p_frame + sync.length;
1545 memcpy( p, &tag, 2 );
1550 i_size = (uint32_t)(p - p_field);
1551 i_size = hton32( i_size );
1552 memcpy( &p_app1->i_field_size, &i_size, 4 );
1553 i_size = (uint32_t)(sync.length - (uint32_t)(p_field - p_frame));
1554 i_size = hton32( i_size );
1555 memcpy( &p_app1->i_padded_field_size, &i_size, 4 );
1558 p_sys->i_video_frame_size = sync.length;
1562 static block_t *GrabVideo( demux_t *p_demux )
1564 demux_sys_t *p_sys = p_demux->p_sys;
1568 if( p_sys->f_fps >= 0.1 && p_sys->i_video_pts > 0 )
1570 mtime_t i_dur = (mtime_t)((double)1000000 / (double)p_sys->f_fps);
1572 /* Did we wait long enough ? (frame rate reduction) */
1573 if( p_sys->i_video_pts + i_dur > mdate() ) return 0;
1576 if( p_sys->b_mjpeg ) p_frame = GrabMJPEG( p_demux );
1577 else p_frame = GrabCapture( p_demux );
1579 if( !p_frame ) return 0;
1581 if( !( p_block = block_New( p_demux, p_sys->i_video_frame_size ) ) )
1583 msg_Warn( p_demux, "cannot get block" );
1587 memcpy( p_block->p_buffer, p_frame, p_sys->i_video_frame_size );
1588 p_sys->i_video_pts = p_block->i_pts = p_block->i_dts = mdate();