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.

281 lines
9.5KB

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