xwma.c 8.9 KB

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