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.

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