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