00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "libavutil/log.h"
00028 #include "libavutil/opt.h"
00029 #include "libavutil/parseutils.h"
00030 #if HAVE_DEV_BKTR_IOCTL_METEOR_H && HAVE_DEV_BKTR_IOCTL_BT848_H
00031 # include <dev/bktr/ioctl_meteor.h>
00032 # include <dev/bktr/ioctl_bt848.h>
00033 #elif HAVE_MACHINE_IOCTL_METEOR_H && HAVE_MACHINE_IOCTL_BT848_H
00034 # include <machine/ioctl_meteor.h>
00035 # include <machine/ioctl_bt848.h>
00036 #elif HAVE_DEV_VIDEO_METEOR_IOCTL_METEOR_H && HAVE_DEV_VIDEO_BKTR_IOCTL_BT848_H
00037 # include <dev/video/meteor/ioctl_meteor.h>
00038 # include <dev/video/bktr/ioctl_bt848.h>
00039 #elif HAVE_DEV_IC_BT8XX_H
00040 # include <dev/ic/bt8xx.h>
00041 #endif
00042 #include <unistd.h>
00043 #include <fcntl.h>
00044 #include <sys/ioctl.h>
00045 #include <sys/mman.h>
00046 #include <sys/time.h>
00047 #include <signal.h>
00048 #include <stdint.h>
00049 #include <strings.h>
00050 #include "avdevice.h"
00051
00052 typedef struct {
00053 AVClass *class;
00054 int video_fd;
00055 int tuner_fd;
00056 int width, height;
00057 uint64_t per_frame;
00058 int standard;
00059 char *video_size;
00060 char *framerate;
00061 } VideoData;
00062
00063
00064 #define PAL 1
00065 #define PALBDGHI 1
00066 #define NTSC 2
00067 #define NTSCM 2
00068 #define SECAM 3
00069 #define PALN 4
00070 #define PALM 5
00071 #define NTSCJ 6
00072
00073
00074 #define PAL_HEIGHT 576
00075 #define SECAM_HEIGHT 576
00076 #define NTSC_HEIGHT 480
00077
00078 #ifndef VIDEO_FORMAT
00079 #define VIDEO_FORMAT NTSC
00080 #endif
00081
00082 static int bktr_dev[] = { METEOR_DEV0, METEOR_DEV1, METEOR_DEV2,
00083 METEOR_DEV3, METEOR_DEV_SVIDEO };
00084
00085 uint8_t *video_buf;
00086 size_t video_buf_size;
00087 uint64_t last_frame_time;
00088 volatile sig_atomic_t nsignals;
00089
00090
00091 static void catchsignal(int signal)
00092 {
00093 nsignals++;
00094 return;
00095 }
00096
00097 static av_cold int bktr_init(const char *video_device, int width, int height,
00098 int format, int *video_fd, int *tuner_fd, int idev, double frequency)
00099 {
00100 struct meteor_geomet geo;
00101 int h_max;
00102 long ioctl_frequency;
00103 char *arg;
00104 int c;
00105 struct sigaction act, old;
00106
00107 if (idev < 0 || idev > 4)
00108 {
00109 arg = getenv ("BKTR_DEV");
00110 if (arg)
00111 idev = atoi (arg);
00112 if (idev < 0 || idev > 4)
00113 idev = 1;
00114 }
00115
00116 if (format < 1 || format > 6)
00117 {
00118 arg = getenv ("BKTR_FORMAT");
00119 if (arg)
00120 format = atoi (arg);
00121 if (format < 1 || format > 6)
00122 format = VIDEO_FORMAT;
00123 }
00124
00125 if (frequency <= 0)
00126 {
00127 arg = getenv ("BKTR_FREQUENCY");
00128 if (arg)
00129 frequency = atof (arg);
00130 if (frequency <= 0)
00131 frequency = 0.0;
00132 }
00133
00134 memset(&act, 0, sizeof(act));
00135 sigemptyset(&act.sa_mask);
00136 act.sa_handler = catchsignal;
00137 sigaction(SIGUSR1, &act, &old);
00138
00139 *tuner_fd = open("/dev/tuner0", O_RDONLY);
00140 if (*tuner_fd < 0)
00141 av_log(NULL, AV_LOG_ERROR, "Warning. Tuner not opened, continuing: %s\n", strerror(errno));
00142
00143 *video_fd = open(video_device, O_RDONLY);
00144 if (*video_fd < 0) {
00145 av_log(NULL, AV_LOG_ERROR, "%s: %s\n", video_device, strerror(errno));
00146 return -1;
00147 }
00148
00149 geo.rows = height;
00150 geo.columns = width;
00151 geo.frames = 1;
00152 geo.oformat = METEOR_GEO_YUV_422 | METEOR_GEO_YUV_12;
00153
00154 switch (format) {
00155 case PAL: h_max = PAL_HEIGHT; c = BT848_IFORM_F_PALBDGHI; break;
00156 case PALN: h_max = PAL_HEIGHT; c = BT848_IFORM_F_PALN; break;
00157 case PALM: h_max = PAL_HEIGHT; c = BT848_IFORM_F_PALM; break;
00158 case SECAM: h_max = SECAM_HEIGHT; c = BT848_IFORM_F_SECAM; break;
00159 case NTSC: h_max = NTSC_HEIGHT; c = BT848_IFORM_F_NTSCM; break;
00160 case NTSCJ: h_max = NTSC_HEIGHT; c = BT848_IFORM_F_NTSCJ; break;
00161 default: h_max = PAL_HEIGHT; c = BT848_IFORM_F_PALBDGHI; break;
00162 }
00163
00164 if (height <= h_max / 2)
00165 geo.oformat |= METEOR_GEO_EVEN_ONLY;
00166
00167 if (ioctl(*video_fd, METEORSETGEO, &geo) < 0) {
00168 av_log(NULL, AV_LOG_ERROR, "METEORSETGEO: %s\n", strerror(errno));
00169 return -1;
00170 }
00171
00172 if (ioctl(*video_fd, BT848SFMT, &c) < 0) {
00173 av_log(NULL, AV_LOG_ERROR, "BT848SFMT: %s\n", strerror(errno));
00174 return -1;
00175 }
00176
00177 c = bktr_dev[idev];
00178 if (ioctl(*video_fd, METEORSINPUT, &c) < 0) {
00179 av_log(NULL, AV_LOG_ERROR, "METEORSINPUT: %s\n", strerror(errno));
00180 return -1;
00181 }
00182
00183 video_buf_size = width * height * 12 / 8;
00184
00185 video_buf = (uint8_t *)mmap((caddr_t)0, video_buf_size,
00186 PROT_READ, MAP_SHARED, *video_fd, (off_t)0);
00187 if (video_buf == MAP_FAILED) {
00188 av_log(NULL, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
00189 return -1;
00190 }
00191
00192 if (frequency != 0.0) {
00193 ioctl_frequency = (unsigned long)(frequency*16);
00194 if (ioctl(*tuner_fd, TVTUNER_SETFREQ, &ioctl_frequency) < 0)
00195 av_log(NULL, AV_LOG_ERROR, "TVTUNER_SETFREQ: %s\n", strerror(errno));
00196 }
00197
00198 c = AUDIO_UNMUTE;
00199 if (ioctl(*tuner_fd, BT848_SAUDIO, &c) < 0)
00200 av_log(NULL, AV_LOG_ERROR, "TVTUNER_SAUDIO: %s\n", strerror(errno));
00201
00202 c = METEOR_CAP_CONTINOUS;
00203 ioctl(*video_fd, METEORCAPTUR, &c);
00204
00205 c = SIGUSR1;
00206 ioctl(*video_fd, METEORSSIGNAL, &c);
00207
00208 return 0;
00209 }
00210
00211 static void bktr_getframe(uint64_t per_frame)
00212 {
00213 uint64_t curtime;
00214
00215 curtime = av_gettime();
00216 if (!last_frame_time
00217 || ((last_frame_time + per_frame) > curtime)) {
00218 if (!usleep(last_frame_time + per_frame + per_frame / 8 - curtime)) {
00219 if (!nsignals)
00220 av_log(NULL, AV_LOG_INFO,
00221 "SLEPT NO signals - %d microseconds late\n",
00222 (int)(av_gettime() - last_frame_time - per_frame));
00223 }
00224 }
00225 nsignals = 0;
00226 last_frame_time = curtime;
00227 }
00228
00229
00230
00231 static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
00232 {
00233 VideoData *s = s1->priv_data;
00234
00235 if (av_new_packet(pkt, video_buf_size) < 0)
00236 return AVERROR(EIO);
00237
00238 bktr_getframe(s->per_frame);
00239
00240 pkt->pts = av_gettime();
00241 memcpy(pkt->data, video_buf, video_buf_size);
00242
00243 return video_buf_size;
00244 }
00245
00246 static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
00247 {
00248 VideoData *s = s1->priv_data;
00249 AVStream *st;
00250 int width, height;
00251 AVRational fps;
00252 int ret = 0;
00253
00254 #if FF_API_FORMAT_PARAMETERS
00255 if (ap->standard) {
00256 if (!strcasecmp(ap->standard, "pal"))
00257 s->standard = PAL;
00258 else if (!strcasecmp(ap->standard, "secam"))
00259 s->standard = SECAM;
00260 else if (!strcasecmp(ap->standard, "ntsc"))
00261 s->standard = NTSC;
00262 }
00263 #endif
00264
00265 if ((ret = av_parse_video_size(&width, &height, s->video_size)) < 0) {
00266 av_log(s1, AV_LOG_ERROR, "Couldn't parse video size.\n");
00267 goto out;
00268 }
00269
00270 if (!s->framerate)
00271 switch (s->standard) {
00272 case PAL: s->framerate = av_strdup("pal"); break;
00273 case NTSC: s->framerate = av_strdup("ntsc"); break;
00274 case SECAM: s->framerate = av_strdup("25"); break;
00275 default:
00276 av_log(s1, AV_LOG_ERROR, "Unknown standard.\n");
00277 ret = AVERROR(EINVAL);
00278 goto out;
00279 }
00280 if ((ret = av_parse_video_rate(&fps, s->framerate)) < 0) {
00281 av_log(s1, AV_LOG_ERROR, "Couldn't parse framerate.\n");
00282 goto out;
00283 }
00284 #if FF_API_FORMAT_PARAMETERS
00285 if (ap->width > 0)
00286 width = ap->width;
00287 if (ap->height > 0)
00288 height = ap->height;
00289 if (ap->time_base.num)
00290 fps = (AVRational){ap->time_base.den, ap->time_base.num};
00291 #endif
00292
00293 st = av_new_stream(s1, 0);
00294 if (!st) {
00295 ret = AVERROR(ENOMEM);
00296 goto out;
00297 }
00298 av_set_pts_info(st, 64, 1, 1000000);
00299
00300 s->width = width;
00301 s->height = height;
00302 s->per_frame = ((uint64_t)1000000 * fps.den) / fps.num;
00303
00304 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00305 st->codec->pix_fmt = PIX_FMT_YUV420P;
00306 st->codec->codec_id = CODEC_ID_RAWVIDEO;
00307 st->codec->width = width;
00308 st->codec->height = height;
00309 st->codec->time_base.den = fps.num;
00310 st->codec->time_base.num = fps.den;
00311
00312
00313 if (bktr_init(s1->filename, width, height, s->standard,
00314 &(s->video_fd), &(s->tuner_fd), -1, 0.0) < 0) {
00315 ret = AVERROR(EIO);
00316 goto out;
00317 }
00318
00319 nsignals = 0;
00320 last_frame_time = 0;
00321
00322 out:
00323 return ret;
00324 }
00325
00326 static int grab_read_close(AVFormatContext *s1)
00327 {
00328 VideoData *s = s1->priv_data;
00329 int c;
00330
00331 c = METEOR_CAP_STOP_CONT;
00332 ioctl(s->video_fd, METEORCAPTUR, &c);
00333 close(s->video_fd);
00334
00335 c = AUDIO_MUTE;
00336 ioctl(s->tuner_fd, BT848_SAUDIO, &c);
00337 close(s->tuner_fd);
00338
00339 munmap((caddr_t)video_buf, video_buf_size);
00340
00341 return 0;
00342 }
00343
00344 #define OFFSET(x) offsetof(VideoData, x)
00345 #define DEC AV_OPT_FLAG_DECODING_PARAM
00346 static const AVOption options[] = {
00347 { "standard", "", offsetof(VideoData, standard), FF_OPT_TYPE_INT, {.dbl = VIDEO_FORMAT}, PAL, NTSCJ, AV_OPT_FLAG_DECODING_PARAM, "standard" },
00348 { "PAL", "", 0, FF_OPT_TYPE_CONST, {.dbl = PAL}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
00349 { "NTSC", "", 0, FF_OPT_TYPE_CONST, {.dbl = NTSC}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
00350 { "SECAM", "", 0, FF_OPT_TYPE_CONST, {.dbl = SECAM}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
00351 { "PALN", "", 0, FF_OPT_TYPE_CONST, {.dbl = PALN}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
00352 { "PALM", "", 0, FF_OPT_TYPE_CONST, {.dbl = PALM}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
00353 { "NTSCJ", "", 0, FF_OPT_TYPE_CONST, {.dbl = NTSCJ}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
00354 { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = "vga"}, 0, 0, DEC },
00355 { "framerate", "", OFFSET(framerate), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
00356 { NULL },
00357 };
00358
00359 static const AVClass bktr_class = {
00360 .class_name = "BKTR grab interface",
00361 .item_name = av_default_item_name,
00362 .option = options,
00363 .version = LIBAVUTIL_VERSION_INT,
00364 };
00365
00366 AVInputFormat ff_bktr_demuxer = {
00367 "bktr",
00368 NULL_IF_CONFIG_SMALL("video grab"),
00369 sizeof(VideoData),
00370 NULL,
00371 grab_read_header,
00372 grab_read_packet,
00373 grab_read_close,
00374 .flags = AVFMT_NOFILE,
00375 .priv_class = &bktr_class,
00376 };