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>
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 */
217 } v4lchroma_to_fourcc[] =
219 { VIDEO_PALETTE_GREY, VLC_FOURCC( 'G', 'R', 'E', 'Y' ) },
220 { VIDEO_PALETTE_HI240, VLC_FOURCC( 'I', '2', '4', '0' ) },
221 { VIDEO_PALETTE_RGB565, VLC_FOURCC( 'R', 'V', '1', '6' ) },
222 { VIDEO_PALETTE_RGB555, VLC_FOURCC( 'R', 'V', '1', '5' ) },
223 { VIDEO_PALETTE_RGB24, VLC_FOURCC( 'R', 'V', '2', '4' ) },
224 { VIDEO_PALETTE_RGB32, VLC_FOURCC( 'R', 'V', '3', '2' ) },
225 { VIDEO_PALETTE_YUV422, VLC_FOURCC( 'Y', 'U', 'Y', '2' ) },
226 { VIDEO_PALETTE_YUV422, VLC_FOURCC( 'Y', 'U', 'Y', 'V' ) },
227 { VIDEO_PALETTE_YUYV, VLC_FOURCC( 'Y', 'U', 'Y', '2' ) },
228 { VIDEO_PALETTE_YUYV, VLC_FOURCC( 'Y', 'U', 'Y', 'V' ) },
229 { VIDEO_PALETTE_UYVY, VLC_FOURCC( 'U', 'Y', 'V', 'Y' ) },
230 { VIDEO_PALETTE_YUV420, VLC_FOURCC( 'I', '4', '2', 'N' ) },
231 { VIDEO_PALETTE_YUV411, VLC_FOURCC( 'I', '4', '1', 'N' ) },
232 { VIDEO_PALETTE_RAW, VLC_FOURCC( 'G', 'R', 'A', 'W' ) },
233 { VIDEO_PALETTE_YUV422P, VLC_FOURCC( 'I', '4', '2', '2' ) },
234 { VIDEO_PALETTE_YUV420P, VLC_FOURCC( 'I', '4', '2', '0' ) },
235 { VIDEO_PALETTE_YUV411P, VLC_FOURCC( 'I', '4', '1', '1' ) },
242 char *psz_device; /* Main device from MRL */
245 /* Video properties */
262 float f_fps; /* <= 0.0 mean to grab at full rate */
263 mtime_t i_video_pts; /* only used when f_fps > 0 */
269 struct video_capability vid_cap;
270 struct video_mbuf vid_mbuf;
271 struct mjpeg_requestbuffers mjpeg_buffers;
273 uint8_t *p_video_mmap;
276 struct video_mmap vid_mmap;
277 struct video_picture vid_picture;
279 int i_video_frame_size;
283 /*****************************************************************************
284 * Open: opens v4l device
285 *****************************************************************************
287 * url: <video device>::::
289 *****************************************************************************/
290 static int Open( vlc_object_t *p_this )
292 demux_t *p_demux = (demux_t*)p_this;
295 /* Only when selected */
296 if( *p_demux->psz_access == '\0' )
300 p_demux->pf_demux = Demux;
301 p_demux->pf_control = Control;
302 p_demux->info.i_update = 0;
303 p_demux->info.i_title = 0;
304 p_demux->info.i_seekpoint = 0;
305 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
309 p_sys->i_audio = var_CreateGetInteger( p_demux, "v4l-audio" );
310 p_sys->i_channel = var_CreateGetInteger( p_demux, "v4l-channel" );
311 p_sys->i_norm = var_CreateGetInteger( p_demux, "v4l-norm" );
312 p_sys->i_tuner = var_CreateGetInteger( p_demux, "v4l-tuner" );
313 p_sys->i_frequency = var_CreateGetInteger( p_demux, "v4l-frequency" );
315 p_sys->f_fps = var_CreateGetFloat( p_demux, "v4l-fps" );
316 p_sys->i_width = var_CreateGetInteger( p_demux, "v4l-width" );
317 p_sys->i_height = var_CreateGetInteger( p_demux, "v4l-height" );
318 p_sys->i_video_pts = -1;
319 p_sys->i_brightness = var_CreateGetInteger( p_demux, "v4l-brightness" );
321 p_sys->i_hue = var_CreateGetInteger( p_demux, "v4l-hue" );
322 p_sys->i_colour = var_CreateGetInteger( p_demux, "v4l-colour" );
323 p_sys->i_contrast = var_CreateGetInteger( p_demux, "v4l-contrast" );
325 p_sys->b_mjpeg = var_CreateGetBool( p_demux, "v4l-mjpeg" );
326 p_sys->i_decimation = var_CreateGetInteger( p_demux, "v4l-decimation" );
327 p_sys->i_quality = var_CreateGetInteger( p_demux, "v4l-quality" );
329 p_sys->psz_device = NULL;
336 msg_Dbg( p_this, "opening device '%s'", p_sys->psz_device );
337 p_sys->i_fd = OpenVideoDev( p_demux, p_sys->psz_device );
338 if( p_sys->i_fd < 0 )
345 msg_Dbg( p_demux, "v4l grabbing started" );
347 /* Declare elementary streams */
349 es_format_Init( &fmt, VIDEO_ES, p_sys->i_fourcc );
350 fmt.video.i_width = p_sys->i_width;
351 fmt.video.i_height = p_sys->i_height;
352 fmt.video.i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
354 /* Setup rgb mask for RGB formats */
355 switch( p_sys->i_fourcc )
357 case VLC_FOURCC('R','V','1','5'):
358 fmt.video.i_rmask = 0x001f;
359 fmt.video.i_gmask = 0x03e0;
360 fmt.video.i_bmask = 0x7c00;
362 case VLC_FOURCC('R','V','1','6'):
363 fmt.video.i_rmask = 0x001f;
364 fmt.video.i_gmask = 0x07e0;
365 fmt.video.i_bmask = 0xf800;
367 case VLC_FOURCC('R','V','2','4'):
368 case VLC_FOURCC('R','V','3','2'):
369 fmt.video.i_rmask = 0x00ff0000;
370 fmt.video.i_gmask = 0x0000ff00;
371 fmt.video.i_bmask = 0x000000ff;
375 msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
376 (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
377 p_sys->p_es = es_out_Add( p_demux->out, &fmt );
379 /* Update default_pts to a suitable value for access */
380 var_Create( p_demux, "v4l-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
385 /*****************************************************************************
386 * Close: close device, free resources
387 *****************************************************************************/
388 static void Close( vlc_object_t *p_this )
390 demux_t *p_demux = (demux_t *)p_this;
391 demux_sys_t *p_sys = p_demux->p_sys;
393 free( p_sys->psz_device );
394 if( p_sys->i_fd >= 0 ) close( p_sys->i_fd );
399 ioctl( p_sys->i_fd, MJPIOC_QBUF_CAPT, &i_noframe );
402 if( p_sys->p_video_mmap && p_sys->p_video_mmap != MAP_FAILED )
405 munmap( p_sys->p_video_mmap, p_sys->mjpeg_buffers.size *
406 p_sys->mjpeg_buffers.count );
408 munmap( p_sys->p_video_mmap, p_sys->vid_mbuf.size );
414 /*****************************************************************************
416 *****************************************************************************/
417 static int Control( demux_t *p_demux, int i_query, va_list args )
424 /* Special for access_demux */
425 case DEMUX_CAN_PAUSE:
427 case DEMUX_SET_PAUSE_STATE:
428 case DEMUX_CAN_CONTROL_PACE:
429 pb = (bool*)va_arg( args, bool * );
433 case DEMUX_GET_PTS_DELAY:
434 pi64 = (int64_t*)va_arg( args, int64_t * );
435 *pi64 = (int64_t)var_GetInteger( p_demux, "v4l-caching" ) * 1000;
439 pi64 = (int64_t*)va_arg( args, int64_t * );
443 /* TODO implement others */
451 /*****************************************************************************
453 *****************************************************************************/
454 static int Demux( demux_t *p_demux )
456 demux_sys_t *p_sys = p_demux->p_sys;
458 block_t *p_block = GrabVideo( p_demux );
462 msleep( 10000 ); /* Unfortunately v4l doesn't allow polling */
466 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
467 es_out_Send( p_demux->out, p_sys->p_es, p_block );
472 /*****************************************************************************
473 * ParseMRL: parse the options contained in the MRL
474 *****************************************************************************/
475 static void ParseMRL( demux_t *p_demux )
477 demux_sys_t *p_sys = p_demux->p_sys;
479 char *psz_dup = strdup( p_demux->psz_path );
480 char *psz_parser = psz_dup;
482 while( *psz_parser && *psz_parser != ':' )
487 if( *psz_parser == ':' )
492 *psz_parser++ = '\0';
493 if( !strncmp( psz_parser, "channel=", strlen( "channel=" ) ) )
495 p_sys->i_channel = strtol( psz_parser + strlen( "channel=" ),
498 else if( !strncmp( psz_parser, "norm=", strlen( "norm=" ) ) )
500 psz_parser += strlen( "norm=" );
501 if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
503 p_sys->i_norm = VIDEO_MODE_PAL;
504 psz_parser += strlen( "pal" );
506 else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
508 p_sys->i_norm = VIDEO_MODE_NTSC;
509 psz_parser += strlen( "ntsc" );
511 else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
513 p_sys->i_norm = VIDEO_MODE_SECAM;
514 psz_parser += strlen( "secam" );
516 else if( !strncmp( psz_parser, "auto", strlen( "auto" ) ) )
518 p_sys->i_norm = VIDEO_MODE_AUTO;
519 psz_parser += strlen( "auto" );
523 p_sys->i_norm = strtol( psz_parser, &psz_parser, 0 );
526 else if( !strncmp( psz_parser, "frequency=",
527 strlen( "frequency=" ) ) )
530 strtol( psz_parser + strlen( "frequency=" ),
532 if( p_sys->i_frequency < 30000 )
534 msg_Warn( p_demux, "v4l syntax has changed : "
535 "'frequency' is now channel frequency in kHz");
538 else if( !strncmp( psz_parser, "audio=", strlen( "audio=" ) ) )
540 p_sys->i_audio = strtol( psz_parser + strlen( "audio=" ),
543 else if( !strncmp( psz_parser, "size=", strlen( "size=" ) ) )
545 psz_parser += strlen( "size=" );
546 if( !strncmp( psz_parser, "subqcif", strlen( "subqcif" ) ) )
548 p_sys->i_width = 128;
549 p_sys->i_height = 96;
551 else if( !strncmp( psz_parser, "qsif", strlen( "qsif" ) ) )
553 p_sys->i_width = 160;
554 p_sys->i_height = 120;
556 else if( !strncmp( psz_parser, "qcif", strlen( "qcif" ) ) )
558 p_sys->i_width = 176;
559 p_sys->i_height = 144;
561 else if( !strncmp( psz_parser, "sif", strlen( "sif" ) ) )
563 p_sys->i_width = 320;
564 p_sys->i_height = 244;
566 else if( !strncmp( psz_parser, "cif", strlen( "cif" ) ) )
568 p_sys->i_width = 352;
569 p_sys->i_height = 288;
571 else if( !strncmp( psz_parser, "vga", strlen( "vga" ) ) )
573 p_sys->i_width = 640;
574 p_sys->i_height = 480;
579 p_sys->i_width = strtol( psz_parser, &psz_parser, 0 );
580 if( *psz_parser == 'x' || *psz_parser == 'X')
582 p_sys->i_height = strtol( psz_parser + 1,
585 msg_Dbg( p_demux, "WxH %dx%d", p_sys->i_width,
589 else if( !strncmp( psz_parser, "brightness=", strlen( "brightness=" ) ) )
591 p_sys->i_brightness = strtol( psz_parser + strlen( "brightness=" ),
594 else if( !strncmp( psz_parser, "colour=", strlen( "colour=" ) ) )
596 p_sys->i_colour = strtol( psz_parser + strlen( "colour=" ),
599 else if( !strncmp( psz_parser, "hue=", strlen( "hue=" ) ) )
601 p_sys->i_hue = strtol( psz_parser + strlen( "hue=" ),
604 else if( !strncmp( psz_parser, "contrast=", strlen( "contrast=" ) ) )
606 p_sys->i_contrast = strtol( psz_parser + strlen( "contrast=" ),
609 else if( !strncmp( psz_parser, "tuner=", strlen( "tuner=" ) ) )
611 p_sys->i_tuner = strtol( psz_parser + strlen( "tuner=" ),
614 else if( !strncmp( psz_parser, "mjpeg", strlen( "mjpeg" ) ) )
616 psz_parser += strlen( "mjpeg" );
618 p_sys->b_mjpeg = true;
620 else if( !strncmp( psz_parser, "decimation=",
621 strlen( "decimation=" ) ) )
623 p_sys->i_decimation =
624 strtol( psz_parser + strlen( "decimation=" ),
627 else if( !strncmp( psz_parser, "quality=",
628 strlen( "quality=" ) ) )
631 strtol( psz_parser + strlen( "quality=" ),
634 else if( !strncmp( psz_parser, "fps=", strlen( "fps=" ) ) )
636 p_sys->f_fps = strtof( psz_parser + strlen( "fps=" ),
639 else if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) )
640 || !strncmp( psz_parser, "samplerate=", strlen( "samplerate=" ) )
641 || !strncmp( psz_parser, "stereo", strlen( "stereo" ) )
642 || !strncmp( psz_parser, "mono", strlen( "mono" ) ) )
644 if( strchr( psz_parser, ':' ) )
645 psz_parser = strchr( psz_parser, ':' );
647 psz_parser += strlen( psz_parser );
648 msg_Err( p_demux, AUDIO_DEPRECATED_ERROR );
652 msg_Warn( p_demux, "unknown option" );
655 while( *psz_parser && *psz_parser != ':' )
660 if( *psz_parser == '\0' )
668 p_sys->psz_device = strdup( psz_dup );
670 p_sys->psz_device = strdup( V4L_DEFAULT );
674 /*****************************************************************************
676 *****************************************************************************/
677 static int OpenVideoDev( demux_t *p_demux, char *psz_device )
679 demux_sys_t *p_sys = p_demux->p_sys;
682 struct video_channel vid_channel;
683 struct mjpeg_params mjpeg;
686 if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
688 msg_Err( p_demux, "cannot open device (%m)" );
692 if( ioctl( i_fd, VIDIOCGCAP, &p_sys->vid_cap ) < 0 )
694 msg_Err( p_demux, "cannot get capabilities (%m)" );
699 "V4L device %s %d channels %d audios %d < w < %d %d < h < %d",
701 p_sys->vid_cap.channels,
702 p_sys->vid_cap.audios,
703 p_sys->vid_cap.minwidth, p_sys->vid_cap.maxwidth,
704 p_sys->vid_cap.minheight, p_sys->vid_cap.maxheight );
706 if( p_sys->i_channel < 0 || p_sys->i_channel >= p_sys->vid_cap.channels )
708 msg_Dbg( p_demux, "invalid channel, falling back on channel 0" );
709 p_sys->i_channel = 0;
711 if( p_sys->vid_cap.audios && p_sys->i_audio >= p_sys->vid_cap.audios )
713 msg_Dbg( p_demux, "invalid audio, falling back with no audio" );
717 if( p_sys->i_width < p_sys->vid_cap.minwidth ||
718 p_sys->i_width > p_sys->vid_cap.maxwidth )
720 msg_Dbg( p_demux, "invalid width %i", p_sys->i_width );
723 if( p_sys->i_height < p_sys->vid_cap.minheight ||
724 p_sys->i_height > p_sys->vid_cap.maxheight )
726 msg_Dbg( p_demux, "invalid height %i", p_sys->i_height );
730 if( !( p_sys->vid_cap.type & VID_TYPE_CAPTURE ) )
732 msg_Err( p_demux, "cannot grab" );
736 vid_channel.channel = p_sys->i_channel;
737 if( ioctl( i_fd, VIDIOCGCHAN, &vid_channel ) < 0 )
739 msg_Err( p_demux, "cannot get channel infos (%m)" );
743 "setting channel %s(%d) %d tuners flags=0x%x type=0x%x norm=0x%x",
744 vid_channel.name, vid_channel.channel, vid_channel.tuners,
745 vid_channel.flags, vid_channel.type, vid_channel.norm );
747 if( p_sys->i_tuner >= vid_channel.tuners )
749 msg_Dbg( p_demux, "invalid tuner, falling back on tuner 0" );
753 vid_channel.norm = p_sys->i_norm;
754 if( ioctl( i_fd, VIDIOCSCHAN, &vid_channel ) < 0 )
756 msg_Err( p_demux, "cannot set channel (%m)" );
760 if( vid_channel.flags & VIDEO_VC_TUNER )
765 struct video_tuner vid_tuner;
766 if( p_sys->i_tuner >= 0 )
768 vid_tuner.tuner = p_sys->i_tuner;
769 if( ioctl( i_fd, VIDIOCGTUNER, &vid_tuner ) < 0 )
771 msg_Err( p_demux, "cannot get tuner (%m)" );
774 msg_Dbg( p_demux, "tuner %s low=%d high=%d, flags=0x%x "
775 "mode=0x%x signal=0x%x",
776 vid_tuner.name, vid_tuner.rangelow, vid_tuner.rangehigh,
777 vid_tuner.flags, vid_tuner.mode, vid_tuner.signal );
779 msg_Dbg( p_demux, "setting tuner %s (%d)",
780 vid_tuner.name, vid_tuner.tuner );
782 /* FIXME FIXME to be checked FIXME FIXME */
783 //vid_tuner.mode = p_sys->i_norm;
784 if( ioctl( i_fd, VIDIOCSTUNER, &vid_tuner ) < 0 )
786 msg_Err( p_demux, "cannot set tuner (%m)" );
792 /* Show a warning if frequency is < than 30000.
793 * User is certainly usint old syntax. */
797 if( p_sys->i_frequency >= 0 )
799 int driver_frequency = p_sys->i_frequency * 16 /1000;
800 if( ioctl( i_fd, VIDIOCSFREQ, &driver_frequency ) < 0 )
802 msg_Err( p_demux, "cannot set frequency (%m)" );
805 msg_Dbg( p_demux, "frequency %d (%d)", p_sys->i_frequency,
811 if( vid_channel.flags & VIDEO_VC_AUDIO )
813 struct video_audio vid_audio;
815 /* XXX TODO volume, balance, ... */
816 if( p_sys->i_audio >= 0 )
818 vid_audio.audio = p_sys->i_audio;
819 if( ioctl( i_fd, VIDIOCGAUDIO, &vid_audio ) < 0 )
821 msg_Err( p_demux, "cannot get audio (%m)" );
826 vid_audio.flags &= ~VIDEO_AUDIO_MUTE;
828 if( ioctl( i_fd, VIDIOCSAUDIO, &vid_audio ) < 0 )
830 msg_Err( p_demux, "cannot set audio (%m)" );
837 /* establish basic params with input and norm before feeling width
841 struct quicktime_mjpeg_app1 *p_app1;
844 if( ioctl( i_fd, MJPIOC_G_PARAMS, &mjpeg ) < 0 )
846 msg_Err( p_demux, "cannot get mjpeg params (%m)" );
849 mjpeg.input = p_sys->i_channel;
850 mjpeg.norm = p_sys->i_norm;
851 mjpeg.decimation = p_sys->i_decimation;
854 mjpeg.img_width = p_sys->i_width / p_sys->i_decimation;
855 if( p_sys->i_height )
856 mjpeg.img_height = p_sys->i_height / p_sys->i_decimation;
858 /* establish Quicktime APP1 marker while we are here */
863 p_app1 = (struct quicktime_mjpeg_app1 *)mjpeg.APP_data;
864 p_app1->i_reserved = 0;
865 p_app1->i_tag = VLC_FOURCC( 'm','j','p','g' );
866 p_app1->i_field_size = 0;
867 p_app1->i_padded_field_size = 0;
868 p_app1->i_next_field = 0;
869 /* XXX WARNING XXX */
870 /* these's nothing magic about these values. We are dangerously
871 * assuming the encoder card is encoding mjpeg-a and is not throwing
872 * in marker tags we aren't expecting. It's bad enough we have to
873 * search through the jpeg output for every frame we grab just to
874 * find the first field's end marker, so we take this risk to boost
876 * This is really something the driver could do for us because this
877 * does conform to standards outside of Apple Quicktime.
880 p_app1->i_DQT_offset = hton32( i_offset );
882 p_app1->i_DHT_offset = hton32( i_offset );
884 p_app1->i_SOF_offset = hton32( i_offset );
886 p_app1->i_SOS_offset = hton32( i_offset );
888 p_app1->i_data_offset = hton32( i_offset );
890 /* SOF and SOS aren't specified by the mjpeg API because they aren't
891 * optional. They will be present in the output. */
892 mjpeg.jpeg_markers = JPEG_MARKER_DHT | JPEG_MARKER_DQT;
894 if( ioctl( i_fd, MJPIOC_S_PARAMS, &mjpeg ) < 0 )
896 msg_Err( p_demux, "cannot set mjpeg params (%m)" );
900 p_sys->i_width = mjpeg.img_width * mjpeg.HorDcm;
901 p_sys->i_height = mjpeg.img_height * mjpeg.VerDcm *
902 mjpeg.field_per_buff;
905 /* fix width/height */
906 if( !p_sys->b_mjpeg && ( p_sys->i_width == 0 || p_sys->i_height == 0 ) )
908 struct video_window vid_win;
910 if( ioctl( i_fd, VIDIOCGWIN, &vid_win ) < 0 )
912 msg_Err( p_demux, "cannot get win (%m)" );
915 p_sys->i_width = vid_win.width;
916 p_sys->i_height = vid_win.height;
918 if( !p_sys->i_width || !p_sys->i_height )
920 p_sys->i_width = p_sys->vid_cap.maxwidth;
921 p_sys->i_height = p_sys->vid_cap.maxheight;
924 if( !p_sys->i_width || !p_sys->i_height )
926 msg_Err( p_demux, "invalid video size (%ix%i)",
927 p_sys->i_width, p_sys->i_height );
931 msg_Dbg( p_demux, "will use %dx%d", p_sys->i_width, p_sys->i_height );
934 if( !p_sys->b_mjpeg )
936 /* set hue/color/.. */
937 if( ioctl( i_fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 )
939 struct video_picture vid_picture = p_sys->vid_picture;
941 if( p_sys->i_brightness >= 0 && p_sys->i_brightness < 65536 )
943 vid_picture.brightness = p_sys->i_brightness;
945 if( p_sys->i_colour >= 0 && p_sys->i_colour < 65536 )
947 vid_picture.colour = p_sys->i_colour;
949 if( p_sys->i_hue >= 0 && p_sys->i_hue < 65536 )
951 vid_picture.hue = p_sys->i_hue;
953 if( p_sys->i_contrast >= 0 && p_sys->i_contrast < 65536 )
955 vid_picture.contrast = p_sys->i_contrast;
957 if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
959 msg_Dbg( p_demux, "v4l device uses brightness: %d",
960 vid_picture.brightness );
961 msg_Dbg( p_demux, "v4l device uses colour: %d",
962 vid_picture.colour );
963 msg_Dbg( p_demux, "v4l device uses hue: %d", vid_picture.hue );
964 msg_Dbg( p_demux, "v4l device uses contrast: %d",
965 vid_picture.contrast );
966 p_sys->vid_picture = vid_picture;
970 /* Find out video format used by device */
971 if( ioctl( i_fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 )
973 struct video_picture vid_picture = p_sys->vid_picture;
979 psz = var_CreateGetString( p_demux, "v4l-chroma" );
980 if( strlen( psz ) >= 4 )
982 vid_picture.palette = 0;
983 int i_chroma = VLC_FOURCC( psz[0], psz[1], psz[2], psz[3] );
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_FOURCC( 'm','j','p','g' );
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 vout_InitPicture( VLC_OBJECT(p_demux), &p_sys->pic, p_sys->i_fourcc,
1076 p_sys->i_width, p_sys->i_height, p_sys->i_width *
1077 VOUT_ASPECT_FACTOR / p_sys->i_height );
1078 if( !p_sys->pic.i_planes )
1080 msg_Err( p_demux, "unsupported chroma" );
1083 p_sys->i_video_frame_size = 0;
1084 for( i = 0; i < p_sys->pic.i_planes; i++ )
1086 p_sys->i_video_frame_size += p_sys->pic.p[i].i_visible_lines *
1087 p_sys->pic.p[i].i_visible_pitch;
1090 msg_Dbg( p_demux, "v4l device uses frame size: %i",
1091 p_sys->i_video_frame_size );
1092 msg_Dbg( p_demux, "v4l device uses chroma: %4.4s",
1093 (char*)&p_sys->i_fourcc );
1095 /* Allocate mmap buffer */
1096 if( ioctl( i_fd, VIDIOCGMBUF, &p_sys->vid_mbuf ) < 0 )
1098 msg_Err( p_demux, "mmap unsupported" );
1102 p_sys->p_video_mmap = mmap( 0, p_sys->vid_mbuf.size,
1103 PROT_READ|PROT_WRITE, MAP_SHARED,
1105 if( p_sys->p_video_mmap == MAP_FAILED )
1107 /* FIXME -> normal read */
1108 msg_Err( p_demux, "mmap failed" );
1113 p_sys->vid_mmap.frame = 0;
1114 p_sys->vid_mmap.width = p_sys->i_width;
1115 p_sys->vid_mmap.height = p_sys->i_height;
1116 p_sys->vid_mmap.format = p_sys->vid_picture.palette;
1117 if( ioctl( i_fd, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 )
1119 msg_Warn( p_demux, "%4.4s refused", (char*)&p_sys->i_fourcc );
1120 msg_Err( p_demux, "chroma selection failed" );
1128 if( i_fd >= 0 ) close( i_fd );
1132 /*****************************************************************************
1134 *****************************************************************************/
1135 static uint8_t *GrabCapture( demux_t *p_demux )
1137 demux_sys_t *p_sys = p_demux->p_sys;
1138 int i_captured_frame = p_sys->i_frame_pos;
1140 p_sys->vid_mmap.frame = (p_sys->i_frame_pos + 1) % p_sys->vid_mbuf.frames;
1142 while( ioctl( p_sys->i_fd, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 )
1144 if( errno != EAGAIN )
1146 msg_Err( p_demux, "failed capturing new frame" );
1150 if( !vlc_object_alive (p_demux) )
1155 msg_Dbg( p_demux, "grab failed, trying again" );
1158 while( ioctl(p_sys->i_fd, VIDIOCSYNC, &p_sys->i_frame_pos) < 0 )
1160 if( errno != EAGAIN && errno != EINTR )
1162 msg_Err( p_demux, "failed syncing new frame" );
1167 p_sys->i_frame_pos = p_sys->vid_mmap.frame;
1168 /* leave i_video_frame_size alone */
1169 return p_sys->p_video_mmap + p_sys->vid_mbuf.offsets[i_captured_frame];
1172 static uint8_t *GrabMJPEG( demux_t *p_demux )
1174 demux_sys_t *p_sys = p_demux->p_sys;
1175 struct mjpeg_sync sync;
1176 uint8_t *p_frame, *p_field, *p;
1179 struct quicktime_mjpeg_app1 *p_app1 = NULL;
1181 /* re-queue the last frame we sync'd */
1182 if( p_sys->i_frame_pos != -1 )
1184 while( ioctl( p_sys->i_fd, MJPIOC_QBUF_CAPT,
1185 &p_sys->i_frame_pos ) < 0 )
1187 if( errno != EAGAIN && errno != EINTR )
1189 msg_Err( p_demux, "failed capturing new frame" );
1195 /* sync on the next frame */
1196 while( ioctl( p_sys->i_fd, MJPIOC_SYNC, &sync ) < 0 )
1198 if( errno != EAGAIN && errno != EINTR )
1200 msg_Err( p_demux, "failed syncing new frame" );
1205 p_sys->i_frame_pos = sync.frame;
1206 p_frame = p_sys->p_video_mmap + p_sys->mjpeg_buffers.size * sync.frame;
1208 /* p_frame now points to the data. fix up the Quicktime APP1 marker */
1210 tag = hton16( tag );
1214 p = memmem( p_field, sync.length, &tag, 2 );
1218 p += 2; /* data immediately following EOI */
1220 p_app1 = (struct quicktime_mjpeg_app1 *)(p_field + 6);
1222 i_size = ((uint32_t)(p - p_field));
1223 i_size = hton32( i_size );
1224 memcpy( &p_app1->i_field_size, &i_size, 4 );
1226 while( *p == 0xff && *(p+1) == 0xff )
1229 i_size = ((uint32_t)(p - p_field));
1230 i_size = hton32( i_size );
1231 memcpy( &p_app1->i_padded_field_size, &i_size, 4 );
1235 tag = hton16( tag );
1236 p_field = memmem( p, sync.length - (size_t)(p - p_frame), &tag, 2 );
1240 i_size = (uint32_t)(p_field - p_frame);
1241 i_size = hton32( i_size );
1242 memcpy( &p_app1->i_next_field, &i_size, 4 );
1245 p_app1 = (struct quicktime_mjpeg_app1 *)(p_field + 6);
1247 tag = hton16( tag );
1248 p = memmem( p_field, sync.length - (size_t)(p_field - p_frame),
1253 /* sometimes the second field doesn't have the EOI. just put it
1256 p = p_frame + sync.length;
1257 memcpy( p, &tag, 2 );
1262 i_size = (uint32_t)(p - p_field);
1263 i_size = hton32( i_size );
1264 memcpy( &p_app1->i_field_size, &i_size, 4 );
1265 i_size = (uint32_t)(sync.length - (uint32_t)(p_field - p_frame));
1266 i_size = hton32( i_size );
1267 memcpy( &p_app1->i_padded_field_size, &i_size, 4 );
1270 p_sys->i_video_frame_size = sync.length;
1274 static block_t *GrabVideo( demux_t *p_demux )
1276 demux_sys_t *p_sys = p_demux->p_sys;
1280 if( p_sys->f_fps >= 0.1 && p_sys->i_video_pts > 0 )
1282 mtime_t i_dur = (mtime_t)((double)1000000 / (double)p_sys->f_fps);
1284 /* Did we wait long enough ? (frame rate reduction) */
1285 if( p_sys->i_video_pts + i_dur > mdate() ) return 0;
1288 if( p_sys->b_mjpeg ) p_frame = GrabMJPEG( p_demux );
1289 else p_frame = GrabCapture( p_demux );
1291 if( !p_frame ) return 0;
1293 if( !( p_block = block_New( p_demux, p_sys->i_video_frame_size ) ) )
1295 msg_Warn( p_demux, "cannot get block" );
1299 memcpy( p_block->p_buffer, p_frame, p_sys->i_video_frame_size );
1300 p_sys->i_video_pts = p_block->i_pts = p_block->i_dts = mdate();