yading@11: /* yading@11: * xWMA demuxer yading@11: * Copyright (c) 2011 Max Horn yading@11: * yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: #include yading@11: yading@11: #include "avformat.h" yading@11: #include "internal.h" yading@11: #include "riff.h" yading@11: yading@11: /* yading@11: * Demuxer for xWMA, a Microsoft audio container used by XAudio 2. yading@11: */ yading@11: yading@11: typedef struct { yading@11: int64_t data_end; yading@11: } XWMAContext; yading@11: yading@11: static int xwma_probe(AVProbeData *p) yading@11: { yading@11: if (!memcmp(p->buf, "RIFF", 4) && !memcmp(p->buf + 8, "XWMA", 4)) yading@11: return AVPROBE_SCORE_MAX; yading@11: return 0; yading@11: } yading@11: yading@11: static int xwma_read_header(AVFormatContext *s) yading@11: { yading@11: int64_t size; yading@11: int ret; yading@11: uint32_t dpds_table_size = 0; yading@11: uint32_t *dpds_table = 0; yading@11: unsigned int tag; yading@11: AVIOContext *pb = s->pb; yading@11: AVStream *st; yading@11: XWMAContext *xwma = s->priv_data; yading@11: int i; yading@11: yading@11: /* The following code is mostly copied from wav.c, with some yading@11: * minor alterations. yading@11: */ yading@11: yading@11: /* check RIFF header */ yading@11: tag = avio_rl32(pb); yading@11: if (tag != MKTAG('R', 'I', 'F', 'F')) yading@11: return -1; yading@11: avio_rl32(pb); /* file size */ yading@11: tag = avio_rl32(pb); yading@11: if (tag != MKTAG('X', 'W', 'M', 'A')) yading@11: return -1; yading@11: yading@11: /* parse fmt header */ yading@11: tag = avio_rl32(pb); yading@11: if (tag != MKTAG('f', 'm', 't', ' ')) yading@11: return -1; yading@11: size = avio_rl32(pb); yading@11: st = avformat_new_stream(s, NULL); yading@11: if (!st) yading@11: return AVERROR(ENOMEM); yading@11: yading@11: ret = ff_get_wav_header(pb, st->codec, size); yading@11: if (ret < 0) yading@11: return ret; yading@11: st->need_parsing = AVSTREAM_PARSE_NONE; yading@11: yading@11: /* All xWMA files I have seen contained WMAv2 data. If there are files yading@11: * using WMA Pro or some other codec, then we need to figure out the right yading@11: * extradata for that. Thus, ask the user for feedback, but try to go on yading@11: * anyway. yading@11: */ yading@11: if (st->codec->codec_id != AV_CODEC_ID_WMAV2) { yading@11: avpriv_request_sample(s, "Unexpected codec (tag 0x04%x; id %d)", yading@11: st->codec->codec_tag, st->codec->codec_id); yading@11: } else { yading@11: /* In all xWMA files I have seen, there is no extradata. But the WMA yading@11: * codecs require extradata, so we provide our own fake extradata. yading@11: * yading@11: * First, check that there really was no extradata in the header. If yading@11: * there was, then try to use it, after asking the user to provide a yading@11: * sample of this unusual file. yading@11: */ yading@11: if (st->codec->extradata_size != 0) { yading@11: /* Surprise, surprise: We *did* get some extradata. No idea yading@11: * if it will work, but just go on and try it, after asking yading@11: * the user for a sample. yading@11: */ yading@11: avpriv_request_sample(s, "Unexpected extradata (%d bytes)", yading@11: st->codec->extradata_size); yading@11: } else { yading@11: st->codec->extradata_size = 6; yading@11: st->codec->extradata = av_mallocz(6 + FF_INPUT_BUFFER_PADDING_SIZE); yading@11: if (!st->codec->extradata) yading@11: return AVERROR(ENOMEM); yading@11: yading@11: /* setup extradata with our experimentally obtained value */ yading@11: st->codec->extradata[4] = 31; yading@11: } yading@11: } yading@11: yading@11: if (!st->codec->channels) { yading@11: av_log(s, AV_LOG_WARNING, "Invalid channel count: %d\n", yading@11: st->codec->channels); yading@11: return AVERROR_INVALIDDATA; yading@11: } yading@11: if (!st->codec->bits_per_coded_sample) { yading@11: av_log(s, AV_LOG_WARNING, "Invalid bits_per_coded_sample: %d\n", yading@11: st->codec->bits_per_coded_sample); yading@11: return AVERROR_INVALIDDATA; yading@11: } yading@11: yading@11: /* set the sample rate */ yading@11: avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate); yading@11: yading@11: /* parse the remaining RIFF chunks */ yading@11: for (;;) { yading@11: if (pb->eof_reached) yading@11: return -1; yading@11: /* read next chunk tag */ yading@11: tag = avio_rl32(pb); yading@11: size = avio_rl32(pb); yading@11: if (tag == MKTAG('d', 'a', 't', 'a')) { yading@11: /* We assume that the data chunk comes last. */ yading@11: break; yading@11: } else if (tag == MKTAG('d','p','d','s')) { yading@11: /* Quoting the MSDN xWMA docs on the dpds chunk: "Contains the yading@11: * decoded packet cumulative data size array, each element is the yading@11: * number of bytes accumulated after the corresponding xWMA packet yading@11: * is decoded in order." yading@11: * yading@11: * Each packet has size equal to st->codec->block_align, which in yading@11: * all cases I saw so far was always 2230. Thus, we can use the yading@11: * dpds data to compute a seeking index. yading@11: */ yading@11: yading@11: /* Error out if there is more than one dpds chunk. */ yading@11: if (dpds_table) { yading@11: av_log(s, AV_LOG_ERROR, "two dpds chunks present\n"); yading@11: return -1; yading@11: } yading@11: yading@11: /* Compute the number of entries in the dpds chunk. */ yading@11: if (size & 3) { /* Size should be divisible by four */ yading@11: av_log(s, AV_LOG_WARNING, yading@11: "dpds chunk size %"PRId64" not divisible by 4\n", size); yading@11: } yading@11: dpds_table_size = size / 4; yading@11: if (dpds_table_size == 0 || dpds_table_size >= INT_MAX / 4) { yading@11: av_log(s, AV_LOG_ERROR, yading@11: "dpds chunk size %"PRId64" invalid\n", size); yading@11: return -1; yading@11: } yading@11: yading@11: /* Allocate some temporary storage to keep the dpds data around. yading@11: * for processing later on. yading@11: */ yading@11: dpds_table = av_malloc(dpds_table_size * sizeof(uint32_t)); yading@11: if (!dpds_table) { yading@11: return AVERROR(ENOMEM); yading@11: } yading@11: yading@11: for (i = 0; i < dpds_table_size; ++i) { yading@11: dpds_table[i] = avio_rl32(pb); yading@11: size -= 4; yading@11: } yading@11: } yading@11: avio_skip(pb, size); yading@11: } yading@11: yading@11: /* Determine overall data length */ yading@11: if (size < 0) yading@11: return -1; yading@11: if (!size) { yading@11: xwma->data_end = INT64_MAX; yading@11: } else yading@11: xwma->data_end = avio_tell(pb) + size; yading@11: yading@11: yading@11: if (dpds_table && dpds_table_size) { yading@11: int64_t cur_pos; yading@11: const uint32_t bytes_per_sample yading@11: = (st->codec->channels * st->codec->bits_per_coded_sample) >> 3; yading@11: yading@11: /* Estimate the duration from the total number of output bytes. */ yading@11: const uint64_t total_decoded_bytes = dpds_table[dpds_table_size - 1]; yading@11: yading@11: if(!bytes_per_sample) { yading@11: av_log(s, AV_LOG_ERROR, "bytes_per_sample is 0\n"); yading@11: return AVERROR_INVALIDDATA; yading@11: } yading@11: yading@11: st->duration = total_decoded_bytes / bytes_per_sample; yading@11: yading@11: /* Use the dpds data to build a seek table. We can only do this after yading@11: * we know the offset to the data chunk, as we need that to determine yading@11: * the actual offset to each input block. yading@11: * Note: If we allowed ourselves to assume that the data chunk always yading@11: * follows immediately after the dpds block, we could of course guess yading@11: * the data block's start offset already while reading the dpds chunk. yading@11: * I decided against that, just in case other chunks ever are yading@11: * discovered. yading@11: */ yading@11: cur_pos = avio_tell(pb); yading@11: for (i = 0; i < dpds_table_size; ++i) { yading@11: /* From the number of output bytes that would accumulate in the yading@11: * output buffer after decoding the first (i+1) packets, we compute yading@11: * an offset / timestamp pair. yading@11: */ yading@11: av_add_index_entry(st, yading@11: cur_pos + (i+1) * st->codec->block_align, /* pos */ yading@11: dpds_table[i] / bytes_per_sample, /* timestamp */ yading@11: st->codec->block_align, /* size */ yading@11: 0, /* duration */ yading@11: AVINDEX_KEYFRAME); yading@11: } yading@11: } else if (st->codec->bit_rate) { yading@11: /* No dpds chunk was present (or only an empty one), so estimate yading@11: * the total duration using the average bits per sample and the yading@11: * total data length. yading@11: */ yading@11: st->duration = (size<<3) * st->codec->sample_rate / st->codec->bit_rate; yading@11: } yading@11: yading@11: av_free(dpds_table); yading@11: yading@11: return 0; yading@11: } yading@11: yading@11: static int xwma_read_packet(AVFormatContext *s, AVPacket *pkt) yading@11: { yading@11: int ret, size; yading@11: int64_t left; yading@11: AVStream *st; yading@11: XWMAContext *xwma = s->priv_data; yading@11: yading@11: st = s->streams[0]; yading@11: yading@11: left = xwma->data_end - avio_tell(s->pb); yading@11: if (left <= 0) { yading@11: return AVERROR_EOF; yading@11: } yading@11: yading@11: /* read a single block; the default block size is 2230. */ yading@11: size = (st->codec->block_align > 1) ? st->codec->block_align : 2230; yading@11: size = FFMIN(size, left); yading@11: yading@11: ret = av_get_packet(s->pb, pkt, size); yading@11: if (ret < 0) yading@11: return ret; yading@11: yading@11: pkt->stream_index = 0; yading@11: return ret; yading@11: } yading@11: yading@11: AVInputFormat ff_xwma_demuxer = { yading@11: .name = "xwma", yading@11: .long_name = NULL_IF_CONFIG_SMALL("Microsoft xWMA"), yading@11: .priv_data_size = sizeof(XWMAContext), yading@11: .read_probe = xwma_probe, yading@11: .read_header = xwma_read_header, yading@11: .read_packet = xwma_read_packet, yading@11: };