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.

331 lines
10KB

  1. /*
  2. * WavPack demuxer
  3. * Copyright (c) 2006,2011 Konstantin Shishkov
  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 "libavutil/channel_layout.h"
  22. #include "libavutil/intreadwrite.h"
  23. #include "libavutil/dict.h"
  24. #include "avformat.h"
  25. #include "internal.h"
  26. #include "apetag.h"
  27. #include "id3v1.h"
  28. #include "wv.h"
  29. enum WV_FLAGS {
  30. WV_MONO = 0x0004,
  31. WV_HYBRID = 0x0008,
  32. WV_JOINT = 0x0010,
  33. WV_CROSSD = 0x0020,
  34. WV_HSHAPE = 0x0040,
  35. WV_FLOAT = 0x0080,
  36. WV_INT32 = 0x0100,
  37. WV_HBR = 0x0200,
  38. WV_HBAL = 0x0400,
  39. WV_MCINIT = 0x0800,
  40. WV_MCEND = 0x1000,
  41. WV_DSD = 0x80000000,
  42. };
  43. static const int wv_rates[16] = {
  44. 6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000,
  45. 32000, 44100, 48000, 64000, 88200, 96000, 192000, -1
  46. };
  47. typedef struct WVContext {
  48. uint8_t block_header[WV_HEADER_SIZE];
  49. WvHeader header;
  50. int rate, chan, bpp;
  51. uint32_t chmask;
  52. int multichannel;
  53. int block_parsed;
  54. int64_t pos;
  55. int64_t apetag_start;
  56. } WVContext;
  57. static int wv_probe(const AVProbeData *p)
  58. {
  59. /* check file header */
  60. if (p->buf_size <= 32)
  61. return 0;
  62. if (AV_RL32(&p->buf[0]) == MKTAG('w', 'v', 'p', 'k') &&
  63. AV_RL32(&p->buf[4]) >= 24 &&
  64. AV_RL32(&p->buf[4]) <= WV_BLOCK_LIMIT &&
  65. AV_RL16(&p->buf[8]) >= 0x402 &&
  66. AV_RL16(&p->buf[8]) <= 0x410)
  67. return AVPROBE_SCORE_MAX;
  68. else
  69. return 0;
  70. }
  71. static int wv_read_block_header(AVFormatContext *ctx, AVIOContext *pb)
  72. {
  73. WVContext *wc = ctx->priv_data;
  74. int ret;
  75. int rate, bpp, chan;
  76. uint32_t chmask, flags;
  77. wc->pos = avio_tell(pb);
  78. /* don't return bogus packets with the ape tag data */
  79. if (wc->apetag_start && wc->pos >= wc->apetag_start)
  80. return AVERROR_EOF;
  81. ret = avio_read(pb, wc->block_header, WV_HEADER_SIZE);
  82. if (ret != WV_HEADER_SIZE)
  83. return (ret < 0) ? ret : AVERROR_EOF;
  84. ret = ff_wv_parse_header(&wc->header, wc->block_header);
  85. if (ret < 0) {
  86. av_log(ctx, AV_LOG_ERROR, "Invalid block header.\n");
  87. return ret;
  88. }
  89. if (wc->header.flags & WV_DSD) {
  90. avpriv_report_missing_feature(ctx, "WV DSD");
  91. return AVERROR_PATCHWELCOME;
  92. }
  93. if (wc->header.version < 0x402 || wc->header.version > 0x410) {
  94. avpriv_report_missing_feature(ctx, "WV version 0x%03X",
  95. wc->header.version);
  96. return AVERROR_PATCHWELCOME;
  97. }
  98. /* Blocks with zero samples don't contain actual audio information
  99. * and should be ignored */
  100. if (!wc->header.samples)
  101. return 0;
  102. // parse flags
  103. flags = wc->header.flags;
  104. bpp = ((flags & 3) + 1) << 3;
  105. chan = 1 + !(flags & WV_MONO);
  106. chmask = flags & WV_MONO ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
  107. rate = wv_rates[(flags >> 23) & 0xF];
  108. wc->multichannel = !(wc->header.initial && wc->header.final);
  109. if (wc->multichannel) {
  110. chan = wc->chan;
  111. chmask = wc->chmask;
  112. }
  113. if ((rate == -1 || !chan) && !wc->block_parsed) {
  114. int64_t block_end = avio_tell(pb) + wc->header.blocksize;
  115. if (!(pb->seekable & AVIO_SEEKABLE_NORMAL)) {
  116. av_log(ctx, AV_LOG_ERROR,
  117. "Cannot determine additional parameters\n");
  118. return AVERROR_INVALIDDATA;
  119. }
  120. while (avio_tell(pb) < block_end && !avio_feof(pb)) {
  121. int id, size;
  122. id = avio_r8(pb);
  123. size = (id & 0x80) ? avio_rl24(pb) : avio_r8(pb);
  124. size <<= 1;
  125. if (id & 0x40)
  126. size--;
  127. switch (id & 0x3F) {
  128. case 0xD:
  129. if (size <= 1) {
  130. av_log(ctx, AV_LOG_ERROR,
  131. "Insufficient channel information\n");
  132. return AVERROR_INVALIDDATA;
  133. }
  134. chan = avio_r8(pb);
  135. switch (size - 2) {
  136. case 0:
  137. chmask = avio_r8(pb);
  138. break;
  139. case 1:
  140. chmask = avio_rl16(pb);
  141. break;
  142. case 2:
  143. chmask = avio_rl24(pb);
  144. break;
  145. case 3:
  146. chmask = avio_rl32(pb);
  147. break;
  148. case 4:
  149. avio_skip(pb, 1);
  150. chan |= (avio_r8(pb) & 0xF) << 8;
  151. chan += 1;
  152. chmask = avio_rl24(pb);
  153. break;
  154. case 5:
  155. avio_skip(pb, 1);
  156. chan |= (avio_r8(pb) & 0xF) << 8;
  157. chan += 1;
  158. chmask = avio_rl32(pb);
  159. break;
  160. default:
  161. av_log(ctx, AV_LOG_ERROR,
  162. "Invalid channel info size %d\n", size);
  163. return AVERROR_INVALIDDATA;
  164. }
  165. break;
  166. case 0x27:
  167. rate = avio_rl24(pb);
  168. break;
  169. default:
  170. avio_skip(pb, size);
  171. }
  172. if (id & 0x40)
  173. avio_skip(pb, 1);
  174. }
  175. if (rate == -1) {
  176. av_log(ctx, AV_LOG_ERROR,
  177. "Cannot determine custom sampling rate\n");
  178. return AVERROR_INVALIDDATA;
  179. }
  180. avio_seek(pb, block_end - wc->header.blocksize, SEEK_SET);
  181. }
  182. if (!wc->bpp)
  183. wc->bpp = bpp;
  184. if (!wc->chan)
  185. wc->chan = chan;
  186. if (!wc->chmask)
  187. wc->chmask = chmask;
  188. if (!wc->rate)
  189. wc->rate = rate;
  190. if (flags && bpp != wc->bpp) {
  191. av_log(ctx, AV_LOG_ERROR,
  192. "Bits per sample differ, this block: %i, header block: %i\n",
  193. bpp, wc->bpp);
  194. return AVERROR_INVALIDDATA;
  195. }
  196. if (flags && !wc->multichannel && chan != wc->chan) {
  197. av_log(ctx, AV_LOG_ERROR,
  198. "Channels differ, this block: %i, header block: %i\n",
  199. chan, wc->chan);
  200. return AVERROR_INVALIDDATA;
  201. }
  202. if (flags && rate != -1 && rate != wc->rate) {
  203. av_log(ctx, AV_LOG_ERROR,
  204. "Sampling rate differ, this block: %i, header block: %i\n",
  205. rate, wc->rate);
  206. return AVERROR_INVALIDDATA;
  207. }
  208. return 0;
  209. }
  210. static int wv_read_header(AVFormatContext *s)
  211. {
  212. AVIOContext *pb = s->pb;
  213. WVContext *wc = s->priv_data;
  214. AVStream *st;
  215. int ret;
  216. wc->block_parsed = 0;
  217. for (;;) {
  218. if ((ret = wv_read_block_header(s, pb)) < 0)
  219. return ret;
  220. if (!wc->header.samples)
  221. avio_skip(pb, wc->header.blocksize);
  222. else
  223. break;
  224. }
  225. /* now we are ready: build format streams */
  226. st = avformat_new_stream(s, NULL);
  227. if (!st)
  228. return AVERROR(ENOMEM);
  229. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  230. st->codecpar->codec_id = AV_CODEC_ID_WAVPACK;
  231. st->codecpar->channels = wc->chan;
  232. st->codecpar->channel_layout = wc->chmask;
  233. st->codecpar->sample_rate = wc->rate;
  234. st->codecpar->bits_per_coded_sample = wc->bpp;
  235. avpriv_set_pts_info(st, 64, 1, wc->rate);
  236. st->start_time = 0;
  237. if (wc->header.total_samples != 0xFFFFFFFFu)
  238. st->duration = wc->header.total_samples;
  239. if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
  240. int64_t cur = avio_tell(s->pb);
  241. wc->apetag_start = ff_ape_parse_tag(s);
  242. if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
  243. ff_id3v1_read(s);
  244. avio_seek(s->pb, cur, SEEK_SET);
  245. }
  246. return 0;
  247. }
  248. static int wv_read_packet(AVFormatContext *s, AVPacket *pkt)
  249. {
  250. WVContext *wc = s->priv_data;
  251. int ret;
  252. int off;
  253. int64_t pos;
  254. uint32_t block_samples;
  255. if (avio_feof(s->pb))
  256. return AVERROR_EOF;
  257. if (wc->block_parsed) {
  258. if ((ret = wv_read_block_header(s, s->pb)) < 0)
  259. return ret;
  260. }
  261. pos = wc->pos;
  262. if ((ret = av_new_packet(pkt, wc->header.blocksize + WV_HEADER_SIZE)) < 0)
  263. return ret;
  264. memcpy(pkt->data, wc->block_header, WV_HEADER_SIZE);
  265. ret = avio_read(s->pb, pkt->data + WV_HEADER_SIZE, wc->header.blocksize);
  266. if (ret != wc->header.blocksize) {
  267. return AVERROR(EIO);
  268. }
  269. while (!(wc->header.flags & WV_FLAG_FINAL_BLOCK)) {
  270. if ((ret = wv_read_block_header(s, s->pb)) < 0) {
  271. return ret;
  272. }
  273. off = pkt->size;
  274. if ((ret = av_grow_packet(pkt, WV_HEADER_SIZE + wc->header.blocksize)) < 0) {
  275. return ret;
  276. }
  277. memcpy(pkt->data + off, wc->block_header, WV_HEADER_SIZE);
  278. ret = avio_read(s->pb, pkt->data + off + WV_HEADER_SIZE, wc->header.blocksize);
  279. if (ret != wc->header.blocksize) {
  280. return (ret < 0) ? ret : AVERROR_EOF;
  281. }
  282. }
  283. pkt->stream_index = 0;
  284. pkt->pos = pos;
  285. wc->block_parsed = 1;
  286. pkt->pts = wc->header.block_idx;
  287. block_samples = wc->header.samples;
  288. if (block_samples > INT32_MAX)
  289. av_log(s, AV_LOG_WARNING,
  290. "Too many samples in block: %"PRIu32"\n", block_samples);
  291. else
  292. pkt->duration = block_samples;
  293. return 0;
  294. }
  295. AVInputFormat ff_wv_demuxer = {
  296. .name = "wv",
  297. .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
  298. .priv_data_size = sizeof(WVContext),
  299. .read_probe = wv_probe,
  300. .read_header = wv_read_header,
  301. .read_packet = wv_read_packet,
  302. .flags = AVFMT_GENERIC_INDEX,
  303. };