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 *****************************************************************************/
35 #include <vlc_input.h>
36 #include <vlc_demux.h>
37 #include <vlc_access.h>
39 #include <vlc_codecs.h>
41 #include <sys/types.h>
43 #include <sys/ioctl.h>
49 /* From GStreamer's v4l plugin:
50 * Because of some really cool feature in video4linux1, also known as
51 * 'not including sys/types.h and sys/time.h', we had to include it
52 * ourselves. In all their intelligence, these people decided to fix
53 * this in the next version (video4linux2) in such a cool way that it
54 * breaks all compilations of old stuff...
55 * The real problem is actually that linux/time.h doesn't use proper
56 * macro checks before defining types like struct timeval. The proper
57 * fix here is to either fuck the kernel header (which is what we do
58 * by defining _LINUX_TIME_H, an innocent little hack) or by fixing it
59 * upstream, which I'll consider doing later on. If you get compiler
60 * errors here, check your linux/time.h && sys/time.h header setup.
64 #include <linux/videodev.h>
65 #include "videodev_mjpeg.h"
67 #include <sys/soundcard.h>
69 /*****************************************************************************
71 *****************************************************************************/
72 static int Open ( vlc_object_t * );
73 static void Close( vlc_object_t * );
75 #define CACHING_TEXT N_("Caching value in ms")
76 #define CACHING_LONGTEXT N_( \
77 "Caching value for V4L captures. This " \
78 "value should be set in milliseconds." )
79 #define VDEV_TEXT N_("Video device name")
80 #define VDEV_LONGTEXT N_( \
81 "Name of the video device to use. " \
82 "If you don't specify anything, no video device will be used.")
83 #define ADEV_TEXT N_("Audio device name")
84 #define ADEV_LONGTEXT N_( \
85 "Name of the audio device to use. " \
86 "If you don't specify anything, no audio device will be used.")
87 #define CHROMA_TEXT N_("Video input chroma format")
88 #define CHROMA_LONGTEXT N_( \
89 "Force the Video4Linux video device to use a specific chroma format " \
90 "(eg. I420 (default), RV24, etc.)")
91 #define FREQUENCY_TEXT N_( "Frequency" )
92 #define FREQUENCY_LONGTEXT N_( \
93 "Frequency to capture (in kHz), if applicable." )
94 #define CHANNEL_TEXT N_( "Channel" )
95 #define CHANNEL_LONGTEXT N_( \
96 "Channel of the card to use (Usually, 0 = tuner, " \
97 "1 = composite, 2 = svideo)." )
98 #define NORM_TEXT N_( "Norm" )
99 #define NORM_LONGTEXT N_( \
100 "Norm of the stream (Automatic, SECAM, PAL, or NTSC)." )
101 #define AUDIO_TEXT N_( "Audio Channel" )
102 #define AUDIO_LONGTEXT N_( \
103 "Audio Channel to use, if there are several audio inputs." )
104 #define WIDTH_TEXT N_( "Width" )
105 #define WIDTH_LONGTEXT N_( "Width of the stream to capture " \
106 "(-1 for autodetect)." )
107 #define HEIGHT_TEXT N_( "Height" )
108 #define HEIGHT_LONGTEXT N_( "Height of the stream to capture " \
109 "(-1 for autodetect)." )
110 #define BRIGHTNESS_TEXT N_( "Brightness" )
111 #define BRIGHTNESS_LONGTEXT N_( \
112 "Brightness of the video input." )
113 #define HUE_TEXT N_( "Hue" )
114 #define HUE_LONGTEXT N_( \
115 "Hue of the video input." )
116 #define COLOUR_TEXT N_( "Color" )
117 #define COLOUR_LONGTEXT N_( \
118 "Color of the video input." )
119 #define CONTRAST_TEXT N_( "Contrast" )
120 #define CONTRAST_LONGTEXT N_( \
121 "Contrast of the video input." )
122 #define TUNER_TEXT N_( "Tuner" )
123 #define TUNER_LONGTEXT N_( "Tuner to use, if there are several ones." )
124 #define SAMPLERATE_TEXT N_( "Samplerate" )
125 #define SAMPLERATE_LONGTEXT N_( \
126 "Samplerate of the captured audio stream, in Hz (eg: 11025, 22050, 44100)" )
127 #define STEREO_TEXT N_( "Stereo" )
128 #define STEREO_LONGTEXT N_( \
129 "Capture the audio stream in stereo." )
130 #define MJPEG_TEXT N_( "MJPEG" )
131 #define MJPEG_LONGTEXT N_( \
132 "Set this option if the capture device outputs MJPEG" )
133 #define DECIMATION_TEXT N_( "Decimation" )
134 #define DECIMATION_LONGTEXT N_( \
135 "Decimation level for MJPEG streams" )
136 #define QUALITY_TEXT N_( "Quality" )
137 #define QUALITY_LONGTEXT N_( "Quality of the stream." )
138 #define FPS_TEXT N_( "Framerate" )
139 #define FPS_LONGTEXT N_( "Framerate to capture, if applicable " \
140 "(-1 for autodetect)." )
142 static int i_norm_list[] =
143 { VIDEO_MODE_AUTO, VIDEO_MODE_SECAM, VIDEO_MODE_PAL, VIDEO_MODE_NTSC };
144 static const char *psz_norm_list_text[] =
145 { N_("Automatic"), N_("SECAM"), N_("PAL"), N_("NTSC") };
148 set_shortname( _("Video4Linux") );
149 set_description( _("Video4Linux input") );
150 set_category( CAT_INPUT );
151 set_subcategory( SUBCAT_INPUT_ACCESS );
153 add_integer( "v4l-caching", DEFAULT_PTS_DELAY / 1000, NULL,
154 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
155 add_string( "v4l-vdev", "/dev/video", 0, VDEV_TEXT, VDEV_LONGTEXT,
157 add_string( "v4l-adev", "/dev/dsp", 0, ADEV_TEXT, ADEV_LONGTEXT,
159 add_string( "v4l-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
161 add_float( "v4l-fps", -1.0, NULL, FPS_TEXT, FPS_LONGTEXT, VLC_TRUE );
162 add_integer( "v4l-samplerate", 44100, NULL, SAMPLERATE_TEXT,
163 SAMPLERATE_LONGTEXT, VLC_TRUE );
164 add_integer( "v4l-channel", 0, NULL, CHANNEL_TEXT, CHANNEL_LONGTEXT,
166 add_integer( "v4l-tuner", -1, NULL, TUNER_TEXT, TUNER_LONGTEXT, VLC_TRUE );
167 add_integer( "v4l-norm", VIDEO_MODE_AUTO, NULL, NORM_TEXT, NORM_LONGTEXT,
169 change_integer_list( i_norm_list, psz_norm_list_text, 0 );
170 add_integer( "v4l-frequency", -1, NULL, FREQUENCY_TEXT, FREQUENCY_LONGTEXT,
172 add_integer( "v4l-audio", -1, NULL, AUDIO_TEXT, AUDIO_LONGTEXT, VLC_TRUE );
173 add_bool( "v4l-stereo", VLC_TRUE, NULL, STEREO_TEXT, STEREO_LONGTEXT,
175 add_integer( "v4l-width", 0, NULL, WIDTH_TEXT, WIDTH_LONGTEXT, VLC_TRUE );
176 add_integer( "v4l-height", 0, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT,
178 add_integer( "v4l-brightness", -1, NULL, BRIGHTNESS_TEXT,
179 BRIGHTNESS_LONGTEXT, VLC_TRUE );
180 add_integer( "v4l-colour", -1, NULL, COLOUR_TEXT, COLOUR_LONGTEXT,
182 add_integer( "v4l-hue", -1, NULL, HUE_TEXT, HUE_LONGTEXT, VLC_TRUE );
183 add_integer( "v4l-contrast", -1, NULL, CONTRAST_TEXT, CONTRAST_LONGTEXT,
185 add_bool( "v4l-mjpeg", VLC_FALSE, NULL, MJPEG_TEXT, MJPEG_LONGTEXT,
187 add_integer( "v4l-decimation", 1, NULL, DECIMATION_TEXT,
188 DECIMATION_LONGTEXT, VLC_TRUE );
189 add_integer( "v4l-quality", 100, NULL, QUALITY_TEXT, QUALITY_LONGTEXT,
192 add_shortcut( "v4l" );
193 set_capability( "access_demux", 10 );
194 set_callbacks( Open, Close );
197 /*****************************************************************************
198 * Access: local prototypes
199 *****************************************************************************/
200 static int Demux ( demux_t * );
201 static int Control( demux_t *, int, va_list );
203 static void ParseMRL ( demux_t * );
204 static int OpenVideoDev( demux_t *, char * );
205 static int OpenAudioDev( demux_t *, char * );
207 static block_t *GrabAudio( demux_t * );
208 static block_t *GrabVideo( demux_t * );
210 #define MJPEG_BUFFER_SIZE (256*1024)
212 struct quicktime_mjpeg_app1
214 uint32_t i_reserved; /* set to 0 */
215 uint32_t i_tag; /* 'mjpg' */
216 uint32_t i_field_size; /* offset following EOI */
217 uint32_t i_padded_field_size; /* offset following EOI+pad */
218 uint32_t i_next_field; /* offset to next field */
219 uint32_t i_DQT_offset;
220 uint32_t i_DHT_offset;
221 uint32_t i_SOF_offset;
222 uint32_t i_SOS_offset;
223 uint32_t i_data_offset; /* following SOS marker data */
231 } v4lchroma_to_fourcc[] =
233 { VIDEO_PALETTE_GREY, VLC_FOURCC( 'G', 'R', 'E', 'Y' ) },
234 { VIDEO_PALETTE_HI240, VLC_FOURCC( 'I', '2', '4', '0' ) },
235 { VIDEO_PALETTE_RGB565, VLC_FOURCC( 'R', 'V', '1', '6' ) },
236 { VIDEO_PALETTE_RGB555, VLC_FOURCC( 'R', 'V', '1', '5' ) },
237 { VIDEO_PALETTE_RGB24, VLC_FOURCC( 'R', 'V', '2', '4' ) },
238 { VIDEO_PALETTE_RGB32, VLC_FOURCC( 'R', 'V', '3', '2' ) },
239 { VIDEO_PALETTE_YUV422, VLC_FOURCC( 'Y', 'U', 'Y', '2' ) },
240 { VIDEO_PALETTE_YUV422, VLC_FOURCC( 'Y', 'U', 'Y', 'V' ) },
241 { VIDEO_PALETTE_YUYV, VLC_FOURCC( 'Y', 'U', 'Y', '2' ) },
242 { VIDEO_PALETTE_YUYV, VLC_FOURCC( 'Y', 'U', 'Y', 'V' ) },
243 { VIDEO_PALETTE_UYVY, VLC_FOURCC( 'U', 'Y', 'V', 'Y' ) },
244 { VIDEO_PALETTE_YUV420, VLC_FOURCC( 'I', '4', '2', 'N' ) },
245 { VIDEO_PALETTE_YUV411, VLC_FOURCC( 'I', '4', '1', 'N' ) },
246 { VIDEO_PALETTE_RAW, VLC_FOURCC( 'G', 'R', 'A', 'W' ) },
247 { VIDEO_PALETTE_YUV422P, VLC_FOURCC( 'I', '4', '2', '2' ) },
248 { VIDEO_PALETTE_YUV420P, VLC_FOURCC( 'I', '4', '2', '0' ) },
249 { VIDEO_PALETTE_YUV411P, VLC_FOURCC( 'I', '4', '1', '1' ) },
256 char *psz_device; /* Main device from MRL, can be video or audio */
264 /* Video properties */
281 float f_fps; /* <= 0.0 mean to grab at full rate */
282 mtime_t i_video_pts; /* only used when f_fps > 0 */
288 struct video_capability vid_cap;
289 struct video_mbuf vid_mbuf;
290 struct mjpeg_requestbuffers mjpeg_buffers;
292 uint8_t *p_video_mmap;
295 struct video_mmap vid_mmap;
296 struct video_picture vid_picture;
298 int i_video_frame_size;
299 es_out_id_t *p_es_video;
301 /* Audio properties */
302 vlc_fourcc_t i_acodec_raw;
305 int i_audio_max_frame_size;
306 block_t *p_block_audio;
307 es_out_id_t *p_es_audio;
310 /*****************************************************************************
311 * Open: opens v4l device
312 *****************************************************************************
314 * url: <video device>::::
316 *****************************************************************************/
317 static int Open( vlc_object_t *p_this )
319 demux_t *p_demux = (demux_t*)p_this;
323 /* Only when selected */
324 if( *p_demux->psz_access == '\0' )
328 p_demux->pf_demux = Demux;
329 p_demux->pf_control = Control;
330 p_demux->info.i_update = 0;
331 p_demux->info.i_title = 0;
332 p_demux->info.i_seekpoint = 0;
333 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
334 memset( p_sys, 0, sizeof( demux_sys_t ) );
336 var_Create( p_demux, "v4l-audio", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
337 var_Get( p_demux, "v4l-audio", &val );
338 p_sys->i_audio = val.i_int;
340 var_Create( p_demux, "v4l-channel", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
341 var_Get( p_demux, "v4l-channel", &val );
342 p_sys->i_channel = val.i_int;
344 var_Create( p_demux, "v4l-norm", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
345 var_Get( p_demux, "v4l-norm", &val );
346 p_sys->i_norm = val.i_int;
348 var_Create( p_demux, "v4l-tuner", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
349 var_Get( p_demux, "v4l-tuner", &val );
350 p_sys->i_tuner = val.i_int;
352 var_Create( p_demux, "v4l-frequency",
353 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
354 var_Get( p_demux, "v4l-frequency", &val );
355 p_sys->i_frequency = val.i_int;
357 var_Create( p_demux, "v4l-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
358 var_Get( p_demux, "v4l-fps", &val );
359 p_sys->f_fps = val.f_float;
361 var_Create( p_demux, "v4l-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
362 var_Get( p_demux, "v4l-width", &val );
363 p_sys->i_width = val.i_int;
365 var_Create( p_demux, "v4l-height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
366 var_Get( p_demux, "v4l-height", &val );
367 p_sys->i_height = val.i_int;
369 p_sys->i_video_pts = -1;
371 var_Create( p_demux, "v4l-brightness", VLC_VAR_INTEGER |
373 var_Get( p_demux, "v4l-brightness", &val );
374 p_sys->i_brightness = val.i_int;
376 var_Create( p_demux, "v4l-hue", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
377 var_Get( p_demux, "v4l-hue", &val );
380 var_Create( p_demux, "v4l-colour", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
381 var_Get( p_demux, "v4l-colour", &val );
382 p_sys->i_colour = val.i_int;
384 var_Create( p_demux, "v4l-contrast", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
385 var_Get( p_demux, "v4l-contrast", &val );
386 p_sys->i_contrast = val.i_int;
388 var_Create( p_demux, "v4l-mjpeg", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
389 var_Get( p_demux, "v4l-mjpeg", &val );
390 p_sys->b_mjpeg = val.b_bool;
392 var_Create( p_demux, "v4l-decimation", VLC_VAR_INTEGER |
394 var_Get( p_demux, "v4l-decimation", &val );
395 p_sys->i_decimation = val.i_int;
397 var_Create( p_demux, "v4l-quality", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
398 var_Get( p_demux, "v4l-quality", &val );
399 p_sys->i_quality = val.i_int;
401 var_Create( p_demux, "v4l-samplerate",
402 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
403 var_Get( p_demux, "v4l-samplerate", &val );
404 p_sys->i_sample_rate = val.i_int;
406 var_Create( p_demux, "v4l-stereo", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
407 var_Get( p_demux, "v4l-stereo", &val );
408 p_sys->b_stereo = val.b_bool;
410 p_sys->psz_device = p_sys->psz_vdev = p_sys->psz_adev = NULL;
411 p_sys->fd_video = -1;
412 p_sys->fd_audio = -1;
414 p_sys->p_es_video = p_sys->p_es_audio = 0;
415 p_sys->p_block_audio = 0;
419 /* Find main device (video or audio) */
420 if( p_sys->psz_device && *p_sys->psz_device )
422 msg_Dbg( p_demux, "main device=`%s'", p_sys->psz_device );
424 /* Try to open as video device */
425 p_sys->fd_video = OpenVideoDev( p_demux, p_sys->psz_device );
427 if( p_sys->fd_video < 0 )
429 /* Try to open as audio device */
430 p_sys->fd_audio = OpenAudioDev( p_demux, p_sys->psz_device );
431 if( p_sys->fd_audio >= 0 )
433 if( p_sys->psz_adev ) free( p_sys->psz_adev );
434 p_sys->psz_adev = p_sys->psz_device;
435 p_sys->psz_device = NULL;
440 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
441 p_sys->psz_vdev = p_sys->psz_device;
442 p_sys->psz_device = NULL;
446 /* If no device opened, only continue if the access was forced */
447 if( p_sys->fd_video < 0 && p_sys->fd_audio < 0 )
449 if( strcmp( p_demux->psz_access, "v4l" ) )
456 /* Find video device */
457 if( p_sys->fd_video < 0 )
459 if( !p_sys->psz_vdev || !*p_sys->psz_vdev )
461 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
462 p_sys->psz_vdev = var_CreateGetString( p_demux, "v4l-vdev" );;
465 if( p_sys->psz_vdev && *p_sys->psz_vdev )
467 p_sys->fd_video = OpenVideoDev( p_demux, p_sys->psz_vdev );
471 /* Find audio device */
472 if( p_sys->fd_audio < 0 )
474 if( !p_sys->psz_adev || !*p_sys->psz_adev )
476 if( p_sys->psz_adev ) free( p_sys->psz_adev );
477 p_sys->psz_adev = var_CreateGetString( p_demux, "v4l-adev" );;
480 if( p_sys->psz_adev && *p_sys->psz_adev )
482 p_sys->fd_audio = OpenAudioDev( p_demux, p_sys->psz_adev );
486 if( p_sys->fd_video < 0 && p_sys->fd_audio < 0 )
492 msg_Dbg( p_demux, "v4l grabbing started" );
494 /* Declare elementary streams */
495 if( p_sys->fd_video >= 0 )
498 es_format_Init( &fmt, VIDEO_ES, p_sys->i_fourcc );
499 fmt.video.i_width = p_sys->i_width;
500 fmt.video.i_height = p_sys->i_height;
501 fmt.video.i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
503 /* Setup rgb mask for RGB formats */
504 if( p_sys->i_fourcc == VLC_FOURCC('R','V','2','4') )
506 /* This is in BGR format */
507 fmt.video.i_bmask = 0x00ff0000;
508 fmt.video.i_gmask = 0x0000ff00;
509 fmt.video.i_rmask = 0x000000ff;
512 msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
513 (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
514 p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );
517 if( p_sys->fd_audio >= 0 )
520 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC('a','r','a','w') );
522 fmt.audio.i_channels = p_sys->b_stereo ? 2 : 1;
523 fmt.audio.i_rate = p_sys->i_sample_rate;
524 fmt.audio.i_bitspersample = 16; // FIXME ?
525 fmt.audio.i_blockalign = fmt.audio.i_channels *
526 fmt.audio.i_bitspersample / 8;
527 fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate *
528 fmt.audio.i_bitspersample;
530 msg_Dbg( p_demux, "new audio es %d channels %dHz",
531 fmt.audio.i_channels, fmt.audio.i_rate );
533 p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
536 /* Update default_pts to a suitable value for access */
537 var_Create( p_demux, "v4l-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
542 /*****************************************************************************
543 * Close: close device, free resources
544 *****************************************************************************/
545 static void Close( vlc_object_t *p_this )
547 demux_t *p_demux = (demux_t *)p_this;
548 demux_sys_t *p_sys = p_demux->p_sys;
550 if( p_sys->psz_device ) free( p_sys->psz_device );
551 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
552 if( p_sys->psz_adev ) free( p_sys->psz_adev );
553 if( p_sys->fd_video >= 0 ) close( p_sys->fd_video );
554 if( p_sys->fd_audio >= 0 ) close( p_sys->fd_audio );
555 if( p_sys->p_block_audio ) block_Release( p_sys->p_block_audio );
560 ioctl( p_sys->fd_video, MJPIOC_QBUF_CAPT, &i_noframe );
563 if( p_sys->p_video_mmap && p_sys->p_video_mmap != MAP_FAILED )
566 munmap( p_sys->p_video_mmap, p_sys->mjpeg_buffers.size *
567 p_sys->mjpeg_buffers.count );
569 munmap( p_sys->p_video_mmap, p_sys->vid_mbuf.size );
575 /*****************************************************************************
577 *****************************************************************************/
578 static int Control( demux_t *p_demux, int i_query, va_list args )
585 /* Special for access_demux */
586 case DEMUX_CAN_PAUSE:
587 case DEMUX_SET_PAUSE_STATE:
588 case DEMUX_CAN_CONTROL_PACE:
589 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
593 case DEMUX_GET_PTS_DELAY:
594 pi64 = (int64_t*)va_arg( args, int64_t * );
595 *pi64 = (int64_t)var_GetInteger( p_demux, "v4l-caching" ) * 1000;
599 pi64 = (int64_t*)va_arg( args, int64_t * );
603 /* TODO implement others */
611 /*****************************************************************************
613 *****************************************************************************/
614 static int Demux( demux_t *p_demux )
616 demux_sys_t *p_sys = p_demux->p_sys;
617 es_out_id_t *p_es = p_sys->p_es_audio;
618 block_t *p_block = NULL;
620 /* Try grabbing audio frames first */
621 if( p_sys->fd_audio < 0 || !( p_block = GrabAudio( p_demux ) ) )
623 /* Try grabbing video frame */
624 p_es = p_sys->p_es_video;
625 if( p_sys->fd_video > 0 ) p_block = GrabVideo( p_demux );
630 /* Sleep so we do not consume all the cpu, 10ms seems
631 * like a good value (100fps) */
636 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
637 es_out_Send( p_demux->out, p_es, p_block );
642 /*****************************************************************************
643 * ParseMRL: parse the options contained in the MRL
644 *****************************************************************************/
645 static void ParseMRL( demux_t *p_demux )
647 demux_sys_t *p_sys = p_demux->p_sys;
649 char *psz_dup = strdup( p_demux->psz_path );
650 char *psz_parser = psz_dup;
652 while( *psz_parser && *psz_parser != ':' )
657 if( *psz_parser == ':' )
662 *psz_parser++ = '\0';
663 if( !strncmp( psz_parser, "channel=", strlen( "channel=" ) ) )
665 p_sys->i_channel = strtol( psz_parser + strlen( "channel=" ),
668 else if( !strncmp( psz_parser, "norm=", strlen( "norm=" ) ) )
670 psz_parser += strlen( "norm=" );
671 if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
673 p_sys->i_norm = VIDEO_MODE_PAL;
674 psz_parser += strlen( "pal" );
676 else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
678 p_sys->i_norm = VIDEO_MODE_NTSC;
679 psz_parser += strlen( "ntsc" );
681 else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
683 p_sys->i_norm = VIDEO_MODE_SECAM;
684 psz_parser += strlen( "secam" );
686 else if( !strncmp( psz_parser, "auto", strlen( "auto" ) ) )
688 p_sys->i_norm = VIDEO_MODE_AUTO;
689 psz_parser += strlen( "auto" );
693 p_sys->i_norm = strtol( psz_parser, &psz_parser, 0 );
696 else if( !strncmp( psz_parser, "frequency=",
697 strlen( "frequency=" ) ) )
700 strtol( psz_parser + strlen( "frequency=" ),
702 if( p_sys->i_frequency < 30000 )
704 msg_Warn( p_demux, "v4l syntax has changed : "
705 "'frequency' is now channel frequency in kHz");
708 else if( !strncmp( psz_parser, "audio=", strlen( "audio=" ) ) )
710 p_sys->i_audio = strtol( psz_parser + strlen( "audio=" ),
713 else if( !strncmp( psz_parser, "size=", strlen( "size=" ) ) )
715 psz_parser += strlen( "size=" );
716 if( !strncmp( psz_parser, "subqcif", strlen( "subqcif" ) ) )
718 p_sys->i_width = 128;
719 p_sys->i_height = 96;
721 else if( !strncmp( psz_parser, "qsif", strlen( "qsif" ) ) )
723 p_sys->i_width = 160;
724 p_sys->i_height = 120;
726 else if( !strncmp( psz_parser, "qcif", strlen( "qcif" ) ) )
728 p_sys->i_width = 176;
729 p_sys->i_height = 144;
731 else if( !strncmp( psz_parser, "sif", strlen( "sif" ) ) )
733 p_sys->i_width = 320;
734 p_sys->i_height = 244;
736 else if( !strncmp( psz_parser, "cif", strlen( "cif" ) ) )
738 p_sys->i_width = 352;
739 p_sys->i_height = 288;
741 else if( !strncmp( psz_parser, "vga", strlen( "vga" ) ) )
743 p_sys->i_width = 640;
744 p_sys->i_height = 480;
749 p_sys->i_width = strtol( psz_parser, &psz_parser, 0 );
750 if( *psz_parser == 'x' || *psz_parser == 'X')
752 p_sys->i_height = strtol( psz_parser + 1,
755 msg_Dbg( p_demux, "WxH %dx%d", p_sys->i_width,
759 else if( !strncmp( psz_parser, "brightness=", strlen( "brightness=" ) ) )
761 p_sys->i_brightness = strtol( psz_parser + strlen( "brightness=" ),
764 else if( !strncmp( psz_parser, "colour=", strlen( "colour=" ) ) )
766 p_sys->i_colour = strtol( psz_parser + strlen( "colour=" ),
769 else if( !strncmp( psz_parser, "hue=", strlen( "hue=" ) ) )
771 p_sys->i_hue = strtol( psz_parser + strlen( "hue=" ),
774 else if( !strncmp( psz_parser, "contrast=", strlen( "contrast=" ) ) )
776 p_sys->i_contrast = strtol( psz_parser + strlen( "contrast=" ),
779 else if( !strncmp( psz_parser, "tuner=", strlen( "tuner=" ) ) )
781 p_sys->i_tuner = strtol( psz_parser + strlen( "tuner=" ),
784 else if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) ) )
788 psz_parser += strlen( "adev=" );
789 if( strchr( psz_parser, ':' ) )
791 i_len = strchr( psz_parser, ':' ) - psz_parser;
795 i_len = strlen( psz_parser );
798 p_sys->psz_adev = strndup( psz_parser, i_len );
802 else if( !strncmp( psz_parser, "samplerate=",
803 strlen( "samplerate=" ) ) )
805 p_sys->i_sample_rate =
806 strtol( psz_parser + strlen( "samplerate=" ),
809 else if( !strncmp( psz_parser, "stereo", strlen( "stereo" ) ) )
811 psz_parser += strlen( "stereo" );
813 p_sys->b_stereo = VLC_TRUE;
815 else if( !strncmp( psz_parser, "mono", strlen( "mono" ) ) )
817 psz_parser += strlen( "mono" );
819 p_sys->b_stereo = VLC_FALSE;
821 else if( !strncmp( psz_parser, "mjpeg", strlen( "mjpeg" ) ) )
823 psz_parser += strlen( "mjpeg" );
825 p_sys->b_mjpeg = VLC_TRUE;
827 else if( !strncmp( psz_parser, "decimation=",
828 strlen( "decimation=" ) ) )
830 p_sys->i_decimation =
831 strtol( psz_parser + strlen( "decimation=" ),
834 else if( !strncmp( psz_parser, "quality=",
835 strlen( "quality=" ) ) )
838 strtol( psz_parser + strlen( "quality=" ),
841 else if( !strncmp( psz_parser, "fps=", strlen( "fps=" ) ) )
843 p_sys->f_fps = strtof( psz_parser + strlen( "fps=" ),
848 msg_Warn( p_demux, "unknown option" );
851 while( *psz_parser && *psz_parser != ':' )
856 if( *psz_parser == '\0' )
865 p_sys->psz_device = strdup( psz_dup );
867 if( psz_dup ) free( psz_dup );
870 /*****************************************************************************
872 *****************************************************************************/
873 static int OpenVideoDev( demux_t *p_demux, char *psz_device )
875 demux_sys_t *p_sys = p_demux->p_sys;
878 struct video_channel vid_channel;
879 struct mjpeg_params mjpeg;
882 if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
884 msg_Err( p_demux, "cannot open device (%s)", strerror( errno ) );
888 if( ioctl( i_fd, VIDIOCGCAP, &p_sys->vid_cap ) < 0 )
890 msg_Err( p_demux, "cannot get capabilities (%s)", strerror( errno ) );
895 "V4L device %s %d channels %d audios %d < w < %d %d < h < %d",
897 p_sys->vid_cap.channels,
898 p_sys->vid_cap.audios,
899 p_sys->vid_cap.minwidth, p_sys->vid_cap.maxwidth,
900 p_sys->vid_cap.minheight, p_sys->vid_cap.maxheight );
902 if( p_sys->i_channel < 0 || p_sys->i_channel >= p_sys->vid_cap.channels )
904 msg_Dbg( p_demux, "invalid channel, falling back on channel 0" );
905 p_sys->i_channel = 0;
907 if( p_sys->vid_cap.audios && p_sys->i_audio >= p_sys->vid_cap.audios )
909 msg_Dbg( p_demux, "invalid audio, falling back with no audio" );
913 if( p_sys->i_width < p_sys->vid_cap.minwidth ||
914 p_sys->i_width > p_sys->vid_cap.maxwidth )
916 msg_Dbg( p_demux, "invalid width %i", p_sys->i_width );
919 if( p_sys->i_height < p_sys->vid_cap.minheight ||
920 p_sys->i_height > p_sys->vid_cap.maxheight )
922 msg_Dbg( p_demux, "invalid height %i", p_sys->i_height );
926 if( !( p_sys->vid_cap.type & VID_TYPE_CAPTURE ) )
928 msg_Err( p_demux, "cannot grab" );
932 vid_channel.channel = p_sys->i_channel;
933 if( ioctl( i_fd, VIDIOCGCHAN, &vid_channel ) < 0 )
935 msg_Err( p_demux, "cannot get channel infos (%s)",
940 "setting channel %s(%d) %d tuners flags=0x%x type=0x%x norm=0x%x",
941 vid_channel.name, vid_channel.channel, vid_channel.tuners,
942 vid_channel.flags, vid_channel.type, vid_channel.norm );
944 if( p_sys->i_tuner >= vid_channel.tuners )
946 msg_Dbg( p_demux, "invalid tuner, falling back on tuner 0" );
950 vid_channel.norm = p_sys->i_norm;
951 if( ioctl( i_fd, VIDIOCSCHAN, &vid_channel ) < 0 )
953 msg_Err( p_demux, "cannot set channel (%s)", strerror( errno ) );
957 if( vid_channel.flags & VIDEO_VC_TUNER )
962 struct video_tuner vid_tuner;
963 if( p_sys->i_tuner >= 0 )
965 vid_tuner.tuner = p_sys->i_tuner;
966 if( ioctl( i_fd, VIDIOCGTUNER, &vid_tuner ) < 0 )
968 msg_Err( p_demux, "cannot get tuner (%s)", strerror( errno ) );
971 msg_Dbg( p_demux, "tuner %s low=%d high=%d, flags=0x%x "
972 "mode=0x%x signal=0x%x",
973 vid_tuner.name, vid_tuner.rangelow, vid_tuner.rangehigh,
974 vid_tuner.flags, vid_tuner.mode, vid_tuner.signal );
976 msg_Dbg( p_demux, "setting tuner %s (%d)",
977 vid_tuner.name, vid_tuner.tuner );
979 /* FIXME FIXME to be checked FIXME FIXME */
980 //vid_tuner.mode = p_sys->i_norm;
981 if( ioctl( i_fd, VIDIOCSTUNER, &vid_tuner ) < 0 )
983 msg_Err( p_demux, "cannot set tuner (%s)", strerror( errno ) );
989 /* Show a warning if frequency is < than 30000.
990 * User is certainly usint old syntax. */
994 if( p_sys->i_frequency >= 0 )
996 int driver_frequency = p_sys->i_frequency * 16 /1000;
997 if( ioctl( i_fd, VIDIOCSFREQ, &driver_frequency ) < 0 )
999 msg_Err( p_demux, "cannot set frequency (%s)",
1000 strerror( errno ) );
1003 msg_Dbg( p_demux, "frequency %d (%d)", p_sys->i_frequency,
1009 if( vid_channel.flags & VIDEO_VC_AUDIO )
1011 struct video_audio vid_audio;
1013 /* XXX TODO volume, balance, ... */
1014 if( p_sys->i_audio >= 0 )
1016 vid_audio.audio = p_sys->i_audio;
1017 if( ioctl( i_fd, VIDIOCGAUDIO, &vid_audio ) < 0 )
1019 msg_Err( p_demux, "cannot get audio (%s)", strerror( errno ) );
1024 vid_audio.flags &= ~VIDEO_AUDIO_MUTE;
1026 if( ioctl( i_fd, VIDIOCSAUDIO, &vid_audio ) < 0 )
1028 msg_Err( p_demux, "cannot set audio (%s)", strerror( errno ) );
1035 /* establish basic params with input and norm before feeling width
1037 if( p_sys->b_mjpeg )
1039 struct quicktime_mjpeg_app1 *p_app1;
1042 if( ioctl( i_fd, MJPIOC_G_PARAMS, &mjpeg ) < 0 )
1044 msg_Err( p_demux, "cannot get mjpeg params (%s)",
1045 strerror( errno ) );
1048 mjpeg.input = p_sys->i_channel;
1049 mjpeg.norm = p_sys->i_norm;
1050 mjpeg.decimation = p_sys->i_decimation;
1052 if( p_sys->i_width )
1053 mjpeg.img_width = p_sys->i_width / p_sys->i_decimation;
1054 if( p_sys->i_height )
1055 mjpeg.img_height = p_sys->i_height / p_sys->i_decimation;
1057 /* establish Quicktime APP1 marker while we are here */
1062 p_app1 = (struct quicktime_mjpeg_app1 *)mjpeg.APP_data;
1063 p_app1->i_reserved = 0;
1064 p_app1->i_tag = VLC_FOURCC( 'm','j','p','g' );
1065 p_app1->i_field_size = 0;
1066 p_app1->i_padded_field_size = 0;
1067 p_app1->i_next_field = 0;
1068 /* XXX WARNING XXX */
1069 /* these's nothing magic about these values. We are dangerously
1070 * assuming the encoder card is encoding mjpeg-a and is not throwing
1071 * in marker tags we aren't expecting. It's bad enough we have to
1072 * search through the jpeg output for every frame we grab just to
1073 * find the first field's end marker, so we take this risk to boost
1075 * This is really something the driver could do for us because this
1076 * does conform to standards outside of Apple Quicktime.
1079 p_app1->i_DQT_offset = hton32( i_offset );
1081 p_app1->i_DHT_offset = hton32( i_offset );
1083 p_app1->i_SOF_offset = hton32( i_offset );
1085 p_app1->i_SOS_offset = hton32( i_offset );
1087 p_app1->i_data_offset = hton32( i_offset );
1089 /* SOF and SOS aren't specified by the mjpeg API because they aren't
1090 * optional. They will be present in the output. */
1091 mjpeg.jpeg_markers = JPEG_MARKER_DHT | JPEG_MARKER_DQT;
1093 if( ioctl( i_fd, MJPIOC_S_PARAMS, &mjpeg ) < 0 )
1095 msg_Err( p_demux, "cannot set mjpeg params (%s)",
1096 strerror( errno ) );
1100 p_sys->i_width = mjpeg.img_width * mjpeg.HorDcm;
1101 p_sys->i_height = mjpeg.img_height * mjpeg.VerDcm *
1102 mjpeg.field_per_buff;
1105 /* fix width/height */
1106 if( !p_sys->b_mjpeg && ( p_sys->i_width == 0 || p_sys->i_height == 0 ) )
1108 struct video_window vid_win;
1110 if( ioctl( i_fd, VIDIOCGWIN, &vid_win ) < 0 )
1112 msg_Err( p_demux, "cannot get win (%s)", strerror( errno ) );
1115 p_sys->i_width = vid_win.width;
1116 p_sys->i_height = vid_win.height;
1118 if( !p_sys->i_width || !p_sys->i_height )
1120 p_sys->i_width = p_sys->vid_cap.maxwidth;
1121 p_sys->i_height = p_sys->vid_cap.maxheight;
1124 if( !p_sys->i_width || !p_sys->i_height )
1126 msg_Err( p_demux, "invalid video size (%ix%i)",
1127 p_sys->i_width, p_sys->i_height );
1131 msg_Dbg( p_demux, "will use %dx%d", p_sys->i_width, p_sys->i_height );
1134 if( !p_sys->b_mjpeg )
1136 /* set hue/color/.. */
1137 if( ioctl( i_fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 )
1139 struct video_picture vid_picture = p_sys->vid_picture;
1141 if( p_sys->i_brightness >= 0 && p_sys->i_brightness < 65536 )
1143 vid_picture.brightness = p_sys->i_brightness;
1145 if( p_sys->i_colour >= 0 && p_sys->i_colour < 65536 )
1147 vid_picture.colour = p_sys->i_colour;
1149 if( p_sys->i_hue >= 0 && p_sys->i_hue < 65536 )
1151 vid_picture.hue = p_sys->i_hue;
1153 if( p_sys->i_contrast >= 0 && p_sys->i_contrast < 65536 )
1155 vid_picture.contrast = p_sys->i_contrast;
1157 if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
1159 msg_Dbg( p_demux, "v4l device uses brightness: %d",
1160 vid_picture.brightness );
1161 msg_Dbg( p_demux, "v4l device uses colour: %d",
1162 vid_picture.colour );
1163 msg_Dbg( p_demux, "v4l device uses hue: %d", vid_picture.hue );
1164 msg_Dbg( p_demux, "v4l device uses contrast: %d",
1165 vid_picture.contrast );
1166 p_sys->vid_picture = vid_picture;
1170 /* Find out video format used by device */
1171 if( ioctl( i_fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 )
1173 struct video_picture vid_picture = p_sys->vid_picture;
1177 p_sys->i_fourcc = 0;
1179 psz = var_CreateGetString( p_demux, "v4l-chroma" );
1180 if( strlen( psz ) >= 4 )
1182 vid_picture.palette = 0;
1183 int i_chroma = VLC_FOURCC( psz[0], psz[1], psz[2], psz[3] );
1185 /* Find out v4l chroma code */
1186 for( i = 0; v4lchroma_to_fourcc[i].i_v4l != 0; i++ )
1188 if( v4lchroma_to_fourcc[i].i_fourcc == i_chroma )
1190 vid_picture.palette = v4lchroma_to_fourcc[i].i_v4l;
1197 if( vid_picture.palette &&
1198 !ioctl( i_fd, VIDIOCSPICT, &vid_picture ) )
1200 p_sys->vid_picture = vid_picture;
1204 /* Try to set the format to something easy to encode */
1205 vid_picture.palette = VIDEO_PALETTE_YUV420P;
1206 if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
1208 p_sys->vid_picture = vid_picture;
1212 vid_picture.palette = VIDEO_PALETTE_YUV422P;
1213 if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
1215 p_sys->vid_picture = vid_picture;
1220 /* Find out final format */
1221 for( i = 0; v4lchroma_to_fourcc[i].i_v4l != 0; i++ )
1223 if( v4lchroma_to_fourcc[i].i_v4l == p_sys->vid_picture.palette)
1225 p_sys->i_fourcc = v4lchroma_to_fourcc[i].i_fourcc;
1232 msg_Err( p_demux, "ioctl VIDIOCGPICT failed" );
1237 if( p_sys->b_mjpeg )
1241 p_sys->mjpeg_buffers.count = 8;
1242 p_sys->mjpeg_buffers.size = MJPEG_BUFFER_SIZE;
1244 if( ioctl( i_fd, MJPIOC_REQBUFS, &p_sys->mjpeg_buffers ) < 0 )
1246 msg_Err( p_demux, "mmap unsupported" );
1250 p_sys->p_video_mmap = mmap( 0,
1251 p_sys->mjpeg_buffers.size * p_sys->mjpeg_buffers.count,
1252 PROT_READ | PROT_WRITE, MAP_SHARED, i_fd, 0 );
1253 if( p_sys->p_video_mmap == MAP_FAILED )
1255 msg_Err( p_demux, "mmap failed" );
1259 p_sys->i_fourcc = VLC_FOURCC( 'm','j','p','g' );
1260 p_sys->i_frame_pos = -1;
1262 /* queue up all the frames */
1263 for( i = 0; i < (int)p_sys->mjpeg_buffers.count; i++ )
1265 if( ioctl( i_fd, MJPIOC_QBUF_CAPT, &i ) < 0 )
1267 msg_Err( p_demux, "unable to queue frame" );
1274 /* Fill in picture_t fields */
1275 vout_InitPicture( VLC_OBJECT(p_demux), &p_sys->pic, p_sys->i_fourcc,
1276 p_sys->i_width, p_sys->i_height, p_sys->i_width *
1277 VOUT_ASPECT_FACTOR / p_sys->i_height );
1278 if( !p_sys->pic.i_planes )
1280 msg_Err( p_demux, "unsupported chroma" );
1283 p_sys->i_video_frame_size = 0;
1284 for( i = 0; i < p_sys->pic.i_planes; i++ )
1286 p_sys->i_video_frame_size += p_sys->pic.p[i].i_visible_lines *
1287 p_sys->pic.p[i].i_visible_pitch;
1290 msg_Dbg( p_demux, "v4l device uses frame size: %i",
1291 p_sys->i_video_frame_size );
1292 msg_Dbg( p_demux, "v4l device uses chroma: %4.4s",
1293 (char*)&p_sys->i_fourcc );
1295 /* Allocate mmap buffer */
1296 if( ioctl( i_fd, VIDIOCGMBUF, &p_sys->vid_mbuf ) < 0 )
1298 msg_Err( p_demux, "mmap unsupported" );
1302 p_sys->p_video_mmap = mmap( 0, p_sys->vid_mbuf.size,
1303 PROT_READ|PROT_WRITE, MAP_SHARED,
1305 if( p_sys->p_video_mmap == MAP_FAILED )
1307 /* FIXME -> normal read */
1308 msg_Err( p_demux, "mmap failed" );
1313 p_sys->vid_mmap.frame = 0;
1314 p_sys->vid_mmap.width = p_sys->i_width;
1315 p_sys->vid_mmap.height = p_sys->i_height;
1316 p_sys->vid_mmap.format = p_sys->vid_picture.palette;
1317 if( ioctl( i_fd, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 )
1319 msg_Warn( p_demux, "%4.4s refused", (char*)&p_sys->i_fourcc );
1320 msg_Err( p_demux, "chroma selection failed" );
1328 if( i_fd >= 0 ) close( i_fd );
1332 /*****************************************************************************
1334 *****************************************************************************/
1335 static int OpenAudioDev( demux_t *p_demux, char *psz_device )
1337 demux_sys_t *p_sys = p_demux->p_sys;
1340 if( (i_fd = open( psz_device, O_RDONLY | O_NONBLOCK )) < 0 )
1342 msg_Err( p_demux, "cannot open audio device (%s)", strerror( errno ) );
1346 i_format = AFMT_S16_LE;
1347 if( ioctl( i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
1348 || i_format != AFMT_S16_LE )
1350 msg_Err( p_demux, "cannot set audio format (16b little endian) "
1351 "(%s)", strerror( errno ) );
1355 if( ioctl( i_fd, SNDCTL_DSP_STEREO,
1356 &p_sys->b_stereo ) < 0 )
1358 msg_Err( p_demux, "cannot set audio channels count (%s)",
1359 strerror( errno ) );
1363 if( ioctl( i_fd, SNDCTL_DSP_SPEED,
1364 &p_sys->i_sample_rate ) < 0 )
1366 msg_Err( p_demux, "cannot set audio sample rate (%s)",
1367 strerror( errno ) );
1371 msg_Dbg( p_demux, "openened adev=`%s' %s %dHz",
1372 psz_device, p_sys->b_stereo ? "stereo" : "mono",
1373 p_sys->i_sample_rate );
1375 p_sys->i_audio_max_frame_size = 6 * 1024;
1381 if( i_fd >= 0 ) close( i_fd );
1385 /*****************************************************************************
1386 * GrabAudio: grab audio
1387 *****************************************************************************/
1388 static block_t *GrabAudio( demux_t *p_demux )
1390 demux_sys_t *p_sys = p_demux->p_sys;
1391 struct audio_buf_info buf_info;
1392 int i_read, i_correct;
1395 if( p_sys->p_block_audio ) p_block = p_sys->p_block_audio;
1396 else p_block = block_New( p_demux, p_sys->i_audio_max_frame_size );
1400 msg_Warn( p_demux, "cannot get block" );
1404 p_sys->p_block_audio = p_block;
1406 i_read = read( p_sys->fd_audio, p_block->p_buffer,
1407 p_sys->i_audio_max_frame_size );
1409 if( i_read <= 0 ) return 0;
1411 p_block->i_buffer = i_read;
1412 p_sys->p_block_audio = 0;
1414 /* Correct the date because of kernel buffering */
1416 if( ioctl( p_sys->fd_audio, SNDCTL_DSP_GETISPACE, &buf_info ) == 0 )
1418 i_correct += buf_info.bytes;
1421 p_block->i_pts = p_block->i_dts =
1422 mdate() - I64C(1000000) * (mtime_t)i_correct /
1423 2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate;
1428 /*****************************************************************************
1430 *****************************************************************************/
1431 static uint8_t *GrabCapture( demux_t *p_demux )
1433 demux_sys_t *p_sys = p_demux->p_sys;
1434 int i_captured_frame = p_sys->i_frame_pos;
1436 p_sys->vid_mmap.frame = (p_sys->i_frame_pos + 1) % p_sys->vid_mbuf.frames;
1438 while( ioctl( p_sys->fd_video, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 )
1440 if( errno != EAGAIN )
1442 msg_Err( p_demux, "failed capturing new frame" );
1446 if( p_demux->b_die )
1451 msg_Dbg( p_demux, "grab failed, trying again" );
1454 while( ioctl(p_sys->fd_video, VIDIOCSYNC, &p_sys->i_frame_pos) < 0 )
1456 if( errno != EAGAIN && errno != EINTR )
1458 msg_Err( p_demux, "failed syncing new frame" );
1463 p_sys->i_frame_pos = p_sys->vid_mmap.frame;
1464 /* leave i_video_frame_size alone */
1465 return p_sys->p_video_mmap + p_sys->vid_mbuf.offsets[i_captured_frame];
1468 static uint8_t *GrabMJPEG( demux_t *p_demux )
1470 demux_sys_t *p_sys = p_demux->p_sys;
1471 struct mjpeg_sync sync;
1472 uint8_t *p_frame, *p_field, *p;
1475 struct quicktime_mjpeg_app1 *p_app1 = NULL;
1477 /* re-queue the last frame we sync'd */
1478 if( p_sys->i_frame_pos != -1 )
1480 while( ioctl( p_sys->fd_video, MJPIOC_QBUF_CAPT,
1481 &p_sys->i_frame_pos ) < 0 )
1483 if( errno != EAGAIN && errno != EINTR )
1485 msg_Err( p_demux, "failed capturing new frame" );
1491 /* sync on the next frame */
1492 while( ioctl( p_sys->fd_video, MJPIOC_SYNC, &sync ) < 0 )
1494 if( errno != EAGAIN && errno != EINTR )
1496 msg_Err( p_demux, "failed syncing new frame" );
1501 p_sys->i_frame_pos = sync.frame;
1502 p_frame = p_sys->p_video_mmap + p_sys->mjpeg_buffers.size * sync.frame;
1504 /* p_frame now points to the data. fix up the Quicktime APP1 marker */
1506 tag = hton16( tag );
1510 p = memmem( p_field, sync.length, &tag, 2 );
1514 p += 2; /* data immediately following EOI */
1516 p_app1 = (struct quicktime_mjpeg_app1 *)(p_field + 6);
1518 i_size = ((uint32_t)(p - p_field));
1519 i_size = hton32( i_size );
1520 memcpy( &p_app1->i_field_size, &i_size, 4 );
1522 while( *p == 0xff && *(p+1) == 0xff )
1525 i_size = ((uint32_t)(p - p_field));
1526 i_size = hton32( i_size );
1527 memcpy( &p_app1->i_padded_field_size, &i_size, 4 );
1531 tag = hton16( tag );
1532 p_field = memmem( p, sync.length - (size_t)(p - p_frame), &tag, 2 );
1536 i_size = (uint32_t)(p_field - p_frame);
1537 i_size = hton32( i_size );
1538 memcpy( &p_app1->i_next_field, &i_size, 4 );
1541 p_app1 = (struct quicktime_mjpeg_app1 *)(p_field + 6);
1543 tag = hton16( tag );
1544 p = memmem( p_field, sync.length - (size_t)(p_field - p_frame),
1549 /* sometimes the second field doesn't have the EOI. just put it
1552 p = p_frame + sync.length;
1553 memcpy( p, &tag, 2 );
1558 i_size = (uint32_t)(p - p_field);
1559 i_size = hton32( i_size );
1560 memcpy( &p_app1->i_field_size, &i_size, 4 );
1561 i_size = (uint32_t)(sync.length - (uint32_t)(p_field - p_frame));
1562 i_size = hton32( i_size );
1563 memcpy( &p_app1->i_padded_field_size, &i_size, 4 );
1566 p_sys->i_video_frame_size = sync.length;
1570 static block_t *GrabVideo( demux_t *p_demux )
1572 demux_sys_t *p_sys = p_demux->p_sys;
1576 if( p_sys->f_fps >= 0.1 && p_sys->i_video_pts > 0 )
1578 mtime_t i_dur = (mtime_t)((double)1000000 / (double)p_sys->f_fps);
1580 /* Did we wait long enough ? (frame rate reduction) */
1581 if( p_sys->i_video_pts + i_dur > mdate() ) return 0;
1584 if( p_sys->b_mjpeg ) p_frame = GrabMJPEG( p_demux );
1585 else p_frame = GrabCapture( p_demux );
1587 if( !p_frame ) return 0;
1589 if( !( p_block = block_New( p_demux, p_sys->i_video_frame_size ) ) )
1591 msg_Warn( p_demux, "cannot get block" );
1595 memcpy( p_block->p_buffer, p_frame, p_sys->i_video_frame_size );
1596 p_sys->i_video_pts = p_block->i_pts = p_block->i_dts = mdate();