annotate v4l2.c @ 972:d983c2b9333e libavformat

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