FFmpeg
Loading...
Searching...
No Matches
bintext.c
Go to the documentation of this file.
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
36#include "libavutil/opt.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
45typedef 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 */
54
56{
57 BinDemuxContext *bin = s->priv_data;
59 if (!st)
60 return NULL;
61 st->codecpar->codec_tag = 0;
63
64 if (!bin->width) {
65 st->codecpar->width = (80<<3);
66 st->codecpar->height = (25<<4);
67 }
68
69 avpriv_set_pts_info(st, 60, bin->framerate.den, bin->framerate.num);
70
71 /* simulate tty display speed */
72 bin->chars_per_frame = av_clip(av_q2d(st->time_base) * bin->chars_per_frame, 1, INT_MAX);
73
74 return st;
75}
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 */
81static void calculate_height(AVCodecParameters *par, uint64_t fsize)
82{
83 par->height = (fsize / ((par->width>>3)*2)) << 4;
84}
85#endif
86
87#if CONFIG_BINTEXT_DEMUXER
88static const uint8_t next_magic[]={
89 0x1A, 0x1B, '[', '0', ';', '3', '0', ';', '4', '0', 'm', 'N', 'E', 'X', 'T', 0x00
90};
91
92static int next_tag_read(AVFormatContext *avctx, uint64_t *fsize)
93{
94 AVIOContext *pb = avctx->pb;
95 char buf[36];
96 int len;
97 int64_t start_pos = avio_size(pb);
98
99 if (start_pos < 256)
100 return AVERROR_INVALIDDATA;
101
102 avio_seek(pb, start_pos - 256, SEEK_SET);
103 if ((len = ffio_read_size(pb, buf, sizeof(next_magic))) < 0)
104 return len;
105 if (memcmp(buf, next_magic, sizeof(next_magic)))
106 return -1;
107 if (avio_r8(pb) != 0x01)
108 return -1;
109
110 *fsize -= 256;
111
112#define GET_EFI2_META(name,size) \
113 len = avio_r8(pb); \
114 if (len < 1 || len > size) \
115 return -1; \
116 if (avio_read(pb, buf, size) == size && *buf) { \
117 buf[len] = 0; \
118 av_dict_set(&avctx->metadata, name, buf, 0); \
119 }
120
121 GET_EFI2_META("filename", 12)
122 GET_EFI2_META("author", 20)
123 GET_EFI2_META("publisher", 20)
124 GET_EFI2_META("title", 35)
125
126 return 0;
127}
128
129static void predict_width(AVCodecParameters *par, uint64_t fsize, int got_width)
130{
131 /** attempt to guess width */
132 if (!got_width)
133 par->width = fsize > 4000 ? (160<<3) : (80<<3);
134}
135
136static int bin_probe(const AVProbeData *p)
137{
138 const uint8_t *d = p->buf;
139 int magic = 0, sauce = 0;
140
141 if (p->buf_size > 256)
142 magic = !memcmp(d + p->buf_size - 256, next_magic, sizeof(next_magic));
143 if (p->buf_size > 128)
144 sauce = !memcmp(d + p->buf_size - 128, "SAUCE00", 7);
145
146 if (magic)
147 return AVPROBE_SCORE_EXTENSION + 1;
148
149 if (av_match_ext(p->filename, "bin")) {
151 int got_width = 0;
152 par.width = par.height = 0;
153 if (sauce)
154 return AVPROBE_SCORE_EXTENSION + 1;
155
156 predict_width(&par, p->buf_size, got_width);
157 if (par.width < 8)
158 return 0;
159 calculate_height(&par, p->buf_size);
160 if (par.height <= 0)
161 return 0;
162
163 if (par.width * par.height * 2 / (8*16) == p->buf_size)
164 return AVPROBE_SCORE_MAX / 2;
165 return 0;
166 }
167
168 if (sauce)
169 return 1;
170
171 return 0;
172}
173
174
175static int bintext_read_header(AVFormatContext *s)
176{
177 BinDemuxContext *bin = s->priv_data;
178 AVIOContext *pb = s->pb;
179 int ret;
180 AVStream *st = init_stream(s);
181 if (!st)
182 return AVERROR(ENOMEM);
184
185 if ((ret = ff_alloc_extradata(st->codecpar, 2)) < 0)
186 return ret;
187 st->codecpar->extradata[0] = 16;
188 st->codecpar->extradata[1] = 0;
189
190 if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
191 int got_width = 0;
192 bin->fsize = avio_size(pb);
193 if (ff_sauce_read(s, &bin->fsize, &got_width, 0) < 0)
194 next_tag_read(s, &bin->fsize);
195 if (!bin->width) {
196 predict_width(st->codecpar, bin->fsize, got_width);
197 if (st->codecpar->width < 8)
198 return AVERROR_INVALIDDATA;
199 calculate_height(st->codecpar, bin->fsize);
200 }
201 avio_seek(pb, 0, SEEK_SET);
202 }
203 return 0;
204}
205#endif /* CONFIG_BINTEXT_DEMUXER */
206
207#if CONFIG_XBIN_DEMUXER
208static int xbin_probe(const AVProbeData *p)
209{
210 const uint8_t *d = p->buf;
211
212 if (AV_RL32(d) == MKTAG('X','B','I','N') && d[4] == 0x1A &&
213 AV_RL16(d+5) > 0 && AV_RL16(d+5) <= 160 &&
214 d[9] > 0 && d[9] <= 32)
215 return AVPROBE_SCORE_MAX;
216 return 0;
217}
218
219static int xbin_read_header(AVFormatContext *s)
220{
221 BinDemuxContext *bin = s->priv_data;
222 AVIOContext *pb = s->pb;
223 char fontheight, flags;
224 int ret;
225 AVStream *st = init_stream(s);
226 if (!st)
227 return AVERROR(ENOMEM);
228
229 avio_skip(pb, 5);
230 st->codecpar->width = avio_rl16(pb)<<3;
231 st->codecpar->height = avio_rl16(pb);
232 fontheight = avio_r8(pb);
233 st->codecpar->height *= fontheight;
234 flags = avio_r8(pb);
235
236 st->codecpar->extradata_size = 2;
237 if ((flags & BINTEXT_PALETTE))
238 st->codecpar->extradata_size += 48;
239 if ((flags & BINTEXT_FONT))
240 st->codecpar->extradata_size += fontheight * (flags & 0x10 ? 512 : 256);
242
244 if (ret < 0)
245 return ret;
246 st->codecpar->extradata[0] = fontheight;
247 st->codecpar->extradata[1] = flags;
248 if ((ret = ffio_read_size(pb, st->codecpar->extradata + 2, st->codecpar->extradata_size - 2)) < 0)
249 return ret;
250
251 if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
252 int64_t fsize = avio_size(pb);
253 if (fsize < 9 + st->codecpar->extradata_size)
254 return 0;
255 bin->fsize = fsize - 9 - st->codecpar->extradata_size;
256 ff_sauce_read(s, &bin->fsize, NULL, 0);
257 avio_seek(pb, 9 + st->codecpar->extradata_size, SEEK_SET);
258 }
259
260 return 0;
261}
262#endif /* CONFIG_XBIN_DEMUXER */
263
264#if CONFIG_ADF_DEMUXER
265static int adf_read_header(AVFormatContext *s)
266{
267 BinDemuxContext *bin = s->priv_data;
268 AVIOContext *pb = s->pb;
269 AVStream *st;
270 int ret;
271
272 if (avio_r8(pb) != 1)
273 return AVERROR_INVALIDDATA;
274
275 st = init_stream(s);
276 if (!st)
277 return AVERROR(ENOMEM);
279
280 if ((ret = ff_alloc_extradata(st->codecpar, 2 + 48 + 4096)) < 0)
281 return ret;
282 st->codecpar->extradata[0] = 16;
284
285 if ((ret = ffio_read_size(pb, st->codecpar->extradata + 2, 24)) < 0)
286 return ret;
287 avio_skip(pb, 144);
288 if ((ret = ffio_read_size(pb, st->codecpar->extradata + 2 + 24, 24)) < 0)
289 return ret;
290 if ((ret = ffio_read_size(pb, st->codecpar->extradata + 2 + 48, 4096)) < 0)
291 return ret;
292
293 if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
294 int got_width = 0;
295 int64_t fsize = avio_size(pb);
296 if (fsize < 1 + 192 + 4096)
297 return 0;
298 bin->fsize = fsize - 1 - 192 - 4096;
299 st->codecpar->width = 80<<3;
300 ff_sauce_read(s, &bin->fsize, &got_width, 0);
301 if (st->codecpar->width < 8)
302 return AVERROR_INVALIDDATA;
303 if (!bin->width)
304 calculate_height(st->codecpar, bin->fsize);
305 avio_seek(pb, 1 + 192 + 4096, SEEK_SET);
306 }
307 return 0;
308}
309#endif /* CONFIG_ADF_DEMUXER */
310
311#if CONFIG_IDF_DEMUXER
312static const uint8_t idf_magic[] = {
313 0x04, 0x31, 0x2e, 0x34, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x00, 0x15, 0x00
314};
315
316static int idf_probe(const AVProbeData *p)
317{
318 if (p->buf_size < sizeof(idf_magic))
319 return 0;
320 if (!memcmp(p->buf, idf_magic, sizeof(idf_magic)))
321 return AVPROBE_SCORE_MAX;
322 return 0;
323}
324
325static int idf_read_header(AVFormatContext *s)
326{
327 BinDemuxContext *bin = s->priv_data;
328 AVIOContext *pb = s->pb;
329 AVStream *st;
330 int got_width = 0, ret;
332
333 if (!(pb->seekable & AVIO_SEEKABLE_NORMAL))
334 return AVERROR_INVALIDDATA;
335
336 st = init_stream(s);
337 if (!st)
338 return AVERROR(ENOMEM);
340
341 if ((ret = ff_alloc_extradata(st->codecpar, 2 + 48 + 4096)) < 0)
342 return ret;
343 st->codecpar->extradata[0] = 16;
345
346 fsize = avio_size(pb);
347 if (fsize < 12 + 4096 + 48)
348 return AVERROR_INVALIDDATA;
349 bin->fsize = fsize - 12 - 4096 - 48;
350
351 avio_seek(pb, bin->fsize + 12, SEEK_SET);
352
353 if ((ret = ffio_read_size(pb, st->codecpar->extradata + 2 + 48, 4096)) < 0)
354 return ret;
355 if ((ret = ffio_read_size(pb, st->codecpar->extradata + 2, 48)) < 0)
356 return ret;
357
358 ff_sauce_read(s, &bin->fsize, &got_width, 0);
359 if (st->codecpar->width < 8)
360 return AVERROR_INVALIDDATA;
361 if (!bin->width)
362 calculate_height(st->codecpar, bin->fsize);
363 avio_seek(pb, 12, SEEK_SET);
364 return 0;
365}
366#endif /* CONFIG_IDF_DEMUXER */
367
369 AVPacket *pkt)
370{
371 BinDemuxContext *bin = s->priv_data;
372
373 if (bin->fsize > 0) {
374 if (av_get_packet(s->pb, pkt, bin->fsize) < 0)
375 return AVERROR_INVALIDDATA;
376 bin->fsize = -1; /* done */
377 } else if (!bin->fsize) {
378 if (avio_feof(s->pb))
379 return AVERROR_INVALIDDATA;
380 if (av_get_packet(s->pb, pkt, bin->chars_per_frame) < 0)
381 return AVERROR_INVALIDDATA;
382 } else {
383 return AVERROR_INVALIDDATA;
384 }
385
386 pkt->flags |= AV_PKT_FLAG_KEY;
387 return 0;
388}
389
390#define OFFSET(x) offsetof(BinDemuxContext, x)
391static 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
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,
415};
416#endif
417
418#if CONFIG_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,
427};
428#endif
429
430#if CONFIG_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,
439};
440#endif
441
442#if CONFIG_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,
452};
453#endif
const FFInputFormat ff_idf_demuxer
const FFInputFormat ff_adf_demuxer
const FFInputFormat ff_xbin_demuxer
const FFInputFormat ff_bintext_demuxer
return
static int64_t fsize(FILE *f)
Definition audiomatch.c:29
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition avformat.h:481
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition utils.c:98
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition avio.h:41
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition aviobuf.c:326
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
unsigned int avio_rl16(AVIOContext *s)
Definition aviobuf.c:717
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Binary text decoder.
#define BINTEXT_FONT
Definition bintext.h:35
#define BINTEXT_PALETTE
Definition bintext.h:34
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define av_clip
Definition common.h:100
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
#define AV_OPT_FLAG_DECODING_PARAM
A generic parameter which can be set by the user for demuxing or decoding.
Definition opt.h:355
@ AV_OPT_TYPE_IMAGE_SIZE
Underlying C type is two consecutive integers.
Definition opt.h:302
@ AV_OPT_TYPE_VIDEO_RATE
Underlying C type is AVRational.
Definition opt.h:314
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_CODEC_ID_IDF
Definition codec_id.h:602
@ AV_CODEC_ID_XBIN
Definition codec_id.h:601
@ AV_CODEC_ID_BINTEXT
Definition codec_id.h:600
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
int av_match_ext(const char *filename, const char *extensions)
Return a positive value if the given filename has one of the given extensions, 0 otherwise.
Definition format.c:41
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition rational.h:104
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define AV_RL32(p)
#define AV_RL16(p)
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition bintext.c:368
static AVStream * init_stream(AVFormatContext *s)
Definition bintext.c:55
#define CLASS(name)
Definition bintext.c:398
#define OFFSET(x)
Definition bintext.c:390
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0.
Definition utils.c:237
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define MKTAG(a, b, c, d)
Definition macros.h:55
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
AVOptions.
misc parsing utilities
int ff_sauce_read(AVFormatContext *avctx, uint64_t *fsize, int *got_width, int get_height)
Definition sauce.c:32
SAUCE header parser.
Describe the class of an AVClass context structure.
Definition log.h:76
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
int extradata_size
Size of the extradata content in bytes.
Definition codec_par.h:75
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int width
The width of the video frame in pixels.
Definition codec_par.h:143
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
Format I/O context.
Definition avformat.h:1333
AVIOContext * pb
I/O context.
Definition avformat.h:1375
Bytestream IO Context.
Definition avio.h:160
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition avio.h:261
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avformat.h:805
int height
video size (WxH pixels) (private option)
Definition bintext.c:50
uint64_t fsize
file size less metadata buffer
Definition bintext.c:52
AVRational framerate
frames per second (private option)
Definition bintext.c:51
int chars_per_frame
characters to send decoder per frame; set by private options as characters per second,...
Definition bintext.c:47
float framerate
Definition av1_levels.c:29
#define width
Definition dsp.h:89
int len