1 /*
2  * Binary text demuxer
3  * eXtended BINary text (XBIN) demuxer
4  * Artworx Data Format demuxer
5  * iCEDraw File demuxer
6  * Copyright (c) 2010 Peter Ross <pross@xvid.org>
7  *
8  * This file is part of FFmpeg.
9  *
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.
14  *
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.
19  *
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
23  */
24 
25 /**
26  * @file
27  * Binary text demuxer
28  * eXtended BINary text (XBIN) demuxer
29  * Artworx Data Format demuxer
30  * iCEDraw File demuxer
31  */
32 
33 #include "config_components.h"
34 
35 #include "libavutil/intreadwrite.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/parseutils.h"
38 #include "avformat.h"
39 #include "internal.h"
40 #include "sauce.h"
41 #include "libavcodec/bintext.h"
42 
43 typedef struct {
44     const AVClass *class;
45     int chars_per_frame; /**< characters to send decoder per frame;
46                               set by private options as characters per second, and then
47                               converted to characters per frame at runtime */
48     int width, height;    /**< video size (WxH pixels) (private option) */
49     AVRational framerate; /**< frames per second (private option) */
50     uint64_t fsize;  /**< file size less metadata buffer */
51 } BinDemuxContext;
52 
init_stream(AVFormatContext *s)53 static AVStream * init_stream(AVFormatContext *s)
54 {
55     BinDemuxContext *bin = s->priv_data;
56     AVStream *st = avformat_new_stream(s, NULL);
57     if (!st)
58         return NULL;
59     st->codecpar->codec_tag   = 0;
60     st->codecpar->codec_type  = AVMEDIA_TYPE_VIDEO;
61 
62     if (!bin->width) {
63         st->codecpar->width  = (80<<3);
64         st->codecpar->height = (25<<4);
65     }
66 
67     avpriv_set_pts_info(st, 60, bin->framerate.den, bin->framerate.num);
68 
69     /* simulate tty display speed */
70     bin->chars_per_frame = av_clip(av_q2d(st->time_base) * bin->chars_per_frame, 1, INT_MAX);
71 
72     return st;
73 }
74 
75 #if CONFIG_BINTEXT_DEMUXER | CONFIG_ADF_DEMUXER | CONFIG_IDF_DEMUXER
76 /**
77  * Given filesize and width, calculate height (assume font_height of 16)
78  */
calculate_height(AVCodecParameters *par, uint64_t fsize)79 static void calculate_height(AVCodecParameters *par, uint64_t fsize)
80 {
81     par->height = (fsize / ((par->width>>3)*2)) << 4;
82 }
83 #endif
84 
85 #if CONFIG_BINTEXT_DEMUXER
86 static const uint8_t next_magic[]={
87     0x1A, 0x1B, '[', '0', ';', '3', '0', ';', '4', '0', 'm', 'N', 'E', 'X', 'T', 0x00
88 };
89 
next_tag_read(AVFormatContext *avctx, uint64_t *fsize)90 static int next_tag_read(AVFormatContext *avctx, uint64_t *fsize)
91 {
92     AVIOContext *pb = avctx->pb;
93     char buf[36];
94     int len;
95     uint64_t start_pos = avio_size(pb) - 256;
96 
97     avio_seek(pb, start_pos, SEEK_SET);
98     if (avio_read(pb, buf, sizeof(next_magic)) != sizeof(next_magic))
99         return -1;
100     if (memcmp(buf, next_magic, sizeof(next_magic)))
101         return -1;
102     if (avio_r8(pb) != 0x01)
103         return -1;
104 
105     *fsize -= 256;
106 
107 #define GET_EFI2_META(name,size) \
108     len = avio_r8(pb); \
109     if (len < 1 || len > size) \
110         return -1; \
111     if (avio_read(pb, buf, size) == size && *buf) { \
112         buf[len] = 0; \
113         av_dict_set(&avctx->metadata, name, buf, 0); \
114     }
115 
116     GET_EFI2_META("filename",  12)
117     GET_EFI2_META("author",    20)
118     GET_EFI2_META("publisher", 20)
119     GET_EFI2_META("title",     35)
120 
121     return 0;
122 }
123 
predict_width(AVCodecParameters *par, uint64_t fsize, int got_width)124 static void predict_width(AVCodecParameters *par, uint64_t fsize, int got_width)
125 {
126     /** attempt to guess width */
127     if (!got_width)
128         par->width = fsize > 4000 ? (160<<3) : (80<<3);
129 }
130 
bin_probe(const AVProbeData *p)131 static int bin_probe(const AVProbeData *p)
132 {
133     const uint8_t *d = p->buf;
134     int magic = 0, sauce = 0;
135 
136     if (p->buf_size > 256)
137         magic = !memcmp(d + p->buf_size - 256, next_magic, sizeof(next_magic));
138     if (p->buf_size > 128)
139         sauce = !memcmp(d + p->buf_size - 128, "SAUCE00", 7);
140 
141     if (magic)
142         return AVPROBE_SCORE_EXTENSION + 1;
143 
144     if (av_match_ext(p->filename, "bin")) {
145         AVCodecParameters par;
146         int got_width = 0;
147         par.width = par.height = 0;
148         if (sauce)
149             return AVPROBE_SCORE_EXTENSION + 1;
150 
151         predict_width(&par, p->buf_size, got_width);
152         if (par.width < 8)
153             return 0;
154         calculate_height(&par, p->buf_size);
155         if (par.height <= 0)
156             return 0;
157 
158         if (par.width * par.height * 2 / (8*16) == p->buf_size)
159             return AVPROBE_SCORE_MAX / 2;
160         return 0;
161     }
162 
163     if (sauce)
164         return 1;
165 
166     return 0;
167 }
168 
169 
bintext_read_header(AVFormatContext *s)170 static int bintext_read_header(AVFormatContext *s)
171 {
172     BinDemuxContext *bin = s->priv_data;
173     AVIOContext *pb = s->pb;
174     int ret;
175     AVStream *st = init_stream(s);
176     if (!st)
177         return AVERROR(ENOMEM);
178     st->codecpar->codec_id    = AV_CODEC_ID_BINTEXT;
179 
180     if ((ret = ff_alloc_extradata(st->codecpar, 2)) < 0)
181         return ret;
182     st->codecpar->extradata[0] = 16;
183     st->codecpar->extradata[1] = 0;
184 
185     if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
186         int got_width = 0;
187         bin->fsize = avio_size(pb);
188         if (ff_sauce_read(s, &bin->fsize, &got_width, 0) < 0)
189             next_tag_read(s, &bin->fsize);
190         if (!bin->width) {
191             predict_width(st->codecpar, bin->fsize, got_width);
192             if (st->codecpar->width < 8)
193                 return AVERROR_INVALIDDATA;
194             calculate_height(st->codecpar, bin->fsize);
195         }
196         avio_seek(pb, 0, SEEK_SET);
197     }
198     return 0;
199 }
200 #endif /* CONFIG_BINTEXT_DEMUXER */
201 
202 #if CONFIG_XBIN_DEMUXER
xbin_probe(const AVProbeData *p)203 static int xbin_probe(const AVProbeData *p)
204 {
205     const uint8_t *d = p->buf;
206 
207     if (AV_RL32(d) == MKTAG('X','B','I','N') && d[4] == 0x1A &&
208         AV_RL16(d+5) > 0 && AV_RL16(d+5) <= 160 &&
209         d[9] > 0 && d[9] <= 32)
210         return AVPROBE_SCORE_MAX;
211     return 0;
212 }
213 
xbin_read_header(AVFormatContext *s)214 static int xbin_read_header(AVFormatContext *s)
215 {
216     BinDemuxContext *bin = s->priv_data;
217     AVIOContext *pb = s->pb;
218     char fontheight, flags;
219     int ret;
220     AVStream *st = init_stream(s);
221     if (!st)
222         return AVERROR(ENOMEM);
223 
224     avio_skip(pb, 5);
225     st->codecpar->width   = avio_rl16(pb)<<3;
226     st->codecpar->height  = avio_rl16(pb);
227     fontheight         = avio_r8(pb);
228     st->codecpar->height *= fontheight;
229     flags              = avio_r8(pb);
230 
231     st->codecpar->extradata_size = 2;
232     if ((flags & BINTEXT_PALETTE))
233         st->codecpar->extradata_size += 48;
234     if ((flags & BINTEXT_FONT))
235         st->codecpar->extradata_size += fontheight * (flags & 0x10 ? 512 : 256);
236     st->codecpar->codec_id    = flags & 4 ? AV_CODEC_ID_XBIN : AV_CODEC_ID_BINTEXT;
237 
238     ret = ff_alloc_extradata(st->codecpar, st->codecpar->extradata_size);
239     if (ret < 0)
240         return ret;
241     st->codecpar->extradata[0] = fontheight;
242     st->codecpar->extradata[1] = flags;
243     if (avio_read(pb, st->codecpar->extradata + 2, st->codecpar->extradata_size - 2) < 0)
244         return AVERROR(EIO);
245 
246     if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
247         bin->fsize = avio_size(pb) - 9 - st->codecpar->extradata_size;
248         ff_sauce_read(s, &bin->fsize, NULL, 0);
249         avio_seek(pb, 9 + st->codecpar->extradata_size, SEEK_SET);
250     }
251 
252     return 0;
253 }
254 #endif /* CONFIG_XBIN_DEMUXER */
255 
256 #if CONFIG_ADF_DEMUXER
adf_read_header(AVFormatContext *s)257 static int adf_read_header(AVFormatContext *s)
258 {
259     BinDemuxContext *bin = s->priv_data;
260     AVIOContext *pb = s->pb;
261     AVStream *st;
262     int ret;
263 
264     if (avio_r8(pb) != 1)
265         return AVERROR_INVALIDDATA;
266 
267     st = init_stream(s);
268     if (!st)
269         return AVERROR(ENOMEM);
270     st->codecpar->codec_id    = AV_CODEC_ID_BINTEXT;
271 
272     if ((ret = ff_alloc_extradata(st->codecpar, 2 + 48 + 4096)) < 0)
273         return ret;
274     st->codecpar->extradata[0] = 16;
275     st->codecpar->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;
276 
277     if (avio_read(pb, st->codecpar->extradata + 2, 24) < 0)
278         return AVERROR(EIO);
279     avio_skip(pb, 144);
280     if (avio_read(pb, st->codecpar->extradata + 2 + 24, 24) < 0)
281         return AVERROR(EIO);
282     if (avio_read(pb, st->codecpar->extradata + 2 + 48, 4096) < 0)
283         return AVERROR(EIO);
284 
285     if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
286         int got_width = 0;
287         bin->fsize = avio_size(pb) - 1 - 192 - 4096;
288         st->codecpar->width = 80<<3;
289         ff_sauce_read(s, &bin->fsize, &got_width, 0);
290         if (st->codecpar->width < 8)
291             return AVERROR_INVALIDDATA;
292         if (!bin->width)
293             calculate_height(st->codecpar, bin->fsize);
294         avio_seek(pb, 1 + 192 + 4096, SEEK_SET);
295     }
296     return 0;
297 }
298 #endif /* CONFIG_ADF_DEMUXER */
299 
300 #if CONFIG_IDF_DEMUXER
301 static const uint8_t idf_magic[] = {
302     0x04, 0x31, 0x2e, 0x34, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x00, 0x15, 0x00
303 };
304 
idf_probe(const AVProbeData *p)305 static int idf_probe(const AVProbeData *p)
306 {
307     if (p->buf_size < sizeof(idf_magic))
308         return 0;
309     if (!memcmp(p->buf, idf_magic, sizeof(idf_magic)))
310         return AVPROBE_SCORE_MAX;
311     return 0;
312 }
313 
idf_read_header(AVFormatContext *s)314 static int idf_read_header(AVFormatContext *s)
315 {
316     BinDemuxContext *bin = s->priv_data;
317     AVIOContext *pb = s->pb;
318     AVStream *st;
319     int got_width = 0, ret;
320 
321     if (!(pb->seekable & AVIO_SEEKABLE_NORMAL))
322         return AVERROR(EIO);
323 
324     st = init_stream(s);
325     if (!st)
326         return AVERROR(ENOMEM);
327     st->codecpar->codec_id    = AV_CODEC_ID_IDF;
328 
329     if ((ret = ff_alloc_extradata(st->codecpar, 2 + 48 + 4096)) < 0)
330         return ret;
331     st->codecpar->extradata[0] = 16;
332     st->codecpar->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;
333 
334     avio_seek(pb, avio_size(pb) - 4096 - 48, SEEK_SET);
335 
336     if (avio_read(pb, st->codecpar->extradata + 2 + 48, 4096) < 0)
337         return AVERROR(EIO);
338     if (avio_read(pb, st->codecpar->extradata + 2, 48) < 0)
339         return AVERROR(EIO);
340 
341     bin->fsize = avio_size(pb) - 12 - 4096 - 48;
342     ff_sauce_read(s, &bin->fsize, &got_width, 0);
343     if (st->codecpar->width < 8)
344         return AVERROR_INVALIDDATA;
345     if (!bin->width)
346         calculate_height(st->codecpar, bin->fsize);
347     avio_seek(pb, 12, SEEK_SET);
348     return 0;
349 }
350 #endif /* CONFIG_IDF_DEMUXER */
351 
read_packet(AVFormatContext *s, AVPacket *pkt)352 static int read_packet(AVFormatContext *s,
353                            AVPacket *pkt)
354 {
355     BinDemuxContext *bin = s->priv_data;
356 
357     if (bin->fsize > 0) {
358         if (av_get_packet(s->pb, pkt, bin->fsize) < 0)
359             return AVERROR(EIO);
360         bin->fsize = -1; /* done */
361     } else if (!bin->fsize) {
362         if (avio_feof(s->pb))
363             return AVERROR(EIO);
364         if (av_get_packet(s->pb, pkt, bin->chars_per_frame) < 0)
365             return AVERROR(EIO);
366     } else {
367         return AVERROR(EIO);
368     }
369 
370     pkt->flags |= AV_PKT_FLAG_KEY;
371     return 0;
372 }
373 
374 #define OFFSET(x) offsetof(BinDemuxContext, x)
375 static const AVOption options[] = {
376     { "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},
377     { "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 },
378     { "framerate", "set framerate (frames per second)", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
379     { NULL },
380 };
381 
382 #define CLASS(name) \
383 (const AVClass[1]){{ \
384     .class_name     = name, \
385     .item_name      = av_default_item_name, \
386     .option         = options, \
387     .version        = LIBAVUTIL_VERSION_INT, \
388 }}
389 
390 #if CONFIG_BINTEXT_DEMUXER
391 const AVInputFormat ff_bintext_demuxer = {
392     .name           = "bin",
393     .long_name      = NULL_IF_CONFIG_SMALL("Binary text"),
394     .priv_data_size = sizeof(BinDemuxContext),
395     .read_probe     = bin_probe,
396     .read_header    = bintext_read_header,
397     .read_packet    = read_packet,
398     .priv_class     = CLASS("Binary text demuxer"),
399 };
400 #endif
401 
402 #if CONFIG_XBIN_DEMUXER
403 const AVInputFormat ff_xbin_demuxer = {
404     .name           = "xbin",
405     .long_name      = NULL_IF_CONFIG_SMALL("eXtended BINary text (XBIN)"),
406     .priv_data_size = sizeof(BinDemuxContext),
407     .read_probe     = xbin_probe,
408     .read_header    = xbin_read_header,
409     .read_packet    = read_packet,
410     .priv_class     = CLASS("eXtended BINary text (XBIN) demuxer"),
411 };
412 #endif
413 
414 #if CONFIG_ADF_DEMUXER
415 const AVInputFormat ff_adf_demuxer = {
416     .name           = "adf",
417     .long_name      = NULL_IF_CONFIG_SMALL("Artworx Data Format"),
418     .priv_data_size = sizeof(BinDemuxContext),
419     .read_header    = adf_read_header,
420     .read_packet    = read_packet,
421     .extensions     = "adf",
422     .priv_class     = CLASS("Artworx Data Format demuxer"),
423 };
424 #endif
425 
426 #if CONFIG_IDF_DEMUXER
427 const AVInputFormat ff_idf_demuxer = {
428     .name           = "idf",
429     .long_name      = NULL_IF_CONFIG_SMALL("iCE Draw File"),
430     .priv_data_size = sizeof(BinDemuxContext),
431     .read_probe     = idf_probe,
432     .read_header    = idf_read_header,
433     .read_packet    = read_packet,
434     .extensions     = "idf",
435     .priv_class     = CLASS("iCE Draw File demuxer"),
436 };
437 #endif
438