You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

263 lines
8.9KB

  1. /*
  2. * xWMA demuxer
  3. * Copyright (c) 2011 Max Horn
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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)
  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. /* set the sample rate */
  106. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  107. /* parse the remaining RIFF chunks */
  108. for (;;) {
  109. if (pb->eof_reached)
  110. return -1;
  111. /* read next chunk tag */
  112. tag = avio_rl32(pb);
  113. size = avio_rl32(pb);
  114. if (tag == MKTAG('d', 'a', 't', 'a')) {
  115. /* We assume that the data chunk comes last. */
  116. break;
  117. } else if (tag == MKTAG('d','p','d','s')) {
  118. /* Quoting the MSDN xWMA docs on the dpds chunk: "Contains the
  119. * decoded packet cumulative data size array, each element is the
  120. * number of bytes accumulated after the corresponding xWMA packet
  121. * is decoded in order."
  122. *
  123. * Each packet has size equal to st->codec->block_align, which in
  124. * all cases I saw so far was always 2230. Thus, we can use the
  125. * dpds data to compute a seeking index.
  126. */
  127. /* Error out if there is more than one dpds chunk. */
  128. if (dpds_table) {
  129. av_log(s, AV_LOG_ERROR, "two dpds chunks present\n");
  130. return -1;
  131. }
  132. /* Compute the number of entries in the dpds chunk. */
  133. if (size & 3) { /* Size should be divisible by four */
  134. av_log(s, AV_LOG_WARNING,
  135. "dpds chunk size %"PRId64" not divisible by 4\n", size);
  136. }
  137. dpds_table_size = size / 4;
  138. if (dpds_table_size == 0 || dpds_table_size >= INT_MAX / 4) {
  139. av_log(s, AV_LOG_ERROR,
  140. "dpds chunk size %"PRId64" invalid\n", size);
  141. return -1;
  142. }
  143. /* Allocate some temporary storage to keep the dpds data around.
  144. * for processing later on.
  145. */
  146. dpds_table = av_malloc(dpds_table_size * sizeof(uint32_t));
  147. if (!dpds_table) {
  148. return AVERROR(ENOMEM);
  149. }
  150. for (i = 0; i < dpds_table_size; ++i) {
  151. dpds_table[i] = avio_rl32(pb);
  152. size -= 4;
  153. }
  154. }
  155. avio_skip(pb, size);
  156. }
  157. /* Determine overall data length */
  158. if (size < 0)
  159. return -1;
  160. if (!size) {
  161. xwma->data_end = INT64_MAX;
  162. } else
  163. xwma->data_end = avio_tell(pb) + size;
  164. if (dpds_table && dpds_table_size) {
  165. int64_t cur_pos;
  166. const uint32_t bytes_per_sample
  167. = (st->codec->channels * st->codec->bits_per_coded_sample) >> 3;
  168. /* Estimate the duration from the total number of output bytes. */
  169. const uint64_t total_decoded_bytes = dpds_table[dpds_table_size - 1];
  170. st->duration = total_decoded_bytes / bytes_per_sample;
  171. /* Use the dpds data to build a seek table. We can only do this after
  172. * we know the offset to the data chunk, as we need that to determine
  173. * the actual offset to each input block.
  174. * Note: If we allowed ourselves to assume that the data chunk always
  175. * follows immediately after the dpds block, we could of course guess
  176. * the data block's start offset already while reading the dpds chunk.
  177. * I decided against that, just in case other chunks ever are
  178. * discovered.
  179. */
  180. cur_pos = avio_tell(pb);
  181. for (i = 0; i < dpds_table_size; ++i) {
  182. /* From the number of output bytes that would accumulate in the
  183. * output buffer after decoding the first (i+1) packets, we compute
  184. * an offset / timestamp pair.
  185. */
  186. av_add_index_entry(st,
  187. cur_pos + (i+1) * st->codec->block_align, /* pos */
  188. dpds_table[i] / bytes_per_sample, /* timestamp */
  189. st->codec->block_align, /* size */
  190. 0, /* duration */
  191. AVINDEX_KEYFRAME);
  192. }
  193. } else if (st->codec->bit_rate) {
  194. /* No dpds chunk was present (or only an empty one), so estimate
  195. * the total duration using the average bits per sample and the
  196. * total data length.
  197. */
  198. st->duration = (size<<3) * st->codec->sample_rate / st->codec->bit_rate;
  199. }
  200. av_free(dpds_table);
  201. return 0;
  202. }
  203. static int xwma_read_packet(AVFormatContext *s, AVPacket *pkt)
  204. {
  205. int ret, size;
  206. int64_t left;
  207. AVStream *st;
  208. XWMAContext *xwma = s->priv_data;
  209. st = s->streams[0];
  210. left = xwma->data_end - avio_tell(s->pb);
  211. if (left <= 0) {
  212. return AVERROR_EOF;
  213. }
  214. /* read a single block; the default block size is 2230. */
  215. size = (st->codec->block_align > 1) ? st->codec->block_align : 2230;
  216. size = FFMIN(size, left);
  217. ret = av_get_packet(s->pb, pkt, size);
  218. if (ret < 0)
  219. return ret;
  220. pkt->stream_index = 0;
  221. return ret;
  222. }
  223. AVInputFormat ff_xwma_demuxer = {
  224. .name = "xwma",
  225. .long_name = NULL_IF_CONFIG_SMALL("Microsoft xWMA"),
  226. .priv_data_size = sizeof(XWMAContext),
  227. .read_probe = xwma_probe,
  228. .read_header = xwma_read_header,
  229. .read_packet = xwma_read_packet,
  230. };