1 /*****************************************************************************
2 * v4l2.c : Video4Linux2 input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2004 the VideoLAN team
5 * $Id: v4l.c 16084 2006-07-19 09:45:02Z zorglub $
7 * Author: Benjamin Pracht <bigben at videolan dot org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
31 #include <<vlc_input.h>>
32 #include <<vlc_vout.h>>
36 #include <sys/types.h>
38 #include <sys/ioctl.h>
39 #include <sys/errno.h>
41 #include <asm/types.h> /* for videodev2.h */
43 #include <linux/videodev2.h>
45 /*****************************************************************************
47 *****************************************************************************/
49 static int Open ( vlc_object_t * );
50 static void Close( vlc_object_t * );
52 #define DEV_TEXT N_("Device name")
53 #define DEV_LONGTEXT N_( \
54 "Name of the device to use. " \
55 "If you don't specify anything, /dev/video0 will be used.")
56 #define INPUT_TEXT N_( "Input" )
57 #define INPUT_LONGTEXT N_( \
58 "Input of the card to use (Usually, 0 = tuner, " \
59 "1 = composite, 2 = svideo)." )
63 set_shortname( _("Video4Linux2") );
64 set_description( _("Video4Linux2 input") );
65 set_category( CAT_INPUT );
66 set_subcategory( SUBCAT_INPUT_ACCESS );
68 add_string( "v4l2-dev", "/dev/video0", 0, DEV_TEXT, DEV_LONGTEXT,
70 add_integer( "v4l2-input", 0, NULL, INPUT_TEXT, INPUT_LONGTEXT,
73 add_shortcut( "v4l2" );
74 set_capability( "access_demux", 10 );
75 set_callbacks( Open, Close );
78 /*****************************************************************************
79 * Access: local prototypes
80 *****************************************************************************/
82 static int DemuxMMAP( demux_t * );
83 static int Control( demux_t *, int, va_list );
85 static int ProbeDev( demux_t * );
86 static int OpenVideoDev( demux_t * );
88 static block_t *GrabVideo( demux_t * );
96 struct v4l2_capability dev_cap;
99 struct v4l2_input *p_inputs;
100 int i_selected_input;
103 /* V4L2 devices cannot have more than 32 audio inputs */
104 struct v4l2_audio p_audios[32];
107 struct v4l2_tuner *p_tuners;
110 struct v4l2_fmtdesc *p_codecs;
113 /*****************************************************************************
114 * Open: opens v4l device
115 *****************************************************************************
117 * url: <video device>::::
119 *****************************************************************************/
120 static int Open( vlc_object_t *p_this )
122 demux_t *p_demux = (demux_t*)p_this;
127 p_demux->pf_control = Control;
128 p_demux->info.i_update = 0;
129 p_demux->info.i_title = 0;
130 p_demux->info.i_seekpoint = 0;
132 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
133 if( p_sys == NULL ) return VLC_ENOMEM;
134 memset( p_sys, 0, sizeof( demux_sys_t ) );
136 p_sys->psz_device = var_CreateGetString( p_demux, "v4l2-dev" );
138 if( ProbeDev( p_demux ) < 0 ) return VLC_EGENERIC;
140 if( OpenVideoDev( p_demux ) < 0 ) return VLC_EGENERIC;
145 /*****************************************************************************
146 * Close: close device, free resources
147 *****************************************************************************/
148 static void Close( vlc_object_t *p_this )
150 demux_t *p_demux = (demux_t *)p_this;
151 demux_sys_t *p_sys = p_demux->p_sys;
153 if( p_sys->i_fd_video >= 0 ) close( p_sys->i_fd_video );
155 if( p_sys->psz_device ) free( p_sys->psz_device );
156 if( p_sys->p_inputs ) free( p_sys->p_inputs );
157 if( p_sys->p_tuners ) free( p_sys->p_tuners );
158 if( p_sys->p_codecs ) free( p_sys->p_codecs );
163 /*****************************************************************************
165 *****************************************************************************/
166 static int Control( demux_t *p_demux, int i_query, va_list args )
170 /*****************************************************************************
172 *****************************************************************************/
173 static int DemuxMMAP( demux_t *p_demux )
178 /*****************************************************************************
179 * OpenVideoDev: open and set up the video device and probe for capabilities
180 *****************************************************************************/
181 int OpenVideoDev( demux_t *p_demux )
184 demux_sys_t *p_sys = p_demux->p_sys;
186 if( ( i_fd = open( p_sys->psz_device, O_RDWR ) ) < 0 )
188 msg_Err( p_demux, "cannot open device (%s)", strerror( errno ) );
192 p_sys->i_fd_video = i_fd;
194 if( p_sys->i_selected_input = var_CreateGetInteger( p_demux, "v4l2-input" )
197 msg_Warn( p_demux, "invalid input. Using the default one" );
198 p_sys->i_selected_input = 0;
201 if( ioctl( i_fd, VIDIOC_S_INPUT, &p_sys->i_selected_input ) < 0 )
203 msg_Err( p_demux, "cannot set input (%s)", strerror( errno ) );
207 if( p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING )
209 struct v4l2_requestbuffers reqbuf;
211 reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
212 reqbuf.memory = V4L2_MEMORY_MMAP;
215 if( ioctl( i_fd, VIDIOC_REQBUFS, &reqbuf ) < 0 )
217 msg_Err( p_demux, "cannot initiate I/O operation (%s). "
218 "Only MMAP is supported at the moment", strerror( errno ) );
221 p_demux->pf_demux = DemuxMMAP;
225 msg_Warn( p_demux, "I/O method not supported at the moment" );
232 if( i_fd ) close( i_fd );
233 p_sys->i_fd_video = 0;
238 /*****************************************************************************
239 * ProbeDev: probe for capabilities
240 *****************************************************************************/
241 int ProbeDev( demux_t *p_demux )
246 demux_sys_t *p_sys = p_demux->p_sys;
248 if( ( i_fd = open( p_sys->psz_device, O_RDWR ) ) < 0 )
250 msg_Err( p_demux, "cannot open device (%s)", strerror( errno ) );
254 /* Get device capabilites */
256 if( ioctl( i_fd, VIDIOC_QUERYCAP, &p_sys->dev_cap ) < 0 )
258 msg_Err( p_demux, "cannot get capabilities (%s)", strerror( errno ) );
262 msg_Dbg( p_demux, "V4L2 device: %s using driver: %s (version: %u.%u.%u) on %s",
264 p_sys->dev_cap.driver,
265 (p_sys->dev_cap.version >> 16) & 0xFF,
266 (p_sys->dev_cap.version >> 8) & 0xFF,
267 p_sys->dev_cap.version & 0xFF,
268 p_sys->dev_cap.bus_info );
270 msg_Dbg( p_demux, "the device has the capabilities: (%c) Video Capure, "
273 ( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE ? 'X':' '),
274 ( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO ? 'X':' '),
275 ( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER ? 'X':' ') );
277 msg_Dbg( p_demux, "supported I/O methods are: (%c) Read/Write, "
280 ( p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE ? 'X':' ' ),
281 ( p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING ? 'X':' ' ),
282 ( p_sys->dev_cap.capabilities & V4L2_CAP_ASYNCIO ? 'X':' ' ) );
284 /* Now, enumerate all the video inputs. This is useless at the moment
285 since we have no way to present that info to the user except with
288 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
290 while( ioctl( i_fd, VIDIOC_S_INPUT, &p_sys->i_input ) >= 0 )
295 p_sys->p_inputs = malloc( p_sys->i_input * sizeof( struct v4l2_input ) );
296 if( !p_sys->p_inputs ) goto open_failed;
297 memset( p_sys->p_inputs, 0, sizeof( struct v4l2_input ) );
299 for( i_index = 0; i_index < p_sys->i_input; i_index++ )
301 p_sys->p_inputs[i_index].index = i_index;
303 if( ioctl( i_fd, VIDIOC_ENUMINPUT, &p_sys->p_inputs[i_index] ) )
305 msg_Err( p_demux, "cannot get video input characteristics (%s)",
309 msg_Dbg( p_demux, "video input %i (%s) has type: %s",
311 p_sys->p_inputs[i_index].name,
312 p_sys->p_inputs[i_index].type
313 == V4L2_INPUT_TYPE_TUNER ?
315 "External analog input" );
319 /* initialize the structures for the ioctls */
320 for( i_index = 0; i_index < 32; i_index++ )
322 p_sys->p_audios[i_index].index = i_index;
325 /* Probe audio inputs */
327 if( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO )
329 while( p_sys->i_audio < 32 &&
330 ioctl( i_fd, VIDIOC_S_AUDIO, &p_sys->p_audios[p_sys->i_audio] ) >= 0 )
332 if( ioctl( i_fd, VIDIOC_G_AUDIO, &p_sys->p_audios[ p_sys->i_audio] ) < 0 )
334 msg_Err( p_demux, "cannot get video input characteristics (%s)",
339 msg_Dbg( p_demux, "audio device %i (%s) is %s",
341 p_sys->p_audios[p_sys->i_audio].name,
342 p_sys->p_audios[p_sys->i_audio].capability &
350 if( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER )
352 struct v4l2_tuner tuner = {};
354 while( ioctl( i_fd, VIDIOC_S_TUNER, &tuner ) >= 0 )
357 tuner.index = p_sys->i_tuner;
360 p_sys->p_tuners = malloc( p_sys->i_tuner * sizeof( struct v4l2_tuner ) );
361 if( !p_sys->p_tuners ) goto open_failed;
362 memset( p_sys->p_tuners, 0, sizeof( struct v4l2_tuner ) );
364 for( i_index = 0; i_index < p_sys->i_tuner; i_index++ )
366 p_sys->p_tuners[i_index].index = i_index;
368 if( ioctl( i_fd, VIDIOC_G_TUNER, &p_sys->p_tuners[i_index] ) )
370 msg_Err( p_demux, "cannot get tuner characteristics (%s)",
374 msg_Dbg( p_demux, "tuner %i (%s) has type: %s, "
375 "frequency range: %.1f %s -> %.1f %s",
377 p_sys->p_tuners[i_index].name,
378 p_sys->p_tuners[i_index].type
379 == V4L2_TUNER_RADIO ?
380 "Radio" : "Analog TV",
381 p_sys->p_tuners[i_index].rangelow * 62.5,
382 p_sys->p_tuners[i_index].capability &
385 p_sys->p_tuners[i_index].rangehigh * 62.5,
386 p_sys->p_tuners[i_index].capability &
392 /* Probe for available chromas */
393 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
395 struct v4l2_fmtdesc codec = {};
398 codec.index = i_index;
399 codec.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
401 while( ioctl( i_fd, VIDIOC_ENUM_FMT, &codec ) >= 0 )
404 codec.index = i_index;
407 p_sys->i_codec = i_index;
409 p_sys->p_codecs = malloc( p_sys->i_codec * sizeof( struct v4l2_fmtdesc ) );
410 memset( p_sys->p_codecs, 0, p_sys->i_codec * sizeof( struct v4l2_fmtdesc ) );
412 for( i_index = 0; i_index < p_sys->i_codec; i_index++ )
414 p_sys->p_codecs[i_index].index = i_index;
415 p_sys->p_codecs[i_index].type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
417 if( ioctl( i_fd, VIDIOC_ENUM_FMT, &p_sys->p_codecs[i_index] ) < 0 )
419 msg_Err( p_demux, "cannot get codec description (%s)", strerror( errno ) );
423 msg_Dbg( p_demux, "device supports Codec %s",
424 p_sys->p_codecs[i_index].description );
429 if( i_fd >= 0 ) close( i_fd );
434 if( i_fd >= 0 ) close( i_fd );