wv.c 11 KB

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