Browse code

add xWMA demuxer

Signed-off-by: Luca Barbato <lu_zero@gentoo.org>

Max Horn authored on 2011/04/12 20:59:39
Showing 6 changed files
... ...
@@ -85,6 +85,7 @@ version <next>:
85 85
 - Mobotix MxPEG decoder
86 86
 - AAC encoding via libvo-aacenc
87 87
 - AMR-WB encoding via libvo-amrwbenc
88
+- xWMA demuxer
88 89
 
89 90
 
90 91
 version 0.6:
... ...
@@ -258,6 +258,8 @@ library:
258 258
     @tab Multimedia format used in Westwood Studios games.
259 259
 @item Westwood Studios VQA      @tab   @tab X
260 260
     @tab Multimedia format used in Westwood Studios games.
261
+@item xWMA                      @tab   @tab X
262
+    @tab Microsoft audio container used by XAudio 2.
261 263
 @item YUV4MPEG pipe             @tab X @tab X
262 264
 @item Psygnosis YOP             @tab   @tab X
263 265
 @end multitable
... ...
@@ -299,6 +299,7 @@ OBJS-$(CONFIG_WSVQA_DEMUXER)             += westwood.o
299 299
 OBJS-$(CONFIG_WTV_DEMUXER)               += wtv.o asf.o asfdec.o mpegts.o riff.o
300 300
 OBJS-$(CONFIG_WV_DEMUXER)                += wv.o apetag.o
301 301
 OBJS-$(CONFIG_XA_DEMUXER)                += xa.o
302
+OBJS-$(CONFIG_XWMA_DEMUXER)              += xwma.o riff.o
302 303
 OBJS-$(CONFIG_YOP_DEMUXER)               += yop.o
303 304
 OBJS-$(CONFIG_YUV4MPEGPIPE_MUXER)        += yuv4mpeg.o
304 305
 OBJS-$(CONFIG_YUV4MPEGPIPE_DEMUXER)      += yuv4mpeg.o
... ...
@@ -225,6 +225,7 @@ void av_register_all(void)
225 225
     REGISTER_DEMUXER  (WTV, wtv);
226 226
     REGISTER_DEMUXER  (WV, wv);
227 227
     REGISTER_DEMUXER  (XA, xa);
228
+    REGISTER_DEMUXER  (XWMA, xwma);
228 229
     REGISTER_DEMUXER  (YOP, yop);
229 230
     REGISTER_MUXDEMUX (YUV4MPEGPIPE, yuv4mpegpipe);
230 231
 
... ...
@@ -24,7 +24,7 @@
24 24
 #include "libavutil/avutil.h"
25 25
 
26 26
 #define LIBAVFORMAT_VERSION_MAJOR 52
27
-#define LIBAVFORMAT_VERSION_MINOR 107
27
+#define LIBAVFORMAT_VERSION_MINOR 108
28 28
 #define LIBAVFORMAT_VERSION_MICRO  0
29 29
 
30 30
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
31 31
new file mode 100644
... ...
@@ -0,0 +1,254 @@
0
+/*
1
+ * xWMA demuxer
2
+ * Copyright (c) 2011 Max Horn
3
+ *
4
+ * This file is part of Libav.
5
+ *
6
+ * Libav 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
+ * Libav 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 Libav; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include "avformat.h"
22
+#include "riff.h"
23
+
24
+/*
25
+ * Demuxer for xWMA, a Microsoft audio container used by XAudio 2.
26
+ */
27
+
28
+typedef struct {
29
+    int64_t data_end;
30
+} XWMAContext;
31
+
32
+static int xwma_probe(AVProbeData *p)
33
+{
34
+    if (!memcmp(p->buf, "RIFF", 4) && !memcmp(p->buf + 8, "XWMA", 4))
35
+        return AVPROBE_SCORE_MAX;
36
+    return 0;
37
+}
38
+
39
+static int xwma_read_header(AVFormatContext *s, AVFormatParameters *ap)
40
+{
41
+    int64_t size, av_uninit(data_size);
42
+    uint32_t dpds_table_size = 0;
43
+    uint32_t *dpds_table = 0;
44
+    unsigned int tag;
45
+    AVIOContext *pb = s->pb;
46
+    AVStream *st;
47
+    XWMAContext *xwma = s->priv_data;
48
+    int i;
49
+
50
+    /* The following code is mostly copied from wav.c, with some
51
+     * minor alterations.
52
+     */
53
+
54
+    /* check RIFF header */
55
+    tag = avio_rl32(pb);
56
+    if (tag != MKTAG('R', 'I', 'F', 'F'))
57
+        return -1;
58
+    avio_rl32(pb); /* file size */
59
+    tag = avio_rl32(pb);
60
+    if (tag != MKTAG('X', 'W', 'M', 'A'))
61
+        return -1;
62
+
63
+    /* parse fmt header */
64
+    tag = avio_rl32(pb);
65
+    if (tag != MKTAG('f', 'm', 't', ' '))
66
+        return -1;
67
+    size = avio_rl32(pb);
68
+    st = av_new_stream(s, 0);
69
+    if (!st)
70
+        return AVERROR(ENOMEM);
71
+
72
+    ff_get_wav_header(pb, st->codec, size);
73
+    st->need_parsing = AVSTREAM_PARSE_NONE;
74
+
75
+    /* All xWMA files I have seen contained WMAv2 data. If there are files
76
+     * using WMA Pro or some other codec, then we need to figure out the right
77
+     * extradata for that. Thus, ask the user for feedback, but try to go on
78
+     * anyway.
79
+     */
80
+    if (st->codec->codec_id != CODEC_ID_WMAV2) {
81
+        av_log(s, AV_LOG_WARNING, "unexpected codec (tag 0x04%x; id %d)\n",
82
+                              st->codec->codec_tag, st->codec->codec_id);
83
+        av_log_ask_for_sample(s, NULL);
84
+    } else {
85
+        /* In all xWMA files I have seen, there is no extradata. But the WMA
86
+         * codecs require extradata, so we provide our own fake extradata.
87
+         *
88
+         * First, check that there really was no extradata in the header. If
89
+         * there was, then try to use, after asking the the user to provide a
90
+         * sample of this unusual file.
91
+         */
92
+        if (st->codec->extradata_size != 0) {
93
+            /* Surprise, surprise: We *did* get some extradata. No idea
94
+             * if it will work, but just go on and try it, after asking
95
+             * the user for a sample.
96
+             */
97
+            av_log(s, AV_LOG_WARNING, "unexpected extradata (%d bytes)\n",
98
+                                  st->codec->extradata_size);
99
+            av_log_ask_for_sample(s, NULL);
100
+        } else {
101
+            st->codec->extradata_size = 6;
102
+            st->codec->extradata      = av_mallocz(6 + FF_INPUT_BUFFER_PADDING_SIZE);
103
+            if (!st->codec->extradata)
104
+                return AVERROR(ENOMEM);
105
+
106
+            /* setup extradata with our experimentally obtained value */
107
+            st->codec->extradata[4] = 31;
108
+        }
109
+    }
110
+
111
+    /* set the sample rate */
112
+    av_set_pts_info(st, 64, 1, st->codec->sample_rate);
113
+
114
+    /* parse the remaining RIFF chunks */
115
+    for (;;) {
116
+        if (pb->eof_reached)
117
+            return -1;
118
+        /* read next chunk tag */
119
+        tag = avio_rl32(pb);
120
+        size = avio_rl32(pb);
121
+        if (tag == MKTAG('d', 'a', 't', 'a')) {
122
+            /* We assume that the data chunk comes last. */
123
+            break;
124
+        } else if (tag == MKTAG('d','p','d','s')) {
125
+            /* Quoting the MSDN xWMA docs on the dpds chunk: "Contains the
126
+             * decoded packet cumulative data size array, each element is the
127
+             * number of bytes accumulated after the corresponding xWMA packet
128
+             * is decoded in order"
129
+             *
130
+             * Each packet has size equal to st->codec->block_align, which in
131
+             * all cases I saw so far was always 2230. Thus, we can use the
132
+             * dpds data to compute a seeking index.
133
+             */
134
+
135
+            /* Error out if there is more than one dpds chunk. */
136
+            if (dpds_table) {
137
+                av_log(s, AV_LOG_ERROR, "two dpds chunks present\n");
138
+                return -1;
139
+            }
140
+
141
+            /* Compute the number of entries in the dpds chunk. */
142
+            if (size & 3) {  /* Size should be divisible by four */
143
+                av_log(s, AV_LOG_WARNING, "dpds chunk size "PRId64" not divisible by 4\n", size);
144
+            }
145
+            dpds_table_size = size / 4;
146
+            if (dpds_table_size == 0 || dpds_table_size >= INT_MAX / 4) {
147
+                av_log(s, AV_LOG_ERROR, "dpds chunk size "PRId64" invalid\n", size);
148
+                return -1;
149
+            }
150
+
151
+            /* Allocate some temporary storage to keep the dpds data around.
152
+             * for processing later on.
153
+             */
154
+            dpds_table = av_malloc(dpds_table_size * sizeof(uint32_t));
155
+            if (!dpds_table) {
156
+                return AVERROR(ENOMEM);
157
+            }
158
+
159
+            for (i = 0; i < dpds_table_size; ++i) {
160
+                dpds_table[i] = avio_rl32(pb);
161
+                size -= 4;
162
+            }
163
+        }
164
+        avio_skip(pb, size);
165
+    }
166
+
167
+    /* Determine overall data length */
168
+    if (size < 0)
169
+        return -1;
170
+    if (!size) {
171
+        xwma->data_end = INT64_MAX;
172
+    } else
173
+        xwma->data_end = avio_tell(pb) + size;
174
+
175
+
176
+    if (dpds_table && dpds_table_size) {
177
+        int64_t cur_pos;
178
+        const uint32_t bytes_per_sample
179
+                = (st->codec->channels * st->codec->bits_per_coded_sample) >> 3;
180
+
181
+        /* Estimate the duration from the total number of output bytes. */
182
+        const uint64_t total_decoded_bytes = dpds_table[dpds_table_size - 1];
183
+        st->duration = total_decoded_bytes / bytes_per_sample;
184
+
185
+        /* Use the dpds data to build a seek table.  We can only do this after
186
+         * we know the offset to the data chunk, as we need that to determine
187
+         * the actual offset to each input block.
188
+         * Note: If we allowed ourselves to assume that the data chunk always
189
+         * follows immediately after the dpds block, we could of course guess
190
+         * the data block's start offset already while reading the dpds chunk.
191
+         * I decided against that, just in case other chunks ever are
192
+         * discovered.
193
+         */
194
+        cur_pos = avio_tell(pb);
195
+        for (i = 0; i < dpds_table_size; ++i) {
196
+            /* From the number of output bytes that would accumulate in the
197
+             * output buffer after decoding the first (i+1) packets, we compute
198
+             * an offset / timestamp pair.
199
+             */
200
+            av_add_index_entry(st,
201
+                               cur_pos + (i+1) * st->codec->block_align, /* pos */
202
+                               dpds_table[i] / bytes_per_sample,         /* timestamp */
203
+                               st->codec->block_align,                   /* size */
204
+                               0,                                        /* duration */
205
+                               AVINDEX_KEYFRAME);
206
+        }
207
+    } else if (st->codec->bit_rate) {
208
+        /* No dpds chunk was present (or only an empty one), so estimate
209
+         * the total duration using the average bits per sample and the
210
+         * total data length.
211
+         */
212
+        st->duration = (size<<3) * st->codec->sample_rate / st->codec->bit_rate;
213
+    }
214
+
215
+    av_free(dpds_table);
216
+
217
+    return 0;
218
+}
219
+
220
+static int xwma_read_packet(AVFormatContext *s, AVPacket *pkt)
221
+{
222
+    int ret, size;
223
+    int64_t left;
224
+    AVStream *st;
225
+    XWMAContext *xwma = s->priv_data;
226
+
227
+    st = s->streams[0];
228
+
229
+    left = xwma->data_end - avio_tell(s->pb);
230
+    if (left <= 0) {
231
+        return AVERROR_EOF;
232
+    }
233
+
234
+    /* read a single block; the default block size is 2230. */
235
+    size = (st->codec->block_align > 1) ? st->codec->block_align : 2230;
236
+    size = FFMIN(size, left);
237
+
238
+    ret  = av_get_packet(s->pb, pkt, size);
239
+    if (ret < 0)
240
+        return ret;
241
+
242
+    pkt->stream_index = 0;
243
+    return ret;
244
+}
245
+
246
+AVInputFormat ff_xwma_demuxer = {
247
+    "xwma",
248
+    NULL_IF_CONFIG_SMALL("Microsoft xWMA"),
249
+    sizeof(XWMAContext),
250
+    xwma_probe,
251
+    xwma_read_header,
252
+    xwma_read_packet,
253
+};