1 /*****************************************************************************
2 * v4l2.c : Video4Linux2 input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2007 the VideoLAN team
7 * Author: Benjamin Pracht <bigben at videolan dot org>
8 * Richard Hosking <richard at hovis dot net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
26 * Sections based on the reference V4L2 capture example at
27 * http://v4l2spec.bytesex.org/spec/capture-example.html
31 * TODO: No mjpeg support yet.
32 * TODO: Tuner partial implementation.
33 * TODO: Alsa input support?
36 /*****************************************************************************
38 *****************************************************************************/
41 #include <vlc_access.h>
42 #include <vlc_demux.h>
43 #include <vlc_input.h>
48 #include <sys/ioctl.h>
51 #include <linux/videodev2.h>
53 #include <sys/soundcard.h>
55 /*****************************************************************************
57 *****************************************************************************/
59 static int Open ( vlc_object_t * );
60 static void Close( vlc_object_t * );
62 #define DEV_TEXT N_("Device name")
63 #define DEV_LONGTEXT N_( \
64 "Name of the device to use. " \
65 "If you don't specify anything, /dev/video0 will be used.")
66 #define ADEV_TEXT N_("Audio device name")
67 #define ADEV_LONGTEXT N_( \
68 "Name of the audio device to use. " \
69 "If you don't specify anything, /dev/dsp will be used.")
70 #define STANDARD_TEXT N_( "Standard" )
71 #define STANDARD_LONGTEXT N_( \
72 "Video standard (Default, SECAM, PAL, or NTSC)." )
73 #define CHROMA_TEXT N_("Video input chroma format")
74 #define CHROMA_LONGTEXT N_( \
75 "Force the Video4Linux2 video device to use a specific chroma format " \
76 "(eg. I420, RV24, etc.)")
77 #define INPUT_TEXT N_( "Input" )
78 #define INPUT_LONGTEXT N_( \
79 "Input of the card to use (Usually, 0 = tuner, " \
80 "1 = composite, 2 = svideo)." )
81 #define IOMETHOD_TEXT N_( "IO Method" )
82 #define IOMETHOD_LONGTEXT N_( \
83 "IO Method (READ, MMAP, USERPTR)." )
84 #define WIDTH_TEXT N_( "Width" )
85 #define WIDTH_LONGTEXT N_( \
86 "Force width (-1 for autodetect)." )
87 #define HEIGHT_TEXT N_( "Height" )
88 #define HEIGHT_LONGTEXT N_( \
89 "Force height (-1 for autodetect)." )
90 #define BRIGHTNESS_TEXT N_( "Brightness" )
91 #define BRIGHTNESS_LONGTEXT N_( \
92 "Brightness of the video input." )
93 #define CONTRAST_TEXT N_( "Contrast" )
94 #define CONTRAST_LONGTEXT N_( \
95 "Contrast of the video input." )
96 #define SATURATION_TEXT N_( "Saturation" )
97 #define SATURATION_LONGTEXT N_( \
98 "Saturation of the video input." )
99 #define HUE_TEXT N_( "Hue" )
100 #define HUE_LONGTEXT N_( \
101 "Hue of the video input." )
102 #define FPS_TEXT N_( "Framerate" )
103 #define FPS_LONGTEXT N_( "Framerate to capture, if applicable " \
104 "(-1 for autodetect)." )
105 #define STEREO_TEXT N_( "Stereo" )
106 #define STEREO_LONGTEXT N_( \
107 "Capture the audio stream in stereo." )
108 #define SAMPLERATE_TEXT N_( "Samplerate" )
109 #define SAMPLERATE_LONGTEXT N_( \
110 "Samplerate of the captured audio stream, in Hz (eg: 11025, 22050, 44100, 48000)" )
111 #define CACHING_TEXT N_("Caching value in ms")
112 #define CACHING_LONGTEXT N_( \
113 "Caching value for V4L2 captures. This " \
114 "value should be set in milliseconds." )
122 static int i_standards_list[] =
123 { V4L2_STD_UNKNOWN, V4L2_STD_SECAM, V4L2_STD_PAL, V4L2_STD_NTSC };
124 static const char *psz_standards_list_text[] =
125 { N_("Default"), N_("SECAM"), N_("PAL"), N_("NTSC") };
127 static int i_iomethod_list[] =
128 { IO_METHOD_READ, IO_METHOD_MMAP, IO_METHOD_USERPTR };
129 static const char *psz_iomethod_list_text[] =
130 { N_("READ"), N_("MMAP"), N_("USERPTR") };
133 set_shortname( _("Video4Linux2") );
134 set_description( _("Video4Linux2 input") );
135 set_category( CAT_INPUT );
136 set_subcategory( SUBCAT_INPUT_ACCESS );
138 add_string( "v4l2-dev", "/dev/video0", 0, DEV_TEXT, DEV_LONGTEXT,
140 add_string( "v4l2-adev", "/dev/dsp", 0, ADEV_TEXT, ADEV_LONGTEXT,
142 add_integer( "v4l2-standard", 0, NULL, STANDARD_TEXT, STANDARD_LONGTEXT,
144 change_integer_list( i_standards_list, psz_standards_list_text, 0 );
145 add_string( "v4l2-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
147 add_integer( "v4l2-input", 0, NULL, INPUT_TEXT, INPUT_LONGTEXT,
149 add_integer( "v4l2-io", IO_METHOD_MMAP, NULL, IOMETHOD_TEXT,
150 IOMETHOD_LONGTEXT, VLC_TRUE );
151 change_integer_list( i_iomethod_list, psz_iomethod_list_text, 0 );
152 add_integer( "v4l2-width", 0, NULL, WIDTH_TEXT,
153 WIDTH_LONGTEXT, VLC_TRUE );
154 add_integer( "v4l2-height", 0, NULL, HEIGHT_TEXT,
155 HEIGHT_LONGTEXT, VLC_TRUE );
156 add_integer( "v4l2-brightness", -1, NULL, BRIGHTNESS_TEXT,
157 BRIGHTNESS_LONGTEXT, VLC_TRUE );
158 add_integer( "v4l2-contrast", -1, NULL, CONTRAST_TEXT,
159 CONTRAST_LONGTEXT, VLC_TRUE );
160 add_integer( "v4l2-saturation", -1, NULL, SATURATION_TEXT,
161 SATURATION_LONGTEXT, VLC_TRUE );
162 add_integer( "v4l2-hue", -1, NULL, HUE_TEXT,
163 HUE_LONGTEXT, VLC_TRUE );
164 add_float( "v4l2-fps", 0, NULL, FPS_TEXT, FPS_LONGTEXT, VLC_TRUE );
165 add_bool( "v4l2-stereo", VLC_TRUE, NULL, STEREO_TEXT, STEREO_LONGTEXT,
167 add_integer( "v4l2-samplerate", 48000, NULL, SAMPLERATE_TEXT,
168 SAMPLERATE_LONGTEXT, VLC_TRUE );
169 add_integer( "v4l2-caching", DEFAULT_PTS_DELAY / 1000, NULL,
170 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
172 add_shortcut( "v4l2" );
173 set_capability( "access_demux", 10 );
174 set_callbacks( Open, Close );
177 /*****************************************************************************
178 * Access: local prototypes
179 *****************************************************************************/
181 static void ParseMRL( demux_t * );
183 static int Control( demux_t *, int, va_list );
185 static int Demux( demux_t * );
186 static block_t* GrabVideo( demux_t *p_demux );
187 static block_t* ProcessVideoFrame( demux_t *p_demux, uint8_t *p_frame );
188 static block_t* GrabAudio( demux_t *p_demux );
190 vlc_bool_t IsPixelFormatSupported( demux_t *p_demux, unsigned int i_pixelformat );
192 static int OpenVideoDev( demux_t *, char *psz_device );
193 static int OpenAudioDev( demux_t *, char *psz_device );
194 static vlc_bool_t ProbeVideoDev( demux_t *, char *psz_device );
195 static vlc_bool_t ProbeAudioDev( demux_t *, char *psz_device );
201 } v4l2chroma_to_fourcc[] =
203 { V4L2_PIX_FMT_GREY, VLC_FOURCC( 'G', 'R', 'E', 'Y' ) },
204 { V4L2_PIX_FMT_HI240, VLC_FOURCC( 'I', '2', '4', '0' ) },
205 { V4L2_PIX_FMT_RGB565, VLC_FOURCC( 'R', 'V', '1', '6' ) },
206 { V4L2_PIX_FMT_RGB555, VLC_FOURCC( 'R', 'V', '1', '5' ) },
207 { V4L2_PIX_FMT_BGR24, VLC_FOURCC( 'R', 'V', '2', '4' ) },
208 { V4L2_PIX_FMT_BGR32, VLC_FOURCC( 'R', 'V', '3', '2' ) },
209 { V4L2_PIX_FMT_YUYV, VLC_FOURCC( 'Y', 'U', 'Y', '2' ) },
210 { V4L2_PIX_FMT_YUYV, VLC_FOURCC( 'Y', 'U', 'Y', 'V' ) },
211 { V4L2_PIX_FMT_UYVY, VLC_FOURCC( 'U', 'Y', 'V', 'Y' ) },
212 { V4L2_PIX_FMT_Y41P, VLC_FOURCC( 'I', '4', '1', 'N' ) },
213 { V4L2_PIX_FMT_YUV422P, VLC_FOURCC( 'I', '4', '2', '2' ) },
214 { V4L2_PIX_FMT_YVU420, VLC_FOURCC( 'I', '4', '2', '0' ) },
215 { V4L2_PIX_FMT_YUV411P, VLC_FOURCC( 'I', '4', '1', '1' ) },
216 { V4L2_PIX_FMT_YUV410, VLC_FOURCC( 'I', '4', '1', '0' ) },
229 char *psz_device; /* Main device from MRL, can be video or audio */
237 char *psz_requested_chroma;
244 struct v4l2_capability dev_cap;
247 struct v4l2_input *p_inputs;
248 int i_selected_input;
251 struct v4l2_standard *p_standards;
252 v4l2_std_id i_selected_standard_id;
255 /* V4L2 devices cannot have more than 32 audio inputs */
256 struct v4l2_audio p_audios[32];
259 struct v4l2_tuner *p_tuners;
262 struct v4l2_fmtdesc *p_codecs;
264 struct buffer_t *p_buffers;
265 unsigned int i_nbuffers;
269 float f_fps; /* <= 0.0 mean to grab at full rate */
270 mtime_t i_video_pts; /* only used when f_fps > 0 */
279 int i_video_frame_size;
281 es_out_id_t *p_es_video;
286 int i_audio_max_frame_size;
287 block_t *p_block_audio;
288 es_out_id_t *p_es_audio;
291 /*****************************************************************************
292 * Open: opens v4l2 device
293 *****************************************************************************
295 * url: <video device>::::
297 *****************************************************************************/
298 static int Open( vlc_object_t *p_this )
300 demux_t *p_demux = (demux_t*)p_this;
304 /* Only when selected */
305 if( *p_demux->psz_access == '\0' ) return VLC_EGENERIC;
308 p_demux->pf_control = Control;
309 p_demux->pf_demux = Demux;
310 p_demux->info.i_update = 0;
311 p_demux->info.i_title = 0;
312 p_demux->info.i_seekpoint = 0;
314 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
315 if( p_sys == NULL ) return VLC_ENOMEM;
317 p_sys->i_video_pts = -1;
319 var_Create( p_demux, "v4l2-standard", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
320 var_Get( p_demux, "v4l2-standard", &val );
321 p_sys->i_selected_standard_id = i_standards_list[val.i_int];
323 p_sys->i_selected_input = var_CreateGetInteger( p_demux, "v4l2-input" );
325 p_sys->io = var_CreateGetInteger( p_demux, "v4l2-io" );
327 p_sys->i_width = var_CreateGetInteger( p_demux, "v4l2-width" );
328 p_sys->i_height = var_CreateGetInteger( p_demux, "v4l2-height" );
330 p_sys->i_brightness = var_CreateGetInteger( p_demux, "v4l2-brightness" );
331 p_sys->i_contrast = var_CreateGetInteger( p_demux, "v4l2-contrast" );
332 p_sys->i_saturation = var_CreateGetInteger( p_demux, "v4l2-saturation" );
333 p_sys->i_hue = var_CreateGetInteger( p_demux, "v4l2-hue" );
335 var_Create( p_demux, "v4l2-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
336 var_Get( p_demux, "v4l2-fps", &val );
337 p_sys->f_fps = val.f_float;
339 var_Create( p_demux, "v4l2-samplerate", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
340 var_Get( p_demux, "v4l2-samplerate", &val );
341 p_sys->i_sample_rate = val.i_int;
343 p_sys->psz_requested_chroma = var_CreateGetString( p_demux, "v4l2-chroma" );
345 var_Create( p_demux, "v4l2-stereo", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
346 var_Get( p_demux, "v4l2-stereo", &val );
347 p_sys->b_stereo = val.b_bool;
349 p_sys->i_pts = var_CreateGetInteger( p_demux, "v4l2-caching" );
351 p_sys->psz_device = p_sys->psz_vdev = p_sys->psz_adev = NULL;
352 p_sys->i_fd_video = -1;
353 p_sys->i_fd_audio = -1;
355 p_sys->p_es_video = p_sys->p_es_audio = 0;
356 p_sys->p_block_audio = 0;
360 /* Find main device (video or audio) */
361 if( p_sys->psz_device && *p_sys->psz_device )
363 msg_Dbg( p_demux, "main device='%s'", p_sys->psz_device );
365 /* Try to open as video device */
366 msg_Dbg( p_demux, "trying device '%s' as video", p_sys->psz_device );
367 if( ProbeVideoDev( p_demux, p_sys->psz_device ) )
369 msg_Dbg( p_demux, "'%s' is a video device", p_sys->psz_device );
370 /* Device was a video device */
371 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
372 p_sys->psz_vdev = p_sys->psz_device;
373 p_sys->psz_device = NULL;
374 p_sys->i_fd_video = OpenVideoDev( p_demux, p_sys->psz_vdev );
375 if( p_sys->i_fd_video < 0 )
383 /* Try to open as audio device */
384 msg_Dbg( p_demux, "trying device '%s' as audio", p_sys->psz_device );
385 if( ProbeAudioDev( p_demux, p_sys->psz_device ) )
387 msg_Dbg( p_demux, "'%s' is an audio device", p_sys->psz_device );
388 /* Device was an audio device */
389 if( p_sys->psz_adev ) free( p_sys->psz_adev );
390 p_sys->psz_adev = p_sys->psz_device;
391 p_sys->psz_device = NULL;
392 p_sys->i_fd_audio = OpenAudioDev( p_demux, p_sys->psz_adev );
393 if( p_sys->i_fd_audio < 0 )
402 /* If no device opened, only continue if the access was forced */
403 if( p_sys->i_fd_video < 0 && p_sys->i_fd_audio < 0 )
405 if( strcmp( p_demux->psz_access, "v4l2" ) )
412 /* Find video device */
413 if( p_sys->i_fd_video < 0 )
415 if( !p_sys->psz_vdev || !*p_sys->psz_vdev )
417 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
418 p_sys->psz_vdev = var_CreateGetString( p_demux, "v4l2-dev" );
421 msg_Dbg( p_demux, "opening '%s' as video", p_sys->psz_vdev );
422 if( p_sys->psz_vdev && *p_sys->psz_vdev && ProbeVideoDev( p_demux, p_sys->psz_vdev ) )
424 p_sys->i_fd_video = OpenVideoDev( p_demux, p_sys->psz_vdev );
428 /* Find audio device */
429 if( p_sys->i_fd_audio < 0 )
431 if( !p_sys->psz_adev || !*p_sys->psz_adev )
433 if( p_sys->psz_adev ) free( p_sys->psz_adev );
434 p_sys->psz_adev = var_CreateGetString( p_demux, "v4l2-adev" );
437 msg_Dbg( p_demux, "opening '%s' as audio", p_sys->psz_adev );
438 if( p_sys->psz_adev && *p_sys->psz_adev && ProbeAudioDev( p_demux, p_sys->psz_adev ) )
440 p_sys->i_fd_audio = OpenAudioDev( p_demux, p_sys->psz_adev );
444 if( p_sys->i_fd_video < 0 && p_sys->i_fd_audio < 0 )
453 /*****************************************************************************
454 * ParseMRL: parse the options contained in the MRL
455 *****************************************************************************/
456 static void ParseMRL( demux_t *p_demux )
458 demux_sys_t *p_sys = p_demux->p_sys;
460 char *psz_dup = strdup( p_demux->psz_path );
461 char *psz_parser = psz_dup;
463 while( *psz_parser && *psz_parser != ':' )
468 if( *psz_parser == ':' )
473 *psz_parser++ = '\0';
475 if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) ) )
479 psz_parser += strlen( "adev=" );
480 if( strchr( psz_parser, ':' ) )
482 i_len = strchr( psz_parser, ':' ) - psz_parser;
486 i_len = strlen( psz_parser );
489 p_sys->psz_adev = strndup( psz_parser, i_len );
493 else if( !strncmp( psz_parser, "standard=", strlen( "standard=" ) ) )
495 psz_parser += strlen( "standard=" );
496 if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
498 p_sys->i_selected_standard_id = V4L2_STD_PAL;
499 psz_parser += strlen( "pal" );
501 else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
503 p_sys->i_selected_standard_id = V4L2_STD_NTSC;
504 psz_parser += strlen( "ntsc" );
506 else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
508 p_sys->i_selected_standard_id = V4L2_STD_SECAM;
509 psz_parser += strlen( "secam" );
511 else if( !strncmp( psz_parser, "default", strlen( "default" ) ) )
513 p_sys->i_selected_standard_id = V4L2_STD_UNKNOWN;
514 psz_parser += strlen( "default" );
518 p_sys->i_selected_standard_id = i_standards_list[strtol( psz_parser, &psz_parser, 0 )];
521 else if( !strncmp( psz_parser, "chroma=", strlen( "chroma=" ) ) )
525 psz_parser += strlen( "chroma=" );
526 if( strchr( psz_parser, ':' ) )
528 i_len = strchr( psz_parser, ':' ) - psz_parser;
532 i_len = strlen( psz_parser );
535 if( p_sys->psz_requested_chroma ) free( p_sys->psz_requested_chroma );
536 p_sys->psz_requested_chroma = strndup( psz_parser, i_len );
540 else if( !strncmp( psz_parser, "input=", strlen( "input=" ) ) )
542 p_sys->i_selected_input = strtol( psz_parser + strlen( "input=" ),
545 else if( !strncmp( psz_parser, "fps=", strlen( "fps=" ) ) )
547 p_sys->f_fps = strtof( psz_parser + strlen( "fps=" ),
550 else if( !strncmp( psz_parser, "io=", strlen( "io=" ) ) )
552 psz_parser += strlen( "io=" );
553 if( !strncmp( psz_parser, "read", strlen( "read" ) ) )
555 p_sys->io = IO_METHOD_READ;
556 psz_parser += strlen( "read" );
558 else if( !strncmp( psz_parser, "mmap", strlen( "mmap" ) ) )
560 p_sys->io = IO_METHOD_MMAP;
561 psz_parser += strlen( "mmap" );
563 else if( !strncmp( psz_parser, "userptr", strlen( "userptr" ) ) )
565 p_sys->io = IO_METHOD_USERPTR;
566 psz_parser += strlen( "userptr" );
570 p_sys->io = strtol( psz_parser, &psz_parser, 0 );
573 else if( !strncmp( psz_parser, "width=",
574 strlen( "width=" ) ) )
577 strtol( psz_parser + strlen( "width=" ),
580 else if( !strncmp( psz_parser, "height=",
581 strlen( "height=" ) ) )
584 strtol( psz_parser + strlen( "height=" ),
587 else if( !strncmp( psz_parser, "brightness=",
588 strlen( "brightness=" ) ) )
590 p_sys->i_brightness =
591 strtol( psz_parser + strlen( "brightness=" ),
594 else if( !strncmp( psz_parser, "contrast=",
595 strlen( "contrast=" ) ) )
598 strtol( psz_parser + strlen( "contrast=" ),
601 else if( !strncmp( psz_parser, "saturation=",
602 strlen( "saturation=" ) ) )
604 p_sys->i_saturation =
605 strtol( psz_parser + strlen( "saturation=" ),
608 else if( !strncmp( psz_parser, "hue=",
612 strtol( psz_parser + strlen( "hue=" ),
615 else if( !strncmp( psz_parser, "samplerate=",
616 strlen( "samplerate=" ) ) )
618 p_sys->i_sample_rate =
619 strtol( psz_parser + strlen( "samplerate=" ),
622 else if( !strncmp( psz_parser, "stereo", strlen( "stereo" ) ) )
624 psz_parser += strlen( "stereo" );
625 p_sys->b_stereo = VLC_TRUE;
627 else if( !strncmp( psz_parser, "mono", strlen( "mono" ) ) )
629 psz_parser += strlen( "mono" );
630 p_sys->b_stereo = VLC_FALSE;
632 else if( !strncmp( psz_parser, "caching=", strlen( "caching=" ) ) )
634 p_sys->i_pts = strtol( psz_parser + strlen( "caching=" ),
639 msg_Warn( p_demux, "unknown option" );
642 while( *psz_parser && *psz_parser != ':' )
647 if( *psz_parser == '\0' )
657 p_sys->psz_device = strdup( psz_dup );
659 if( psz_dup ) free( psz_dup );
662 /*****************************************************************************
663 * Close: close device, free resources
664 *****************************************************************************/
665 static void Close( vlc_object_t *p_this )
667 demux_t *p_demux = (demux_t *)p_this;
668 demux_sys_t *p_sys = p_demux->p_sys;
669 struct v4l2_buffer buf;
670 enum v4l2_buf_type buf_type;
673 /* Stop video capture */
674 if( p_sys->i_fd_video >= 0 )
683 case IO_METHOD_USERPTR:
684 /* Some drivers 'hang' internally if this is not done before streamoff */
685 for( unsigned int i = 0; i < p_sys->i_nbuffers; i++ )
687 memset( &buf, 0, sizeof(buf) );
688 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
689 buf.memory = ( p_sys->io == IO_METHOD_USERPTR ) ?
690 V4L2_MEMORY_USERPTR : V4L2_MEMORY_MMAP;
691 ioctl( p_sys->i_fd_video, VIDIOC_DQBUF, &buf ); /* ignore result */
694 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
695 if( ioctl( p_sys->i_fd_video, VIDIOC_STREAMOFF, &buf_type ) < 0 ) {
696 msg_Err( p_demux, "VIDIOC_STREAMOFF failed" );
703 /* Free Video Buffers */
704 if( p_sys->p_buffers ) {
708 free( p_sys->p_buffers[0].start );
712 for( i = 0; i < p_sys->i_nbuffers; ++i )
714 if( munmap( p_sys->p_buffers[i].start, p_sys->p_buffers[i].length ) )
716 msg_Err( p_demux, "munmap failed" );
721 case IO_METHOD_USERPTR:
722 for( i = 0; i < p_sys->i_nbuffers; ++i )
724 free( p_sys->p_buffers[i].orig_userp );
728 free( p_sys->p_buffers );
732 if( p_sys->i_fd_video >= 0 ) close( p_sys->i_fd_video );
733 if( p_sys->i_fd_audio >= 0 ) close( p_sys->i_fd_audio );
735 if( p_sys->p_block_audio ) block_Release( p_sys->p_block_audio );
736 if( p_sys->psz_device ) free( p_sys->psz_device );
737 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
738 if( p_sys->psz_adev ) free( p_sys->psz_adev );
739 if( p_sys->p_standards ) free( p_sys->p_standards );
740 if( p_sys->p_inputs ) free( p_sys->p_inputs );
741 if( p_sys->p_tuners ) free( p_sys->p_tuners );
742 if( p_sys->p_codecs ) free( p_sys->p_codecs );
743 if( p_sys->psz_requested_chroma ) free( p_sys->psz_requested_chroma );
748 /*****************************************************************************
750 *****************************************************************************/
751 static int Control( demux_t *p_demux, int i_query, va_list args )
753 demux_sys_t *p_sys = p_demux->p_sys;
759 /* Special for access_demux */
760 case DEMUX_CAN_PAUSE:
761 case DEMUX_SET_PAUSE_STATE:
762 case DEMUX_CAN_CONTROL_PACE:
763 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
767 case DEMUX_GET_PTS_DELAY:
768 pi64 = (int64_t*)va_arg( args, int64_t * );
769 *pi64 = (int64_t)p_sys->i_pts * 1000;
773 pi64 = (int64_t*)va_arg( args, int64_t * );
777 /* TODO implement others */
785 /*****************************************************************************
786 * Demux: Processes the audio or video frame
787 *****************************************************************************/
788 static int Demux( demux_t *p_demux )
790 demux_sys_t *p_sys = p_demux->p_sys;
791 es_out_id_t *p_es = p_sys->p_es_audio;
792 block_t *p_block = NULL;
794 /* Try grabbing audio frames first */
795 if( p_sys->i_fd_audio < 0 || !( p_block = GrabAudio( p_demux ) ) )
797 /* Try grabbing video frame */
798 p_es = p_sys->p_es_video;
799 if( p_sys->i_fd_video > 0 ) p_block = GrabVideo( p_demux );
804 /* Sleep so we do not consume all the cpu, 10ms seems
805 * like a good value (100fps) */
810 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
811 es_out_Send( p_demux->out, p_es, p_block );
816 /*****************************************************************************
817 * GrabVideo: Grab a video frame
818 *****************************************************************************/
819 static block_t* GrabVideo( demux_t *p_demux )
821 demux_sys_t *p_sys = p_demux->p_sys;
823 block_t *p_block = NULL;
824 struct v4l2_buffer buf;
826 if( p_sys->f_fps >= 0.1 && p_sys->i_video_pts > 0 )
828 mtime_t i_dur = (mtime_t)((double)1000000 / (double)p_sys->f_fps);
830 /* Did we wait long enough ? (frame rate reduction) */
831 if( p_sys->i_video_pts + i_dur > mdate() ) return 0;
834 /* Grab Video Frame */
838 if( read( p_sys->i_fd_video, p_sys->p_buffers[0].start, p_sys->p_buffers[0].length ) )
845 /* Could ignore EIO, see spec. */
848 msg_Err( p_demux, "Failed to read frame" );
853 p_block = ProcessVideoFrame( p_demux, (uint8_t*)p_sys->p_buffers[0].start );
854 if( !p_block ) return 0;
859 memset( &buf, 0, sizeof(buf) );
860 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
861 buf.memory = V4L2_MEMORY_MMAP;
863 /* Wait for next frame */
864 if (ioctl( p_sys->i_fd_video, VIDIOC_DQBUF, &buf ) < 0 )
871 /* Could ignore EIO, see spec. */
874 msg_Err( p_demux, "Failed to wait (VIDIOC_DQBUF)" );
879 if( buf.index >= p_sys->i_nbuffers ) {
880 msg_Err( p_demux, "Failed capturing new frame as i>=nbuffers" );
884 p_block = ProcessVideoFrame( p_demux, p_sys->p_buffers[buf.index].start );
885 if( !p_block ) return 0;
888 if( ioctl( p_sys->i_fd_video, VIDIOC_QBUF, &buf ) < 0 )
890 msg_Err (p_demux, "Failed to unlock (VIDIOC_QBUF)");
896 case IO_METHOD_USERPTR:
897 memset( &buf, 0, sizeof(buf) );
898 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
899 buf.memory = V4L2_MEMORY_USERPTR;
901 /* Wait for next frame */
902 if (ioctl( p_sys->i_fd_video, VIDIOC_DQBUF, &buf ) < 0 )
909 /* Could ignore EIO, see spec. */
912 msg_Err( p_demux, "Failed to wait (VIDIOC_DQBUF)" );
919 for( i = 0; i < p_sys->i_nbuffers; i++ )
921 if( buf.m.userptr == (unsigned long)p_sys->p_buffers[i].start &&
922 buf.length == p_sys->p_buffers[i].length ) break;
925 if( i >= p_sys->i_nbuffers )
927 msg_Err( p_demux, "Failed capturing new frame as i>=nbuffers" );
931 p_block = ProcessVideoFrame( p_demux, (uint8_t*)buf.m.userptr );
932 if( !p_block ) return 0;
935 if( ioctl( p_sys->i_fd_video, VIDIOC_QBUF, &buf ) < 0 )
937 msg_Err (p_demux, "Failed to unlock (VIDIOC_QBUF)");
946 p_sys->i_video_pts = p_block->i_pts = p_block->i_dts = mdate();
951 /*****************************************************************************
952 * ProcessVideoFrame: Helper function to take a buffer and copy it into
954 *****************************************************************************/
955 static block_t* ProcessVideoFrame( demux_t *p_demux, uint8_t *p_frame )
957 demux_sys_t *p_sys = p_demux->p_sys;
960 if( !p_frame ) return 0;
963 if( !( p_block = block_New( p_demux, p_sys->i_video_frame_size ) ) )
965 msg_Warn( p_demux, "Cannot get new block" );
970 memcpy( p_block->p_buffer, p_frame, p_sys->i_video_frame_size );
975 /*****************************************************************************
976 * GrabAudio: Grab an audio frame
977 *****************************************************************************/
978 static block_t* GrabAudio( demux_t *p_demux )
980 demux_sys_t *p_sys = p_demux->p_sys;
981 struct audio_buf_info buf_info;
982 int i_read, i_correct;
985 /* Copied from v4l.c */
987 if( p_sys->p_block_audio ) p_block = p_sys->p_block_audio;
988 else p_block = block_New( p_demux, p_sys->i_audio_max_frame_size );
992 msg_Warn( p_demux, "cannot get block" );
996 p_sys->p_block_audio = p_block;
998 i_read = read( p_sys->i_fd_audio, p_block->p_buffer,
999 p_sys->i_audio_max_frame_size );
1001 if( i_read <= 0 ) return 0;
1003 p_block->i_buffer = i_read;
1004 p_sys->p_block_audio = 0;
1006 /* Correct the date because of kernel buffering */
1008 if( ioctl( p_sys->i_fd_audio, SNDCTL_DSP_GETISPACE, &buf_info ) == 0 )
1010 i_correct += buf_info.bytes;
1014 p_block->i_pts = p_block->i_dts =
1015 mdate() - I64C(1000000) * (mtime_t)i_correct /
1016 2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate;
1021 /*****************************************************************************
1022 * Helper function to initalise video IO using the Read method
1023 *****************************************************************************/
1024 static int InitRead( demux_t *p_demux, int i_fd, unsigned int i_buffer_size )
1026 demux_sys_t *p_sys = p_demux->p_sys;
1028 p_sys->p_buffers = calloc( 1, sizeof( *p_sys->p_buffers ) );
1029 if( !p_sys->p_buffers )
1031 msg_Err( p_demux, "Out of memory" );
1035 p_sys->p_buffers[0].length = i_buffer_size;
1036 p_sys->p_buffers[0].start = malloc( i_buffer_size );
1037 if( !p_sys->p_buffers[0].start )
1039 msg_Err( p_demux, "Out of memory" );
1046 return VLC_EGENERIC;
1050 /*****************************************************************************
1051 * Helper function to initalise video IO using the mmap method
1052 *****************************************************************************/
1053 static int InitMmap( demux_t *p_demux, int i_fd )
1055 demux_sys_t *p_sys = p_demux->p_sys;
1056 struct v4l2_requestbuffers req;
1058 memset( &req, 0, sizeof(req) );
1060 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1061 req.memory = V4L2_MEMORY_MMAP;
1063 if( ioctl( i_fd, VIDIOC_REQBUFS, &req ) < 0 )
1065 msg_Err( p_demux, "device does not support mmap i/o" );
1071 msg_Err( p_demux, "Insufficient buffer memory" );
1075 p_sys->p_buffers = calloc( req.count, sizeof( *p_sys->p_buffers ) );
1076 if( !p_sys->p_buffers )
1078 msg_Err( p_demux, "Out of memory" );
1082 for( p_sys->i_nbuffers = 0; p_sys->i_nbuffers < req.count; ++p_sys->i_nbuffers )
1084 struct v4l2_buffer buf;
1086 memset( &buf, 0, sizeof(buf) );
1087 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1088 buf.memory = V4L2_MEMORY_MMAP;
1089 buf.index = p_sys->i_nbuffers;
1091 if( ioctl( i_fd, VIDIOC_QUERYBUF, &buf ) < 0 )
1093 msg_Err( p_demux, "VIDIOC_QUERYBUF" );
1097 p_sys->p_buffers[p_sys->i_nbuffers].length = buf.length;
1098 p_sys->p_buffers[p_sys->i_nbuffers].start =
1099 mmap( NULL, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, i_fd, buf.m.offset );
1101 if( p_sys->p_buffers[p_sys->i_nbuffers].start == MAP_FAILED )
1103 msg_Err( p_demux, "mmap failed (%m)" );
1111 return VLC_EGENERIC;
1115 /*****************************************************************************
1116 * Helper function to initalise video IO using the userbuf method
1117 *****************************************************************************/
1118 static int InitUserP( demux_t *p_demux, int i_fd, unsigned int i_buffer_size )
1120 demux_sys_t *p_sys = p_demux->p_sys;
1121 struct v4l2_requestbuffers req;
1122 unsigned int i_page_size;
1124 i_page_size = getpagesize();
1125 i_buffer_size = ( i_buffer_size + i_page_size - 1 ) & ~( i_page_size - 1);
1127 memset( &req, 0, sizeof(req) );
1129 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1130 req.memory = V4L2_MEMORY_USERPTR;
1132 if( ioctl( i_fd, VIDIOC_REQBUFS, &req ) < 0 )
1134 msg_Err( p_demux, "device does not support user pointer i/o" );
1138 p_sys->p_buffers = calloc( 4, sizeof( *p_sys->p_buffers ) );
1139 if( !p_sys->p_buffers )
1141 msg_Err( p_demux, "Out of memory" );
1145 for( p_sys->i_nbuffers = 0; p_sys->i_nbuffers < 4; ++p_sys->i_nbuffers )
1147 p_sys->p_buffers[p_sys->i_nbuffers].length = i_buffer_size;
1148 p_sys->p_buffers[p_sys->i_nbuffers].start =
1149 vlc_memalign( &p_sys->p_buffers[p_sys->i_nbuffers].orig_userp,
1150 /* boundary */ i_page_size, i_buffer_size );
1152 if( !p_sys->p_buffers[p_sys->i_nbuffers].start )
1154 msg_Err( p_demux, "out of memory" );
1162 return VLC_EGENERIC;
1166 /*****************************************************************************
1167 * IsPixelFormatSupported: returns true if the specified V4L2 pixel format is
1168 * in the array of supported formats returned by the driver
1169 *****************************************************************************/
1170 vlc_bool_t IsPixelFormatSupported( demux_t *p_demux, unsigned int i_pixelformat )
1172 demux_sys_t *p_sys = p_demux->p_sys;
1174 for( int i_index = 0; i_index < p_sys->i_codec; i_index++ )
1176 if( p_sys->p_codecs[i_index].pixelformat == i_pixelformat ) return VLC_TRUE;
1182 /*****************************************************************************
1183 * OpenVideoDev: open and set up the video device and probe for capabilities
1184 *****************************************************************************/
1185 int OpenVideoDev( demux_t *p_demux, char *psz_device )
1188 demux_sys_t *p_sys = p_demux->p_sys;
1189 struct v4l2_cropcap cropcap;
1190 struct v4l2_crop crop;
1191 struct v4l2_format fmt;
1193 enum v4l2_buf_type buf_type;
1195 if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
1197 msg_Err( p_demux, "cannot open device (%m)" );
1201 /* Select standard */
1203 if( p_sys->i_selected_standard_id != V4L2_STD_UNKNOWN )
1205 if( ioctl( i_fd, VIDIOC_S_STD, &p_sys->i_selected_standard_id ) < 0 )
1207 msg_Err( p_demux, "cannot set standard (%m)" );
1210 msg_Dbg( p_demux, "Set standard" );
1215 if( p_sys->i_selected_input > p_sys->i_input )
1217 msg_Warn( p_demux, "invalid input. Using the default one" );
1218 p_sys->i_selected_input = 0;
1221 if( ioctl( i_fd, VIDIOC_S_INPUT, &p_sys->i_selected_input ) < 0 )
1223 msg_Err( p_demux, "cannot set input (%m)" );
1227 /* Verify device support for the various IO methods */
1230 case IO_METHOD_READ:
1231 if( !(p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE) )
1233 msg_Err( p_demux, "device does not support read i/o" );
1238 case IO_METHOD_MMAP:
1239 case IO_METHOD_USERPTR:
1240 if( !(p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING) )
1242 msg_Err( p_demux, "device does not support streaming i/o" );
1248 msg_Err( p_demux, "io method not supported" );
1252 /* Reset Cropping */
1253 memset( &cropcap, 0, sizeof(cropcap) );
1254 cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1255 if( ioctl( i_fd, VIDIOC_CROPCAP, &cropcap ) >= 0 )
1257 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1258 crop.c = cropcap.defrect; /* reset to default */
1259 if( ioctl( i_fd, VIDIOC_S_CROP, &crop ) < 0 )
1264 /* Cropping not supported. */
1267 /* Errors ignored. */
1273 /* Try and find default resolution if not specified */
1274 memset( &fmt, 0, sizeof(fmt) );
1275 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1277 if( p_sys->i_width <= 0 || p_sys->i_height <= 0 )
1279 if( ioctl( i_fd, VIDIOC_G_FMT, &fmt ) < 0 )
1281 msg_Err( p_demux, "Cannot get default width and height." );
1285 p_sys->i_width = fmt.fmt.pix.width;
1286 p_sys->i_height = fmt.fmt.pix.height;
1288 if( fmt.fmt.pix.field == V4L2_FIELD_ALTERNATE )
1290 p_sys->i_height = p_sys->i_height * 2;
1295 msg_Dbg( p_demux, "trying specified size %dx%d", p_sys->i_width, p_sys->i_height );
1298 fmt.fmt.pix.width = p_sys->i_width;
1299 fmt.fmt.pix.height = p_sys->i_height;
1300 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
1302 /* Test and set Chroma */
1303 fmt.fmt.pix.pixelformat = 0;
1304 if( p_sys->psz_requested_chroma && strlen( p_sys->psz_requested_chroma ) > 0 )
1306 /* User specified chroma */
1307 if( strlen( p_sys->psz_requested_chroma ) >= 4 )
1309 int i_requested_fourcc = VLC_FOURCC(
1310 p_sys->psz_requested_chroma[0], p_sys->psz_requested_chroma[1],
1311 p_sys->psz_requested_chroma[2], p_sys->psz_requested_chroma[3] );
1312 for( int i = 0; v4l2chroma_to_fourcc[i].i_v4l2 != 0; i++ )
1314 if( v4l2chroma_to_fourcc[i].i_fourcc == i_requested_fourcc )
1316 fmt.fmt.pix.pixelformat = v4l2chroma_to_fourcc[i].i_v4l2;
1321 /* Try and set user chroma */
1322 if( !IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat ) || ( fmt.fmt.pix.pixelformat && ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 ) )
1324 msg_Warn( p_demux, "Driver is unable to use specified chroma %s. Trying defaults.", p_sys->psz_requested_chroma );
1325 fmt.fmt.pix.pixelformat = 0;
1329 /* If no user specified chroma, find best */
1330 if( !fmt.fmt.pix.pixelformat )
1332 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YVU420;
1333 if( !IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat ) || ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 )
1335 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
1336 if( !IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat ) || ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 )
1338 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1339 if( !IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat ) || ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 )
1341 msg_Err( p_demux, "Could not select any of the default chromas!" );
1348 /* Reassign width, height and chroma incase driver override */
1349 p_sys->i_width = fmt.fmt.pix.width;
1350 p_sys->i_height = fmt.fmt.pix.height;
1352 /* Look up final fourcc */
1353 p_sys->i_fourcc = 0;
1354 for( int i = 0; v4l2chroma_to_fourcc[i].i_fourcc != 0; i++ )
1356 if( v4l2chroma_to_fourcc[i].i_v4l2 == fmt.fmt.pix.pixelformat )
1358 p_sys->i_fourcc = v4l2chroma_to_fourcc[i].i_fourcc;
1363 /* Buggy driver paranoia */
1364 i_min = fmt.fmt.pix.width * 2;
1365 if( fmt.fmt.pix.bytesperline < i_min )
1366 fmt.fmt.pix.bytesperline = i_min;
1367 i_min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
1368 if( fmt.fmt.pix.sizeimage < i_min )
1369 fmt.fmt.pix.sizeimage = i_min;
1371 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
1372 /* This is new in Linux 2.6.19 */
1373 /* List supported frame rates */
1374 struct v4l2_frmivalenum frmival;
1376 frmival.pixel_format = fmt.fmt.pix.pixelformat;
1377 frmival.width = p_sys->i_width;
1378 frmival.height = p_sys->i_height;
1379 if( ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival ) >= 0 )
1382 memset( &sz_fourcc, 0, sizeof( sz_fourcc ) );
1383 vlc_fourcc_to_char( p_sys->i_fourcc, &sz_fourcc );
1384 msg_Dbg( p_demux, "supported frame intervals for %4s, %dx%d:",
1385 sz_fourcc, frmival.width, frmival.height );
1386 switch( frmival.type )
1388 case V4L2_FRMIVAL_TYPE_DISCRETE:
1391 msg_Dbg( p_demux, " supported frame interval: %d/%d",
1392 frmival.discrete.numerator,
1393 frmival.discrete.denominator );
1395 } while( ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival ) >= 0 );
1397 case V4L2_FRMIVAL_TYPE_STEPWISE:
1398 msg_Dbg( p_demux, " supported frame intervals: %d/%d to "
1399 "%d/%d using %d/%d increments",
1400 frmival.stepwise.min.numerator,
1401 frmival.stepwise.min.denominator,
1402 frmival.stepwise.max.numerator,
1403 frmival.stepwise.max.denominator,
1404 frmival.stepwise.step.numerator,
1405 frmival.stepwise.step.denominator );
1407 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
1408 msg_Dbg( p_demux, " supported frame intervals: %d/%d to %d/%d",
1409 frmival.stepwise.min.numerator,
1410 frmival.stepwise.min.denominator,
1411 frmival.stepwise.max.numerator,
1412 frmival.stepwise.max.denominator );
1418 /* Set picture controls... */
1419 struct v4l2_control control;
1422 memset( &control, 0, sizeof( control ) );
1423 control.id = V4L2_CID_BRIGHTNESS;
1424 if( p_sys->i_brightness >= 0 )
1426 control.value = p_sys->i_brightness;
1427 if( ioctl( i_fd, VIDIOC_S_CTRL, &control ) < 0 )
1429 msg_Err( p_demux, "unable to set brightness to %d (%m)", p_sys->i_brightness );
1433 if( ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0 )
1435 msg_Dbg( p_demux, "video brightness: %d", control.value );
1439 memset( &control, 0, sizeof( control ) );
1440 control.id = V4L2_CID_CONTRAST;
1441 if( p_sys->i_contrast >= 0 )
1443 control.value = p_sys->i_contrast;
1444 if( ioctl( i_fd, VIDIOC_S_CTRL, &control ) < 0 )
1446 msg_Err( p_demux, "unable to set contrast to %d (%m)", p_sys->i_contrast );
1450 if( ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0 )
1452 msg_Dbg( p_demux, "video contrast: %d", control.value );
1456 memset( &control, 0, sizeof( control ) );
1457 control.id = V4L2_CID_SATURATION;
1458 if( p_sys->i_saturation >= 0 )
1460 control.value = p_sys->i_saturation;
1461 if( ioctl( i_fd, VIDIOC_S_CTRL, &control ) < 0 )
1463 msg_Err( p_demux, "unable to set saturation to %d (%m)", p_sys->i_saturation );
1467 if( ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0 )
1469 msg_Dbg( p_demux, "video saturation: %d", control.value );
1473 memset( &control, 0, sizeof( control ) );
1474 control.id = V4L2_CID_HUE;
1475 if( p_sys->i_hue >= 0 )
1477 control.value = p_sys->i_hue;
1478 if( ioctl( i_fd, VIDIOC_S_CTRL, &control ) < 0 )
1480 msg_Err( p_demux, "unable to set hue to %d (%m)", p_sys->i_hue );
1484 if( ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0 )
1486 msg_Dbg( p_demux, "video hue: %d", control.value );
1489 /* Init vout Picture */
1490 vout_InitPicture( VLC_OBJECT(p_demux), &p_sys->pic, p_sys->i_fourcc,
1491 p_sys->i_width, p_sys->i_height, p_sys->i_width *
1492 VOUT_ASPECT_FACTOR / p_sys->i_height );
1493 if( !p_sys->pic.i_planes )
1495 msg_Err( p_demux, "unsupported chroma" );
1498 p_sys->i_video_frame_size = 0;
1499 for( int i = 0; i < p_sys->pic.i_planes; i++ )
1501 p_sys->i_video_frame_size += p_sys->pic.p[i].i_visible_lines * p_sys->pic.p[i].i_visible_pitch;
1504 /* Init IO method */
1507 case IO_METHOD_READ:
1508 if( InitRead( p_demux, i_fd, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
1511 case IO_METHOD_MMAP:
1512 if( InitMmap( p_demux, i_fd ) != VLC_SUCCESS ) goto open_failed;
1515 case IO_METHOD_USERPTR:
1516 if( InitUserP( p_demux, i_fd, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
1523 es_format_Init( &es_fmt, VIDEO_ES, p_sys->i_fourcc );
1524 es_fmt.video.i_width = p_sys->i_width;
1525 es_fmt.video.i_height = p_sys->i_height;
1526 es_fmt.video.i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
1528 /* Setup rgb mask for RGB formats */
1529 if( p_sys->i_fourcc == VLC_FOURCC( 'R','V','2','4' ) )
1531 /* This is in BGR format */
1532 es_fmt.video.i_bmask = 0x00ff0000;
1533 es_fmt.video.i_gmask = 0x0000ff00;
1534 es_fmt.video.i_rmask = 0x000000ff;
1537 msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
1538 (char*)&es_fmt.i_codec, es_fmt.video.i_width, es_fmt.video.i_height );
1539 p_sys->p_es_video = es_out_Add( p_demux->out, &es_fmt );
1545 case IO_METHOD_READ:
1549 case IO_METHOD_MMAP:
1550 for (unsigned int i = 0; i < p_sys->i_nbuffers; ++i)
1552 struct v4l2_buffer buf;
1554 memset( &buf, 0, sizeof(buf) );
1555 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1556 buf.memory = V4L2_MEMORY_MMAP;
1559 if( ioctl( i_fd, VIDIOC_QBUF, &buf ) < 0 )
1561 msg_Err( p_demux, "VIDIOC_QBUF failed" );
1566 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1567 if( ioctl( i_fd, VIDIOC_STREAMON, &buf_type ) < 0 )
1569 msg_Err( p_demux, "VIDIOC_STREAMON failed" );
1575 case IO_METHOD_USERPTR:
1576 for( unsigned int i = 0; i < p_sys->i_nbuffers; ++i )
1578 struct v4l2_buffer buf;
1580 memset( &buf, 0, sizeof(buf) );
1581 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1582 buf.memory = V4L2_MEMORY_USERPTR;
1584 buf.m.userptr = (unsigned long)p_sys->p_buffers[i].start;
1585 buf.length = p_sys->p_buffers[i].length;
1587 if( ioctl( i_fd, VIDIOC_QBUF, &buf ) < 0 )
1589 msg_Err( p_demux, "VIDIOC_QBUF failed" );
1594 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1595 if( ioctl( i_fd, VIDIOC_STREAMON, &buf_type ) < 0 )
1597 msg_Err( p_demux, "VIDIOC_STREAMON failed" );
1605 if( p_sys->f_fps >= 0.1 )
1607 msg_Dbg( p_demux, "User set fps=%f", p_sys->f_fps );
1613 if( i_fd >= 0 ) close( i_fd );
1618 /*****************************************************************************
1619 * OpenAudioDev: open and set up the audio device and probe for capabilities
1620 *****************************************************************************/
1621 int OpenAudioDev( demux_t *p_demux, char *psz_device )
1623 demux_sys_t *p_sys = p_demux->p_sys;
1626 if( (i_fd = open( psz_device, O_RDONLY | O_NONBLOCK )) < 0 )
1628 msg_Err( p_demux, "cannot open audio device (%m)" );
1632 i_format = AFMT_S16_LE;
1633 if( ioctl( i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
1634 || i_format != AFMT_S16_LE )
1636 msg_Err( p_demux, "cannot set audio format (16b little endian) "
1641 if( ioctl( i_fd, SNDCTL_DSP_STEREO,
1642 &p_sys->b_stereo ) < 0 )
1644 msg_Err( p_demux, "cannot set audio channels count (%m)" );
1648 if( ioctl( i_fd, SNDCTL_DSP_SPEED,
1649 &p_sys->i_sample_rate ) < 0 )
1651 msg_Err( p_demux, "cannot set audio sample rate (%m)" );
1655 msg_Dbg( p_demux, "opened adev=`%s' %s %dHz",
1656 psz_device, p_sys->b_stereo ? "stereo" : "mono",
1657 p_sys->i_sample_rate );
1659 p_sys->i_audio_max_frame_size = 6 * 1024;
1662 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC('a','r','a','w') );
1664 fmt.audio.i_channels = p_sys->b_stereo ? 2 : 1;
1665 fmt.audio.i_rate = p_sys->i_sample_rate;
1666 fmt.audio.i_bitspersample = 16; /* FIXME ? */
1667 fmt.audio.i_blockalign = fmt.audio.i_channels * fmt.audio.i_bitspersample / 8;
1668 fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate * fmt.audio.i_bitspersample;
1670 msg_Dbg( p_demux, "new audio es %d channels %dHz",
1671 fmt.audio.i_channels, fmt.audio.i_rate );
1673 p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
1679 if( i_fd >= 0 ) close( i_fd );
1684 /*****************************************************************************
1685 * ProbeVideoDev: probe video for capabilities
1686 *****************************************************************************/
1687 vlc_bool_t ProbeVideoDev( demux_t *p_demux, char *psz_device )
1693 demux_sys_t *p_sys = p_demux->p_sys;
1695 if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
1697 msg_Err( p_demux, "cannot open video device (%m)" );
1701 /* Get device capabilites */
1703 if( ioctl( i_fd, VIDIOC_QUERYCAP, &p_sys->dev_cap ) < 0 )
1705 msg_Err( p_demux, "cannot get video capabilities (%m)" );
1709 msg_Dbg( p_demux, "V4L2 device: %s using driver: %s (version: %u.%u.%u) on %s",
1710 p_sys->dev_cap.card,
1711 p_sys->dev_cap.driver,
1712 (p_sys->dev_cap.version >> 16) & 0xFF,
1713 (p_sys->dev_cap.version >> 8) & 0xFF,
1714 p_sys->dev_cap.version & 0xFF,
1715 p_sys->dev_cap.bus_info );
1717 msg_Dbg( p_demux, "the device has the capabilities: (%c) Video Capure, "
1720 ( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE ? 'X':' '),
1721 ( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO ? 'X':' '),
1722 ( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER ? 'X':' ') );
1724 msg_Dbg( p_demux, "supported I/O methods are: (%c) Read/Write, "
1726 "(%c) Asynchronous",
1727 ( p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE ? 'X':' ' ),
1728 ( p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING ? 'X':' ' ),
1729 ( p_sys->dev_cap.capabilities & V4L2_CAP_ASYNCIO ? 'X':' ' ) );
1731 /* Now, enumerate all the video inputs. This is useless at the moment
1732 since we have no way to present that info to the user except with
1735 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
1737 while( ioctl( i_fd, VIDIOC_S_INPUT, &p_sys->i_input ) >= 0 )
1742 p_sys->p_inputs = calloc( 1, p_sys->i_input * sizeof( struct v4l2_input ) );
1743 if( !p_sys->p_inputs ) goto open_failed;
1745 for( i_index = 0; i_index < p_sys->i_input; i_index++ )
1747 p_sys->p_inputs[i_index].index = i_index;
1749 if( ioctl( i_fd, VIDIOC_ENUMINPUT, &p_sys->p_inputs[i_index] ) )
1751 msg_Err( p_demux, "cannot get video input characteristics (%m)" );
1754 msg_Dbg( p_demux, "video input %i (%s) has type: %s",
1756 p_sys->p_inputs[i_index].name,
1757 p_sys->p_inputs[i_index].type
1758 == V4L2_INPUT_TYPE_TUNER ?
1760 "External analog input" );
1764 /* Probe video standards */
1765 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
1767 struct v4l2_standard t_standards;
1768 t_standards.index = 0;
1769 while( ioctl( i_fd, VIDIOC_ENUMSTD, &t_standards ) >=0 )
1771 p_sys->i_standard++;
1772 t_standards.index = p_sys->i_standard;
1775 p_sys->p_standards = calloc( 1, p_sys->i_standard * sizeof( struct v4l2_standard ) );
1776 if( !p_sys->p_standards ) goto open_failed;
1778 for( i_standard = 0; i_standard < p_sys->i_standard; i_standard++ )
1780 p_sys->p_standards[i_standard].index = i_standard;
1782 if( ioctl( i_fd, VIDIOC_ENUMSTD, &p_sys->p_standards[i_standard] ) )
1784 msg_Err( p_demux, "cannot get video input standards (%m)" );
1787 msg_Dbg( p_demux, "video standard %i is: %s",
1789 p_sys->p_standards[i_standard].name);
1793 /* initialize the structures for the ioctls */
1794 for( i_index = 0; i_index < 32; i_index++ )
1796 p_sys->p_audios[i_index].index = i_index;
1799 /* Probe audio inputs */
1801 if( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO )
1803 while( p_sys->i_audio < 32 &&
1804 ioctl( i_fd, VIDIOC_S_AUDIO, &p_sys->p_audios[p_sys->i_audio] ) >= 0 )
1806 if( ioctl( i_fd, VIDIOC_G_AUDIO, &p_sys->p_audios[ p_sys->i_audio] ) < 0 )
1808 msg_Err( p_demux, "cannot get audio input characteristics (%m)" );
1812 msg_Dbg( p_demux, "audio device %i (%s) is %s",
1814 p_sys->p_audios[p_sys->i_audio].name,
1815 p_sys->p_audios[p_sys->i_audio].capability &
1816 V4L2_AUDCAP_STEREO ?
1817 "Stereo" : "Mono" );
1823 if( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER )
1825 struct v4l2_tuner tuner;
1827 memset( &tuner, 0, sizeof(tuner) );
1828 while( ioctl( i_fd, VIDIOC_S_TUNER, &tuner ) >= 0 )
1831 tuner.index = p_sys->i_tuner;
1834 p_sys->p_tuners = calloc( 1, p_sys->i_tuner * sizeof( struct v4l2_tuner ) );
1835 if( !p_sys->p_tuners ) goto open_failed;
1837 for( i_index = 0; i_index < p_sys->i_tuner; i_index++ )
1839 p_sys->p_tuners[i_index].index = i_index;
1841 if( ioctl( i_fd, VIDIOC_G_TUNER, &p_sys->p_tuners[i_index] ) )
1843 msg_Err( p_demux, "cannot get tuner characteristics (%m)" );
1846 msg_Dbg( p_demux, "tuner %i (%s) has type: %s, "
1847 "frequency range: %.1f %s -> %.1f %s",
1849 p_sys->p_tuners[i_index].name,
1850 p_sys->p_tuners[i_index].type
1851 == V4L2_TUNER_RADIO ?
1852 "Radio" : "Analog TV",
1853 p_sys->p_tuners[i_index].rangelow * 62.5,
1854 p_sys->p_tuners[i_index].capability &
1855 V4L2_TUNER_CAP_LOW ?
1857 p_sys->p_tuners[i_index].rangehigh * 62.5,
1858 p_sys->p_tuners[i_index].capability &
1859 V4L2_TUNER_CAP_LOW ?
1864 /* Probe for available chromas */
1865 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
1867 struct v4l2_fmtdesc codec;
1870 memset( &codec, 0, sizeof(codec) );
1871 codec.index = i_index;
1872 codec.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1874 while( ioctl( i_fd, VIDIOC_ENUM_FMT, &codec ) >= 0 )
1877 codec.index = i_index;
1880 p_sys->i_codec = i_index;
1882 p_sys->p_codecs = calloc( 1, p_sys->i_codec * sizeof( struct v4l2_fmtdesc ) );
1884 for( i_index = 0; i_index < p_sys->i_codec; i_index++ )
1886 p_sys->p_codecs[i_index].index = i_index;
1887 p_sys->p_codecs[i_index].type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1889 if( ioctl( i_fd, VIDIOC_ENUM_FMT, &p_sys->p_codecs[i_index] ) < 0 )
1891 msg_Err( p_demux, "cannot get codec description (%m)" );
1895 /* only print if vlc supports the format */
1896 vlc_bool_t b_codec_supported = VLC_FALSE;
1897 for( int i = 0; v4l2chroma_to_fourcc[i].i_v4l2 != 0; i++ )
1899 if( v4l2chroma_to_fourcc[i].i_v4l2 == p_sys->p_codecs[i_index].pixelformat )
1901 b_codec_supported = VLC_TRUE;
1904 memset( &sz_fourcc, 0, sizeof( sz_fourcc ) );
1905 vlc_fourcc_to_char( v4l2chroma_to_fourcc[i].i_fourcc, &sz_fourcc );
1906 msg_Dbg( p_demux, "device supports chroma %4s [%s]",
1908 p_sys->p_codecs[i_index].description );
1910 #ifdef VIDIOC_ENUM_FRAMESIZES
1911 /* This is new in Linux 2.6.19 */
1912 /* List valid frame sizes for this format */
1913 struct v4l2_frmsizeenum frmsize;
1915 frmsize.pixel_format = p_sys->p_codecs[i_index].pixelformat;
1916 if( ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) < 0 )
1918 msg_Err( p_demux, "Error while querying for frame size" );
1922 switch( frmsize.type )
1924 case V4L2_FRMSIZE_TYPE_DISCRETE:
1928 " device supports size %dx%d",
1929 frmsize.discrete.width, frmsize.discrete.height );
1931 } while( ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) >= 0 );
1933 case V4L2_FRMSIZE_TYPE_STEPWISE:
1935 " device supports sizes %dx%d to %dx%d using %dx%d increments",
1936 frmsize.stepwise.min_width, frmsize.stepwise.min_height,
1937 frmsize.stepwise.max_width, frmsize.stepwise.max_height,
1938 frmsize.stepwise.step_width, frmsize.stepwise.step_height );
1940 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
1942 " device supports all sizes %dx%d to %dx%d",
1943 frmsize.stepwise.min_width, frmsize.stepwise.min_height,
1944 frmsize.stepwise.max_width, frmsize.stepwise.max_height );
1951 if( !b_codec_supported )
1953 msg_Dbg( p_demux, "device codec %s not supported",
1954 p_sys->p_codecs[i_index].description );
1961 if( i_fd >= 0 ) close( i_fd );
1966 if( i_fd >= 0 ) close( i_fd );
1971 /*****************************************************************************
1972 * ProbeAudioDev: probe audio for capabilities
1973 *****************************************************************************/
1974 vlc_bool_t ProbeAudioDev( demux_t *p_demux, char *psz_device )
1979 if( ( i_fd = open( psz_device, O_RDONLY | O_NONBLOCK ) ) < 0 )
1981 msg_Err( p_demux, "cannot open audio device (%m)" );
1985 /* this will fail if the device is video */
1986 if( ioctl( i_fd, SNDCTL_DSP_GETCAPS, &i_caps ) < 0 )
1988 msg_Err( p_demux, "cannot get audio caps (%m)" );
1992 if( i_fd >= 0 ) close( i_fd );
1996 if( i_fd >= 0 ) close( i_fd );