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.

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