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, no audio device 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 FPS_TEXT N_( "Framerate" )
85 #define FPS_LONGTEXT N_( "Framerate to capture, if applicable " \
86 "(-1 for autodetect)." )
87 #define STEREO_TEXT N_( "Stereo" )
88 #define STEREO_LONGTEXT N_( \
89 "Capture the audio stream in stereo." )
90 #define SAMPLERATE_TEXT N_( "Samplerate" )
91 #define SAMPLERATE_LONGTEXT N_( \
92 "Samplerate of the captured audio stream, in Hz (eg: 11025, 22050, 44100, 48000)" )
93 #define CACHING_TEXT N_("Caching value in ms")
94 #define CACHING_LONGTEXT N_( \
95 "Caching value for V4L2 captures. This " \
96 "value should be set in milliseconds." )
104 static int i_standards_list[] =
105 { V4L2_STD_UNKNOWN, V4L2_STD_SECAM, V4L2_STD_PAL, V4L2_STD_NTSC };
106 static const char *psz_standards_list_text[] =
107 { N_("Default"), N_("SECAM"), N_("PAL"), N_("NTSC") };
109 static int i_iomethod_list[] =
110 { IO_METHOD_READ, IO_METHOD_MMAP, IO_METHOD_USERPTR };
111 static const char *psz_iomethod_list_text[] =
112 { N_("READ"), N_("MMAP"), N_("USERPTR") };
115 set_shortname( _("Video4Linux2") );
116 set_description( _("Video4Linux2 input") );
117 set_category( CAT_INPUT );
118 set_subcategory( SUBCAT_INPUT_ACCESS );
120 add_string( "v4l2-dev", "/dev/video0", 0, DEV_TEXT, DEV_LONGTEXT,
122 add_string( "v4l2-adev", "/dev/dsp", 0, DEV_TEXT, DEV_LONGTEXT,
124 add_integer( "v4l2-standard", 0, NULL, STANDARD_TEXT, STANDARD_LONGTEXT,
126 change_integer_list( i_standards_list, psz_standards_list_text, 0 );
127 add_string( "v4l2-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
129 add_integer( "v4l2-input", 0, NULL, INPUT_TEXT, INPUT_LONGTEXT,
131 add_integer( "v4l2-io", IO_METHOD_MMAP, NULL, IOMETHOD_TEXT,
132 IOMETHOD_LONGTEXT, VLC_FALSE );
133 change_integer_list( i_iomethod_list, psz_iomethod_list_text, 0 );
134 add_float( "v4l2-fps", 0, NULL, FPS_TEXT, FPS_LONGTEXT, VLC_TRUE );
135 add_bool( "v4l2-stereo", VLC_TRUE, NULL, STEREO_TEXT, STEREO_LONGTEXT,
137 add_integer( "v4l2-samplerate", 48000, NULL, SAMPLERATE_TEXT,
138 SAMPLERATE_LONGTEXT, VLC_TRUE );
139 add_integer( "v4l2-caching", DEFAULT_PTS_DELAY / 1000, NULL,
140 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
142 add_shortcut( "v4l2" );
143 set_capability( "access_demux", 10 );
144 set_callbacks( Open, Close );
147 /*****************************************************************************
148 * Access: local prototypes
149 *****************************************************************************/
151 static void ParseMRL( demux_t * );
153 static int Control( demux_t *, int, va_list );
155 static int Demux( demux_t * );
156 static block_t* GrabVideo( demux_t *p_demux );
157 static block_t* ProcessVideoFrame( demux_t *p_demux, uint8_t *p_frame );
158 static block_t* GrabAudio( demux_t *p_demux );
160 vlc_bool_t IsChromaSupported( demux_t *p_demux, unsigned int i_v4l2 );
161 unsigned int GetFourccFromString( char *psz_fourcc );
163 static int OpenVideoDev( demux_t *, char *psz_device );
164 static int OpenAudioDev( demux_t *, char *psz_device );
165 static vlc_bool_t ProbeVideoDev( demux_t *, char *psz_device );
166 static vlc_bool_t ProbeAudioDev( demux_t *, char *psz_device );
172 } v4l2chroma_to_fourcc[] =
174 { V4L2_PIX_FMT_GREY, VLC_FOURCC( 'G', 'R', 'E', 'Y' ) },
175 { V4L2_PIX_FMT_HI240, VLC_FOURCC( 'I', '2', '4', '0' ) },
176 { V4L2_PIX_FMT_RGB565, VLC_FOURCC( 'R', 'V', '1', '6' ) },
177 { V4L2_PIX_FMT_RGB555, VLC_FOURCC( 'R', 'V', '1', '5' ) },
178 { V4L2_PIX_FMT_BGR24, VLC_FOURCC( 'R', 'V', '2', '4' ) },
179 { V4L2_PIX_FMT_BGR32, VLC_FOURCC( 'R', 'V', '3', '2' ) },
180 { V4L2_PIX_FMT_YUYV, VLC_FOURCC( 'Y', 'U', 'Y', '2' ) },
181 { V4L2_PIX_FMT_YUYV, VLC_FOURCC( 'Y', 'U', 'Y', 'V' ) },
182 { V4L2_PIX_FMT_UYVY, VLC_FOURCC( 'U', 'Y', 'V', 'Y' ) },
183 { V4L2_PIX_FMT_Y41P, VLC_FOURCC( 'I', '4', '1', 'N' ) },
184 { V4L2_PIX_FMT_YUV422P, VLC_FOURCC( 'I', '4', '2', '2' ) },
185 { V4L2_PIX_FMT_YVU420, VLC_FOURCC( 'I', '4', '2', '0' ) },
186 { V4L2_PIX_FMT_YUV411P, VLC_FOURCC( 'I', '4', '1', '1' ) },
187 { V4L2_PIX_FMT_YUV410, VLC_FOURCC( 'I', '4', '1', '0' ) },
200 char *psz_device; /* Main device from MRL, can be video or audio */
213 struct v4l2_capability dev_cap;
216 struct v4l2_input *p_inputs;
217 int i_selected_input;
220 struct v4l2_standard *p_standards;
221 v4l2_std_id i_selected_standard_id;
224 /* V4L2 devices cannot have more than 32 audio inputs */
225 struct v4l2_audio p_audios[32];
228 struct v4l2_tuner *p_tuners;
231 struct v4l2_fmtdesc *p_codecs;
233 struct buffer_t *p_buffers;
234 unsigned int i_nbuffers;
238 float f_fps; /* <= 0.0 mean to grab at full rate */
239 mtime_t i_video_pts; /* only used when f_fps > 0 */
243 int i_video_frame_size;
245 es_out_id_t *p_es_video;
250 int i_audio_max_frame_size;
251 block_t *p_block_audio;
252 es_out_id_t *p_es_audio;
255 /*****************************************************************************
256 * Open: opens v4l2 device
257 *****************************************************************************
259 * url: <video device>::::
261 *****************************************************************************/
262 static int Open( vlc_object_t *p_this )
264 demux_t *p_demux = (demux_t*)p_this;
269 /* Only when selected */
270 if( *p_demux->psz_access == '\0' ) return VLC_EGENERIC;
273 p_demux->pf_control = Control;
274 p_demux->pf_demux = Demux;
275 p_demux->info.i_update = 0;
276 p_demux->info.i_title = 0;
277 p_demux->info.i_seekpoint = 0;
279 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
280 if( p_sys == NULL ) return VLC_ENOMEM;
282 p_sys->i_video_pts = -1;
284 var_Create( p_demux, "v4l2-standard", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
285 var_Get( p_demux, "v4l2-standard", &val );
286 p_sys->i_selected_standard_id = i_standards_list[val.i_int];
288 p_sys->i_selected_input = var_CreateGetInteger( p_demux, "v4l2-input" );
290 p_sys->io = var_CreateGetInteger( p_demux, "v4l2-io" );
292 var_Create( p_demux, "v4l2-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
293 var_Get( p_demux, "v4l2-fps", &val );
294 p_sys->f_fps = val.f_float;
296 var_Create( p_demux, "v4l2-samplerate", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
297 var_Get( p_demux, "v4l2-samplerate", &val );
298 p_sys->i_sample_rate = val.i_int;
300 psz = var_CreateGetString( p_demux, "v4l2-chroma" );
301 p_sys->i_fourcc = GetFourccFromString( psz );
304 var_Create( p_demux, "v4l2-stereo", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
305 var_Get( p_demux, "v4l2-stereo", &val );
306 p_sys->b_stereo = val.b_bool;
308 p_sys->i_pts = var_CreateGetInteger( p_demux, "v4l2-caching" );
310 p_sys->psz_device = p_sys->psz_vdev = p_sys->psz_adev = NULL;
311 p_sys->i_fd_video = -1;
312 p_sys->i_fd_audio = -1;
314 p_sys->p_es_video = p_sys->p_es_audio = 0;
315 p_sys->p_block_audio = 0;
319 /* Find main device (video or audio) */
320 if( p_sys->psz_device && *p_sys->psz_device )
322 msg_Dbg( p_demux, "main device='%s'", p_sys->psz_device );
324 /* Try to open as video device */
325 msg_Dbg( p_demux, "trying device '%s' as video", p_sys->psz_device );
326 if( ProbeVideoDev( p_demux, p_sys->psz_device ) )
328 msg_Dbg( p_demux, "'%s' is a video device", p_sys->psz_device );
329 /* Device was a video device */
330 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
331 p_sys->psz_vdev = p_sys->psz_device;
332 p_sys->psz_device = NULL;
333 p_sys->i_fd_video = OpenVideoDev( p_demux, p_sys->psz_vdev );
334 if( p_sys->i_fd_video < 0 )
342 /* Try to open as audio device */
343 msg_Dbg( p_demux, "trying device '%s' as audio", p_sys->psz_device );
344 if( ProbeAudioDev( p_demux, p_sys->psz_device ) )
346 msg_Dbg( p_demux, "'%s' is an audio device", p_sys->psz_device );
347 /* Device was an audio device */
348 if( p_sys->psz_adev ) free( p_sys->psz_adev );
349 p_sys->psz_adev = p_sys->psz_device;
350 p_sys->psz_device = NULL;
351 p_sys->i_fd_audio = OpenAudioDev( p_demux, p_sys->psz_adev );
352 if( p_sys->i_fd_audio < 0 )
361 /* If no device opened, only continue if the access was forced */
362 if( p_sys->i_fd_video < 0 && p_sys->i_fd_audio < 0 )
364 if( strcmp( p_demux->psz_access, "v4l2" ) )
371 /* Find video device */
372 if( p_sys->i_fd_video < 0 )
374 if( !p_sys->psz_vdev || !*p_sys->psz_vdev )
376 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
377 p_sys->psz_vdev = var_CreateGetString( p_demux, "v4l2-dev" );;
380 if( p_sys->psz_vdev && *p_sys->psz_vdev && ProbeVideoDev( p_demux, p_sys->psz_vdev ) )
382 p_sys->i_fd_video = OpenVideoDev( p_demux, p_sys->psz_vdev );
386 /* Find audio device */
387 if( p_sys->i_fd_audio < 0 )
389 if( !p_sys->psz_adev || !*p_sys->psz_adev )
391 if( p_sys->psz_adev ) free( p_sys->psz_adev );
392 p_sys->psz_adev = var_CreateGetString( p_demux, "v4l2-adev" );;
395 if( p_sys->psz_adev && *p_sys->psz_adev && ProbeAudioDev( p_demux, p_sys->psz_adev ) )
397 p_sys->i_fd_audio = OpenAudioDev( p_demux, p_sys->psz_adev );
401 if( p_sys->i_fd_video < 0 && p_sys->i_fd_audio < 0 )
410 /*****************************************************************************
411 * ParseMRL: parse the options contained in the MRL
412 *****************************************************************************/
413 static void ParseMRL( demux_t *p_demux )
415 demux_sys_t *p_sys = p_demux->p_sys;
417 char *psz_dup = strdup( p_demux->psz_path );
418 char *psz_parser = psz_dup;
420 while( *psz_parser && *psz_parser != ':' )
425 if( *psz_parser == ':' )
430 *psz_parser++ = '\0';
432 if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) ) )
436 psz_parser += strlen( "adev=" );
437 if( strchr( psz_parser, ':' ) )
439 i_len = strchr( psz_parser, ':' ) - psz_parser;
443 i_len = strlen( psz_parser );
446 p_sys->psz_adev = strndup( psz_parser, i_len );
450 else if( !strncmp( psz_parser, "standard=", strlen( "standard=" ) ) )
452 psz_parser += strlen( "standard=" );
453 if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
455 p_sys->i_selected_standard_id = V4L2_STD_PAL;
456 psz_parser += strlen( "pal" );
458 else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
460 p_sys->i_selected_standard_id = V4L2_STD_NTSC;
461 psz_parser += strlen( "ntsc" );
463 else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
465 p_sys->i_selected_standard_id = V4L2_STD_SECAM;
466 psz_parser += strlen( "secam" );
468 else if( !strncmp( psz_parser, "default", strlen( "default" ) ) )
470 p_sys->i_selected_standard_id = V4L2_STD_UNKNOWN;
471 psz_parser += strlen( "default" );
475 p_sys->i_selected_standard_id = i_standards_list[strtol( psz_parser, &psz_parser, 0 )];
478 else if( !strncmp( psz_parser, "chroma=", strlen( "chroma=" ) ) )
482 psz_parser += strlen( "chroma=" );
483 if( strchr( psz_parser, ':' ) )
485 i_len = strchr( psz_parser, ':' ) - psz_parser;
489 i_len = strlen( psz_parser );
492 char* chroma = strndup( psz_parser, i_len );
493 p_sys->i_fourcc = GetFourccFromString( chroma );
498 else if( !strncmp( psz_parser, "input=", strlen( "input=" ) ) )
500 p_sys->i_selected_input = strtol( psz_parser + strlen( "input=" ),
503 else if( !strncmp( psz_parser, "fps=", strlen( "fps=" ) ) )
505 p_sys->f_fps = strtof( psz_parser + strlen( "fps=" ),
508 else if( !strncmp( psz_parser, "io=", strlen( "io=" ) ) )
510 psz_parser += strlen( "io=" );
511 if( !strncmp( psz_parser, "read", strlen( "read" ) ) )
513 p_sys->io = IO_METHOD_READ;
514 psz_parser += strlen( "read" );
516 else if( !strncmp( psz_parser, "mmap", strlen( "mmap" ) ) )
518 p_sys->io = IO_METHOD_MMAP;
519 psz_parser += strlen( "mmap" );
521 else if( !strncmp( psz_parser, "userptr", strlen( "userptr" ) ) )
523 p_sys->io = IO_METHOD_USERPTR;
524 psz_parser += strlen( "userptr" );
528 p_sys->io = strtol( psz_parser, &psz_parser, 0 );
531 else if( !strncmp( psz_parser, "samplerate=",
532 strlen( "samplerate=" ) ) )
534 p_sys->i_sample_rate =
535 strtol( psz_parser + strlen( "samplerate=" ),
538 else if( !strncmp( psz_parser, "stereo", strlen( "stereo" ) ) )
540 psz_parser += strlen( "stereo" );
541 p_sys->b_stereo = VLC_TRUE;
543 else if( !strncmp( psz_parser, "mono", strlen( "mono" ) ) )
545 psz_parser += strlen( "mono" );
546 p_sys->b_stereo = VLC_FALSE;
548 else if( !strncmp( psz_parser, "caching=", strlen( "caching=" ) ) )
550 p_sys->i_pts = strtol( psz_parser + strlen( "caching=" ),
551 &psz_parser, DEFAULT_PTS_DELAY / 1000 );
555 msg_Warn( p_demux, "unknown option" );
558 while( *psz_parser && *psz_parser != ':' )
563 if( *psz_parser == '\0' )
573 p_sys->psz_device = strdup( psz_dup );
575 if( psz_dup ) free( psz_dup );
578 /*****************************************************************************
579 * Close: close device, free resources
580 *****************************************************************************/
581 static void Close( vlc_object_t *p_this )
583 demux_t *p_demux = (demux_t *)p_this;
584 demux_sys_t *p_sys = p_demux->p_sys;
585 struct v4l2_buffer buf;
586 enum v4l2_buf_type buf_type;
589 /* Stop video capture */
590 if( p_sys->i_fd_video >= 0 )
599 case IO_METHOD_USERPTR:
600 /* Some drivers 'hang' internally if this is not done before streamoff */
601 for( unsigned int i = 0; i < p_sys->i_nbuffers; i++ )
603 memset( &buf, 0, sizeof(buf) );
604 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
605 buf.memory = V4L2_MEMORY_USERPTR;
606 ioctl( p_sys->i_fd_video, VIDIOC_DQBUF, &buf ); /* ignore result */
609 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
610 if( ioctl( p_sys->i_fd_video, VIDIOC_STREAMOFF, &buf_type ) < 0 ) {
611 msg_Err( p_demux, "VIDIOC_STREAMOFF failed" );
618 /* Free Video Buffers */
619 if( p_sys->p_buffers ) {
623 free( p_sys->p_buffers[0].start );
627 for( i = 0; i < p_sys->i_nbuffers; ++i )
629 if( munmap( p_sys->p_buffers[i].start, p_sys->p_buffers[i].length ) )
631 msg_Err( p_demux, "munmap failed" );
636 case IO_METHOD_USERPTR:
637 for( i = 0; i < p_sys->i_nbuffers; ++i )
639 free( p_sys->p_buffers[i].orig_userp );
643 free( p_sys->p_buffers );
647 if( p_sys->i_fd_video >= 0 ) close( p_sys->i_fd_video );
648 if( p_sys->i_fd_audio >= 0 ) close( p_sys->i_fd_audio );
650 if( p_sys->p_block_audio ) block_Release( p_sys->p_block_audio );
651 if( p_sys->psz_device ) free( p_sys->psz_device );
652 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
653 if( p_sys->psz_adev ) free( p_sys->psz_adev );
654 if( p_sys->p_standards ) free( p_sys->p_standards );
655 if( p_sys->p_inputs ) free( p_sys->p_inputs );
656 if( p_sys->p_tuners ) free( p_sys->p_tuners );
657 if( p_sys->p_codecs ) free( p_sys->p_codecs );
662 /*****************************************************************************
664 *****************************************************************************/
665 static int Control( demux_t *p_demux, int i_query, va_list args )
667 demux_sys_t *p_sys = p_demux->p_sys;
673 /* Special for access_demux */
674 case DEMUX_CAN_PAUSE:
675 case DEMUX_SET_PAUSE_STATE:
676 case DEMUX_CAN_CONTROL_PACE:
677 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
681 case DEMUX_GET_PTS_DELAY:
682 pi64 = (int64_t*)va_arg( args, int64_t * );
683 *pi64 = (int64_t)p_sys->i_pts * 1000;
687 pi64 = (int64_t*)va_arg( args, int64_t * );
691 /* TODO implement others */
699 /*****************************************************************************
700 * Demux: Processes the audio or video frame
701 *****************************************************************************/
702 static int Demux( demux_t *p_demux )
704 demux_sys_t *p_sys = p_demux->p_sys;
705 es_out_id_t *p_es = p_sys->p_es_audio;
706 block_t *p_block = NULL;
708 /* Try grabbing audio frames first */
709 if( p_sys->i_fd_audio < 0 || !( p_block = GrabAudio( p_demux ) ) )
711 /* Try grabbing video frame */
712 p_es = p_sys->p_es_video;
713 if( p_sys->i_fd_video > 0 ) p_block = GrabVideo( p_demux );
718 /* Sleep so we do not consume all the cpu, 10ms seems
719 * like a good value (100fps) */
724 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
725 es_out_Send( p_demux->out, p_es, p_block );
730 /*****************************************************************************
731 * GrabVideo: Grab a video frame
732 *****************************************************************************/
733 static block_t* GrabVideo( demux_t *p_demux )
735 demux_sys_t *p_sys = p_demux->p_sys;
737 block_t *p_block = NULL;
738 struct v4l2_buffer buf;
740 if( p_sys->f_fps >= 0.1 && p_sys->i_video_pts > 0 )
742 mtime_t i_dur = (mtime_t)((double)1000000 / (double)p_sys->f_fps);
744 /* Did we wait long enough ? (frame rate reduction) */
745 if( p_sys->i_video_pts + i_dur > mdate() ) return 0;
748 /* Grab Video Frame */
752 if( read( p_sys->i_fd_video, p_sys->p_buffers[0].start, p_sys->p_buffers[0].length ) )
759 /* Could ignore EIO, see spec. */
762 msg_Err( p_demux, "Failed to read frame" );
767 p_block = ProcessVideoFrame( p_demux, (uint8_t*)p_sys->p_buffers[0].start );
768 if( !p_block ) return 0;
773 memset( &buf, 0, sizeof(buf) );
774 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
775 buf.memory = V4L2_MEMORY_MMAP;
777 /* Wait for next frame */
778 if (ioctl( p_sys->i_fd_video, VIDIOC_DQBUF, &buf ) < 0 )
785 /* Could ignore EIO, see spec. */
788 msg_Err( p_demux, "Failed to wait (VIDIOC_DQBUF)" );
793 if( buf.index >= p_sys->i_nbuffers ) {
794 msg_Err( p_demux, "Failed capturing new frame as i>=nbuffers" );
798 p_block = ProcessVideoFrame( p_demux, p_sys->p_buffers[buf.index].start );
799 if( !p_block ) return 0;
802 if( ioctl( p_sys->i_fd_video, VIDIOC_QBUF, &buf ) < 0 )
804 msg_Err (p_demux, "Failed to unlock (VIDIOC_QBUF)");
810 case IO_METHOD_USERPTR:
811 memset( &buf, 0, sizeof(buf) );
812 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
813 buf.memory = V4L2_MEMORY_USERPTR;
815 /* Wait for next frame */
816 if (ioctl( p_sys->i_fd_video, VIDIOC_DQBUF, &buf ) < 0 )
823 /* Could ignore EIO, see spec. */
826 msg_Err( p_demux, "Failed to wait (VIDIOC_DQBUF)" );
833 for( i = 0; i < p_sys->i_nbuffers; i++ )
835 if( buf.m.userptr == (unsigned long)p_sys->p_buffers[i].start &&
836 buf.length == p_sys->p_buffers[i].length ) break;
839 if( i >= p_sys->i_nbuffers ) {
840 msg_Err( p_demux, "Failed capturing new frame as i>=nbuffers" );
844 p_block = ProcessVideoFrame( p_demux, (uint8_t*)buf.m.userptr );
845 if( !p_block ) return 0;
848 if( ioctl( p_sys->i_fd_video, VIDIOC_QBUF, &buf ) < 0 )
850 msg_Err (p_demux, "Failed to unlock (VIDIOC_QBUF)");
859 p_sys->i_video_pts = p_block->i_pts = p_block->i_dts = mdate();
864 /*****************************************************************************
865 * ProcessVideoFrame: Helper function to take a buffer and copy it into
867 *****************************************************************************/
868 static block_t* ProcessVideoFrame( demux_t *p_demux, uint8_t *p_frame )
870 demux_sys_t *p_sys = p_demux->p_sys;
873 if( !p_frame ) return 0;
876 if( !( p_block = block_New( p_demux, p_sys->i_video_frame_size ) ) )
878 msg_Warn( p_demux, "Cannot get new block" );
883 memcpy( p_block->p_buffer, p_frame, p_sys->i_video_frame_size );
888 /*****************************************************************************
889 * GrabAudio: Grab an audio frame
890 *****************************************************************************/
891 static block_t* GrabAudio( demux_t *p_demux )
893 demux_sys_t *p_sys = p_demux->p_sys;
894 struct audio_buf_info buf_info;
895 int i_read, i_correct;
898 /* Copied from v4l.c */
900 if( p_sys->p_block_audio ) p_block = p_sys->p_block_audio;
901 else p_block = block_New( p_demux, p_sys->i_audio_max_frame_size );
905 msg_Warn( p_demux, "cannot get block" );
909 p_sys->p_block_audio = p_block;
911 i_read = read( p_sys->i_fd_audio, p_block->p_buffer,
912 p_sys->i_audio_max_frame_size );
914 if( i_read <= 0 ) return 0;
916 p_block->i_buffer = i_read;
917 p_sys->p_block_audio = 0;
919 /* Correct the date because of kernel buffering */
921 if( ioctl( p_sys->i_fd_audio, SNDCTL_DSP_GETISPACE, &buf_info ) == 0 )
923 i_correct += buf_info.bytes;
927 p_block->i_pts = p_block->i_dts =
928 mdate() - I64C(1000000) * (mtime_t)i_correct /
929 2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate;
934 /*****************************************************************************
935 * Helper function to initalise video IO using the Read method
936 *****************************************************************************/
937 static int InitRead( demux_t *p_demux, int i_fd, unsigned int i_buffer_size )
939 demux_sys_t *p_sys = p_demux->p_sys;
941 p_sys->p_buffers = calloc( 1, sizeof( *p_sys->p_buffers ) );
942 if( !p_sys->p_buffers )
944 msg_Err( p_demux, "Out of memory" );
948 p_sys->p_buffers[0].length = i_buffer_size;
949 p_sys->p_buffers[0].start = malloc( i_buffer_size );
950 if( !p_sys->p_buffers[0].start )
952 msg_Err( p_demux, "Out of memory" );
963 /*****************************************************************************
964 * Helper function to initalise video IO using the mmap method
965 *****************************************************************************/
966 static int InitMmap( demux_t *p_demux, int i_fd )
968 demux_sys_t *p_sys = p_demux->p_sys;
969 struct v4l2_requestbuffers req;
971 memset( &req, 0, sizeof(req) );
973 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
974 req.memory = V4L2_MEMORY_MMAP;
976 if( ioctl( i_fd, VIDIOC_REQBUFS, &req ) < 0 )
978 msg_Err( p_demux, "device does not support mmap i/o" );
984 msg_Err( p_demux, "Insufficient buffer memory" );
988 p_sys->p_buffers = calloc( req.count, sizeof( *p_sys->p_buffers ) );
989 if( !p_sys->p_buffers )
991 msg_Err( p_demux, "Out of memory" );
995 for( p_sys->i_nbuffers = 0; p_sys->i_nbuffers < req.count; ++p_sys->i_nbuffers )
997 struct v4l2_buffer buf;
999 memset( &buf, 0, sizeof(buf) );
1000 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1001 buf.memory = V4L2_MEMORY_MMAP;
1002 buf.index = p_sys->i_nbuffers;
1004 if( ioctl( i_fd, VIDIOC_QUERYBUF, &buf ) < 0 )
1006 msg_Err( p_demux, "VIDIOC_QUERYBUF" );
1010 p_sys->p_buffers[p_sys->i_nbuffers].length = buf.length;
1011 p_sys->p_buffers[p_sys->i_nbuffers].start =
1012 mmap( NULL, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, i_fd, buf.m.offset );
1014 if( p_sys->p_buffers[p_sys->i_nbuffers].start == MAP_FAILED )
1016 msg_Err( p_demux, "mmap failed (%m)" );
1024 return VLC_EGENERIC;
1028 /*****************************************************************************
1029 * Helper function to initalise video IO using the userbuf method
1030 *****************************************************************************/
1031 static int InitUserP( demux_t *p_demux, int i_fd, unsigned int i_buffer_size )
1033 demux_sys_t *p_sys = p_demux->p_sys;
1034 struct v4l2_requestbuffers req;
1035 unsigned int i_page_size;
1037 i_page_size = getpagesize();
1038 i_buffer_size = ( i_buffer_size + i_page_size - 1 ) & ~( i_page_size - 1);
1040 memset( &req, 0, sizeof(req) );
1042 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1043 req.memory = V4L2_MEMORY_USERPTR;
1045 if( ioctl( i_fd, VIDIOC_REQBUFS, &req ) < 0 )
1047 msg_Err( p_demux, "device does not support user pointer i/o" );
1051 p_sys->p_buffers = calloc( 4, sizeof( *p_sys->p_buffers ) );
1052 if( !p_sys->p_buffers )
1054 msg_Err( p_demux, "Out of memory" );
1058 for( p_sys->i_nbuffers = 0; p_sys->i_nbuffers < 4; ++p_sys->i_nbuffers )
1060 p_sys->p_buffers[p_sys->i_nbuffers].length = i_buffer_size;
1061 p_sys->p_buffers[p_sys->i_nbuffers].start =
1062 vlc_memalign( &p_sys->p_buffers[p_sys->i_nbuffers].orig_userp,
1063 /* boundary */ i_page_size, i_buffer_size );
1065 if( !p_sys->p_buffers[p_sys->i_nbuffers].start )
1067 msg_Err( p_demux, "out of memory" );
1075 return VLC_EGENERIC;
1079 /*****************************************************************************
1080 * GetFourccFromString: Returns the fourcc code from the given string
1081 *****************************************************************************/
1082 unsigned int GetFourccFromString( char *psz_fourcc )
1084 if( strlen( psz_fourcc ) >= 4 )
1086 int i_chroma = VLC_FOURCC( psz_fourcc[0], psz_fourcc[1], psz_fourcc[2], psz_fourcc[3] );
1088 /* Find out v4l2 chroma code */
1089 for( int i = 0; v4l2chroma_to_fourcc[i].i_fourcc != 0; i++ )
1091 if( v4l2chroma_to_fourcc[i].i_fourcc == i_chroma )
1093 return v4l2chroma_to_fourcc[i].i_fourcc;
1101 /*****************************************************************************
1102 * IsChromaSupported: returns true if the specified V4L2 chroma constant is
1103 * in the array of supported chromas returned by the driver
1104 *****************************************************************************/
1105 vlc_bool_t IsChromaSupported( demux_t *p_demux, unsigned int i_chroma )
1107 demux_sys_t *p_sys = p_demux->p_sys;
1109 for( int i_index = 0; i_index < p_sys->i_codec; i_index++ )
1111 if( p_sys->p_codecs[i_index].pixelformat == i_chroma ) return VLC_TRUE;
1117 /*****************************************************************************
1118 * OpenVideoDev: open and set up the video device and probe for capabilities
1119 *****************************************************************************/
1120 int OpenVideoDev( demux_t *p_demux, char *psz_device )
1123 demux_sys_t *p_sys = p_demux->p_sys;
1124 struct v4l2_cropcap cropcap;
1125 struct v4l2_crop crop;
1126 struct v4l2_format fmt;
1128 enum v4l2_buf_type buf_type;
1130 if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
1132 msg_Err( p_demux, "cannot open device (%m)" );
1136 /* Select standard */
1138 if( p_sys->i_selected_standard_id != V4L2_STD_UNKNOWN )
1140 if( ioctl( i_fd, VIDIOC_S_STD, &p_sys->i_selected_standard_id ) < 0 )
1142 msg_Err( p_demux, "cannot set input (%m)" );
1145 msg_Dbg( p_demux, "Set standard" );
1150 if( p_sys->i_selected_input > p_sys->i_input )
1152 msg_Warn( p_demux, "invalid input. Using the default one" );
1153 p_sys->i_selected_input = 0;
1156 if( ioctl( i_fd, VIDIOC_S_INPUT, &p_sys->i_selected_input ) < 0 )
1158 msg_Err( p_demux, "cannot set input (%m)" );
1162 /* Verify device support for the various IO methods */
1165 case IO_METHOD_READ:
1166 if( !(p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE) )
1168 msg_Err( p_demux, "device does not support read i/o" );
1173 case IO_METHOD_MMAP:
1174 case IO_METHOD_USERPTR:
1175 if( !(p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING) )
1177 msg_Err( p_demux, "device does not support streaming i/o" );
1183 msg_Err( p_demux, "io method not supported" );
1187 /* Reset Cropping */
1188 memset( &cropcap, 0, sizeof(cropcap) );
1189 cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1190 if( ioctl( i_fd, VIDIOC_CROPCAP, &cropcap ) >= 0 )
1192 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1193 crop.c = cropcap.defrect; /* reset to default */
1194 if( ioctl( i_fd, VIDIOC_S_CROP, &crop ) < 0 )
1199 /* Cropping not supported. */
1202 /* Errors ignored. */
1208 /* Try and find default resolution if not specified */
1209 if( !p_sys->i_width && !p_sys->i_height ) {
1210 memset( &fmt, 0, sizeof(fmt) );
1211 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1213 if ( ioctl( i_fd, VIDIOC_G_FMT, &fmt ) < 0 )
1215 msg_Err( p_demux, "Cannot get default width and height." );
1219 p_sys->i_width = fmt.fmt.pix.width;
1220 p_sys->i_height = fmt.fmt.pix.height;
1222 if( fmt.fmt.pix.field == V4L2_FIELD_ALTERNATE )
1224 p_sys->i_height = p_sys->i_height * 2;
1228 fmt.fmt.pix.width = p_sys->i_width;
1229 fmt.fmt.pix.height = p_sys->i_height;
1230 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
1232 /* Test and set Chroma */
1233 fmt.fmt.pix.pixelformat = 0;
1234 if( p_sys->i_fourcc )
1236 /* User specified chroma */
1237 for( int i = 0; v4l2chroma_to_fourcc[i].i_v4l2 != 0; i++ )
1239 if( v4l2chroma_to_fourcc[i].i_fourcc == p_sys->i_fourcc )
1241 fmt.fmt.pix.pixelformat = v4l2chroma_to_fourcc[i].i_v4l2;
1245 /* Try and set user chroma */
1246 if( !IsChromaSupported( p_demux, fmt.fmt.pix.pixelformat ) || ( fmt.fmt.pix.pixelformat && ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 ) )
1248 msg_Warn( p_demux, "Driver is unable to use specified chroma. Using defaults." );
1249 fmt.fmt.pix.pixelformat = 0;
1253 /* If no user specified chroma, find best */
1254 if( !fmt.fmt.pix.pixelformat )
1256 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YVU420;
1257 if( !IsChromaSupported( p_demux, fmt.fmt.pix.pixelformat ) || ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 )
1259 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
1260 if( !IsChromaSupported( p_demux, fmt.fmt.pix.pixelformat ) || ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 )
1262 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1263 if( !IsChromaSupported( p_demux, fmt.fmt.pix.pixelformat ) || ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 )
1265 msg_Err( p_demux, "Could not select any of the default chromas!" );
1272 /* Reassign width, height and chroma incase driver override */
1273 p_sys->i_width = fmt.fmt.pix.width;
1274 p_sys->i_height = fmt.fmt.pix.height;
1276 /* Look up final fourcc */
1277 p_sys->i_fourcc = 0;
1278 for( int i = 0; v4l2chroma_to_fourcc[i].i_fourcc != 0; i++ )
1280 if( v4l2chroma_to_fourcc[i].i_v4l2 == fmt.fmt.pix.pixelformat )
1282 p_sys->i_fourcc = v4l2chroma_to_fourcc[i].i_fourcc;
1287 /* Buggy driver paranoia */
1288 i_min = fmt.fmt.pix.width * 2;
1289 if( fmt.fmt.pix.bytesperline < i_min )
1290 fmt.fmt.pix.bytesperline = i_min;
1291 i_min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
1292 if( fmt.fmt.pix.sizeimage < i_min )
1293 fmt.fmt.pix.sizeimage = i_min;
1295 /* Init vout Picture */
1296 vout_InitPicture( VLC_OBJECT(p_demux), &p_sys->pic, p_sys->i_fourcc,
1297 p_sys->i_width, p_sys->i_height, p_sys->i_width *
1298 VOUT_ASPECT_FACTOR / p_sys->i_height );
1299 if( !p_sys->pic.i_planes )
1301 msg_Err( p_demux, "unsupported chroma" );
1304 p_sys->i_video_frame_size = 0;
1305 for( int i = 0; i < p_sys->pic.i_planes; i++ )
1307 p_sys->i_video_frame_size += p_sys->pic.p[i].i_visible_lines * p_sys->pic.p[i].i_visible_pitch;
1310 /* Init IO method */
1313 case IO_METHOD_READ:
1314 if( InitRead( p_demux, i_fd, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
1317 case IO_METHOD_MMAP:
1318 if( InitMmap( p_demux, i_fd ) != VLC_SUCCESS ) goto open_failed;
1321 case IO_METHOD_USERPTR:
1322 if( InitUserP( p_demux, i_fd, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
1329 es_format_Init( &es_fmt, VIDEO_ES, p_sys->i_fourcc );
1330 es_fmt.video.i_width = p_sys->i_width;
1331 es_fmt.video.i_height = p_sys->i_height;
1332 es_fmt.video.i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
1334 /* Setup rgb mask for RGB formats */
1335 if( p_sys->i_fourcc == VLC_FOURCC( 'R','V','2','4' ) )
1337 /* This is in BGR format */
1338 es_fmt.video.i_bmask = 0x00ff0000;
1339 es_fmt.video.i_gmask = 0x0000ff00;
1340 es_fmt.video.i_rmask = 0x000000ff;
1343 msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
1344 (char*)&es_fmt.i_codec, es_fmt.video.i_width, es_fmt.video.i_height );
1345 p_sys->p_es_video = es_out_Add( p_demux->out, &es_fmt );
1351 case IO_METHOD_READ:
1355 case IO_METHOD_MMAP:
1356 for (unsigned int i = 0; i < p_sys->i_nbuffers; ++i)
1358 struct v4l2_buffer buf;
1360 memset( &buf, 0, sizeof(buf) );
1361 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1362 buf.memory = V4L2_MEMORY_MMAP;
1365 if( ioctl( i_fd, VIDIOC_QBUF, &buf ) < 0 ) {
1366 msg_Err( p_demux, "VIDIOC_QBUF failed" );
1371 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1372 if( ioctl( i_fd, VIDIOC_STREAMON, &buf_type ) < 0 ) {
1373 msg_Err( p_demux, "VIDIOC_STREAMON failed" );
1379 case IO_METHOD_USERPTR:
1380 for( unsigned int i = 0; i < p_sys->i_nbuffers; ++i )
1382 struct v4l2_buffer buf;
1384 memset( &buf, 0, sizeof(buf) );
1385 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1386 buf.memory = V4L2_MEMORY_USERPTR;
1388 buf.m.userptr = (unsigned long)p_sys->p_buffers[i].start;
1389 buf.length = p_sys->p_buffers[i].length;
1391 if( ioctl( i_fd, VIDIOC_QBUF, &buf ) < 0 ) {
1392 msg_Err( p_demux, "VIDIOC_QBUF failed" );
1397 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1398 if( ioctl( i_fd, VIDIOC_STREAMON, &buf_type ) < 0 ) {
1399 msg_Err( p_demux, "VIDIOC_STREAMON failed" );
1407 if( p_sys->f_fps >= 0.1 )
1409 msg_Dbg( p_demux, "User set fps=%f", p_sys->f_fps );
1415 if( i_fd ) close( i_fd );
1420 /*****************************************************************************
1421 * OpenAudioDev: open and set up the audio device and probe for capabilities
1422 *****************************************************************************/
1423 int OpenAudioDev( demux_t *p_demux, char *psz_device )
1425 demux_sys_t *p_sys = p_demux->p_sys;
1428 if( (i_fd = open( psz_device, O_RDONLY | O_NONBLOCK )) < 0 )
1430 msg_Err( p_demux, "cannot open audio device (%m)" );
1434 i_format = AFMT_S16_LE;
1435 if( ioctl( i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
1436 || i_format != AFMT_S16_LE )
1438 msg_Err( p_demux, "cannot set audio format (16b little endian) "
1443 if( ioctl( i_fd, SNDCTL_DSP_STEREO,
1444 &p_sys->b_stereo ) < 0 )
1446 msg_Err( p_demux, "cannot set audio channels count (%m)" );
1450 if( ioctl( i_fd, SNDCTL_DSP_SPEED,
1451 &p_sys->i_sample_rate ) < 0 )
1453 msg_Err( p_demux, "cannot set audio sample rate (%m)" );
1457 msg_Dbg( p_demux, "opened adev=`%s' %s %dHz",
1458 psz_device, p_sys->b_stereo ? "stereo" : "mono",
1459 p_sys->i_sample_rate );
1461 p_sys->i_audio_max_frame_size = 6 * 1024;
1464 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC('a','r','a','w') );
1466 fmt.audio.i_channels = p_sys->b_stereo ? 2 : 1;
1467 fmt.audio.i_rate = p_sys->i_sample_rate;
1468 fmt.audio.i_bitspersample = 16; /* FIXME ? */
1469 fmt.audio.i_blockalign = fmt.audio.i_channels * fmt.audio.i_bitspersample / 8;
1470 fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate * fmt.audio.i_bitspersample;
1472 msg_Dbg( p_demux, "new audio es %d channels %dHz",
1473 fmt.audio.i_channels, fmt.audio.i_rate );
1475 p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
1481 if( i_fd >= 0 ) close( i_fd );
1486 /*****************************************************************************
1487 * ProbeVideoDev: probe video for capabilities
1488 *****************************************************************************/
1489 vlc_bool_t ProbeVideoDev( demux_t *p_demux, char *psz_device )
1495 demux_sys_t *p_sys = p_demux->p_sys;
1497 if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
1499 msg_Err( p_demux, "cannot open video device (%m)" );
1503 /* Get device capabilites */
1505 if( ioctl( i_fd, VIDIOC_QUERYCAP, &p_sys->dev_cap ) < 0 )
1507 msg_Err( p_demux, "cannot get video capabilities (%m)" );
1511 msg_Dbg( p_demux, "V4L2 device: %s using driver: %s (version: %u.%u.%u) on %s",
1512 p_sys->dev_cap.card,
1513 p_sys->dev_cap.driver,
1514 (p_sys->dev_cap.version >> 16) & 0xFF,
1515 (p_sys->dev_cap.version >> 8) & 0xFF,
1516 p_sys->dev_cap.version & 0xFF,
1517 p_sys->dev_cap.bus_info );
1519 msg_Dbg( p_demux, "the device has the capabilities: (%c) Video Capure, "
1522 ( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE ? 'X':' '),
1523 ( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO ? 'X':' '),
1524 ( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER ? 'X':' ') );
1526 msg_Dbg( p_demux, "supported I/O methods are: (%c) Read/Write, "
1528 "(%c) Asynchronous",
1529 ( p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE ? 'X':' ' ),
1530 ( p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING ? 'X':' ' ),
1531 ( p_sys->dev_cap.capabilities & V4L2_CAP_ASYNCIO ? 'X':' ' ) );
1533 /* Now, enumerate all the video inputs. This is useless at the moment
1534 since we have no way to present that info to the user except with
1537 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
1539 while( ioctl( i_fd, VIDIOC_S_INPUT, &p_sys->i_input ) >= 0 )
1544 p_sys->p_inputs = calloc( 1, p_sys->i_input * sizeof( struct v4l2_input ) );
1545 if( !p_sys->p_inputs ) goto open_failed;
1547 for( i_index = 0; i_index < p_sys->i_input; i_index++ )
1549 p_sys->p_inputs[i_index].index = i_index;
1551 if( ioctl( i_fd, VIDIOC_ENUMINPUT, &p_sys->p_inputs[i_index] ) )
1553 msg_Err( p_demux, "cannot get video input characteristics (%m)" );
1556 msg_Dbg( p_demux, "video input %i (%s) has type: %s",
1558 p_sys->p_inputs[i_index].name,
1559 p_sys->p_inputs[i_index].type
1560 == V4L2_INPUT_TYPE_TUNER ?
1562 "External analog input" );
1566 /* Probe video standards */
1567 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
1569 struct v4l2_standard t_standards;
1570 t_standards.index = 0;
1571 while( ioctl( i_fd, VIDIOC_ENUMSTD, &t_standards ) >=0 )
1573 p_sys->i_standard++;
1574 t_standards.index = p_sys->i_standard;
1577 p_sys->p_standards = calloc( 1, p_sys->i_standard * sizeof( struct v4l2_standard ) );
1578 if( !p_sys->p_standards ) goto open_failed;
1580 for( i_standard = 0; i_standard < p_sys->i_standard; i_standard++ )
1582 p_sys->p_standards[i_standard].index = i_standard;
1584 if( ioctl( i_fd, VIDIOC_ENUMSTD, &p_sys->p_standards[i_standard] ) )
1586 msg_Err( p_demux, "cannot get video input standards (%m)" );
1589 msg_Dbg( p_demux, "video standard %i is: %s",
1591 p_sys->p_standards[i_standard].name);
1595 /* initialize the structures for the ioctls */
1596 for( i_index = 0; i_index < 32; i_index++ )
1598 p_sys->p_audios[i_index].index = i_index;
1601 /* Probe audio inputs */
1603 if( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO )
1605 while( p_sys->i_audio < 32 &&
1606 ioctl( i_fd, VIDIOC_S_AUDIO, &p_sys->p_audios[p_sys->i_audio] ) >= 0 )
1608 if( ioctl( i_fd, VIDIOC_G_AUDIO, &p_sys->p_audios[ p_sys->i_audio] ) < 0 )
1610 msg_Err( p_demux, "cannot get audio input characteristics (%m)" );
1614 msg_Dbg( p_demux, "audio device %i (%s) is %s",
1616 p_sys->p_audios[p_sys->i_audio].name,
1617 p_sys->p_audios[p_sys->i_audio].capability &
1618 V4L2_AUDCAP_STEREO ?
1619 "Stereo" : "Mono" );
1625 if( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER )
1627 struct v4l2_tuner tuner;
1629 memset( &tuner, 0, sizeof(tuner) );
1630 while( ioctl( i_fd, VIDIOC_S_TUNER, &tuner ) >= 0 )
1633 tuner.index = p_sys->i_tuner;
1636 p_sys->p_tuners = calloc( 1, p_sys->i_tuner * sizeof( struct v4l2_tuner ) );
1637 if( !p_sys->p_tuners ) goto open_failed;
1639 for( i_index = 0; i_index < p_sys->i_tuner; i_index++ )
1641 p_sys->p_tuners[i_index].index = i_index;
1643 if( ioctl( i_fd, VIDIOC_G_TUNER, &p_sys->p_tuners[i_index] ) )
1645 msg_Err( p_demux, "cannot get tuner characteristics (%m)" );
1648 msg_Dbg( p_demux, "tuner %i (%s) has type: %s, "
1649 "frequency range: %.1f %s -> %.1f %s",
1651 p_sys->p_tuners[i_index].name,
1652 p_sys->p_tuners[i_index].type
1653 == V4L2_TUNER_RADIO ?
1654 "Radio" : "Analog TV",
1655 p_sys->p_tuners[i_index].rangelow * 62.5,
1656 p_sys->p_tuners[i_index].capability &
1657 V4L2_TUNER_CAP_LOW ?
1659 p_sys->p_tuners[i_index].rangehigh * 62.5,
1660 p_sys->p_tuners[i_index].capability &
1661 V4L2_TUNER_CAP_LOW ?
1666 /* Probe for available chromas */
1667 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
1669 struct v4l2_fmtdesc codec;
1672 memset( &codec, 0, sizeof(codec) );
1673 codec.index = i_index;
1674 codec.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1676 while( ioctl( i_fd, VIDIOC_ENUM_FMT, &codec ) >= 0 )
1679 codec.index = i_index;
1682 p_sys->i_codec = i_index;
1684 p_sys->p_codecs = calloc( 1, p_sys->i_codec * sizeof( struct v4l2_fmtdesc ) );
1686 for( i_index = 0; i_index < p_sys->i_codec; i_index++ )
1688 p_sys->p_codecs[i_index].index = i_index;
1689 p_sys->p_codecs[i_index].type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1691 if( ioctl( i_fd, VIDIOC_ENUM_FMT, &p_sys->p_codecs[i_index] ) < 0 )
1693 msg_Err( p_demux, "cannot get codec description (%m)" );
1697 msg_Dbg( p_demux, "device supports Codec %s",
1698 p_sys->p_codecs[i_index].description );
1703 if( i_fd >= 0 ) close( i_fd );
1708 if( i_fd >= 0 ) close( i_fd );
1713 /*****************************************************************************
1714 * ProbeAudioDev: probe audio for capabilities
1715 *****************************************************************************/
1716 vlc_bool_t ProbeAudioDev( demux_t *p_demux, char *psz_device )
1721 if( ( i_fd = open( psz_device, O_RDONLY | O_NONBLOCK ) ) < 0 )
1723 msg_Err( p_demux, "cannot open audio device (%m)" );
1727 /* this will fail if the device is video */
1728 if( ioctl( i_fd, SNDCTL_DSP_GETCAPS, &i_caps ) < 0 )
1730 msg_Err( p_demux, "cannot get audio caps (%m)" );
1734 if( i_fd ) close( i_fd );
1738 if( i_fd ) close( i_fd );