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.

288 lines
9.6KB

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