FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "libavutil/intreadwrite.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/parseutils.h"
36 #include "avformat.h"
37 #include "internal.h"
38 #include "sauce.h"
39 #include "libavcodec/bintext.h"
40 
41 typedef struct {
42  const AVClass *class;
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  int width, height; /**< video size (WxH pixels) (private option) */
47  AVRational framerate; /**< frames per second (private option) */
48  uint64_t fsize; /**< file size less metadata buffer */
50 
52 {
53  BinDemuxContext *bin = s->priv_data;
55  if (!st)
56  return NULL;
57  st->codecpar->codec_tag = 0;
59 
60  if (!bin->width) {
61  st->codecpar->width = (80<<3);
62  st->codecpar->height = (25<<4);
63  }
64 
65  avpriv_set_pts_info(st, 60, bin->framerate.den, bin->framerate.num);
66 
67  /* simulate tty display speed */
68  bin->chars_per_frame = av_clip(av_q2d(st->time_base) * bin->chars_per_frame, 1, INT_MAX);
69 
70  return st;
71 }
72 
73 #if CONFIG_BINTEXT_DEMUXER | CONFIG_ADF_DEMUXER | CONFIG_IDF_DEMUXER
74 /**
75  * Given filesize and width, calculate height (assume font_height of 16)
76  */
77 static void calculate_height(AVCodecParameters *par, uint64_t fsize)
78 {
79  par->height = (fsize / ((par->width>>3)*2)) << 4;
80 }
81 #endif
82 
83 #if CONFIG_BINTEXT_DEMUXER
84 static const uint8_t next_magic[]={
85  0x1A, 0x1B, '[', '0', ';', '3', '0', ';', '4', '0', 'm', 'N', 'E', 'X', 'T', 0x00
86 };
87 
88 static int next_tag_read(AVFormatContext *avctx, uint64_t *fsize)
89 {
90  AVIOContext *pb = avctx->pb;
91  char buf[36];
92  int len;
93  uint64_t start_pos = avio_size(pb) - 256;
94 
95  avio_seek(pb, start_pos, SEEK_SET);
96  if (avio_read(pb, buf, sizeof(next_magic)) != sizeof(next_magic))
97  return -1;
98  if (memcmp(buf, next_magic, sizeof(next_magic)))
99  return -1;
100  if (avio_r8(pb) != 0x01)
101  return -1;
102 
103  *fsize -= 256;
104 
105 #define GET_EFI2_META(name,size) \
106  len = avio_r8(pb); \
107  if (len < 1 || len > size) \
108  return -1; \
109  if (avio_read(pb, buf, size) == size && *buf) { \
110  buf[len] = 0; \
111  av_dict_set(&avctx->metadata, name, buf, 0); \
112  }
113 
114  GET_EFI2_META("filename", 12)
115  GET_EFI2_META("author", 20)
116  GET_EFI2_META("publisher", 20)
117  GET_EFI2_META("title", 35)
118 
119  return 0;
120 }
121 
122 static void predict_width(AVCodecParameters *par, uint64_t fsize, int got_width)
123 {
124  /** attempt to guess width */
125  if (!got_width)
126  par->width = fsize > 4000 ? (160<<3) : (80<<3);
127 }
128 
129 static int bintext_read_header(AVFormatContext *s)
130 {
131  BinDemuxContext *bin = s->priv_data;
132  AVIOContext *pb = s->pb;
133 
134  AVStream *st = init_stream(s);
135  if (!st)
136  return AVERROR(ENOMEM);
138 
139  if (ff_alloc_extradata(st->codecpar, 2))
140  return AVERROR(ENOMEM);
141  st->codecpar->extradata[0] = 16;
142  st->codecpar->extradata[1] = 0;
143 
144  if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
145  int got_width = 0;
146  bin->fsize = avio_size(pb);
147  if (ff_sauce_read(s, &bin->fsize, &got_width, 0) < 0)
148  next_tag_read(s, &bin->fsize);
149  if (!bin->width) {
150  predict_width(st->codecpar, bin->fsize, got_width);
151  calculate_height(st->codecpar, bin->fsize);
152  }
153  avio_seek(pb, 0, SEEK_SET);
154  }
155  return 0;
156 }
157 #endif /* CONFIG_BINTEXT_DEMUXER */
158 
159 #if CONFIG_XBIN_DEMUXER
160 static int xbin_probe(AVProbeData *p)
161 {
162  const uint8_t *d = p->buf;
163 
164  if (AV_RL32(d) == MKTAG('X','B','I','N') && d[4] == 0x1A &&
165  AV_RL16(d+5) > 0 && AV_RL16(d+5) <= 160 &&
166  d[9] > 0 && d[9] <= 32)
167  return AVPROBE_SCORE_MAX;
168  return 0;
169 }
170 
171 static int xbin_read_header(AVFormatContext *s)
172 {
173  BinDemuxContext *bin = s->priv_data;
174  AVIOContext *pb = s->pb;
175  char fontheight, flags;
176 
177  AVStream *st = init_stream(s);
178  if (!st)
179  return AVERROR(ENOMEM);
180 
181  avio_skip(pb, 5);
182  st->codecpar->width = avio_rl16(pb)<<3;
183  st->codecpar->height = avio_rl16(pb);
184  fontheight = avio_r8(pb);
185  st->codecpar->height *= fontheight;
186  flags = avio_r8(pb);
187 
188  st->codecpar->extradata_size = 2;
189  if ((flags & BINTEXT_PALETTE))
190  st->codecpar->extradata_size += 48;
191  if ((flags & BINTEXT_FONT))
192  st->codecpar->extradata_size += fontheight * (flags & 0x10 ? 512 : 256);
194 
196  return AVERROR(ENOMEM);
197  st->codecpar->extradata[0] = fontheight;
198  st->codecpar->extradata[1] = flags;
199  if (avio_read(pb, st->codecpar->extradata + 2, st->codecpar->extradata_size - 2) < 0)
200  return AVERROR(EIO);
201 
202  if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
203  bin->fsize = avio_size(pb) - 9 - st->codecpar->extradata_size;
204  ff_sauce_read(s, &bin->fsize, NULL, 0);
205  avio_seek(pb, 9 + st->codecpar->extradata_size, SEEK_SET);
206  }
207 
208  return 0;
209 }
210 #endif /* CONFIG_XBIN_DEMUXER */
211 
212 #if CONFIG_ADF_DEMUXER
213 static int adf_read_header(AVFormatContext *s)
214 {
215  BinDemuxContext *bin = s->priv_data;
216  AVIOContext *pb = s->pb;
217  AVStream *st;
218 
219  if (avio_r8(pb) != 1)
220  return AVERROR_INVALIDDATA;
221 
222  st = init_stream(s);
223  if (!st)
224  return AVERROR(ENOMEM);
226 
227  if (ff_alloc_extradata(st->codecpar, 2 + 48 + 4096))
228  return AVERROR(ENOMEM);
229  st->codecpar->extradata[0] = 16;
230  st->codecpar->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;
231 
232  if (avio_read(pb, st->codecpar->extradata + 2, 24) < 0)
233  return AVERROR(EIO);
234  avio_skip(pb, 144);
235  if (avio_read(pb, st->codecpar->extradata + 2 + 24, 24) < 0)
236  return AVERROR(EIO);
237  if (avio_read(pb, st->codecpar->extradata + 2 + 48, 4096) < 0)
238  return AVERROR(EIO);
239 
240  if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
241  int got_width = 0;
242  bin->fsize = avio_size(pb) - 1 - 192 - 4096;
243  st->codecpar->width = 80<<3;
244  ff_sauce_read(s, &bin->fsize, &got_width, 0);
245  if (!bin->width)
246  calculate_height(st->codecpar, bin->fsize);
247  avio_seek(pb, 1 + 192 + 4096, SEEK_SET);
248  }
249  return 0;
250 }
251 #endif /* CONFIG_ADF_DEMUXER */
252 
253 #if CONFIG_IDF_DEMUXER
254 static const uint8_t idf_magic[] = {
255  0x04, 0x31, 0x2e, 0x34, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x00, 0x15, 0x00
256 };
257 
258 static int idf_probe(AVProbeData *p)
259 {
260  if (p->buf_size < sizeof(idf_magic))
261  return 0;
262  if (!memcmp(p->buf, idf_magic, sizeof(idf_magic)))
263  return AVPROBE_SCORE_MAX;
264  return 0;
265 }
266 
267 static int idf_read_header(AVFormatContext *s)
268 {
269  BinDemuxContext *bin = s->priv_data;
270  AVIOContext *pb = s->pb;
271  AVStream *st;
272  int got_width = 0;
273 
274  if (!(pb->seekable & AVIO_SEEKABLE_NORMAL))
275  return AVERROR(EIO);
276 
277  st = init_stream(s);
278  if (!st)
279  return AVERROR(ENOMEM);
281 
282  if (ff_alloc_extradata(st->codecpar, 2 + 48 + 4096))
283  return AVERROR(ENOMEM);
284  st->codecpar->extradata[0] = 16;
285  st->codecpar->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;
286 
287  avio_seek(pb, avio_size(pb) - 4096 - 48, SEEK_SET);
288 
289  if (avio_read(pb, st->codecpar->extradata + 2 + 48, 4096) < 0)
290  return AVERROR(EIO);
291  if (avio_read(pb, st->codecpar->extradata + 2, 48) < 0)
292  return AVERROR(EIO);
293 
294  bin->fsize = avio_size(pb) - 12 - 4096 - 48;
295  ff_sauce_read(s, &bin->fsize, &got_width, 0);
296  if (!bin->width)
297  calculate_height(st->codecpar, bin->fsize);
298  avio_seek(pb, 12, SEEK_SET);
299  return 0;
300 }
301 #endif /* CONFIG_IDF_DEMUXER */
302 
304  AVPacket *pkt)
305 {
306  BinDemuxContext *bin = s->priv_data;
307 
308  if (bin->fsize > 0) {
309  if (av_get_packet(s->pb, pkt, bin->fsize) < 0)
310  return AVERROR(EIO);
311  bin->fsize = -1; /* done */
312  } else if (!bin->fsize) {
313  if (avio_feof(s->pb))
314  return AVERROR(EIO);
315  if (av_get_packet(s->pb, pkt, bin->chars_per_frame) < 0)
316  return AVERROR(EIO);
317  } else {
318  return AVERROR(EIO);
319  }
320 
321  pkt->flags |= AV_PKT_FLAG_KEY;
322  return 0;
323 }
324 
325 #define OFFSET(x) offsetof(BinDemuxContext, x)
326 static const AVOption options[] = {
327  { "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},
328  { "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 },
329  { "framerate", "set framerate (frames per second)", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
330  { NULL },
331 };
332 
333 #define CLASS(name) \
334 (const AVClass[1]){{ \
335  .class_name = name, \
336  .item_name = av_default_item_name, \
337  .option = options, \
338  .version = LIBAVUTIL_VERSION_INT, \
339 }}
340 
341 #if CONFIG_BINTEXT_DEMUXER
342 AVInputFormat ff_bintext_demuxer = {
343  .name = "bin",
344  .long_name = NULL_IF_CONFIG_SMALL("Binary text"),
345  .priv_data_size = sizeof(BinDemuxContext),
346  .read_header = bintext_read_header,
348  .extensions = "bin",
349  .priv_class = CLASS("Binary text demuxer"),
350 };
351 #endif
352 
353 #if CONFIG_XBIN_DEMUXER
354 AVInputFormat ff_xbin_demuxer = {
355  .name = "xbin",
356  .long_name = NULL_IF_CONFIG_SMALL("eXtended BINary text (XBIN)"),
357  .priv_data_size = sizeof(BinDemuxContext),
358  .read_probe = xbin_probe,
359  .read_header = xbin_read_header,
361  .priv_class = CLASS("eXtended BINary text (XBIN) demuxer"),
362 };
363 #endif
364 
365 #if CONFIG_ADF_DEMUXER
366 AVInputFormat ff_adf_demuxer = {
367  .name = "adf",
368  .long_name = NULL_IF_CONFIG_SMALL("Artworx Data Format"),
369  .priv_data_size = sizeof(BinDemuxContext),
370  .read_header = adf_read_header,
372  .extensions = "adf",
373  .priv_class = CLASS("Artworx Data Format demuxer"),
374 };
375 #endif
376 
377 #if CONFIG_IDF_DEMUXER
378 AVInputFormat ff_idf_demuxer = {
379  .name = "idf",
380  .long_name = NULL_IF_CONFIG_SMALL("iCE Draw File"),
381  .priv_data_size = sizeof(BinDemuxContext),
382  .read_probe = idf_probe,
383  .read_header = idf_read_header,
385  .extensions = "idf",
386  .priv_class = CLASS("iCE Draw File demuxer"),
387 };
388 #endif
Binary text decoder.
#define NULL
Definition: coverity.c:32
#define OFFSET(x)
Definition: bintext.c:325
const char * s
Definition: avisynth_c.h:768
Bytestream IO Context.
Definition: avio.h:155
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:321
AVOption.
Definition: opt.h:246
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4601
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:4066
int num
Numerator.
Definition: rational.h:59
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:231
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:316
static AVPacket pkt
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:87
This struct describes the properties of an encoded stream.
Definition: avcodec.h:4058
int chars_per_frame
characters to send decoder per frame; set by private options as characters per second, and then converted to characters per frame at runtime
Definition: bintext.c:43
Format I/O context.
Definition: avformat.h:1349
uint8_t
int width
Video only.
Definition: avcodec.h:4132
AVOptions.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4231
#define height
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
static int flags
Definition: log.c:57
static const AVOption options[]
Definition: bintext.c:326
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:289
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:616
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1689
#define BINTEXT_PALETTE
Definition: bintext.h:34
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
int ff_sauce_read(AVFormatContext *avctx, uint64_t *fsize, int *got_width, int get_height)
Definition: sauce.c:32
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:4062
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1663
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:4084
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:607
return
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:464
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:463
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:251
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:3213
#define width
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
uint64_t fsize
file size less metadata buffer
Definition: bintext.c:48
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:510
Stream structure.
Definition: avformat.h:889
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:40
AVRational framerate
frames per second (private option)
Definition: bintext.c:47
AVIOContext * pb
I/O context.
Definition: avformat.h:1391
void * buf
Definition: avisynth_c.h:690
Describe the class of an AVClass context structure.
Definition: log.h:67
Rational number (pair of numerator and denominator).
Definition: rational.h:58
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:277
offset must point to AVRational
Definition: opt.h:236
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: bintext.c:303
offset must point to two consecutive integers
Definition: opt.h:233
This structure contains the data a format has to probe a file.
Definition: avformat.h:461
misc parsing utilities
SAUCE header parser.
static AVStream * init_stream(AVFormatContext *s)
Definition: bintext.c:51
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:473
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:722
Main libavformat public API header.
int den
Denominator.
Definition: rational.h:60
int len
void * priv_data
Format private data.
Definition: avformat.h:1377
#define CLASS(name)
Definition: bintext.c:333
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:4080
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:664
AVCodecParameters * codecpar
Definition: avformat.h:1252
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:340
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:4070
static int64_t fsize(FILE *f)
Definition: audiomatch.c:28
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:926
#define MKTAG(a, b, c, d)
Definition: common.h:342
#define BINTEXT_FONT
Definition: bintext.h:35
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1634