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 Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
34 #include <sys/ioctl.h>
38 #include <vlc_common.h>
39 #include <vlc_demux.h>
49 struct buffer_t *bufv;
58 vlc_v4l2_ctrl_t *controls;
61 static void *StreamThread (void *);
62 static void *ReadThread (void *);
63 static int DemuxControl( demux_t *, int, va_list );
64 static int InitVideo (demux_t *, int);
66 int DemuxOpen( vlc_object_t *obj )
68 demux_t *demux = (demux_t *)obj;
70 demux_sys_t *sys = calloc( 1, sizeof( demux_sys_t ) );
71 if( unlikely(sys == NULL) )
75 ParseMRL( obj, demux->psz_location );
77 char *path = var_InheritString (obj, CFG_PREFIX"dev");
78 if (unlikely(path == NULL))
79 goto error; /* probably OOM */
80 msg_Dbg (obj, "opening device '%s'", path);
82 int rawfd = vlc_open (path, O_RDWR);
85 msg_Err (obj, "cannot open device '%s': %m", path);
91 int fd = v4l2_fd_open (rawfd, 0);
94 msg_Warn (obj, "cannot initialize user-space library: %m");
95 /* fallback to direct kernel mode anyway */
99 if (InitVideo (demux, fd))
106 sys->controls = ControlsInit (VLC_OBJECT(demux), fd);
107 demux->pf_demux = NULL;
108 demux->pf_control = DemuxControl;
109 demux->info.i_update = 0;
110 demux->info.i_title = 0;
111 demux->info.i_seekpoint = 0;
127 /* NOTE: Currently vlc_v4l2_fmt_rank() assumes format are sorted in order of
128 * decreasing preference. */
129 static const vlc_v4l2_fmt_t v4l2_fmts[] =
131 /* Planar YUV 4:2:0 */
132 { V4L2_PIX_FMT_YUV420, VLC_CODEC_I420, 0, 0, 0 },
133 { V4L2_PIX_FMT_YVU420, VLC_CODEC_YV12, 0, 0, 0 },
134 { V4L2_PIX_FMT_YUV422P, VLC_CODEC_I422, 0, 0, 0 },
135 /* Packed YUV 4:2:2 */
136 { V4L2_PIX_FMT_YUYV, VLC_CODEC_YUYV, 0, 0, 0 },
137 { V4L2_PIX_FMT_UYVY, VLC_CODEC_UYVY, 0, 0, 0 },
138 { V4L2_PIX_FMT_YVYU, VLC_CODEC_YVYU, 0, 0, 0 },
139 { V4L2_PIX_FMT_VYUY, VLC_CODEC_VYUY, 0, 0, 0 },
141 { V4L2_PIX_FMT_YUV411P, VLC_CODEC_I411, 0, 0, 0 },
143 { V4L2_PIX_FMT_YUV410, VLC_CODEC_I410, 0, 0, 0 },
144 // { V4L2_PIX_FMT_YVU410 },
146 // { V4L2_PIX_FMT_NV24, },
147 // { V4L2_PIX_FMT_NV42, },
148 // { V4L2_PIX_FMT_NV16, VLC_CODEC_NV16, 0, 0, 0 },
149 // { V4L2_PIX_FMT_NV61, VLC_CODEC_NV61, 0, 0, 0 },
150 { V4L2_PIX_FMT_NV12, VLC_CODEC_NV12, 0, 0, 0 },
151 { V4L2_PIX_FMT_NV21, VLC_CODEC_NV21, 0, 0, 0 },
153 /* V4L2-documented but VLC-unsupported misc. YUV formats */
154 // { V4L2_PIX_FMT_Y41P },
155 // { V4L2_PIX_FMT_NV12MT, },
156 // { V4L2_PIX_FMT_M420, },
159 #ifdef WORDS_BIGENDIAN
160 { V4L2_PIX_FMT_RGB32, VLC_CODEC_RGB32, 0xFF00, 0xFF0000, 0xFF000000 },
161 { V4L2_PIX_FMT_BGR32, VLC_CODEC_RGB32, 0xFF000000, 0xFF0000, 0xFF00 },
162 { V4L2_PIX_FMT_RGB24, VLC_CODEC_RGB24, 0xFF0000, 0x00FF00, 0x0000FF },
163 { V4L2_PIX_FMT_BGR24, VLC_CODEC_RGB24, 0x0000FF, 0x00FF00, 0xFF0000 },
164 // { V4L2_PIX_FMT_BGR666, },
165 // { V4L2_PIX_FMT_RGB565, },
166 { V4L2_PIX_FMT_RGB565X, VLC_CODEC_RGB16, 0x001F, 0x07E0, 0xF800 },
167 // { V4L2_PIX_FMT_RGB555, },
168 { V4L2_PIX_FMT_RGB555X, VLC_CODEC_RGB15, 0x001F, 0x03E0, 0x7C00 },
169 // { V4L2_PIX_FMT_RGB444, VLC_CODEC_RGB12, 0x000F, 0xF000, 0x0F00 },
171 { V4L2_PIX_FMT_RGB32, VLC_CODEC_RGB32, 0x0000FF, 0x00FF00, 0xFF0000 },
172 { V4L2_PIX_FMT_BGR32, VLC_CODEC_RGB32, 0xFF0000, 0x00FF00, 0x0000FF },
173 { V4L2_PIX_FMT_RGB24, VLC_CODEC_RGB24, 0x0000FF, 0x00FF00, 0xFF0000 },
174 { V4L2_PIX_FMT_BGR24, VLC_CODEC_RGB24, 0xFF0000, 0x00FF00, 0x0000FF },
175 // { V4L2_PIX_FMT_BGR666, },
176 { V4L2_PIX_FMT_RGB565, VLC_CODEC_RGB16, 0x001F, 0x07E0, 0xF800 },
177 // { V4L2_PIX_FMT_RGB565X, },
178 { V4L2_PIX_FMT_RGB555, VLC_CODEC_RGB15, 0x001F, 0x03E0, 0x7C00 },
179 // { V4L2_PIX_FMT_RGB555X, },
180 // { V4L2_PIX_FMT_RGB444, VLC_CODEC_RGB12, 0x0F00, 0x00F0, 0x000F },
182 // { V4L2_PIX_FMT_RGB332, VLC_CODEC_RGB8, 0xC0, 0x38, 0x07 },
184 /* Bayer (sub-sampled RGB). Not supported. */
185 // { V4L2_PIX_FMT_SBGGR16, }
186 // { V4L2_PIX_FMT_SRGGB12, }
187 // { V4L2_PIX_FMT_SGRBG12, }
188 // { V4L2_PIX_FMT_SGBRG12, }
189 // { V4L2_PIX_FMT_SBGGR12, }
190 // { V4L2_PIX_FMT_SRGGB10, }
191 // { V4L2_PIX_FMT_SGRBG10, }
192 // { V4L2_PIX_FMT_SGBRG10, }
193 // { V4L2_PIX_FMT_SBGGR10, }
194 // { V4L2_PIX_FMT_SBGGR8, }
195 // { V4L2_PIX_FMT_SGBRG8, }
196 // { V4L2_PIX_FMT_SGRBG8, }
197 // { V4L2_PIX_FMT_SRGGB8, }
199 /* Compressed data types */
200 { V4L2_PIX_FMT_JPEG, VLC_CODEC_MJPG, 0, 0, 0 },
201 #ifdef V4L2_PIX_FMT_H264
202 { V4L2_PIX_FMT_H264, VLC_CODEC_H264, 0, 0, 0 },
203 /* FIXME: fill p_extra for avc1... */
204 // { V4L2_PIX_FMT_H264_NO_SC, VLC_FOURCC('a','v','c','1'), 0, 0, 0 }
205 { V4L2_PIX_FMT_MPEG4, VLC_CODEC_MP4V, 0, 0, 0 },
206 { V4L2_PIX_FMT_XVID, VLC_CODEC_MP4V, 0, 0, 0 },
207 { V4L2_PIX_FMT_H263, VLC_CODEC_H263, 0, 0, 0 },
208 { V4L2_PIX_FMT_MPEG2, VLC_CODEC_MPGV, 0, 0, 0 },
209 { V4L2_PIX_FMT_MPEG1, VLC_CODEC_MPGV, 0, 0, 0 },
210 { V4L2_PIX_FMT_VC1_ANNEX_G, VLC_CODEC_VC1, 0, 0, 0 },
211 { V4L2_PIX_FMT_VC1_ANNEX_L, VLC_CODEC_VC1, 0, 0, 0 },
213 //V4L2_PIX_FMT_MPEG -> use access
215 /* Reserved formats */
216 { V4L2_PIX_FMT_MJPEG, VLC_CODEC_MJPG, 0, 0, 0 },
217 //V4L2_PIX_FMT_DV -> use access
220 // { V4L2_PIX_FMT_Y16, },
221 // { V4L2_PIX_FMT_Y12, },
222 // { V4L2_PIX_FMT_Y10, },
223 // { V4L2_PIX_FMT_Y10BPACK, },
224 { V4L2_PIX_FMT_GREY, VLC_CODEC_GREY, 0, 0, 0 },
227 static const vlc_v4l2_fmt_t *vlc_from_v4l2_fourcc (uint32_t fourcc)
229 for (size_t i = 0; i < sizeof (v4l2_fmts) / sizeof (v4l2_fmts[0]); i++)
230 if (v4l2_fmts[i].v4l2 == fourcc)
231 return v4l2_fmts + i;
235 static size_t vlc_v4l2_fmt_rank (const vlc_v4l2_fmt_t *fmt)
240 ptrdiff_t d = fmt - v4l2_fmts;
242 assert (d < (ptrdiff_t)(sizeof (v4l2_fmts) / sizeof (v4l2_fmts[0])));
246 static vlc_fourcc_t var_InheritFourCC (vlc_object_t *obj, const char *varname)
248 char *str = var_InheritString (obj, varname);
252 vlc_fourcc_t fourcc = vlc_fourcc_GetCodecFromString (VIDEO_ES, str);
254 msg_Err (obj, "invalid codec %s", str);
258 #define var_InheritFourCC(o, v) var_InheritFourCC(VLC_OBJECT(o), v)
260 static void GetAR (int fd, unsigned *restrict num, unsigned *restrict den)
262 struct v4l2_cropcap cropcap = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
264 /* TODO: get CROPCAP only once (see ResetCrop()). */
265 if (v4l2_ioctl (fd, VIDIOC_CROPCAP, &cropcap) < 0)
270 *num = cropcap.pixelaspect.numerator;
271 *den = cropcap.pixelaspect.denominator;
274 static int InitVideo (demux_t *demux, int fd)
276 demux_sys_t *sys = demux->p_sys;
278 /* Get device capabilites */
279 struct v4l2_capability cap;
280 if (v4l2_ioctl (fd, VIDIOC_QUERYCAP, &cap) < 0)
282 msg_Err (demux, "cannot get device capabilities: %m");
286 msg_Dbg (demux, "device %s using driver %s (version %u.%u.%u) on %s",
287 cap.card, cap.driver, (cap.version >> 16) & 0xFF,
288 (cap.version >> 8) & 0xFF, cap.version & 0xFF, cap.bus_info);
291 #ifdef V4L2_CAP_DEVICE_CAPS
292 if (cap.capabilities & V4L2_CAP_DEVICE_CAPS)
294 msg_Dbg (demux, " with capabilities 0x%08"PRIX32" "
295 "(overall 0x%08"PRIX32")", cap.device_caps, cap.capabilities);
296 caps = cap.device_caps;
301 msg_Dbg (demux, " with unknown capabilities "
302 "(overall 0x%08"PRIX32")", cap.capabilities);
303 caps = cap.capabilities;
305 if (!(caps & V4L2_CAP_VIDEO_CAPTURE))
307 msg_Err (demux, "not a video capture device");
311 if (SetupInput (VLC_OBJECT(demux), fd))
314 /* Picture format negotiation */
315 const vlc_v4l2_fmt_t *selected = NULL;
316 vlc_fourcc_t reqfourcc = var_InheritFourCC (demux, CFG_PREFIX"chroma");
319 for (struct v4l2_fmtdesc codec = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
320 v4l2_ioctl (fd, VIDIOC_ENUM_FMT, &codec) >= 0;
322 { /* Enumerate available chromas */
323 const vlc_v4l2_fmt_t *dsc = vlc_from_v4l2_fourcc (codec.pixelformat);
325 msg_Dbg (demux, " %s %s format %4.4s (%4.4s): %s",
326 (codec.flags & V4L2_FMT_FLAG_EMULATED) ? "emulates" : "supports",
327 (codec.flags & V4L2_FMT_FLAG_COMPRESSED) ? "compressed" : "raw",
328 (char *)&codec.pixelformat,
329 (dsc != NULL) ? (const char *)&dsc->vlc : "N.A.",
333 continue; /* ignore VLC-unsupported codec */
335 if (dsc->vlc == reqfourcc)
337 msg_Dbg (demux, " matches the requested format");
339 break; /* always select the requested format if found */
342 if (codec.flags & V4L2_FMT_FLAG_EMULATED)
345 continue; /* ignore emulated format if possible */
350 if (vlc_v4l2_fmt_rank (dsc) > vlc_v4l2_fmt_rank (selected))
351 continue; /* ignore if rank is worse */
356 if (selected == NULL)
358 msg_Err (demux, "cannot negotiate supported video format");
361 msg_Dbg (demux, "selected format %4.4s (%4.4s)",
362 (const char *)&selected->v4l2, (const char *)&selected->vlc);
364 /* Find best resolution and frame rate available */
365 struct v4l2_format fmt;
366 struct v4l2_streamparm parm;
367 if (SetupFormat (demux, fd, selected->v4l2, &fmt, &parm))
370 /* Print extra info */
371 msg_Dbg (demux, "%d bytes maximum for complete image",
372 fmt.fmt.pix.sizeimage);
373 /* Check interlacing */
374 switch (fmt.fmt.pix.field)
376 case V4L2_FIELD_NONE:
377 msg_Dbg (demux, "Interlacing setting: progressive");
380 msg_Dbg (demux, "Interlacing setting: top field only");
382 case V4L2_FIELD_BOTTOM:
383 msg_Dbg (demux, "Interlacing setting: bottom field only");
385 case V4L2_FIELD_INTERLACED:
386 msg_Dbg (demux, "Interlacing setting: interleaved");
388 sys->block_flags = BLOCK_FLAG_BOTTOM_FIELD_FIRST;
390 sys->block_flags = BLOCK_FLAG_TOP_FIELD_FIRST;
392 case V4L2_FIELD_SEQ_TB:
393 msg_Dbg (demux, "Interlacing setting: sequential top bottom (TODO)");
395 case V4L2_FIELD_SEQ_BT:
396 msg_Dbg (demux, "Interlacing setting: sequential bottom top (TODO)");
398 case V4L2_FIELD_ALTERNATE:
399 msg_Dbg (demux, "Interlacing setting: alternate fields (TODO)");
400 fmt.fmt.pix.height *= 2;
402 case V4L2_FIELD_INTERLACED_TB:
403 msg_Dbg (demux, "Interlacing setting: interleaved top bottom");
404 sys->block_flags = BLOCK_FLAG_TOP_FIELD_FIRST;
406 case V4L2_FIELD_INTERLACED_BT:
407 msg_Dbg (demux, "Interlacing setting: interleaved bottom top");
408 sys->block_flags = BLOCK_FLAG_BOTTOM_FIELD_FIRST;
411 msg_Warn (demux, "Interlacing setting: unknown type (%d)",
416 /* Declare our unique elementary (video) stream */
419 es_format_Init (&es_fmt, VIDEO_ES, selected->vlc);
420 es_fmt.video.i_rmask = selected->red;
421 es_fmt.video.i_gmask = selected->green;
422 es_fmt.video.i_bmask = selected->blue;
423 es_fmt.video.i_width = fmt.fmt.pix.width;
424 es_fmt.video.i_height = fmt.fmt.pix.height;
425 es_fmt.video.i_frame_rate = parm.parm.capture.timeperframe.denominator;
426 es_fmt.video.i_frame_rate_base = parm.parm.capture.timeperframe.numerator;
427 GetAR (fd, &es_fmt.video.i_sar_num, &es_fmt.video.i_sar_den);
429 msg_Dbg (demux, "added new video ES %4.4s %ux%u", (char *)&es_fmt.i_codec,
430 es_fmt.video.i_width, es_fmt.video.i_height);
431 msg_Dbg (demux, " frame rate: %u/%u", es_fmt.video.i_frame_rate,
432 es_fmt.video.i_frame_rate_base);
433 msg_Dbg (demux, " aspect ratio: %u/%u", es_fmt.video.i_sar_num,
434 es_fmt.video.i_sar_den);
435 sys->es = es_out_Add (demux->out, &es_fmt);
437 /* Init I/O method */
438 void *(*entry) (void *);
439 if (caps & V4L2_CAP_STREAMING)
442 sys->bufv = StartMmap (VLC_OBJECT(demux), fd, &sys->bufc);
443 if (sys->bufv == NULL)
445 entry = StreamThread;
447 else if (caps & V4L2_CAP_READWRITE)
450 sys->blocksize = fmt.fmt.pix.sizeimage;
455 msg_Err (demux, "no supported I/O method");
459 if (vlc_clone (&sys->thread, entry, demux, VLC_THREAD_PRIORITY_INPUT))
464 void DemuxClose( vlc_object_t *obj )
466 demux_t *demux = (demux_t *)obj;
467 demux_sys_t *sys = demux->p_sys;
469 vlc_cancel (sys->thread);
470 vlc_join (sys->thread, NULL);
471 if (sys->bufv != NULL)
472 StopMmap (sys->fd, sys->bufv, sys->bufc);
473 ControlsDeinit( obj, sys->controls );
474 v4l2_close (sys->fd);
478 static void *StreamThread (void *data)
480 demux_t *demux = data;
481 demux_sys_t *sys = demux->p_sys;
483 struct pollfd ufd[1];
486 ufd[0].events = POLLIN | POLLPRI;
491 if (poll (ufd, 1, -1) == -1)
494 msg_Err (demux, "poll error: %m");
498 int canc = vlc_savecancel ();
499 block_t *block = GrabVideo (VLC_OBJECT(demux), fd, sys->bufv);
502 block->i_pts = block->i_dts = mdate ();
503 block->i_flags |= sys->block_flags;
504 es_out_Control (demux->out, ES_OUT_SET_PCR, block->i_pts);
505 es_out_Send (demux->out, sys->es, block);
507 vlc_restorecancel (canc);
513 static void *ReadThread (void *data)
515 demux_t *demux = data;
516 demux_sys_t *sys = demux->p_sys;
518 struct pollfd ufd[1];
521 ufd[0].events = POLLIN | POLLPRI;
526 if (poll (ufd, 1, -1) == -1)
529 msg_Err (demux, "poll error: %m");
533 block_t *block = block_Alloc (sys->blocksize);
534 if (unlikely(block == NULL))
536 msg_Err (demux, "read error: %m");
537 v4l2_read (fd, NULL, 0); /* discard frame */
540 block->i_pts = block->i_dts = mdate ();
541 block->i_flags |= sys->block_flags;
543 int canc = vlc_savecancel ();
544 ssize_t val = v4l2_read (fd, block->p_buffer, block->i_buffer);
547 block->i_buffer = val;
548 es_out_Control (demux->out, ES_OUT_SET_PCR, block->i_pts);
549 es_out_Send (demux->out, sys->es, block);
552 block_Release (block);
553 vlc_restorecancel (canc);
558 static int DemuxControl( demux_t *demux, int query, va_list args )
562 /* Special for access_demux */
563 case DEMUX_CAN_PAUSE:
565 case DEMUX_CAN_CONTROL_PACE:
566 *va_arg( args, bool * ) = false;
569 case DEMUX_GET_PTS_DELAY:
570 *va_arg(args,int64_t *) = INT64_C(1000)
571 * var_InheritInteger( demux, "live-caching" );
575 *va_arg( args, int64_t * ) = mdate();
578 /* TODO implement others */