3 * eXtended BINary text (XBIN) demuxer
4 * Artworx Data Format demuxer
6 * Copyright (c) 2010 Peter Ross <pross@xvid.org>
8 * This file is part of FFmpeg.
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 * eXtended BINary text (XBIN) demuxer
29 * Artworx Data Format demuxer
30 * iCEDraw File demuxer
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/parseutils.h"
39 #include "libavcodec/bintext.h"
43 int chars_per_frame; /**< characters to send decoder per frame;
44 set by private options as characters per second, and then
45 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) */
48 uint64_t fsize; /**< file size less metadata buffer */
51 static AVStream * init_stream(AVFormatContext *s)
53 BinDemuxContext *bin = s->priv_data;
54 AVStream *st = avformat_new_stream(s, NULL);
57 st->codec->codec_tag = 0;
58 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
61 st->codec->width = (80<<3);
62 st->codec->height = (25<<4);
65 avpriv_set_pts_info(st, 60, bin->framerate.den, bin->framerate.num);
67 /* simulate tty display speed */
68 bin->chars_per_frame = FFMAX(av_q2d(st->time_base) * bin->chars_per_frame, 1);
73 #if CONFIG_BINTEXT_DEMUXER | CONFIG_ADF_DEMUXER | CONFIG_IDF_DEMUXER
75 * Given filesize and width, calculate height (assume font_height of 16)
77 static void calculate_height(AVCodecContext *avctx, uint64_t fsize)
79 avctx->height = (fsize / ((avctx->width>>3)*2)) << 4;
83 #if CONFIG_BINTEXT_DEMUXER
84 static const uint8_t next_magic[]={
85 0x1A, 0x1B, '[', '0', ';', '3', '0', ';', '4', '0', 'm', 'N', 'E', 'X', 'T', 0x00
88 static int next_tag_read(AVFormatContext *avctx, uint64_t *fsize)
90 AVIOContext *pb = avctx->pb;
93 uint64_t start_pos = avio_size(pb) - 256;
95 avio_seek(pb, start_pos, SEEK_SET);
96 if (avio_read(pb, buf, sizeof(next_magic)) != sizeof(next_magic))
98 if (memcmp(buf, next_magic, sizeof(next_magic)))
100 if (avio_r8(pb) != 0x01)
105 #define GET_EFI2_META(name,size) \
107 if (len < 1 || len > size) \
109 if (avio_read(pb, buf, size) == size && *buf) { \
111 av_dict_set(&avctx->metadata, name, buf, 0); \
114 GET_EFI2_META("filename", 12)
115 GET_EFI2_META("author", 20)
116 GET_EFI2_META("publisher", 20)
117 GET_EFI2_META("title", 35)
122 static void predict_width(AVCodecContext *avctx, uint64_t fsize, int got_width)
124 /** attempt to guess width */
126 avctx->width = fsize > 4000 ? (160<<3) : (80<<3);
129 static int bintext_read_header(AVFormatContext *s)
131 BinDemuxContext *bin = s->priv_data;
132 AVIOContext *pb = s->pb;
134 AVStream *st = init_stream(s);
136 return AVERROR(ENOMEM);
137 st->codec->codec_id = AV_CODEC_ID_BINTEXT;
139 st->codec->extradata_size = 2;
140 st->codec->extradata = av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
141 if (!st->codec->extradata)
142 return AVERROR(ENOMEM);
143 st->codec->extradata[0] = 16;
144 st->codec->extradata[1] = 0;
148 bin->fsize = avio_size(pb);
149 if (ff_sauce_read(s, &bin->fsize, &got_width, 0) < 0)
150 next_tag_read(s, &bin->fsize);
152 predict_width(st->codec, bin->fsize, got_width);
153 calculate_height(st->codec, bin->fsize);
155 avio_seek(pb, 0, SEEK_SET);
159 #endif /* CONFIG_BINTEXT_DEMUXER */
161 #if CONFIG_XBIN_DEMUXER
162 static int xbin_probe(AVProbeData *p)
164 const uint8_t *d = p->buf;
166 if (AV_RL32(d) == MKTAG('X','B','I','N') && d[4] == 0x1A &&
167 AV_RL16(d+5) > 0 && AV_RL16(d+5) <= 160 &&
168 d[9] > 0 && d[9] <= 32)
169 return AVPROBE_SCORE_MAX;
173 static int xbin_read_header(AVFormatContext *s)
175 BinDemuxContext *bin = s->priv_data;
176 AVIOContext *pb = s->pb;
177 char fontheight, flags;
179 AVStream *st = init_stream(s);
181 return AVERROR(ENOMEM);
184 st->codec->width = avio_rl16(pb)<<3;
185 st->codec->height = avio_rl16(pb);
186 fontheight = avio_r8(pb);
187 st->codec->height *= fontheight;
190 st->codec->extradata_size = 2;
191 if ((flags & BINTEXT_PALETTE))
192 st->codec->extradata_size += 48;
193 if ((flags & BINTEXT_FONT))
194 st->codec->extradata_size += fontheight * (flags & 0x10 ? 512 : 256);
195 st->codec->codec_id = flags & 4 ? AV_CODEC_ID_XBIN : AV_CODEC_ID_BINTEXT;
197 st->codec->extradata = av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
198 if (!st->codec->extradata)
199 return AVERROR(ENOMEM);
200 st->codec->extradata[0] = fontheight;
201 st->codec->extradata[1] = flags;
202 if (avio_read(pb, st->codec->extradata + 2, st->codec->extradata_size - 2) < 0)
206 bin->fsize = avio_size(pb) - 9 - st->codec->extradata_size;
207 ff_sauce_read(s, &bin->fsize, NULL, 0);
208 avio_seek(pb, 9 + st->codec->extradata_size, SEEK_SET);
213 #endif /* CONFIG_XBIN_DEMUXER */
215 #if CONFIG_ADF_DEMUXER
216 static int adf_read_header(AVFormatContext *s)
218 BinDemuxContext *bin = s->priv_data;
219 AVIOContext *pb = s->pb;
222 if (avio_r8(pb) != 1)
223 return AVERROR_INVALIDDATA;
227 return AVERROR(ENOMEM);
228 st->codec->codec_id = AV_CODEC_ID_BINTEXT;
230 st->codec->extradata_size = 2 + 48 + 4096;
231 st->codec->extradata = av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
232 if (!st->codec->extradata)
233 return AVERROR(ENOMEM);
234 st->codec->extradata[0] = 16;
235 st->codec->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;
237 if (avio_read(pb, st->codec->extradata + 2, 24) < 0)
240 if (avio_read(pb, st->codec->extradata + 2 + 24, 24) < 0)
242 if (avio_read(pb, st->codec->extradata + 2 + 48, 4096) < 0)
247 bin->fsize = avio_size(pb) - 1 - 192 - 4096;
248 st->codec->width = 80<<3;
249 ff_sauce_read(s, &bin->fsize, &got_width, 0);
251 calculate_height(st->codec, bin->fsize);
252 avio_seek(pb, 1 + 192 + 4096, SEEK_SET);
256 #endif /* CONFIG_ADF_DEMUXER */
258 #if CONFIG_IDF_DEMUXER
259 static const uint8_t idf_magic[] = {
260 0x04, 0x31, 0x2e, 0x34, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x00, 0x15, 0x00
263 static int idf_probe(AVProbeData *p)
265 if (p->buf_size < sizeof(idf_magic))
267 if (!memcmp(p->buf, idf_magic, sizeof(idf_magic)))
268 return AVPROBE_SCORE_MAX;
272 static int idf_read_header(AVFormatContext *s)
274 BinDemuxContext *bin = s->priv_data;
275 AVIOContext *pb = s->pb;
284 return AVERROR(ENOMEM);
285 st->codec->codec_id = AV_CODEC_ID_IDF;
287 st->codec->extradata_size = 2 + 48 + 4096;
288 st->codec->extradata = av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
289 if (!st->codec->extradata)
290 return AVERROR(ENOMEM);
291 st->codec->extradata[0] = 16;
292 st->codec->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;
294 avio_seek(pb, avio_size(pb) - 4096 - 48, SEEK_SET);
296 if (avio_read(pb, st->codec->extradata + 2 + 48, 4096) < 0)
298 if (avio_read(pb, st->codec->extradata + 2, 48) < 0)
301 bin->fsize = avio_size(pb) - 12 - 4096 - 48;
302 ff_sauce_read(s, &bin->fsize, &got_width, 0);
304 calculate_height(st->codec, bin->fsize);
305 avio_seek(pb, 12, SEEK_SET);
308 #endif /* CONFIG_IDF_DEMUXER */
310 static int read_packet(AVFormatContext *s,
313 BinDemuxContext *bin = s->priv_data;
315 if (bin->fsize > 0) {
316 if (av_get_packet(s->pb, pkt, bin->fsize) < 0)
318 bin->fsize = -1; /* done */
319 } else if (!bin->fsize) {
322 if (av_get_packet(s->pb, pkt, bin->chars_per_frame) < 0)
328 pkt->flags |= AV_PKT_FLAG_KEY;
332 #define OFFSET(x) offsetof(BinDemuxContext, x)
333 static const AVOption options[] = {
334 { "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},
335 { "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 },
336 { "framerate", "set framerate (frames per second)", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
340 #define CLASS(name) \
341 (const AVClass[1]){{ \
342 .class_name = name, \
343 .item_name = av_default_item_name, \
345 .version = LIBAVUTIL_VERSION_INT, \
348 #if CONFIG_BINTEXT_DEMUXER
349 AVInputFormat ff_bintext_demuxer = {
351 .long_name = NULL_IF_CONFIG_SMALL("Binary text"),
352 .priv_data_size = sizeof(BinDemuxContext),
353 .read_header = bintext_read_header,
354 .read_packet = read_packet,
356 .priv_class = CLASS("Binary text demuxer"),
360 #if CONFIG_XBIN_DEMUXER
361 AVInputFormat ff_xbin_demuxer = {
363 .long_name = NULL_IF_CONFIG_SMALL("eXtended BINary text (XBIN)"),
364 .priv_data_size = sizeof(BinDemuxContext),
365 .read_probe = xbin_probe,
366 .read_header = xbin_read_header,
367 .read_packet = read_packet,
368 .priv_class = CLASS("eXtended BINary text (XBIN) demuxer"),
372 #if CONFIG_ADF_DEMUXER
373 AVInputFormat ff_adf_demuxer = {
375 .long_name = NULL_IF_CONFIG_SMALL("Artworx Data Format"),
376 .priv_data_size = sizeof(BinDemuxContext),
377 .read_header = adf_read_header,
378 .read_packet = read_packet,
380 .priv_class = CLASS("Artworx Data Format demuxer"),
384 #if CONFIG_IDF_DEMUXER
385 AVInputFormat ff_idf_demuxer = {
387 .long_name = NULL_IF_CONFIG_SMALL("iCE Draw File"),
388 .priv_data_size = sizeof(BinDemuxContext),
389 .read_probe = idf_probe,
390 .read_header = idf_read_header,
391 .read_packet = read_packet,
393 .priv_class = CLASS("iCE Draw File demuxer"),