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.

371 lines
11KB

  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_HEADER_SIZE 32
  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. uint8_t block_header[WV_HEADER_SIZE];
  53. uint32_t blksize, flags;
  54. int rate, chan, bpp;
  55. uint32_t chmask;
  56. uint32_t samples, soff;
  57. int multichannel;
  58. int block_parsed;
  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. {
  78. WVContext *wc = ctx->priv_data;
  79. uint32_t ver;
  80. int size, ret;
  81. int rate, bpp, chan;
  82. uint32_t chmask;
  83. wc->pos = avio_tell(pb);
  84. /* don't return bogus packets with the ape tag data */
  85. if (wc->apetag_start && wc->pos >= wc->apetag_start)
  86. return AVERROR_EOF;
  87. ret = avio_read(pb, wc->block_header, WV_HEADER_SIZE);
  88. if (ret != WV_HEADER_SIZE)
  89. return (ret < 0) ? ret : AVERROR_EOF;
  90. if (AV_RL32(wc->block_header) != MKTAG('w', 'v', 'p', 'k'))
  91. return AVERROR_INVALIDDATA;
  92. size = AV_RL32(wc->block_header + 4);
  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 = AV_RL32(wc->block_header + 8);
  99. if (ver < 0x402 || ver > 0x410) {
  100. av_log(ctx, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
  101. return AVERROR_PATCHWELCOME;
  102. }
  103. wc->samples = AV_RL32(wc->block_header + 12); // total samples in file
  104. wc->soff = AV_RL32(wc->block_header + 16); // offset in samples of current block
  105. wc->flags = AV_RL32(wc->block_header + 24);
  106. /* Blocks with zero samples don't contain actual audio information
  107. * and should be ignored */
  108. if (!AV_RN32(wc->block_header + 20))
  109. return 0;
  110. // parse flags
  111. bpp = ((wc->flags & 3) + 1) << 3;
  112. chan = 1 + !(wc->flags & WV_MONO);
  113. chmask = wc->flags & WV_MONO ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
  114. rate = wv_rates[(wc->flags >> 23) & 0xF];
  115. wc->multichannel = !!((wc->flags & WV_SINGLE_BLOCK) != WV_SINGLE_BLOCK);
  116. if (wc->multichannel) {
  117. chan = wc->chan;
  118. chmask = wc->chmask;
  119. }
  120. if ((rate == -1 || !chan) && !wc->block_parsed) {
  121. int64_t block_end = avio_tell(pb) + wc->blksize - 24;
  122. if (!pb->seekable) {
  123. av_log(ctx, AV_LOG_ERROR,
  124. "Cannot determine additional parameters\n");
  125. return AVERROR_INVALIDDATA;
  126. }
  127. while (avio_tell(pb) < block_end) {
  128. int id, size;
  129. id = avio_r8(pb);
  130. size = (id & 0x80) ? avio_rl24(pb) : avio_r8(pb);
  131. size <<= 1;
  132. if (id & 0x40)
  133. size--;
  134. switch (id & 0x3F) {
  135. case 0xD:
  136. if (size <= 1) {
  137. av_log(ctx, AV_LOG_ERROR,
  138. "Insufficient channel information\n");
  139. return AVERROR_INVALIDDATA;
  140. }
  141. chan = avio_r8(pb);
  142. switch (size - 2) {
  143. case 0:
  144. chmask = avio_r8(pb);
  145. break;
  146. case 1:
  147. chmask = avio_rl16(pb);
  148. break;
  149. case 2:
  150. chmask = avio_rl24(pb);
  151. break;
  152. case 3:
  153. chmask = avio_rl32(pb);
  154. break;
  155. case 5:
  156. avio_skip(pb, 1);
  157. chan |= (avio_r8(pb) & 0xF) << 8;
  158. chmask = avio_rl24(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->blksize + 24, 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 (wc->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 (wc->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 (wc->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. wc->blksize = size - 24;
  209. return 0;
  210. }
  211. static int wv_read_header(AVFormatContext *s)
  212. {
  213. AVIOContext *pb = s->pb;
  214. WVContext *wc = s->priv_data;
  215. AVStream *st;
  216. int ret;
  217. wc->block_parsed = 0;
  218. for (;;) {
  219. if ((ret = wv_read_block_header(s, pb)) < 0)
  220. return ret;
  221. if (!AV_RL32(wc->block_header + 20))
  222. avio_skip(pb, wc->blksize - 24);
  223. else
  224. break;
  225. }
  226. /* now we are ready: build format streams */
  227. st = avformat_new_stream(s, NULL);
  228. if (!st)
  229. return AVERROR(ENOMEM);
  230. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  231. st->codec->codec_id = AV_CODEC_ID_WAVPACK;
  232. st->codec->channels = wc->chan;
  233. st->codec->channel_layout = wc->chmask;
  234. st->codec->sample_rate = wc->rate;
  235. st->codec->bits_per_coded_sample = wc->bpp;
  236. avpriv_set_pts_info(st, 64, 1, wc->rate);
  237. st->start_time = 0;
  238. if (wc->samples != 0xFFFFFFFFu)
  239. st->duration = wc->samples;
  240. if (s->pb->seekable) {
  241. int64_t cur = avio_tell(s->pb);
  242. wc->apetag_start = ff_ape_parse_tag(s);
  243. if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
  244. ff_id3v1_read(s);
  245. avio_seek(s->pb, cur, SEEK_SET);
  246. }
  247. return 0;
  248. }
  249. static int wv_read_packet(AVFormatContext *s, AVPacket *pkt)
  250. {
  251. WVContext *wc = s->priv_data;
  252. int ret;
  253. int off;
  254. int64_t pos;
  255. uint32_t block_samples;
  256. if (url_feof(s->pb))
  257. return AVERROR_EOF;
  258. if (wc->block_parsed) {
  259. if ((ret = wv_read_block_header(s, s->pb)) < 0)
  260. return ret;
  261. }
  262. pos = wc->pos;
  263. if (av_new_packet(pkt, wc->blksize + WV_HEADER_SIZE) < 0)
  264. return AVERROR(ENOMEM);
  265. memcpy(pkt->data, wc->block_header, WV_HEADER_SIZE);
  266. ret = avio_read(s->pb, pkt->data + WV_HEADER_SIZE, wc->blksize);
  267. if (ret != wc->blksize) {
  268. av_free_packet(pkt);
  269. return AVERROR(EIO);
  270. }
  271. while (!(wc->flags & WV_END_BLOCK)) {
  272. if ((ret = wv_read_block_header(s, s->pb)) < 0) {
  273. av_free_packet(pkt);
  274. return ret;
  275. }
  276. off = pkt->size;
  277. if ((ret = av_grow_packet(pkt, WV_HEADER_SIZE + wc->blksize)) < 0) {
  278. av_free_packet(pkt);
  279. return ret;
  280. }
  281. memcpy(pkt->data + off, wc->block_header, WV_HEADER_SIZE);
  282. ret = avio_read(s->pb, pkt->data + off + WV_HEADER_SIZE, wc->blksize);
  283. if (ret != wc->blksize) {
  284. av_free_packet(pkt);
  285. return (ret < 0) ? ret : AVERROR_EOF;
  286. }
  287. }
  288. pkt->stream_index = 0;
  289. wc->block_parsed = 1;
  290. pkt->pts = wc->soff;
  291. block_samples = AV_RL32(wc->block_header + 20);
  292. if (block_samples > INT32_MAX)
  293. av_log(s, AV_LOG_WARNING,
  294. "Too many samples in block: %"PRIu32"\n", block_samples);
  295. else
  296. pkt->duration = block_samples;
  297. av_add_index_entry(s->streams[0], pos, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
  298. return 0;
  299. }
  300. static int wv_read_seek(AVFormatContext *s, int stream_index,
  301. int64_t timestamp, int flags)
  302. {
  303. AVStream *st = s->streams[stream_index];
  304. WVContext *wc = s->priv_data;
  305. AVPacket pkt1, *pkt = &pkt1;
  306. int ret;
  307. int index = av_index_search_timestamp(st, timestamp, flags);
  308. int64_t pos, pts;
  309. /* if found, seek there */
  310. if (index >= 0 &&
  311. timestamp <= st->index_entries[st->nb_index_entries - 1].timestamp) {
  312. wc->block_parsed = 1;
  313. avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET);
  314. return 0;
  315. }
  316. /* if timestamp is out of bounds, return error */
  317. if (timestamp < 0 || timestamp >= s->duration)
  318. return AVERROR(EINVAL);
  319. pos = avio_tell(s->pb);
  320. do {
  321. ret = av_read_frame(s, pkt);
  322. if (ret < 0) {
  323. avio_seek(s->pb, pos, SEEK_SET);
  324. return ret;
  325. }
  326. pts = pkt->pts;
  327. av_free_packet(pkt);
  328. } while(pts < timestamp);
  329. return 0;
  330. }
  331. AVInputFormat ff_wv_demuxer = {
  332. .name = "wv",
  333. .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
  334. .priv_data_size = sizeof(WVContext),
  335. .read_probe = wv_probe,
  336. .read_header = wv_read_header,
  337. .read_packet = wv_read_packet,
  338. .read_seek = wv_read_seek,
  339. };