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 FPS_TEXT N_( "Framerate" )
91 #define FPS_LONGTEXT N_( "Framerate to capture, if applicable " \
92 "(-1 for autodetect)." )
93 #define STEREO_TEXT N_( "Stereo" )
94 #define STEREO_LONGTEXT N_( \
95 "Capture the audio stream in stereo." )
96 #define SAMPLERATE_TEXT N_( "Samplerate" )
97 #define SAMPLERATE_LONGTEXT N_( \
98 "Samplerate of the captured audio stream, in Hz (eg: 11025, 22050, 44100, 48000)" )
99 #define CACHING_TEXT N_("Caching value in ms")
100 #define CACHING_LONGTEXT N_( \
101 "Caching value for V4L2 captures. This " \
102 "value should be set in milliseconds." )
110 static int i_standards_list[] =
111 { V4L2_STD_UNKNOWN, V4L2_STD_SECAM, V4L2_STD_PAL, V4L2_STD_NTSC };
112 static const char *psz_standards_list_text[] =
113 { N_("Default"), N_("SECAM"), N_("PAL"), N_("NTSC") };
115 static int i_iomethod_list[] =
116 { IO_METHOD_READ, IO_METHOD_MMAP, IO_METHOD_USERPTR };
117 static const char *psz_iomethod_list_text[] =
118 { N_("READ"), N_("MMAP"), N_("USERPTR") };
121 set_shortname( _("Video4Linux2") );
122 set_description( _("Video4Linux2 input") );
123 set_category( CAT_INPUT );
124 set_subcategory( SUBCAT_INPUT_ACCESS );
126 add_string( "v4l2-dev", "/dev/video0", 0, DEV_TEXT, DEV_LONGTEXT,
128 add_string( "v4l2-adev", "/dev/dsp", 0, ADEV_TEXT, ADEV_LONGTEXT,
130 add_integer( "v4l2-standard", 0, NULL, STANDARD_TEXT, STANDARD_LONGTEXT,
132 change_integer_list( i_standards_list, psz_standards_list_text, 0 );
133 add_string( "v4l2-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
135 add_integer( "v4l2-input", 0, NULL, INPUT_TEXT, INPUT_LONGTEXT,
137 add_integer( "v4l2-io", IO_METHOD_MMAP, NULL, IOMETHOD_TEXT,
138 IOMETHOD_LONGTEXT, VLC_TRUE );
139 change_integer_list( i_iomethod_list, psz_iomethod_list_text, 0 );
140 add_integer( "v4l2-width", 0, NULL, WIDTH_TEXT,
141 WIDTH_LONGTEXT, VLC_TRUE );
142 add_integer( "v4l2-height", 0, NULL, HEIGHT_TEXT,
143 HEIGHT_LONGTEXT, VLC_TRUE );
144 add_float( "v4l2-fps", 0, NULL, FPS_TEXT, FPS_LONGTEXT, VLC_TRUE );
145 add_bool( "v4l2-stereo", VLC_TRUE, NULL, STEREO_TEXT, STEREO_LONGTEXT,
147 add_integer( "v4l2-samplerate", 48000, NULL, SAMPLERATE_TEXT,
148 SAMPLERATE_LONGTEXT, VLC_TRUE );
149 add_integer( "v4l2-caching", DEFAULT_PTS_DELAY / 1000, NULL,
150 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
152 add_shortcut( "v4l2" );
153 set_capability( "access_demux", 10 );
154 set_callbacks( Open, Close );
157 /*****************************************************************************
158 * Access: local prototypes
159 *****************************************************************************/
161 static void ParseMRL( demux_t * );
163 static int Control( demux_t *, int, va_list );
165 static int Demux( demux_t * );
166 static block_t* GrabVideo( demux_t *p_demux );
167 static block_t* ProcessVideoFrame( demux_t *p_demux, uint8_t *p_frame );
168 static block_t* GrabAudio( demux_t *p_demux );
170 vlc_bool_t IsPixelFormatSupported( demux_t *p_demux, unsigned int i_pixelformat );
172 static int OpenVideoDev( demux_t *, char *psz_device );
173 static int OpenAudioDev( demux_t *, char *psz_device );
174 static vlc_bool_t ProbeVideoDev( demux_t *, char *psz_device );
175 static vlc_bool_t ProbeAudioDev( demux_t *, char *psz_device );
181 } v4l2chroma_to_fourcc[] =
183 { V4L2_PIX_FMT_GREY, VLC_FOURCC( 'G', 'R', 'E', 'Y' ) },
184 { V4L2_PIX_FMT_HI240, VLC_FOURCC( 'I', '2', '4', '0' ) },
185 { V4L2_PIX_FMT_RGB565, VLC_FOURCC( 'R', 'V', '1', '6' ) },
186 { V4L2_PIX_FMT_RGB555, VLC_FOURCC( 'R', 'V', '1', '5' ) },
187 { V4L2_PIX_FMT_BGR24, VLC_FOURCC( 'R', 'V', '2', '4' ) },
188 { V4L2_PIX_FMT_BGR32, VLC_FOURCC( 'R', 'V', '3', '2' ) },
189 { V4L2_PIX_FMT_YUYV, VLC_FOURCC( 'Y', 'U', 'Y', '2' ) },
190 { V4L2_PIX_FMT_YUYV, VLC_FOURCC( 'Y', 'U', 'Y', 'V' ) },
191 { V4L2_PIX_FMT_UYVY, VLC_FOURCC( 'U', 'Y', 'V', 'Y' ) },
192 { V4L2_PIX_FMT_Y41P, VLC_FOURCC( 'I', '4', '1', 'N' ) },
193 { V4L2_PIX_FMT_YUV422P, VLC_FOURCC( 'I', '4', '2', '2' ) },
194 { V4L2_PIX_FMT_YVU420, VLC_FOURCC( 'I', '4', '2', '0' ) },
195 { V4L2_PIX_FMT_YUV411P, VLC_FOURCC( 'I', '4', '1', '1' ) },
196 { V4L2_PIX_FMT_YUV410, VLC_FOURCC( 'I', '4', '1', '0' ) },
209 char *psz_device; /* Main device from MRL, can be video or audio */
217 char *psz_requested_chroma;
224 struct v4l2_capability dev_cap;
227 struct v4l2_input *p_inputs;
228 int i_selected_input;
231 struct v4l2_standard *p_standards;
232 v4l2_std_id i_selected_standard_id;
235 /* V4L2 devices cannot have more than 32 audio inputs */
236 struct v4l2_audio p_audios[32];
239 struct v4l2_tuner *p_tuners;
242 struct v4l2_fmtdesc *p_codecs;
244 struct buffer_t *p_buffers;
245 unsigned int i_nbuffers;
249 float f_fps; /* <= 0.0 mean to grab at full rate */
250 mtime_t i_video_pts; /* only used when f_fps > 0 */
254 int i_video_frame_size;
256 es_out_id_t *p_es_video;
261 int i_audio_max_frame_size;
262 block_t *p_block_audio;
263 es_out_id_t *p_es_audio;
266 /*****************************************************************************
267 * Open: opens v4l2 device
268 *****************************************************************************
270 * url: <video device>::::
272 *****************************************************************************/
273 static int Open( vlc_object_t *p_this )
275 demux_t *p_demux = (demux_t*)p_this;
279 /* Only when selected */
280 if( *p_demux->psz_access == '\0' ) return VLC_EGENERIC;
283 p_demux->pf_control = Control;
284 p_demux->pf_demux = Demux;
285 p_demux->info.i_update = 0;
286 p_demux->info.i_title = 0;
287 p_demux->info.i_seekpoint = 0;
289 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
290 if( p_sys == NULL ) return VLC_ENOMEM;
292 p_sys->i_video_pts = -1;
294 var_Create( p_demux, "v4l2-standard", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
295 var_Get( p_demux, "v4l2-standard", &val );
296 p_sys->i_selected_standard_id = i_standards_list[val.i_int];
298 p_sys->i_selected_input = var_CreateGetInteger( p_demux, "v4l2-input" );
300 p_sys->io = var_CreateGetInteger( p_demux, "v4l2-io" );
302 p_sys->i_width = var_CreateGetInteger( p_demux, "v4l2-width" );
303 p_sys->i_height = var_CreateGetInteger( p_demux, "v4l2-height" );
305 var_Create( p_demux, "v4l2-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
306 var_Get( p_demux, "v4l2-fps", &val );
307 p_sys->f_fps = val.f_float;
309 var_Create( p_demux, "v4l2-samplerate", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
310 var_Get( p_demux, "v4l2-samplerate", &val );
311 p_sys->i_sample_rate = val.i_int;
313 p_sys->psz_requested_chroma = var_CreateGetString( p_demux, "v4l2-chroma" );
315 var_Create( p_demux, "v4l2-stereo", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
316 var_Get( p_demux, "v4l2-stereo", &val );
317 p_sys->b_stereo = val.b_bool;
319 p_sys->i_pts = var_CreateGetInteger( p_demux, "v4l2-caching" );
321 p_sys->psz_device = p_sys->psz_vdev = p_sys->psz_adev = NULL;
322 p_sys->i_fd_video = -1;
323 p_sys->i_fd_audio = -1;
325 p_sys->p_es_video = p_sys->p_es_audio = 0;
326 p_sys->p_block_audio = 0;
330 /* Find main device (video or audio) */
331 if( p_sys->psz_device && *p_sys->psz_device )
333 msg_Dbg( p_demux, "main device='%s'", p_sys->psz_device );
335 /* Try to open as video device */
336 msg_Dbg( p_demux, "trying device '%s' as video", p_sys->psz_device );
337 if( ProbeVideoDev( p_demux, p_sys->psz_device ) )
339 msg_Dbg( p_demux, "'%s' is a video device", p_sys->psz_device );
340 /* Device was a video device */
341 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
342 p_sys->psz_vdev = p_sys->psz_device;
343 p_sys->psz_device = NULL;
344 p_sys->i_fd_video = OpenVideoDev( p_demux, p_sys->psz_vdev );
345 if( p_sys->i_fd_video < 0 )
353 /* Try to open as audio device */
354 msg_Dbg( p_demux, "trying device '%s' as audio", p_sys->psz_device );
355 if( ProbeAudioDev( p_demux, p_sys->psz_device ) )
357 msg_Dbg( p_demux, "'%s' is an audio device", p_sys->psz_device );
358 /* Device was an audio device */
359 if( p_sys->psz_adev ) free( p_sys->psz_adev );
360 p_sys->psz_adev = p_sys->psz_device;
361 p_sys->psz_device = NULL;
362 p_sys->i_fd_audio = OpenAudioDev( p_demux, p_sys->psz_adev );
363 if( p_sys->i_fd_audio < 0 )
372 /* If no device opened, only continue if the access was forced */
373 if( p_sys->i_fd_video < 0 && p_sys->i_fd_audio < 0 )
375 if( strcmp( p_demux->psz_access, "v4l2" ) )
382 /* Find video device */
383 if( p_sys->i_fd_video < 0 )
385 if( !p_sys->psz_vdev || !*p_sys->psz_vdev )
387 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
388 p_sys->psz_vdev = var_CreateGetString( p_demux, "v4l2-dev" );
391 msg_Dbg( p_demux, "opening '%s' as video", p_sys->psz_vdev );
392 if( p_sys->psz_vdev && *p_sys->psz_vdev && ProbeVideoDev( p_demux, p_sys->psz_vdev ) )
394 p_sys->i_fd_video = OpenVideoDev( p_demux, p_sys->psz_vdev );
398 /* Find audio device */
399 if( p_sys->i_fd_audio < 0 )
401 if( !p_sys->psz_adev || !*p_sys->psz_adev )
403 if( p_sys->psz_adev ) free( p_sys->psz_adev );
404 p_sys->psz_adev = var_CreateGetString( p_demux, "v4l2-adev" );
407 msg_Dbg( p_demux, "opening '%s' as audio", p_sys->psz_adev );
408 if( p_sys->psz_adev && *p_sys->psz_adev && ProbeAudioDev( p_demux, p_sys->psz_adev ) )
410 p_sys->i_fd_audio = OpenAudioDev( p_demux, p_sys->psz_adev );
414 if( p_sys->i_fd_video < 0 && p_sys->i_fd_audio < 0 )
423 /*****************************************************************************
424 * ParseMRL: parse the options contained in the MRL
425 *****************************************************************************/
426 static void ParseMRL( demux_t *p_demux )
428 demux_sys_t *p_sys = p_demux->p_sys;
430 char *psz_dup = strdup( p_demux->psz_path );
431 char *psz_parser = psz_dup;
433 while( *psz_parser && *psz_parser != ':' )
438 if( *psz_parser == ':' )
443 *psz_parser++ = '\0';
445 if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) ) )
449 psz_parser += strlen( "adev=" );
450 if( strchr( psz_parser, ':' ) )
452 i_len = strchr( psz_parser, ':' ) - psz_parser;
456 i_len = strlen( psz_parser );
459 p_sys->psz_adev = strndup( psz_parser, i_len );
463 else if( !strncmp( psz_parser, "standard=", strlen( "standard=" ) ) )
465 psz_parser += strlen( "standard=" );
466 if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
468 p_sys->i_selected_standard_id = V4L2_STD_PAL;
469 psz_parser += strlen( "pal" );
471 else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
473 p_sys->i_selected_standard_id = V4L2_STD_NTSC;
474 psz_parser += strlen( "ntsc" );
476 else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
478 p_sys->i_selected_standard_id = V4L2_STD_SECAM;
479 psz_parser += strlen( "secam" );
481 else if( !strncmp( psz_parser, "default", strlen( "default" ) ) )
483 p_sys->i_selected_standard_id = V4L2_STD_UNKNOWN;
484 psz_parser += strlen( "default" );
488 p_sys->i_selected_standard_id = i_standards_list[strtol( psz_parser, &psz_parser, 0 )];
491 else if( !strncmp( psz_parser, "chroma=", strlen( "chroma=" ) ) )
495 psz_parser += strlen( "chroma=" );
496 if( strchr( psz_parser, ':' ) )
498 i_len = strchr( psz_parser, ':' ) - psz_parser;
502 i_len = strlen( psz_parser );
505 if( p_sys->psz_requested_chroma ) free( p_sys->psz_requested_chroma );
506 p_sys->psz_requested_chroma = strndup( psz_parser, i_len );
510 else if( !strncmp( psz_parser, "input=", strlen( "input=" ) ) )
512 p_sys->i_selected_input = strtol( psz_parser + strlen( "input=" ),
515 else if( !strncmp( psz_parser, "fps=", strlen( "fps=" ) ) )
517 p_sys->f_fps = strtof( psz_parser + strlen( "fps=" ),
520 else if( !strncmp( psz_parser, "io=", strlen( "io=" ) ) )
522 psz_parser += strlen( "io=" );
523 if( !strncmp( psz_parser, "read", strlen( "read" ) ) )
525 p_sys->io = IO_METHOD_READ;
526 psz_parser += strlen( "read" );
528 else if( !strncmp( psz_parser, "mmap", strlen( "mmap" ) ) )
530 p_sys->io = IO_METHOD_MMAP;
531 psz_parser += strlen( "mmap" );
533 else if( !strncmp( psz_parser, "userptr", strlen( "userptr" ) ) )
535 p_sys->io = IO_METHOD_USERPTR;
536 psz_parser += strlen( "userptr" );
540 p_sys->io = strtol( psz_parser, &psz_parser, 0 );
543 else if( !strncmp( psz_parser, "width=",
544 strlen( "width=" ) ) )
547 strtol( psz_parser + strlen( "width=" ),
550 else if( !strncmp( psz_parser, "height=",
551 strlen( "height=" ) ) )
554 strtol( psz_parser + strlen( "height=" ),
557 else if( !strncmp( psz_parser, "samplerate=",
558 strlen( "samplerate=" ) ) )
560 p_sys->i_sample_rate =
561 strtol( psz_parser + strlen( "samplerate=" ),
564 else if( !strncmp( psz_parser, "stereo", strlen( "stereo" ) ) )
566 psz_parser += strlen( "stereo" );
567 p_sys->b_stereo = VLC_TRUE;
569 else if( !strncmp( psz_parser, "mono", strlen( "mono" ) ) )
571 psz_parser += strlen( "mono" );
572 p_sys->b_stereo = VLC_FALSE;
574 else if( !strncmp( psz_parser, "caching=", strlen( "caching=" ) ) )
576 p_sys->i_pts = strtol( psz_parser + strlen( "caching=" ),
581 msg_Warn( p_demux, "unknown option" );
584 while( *psz_parser && *psz_parser != ':' )
589 if( *psz_parser == '\0' )
599 p_sys->psz_device = strdup( psz_dup );
601 if( psz_dup ) free( psz_dup );
604 /*****************************************************************************
605 * Close: close device, free resources
606 *****************************************************************************/
607 static void Close( vlc_object_t *p_this )
609 demux_t *p_demux = (demux_t *)p_this;
610 demux_sys_t *p_sys = p_demux->p_sys;
611 struct v4l2_buffer buf;
612 enum v4l2_buf_type buf_type;
615 /* Stop video capture */
616 if( p_sys->i_fd_video >= 0 )
625 case IO_METHOD_USERPTR:
626 /* Some drivers 'hang' internally if this is not done before streamoff */
627 for( unsigned int i = 0; i < p_sys->i_nbuffers; i++ )
629 memset( &buf, 0, sizeof(buf) );
630 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
631 buf.memory = ( p_sys->io == IO_METHOD_USERPTR ) ?
632 V4L2_MEMORY_USERPTR : V4L2_MEMORY_MMAP;
633 ioctl( p_sys->i_fd_video, VIDIOC_DQBUF, &buf ); /* ignore result */
636 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
637 if( ioctl( p_sys->i_fd_video, VIDIOC_STREAMOFF, &buf_type ) < 0 ) {
638 msg_Err( p_demux, "VIDIOC_STREAMOFF failed" );
645 /* Free Video Buffers */
646 if( p_sys->p_buffers ) {
650 free( p_sys->p_buffers[0].start );
654 for( i = 0; i < p_sys->i_nbuffers; ++i )
656 if( munmap( p_sys->p_buffers[i].start, p_sys->p_buffers[i].length ) )
658 msg_Err( p_demux, "munmap failed" );
663 case IO_METHOD_USERPTR:
664 for( i = 0; i < p_sys->i_nbuffers; ++i )
666 free( p_sys->p_buffers[i].orig_userp );
670 free( p_sys->p_buffers );
674 if( p_sys->i_fd_video >= 0 ) close( p_sys->i_fd_video );
675 if( p_sys->i_fd_audio >= 0 ) close( p_sys->i_fd_audio );
677 if( p_sys->p_block_audio ) block_Release( p_sys->p_block_audio );
678 if( p_sys->psz_device ) free( p_sys->psz_device );
679 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
680 if( p_sys->psz_adev ) free( p_sys->psz_adev );
681 if( p_sys->p_standards ) free( p_sys->p_standards );
682 if( p_sys->p_inputs ) free( p_sys->p_inputs );
683 if( p_sys->p_tuners ) free( p_sys->p_tuners );
684 if( p_sys->p_codecs ) free( p_sys->p_codecs );
685 if( p_sys->psz_requested_chroma ) free( p_sys->psz_requested_chroma );
690 /*****************************************************************************
692 *****************************************************************************/
693 static int Control( demux_t *p_demux, int i_query, va_list args )
695 demux_sys_t *p_sys = p_demux->p_sys;
701 /* Special for access_demux */
702 case DEMUX_CAN_PAUSE:
703 case DEMUX_SET_PAUSE_STATE:
704 case DEMUX_CAN_CONTROL_PACE:
705 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
709 case DEMUX_GET_PTS_DELAY:
710 pi64 = (int64_t*)va_arg( args, int64_t * );
711 *pi64 = (int64_t)p_sys->i_pts * 1000;
715 pi64 = (int64_t*)va_arg( args, int64_t * );
719 /* TODO implement others */
727 /*****************************************************************************
728 * Demux: Processes the audio or video frame
729 *****************************************************************************/
730 static int Demux( demux_t *p_demux )
732 demux_sys_t *p_sys = p_demux->p_sys;
733 es_out_id_t *p_es = p_sys->p_es_audio;
734 block_t *p_block = NULL;
736 /* Try grabbing audio frames first */
737 if( p_sys->i_fd_audio < 0 || !( p_block = GrabAudio( p_demux ) ) )
739 /* Try grabbing video frame */
740 p_es = p_sys->p_es_video;
741 if( p_sys->i_fd_video > 0 ) p_block = GrabVideo( p_demux );
746 /* Sleep so we do not consume all the cpu, 10ms seems
747 * like a good value (100fps) */
752 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
753 es_out_Send( p_demux->out, p_es, p_block );
758 /*****************************************************************************
759 * GrabVideo: Grab a video frame
760 *****************************************************************************/
761 static block_t* GrabVideo( demux_t *p_demux )
763 demux_sys_t *p_sys = p_demux->p_sys;
765 block_t *p_block = NULL;
766 struct v4l2_buffer buf;
768 if( p_sys->f_fps >= 0.1 && p_sys->i_video_pts > 0 )
770 mtime_t i_dur = (mtime_t)((double)1000000 / (double)p_sys->f_fps);
772 /* Did we wait long enough ? (frame rate reduction) */
773 if( p_sys->i_video_pts + i_dur > mdate() ) return 0;
776 /* Grab Video Frame */
780 if( read( p_sys->i_fd_video, p_sys->p_buffers[0].start, p_sys->p_buffers[0].length ) )
787 /* Could ignore EIO, see spec. */
790 msg_Err( p_demux, "Failed to read frame" );
795 p_block = ProcessVideoFrame( p_demux, (uint8_t*)p_sys->p_buffers[0].start );
796 if( !p_block ) return 0;
801 memset( &buf, 0, sizeof(buf) );
802 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
803 buf.memory = V4L2_MEMORY_MMAP;
805 /* Wait for next frame */
806 if (ioctl( p_sys->i_fd_video, VIDIOC_DQBUF, &buf ) < 0 )
813 /* Could ignore EIO, see spec. */
816 msg_Err( p_demux, "Failed to wait (VIDIOC_DQBUF)" );
821 if( buf.index >= p_sys->i_nbuffers ) {
822 msg_Err( p_demux, "Failed capturing new frame as i>=nbuffers" );
826 p_block = ProcessVideoFrame( p_demux, p_sys->p_buffers[buf.index].start );
827 if( !p_block ) return 0;
830 if( ioctl( p_sys->i_fd_video, VIDIOC_QBUF, &buf ) < 0 )
832 msg_Err (p_demux, "Failed to unlock (VIDIOC_QBUF)");
838 case IO_METHOD_USERPTR:
839 memset( &buf, 0, sizeof(buf) );
840 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
841 buf.memory = V4L2_MEMORY_USERPTR;
843 /* Wait for next frame */
844 if (ioctl( p_sys->i_fd_video, VIDIOC_DQBUF, &buf ) < 0 )
851 /* Could ignore EIO, see spec. */
854 msg_Err( p_demux, "Failed to wait (VIDIOC_DQBUF)" );
861 for( i = 0; i < p_sys->i_nbuffers; i++ )
863 if( buf.m.userptr == (unsigned long)p_sys->p_buffers[i].start &&
864 buf.length == p_sys->p_buffers[i].length ) break;
867 if( i >= p_sys->i_nbuffers )
869 msg_Err( p_demux, "Failed capturing new frame as i>=nbuffers" );
873 p_block = ProcessVideoFrame( p_demux, (uint8_t*)buf.m.userptr );
874 if( !p_block ) return 0;
877 if( ioctl( p_sys->i_fd_video, VIDIOC_QBUF, &buf ) < 0 )
879 msg_Err (p_demux, "Failed to unlock (VIDIOC_QBUF)");
888 p_sys->i_video_pts = p_block->i_pts = p_block->i_dts = mdate();
893 /*****************************************************************************
894 * ProcessVideoFrame: Helper function to take a buffer and copy it into
896 *****************************************************************************/
897 static block_t* ProcessVideoFrame( demux_t *p_demux, uint8_t *p_frame )
899 demux_sys_t *p_sys = p_demux->p_sys;
902 if( !p_frame ) return 0;
905 if( !( p_block = block_New( p_demux, p_sys->i_video_frame_size ) ) )
907 msg_Warn( p_demux, "Cannot get new block" );
912 memcpy( p_block->p_buffer, p_frame, p_sys->i_video_frame_size );
917 /*****************************************************************************
918 * GrabAudio: Grab an audio frame
919 *****************************************************************************/
920 static block_t* GrabAudio( demux_t *p_demux )
922 demux_sys_t *p_sys = p_demux->p_sys;
923 struct audio_buf_info buf_info;
924 int i_read, i_correct;
927 /* Copied from v4l.c */
929 if( p_sys->p_block_audio ) p_block = p_sys->p_block_audio;
930 else p_block = block_New( p_demux, p_sys->i_audio_max_frame_size );
934 msg_Warn( p_demux, "cannot get block" );
938 p_sys->p_block_audio = p_block;
940 i_read = read( p_sys->i_fd_audio, p_block->p_buffer,
941 p_sys->i_audio_max_frame_size );
943 if( i_read <= 0 ) return 0;
945 p_block->i_buffer = i_read;
946 p_sys->p_block_audio = 0;
948 /* Correct the date because of kernel buffering */
950 if( ioctl( p_sys->i_fd_audio, SNDCTL_DSP_GETISPACE, &buf_info ) == 0 )
952 i_correct += buf_info.bytes;
956 p_block->i_pts = p_block->i_dts =
957 mdate() - I64C(1000000) * (mtime_t)i_correct /
958 2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate;
963 /*****************************************************************************
964 * Helper function to initalise video IO using the Read method
965 *****************************************************************************/
966 static int InitRead( demux_t *p_demux, int i_fd, unsigned int i_buffer_size )
968 demux_sys_t *p_sys = p_demux->p_sys;
970 p_sys->p_buffers = calloc( 1, sizeof( *p_sys->p_buffers ) );
971 if( !p_sys->p_buffers )
973 msg_Err( p_demux, "Out of memory" );
977 p_sys->p_buffers[0].length = i_buffer_size;
978 p_sys->p_buffers[0].start = malloc( i_buffer_size );
979 if( !p_sys->p_buffers[0].start )
981 msg_Err( p_demux, "Out of memory" );
992 /*****************************************************************************
993 * Helper function to initalise video IO using the mmap method
994 *****************************************************************************/
995 static int InitMmap( demux_t *p_demux, int i_fd )
997 demux_sys_t *p_sys = p_demux->p_sys;
998 struct v4l2_requestbuffers req;
1000 memset( &req, 0, sizeof(req) );
1002 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1003 req.memory = V4L2_MEMORY_MMAP;
1005 if( ioctl( i_fd, VIDIOC_REQBUFS, &req ) < 0 )
1007 msg_Err( p_demux, "device does not support mmap i/o" );
1013 msg_Err( p_demux, "Insufficient buffer memory" );
1017 p_sys->p_buffers = calloc( req.count, sizeof( *p_sys->p_buffers ) );
1018 if( !p_sys->p_buffers )
1020 msg_Err( p_demux, "Out of memory" );
1024 for( p_sys->i_nbuffers = 0; p_sys->i_nbuffers < req.count; ++p_sys->i_nbuffers )
1026 struct v4l2_buffer buf;
1028 memset( &buf, 0, sizeof(buf) );
1029 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1030 buf.memory = V4L2_MEMORY_MMAP;
1031 buf.index = p_sys->i_nbuffers;
1033 if( ioctl( i_fd, VIDIOC_QUERYBUF, &buf ) < 0 )
1035 msg_Err( p_demux, "VIDIOC_QUERYBUF" );
1039 p_sys->p_buffers[p_sys->i_nbuffers].length = buf.length;
1040 p_sys->p_buffers[p_sys->i_nbuffers].start =
1041 mmap( NULL, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, i_fd, buf.m.offset );
1043 if( p_sys->p_buffers[p_sys->i_nbuffers].start == MAP_FAILED )
1045 msg_Err( p_demux, "mmap failed (%m)" );
1053 return VLC_EGENERIC;
1057 /*****************************************************************************
1058 * Helper function to initalise video IO using the userbuf method
1059 *****************************************************************************/
1060 static int InitUserP( demux_t *p_demux, int i_fd, unsigned int i_buffer_size )
1062 demux_sys_t *p_sys = p_demux->p_sys;
1063 struct v4l2_requestbuffers req;
1064 unsigned int i_page_size;
1066 i_page_size = getpagesize();
1067 i_buffer_size = ( i_buffer_size + i_page_size - 1 ) & ~( i_page_size - 1);
1069 memset( &req, 0, sizeof(req) );
1071 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1072 req.memory = V4L2_MEMORY_USERPTR;
1074 if( ioctl( i_fd, VIDIOC_REQBUFS, &req ) < 0 )
1076 msg_Err( p_demux, "device does not support user pointer i/o" );
1080 p_sys->p_buffers = calloc( 4, sizeof( *p_sys->p_buffers ) );
1081 if( !p_sys->p_buffers )
1083 msg_Err( p_demux, "Out of memory" );
1087 for( p_sys->i_nbuffers = 0; p_sys->i_nbuffers < 4; ++p_sys->i_nbuffers )
1089 p_sys->p_buffers[p_sys->i_nbuffers].length = i_buffer_size;
1090 p_sys->p_buffers[p_sys->i_nbuffers].start =
1091 vlc_memalign( &p_sys->p_buffers[p_sys->i_nbuffers].orig_userp,
1092 /* boundary */ i_page_size, i_buffer_size );
1094 if( !p_sys->p_buffers[p_sys->i_nbuffers].start )
1096 msg_Err( p_demux, "out of memory" );
1104 return VLC_EGENERIC;
1108 /*****************************************************************************
1109 * IsPixelFormatSupported: returns true if the specified V4L2 pixel format is
1110 * in the array of supported formats returned by the driver
1111 *****************************************************************************/
1112 vlc_bool_t IsPixelFormatSupported( demux_t *p_demux, unsigned int i_pixelformat )
1114 demux_sys_t *p_sys = p_demux->p_sys;
1116 for( int i_index = 0; i_index < p_sys->i_codec; i_index++ )
1118 if( p_sys->p_codecs[i_index].pixelformat == i_pixelformat ) return VLC_TRUE;
1124 /*****************************************************************************
1125 * OpenVideoDev: open and set up the video device and probe for capabilities
1126 *****************************************************************************/
1127 int OpenVideoDev( demux_t *p_demux, char *psz_device )
1130 demux_sys_t *p_sys = p_demux->p_sys;
1131 struct v4l2_cropcap cropcap;
1132 struct v4l2_crop crop;
1133 struct v4l2_format fmt;
1135 enum v4l2_buf_type buf_type;
1137 if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
1139 msg_Err( p_demux, "cannot open device (%m)" );
1143 /* Select standard */
1145 if( p_sys->i_selected_standard_id != V4L2_STD_UNKNOWN )
1147 if( ioctl( i_fd, VIDIOC_S_STD, &p_sys->i_selected_standard_id ) < 0 )
1149 msg_Err( p_demux, "cannot set standard (%m)" );
1152 msg_Dbg( p_demux, "Set standard" );
1157 if( p_sys->i_selected_input > p_sys->i_input )
1159 msg_Warn( p_demux, "invalid input. Using the default one" );
1160 p_sys->i_selected_input = 0;
1163 if( ioctl( i_fd, VIDIOC_S_INPUT, &p_sys->i_selected_input ) < 0 )
1165 msg_Err( p_demux, "cannot set input (%m)" );
1169 /* Verify device support for the various IO methods */
1172 case IO_METHOD_READ:
1173 if( !(p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE) )
1175 msg_Err( p_demux, "device does not support read i/o" );
1180 case IO_METHOD_MMAP:
1181 case IO_METHOD_USERPTR:
1182 if( !(p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING) )
1184 msg_Err( p_demux, "device does not support streaming i/o" );
1190 msg_Err( p_demux, "io method not supported" );
1194 /* Reset Cropping */
1195 memset( &cropcap, 0, sizeof(cropcap) );
1196 cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1197 if( ioctl( i_fd, VIDIOC_CROPCAP, &cropcap ) >= 0 )
1199 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1200 crop.c = cropcap.defrect; /* reset to default */
1201 if( ioctl( i_fd, VIDIOC_S_CROP, &crop ) < 0 )
1206 /* Cropping not supported. */
1209 /* Errors ignored. */
1215 /* Try and find default resolution if not specified */
1216 memset( &fmt, 0, sizeof(fmt) );
1217 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1219 if( p_sys->i_width <= 0 || p_sys->i_height <= 0 )
1221 if( ioctl( i_fd, VIDIOC_G_FMT, &fmt ) < 0 )
1223 msg_Err( p_demux, "Cannot get default width and height." );
1227 p_sys->i_width = fmt.fmt.pix.width;
1228 p_sys->i_height = fmt.fmt.pix.height;
1230 if( fmt.fmt.pix.field == V4L2_FIELD_ALTERNATE )
1232 p_sys->i_height = p_sys->i_height * 2;
1237 msg_Dbg( p_demux, "trying specified size %dx%d", p_sys->i_width, p_sys->i_height );
1240 fmt.fmt.pix.width = p_sys->i_width;
1241 fmt.fmt.pix.height = p_sys->i_height;
1242 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
1244 /* Test and set Chroma */
1245 fmt.fmt.pix.pixelformat = 0;
1246 if( p_sys->psz_requested_chroma && strlen( p_sys->psz_requested_chroma ) > 0 )
1248 /* User specified chroma */
1249 if( strlen( p_sys->psz_requested_chroma ) >= 4 )
1251 int i_requested_fourcc = VLC_FOURCC(
1252 p_sys->psz_requested_chroma[0], p_sys->psz_requested_chroma[1],
1253 p_sys->psz_requested_chroma[2], p_sys->psz_requested_chroma[3] );
1254 for( int i = 0; v4l2chroma_to_fourcc[i].i_v4l2 != 0; i++ )
1256 if( v4l2chroma_to_fourcc[i].i_fourcc == i_requested_fourcc )
1258 fmt.fmt.pix.pixelformat = v4l2chroma_to_fourcc[i].i_v4l2;
1263 /* Try and set user chroma */
1264 if( !IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat ) || ( fmt.fmt.pix.pixelformat && ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 ) )
1266 msg_Warn( p_demux, "Driver is unable to use specified chroma %s. Trying defaults.", p_sys->psz_requested_chroma );
1267 fmt.fmt.pix.pixelformat = 0;
1271 /* If no user specified chroma, find best */
1272 if( !fmt.fmt.pix.pixelformat )
1274 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YVU420;
1275 if( !IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat ) || ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 )
1277 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
1278 if( !IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat ) || ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 )
1280 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1281 if( !IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat ) || ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 )
1283 msg_Err( p_demux, "Could not select any of the default chromas!" );
1290 /* Reassign width, height and chroma incase driver override */
1291 p_sys->i_width = fmt.fmt.pix.width;
1292 p_sys->i_height = fmt.fmt.pix.height;
1294 /* Look up final fourcc */
1295 p_sys->i_fourcc = 0;
1296 for( int i = 0; v4l2chroma_to_fourcc[i].i_fourcc != 0; i++ )
1298 if( v4l2chroma_to_fourcc[i].i_v4l2 == fmt.fmt.pix.pixelformat )
1300 p_sys->i_fourcc = v4l2chroma_to_fourcc[i].i_fourcc;
1305 /* Buggy driver paranoia */
1306 i_min = fmt.fmt.pix.width * 2;
1307 if( fmt.fmt.pix.bytesperline < i_min )
1308 fmt.fmt.pix.bytesperline = i_min;
1309 i_min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
1310 if( fmt.fmt.pix.sizeimage < i_min )
1311 fmt.fmt.pix.sizeimage = i_min;
1313 /* Init vout Picture */
1314 vout_InitPicture( VLC_OBJECT(p_demux), &p_sys->pic, p_sys->i_fourcc,
1315 p_sys->i_width, p_sys->i_height, p_sys->i_width *
1316 VOUT_ASPECT_FACTOR / p_sys->i_height );
1317 if( !p_sys->pic.i_planes )
1319 msg_Err( p_demux, "unsupported chroma" );
1322 p_sys->i_video_frame_size = 0;
1323 for( int i = 0; i < p_sys->pic.i_planes; i++ )
1325 p_sys->i_video_frame_size += p_sys->pic.p[i].i_visible_lines * p_sys->pic.p[i].i_visible_pitch;
1328 /* Init IO method */
1331 case IO_METHOD_READ:
1332 if( InitRead( p_demux, i_fd, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
1335 case IO_METHOD_MMAP:
1336 if( InitMmap( p_demux, i_fd ) != VLC_SUCCESS ) goto open_failed;
1339 case IO_METHOD_USERPTR:
1340 if( InitUserP( p_demux, i_fd, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
1347 es_format_Init( &es_fmt, VIDEO_ES, p_sys->i_fourcc );
1348 es_fmt.video.i_width = p_sys->i_width;
1349 es_fmt.video.i_height = p_sys->i_height;
1350 es_fmt.video.i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
1352 /* Setup rgb mask for RGB formats */
1353 if( p_sys->i_fourcc == VLC_FOURCC( 'R','V','2','4' ) )
1355 /* This is in BGR format */
1356 es_fmt.video.i_bmask = 0x00ff0000;
1357 es_fmt.video.i_gmask = 0x0000ff00;
1358 es_fmt.video.i_rmask = 0x000000ff;
1361 msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
1362 (char*)&es_fmt.i_codec, es_fmt.video.i_width, es_fmt.video.i_height );
1363 p_sys->p_es_video = es_out_Add( p_demux->out, &es_fmt );
1369 case IO_METHOD_READ:
1373 case IO_METHOD_MMAP:
1374 for (unsigned int i = 0; i < p_sys->i_nbuffers; ++i)
1376 struct v4l2_buffer buf;
1378 memset( &buf, 0, sizeof(buf) );
1379 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1380 buf.memory = V4L2_MEMORY_MMAP;
1383 if( ioctl( i_fd, VIDIOC_QBUF, &buf ) < 0 )
1385 msg_Err( p_demux, "VIDIOC_QBUF failed" );
1390 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1391 if( ioctl( i_fd, VIDIOC_STREAMON, &buf_type ) < 0 )
1393 msg_Err( p_demux, "VIDIOC_STREAMON failed" );
1399 case IO_METHOD_USERPTR:
1400 for( unsigned int i = 0; i < p_sys->i_nbuffers; ++i )
1402 struct v4l2_buffer buf;
1404 memset( &buf, 0, sizeof(buf) );
1405 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1406 buf.memory = V4L2_MEMORY_USERPTR;
1408 buf.m.userptr = (unsigned long)p_sys->p_buffers[i].start;
1409 buf.length = p_sys->p_buffers[i].length;
1411 if( ioctl( i_fd, VIDIOC_QBUF, &buf ) < 0 )
1413 msg_Err( p_demux, "VIDIOC_QBUF failed" );
1418 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1419 if( ioctl( i_fd, VIDIOC_STREAMON, &buf_type ) < 0 )
1421 msg_Err( p_demux, "VIDIOC_STREAMON failed" );
1429 if( p_sys->f_fps >= 0.1 )
1431 msg_Dbg( p_demux, "User set fps=%f", p_sys->f_fps );
1437 if( i_fd >= 0 ) close( i_fd );
1442 /*****************************************************************************
1443 * OpenAudioDev: open and set up the audio device and probe for capabilities
1444 *****************************************************************************/
1445 int OpenAudioDev( demux_t *p_demux, char *psz_device )
1447 demux_sys_t *p_sys = p_demux->p_sys;
1450 if( (i_fd = open( psz_device, O_RDONLY | O_NONBLOCK )) < 0 )
1452 msg_Err( p_demux, "cannot open audio device (%m)" );
1456 i_format = AFMT_S16_LE;
1457 if( ioctl( i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
1458 || i_format != AFMT_S16_LE )
1460 msg_Err( p_demux, "cannot set audio format (16b little endian) "
1465 if( ioctl( i_fd, SNDCTL_DSP_STEREO,
1466 &p_sys->b_stereo ) < 0 )
1468 msg_Err( p_demux, "cannot set audio channels count (%m)" );
1472 if( ioctl( i_fd, SNDCTL_DSP_SPEED,
1473 &p_sys->i_sample_rate ) < 0 )
1475 msg_Err( p_demux, "cannot set audio sample rate (%m)" );
1479 msg_Dbg( p_demux, "opened adev=`%s' %s %dHz",
1480 psz_device, p_sys->b_stereo ? "stereo" : "mono",
1481 p_sys->i_sample_rate );
1483 p_sys->i_audio_max_frame_size = 6 * 1024;
1486 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC('a','r','a','w') );
1488 fmt.audio.i_channels = p_sys->b_stereo ? 2 : 1;
1489 fmt.audio.i_rate = p_sys->i_sample_rate;
1490 fmt.audio.i_bitspersample = 16; /* FIXME ? */
1491 fmt.audio.i_blockalign = fmt.audio.i_channels * fmt.audio.i_bitspersample / 8;
1492 fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate * fmt.audio.i_bitspersample;
1494 msg_Dbg( p_demux, "new audio es %d channels %dHz",
1495 fmt.audio.i_channels, fmt.audio.i_rate );
1497 p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
1503 if( i_fd >= 0 ) close( i_fd );
1508 /*****************************************************************************
1509 * ProbeVideoDev: probe video for capabilities
1510 *****************************************************************************/
1511 vlc_bool_t ProbeVideoDev( demux_t *p_demux, char *psz_device )
1517 demux_sys_t *p_sys = p_demux->p_sys;
1519 if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
1521 msg_Err( p_demux, "cannot open video device (%m)" );
1525 /* Get device capabilites */
1527 if( ioctl( i_fd, VIDIOC_QUERYCAP, &p_sys->dev_cap ) < 0 )
1529 msg_Err( p_demux, "cannot get video capabilities (%m)" );
1533 msg_Dbg( p_demux, "V4L2 device: %s using driver: %s (version: %u.%u.%u) on %s",
1534 p_sys->dev_cap.card,
1535 p_sys->dev_cap.driver,
1536 (p_sys->dev_cap.version >> 16) & 0xFF,
1537 (p_sys->dev_cap.version >> 8) & 0xFF,
1538 p_sys->dev_cap.version & 0xFF,
1539 p_sys->dev_cap.bus_info );
1541 msg_Dbg( p_demux, "the device has the capabilities: (%c) Video Capure, "
1544 ( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE ? 'X':' '),
1545 ( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO ? 'X':' '),
1546 ( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER ? 'X':' ') );
1548 msg_Dbg( p_demux, "supported I/O methods are: (%c) Read/Write, "
1550 "(%c) Asynchronous",
1551 ( p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE ? 'X':' ' ),
1552 ( p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING ? 'X':' ' ),
1553 ( p_sys->dev_cap.capabilities & V4L2_CAP_ASYNCIO ? 'X':' ' ) );
1555 /* Now, enumerate all the video inputs. This is useless at the moment
1556 since we have no way to present that info to the user except with
1559 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
1561 while( ioctl( i_fd, VIDIOC_S_INPUT, &p_sys->i_input ) >= 0 )
1566 p_sys->p_inputs = calloc( 1, p_sys->i_input * sizeof( struct v4l2_input ) );
1567 if( !p_sys->p_inputs ) goto open_failed;
1569 for( i_index = 0; i_index < p_sys->i_input; i_index++ )
1571 p_sys->p_inputs[i_index].index = i_index;
1573 if( ioctl( i_fd, VIDIOC_ENUMINPUT, &p_sys->p_inputs[i_index] ) )
1575 msg_Err( p_demux, "cannot get video input characteristics (%m)" );
1578 msg_Dbg( p_demux, "video input %i (%s) has type: %s",
1580 p_sys->p_inputs[i_index].name,
1581 p_sys->p_inputs[i_index].type
1582 == V4L2_INPUT_TYPE_TUNER ?
1584 "External analog input" );
1588 /* Probe video standards */
1589 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
1591 struct v4l2_standard t_standards;
1592 t_standards.index = 0;
1593 while( ioctl( i_fd, VIDIOC_ENUMSTD, &t_standards ) >=0 )
1595 p_sys->i_standard++;
1596 t_standards.index = p_sys->i_standard;
1599 p_sys->p_standards = calloc( 1, p_sys->i_standard * sizeof( struct v4l2_standard ) );
1600 if( !p_sys->p_standards ) goto open_failed;
1602 for( i_standard = 0; i_standard < p_sys->i_standard; i_standard++ )
1604 p_sys->p_standards[i_standard].index = i_standard;
1606 if( ioctl( i_fd, VIDIOC_ENUMSTD, &p_sys->p_standards[i_standard] ) )
1608 msg_Err( p_demux, "cannot get video input standards (%m)" );
1611 msg_Dbg( p_demux, "video standard %i is: %s",
1613 p_sys->p_standards[i_standard].name);
1617 /* initialize the structures for the ioctls */
1618 for( i_index = 0; i_index < 32; i_index++ )
1620 p_sys->p_audios[i_index].index = i_index;
1623 /* Probe audio inputs */
1625 if( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO )
1627 while( p_sys->i_audio < 32 &&
1628 ioctl( i_fd, VIDIOC_S_AUDIO, &p_sys->p_audios[p_sys->i_audio] ) >= 0 )
1630 if( ioctl( i_fd, VIDIOC_G_AUDIO, &p_sys->p_audios[ p_sys->i_audio] ) < 0 )
1632 msg_Err( p_demux, "cannot get audio input characteristics (%m)" );
1636 msg_Dbg( p_demux, "audio device %i (%s) is %s",
1638 p_sys->p_audios[p_sys->i_audio].name,
1639 p_sys->p_audios[p_sys->i_audio].capability &
1640 V4L2_AUDCAP_STEREO ?
1641 "Stereo" : "Mono" );
1647 if( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER )
1649 struct v4l2_tuner tuner;
1651 memset( &tuner, 0, sizeof(tuner) );
1652 while( ioctl( i_fd, VIDIOC_S_TUNER, &tuner ) >= 0 )
1655 tuner.index = p_sys->i_tuner;
1658 p_sys->p_tuners = calloc( 1, p_sys->i_tuner * sizeof( struct v4l2_tuner ) );
1659 if( !p_sys->p_tuners ) goto open_failed;
1661 for( i_index = 0; i_index < p_sys->i_tuner; i_index++ )
1663 p_sys->p_tuners[i_index].index = i_index;
1665 if( ioctl( i_fd, VIDIOC_G_TUNER, &p_sys->p_tuners[i_index] ) )
1667 msg_Err( p_demux, "cannot get tuner characteristics (%m)" );
1670 msg_Dbg( p_demux, "tuner %i (%s) has type: %s, "
1671 "frequency range: %.1f %s -> %.1f %s",
1673 p_sys->p_tuners[i_index].name,
1674 p_sys->p_tuners[i_index].type
1675 == V4L2_TUNER_RADIO ?
1676 "Radio" : "Analog TV",
1677 p_sys->p_tuners[i_index].rangelow * 62.5,
1678 p_sys->p_tuners[i_index].capability &
1679 V4L2_TUNER_CAP_LOW ?
1681 p_sys->p_tuners[i_index].rangehigh * 62.5,
1682 p_sys->p_tuners[i_index].capability &
1683 V4L2_TUNER_CAP_LOW ?
1688 /* Probe for available chromas */
1689 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
1691 struct v4l2_fmtdesc codec;
1694 memset( &codec, 0, sizeof(codec) );
1695 codec.index = i_index;
1696 codec.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1698 while( ioctl( i_fd, VIDIOC_ENUM_FMT, &codec ) >= 0 )
1701 codec.index = i_index;
1704 p_sys->i_codec = i_index;
1706 p_sys->p_codecs = calloc( 1, p_sys->i_codec * sizeof( struct v4l2_fmtdesc ) );
1708 for( i_index = 0; i_index < p_sys->i_codec; i_index++ )
1710 p_sys->p_codecs[i_index].index = i_index;
1711 p_sys->p_codecs[i_index].type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1713 if( ioctl( i_fd, VIDIOC_ENUM_FMT, &p_sys->p_codecs[i_index] ) < 0 )
1715 msg_Err( p_demux, "cannot get codec description (%m)" );
1719 /* only print if vlc supports the format */
1720 vlc_bool_t b_codec_supported = VLC_FALSE;
1721 for( int i = 0; v4l2chroma_to_fourcc[i].i_v4l2 != 0; i++ )
1723 if( v4l2chroma_to_fourcc[i].i_v4l2 == p_sys->p_codecs[i_index].pixelformat )
1725 b_codec_supported = VLC_TRUE;
1728 memset( &psz_fourcc, 0, sizeof( psz_fourcc ) );
1729 vlc_fourcc_to_char( v4l2chroma_to_fourcc[i].i_fourcc, &psz_fourcc );
1730 msg_Dbg( p_demux, "device supports chroma %4s [%s]",
1732 p_sys->p_codecs[i_index].description );
1735 if( !b_codec_supported )
1737 msg_Dbg( p_demux, "device codec %s not supported",
1738 p_sys->p_codecs[i_index].description );
1745 if( i_fd >= 0 ) close( i_fd );
1750 if( i_fd >= 0 ) close( i_fd );
1755 /*****************************************************************************
1756 * ProbeAudioDev: probe audio for capabilities
1757 *****************************************************************************/
1758 vlc_bool_t ProbeAudioDev( demux_t *p_demux, char *psz_device )
1763 if( ( i_fd = open( psz_device, O_RDONLY | O_NONBLOCK ) ) < 0 )
1765 msg_Err( p_demux, "cannot open audio device (%m)" );
1769 /* this will fail if the device is video */
1770 if( ioctl( i_fd, SNDCTL_DSP_GETCAPS, &i_caps ) < 0 )
1772 msg_Err( p_demux, "cannot get audio caps (%m)" );
1776 if( i_fd >= 0 ) close( i_fd );
1780 if( i_fd >= 0 ) close( i_fd );