V4L2 support for OpenBSD, patch by Brad, brad comstyle com
[libav.git] / libavdevice / v4l2.c
CommitLineData
0a7b514f
LA
1/*
2 * Video4Linux2 grab interface
3 * Copyright (c) 2000,2001 Fabrice Bellard.
4 * Copyright (c) 2006 Luca Abeni.
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
10 * V4L2_PIX_FMT_* and PIX_FMT_*
11 *
12 *
b78e7197
DB
13 * This file is part of FFmpeg.
14 *
15 * FFmpeg 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 *
b78e7197 20 * FFmpeg 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
b78e7197 26 * License along with FFmpeg; 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"
0a7b514f
LA
33#include <unistd.h>
34#include <fcntl.h>
35#include <sys/ioctl.h>
36#include <sys/mman.h>
37#include <sys/time.h>
1b6e8b73
B
38#ifdef HAVE_SYS_VIDEOIO_H
39#include <sys/videoio.h>
40#else
f743a062
LA
41#include <asm/types.h>
42#include <linux/videodev2.h>
1b6e8b73 43#endif
0a7b514f 44#include <time.h>
ea452b54 45#include <strings.h>
0a7b514f
LA
46
47static const int desired_video_buffers = 256;
48
49enum io_method {
50 io_read,
51 io_mmap,
52 io_userptr
53};
54
55struct video_data {
56 int fd;
57 int frame_format; /* V4L2_PIX_FMT_* */
58 enum io_method io_method;
59 int width, height;
60 int frame_rate;
61 int frame_rate_base;
62 int frame_size;
63 int top_field_first;
64
65 int buffers;
66 void **buf_start;
67 unsigned int *buf_len;
68};
69
41536a60
LA
70struct buff_data {
71 int index;
72 int fd;
73};
74
0a7b514f
LA
75struct fmt_map {
76 enum PixelFormat ff_fmt;
77 int32_t v4l2_fmt;
78};
79
80static struct fmt_map fmt_conversion_table[] = {
81 {
82 .ff_fmt = PIX_FMT_YUV420P,
83 .v4l2_fmt = V4L2_PIX_FMT_YUV420,
84 },
85 {
86 .ff_fmt = PIX_FMT_YUV422P,
87 .v4l2_fmt = V4L2_PIX_FMT_YUV422P,
88 },
89 {
71e445fc 90 .ff_fmt = PIX_FMT_YUYV422,
0a7b514f
LA
91 .v4l2_fmt = V4L2_PIX_FMT_YUYV,
92 },
93 {
94 .ff_fmt = PIX_FMT_UYVY422,
95 .v4l2_fmt = V4L2_PIX_FMT_UYVY,
96 },
97 {
98 .ff_fmt = PIX_FMT_YUV411P,
99 .v4l2_fmt = V4L2_PIX_FMT_YUV411P,
100 },
101 {
102 .ff_fmt = PIX_FMT_YUV410P,
103 .v4l2_fmt = V4L2_PIX_FMT_YUV410,
104 },
105 {
97a290ef
JD
106 .ff_fmt = PIX_FMT_RGB555,
107 .v4l2_fmt = V4L2_PIX_FMT_RGB555,
108 },
109 {
110 .ff_fmt = PIX_FMT_RGB565,
111 .v4l2_fmt = V4L2_PIX_FMT_RGB565,
112 },
113 {
0a7b514f
LA
114 .ff_fmt = PIX_FMT_BGR24,
115 .v4l2_fmt = V4L2_PIX_FMT_BGR24,
116 },
117 {
118 .ff_fmt = PIX_FMT_RGB24,
119 .v4l2_fmt = V4L2_PIX_FMT_RGB24,
120 },
0a7b514f 121 {
92d04e76 122 .ff_fmt = PIX_FMT_BGRA,
0a7b514f
LA
123 .v4l2_fmt = V4L2_PIX_FMT_BGR32,
124 },
0a7b514f
LA
125 {
126 .ff_fmt = PIX_FMT_GRAY8,
127 .v4l2_fmt = V4L2_PIX_FMT_GREY,
128 },
129};
130
cc58300e 131static int device_open(AVFormatContext *ctx, uint32_t *capabilities)
0a7b514f
LA
132{
133 struct v4l2_capability cap;
134 int fd;
135 int res;
653387d8 136 int flags = O_RDWR;
0a7b514f 137
653387d8
LA
138 if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
139 flags |= O_NONBLOCK;
140 }
141 fd = open(ctx->filename, flags, 0);
0a7b514f 142 if (fd < 0) {
c7238c72 143 av_log(ctx, AV_LOG_ERROR, "Cannot open video device %s : %s\n",
cc58300e 144 ctx->filename, strerror(errno));
0a7b514f
LA
145
146 return -1;
147 }
148
149 res = ioctl(fd, VIDIOC_QUERYCAP, &cap);
c16601bb
AB
150 // ENOIOCTLCMD definition only availble on __KERNEL__
151 if (res < 0 && errno == 515)
152 {
c7238c72 153 av_log(ctx, AV_LOG_ERROR, "QUERYCAP not implemented, probably V4L device but not supporting V4L2\n");
2b63c222 154 close(fd);
c16601bb
AB
155
156 return -1;
157 }
0a7b514f 158 if (res < 0) {
c7238c72 159 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n",
0a7b514f 160 strerror(errno));
2b63c222 161 close(fd);
0a7b514f
LA
162
163 return -1;
164 }
165 if ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {
c7238c72 166 av_log(ctx, AV_LOG_ERROR, "Not a video capture device\n");
2b63c222 167 close(fd);
0a7b514f
LA
168
169 return -1;
170 }
171 *capabilities = cap.capabilities;
172
173 return fd;
174}
175
c7238c72 176static int device_init(AVFormatContext *ctx, int *width, int *height, int pix_fmt)
0a7b514f 177{
c7238c72
LA
178 struct video_data *s = ctx->priv_data;
179 int fd = s->fd;
0a7b514f 180 struct v4l2_format fmt;
24300af4 181 int res;
0a7b514f
LA
182
183 memset(&fmt, 0, sizeof(struct v4l2_format));
184 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
24300af4
LA
185 fmt.fmt.pix.width = *width;
186 fmt.fmt.pix.height = *height;
0a7b514f
LA
187 fmt.fmt.pix.pixelformat = pix_fmt;
188 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
24300af4
LA
189 res = ioctl(fd, VIDIOC_S_FMT, &fmt);
190 if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
c7238c72 191 av_log(ctx, AV_LOG_INFO, "The V4L2 driver changed the video from %dx%d to %dx%d\n", *width, *height, fmt.fmt.pix.width, fmt.fmt.pix.height);
24300af4
LA
192 *width = fmt.fmt.pix.width;
193 *height = fmt.fmt.pix.height;
194 }
195
196 return res;
0a7b514f
LA
197}
198
199static int first_field(int fd)
200{
201 int res;
202 v4l2_std_id std;
203
204 res = ioctl(fd, VIDIOC_G_STD, &std);
205 if (res < 0) {
206 return 0;
207 }
208 if (std & V4L2_STD_NTSC) {
209 return 0;
210 }
211
212 return 1;
213}
214
215static uint32_t fmt_ff2v4l(enum PixelFormat pix_fmt)
216{
217 int i;
218
219 for (i = 0; i < sizeof(fmt_conversion_table) / sizeof(struct fmt_map); i++) {
220 if (fmt_conversion_table[i].ff_fmt == pix_fmt) {
221 return fmt_conversion_table[i].v4l2_fmt;
222 }
223 }
224
225 return 0;
226}
227
228static enum PixelFormat fmt_v4l2ff(uint32_t pix_fmt)
229{
230 int i;
231
232 for (i = 0; i < sizeof(fmt_conversion_table) / sizeof(struct fmt_map); i++) {
233 if (fmt_conversion_table[i].v4l2_fmt == pix_fmt) {
234 return fmt_conversion_table[i].ff_fmt;
235 }
236 }
237
eacced45 238 return PIX_FMT_NONE;
0a7b514f
LA
239}
240
c7238c72 241static int mmap_init(AVFormatContext *ctx)
0a7b514f 242{
c7238c72 243 struct video_data *s = ctx->priv_data;
0a7b514f
LA
244 struct v4l2_requestbuffers req;
245 int i, res;
246
247 memset(&req, 0, sizeof(struct v4l2_requestbuffers));
248 req.count = desired_video_buffers;
249 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
250 req.memory = V4L2_MEMORY_MMAP;
251 res = ioctl (s->fd, VIDIOC_REQBUFS, &req);
252 if (res < 0) {
253 if (errno == EINVAL) {
c7238c72 254 av_log(ctx, AV_LOG_ERROR, "Device does not support mmap\n");
0a7b514f 255 } else {
c7238c72 256 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_REQBUFS)\n");
0a7b514f
LA
257 }
258
259 return -1;
260 }
261
262 if (req.count < 2) {
c7238c72 263 av_log(ctx, AV_LOG_ERROR, "Insufficient buffer memory\n");
0a7b514f
LA
264
265 return -1;
266 }
267 s->buffers = req.count;
268 s->buf_start = av_malloc(sizeof(void *) * s->buffers);
269 if (s->buf_start == NULL) {
c7238c72 270 av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer pointers\n");
0a7b514f
LA
271
272 return -1;
273 }
274 s->buf_len = av_malloc(sizeof(unsigned int) * s->buffers);
275 if (s->buf_len == NULL) {
c7238c72 276 av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer sizes\n");
0a7b514f
LA
277 av_free(s->buf_start);
278
279 return -1;
280 }
281
282 for (i = 0; i < req.count; i++) {
283 struct v4l2_buffer buf;
284
285 memset(&buf, 0, sizeof(struct v4l2_buffer));
286 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
287 buf.memory = V4L2_MEMORY_MMAP;
288 buf.index = i;
289 res = ioctl (s->fd, VIDIOC_QUERYBUF, &buf);
290 if (res < 0) {
c7238c72 291 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYBUF)\n");
0a7b514f
LA
292
293 return -1;
294 }
295
296 s->buf_len[i] = buf.length;
297 if (s->buf_len[i] < s->frame_size) {
c7238c72 298 av_log(ctx, AV_LOG_ERROR, "Buffer len [%d] = %d != %d\n", i, s->buf_len[i], s->frame_size);
0a7b514f
LA
299
300 return -1;
301 }
302 s->buf_start[i] = mmap (NULL, buf.length,
303 PROT_READ | PROT_WRITE, MAP_SHARED, s->fd, buf.m.offset);
304 if (s->buf_start[i] == MAP_FAILED) {
c7238c72 305 av_log(ctx, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
0a7b514f
LA
306
307 return -1;
308 }
309 }
310
311 return 0;
312}
313
c7238c72 314static int read_init(AVFormatContext *ctx)
0a7b514f
LA
315{
316 return -1;
317}
318
41536a60
LA
319static void mmap_release_buffer(AVPacket *pkt)
320{
321 struct v4l2_buffer buf;
322 int res, fd;
323 struct buff_data *buf_descriptor = pkt->priv;
324
325 memset(&buf, 0, sizeof(struct v4l2_buffer));
326 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
327 buf.memory = V4L2_MEMORY_MMAP;
328 buf.index = buf_descriptor->index;
329 fd = buf_descriptor->fd;
330 av_free(buf_descriptor);
331
332 res = ioctl (fd, VIDIOC_QBUF, &buf);
333 if (res < 0) {
334 av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF)\n");
335 }
336 pkt->data = NULL;
337 pkt->size = 0;
338}
339
340static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt)
0a7b514f 341{
c7238c72 342 struct video_data *s = ctx->priv_data;
0a7b514f 343 struct v4l2_buffer buf;
41536a60 344 struct buff_data *buf_descriptor;
0a7b514f
LA
345 int res;
346
347 memset(&buf, 0, sizeof(struct v4l2_buffer));
348 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
349 buf.memory = V4L2_MEMORY_MMAP;
350
351 /* FIXME: Some special treatment might be needed in case of loss of signal... */
653387d8 352 while ((res = ioctl(s->fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
0a7b514f 353 if (res < 0) {
653387d8
LA
354 if (errno == EAGAIN) {
355 pkt->size = 0;
356
357 return AVERROR(EAGAIN);
358 }
c7238c72 359 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n", strerror(errno));
0a7b514f
LA
360
361 return -1;
362 }
363 assert (buf.index < s->buffers);
7c7e7464 364 if (buf.bytesused != s->frame_size) {
c7238c72 365 av_log(ctx, AV_LOG_ERROR, "The v4l2 frame is %d bytes, but %d bytes are expected\n", buf.bytesused, s->frame_size);
7c7e7464
LA
366
367 return -1;
368 }
369
0a7b514f 370 /* Image is at s->buff_start[buf.index] */
41536a60
LA
371 pkt->data= s->buf_start[buf.index];
372 pkt->size = buf.bytesused;
373 pkt->pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
374 pkt->destruct = mmap_release_buffer;
375 buf_descriptor = av_malloc(sizeof(struct buff_data));
376 if (buf_descriptor == NULL) {
377 /* Something went wrong... Since av_malloc() failed, we cannot even
378 * allocate a buffer for memcopying into it
379 */
380 av_log(ctx, AV_LOG_ERROR, "Failed to allocate a buffer descriptor\n");
381 res = ioctl (s->fd, VIDIOC_QBUF, &buf);
0a7b514f
LA
382
383 return -1;
384 }
41536a60
LA
385 buf_descriptor->fd = s->fd;
386 buf_descriptor->index = buf.index;
387 pkt->priv = buf_descriptor;
0a7b514f
LA
388
389 return s->buf_len[buf.index];
390}
391
41536a60 392static int read_frame(AVFormatContext *ctx, AVPacket *pkt)
0a7b514f
LA
393{
394 return -1;
395}
396
c7238c72 397static int mmap_start(AVFormatContext *ctx)
0a7b514f 398{
c7238c72 399 struct video_data *s = ctx->priv_data;
0a7b514f
LA
400 enum v4l2_buf_type type;
401 int i, res;
402
403 for (i = 0; i < s->buffers; i++) {
404 struct v4l2_buffer buf;
405
406 memset(&buf, 0, sizeof(struct v4l2_buffer));
407 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
408 buf.memory = V4L2_MEMORY_MMAP;
409 buf.index = i;
410
411 res = ioctl (s->fd, VIDIOC_QBUF, &buf);
412 if (res < 0) {
c7238c72 413 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", strerror(errno));
0a7b514f
LA
414
415 return -1;
416 }
417 }
418
419 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
420 res = ioctl (s->fd, VIDIOC_STREAMON, &type);
421 if (res < 0) {
c7238c72 422 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n", strerror(errno));
0a7b514f
LA
423
424 return -1;
425 }
426
427 return 0;
428}
429
430static void mmap_close(struct video_data *s)
431{
432 enum v4l2_buf_type type;
433 int i;
434
435 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
436 /* We do not check for the result, because we could
437 * not do anything about it anyway...
438 */
439 ioctl(s->fd, VIDIOC_STREAMOFF, &type);
440 for (i = 0; i < s->buffers; i++) {
441 munmap(s->buf_start[i], s->buf_len[i]);
442 }
443 av_free(s->buf_start);
444 av_free(s->buf_len);
445}
446
f5ad81f5
LW
447static int v4l2_set_parameters( AVFormatContext *s1, AVFormatParameters *ap )
448{
449 struct video_data *s = s1->priv_data;
450 struct v4l2_input input;
451 struct v4l2_standard standard;
452 int i;
453
0d6c0732 454 if(ap->channel>=0) {
e4dd03f3
LA
455 /* set tv video input */
456 memset (&input, 0, sizeof (input));
457 input.index = ap->channel;
458 if(ioctl (s->fd, VIDIOC_ENUMINPUT, &input) < 0) {
459 av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl enum input failed:\n");
6f3e0b21 460 return AVERROR(EIO);
e4dd03f3 461 }
f5ad81f5 462
e4dd03f3
LA
463 av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set input_id: %d, input: %s\n",
464 ap->channel, input.name);
465 if(ioctl (s->fd, VIDIOC_S_INPUT, &input.index) < 0 ) {
466 av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set input(%d) failed\n",
467 ap->channel);
6f3e0b21 468 return AVERROR(EIO);
e4dd03f3 469 }
0d6c0732 470 }
f5ad81f5 471
0d6c0732 472 if(ap->standard) {
e4dd03f3
LA
473 av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s\n",
474 ap->standard );
475 /* set tv standard */
476 memset (&standard, 0, sizeof (standard));
477 for(i=0;;i++) {
478 standard.index = i;
479 if (ioctl(s->fd, VIDIOC_ENUMSTD, &standard) < 0) {
480 av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n",
481 ap->standard);
6f3e0b21 482 return AVERROR(EIO);
e4dd03f3
LA
483 }
484
485 if(!strcasecmp(standard.name, ap->standard)) {
486 break;
487 }
488 }
489
490 av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s, id: %"PRIu64"\n",
491 ap->standard, standard.id);
492 if (ioctl(s->fd, VIDIOC_S_STD, &standard.id) < 0) {
f5ad81f5
LW
493 av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n",
494 ap->standard);
6f3e0b21 495 return AVERROR(EIO);
f5ad81f5 496 }
0d6c0732 497 }
f5ad81f5
LW
498
499 return 0;
500}
501
0a7b514f
LA
502static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
503{
504 struct video_data *s = s1->priv_data;
505 AVStream *st;
506 int width, height;
507 int res, frame_rate, frame_rate_base;
508 uint32_t desired_format, capabilities;
0a7b514f 509
99423fa0
SS
510 if (ap->width <= 0 || ap->height <= 0) {
511 av_log(s1, AV_LOG_ERROR, "Wrong size (%dx%d)\n", ap->width, ap->height);
512 return -1;
513 }
514 if (ap->time_base.den <= 0) {
515 av_log(s1, AV_LOG_ERROR, "Wrong time base (%d)\n", ap->time_base.den);
0a7b514f
LA
516 return -1;
517 }
518
519 width = ap->width;
520 height = ap->height;
521 frame_rate = ap->time_base.den;
522 frame_rate_base = ap->time_base.num;
523
524 if((unsigned)width > 32767 || (unsigned)height > 32767) {
3241ec1d 525 av_log(s1, AV_LOG_ERROR, "Wrong size (%dx%d)\n", width, height);
0a7b514f
LA
526
527 return -1;
528 }
529
530 st = av_new_stream(s1, 0);
531 if (!st) {
8fa36ae0 532 return AVERROR(ENOMEM);
0a7b514f
LA
533 }
534 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
535
536 s->width = width;
537 s->height = height;
538 s->frame_rate = frame_rate;
539 s->frame_rate_base = frame_rate_base;
540
0a7b514f 541 capabilities = 0;
cc58300e 542 s->fd = device_open(s1, &capabilities);
0a7b514f 543 if (s->fd < 0) {
6f3e0b21 544 return AVERROR(EIO);
0a7b514f 545 }
60023e32 546 av_log(s1, AV_LOG_INFO, "[%d]Capabilities: %x\n", s->fd, capabilities);
0a7b514f
LA
547
548 desired_format = fmt_ff2v4l(ap->pix_fmt);
c7238c72 549 if (desired_format == 0 || (device_init(s1, &width, &height, desired_format) < 0)) {
0a7b514f
LA
550 int i, done;
551
552 done = 0; i = 0;
553 while (!done) {
554 desired_format = fmt_conversion_table[i].v4l2_fmt;
c7238c72 555 if (device_init(s1, &width, &height, desired_format) < 0) {
0a7b514f
LA
556 desired_format = 0;
557 i++;
558 } else {
559 done = 1;
560 }
561 if (i == sizeof(fmt_conversion_table) / sizeof(struct fmt_map)) {
562 done = 1;
563 }
564 }
565 }
566 if (desired_format == 0) {
567 av_log(s1, AV_LOG_ERROR, "Cannot find a proper format.\n");
568 close(s->fd);
0a7b514f 569
6f3e0b21 570 return AVERROR(EIO);
0a7b514f
LA
571 }
572 s->frame_format = desired_format;
573
f5ad81f5 574 if( v4l2_set_parameters( s1, ap ) < 0 )
6f3e0b21 575 return AVERROR(EIO);
f5ad81f5 576
0a7b514f
LA
577 st->codec->pix_fmt = fmt_v4l2ff(desired_format);
578 s->frame_size = avpicture_get_size(st->codec->pix_fmt, width, height);
579 if (capabilities & V4L2_CAP_STREAMING) {
580 s->io_method = io_mmap;
c7238c72 581 res = mmap_init(s1);
eaddc471 582 if (res == 0) {
c7238c72 583 res = mmap_start(s1);
eaddc471 584 }
0a7b514f
LA
585 } else {
586 s->io_method = io_read;
c7238c72 587 res = read_init(s1);
0a7b514f
LA
588 }
589 if (res < 0) {
590 close(s->fd);
0a7b514f 591
6f3e0b21 592 return AVERROR(EIO);
0a7b514f
LA
593 }
594 s->top_field_first = first_field(s->fd);
595
596 st->codec->codec_type = CODEC_TYPE_VIDEO;
597 st->codec->codec_id = CODEC_ID_RAWVIDEO;
598 st->codec->width = width;
599 st->codec->height = height;
600 st->codec->time_base.den = frame_rate;
601 st->codec->time_base.num = frame_rate_base;
602 st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
603
604 return 0;
605}
606
607static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
608{
609 struct video_data *s = s1->priv_data;
610 int res;
611
0a7b514f 612 if (s->io_method == io_mmap) {
41536a60
LA
613 av_init_packet(pkt);
614 res = mmap_read_frame(s1, pkt);
0a7b514f 615 } else if (s->io_method == io_read) {
41536a60 616 if (av_new_packet(pkt, s->frame_size) < 0)
6f3e0b21 617 return AVERROR(EIO);
41536a60
LA
618
619 res = read_frame(s1, pkt);
0a7b514f 620 } else {
6f3e0b21 621 return AVERROR(EIO);
0a7b514f
LA
622 }
623 if (res < 0) {
411f5c6a 624 return res;
0a7b514f
LA
625 }
626
627 if (s1->streams[0]->codec->coded_frame) {
628 s1->streams[0]->codec->coded_frame->interlaced_frame = 1;
629 s1->streams[0]->codec->coded_frame->top_field_first = s->top_field_first;
630 }
631
632 return s->frame_size;
633}
634
635static int v4l2_read_close(AVFormatContext *s1)
636{
637 struct video_data *s = s1->priv_data;
638
639 if (s->io_method == io_mmap) {
640 mmap_close(s);
641 }
642
643 close(s->fd);
644 return 0;
645}
646
ff70e601 647AVInputFormat v4l2_demuxer = {
0a7b514f 648 "video4linux2",
bde15e74 649 NULL_IF_CONFIG_SMALL("video grab"),
0a7b514f
LA
650 sizeof(struct video_data),
651 NULL,
652 v4l2_read_header,
653 v4l2_read_packet,
654 v4l2_read_close,
655 .flags = AVFMT_NOFILE,
656};