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