]> git.sesse.net Git - ffmpeg/blob - libavformat/brstm.c
Merge commit 'd8d124eebcf2ec1f6a1936b12a4f00a48f08e85c'
[ffmpeg] / libavformat / brstm.c
1 /*
2  * BRSTM demuxer
3  * Copyright (c) 2012 Paul B Mahol
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "libavutil/intreadwrite.h"
23 #include "libavcodec/bytestream.h"
24 #include "avformat.h"
25 #include "internal.h"
26
27 typedef struct BRSTMDemuxContext {
28     uint32_t    block_size;
29     uint32_t    block_count;
30     uint32_t    current_block;
31     uint32_t    samples_per_block;
32     uint32_t    last_block_used_bytes;
33     uint8_t     *table;
34     uint8_t     *adpc;
35     int         bfstm;
36 } BRSTMDemuxContext;
37
38 static int probe(AVProbeData *p)
39 {
40     if (AV_RL32(p->buf) == MKTAG('R','S','T','M') &&
41         (AV_RL16(p->buf + 4) == 0xFFFE ||
42          AV_RL16(p->buf + 4) == 0xFEFF))
43         return AVPROBE_SCORE_MAX / 3 * 2;
44     return 0;
45 }
46
47 static int probe_bfstm(AVProbeData *p)
48 {
49     if (AV_RL32(p->buf) == MKTAG('F','S','T','M') &&
50         (AV_RL16(p->buf + 4) == 0xFFFE ||
51          AV_RL16(p->buf + 4) == 0xFEFF))
52         return AVPROBE_SCORE_MAX / 3 * 2;
53     return 0;
54 }
55
56 static int read_close(AVFormatContext *s)
57 {
58     BRSTMDemuxContext *b = s->priv_data;
59
60     av_freep(&b->table);
61     av_freep(&b->adpc);
62
63     return 0;
64 }
65
66 static int read_header(AVFormatContext *s)
67 {
68     BRSTMDemuxContext *b = s->priv_data;
69     int bom, major, minor, codec, chunk;
70     int64_t h1offset, pos, toffset, data_offset = 0;
71     uint32_t size, start, asize;
72     AVStream *st;
73     int ret = AVERROR_EOF;
74
75     b->bfstm = !strcmp("bfstm", s->iformat->name);
76
77     st = avformat_new_stream(s, NULL);
78     if (!st)
79         return AVERROR(ENOMEM);
80     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
81
82     avio_skip(s->pb, 4);
83
84     bom = avio_rb16(s->pb);
85     if (bom != 0xFEFF && bom != 0xFFFE) {
86         av_log(s, AV_LOG_ERROR, "invalid byte order: %X\n", bom);
87         return AVERROR_INVALIDDATA;
88     }
89     if (bom == 0xFFFE) {
90         avpriv_request_sample(s, "little endian byte order");
91         return AVERROR_PATCHWELCOME;
92     }
93
94     if (!b->bfstm) {
95         major = avio_r8(s->pb);
96         minor = avio_r8(s->pb);
97         avio_skip(s->pb, 4); // size of file
98         size = avio_rb16(s->pb);
99         if (size < 14)
100             return AVERROR_INVALIDDATA;
101
102         avio_skip(s->pb, size - 14);
103         pos = avio_tell(s->pb);
104         if (avio_rl32(s->pb) != MKTAG('H','E','A','D'))
105             return AVERROR_INVALIDDATA;
106     } else {
107         uint32_t info_offset = 0, info_size;
108         uint16_t section_count, header_size, i;
109
110         header_size = avio_rb16(s->pb); // 6
111
112         avio_skip(s->pb, 4); // Unknown constant 0x00030000
113         avio_skip(s->pb, 4); // size of file
114         section_count = avio_rb16(s->pb);
115         avio_skip(s->pb, 2); // padding
116         for (i = 0; avio_tell(s->pb) < header_size
117                     && !(data_offset && info_offset)
118                     && i < section_count; i++) {
119             uint32_t flag = avio_rb32(s->pb);
120             switch (flag) {
121             case 0x40000000:
122                 info_offset = avio_rb32(s->pb);
123                 info_size   = avio_rb32(s->pb);
124                 break;
125             case 0x40010000:
126                 avio_skip(s->pb, 4); // seek offset
127                 avio_skip(s->pb, 4); // seek size
128                 break;
129             case 0x40020000:
130                 data_offset = avio_rb32(s->pb);
131                 avio_skip(s->pb, 4); //data_size = avio_rb32(s->pb);
132                 break;
133             case 0x40030000:
134                 avio_skip(s->pb, 4); // REGN offset
135                 avio_skip(s->pb, 4); // REGN size
136                 break;
137             }
138         }
139
140         if (!info_offset || !data_offset)
141             return AVERROR_INVALIDDATA;
142
143         start = data_offset + 8;
144
145         avio_skip(s->pb, info_offset - avio_tell(s->pb));
146         pos = avio_tell(s->pb);
147         if (avio_rl32(s->pb) != MKTAG('I','N','F','O'))
148             return AVERROR_INVALIDDATA;
149     }
150
151     size = avio_rb32(s->pb);
152     if (size < 192)
153         return AVERROR_INVALIDDATA;
154     avio_skip(s->pb, 4); // unknown
155     h1offset = avio_rb32(s->pb);
156     if (h1offset > size)
157         return AVERROR_INVALIDDATA;
158     avio_skip(s->pb, 12);
159     toffset = avio_rb32(s->pb) + 16LL;
160     if (toffset > size)
161         return AVERROR_INVALIDDATA;
162
163     avio_skip(s->pb, pos + h1offset + 8 - avio_tell(s->pb));
164     codec = avio_r8(s->pb);
165
166     switch (codec) {
167     case 0: codec = AV_CODEC_ID_PCM_S8_PLANAR;    break;
168     case 1: codec = AV_CODEC_ID_PCM_S16BE_PLANAR; break;
169     case 2: codec = AV_CODEC_ID_ADPCM_THP;        break;
170     default:
171         avpriv_request_sample(s, "codec %d", codec);
172         return AVERROR_PATCHWELCOME;
173     }
174
175     avio_skip(s->pb, 1); // loop flag
176     st->codec->codec_id = codec;
177     st->codec->channels = avio_r8(s->pb);
178     if (!st->codec->channels)
179         return AVERROR_INVALIDDATA;
180
181     avio_skip(s->pb, 1); // padding
182     if (b->bfstm)
183         avio_skip(s->pb, 2); // padding
184
185     st->codec->sample_rate = avio_rb16(s->pb);
186     if (!st->codec->sample_rate)
187         return AVERROR_INVALIDDATA;
188
189     if (!b->bfstm)
190         avio_skip(s->pb, 2); // padding
191     avio_skip(s->pb, 4); // loop start sample
192     st->start_time = 0;
193     st->duration = avio_rb32(s->pb);
194     avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
195
196     if (!b->bfstm)
197         start = avio_rb32(s->pb);
198     b->current_block = 0;
199     b->block_count = avio_rb32(s->pb);
200     if (b->block_count > UINT16_MAX) {
201         av_log(s, AV_LOG_WARNING, "too many blocks: %u\n", b->block_count);
202         return AVERROR_INVALIDDATA;
203     }
204
205     b->block_size = avio_rb32(s->pb);
206     if (b->block_size > UINT16_MAX / st->codec->channels)
207         return AVERROR_INVALIDDATA;
208     b->block_size *= st->codec->channels;
209
210     b->samples_per_block = avio_rb32(s->pb);
211     b->last_block_used_bytes = avio_rb32(s->pb);
212     if (b->last_block_used_bytes > UINT16_MAX / st->codec->channels)
213         return AVERROR_INVALIDDATA;
214     b->last_block_used_bytes *= st->codec->channels;
215
216     avio_skip(s->pb, 4); // last block samples
217     avio_skip(s->pb, 4); // last block size
218
219     if (codec == AV_CODEC_ID_ADPCM_THP) {
220         int ch;
221
222         avio_skip(s->pb, pos + toffset - avio_tell(s->pb));
223         if (!b->bfstm)
224             toffset = avio_rb32(s->pb) + 16LL;
225         else
226             toffset = toffset + avio_rb32(s->pb) + st->codec->channels * 8 - 8;
227         if (toffset > size)
228             return AVERROR_INVALIDDATA;
229
230         avio_skip(s->pb, pos + toffset - avio_tell(s->pb));
231         b->table = av_mallocz(32 * st->codec->channels);
232         if (!b->table)
233             return AVERROR(ENOMEM);
234
235         for (ch = 0; ch < st->codec->channels; ch++) {
236             if (avio_read(s->pb, b->table + ch * 32, 32) != 32) {
237                 ret = AVERROR_INVALIDDATA;
238                 goto fail;
239             }
240             avio_skip(s->pb, b->bfstm ? 14 : 24);
241         }
242
243         if (b->bfstm) {
244             st->codec->extradata_size = 32 * st->codec->channels;
245             st->codec->extradata = av_malloc(st->codec->extradata_size);
246             if (!st->codec->extradata)
247                 return AVERROR(ENOMEM);
248             memcpy(st->codec->extradata, b->table, st->codec->extradata_size);
249         }
250     }
251
252     if (size < (avio_tell(s->pb) - pos)) {
253         ret = AVERROR_INVALIDDATA;
254         goto fail;
255     }
256
257     if (!b->bfstm)
258         avio_skip(s->pb, size - (avio_tell(s->pb) - pos));
259     else
260         avio_skip(s->pb, data_offset - avio_tell(s->pb));
261
262     while (!avio_feof(s->pb)) {
263         chunk = avio_rl32(s->pb);
264         size  = avio_rb32(s->pb);
265         if (size < 8) {
266             ret = AVERROR_INVALIDDATA;
267             goto fail;
268         }
269         size -= 8;
270         switch (chunk) {
271         case MKTAG('A','D','P','C'):
272             if (codec != AV_CODEC_ID_ADPCM_THP)
273                 goto skip;
274
275             asize = b->block_count * st->codec->channels * 4;
276             if (size < asize) {
277                 ret = AVERROR_INVALIDDATA;
278                 goto fail;
279             }
280             if (b->adpc) {
281                 av_log(s, AV_LOG_WARNING, "skipping additional ADPC chunk\n");
282                 goto skip;
283             } else {
284                 b->adpc = av_mallocz(asize);
285                 if (!b->adpc) {
286                     ret = AVERROR(ENOMEM);
287                     goto fail;
288                 }
289                 avio_read(s->pb, b->adpc, asize);
290                 avio_skip(s->pb, size - asize);
291             }
292             break;
293         case MKTAG('D','A','T','A'):
294             if ((start < avio_tell(s->pb)) ||
295                 (!b->adpc && codec == AV_CODEC_ID_ADPCM_THP && !b->bfstm)) {
296                 ret = AVERROR_INVALIDDATA;
297                 goto fail;
298             }
299             avio_skip(s->pb, start - avio_tell(s->pb));
300
301             if ((major != 1 || minor) && !b->bfstm)
302                 avpriv_request_sample(s, "Version %d.%d", major, minor);
303
304             return 0;
305         default:
306             av_log(s, AV_LOG_WARNING, "skipping unknown chunk: %X\n", chunk);
307 skip:
308             avio_skip(s->pb, size);
309         }
310     }
311
312 fail:
313     read_close(s);
314
315     return ret;
316 }
317
318 static int read_packet(AVFormatContext *s, AVPacket *pkt)
319 {
320     AVCodecContext *codec = s->streams[0]->codec;
321     BRSTMDemuxContext *b = s->priv_data;
322     uint32_t samples, size;
323     int ret;
324
325     if (avio_feof(s->pb))
326         return AVERROR_EOF;
327     b->current_block++;
328     if (b->current_block == b->block_count) {
329         size    = b->last_block_used_bytes;
330         samples = size / (8 * codec->channels) * 14;
331     } else if (b->current_block < b->block_count) {
332         size    = b->block_size;
333         samples = b->samples_per_block;
334     } else {
335         return AVERROR_EOF;
336     }
337
338     if (codec->codec_id == AV_CODEC_ID_ADPCM_THP && !codec->extradata) {
339         uint8_t *dst;
340
341         if (av_new_packet(pkt, 8 + (32 + 4) * codec->channels + size) < 0)
342             return AVERROR(ENOMEM);
343         dst = pkt->data;
344         bytestream_put_be32(&dst, size);
345         bytestream_put_be32(&dst, samples);
346         bytestream_put_buffer(&dst, b->table, 32 * codec->channels);
347         bytestream_put_buffer(&dst, b->adpc + 4 * codec->channels *
348                                     (b->current_block - 1), 4 * codec->channels);
349
350         ret = avio_read(s->pb, dst, size);
351         if (ret != size)
352             av_free_packet(pkt);
353         pkt->duration = samples;
354     } else {
355         ret = av_get_packet(s->pb, pkt, size);
356     }
357
358     pkt->stream_index = 0;
359
360     if (ret != size)
361         ret = AVERROR(EIO);
362
363     return ret;
364 }
365
366 AVInputFormat ff_brstm_demuxer = {
367     .name           = "brstm",
368     .long_name      = NULL_IF_CONFIG_SMALL("BRSTM (Binary Revolution Stream)"),
369     .priv_data_size = sizeof(BRSTMDemuxContext),
370     .read_probe     = probe,
371     .read_header    = read_header,
372     .read_packet    = read_packet,
373     .read_close     = read_close,
374     .extensions     = "brstm",
375 };
376
377 AVInputFormat ff_bfstm_demuxer = {
378     .name           = "bfstm",
379     .long_name      = NULL_IF_CONFIG_SMALL("BFSTM (Binary Cafe Stream)"),
380     .priv_data_size = sizeof(BRSTMDemuxContext),
381     .read_probe     = probe_bfstm,
382     .read_header    = read_header,
383     .read_packet    = read_packet,
384     .read_close     = read_close,
385     .extensions     = "bfstm",
386 };