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