wv.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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/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. // blocks with zero samples don't contain actual audio information and should be ignored
  104. if (!AV_RN32(wc->extra))
  105. return 0;
  106. //parse flags
  107. bpp = ((wc->flags & 3) + 1) << 3;
  108. chan = 1 + !(wc->flags & WV_MONO);
  109. chmask = wc->flags & WV_MONO ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
  110. rate = wv_rates[(wc->flags >> 23) & 0xF];
  111. wc->multichannel = !!((wc->flags & WV_SINGLE_BLOCK) != WV_SINGLE_BLOCK);
  112. if(wc->multichannel){
  113. chan = wc->chan;
  114. chmask = wc->chmask;
  115. }
  116. if((rate == -1 || !chan) && !wc->block_parsed){
  117. int64_t block_end = avio_tell(pb) + wc->blksize - 24;
  118. if(!pb->seekable){
  119. av_log(ctx, AV_LOG_ERROR, "Cannot determine additional parameters\n");
  120. return -1;
  121. }
  122. while(avio_tell(pb) < block_end){
  123. int id, size;
  124. id = avio_r8(pb);
  125. size = (id & 0x80) ? avio_rl24(pb) : avio_r8(pb);
  126. size <<= 1;
  127. if(id&0x40)
  128. size--;
  129. switch(id&0x3F){
  130. case 0xD:
  131. if(size <= 1){
  132. av_log(ctx, AV_LOG_ERROR, "Insufficient channel information\n");
  133. return -1;
  134. }
  135. chan = avio_r8(pb);
  136. switch(size - 2){
  137. case 0:
  138. chmask = avio_r8(pb);
  139. break;
  140. case 1:
  141. chmask = avio_rl16(pb);
  142. break;
  143. case 2:
  144. chmask = avio_rl24(pb);
  145. break;
  146. case 3:
  147. chmask = avio_rl32(pb);
  148. break;
  149. case 5:
  150. avio_skip(pb, 1);
  151. chan |= (avio_r8(pb) & 0xF) << 8;
  152. chmask = avio_rl24(pb);
  153. break;
  154. default:
  155. av_log(ctx, AV_LOG_ERROR, "Invalid channel info size %d\n", size);
  156. return -1;
  157. }
  158. break;
  159. case 0x27:
  160. rate = avio_rl24(pb);
  161. break;
  162. default:
  163. avio_skip(pb, size);
  164. }
  165. if(id&0x40)
  166. avio_skip(pb, 1);
  167. }
  168. if(rate == -1){
  169. av_log(ctx, AV_LOG_ERROR, "Cannot determine custom sampling rate\n");
  170. return -1;
  171. }
  172. avio_seek(pb, block_end - wc->blksize + 24, SEEK_SET);
  173. }
  174. if(!wc->bpp) wc->bpp = bpp;
  175. if(!wc->chan) wc->chan = chan;
  176. if(!wc->chmask) wc->chmask = chmask;
  177. if(!wc->rate) wc->rate = rate;
  178. if(wc->flags && bpp != wc->bpp){
  179. av_log(ctx, AV_LOG_ERROR, "Bits per sample differ, this block: %i, header block: %i\n", bpp, wc->bpp);
  180. return -1;
  181. }
  182. if(wc->flags && !wc->multichannel && chan != wc->chan){
  183. av_log(ctx, AV_LOG_ERROR, "Channels differ, this block: %i, header block: %i\n", chan, wc->chan);
  184. return -1;
  185. }
  186. if(wc->flags && rate != -1 && rate != wc->rate){
  187. av_log(ctx, AV_LOG_ERROR, "Sampling rate differ, this block: %i, header block: %i\n", rate, wc->rate);
  188. return -1;
  189. }
  190. wc->blksize = size - 24;
  191. return 0;
  192. }
  193. static int wv_read_header(AVFormatContext *s,
  194. AVFormatParameters *ap)
  195. {
  196. AVIOContext *pb = s->pb;
  197. WVContext *wc = s->priv_data;
  198. AVStream *st;
  199. wc->block_parsed = 0;
  200. for(;;){
  201. if(wv_read_block_header(s, pb, 0) < 0)
  202. return -1;
  203. if(!AV_RN32(wc->extra))
  204. avio_skip(pb, wc->blksize - 24);
  205. else
  206. break;
  207. }
  208. /* now we are ready: build format streams */
  209. st = av_new_stream(s, 0);
  210. if (!st)
  211. return -1;
  212. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  213. st->codec->codec_id = CODEC_ID_WAVPACK;
  214. st->codec->channels = wc->chan;
  215. st->codec->channel_layout = wc->chmask;
  216. st->codec->sample_rate = wc->rate;
  217. st->codec->bits_per_coded_sample = wc->bpp;
  218. av_set_pts_info(st, 64, 1, wc->rate);
  219. st->start_time = 0;
  220. st->duration = wc->samples;
  221. if(s->pb->seekable) {
  222. int64_t cur = avio_tell(s->pb);
  223. ff_ape_parse_tag(s);
  224. if(!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
  225. ff_id3v1_read(s);
  226. avio_seek(s->pb, cur, SEEK_SET);
  227. }
  228. return 0;
  229. }
  230. static int wv_read_packet(AVFormatContext *s,
  231. AVPacket *pkt)
  232. {
  233. WVContext *wc = s->priv_data;
  234. int ret;
  235. int size, ver, off;
  236. if (url_feof(s->pb))
  237. return AVERROR(EIO);
  238. if(wc->block_parsed){
  239. if(wv_read_block_header(s, s->pb, 0) < 0)
  240. return -1;
  241. }
  242. off = wc->multichannel ? 4 : 0;
  243. if(av_new_packet(pkt, wc->blksize + WV_EXTRA_SIZE + off) < 0)
  244. return AVERROR(ENOMEM);
  245. if(wc->multichannel)
  246. AV_WL32(pkt->data, wc->blksize + WV_EXTRA_SIZE + 12);
  247. memcpy(pkt->data + off, wc->extra, WV_EXTRA_SIZE);
  248. ret = avio_read(s->pb, pkt->data + WV_EXTRA_SIZE + off, wc->blksize);
  249. if(ret != wc->blksize){
  250. av_free_packet(pkt);
  251. return AVERROR(EIO);
  252. }
  253. while(!(wc->flags & WV_END_BLOCK)){
  254. if(avio_rl32(s->pb) != MKTAG('w', 'v', 'p', 'k')){
  255. av_free_packet(pkt);
  256. return -1;
  257. }
  258. if((ret = av_append_packet(s->pb, pkt, 4)) < 0){
  259. av_free_packet(pkt);
  260. return ret;
  261. }
  262. size = AV_RL32(pkt->data + pkt->size - 4);
  263. if(size < 24 || size > WV_BLOCK_LIMIT){
  264. av_free_packet(pkt);
  265. av_log(s, AV_LOG_ERROR, "Incorrect block size %d\n", size);
  266. return -1;
  267. }
  268. wc->blksize = size;
  269. ver = avio_rl16(s->pb);
  270. if(ver < 0x402 || ver > 0x410){
  271. av_free_packet(pkt);
  272. av_log(s, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
  273. return -1;
  274. }
  275. avio_r8(s->pb); // track no
  276. avio_r8(s->pb); // track sub index
  277. wc->samples = avio_rl32(s->pb); // total samples in file
  278. wc->soff = avio_rl32(s->pb); // offset in samples of current block
  279. if((ret = av_append_packet(s->pb, pkt, WV_EXTRA_SIZE)) < 0){
  280. av_free_packet(pkt);
  281. return ret;
  282. }
  283. memcpy(wc->extra, pkt->data + pkt->size - WV_EXTRA_SIZE, WV_EXTRA_SIZE);
  284. if(wv_read_block_header(s, s->pb, 1) < 0){
  285. av_free_packet(pkt);
  286. return -1;
  287. }
  288. ret = av_append_packet(s->pb, pkt, wc->blksize);
  289. if(ret < 0){
  290. av_free_packet(pkt);
  291. return ret;
  292. }
  293. }
  294. pkt->stream_index = 0;
  295. wc->block_parsed = 1;
  296. pkt->pts = wc->soff;
  297. av_add_index_entry(s->streams[0], wc->pos, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
  298. return 0;
  299. }
  300. static int wv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  301. {
  302. AVStream *st = s->streams[stream_index];
  303. WVContext *wc = s->priv_data;
  304. AVPacket pkt1, *pkt = &pkt1;
  305. int ret;
  306. int index = av_index_search_timestamp(st, timestamp, flags);
  307. int64_t pos, pts;
  308. /* if found, seek there */
  309. if (index >= 0){
  310. wc->block_parsed = 1;
  311. avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET);
  312. return 0;
  313. }
  314. /* if timestamp is out of bounds, return error */
  315. if(timestamp < 0 || timestamp >= s->duration)
  316. return -1;
  317. pos = avio_tell(s->pb);
  318. do{
  319. ret = av_read_frame(s, pkt);
  320. if (ret < 0){
  321. avio_seek(s->pb, pos, SEEK_SET);
  322. return -1;
  323. }
  324. pts = pkt->pts;
  325. av_free_packet(pkt);
  326. }while(pts < timestamp);
  327. return 0;
  328. }
  329. AVInputFormat ff_wv_demuxer = {
  330. "wv",
  331. NULL_IF_CONFIG_SMALL("WavPack"),
  332. sizeof(WVContext),
  333. wv_probe,
  334. wv_read_header,
  335. wv_read_packet,
  336. NULL,
  337. wv_read_seek,
  338. };