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.

351 lines
10KB

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