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.

366 lines
11KB

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