Browse code

BRSTM demuxer

Signed-off-by: Paul B Mahol <onemda@gmail.com>

Paul B Mahol authored on 2012/11/23 20:20:11
Showing 6 changed files
... ...
@@ -30,6 +30,7 @@ version <next>:
30 30
 - ffprobe -show_entries option
31 31
 - ffprobe -sections option
32 32
 - ADPCM IMA Dialogic decoder
33
+- BRSTM demuxer
33 34
 
34 35
 
35 36
 version 1.0:
... ...
@@ -169,6 +169,8 @@ library:
169 169
     @tab Used in Z and Z95 games.
170 170
 @item Brute Force & Ignorance   @tab   @tab X
171 171
     @tab Used in the game Flash Traffic: City of Angels.
172
+@item BRSTM                     @tab   @tab X
173
+    @tab Audio format used on the Nintendo Wii.
172 174
 @item BWF                       @tab X @tab X
173 175
 @item CRI ADX                   @tab X @tab X
174 176
     @tab Audio-only format used in console video games.
... ...
@@ -67,6 +67,7 @@ OBJS-$(CONFIG_BINTEXT_DEMUXER)           += bintext.o sauce.o
67 67
 OBJS-$(CONFIG_BIT_DEMUXER)               += bit.o
68 68
 OBJS-$(CONFIG_BIT_MUXER)                 += bit.o
69 69
 OBJS-$(CONFIG_BMV_DEMUXER)               += bmv.o
70
+OBJS-$(CONFIG_BRSTM_DEMUXER)             += brstm.o
70 71
 OBJS-$(CONFIG_C93_DEMUXER)               += c93.o vocdec.o voc.o
71 72
 OBJS-$(CONFIG_CAF_DEMUXER)               += cafdec.o caf.o mov.o mov_chan.o \
72 73
                                             isom.o
... ...
@@ -79,6 +79,7 @@ void av_register_all(void)
79 79
     REGISTER_DEMUXER  (BINK, bink);
80 80
     REGISTER_MUXDEMUX (BIT, bit);
81 81
     REGISTER_DEMUXER  (BMV, bmv);
82
+    REGISTER_DEMUXER  (BRSTM, brstm);
82 83
     REGISTER_DEMUXER  (C93, c93);
83 84
     REGISTER_MUXDEMUX (CAF, caf);
84 85
     REGISTER_MUXDEMUX (CAVSVIDEO, cavsvideo);
85 86
new file mode 100644
... ...
@@ -0,0 +1,294 @@
0
+/*
1
+ * BRSTM demuxer
2
+ * Copyright (c) 2012 Paul B Mahol
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include "libavutil/intreadwrite.h"
22
+#include "libavcodec/bytestream.h"
23
+#include "avformat.h"
24
+#include "internal.h"
25
+
26
+typedef struct BRSTMDemuxContext {
27
+    uint32_t    block_size;
28
+    uint32_t    block_count;
29
+    uint32_t    current_block;
30
+    uint32_t    samples_per_block;
31
+    uint32_t    last_block_used_bytes;
32
+    uint8_t     *table;
33
+    uint8_t     *adpc;
34
+} BRSTMDemuxContext;
35
+
36
+static int probe(AVProbeData *p)
37
+{
38
+    if (AV_RL32(p->buf) == MKTAG('R','S','T','M') &&
39
+        (AV_RL16(p->buf + 4) == 0xFFFE ||
40
+         AV_RL16(p->buf + 4) == 0xFEFF))
41
+        return AVPROBE_SCORE_MAX / 3 * 2;
42
+    return 0;
43
+}
44
+
45
+static int read_close(AVFormatContext *s)
46
+{
47
+    BRSTMDemuxContext *b = s->priv_data;
48
+
49
+    av_freep(&b->table);
50
+    av_freep(&b->adpc);
51
+
52
+    return 0;
53
+}
54
+
55
+static int read_header(AVFormatContext *s)
56
+{
57
+    BRSTMDemuxContext *b = s->priv_data;
58
+    int bom, major, minor, codec, chunk;
59
+    int64_t pos, h1offset, toffset;
60
+    uint32_t size, start, asize;
61
+    AVStream *st;
62
+    int ret = AVERROR_EOF;
63
+
64
+    st = avformat_new_stream(s, NULL);
65
+    if (!st)
66
+        return AVERROR(ENOMEM);
67
+    st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
68
+
69
+    avio_skip(s->pb, 4);
70
+
71
+    bom = avio_rb16(s->pb);
72
+    if (bom != 0xFEFF && bom != 0xFFFE) {
73
+        av_log(s, AV_LOG_ERROR, "invalid byte order: %X\n", bom);
74
+        return AVERROR_INVALIDDATA;
75
+    }
76
+    if (bom == 0xFFFE) {
77
+        av_log_ask_for_sample(s, "unsupported byte order");
78
+        return AVERROR_PATCHWELCOME;
79
+    }
80
+
81
+    major = avio_r8(s->pb);
82
+    minor = avio_r8(s->pb);
83
+    avio_skip(s->pb, 4); // size of file
84
+    size = avio_rb16(s->pb);
85
+    if (size < 14)
86
+        return AVERROR_INVALIDDATA;
87
+
88
+    avio_skip(s->pb, size - 14);
89
+    pos = avio_tell(s->pb);
90
+    if (avio_rl32(s->pb) != MKTAG('H','E','A','D'))
91
+        return AVERROR_INVALIDDATA;
92
+    size = avio_rb32(s->pb);
93
+    if (size < 256)
94
+        return AVERROR_INVALIDDATA;
95
+    avio_skip(s->pb, 4); // unknown
96
+    h1offset = avio_rb32(s->pb);
97
+    if (h1offset > size)
98
+        return AVERROR_INVALIDDATA;
99
+    avio_skip(s->pb, 12);
100
+    toffset = avio_rb32(s->pb) + 16LL;
101
+    if (toffset > size)
102
+        return AVERROR_INVALIDDATA;
103
+
104
+    avio_skip(s->pb, pos + h1offset + 8 - avio_tell(s->pb));
105
+    codec = avio_r8(s->pb);
106
+
107
+    switch (codec) {
108
+    case 0: codec = AV_CODEC_ID_PCM_S8_PLANAR;    break;
109
+    case 1: codec = AV_CODEC_ID_PCM_S16BE_PLANAR; break;
110
+    case 2: codec = AV_CODEC_ID_ADPCM_THP;        break;
111
+    default:
112
+        av_log_ask_for_sample(s, "unsupported codec: %d", codec);
113
+        return AVERROR_PATCHWELCOME;
114
+    }
115
+
116
+    avio_skip(s->pb, 1); // loop flag
117
+    st->codec->codec_id = codec;
118
+    st->codec->channels = avio_r8(s->pb);
119
+    if (!st->codec->channels)
120
+        return AVERROR_INVALIDDATA;
121
+
122
+    avio_skip(s->pb, 1); // padding
123
+    st->codec->sample_rate = avio_rb16(s->pb);
124
+    if (!st->codec->sample_rate)
125
+        return AVERROR_INVALIDDATA;
126
+
127
+    avio_skip(s->pb, 2); // padding
128
+    avio_skip(s->pb, 4); // loop start sample
129
+    st->start_time = 0;
130
+    st->duration = avio_rb32(s->pb);
131
+    avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
132
+
133
+    start = avio_rb32(s->pb);
134
+    b->current_block = 0;
135
+    b->block_count = avio_rb32(s->pb);
136
+    if (b->block_count > UINT16_MAX) {
137
+        av_log(s, AV_LOG_WARNING, "too many blocks: %u\n", b->block_count);
138
+        return AVERROR_INVALIDDATA;
139
+    }
140
+
141
+    b->block_size = avio_rb32(s->pb);
142
+    if (b->block_size > UINT16_MAX / st->codec->channels)
143
+        return AVERROR_INVALIDDATA;
144
+    b->block_size *= st->codec->channels;
145
+
146
+    b->samples_per_block = avio_rb32(s->pb);
147
+    b->last_block_used_bytes = avio_rb32(s->pb);
148
+    if (b->last_block_used_bytes > UINT16_MAX / st->codec->channels)
149
+        return AVERROR_INVALIDDATA;
150
+    b->last_block_used_bytes *= st->codec->channels;
151
+
152
+    avio_skip(s->pb, 4); // last block samples
153
+    avio_skip(s->pb, 4); // last block size
154
+
155
+    if (codec == AV_CODEC_ID_ADPCM_THP) {
156
+        int ch;
157
+
158
+        avio_skip(s->pb, pos + toffset - avio_tell(s->pb));
159
+        toffset = avio_rb32(s->pb) + 16LL;
160
+        if (toffset > size)
161
+            return AVERROR_INVALIDDATA;
162
+
163
+        avio_skip(s->pb, pos + toffset - avio_tell(s->pb));
164
+        b->table = av_mallocz(32 * st->codec->channels);
165
+        if (!b->table)
166
+            return AVERROR(ENOMEM);
167
+
168
+        for (ch = 0; ch < st->codec->channels; ch++) {
169
+            if (avio_read(s->pb, b->table + ch * 32, 32) != 32) {
170
+                ret = AVERROR_INVALIDDATA;
171
+                goto fail;
172
+            }
173
+            avio_skip(s->pb, 24);
174
+        }
175
+    }
176
+
177
+    if (size < (avio_tell(s->pb) - pos)) {
178
+        ret = AVERROR_INVALIDDATA;
179
+        goto fail;
180
+    }
181
+    avio_skip(s->pb, size - (avio_tell(s->pb) - pos));
182
+
183
+    while (!url_feof(s->pb)) {
184
+        chunk = avio_rl32(s->pb);
185
+        size  = avio_rb32(s->pb);
186
+        if (size < 8) {
187
+            ret = AVERROR_INVALIDDATA;
188
+            goto fail;
189
+        }
190
+        size -= 8;
191
+        switch (chunk) {
192
+        case MKTAG('A','D','P','C'):
193
+            if (codec != AV_CODEC_ID_ADPCM_THP)
194
+                goto skip;
195
+
196
+            asize = b->block_count * st->codec->channels * 4;
197
+            if (size < asize) {
198
+                ret = AVERROR_INVALIDDATA;
199
+                goto fail;
200
+            }
201
+            if (b->adpc) {
202
+                av_log(s, AV_LOG_WARNING, "skipping additonal ADPC chunk\n");
203
+                goto skip;
204
+            } else {
205
+                b->adpc = av_mallocz(asize);
206
+                if (!b->adpc) {
207
+                    ret = AVERROR(ENOMEM);
208
+                    goto fail;
209
+                }
210
+                avio_read(s->pb, b->adpc, asize);
211
+                avio_skip(s->pb, size - asize);
212
+            }
213
+            break;
214
+        case MKTAG('D','A','T','A'):
215
+            if ((start < avio_tell(s->pb)) ||
216
+                (!b->adpc && codec == AV_CODEC_ID_ADPCM_THP)) {
217
+                ret = AVERROR_INVALIDDATA;
218
+                goto fail;
219
+            }
220
+            avio_skip(s->pb, start - avio_tell(s->pb));
221
+            return 0;
222
+        default:
223
+            av_log(s, AV_LOG_WARNING, "skipping unknown chunk: %X\n", chunk);
224
+skip:
225
+            avio_skip(s->pb, size);
226
+        }
227
+    }
228
+
229
+fail:
230
+    read_close(s);
231
+
232
+    return ret;
233
+}
234
+
235
+static int read_packet(AVFormatContext *s, AVPacket *pkt)
236
+{
237
+    AVCodecContext *codec = s->streams[0]->codec;
238
+    BRSTMDemuxContext *b = s->priv_data;
239
+    uint32_t samples, size;
240
+    int ret;
241
+
242
+    if (url_feof(s->pb))
243
+        return AVERROR_EOF;
244
+    b->current_block++;
245
+    if (b->current_block == b->block_count) {
246
+        size    = b->last_block_used_bytes;
247
+        samples = size / 16 * 14;
248
+    } else if (b->current_block < b->block_count) {
249
+        size    = b->block_size;
250
+        samples = b->samples_per_block;
251
+    } else {
252
+        return AVERROR_EOF;
253
+    }
254
+
255
+    if (codec->codec_id == AV_CODEC_ID_ADPCM_THP) {
256
+        uint8_t *dst;
257
+
258
+        if (av_new_packet(pkt, 8 + (32 + 4) * codec->channels + size) < 0)
259
+            return AVERROR(ENOMEM);
260
+        dst = pkt->data;
261
+        bytestream_put_be32(&dst, size);
262
+        bytestream_put_be32(&dst, samples);
263
+        bytestream_put_buffer(&dst, b->table, 32 * codec->channels);
264
+        bytestream_put_buffer(&dst, b->adpc + 4 * codec->channels *
265
+                                    (b->current_block - 1), 4 * codec->channels);
266
+
267
+        ret = avio_read(s->pb, dst, size);
268
+        if (ret < 0) {
269
+            av_free_packet(pkt);
270
+            return ret;
271
+        }
272
+        pkt->duration = samples;
273
+    } else {
274
+        ret = av_get_packet(s->pb, pkt, size);
275
+        if (ret < 0)
276
+            return ret;
277
+    }
278
+
279
+    pkt->stream_index = 0;
280
+
281
+    return ret;
282
+}
283
+
284
+AVInputFormat ff_brstm_demuxer = {
285
+    .name           = "brstm",
286
+    .long_name      = NULL_IF_CONFIG_SMALL("BRSTM (Binary Revolution Stream)"),
287
+    .priv_data_size = sizeof(BRSTMDemuxContext),
288
+    .read_probe     = probe,
289
+    .read_header    = read_header,
290
+    .read_packet    = read_packet,
291
+    .read_close     = read_close,
292
+    .extensions     = "brstm",
293
+};
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/avutil.h"
31 31
 
32 32
 #define LIBAVFORMAT_VERSION_MAJOR 54
33
-#define LIBAVFORMAT_VERSION_MINOR 39
33
+#define LIBAVFORMAT_VERSION_MINOR 40
34 34
 #define LIBAVFORMAT_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \