tty.c 4.93 KB
Newer Older
Peter Ross's avatar
Peter Ross committed
1 2 3 4
/*
 * Tele-typewriter demuxer
 * Copyright (c) 2010 Peter Ross <pross@xvid.org>
 *
5
 * This file is part of Libav.
Peter Ross's avatar
Peter Ross committed
6
 *
7
 * Libav is free software; you can redistribute it and/or
Peter Ross's avatar
Peter Ross committed
8 9 10 11
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
12
 * Libav is distributed in the hope that it will be useful,
Peter Ross's avatar
Peter Ross committed
13 14 15 16 17
 * 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
18
 * License along with Libav; if not, write to the Free Software
Peter Ross's avatar
Peter Ross committed
19 20 21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

22 23 24 25 26
/**
 * @file
 * Tele-typewriter demuxer
 */

Peter Ross's avatar
Peter Ross committed
27 28
#include "libavutil/intreadwrite.h"
#include "libavutil/avstring.h"
29
#include "libavutil/log.h"
30
#include "libavutil/dict.h"
31
#include "libavutil/opt.h"
32
#include "libavutil/parseutils.h"
Peter Ross's avatar
Peter Ross committed
33 34 35 36
#include "avformat.h"
#include "sauce.h"

typedef struct {
37
    AVClass *class;
Peter Ross's avatar
Peter Ross committed
38
    int chars_per_frame;
39
    uint64_t fsize;  /**< file size less metadata buffer */
40
    char *video_size;/**< A string describing video size, set by a private option. */
41
    char *framerate; /**< Set by a private option. */
Peter Ross's avatar
Peter Ross committed
42 43 44 45 46 47 48 49
} TtyDemuxContext;

/**
 * Parse EFI header
 */
static int efi_read(AVFormatContext *avctx, uint64_t start_pos)
{
    TtyDemuxContext *s = avctx->priv_data;
50
    AVIOContext *pb = avctx->pb;
Peter Ross's avatar
Peter Ross committed
51 52 53
    char buf[37];
    int len;

54
    avio_seek(pb, start_pos, SEEK_SET);
55
    if (avio_r8(pb) != 0x1A)
Peter Ross's avatar
Peter Ross committed
56 57 58
        return -1;

#define GET_EFI_META(name,size) \
59
    len = avio_r8(pb); \
Peter Ross's avatar
Peter Ross committed
60 61
    if (len < 1 || len > size) \
        return -1; \
62
    if (avio_read(pb, buf, size) == size) { \
Peter Ross's avatar
Peter Ross committed
63
        buf[len] = 0; \
64
        av_dict_set(&avctx->metadata, name, buf, 0); \
Peter Ross's avatar
Peter Ross committed
65 66 67 68 69 70 71 72 73 74 75 76 77
    }

    GET_EFI_META("filename", 12)
    GET_EFI_META("title",    36)

    s->fsize = start_pos;
    return 0;
}

static int read_header(AVFormatContext *avctx,
                       AVFormatParameters *ap)
{
    TtyDemuxContext *s = avctx->priv_data;
78
    int width = 0, height = 0, ret = 0;
79
    AVStream *st = avformat_new_stream(avctx, NULL);
80
    AVRational framerate;
81 82 83 84 85

    if (!st) {
        ret = AVERROR(ENOMEM);
        goto fail;
    }
Peter Ross's avatar
Peter Ross committed
86
    st->codec->codec_tag   = 0;
87
    st->codec->codec_type  = AVMEDIA_TYPE_VIDEO;
Peter Ross's avatar
Peter Ross committed
88
    st->codec->codec_id    = CODEC_ID_ANSI;
89

90 91 92
    if (s->video_size && (ret = av_parse_video_size(&width, &height, s->video_size)) < 0) {
        av_log (avctx, AV_LOG_ERROR, "Couldn't parse video size.\n");
        goto fail;
93
    }
94 95 96 97
    if ((ret = av_parse_video_rate(&framerate, s->framerate)) < 0) {
        av_log(avctx, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s->framerate);
        goto fail;
    }
98 99
    st->codec->width  = width;
    st->codec->height = height;
100
    av_set_pts_info(st, 60, framerate.den, framerate.num);
Peter Ross's avatar
Peter Ross committed
101 102

    /* simulate tty display speed */
103
    s->chars_per_frame = FFMAX(av_q2d(st->time_base)*s->chars_per_frame, 1);
Peter Ross's avatar
Peter Ross committed
104

105
    if (avctx->pb->seekable) {
106
        s->fsize = avio_size(avctx->pb);
Peter Ross's avatar
Peter Ross committed
107 108 109 110 111
        st->duration = (s->fsize + s->chars_per_frame - 1) / s->chars_per_frame;

        if (ff_sauce_read(avctx, &s->fsize, 0, 0) < 0)
            efi_read(avctx, s->fsize - 51);

112
        avio_seek(avctx->pb, 0, SEEK_SET);
Peter Ross's avatar
Peter Ross committed
113 114
    }

115 116
fail:
    return ret;
Peter Ross's avatar
Peter Ross committed
117 118 119 120 121 122 123
}

static int read_packet(AVFormatContext *avctx, AVPacket *pkt)
{
    TtyDemuxContext *s = avctx->priv_data;
    int n;

Anton Khirnov's avatar
Anton Khirnov committed
124
    if (avctx->pb->eof_reached)
Peter Ross's avatar
Peter Ross committed
125 126 127 128 129
        return AVERROR_EOF;

    n = s->chars_per_frame;
    if (s->fsize) {
        // ignore metadata buffer
130
        uint64_t p = avio_tell(avctx->pb);
Peter Ross's avatar
Peter Ross committed
131 132 133 134 135 136 137
        if (p + s->chars_per_frame > s->fsize)
            n = s->fsize - p;
    }

    pkt->size = av_get_packet(avctx->pb, pkt, n);
    if (pkt->size <= 0)
        return AVERROR(EIO);
138
    pkt->flags |= AV_PKT_FLAG_KEY;
Peter Ross's avatar
Peter Ross committed
139 140 141
    return 0;
}

142 143
#define OFFSET(x) offsetof(TtyDemuxContext, x)
#define DEC AV_OPT_FLAG_DECODING_PARAM
144
static const AVOption options[] = {
145 146 147
    { "chars_per_frame", "", offsetof(TtyDemuxContext, chars_per_frame), AV_OPT_TYPE_INT, {.dbl = 6000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM},
    { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
    { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
148 149 150 151 152 153 154 155 156 157
    { NULL },
};

static const AVClass tty_demuxer_class = {
    .class_name     = "TTY demuxer",
    .item_name      = av_default_item_name,
    .option         = options,
    .version        = LIBAVUTIL_VERSION_INT,
};

158
AVInputFormat ff_tty_demuxer = {
Peter Ross's avatar
Peter Ross committed
159 160 161 162 163 164
    .name           = "tty",
    .long_name      = NULL_IF_CONFIG_SMALL("Tele-typewriter"),
    .priv_data_size = sizeof(TtyDemuxContext),
    .read_header    = read_header,
    .read_packet    = read_packet,
    .extensions     = "ans,art,asc,diz,ice,nfo,txt,vt",
165
    .priv_class     = &tty_demuxer_class,
Peter Ross's avatar
Peter Ross committed
166
};