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 #include "avformat.h"
00027 #if defined (HAVE_DEV_BKTR_IOCTL_METEOR_H) && defined (HAVE_DEV_BKTR_IOCTL_BT848_H)
00028 # include <dev/bktr/ioctl_meteor.h>
00029 # include <dev/bktr/ioctl_bt848.h>
00030 #elif defined (HAVE_MACHINE_IOCTL_METEOR_H) && defined (HAVE_MACHINE_IOCTL_BT848_H)
00031 # include <machine/ioctl_meteor.h>
00032 # include <machine/ioctl_bt848.h>
00033 #elif defined (HAVE_DEV_VIDEO_METEOR_IOCTL_METEOR_H) && defined (HAVE_DEV_VIDEO_METEOR_IOCTL_BT848_H)
00034 # include <dev/video/meteor/ioctl_meteor.h>
00035 # include <dev/video/bktr/ioctl_bt848.h>
00036 #elif HAVE_DEV_IC_BT8XX_H
00037 # include <dev/ic/bt8xx.h>
00038 #endif
00039 #include <unistd.h>
00040 #include <fcntl.h>
00041 #include <sys/ioctl.h>
00042 #include <sys/mman.h>
00043 #include <sys/time.h>
00044 #include <signal.h>
00045
00046 typedef struct {
00047 int video_fd;
00048 int tuner_fd;
00049 int width, height;
00050 int frame_rate;
00051 int frame_rate_base;
00052 u_int64_t per_frame;
00053 } VideoData;
00054
00055
00056 #define PAL 1
00057 #define PALBDGHI 1
00058 #define NTSC 2
00059 #define NTSCM 2
00060 #define SECAM 3
00061 #define PALN 4
00062 #define PALM 5
00063 #define NTSCJ 6
00064
00065
00066 #define PAL_HEIGHT 576
00067 #define SECAM_HEIGHT 576
00068 #define NTSC_HEIGHT 480
00069
00070 #ifndef VIDEO_FORMAT
00071 #define VIDEO_FORMAT NTSC
00072 #endif
00073
00074 static int bktr_dev[] = { METEOR_DEV0, METEOR_DEV1, METEOR_DEV2,
00075 METEOR_DEV3, METEOR_DEV_SVIDEO };
00076
00077 uint8_t *video_buf;
00078 size_t video_buf_size;
00079 u_int64_t last_frame_time;
00080 volatile sig_atomic_t nsignals;
00081
00082
00083 static void catchsignal(int signal)
00084 {
00085 nsignals++;
00086 return;
00087 }
00088
00089 static int bktr_init(const char *video_device, int width, int height,
00090 int format, int *video_fd, int *tuner_fd, int idev, double frequency)
00091 {
00092 struct meteor_geomet geo;
00093 int h_max;
00094 long ioctl_frequency;
00095 char *arg;
00096 int c;
00097 struct sigaction act, old;
00098
00099 if (idev < 0 || idev > 4)
00100 {
00101 arg = getenv ("BKTR_DEV");
00102 if (arg)
00103 idev = atoi (arg);
00104 if (idev < 0 || idev > 4)
00105 idev = 1;
00106 }
00107
00108 if (format < 1 || format > 6)
00109 {
00110 arg = getenv ("BKTR_FORMAT");
00111 if (arg)
00112 format = atoi (arg);
00113 if (format < 1 || format > 6)
00114 format = VIDEO_FORMAT;
00115 }
00116
00117 if (frequency <= 0)
00118 {
00119 arg = getenv ("BKTR_FREQUENCY");
00120 if (arg)
00121 frequency = atof (arg);
00122 if (frequency <= 0)
00123 frequency = 0.0;
00124 }
00125
00126 memset(&act, 0, sizeof(act));
00127 sigemptyset(&act.sa_mask);
00128 act.sa_handler = catchsignal;
00129 sigaction(SIGUSR1, &act, &old);
00130
00131 *tuner_fd = open("/dev/tuner0", O_RDONLY);
00132 if (*tuner_fd < 0)
00133 av_log(NULL, AV_LOG_ERROR, "Warning. Tuner not opened, continuing: %s\n", strerror(errno));
00134
00135 *video_fd = open(video_device, O_RDONLY);
00136 if (*video_fd < 0) {
00137 av_log(NULL, AV_LOG_ERROR, "%s: %s\n", video_device, strerror(errno));
00138 return -1;
00139 }
00140
00141 geo.rows = height;
00142 geo.columns = width;
00143 geo.frames = 1;
00144 geo.oformat = METEOR_GEO_YUV_422 | METEOR_GEO_YUV_12;
00145
00146 switch (format) {
00147 case PAL: h_max = PAL_HEIGHT; c = BT848_IFORM_F_PALBDGHI; break;
00148 case PALN: h_max = PAL_HEIGHT; c = BT848_IFORM_F_PALN; break;
00149 case PALM: h_max = PAL_HEIGHT; c = BT848_IFORM_F_PALM; break;
00150 case SECAM: h_max = SECAM_HEIGHT; c = BT848_IFORM_F_SECAM; break;
00151 case NTSC: h_max = NTSC_HEIGHT; c = BT848_IFORM_F_NTSCM; break;
00152 case NTSCJ: h_max = NTSC_HEIGHT; c = BT848_IFORM_F_NTSCJ; break;
00153 default: h_max = PAL_HEIGHT; c = BT848_IFORM_F_PALBDGHI; break;
00154 }
00155
00156 if (height <= h_max / 2)
00157 geo.oformat |= METEOR_GEO_EVEN_ONLY;
00158
00159 if (ioctl(*video_fd, METEORSETGEO, &geo) < 0) {
00160 av_log(NULL, AV_LOG_ERROR, "METEORSETGEO: %s\n", strerror(errno));
00161 return -1;
00162 }
00163
00164 if (ioctl(*video_fd, BT848SFMT, &c) < 0) {
00165 av_log(NULL, AV_LOG_ERROR, "BT848SFMT: %s\n", strerror(errno));
00166 return -1;
00167 }
00168
00169 c = bktr_dev[idev];
00170 if (ioctl(*video_fd, METEORSINPUT, &c) < 0) {
00171 av_log(NULL, AV_LOG_ERROR, "METEORSINPUT: %s\n", strerror(errno));
00172 return -1;
00173 }
00174
00175 video_buf_size = width * height * 12 / 8;
00176
00177 video_buf = (uint8_t *)mmap((caddr_t)0, video_buf_size,
00178 PROT_READ, MAP_SHARED, *video_fd, (off_t)0);
00179 if (video_buf == MAP_FAILED) {
00180 av_log(NULL, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
00181 return -1;
00182 }
00183
00184 if (frequency != 0.0) {
00185 ioctl_frequency = (unsigned long)(frequency*16);
00186 if (ioctl(*tuner_fd, TVTUNER_SETFREQ, &ioctl_frequency) < 0)
00187 av_log(NULL, AV_LOG_ERROR, "TVTUNER_SETFREQ: %s\n", strerror(errno));
00188 }
00189
00190 c = AUDIO_UNMUTE;
00191 if (ioctl(*tuner_fd, BT848_SAUDIO, &c) < 0)
00192 av_log(NULL, AV_LOG_ERROR, "TVTUNER_SAUDIO: %s\n", strerror(errno));
00193
00194 c = METEOR_CAP_CONTINOUS;
00195 ioctl(*video_fd, METEORCAPTUR, &c);
00196
00197 c = SIGUSR1;
00198 ioctl(*video_fd, METEORSSIGNAL, &c);
00199
00200 return 0;
00201 }
00202
00203 static void bktr_getframe(u_int64_t per_frame)
00204 {
00205 u_int64_t curtime;
00206
00207 curtime = av_gettime();
00208 if (!last_frame_time
00209 || ((last_frame_time + per_frame) > curtime)) {
00210 if (!usleep(last_frame_time + per_frame + per_frame / 8 - curtime)) {
00211 if (!nsignals)
00212 av_log(NULL, AV_LOG_INFO,
00213 "SLEPT NO signals - %d microseconds late\n",
00214 (int)(av_gettime() - last_frame_time - per_frame));
00215 }
00216 }
00217 nsignals = 0;
00218 last_frame_time = curtime;
00219 }
00220
00221
00222
00223 static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
00224 {
00225 VideoData *s = s1->priv_data;
00226
00227 if (av_new_packet(pkt, video_buf_size) < 0)
00228 return AVERROR(EIO);
00229
00230 bktr_getframe(s->per_frame);
00231
00232 pkt->pts = av_gettime();
00233 memcpy(pkt->data, video_buf, video_buf_size);
00234
00235 return video_buf_size;
00236 }
00237
00238 static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
00239 {
00240 VideoData *s = s1->priv_data;
00241 AVStream *st;
00242 int width, height;
00243 int frame_rate;
00244 int frame_rate_base;
00245 int format = -1;
00246
00247 if (ap->width <= 0 || ap->height <= 0 || ap->time_base.den <= 0)
00248 return -1;
00249
00250 width = ap->width;
00251 height = ap->height;
00252 frame_rate = ap->time_base.den;
00253 frame_rate_base = ap->time_base.num;
00254
00255 st = av_new_stream(s1, 0);
00256 if (!st)
00257 return AVERROR(ENOMEM);
00258 av_set_pts_info(st, 64, 1, 1000000);
00259
00260 s->width = width;
00261 s->height = height;
00262 s->frame_rate = frame_rate;
00263 s->frame_rate_base = frame_rate_base;
00264 s->per_frame = ((u_int64_t)1000000 * s->frame_rate_base) / s->frame_rate;
00265
00266 st->codec->codec_type = CODEC_TYPE_VIDEO;
00267 st->codec->pix_fmt = PIX_FMT_YUV420P;
00268 st->codec->codec_id = CODEC_ID_RAWVIDEO;
00269 st->codec->width = width;
00270 st->codec->height = height;
00271 st->codec->time_base.den = frame_rate;
00272 st->codec->time_base.num = frame_rate_base;
00273
00274 if (ap->standard) {
00275 if (!strcasecmp(ap->standard, "pal"))
00276 format = PAL;
00277 else if (!strcasecmp(ap->standard, "secam"))
00278 format = SECAM;
00279 else if (!strcasecmp(ap->standard, "ntsc"))
00280 format = NTSC;
00281 }
00282
00283 if (bktr_init(s1->filename, width, height, format,
00284 &(s->video_fd), &(s->tuner_fd), -1, 0.0) < 0)
00285 return AVERROR(EIO);
00286
00287 nsignals = 0;
00288 last_frame_time = 0;
00289
00290 return 0;
00291 }
00292
00293 static int grab_read_close(AVFormatContext *s1)
00294 {
00295 VideoData *s = s1->priv_data;
00296 int c;
00297
00298 c = METEOR_CAP_STOP_CONT;
00299 ioctl(s->video_fd, METEORCAPTUR, &c);
00300 close(s->video_fd);
00301
00302 c = AUDIO_MUTE;
00303 ioctl(s->tuner_fd, BT848_SAUDIO, &c);
00304 close(s->tuner_fd);
00305
00306 munmap((caddr_t)video_buf, video_buf_size);
00307
00308 return 0;
00309 }
00310
00311 AVInputFormat bktr_demuxer = {
00312 "bktr",
00313 "video grab",
00314 sizeof(VideoData),
00315 NULL,
00316 grab_read_header,
00317 grab_read_packet,
00318 grab_read_close,
00319 .flags = AVFMT_NOFILE,
00320 };