wv.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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/audioconvert.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. // specs say that maximum block size is 1Mb
  29. #define WV_BLOCK_LIMIT 1047576
  30. #define WV_EXTRA_SIZE 12
  31. #define WV_START_BLOCK 0x0800
  32. #define WV_END_BLOCK 0x1000
  33. #define WV_SINGLE_BLOCK (WV_START_BLOCK | WV_END_BLOCK)
  34. enum WV_FLAGS {
  35. WV_MONO = 0x0004,
  36. WV_HYBRID = 0x0008,
  37. WV_JOINT = 0x0010,
  38. WV_CROSSD = 0x0020,
  39. WV_HSHAPE = 0x0040,
  40. WV_FLOAT = 0x0080,
  41. WV_INT32 = 0x0100,
  42. WV_HBR = 0x0200,
  43. WV_HBAL = 0x0400,
  44. WV_MCINIT = 0x0800,
  45. WV_MCEND = 0x1000,
  46. };
  47. static const int wv_rates[16] = {
  48. 6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000,
  49. 32000, 44100, 48000, 64000, 88200, 96000, 192000, -1
  50. };
  51. typedef struct {
  52. uint32_t blksize, flags;
  53. int rate, chan, bpp;
  54. uint32_t chmask;
  55. uint32_t samples, soff;
  56. int multichannel;
  57. int block_parsed;
  58. uint8_t extra[WV_EXTRA_SIZE];
  59. int64_t pos;
  60. int64_t apetag_start;
  61. } WVContext;
  62. static int wv_probe(AVProbeData *p)
  63. {
  64. /* check file header */
  65. if (p->buf_size <= 32)
  66. return 0;
  67. if (p->buf[0] == 'w' && p->buf[1] == 'v' &&
  68. p->buf[2] == 'p' && p->buf[3] == 'k')
  69. return AVPROBE_SCORE_MAX;
  70. else
  71. return 0;
  72. }
  73. static int wv_read_block_header(AVFormatContext *ctx, AVIOContext *pb,
  74. int append)
  75. {
  76. WVContext *wc = ctx->priv_data;
  77. uint32_t tag, ver;
  78. int size;
  79. int rate, bpp, chan;
  80. uint32_t chmask;
  81. wc->pos = avio_tell(pb);
  82. /* don't return bogus packets with the ape tag data */
  83. if (wc->apetag_start && wc->pos >= wc->apetag_start)
  84. return AVERROR_EOF;
  85. if (!append) {
  86. tag = avio_rl32(pb);
  87. if (tag != MKTAG('w', 'v', 'p', 'k'))
  88. return AVERROR_INVALIDDATA;
  89. size = avio_rl32(pb);
  90. if (size < 24 || size > WV_BLOCK_LIMIT) {
  91. av_log(ctx, AV_LOG_ERROR, "Incorrect block size %i\n", size);
  92. return AVERROR_INVALIDDATA;
  93. }
  94. wc->blksize = size;
  95. ver = avio_rl16(pb);
  96. if (ver < 0x402 || ver > 0x410) {
  97. av_log(ctx, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
  98. return AVERROR_PATCHWELCOME;
  99. }
  100. avio_r8(pb); // track no
  101. avio_r8(pb); // track sub index
  102. wc->samples = avio_rl32(pb); // total samples in file
  103. wc->soff = avio_rl32(pb); // offset in samples of current block
  104. avio_read(pb, wc->extra, WV_EXTRA_SIZE);
  105. } else {
  106. size = wc->blksize;
  107. }
  108. wc->flags = AV_RL32(wc->extra + 4);
  109. /* Blocks with zero samples don't contain actual audio information
  110. * and should be ignored */
  111. if (!AV_RN32(wc->extra))
  112. return 0;
  113. // parse flags
  114. bpp = ((wc->flags & 3) + 1) << 3;
  115. chan = 1 + !(wc->flags & WV_MONO);
  116. chmask = wc->flags & WV_MONO ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
  117. rate = wv_rates[(wc->flags >> 23) & 0xF];
  118. wc->multichannel = !!((wc->flags & WV_SINGLE_BLOCK) != WV_SINGLE_BLOCK);
  119. if (wc->multichannel) {
  120. chan = wc->chan;
  121. chmask = wc->chmask;
  122. }
  123. if ((rate == -1 || !chan) && !wc->block_parsed) {
  124. int64_t block_end = avio_tell(pb) + wc->blksize - 24;
  125. if (!pb->seekable) {
  126. av_log(ctx, AV_LOG_ERROR,
  127. "Cannot determine additional parameters\n");
  128. return AVERROR_INVALIDDATA;
  129. }
  130. while (avio_tell(pb) < block_end) {
  131. int id, size;
  132. id = avio_r8(pb);
  133. size = (id & 0x80) ? avio_rl24(pb) : avio_r8(pb);
  134. size <<= 1;
  135. if (id & 0x40)
  136. size--;
  137. switch (id & 0x3F) {
  138. case 0xD:
  139. if (size <= 1) {
  140. av_log(ctx, AV_LOG_ERROR,
  141. "Insufficient channel information\n");
  142. return AVERROR_INVALIDDATA;
  143. }
  144. chan = avio_r8(pb);
  145. switch (size - 2) {
  146. case 0:
  147. chmask = avio_r8(pb);
  148. break;
  149. case 1:
  150. chmask = avio_rl16(pb);
  151. break;
  152. case 2:
  153. chmask = avio_rl24(pb);
  154. break;
  155. case 3:
  156. chmask = avio_rl32(pb);
  157. break;
  158. case 5:
  159. avio_skip(pb, 1);
  160. chan |= (avio_r8(pb) & 0xF) << 8;
  161. chmask = avio_rl24(pb);
  162. break;
  163. default:
  164. av_log(ctx, AV_LOG_ERROR,
  165. "Invalid channel info size %d\n", size);
  166. return AVERROR_INVALIDDATA;
  167. }
  168. break;
  169. case 0x27:
  170. rate = avio_rl24(pb);
  171. break;
  172. default:
  173. avio_skip(pb, size);
  174. }
  175. if (id & 0x40)
  176. avio_skip(pb, 1);
  177. }
  178. if (rate == -1) {
  179. av_log(ctx, AV_LOG_ERROR,
  180. "Cannot determine custom sampling rate\n");
  181. return AVERROR_INVALIDDATA;
  182. }
  183. avio_seek(pb, block_end - wc->blksize + 24, SEEK_SET);
  184. }
  185. if (!wc->bpp)
  186. wc->bpp = bpp;
  187. if (!wc->chan)
  188. wc->chan = chan;
  189. if (!wc->chmask)
  190. wc->chmask = chmask;
  191. if (!wc->rate)
  192. wc->rate = rate;
  193. if (wc->flags && bpp != wc->bpp) {
  194. av_log(ctx, AV_LOG_ERROR,
  195. "Bits per sample differ, this block: %i, header block: %i\n",
  196. bpp, wc->bpp);
  197. return AVERROR_INVALIDDATA;
  198. }
  199. if (wc->flags && !wc->multichannel && chan != wc->chan) {
  200. av_log(ctx, AV_LOG_ERROR,
  201. "Channels differ, this block: %i, header block: %i\n",
  202. chan, wc->chan);
  203. return AVERROR_INVALIDDATA;
  204. }
  205. if (wc->flags && rate != -1 && rate != wc->rate) {
  206. av_log(ctx, AV_LOG_ERROR,
  207. "Sampling rate differ, this block: %i, header block: %i\n",
  208. rate, wc->rate);
  209. return AVERROR_INVALIDDATA;
  210. }
  211. wc->blksize = size - 24;
  212. return 0;
  213. }
  214. static int wv_read_header(AVFormatContext *s)
  215. {
  216. AVIOContext *pb = s->pb;
  217. WVContext *wc = s->priv_data;
  218. AVStream *st;
  219. int ret;
  220. wc->block_parsed = 0;
  221. for (;;) {
  222. if ((ret = wv_read_block_header(s, pb, 0)) < 0)
  223. return ret;
  224. if (!AV_RN32(wc->extra))
  225. avio_skip(pb, wc->blksize - 24);
  226. else
  227. break;
  228. }
  229. /* now we are ready: build format streams */
  230. st = avformat_new_stream(s, NULL);
  231. if (!st)
  232. return AVERROR(ENOMEM);
  233. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  234. st->codec->codec_id = AV_CODEC_ID_WAVPACK;
  235. st->codec->channels = wc->chan;
  236. st->codec->channel_layout = wc->chmask;
  237. st->codec->sample_rate = wc->rate;
  238. st->codec->bits_per_coded_sample = wc->bpp;
  239. avpriv_set_pts_info(st, 64, 1, wc->rate);
  240. st->start_time = 0;
  241. st->duration = wc->samples;
  242. if (s->pb->seekable) {
  243. int64_t cur = avio_tell(s->pb);
  244. wc->apetag_start = ff_ape_parse_tag(s);
  245. if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
  246. ff_id3v1_read(s);
  247. avio_seek(s->pb, cur, SEEK_SET);
  248. }
  249. return 0;
  250. }
  251. static int wv_read_packet(AVFormatContext *s, AVPacket *pkt)
  252. {
  253. WVContext *wc = s->priv_data;
  254. int ret;
  255. int size, ver, off;
  256. int64_t pos;
  257. uint32_t block_samples;
  258. if (url_feof(s->pb))
  259. return AVERROR_EOF;
  260. if (wc->block_parsed) {
  261. if ((ret = wv_read_block_header(s, s->pb, 0)) < 0)
  262. return ret;
  263. }
  264. pos = wc->pos;
  265. off = wc->multichannel ? 4 : 0;
  266. if (av_new_packet(pkt, wc->blksize + WV_EXTRA_SIZE + off) < 0)
  267. return AVERROR(ENOMEM);
  268. if (wc->multichannel)
  269. AV_WL32(pkt->data, wc->blksize + WV_EXTRA_SIZE + 12);
  270. memcpy(pkt->data + off, wc->extra, WV_EXTRA_SIZE);
  271. ret = avio_read(s->pb, pkt->data + WV_EXTRA_SIZE + off, wc->blksize);
  272. if (ret != wc->blksize) {
  273. av_free_packet(pkt);
  274. return AVERROR(EIO);
  275. }
  276. while (!(wc->flags & WV_END_BLOCK)) {
  277. if (avio_rl32(s->pb) != MKTAG('w', 'v', 'p', 'k')) {
  278. av_free_packet(pkt);
  279. return AVERROR_INVALIDDATA;
  280. }
  281. if ((ret = av_append_packet(s->pb, pkt, 4)) < 0) {
  282. av_free_packet(pkt);
  283. return ret;
  284. }
  285. size = AV_RL32(pkt->data + pkt->size - 4);
  286. if (size < 24 || size > WV_BLOCK_LIMIT) {
  287. av_free_packet(pkt);
  288. av_log(s, AV_LOG_ERROR, "Incorrect block size %d\n", size);
  289. return AVERROR_INVALIDDATA;
  290. }
  291. wc->blksize = size;
  292. ver = avio_rl16(s->pb);
  293. if (ver < 0x402 || ver > 0x410) {
  294. av_free_packet(pkt);
  295. av_log(s, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
  296. return AVERROR_PATCHWELCOME;
  297. }
  298. avio_r8(s->pb); // track no
  299. avio_r8(s->pb); // track sub index
  300. wc->samples = avio_rl32(s->pb); // total samples in file
  301. wc->soff = avio_rl32(s->pb); // offset in samples of current block
  302. if ((ret = av_append_packet(s->pb, pkt, WV_EXTRA_SIZE)) < 0) {
  303. av_free_packet(pkt);
  304. return ret;
  305. }
  306. memcpy(wc->extra, pkt->data + pkt->size - WV_EXTRA_SIZE, WV_EXTRA_SIZE);
  307. if ((ret = wv_read_block_header(s, s->pb, 1)) < 0) {
  308. av_free_packet(pkt);
  309. return ret;
  310. }
  311. ret = av_append_packet(s->pb, pkt, wc->blksize);
  312. if (ret < 0) {
  313. av_free_packet(pkt);
  314. return ret;
  315. }
  316. }
  317. pkt->stream_index = 0;
  318. wc->block_parsed = 1;
  319. pkt->pts = wc->soff;
  320. block_samples = AV_RN32(wc->extra);
  321. if (block_samples > INT32_MAX)
  322. av_log(s, AV_LOG_WARNING,
  323. "Too many samples in block: %"PRIu32"\n", block_samples);
  324. else
  325. pkt->duration = block_samples;
  326. av_add_index_entry(s->streams[0], pos, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
  327. return 0;
  328. }
  329. static int wv_read_seek(AVFormatContext *s, int stream_index,
  330. int64_t timestamp, int flags)
  331. {
  332. AVStream *st = s->streams[stream_index];
  333. WVContext *wc = s->priv_data;
  334. AVPacket pkt1, *pkt = &pkt1;
  335. int ret;
  336. int index = av_index_search_timestamp(st, timestamp, flags);
  337. int64_t pos, pts;
  338. /* if found, seek there */
  339. if (index >= 0 &&
  340. timestamp <= st->index_entries[st->nb_index_entries - 1].timestamp) {
  341. wc->block_parsed = 1;
  342. avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET);
  343. return 0;
  344. }
  345. /* if timestamp is out of bounds, return error */
  346. if (timestamp < 0 || timestamp >= s->duration)
  347. return AVERROR(EINVAL);
  348. pos = avio_tell(s->pb);
  349. do {
  350. ret = av_read_frame(s, pkt);
  351. if (ret < 0) {
  352. avio_seek(s->pb, pos, SEEK_SET);
  353. return ret;
  354. }
  355. pts = pkt->pts;
  356. av_free_packet(pkt);
  357. } while(pts < timestamp);
  358. return 0;
  359. }
  360. AVInputFormat ff_wv_demuxer = {
  361. .name = "wv",
  362. .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
  363. .priv_data_size = sizeof(WVContext),
  364. .read_probe = wv_probe,
  365. .read_header = wv_read_header,
  366. .read_packet = wv_read_packet,
  367. .read_seek = wv_read_seek,
  368. };