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 "libavformat/internal.h"
00028 #include "libavutil/log.h"
00029 #include "libavutil/opt.h"
00030 #include "libavutil/parseutils.h"
00031 #if HAVE_DEV_BKTR_IOCTL_METEOR_H && HAVE_DEV_BKTR_IOCTL_BT848_H
00032 # include <dev/bktr/ioctl_meteor.h>
00033 # include <dev/bktr/ioctl_bt848.h>
00034 #elif HAVE_MACHINE_IOCTL_METEOR_H && HAVE_MACHINE_IOCTL_BT848_H
00035 # include <machine/ioctl_meteor.h>
00036 # include <machine/ioctl_bt848.h>
00037 #elif HAVE_DEV_VIDEO_METEOR_IOCTL_METEOR_H && HAVE_DEV_VIDEO_BKTR_IOCTL_BT848_H
00038 # include <dev/video/meteor/ioctl_meteor.h>
00039 # include <dev/video/bktr/ioctl_bt848.h>
00040 #elif HAVE_DEV_IC_BT8XX_H
00041 # include <dev/ic/bt8xx.h>
00042 #endif
00043 #include <unistd.h>
00044 #include <fcntl.h>
00045 #include <sys/ioctl.h>
00046 #include <sys/mman.h>
00047 #include <sys/time.h>
00048 #include <signal.h>
00049 #include <stdint.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 framerate;
00252 int ret = 0;
00253
00254 if ((ret = av_parse_video_size(&width, &height, s->video_size)) < 0) {
00255 av_log(s1, AV_LOG_ERROR, "Could not parse video size '%s'.\n", s->video_size);
00256 goto out;
00257 }
00258
00259 if (!s->framerate)
00260 switch (s->standard) {
00261 case PAL: s->framerate = av_strdup("pal"); break;
00262 case NTSC: s->framerate = av_strdup("ntsc"); break;
00263 case SECAM: s->framerate = av_strdup("25"); break;
00264 default:
00265 av_log(s1, AV_LOG_ERROR, "Unknown standard.\n");
00266 ret = AVERROR(EINVAL);
00267 goto out;
00268 }
00269 if ((ret = av_parse_video_rate(&framerate, s->framerate)) < 0) {
00270 av_log(s1, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", s->framerate);
00271 goto out;
00272 }
00273
00274 st = avformat_new_stream(s1, NULL);
00275 if (!st) {
00276 ret = AVERROR(ENOMEM);
00277 goto out;
00278 }
00279 avpriv_set_pts_info(st, 64, 1, 1000000);
00280
00281 s->width = width;
00282 s->height = height;
00283 s->per_frame = ((uint64_t)1000000 * framerate.den) / framerate.num;
00284
00285 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00286 st->codec->pix_fmt = PIX_FMT_YUV420P;
00287 st->codec->codec_id = CODEC_ID_RAWVIDEO;
00288 st->codec->width = width;
00289 st->codec->height = height;
00290 st->codec->time_base.den = framerate.num;
00291 st->codec->time_base.num = framerate.den;
00292
00293
00294 if (bktr_init(s1->filename, width, height, s->standard,
00295 &(s->video_fd), &(s->tuner_fd), -1, 0.0) < 0) {
00296 ret = AVERROR(EIO);
00297 goto out;
00298 }
00299
00300 nsignals = 0;
00301 last_frame_time = 0;
00302
00303 out:
00304 return ret;
00305 }
00306
00307 static int grab_read_close(AVFormatContext *s1)
00308 {
00309 VideoData *s = s1->priv_data;
00310 int c;
00311
00312 c = METEOR_CAP_STOP_CONT;
00313 ioctl(s->video_fd, METEORCAPTUR, &c);
00314 close(s->video_fd);
00315
00316 c = AUDIO_MUTE;
00317 ioctl(s->tuner_fd, BT848_SAUDIO, &c);
00318 close(s->tuner_fd);
00319
00320 munmap((caddr_t)video_buf, video_buf_size);
00321
00322 return 0;
00323 }
00324
00325 #define OFFSET(x) offsetof(VideoData, x)
00326 #define DEC AV_OPT_FLAG_DECODING_PARAM
00327 static const AVOption options[] = {
00328 { "standard", "", offsetof(VideoData, standard), AV_OPT_TYPE_INT, {.dbl = VIDEO_FORMAT}, PAL, NTSCJ, AV_OPT_FLAG_DECODING_PARAM, "standard" },
00329 { "PAL", "", 0, AV_OPT_TYPE_CONST, {.dbl = PAL}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
00330 { "NTSC", "", 0, AV_OPT_TYPE_CONST, {.dbl = NTSC}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
00331 { "SECAM", "", 0, AV_OPT_TYPE_CONST, {.dbl = SECAM}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
00332 { "PALN", "", 0, AV_OPT_TYPE_CONST, {.dbl = PALN}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
00333 { "PALM", "", 0, AV_OPT_TYPE_CONST, {.dbl = PALM}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
00334 { "NTSCJ", "", 0, AV_OPT_TYPE_CONST, {.dbl = NTSCJ}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
00335 { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = "vga"}, 0, 0, DEC },
00336 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
00337 { NULL },
00338 };
00339
00340 static const AVClass bktr_class = {
00341 .class_name = "BKTR grab interface",
00342 .item_name = av_default_item_name,
00343 .option = options,
00344 .version = LIBAVUTIL_VERSION_INT,
00345 };
00346
00347 AVInputFormat ff_bktr_demuxer = {
00348 .name = "bktr",
00349 .long_name = NULL_IF_CONFIG_SMALL("video grab"),
00350 .priv_data_size = sizeof(VideoData),
00351 .read_header = grab_read_header,
00352 .read_packet = grab_read_packet,
00353 .read_close = grab_read_close,
00354 .flags = AVFMT_NOFILE,
00355 .priv_class = &bktr_class,
00356 };