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.

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