libavformat/xwma.c
Go to the documentation of this file.
00001 /*
00002  * xWMA demuxer
00003  * Copyright (c) 2011 Max Horn
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include <inttypes.h>
00023 
00024 #include "avformat.h"
00025 #include "internal.h"
00026 #include "riff.h"
00027 
00028 /*
00029  * Demuxer for xWMA, a Microsoft audio container used by XAudio 2.
00030  */
00031 
00032 typedef struct {
00033     int64_t data_end;
00034 } XWMAContext;
00035 
00036 static int xwma_probe(AVProbeData *p)
00037 {
00038     if (!memcmp(p->buf, "RIFF", 4) && !memcmp(p->buf + 8, "XWMA", 4))
00039         return AVPROBE_SCORE_MAX;
00040     return 0;
00041 }
00042 
00043 static int xwma_read_header(AVFormatContext *s, AVFormatParameters *ap)
00044 {
00045     int64_t size, av_uninit(data_size);
00046     int ret;
00047     uint32_t dpds_table_size = 0;
00048     uint32_t *dpds_table = 0;
00049     unsigned int tag;
00050     AVIOContext *pb = s->pb;
00051     AVStream *st;
00052     XWMAContext *xwma = s->priv_data;
00053     int i;
00054 
00055     /* The following code is mostly copied from wav.c, with some
00056      * minor alterations.
00057      */
00058 
00059     /* check RIFF header */
00060     tag = avio_rl32(pb);
00061     if (tag != MKTAG('R', 'I', 'F', 'F'))
00062         return -1;
00063     avio_rl32(pb); /* file size */
00064     tag = avio_rl32(pb);
00065     if (tag != MKTAG('X', 'W', 'M', 'A'))
00066         return -1;
00067 
00068     /* parse fmt header */
00069     tag = avio_rl32(pb);
00070     if (tag != MKTAG('f', 'm', 't', ' '))
00071         return -1;
00072     size = avio_rl32(pb);
00073     st = avformat_new_stream(s, NULL);
00074     if (!st)
00075         return AVERROR(ENOMEM);
00076 
00077     ret = ff_get_wav_header(pb, st->codec, size);
00078     if (ret < 0)
00079         return ret;
00080     st->need_parsing = AVSTREAM_PARSE_NONE;
00081 
00082     /* All xWMA files I have seen contained WMAv2 data. If there are files
00083      * using WMA Pro or some other codec, then we need to figure out the right
00084      * extradata for that. Thus, ask the user for feedback, but try to go on
00085      * anyway.
00086      */
00087     if (st->codec->codec_id != CODEC_ID_WMAV2) {
00088         av_log(s, AV_LOG_WARNING, "unexpected codec (tag 0x04%x; id %d)\n",
00089                               st->codec->codec_tag, st->codec->codec_id);
00090         av_log_ask_for_sample(s, NULL);
00091     } else {
00092         /* In all xWMA files I have seen, there is no extradata. But the WMA
00093          * codecs require extradata, so we provide our own fake extradata.
00094          *
00095          * First, check that there really was no extradata in the header. If
00096          * there was, then try to use it, after asking the user to provide a
00097          * sample of this unusual file.
00098          */
00099         if (st->codec->extradata_size != 0) {
00100             /* Surprise, surprise: We *did* get some extradata. No idea
00101              * if it will work, but just go on and try it, after asking
00102              * the user for a sample.
00103              */
00104             av_log(s, AV_LOG_WARNING, "unexpected extradata (%d bytes)\n",
00105                                   st->codec->extradata_size);
00106             av_log_ask_for_sample(s, NULL);
00107         } else {
00108             st->codec->extradata_size = 6;
00109             st->codec->extradata      = av_mallocz(6 + FF_INPUT_BUFFER_PADDING_SIZE);
00110             if (!st->codec->extradata)
00111                 return AVERROR(ENOMEM);
00112 
00113             /* setup extradata with our experimentally obtained value */
00114             st->codec->extradata[4] = 31;
00115         }
00116     }
00117 
00118     /* set the sample rate */
00119     avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
00120 
00121     /* parse the remaining RIFF chunks */
00122     for (;;) {
00123         if (pb->eof_reached)
00124             return -1;
00125         /* read next chunk tag */
00126         tag = avio_rl32(pb);
00127         size = avio_rl32(pb);
00128         if (tag == MKTAG('d', 'a', 't', 'a')) {
00129             /* We assume that the data chunk comes last. */
00130             break;
00131         } else if (tag == MKTAG('d','p','d','s')) {
00132             /* Quoting the MSDN xWMA docs on the dpds chunk: "Contains the
00133              * decoded packet cumulative data size array, each element is the
00134              * number of bytes accumulated after the corresponding xWMA packet
00135              * is decoded in order."
00136              *
00137              * Each packet has size equal to st->codec->block_align, which in
00138              * all cases I saw so far was always 2230. Thus, we can use the
00139              * dpds data to compute a seeking index.
00140              */
00141 
00142             /* Error out if there is more than one dpds chunk. */
00143             if (dpds_table) {
00144                 av_log(s, AV_LOG_ERROR, "two dpds chunks present\n");
00145                 return -1;
00146             }
00147 
00148             /* Compute the number of entries in the dpds chunk. */
00149             if (size & 3) {  /* Size should be divisible by four */
00150                 av_log(s, AV_LOG_WARNING,
00151                        "dpds chunk size %"PRId64" not divisible by 4\n", size);
00152             }
00153             dpds_table_size = size / 4;
00154             if (dpds_table_size == 0 || dpds_table_size >= INT_MAX / 4) {
00155                 av_log(s, AV_LOG_ERROR,
00156                        "dpds chunk size %"PRId64" invalid\n", size);
00157                 return -1;
00158             }
00159 
00160             /* Allocate some temporary storage to keep the dpds data around.
00161              * for processing later on.
00162              */
00163             dpds_table = av_malloc(dpds_table_size * sizeof(uint32_t));
00164             if (!dpds_table) {
00165                 return AVERROR(ENOMEM);
00166             }
00167 
00168             for (i = 0; i < dpds_table_size; ++i) {
00169                 dpds_table[i] = avio_rl32(pb);
00170                 size -= 4;
00171             }
00172         }
00173         avio_skip(pb, size);
00174     }
00175 
00176     /* Determine overall data length */
00177     if (size < 0)
00178         return -1;
00179     if (!size) {
00180         xwma->data_end = INT64_MAX;
00181     } else
00182         xwma->data_end = avio_tell(pb) + size;
00183 
00184 
00185     if (dpds_table && dpds_table_size) {
00186         int64_t cur_pos;
00187         const uint32_t bytes_per_sample
00188                 = (st->codec->channels * st->codec->bits_per_coded_sample) >> 3;
00189 
00190         /* Estimate the duration from the total number of output bytes. */
00191         const uint64_t total_decoded_bytes = dpds_table[dpds_table_size - 1];
00192         st->duration = total_decoded_bytes / bytes_per_sample;
00193 
00194         /* Use the dpds data to build a seek table.  We can only do this after
00195          * we know the offset to the data chunk, as we need that to determine
00196          * the actual offset to each input block.
00197          * Note: If we allowed ourselves to assume that the data chunk always
00198          * follows immediately after the dpds block, we could of course guess
00199          * the data block's start offset already while reading the dpds chunk.
00200          * I decided against that, just in case other chunks ever are
00201          * discovered.
00202          */
00203         cur_pos = avio_tell(pb);
00204         for (i = 0; i < dpds_table_size; ++i) {
00205             /* From the number of output bytes that would accumulate in the
00206              * output buffer after decoding the first (i+1) packets, we compute
00207              * an offset / timestamp pair.
00208              */
00209             av_add_index_entry(st,
00210                                cur_pos + (i+1) * st->codec->block_align, /* pos */
00211                                dpds_table[i] / bytes_per_sample,         /* timestamp */
00212                                st->codec->block_align,                   /* size */
00213                                0,                                        /* duration */
00214                                AVINDEX_KEYFRAME);
00215         }
00216     } else if (st->codec->bit_rate) {
00217         /* No dpds chunk was present (or only an empty one), so estimate
00218          * the total duration using the average bits per sample and the
00219          * total data length.
00220          */
00221         st->duration = (size<<3) * st->codec->sample_rate / st->codec->bit_rate;
00222     }
00223 
00224     av_free(dpds_table);
00225 
00226     return 0;
00227 }
00228 
00229 static int xwma_read_packet(AVFormatContext *s, AVPacket *pkt)
00230 {
00231     int ret, size;
00232     int64_t left;
00233     AVStream *st;
00234     XWMAContext *xwma = s->priv_data;
00235 
00236     st = s->streams[0];
00237 
00238     left = xwma->data_end - avio_tell(s->pb);
00239     if (left <= 0) {
00240         return AVERROR_EOF;
00241     }
00242 
00243     /* read a single block; the default block size is 2230. */
00244     size = (st->codec->block_align > 1) ? st->codec->block_align : 2230;
00245     size = FFMIN(size, left);
00246 
00247     ret  = av_get_packet(s->pb, pkt, size);
00248     if (ret < 0)
00249         return ret;
00250 
00251     pkt->stream_index = 0;
00252     return ret;
00253 }
00254 
00255 AVInputFormat ff_xwma_demuxer = {
00256     .name           = "xwma",
00257     .long_name      = NULL_IF_CONFIG_SMALL("Microsoft xWMA"),
00258     .priv_data_size = sizeof(XWMAContext),
00259     .read_probe     = xwma_probe,
00260     .read_header    = xwma_read_header,
00261     .read_packet    = xwma_read_packet,
00262 };