1 /*****************************************************************************
2 * demux.c : V4L2 raw video demux module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2011 the VideoLAN team
6 * Authors: Benjamin Pracht <bigben at videolan dot org>
7 * Richard Hosking <richard at hovis dot net>
8 * Antoine Cellerier <dionoea at videolan d.t org>
9 * Dennis Lou <dlou99 at yahoo dot com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
31 #include <vlc_demux.h>
33 #include <sys/ioctl.h>
37 static int DemuxControl( demux_t *, int, va_list );
38 static int Demux( demux_t * );
40 int DemuxOpen( vlc_object_t *obj )
42 demux_t *demux = (demux_t *)obj;
43 demux_sys_t *sys = calloc( 1, sizeof( demux_sys_t ) );
44 if( unlikely(sys == NULL) )
48 ParseMRL( obj, demux->psz_location );
49 sys->i_fd = OpenVideo( obj, sys, true );
56 demux->pf_demux = Demux;
57 demux->pf_control = DemuxControl;
58 demux->info.i_update = 0;
59 demux->info.i_title = 0;
60 demux->info.i_seekpoint = 0;
64 void DemuxClose( vlc_object_t *obj )
66 demux_t *demux = (demux_t *)obj;
67 demux_sys_t *sys = demux->p_sys;
70 /* Stop video capture */
78 case IO_METHOD_USERPTR:
80 /* NOTE: Some buggy drivers hang if buffers are not unmapped before
82 for( unsigned i = 0; i < sys->i_nbuffers; i++ )
84 struct v4l2_buffer buf = {
85 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
86 .memory = ( sys->io == IO_METHOD_USERPTR ) ?
87 V4L2_MEMORY_USERPTR : V4L2_MEMORY_MMAP,
89 v4l2_ioctl( fd, VIDIOC_DQBUF, &buf );
91 enum v4l2_buf_type buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
92 v4l2_ioctl( sys->i_fd, VIDIOC_STREAMOFF, &buf_type );
97 /* Free Video Buffers */
98 if( sys->p_buffers ) {
102 free( sys->p_buffers[0].start );
106 for( unsigned i = 0; i < sys->i_nbuffers; ++i )
107 v4l2_munmap( sys->p_buffers[i].start,
108 sys->p_buffers[i].length );
111 case IO_METHOD_USERPTR:
112 for( unsigned i = 0; i < sys->i_nbuffers; ++i )
113 free( sys->p_buffers[i].start );
116 free( sys->p_buffers );
119 ControlsDeinit( obj, sys->controls );
124 static int DemuxControl( demux_t *demux, int query, va_list args )
128 /* Special for access_demux */
129 case DEMUX_CAN_PAUSE:
131 case DEMUX_CAN_CONTROL_PACE:
132 *va_arg( args, bool * ) = false;
135 case DEMUX_GET_PTS_DELAY:
136 *va_arg(args,int64_t *) = INT64_C(1000)
137 * var_InheritInteger( demux, "live-caching" );
141 *va_arg( args, int64_t * ) = mdate();
144 /* TODO implement others */
152 /** Gets a frame in read/write mode */
153 static block_t *BlockRead( vlc_object_t *obj, int fd, size_t size )
155 block_t *block = block_Alloc( size );
156 if( unlikely(block == NULL) )
159 ssize_t val = v4l2_read( fd, block->p_buffer, size );
162 block_Release( block );
167 case EIO: /* could be ignored per specification */
170 msg_Err( obj, "cannot read frame: %m" );
174 block->i_buffer = val;
178 static int Demux( demux_t *demux )
180 demux_sys_t *sys = demux->p_sys;
184 ufd.events = POLLIN|POLLPRI;
186 /* FIXME: remove timeout */
187 while( poll( &ufd, 1, 500 ) == -1 )
190 msg_Err( demux, "poll error: %m" );
194 if( ufd.revents == 0 )
199 if( sys->io == IO_METHOD_READ )
200 block = BlockRead( VLC_OBJECT(demux), ufd.fd, sys->blocksize );
202 block = GrabVideo( VLC_OBJECT(demux), sys );
206 block->i_pts = block->i_dts = mdate();
207 block->i_flags |= sys->i_block_flags;
208 es_out_Control( demux->out, ES_OUT_SET_PCR, block->i_pts );
209 es_out_Send( demux->out, sys->p_es, block );
213 static float GetMaxFPS( vlc_object_t *obj, int fd, uint32_t pixel_format,
214 uint32_t width, uint32_t height )
216 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
217 /* This is new in Linux 2.6.19 */
218 struct v4l2_frmivalenum fie = {
219 .pixel_format = pixel_format,
224 if( v4l2_ioctl( fd, VIDIOC_ENUM_FRAMEINTERVALS, &fie ) < 0 )
229 case V4L2_FRMIVAL_TYPE_DISCRETE:
234 float fps = (float)fie.discrete.denominator
235 / (float)fie.discrete.numerator;
238 msg_Dbg( obj, " discrete frame interval %"PRIu32"/%"PRIu32
240 fie.discrete.numerator, fie.discrete.denominator );
242 } while( v4l2_ioctl( fd, VIDIOC_ENUM_FRAMEINTERVALS, &fie ) >= 0 );
246 case V4L2_FRMIVAL_TYPE_STEPWISE:
247 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
248 msg_Dbg( obj, " frame intervals from %"PRIu32"/%"PRIu32
249 "to %"PRIu32"/%"PRIu32" supported",
250 fie.stepwise.min.numerator, fie.stepwise.min.denominator,
251 fie.stepwise.max.numerator, fie.stepwise.max.denominator );
252 if( fie.type == V4L2_FRMIVAL_TYPE_STEPWISE )
253 msg_Dbg( obj, " with %"PRIu32"/%"PRIu32" step",
254 fie.stepwise.step.numerator,
255 fie.stepwise.step.denominator );
256 return __MAX( (float)fie.stepwise.max.denominator
257 / (float)fie.stepwise.max.numerator,
258 (float)fie.stepwise.min.denominator
259 / (float)fie.stepwise.min.numerator );
265 float GetAbsoluteMaxFrameRate( vlc_object_t *obj, int fd,
266 uint32_t pixel_format )
268 #ifdef VIDIOC_ENUM_FRAMESIZES
269 /* This is new in Linux 2.6.19 */
270 struct v4l2_frmsizeenum fse = {
271 .pixel_format = pixel_format
274 if( v4l2_ioctl( fd, VIDIOC_ENUM_FRAMESIZES, &fse ) < 0 )
280 case V4L2_FRMSIZE_TYPE_DISCRETE:
283 float fps = GetMaxFPS( obj, fd, pixel_format,
284 fse.discrete.width, fse.discrete.height );
288 } while( v4l2_ioctl( fd, VIDIOC_ENUM_FRAMESIZES, &fse ) >= 0 );
291 case V4L2_FRMSIZE_TYPE_STEPWISE:
292 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
293 msg_Dbg( obj, " sizes from %"PRIu32"x%"PRIu32" "
294 "to %"PRIu32"x%"PRIu32" supported",
295 fse.stepwise.min_width, fse.stepwise.min_height,
296 fse.stepwise.max_width, fse.stepwise.max_height );
297 if( fse.type == V4L2_FRMSIZE_TYPE_STEPWISE )
298 msg_Dbg( obj, " with %"PRIu32"x%"PRIu32" steps",
299 fse.stepwise.step_width, fse.stepwise.step_height );
301 for( uint32_t width = fse.stepwise.min_width;
302 width <= fse.stepwise.max_width;
303 width += fse.stepwise.step_width )
304 for( uint32_t height = fse.stepwise.min_height;
305 height <= fse.stepwise.max_width;
306 height += fse.stepwise.step_height )
308 float fps = GetMaxFPS( obj, fd, pixel_format, width, height );
320 void GetMaxDimensions( vlc_object_t *obj, int fd, uint32_t pixel_format,
321 float fps_min, uint32_t *pwidth, uint32_t *pheight )
326 #ifdef VIDIOC_ENUM_FRAMESIZES
327 /* This is new in Linux 2.6.19 */
328 struct v4l2_frmsizeenum fse = {
329 .pixel_format = pixel_format
332 if( v4l2_ioctl( fd, VIDIOC_ENUM_FRAMESIZES, &fse ) < 0 )
337 case V4L2_FRMSIZE_TYPE_DISCRETE:
340 msg_Dbg( obj, " discrete size %"PRIu32"x%"PRIu32" supported",
341 fse.discrete.width, fse.discrete.height );
343 float fps = GetMaxFPS( obj, fd, pixel_format,
344 fse.discrete.width, fse.discrete.height );
345 if( fps >= fps_min && fse.discrete.width > *pwidth )
347 *pwidth = fse.discrete.width;
348 *pheight = fse.discrete.height;
352 while( v4l2_ioctl( fd, VIDIOC_ENUM_FRAMESIZES, &fse ) >= 0 );
355 case V4L2_FRMSIZE_TYPE_STEPWISE:
356 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
357 msg_Dbg( obj, " sizes from %"PRIu32"x%"PRIu32" "
358 "to %"PRIu32"x%"PRIu32" supported",
359 fse.stepwise.min_width, fse.stepwise.min_height,
360 fse.stepwise.max_width, fse.stepwise.max_height );
361 if( fse.type == V4L2_FRMSIZE_TYPE_STEPWISE )
362 msg_Dbg( obj, " with %"PRIu32"x%"PRIu32" steps",
363 fse.stepwise.step_width, fse.stepwise.step_height );
365 for( uint32_t width = fse.stepwise.min_width;
366 width <= fse.stepwise.max_width;
367 width += fse.stepwise.step_width )
368 for( uint32_t height = fse.stepwise.min_height;
369 height <= fse.stepwise.max_width;
370 height += fse.stepwise.step_height )
372 float fps = GetMaxFPS( obj, fd, pixel_format, width, height );
373 if( fps >= fps_min && width > *pwidth )