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>
42 #include <sys/ioctl.h>
45 #include <arpa/inet.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 /*****************************************************************************
69 *****************************************************************************/
70 static int Open ( vlc_object_t * );
71 static void Close( vlc_object_t * );
73 #define CACHING_TEXT N_("Caching value in ms")
74 #define CACHING_LONGTEXT N_( \
75 "Caching value for V4L captures. This " \
76 "value should be set in milliseconds." )
77 #define VDEV_TEXT N_("Video device name")
78 #define VDEV_LONGTEXT N_( \
79 "Name of the video device to use. " \
80 "If you don't specify anything, no video device will be used.")
81 #define CHROMA_TEXT N_("Video input chroma format")
82 #define CHROMA_LONGTEXT N_( \
83 "Force the Video4Linux video device to use a specific chroma format " \
84 "(eg. I420 (default), RV24, etc.)")
85 #define FREQUENCY_TEXT N_( "Frequency" )
86 #define FREQUENCY_LONGTEXT N_( \
87 "Frequency to capture (in kHz), if applicable." )
88 #define CHANNEL_TEXT N_( "Channel" )
89 #define CHANNEL_LONGTEXT N_( \
90 "Channel of the card to use (Usually, 0 = tuner, " \
91 "1 = composite, 2 = svideo)." )
92 #define NORM_TEXT N_( "Norm" )
93 #define NORM_LONGTEXT N_( \
94 "Norm of the stream (Automatic, SECAM, PAL, or NTSC)." )
95 #define AUDIO_TEXT N_( "Audio Channel" )
96 #define AUDIO_LONGTEXT N_( \
97 "Audio Channel to use, if there are several audio inputs." )
98 #define WIDTH_TEXT N_( "Width" )
99 #define WIDTH_LONGTEXT N_( "Width of the stream to capture " \
100 "(-1 for autodetect)." )
101 #define HEIGHT_TEXT N_( "Height" )
102 #define HEIGHT_LONGTEXT N_( "Height of the stream to capture " \
103 "(-1 for autodetect)." )
104 #define BRIGHTNESS_TEXT N_( "Brightness" )
105 #define BRIGHTNESS_LONGTEXT N_( \
106 "Brightness of the video input." )
107 #define HUE_TEXT N_( "Hue" )
108 #define HUE_LONGTEXT N_( \
109 "Hue of the video input." )
110 #define COLOUR_TEXT N_( "Color" )
111 #define COLOUR_LONGTEXT N_( \
112 "Color of the video input." )
113 #define CONTRAST_TEXT N_( "Contrast" )
114 #define CONTRAST_LONGTEXT N_( \
115 "Contrast of the video input." )
116 #define TUNER_TEXT N_( "Tuner" )
117 #define TUNER_LONGTEXT N_( "Tuner to use, if there are several ones." )
118 #define MJPEG_TEXT N_( "MJPEG" )
119 #define MJPEG_LONGTEXT N_( \
120 "Set this option if the capture device outputs MJPEG" )
121 #define DECIMATION_TEXT N_( "Decimation" )
122 #define DECIMATION_LONGTEXT N_( \
123 "Decimation level for MJPEG streams" )
124 #define QUALITY_TEXT N_( "Quality" )
125 #define QUALITY_LONGTEXT N_( "Quality of the stream." )
126 #define FPS_TEXT N_( "Framerate" )
127 #define FPS_LONGTEXT N_( "Framerate to capture, if applicable " \
128 "(-1 for autodetect)." )
130 #define AUDIO_DEPRECATED_ERROR N_( \
131 "Alsa or OSS audio capture in the v4l access is deprecated. " \
132 "please use 'v4l:/""/ :input-slave=alsa:/""/' or " \
133 "'v4l:/""/ :input-slave=oss:/""/' instead." )
135 static const int i_norm_list[] =
136 { VIDEO_MODE_AUTO, VIDEO_MODE_SECAM, VIDEO_MODE_PAL, VIDEO_MODE_NTSC };
137 static const char *const psz_norm_list_text[] =
138 { N_("Automatic"), N_("SECAM"), N_("PAL"), N_("NTSC") };
140 #define V4L_DEFAULT "/dev/video"
143 set_shortname( N_("Video4Linux") )
144 set_description( N_("Video4Linux input") )
145 set_category( CAT_INPUT )
146 set_subcategory( SUBCAT_INPUT_ACCESS )
148 add_integer( "v4l-caching", DEFAULT_PTS_DELAY / 1000, NULL,
149 CACHING_TEXT, CACHING_LONGTEXT, true )
150 add_obsolete_string( "v4l-vdev" );
151 add_obsolete_string( "v4l-adev" );
152 add_string( "v4l-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
154 add_float( "v4l-fps", -1.0, NULL, FPS_TEXT, FPS_LONGTEXT, true )
155 add_obsolete_integer( "v4l-samplerate" );
156 add_integer( "v4l-channel", 0, NULL, CHANNEL_TEXT, CHANNEL_LONGTEXT,
158 add_integer( "v4l-tuner", -1, NULL, TUNER_TEXT, TUNER_LONGTEXT, true )
159 add_integer( "v4l-norm", VIDEO_MODE_AUTO, NULL, NORM_TEXT, NORM_LONGTEXT,
161 change_integer_list( i_norm_list, psz_norm_list_text, NULL );
162 add_integer( "v4l-frequency", -1, NULL, FREQUENCY_TEXT, FREQUENCY_LONGTEXT,
164 add_integer( "v4l-audio", -1, NULL, AUDIO_TEXT, AUDIO_LONGTEXT, true )
165 add_obsolete_bool( "v4l-stereo" );
166 add_integer( "v4l-width", 0, NULL, WIDTH_TEXT, WIDTH_LONGTEXT, true )
167 add_integer( "v4l-height", 0, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT,
169 add_integer( "v4l-brightness", -1, NULL, BRIGHTNESS_TEXT,
170 BRIGHTNESS_LONGTEXT, true )
171 add_integer( "v4l-colour", -1, NULL, COLOUR_TEXT, COLOUR_LONGTEXT,
173 add_integer( "v4l-hue", -1, NULL, HUE_TEXT, HUE_LONGTEXT, true )
174 add_integer( "v4l-contrast", -1, NULL, CONTRAST_TEXT, CONTRAST_LONGTEXT,
176 add_bool( "v4l-mjpeg", false, NULL, MJPEG_TEXT, MJPEG_LONGTEXT,
178 add_integer( "v4l-decimation", 1, NULL, DECIMATION_TEXT,
179 DECIMATION_LONGTEXT, true )
180 add_integer( "v4l-quality", 100, NULL, QUALITY_TEXT, QUALITY_LONGTEXT,
183 add_shortcut( "v4l" )
184 set_capability( "access_demux", 10 )
185 set_callbacks( Open, Close )
188 /*****************************************************************************
189 * Access: local prototypes
190 *****************************************************************************/
191 static int Demux ( demux_t * );
192 static int Control( demux_t *, int, va_list );
194 static void ParseMRL ( demux_t * );
195 static int OpenVideoDev( demux_t *, char * );
196 static block_t *GrabVideo( demux_t * );
198 #define MJPEG_BUFFER_SIZE (256*1024)
200 struct quicktime_mjpeg_app1
202 uint32_t i_reserved; /* set to 0 */
203 uint32_t i_tag; /* 'mjpg' */
204 uint32_t i_field_size; /* offset following EOI */
205 uint32_t i_padded_field_size; /* offset following EOI+pad */
206 uint32_t i_next_field; /* offset to next field */
207 uint32_t i_DQT_offset;
208 uint32_t i_DHT_offset;
209 uint32_t i_SOF_offset;
210 uint32_t i_SOS_offset;
211 uint32_t i_data_offset; /* following SOS marker data */
219 } v4lchroma_to_fourcc[] =
221 { VIDEO_PALETTE_GREY, VLC_FOURCC( 'G', 'R', 'E', 'Y' ) },
222 { VIDEO_PALETTE_HI240, VLC_FOURCC( 'I', '2', '4', '0' ) },
223 { VIDEO_PALETTE_RGB565, VLC_FOURCC( 'R', 'V', '1', '6' ) },
224 { VIDEO_PALETTE_RGB555, VLC_FOURCC( 'R', 'V', '1', '5' ) },
225 { VIDEO_PALETTE_RGB24, VLC_FOURCC( 'R', 'V', '2', '4' ) },
226 { VIDEO_PALETTE_RGB32, VLC_FOURCC( 'R', 'V', '3', '2' ) },
227 { VIDEO_PALETTE_YUV422, VLC_FOURCC( 'Y', 'U', 'Y', '2' ) },
228 { VIDEO_PALETTE_YUV422, VLC_FOURCC( 'Y', 'U', 'Y', 'V' ) },
229 { VIDEO_PALETTE_YUYV, VLC_FOURCC( 'Y', 'U', 'Y', '2' ) },
230 { VIDEO_PALETTE_YUYV, VLC_FOURCC( 'Y', 'U', 'Y', 'V' ) },
231 { VIDEO_PALETTE_UYVY, VLC_FOURCC( 'U', 'Y', 'V', 'Y' ) },
232 { VIDEO_PALETTE_YUV420, VLC_FOURCC( 'I', '4', '2', 'N' ) },
233 { VIDEO_PALETTE_YUV411, VLC_FOURCC( 'I', '4', '1', 'N' ) },
234 { VIDEO_PALETTE_RAW, VLC_FOURCC( 'G', 'R', 'A', 'W' ) },
235 { VIDEO_PALETTE_YUV422P, VLC_FOURCC( 'I', '4', '2', '2' ) },
236 { VIDEO_PALETTE_YUV420P, VLC_FOURCC( 'I', '4', '2', '0' ) },
237 { VIDEO_PALETTE_YUV411P, VLC_FOURCC( 'I', '4', '1', '1' ) },
244 char *psz_device; /* Main device from MRL */
247 /* Video properties */
264 float f_fps; /* <= 0.0 mean to grab at full rate */
265 mtime_t i_video_pts; /* only used when f_fps > 0 */
271 struct video_capability vid_cap;
272 struct video_mbuf vid_mbuf;
273 struct mjpeg_requestbuffers mjpeg_buffers;
275 uint8_t *p_video_mmap;
278 struct video_mmap vid_mmap;
279 struct video_picture vid_picture;
281 int i_video_frame_size;
285 /*****************************************************************************
286 * Open: opens v4l device
287 *****************************************************************************
289 * url: <video device>::::
291 *****************************************************************************/
292 static int Open( vlc_object_t *p_this )
294 demux_t *p_demux = (demux_t*)p_this;
297 /* Only when selected */
298 if( *p_demux->psz_access == '\0' )
302 p_demux->pf_demux = Demux;
303 p_demux->pf_control = Control;
304 p_demux->info.i_update = 0;
305 p_demux->info.i_title = 0;
306 p_demux->info.i_seekpoint = 0;
307 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
311 p_sys->i_audio = var_CreateGetInteger( p_demux, "v4l-audio" );
312 p_sys->i_channel = var_CreateGetInteger( p_demux, "v4l-channel" );
313 p_sys->i_norm = var_CreateGetInteger( p_demux, "v4l-norm" );
314 p_sys->i_tuner = var_CreateGetInteger( p_demux, "v4l-tuner" );
315 p_sys->i_frequency = var_CreateGetInteger( p_demux, "v4l-frequency" );
317 p_sys->f_fps = var_CreateGetFloat( p_demux, "v4l-fps" );
318 p_sys->i_width = var_CreateGetInteger( p_demux, "v4l-width" );
319 p_sys->i_height = var_CreateGetInteger( p_demux, "v4l-height" );
320 p_sys->i_video_pts = -1;
321 p_sys->i_brightness = var_CreateGetInteger( p_demux, "v4l-brightness" );
323 p_sys->i_hue = var_CreateGetInteger( p_demux, "v4l-hue" );
324 p_sys->i_colour = var_CreateGetInteger( p_demux, "v4l-colour" );
325 p_sys->i_contrast = var_CreateGetInteger( p_demux, "v4l-contrast" );
327 p_sys->b_mjpeg = var_CreateGetBool( p_demux, "v4l-mjpeg" );
328 p_sys->i_decimation = var_CreateGetInteger( p_demux, "v4l-decimation" );
329 p_sys->i_quality = var_CreateGetInteger( p_demux, "v4l-quality" );
331 p_sys->psz_device = NULL;
338 msg_Dbg( p_this, "opening device '%s'", p_sys->psz_device );
339 p_sys->i_fd = OpenVideoDev( p_demux, p_sys->psz_device );
340 if( p_sys->i_fd < 0 )
347 msg_Dbg( p_demux, "v4l grabbing started" );
349 /* Declare elementary streams */
351 es_format_Init( &fmt, VIDEO_ES, p_sys->i_fourcc );
352 fmt.video.i_width = p_sys->i_width;
353 fmt.video.i_height = p_sys->i_height;
354 fmt.video.i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
356 /* Setup rgb mask for RGB formats */
357 switch( p_sys->i_fourcc )
359 case VLC_FOURCC('R','V','1','5'):
360 fmt.video.i_rmask = 0x001f;
361 fmt.video.i_gmask = 0x03e0;
362 fmt.video.i_bmask = 0x7c00;
364 case VLC_FOURCC('R','V','1','6'):
365 fmt.video.i_rmask = 0x001f;
366 fmt.video.i_gmask = 0x07e0;
367 fmt.video.i_bmask = 0xf800;
369 case VLC_FOURCC('R','V','2','4'):
370 case VLC_FOURCC('R','V','3','2'):
371 fmt.video.i_rmask = 0x00ff0000;
372 fmt.video.i_gmask = 0x0000ff00;
373 fmt.video.i_bmask = 0x000000ff;
377 msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
378 (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
379 p_sys->p_es = es_out_Add( p_demux->out, &fmt );
381 /* Update default_pts to a suitable value for access */
382 var_Create( p_demux, "v4l-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
387 /*****************************************************************************
388 * Close: close device, free resources
389 *****************************************************************************/
390 static void Close( vlc_object_t *p_this )
392 demux_t *p_demux = (demux_t *)p_this;
393 demux_sys_t *p_sys = p_demux->p_sys;
395 free( p_sys->psz_device );
396 if( p_sys->i_fd >= 0 ) close( p_sys->i_fd );
401 ioctl( p_sys->i_fd, MJPIOC_QBUF_CAPT, &i_noframe );
404 if( p_sys->p_video_mmap && p_sys->p_video_mmap != MAP_FAILED )
407 munmap( p_sys->p_video_mmap, p_sys->mjpeg_buffers.size *
408 p_sys->mjpeg_buffers.count );
410 munmap( p_sys->p_video_mmap, p_sys->vid_mbuf.size );
416 /*****************************************************************************
418 *****************************************************************************/
419 static int Control( demux_t *p_demux, int i_query, va_list args )
426 /* Special for access_demux */
427 case DEMUX_CAN_PAUSE:
429 case DEMUX_SET_PAUSE_STATE:
430 case DEMUX_CAN_CONTROL_PACE:
431 pb = (bool*)va_arg( args, bool * );
435 case DEMUX_GET_PTS_DELAY:
436 pi64 = (int64_t*)va_arg( args, int64_t * );
437 *pi64 = (int64_t)var_GetInteger( p_demux, "v4l-caching" ) * 1000;
441 pi64 = (int64_t*)va_arg( args, int64_t * );
445 /* TODO implement others */
453 /*****************************************************************************
455 *****************************************************************************/
456 static int Demux( demux_t *p_demux )
458 demux_sys_t *p_sys = p_demux->p_sys;
462 fd.events = POLLIN|POLLPRI;
466 if( poll( &fd, 1, 500 ) )
468 if( fd.revents & (POLLIN|POLLPRI) )
470 block_t *p_block = GrabVideo( p_demux );
473 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
474 es_out_Send( p_demux->out, p_sys->p_es, p_block );
482 /*****************************************************************************
483 * ParseMRL: parse the options contained in the MRL
484 *****************************************************************************/
485 static void ParseMRL( demux_t *p_demux )
487 demux_sys_t *p_sys = p_demux->p_sys;
489 char *psz_dup = strdup( p_demux->psz_path );
490 char *psz_parser = psz_dup;
492 while( *psz_parser && *psz_parser != ':' )
497 if( *psz_parser == ':' )
502 *psz_parser++ = '\0';
503 if( !strncmp( psz_parser, "channel=", strlen( "channel=" ) ) )
505 p_sys->i_channel = strtol( psz_parser + strlen( "channel=" ),
508 else if( !strncmp( psz_parser, "norm=", strlen( "norm=" ) ) )
510 psz_parser += strlen( "norm=" );
511 if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
513 p_sys->i_norm = VIDEO_MODE_PAL;
514 psz_parser += strlen( "pal" );
516 else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
518 p_sys->i_norm = VIDEO_MODE_NTSC;
519 psz_parser += strlen( "ntsc" );
521 else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
523 p_sys->i_norm = VIDEO_MODE_SECAM;
524 psz_parser += strlen( "secam" );
526 else if( !strncmp( psz_parser, "auto", strlen( "auto" ) ) )
528 p_sys->i_norm = VIDEO_MODE_AUTO;
529 psz_parser += strlen( "auto" );
533 p_sys->i_norm = strtol( psz_parser, &psz_parser, 0 );
536 else if( !strncmp( psz_parser, "frequency=",
537 strlen( "frequency=" ) ) )
540 strtol( psz_parser + strlen( "frequency=" ),
542 if( p_sys->i_frequency < 30000 )
544 msg_Warn( p_demux, "v4l syntax has changed : "
545 "'frequency' is now channel frequency in kHz");
548 else if( !strncmp( psz_parser, "audio=", strlen( "audio=" ) ) )
550 p_sys->i_audio = strtol( psz_parser + strlen( "audio=" ),
553 else if( !strncmp( psz_parser, "size=", strlen( "size=" ) ) )
555 psz_parser += strlen( "size=" );
556 if( !strncmp( psz_parser, "subqcif", strlen( "subqcif" ) ) )
558 p_sys->i_width = 128;
559 p_sys->i_height = 96;
561 else if( !strncmp( psz_parser, "qsif", strlen( "qsif" ) ) )
563 p_sys->i_width = 160;
564 p_sys->i_height = 120;
566 else if( !strncmp( psz_parser, "qcif", strlen( "qcif" ) ) )
568 p_sys->i_width = 176;
569 p_sys->i_height = 144;
571 else if( !strncmp( psz_parser, "sif", strlen( "sif" ) ) )
573 p_sys->i_width = 320;
574 p_sys->i_height = 244;
576 else if( !strncmp( psz_parser, "cif", strlen( "cif" ) ) )
578 p_sys->i_width = 352;
579 p_sys->i_height = 288;
581 else if( !strncmp( psz_parser, "vga", strlen( "vga" ) ) )
583 p_sys->i_width = 640;
584 p_sys->i_height = 480;
589 p_sys->i_width = strtol( psz_parser, &psz_parser, 0 );
590 if( *psz_parser == 'x' || *psz_parser == 'X')
592 p_sys->i_height = strtol( psz_parser + 1,
595 msg_Dbg( p_demux, "WxH %dx%d", p_sys->i_width,
599 else if( !strncmp( psz_parser, "brightness=", strlen( "brightness=" ) ) )
601 p_sys->i_brightness = strtol( psz_parser + strlen( "brightness=" ),
604 else if( !strncmp( psz_parser, "colour=", strlen( "colour=" ) ) )
606 p_sys->i_colour = strtol( psz_parser + strlen( "colour=" ),
609 else if( !strncmp( psz_parser, "hue=", strlen( "hue=" ) ) )
611 p_sys->i_hue = strtol( psz_parser + strlen( "hue=" ),
614 else if( !strncmp( psz_parser, "contrast=", strlen( "contrast=" ) ) )
616 p_sys->i_contrast = strtol( psz_parser + strlen( "contrast=" ),
619 else if( !strncmp( psz_parser, "tuner=", strlen( "tuner=" ) ) )
621 p_sys->i_tuner = strtol( psz_parser + strlen( "tuner=" ),
624 else if( !strncmp( psz_parser, "mjpeg", strlen( "mjpeg" ) ) )
626 psz_parser += strlen( "mjpeg" );
628 p_sys->b_mjpeg = true;
630 else if( !strncmp( psz_parser, "decimation=",
631 strlen( "decimation=" ) ) )
633 p_sys->i_decimation =
634 strtol( psz_parser + strlen( "decimation=" ),
637 else if( !strncmp( psz_parser, "quality=",
638 strlen( "quality=" ) ) )
641 strtol( psz_parser + strlen( "quality=" ),
644 else if( !strncmp( psz_parser, "fps=", strlen( "fps=" ) ) )
646 p_sys->f_fps = strtof( psz_parser + strlen( "fps=" ),
649 else if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) )
650 || !strncmp( psz_parser, "samplerate=", strlen( "samplerate=" ) )
651 || !strncmp( psz_parser, "stereo", strlen( "stereo" ) )
652 || !strncmp( psz_parser, "mono", strlen( "mono" ) ) )
654 if( strchr( psz_parser, ':' ) )
655 psz_parser = strchr( psz_parser, ':' );
657 psz_parser += strlen( psz_parser );
658 msg_Err( p_demux, AUDIO_DEPRECATED_ERROR );
662 msg_Warn( p_demux, "unknown option" );
665 while( *psz_parser && *psz_parser != ':' )
670 if( *psz_parser == '\0' )
678 p_sys->psz_device = strdup( psz_dup );
680 p_sys->psz_device = strdup( V4L_DEFAULT );
684 /*****************************************************************************
686 *****************************************************************************/
687 static int OpenVideoDev( demux_t *p_demux, char *psz_device )
689 demux_sys_t *p_sys = p_demux->p_sys;
692 struct video_channel vid_channel;
693 struct mjpeg_params mjpeg;
696 if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
698 msg_Err( p_demux, "cannot open device (%m)" );
702 if( ioctl( i_fd, VIDIOCGCAP, &p_sys->vid_cap ) < 0 )
704 msg_Err( p_demux, "cannot get capabilities (%m)" );
709 "V4L device %s %d channels %d audios %d < w < %d %d < h < %d",
711 p_sys->vid_cap.channels,
712 p_sys->vid_cap.audios,
713 p_sys->vid_cap.minwidth, p_sys->vid_cap.maxwidth,
714 p_sys->vid_cap.minheight, p_sys->vid_cap.maxheight );
716 if( p_sys->i_channel < 0 || p_sys->i_channel >= p_sys->vid_cap.channels )
718 msg_Dbg( p_demux, "invalid channel, falling back on channel 0" );
719 p_sys->i_channel = 0;
721 if( p_sys->vid_cap.audios && p_sys->i_audio >= p_sys->vid_cap.audios )
723 msg_Dbg( p_demux, "invalid audio, falling back with no audio" );
727 if( p_sys->i_width < p_sys->vid_cap.minwidth ||
728 p_sys->i_width > p_sys->vid_cap.maxwidth )
730 msg_Dbg( p_demux, "invalid width %i", p_sys->i_width );
733 if( p_sys->i_height < p_sys->vid_cap.minheight ||
734 p_sys->i_height > p_sys->vid_cap.maxheight )
736 msg_Dbg( p_demux, "invalid height %i", p_sys->i_height );
740 if( !( p_sys->vid_cap.type & VID_TYPE_CAPTURE ) )
742 msg_Err( p_demux, "cannot grab" );
746 vid_channel.channel = p_sys->i_channel;
747 if( ioctl( i_fd, VIDIOCGCHAN, &vid_channel ) < 0 )
749 msg_Err( p_demux, "cannot get channel infos (%m)" );
753 "setting channel %s(%d) %d tuners flags=0x%x type=0x%x norm=0x%x",
754 vid_channel.name, vid_channel.channel, vid_channel.tuners,
755 vid_channel.flags, vid_channel.type, vid_channel.norm );
757 if( p_sys->i_tuner >= vid_channel.tuners )
759 msg_Dbg( p_demux, "invalid tuner, falling back on tuner 0" );
763 vid_channel.norm = p_sys->i_norm;
764 if( ioctl( i_fd, VIDIOCSCHAN, &vid_channel ) < 0 )
766 msg_Err( p_demux, "cannot set channel (%m)" );
770 if( vid_channel.flags & VIDEO_VC_TUNER )
775 struct video_tuner vid_tuner;
776 if( p_sys->i_tuner >= 0 )
778 vid_tuner.tuner = p_sys->i_tuner;
779 if( ioctl( i_fd, VIDIOCGTUNER, &vid_tuner ) < 0 )
781 msg_Err( p_demux, "cannot get tuner (%m)" );
784 msg_Dbg( p_demux, "tuner %s low=%d high=%d, flags=0x%x "
785 "mode=0x%x signal=0x%x",
786 vid_tuner.name, vid_tuner.rangelow, vid_tuner.rangehigh,
787 vid_tuner.flags, vid_tuner.mode, vid_tuner.signal );
789 msg_Dbg( p_demux, "setting tuner %s (%d)",
790 vid_tuner.name, vid_tuner.tuner );
792 /* FIXME FIXME to be checked FIXME FIXME */
793 //vid_tuner.mode = p_sys->i_norm;
794 if( ioctl( i_fd, VIDIOCSTUNER, &vid_tuner ) < 0 )
796 msg_Err( p_demux, "cannot set tuner (%m)" );
802 /* Show a warning if frequency is < than 30000.
803 * User is certainly usint old syntax. */
807 if( p_sys->i_frequency >= 0 )
809 int driver_frequency = p_sys->i_frequency * 16 /1000;
810 if( ioctl( i_fd, VIDIOCSFREQ, &driver_frequency ) < 0 )
812 msg_Err( p_demux, "cannot set frequency (%m)" );
815 msg_Dbg( p_demux, "frequency %d (%d)", p_sys->i_frequency,
821 if( vid_channel.flags & VIDEO_VC_AUDIO )
823 struct video_audio vid_audio;
825 /* XXX TODO volume, balance, ... */
826 if( p_sys->i_audio >= 0 )
828 vid_audio.audio = p_sys->i_audio;
829 if( ioctl( i_fd, VIDIOCGAUDIO, &vid_audio ) < 0 )
831 msg_Err( p_demux, "cannot get audio (%m)" );
836 vid_audio.flags &= ~VIDEO_AUDIO_MUTE;
838 if( ioctl( i_fd, VIDIOCSAUDIO, &vid_audio ) < 0 )
840 msg_Err( p_demux, "cannot set audio (%m)" );
847 /* establish basic params with input and norm before feeling width
851 struct quicktime_mjpeg_app1 *p_app1;
854 if( ioctl( i_fd, MJPIOC_G_PARAMS, &mjpeg ) < 0 )
856 msg_Err( p_demux, "cannot get mjpeg params (%m)" );
859 mjpeg.input = p_sys->i_channel;
860 mjpeg.norm = p_sys->i_norm;
861 mjpeg.decimation = p_sys->i_decimation;
864 mjpeg.img_width = p_sys->i_width / p_sys->i_decimation;
865 if( p_sys->i_height )
866 mjpeg.img_height = p_sys->i_height / p_sys->i_decimation;
868 /* establish Quicktime APP1 marker while we are here */
873 p_app1 = (struct quicktime_mjpeg_app1 *)mjpeg.APP_data;
874 p_app1->i_reserved = 0;
875 p_app1->i_tag = VLC_FOURCC( 'm','j','p','g' );
876 p_app1->i_field_size = 0;
877 p_app1->i_padded_field_size = 0;
878 p_app1->i_next_field = 0;
879 /* XXX WARNING XXX */
880 /* these's nothing magic about these values. We are dangerously
881 * assuming the encoder card is encoding mjpeg-a and is not throwing
882 * in marker tags we aren't expecting. It's bad enough we have to
883 * search through the jpeg output for every frame we grab just to
884 * find the first field's end marker, so we take this risk to boost
886 * This is really something the driver could do for us because this
887 * does conform to standards outside of Apple Quicktime.
890 p_app1->i_DQT_offset = hton32( i_offset );
892 p_app1->i_DHT_offset = hton32( i_offset );
894 p_app1->i_SOF_offset = hton32( i_offset );
896 p_app1->i_SOS_offset = hton32( i_offset );
898 p_app1->i_data_offset = hton32( i_offset );
900 /* SOF and SOS aren't specified by the mjpeg API because they aren't
901 * optional. They will be present in the output. */
902 mjpeg.jpeg_markers = JPEG_MARKER_DHT | JPEG_MARKER_DQT;
904 if( ioctl( i_fd, MJPIOC_S_PARAMS, &mjpeg ) < 0 )
906 msg_Err( p_demux, "cannot set mjpeg params (%m)" );
910 p_sys->i_width = mjpeg.img_width * mjpeg.HorDcm;
911 p_sys->i_height = mjpeg.img_height * mjpeg.VerDcm *
912 mjpeg.field_per_buff;
915 /* fix width/height */
916 if( !p_sys->b_mjpeg && ( p_sys->i_width == 0 || p_sys->i_height == 0 ) )
918 struct video_window vid_win;
920 if( ioctl( i_fd, VIDIOCGWIN, &vid_win ) < 0 )
922 msg_Err( p_demux, "cannot get win (%m)" );
925 p_sys->i_width = vid_win.width;
926 p_sys->i_height = vid_win.height;
928 if( !p_sys->i_width || !p_sys->i_height )
930 p_sys->i_width = p_sys->vid_cap.maxwidth;
931 p_sys->i_height = p_sys->vid_cap.maxheight;
934 if( !p_sys->i_width || !p_sys->i_height )
936 msg_Err( p_demux, "invalid video size (%ix%i)",
937 p_sys->i_width, p_sys->i_height );
941 msg_Dbg( p_demux, "will use %dx%d", p_sys->i_width, p_sys->i_height );
944 if( !p_sys->b_mjpeg )
946 /* set hue/color/.. */
947 if( ioctl( i_fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 )
949 struct video_picture vid_picture = p_sys->vid_picture;
951 if( p_sys->i_brightness >= 0 && p_sys->i_brightness < 65536 )
953 vid_picture.brightness = p_sys->i_brightness;
955 if( p_sys->i_colour >= 0 && p_sys->i_colour < 65536 )
957 vid_picture.colour = p_sys->i_colour;
959 if( p_sys->i_hue >= 0 && p_sys->i_hue < 65536 )
961 vid_picture.hue = p_sys->i_hue;
963 if( p_sys->i_contrast >= 0 && p_sys->i_contrast < 65536 )
965 vid_picture.contrast = p_sys->i_contrast;
967 if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
969 msg_Dbg( p_demux, "v4l device uses brightness: %d",
970 vid_picture.brightness );
971 msg_Dbg( p_demux, "v4l device uses colour: %d",
972 vid_picture.colour );
973 msg_Dbg( p_demux, "v4l device uses hue: %d", vid_picture.hue );
974 msg_Dbg( p_demux, "v4l device uses contrast: %d",
975 vid_picture.contrast );
976 p_sys->vid_picture = vid_picture;
980 /* Find out video format used by device */
981 if( ioctl( i_fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 )
983 struct video_picture vid_picture = p_sys->vid_picture;
989 psz = var_CreateGetString( p_demux, "v4l-chroma" );
990 if( strlen( psz ) >= 4 )
992 vid_picture.palette = 0;
993 int i_chroma = VLC_FOURCC( psz[0], psz[1], psz[2], psz[3] );
995 /* Find out v4l chroma code */
996 for( i = 0; v4lchroma_to_fourcc[i].i_v4l != 0; i++ )
998 if( v4lchroma_to_fourcc[i].i_fourcc == i_chroma )
1000 vid_picture.palette = v4lchroma_to_fourcc[i].i_v4l;
1007 if( vid_picture.palette &&
1008 !ioctl( i_fd, VIDIOCSPICT, &vid_picture ) )
1010 p_sys->vid_picture = vid_picture;
1014 /* Try to set the format to something easy to encode */
1015 vid_picture.palette = VIDEO_PALETTE_YUV420P;
1016 if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
1018 p_sys->vid_picture = vid_picture;
1022 vid_picture.palette = VIDEO_PALETTE_YUV422P;
1023 if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
1025 p_sys->vid_picture = vid_picture;
1030 /* Find out final format */
1031 for( i = 0; v4lchroma_to_fourcc[i].i_v4l != 0; i++ )
1033 if( v4lchroma_to_fourcc[i].i_v4l == p_sys->vid_picture.palette)
1035 p_sys->i_fourcc = v4lchroma_to_fourcc[i].i_fourcc;
1042 msg_Err( p_demux, "ioctl VIDIOCGPICT failed" );
1047 if( p_sys->b_mjpeg )
1051 p_sys->mjpeg_buffers.count = 8;
1052 p_sys->mjpeg_buffers.size = MJPEG_BUFFER_SIZE;
1054 if( ioctl( i_fd, MJPIOC_REQBUFS, &p_sys->mjpeg_buffers ) < 0 )
1056 msg_Err( p_demux, "mmap unsupported" );
1060 p_sys->p_video_mmap = mmap( 0,
1061 p_sys->mjpeg_buffers.size * p_sys->mjpeg_buffers.count,
1062 PROT_READ | PROT_WRITE, MAP_SHARED, i_fd, 0 );
1063 if( p_sys->p_video_mmap == MAP_FAILED )
1065 msg_Err( p_demux, "mmap failed" );
1069 p_sys->i_fourcc = VLC_FOURCC( 'm','j','p','g' );
1070 p_sys->i_frame_pos = -1;
1072 /* queue up all the frames */
1073 for( i = 0; i < (int)p_sys->mjpeg_buffers.count; i++ )
1075 if( ioctl( i_fd, MJPIOC_QBUF_CAPT, &i ) < 0 )
1077 msg_Err( p_demux, "unable to queue frame" );
1084 /* Fill in picture_t fields */
1085 vout_InitPicture( VLC_OBJECT(p_demux), &p_sys->pic, p_sys->i_fourcc,
1086 p_sys->i_width, p_sys->i_height, p_sys->i_width *
1087 VOUT_ASPECT_FACTOR / p_sys->i_height );
1088 if( !p_sys->pic.i_planes )
1090 msg_Err( p_demux, "unsupported chroma" );
1093 p_sys->i_video_frame_size = 0;
1094 for( i = 0; i < p_sys->pic.i_planes; i++ )
1096 p_sys->i_video_frame_size += p_sys->pic.p[i].i_visible_lines *
1097 p_sys->pic.p[i].i_visible_pitch;
1100 msg_Dbg( p_demux, "v4l device uses frame size: %i",
1101 p_sys->i_video_frame_size );
1102 msg_Dbg( p_demux, "v4l device uses chroma: %4.4s",
1103 (char*)&p_sys->i_fourcc );
1105 /* Allocate mmap buffer */
1106 if( ioctl( i_fd, VIDIOCGMBUF, &p_sys->vid_mbuf ) < 0 )
1108 msg_Err( p_demux, "mmap unsupported" );
1112 p_sys->p_video_mmap = mmap( 0, p_sys->vid_mbuf.size,
1113 PROT_READ|PROT_WRITE, MAP_SHARED,
1115 if( p_sys->p_video_mmap == MAP_FAILED )
1117 /* FIXME -> normal read */
1118 msg_Err( p_demux, "mmap failed" );
1123 p_sys->vid_mmap.frame = 0;
1124 p_sys->vid_mmap.width = p_sys->i_width;
1125 p_sys->vid_mmap.height = p_sys->i_height;
1126 p_sys->vid_mmap.format = p_sys->vid_picture.palette;
1127 if( ioctl( i_fd, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 )
1129 msg_Warn( p_demux, "%4.4s refused", (char*)&p_sys->i_fourcc );
1130 msg_Err( p_demux, "chroma selection failed" );
1138 if( i_fd >= 0 ) close( i_fd );
1142 /*****************************************************************************
1144 *****************************************************************************/
1145 static uint8_t *GrabCapture( demux_t *p_demux )
1147 demux_sys_t *p_sys = p_demux->p_sys;
1148 int i_captured_frame = p_sys->i_frame_pos;
1150 p_sys->vid_mmap.frame = (p_sys->i_frame_pos + 1) % p_sys->vid_mbuf.frames;
1152 while( ioctl( p_sys->i_fd, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 )
1154 if( errno != EAGAIN )
1156 msg_Err( p_demux, "failed capturing new frame" );
1160 if( !vlc_object_alive (p_demux) )
1165 msg_Dbg( p_demux, "grab failed, trying again" );
1168 while( ioctl(p_sys->i_fd, VIDIOCSYNC, &p_sys->i_frame_pos) < 0 )
1170 if( errno != EAGAIN && errno != EINTR )
1172 msg_Err( p_demux, "failed syncing new frame" );
1177 p_sys->i_frame_pos = p_sys->vid_mmap.frame;
1178 /* leave i_video_frame_size alone */
1179 return p_sys->p_video_mmap + p_sys->vid_mbuf.offsets[i_captured_frame];
1182 static uint8_t *GrabMJPEG( demux_t *p_demux )
1184 demux_sys_t *p_sys = p_demux->p_sys;
1185 struct mjpeg_sync sync;
1186 uint8_t *p_frame, *p_field, *p;
1189 struct quicktime_mjpeg_app1 *p_app1 = NULL;
1191 /* re-queue the last frame we sync'd */
1192 if( p_sys->i_frame_pos != -1 )
1194 while( ioctl( p_sys->i_fd, MJPIOC_QBUF_CAPT,
1195 &p_sys->i_frame_pos ) < 0 )
1197 if( errno != EAGAIN && errno != EINTR )
1199 msg_Err( p_demux, "failed capturing new frame" );
1205 /* sync on the next frame */
1206 while( ioctl( p_sys->i_fd, MJPIOC_SYNC, &sync ) < 0 )
1208 if( errno != EAGAIN && errno != EINTR )
1210 msg_Err( p_demux, "failed syncing new frame" );
1215 p_sys->i_frame_pos = sync.frame;
1216 p_frame = p_sys->p_video_mmap + p_sys->mjpeg_buffers.size * sync.frame;
1218 /* p_frame now points to the data. fix up the Quicktime APP1 marker */
1220 tag = hton16( tag );
1224 p = memmem( p_field, sync.length, &tag, 2 );
1228 p += 2; /* data immediately following EOI */
1230 p_app1 = (struct quicktime_mjpeg_app1 *)(p_field + 6);
1232 i_size = ((uint32_t)(p - p_field));
1233 i_size = hton32( i_size );
1234 memcpy( &p_app1->i_field_size, &i_size, 4 );
1236 while( *p == 0xff && *(p+1) == 0xff )
1239 i_size = ((uint32_t)(p - p_field));
1240 i_size = hton32( i_size );
1241 memcpy( &p_app1->i_padded_field_size, &i_size, 4 );
1245 tag = hton16( tag );
1246 p_field = memmem( p, sync.length - (size_t)(p - p_frame), &tag, 2 );
1250 i_size = (uint32_t)(p_field - p_frame);
1251 i_size = hton32( i_size );
1252 memcpy( &p_app1->i_next_field, &i_size, 4 );
1255 p_app1 = (struct quicktime_mjpeg_app1 *)(p_field + 6);
1257 tag = hton16( tag );
1258 p = memmem( p_field, sync.length - (size_t)(p_field - p_frame),
1263 /* sometimes the second field doesn't have the EOI. just put it
1266 p = p_frame + sync.length;
1267 memcpy( p, &tag, 2 );
1272 i_size = (uint32_t)(p - p_field);
1273 i_size = hton32( i_size );
1274 memcpy( &p_app1->i_field_size, &i_size, 4 );
1275 i_size = (uint32_t)(sync.length - (uint32_t)(p_field - p_frame));
1276 i_size = hton32( i_size );
1277 memcpy( &p_app1->i_padded_field_size, &i_size, 4 );
1280 p_sys->i_video_frame_size = sync.length;
1284 static block_t *GrabVideo( demux_t *p_demux )
1286 demux_sys_t *p_sys = p_demux->p_sys;
1290 if( p_sys->f_fps >= 0.1 && p_sys->i_video_pts > 0 )
1292 mtime_t i_dur = (mtime_t)((double)1000000 / (double)p_sys->f_fps);
1294 /* Did we wait long enough ? (frame rate reduction) */
1295 if( p_sys->i_video_pts + i_dur > mdate() ) return 0;
1298 if( p_sys->b_mjpeg ) p_frame = GrabMJPEG( p_demux );
1299 else p_frame = GrabCapture( p_demux );
1301 if( !p_frame ) return 0;
1303 if( !( p_block = block_New( p_demux, p_sys->i_video_frame_size ) ) )
1305 msg_Warn( p_demux, "cannot get block" );
1309 memcpy( p_block->p_buffer, p_frame, p_sys->i_video_frame_size );
1310 p_sys->i_video_pts = p_block->i_pts = p_block->i_dts = mdate();