]> git.sesse.net Git - ffmpeg/blob - libavformat/brstm.c
lavf/brstm: handle a BFSTM endianness oddity
[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     uint32_t    last_block_size;
34     uint32_t    last_block_samples;
35     uint32_t    data_start;
36     uint8_t     *table;
37     uint8_t     *adpc;
38     int         little_endian;
39 } BRSTMDemuxContext;
40
41 static int probe(AVProbeData *p)
42 {
43     if (AV_RL32(p->buf) == MKTAG('R','S','T','M') &&
44         (AV_RL16(p->buf + 4) == 0xFFFE ||
45          AV_RL16(p->buf + 4) == 0xFEFF))
46         return AVPROBE_SCORE_MAX / 3 * 2;
47     return 0;
48 }
49
50 static int probe_bfstm(AVProbeData *p)
51 {
52     if ((AV_RL32(p->buf) == MKTAG('F','S','T','M') ||
53          AV_RL32(p->buf) == MKTAG('C','S','T','M')) &&
54         (AV_RL16(p->buf + 4) == 0xFFFE ||
55          AV_RL16(p->buf + 4) == 0xFEFF))
56         return AVPROBE_SCORE_MAX / 3 * 2;
57     return 0;
58 }
59
60 static int read_close(AVFormatContext *s)
61 {
62     BRSTMDemuxContext *b = s->priv_data;
63
64     av_freep(&b->table);
65     av_freep(&b->adpc);
66
67     return 0;
68 }
69
70 static av_always_inline unsigned int read16(AVFormatContext *s)
71 {
72     BRSTMDemuxContext *b = s->priv_data;
73     if (b->little_endian)
74         return avio_rl16(s->pb);
75     else
76         return avio_rb16(s->pb);
77 }
78
79 static av_always_inline unsigned int read32(AVFormatContext *s)
80 {
81     BRSTMDemuxContext *b = s->priv_data;
82     if (b->little_endian)
83         return avio_rl32(s->pb);
84     else
85         return avio_rb32(s->pb);
86 }
87
88 static int read_header(AVFormatContext *s)
89 {
90     BRSTMDemuxContext *b = s->priv_data;
91     int bom, major, minor, codec, chunk;
92     int64_t h1offset, pos, toffset;
93     uint32_t size, asize, start = 0;
94     AVStream *st;
95     int ret = AVERROR_EOF;
96     int bfstm = !strcmp("bfstm", s->iformat->name);
97
98     st = avformat_new_stream(s, NULL);
99     if (!st)
100         return AVERROR(ENOMEM);
101     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
102
103     avio_skip(s->pb, 4);
104
105     bom = avio_rb16(s->pb);
106     if (bom != 0xFEFF && bom != 0xFFFE) {
107         av_log(s, AV_LOG_ERROR, "invalid byte order: %X\n", bom);
108         return AVERROR_INVALIDDATA;
109     }
110
111     if (bom == 0xFFFE)
112         b->little_endian = 1;
113
114     if (!bfstm) {
115         major = avio_r8(s->pb);
116         minor = avio_r8(s->pb);
117         avio_skip(s->pb, 4); // size of file
118         size = read16(s);
119         if (size < 14)
120             return AVERROR_INVALIDDATA;
121
122         avio_skip(s->pb, size - 14);
123         pos = avio_tell(s->pb);
124         if (avio_rl32(s->pb) != MKTAG('H','E','A','D'))
125             return AVERROR_INVALIDDATA;
126     } else {
127         uint32_t info_offset = 0, info_size;
128         uint16_t section_count, header_size, i;
129
130         header_size = read16(s); // 6
131
132         avio_skip(s->pb, 4); // Unknown constant 0x00030000
133         avio_skip(s->pb, 4); // size of file
134         section_count = read16(s);
135         avio_skip(s->pb, 2); // padding
136         for (i = 0; avio_tell(s->pb) < header_size
137                     && !(start && info_offset)
138                     && i < section_count; i++) {
139             uint16_t flag = read16(s);
140             avio_skip(s->pb, 2);
141             switch (flag) {
142             case 0x4000:
143                 info_offset = read32(s);
144                 info_size   = read32(s);
145                 break;
146             case 0x4001:
147                 avio_skip(s->pb, 4); // seek offset
148                 avio_skip(s->pb, 4); // seek size
149                 break;
150             case 0x4002:
151                 start = read32(s) + 8;
152                 avio_skip(s->pb, 4); //data_size = read32(s);
153                 break;
154             case 0x4003:
155                 avio_skip(s->pb, 4); // REGN offset
156                 avio_skip(s->pb, 4); // REGN size
157                 break;
158             }
159         }
160
161         if (!info_offset || !start)
162             return AVERROR_INVALIDDATA;
163
164         avio_skip(s->pb, info_offset - avio_tell(s->pb));
165         pos = avio_tell(s->pb);
166         if (avio_rl32(s->pb) != MKTAG('I','N','F','O'))
167             return AVERROR_INVALIDDATA;
168     }
169
170     size = read32(s);
171     if (size < 192)
172         return AVERROR_INVALIDDATA;
173     avio_skip(s->pb, 4); // unknown
174     h1offset = read32(s);
175     if (h1offset > size)
176         return AVERROR_INVALIDDATA;
177     avio_skip(s->pb, 12);
178     toffset = read32(s) + 16LL;
179     if (toffset > size)
180         return AVERROR_INVALIDDATA;
181
182     avio_skip(s->pb, pos + h1offset + 8 - avio_tell(s->pb));
183     codec = avio_r8(s->pb);
184
185     switch (codec) {
186     case 0: codec = AV_CODEC_ID_PCM_S8_PLANAR;    break;
187     case 1: codec = AV_CODEC_ID_PCM_S16BE_PLANAR; break;
188     case 2: codec = b->little_endian ?
189                     AV_CODEC_ID_ADPCM_THP_LE :
190                     AV_CODEC_ID_ADPCM_THP;        break;
191     default:
192         avpriv_request_sample(s, "codec %d", codec);
193         return AVERROR_PATCHWELCOME;
194     }
195
196     avio_skip(s->pb, 1); // loop flag
197     st->codec->codec_id = codec;
198     st->codec->channels = avio_r8(s->pb);
199     if (!st->codec->channels)
200         return AVERROR_INVALIDDATA;
201
202     avio_skip(s->pb, 1); // padding
203
204     st->codec->sample_rate = bfstm ? read32(s) : read16(s);
205     if (!st->codec->sample_rate)
206         return AVERROR_INVALIDDATA;
207
208     if (!bfstm)
209         avio_skip(s->pb, 2); // padding
210     avio_skip(s->pb, 4); // loop start sample
211     st->start_time = 0;
212     st->duration = read32(s);
213     avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
214
215     if (!bfstm)
216         start = read32(s);
217     b->current_block = 0;
218     b->block_count = read32(s);
219     if (b->block_count > UINT16_MAX) {
220         av_log(s, AV_LOG_WARNING, "too many blocks: %u\n", b->block_count);
221         return AVERROR_INVALIDDATA;
222     }
223
224     b->block_size = read32(s);
225     if (b->block_size > UINT32_MAX / st->codec->channels)
226         return AVERROR_INVALIDDATA;
227
228     b->samples_per_block = read32(s);
229     b->last_block_used_bytes = read32(s);
230     b->last_block_samples = read32(s);
231     b->last_block_size = read32(s);
232     if (b->last_block_size > UINT32_MAX / st->codec->channels)
233         return AVERROR_INVALIDDATA;
234     if (b->last_block_used_bytes > b->last_block_size)
235         return AVERROR_INVALIDDATA;
236
237
238     if (codec == AV_CODEC_ID_ADPCM_THP || codec == AV_CODEC_ID_ADPCM_THP_LE) {
239         int ch;
240
241         avio_skip(s->pb, pos + toffset - avio_tell(s->pb));
242         if (!bfstm)
243             toffset = read32(s) + 16LL;
244         else
245             toffset = toffset + read32(s) + st->codec->channels * 8 - 8;
246         if (toffset > size)
247             return AVERROR_INVALIDDATA;
248
249         avio_skip(s->pb, pos + toffset - avio_tell(s->pb));
250         b->table = av_mallocz(32 * st->codec->channels);
251         if (!b->table)
252             return AVERROR(ENOMEM);
253
254         for (ch = 0; ch < st->codec->channels; ch++) {
255             if (avio_read(s->pb, b->table + ch * 32, 32) != 32) {
256                 ret = AVERROR_INVALIDDATA;
257                 goto fail;
258             }
259             avio_skip(s->pb, bfstm ? 14 : 24);
260         }
261     }
262
263     if (size < (avio_tell(s->pb) - pos)) {
264         ret = AVERROR_INVALIDDATA;
265         goto fail;
266     }
267
268     avio_skip(s->pb, size - (avio_tell(s->pb) - pos));
269
270     while (!avio_feof(s->pb)) {
271         chunk = avio_rl32(s->pb);
272         size  = read32(s);
273         if (size < 8) {
274             ret = AVERROR_INVALIDDATA;
275             goto fail;
276         }
277         size -= 8;
278         switch (chunk) {
279         case MKTAG('S','E','E','K'):
280         case MKTAG('A','D','P','C'):
281             if (codec != AV_CODEC_ID_ADPCM_THP &&
282                 codec != AV_CODEC_ID_ADPCM_THP_LE)
283                 goto skip;
284
285             asize = b->block_count * st->codec->channels * 4;
286             if (size < asize) {
287                 ret = AVERROR_INVALIDDATA;
288                 goto fail;
289             }
290             if (b->adpc) {
291                 av_log(s, AV_LOG_WARNING, "skipping additional ADPC chunk\n");
292                 goto skip;
293             } else {
294                 b->adpc = av_mallocz(asize);
295                 if (!b->adpc) {
296                     ret = AVERROR(ENOMEM);
297                     goto fail;
298                 }
299                 if (bfstm && codec != AV_CODEC_ID_ADPCM_THP_LE) {
300                     // Big-endian BFSTMs have little-endian SEEK tables
301                     // for some strange reason.
302                     int i;
303                     for (i = 0; i < asize; i += 2) {
304                         b->adpc[i+1] = avio_r8(s->pb);
305                         b->adpc[i]   = avio_r8(s->pb);
306                     }
307                 } else {
308                     avio_read(s->pb, b->adpc, asize);
309                 }
310                 avio_skip(s->pb, size - asize);
311             }
312             break;
313         case MKTAG('D','A','T','A'):
314             if ((start < avio_tell(s->pb)) ||
315                 (!b->adpc && (codec == AV_CODEC_ID_ADPCM_THP ||
316                               codec == AV_CODEC_ID_ADPCM_THP_LE))) {
317                 ret = AVERROR_INVALIDDATA;
318                 goto fail;
319             }
320             avio_skip(s->pb, start - avio_tell(s->pb));
321
322             if (bfstm && (codec == AV_CODEC_ID_ADPCM_THP ||
323                           codec == AV_CODEC_ID_ADPCM_THP_LE))
324                 avio_skip(s->pb, 24);
325
326             b->data_start = avio_tell(s->pb);
327
328             if ((major != 1 || minor) && !bfstm)
329                 avpriv_request_sample(s, "Version %d.%d", major, minor);
330
331             return 0;
332         default:
333             av_log(s, AV_LOG_WARNING, "skipping unknown chunk: %X\n", chunk);
334 skip:
335             avio_skip(s->pb, size);
336         }
337     }
338
339 fail:
340     read_close(s);
341
342     return ret;
343 }
344
345 static int read_packet(AVFormatContext *s, AVPacket *pkt)
346 {
347     AVCodecContext *codec = s->streams[0]->codec;
348     BRSTMDemuxContext *b = s->priv_data;
349     uint32_t samples, size, skip = 0;
350     int ret, i;
351
352     if (avio_feof(s->pb))
353         return AVERROR_EOF;
354     b->current_block++;
355     if (b->current_block == b->block_count) {
356         size    = b->last_block_used_bytes;
357         samples = b->last_block_samples;
358         skip    = b->last_block_size - b->last_block_used_bytes;
359     } else if (b->current_block < b->block_count) {
360         size    = b->block_size;
361         samples = b->samples_per_block;
362     } else {
363         return AVERROR_EOF;
364     }
365
366     if (codec->codec_id == AV_CODEC_ID_ADPCM_THP ||
367         codec->codec_id == AV_CODEC_ID_ADPCM_THP_LE) {
368         uint8_t *dst;
369
370         if (av_new_packet(pkt, 8 + (32 + 4 + size) * codec->channels) < 0)
371             return AVERROR(ENOMEM);
372         dst = pkt->data;
373         if (codec->codec_id == AV_CODEC_ID_ADPCM_THP_LE) {
374             bytestream_put_le32(&dst, size * codec->channels);
375             bytestream_put_le32(&dst, samples);
376         } else {
377             bytestream_put_be32(&dst, size * codec->channels);
378             bytestream_put_be32(&dst, samples);
379         }
380         bytestream_put_buffer(&dst, b->table, 32 * codec->channels);
381         bytestream_put_buffer(&dst, b->adpc + 4 * codec->channels *
382                                     (b->current_block - 1), 4 * codec->channels);
383
384         for (i = 0; i < codec->channels; i++) {
385             ret = avio_read(s->pb, dst, size);
386             dst += size;
387             avio_skip(s->pb, skip);
388             if (ret != size) {
389                 av_free_packet(pkt);
390                 break;
391             }
392         }
393         pkt->duration = samples;
394     } else {
395         size *= codec->channels;
396         ret = av_get_packet(s->pb, pkt, size);
397     }
398
399     pkt->stream_index = 0;
400
401     if (ret != size)
402         ret = AVERROR(EIO);
403
404     return ret;
405 }
406
407 static int read_seek(AVFormatContext *s, int stream_index,
408                      int64_t timestamp, int flags)
409 {
410     AVStream *st = s->streams[stream_index];
411     BRSTMDemuxContext *b = s->priv_data;
412     int64_t ret = 0;
413
414     timestamp /= b->samples_per_block;
415     ret = avio_seek(s->pb, b->data_start + timestamp * b->block_size *
416                            st->codec->channels, SEEK_SET);
417     if (ret < 0)
418         return ret;
419
420     b->current_block = timestamp;
421     ff_update_cur_dts(s, st, timestamp * b->samples_per_block);
422     return 0;
423 }
424
425 AVInputFormat ff_brstm_demuxer = {
426     .name           = "brstm",
427     .long_name      = NULL_IF_CONFIG_SMALL("BRSTM (Binary Revolution Stream)"),
428     .priv_data_size = sizeof(BRSTMDemuxContext),
429     .read_probe     = probe,
430     .read_header    = read_header,
431     .read_packet    = read_packet,
432     .read_close     = read_close,
433     .read_seek      = read_seek,
434     .extensions     = "brstm",
435 };
436
437 AVInputFormat ff_bfstm_demuxer = {
438     .name           = "bfstm",
439     .long_name      = NULL_IF_CONFIG_SMALL("BFSTM (Binary Cafe Stream)"),
440     .priv_data_size = sizeof(BRSTMDemuxContext),
441     .read_probe     = probe_bfstm,
442     .read_header    = read_header,
443     .read_packet    = read_packet,
444     .read_close     = read_close,
445     .read_seek      = read_seek,
446     .extensions     = "bfstm,bcstm",
447 };