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.

280 lines
9.4KB

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