]> git.sesse.net Git - ffmpeg/blob - libavformat/bintext.c
smackerdemuxer: check some values before instead of just after malloc()
[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                               AVFormatParameters *ap)
104 {
105     BinDemuxContext *bin = s->priv_data;
106     AVStream *st = avformat_new_stream(s, NULL);
107     if (!st)
108         return NULL;
109     st->codec->codec_tag   = 0;
110     st->codec->codec_type  = AVMEDIA_TYPE_VIDEO;
111
112     if (!ap->time_base.num) {
113         avpriv_set_pts_info(st, 60, 1, 25);
114     } else {
115         avpriv_set_pts_info(st, 60, ap->time_base.num, ap->time_base.den);
116     }
117
118     /* simulate tty display speed */
119     bin->chars_per_frame = FFMAX(av_q2d(st->time_base) * (ap->sample_rate ? ap->sample_rate : LINE_RATE), 1);
120
121     st->codec->width  = ap->width  ? ap->width  : (80<<3);
122     st->codec->height = ap->height ? ap->height : (25<<4);
123     return st;
124 }
125
126 static int bintext_read_header(AVFormatContext *s,
127                                AVFormatParameters *ap)
128 {
129     BinDemuxContext *bin = s->priv_data;
130     AVIOContext *pb = s->pb;
131
132     AVStream *st = init_stream(s, ap);
133     if (!st)
134         return AVERROR(ENOMEM);
135     st->codec->codec_id    = CODEC_ID_BINTEXT;
136
137     st->codec->extradata_size = 2;
138     st->codec->extradata = av_malloc(st->codec->extradata_size);
139     if (!st->codec->extradata)
140         return AVERROR(ENOMEM);
141     st->codec->extradata[0] = 16;
142     st->codec->extradata[1] = 0;
143
144     if (pb->seekable) {
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 (!ap->width)
150             predict_width(st->codec, bin->fsize, got_width);
151         if (!ap->height)
152             calculate_height(st->codec, bin->fsize);
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                            AVFormatParameters *ap)
173 {
174     BinDemuxContext *bin = s->priv_data;
175     AVIOContext *pb = s->pb;
176     char fontheight, flags;
177
178     AVStream *st = init_stream(s, ap);
179     if (!st)
180         return AVERROR(ENOMEM);
181
182     avio_skip(pb, 5);
183     st->codec->width   = avio_rl16(pb)<<3;
184     st->codec->height  = avio_rl16(pb);
185     fontheight         = avio_r8(pb);
186     st->codec->height *= fontheight;
187     flags              = avio_r8(pb);
188
189     st->codec->extradata_size = 2;
190     if ((flags & BINTEXT_PALETTE))
191         st->codec->extradata_size += 48;
192     if ((flags & BINTEXT_FONT))
193         st->codec->extradata_size += fontheight * (flags & 0x10 ? 512 : 256);
194     st->codec->codec_id    = flags & 4 ? CODEC_ID_XBIN : CODEC_ID_BINTEXT;
195
196     st->codec->extradata = av_malloc(st->codec->extradata_size);
197     if (!st->codec->extradata)
198         return AVERROR(ENOMEM);
199     st->codec->extradata[0] = fontheight;
200     st->codec->extradata[1] = flags;
201     if (avio_read(pb, st->codec->extradata + 2, st->codec->extradata_size - 2) < 0)
202         return AVERROR(EIO);
203
204     if (pb->seekable) {
205         bin->fsize = avio_size(pb) - 9 - st->codec->extradata_size;
206         ff_sauce_read(s, &bin->fsize, NULL, 0);
207         avio_seek(pb, 9 + st->codec->extradata_size, SEEK_SET);
208     }
209
210     return 0;
211 }
212 #endif /* CONFIG_XBIN_DEMUXER */
213
214 #if CONFIG_ADF_DEMUXER
215 static int adf_read_header(AVFormatContext *s,
216                            AVFormatParameters *ap)
217 {
218     BinDemuxContext *bin = s->priv_data;
219     AVIOContext *pb = s->pb;
220     AVStream *st;
221
222     if (avio_r8(pb) != 1)
223         return AVERROR_INVALIDDATA;
224
225     st = init_stream(s, ap);
226     if (!st)
227         return AVERROR(ENOMEM);
228     st->codec->codec_id    = CODEC_ID_BINTEXT;
229
230     st->codec->extradata_size = 2 + 48 + 4096;
231     st->codec->extradata = av_malloc(st->codec->extradata_size);
232     if (!st->codec->extradata)
233         return AVERROR(ENOMEM);
234     st->codec->extradata[0] = 16;
235     st->codec->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;
236
237     if (avio_read(pb, st->codec->extradata + 2, 24) < 0)
238         return AVERROR(EIO);
239     avio_skip(pb, 144);
240     if (avio_read(pb, st->codec->extradata + 2 + 24, 24) < 0)
241         return AVERROR(EIO);
242     if (avio_read(pb, st->codec->extradata + 2 + 48, 4096) < 0)
243         return AVERROR(EIO);
244
245     if (pb->seekable) {
246         int got_width = 0;
247         bin->fsize = avio_size(pb) - 1 - 192 - 4096;
248         st->codec->width = 80<<3;
249         ff_sauce_read(s, &bin->fsize, &got_width, 0);
250         if (!ap->height)
251             calculate_height(st->codec, bin->fsize);
252         avio_seek(pb, 1 + 192 + 4096, SEEK_SET);
253     }
254     return 0;
255 }
256 #endif /* CONFIG_ADF_DEMUXER */
257
258 #if CONFIG_IDF_DEMUXER
259 static const uint8_t idf_magic[] = {
260     0x04, 0x31, 0x2e, 0x34, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x00, 0x15, 0x00
261 };
262
263 static int idf_probe(AVProbeData *p)
264 {
265     if (p->buf_size < sizeof(idf_magic))
266         return 0;
267     if (!memcmp(p->buf, idf_magic, sizeof(idf_magic)))
268         return AVPROBE_SCORE_MAX;
269     return 0;
270 }
271
272 static int idf_read_header(AVFormatContext *s,
273                            AVFormatParameters *ap)
274 {
275     BinDemuxContext *bin = s->priv_data;
276     AVIOContext *pb = s->pb;
277     AVStream *st;
278     int got_width = 0;
279
280     if (!pb->seekable)
281         return AVERROR(EIO);
282
283     st = init_stream(s, ap);
284     if (!st)
285         return AVERROR(ENOMEM);
286     st->codec->codec_id    = CODEC_ID_IDF;
287
288     st->codec->extradata_size = 2 + 48 + 4096;
289     st->codec->extradata = av_malloc(st->codec->extradata_size);
290     if (!st->codec->extradata)
291         return AVERROR(ENOMEM);
292     st->codec->extradata[0] = 16;
293     st->codec->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;
294
295     avio_seek(pb, avio_size(pb) - 4096 - 48, SEEK_SET);
296
297     if (avio_read(pb, st->codec->extradata + 2 + 48, 4096) < 0)
298         return AVERROR(EIO);
299     if (avio_read(pb, st->codec->extradata + 2, 48) < 0)
300         return AVERROR(EIO);
301
302     bin->fsize = avio_size(pb) - 12 - 4096 - 48;
303     ff_sauce_read(s, &bin->fsize, &got_width, 0);
304     if (!ap->height)
305         calculate_height(st->codec, bin->fsize);
306     avio_seek(pb, 12, SEEK_SET);
307     return 0;
308 }
309 #endif /* CONFIG_IDF_DEMUXER */
310
311 static int read_packet(AVFormatContext *s,
312                            AVPacket *pkt)
313 {
314     BinDemuxContext *bin = s->priv_data;
315
316     if (bin->fsize > 0) {
317         if (av_get_packet(s->pb, pkt, bin->fsize) < 0)
318             return AVERROR(EIO);
319         bin->fsize = -1; /* done */
320     } else if (!bin->fsize) {
321         if (url_feof(s->pb))
322             return AVERROR(EIO);
323         if (av_get_packet(s->pb, pkt, bin->chars_per_frame) < 0)
324             return AVERROR(EIO);
325     } else {
326         return AVERROR(EIO);
327     }
328
329     pkt->flags |= AV_PKT_FLAG_KEY;
330     return 0;
331 }
332
333 #if CONFIG_BINTEXT_DEMUXER
334 AVInputFormat ff_bintext_demuxer = {
335     .name           = "bin",
336     .long_name      = NULL_IF_CONFIG_SMALL("Binary text"),
337     .priv_data_size = sizeof(BinDemuxContext),
338     .read_header    = bintext_read_header,
339     .read_packet    = read_packet,
340     .extensions     = "bin",
341 };
342 #endif
343
344 #if CONFIG_XBIN_DEMUXER
345 AVInputFormat ff_xbin_demuxer = {
346     .name           = "xbin",
347     .long_name      = NULL_IF_CONFIG_SMALL("eXtended BINary text (XBIN)"),
348     .priv_data_size = sizeof(BinDemuxContext),
349     .read_probe     = xbin_probe,
350     .read_header    = xbin_read_header,
351     .read_packet    = read_packet,
352 };
353 #endif
354
355 #if CONFIG_ADF_DEMUXER
356 AVInputFormat ff_adf_demuxer = {
357     .name           = "adf",
358     .long_name      = NULL_IF_CONFIG_SMALL("Artworx Data Format"),
359     .priv_data_size = sizeof(BinDemuxContext),
360     .read_header    = adf_read_header,
361     .read_packet    = read_packet,
362     .extensions     = "adf",
363 };
364 #endif
365
366 #if CONFIG_IDF_DEMUXER
367 AVInputFormat ff_idf_demuxer = {
368     .name           = "idf",
369     .long_name      = NULL_IF_CONFIG_SMALL("iCE Draw File"),
370     .priv_data_size = sizeof(BinDemuxContext),
371     .read_probe     = idf_probe,
372     .read_header    = idf_read_header,
373     .read_packet    = read_packet,
374     .extensions     = "idf",
375 };
376 #endif