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 char *video_size; /**< video size (WxH pixels) (private option) */
47 char *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;
60 if (bin->video_size) {
61 if (av_parse_video_size(&st->codec->width, &st->codec->height, bin->video_size) < 0) {
62 av_log(s, AV_LOG_ERROR, "Could not parse video size: '%s'\n", bin->video_size);
66 st->codec->width = (80<<3);
67 st->codec->height = (25<<4);
72 if (av_parse_video_rate(&framerate, bin->framerate) < 0) {
73 av_log(s, AV_LOG_ERROR, "Could not parse framerate: '%s'\n", bin->framerate);
76 avpriv_set_pts_info(st, 60, framerate.den, framerate.num);
78 avpriv_set_pts_info(st, 60, 1, 25);
81 /* simulate tty display speed */
82 bin->chars_per_frame = FFMAX(av_q2d(st->time_base) * bin->chars_per_frame, 1);
87 #if CONFIG_BINTEXT_DEMUXER | CONFIG_ADF_DEMUXER | CONFIG_IDF_DEMUXER
89 * Given filesize and width, calculate height (assume font_height of 16)
91 static void calculate_height(AVCodecContext *avctx, uint64_t fsize)
93 avctx->height = (fsize / ((avctx->width>>3)*2)) << 4;
97 #if CONFIG_BINTEXT_DEMUXER
98 static const uint8_t next_magic[]={
99 0x1A, 0x1B, '[', '0', ';', '3', '0', ';', '4', '0', 'm', 'N', 'E', 'X', 'T', 0x00
102 static int next_tag_read(AVFormatContext *avctx, uint64_t *fsize)
104 AVIOContext *pb = avctx->pb;
107 uint64_t start_pos = avio_size(pb) - 256;
109 avio_seek(pb, start_pos, SEEK_SET);
110 if (avio_read(pb, buf, sizeof(next_magic)) != sizeof(next_magic))
112 if (memcmp(buf, next_magic, sizeof(next_magic)))
114 if (avio_r8(pb) != 0x01)
119 #define GET_EFI2_META(name,size) \
121 if (len < 1 || len > size) \
123 if (avio_read(pb, buf, size) == size && *buf) { \
125 av_dict_set(&avctx->metadata, name, buf, 0); \
128 GET_EFI2_META("filename", 12)
129 GET_EFI2_META("author", 20)
130 GET_EFI2_META("publisher", 20)
131 GET_EFI2_META("title", 35)
136 static void predict_width(AVCodecContext *avctx, uint64_t fsize, int got_width)
138 /** attempt to guess width */
140 avctx->width = fsize > 4000 ? (160<<3) : (80<<3);
143 static int bintext_read_header(AVFormatContext *s)
145 BinDemuxContext *bin = s->priv_data;
146 AVIOContext *pb = s->pb;
148 AVStream *st = init_stream(s);
150 return AVERROR(ENOMEM);
151 st->codec->codec_id = CODEC_ID_BINTEXT;
153 st->codec->extradata_size = 2;
154 st->codec->extradata = av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
155 if (!st->codec->extradata)
156 return AVERROR(ENOMEM);
157 st->codec->extradata[0] = 16;
158 st->codec->extradata[1] = 0;
162 bin->fsize = avio_size(pb);
163 if (ff_sauce_read(s, &bin->fsize, &got_width, 0) < 0)
164 next_tag_read(s, &bin->fsize);
165 if (!bin->video_size) {
166 predict_width(st->codec, bin->fsize, got_width);
167 calculate_height(st->codec, bin->fsize);
169 avio_seek(pb, 0, SEEK_SET);
173 #endif /* CONFIG_BINTEXT_DEMUXER */
175 #if CONFIG_XBIN_DEMUXER
176 static int xbin_probe(AVProbeData *p)
178 const uint8_t *d = p->buf;
180 if (AV_RL32(d) == MKTAG('X','B','I','N') && d[4] == 0x1A &&
181 AV_RL16(d+5) > 0 && AV_RL16(d+5) <= 160 &&
182 d[9] > 0 && d[9] <= 32)
183 return AVPROBE_SCORE_MAX;
187 static int xbin_read_header(AVFormatContext *s)
189 BinDemuxContext *bin = s->priv_data;
190 AVIOContext *pb = s->pb;
191 char fontheight, flags;
193 AVStream *st = init_stream(s);
195 return AVERROR(ENOMEM);
198 st->codec->width = avio_rl16(pb)<<3;
199 st->codec->height = avio_rl16(pb);
200 fontheight = avio_r8(pb);
201 st->codec->height *= fontheight;
204 st->codec->extradata_size = 2;
205 if ((flags & BINTEXT_PALETTE))
206 st->codec->extradata_size += 48;
207 if ((flags & BINTEXT_FONT))
208 st->codec->extradata_size += fontheight * (flags & 0x10 ? 512 : 256);
209 st->codec->codec_id = flags & 4 ? CODEC_ID_XBIN : CODEC_ID_BINTEXT;
211 st->codec->extradata = av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
212 if (!st->codec->extradata)
213 return AVERROR(ENOMEM);
214 st->codec->extradata[0] = fontheight;
215 st->codec->extradata[1] = flags;
216 if (avio_read(pb, st->codec->extradata + 2, st->codec->extradata_size - 2) < 0)
220 bin->fsize = avio_size(pb) - 9 - st->codec->extradata_size;
221 ff_sauce_read(s, &bin->fsize, NULL, 0);
222 avio_seek(pb, 9 + st->codec->extradata_size, SEEK_SET);
227 #endif /* CONFIG_XBIN_DEMUXER */
229 #if CONFIG_ADF_DEMUXER
230 static int adf_read_header(AVFormatContext *s)
232 BinDemuxContext *bin = s->priv_data;
233 AVIOContext *pb = s->pb;
236 if (avio_r8(pb) != 1)
237 return AVERROR_INVALIDDATA;
241 return AVERROR(ENOMEM);
242 st->codec->codec_id = CODEC_ID_BINTEXT;
244 st->codec->extradata_size = 2 + 48 + 4096;
245 st->codec->extradata = av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
246 if (!st->codec->extradata)
247 return AVERROR(ENOMEM);
248 st->codec->extradata[0] = 16;
249 st->codec->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;
251 if (avio_read(pb, st->codec->extradata + 2, 24) < 0)
254 if (avio_read(pb, st->codec->extradata + 2 + 24, 24) < 0)
256 if (avio_read(pb, st->codec->extradata + 2 + 48, 4096) < 0)
261 bin->fsize = avio_size(pb) - 1 - 192 - 4096;
262 st->codec->width = 80<<3;
263 ff_sauce_read(s, &bin->fsize, &got_width, 0);
264 if (!bin->video_size)
265 calculate_height(st->codec, bin->fsize);
266 avio_seek(pb, 1 + 192 + 4096, SEEK_SET);
270 #endif /* CONFIG_ADF_DEMUXER */
272 #if CONFIG_IDF_DEMUXER
273 static const uint8_t idf_magic[] = {
274 0x04, 0x31, 0x2e, 0x34, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x00, 0x15, 0x00
277 static int idf_probe(AVProbeData *p)
279 if (p->buf_size < sizeof(idf_magic))
281 if (!memcmp(p->buf, idf_magic, sizeof(idf_magic)))
282 return AVPROBE_SCORE_MAX;
286 static int idf_read_header(AVFormatContext *s)
288 BinDemuxContext *bin = s->priv_data;
289 AVIOContext *pb = s->pb;
298 return AVERROR(ENOMEM);
299 st->codec->codec_id = CODEC_ID_IDF;
301 st->codec->extradata_size = 2 + 48 + 4096;
302 st->codec->extradata = av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
303 if (!st->codec->extradata)
304 return AVERROR(ENOMEM);
305 st->codec->extradata[0] = 16;
306 st->codec->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;
308 avio_seek(pb, avio_size(pb) - 4096 - 48, SEEK_SET);
310 if (avio_read(pb, st->codec->extradata + 2 + 48, 4096) < 0)
312 if (avio_read(pb, st->codec->extradata + 2, 48) < 0)
315 bin->fsize = avio_size(pb) - 12 - 4096 - 48;
316 ff_sauce_read(s, &bin->fsize, &got_width, 0);
317 if (!bin->video_size)
318 calculate_height(st->codec, bin->fsize);
319 avio_seek(pb, 12, SEEK_SET);
322 #endif /* CONFIG_IDF_DEMUXER */
324 static int read_packet(AVFormatContext *s,
327 BinDemuxContext *bin = s->priv_data;
329 if (bin->fsize > 0) {
330 if (av_get_packet(s->pb, pkt, bin->fsize) < 0)
332 bin->fsize = -1; /* done */
333 } else if (!bin->fsize) {
336 if (av_get_packet(s->pb, pkt, bin->chars_per_frame) < 0)
342 pkt->flags |= AV_PKT_FLAG_KEY;
346 #define OFFSET(x) offsetof(BinDemuxContext, x)
347 static const AVOption options[] = {
348 { "linespeed", "set simulated line speed (bytes per second)", OFFSET(chars_per_frame), AV_OPT_TYPE_INT, {.dbl = 6000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM},
349 { "video_size", "set video size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
350 { "framerate", "set framerate (frames per second)", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
354 #define CLASS(name) \
355 (const AVClass[1]){{ \
356 .class_name = name, \
357 .item_name = av_default_item_name, \
359 .version = LIBAVUTIL_VERSION_INT, \
362 #if CONFIG_BINTEXT_DEMUXER
363 AVInputFormat ff_bintext_demuxer = {
365 .long_name = NULL_IF_CONFIG_SMALL("Binary text"),
366 .priv_data_size = sizeof(BinDemuxContext),
367 .read_header = bintext_read_header,
368 .read_packet = read_packet,
370 .priv_class = CLASS("Binary text demuxer"),
374 #if CONFIG_XBIN_DEMUXER
375 AVInputFormat ff_xbin_demuxer = {
377 .long_name = NULL_IF_CONFIG_SMALL("eXtended BINary text (XBIN)"),
378 .priv_data_size = sizeof(BinDemuxContext),
379 .read_probe = xbin_probe,
380 .read_header = xbin_read_header,
381 .read_packet = read_packet,
382 .priv_class = CLASS("eXtended BINary text (XBIN) demuxer"),
386 #if CONFIG_ADF_DEMUXER
387 AVInputFormat ff_adf_demuxer = {
389 .long_name = NULL_IF_CONFIG_SMALL("Artworx Data Format"),
390 .priv_data_size = sizeof(BinDemuxContext),
391 .read_header = adf_read_header,
392 .read_packet = read_packet,
394 .priv_class = CLASS("Artworx Data Format demuxer"),
398 #if CONFIG_IDF_DEMUXER
399 AVInputFormat ff_idf_demuxer = {
401 .long_name = NULL_IF_CONFIG_SMALL("iCE Draw File"),
402 .priv_data_size = sizeof(BinDemuxContext),
403 .read_probe = idf_probe,
404 .read_header = idf_read_header,
405 .read_packet = read_packet,
407 .priv_class = CLASS("iCE Draw File demuxer"),