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