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.

390 lines
12KB

  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. } WVContext;
  61. static int wv_probe(AVProbeData *p)
  62. {
  63. /* check file header */
  64. if (p->buf_size <= 32)
  65. return 0;
  66. if (p->buf[0] == 'w' && p->buf[1] == 'v' &&
  67. p->buf[2] == 'p' && p->buf[3] == 'k')
  68. return AVPROBE_SCORE_MAX;
  69. else
  70. return 0;
  71. }
  72. static int wv_read_block_header(AVFormatContext *ctx, AVIOContext *pb,
  73. int append)
  74. {
  75. WVContext *wc = ctx->priv_data;
  76. uint32_t tag, ver;
  77. int size;
  78. int rate, bpp, chan;
  79. uint32_t chmask;
  80. wc->pos = avio_tell(pb);
  81. if (!append) {
  82. tag = avio_rl32(pb);
  83. if (tag != MKTAG('w', 'v', 'p', 'k'))
  84. return AVERROR_INVALIDDATA;
  85. size = avio_rl32(pb);
  86. if (size < 24 || size > WV_BLOCK_LIMIT) {
  87. av_log(ctx, AV_LOG_ERROR, "Incorrect block size %i\n", size);
  88. return AVERROR_INVALIDDATA;
  89. }
  90. wc->blksize = size;
  91. ver = avio_rl16(pb);
  92. if (ver < 0x402 || ver > 0x410) {
  93. av_log(ctx, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
  94. return AVERROR_PATCHWELCOME;
  95. }
  96. avio_r8(pb); // track no
  97. avio_r8(pb); // track sub index
  98. wc->samples = avio_rl32(pb); // total samples in file
  99. wc->soff = avio_rl32(pb); // offset in samples of current block
  100. avio_read(pb, wc->extra, WV_EXTRA_SIZE);
  101. } else {
  102. size = wc->blksize;
  103. }
  104. wc->flags = AV_RL32(wc->extra + 4);
  105. /* Blocks with zero samples don't contain actual audio information
  106. * and should be ignored */
  107. if (!AV_RN32(wc->extra))
  108. return 0;
  109. // parse flags
  110. bpp = ((wc->flags & 3) + 1) << 3;
  111. chan = 1 + !(wc->flags & WV_MONO);
  112. chmask = wc->flags & WV_MONO ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
  113. rate = wv_rates[(wc->flags >> 23) & 0xF];
  114. wc->multichannel = !!((wc->flags & WV_SINGLE_BLOCK) != WV_SINGLE_BLOCK);
  115. if (wc->multichannel) {
  116. chan = wc->chan;
  117. chmask = wc->chmask;
  118. }
  119. if ((rate == -1 || !chan) && !wc->block_parsed) {
  120. int64_t block_end = avio_tell(pb) + wc->blksize - 24;
  121. if (!pb->seekable) {
  122. av_log(ctx, AV_LOG_ERROR,
  123. "Cannot determine additional parameters\n");
  124. return AVERROR_INVALIDDATA;
  125. }
  126. while (avio_tell(pb) < block_end) {
  127. int id, size;
  128. id = avio_r8(pb);
  129. size = (id & 0x80) ? avio_rl24(pb) : avio_r8(pb);
  130. size <<= 1;
  131. if (id & 0x40)
  132. size--;
  133. switch (id & 0x3F) {
  134. case 0xD:
  135. if (size <= 1) {
  136. av_log(ctx, AV_LOG_ERROR,
  137. "Insufficient channel information\n");
  138. return AVERROR_INVALIDDATA;
  139. }
  140. chan = avio_r8(pb);
  141. switch (size - 2) {
  142. case 0:
  143. chmask = avio_r8(pb);
  144. break;
  145. case 1:
  146. chmask = avio_rl16(pb);
  147. break;
  148. case 2:
  149. chmask = avio_rl24(pb);
  150. break;
  151. case 3:
  152. chmask = avio_rl32(pb);
  153. break;
  154. case 5:
  155. avio_skip(pb, 1);
  156. chan |= (avio_r8(pb) & 0xF) << 8;
  157. chmask = avio_rl24(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 0x27:
  166. rate = avio_rl24(pb);
  167. break;
  168. default:
  169. avio_skip(pb, size);
  170. }
  171. if (id & 0x40)
  172. avio_skip(pb, 1);
  173. }
  174. if (rate == -1) {
  175. av_log(ctx, AV_LOG_ERROR,
  176. "Cannot determine custom sampling rate\n");
  177. return AVERROR_INVALIDDATA;
  178. }
  179. avio_seek(pb, block_end - wc->blksize + 24, SEEK_SET);
  180. }
  181. if (!wc->bpp)
  182. wc->bpp = bpp;
  183. if (!wc->chan)
  184. wc->chan = chan;
  185. if (!wc->chmask)
  186. wc->chmask = chmask;
  187. if (!wc->rate)
  188. wc->rate = rate;
  189. if (wc->flags && bpp != wc->bpp) {
  190. av_log(ctx, AV_LOG_ERROR,
  191. "Bits per sample differ, this block: %i, header block: %i\n",
  192. bpp, wc->bpp);
  193. return AVERROR_INVALIDDATA;
  194. }
  195. if (wc->flags && !wc->multichannel && chan != wc->chan) {
  196. av_log(ctx, AV_LOG_ERROR,
  197. "Channels differ, this block: %i, header block: %i\n",
  198. chan, wc->chan);
  199. return AVERROR_INVALIDDATA;
  200. }
  201. if (wc->flags && rate != -1 && rate != wc->rate) {
  202. av_log(ctx, AV_LOG_ERROR,
  203. "Sampling rate differ, this block: %i, header block: %i\n",
  204. rate, wc->rate);
  205. return AVERROR_INVALIDDATA;
  206. }
  207. wc->blksize = size - 24;
  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)) < 0)
  219. return ret;
  220. if (!AV_RN32(wc->extra))
  221. avio_skip(pb, wc->blksize - 24);
  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->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  230. st->codec->codec_id = CODEC_ID_WAVPACK;
  231. st->codec->channels = wc->chan;
  232. st->codec->channel_layout = wc->chmask;
  233. st->codec->sample_rate = wc->rate;
  234. st->codec->bits_per_coded_sample = wc->bpp;
  235. avpriv_set_pts_info(st, 64, 1, wc->rate);
  236. st->start_time = 0;
  237. st->duration = wc->samples;
  238. if (s->pb->seekable) {
  239. int64_t cur = avio_tell(s->pb);
  240. ff_ape_parse_tag(s);
  241. if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
  242. ff_id3v1_read(s);
  243. avio_seek(s->pb, cur, SEEK_SET);
  244. }
  245. return 0;
  246. }
  247. static int wv_read_packet(AVFormatContext *s, AVPacket *pkt)
  248. {
  249. WVContext *wc = s->priv_data;
  250. int ret;
  251. int size, ver, off;
  252. int64_t pos;
  253. uint32_t block_samples;
  254. if (url_feof(s->pb))
  255. return AVERROR_EOF;
  256. if (wc->block_parsed) {
  257. if ((ret = wv_read_block_header(s, s->pb, 0)) < 0)
  258. return ret;
  259. }
  260. pos = wc->pos;
  261. off = wc->multichannel ? 4 : 0;
  262. if (av_new_packet(pkt, wc->blksize + WV_EXTRA_SIZE + off) < 0)
  263. return AVERROR(ENOMEM);
  264. if (wc->multichannel)
  265. AV_WL32(pkt->data, wc->blksize + WV_EXTRA_SIZE + 12);
  266. memcpy(pkt->data + off, wc->extra, WV_EXTRA_SIZE);
  267. ret = avio_read(s->pb, pkt->data + WV_EXTRA_SIZE + off, wc->blksize);
  268. if (ret != wc->blksize) {
  269. av_free_packet(pkt);
  270. return AVERROR(EIO);
  271. }
  272. while (!(wc->flags & WV_END_BLOCK)) {
  273. if (avio_rl32(s->pb) != MKTAG('w', 'v', 'p', 'k')) {
  274. av_free_packet(pkt);
  275. return AVERROR_INVALIDDATA;
  276. }
  277. if ((ret = av_append_packet(s->pb, pkt, 4)) < 0) {
  278. av_free_packet(pkt);
  279. return ret;
  280. }
  281. size = AV_RL32(pkt->data + pkt->size - 4);
  282. if (size < 24 || size > WV_BLOCK_LIMIT) {
  283. av_free_packet(pkt);
  284. av_log(s, AV_LOG_ERROR, "Incorrect block size %d\n", size);
  285. return AVERROR_INVALIDDATA;
  286. }
  287. wc->blksize = size;
  288. ver = avio_rl16(s->pb);
  289. if (ver < 0x402 || ver > 0x410) {
  290. av_free_packet(pkt);
  291. av_log(s, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
  292. return AVERROR_PATCHWELCOME;
  293. }
  294. avio_r8(s->pb); // track no
  295. avio_r8(s->pb); // track sub index
  296. wc->samples = avio_rl32(s->pb); // total samples in file
  297. wc->soff = avio_rl32(s->pb); // offset in samples of current block
  298. if ((ret = av_append_packet(s->pb, pkt, WV_EXTRA_SIZE)) < 0) {
  299. av_free_packet(pkt);
  300. return ret;
  301. }
  302. memcpy(wc->extra, pkt->data + pkt->size - WV_EXTRA_SIZE, WV_EXTRA_SIZE);
  303. if ((ret = wv_read_block_header(s, s->pb, 1)) < 0) {
  304. av_free_packet(pkt);
  305. return ret;
  306. }
  307. ret = av_append_packet(s->pb, pkt, wc->blksize);
  308. if (ret < 0) {
  309. av_free_packet(pkt);
  310. return ret;
  311. }
  312. }
  313. pkt->stream_index = 0;
  314. wc->block_parsed = 1;
  315. pkt->pts = wc->soff;
  316. block_samples = AV_RN32(wc->extra);
  317. if (block_samples > INT32_MAX)
  318. av_log(s, AV_LOG_WARNING,
  319. "Too many samples in block: %"PRIu32"\n", block_samples);
  320. else
  321. pkt->duration = block_samples;
  322. av_add_index_entry(s->streams[0], pos, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
  323. return 0;
  324. }
  325. static int wv_read_seek(AVFormatContext *s, int stream_index,
  326. int64_t timestamp, int flags)
  327. {
  328. AVStream *st = s->streams[stream_index];
  329. WVContext *wc = s->priv_data;
  330. AVPacket pkt1, *pkt = &pkt1;
  331. int ret;
  332. int index = av_index_search_timestamp(st, timestamp, flags);
  333. int64_t pos, pts;
  334. /* if found, seek there */
  335. if (index >= 0 &&
  336. timestamp <= st->index_entries[st->nb_index_entries - 1].timestamp) {
  337. wc->block_parsed = 1;
  338. avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET);
  339. return 0;
  340. }
  341. /* if timestamp is out of bounds, return error */
  342. if (timestamp < 0 || timestamp >= s->duration)
  343. return AVERROR(EINVAL);
  344. pos = avio_tell(s->pb);
  345. do {
  346. ret = av_read_frame(s, pkt);
  347. if (ret < 0) {
  348. avio_seek(s->pb, pos, SEEK_SET);
  349. return ret;
  350. }
  351. pts = pkt->pts;
  352. av_free_packet(pkt);
  353. } while(pts < timestamp);
  354. return 0;
  355. }
  356. AVInputFormat ff_wv_demuxer = {
  357. .name = "wv",
  358. .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
  359. .priv_data_size = sizeof(WVContext),
  360. .read_probe = wv_probe,
  361. .read_header = wv_read_header,
  362. .read_packet = wv_read_packet,
  363. .read_seek = wv_read_seek,
  364. };