annotate src/ffmpeg/libavformat/v4l2.c @ 823:a195f1259a6b trunk

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