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_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>
42 #include <sys/ioctl.h>
45 #include <arpa/inet.h>
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.
62 #include <linux/videodev.h>
63 #include "videodev_mjpeg.h"
65 /*****************************************************************************
67 *****************************************************************************/
68 static int Open ( vlc_object_t * );
69 static void Close( vlc_object_t * );
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)." )
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." )
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") };
138 #define V4L_DEFAULT "/dev/video"
141 set_shortname( N_("Video4Linux") )
142 set_description( N_("Video4Linux input") )
143 set_category( CAT_INPUT )
144 set_subcategory( SUBCAT_INPUT_ACCESS )
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,
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,
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,
159 change_integer_list( i_norm_list, psz_norm_list_text, NULL );
160 add_integer( "v4l-frequency", -1, NULL, FREQUENCY_TEXT, FREQUENCY_LONGTEXT,
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,
167 add_integer( "v4l-brightness", -1, NULL, BRIGHTNESS_TEXT,
168 BRIGHTNESS_LONGTEXT, true )
169 add_integer( "v4l-colour", -1, NULL, COLOUR_TEXT, COLOUR_LONGTEXT,
171 add_integer( "v4l-hue", -1, NULL, HUE_TEXT, HUE_LONGTEXT, true )
172 add_integer( "v4l-contrast", -1, NULL, CONTRAST_TEXT, CONTRAST_LONGTEXT,
174 add_bool( "v4l-mjpeg", false, NULL, MJPEG_TEXT, MJPEG_LONGTEXT,
176 add_integer( "v4l-decimation", 1, NULL, DECIMATION_TEXT,
177 DECIMATION_LONGTEXT, true )
178 add_integer( "v4l-quality", 100, NULL, QUALITY_TEXT, QUALITY_LONGTEXT,
181 add_shortcut( "v4l" )
182 set_capability( "access_demux", 10 )
183 set_callbacks( Open, Close )
186 /*****************************************************************************
187 * Access: local prototypes
188 *****************************************************************************/
189 static int Demux ( demux_t * );
190 static int Control( demux_t *, int, va_list );
192 static void ParseMRL ( demux_t * );
193 static int OpenVideoDev( demux_t *, char * );
194 static block_t *GrabVideo( demux_t * );
196 #define MJPEG_BUFFER_SIZE (256*1024)
198 struct quicktime_mjpeg_app1
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 */
215 vlc_fourcc_t i_fourcc;
217 } v4lchroma_to_fourcc[] =
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 },
240 char *psz_device; /* Main device from MRL */
243 /* Video properties */
260 float f_fps; /* <= 0.0 mean to grab at full rate */
261 mtime_t i_video_pts; /* only used when f_fps > 0 */
267 struct video_capability vid_cap;
268 struct video_mbuf vid_mbuf;
269 struct mjpeg_requestbuffers mjpeg_buffers;
271 uint8_t *p_video_mmap;
274 struct video_mmap vid_mmap;
275 struct video_picture vid_picture;
277 int i_video_frame_size;
281 /*****************************************************************************
282 * Open: opens v4l device
283 *****************************************************************************
285 * url: <video device>::::
287 *****************************************************************************/
288 static int Open( vlc_object_t *p_this )
290 demux_t *p_demux = (demux_t*)p_this;
293 /* Only when selected */
294 if( *p_demux->psz_access == '\0' )
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 ) );
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" );
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" );
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" );
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" );
327 p_sys->psz_device = NULL;
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 )
343 msg_Dbg( p_demux, "v4l grabbing started" );
345 /* Declare elementary streams */
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;
352 /* Setup rgb mask for RGB formats */
353 switch( p_sys->i_fourcc )
355 case VLC_CODEC_RGB15:
356 fmt.video.i_rmask = 0x001f;
357 fmt.video.i_gmask = 0x03e0;
358 fmt.video.i_bmask = 0x7c00;
360 case VLC_CODEC_RGB16:
361 fmt.video.i_rmask = 0x001f;
362 fmt.video.i_gmask = 0x07e0;
363 fmt.video.i_bmask = 0xf800;
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;
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 );
377 /* Update default_pts to a suitable value for access */
378 var_Create( p_demux, "v4l-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
383 /*****************************************************************************
384 * Close: close device, free resources
385 *****************************************************************************/
386 static void Close( vlc_object_t *p_this )
388 demux_t *p_demux = (demux_t *)p_this;
389 demux_sys_t *p_sys = p_demux->p_sys;
391 free( p_sys->psz_device );
392 if( p_sys->i_fd >= 0 ) close( p_sys->i_fd );
397 ioctl( p_sys->i_fd, MJPIOC_QBUF_CAPT, &i_noframe );
400 if( p_sys->p_video_mmap && p_sys->p_video_mmap != MAP_FAILED )
403 munmap( p_sys->p_video_mmap, p_sys->mjpeg_buffers.size *
404 p_sys->mjpeg_buffers.count );
406 munmap( p_sys->p_video_mmap, p_sys->vid_mbuf.size );
412 /*****************************************************************************
414 *****************************************************************************/
415 static int Control( demux_t *p_demux, int i_query, va_list args )
422 /* Special for access_demux */
423 case DEMUX_CAN_PAUSE:
425 case DEMUX_SET_PAUSE_STATE:
426 case DEMUX_CAN_CONTROL_PACE:
427 pb = (bool*)va_arg( args, bool * );
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;
437 pi64 = (int64_t*)va_arg( args, int64_t * );
441 /* TODO implement others */
449 /*****************************************************************************
451 *****************************************************************************/
452 static int Demux( demux_t *p_demux )
454 demux_sys_t *p_sys = p_demux->p_sys;
456 block_t *p_block = GrabVideo( p_demux );
460 msleep( 10000 ); /* Unfortunately v4l doesn't allow polling */
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 );
470 /*****************************************************************************
471 * ParseMRL: parse the options contained in the MRL
472 *****************************************************************************/
473 static void ParseMRL( demux_t *p_demux )
475 demux_sys_t *p_sys = p_demux->p_sys;
477 char *psz_dup = strdup( p_demux->psz_path );
478 char *psz_parser = psz_dup;
480 while( *psz_parser && *psz_parser != ':' )
485 if( *psz_parser == ':' )
490 *psz_parser++ = '\0';
491 if( !strncmp( psz_parser, "channel=", strlen( "channel=" ) ) )
493 p_sys->i_channel = strtol( psz_parser + strlen( "channel=" ),
496 else if( !strncmp( psz_parser, "norm=", strlen( "norm=" ) ) )
498 psz_parser += strlen( "norm=" );
499 if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
501 p_sys->i_norm = VIDEO_MODE_PAL;
502 psz_parser += strlen( "pal" );
504 else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
506 p_sys->i_norm = VIDEO_MODE_NTSC;
507 psz_parser += strlen( "ntsc" );
509 else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
511 p_sys->i_norm = VIDEO_MODE_SECAM;
512 psz_parser += strlen( "secam" );
514 else if( !strncmp( psz_parser, "auto", strlen( "auto" ) ) )
516 p_sys->i_norm = VIDEO_MODE_AUTO;
517 psz_parser += strlen( "auto" );
521 p_sys->i_norm = strtol( psz_parser, &psz_parser, 0 );
524 else if( !strncmp( psz_parser, "frequency=",
525 strlen( "frequency=" ) ) )
528 strtol( psz_parser + strlen( "frequency=" ),
530 if( p_sys->i_frequency < 30000 )
532 msg_Warn( p_demux, "v4l syntax has changed : "
533 "'frequency' is now channel frequency in kHz");
536 else if( !strncmp( psz_parser, "audio=", strlen( "audio=" ) ) )
538 p_sys->i_audio = strtol( psz_parser + strlen( "audio=" ),
541 else if( !strncmp( psz_parser, "size=", strlen( "size=" ) ) )
543 psz_parser += strlen( "size=" );
544 if( !strncmp( psz_parser, "subqcif", strlen( "subqcif" ) ) )
546 p_sys->i_width = 128;
547 p_sys->i_height = 96;
549 else if( !strncmp( psz_parser, "qsif", strlen( "qsif" ) ) )
551 p_sys->i_width = 160;
552 p_sys->i_height = 120;
554 else if( !strncmp( psz_parser, "qcif", strlen( "qcif" ) ) )
556 p_sys->i_width = 176;
557 p_sys->i_height = 144;
559 else if( !strncmp( psz_parser, "sif", strlen( "sif" ) ) )
561 p_sys->i_width = 320;
562 p_sys->i_height = 244;
564 else if( !strncmp( psz_parser, "cif", strlen( "cif" ) ) )
566 p_sys->i_width = 352;
567 p_sys->i_height = 288;
569 else if( !strncmp( psz_parser, "vga", strlen( "vga" ) ) )
571 p_sys->i_width = 640;
572 p_sys->i_height = 480;
577 p_sys->i_width = strtol( psz_parser, &psz_parser, 0 );
578 if( *psz_parser == 'x' || *psz_parser == 'X')
580 p_sys->i_height = strtol( psz_parser + 1,
583 msg_Dbg( p_demux, "WxH %dx%d", p_sys->i_width,
587 else if( !strncmp( psz_parser, "brightness=", strlen( "brightness=" ) ) )
589 p_sys->i_brightness = strtol( psz_parser + strlen( "brightness=" ),
592 else if( !strncmp( psz_parser, "colour=", strlen( "colour=" ) ) )
594 p_sys->i_colour = strtol( psz_parser + strlen( "colour=" ),
597 else if( !strncmp( psz_parser, "hue=", strlen( "hue=" ) ) )
599 p_sys->i_hue = strtol( psz_parser + strlen( "hue=" ),
602 else if( !strncmp( psz_parser, "contrast=", strlen( "contrast=" ) ) )
604 p_sys->i_contrast = strtol( psz_parser + strlen( "contrast=" ),
607 else if( !strncmp( psz_parser, "tuner=", strlen( "tuner=" ) ) )
609 p_sys->i_tuner = strtol( psz_parser + strlen( "tuner=" ),
612 else if( !strncmp( psz_parser, "mjpeg", strlen( "mjpeg" ) ) )
614 psz_parser += strlen( "mjpeg" );
616 p_sys->b_mjpeg = true;
618 else if( !strncmp( psz_parser, "decimation=",
619 strlen( "decimation=" ) ) )
621 p_sys->i_decimation =
622 strtol( psz_parser + strlen( "decimation=" ),
625 else if( !strncmp( psz_parser, "quality=",
626 strlen( "quality=" ) ) )
629 strtol( psz_parser + strlen( "quality=" ),
632 else if( !strncmp( psz_parser, "fps=", strlen( "fps=" ) ) )
634 p_sys->f_fps = strtof( psz_parser + strlen( "fps=" ),
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" ) ) )
642 if( strchr( psz_parser, ':' ) )
643 psz_parser = strchr( psz_parser, ':' );
645 psz_parser += strlen( psz_parser );
646 msg_Err( p_demux, AUDIO_DEPRECATED_ERROR );
650 msg_Warn( p_demux, "unknown option" );
653 while( *psz_parser && *psz_parser != ':' )
658 if( *psz_parser == '\0' )
666 p_sys->psz_device = strdup( psz_dup );
668 p_sys->psz_device = strdup( V4L_DEFAULT );
672 /*****************************************************************************
674 *****************************************************************************/
675 static int OpenVideoDev( demux_t *p_demux, char *psz_device )
677 demux_sys_t *p_sys = p_demux->p_sys;
680 struct video_channel vid_channel;
681 struct mjpeg_params mjpeg;
684 if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
686 msg_Err( p_demux, "cannot open device (%m)" );
690 if( ioctl( i_fd, VIDIOCGCAP, &p_sys->vid_cap ) < 0 )
692 msg_Err( p_demux, "cannot get capabilities (%m)" );
697 "V4L device %s %d channels %d audios %d < w < %d %d < h < %d",
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 );
704 if( p_sys->i_channel < 0 || p_sys->i_channel >= p_sys->vid_cap.channels )
706 msg_Dbg( p_demux, "invalid channel, falling back on channel 0" );
707 p_sys->i_channel = 0;
709 if( p_sys->vid_cap.audios && p_sys->i_audio >= p_sys->vid_cap.audios )
711 msg_Dbg( p_demux, "invalid audio, falling back with no audio" );
715 if( p_sys->i_width < p_sys->vid_cap.minwidth ||
716 p_sys->i_width > p_sys->vid_cap.maxwidth )
718 msg_Dbg( p_demux, "invalid width %i", p_sys->i_width );
721 if( p_sys->i_height < p_sys->vid_cap.minheight ||
722 p_sys->i_height > p_sys->vid_cap.maxheight )
724 msg_Dbg( p_demux, "invalid height %i", p_sys->i_height );
728 if( !( p_sys->vid_cap.type & VID_TYPE_CAPTURE ) )
730 msg_Err( p_demux, "cannot grab" );
734 vid_channel.channel = p_sys->i_channel;
735 if( ioctl( i_fd, VIDIOCGCHAN, &vid_channel ) < 0 )
737 msg_Err( p_demux, "cannot get channel infos (%m)" );
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 );
745 if( p_sys->i_tuner >= vid_channel.tuners )
747 msg_Dbg( p_demux, "invalid tuner, falling back on tuner 0" );
751 vid_channel.norm = p_sys->i_norm;
752 if( ioctl( i_fd, VIDIOCSCHAN, &vid_channel ) < 0 )
754 msg_Err( p_demux, "cannot set channel (%m)" );
758 if( vid_channel.flags & VIDEO_VC_TUNER )
763 struct video_tuner vid_tuner;
764 if( p_sys->i_tuner >= 0 )
766 vid_tuner.tuner = p_sys->i_tuner;
767 if( ioctl( i_fd, VIDIOCGTUNER, &vid_tuner ) < 0 )
769 msg_Err( p_demux, "cannot get tuner (%m)" );
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 );
777 msg_Dbg( p_demux, "setting tuner %s (%d)",
778 vid_tuner.name, vid_tuner.tuner );
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 )
784 msg_Err( p_demux, "cannot set tuner (%m)" );
790 /* Show a warning if frequency is < than 30000.
791 * User is certainly usint old syntax. */
795 if( p_sys->i_frequency >= 0 )
797 int driver_frequency = p_sys->i_frequency * 16 /1000;
798 if( ioctl( i_fd, VIDIOCSFREQ, &driver_frequency ) < 0 )
800 msg_Err( p_demux, "cannot set frequency (%m)" );
803 msg_Dbg( p_demux, "frequency %d (%d)", p_sys->i_frequency,
809 if( vid_channel.flags & VIDEO_VC_AUDIO )
811 struct video_audio vid_audio;
813 /* XXX TODO volume, balance, ... */
814 if( p_sys->i_audio >= 0 )
816 vid_audio.audio = p_sys->i_audio;
817 if( ioctl( i_fd, VIDIOCGAUDIO, &vid_audio ) < 0 )
819 msg_Err( p_demux, "cannot get audio (%m)" );
824 vid_audio.flags &= ~VIDEO_AUDIO_MUTE;
826 if( ioctl( i_fd, VIDIOCSAUDIO, &vid_audio ) < 0 )
828 msg_Err( p_demux, "cannot set audio (%m)" );
835 /* establish basic params with input and norm before feeling width
839 struct quicktime_mjpeg_app1 *p_app1;
842 if( ioctl( i_fd, MJPIOC_G_PARAMS, &mjpeg ) < 0 )
844 msg_Err( p_demux, "cannot get mjpeg params (%m)" );
847 mjpeg.input = p_sys->i_channel;
848 mjpeg.norm = p_sys->i_norm;
849 mjpeg.decimation = p_sys->i_decimation;
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;
856 /* establish Quicktime APP1 marker while we are here */
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
874 * This is really something the driver could do for us because this
875 * does conform to standards outside of Apple Quicktime.
878 p_app1->i_DQT_offset = hton32( i_offset );
880 p_app1->i_DHT_offset = hton32( i_offset );
882 p_app1->i_SOF_offset = hton32( i_offset );
884 p_app1->i_SOS_offset = hton32( i_offset );
886 p_app1->i_data_offset = hton32( i_offset );
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;
892 if( ioctl( i_fd, MJPIOC_S_PARAMS, &mjpeg ) < 0 )
894 msg_Err( p_demux, "cannot set mjpeg params (%m)" );
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;
903 /* fix width/height */
904 if( !p_sys->b_mjpeg && ( p_sys->i_width == 0 || p_sys->i_height == 0 ) )
906 struct video_window vid_win;
908 if( ioctl( i_fd, VIDIOCGWIN, &vid_win ) < 0 )
910 msg_Err( p_demux, "cannot get win (%m)" );
913 p_sys->i_width = vid_win.width;
914 p_sys->i_height = vid_win.height;
916 if( !p_sys->i_width || !p_sys->i_height )
918 p_sys->i_width = p_sys->vid_cap.maxwidth;
919 p_sys->i_height = p_sys->vid_cap.maxheight;
922 if( !p_sys->i_width || !p_sys->i_height )
924 msg_Err( p_demux, "invalid video size (%ix%i)",
925 p_sys->i_width, p_sys->i_height );
929 msg_Dbg( p_demux, "will use %dx%d", p_sys->i_width, p_sys->i_height );
932 if( !p_sys->b_mjpeg )
934 /* set hue/color/.. */
935 if( ioctl( i_fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 )
937 struct video_picture vid_picture = p_sys->vid_picture;
939 if( p_sys->i_brightness >= 0 && p_sys->i_brightness < 65536 )
941 vid_picture.brightness = p_sys->i_brightness;
943 if( p_sys->i_colour >= 0 && p_sys->i_colour < 65536 )
945 vid_picture.colour = p_sys->i_colour;
947 if( p_sys->i_hue >= 0 && p_sys->i_hue < 65536 )
949 vid_picture.hue = p_sys->i_hue;
951 if( p_sys->i_contrast >= 0 && p_sys->i_contrast < 65536 )
953 vid_picture.contrast = p_sys->i_contrast;
955 if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
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;
968 /* Find out video format used by device */
969 if( ioctl( i_fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 )
971 struct video_picture vid_picture = p_sys->vid_picture;
977 psz = var_CreateGetString( p_demux, "v4l-chroma" );
979 const vlc_fourcc_t i_chroma =
980 vlc_fourcc_GetCodecFromString( VIDEO_ES, psz );
983 vid_picture.palette = 0;
985 /* Find out v4l chroma code */
986 for( i = 0; v4lchroma_to_fourcc[i].i_v4l != 0; i++ )
988 if( v4lchroma_to_fourcc[i].i_fourcc == i_chroma )
990 vid_picture.palette = v4lchroma_to_fourcc[i].i_v4l;
997 if( vid_picture.palette &&
998 !ioctl( i_fd, VIDIOCSPICT, &vid_picture ) )
1000 p_sys->vid_picture = vid_picture;
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 )
1008 p_sys->vid_picture = vid_picture;
1012 vid_picture.palette = VIDEO_PALETTE_YUV422P;
1013 if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
1015 p_sys->vid_picture = vid_picture;
1020 /* Find out final format */
1021 for( i = 0; v4lchroma_to_fourcc[i].i_v4l != 0; i++ )
1023 if( v4lchroma_to_fourcc[i].i_v4l == p_sys->vid_picture.palette)
1025 p_sys->i_fourcc = v4lchroma_to_fourcc[i].i_fourcc;
1032 msg_Err( p_demux, "ioctl VIDIOCGPICT failed" );
1037 if( p_sys->b_mjpeg )
1041 p_sys->mjpeg_buffers.count = 8;
1042 p_sys->mjpeg_buffers.size = MJPEG_BUFFER_SIZE;
1044 if( ioctl( i_fd, MJPIOC_REQBUFS, &p_sys->mjpeg_buffers ) < 0 )
1046 msg_Err( p_demux, "mmap unsupported" );
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 )
1055 msg_Err( p_demux, "mmap failed" );
1059 p_sys->i_fourcc = VLC_CODEC_MJPG;
1060 p_sys->i_frame_pos = -1;
1062 /* queue up all the frames */
1063 for( i = 0; i < (int)p_sys->mjpeg_buffers.count; i++ )
1065 if( ioctl( i_fd, MJPIOC_QBUF_CAPT, &i ) < 0 )
1067 msg_Err( p_demux, "unable to queue frame" );
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 ) )
1079 msg_Err( p_demux, "unsupported chroma" );
1082 p_sys->i_video_frame_size = 0;
1083 for( i = 0; i < p_sys->pic.i_planes; i++ )
1085 p_sys->i_video_frame_size += p_sys->pic.p[i].i_visible_lines *
1086 p_sys->pic.p[i].i_visible_pitch;
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 );
1094 /* Allocate mmap buffer */
1095 if( ioctl( i_fd, VIDIOCGMBUF, &p_sys->vid_mbuf ) < 0 )
1097 msg_Err( p_demux, "mmap unsupported" );
1101 p_sys->p_video_mmap = mmap( 0, p_sys->vid_mbuf.size,
1102 PROT_READ|PROT_WRITE, MAP_SHARED,
1104 if( p_sys->p_video_mmap == MAP_FAILED )
1106 /* FIXME -> normal read */
1107 msg_Err( p_demux, "mmap failed" );
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 )
1118 msg_Warn( p_demux, "%4.4s refused", (char*)&p_sys->i_fourcc );
1119 msg_Err( p_demux, "chroma selection failed" );
1127 if( i_fd >= 0 ) close( i_fd );
1131 /*****************************************************************************
1133 *****************************************************************************/
1134 static uint8_t *GrabCapture( demux_t *p_demux )
1136 demux_sys_t *p_sys = p_demux->p_sys;
1137 int i_captured_frame = p_sys->i_frame_pos;
1139 p_sys->vid_mmap.frame = (p_sys->i_frame_pos + 1) % p_sys->vid_mbuf.frames;
1141 while( ioctl( p_sys->i_fd, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 )
1143 if( errno != EAGAIN )
1145 msg_Err( p_demux, "failed capturing new frame" );
1149 if( !vlc_object_alive (p_demux) )
1154 msg_Dbg( p_demux, "grab failed, trying again" );
1157 while( ioctl(p_sys->i_fd, VIDIOCSYNC, &p_sys->i_frame_pos) < 0 )
1159 if( errno != EAGAIN && errno != EINTR )
1161 msg_Err( p_demux, "failed syncing new frame" );
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];
1171 static uint8_t *GrabMJPEG( demux_t *p_demux )
1173 demux_sys_t *p_sys = p_demux->p_sys;
1174 struct mjpeg_sync sync;
1175 uint8_t *p_frame, *p_field, *p;
1178 struct quicktime_mjpeg_app1 *p_app1 = NULL;
1180 /* re-queue the last frame we sync'd */
1181 if( p_sys->i_frame_pos != -1 )
1183 while( ioctl( p_sys->i_fd, MJPIOC_QBUF_CAPT,
1184 &p_sys->i_frame_pos ) < 0 )
1186 if( errno != EAGAIN && errno != EINTR )
1188 msg_Err( p_demux, "failed capturing new frame" );
1194 /* sync on the next frame */
1195 while( ioctl( p_sys->i_fd, MJPIOC_SYNC, &sync ) < 0 )
1197 if( errno != EAGAIN && errno != EINTR )
1199 msg_Err( p_demux, "failed syncing new frame" );
1204 p_sys->i_frame_pos = sync.frame;
1205 p_frame = p_sys->p_video_mmap + p_sys->mjpeg_buffers.size * sync.frame;
1207 /* p_frame now points to the data. fix up the Quicktime APP1 marker */
1209 tag = hton16( tag );
1213 p = memmem( p_field, sync.length, &tag, 2 );
1217 p += 2; /* data immediately following EOI */
1219 p_app1 = (struct quicktime_mjpeg_app1 *)(p_field + 6);
1221 i_size = ((uint32_t)(p - p_field));
1222 i_size = hton32( i_size );
1223 memcpy( &p_app1->i_field_size, &i_size, 4 );
1225 while( *p == 0xff && *(p+1) == 0xff )
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 );
1234 tag = hton16( tag );
1235 p_field = memmem( p, sync.length - (size_t)(p - p_frame), &tag, 2 );
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 );
1244 p_app1 = (struct quicktime_mjpeg_app1 *)(p_field + 6);
1246 tag = hton16( tag );
1247 p = memmem( p_field, sync.length - (size_t)(p_field - p_frame),
1252 /* sometimes the second field doesn't have the EOI. just put it
1255 p = p_frame + sync.length;
1256 memcpy( p, &tag, 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 );
1269 p_sys->i_video_frame_size = sync.length;
1273 static block_t *GrabVideo( demux_t *p_demux )
1275 demux_sys_t *p_sys = p_demux->p_sys;
1279 if( p_sys->f_fps >= 0.1 && p_sys->i_video_pts > 0 )
1281 mtime_t i_dur = (mtime_t)((double)1000000 / (double)p_sys->f_fps);
1283 /* Did we wait long enough ? (frame rate reduction) */
1284 if( p_sys->i_video_pts + i_dur > mdate() ) return 0;
1287 if( p_sys->b_mjpeg ) p_frame = GrabMJPEG( p_demux );
1288 else p_frame = GrabCapture( p_demux );
1290 if( !p_frame ) return 0;
1292 if( !( p_block = block_New( p_demux, p_sys->i_video_frame_size ) ) )
1294 msg_Warn( p_demux, "cannot get block" );
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();