xwma.c 9.6 KB

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