This fixes error handling for BeOS, removing the need for some ifdefs.
[libav.git] / libavformat / v4l2.c
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 *
13 * This file is part of FFmpeg.
14 *
15 * FFmpeg is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
19 *
20 * FFmpeg is distributed in the hope that it will be useful,
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
26 * License along with FFmpeg; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 */
29 #include "avformat.h"
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <sys/ioctl.h>
33 #include <sys/mman.h>
34 #include <sys/time.h>
35 #include <asm/types.h>
36 #include <linux/videodev2.h>
37 #include <time.h>
38
39 static const int desired_video_buffers = 256;
40
41 enum io_method {
42 io_read,
43 io_mmap,
44 io_userptr
45 };
46
47 struct video_data {
48 int fd;
49 int frame_format; /* V4L2_PIX_FMT_* */
50 enum io_method io_method;
51 int width, height;
52 int frame_rate;
53 int frame_rate_base;
54 int frame_size;
55 int top_field_first;
56
57 int buffers;
58 void **buf_start;
59 unsigned int *buf_len;
60 };
61
62 struct fmt_map {
63 enum PixelFormat ff_fmt;
64 int32_t v4l2_fmt;
65 };
66
67 static struct fmt_map fmt_conversion_table[] = {
68 {
69 .ff_fmt = PIX_FMT_YUV420P,
70 .v4l2_fmt = V4L2_PIX_FMT_YUV420,
71 },
72 {
73 .ff_fmt = PIX_FMT_YUV422P,
74 .v4l2_fmt = V4L2_PIX_FMT_YUV422P,
75 },
76 {
77 .ff_fmt = PIX_FMT_YUYV422,
78 .v4l2_fmt = V4L2_PIX_FMT_YUYV,
79 },
80 {
81 .ff_fmt = PIX_FMT_UYVY422,
82 .v4l2_fmt = V4L2_PIX_FMT_UYVY,
83 },
84 {
85 .ff_fmt = PIX_FMT_YUV411P,
86 .v4l2_fmt = V4L2_PIX_FMT_YUV411P,
87 },
88 {
89 .ff_fmt = PIX_FMT_YUV410P,
90 .v4l2_fmt = V4L2_PIX_FMT_YUV410,
91 },
92 {
93 .ff_fmt = PIX_FMT_BGR24,
94 .v4l2_fmt = V4L2_PIX_FMT_BGR24,
95 },
96 {
97 .ff_fmt = PIX_FMT_RGB24,
98 .v4l2_fmt = V4L2_PIX_FMT_RGB24,
99 },
100 /*
101 {
102 .ff_fmt = PIX_FMT_RGB32,
103 .v4l2_fmt = V4L2_PIX_FMT_BGR32,
104 },
105 */
106 {
107 .ff_fmt = PIX_FMT_GRAY8,
108 .v4l2_fmt = V4L2_PIX_FMT_GREY,
109 },
110 };
111
112 static int device_open(AVFormatContext *ctx, const char *devname, uint32_t *capabilities)
113 {
114 struct v4l2_capability cap;
115 int fd;
116 int res;
117
118 fd = open(devname, O_RDWR /*| O_NONBLOCK*/, 0);
119 if (fd < 0) {
120 av_log(ctx, AV_LOG_ERROR, "Cannot open video device %s : %s\n",
121 devname, strerror(errno));
122
123 return -1;
124 }
125
126 res = ioctl(fd, VIDIOC_QUERYCAP, &cap);
127 // ENOIOCTLCMD definition only availble on __KERNEL__
128 if (res < 0 && errno == 515)
129 {
130 av_log(ctx, AV_LOG_ERROR, "QUERYCAP not implemented, probably V4L device but not supporting V4L2\n");
131 close(fd);
132
133 return -1;
134 }
135 if (res < 0) {
136 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n",
137 strerror(errno));
138 close(fd);
139
140 return -1;
141 }
142 if ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {
143 av_log(ctx, AV_LOG_ERROR, "Not a video capture device\n");
144 close(fd);
145
146 return -1;
147 }
148 *capabilities = cap.capabilities;
149
150 return fd;
151 }
152
153 static int device_init(AVFormatContext *ctx, int *width, int *height, int pix_fmt)
154 {
155 struct video_data *s = ctx->priv_data;
156 int fd = s->fd;
157 struct v4l2_format fmt;
158 int res;
159
160 memset(&fmt, 0, sizeof(struct v4l2_format));
161 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
162 fmt.fmt.pix.width = *width;
163 fmt.fmt.pix.height = *height;
164 fmt.fmt.pix.pixelformat = pix_fmt;
165 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
166 res = ioctl(fd, VIDIOC_S_FMT, &fmt);
167 if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
168 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);
169 *width = fmt.fmt.pix.width;
170 *height = fmt.fmt.pix.height;
171 }
172
173 return res;
174 }
175
176 static int first_field(int fd)
177 {
178 int res;
179 v4l2_std_id std;
180
181 res = ioctl(fd, VIDIOC_G_STD, &std);
182 if (res < 0) {
183 return 0;
184 }
185 if (std & V4L2_STD_NTSC) {
186 return 0;
187 }
188
189 return 1;
190 }
191
192 static uint32_t fmt_ff2v4l(enum PixelFormat pix_fmt)
193 {
194 int i;
195
196 for (i = 0; i < sizeof(fmt_conversion_table) / sizeof(struct fmt_map); i++) {
197 if (fmt_conversion_table[i].ff_fmt == pix_fmt) {
198 return fmt_conversion_table[i].v4l2_fmt;
199 }
200 }
201
202 return 0;
203 }
204
205 static enum PixelFormat fmt_v4l2ff(uint32_t pix_fmt)
206 {
207 int i;
208
209 for (i = 0; i < sizeof(fmt_conversion_table) / sizeof(struct fmt_map); i++) {
210 if (fmt_conversion_table[i].v4l2_fmt == pix_fmt) {
211 return fmt_conversion_table[i].ff_fmt;
212 }
213 }
214
215 return -1;
216 }
217
218 static int mmap_init(AVFormatContext *ctx)
219 {
220 struct video_data *s = ctx->priv_data;
221 struct v4l2_requestbuffers req;
222 int i, res;
223
224 memset(&req, 0, sizeof(struct v4l2_requestbuffers));
225 req.count = desired_video_buffers;
226 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
227 req.memory = V4L2_MEMORY_MMAP;
228 res = ioctl (s->fd, VIDIOC_REQBUFS, &req);
229 if (res < 0) {
230 if (errno == EINVAL) {
231 av_log(ctx, AV_LOG_ERROR, "Device does not support mmap\n");
232 } else {
233 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_REQBUFS)\n");
234 }
235
236 return -1;
237 }
238
239 if (req.count < 2) {
240 av_log(ctx, AV_LOG_ERROR, "Insufficient buffer memory\n");
241
242 return -1;
243 }
244 s->buffers = req.count;
245 s->buf_start = av_malloc(sizeof(void *) * s->buffers);
246 if (s->buf_start == NULL) {
247 av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer pointers\n");
248
249 return -1;
250 }
251 s->buf_len = av_malloc(sizeof(unsigned int) * s->buffers);
252 if (s->buf_len == NULL) {
253 av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer sizes\n");
254 av_free(s->buf_start);
255
256 return -1;
257 }
258
259 for (i = 0; i < req.count; i++) {
260 struct v4l2_buffer buf;
261
262 memset(&buf, 0, sizeof(struct v4l2_buffer));
263 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
264 buf.memory = V4L2_MEMORY_MMAP;
265 buf.index = i;
266 res = ioctl (s->fd, VIDIOC_QUERYBUF, &buf);
267 if (res < 0) {
268 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYBUF)\n");
269
270 return -1;
271 }
272
273 s->buf_len[i] = buf.length;
274 if (s->buf_len[i] < s->frame_size) {
275 av_log(ctx, AV_LOG_ERROR, "Buffer len [%d] = %d != %d\n", i, s->buf_len[i], s->frame_size);
276
277 return -1;
278 }
279 s->buf_start[i] = mmap (NULL, buf.length,
280 PROT_READ | PROT_WRITE, MAP_SHARED, s->fd, buf.m.offset);
281 if (s->buf_start[i] == MAP_FAILED) {
282 av_log(ctx, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
283
284 return -1;
285 }
286 }
287
288 return 0;
289 }
290
291 static int read_init(AVFormatContext *ctx)
292 {
293 return -1;
294 }
295
296 static int mmap_read_frame(AVFormatContext *ctx, void *frame, int64_t *ts)
297 {
298 struct video_data *s = ctx->priv_data;
299 struct v4l2_buffer buf;
300 int res;
301
302 memset(&buf, 0, sizeof(struct v4l2_buffer));
303 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
304 buf.memory = V4L2_MEMORY_MMAP;
305
306 /* FIXME: Some special treatment might be needed in case of loss of signal... */
307 while ((res = ioctl(s->fd, VIDIOC_DQBUF, &buf)) < 0 &&
308 ((errno == EAGAIN) || (errno == EINTR)));
309 if (res < 0) {
310 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n", strerror(errno));
311
312 return -1;
313 }
314 assert (buf.index < s->buffers);
315 if (buf.bytesused != s->frame_size) {
316 av_log(ctx, AV_LOG_ERROR, "The v4l2 frame is %d bytes, but %d bytes are expected\n", buf.bytesused, s->frame_size);
317
318 return -1;
319 }
320
321 /* Image is at s->buff_start[buf.index] */
322 memcpy(frame, s->buf_start[buf.index], buf.bytesused);
323 *ts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
324
325 res = ioctl (s->fd, VIDIOC_QBUF, &buf);
326 if (res < 0) {
327 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF)\n");
328
329 return -1;
330 }
331
332 return s->buf_len[buf.index];
333 }
334
335 static int read_frame(AVFormatContext *ctx, void *frame, int64_t *ts)
336 {
337 return -1;
338 }
339
340 static int mmap_start(AVFormatContext *ctx)
341 {
342 struct video_data *s = ctx->priv_data;
343 enum v4l2_buf_type type;
344 int i, res;
345
346 for (i = 0; i < s->buffers; i++) {
347 struct v4l2_buffer buf;
348
349 memset(&buf, 0, sizeof(struct v4l2_buffer));
350 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
351 buf.memory = V4L2_MEMORY_MMAP;
352 buf.index = i;
353
354 res = ioctl (s->fd, VIDIOC_QBUF, &buf);
355 if (res < 0) {
356 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", strerror(errno));
357
358 return -1;
359 }
360 }
361
362 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
363 res = ioctl (s->fd, VIDIOC_STREAMON, &type);
364 if (res < 0) {
365 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n", strerror(errno));
366
367 return -1;
368 }
369
370 return 0;
371 }
372
373 static void mmap_close(struct video_data *s)
374 {
375 enum v4l2_buf_type type;
376 int i;
377
378 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
379 /* We do not check for the result, because we could
380 * not do anything about it anyway...
381 */
382 ioctl(s->fd, VIDIOC_STREAMOFF, &type);
383 for (i = 0; i < s->buffers; i++) {
384 munmap(s->buf_start[i], s->buf_len[i]);
385 }
386 av_free(s->buf_start);
387 av_free(s->buf_len);
388 }
389
390 static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
391 {
392 struct video_data *s = s1->priv_data;
393 AVStream *st;
394 int width, height;
395 int res, frame_rate, frame_rate_base;
396 uint32_t desired_format, capabilities;
397 const char *video_device;
398
399 if (ap->width <= 0 || ap->height <= 0 || ap->time_base.den <= 0) {
400 av_log(s1, AV_LOG_ERROR, "Missing/Wrong parameters\n");
401
402 return -1;
403 }
404
405 width = ap->width;
406 height = ap->height;
407 frame_rate = ap->time_base.den;
408 frame_rate_base = ap->time_base.num;
409
410 if((unsigned)width > 32767 || (unsigned)height > 32767) {
411 av_log(s1, AV_LOG_ERROR, "Wrong size %dx%d\n", width, height);
412
413 return -1;
414 }
415
416 st = av_new_stream(s1, 0);
417 if (!st) {
418 return AVERROR(ENOMEM);
419 }
420 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
421
422 s->width = width;
423 s->height = height;
424 s->frame_rate = frame_rate;
425 s->frame_rate_base = frame_rate_base;
426
427 video_device = ap->device;
428 if (!video_device) {
429 video_device = "/dev/video";
430 }
431 capabilities = 0;
432 s->fd = device_open(s1, video_device, &capabilities);
433 if (s->fd < 0) {
434 av_free(st);
435
436 return AVERROR_IO;
437 }
438 av_log(s1, AV_LOG_INFO, "[%d]Capabilities: %x\n", s->fd, capabilities);
439
440 desired_format = fmt_ff2v4l(ap->pix_fmt);
441 if (desired_format == 0 || (device_init(s1, &width, &height, desired_format) < 0)) {
442 int i, done;
443
444 done = 0; i = 0;
445 while (!done) {
446 desired_format = fmt_conversion_table[i].v4l2_fmt;
447 if (device_init(s1, &width, &height, desired_format) < 0) {
448 desired_format = 0;
449 i++;
450 } else {
451 done = 1;
452 }
453 if (i == sizeof(fmt_conversion_table) / sizeof(struct fmt_map)) {
454 done = 1;
455 }
456 }
457 }
458 if (desired_format == 0) {
459 av_log(s1, AV_LOG_ERROR, "Cannot find a proper format.\n");
460 close(s->fd);
461 av_free(st);
462
463 return AVERROR_IO;
464 }
465 s->frame_format = desired_format;
466
467 st->codec->pix_fmt = fmt_v4l2ff(desired_format);
468 s->frame_size = avpicture_get_size(st->codec->pix_fmt, width, height);
469 if (capabilities & V4L2_CAP_STREAMING) {
470 s->io_method = io_mmap;
471 res = mmap_init(s1);
472 if (res == 0) {
473 res = mmap_start(s1);
474 }
475 } else {
476 s->io_method = io_read;
477 res = read_init(s1);
478 }
479 if (res < 0) {
480 close(s->fd);
481 av_free(st);
482
483 return AVERROR_IO;
484 }
485 s->top_field_first = first_field(s->fd);
486
487 st->codec->codec_type = CODEC_TYPE_VIDEO;
488 st->codec->codec_id = CODEC_ID_RAWVIDEO;
489 st->codec->width = width;
490 st->codec->height = height;
491 st->codec->time_base.den = frame_rate;
492 st->codec->time_base.num = frame_rate_base;
493 st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
494
495 return 0;
496 }
497
498 static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
499 {
500 struct video_data *s = s1->priv_data;
501 int res;
502
503 if (av_new_packet(pkt, s->frame_size) < 0)
504 return AVERROR_IO;
505
506 if (s->io_method == io_mmap) {
507 res = mmap_read_frame(s1, pkt->data, &pkt->pts);
508 } else if (s->io_method == io_read) {
509 res = read_frame(s1, pkt->data, &pkt->pts);
510 } else {
511 return AVERROR_IO;
512 }
513 if (res < 0) {
514 return AVERROR_IO;
515 }
516
517 if (s1->streams[0]->codec->coded_frame) {
518 s1->streams[0]->codec->coded_frame->interlaced_frame = 1;
519 s1->streams[0]->codec->coded_frame->top_field_first = s->top_field_first;
520 }
521
522 return s->frame_size;
523 }
524
525 static int v4l2_read_close(AVFormatContext *s1)
526 {
527 struct video_data *s = s1->priv_data;
528
529 if (s->io_method == io_mmap) {
530 mmap_close(s);
531 }
532
533 close(s->fd);
534 return 0;
535 }
536
537 AVInputFormat v4l2_demuxer = {
538 "video4linux2",
539 "video grab",
540 sizeof(struct video_data),
541 NULL,
542 v4l2_read_header,
543 v4l2_read_packet,
544 v4l2_read_close,
545 .flags = AVFMT_NOFILE,
546 };