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