bintext.c 11.5 KB
Newer Older
Peter Ross's avatar
Peter Ross committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*
 * Binary text demuxer
 * eXtended BINary text (XBIN) demuxer
 * Artworx Data Format demuxer
 * iCEDraw File demuxer
 * Copyright (c) 2010 Peter Ross <pross@xvid.org>
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * 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.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * 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
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
 * @file
 * Binary text demuxer
 * eXtended BINary text (XBIN) demuxer
 * Artworx Data Format demuxer
 * iCEDraw File demuxer
 */

#include "libavutil/intreadwrite.h"
34 35
#include "libavutil/opt.h"
#include "libavutil/parseutils.h"
Peter Ross's avatar
Peter Ross committed
36
#include "avformat.h"
37
#include "internal.h"
Peter Ross's avatar
Peter Ross committed
38 39 40 41
#include "sauce.h"
#include "libavcodec/bintext.h"

typedef struct {
42 43 44 45
    const AVClass *class;
    int chars_per_frame; /**< characters to send decoder per frame;
                              set by private options as characters per second, and then
                              converted to characters per frame at runtime */
46
    int width, height;    /**< video size (WxH pixels) (private option) */
47
    AVRational framerate; /**< frames per second (private option) */
Peter Ross's avatar
Peter Ross committed
48 49 50
    uint64_t fsize;  /**< file size less metadata buffer */
} BinDemuxContext;

51 52 53 54 55 56
static AVStream * init_stream(AVFormatContext *s)
{
    BinDemuxContext *bin = s->priv_data;
    AVStream *st = avformat_new_stream(s, NULL);
    if (!st)
        return NULL;
57 58
    st->codecpar->codec_tag   = 0;
    st->codecpar->codec_type  = AVMEDIA_TYPE_VIDEO;
59

60
    if (!bin->width) {
61 62
        st->codecpar->width  = (80<<3);
        st->codecpar->height = (25<<4);
63 64
    }

65
    avpriv_set_pts_info(st, 60, bin->framerate.den, bin->framerate.num);
66 67

    /* simulate tty display speed */
68
    bin->chars_per_frame = av_clip(av_q2d(st->time_base) * bin->chars_per_frame, 1, INT_MAX);
69 70 71 72

    return st;
}

Peter Ross's avatar
Peter Ross committed
73 74 75 76
#if CONFIG_BINTEXT_DEMUXER | CONFIG_ADF_DEMUXER | CONFIG_IDF_DEMUXER
/**
 * Given filesize and width, calculate height (assume font_height of 16)
 */
77
static void calculate_height(AVCodecParameters *par, uint64_t fsize)
Peter Ross's avatar
Peter Ross committed
78
{
79
    par->height = (fsize / ((par->width>>3)*2)) << 4;
Peter Ross's avatar
Peter Ross committed
80 81 82 83 84 85 86 87 88 89
}
#endif

#if CONFIG_BINTEXT_DEMUXER
static const uint8_t next_magic[]={
    0x1A, 0x1B, '[', '0', ';', '3', '0', ';', '4', '0', 'm', 'N', 'E', 'X', 'T', 0x00
};

static int next_tag_read(AVFormatContext *avctx, uint64_t *fsize)
{
90
    AVIOContext *pb = avctx->pb;
Peter Ross's avatar
Peter Ross committed
91 92
    char buf[36];
    int len;
93
    uint64_t start_pos = avio_size(pb) - 256;
Peter Ross's avatar
Peter Ross committed
94

95
    avio_seek(pb, start_pos, SEEK_SET);
96
    if (avio_read(pb, buf, sizeof(next_magic)) != sizeof(next_magic))
Peter Ross's avatar
Peter Ross committed
97 98 99
        return -1;
    if (memcmp(buf, next_magic, sizeof(next_magic)))
        return -1;
100
    if (avio_r8(pb) != 0x01)
Peter Ross's avatar
Peter Ross committed
101 102 103 104 105
        return -1;

    *fsize -= 256;

#define GET_EFI2_META(name,size) \
106
    len = avio_r8(pb); \
Peter Ross's avatar
Peter Ross committed
107 108
    if (len < 1 || len > size) \
        return -1; \
109
    if (avio_read(pb, buf, size) == size && *buf) { \
Peter Ross's avatar
Peter Ross committed
110
        buf[len] = 0; \
111
        av_dict_set(&avctx->metadata, name, buf, 0); \
Peter Ross's avatar
Peter Ross committed
112 113 114 115 116 117 118 119 120 121
    }

    GET_EFI2_META("filename",  12)
    GET_EFI2_META("author",    20)
    GET_EFI2_META("publisher", 20)
    GET_EFI2_META("title",     35)

    return 0;
}

122
static void predict_width(AVCodecParameters *par, uint64_t fsize, int got_width)
Peter Ross's avatar
Peter Ross committed
123 124 125
{
    /** attempt to guess width */
    if (!got_width)
126
        par->width = fsize > 4000 ? (160<<3) : (80<<3);
Peter Ross's avatar
Peter Ross committed
127 128
}

129
static int bintext_read_header(AVFormatContext *s)
Peter Ross's avatar
Peter Ross committed
130 131
{
    BinDemuxContext *bin = s->priv_data;
132
    AVIOContext *pb = s->pb;
Peter Ross's avatar
Peter Ross committed
133

134
    AVStream *st = init_stream(s);
Peter Ross's avatar
Peter Ross committed
135 136
    if (!st)
        return AVERROR(ENOMEM);
137
    st->codecpar->codec_id    = AV_CODEC_ID_BINTEXT;
Peter Ross's avatar
Peter Ross committed
138

139
    if (ff_alloc_extradata(st->codecpar, 2))
Peter Ross's avatar
Peter Ross committed
140
        return AVERROR(ENOMEM);
141 142
    st->codecpar->extradata[0] = 16;
    st->codecpar->extradata[1] = 0;
Peter Ross's avatar
Peter Ross committed
143

144
    if (pb->seekable) {
Peter Ross's avatar
Peter Ross committed
145
        int got_width = 0;
146
        bin->fsize = avio_size(pb);
Peter Ross's avatar
Peter Ross committed
147 148
        if (ff_sauce_read(s, &bin->fsize, &got_width, 0) < 0)
            next_tag_read(s, &bin->fsize);
149
        if (!bin->width) {
150 151
            predict_width(st->codecpar, bin->fsize, got_width);
            calculate_height(st->codecpar, bin->fsize);
152
        }
153
        avio_seek(pb, 0, SEEK_SET);
Peter Ross's avatar
Peter Ross committed
154 155
    }
    return 0;
156
}
Peter Ross's avatar
Peter Ross committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170
#endif /* CONFIG_BINTEXT_DEMUXER */

#if CONFIG_XBIN_DEMUXER
static int xbin_probe(AVProbeData *p)
{
    const uint8_t *d = p->buf;

    if (AV_RL32(d) == MKTAG('X','B','I','N') && d[4] == 0x1A &&
        AV_RL16(d+5) > 0 && AV_RL16(d+5) <= 160 &&
        d[9] > 0 && d[9] <= 32)
        return AVPROBE_SCORE_MAX;
    return 0;
}

171
static int xbin_read_header(AVFormatContext *s)
Peter Ross's avatar
Peter Ross committed
172 173
{
    BinDemuxContext *bin = s->priv_data;
174
    AVIOContext *pb = s->pb;
Peter Ross's avatar
Peter Ross committed
175 176
    char fontheight, flags;

177
    AVStream *st = init_stream(s);
Peter Ross's avatar
Peter Ross committed
178 179 180
    if (!st)
        return AVERROR(ENOMEM);

181
    avio_skip(pb, 5);
182 183
    st->codecpar->width   = avio_rl16(pb)<<3;
    st->codecpar->height  = avio_rl16(pb);
184
    fontheight         = avio_r8(pb);
185
    st->codecpar->height *= fontheight;
186
    flags              = avio_r8(pb);
Peter Ross's avatar
Peter Ross committed
187

188
    st->codecpar->extradata_size = 2;
Peter Ross's avatar
Peter Ross committed
189
    if ((flags & BINTEXT_PALETTE))
190
        st->codecpar->extradata_size += 48;
Peter Ross's avatar
Peter Ross committed
191
    if ((flags & BINTEXT_FONT))
192 193
        st->codecpar->extradata_size += fontheight * (flags & 0x10 ? 512 : 256);
    st->codecpar->codec_id    = flags & 4 ? AV_CODEC_ID_XBIN : AV_CODEC_ID_BINTEXT;
Peter Ross's avatar
Peter Ross committed
194

195
    if (ff_alloc_extradata(st->codecpar, st->codecpar->extradata_size))
Peter Ross's avatar
Peter Ross committed
196
        return AVERROR(ENOMEM);
197 198 199
    st->codecpar->extradata[0] = fontheight;
    st->codecpar->extradata[1] = flags;
    if (avio_read(pb, st->codecpar->extradata + 2, st->codecpar->extradata_size - 2) < 0)
Peter Ross's avatar
Peter Ross committed
200 201
        return AVERROR(EIO);

202
    if (pb->seekable) {
203
        bin->fsize = avio_size(pb) - 9 - st->codecpar->extradata_size;
Peter Ross's avatar
Peter Ross committed
204
        ff_sauce_read(s, &bin->fsize, NULL, 0);
205
        avio_seek(pb, 9 + st->codecpar->extradata_size, SEEK_SET);
Peter Ross's avatar
Peter Ross committed
206 207 208 209 210 211 212
    }

    return 0;
}
#endif /* CONFIG_XBIN_DEMUXER */

#if CONFIG_ADF_DEMUXER
213
static int adf_read_header(AVFormatContext *s)
Peter Ross's avatar
Peter Ross committed
214 215
{
    BinDemuxContext *bin = s->priv_data;
216
    AVIOContext *pb = s->pb;
Peter Ross's avatar
Peter Ross committed
217 218
    AVStream *st;

219
    if (avio_r8(pb) != 1)
Peter Ross's avatar
Peter Ross committed
220 221
        return AVERROR_INVALIDDATA;

222
    st = init_stream(s);
Peter Ross's avatar
Peter Ross committed
223 224
    if (!st)
        return AVERROR(ENOMEM);
225
    st->codecpar->codec_id    = AV_CODEC_ID_BINTEXT;
Peter Ross's avatar
Peter Ross committed
226

227
    if (ff_alloc_extradata(st->codecpar, 2 + 48 + 4096))
Peter Ross's avatar
Peter Ross committed
228
        return AVERROR(ENOMEM);
229 230
    st->codecpar->extradata[0] = 16;
    st->codecpar->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;
Peter Ross's avatar
Peter Ross committed
231

232
    if (avio_read(pb, st->codecpar->extradata + 2, 24) < 0)
Peter Ross's avatar
Peter Ross committed
233
        return AVERROR(EIO);
234
    avio_skip(pb, 144);
235
    if (avio_read(pb, st->codecpar->extradata + 2 + 24, 24) < 0)
Peter Ross's avatar
Peter Ross committed
236
        return AVERROR(EIO);
237
    if (avio_read(pb, st->codecpar->extradata + 2 + 48, 4096) < 0)
Peter Ross's avatar
Peter Ross committed
238 239
        return AVERROR(EIO);

240
    if (pb->seekable) {
Peter Ross's avatar
Peter Ross committed
241
        int got_width = 0;
242
        bin->fsize = avio_size(pb) - 1 - 192 - 4096;
243
        st->codecpar->width = 80<<3;
Peter Ross's avatar
Peter Ross committed
244
        ff_sauce_read(s, &bin->fsize, &got_width, 0);
245
        if (!bin->width)
246
            calculate_height(st->codecpar, bin->fsize);
247
        avio_seek(pb, 1 + 192 + 4096, SEEK_SET);
Peter Ross's avatar
Peter Ross committed
248 249 250 251 252 253 254 255 256 257 258 259
    }
    return 0;
}
#endif /* CONFIG_ADF_DEMUXER */

#if CONFIG_IDF_DEMUXER
static const uint8_t idf_magic[] = {
    0x04, 0x31, 0x2e, 0x34, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x00, 0x15, 0x00
};

static int idf_probe(AVProbeData *p)
{
260 261 262
    if (p->buf_size < sizeof(idf_magic))
        return 0;
    if (!memcmp(p->buf, idf_magic, sizeof(idf_magic)))
Peter Ross's avatar
Peter Ross committed
263 264 265 266
        return AVPROBE_SCORE_MAX;
    return 0;
}

267
static int idf_read_header(AVFormatContext *s)
Peter Ross's avatar
Peter Ross committed
268 269
{
    BinDemuxContext *bin = s->priv_data;
270
    AVIOContext *pb = s->pb;
Peter Ross's avatar
Peter Ross committed
271 272 273
    AVStream *st;
    int got_width = 0;

274
    if (!pb->seekable)
Peter Ross's avatar
Peter Ross committed
275 276
        return AVERROR(EIO);

277
    st = init_stream(s);
Peter Ross's avatar
Peter Ross committed
278 279
    if (!st)
        return AVERROR(ENOMEM);
280
    st->codecpar->codec_id    = AV_CODEC_ID_IDF;
Peter Ross's avatar
Peter Ross committed
281

282
    if (ff_alloc_extradata(st->codecpar, 2 + 48 + 4096))
Peter Ross's avatar
Peter Ross committed
283
        return AVERROR(ENOMEM);
284 285
    st->codecpar->extradata[0] = 16;
    st->codecpar->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;
Peter Ross's avatar
Peter Ross committed
286

287
    avio_seek(pb, avio_size(pb) - 4096 - 48, SEEK_SET);
Peter Ross's avatar
Peter Ross committed
288

289
    if (avio_read(pb, st->codecpar->extradata + 2 + 48, 4096) < 0)
Peter Ross's avatar
Peter Ross committed
290
        return AVERROR(EIO);
291
    if (avio_read(pb, st->codecpar->extradata + 2, 48) < 0)
Peter Ross's avatar
Peter Ross committed
292 293
        return AVERROR(EIO);

294
    bin->fsize = avio_size(pb) - 12 - 4096 - 48;
Peter Ross's avatar
Peter Ross committed
295
    ff_sauce_read(s, &bin->fsize, &got_width, 0);
296
    if (!bin->width)
297
        calculate_height(st->codecpar, bin->fsize);
298
    avio_seek(pb, 12, SEEK_SET);
Peter Ross's avatar
Peter Ross committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312
    return 0;
}
#endif /* CONFIG_IDF_DEMUXER */

static int read_packet(AVFormatContext *s,
                           AVPacket *pkt)
{
    BinDemuxContext *bin = s->priv_data;

    if (bin->fsize > 0) {
        if (av_get_packet(s->pb, pkt, bin->fsize) < 0)
            return AVERROR(EIO);
        bin->fsize = -1; /* done */
    } else if (!bin->fsize) {
313
        if (avio_feof(s->pb))
Peter Ross's avatar
Peter Ross committed
314 315 316 317 318 319 320 321 322 323 324
            return AVERROR(EIO);
        if (av_get_packet(s->pb, pkt, bin->chars_per_frame) < 0)
            return AVERROR(EIO);
    } else {
        return AVERROR(EIO);
    }

    pkt->flags |= AV_PKT_FLAG_KEY;
    return 0;
}

325 326
#define OFFSET(x) offsetof(BinDemuxContext, x)
static const AVOption options[] = {
327
    { "linespeed", "set simulated line speed (bytes per second)", OFFSET(chars_per_frame), AV_OPT_TYPE_INT, {.i64 = 6000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM},
328
    { "video_size", "set video size, such as 640x480 or hd720.", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
329
    { "framerate", "set framerate (frames per second)", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
330 331 332 333 334 335 336 337 338 339 340
    { NULL },
};

#define CLASS(name) \
(const AVClass[1]){{ \
    .class_name     = name, \
    .item_name      = av_default_item_name, \
    .option         = options, \
    .version        = LIBAVUTIL_VERSION_INT, \
}}

Peter Ross's avatar
Peter Ross committed
341 342
#if CONFIG_BINTEXT_DEMUXER
AVInputFormat ff_bintext_demuxer = {
343 344 345 346 347 348
    .name           = "bin",
    .long_name      = NULL_IF_CONFIG_SMALL("Binary text"),
    .priv_data_size = sizeof(BinDemuxContext),
    .read_header    = bintext_read_header,
    .read_packet    = read_packet,
    .extensions     = "bin",
349
    .priv_class     = CLASS("Binary text demuxer"),
Peter Ross's avatar
Peter Ross committed
350 351 352 353 354
};
#endif

#if CONFIG_XBIN_DEMUXER
AVInputFormat ff_xbin_demuxer = {
355 356 357 358 359 360
    .name           = "xbin",
    .long_name      = NULL_IF_CONFIG_SMALL("eXtended BINary text (XBIN)"),
    .priv_data_size = sizeof(BinDemuxContext),
    .read_probe     = xbin_probe,
    .read_header    = xbin_read_header,
    .read_packet    = read_packet,
361
    .priv_class     = CLASS("eXtended BINary text (XBIN) demuxer"),
Peter Ross's avatar
Peter Ross committed
362 363 364 365 366
};
#endif

#if CONFIG_ADF_DEMUXER
AVInputFormat ff_adf_demuxer = {
367 368 369 370 371 372
    .name           = "adf",
    .long_name      = NULL_IF_CONFIG_SMALL("Artworx Data Format"),
    .priv_data_size = sizeof(BinDemuxContext),
    .read_header    = adf_read_header,
    .read_packet    = read_packet,
    .extensions     = "adf",
373
    .priv_class     = CLASS("Artworx Data Format demuxer"),
Peter Ross's avatar
Peter Ross committed
374 375 376 377 378
};
#endif

#if CONFIG_IDF_DEMUXER
AVInputFormat ff_idf_demuxer = {
379 380 381 382 383 384 385
    .name           = "idf",
    .long_name      = NULL_IF_CONFIG_SMALL("iCE Draw File"),
    .priv_data_size = sizeof(BinDemuxContext),
    .read_probe     = idf_probe,
    .read_header    = idf_read_header,
    .read_packet    = read_packet,
    .extensions     = "idf",
386
    .priv_class     = CLASS("iCE Draw File demuxer"),
Peter Ross's avatar
Peter Ross committed
387 388
};
#endif