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"
37 #include "libavcodec/bintext.h"
39 #define LINE_RATE 6000 /** characters per second */
43 uint64_t fsize; /**< file size less metadata buffer */
46 #if CONFIG_BINTEXT_DEMUXER | CONFIG_ADF_DEMUXER | CONFIG_IDF_DEMUXER
48 * Given filesize and width, calculate height (assume font_height of 16)
50 static void calculate_height(AVCodecContext *avctx, uint64_t fsize)
52 avctx->height = (fsize / ((avctx->width>>3)*2)) << 4;
56 #if CONFIG_BINTEXT_DEMUXER
57 static const uint8_t next_magic[]={
58 0x1A, 0x1B, '[', '0', ';', '3', '0', ';', '4', '0', 'm', 'N', 'E', 'X', 'T', 0x00
61 static int next_tag_read(AVFormatContext *avctx, uint64_t *fsize)
63 AVIOContext *pb = avctx->pb;
66 uint64_t start_pos = avio_size(pb) - 256;
68 avio_seek(pb, start_pos, SEEK_SET);
69 if (avio_read(pb, buf, sizeof(next_magic)) != sizeof(next_magic))
71 if (memcmp(buf, next_magic, sizeof(next_magic)))
73 if (avio_r8(pb) != 0x01)
78 #define GET_EFI2_META(name,size) \
80 if (len < 1 || len > size) \
82 if (avio_read(pb, buf, size) == size && *buf) { \
84 av_dict_set(&avctx->metadata, name, buf, 0); \
87 GET_EFI2_META("filename", 12)
88 GET_EFI2_META("author", 20)
89 GET_EFI2_META("publisher", 20)
90 GET_EFI2_META("title", 35)
95 static void predict_width(AVCodecContext *avctx, uint64_t fsize, int got_width)
97 /** attempt to guess width */
99 avctx->width = fsize > 4000 ? (160<<3) : (80<<3);
102 static AVStream * init_stream(AVFormatContext *s)
104 BinDemuxContext *bin = s->priv_data;
105 AVStream *st = avformat_new_stream(s, NULL);
108 st->codec->codec_tag = 0;
109 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
111 // if (!ap->time_base.num) {
112 avpriv_set_pts_info(st, 60, 1, 25);
114 // avpriv_set_pts_info(st, 60, ap->time_base.num, ap->time_base.den);
117 /* simulate tty display speed */
118 bin->chars_per_frame = FFMAX(av_q2d(st->time_base) * (/*ap->sample_rate ? ap->sample_rate :*/ LINE_RATE), 1);
120 st->codec->width = /*ap->width ? ap->width :*/ (80<<3);
121 st->codec->height = /*ap->height ? ap->height :*/ (25<<4);
125 static int bintext_read_header(AVFormatContext *s)
127 BinDemuxContext *bin = s->priv_data;
128 AVIOContext *pb = s->pb;
130 AVStream *st = init_stream(s);
132 return AVERROR(ENOMEM);
133 st->codec->codec_id = CODEC_ID_BINTEXT;
135 st->codec->extradata_size = 2;
136 st->codec->extradata = av_malloc(st->codec->extradata_size);
137 if (!st->codec->extradata)
138 return AVERROR(ENOMEM);
139 st->codec->extradata[0] = 16;
140 st->codec->extradata[1] = 0;
144 bin->fsize = avio_size(pb);
145 if (ff_sauce_read(s, &bin->fsize, &got_width, 0) < 0)
146 next_tag_read(s, &bin->fsize);
148 predict_width(st->codec, bin->fsize, got_width);
150 calculate_height(st->codec, bin->fsize);
151 avio_seek(pb, 0, SEEK_SET);
155 #endif /* CONFIG_BINTEXT_DEMUXER */
157 #if CONFIG_XBIN_DEMUXER
158 static int xbin_probe(AVProbeData *p)
160 const uint8_t *d = p->buf;
162 if (AV_RL32(d) == MKTAG('X','B','I','N') && d[4] == 0x1A &&
163 AV_RL16(d+5) > 0 && AV_RL16(d+5) <= 160 &&
164 d[9] > 0 && d[9] <= 32)
165 return AVPROBE_SCORE_MAX;
169 static int xbin_read_header(AVFormatContext *s)
171 BinDemuxContext *bin = s->priv_data;
172 AVIOContext *pb = s->pb;
173 char fontheight, flags;
175 AVStream *st = init_stream(s);
177 return AVERROR(ENOMEM);
180 st->codec->width = avio_rl16(pb)<<3;
181 st->codec->height = avio_rl16(pb);
182 fontheight = avio_r8(pb);
183 st->codec->height *= fontheight;
186 st->codec->extradata_size = 2;
187 if ((flags & BINTEXT_PALETTE))
188 st->codec->extradata_size += 48;
189 if ((flags & BINTEXT_FONT))
190 st->codec->extradata_size += fontheight * (flags & 0x10 ? 512 : 256);
191 st->codec->codec_id = flags & 4 ? CODEC_ID_XBIN : CODEC_ID_BINTEXT;
193 st->codec->extradata = av_malloc(st->codec->extradata_size);
194 if (!st->codec->extradata)
195 return AVERROR(ENOMEM);
196 st->codec->extradata[0] = fontheight;
197 st->codec->extradata[1] = flags;
198 if (avio_read(pb, st->codec->extradata + 2, st->codec->extradata_size - 2) < 0)
202 bin->fsize = avio_size(pb) - 9 - st->codec->extradata_size;
203 ff_sauce_read(s, &bin->fsize, NULL, 0);
204 avio_seek(pb, 9 + st->codec->extradata_size, SEEK_SET);
209 #endif /* CONFIG_XBIN_DEMUXER */
211 #if CONFIG_ADF_DEMUXER
212 static int adf_read_header(AVFormatContext *s)
214 BinDemuxContext *bin = s->priv_data;
215 AVIOContext *pb = s->pb;
218 if (avio_r8(pb) != 1)
219 return AVERROR_INVALIDDATA;
223 return AVERROR(ENOMEM);
224 st->codec->codec_id = CODEC_ID_BINTEXT;
226 st->codec->extradata_size = 2 + 48 + 4096;
227 st->codec->extradata = av_malloc(st->codec->extradata_size);
228 if (!st->codec->extradata)
229 return AVERROR(ENOMEM);
230 st->codec->extradata[0] = 16;
231 st->codec->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;
233 if (avio_read(pb, st->codec->extradata + 2, 24) < 0)
236 if (avio_read(pb, st->codec->extradata + 2 + 24, 24) < 0)
238 if (avio_read(pb, st->codec->extradata + 2 + 48, 4096) < 0)
243 bin->fsize = avio_size(pb) - 1 - 192 - 4096;
244 st->codec->width = 80<<3;
245 ff_sauce_read(s, &bin->fsize, &got_width, 0);
247 calculate_height(st->codec, bin->fsize);
248 avio_seek(pb, 1 + 192 + 4096, SEEK_SET);
252 #endif /* CONFIG_ADF_DEMUXER */
254 #if CONFIG_IDF_DEMUXER
255 static const uint8_t idf_magic[] = {
256 0x04, 0x31, 0x2e, 0x34, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x00, 0x15, 0x00
259 static int idf_probe(AVProbeData *p)
261 if (p->buf_size < sizeof(idf_magic))
263 if (!memcmp(p->buf, idf_magic, sizeof(idf_magic)))
264 return AVPROBE_SCORE_MAX;
268 static int idf_read_header(AVFormatContext *s)
270 BinDemuxContext *bin = s->priv_data;
271 AVIOContext *pb = s->pb;
280 return AVERROR(ENOMEM);
281 st->codec->codec_id = CODEC_ID_IDF;
283 st->codec->extradata_size = 2 + 48 + 4096;
284 st->codec->extradata = av_malloc(st->codec->extradata_size);
285 if (!st->codec->extradata)
286 return AVERROR(ENOMEM);
287 st->codec->extradata[0] = 16;
288 st->codec->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;
290 avio_seek(pb, avio_size(pb) - 4096 - 48, SEEK_SET);
292 if (avio_read(pb, st->codec->extradata + 2 + 48, 4096) < 0)
294 if (avio_read(pb, st->codec->extradata + 2, 48) < 0)
297 bin->fsize = avio_size(pb) - 12 - 4096 - 48;
298 ff_sauce_read(s, &bin->fsize, &got_width, 0);
300 calculate_height(st->codec, bin->fsize);
301 avio_seek(pb, 12, SEEK_SET);
304 #endif /* CONFIG_IDF_DEMUXER */
306 static int read_packet(AVFormatContext *s,
309 BinDemuxContext *bin = s->priv_data;
311 if (bin->fsize > 0) {
312 if (av_get_packet(s->pb, pkt, bin->fsize) < 0)
314 bin->fsize = -1; /* done */
315 } else if (!bin->fsize) {
318 if (av_get_packet(s->pb, pkt, bin->chars_per_frame) < 0)
324 pkt->flags |= AV_PKT_FLAG_KEY;
328 #if CONFIG_BINTEXT_DEMUXER
329 AVInputFormat ff_bintext_demuxer = {
331 .long_name = NULL_IF_CONFIG_SMALL("Binary text"),
332 .priv_data_size = sizeof(BinDemuxContext),
333 .read_header = bintext_read_header,
334 .read_packet = read_packet,
339 #if CONFIG_XBIN_DEMUXER
340 AVInputFormat ff_xbin_demuxer = {
342 .long_name = NULL_IF_CONFIG_SMALL("eXtended BINary text (XBIN)"),
343 .priv_data_size = sizeof(BinDemuxContext),
344 .read_probe = xbin_probe,
345 .read_header = xbin_read_header,
346 .read_packet = read_packet,
350 #if CONFIG_ADF_DEMUXER
351 AVInputFormat ff_adf_demuxer = {
353 .long_name = NULL_IF_CONFIG_SMALL("Artworx Data Format"),
354 .priv_data_size = sizeof(BinDemuxContext),
355 .read_header = adf_read_header,
356 .read_packet = read_packet,
361 #if CONFIG_IDF_DEMUXER
362 AVInputFormat ff_idf_demuxer = {
364 .long_name = NULL_IF_CONFIG_SMALL("iCE Draw File"),
365 .priv_data_size = sizeof(BinDemuxContext),
366 .read_probe = idf_probe,
367 .read_header = idf_read_header,
368 .read_packet = read_packet,