695e13f088d9bd5fbaa0452ceab2da6c8e5d2519
[libav.git] / libavdevice / bktr.c
1 /*
2 * *BSD video grab interface
3 * Copyright (c) 2002 Steve O'Hara-Smith
4 * based on
5 * Linux video grab interface
6 * Copyright (c) 2000,2001 Gerard Lantau
7 * and
8 * simple_grab.c Copyright (c) 1999 Roger Hardiman
9 *
10 * This file is part of Libav.
11 *
12 * Libav is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 *
17 * Libav is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with Libav; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 */
26
27 #include "libavformat/avformat.h"
28 #include "libavformat/internal.h"
29 #include "libavutil/internal.h"
30 #include "libavutil/log.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/parseutils.h"
33 #include "libavutil/time.h"
34 #if HAVE_DEV_BKTR_IOCTL_METEOR_H && HAVE_DEV_BKTR_IOCTL_BT848_H
35 # include <dev/bktr/ioctl_meteor.h>
36 # include <dev/bktr/ioctl_bt848.h>
37 #elif HAVE_MACHINE_IOCTL_METEOR_H && HAVE_MACHINE_IOCTL_BT848_H
38 # include <machine/ioctl_meteor.h>
39 # include <machine/ioctl_bt848.h>
40 #elif HAVE_DEV_VIDEO_METEOR_IOCTL_METEOR_H && HAVE_DEV_VIDEO_BKTR_IOCTL_BT848_H
41 # include <dev/video/meteor/ioctl_meteor.h>
42 # include <dev/video/bktr/ioctl_bt848.h>
43 #elif HAVE_DEV_IC_BT8XX_H
44 # include <dev/ic/bt8xx.h>
45 #endif
46 #include <unistd.h>
47 #include <fcntl.h>
48 #include <sys/ioctl.h>
49 #include <sys/mman.h>
50 #include <sys/time.h>
51 #include <signal.h>
52 #include <stdint.h>
53
54 typedef struct VideoData {
55 AVClass *class;
56 int video_fd;
57 int tuner_fd;
58 int width, height;
59 uint64_t per_frame;
60 int standard;
61 char *video_size; /**< String describing video size, set by a private option. */
62 char *framerate; /**< Set by a private option. */
63 } VideoData;
64
65
66 #define PAL 1
67 #define PALBDGHI 1
68 #define NTSC 2
69 #define NTSCM 2
70 #define SECAM 3
71 #define PALN 4
72 #define PALM 5
73 #define NTSCJ 6
74
75 /* PAL is 768 x 576. NTSC is 640 x 480 */
76 #define PAL_HEIGHT 576
77 #define SECAM_HEIGHT 576
78 #define NTSC_HEIGHT 480
79
80 #ifndef VIDEO_FORMAT
81 #define VIDEO_FORMAT NTSC
82 #endif
83
84 static int bktr_dev[] = { METEOR_DEV0, METEOR_DEV1, METEOR_DEV2,
85 METEOR_DEV3, METEOR_DEV_SVIDEO };
86
87 uint8_t *video_buf;
88 size_t video_buf_size;
89 uint64_t last_frame_time;
90 volatile sig_atomic_t nsignals;
91
92
93 static void catchsignal(int signal)
94 {
95 nsignals++;
96 return;
97 }
98
99 static av_cold int bktr_init(const char *video_device, int width, int height,
100 int format, int *video_fd, int *tuner_fd, int idev, double frequency)
101 {
102 struct meteor_geomet geo;
103 int h_max;
104 long ioctl_frequency;
105 char *arg;
106 int c;
107 struct sigaction act = { 0 }, old;
108
109 if (idev < 0 || idev > 4)
110 {
111 arg = getenv ("BKTR_DEV");
112 if (arg)
113 idev = atoi (arg);
114 if (idev < 0 || idev > 4)
115 idev = 1;
116 }
117
118 if (format < 1 || format > 6)
119 {
120 arg = getenv ("BKTR_FORMAT");
121 if (arg)
122 format = atoi (arg);
123 if (format < 1 || format > 6)
124 format = VIDEO_FORMAT;
125 }
126
127 if (frequency <= 0)
128 {
129 arg = getenv ("BKTR_FREQUENCY");
130 if (arg)
131 frequency = atof (arg);
132 if (frequency <= 0)
133 frequency = 0.0;
134 }
135
136 sigemptyset(&act.sa_mask);
137 act.sa_handler = catchsignal;
138 sigaction(SIGUSR1, &act, &old);
139
140 *tuner_fd = avpriv_open("/dev/tuner0", O_RDONLY);
141 if (*tuner_fd < 0)
142 av_log(NULL, AV_LOG_ERROR, "Warning. Tuner not opened, continuing: %s\n", strerror(errno));
143
144 *video_fd = avpriv_open(video_device, O_RDONLY);
145 if (*video_fd < 0) {
146 av_log(NULL, AV_LOG_ERROR, "%s: %s\n", video_device, strerror(errno));
147 return -1;
148 }
149
150 geo.rows = height;
151 geo.columns = width;
152 geo.frames = 1;
153 geo.oformat = METEOR_GEO_YUV_422 | METEOR_GEO_YUV_12;
154
155 switch (format) {
156 case PAL: h_max = PAL_HEIGHT; c = BT848_IFORM_F_PALBDGHI; break;
157 case PALN: h_max = PAL_HEIGHT; c = BT848_IFORM_F_PALN; break;
158 case PALM: h_max = PAL_HEIGHT; c = BT848_IFORM_F_PALM; break;
159 case SECAM: h_max = SECAM_HEIGHT; c = BT848_IFORM_F_SECAM; break;
160 case NTSC: h_max = NTSC_HEIGHT; c = BT848_IFORM_F_NTSCM; break;
161 case NTSCJ: h_max = NTSC_HEIGHT; c = BT848_IFORM_F_NTSCJ; break;
162 default: h_max = PAL_HEIGHT; c = BT848_IFORM_F_PALBDGHI; break;
163 }
164
165 if (height <= h_max / 2)
166 geo.oformat |= METEOR_GEO_EVEN_ONLY;
167
168 if (ioctl(*video_fd, METEORSETGEO, &geo) < 0) {
169 av_log(NULL, AV_LOG_ERROR, "METEORSETGEO: %s\n", strerror(errno));
170 return -1;
171 }
172
173 if (ioctl(*video_fd, BT848SFMT, &c) < 0) {
174 av_log(NULL, AV_LOG_ERROR, "BT848SFMT: %s\n", strerror(errno));
175 return -1;
176 }
177
178 c = bktr_dev[idev];
179 if (ioctl(*video_fd, METEORSINPUT, &c) < 0) {
180 av_log(NULL, AV_LOG_ERROR, "METEORSINPUT: %s\n", strerror(errno));
181 return -1;
182 }
183
184 video_buf_size = width * height * 12 / 8;
185
186 video_buf = (uint8_t *)mmap((caddr_t)0, video_buf_size,
187 PROT_READ, MAP_SHARED, *video_fd, (off_t)0);
188 if (video_buf == MAP_FAILED) {
189 av_log(NULL, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
190 return -1;
191 }
192
193 if (frequency != 0.0) {
194 ioctl_frequency = (unsigned long)(frequency*16);
195 if (ioctl(*tuner_fd, TVTUNER_SETFREQ, &ioctl_frequency) < 0)
196 av_log(NULL, AV_LOG_ERROR, "TVTUNER_SETFREQ: %s\n", strerror(errno));
197 }
198
199 c = AUDIO_UNMUTE;
200 if (ioctl(*tuner_fd, BT848_SAUDIO, &c) < 0)
201 av_log(NULL, AV_LOG_ERROR, "TVTUNER_SAUDIO: %s\n", strerror(errno));
202
203 c = METEOR_CAP_CONTINOUS;
204 ioctl(*video_fd, METEORCAPTUR, &c);
205
206 c = SIGUSR1;
207 ioctl(*video_fd, METEORSSIGNAL, &c);
208
209 return 0;
210 }
211
212 static void bktr_getframe(uint64_t per_frame)
213 {
214 uint64_t curtime;
215
216 curtime = av_gettime();
217 if (!last_frame_time
218 || ((last_frame_time + per_frame) > curtime)) {
219 if (!usleep(last_frame_time + per_frame + per_frame / 8 - curtime)) {
220 if (!nsignals)
221 av_log(NULL, AV_LOG_INFO,
222 "SLEPT NO signals - %d microseconds late\n",
223 (int)(av_gettime() - last_frame_time - per_frame));
224 }
225 }
226 nsignals = 0;
227 last_frame_time = curtime;
228 }
229
230
231 /* note: we support only one picture read at a time */
232 static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
233 {
234 VideoData *s = s1->priv_data;
235
236 if (av_new_packet(pkt, video_buf_size) < 0)
237 return AVERROR(EIO);
238
239 bktr_getframe(s->per_frame);
240
241 pkt->pts = av_gettime();
242 memcpy(pkt->data, video_buf, video_buf_size);
243
244 return video_buf_size;
245 }
246
247 static int grab_read_header(AVFormatContext *s1)
248 {
249 VideoData *s = s1->priv_data;
250 AVStream *st;
251 int width, height;
252 AVRational framerate;
253 int ret = 0;
254
255 if ((ret = av_parse_video_size(&width, &height, s->video_size)) < 0) {
256 av_log(s1, AV_LOG_ERROR, "Could not parse video size '%s'.\n", s->video_size);
257 goto out;
258 }
259
260 if (!s->framerate)
261 switch (s->standard) {
262 case PAL: s->framerate = av_strdup("pal"); break;
263 case NTSC: s->framerate = av_strdup("ntsc"); break;
264 case SECAM: s->framerate = av_strdup("25"); break;
265 default:
266 av_log(s1, AV_LOG_ERROR, "Unknown standard.\n");
267 ret = AVERROR(EINVAL);
268 goto out;
269 }
270 if ((ret = av_parse_video_rate(&framerate, s->framerate)) < 0) {
271 av_log(s1, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", s->framerate);
272 goto out;
273 }
274
275 st = avformat_new_stream(s1, NULL);
276 if (!st) {
277 ret = AVERROR(ENOMEM);
278 goto out;
279 }
280 avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in use */
281
282 s->width = width;
283 s->height = height;
284 s->per_frame = ((uint64_t)1000000 * framerate.den) / framerate.num;
285
286 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
287 st->codec->pix_fmt = AV_PIX_FMT_YUV420P;
288 st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
289 st->codec->width = width;
290 st->codec->height = height;
291 st->codec->time_base.den = framerate.num;
292 st->codec->time_base.num = framerate.den;
293
294
295 if (bktr_init(s1->filename, width, height, s->standard,
296 &s->video_fd, &s->tuner_fd, -1, 0.0) < 0) {
297 ret = AVERROR(EIO);
298 goto out;
299 }
300
301 nsignals = 0;
302 last_frame_time = 0;
303
304 out:
305 return ret;
306 }
307
308 static int grab_read_close(AVFormatContext *s1)
309 {
310 VideoData *s = s1->priv_data;
311 int c;
312
313 c = METEOR_CAP_STOP_CONT;
314 ioctl(s->video_fd, METEORCAPTUR, &c);
315 close(s->video_fd);
316
317 c = AUDIO_MUTE;
318 ioctl(s->tuner_fd, BT848_SAUDIO, &c);
319 close(s->tuner_fd);
320
321 munmap((caddr_t)video_buf, video_buf_size);
322
323 return 0;
324 }
325
326 #define OFFSET(x) offsetof(VideoData, x)
327 #define DEC AV_OPT_FLAG_DECODING_PARAM
328 static const AVOption options[] = {
329 { "standard", "", offsetof(VideoData, standard), AV_OPT_TYPE_INT, {.i64 = VIDEO_FORMAT}, PAL, NTSCJ, AV_OPT_FLAG_DECODING_PARAM, "standard" },
330 { "PAL", "", 0, AV_OPT_TYPE_CONST, {.i64 = PAL}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
331 { "NTSC", "", 0, AV_OPT_TYPE_CONST, {.i64 = NTSC}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
332 { "SECAM", "", 0, AV_OPT_TYPE_CONST, {.i64 = SECAM}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
333 { "PALN", "", 0, AV_OPT_TYPE_CONST, {.i64 = PALN}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
334 { "PALM", "", 0, AV_OPT_TYPE_CONST, {.i64 = PALM}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
335 { "NTSCJ", "", 0, AV_OPT_TYPE_CONST, {.i64 = NTSCJ}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
336 { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = "vga"}, 0, 0, DEC },
337 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
338 { NULL },
339 };
340
341 static const AVClass bktr_class = {
342 .class_name = "BKTR grab interface",
343 .item_name = av_default_item_name,
344 .option = options,
345 .version = LIBAVUTIL_VERSION_INT,
346 };
347
348 AVInputFormat ff_bktr_demuxer = {
349 .name = "bktr",
350 .long_name = NULL_IF_CONFIG_SMALL("video grab"),
351 .priv_data_size = sizeof(VideoData),
352 .read_header = grab_read_header,
353 .read_packet = grab_read_packet,
354 .read_close = grab_read_close,
355 .flags = AVFMT_NOFILE,
356 .priv_class = &bktr_class,
357 };