annotate v4l2.c @ 2392:39e9fb85fcbc libavformat

store asf streaming bitrates consistently patch by Bj«Órn Axelsson: [bjorn axelsson intinor se]
author benoit
date Fri, 17 Aug 2007 06:49:43 +0000
parents b21c2af60bc9
children 0cc87456f0ad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
1 /*
547675092e98 video4linux2 input support
diego
parents:
diff changeset
2 * Video4Linux2 grab interface
547675092e98 video4linux2 input support
diego
parents:
diff changeset
3 * Copyright (c) 2000,2001 Fabrice Bellard.
547675092e98 video4linux2 input support
diego
parents:
diff changeset
4 * Copyright (c) 2006 Luca Abeni.
547675092e98 video4linux2 input support
diego
parents:
diff changeset
5 *
547675092e98 video4linux2 input support
diego
parents:
diff changeset
6 * Part of this file is based on the V4L2 video capture example
547675092e98 video4linux2 input support
diego
parents:
diff changeset
7 * (http://v4l2spec.bytesex.org/v4l2spec/capture.c)
547675092e98 video4linux2 input support
diego
parents:
diff changeset
8 *
547675092e98 video4linux2 input support
diego
parents:
diff changeset
9 * Thanks to Michael Niedermayer for providing the mapping between
547675092e98 video4linux2 input support
diego
parents:
diff changeset
10 * V4L2_PIX_FMT_* and PIX_FMT_*
547675092e98 video4linux2 input support
diego
parents:
diff changeset
11 *
547675092e98 video4linux2 input support
diego
parents:
diff changeset
12 *
1358
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1169
diff changeset
13 * This file is part of FFmpeg.
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1169
diff changeset
14 *
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1169
diff changeset
15 * FFmpeg is free software; you can redistribute it and/or
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
16 * modify it under the terms of the GNU Lesser General Public
547675092e98 video4linux2 input support
diego
parents:
diff changeset
17 * License as published by the Free Software Foundation; either
1358
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1169
diff changeset
18 * version 2.1 of the License, or (at your option) any later version.
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
19 *
1358
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1169
diff changeset
20 * FFmpeg is distributed in the hope that it will be useful,
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
547675092e98 video4linux2 input support
diego
parents:
diff changeset
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
547675092e98 video4linux2 input support
diego
parents:
diff changeset
23 * Lesser General Public License for more details.
547675092e98 video4linux2 input support
diego
parents:
diff changeset
24 *
547675092e98 video4linux2 input support
diego
parents:
diff changeset
25 * You should have received a copy of the GNU Lesser General Public
1358
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1169
diff changeset
26 * License along with FFmpeg; if not, write to the Free Software
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
547675092e98 video4linux2 input support
diego
parents:
diff changeset
28 */
547675092e98 video4linux2 input support
diego
parents:
diff changeset
29 #include "avformat.h"
547675092e98 video4linux2 input support
diego
parents:
diff changeset
30 #include <unistd.h>
547675092e98 video4linux2 input support
diego
parents:
diff changeset
31 #include <fcntl.h>
547675092e98 video4linux2 input support
diego
parents:
diff changeset
32 #include <sys/ioctl.h>
547675092e98 video4linux2 input support
diego
parents:
diff changeset
33 #include <sys/mman.h>
547675092e98 video4linux2 input support
diego
parents:
diff changeset
34 #include <sys/time.h>
1100
d4793a811695 Include the correct video4linux2 header
lucabe
parents: 1013
diff changeset
35 #include <asm/types.h>
d4793a811695 Include the correct video4linux2 header
lucabe
parents: 1013
diff changeset
36 #include <linux/videodev2.h>
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
37 #include <time.h>
547675092e98 video4linux2 input support
diego
parents:
diff changeset
38
547675092e98 video4linux2 input support
diego
parents:
diff changeset
39 static const int desired_video_buffers = 256;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
40
547675092e98 video4linux2 input support
diego
parents:
diff changeset
41 enum io_method {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
42 io_read,
547675092e98 video4linux2 input support
diego
parents:
diff changeset
43 io_mmap,
547675092e98 video4linux2 input support
diego
parents:
diff changeset
44 io_userptr
547675092e98 video4linux2 input support
diego
parents:
diff changeset
45 };
547675092e98 video4linux2 input support
diego
parents:
diff changeset
46
547675092e98 video4linux2 input support
diego
parents:
diff changeset
47 struct video_data {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
48 int fd;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
49 int frame_format; /* V4L2_PIX_FMT_* */
547675092e98 video4linux2 input support
diego
parents:
diff changeset
50 enum io_method io_method;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
51 int width, height;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
52 int frame_rate;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
53 int frame_rate_base;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
54 int frame_size;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
55 int top_field_first;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
56
547675092e98 video4linux2 input support
diego
parents:
diff changeset
57 int buffers;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
58 void **buf_start;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
59 unsigned int *buf_len;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
60 };
547675092e98 video4linux2 input support
diego
parents:
diff changeset
61
1793
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
62 struct buff_data {
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
63 int index;
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
64 int fd;
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
65 };
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
66
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
67 struct fmt_map {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
68 enum PixelFormat ff_fmt;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
69 int32_t v4l2_fmt;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
70 };
547675092e98 video4linux2 input support
diego
parents:
diff changeset
71
547675092e98 video4linux2 input support
diego
parents:
diff changeset
72 static struct fmt_map fmt_conversion_table[] = {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
73 {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
74 .ff_fmt = PIX_FMT_YUV420P,
547675092e98 video4linux2 input support
diego
parents:
diff changeset
75 .v4l2_fmt = V4L2_PIX_FMT_YUV420,
547675092e98 video4linux2 input support
diego
parents:
diff changeset
76 },
547675092e98 video4linux2 input support
diego
parents:
diff changeset
77 {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
78 .ff_fmt = PIX_FMT_YUV422P,
547675092e98 video4linux2 input support
diego
parents:
diff changeset
79 .v4l2_fmt = V4L2_PIX_FMT_YUV422P,
547675092e98 video4linux2 input support
diego
parents:
diff changeset
80 },
547675092e98 video4linux2 input support
diego
parents:
diff changeset
81 {
1760
8cba5672faa4 Replace deprecated PIX_FMT names by the newer variants.
diego
parents: 1556
diff changeset
82 .ff_fmt = PIX_FMT_YUYV422,
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
83 .v4l2_fmt = V4L2_PIX_FMT_YUYV,
547675092e98 video4linux2 input support
diego
parents:
diff changeset
84 },
547675092e98 video4linux2 input support
diego
parents:
diff changeset
85 {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
86 .ff_fmt = PIX_FMT_UYVY422,
547675092e98 video4linux2 input support
diego
parents:
diff changeset
87 .v4l2_fmt = V4L2_PIX_FMT_UYVY,
547675092e98 video4linux2 input support
diego
parents:
diff changeset
88 },
547675092e98 video4linux2 input support
diego
parents:
diff changeset
89 {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
90 .ff_fmt = PIX_FMT_YUV411P,
547675092e98 video4linux2 input support
diego
parents:
diff changeset
91 .v4l2_fmt = V4L2_PIX_FMT_YUV411P,
547675092e98 video4linux2 input support
diego
parents:
diff changeset
92 },
547675092e98 video4linux2 input support
diego
parents:
diff changeset
93 {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
94 .ff_fmt = PIX_FMT_YUV410P,
547675092e98 video4linux2 input support
diego
parents:
diff changeset
95 .v4l2_fmt = V4L2_PIX_FMT_YUV410,
547675092e98 video4linux2 input support
diego
parents:
diff changeset
96 },
547675092e98 video4linux2 input support
diego
parents:
diff changeset
97 {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
98 .ff_fmt = PIX_FMT_BGR24,
547675092e98 video4linux2 input support
diego
parents:
diff changeset
99 .v4l2_fmt = V4L2_PIX_FMT_BGR24,
547675092e98 video4linux2 input support
diego
parents:
diff changeset
100 },
547675092e98 video4linux2 input support
diego
parents:
diff changeset
101 {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
102 .ff_fmt = PIX_FMT_RGB24,
547675092e98 video4linux2 input support
diego
parents:
diff changeset
103 .v4l2_fmt = V4L2_PIX_FMT_RGB24,
547675092e98 video4linux2 input support
diego
parents:
diff changeset
104 },
547675092e98 video4linux2 input support
diego
parents:
diff changeset
105 /*
547675092e98 video4linux2 input support
diego
parents:
diff changeset
106 {
1760
8cba5672faa4 Replace deprecated PIX_FMT names by the newer variants.
diego
parents: 1556
diff changeset
107 .ff_fmt = PIX_FMT_RGB32,
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
108 .v4l2_fmt = V4L2_PIX_FMT_BGR32,
547675092e98 video4linux2 input support
diego
parents:
diff changeset
109 },
547675092e98 video4linux2 input support
diego
parents:
diff changeset
110 */
547675092e98 video4linux2 input support
diego
parents:
diff changeset
111 {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
112 .ff_fmt = PIX_FMT_GRAY8,
547675092e98 video4linux2 input support
diego
parents:
diff changeset
113 .v4l2_fmt = V4L2_PIX_FMT_GREY,
547675092e98 video4linux2 input support
diego
parents:
diff changeset
114 },
547675092e98 video4linux2 input support
diego
parents:
diff changeset
115 };
547675092e98 video4linux2 input support
diego
parents:
diff changeset
116
1795
62792a60f740 implement new grabbing interface, as described here:
gpoirier
parents: 1793
diff changeset
117 static int device_open(AVFormatContext *ctx, uint32_t *capabilities)
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
118 {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
119 struct v4l2_capability cap;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
120 int fd;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
121 int res;
2221
efb7b615d57c Support for the AVFMT_FLAG_NONBLOCK flag (non-blocking input) in v4l2.c
lucabe
parents: 2078
diff changeset
122 int flags = O_RDWR;
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
123
2221
efb7b615d57c Support for the AVFMT_FLAG_NONBLOCK flag (non-blocking input) in v4l2.c
lucabe
parents: 2078
diff changeset
124 if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
efb7b615d57c Support for the AVFMT_FLAG_NONBLOCK flag (non-blocking input) in v4l2.c
lucabe
parents: 2078
diff changeset
125 flags |= O_NONBLOCK;
efb7b615d57c Support for the AVFMT_FLAG_NONBLOCK flag (non-blocking input) in v4l2.c
lucabe
parents: 2078
diff changeset
126 }
efb7b615d57c Support for the AVFMT_FLAG_NONBLOCK flag (non-blocking input) in v4l2.c
lucabe
parents: 2078
diff changeset
127 fd = open(ctx->filename, flags, 0);
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
128 if (fd < 0) {
1780
5e6a9b3a9698 Pass a proper context to av_log()
lucabe
parents: 1760
diff changeset
129 av_log(ctx, AV_LOG_ERROR, "Cannot open video device %s : %s\n",
1795
62792a60f740 implement new grabbing interface, as described here:
gpoirier
parents: 1793
diff changeset
130 ctx->filename, strerror(errno));
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
131
547675092e98 video4linux2 input support
diego
parents:
diff changeset
132 return -1;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
133 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
134
547675092e98 video4linux2 input support
diego
parents:
diff changeset
135 res = ioctl(fd, VIDIOC_QUERYCAP, &cap);
973
136f654dfc03 print a hint when trying V4L2 on V4L device
alex
parents: 921
diff changeset
136 // ENOIOCTLCMD definition only availble on __KERNEL__
136f654dfc03 print a hint when trying V4L2 on V4L device
alex
parents: 921
diff changeset
137 if (res < 0 && errno == 515)
136f654dfc03 print a hint when trying V4L2 on V4L device
alex
parents: 921
diff changeset
138 {
1780
5e6a9b3a9698 Pass a proper context to av_log()
lucabe
parents: 1760
diff changeset
139 av_log(ctx, AV_LOG_ERROR, "QUERYCAP not implemented, probably V4L device but not supporting V4L2\n");
974
e0cfc88ad93a Error path fix: close the video device on failure
lucabe
parents: 973
diff changeset
140 close(fd);
973
136f654dfc03 print a hint when trying V4L2 on V4L device
alex
parents: 921
diff changeset
141
136f654dfc03 print a hint when trying V4L2 on V4L device
alex
parents: 921
diff changeset
142 return -1;
136f654dfc03 print a hint when trying V4L2 on V4L device
alex
parents: 921
diff changeset
143 }
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
144 if (res < 0) {
1780
5e6a9b3a9698 Pass a proper context to av_log()
lucabe
parents: 1760
diff changeset
145 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n",
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
146 strerror(errno));
974
e0cfc88ad93a Error path fix: close the video device on failure
lucabe
parents: 973
diff changeset
147 close(fd);
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
148
547675092e98 video4linux2 input support
diego
parents:
diff changeset
149 return -1;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
150 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
151 if ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {
1780
5e6a9b3a9698 Pass a proper context to av_log()
lucabe
parents: 1760
diff changeset
152 av_log(ctx, AV_LOG_ERROR, "Not a video capture device\n");
974
e0cfc88ad93a Error path fix: close the video device on failure
lucabe
parents: 973
diff changeset
153 close(fd);
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
154
547675092e98 video4linux2 input support
diego
parents:
diff changeset
155 return -1;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
156 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
157 *capabilities = cap.capabilities;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
158
547675092e98 video4linux2 input support
diego
parents:
diff changeset
159 return fd;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
160 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
161
1780
5e6a9b3a9698 Pass a proper context to av_log()
lucabe
parents: 1760
diff changeset
162 static int device_init(AVFormatContext *ctx, int *width, int *height, int pix_fmt)
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
163 {
1780
5e6a9b3a9698 Pass a proper context to av_log()
lucabe
parents: 1760
diff changeset
164 struct video_data *s = ctx->priv_data;
5e6a9b3a9698 Pass a proper context to av_log()
lucabe
parents: 1760
diff changeset
165 int fd = s->fd;
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
166 struct v4l2_format fmt;
975
36a69fbabcfb Add support for drivers that can change the capture size on VIDIOC_S_FMT
lucabe
parents: 974
diff changeset
167 int res;
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
168
547675092e98 video4linux2 input support
diego
parents:
diff changeset
169 memset(&fmt, 0, sizeof(struct v4l2_format));
547675092e98 video4linux2 input support
diego
parents:
diff changeset
170 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
975
36a69fbabcfb Add support for drivers that can change the capture size on VIDIOC_S_FMT
lucabe
parents: 974
diff changeset
171 fmt.fmt.pix.width = *width;
36a69fbabcfb Add support for drivers that can change the capture size on VIDIOC_S_FMT
lucabe
parents: 974
diff changeset
172 fmt.fmt.pix.height = *height;
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
173 fmt.fmt.pix.pixelformat = pix_fmt;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
174 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
975
36a69fbabcfb Add support for drivers that can change the capture size on VIDIOC_S_FMT
lucabe
parents: 974
diff changeset
175 res = ioctl(fd, VIDIOC_S_FMT, &fmt);
36a69fbabcfb Add support for drivers that can change the capture size on VIDIOC_S_FMT
lucabe
parents: 974
diff changeset
176 if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
1780
5e6a9b3a9698 Pass a proper context to av_log()
lucabe
parents: 1760
diff changeset
177 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);
975
36a69fbabcfb Add support for drivers that can change the capture size on VIDIOC_S_FMT
lucabe
parents: 974
diff changeset
178 *width = fmt.fmt.pix.width;
36a69fbabcfb Add support for drivers that can change the capture size on VIDIOC_S_FMT
lucabe
parents: 974
diff changeset
179 *height = fmt.fmt.pix.height;
36a69fbabcfb Add support for drivers that can change the capture size on VIDIOC_S_FMT
lucabe
parents: 974
diff changeset
180 }
36a69fbabcfb Add support for drivers that can change the capture size on VIDIOC_S_FMT
lucabe
parents: 974
diff changeset
181
36a69fbabcfb Add support for drivers that can change the capture size on VIDIOC_S_FMT
lucabe
parents: 974
diff changeset
182 return res;
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
183 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
184
547675092e98 video4linux2 input support
diego
parents:
diff changeset
185 static int first_field(int fd)
547675092e98 video4linux2 input support
diego
parents:
diff changeset
186 {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
187 int res;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
188 v4l2_std_id std;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
189
547675092e98 video4linux2 input support
diego
parents:
diff changeset
190 res = ioctl(fd, VIDIOC_G_STD, &std);
547675092e98 video4linux2 input support
diego
parents:
diff changeset
191 if (res < 0) {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
192 return 0;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
193 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
194 if (std & V4L2_STD_NTSC) {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
195 return 0;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
196 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
197
547675092e98 video4linux2 input support
diego
parents:
diff changeset
198 return 1;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
199 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
200
547675092e98 video4linux2 input support
diego
parents:
diff changeset
201 static uint32_t fmt_ff2v4l(enum PixelFormat pix_fmt)
547675092e98 video4linux2 input support
diego
parents:
diff changeset
202 {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
203 int i;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
204
547675092e98 video4linux2 input support
diego
parents:
diff changeset
205 for (i = 0; i < sizeof(fmt_conversion_table) / sizeof(struct fmt_map); i++) {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
206 if (fmt_conversion_table[i].ff_fmt == pix_fmt) {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
207 return fmt_conversion_table[i].v4l2_fmt;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
208 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
209 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
210
547675092e98 video4linux2 input support
diego
parents:
diff changeset
211 return 0;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
212 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
213
547675092e98 video4linux2 input support
diego
parents:
diff changeset
214 static enum PixelFormat fmt_v4l2ff(uint32_t pix_fmt)
547675092e98 video4linux2 input support
diego
parents:
diff changeset
215 {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
216 int i;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
217
547675092e98 video4linux2 input support
diego
parents:
diff changeset
218 for (i = 0; i < sizeof(fmt_conversion_table) / sizeof(struct fmt_map); i++) {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
219 if (fmt_conversion_table[i].v4l2_fmt == pix_fmt) {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
220 return fmt_conversion_table[i].ff_fmt;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
221 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
222 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
223
547675092e98 video4linux2 input support
diego
parents:
diff changeset
224 return -1;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
225 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
226
1780
5e6a9b3a9698 Pass a proper context to av_log()
lucabe
parents: 1760
diff changeset
227 static int mmap_init(AVFormatContext *ctx)
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
228 {
1780
5e6a9b3a9698 Pass a proper context to av_log()
lucabe
parents: 1760
diff changeset
229 struct video_data *s = ctx->priv_data;
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
230 struct v4l2_requestbuffers req;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
231 int i, res;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
232
547675092e98 video4linux2 input support
diego
parents:
diff changeset
233 memset(&req, 0, sizeof(struct v4l2_requestbuffers));
547675092e98 video4linux2 input support
diego
parents:
diff changeset
234 req.count = desired_video_buffers;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
235 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
236 req.memory = V4L2_MEMORY_MMAP;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
237 res = ioctl (s->fd, VIDIOC_REQBUFS, &req);
547675092e98 video4linux2 input support
diego
parents:
diff changeset
238 if (res < 0) {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
239 if (errno == EINVAL) {
1780
5e6a9b3a9698 Pass a proper context to av_log()
lucabe
parents: 1760
diff changeset
240 av_log(ctx, AV_LOG_ERROR, "Device does not support mmap\n");
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
241 } else {
1780
5e6a9b3a9698 Pass a proper context to av_log()
lucabe
parents: 1760
diff changeset
242 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_REQBUFS)\n");
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
243 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
244
547675092e98 video4linux2 input support
diego
parents:
diff changeset
245 return -1;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
246 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
247
547675092e98 video4linux2 input support
diego
parents:
diff changeset
248 if (req.count < 2) {
1780
5e6a9b3a9698 Pass a proper context to av_log()
lucabe
parents: 1760
diff changeset
249 av_log(ctx, AV_LOG_ERROR, "Insufficient buffer memory\n");
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
250
547675092e98 video4linux2 input support
diego
parents:
diff changeset
251 return -1;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
252 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
253 s->buffers = req.count;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
254 s->buf_start = av_malloc(sizeof(void *) * s->buffers);
547675092e98 video4linux2 input support
diego
parents:
diff changeset
255 if (s->buf_start == NULL) {
1780
5e6a9b3a9698 Pass a proper context to av_log()
lucabe
parents: 1760
diff changeset
256 av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer pointers\n");
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
257
547675092e98 video4linux2 input support
diego
parents:
diff changeset
258 return -1;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
259 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
260 s->buf_len = av_malloc(sizeof(unsigned int) * s->buffers);
547675092e98 video4linux2 input support
diego
parents:
diff changeset
261 if (s->buf_len == NULL) {
1780
5e6a9b3a9698 Pass a proper context to av_log()
lucabe
parents: 1760
diff changeset
262 av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer sizes\n");
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
263 av_free(s->buf_start);
547675092e98 video4linux2 input support
diego
parents:
diff changeset
264
547675092e98 video4linux2 input support
diego
parents:
diff changeset
265 return -1;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
266 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
267
547675092e98 video4linux2 input support
diego
parents:
diff changeset
268 for (i = 0; i < req.count; i++) {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
269 struct v4l2_buffer buf;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
270
547675092e98 video4linux2 input support
diego
parents:
diff changeset
271 memset(&buf, 0, sizeof(struct v4l2_buffer));
547675092e98 video4linux2 input support
diego
parents:
diff changeset
272 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
273 buf.memory = V4L2_MEMORY_MMAP;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
274 buf.index = i;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
275 res = ioctl (s->fd, VIDIOC_QUERYBUF, &buf);
547675092e98 video4linux2 input support
diego
parents:
diff changeset
276 if (res < 0) {
1780
5e6a9b3a9698 Pass a proper context to av_log()
lucabe
parents: 1760
diff changeset
277 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYBUF)\n");
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
278
547675092e98 video4linux2 input support
diego
parents:
diff changeset
279 return -1;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
280 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
281
547675092e98 video4linux2 input support
diego
parents:
diff changeset
282 s->buf_len[i] = buf.length;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
283 if (s->buf_len[i] < s->frame_size) {
1780
5e6a9b3a9698 Pass a proper context to av_log()
lucabe
parents: 1760
diff changeset
284 av_log(ctx, AV_LOG_ERROR, "Buffer len [%d] = %d != %d\n", i, s->buf_len[i], s->frame_size);
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
285
547675092e98 video4linux2 input support
diego
parents:
diff changeset
286 return -1;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
287 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
288 s->buf_start[i] = mmap (NULL, buf.length,
547675092e98 video4linux2 input support
diego
parents:
diff changeset
289 PROT_READ | PROT_WRITE, MAP_SHARED, s->fd, buf.m.offset);
547675092e98 video4linux2 input support
diego
parents:
diff changeset
290 if (s->buf_start[i] == MAP_FAILED) {
1780
5e6a9b3a9698 Pass a proper context to av_log()
lucabe
parents: 1760
diff changeset
291 av_log(ctx, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
292
547675092e98 video4linux2 input support
diego
parents:
diff changeset
293 return -1;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
294 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
295 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
296
547675092e98 video4linux2 input support
diego
parents:
diff changeset
297 return 0;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
298 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
299
1780
5e6a9b3a9698 Pass a proper context to av_log()
lucabe
parents: 1760
diff changeset
300 static int read_init(AVFormatContext *ctx)
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
301 {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
302 return -1;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
303 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
304
1793
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
305 static void mmap_release_buffer(AVPacket *pkt)
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
306 {
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
307 struct v4l2_buffer buf;
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
308 int res, fd;
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
309 struct buff_data *buf_descriptor = pkt->priv;
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
310
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
311 memset(&buf, 0, sizeof(struct v4l2_buffer));
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
312 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
313 buf.memory = V4L2_MEMORY_MMAP;
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
314 buf.index = buf_descriptor->index;
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
315 fd = buf_descriptor->fd;
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
316 av_free(buf_descriptor);
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
317
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
318 res = ioctl (fd, VIDIOC_QBUF, &buf);
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
319 if (res < 0) {
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
320 av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF)\n");
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
321 }
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
322 pkt->data = NULL;
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
323 pkt->size = 0;
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
324 }
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
325
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
326 static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt)
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
327 {
1780
5e6a9b3a9698 Pass a proper context to av_log()
lucabe
parents: 1760
diff changeset
328 struct video_data *s = ctx->priv_data;
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
329 struct v4l2_buffer buf;
1793
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
330 struct buff_data *buf_descriptor;
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
331 int res;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
332
547675092e98 video4linux2 input support
diego
parents:
diff changeset
333 memset(&buf, 0, sizeof(struct v4l2_buffer));
547675092e98 video4linux2 input support
diego
parents:
diff changeset
334 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
335 buf.memory = V4L2_MEMORY_MMAP;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
336
547675092e98 video4linux2 input support
diego
parents:
diff changeset
337 /* FIXME: Some special treatment might be needed in case of loss of signal... */
2221
efb7b615d57c Support for the AVFMT_FLAG_NONBLOCK flag (non-blocking input) in v4l2.c
lucabe
parents: 2078
diff changeset
338 while ((res = ioctl(s->fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
339 if (res < 0) {
2221
efb7b615d57c Support for the AVFMT_FLAG_NONBLOCK flag (non-blocking input) in v4l2.c
lucabe
parents: 2078
diff changeset
340 if (errno == EAGAIN) {
efb7b615d57c Support for the AVFMT_FLAG_NONBLOCK flag (non-blocking input) in v4l2.c
lucabe
parents: 2078
diff changeset
341 pkt->size = 0;
efb7b615d57c Support for the AVFMT_FLAG_NONBLOCK flag (non-blocking input) in v4l2.c
lucabe
parents: 2078
diff changeset
342
efb7b615d57c Support for the AVFMT_FLAG_NONBLOCK flag (non-blocking input) in v4l2.c
lucabe
parents: 2078
diff changeset
343 return AVERROR(EAGAIN);
efb7b615d57c Support for the AVFMT_FLAG_NONBLOCK flag (non-blocking input) in v4l2.c
lucabe
parents: 2078
diff changeset
344 }
1780
5e6a9b3a9698 Pass a proper context to av_log()
lucabe
parents: 1760
diff changeset
345 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n", strerror(errno));
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
346
547675092e98 video4linux2 input support
diego
parents:
diff changeset
347 return -1;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
348 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
349 assert (buf.index < s->buffers);
1407
fb4bf3858f77 Make read_packet fail is the v4l2 driver returns an unexpected frame size
lucabe
parents: 1358
diff changeset
350 if (buf.bytesused != s->frame_size) {
1780
5e6a9b3a9698 Pass a proper context to av_log()
lucabe
parents: 1760
diff changeset
351 av_log(ctx, AV_LOG_ERROR, "The v4l2 frame is %d bytes, but %d bytes are expected\n", buf.bytesused, s->frame_size);
1407
fb4bf3858f77 Make read_packet fail is the v4l2 driver returns an unexpected frame size
lucabe
parents: 1358
diff changeset
352
fb4bf3858f77 Make read_packet fail is the v4l2 driver returns an unexpected frame size
lucabe
parents: 1358
diff changeset
353 return -1;
fb4bf3858f77 Make read_packet fail is the v4l2 driver returns an unexpected frame size
lucabe
parents: 1358
diff changeset
354 }
fb4bf3858f77 Make read_packet fail is the v4l2 driver returns an unexpected frame size
lucabe
parents: 1358
diff changeset
355
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
356 /* Image is at s->buff_start[buf.index] */
1793
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
357 pkt->data= s->buf_start[buf.index];
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
358 pkt->size = buf.bytesused;
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
359 pkt->pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
360 pkt->destruct = mmap_release_buffer;
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
361 buf_descriptor = av_malloc(sizeof(struct buff_data));
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
362 if (buf_descriptor == NULL) {
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
363 /* Something went wrong... Since av_malloc() failed, we cannot even
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
364 * allocate a buffer for memcopying into it
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
365 */
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
366 av_log(ctx, AV_LOG_ERROR, "Failed to allocate a buffer descriptor\n");
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
367 res = ioctl (s->fd, VIDIOC_QBUF, &buf);
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
368
547675092e98 video4linux2 input support
diego
parents:
diff changeset
369 return -1;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
370 }
1793
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
371 buf_descriptor->fd = s->fd;
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
372 buf_descriptor->index = buf.index;
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
373 pkt->priv = buf_descriptor;
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
374
547675092e98 video4linux2 input support
diego
parents:
diff changeset
375 return s->buf_len[buf.index];
547675092e98 video4linux2 input support
diego
parents:
diff changeset
376 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
377
1793
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
378 static int read_frame(AVFormatContext *ctx, AVPacket *pkt)
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
379 {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
380 return -1;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
381 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
382
1780
5e6a9b3a9698 Pass a proper context to av_log()
lucabe
parents: 1760
diff changeset
383 static int mmap_start(AVFormatContext *ctx)
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
384 {
1780
5e6a9b3a9698 Pass a proper context to av_log()
lucabe
parents: 1760
diff changeset
385 struct video_data *s = ctx->priv_data;
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
386 enum v4l2_buf_type type;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
387 int i, res;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
388
547675092e98 video4linux2 input support
diego
parents:
diff changeset
389 for (i = 0; i < s->buffers; i++) {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
390 struct v4l2_buffer buf;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
391
547675092e98 video4linux2 input support
diego
parents:
diff changeset
392 memset(&buf, 0, sizeof(struct v4l2_buffer));
547675092e98 video4linux2 input support
diego
parents:
diff changeset
393 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
394 buf.memory = V4L2_MEMORY_MMAP;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
395 buf.index = i;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
396
547675092e98 video4linux2 input support
diego
parents:
diff changeset
397 res = ioctl (s->fd, VIDIOC_QBUF, &buf);
547675092e98 video4linux2 input support
diego
parents:
diff changeset
398 if (res < 0) {
1780
5e6a9b3a9698 Pass a proper context to av_log()
lucabe
parents: 1760
diff changeset
399 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", strerror(errno));
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
400
547675092e98 video4linux2 input support
diego
parents:
diff changeset
401 return -1;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
402 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
403 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
404
547675092e98 video4linux2 input support
diego
parents:
diff changeset
405 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
406 res = ioctl (s->fd, VIDIOC_STREAMON, &type);
547675092e98 video4linux2 input support
diego
parents:
diff changeset
407 if (res < 0) {
1780
5e6a9b3a9698 Pass a proper context to av_log()
lucabe
parents: 1760
diff changeset
408 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n", strerror(errno));
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
409
547675092e98 video4linux2 input support
diego
parents:
diff changeset
410 return -1;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
411 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
412
547675092e98 video4linux2 input support
diego
parents:
diff changeset
413 return 0;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
414 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
415
547675092e98 video4linux2 input support
diego
parents:
diff changeset
416 static void mmap_close(struct video_data *s)
547675092e98 video4linux2 input support
diego
parents:
diff changeset
417 {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
418 enum v4l2_buf_type type;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
419 int i;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
420
547675092e98 video4linux2 input support
diego
parents:
diff changeset
421 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
422 /* We do not check for the result, because we could
547675092e98 video4linux2 input support
diego
parents:
diff changeset
423 * not do anything about it anyway...
547675092e98 video4linux2 input support
diego
parents:
diff changeset
424 */
547675092e98 video4linux2 input support
diego
parents:
diff changeset
425 ioctl(s->fd, VIDIOC_STREAMOFF, &type);
547675092e98 video4linux2 input support
diego
parents:
diff changeset
426 for (i = 0; i < s->buffers; i++) {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
427 munmap(s->buf_start[i], s->buf_len[i]);
547675092e98 video4linux2 input support
diego
parents:
diff changeset
428 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
429 av_free(s->buf_start);
547675092e98 video4linux2 input support
diego
parents:
diff changeset
430 av_free(s->buf_len);
547675092e98 video4linux2 input support
diego
parents:
diff changeset
431 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
432
1961
0bb1bfbaa031 Allow setting v4l2 input and video standard.
lucabe
parents: 1795
diff changeset
433 static int v4l2_set_parameters( AVFormatContext *s1, AVFormatParameters *ap )
0bb1bfbaa031 Allow setting v4l2 input and video standard.
lucabe
parents: 1795
diff changeset
434 {
0bb1bfbaa031 Allow setting v4l2 input and video standard.
lucabe
parents: 1795
diff changeset
435 struct video_data *s = s1->priv_data;
0bb1bfbaa031 Allow setting v4l2 input and video standard.
lucabe
parents: 1795
diff changeset
436 struct v4l2_input input;
0bb1bfbaa031 Allow setting v4l2 input and video standard.
lucabe
parents: 1795
diff changeset
437 struct v4l2_standard standard;
0bb1bfbaa031 Allow setting v4l2 input and video standard.
lucabe
parents: 1795
diff changeset
438 int i;
0bb1bfbaa031 Allow setting v4l2 input and video standard.
lucabe
parents: 1795
diff changeset
439
2077
4b07eb8709f2 Allow avoid setting the video standard and input when capturing v4l2 video.
lucabe
parents: 1961
diff changeset
440 if(ap->channel>=0) {
2078
17b7ebc3e1f9 Reindent code after last commit
lucabe
parents: 2077
diff changeset
441 /* set tv video input */
17b7ebc3e1f9 Reindent code after last commit
lucabe
parents: 2077
diff changeset
442 memset (&input, 0, sizeof (input));
17b7ebc3e1f9 Reindent code after last commit
lucabe
parents: 2077
diff changeset
443 input.index = ap->channel;
17b7ebc3e1f9 Reindent code after last commit
lucabe
parents: 2077
diff changeset
444 if(ioctl (s->fd, VIDIOC_ENUMINPUT, &input) < 0) {
17b7ebc3e1f9 Reindent code after last commit
lucabe
parents: 2077
diff changeset
445 av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl enum input failed:\n");
2274
b21c2af60bc9 Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents: 2221
diff changeset
446 return AVERROR(EIO);
2078
17b7ebc3e1f9 Reindent code after last commit
lucabe
parents: 2077
diff changeset
447 }
1961
0bb1bfbaa031 Allow setting v4l2 input and video standard.
lucabe
parents: 1795
diff changeset
448
2078
17b7ebc3e1f9 Reindent code after last commit
lucabe
parents: 2077
diff changeset
449 av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set input_id: %d, input: %s\n",
17b7ebc3e1f9 Reindent code after last commit
lucabe
parents: 2077
diff changeset
450 ap->channel, input.name);
17b7ebc3e1f9 Reindent code after last commit
lucabe
parents: 2077
diff changeset
451 if(ioctl (s->fd, VIDIOC_S_INPUT, &input.index) < 0 ) {
17b7ebc3e1f9 Reindent code after last commit
lucabe
parents: 2077
diff changeset
452 av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set input(%d) failed\n",
17b7ebc3e1f9 Reindent code after last commit
lucabe
parents: 2077
diff changeset
453 ap->channel);
2274
b21c2af60bc9 Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents: 2221
diff changeset
454 return AVERROR(EIO);
2078
17b7ebc3e1f9 Reindent code after last commit
lucabe
parents: 2077
diff changeset
455 }
2077
4b07eb8709f2 Allow avoid setting the video standard and input when capturing v4l2 video.
lucabe
parents: 1961
diff changeset
456 }
1961
0bb1bfbaa031 Allow setting v4l2 input and video standard.
lucabe
parents: 1795
diff changeset
457
2077
4b07eb8709f2 Allow avoid setting the video standard and input when capturing v4l2 video.
lucabe
parents: 1961
diff changeset
458 if(ap->standard) {
2078
17b7ebc3e1f9 Reindent code after last commit
lucabe
parents: 2077
diff changeset
459 av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s\n",
17b7ebc3e1f9 Reindent code after last commit
lucabe
parents: 2077
diff changeset
460 ap->standard );
17b7ebc3e1f9 Reindent code after last commit
lucabe
parents: 2077
diff changeset
461 /* set tv standard */
17b7ebc3e1f9 Reindent code after last commit
lucabe
parents: 2077
diff changeset
462 memset (&standard, 0, sizeof (standard));
17b7ebc3e1f9 Reindent code after last commit
lucabe
parents: 2077
diff changeset
463 for(i=0;;i++) {
17b7ebc3e1f9 Reindent code after last commit
lucabe
parents: 2077
diff changeset
464 standard.index = i;
17b7ebc3e1f9 Reindent code after last commit
lucabe
parents: 2077
diff changeset
465 if (ioctl(s->fd, VIDIOC_ENUMSTD, &standard) < 0) {
17b7ebc3e1f9 Reindent code after last commit
lucabe
parents: 2077
diff changeset
466 av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n",
17b7ebc3e1f9 Reindent code after last commit
lucabe
parents: 2077
diff changeset
467 ap->standard);
2274
b21c2af60bc9 Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents: 2221
diff changeset
468 return AVERROR(EIO);
2078
17b7ebc3e1f9 Reindent code after last commit
lucabe
parents: 2077
diff changeset
469 }
17b7ebc3e1f9 Reindent code after last commit
lucabe
parents: 2077
diff changeset
470
17b7ebc3e1f9 Reindent code after last commit
lucabe
parents: 2077
diff changeset
471 if(!strcasecmp(standard.name, ap->standard)) {
17b7ebc3e1f9 Reindent code after last commit
lucabe
parents: 2077
diff changeset
472 break;
17b7ebc3e1f9 Reindent code after last commit
lucabe
parents: 2077
diff changeset
473 }
17b7ebc3e1f9 Reindent code after last commit
lucabe
parents: 2077
diff changeset
474 }
17b7ebc3e1f9 Reindent code after last commit
lucabe
parents: 2077
diff changeset
475
17b7ebc3e1f9 Reindent code after last commit
lucabe
parents: 2077
diff changeset
476 av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s, id: %"PRIu64"\n",
17b7ebc3e1f9 Reindent code after last commit
lucabe
parents: 2077
diff changeset
477 ap->standard, standard.id);
17b7ebc3e1f9 Reindent code after last commit
lucabe
parents: 2077
diff changeset
478 if (ioctl(s->fd, VIDIOC_S_STD, &standard.id) < 0) {
1961
0bb1bfbaa031 Allow setting v4l2 input and video standard.
lucabe
parents: 1795
diff changeset
479 av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n",
0bb1bfbaa031 Allow setting v4l2 input and video standard.
lucabe
parents: 1795
diff changeset
480 ap->standard);
2274
b21c2af60bc9 Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents: 2221
diff changeset
481 return AVERROR(EIO);
1961
0bb1bfbaa031 Allow setting v4l2 input and video standard.
lucabe
parents: 1795
diff changeset
482 }
2077
4b07eb8709f2 Allow avoid setting the video standard and input when capturing v4l2 video.
lucabe
parents: 1961
diff changeset
483 }
1961
0bb1bfbaa031 Allow setting v4l2 input and video standard.
lucabe
parents: 1795
diff changeset
484
0bb1bfbaa031 Allow setting v4l2 input and video standard.
lucabe
parents: 1795
diff changeset
485 return 0;
0bb1bfbaa031 Allow setting v4l2 input and video standard.
lucabe
parents: 1795
diff changeset
486 }
0bb1bfbaa031 Allow setting v4l2 input and video standard.
lucabe
parents: 1795
diff changeset
487
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
488 static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
547675092e98 video4linux2 input support
diego
parents:
diff changeset
489 {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
490 struct video_data *s = s1->priv_data;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
491 AVStream *st;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
492 int width, height;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
493 int res, frame_rate, frame_rate_base;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
494 uint32_t desired_format, capabilities;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
495
1003
2d57ce58f576 simplify AVFormatParameters NULL checks
michael
parents: 975
diff changeset
496 if (ap->width <= 0 || ap->height <= 0 || ap->time_base.den <= 0) {
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
497 av_log(s1, AV_LOG_ERROR, "Missing/Wrong parameters\n");
547675092e98 video4linux2 input support
diego
parents:
diff changeset
498
547675092e98 video4linux2 input support
diego
parents:
diff changeset
499 return -1;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
500 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
501
547675092e98 video4linux2 input support
diego
parents:
diff changeset
502 width = ap->width;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
503 height = ap->height;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
504 frame_rate = ap->time_base.den;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
505 frame_rate_base = ap->time_base.num;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
506
547675092e98 video4linux2 input support
diego
parents:
diff changeset
507 if((unsigned)width > 32767 || (unsigned)height > 32767) {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
508 av_log(s1, AV_LOG_ERROR, "Wrong size %dx%d\n", width, height);
547675092e98 video4linux2 input support
diego
parents:
diff changeset
509
547675092e98 video4linux2 input support
diego
parents:
diff changeset
510 return -1;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
511 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
512
547675092e98 video4linux2 input support
diego
parents:
diff changeset
513 st = av_new_stream(s1, 0);
547675092e98 video4linux2 input support
diego
parents:
diff changeset
514 if (!st) {
1787
eb16c64144ee This fixes error handling for BeOS, removing the need for some ifdefs.
mmu_man
parents: 1780
diff changeset
515 return AVERROR(ENOMEM);
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
516 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
517 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
547675092e98 video4linux2 input support
diego
parents:
diff changeset
518
547675092e98 video4linux2 input support
diego
parents:
diff changeset
519 s->width = width;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
520 s->height = height;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
521 s->frame_rate = frame_rate;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
522 s->frame_rate_base = frame_rate_base;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
523
547675092e98 video4linux2 input support
diego
parents:
diff changeset
524 capabilities = 0;
1795
62792a60f740 implement new grabbing interface, as described here:
gpoirier
parents: 1793
diff changeset
525 s->fd = device_open(s1, &capabilities);
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
526 if (s->fd < 0) {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
527 av_free(st);
547675092e98 video4linux2 input support
diego
parents:
diff changeset
528
2274
b21c2af60bc9 Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents: 2221
diff changeset
529 return AVERROR(EIO);
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
530 }
1013
4abfc175a5d7 Do not be too verbose
lucabe
parents: 1003
diff changeset
531 av_log(s1, AV_LOG_INFO, "[%d]Capabilities: %x\n", s->fd, capabilities);
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
532
547675092e98 video4linux2 input support
diego
parents:
diff changeset
533 desired_format = fmt_ff2v4l(ap->pix_fmt);
1780
5e6a9b3a9698 Pass a proper context to av_log()
lucabe
parents: 1760
diff changeset
534 if (desired_format == 0 || (device_init(s1, &width, &height, desired_format) < 0)) {
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
535 int i, done;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
536
547675092e98 video4linux2 input support
diego
parents:
diff changeset
537 done = 0; i = 0;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
538 while (!done) {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
539 desired_format = fmt_conversion_table[i].v4l2_fmt;
1780
5e6a9b3a9698 Pass a proper context to av_log()
lucabe
parents: 1760
diff changeset
540 if (device_init(s1, &width, &height, desired_format) < 0) {
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
541 desired_format = 0;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
542 i++;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
543 } else {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
544 done = 1;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
545 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
546 if (i == sizeof(fmt_conversion_table) / sizeof(struct fmt_map)) {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
547 done = 1;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
548 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
549 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
550 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
551 if (desired_format == 0) {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
552 av_log(s1, AV_LOG_ERROR, "Cannot find a proper format.\n");
547675092e98 video4linux2 input support
diego
parents:
diff changeset
553 close(s->fd);
547675092e98 video4linux2 input support
diego
parents:
diff changeset
554 av_free(st);
547675092e98 video4linux2 input support
diego
parents:
diff changeset
555
2274
b21c2af60bc9 Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents: 2221
diff changeset
556 return AVERROR(EIO);
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
557 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
558 s->frame_format = desired_format;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
559
1961
0bb1bfbaa031 Allow setting v4l2 input and video standard.
lucabe
parents: 1795
diff changeset
560 if( v4l2_set_parameters( s1, ap ) < 0 )
2274
b21c2af60bc9 Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents: 2221
diff changeset
561 return AVERROR(EIO);
1961
0bb1bfbaa031 Allow setting v4l2 input and video standard.
lucabe
parents: 1795
diff changeset
562
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
563 st->codec->pix_fmt = fmt_v4l2ff(desired_format);
547675092e98 video4linux2 input support
diego
parents:
diff changeset
564 s->frame_size = avpicture_get_size(st->codec->pix_fmt, width, height);
547675092e98 video4linux2 input support
diego
parents:
diff changeset
565 if (capabilities & V4L2_CAP_STREAMING) {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
566 s->io_method = io_mmap;
1780
5e6a9b3a9698 Pass a proper context to av_log()
lucabe
parents: 1760
diff changeset
567 res = mmap_init(s1);
1117
1291569071c6 Fix segfault
lucabe
parents: 1100
diff changeset
568 if (res == 0) {
1780
5e6a9b3a9698 Pass a proper context to av_log()
lucabe
parents: 1760
diff changeset
569 res = mmap_start(s1);
1117
1291569071c6 Fix segfault
lucabe
parents: 1100
diff changeset
570 }
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
571 } else {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
572 s->io_method = io_read;
1780
5e6a9b3a9698 Pass a proper context to av_log()
lucabe
parents: 1760
diff changeset
573 res = read_init(s1);
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
574 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
575 if (res < 0) {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
576 close(s->fd);
547675092e98 video4linux2 input support
diego
parents:
diff changeset
577 av_free(st);
547675092e98 video4linux2 input support
diego
parents:
diff changeset
578
2274
b21c2af60bc9 Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents: 2221
diff changeset
579 return AVERROR(EIO);
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
580 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
581 s->top_field_first = first_field(s->fd);
547675092e98 video4linux2 input support
diego
parents:
diff changeset
582
547675092e98 video4linux2 input support
diego
parents:
diff changeset
583 st->codec->codec_type = CODEC_TYPE_VIDEO;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
584 st->codec->codec_id = CODEC_ID_RAWVIDEO;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
585 st->codec->width = width;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
586 st->codec->height = height;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
587 st->codec->time_base.den = frame_rate;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
588 st->codec->time_base.num = frame_rate_base;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
589 st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
590
547675092e98 video4linux2 input support
diego
parents:
diff changeset
591 return 0;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
592 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
593
547675092e98 video4linux2 input support
diego
parents:
diff changeset
594 static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
547675092e98 video4linux2 input support
diego
parents:
diff changeset
595 {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
596 struct video_data *s = s1->priv_data;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
597 int res;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
598
1793
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
599 if (s->io_method == io_mmap) {
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
600 av_init_packet(pkt);
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
601 res = mmap_read_frame(s1, pkt);
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
602 } else if (s->io_method == io_read) {
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
603 if (av_new_packet(pkt, s->frame_size) < 0)
2274
b21c2af60bc9 Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents: 2221
diff changeset
604 return AVERROR(EIO);
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
605
1793
951d219ab67b Pass the v4l2 buffer into the AVPacket, instead of allocating a new buffer
lucabe
parents: 1787
diff changeset
606 res = read_frame(s1, pkt);
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
607 } else {
2274
b21c2af60bc9 Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents: 2221
diff changeset
608 return AVERROR(EIO);
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
609 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
610 if (res < 0) {
2274
b21c2af60bc9 Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents: 2221
diff changeset
611 return AVERROR(EIO);
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
612 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
613
547675092e98 video4linux2 input support
diego
parents:
diff changeset
614 if (s1->streams[0]->codec->coded_frame) {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
615 s1->streams[0]->codec->coded_frame->interlaced_frame = 1;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
616 s1->streams[0]->codec->coded_frame->top_field_first = s->top_field_first;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
617 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
618
547675092e98 video4linux2 input support
diego
parents:
diff changeset
619 return s->frame_size;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
620 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
621
547675092e98 video4linux2 input support
diego
parents:
diff changeset
622 static int v4l2_read_close(AVFormatContext *s1)
547675092e98 video4linux2 input support
diego
parents:
diff changeset
623 {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
624 struct video_data *s = s1->priv_data;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
625
547675092e98 video4linux2 input support
diego
parents:
diff changeset
626 if (s->io_method == io_mmap) {
547675092e98 video4linux2 input support
diego
parents:
diff changeset
627 mmap_close(s);
547675092e98 video4linux2 input support
diego
parents:
diff changeset
628 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
629
547675092e98 video4linux2 input support
diego
parents:
diff changeset
630 close(s->fd);
547675092e98 video4linux2 input support
diego
parents:
diff changeset
631 return 0;
547675092e98 video4linux2 input support
diego
parents:
diff changeset
632 }
547675092e98 video4linux2 input support
diego
parents:
diff changeset
633
1169
d18cc9a1fd02 allow individual selection of muxers and demuxers
mru
parents: 1167
diff changeset
634 AVInputFormat v4l2_demuxer = {
921
547675092e98 video4linux2 input support
diego
parents:
diff changeset
635 "video4linux2",
547675092e98 video4linux2 input support
diego
parents:
diff changeset
636 "video grab",
547675092e98 video4linux2 input support
diego
parents:
diff changeset
637 sizeof(struct video_data),
547675092e98 video4linux2 input support
diego
parents:
diff changeset
638 NULL,
547675092e98 video4linux2 input support
diego
parents:
diff changeset
639 v4l2_read_header,
547675092e98 video4linux2 input support
diego
parents:
diff changeset
640 v4l2_read_packet,
547675092e98 video4linux2 input support
diego
parents:
diff changeset
641 v4l2_read_close,
547675092e98 video4linux2 input support
diego
parents:
diff changeset
642 .flags = AVFMT_NOFILE,
547675092e98 video4linux2 input support
diego
parents:
diff changeset
643 };