Statistics
| Branch: | Revision:

ffmpeg / libavformat / xwma.c @ ef8b54fc

History | View | Annotate | Download (8.72 KB)

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
    int ret;
44
    uint32_t dpds_table_size = 0;
45
    uint32_t *dpds_table = 0;
46
    unsigned int tag;
47
    AVIOContext *pb = s->pb;
48
    AVStream *st;
49
    XWMAContext *xwma = s->priv_data;
50
    int i;
51

    
52
    /* The following code is mostly copied from wav.c, with some
53
     * minor alterations.
54
     */
55

    
56
    /* check RIFF header */
57
    tag = avio_rl32(pb);
58
    if (tag != MKTAG('R', 'I', 'F', 'F'))
59
        return -1;
60
    avio_rl32(pb); /* file size */
61
    tag = avio_rl32(pb);
62
    if (tag != MKTAG('X', 'W', 'M', 'A'))
63
        return -1;
64

    
65
    /* parse fmt header */
66
    tag = avio_rl32(pb);
67
    if (tag != MKTAG('f', 'm', 't', ' '))
68
        return -1;
69
    size = avio_rl32(pb);
70
    st = av_new_stream(s, 0);
71
    if (!st)
72
        return AVERROR(ENOMEM);
73

    
74
    ret = ff_get_wav_header(pb, st->codec, size);
75
    if (ret < 0)
76
        return ret;
77
    st->need_parsing = AVSTREAM_PARSE_NONE;
78

    
79
    /* All xWMA files I have seen contained WMAv2 data. If there are files
80
     * using WMA Pro or some other codec, then we need to figure out the right
81
     * extradata for that. Thus, ask the user for feedback, but try to go on
82
     * anyway.
83
     */
84
    if (st->codec->codec_id != CODEC_ID_WMAV2) {
85
        av_log(s, AV_LOG_WARNING, "unexpected codec (tag 0x04%x; id %d)\n",
86
                              st->codec->codec_tag, st->codec->codec_id);
87
        av_log_ask_for_sample(s, NULL);
88
    } else {
89
        /* In all xWMA files I have seen, there is no extradata. But the WMA
90
         * codecs require extradata, so we provide our own fake extradata.
91
         *
92
         * First, check that there really was no extradata in the header. If
93
         * there was, then try to use it, after asking the user to provide a
94
         * sample of this unusual file.
95
         */
96
        if (st->codec->extradata_size != 0) {
97
            /* Surprise, surprise: We *did* get some extradata. No idea
98
             * if it will work, but just go on and try it, after asking
99
             * the user for a sample.
100
             */
101
            av_log(s, AV_LOG_WARNING, "unexpected extradata (%d bytes)\n",
102
                                  st->codec->extradata_size);
103
            av_log_ask_for_sample(s, NULL);
104
        } else {
105
            st->codec->extradata_size = 6;
106
            st->codec->extradata      = av_mallocz(6 + FF_INPUT_BUFFER_PADDING_SIZE);
107
            if (!st->codec->extradata)
108
                return AVERROR(ENOMEM);
109

    
110
            /* setup extradata with our experimentally obtained value */
111
            st->codec->extradata[4] = 31;
112
        }
113
    }
114

    
115
    /* set the sample rate */
116
    av_set_pts_info(st, 64, 1, st->codec->sample_rate);
117

    
118
    /* parse the remaining RIFF chunks */
119
    for (;;) {
120
        if (pb->eof_reached)
121
            return -1;
122
        /* read next chunk tag */
123
        tag = avio_rl32(pb);
124
        size = avio_rl32(pb);
125
        if (tag == MKTAG('d', 'a', 't', 'a')) {
126
            /* We assume that the data chunk comes last. */
127
            break;
128
        } else if (tag == MKTAG('d','p','d','s')) {
129
            /* Quoting the MSDN xWMA docs on the dpds chunk: "Contains the
130
             * decoded packet cumulative data size array, each element is the
131
             * number of bytes accumulated after the corresponding xWMA packet
132
             * is decoded in order."
133
             *
134
             * Each packet has size equal to st->codec->block_align, which in
135
             * all cases I saw so far was always 2230. Thus, we can use the
136
             * dpds data to compute a seeking index.
137
             */
138

    
139
            /* Error out if there is more than one dpds chunk. */
140
            if (dpds_table) {
141
                av_log(s, AV_LOG_ERROR, "two dpds chunks present\n");
142
                return -1;
143
            }
144

    
145
            /* Compute the number of entries in the dpds chunk. */
146
            if (size & 3) {  /* Size should be divisible by four */
147
                av_log(s, AV_LOG_WARNING, "dpds chunk size "PRId64" not divisible by 4\n", size);
148
            }
149
            dpds_table_size = size / 4;
150
            if (dpds_table_size == 0 || dpds_table_size >= INT_MAX / 4) {
151
                av_log(s, AV_LOG_ERROR, "dpds chunk size "PRId64" invalid\n", size);
152
                return -1;
153
            }
154

    
155
            /* Allocate some temporary storage to keep the dpds data around.
156
             * for processing later on.
157
             */
158
            dpds_table = av_malloc(dpds_table_size * sizeof(uint32_t));
159
            if (!dpds_table) {
160
                return AVERROR(ENOMEM);
161
            }
162

    
163
            for (i = 0; i < dpds_table_size; ++i) {
164
                dpds_table[i] = avio_rl32(pb);
165
                size -= 4;
166
            }
167
        }
168
        avio_skip(pb, size);
169
    }
170

    
171
    /* Determine overall data length */
172
    if (size < 0)
173
        return -1;
174
    if (!size) {
175
        xwma->data_end = INT64_MAX;
176
    } else
177
        xwma->data_end = avio_tell(pb) + size;
178

    
179

    
180
    if (dpds_table && dpds_table_size) {
181
        int64_t cur_pos;
182
        const uint32_t bytes_per_sample
183
                = (st->codec->channels * st->codec->bits_per_coded_sample) >> 3;
184

    
185
        /* Estimate the duration from the total number of output bytes. */
186
        const uint64_t total_decoded_bytes = dpds_table[dpds_table_size - 1];
187
        st->duration = total_decoded_bytes / bytes_per_sample;
188

    
189
        /* Use the dpds data to build a seek table.  We can only do this after
190
         * we know the offset to the data chunk, as we need that to determine
191
         * the actual offset to each input block.
192
         * Note: If we allowed ourselves to assume that the data chunk always
193
         * follows immediately after the dpds block, we could of course guess
194
         * the data block's start offset already while reading the dpds chunk.
195
         * I decided against that, just in case other chunks ever are
196
         * discovered.
197
         */
198
        cur_pos = avio_tell(pb);
199
        for (i = 0; i < dpds_table_size; ++i) {
200
            /* From the number of output bytes that would accumulate in the
201
             * output buffer after decoding the first (i+1) packets, we compute
202
             * an offset / timestamp pair.
203
             */
204
            av_add_index_entry(st,
205
                               cur_pos + (i+1) * st->codec->block_align, /* pos */
206
                               dpds_table[i] / bytes_per_sample,         /* timestamp */
207
                               st->codec->block_align,                   /* size */
208
                               0,                                        /* duration */
209
                               AVINDEX_KEYFRAME);
210
        }
211
    } else if (st->codec->bit_rate) {
212
        /* No dpds chunk was present (or only an empty one), so estimate
213
         * the total duration using the average bits per sample and the
214
         * total data length.
215
         */
216
        st->duration = (size<<3) * st->codec->sample_rate / st->codec->bit_rate;
217
    }
218

    
219
    av_free(dpds_table);
220

    
221
    return 0;
222
}
223

    
224
static int xwma_read_packet(AVFormatContext *s, AVPacket *pkt)
225
{
226
    int ret, size;
227
    int64_t left;
228
    AVStream *st;
229
    XWMAContext *xwma = s->priv_data;
230

    
231
    st = s->streams[0];
232

    
233
    left = xwma->data_end - avio_tell(s->pb);
234
    if (left <= 0) {
235
        return AVERROR_EOF;
236
    }
237

    
238
    /* read a single block; the default block size is 2230. */
239
    size = (st->codec->block_align > 1) ? st->codec->block_align : 2230;
240
    size = FFMIN(size, left);
241

    
242
    ret  = av_get_packet(s->pb, pkt, size);
243
    if (ret < 0)
244
        return ret;
245

    
246
    pkt->stream_index = 0;
247
    return ret;
248
}
249

    
250
AVInputFormat ff_xwma_demuxer = {
251
    "xwma",
252
    NULL_IF_CONFIG_SMALL("Microsoft xWMA"),
253
    sizeof(XWMAContext),
254
    xwma_probe,
255
    xwma_read_header,
256
    xwma_read_packet,
257
};