wvdec.c 11 KB

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