h264_metadata: Add option to delete filler data
[libav.git] / libavdevice / v4l2.c
CommitLineData
0a7b514f
LA
1/*
2 * Video4Linux2 grab interface
406792e7
DB
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2006 Luca Abeni
0a7b514f
LA
5 *
6 * Part of this file is based on the V4L2 video capture example
7 * (http://v4l2spec.bytesex.org/v4l2spec/capture.c)
8 *
9 * Thanks to Michael Niedermayer for providing the mapping between
716d413c 10 * V4L2_PIX_FMT_* and AV_PIX_FMT_*
0a7b514f
LA
11 *
12 *
2912e87a 13 * This file is part of Libav.
b78e7197 14 *
2912e87a 15 * Libav is free software; you can redistribute it and/or
0a7b514f
LA
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
b78e7197 18 * version 2.1 of the License, or (at your option) any later version.
0a7b514f 19 *
2912e87a 20 * Libav is distributed in the hope that it will be useful,
0a7b514f
LA
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
24 *
25 * You should have received a copy of the GNU Lesser General Public
2912e87a 26 * License along with Libav; if not, write to the Free Software
0a7b514f
LA
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 */
b0067549 29
e9d4b8a5 30#undef __STRICT_ANSI__ //workaround due to broken kernel headers
b0067549 31#include "config.h"
245976da 32#include "libavformat/avformat.h"
c3f9ebf7 33#include "libavformat/internal.h"
0a7b514f
LA
34#include <unistd.h>
35#include <fcntl.h>
3a165c18 36#include <stdatomic.h>
0a7b514f
LA
37#include <sys/ioctl.h>
38#include <sys/mman.h>
39#include <sys/time.h>
0efd48df 40#include <poll.h>
b250f9c6 41#if HAVE_SYS_VIDEOIO_H
1b6e8b73
B
42#include <sys/videoio.h>
43#else
f743a062 44#include <linux/videodev2.h>
1b6e8b73 45#endif
1afddbe5 46#include "libavutil/avassert.h"
737eb597 47#include "libavutil/imgutils.h"
7950e519 48#include "libavutil/internal.h"
b3da2692
AK
49#include "libavutil/log.h"
50#include "libavutil/opt.h"
8fe7b644 51#include "libavutil/parseutils.h"
d576bbf3 52#include "libavutil/pixdesc.h"
bb3244de 53#include "libavutil/avstring.h"
0efd48df 54#include "libavutil/mathematics.h"
0a7b514f
LA
55
56static const int desired_video_buffers = 256;
57
a6a4793d
LB
58#define V4L_ALLFORMATS 3
59#define V4L_RAWFORMATS 1
60#define V4L_COMPFORMATS 2
61
0a7b514f 62struct video_data {
b3da2692 63 AVClass *class;
0a7b514f
LA
64 int fd;
65 int frame_format; /* V4L2_PIX_FMT_* */
0a7b514f 66 int width, height;
0a7b514f 67 int frame_size;
0efd48df 68 int timeout;
af7123b2 69 int interlaced;
0a7b514f
LA
70 int top_field_first;
71
72 int buffers;
3a165c18 73 atomic_int buffers_queued;
0a7b514f
LA
74 void **buf_start;
75 unsigned int *buf_len;
b3da2692 76 char *standard;
a02fd06a 77 int channel;
a6a4793d
LB
78 char *video_size; /**< String describing video size,
79 set by a private option. */
d576bbf3 80 char *pixel_format; /**< Set by a private option. */
a6a4793d 81 int list_format; /**< Set by a private option. */
c21324ee 82 char *framerate; /**< Set by a private option. */
0a7b514f
LA
83};
84
41536a60 85struct buff_data {
1afddbe5 86 struct video_data *s;
41536a60
LA
87 int index;
88 int fd;
89};
90
0a7b514f 91struct fmt_map {
716d413c 92 enum AVPixelFormat ff_fmt;
36ef5369 93 enum AVCodecID codec_id;
9202218e 94 uint32_t v4l2_fmt;
0a7b514f
LA
95};
96
97static struct fmt_map fmt_conversion_table[] = {
01058893 98 //ff_fmt codec_id v4l2_fmt
716d413c
AK
99 { AV_PIX_FMT_YUV420P, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUV420 },
100 { AV_PIX_FMT_YUV422P, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUV422P },
101 { AV_PIX_FMT_YUYV422, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUYV },
102 { AV_PIX_FMT_UYVY422, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_UYVY },
103 { AV_PIX_FMT_YUV411P, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUV411P },
104 { AV_PIX_FMT_YUV410P, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUV410 },
105 { AV_PIX_FMT_RGB555, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_RGB555 },
106 { AV_PIX_FMT_RGB565, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_RGB565 },
107 { AV_PIX_FMT_BGR24, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_BGR24 },
108 { AV_PIX_FMT_RGB24, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_RGB24 },
109 { AV_PIX_FMT_BGRA, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_BGR32 },
110 { AV_PIX_FMT_GRAY8, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_GREY },
111 { AV_PIX_FMT_NV12, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_NV12 },
112 { AV_PIX_FMT_NONE, AV_CODEC_ID_MJPEG, V4L2_PIX_FMT_MJPEG },
113 { AV_PIX_FMT_NONE, AV_CODEC_ID_MJPEG, V4L2_PIX_FMT_JPEG },
9a26ba97
LB
114#ifdef V4L2_PIX_FMT_H264
115 { AV_PIX_FMT_NONE, AV_CODEC_ID_H264, V4L2_PIX_FMT_H264 },
116#endif
0a7b514f
LA
117};
118
eb89b4fc 119static int device_open(AVFormatContext *ctx)
0a7b514f
LA
120{
121 struct v4l2_capability cap;
122 int fd;
50f85218 123 int res, err;
653387d8 124 int flags = O_RDWR;
a1a25988 125 char errbuf[128];
0a7b514f 126
653387d8
LA
127 if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
128 flags |= O_NONBLOCK;
129 }
a896d7f4 130
71bf6b41 131 fd = avpriv_open(ctx->filename, flags);
0a7b514f 132 if (fd < 0) {
a1a25988
TM
133 err = AVERROR(errno);
134 av_strerror(err, errbuf, sizeof(errbuf));
eb89b4fc 135
c7238c72 136 av_log(ctx, AV_LOG_ERROR, "Cannot open video device %s : %s\n",
a1a25988 137 ctx->filename, errbuf);
0a7b514f 138
a1a25988 139 return err;
0a7b514f
LA
140 }
141
142 res = ioctl(fd, VIDIOC_QUERYCAP, &cap);
143 if (res < 0) {
09f25533
LB
144 err = AVERROR(errno);
145 av_strerror(err, errbuf, sizeof(errbuf));
c7238c72 146 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n",
a1a25988 147 errbuf);
0a7b514f 148
eb89b4fc 149 goto fail;
0a7b514f 150 }
a896d7f4 151
eb89b4fc
LB
152 av_log(ctx, AV_LOG_VERBOSE, "[%d]Capabilities: %x\n",
153 fd, cap.capabilities);
154
155 if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
156 av_log(ctx, AV_LOG_ERROR, "Not a video capture device.\n");
09f25533 157 err = AVERROR(ENODEV);
0a7b514f 158
eb89b4fc 159 goto fail;
0a7b514f 160 }
a896d7f4 161
eb89b4fc
LB
162 if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
163 av_log(ctx, AV_LOG_ERROR,
164 "The device does not support the streaming I/O method.\n");
09f25533 165 err = AVERROR(ENOSYS);
eb89b4fc
LB
166
167 goto fail;
168 }
0a7b514f
LA
169
170 return fd;
eb89b4fc
LB
171
172fail:
173 close(fd);
09f25533 174 return err;
0a7b514f
LA
175}
176
a896d7f4
LB
177static int device_init(AVFormatContext *ctx, int *width, int *height,
178 uint32_t pix_fmt)
0a7b514f 179{
c7238c72
LA
180 struct video_data *s = ctx->priv_data;
181 int fd = s->fd;
b6db3859 182 struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
a896d7f4
LB
183 struct v4l2_pix_format *pix = &fmt.fmt.pix;
184
24300af4 185 int res;
0a7b514f 186
a896d7f4
LB
187 pix->width = *width;
188 pix->height = *height;
189 pix->pixelformat = pix_fmt;
190 pix->field = V4L2_FIELD_ANY;
191
24300af4 192 res = ioctl(fd, VIDIOC_S_FMT, &fmt);
a896d7f4 193
24300af4 194 if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
a896d7f4
LB
195 av_log(ctx, AV_LOG_INFO,
196 "The V4L2 driver changed the video from %dx%d to %dx%d\n",
197 *width, *height, fmt.fmt.pix.width, fmt.fmt.pix.height);
24300af4
LA
198 *width = fmt.fmt.pix.width;
199 *height = fmt.fmt.pix.height;
200 }
201
3d0d9a5e 202 if (pix_fmt != fmt.fmt.pix.pixelformat) {
a896d7f4
LB
203 av_log(ctx, AV_LOG_DEBUG,
204 "The V4L2 driver changed the pixel format "
205 "from 0x%08X to 0x%08X\n",
206 pix_fmt, fmt.fmt.pix.pixelformat);
3d0d9a5e
JR
207 res = -1;
208 }
209
af7123b2
LB
210 if (fmt.fmt.pix.field == V4L2_FIELD_INTERLACED) {
211 av_log(ctx, AV_LOG_DEBUG, "The V4L2 driver using the interlaced mode");
212 s->interlaced = 1;
213 }
214
24300af4 215 return res;
0a7b514f
LA
216}
217
218static int first_field(int fd)
219{
220 int res;
221 v4l2_std_id std;
222
223 res = ioctl(fd, VIDIOC_G_STD, &std);
224 if (res < 0) {
225 return 0;
226 }
227 if (std & V4L2_STD_NTSC) {
228 return 0;
229 }
230
231 return 1;
232}
233
716d413c 234static uint32_t fmt_ff2v4l(enum AVPixelFormat pix_fmt, enum AVCodecID codec_id)
0a7b514f
LA
235{
236 int i;
237
37d3e066 238 for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
36ef5369 239 if ((codec_id == AV_CODEC_ID_NONE ||
3db77ccf 240 fmt_conversion_table[i].codec_id == codec_id) &&
716d413c 241 (pix_fmt == AV_PIX_FMT_NONE ||
3db77ccf 242 fmt_conversion_table[i].ff_fmt == pix_fmt)) {
0a7b514f
LA
243 return fmt_conversion_table[i].v4l2_fmt;
244 }
245 }
246
247 return 0;
248}
249
716d413c 250static enum AVPixelFormat fmt_v4l2ff(uint32_t v4l2_fmt, enum AVCodecID codec_id)
0a7b514f
LA
251{
252 int i;
253
37d3e066 254 for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
3db77ccf
LA
255 if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt &&
256 fmt_conversion_table[i].codec_id == codec_id) {
0a7b514f
LA
257 return fmt_conversion_table[i].ff_fmt;
258 }
259 }
260
716d413c 261 return AV_PIX_FMT_NONE;
0a7b514f
LA
262}
263
36ef5369 264static enum AVCodecID fmt_v4l2codec(uint32_t v4l2_fmt)
3db77ccf
LA
265{
266 int i;
267
268 for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
269 if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt) {
270 return fmt_conversion_table[i].codec_id;
271 }
272 }
273
36ef5369 274 return AV_CODEC_ID_NONE;
3db77ccf
LA
275}
276
a6a4793d
LB
277#if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
278static void list_framesizes(AVFormatContext *ctx, int fd, uint32_t pixelformat)
279{
280 struct v4l2_frmsizeenum vfse = { .pixel_format = pixelformat };
281
282 while(!ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &vfse)) {
283 switch (vfse.type) {
284 case V4L2_FRMSIZE_TYPE_DISCRETE:
285 av_log(ctx, AV_LOG_INFO, " %ux%u",
286 vfse.discrete.width, vfse.discrete.height);
287 break;
288 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
289 case V4L2_FRMSIZE_TYPE_STEPWISE:
290 av_log(ctx, AV_LOG_INFO, " {%u-%u, %u}x{%u-%u, %u}",
291 vfse.stepwise.min_width,
292 vfse.stepwise.max_width,
293 vfse.stepwise.step_width,
294 vfse.stepwise.min_height,
295 vfse.stepwise.max_height,
296 vfse.stepwise.step_height);
297 }
298 vfse.index++;
299 }
300}
301#endif
302
303static void list_formats(AVFormatContext *ctx, int fd, int type)
304{
305 struct v4l2_fmtdesc vfd = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
306
307 while(!ioctl(fd, VIDIOC_ENUM_FMT, &vfd)) {
36ef5369 308 enum AVCodecID codec_id = fmt_v4l2codec(vfd.pixelformat);
716d413c 309 enum AVPixelFormat pix_fmt = fmt_v4l2ff(vfd.pixelformat, codec_id);
a6a4793d
LB
310
311 vfd.index++;
312
313 if (!(vfd.flags & V4L2_FMT_FLAG_COMPRESSED) &&
314 type & V4L_RAWFORMATS) {
315 const char *fmt_name = av_get_pix_fmt_name(pix_fmt);
316 av_log(ctx, AV_LOG_INFO, "R : %9s : %20s :",
317 fmt_name ? fmt_name : "Unsupported",
318 vfd.description);
319 } else if (vfd.flags & V4L2_FMT_FLAG_COMPRESSED &&
320 type & V4L_COMPFORMATS) {
619d5e7d 321 const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
a6a4793d 322 av_log(ctx, AV_LOG_INFO, "C : %9s : %20s :",
619d5e7d 323 desc ? desc->name : "Unsupported",
a6a4793d
LB
324 vfd.description);
325 } else {
326 continue;
327 }
328
f13a9ca9 329#ifdef V4L2_FMT_FLAG_EMULATED
a6a4793d
LB
330 if (vfd.flags & V4L2_FMT_FLAG_EMULATED) {
331 av_log(ctx, AV_LOG_WARNING, "%s", "Emulated");
332 continue;
333 }
f13a9ca9 334#endif
a6a4793d
LB
335#if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
336 list_framesizes(ctx, fd, vfd.pixelformat);
337#endif
338 av_log(ctx, AV_LOG_INFO, "\n");
339 }
340}
341
c7238c72 342static int mmap_init(AVFormatContext *ctx)
0a7b514f 343{
0a7b514f 344 int i, res;
b6db3859
LB
345 struct video_data *s = ctx->priv_data;
346 struct v4l2_requestbuffers req = {
347 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
348 .count = desired_video_buffers,
349 .memory = V4L2_MEMORY_MMAP
350 };
0a7b514f 351
680861ca 352 res = ioctl(s->fd, VIDIOC_REQBUFS, &req);
0a7b514f 353 if (res < 0) {
b776113e
MS
354 res = AVERROR(errno);
355 if (res == AVERROR(EINVAL)) {
c7238c72 356 av_log(ctx, AV_LOG_ERROR, "Device does not support mmap\n");
0a7b514f 357 } else {
c7238c72 358 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_REQBUFS)\n");
0a7b514f
LA
359 }
360
b776113e 361 return res;
0a7b514f
LA
362 }
363
364 if (req.count < 2) {
c7238c72 365 av_log(ctx, AV_LOG_ERROR, "Insufficient buffer memory\n");
0a7b514f 366
c57a8fef 367 return AVERROR(ENOMEM);
0a7b514f
LA
368 }
369 s->buffers = req.count;
370 s->buf_start = av_malloc(sizeof(void *) * s->buffers);
f929ab05 371 if (!s->buf_start) {
c7238c72 372 av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer pointers\n");
0a7b514f 373
c57a8fef 374 return AVERROR(ENOMEM);
0a7b514f
LA
375 }
376 s->buf_len = av_malloc(sizeof(unsigned int) * s->buffers);
f929ab05 377 if (!s->buf_len) {
c7238c72 378 av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer sizes\n");
0a7b514f
LA
379 av_free(s->buf_start);
380
c57a8fef 381 return AVERROR(ENOMEM);
0a7b514f
LA
382 }
383
384 for (i = 0; i < req.count; i++) {
b6db3859
LB
385 struct v4l2_buffer buf = {
386 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
387 .index = i,
388 .memory = V4L2_MEMORY_MMAP
389 };
0a7b514f 390
680861ca 391 res = ioctl(s->fd, VIDIOC_QUERYBUF, &buf);
0a7b514f 392 if (res < 0) {
09f25533 393 res = AVERROR(errno);
c7238c72 394 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYBUF)\n");
0a7b514f 395
09f25533 396 return res;
0a7b514f
LA
397 }
398
399 s->buf_len[i] = buf.length;
3db77ccf 400 if (s->frame_size > 0 && s->buf_len[i] < s->frame_size) {
a896d7f4
LB
401 av_log(ctx, AV_LOG_ERROR,
402 "Buffer len [%d] = %d != %d\n",
403 i, s->buf_len[i], s->frame_size);
0a7b514f
LA
404
405 return -1;
406 }
a896d7f4
LB
407 s->buf_start[i] = mmap(NULL, buf.length,
408 PROT_READ | PROT_WRITE, MAP_SHARED,
409 s->fd, buf.m.offset);
410
0a7b514f 411 if (s->buf_start[i] == MAP_FAILED) {
a1a25988 412 char errbuf[128];
09f25533
LB
413 res = AVERROR(errno);
414 av_strerror(res, errbuf, sizeof(errbuf));
a1a25988 415 av_log(ctx, AV_LOG_ERROR, "mmap: %s\n", errbuf);
0a7b514f 416
09f25533 417 return res;
0a7b514f
LA
418 }
419 }
420
421 return 0;
422}
423
1afddbe5 424static void mmap_release_buffer(void *opaque, uint8_t *data)
41536a60 425{
b6db3859 426 struct v4l2_buffer buf = { 0 };
41536a60 427 int res, fd;
1afddbe5
AK
428 struct buff_data *buf_descriptor = opaque;
429 struct video_data *s = buf_descriptor->s;
a1a25988 430 char errbuf[128];
5449a787 431
41536a60
LA
432 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
433 buf.memory = V4L2_MEMORY_MMAP;
434 buf.index = buf_descriptor->index;
435 fd = buf_descriptor->fd;
436 av_free(buf_descriptor);
437
680861ca 438 res = ioctl(fd, VIDIOC_QBUF, &buf);
a1a25988
TM
439 if (res < 0) {
440 av_strerror(AVERROR(errno), errbuf, sizeof(errbuf));
a896d7f4 441 av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n",
a1a25988
TM
442 errbuf);
443 }
3a165c18 444 atomic_fetch_add(&s->buffers_queued, 1);
41536a60
LA
445}
446
447static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt)
0a7b514f 448{
c7238c72 449 struct video_data *s = ctx->priv_data;
b6db3859
LB
450 struct v4l2_buffer buf = {
451 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
452 .memory = V4L2_MEMORY_MMAP
453 };
0efd48df 454 struct pollfd p = { .fd = s->fd, .events = POLLIN };
0a7b514f
LA
455 int res;
456
0efd48df
LB
457 res = poll(&p, 1, s->timeout);
458 if (res < 0)
459 return AVERROR(errno);
460
461 if (!(p.revents & (POLLIN | POLLERR | POLLHUP)))
462 return AVERROR(EAGAIN);
463
0a7b514f 464 /* FIXME: Some special treatment might be needed in case of loss of signal... */
653387d8 465 while ((res = ioctl(s->fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
0a7b514f 466 if (res < 0) {
a1a25988 467 char errbuf[128];
653387d8
LA
468 if (errno == EAGAIN) {
469 pkt->size = 0;
470
471 return AVERROR(EAGAIN);
472 }
09f25533
LB
473 res = AVERROR(errno);
474 av_strerror(res, errbuf, sizeof(errbuf));
a896d7f4 475 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n",
a1a25988 476 errbuf);
0a7b514f 477
09f25533 478 return res;
0a7b514f 479 }
49dc82ee
AK
480
481 if (buf.index >= s->buffers) {
482 av_log(ctx, AV_LOG_ERROR, "Invalid buffer index received.\n");
483 return AVERROR(EINVAL);
484 }
3a165c18 485 atomic_fetch_add(&s->buffers_queued, -1);
1afddbe5 486 // always keep at least one buffer queued
3a165c18 487 av_assert0(atomic_load(&s->buffers_queued) >= 1);
49dc82ee 488
3db77ccf 489 if (s->frame_size > 0 && buf.bytesused != s->frame_size) {
a896d7f4
LB
490 av_log(ctx, AV_LOG_ERROR,
491 "The v4l2 frame is %d bytes, but %d bytes are expected\n",
492 buf.bytesused, s->frame_size);
7c7e7464 493
c57a8fef 494 return AVERROR_INVALIDDATA;
7c7e7464
LA
495 }
496
0a7b514f 497 /* Image is at s->buff_start[buf.index] */
3a165c18 498 if (atomic_load(&s->buffers_queued) == FFMAX(s->buffers / 8, 1)) {
a5f88736 499 /* when we start getting low on queued buffers, fall back on copying data */
1afddbe5
AK
500 res = av_new_packet(pkt, buf.bytesused);
501 if (res < 0) {
502 av_log(ctx, AV_LOG_ERROR, "Error allocating a packet.\n");
503 return res;
504 }
505 memcpy(pkt->data, s->buf_start[buf.index], buf.bytesused);
506
680861ca 507 res = ioctl(s->fd, VIDIOC_QBUF, &buf);
1afddbe5 508 if (res < 0) {
09f25533 509 res = AVERROR(errno);
1afddbe5 510 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF)\n");
ce70f28a 511 av_packet_unref(pkt);
09f25533 512 return res;
1afddbe5 513 }
3a165c18 514 atomic_fetch_add(&s->buffers_queued, 1);
1afddbe5
AK
515 } else {
516 struct buff_data *buf_descriptor;
0a7b514f 517
1afddbe5
AK
518 pkt->data = s->buf_start[buf.index];
519 pkt->size = buf.bytesused;
1afddbe5
AK
520
521 buf_descriptor = av_malloc(sizeof(struct buff_data));
f929ab05 522 if (!buf_descriptor) {
1afddbe5
AK
523 /* Something went wrong... Since av_malloc() failed, we cannot even
524 * allocate a buffer for memcpying into it
525 */
526 av_log(ctx, AV_LOG_ERROR, "Failed to allocate a buffer descriptor\n");
527 res = ioctl(s->fd, VIDIOC_QBUF, &buf);
528
529 return AVERROR(ENOMEM);
530 }
531 buf_descriptor->fd = s->fd;
532 buf_descriptor->index = buf.index;
533 buf_descriptor->s = s;
534
535 pkt->buf = av_buffer_create(pkt->data, pkt->size, mmap_release_buffer,
536 buf_descriptor, 0);
537 if (!pkt->buf) {
538 av_freep(&buf_descriptor);
539 return AVERROR(ENOMEM);
540 }
0a7b514f 541 }
1afddbe5 542 pkt->pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
0a7b514f
LA
543
544 return s->buf_len[buf.index];
545}
546
c7238c72 547static int mmap_start(AVFormatContext *ctx)
0a7b514f 548{
c7238c72 549 struct video_data *s = ctx->priv_data;
0a7b514f 550 enum v4l2_buf_type type;
a1a25988
TM
551 int i, res, err;
552 char errbuf[128];
0a7b514f
LA
553
554 for (i = 0; i < s->buffers; i++) {
b6db3859
LB
555 struct v4l2_buffer buf = {
556 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
557 .index = i,
558 .memory = V4L2_MEMORY_MMAP
559 };
0a7b514f 560
680861ca 561 res = ioctl(s->fd, VIDIOC_QBUF, &buf);
0a7b514f 562 if (res < 0) {
a1a25988
TM
563 err = AVERROR(errno);
564 av_strerror(err, errbuf, sizeof(errbuf));
a896d7f4 565 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n",
a1a25988 566 errbuf);
0a7b514f 567
a1a25988 568 return err;
0a7b514f
LA
569 }
570 }
3a165c18 571 atomic_store(&s->buffers_queued, s->buffers);
0a7b514f
LA
572
573 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
680861ca 574 res = ioctl(s->fd, VIDIOC_STREAMON, &type);
0a7b514f 575 if (res < 0) {
a1a25988
TM
576 err = AVERROR(errno);
577 av_strerror(err, errbuf, sizeof(errbuf));
a896d7f4 578 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n",
a1a25988 579 errbuf);
0a7b514f 580
a1a25988 581 return err;
0a7b514f
LA
582 }
583
584 return 0;
585}
586
587static void mmap_close(struct video_data *s)
588{
589 enum v4l2_buf_type type;
590 int i;
591
592 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
593 /* We do not check for the result, because we could
594 * not do anything about it anyway...
595 */
596 ioctl(s->fd, VIDIOC_STREAMOFF, &type);
597 for (i = 0; i < s->buffers; i++) {
598 munmap(s->buf_start[i], s->buf_len[i]);
599 }
600 av_free(s->buf_start);
601 av_free(s->buf_len);
602}
603
6e9651d1 604static int v4l2_set_parameters(AVFormatContext *s1)
f5ad81f5
LW
605{
606 struct video_data *s = s1->priv_data;
b6db3859
LB
607 struct v4l2_input input = { 0 };
608 struct v4l2_standard standard = { 0 };
5d3d238f
SS
609 struct v4l2_streamparm streamparm = { 0 };
610 struct v4l2_fract *tpf = &streamparm.parm.capture.timeperframe;
b6db3859 611 AVRational framerate_q = { 0 };
c21324ee 612 int i, ret;
f5ad81f5 613
5d3d238f
SS
614 streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
615
a896d7f4
LB
616 if (s->framerate &&
617 (ret = av_parse_video_rate(&framerate_q, s->framerate)) < 0) {
618 av_log(s1, AV_LOG_ERROR, "Could not parse framerate '%s'.\n",
619 s->framerate);
c21324ee
AK
620 return ret;
621 }
a02fd06a 622
3d2a4186 623 /* set tv video input */
3d2a4186
AK
624 input.index = s->channel;
625 if (ioctl(s->fd, VIDIOC_ENUMINPUT, &input) < 0) {
626 av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl enum input failed:\n");
627 return AVERROR(EIO);
628 }
f5ad81f5 629
3d2a4186
AK
630 av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set input_id: %d, input: %s\n",
631 s->channel, input.name);
632 if (ioctl(s->fd, VIDIOC_S_INPUT, &input.index) < 0) {
a896d7f4
LB
633 av_log(s1, AV_LOG_ERROR,
634 "The V4L2 driver ioctl set input(%d) failed\n",
3d2a4186
AK
635 s->channel);
636 return AVERROR(EIO);
637 }
f5ad81f5 638
b3da2692 639 if (s->standard) {
e4dd03f3 640 av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s\n",
b3da2692 641 s->standard);
e4dd03f3 642 /* set tv standard */
e4dd03f3
LA
643 for(i=0;;i++) {
644 standard.index = i;
645 if (ioctl(s->fd, VIDIOC_ENUMSTD, &standard) < 0) {
a896d7f4
LB
646 av_log(s1, AV_LOG_ERROR,
647 "The V4L2 driver ioctl set standard(%s) failed\n",
b3da2692 648 s->standard);
6f3e0b21 649 return AVERROR(EIO);
e4dd03f3
LA
650 }
651
bb3244de 652 if (!av_strcasecmp(standard.name, s->standard)) {
e4dd03f3
LA
653 break;
654 }
655 }
656
a896d7f4
LB
657 av_log(s1, AV_LOG_DEBUG,
658 "The V4L2 driver set standard: %s, id: %"PRIu64"\n",
b3da2692 659 s->standard, (uint64_t)standard.id);
e4dd03f3 660 if (ioctl(s->fd, VIDIOC_S_STD, &standard.id) < 0) {
a896d7f4
LB
661 av_log(s1, AV_LOG_ERROR,
662 "The V4L2 driver ioctl set standard(%s) failed\n",
b3da2692 663 s->standard);
6f3e0b21 664 return AVERROR(EIO);
f5ad81f5 665 }
0d6c0732 666 }
f5ad81f5 667
e60068ba 668 if (framerate_q.num && framerate_q.den) {
70f77361 669 av_log(s1, AV_LOG_DEBUG, "Setting time per frame to %d/%d\n",
e60068ba
SS
670 framerate_q.den, framerate_q.num);
671 tpf->numerator = framerate_q.den;
672 tpf->denominator = framerate_q.num;
a896d7f4 673
70f77361
JMG
674 if (ioctl(s->fd, VIDIOC_S_PARM, &streamparm) != 0) {
675 av_log(s1, AV_LOG_ERROR,
676 "ioctl set time per frame(%d/%d) failed\n",
e60068ba 677 framerate_q.den, framerate_q.num);
70f77361
JMG
678 return AVERROR(EIO);
679 }
680
e60068ba
SS
681 if (framerate_q.num != tpf->denominator ||
682 framerate_q.den != tpf->numerator) {
70f77361 683 av_log(s1, AV_LOG_INFO,
a896d7f4
LB
684 "The driver changed the time per frame from "
685 "%d/%d to %d/%d\n",
e60068ba 686 framerate_q.den, framerate_q.num,
70f77361 687 tpf->numerator, tpf->denominator);
70f77361 688 }
8621a37d 689 } else {
8621a37d 690 if (ioctl(s->fd, VIDIOC_G_PARM, &streamparm) != 0) {
a1a25988 691 char errbuf[128];
09f25533
LB
692 ret = AVERROR(errno);
693 av_strerror(ret, errbuf, sizeof(errbuf));
a896d7f4 694 av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_PARM): %s\n",
a1a25988 695 errbuf);
09f25533 696 return ret;
8621a37d 697 }
70f77361 698 }
838b849e
AK
699 s1->streams[0]->avg_frame_rate.num = tpf->denominator;
700 s1->streams[0]->avg_frame_rate.den = tpf->numerator;
70f77361 701
0efd48df 702 s->timeout = 100 +
838b849e 703 av_rescale_q(1, s1->streams[0]->avg_frame_rate,
0efd48df
LB
704 (AVRational){1, 1000});
705
f5ad81f5
LW
706 return 0;
707}
708
8040c3b2 709static uint32_t device_try_init(AVFormatContext *s1,
716d413c 710 enum AVPixelFormat pix_fmt,
8040c3b2
RT
711 int *width,
712 int *height,
36ef5369 713 enum AVCodecID *codec_id)
3db77ccf 714{
d576bbf3 715 uint32_t desired_format = fmt_ff2v4l(pix_fmt, s1->video_codec_id);
3db77ccf
LA
716
717 if (desired_format == 0 ||
718 device_init(s1, width, height, desired_format) < 0) {
719 int i;
720
721 desired_format = 0;
722 for (i = 0; i<FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
36ef5369 723 if (s1->video_codec_id == AV_CODEC_ID_NONE ||
90d0379f 724 fmt_conversion_table[i].codec_id == s1->video_codec_id) {
3db77ccf
LA
725 desired_format = fmt_conversion_table[i].v4l2_fmt;
726 if (device_init(s1, width, height, desired_format) >= 0) {
727 break;
728 }
729 desired_format = 0;
730 }
731 }
732 }
a896d7f4 733
3db77ccf
LA
734 if (desired_format != 0) {
735 *codec_id = fmt_v4l2codec(desired_format);
36ef5369 736 assert(*codec_id != AV_CODEC_ID_NONE);
3db77ccf
LA
737 }
738
739 return desired_format;
740}
741
6e9651d1 742static int v4l2_read_header(AVFormatContext *s1)
0a7b514f
LA
743{
744 struct video_data *s = s1->priv_data;
745 AVStream *st;
82b5aa0a 746 int res = 0;
eb89b4fc 747 uint32_t desired_format;
36ef5369 748 enum AVCodecID codec_id;
716d413c 749 enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;
0a7b514f 750
3b3bbdd3 751 st = avformat_new_stream(s1, NULL);
246da0b1
AK
752 if (!st)
753 return AVERROR(ENOMEM);
a896d7f4 754
eb89b4fc 755 s->fd = device_open(s1);
246da0b1
AK
756 if (s->fd < 0)
757 return s->fd;
eb89b4fc 758
a6a4793d
LB
759 if (s->list_format) {
760 list_formats(s1, s->fd, s->list_format);
246da0b1 761 return AVERROR_EXIT;
a6a4793d
LB
762 }
763
c3f9ebf7 764 avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
0a7b514f 765
a896d7f4
LB
766 if (s->video_size &&
767 (res = av_parse_video_size(&s->width, &s->height, s->video_size)) < 0) {
768 av_log(s1, AV_LOG_ERROR, "Could not parse video size '%s'.\n",
769 s->video_size);
246da0b1 770 return res;
8fe7b644 771 }
a6a4793d
LB
772
773 if (s->pixel_format) {
0fea8555 774 const AVCodecDescriptor *desc = avcodec_descriptor_get_by_name(s->pixel_format);
b8c310cb 775
0fea8555
AK
776 if (desc) {
777 s1->video_codec_id = desc->id;
9a26ba97
LB
778 st->need_parsing = AVSTREAM_PARSE_HEADERS;
779 }
a6a4793d
LB
780
781 pix_fmt = av_get_pix_fmt(s->pixel_format);
782
0fea8555 783 if (pix_fmt == AV_PIX_FMT_NONE && !desc) {
b8c310cb 784 av_log(s1, AV_LOG_ERROR, "No such input format: %s.\n",
a6a4793d
LB
785 s->pixel_format);
786
246da0b1 787 return AVERROR(EINVAL);
a6a4793d 788 }
d576bbf3 789 }
0a7b514f 790
932d775f
SS
791 if (!s->width && !s->height) {
792 struct v4l2_format fmt;
793
a896d7f4
LB
794 av_log(s1, AV_LOG_VERBOSE,
795 "Querying the device for the current frame size\n");
932d775f
SS
796 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
797 if (ioctl(s->fd, VIDIOC_G_FMT, &fmt) < 0) {
a1a25988 798 char errbuf[128];
09f25533
LB
799 res = AVERROR(errno);
800 av_strerror(res, errbuf, sizeof(errbuf));
a896d7f4 801 av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_FMT): %s\n",
a1a25988 802 errbuf);
09f25533 803 return res;
932d775f 804 }
a896d7f4 805
932d775f
SS
806 s->width = fmt.fmt.pix.width;
807 s->height = fmt.fmt.pix.height;
a896d7f4
LB
808 av_log(s1, AV_LOG_VERBOSE,
809 "Setting frame size to %dx%d\n", s->width, s->height);
932d775f
SS
810 }
811
a896d7f4
LB
812 desired_format = device_try_init(s1, pix_fmt, &s->width, &s->height,
813 &codec_id);
0a7b514f 814 if (desired_format == 0) {
3db77ccf 815 av_log(s1, AV_LOG_ERROR, "Cannot find a proper format for "
fefa67d5 816 "codec_id %d, pix_fmt %d.\n", s1->video_codec_id, pix_fmt);
0a7b514f 817 close(s->fd);
0a7b514f 818
246da0b1 819 return AVERROR(EIO);
0a7b514f 820 }
a896d7f4 821
82b5aa0a 822 if ((res = av_image_check_size(s->width, s->height, 0, s1) < 0))
246da0b1 823 return res;
a896d7f4 824
0a7b514f
LA
825 s->frame_format = desired_format;
826
6e9651d1 827 if ((res = v4l2_set_parameters(s1) < 0))
246da0b1 828 return res;
f5ad81f5 829
9200514a
AK
830 st->codecpar->format = fmt_v4l2ff(desired_format, codec_id);
831 s->frame_size = av_image_get_buffer_size(st->codecpar->format,
f890677d 832 s->width, s->height, 1);
a896d7f4 833
eb89b4fc
LB
834 if ((res = mmap_init(s1)) ||
835 (res = mmap_start(s1)) < 0) {
0a7b514f 836 close(s->fd);
246da0b1 837 return res;
0a7b514f 838 }
eb89b4fc 839
0a7b514f
LA
840 s->top_field_first = first_field(s->fd);
841
9200514a
AK
842 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
843 st->codecpar->codec_id = codec_id;
36ef5369 844 if (codec_id == AV_CODEC_ID_RAWVIDEO)
9200514a
AK
845 st->codecpar->codec_tag =
846 avcodec_pix_fmt_to_codec_tag(st->codecpar->format);
847 st->codecpar->width = s->width;
848 st->codecpar->height = s->height;
849 st->codecpar->bit_rate = s->frame_size * av_q2d(st->avg_frame_rate) * 8;
0a7b514f 850
246da0b1 851 return 0;
0a7b514f
LA
852}
853
854static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
855{
11de006b 856#if FF_API_CODED_FRAME && FF_API_LAVF_AVCTX
40cf1bba 857FF_DISABLE_DEPRECATION_WARNINGS
b7e64fba 858 struct video_data *s = s1->priv_data;
a896d7f4 859 AVFrame *frame = s1->streams[0]->codec->coded_frame;
40cf1bba
VG
860FF_ENABLE_DEPRECATION_WARNINGS
861#endif
0a7b514f
LA
862 int res;
863
246007d3 864 if ((res = mmap_read_frame(s1, pkt)) < 0) {
411f5c6a 865 return res;
0a7b514f
LA
866 }
867
11de006b 868#if FF_API_CODED_FRAME && FF_API_LAVF_AVCTX
40cf1bba 869FF_DISABLE_DEPRECATION_WARNINGS
a896d7f4
LB
870 if (frame && s->interlaced) {
871 frame->interlaced_frame = 1;
872 frame->top_field_first = s->top_field_first;
0a7b514f 873 }
40cf1bba
VG
874FF_ENABLE_DEPRECATION_WARNINGS
875#endif
0a7b514f 876
158aa9f2 877 return pkt->size;
0a7b514f
LA
878}
879
880static int v4l2_read_close(AVFormatContext *s1)
881{
882 struct video_data *s = s1->priv_data;
883
3a165c18 884 if (atomic_load(&s->buffers_queued) != s->buffers)
1afddbe5
AK
885 av_log(s1, AV_LOG_WARNING, "Some buffers are still owned by the caller on "
886 "close.\n");
887
246007d3 888 mmap_close(s);
0a7b514f
LA
889
890 close(s->fd);
891 return 0;
892}
893
8fe7b644
AK
894#define OFFSET(x) offsetof(struct video_data, x)
895#define DEC AV_OPT_FLAG_DECODING_PARAM
b3da2692 896static const AVOption options[] = {
21aa6ae4 897 { "standard", "TV standard, used only by analog frame grabber", OFFSET(standard), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC },
e6153f17 898 { "channel", "TV channel, used only by frame grabber", OFFSET(channel), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC },
21aa6ae4 899 { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
b8c310cb
LB
900 { "pixel_format", "Preferred pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
901 { "input_format", "Preferred pixel format (for raw video) or codec name", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
21aa6ae4 902 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
e6153f17 903 { "list_formats", "List available formats and exit", OFFSET(list_format), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC, "list_formats" },
124134e4
MS
904 { "all", "Show all available formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.i64 = V4L_ALLFORMATS }, 0, INT_MAX, DEC, "list_formats" },
905 { "raw", "Show only non-compressed formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.i64 = V4L_RAWFORMATS }, 0, INT_MAX, DEC, "list_formats" },
906 { "compressed", "Show only compressed formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.i64 = V4L_COMPFORMATS }, 0, INT_MAX, DEC, "list_formats" },
b3da2692
AK
907 { NULL },
908};
909
910static const AVClass v4l2_class = {
911 .class_name = "V4L2 indev",
912 .item_name = av_default_item_name,
913 .option = options,
914 .version = LIBAVUTIL_VERSION_INT,
915};
916
c6610a21 917AVInputFormat ff_v4l2_demuxer = {
30b4ee79
DB
918 .name = "video4linux2",
919 .long_name = NULL_IF_CONFIG_SMALL("Video4Linux2 device grab"),
920 .priv_data_size = sizeof(struct video_data),
921 .read_header = v4l2_read_header,
922 .read_packet = v4l2_read_packet,
923 .read_close = v4l2_read_close,
924 .flags = AVFMT_NOFILE,
925 .priv_class = &v4l2_class,
0a7b514f 926};