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.

352 lines
11KB

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