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.

258 lines
8.7KB

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