bktr.c 11 KB
Newer Older
1 2 3 4 5
/*
 * *BSD video grab interface
 * Copyright (c) 2002 Steve O'Hara-Smith
 * based on
 *           Linux video grab interface
6
 *           Copyright (c) 2000,2001 Gerard Lantau
7 8 9
 * and
 *           simple_grab.c Copyright (c) 1999 Roger Hardiman
 *
10
 * This file is part of Libav.
11
 *
12
 * Libav is free software; you can redistribute it and/or
13 14
 * modify it under the terms of the GNU Lesser General Public
 * 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
 * Libav is distributed in the hope that it will be useful,
18 19 20 21 22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
23
 * License along with Libav; 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/avformat.h"
28 29
#include "libavutil/log.h"
#include "libavutil/opt.h"
30
#include "libavutil/parseutils.h"
31
#if HAVE_DEV_BKTR_IOCTL_METEOR_H && HAVE_DEV_BKTR_IOCTL_BT848_H
32 33
# include <dev/bktr/ioctl_meteor.h>
# include <dev/bktr/ioctl_bt848.h>
34
#elif HAVE_MACHINE_IOCTL_METEOR_H && HAVE_MACHINE_IOCTL_BT848_H
35 36
# include <machine/ioctl_meteor.h>
# include <machine/ioctl_bt848.h>
37
#elif HAVE_DEV_VIDEO_METEOR_IOCTL_METEOR_H && HAVE_DEV_VIDEO_BKTR_IOCTL_BT848_H
38 39
# include <dev/video/meteor/ioctl_meteor.h>
# include <dev/video/bktr/ioctl_bt848.h>
40
#elif HAVE_DEV_IC_BT8XX_H
41 42 43 44 45 46 47 48
# include <dev/ic/bt8xx.h>
#endif
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <signal.h>
49
#include <stdint.h>
50
#include <strings.h>
51 52

typedef struct {
53
    AVClass *class;
54 55 56
    int video_fd;
    int tuner_fd;
    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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
} VideoData;


#define PAL 1
#define PALBDGHI 1
#define NTSC 2
#define NTSCM 2
#define SECAM 3
#define PALN 4
#define PALM 5
#define NTSCJ 6

/* PAL is 768 x 576. NTSC is 640 x 480 */
#define PAL_HEIGHT 576
#define SECAM_HEIGHT 576
#define NTSC_HEIGHT 480

#ifndef VIDEO_FORMAT
#define VIDEO_FORMAT NTSC
#endif

static int bktr_dev[] = { METEOR_DEV0, METEOR_DEV1, METEOR_DEV2,
    METEOR_DEV3, METEOR_DEV_SVIDEO };

uint8_t *video_buf;
size_t video_buf_size;
87
uint64_t last_frame_time;
88 89 90 91 92 93 94 95 96
volatile sig_atomic_t nsignals;


static void catchsignal(int signal)
{
    nsignals++;
    return;
}

97
static av_cold int bktr_init(const char *video_device, int width, int height,
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    int format, int *video_fd, int *tuner_fd, int idev, double frequency)
{
    struct meteor_geomet geo;
    int h_max;
    long ioctl_frequency;
    char *arg;
    int c;
    struct sigaction act, old;

    if (idev < 0 || idev > 4)
    {
        arg = getenv ("BKTR_DEV");
        if (arg)
            idev = atoi (arg);
        if (idev < 0 || idev > 4)
            idev = 1;
    }

    if (format < 1 || format > 6)
    {
        arg = getenv ("BKTR_FORMAT");
        if (arg)
            format = atoi (arg);
        if (format < 1 || format > 6)
            format = VIDEO_FORMAT;
    }

    if (frequency <= 0)
    {
        arg = getenv ("BKTR_FREQUENCY");
        if (arg)
            frequency = atof (arg);
        if (frequency <= 0)
            frequency = 0.0;
    }

    memset(&act, 0, sizeof(act));
    sigemptyset(&act.sa_mask);
    act.sa_handler = catchsignal;
    sigaction(SIGUSR1, &act, &old);

    *tuner_fd = open("/dev/tuner0", O_RDONLY);
    if (*tuner_fd < 0)
141
        av_log(NULL, AV_LOG_ERROR, "Warning. Tuner not opened, continuing: %s\n", strerror(errno));
142 143 144

    *video_fd = open(video_device, O_RDONLY);
    if (*video_fd < 0) {
145
        av_log(NULL, AV_LOG_ERROR, "%s: %s\n", video_device, strerror(errno));
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
        return -1;
    }

    geo.rows = height;
    geo.columns = width;
    geo.frames = 1;
    geo.oformat = METEOR_GEO_YUV_422 | METEOR_GEO_YUV_12;

    switch (format) {
    case PAL:   h_max = PAL_HEIGHT;   c = BT848_IFORM_F_PALBDGHI; break;
    case PALN:  h_max = PAL_HEIGHT;   c = BT848_IFORM_F_PALN;     break;
    case PALM:  h_max = PAL_HEIGHT;   c = BT848_IFORM_F_PALM;     break;
    case SECAM: h_max = SECAM_HEIGHT; c = BT848_IFORM_F_SECAM;    break;
    case NTSC:  h_max = NTSC_HEIGHT;  c = BT848_IFORM_F_NTSCM;    break;
    case NTSCJ: h_max = NTSC_HEIGHT;  c = BT848_IFORM_F_NTSCJ;    break;
    default:    h_max = PAL_HEIGHT;   c = BT848_IFORM_F_PALBDGHI; break;
    }

    if (height <= h_max / 2)
        geo.oformat |= METEOR_GEO_EVEN_ONLY;

    if (ioctl(*video_fd, METEORSETGEO, &geo) < 0) {
168
        av_log(NULL, AV_LOG_ERROR, "METEORSETGEO: %s\n", strerror(errno));
169 170 171 172
        return -1;
    }

    if (ioctl(*video_fd, BT848SFMT, &c) < 0) {
173
        av_log(NULL, AV_LOG_ERROR, "BT848SFMT: %s\n", strerror(errno));
174 175 176 177 178
        return -1;
    }

    c = bktr_dev[idev];
    if (ioctl(*video_fd, METEORSINPUT, &c) < 0) {
179
        av_log(NULL, AV_LOG_ERROR, "METEORSINPUT: %s\n", strerror(errno));
180 181 182 183 184
        return -1;
    }

    video_buf_size = width * height * 12 / 8;

185
    video_buf = (uint8_t *)mmap((caddr_t)0, video_buf_size,
186 187
        PROT_READ, MAP_SHARED, *video_fd, (off_t)0);
    if (video_buf == MAP_FAILED) {
188
        av_log(NULL, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
189 190 191 192
        return -1;
    }

    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 199
    }

    c = AUDIO_UNMUTE;
    if (ioctl(*tuner_fd, BT848_SAUDIO, &c) < 0)
200
        av_log(NULL, AV_LOG_ERROR, "TVTUNER_SAUDIO: %s\n", strerror(errno));
201 202 203 204 205 206 207 208 209 210

    c = METEOR_CAP_CONTINOUS;
    ioctl(*video_fd, METEORCAPTUR, &c);

    c = SIGUSR1;
    ioctl(*video_fd, METEORSSIGNAL, &c);

    return 0;
}

211
static void bktr_getframe(uint64_t per_frame)
212
{
213
    uint64_t curtime;
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235

    curtime = av_gettime();
    if (!last_frame_time
        || ((last_frame_time + per_frame) > curtime)) {
        if (!usleep(last_frame_time + per_frame + per_frame / 8 - curtime)) {
            if (!nsignals)
                av_log(NULL, AV_LOG_INFO,
                       "SLEPT NO signals - %d microseconds late\n",
                       (int)(av_gettime() - last_frame_time - per_frame));
        }
    }
    nsignals = 0;
    last_frame_time = curtime;
}


/* note: we support only one picture read at a time */
static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
{
    VideoData *s = s1->priv_data;

    if (av_new_packet(pkt, video_buf_size) < 0)
236
        return AVERROR(EIO);
237 238 239

    bktr_getframe(s->per_frame);

Luca Abeni's avatar
Luca Abeni committed
240
    pkt->pts = av_gettime();
241 242 243 244 245 246 247 248 249 250
    memcpy(pkt->data, video_buf, video_buf_size);

    return video_buf_size;
}

static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
{
    VideoData *s = s1->priv_data;
    AVStream *st;
    int width, height;
251
    AVRational fps;
252
    int ret = 0;
253

254
    if (ap->time_base.den <= 0) {
255 256 257
        ret = AVERROR(EINVAL);
        goto out;
    }
258

259 260 261 262 263 264 265 266 267 268 269
#if FF_API_FORMAT_PARAMETERS
    if (ap->standard) {
        if (!strcasecmp(ap->standard, "pal"))
            s->standard = PAL;
        else if (!strcasecmp(ap->standard, "secam"))
            s->standard = SECAM;
        else if (!strcasecmp(ap->standard, "ntsc"))
            s->standard = NTSC;
    }
#endif

270 271 272 273
    if ((ret = av_parse_video_size(&width, &height, s->video_size)) < 0) {
        av_log(s1, AV_LOG_ERROR, "Couldn't parse video size.\n");
        goto out;
    }
274 275 276 277 278 279 280 281 282 283 284

    if (!s->framerate)
        switch (s->standard) {
        case PAL:   s->framerate = av_strdup("pal");  break;
        case NTSC:  s->framerate = av_strdup("ntsc"); break;
        case SECAM: s->framerate = av_strdup("25");   break;
        default:
            av_log(s1, AV_LOG_ERROR, "Unknown standard.\n");
            ret = AVERROR(EINVAL);
            goto out;
        }
285 286 287 288
    if ((ret = av_parse_video_rate(&fps, s->framerate)) < 0) {
        av_log(s1, AV_LOG_ERROR, "Couldn't parse framerate.\n");
        goto out;
    }
289 290 291 292 293
#if FF_API_FORMAT_PARAMETERS
    if (ap->width > 0)
        width = ap->width;
    if (ap->height > 0)
        height = ap->height;
294 295
    if (ap->time_base.num)
        fps = (AVRational){ap->time_base.den, ap->time_base.num};
296
#endif
297 298

    st = av_new_stream(s1, 0);
299 300 301 302
    if (!st) {
        ret = AVERROR(ENOMEM);
        goto out;
    }
Luca Abeni's avatar
Luca Abeni committed
303
    av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in use */
304 305 306

    s->width = width;
    s->height = height;
307
    s->per_frame = ((uint64_t)1000000 * fps.den) / fps.num;
308

309
    st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
Sam Hocevar's avatar
Sam Hocevar committed
310 311 312 313
    st->codec->pix_fmt = PIX_FMT_YUV420P;
    st->codec->codec_id = CODEC_ID_RAWVIDEO;
    st->codec->width = width;
    st->codec->height = height;
314 315
    st->codec->time_base.den = fps.num;
    st->codec->time_base.num = fps.den;
316 317


318
    if (bktr_init(s1->filename, width, height, s->standard,
319 320 321 322
            &(s->video_fd), &(s->tuner_fd), -1, 0.0) < 0) {
        ret = AVERROR(EIO);
        goto out;
    }
323 324 325 326

    nsignals = 0;
    last_frame_time = 0;

327
out:
328
    av_freep(&s->video_size);
329
    av_freep(&s->framerate);
330
    return ret;
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
}

static int grab_read_close(AVFormatContext *s1)
{
    VideoData *s = s1->priv_data;
    int c;

    c = METEOR_CAP_STOP_CONT;
    ioctl(s->video_fd, METEORCAPTUR, &c);
    close(s->video_fd);

    c = AUDIO_MUTE;
    ioctl(s->tuner_fd, BT848_SAUDIO, &c);
    close(s->tuner_fd);

    munmap((caddr_t)video_buf, video_buf_size);

    return 0;
}

351 352
#define OFFSET(x) offsetof(VideoData, x)
#define DEC AV_OPT_FLAG_DECODING_PARAM
353 354 355 356 357 358 359 360
static const AVOption options[] = {
    { "standard", "", offsetof(VideoData, standard), FF_OPT_TYPE_INT, {.dbl = VIDEO_FORMAT}, PAL, NTSCJ, AV_OPT_FLAG_DECODING_PARAM, "standard" },
    { "PAL",      "", 0, FF_OPT_TYPE_CONST, {.dbl = PAL},   0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
    { "NTSC",     "", 0, FF_OPT_TYPE_CONST, {.dbl = NTSC},  0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
    { "SECAM",    "", 0, FF_OPT_TYPE_CONST, {.dbl = SECAM}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
    { "PALN",     "", 0, FF_OPT_TYPE_CONST, {.dbl = PALN},  0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
    { "PALM",     "", 0, FF_OPT_TYPE_CONST, {.dbl = PALM},  0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
    { "NTSCJ",    "", 0, FF_OPT_TYPE_CONST, {.dbl = NTSCJ}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
361
    { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = "vga"}, 0, 0, DEC },
362
    { "framerate", "", OFFSET(framerate), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
363 364 365 366 367 368 369 370 371 372
    { NULL },
};

static const AVClass bktr_class = {
    .class_name = "BKTR grab interface",
    .item_name  = av_default_item_name,
    .option     = options,
    .version    = LIBAVUTIL_VERSION_INT,
};

373
AVInputFormat ff_bktr_demuxer = {
374
    "bktr",
375
    NULL_IF_CONFIG_SMALL("video grab"),
Diego Biurrun's avatar
Diego Biurrun committed
376 377
    sizeof(VideoData),
    NULL,
378 379 380 381
    grab_read_header,
    grab_read_packet,
    grab_read_close,
    .flags = AVFMT_NOFILE,
382
    .priv_class = &bktr_class,
383
};