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