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