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 Libav.
00006  *
00007  * Libav 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  * Libav 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 Libav; 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     if (!st->codec->channels) {
00119         av_log(s, AV_LOG_WARNING, "Invalid channel count: %d\n",
00120                st->codec->channels);
00121         return AVERROR_INVALIDDATA;
00122     }
00123     if (!st->codec->bits_per_coded_sample) {
00124         av_log(s, AV_LOG_WARNING, "Invalid bits_per_coded_sample: %d\n",
00125                st->codec->bits_per_coded_sample);
00126         return AVERROR_INVALIDDATA;
00127     }
00128 
00129     /* set the sample rate */
00130     avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
00131 
00132     /* parse the remaining RIFF chunks */
00133     for (;;) {
00134         if (pb->eof_reached)
00135             return -1;
00136         /* read next chunk tag */
00137         tag = avio_rl32(pb);
00138         size = avio_rl32(pb);
00139         if (tag == MKTAG('d', 'a', 't', 'a')) {
00140             /* We assume that the data chunk comes last. */
00141             break;
00142         } else if (tag == MKTAG('d','p','d','s')) {
00143             /* Quoting the MSDN xWMA docs on the dpds chunk: "Contains the
00144              * decoded packet cumulative data size array, each element is the
00145              * number of bytes accumulated after the corresponding xWMA packet
00146              * is decoded in order."
00147              *
00148              * Each packet has size equal to st->codec->block_align, which in
00149              * all cases I saw so far was always 2230. Thus, we can use the
00150              * dpds data to compute a seeking index.
00151              */
00152 
00153             /* Error out if there is more than one dpds chunk. */
00154             if (dpds_table) {
00155                 av_log(s, AV_LOG_ERROR, "two dpds chunks present\n");
00156                 return -1;
00157             }
00158 
00159             /* Compute the number of entries in the dpds chunk. */
00160             if (size & 3) {  /* Size should be divisible by four */
00161                 av_log(s, AV_LOG_WARNING,
00162                        "dpds chunk size %"PRId64" not divisible by 4\n", size);
00163             }
00164             dpds_table_size = size / 4;
00165             if (dpds_table_size == 0 || dpds_table_size >= INT_MAX / 4) {
00166                 av_log(s, AV_LOG_ERROR,
00167                        "dpds chunk size %"PRId64" invalid\n", size);
00168                 return -1;
00169             }
00170 
00171             /* Allocate some temporary storage to keep the dpds data around.
00172              * for processing later on.
00173              */
00174             dpds_table = av_malloc(dpds_table_size * sizeof(uint32_t));
00175             if (!dpds_table) {
00176                 return AVERROR(ENOMEM);
00177             }
00178 
00179             for (i = 0; i < dpds_table_size; ++i) {
00180                 dpds_table[i] = avio_rl32(pb);
00181                 size -= 4;
00182             }
00183         }
00184         avio_skip(pb, size);
00185     }
00186 
00187     /* Determine overall data length */
00188     if (size < 0)
00189         return -1;
00190     if (!size) {
00191         xwma->data_end = INT64_MAX;
00192     } else
00193         xwma->data_end = avio_tell(pb) + size;
00194 
00195 
00196     if (dpds_table && dpds_table_size) {
00197         int64_t cur_pos;
00198         const uint32_t bytes_per_sample
00199                 = (st->codec->channels * st->codec->bits_per_coded_sample) >> 3;
00200 
00201         /* Estimate the duration from the total number of output bytes. */
00202         const uint64_t total_decoded_bytes = dpds_table[dpds_table_size - 1];
00203         st->duration = total_decoded_bytes / bytes_per_sample;
00204 
00205         /* Use the dpds data to build a seek table.  We can only do this after
00206          * we know the offset to the data chunk, as we need that to determine
00207          * the actual offset to each input block.
00208          * Note: If we allowed ourselves to assume that the data chunk always
00209          * follows immediately after the dpds block, we could of course guess
00210          * the data block's start offset already while reading the dpds chunk.
00211          * I decided against that, just in case other chunks ever are
00212          * discovered.
00213          */
00214         cur_pos = avio_tell(pb);
00215         for (i = 0; i < dpds_table_size; ++i) {
00216             /* From the number of output bytes that would accumulate in the
00217              * output buffer after decoding the first (i+1) packets, we compute
00218              * an offset / timestamp pair.
00219              */
00220             av_add_index_entry(st,
00221                                cur_pos + (i+1) * st->codec->block_align, /* pos */
00222                                dpds_table[i] / bytes_per_sample,         /* timestamp */
00223                                st->codec->block_align,                   /* size */
00224                                0,                                        /* duration */
00225                                AVINDEX_KEYFRAME);
00226         }
00227     } else if (st->codec->bit_rate) {
00228         /* No dpds chunk was present (or only an empty one), so estimate
00229          * the total duration using the average bits per sample and the
00230          * total data length.
00231          */
00232         st->duration = (size<<3) * st->codec->sample_rate / st->codec->bit_rate;
00233     }
00234 
00235     av_free(dpds_table);
00236 
00237     return 0;
00238 }
00239 
00240 static int xwma_read_packet(AVFormatContext *s, AVPacket *pkt)
00241 {
00242     int ret, size;
00243     int64_t left;
00244     AVStream *st;
00245     XWMAContext *xwma = s->priv_data;
00246 
00247     st = s->streams[0];
00248 
00249     left = xwma->data_end - avio_tell(s->pb);
00250     if (left <= 0) {
00251         return AVERROR_EOF;
00252     }
00253 
00254     /* read a single block; the default block size is 2230. */
00255     size = (st->codec->block_align > 1) ? st->codec->block_align : 2230;
00256     size = FFMIN(size, left);
00257 
00258     ret  = av_get_packet(s->pb, pkt, size);
00259     if (ret < 0)
00260         return ret;
00261 
00262     pkt->stream_index = 0;
00263     return ret;
00264 }
00265 
00266 AVInputFormat ff_xwma_demuxer = {
00267     .name           = "xwma",
00268     .long_name      = NULL_IF_CONFIG_SMALL("Microsoft xWMA"),
00269     .priv_data_size = sizeof(XWMAContext),
00270     .read_probe     = xwma_probe,
00271     .read_header    = xwma_read_header,
00272     .read_packet    = xwma_read_packet,
00273 };