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.

260 lines
7.3KB

  1. /*
  2. * WavPack demuxer
  3. * Copyright (c) 2006 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/intreadwrite.h"
  22. #include "avformat.h"
  23. #include "apetag.h"
  24. #include "id3v1.h"
  25. // specs say that maximum block size is 1Mb
  26. #define WV_BLOCK_LIMIT 1047576
  27. #define WV_EXTRA_SIZE 12
  28. enum WV_FLAGS{
  29. WV_MONO = 0x0004,
  30. WV_HYBRID = 0x0008,
  31. WV_JOINT = 0x0010,
  32. WV_CROSSD = 0x0020,
  33. WV_HSHAPE = 0x0040,
  34. WV_FLOAT = 0x0080,
  35. WV_INT32 = 0x0100,
  36. WV_HBR = 0x0200,
  37. WV_HBAL = 0x0400,
  38. WV_MCINIT = 0x0800,
  39. WV_MCEND = 0x1000,
  40. };
  41. static const int wv_rates[16] = {
  42. 6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000,
  43. 32000, 44100, 48000, 64000, 88200, 96000, 192000, -1
  44. };
  45. typedef struct{
  46. uint32_t blksize, flags;
  47. int rate, chan, bpp;
  48. uint32_t samples, soff;
  49. int block_parsed;
  50. uint8_t extra[WV_EXTRA_SIZE];
  51. int64_t pos;
  52. }WVContext;
  53. static int wv_probe(AVProbeData *p)
  54. {
  55. /* check file header */
  56. if (p->buf_size <= 32)
  57. return 0;
  58. if (p->buf[0] == 'w' && p->buf[1] == 'v' &&
  59. p->buf[2] == 'p' && p->buf[3] == 'k')
  60. return AVPROBE_SCORE_MAX;
  61. else
  62. return 0;
  63. }
  64. static int wv_read_block_header(AVFormatContext *ctx, ByteIOContext *pb)
  65. {
  66. WVContext *wc = ctx->priv_data;
  67. uint32_t tag, ver;
  68. int size;
  69. int rate, bpp, chan;
  70. wc->pos = url_ftell(pb);
  71. tag = get_le32(pb);
  72. if (tag != MKTAG('w', 'v', 'p', 'k'))
  73. return -1;
  74. size = get_le32(pb);
  75. if(size < 24 || size > WV_BLOCK_LIMIT){
  76. av_log(ctx, AV_LOG_ERROR, "Incorrect block size %i\n", size);
  77. return -1;
  78. }
  79. wc->blksize = size;
  80. ver = get_le16(pb);
  81. if(ver < 0x402 || ver > 0x410){
  82. av_log(ctx, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
  83. return -1;
  84. }
  85. get_byte(pb); // track no
  86. get_byte(pb); // track sub index
  87. wc->samples = get_le32(pb); // total samples in file
  88. wc->soff = get_le32(pb); // offset in samples of current block
  89. get_buffer(pb, wc->extra, WV_EXTRA_SIZE);
  90. wc->flags = AV_RL32(wc->extra + 4);
  91. //parse flags
  92. bpp = ((wc->flags & 3) + 1) << 3;
  93. chan = 1 + !(wc->flags & WV_MONO);
  94. rate = wv_rates[(wc->flags >> 23) & 0xF];
  95. if((wc->flags & 0x1800) != 0x1800){
  96. av_log(ctx, AV_LOG_ERROR, "Multichannel WavPack is not supported yet.\n");
  97. return -1;
  98. }
  99. if(rate == -1 && !wc->block_parsed){
  100. int64_t block_end = url_ftell(pb) + wc->blksize - 24;
  101. if(url_is_streamed(pb)){
  102. av_log(ctx, AV_LOG_ERROR, "Cannot determine custom sampling rate\n");
  103. return -1;
  104. }
  105. while(url_ftell(pb) < block_end){
  106. int id, size;
  107. id = get_byte(pb);
  108. size = (id & 0x80) ? get_le24(pb) : get_byte(pb);
  109. size <<= 1;
  110. if(id&0x40)
  111. size--;
  112. if((id&0x3F) == 0x27){
  113. rate = get_le24(pb);
  114. break;
  115. }else{
  116. url_fskip(pb, size);
  117. }
  118. }
  119. if(rate == -1){
  120. av_log(ctx, AV_LOG_ERROR, "Cannot determine custom sampling rate\n");
  121. return -1;
  122. }
  123. url_fseek(pb, block_end - wc->blksize + 24, SEEK_SET);
  124. }
  125. if(!wc->bpp) wc->bpp = bpp;
  126. if(!wc->chan) wc->chan = chan;
  127. if(!wc->rate) wc->rate = rate;
  128. if(wc->flags && bpp != wc->bpp){
  129. av_log(ctx, AV_LOG_ERROR, "Bits per sample differ, this block: %i, header block: %i\n", bpp, wc->bpp);
  130. return -1;
  131. }
  132. if(wc->flags && chan != wc->chan){
  133. av_log(ctx, AV_LOG_ERROR, "Channels differ, this block: %i, header block: %i\n", chan, wc->chan);
  134. return -1;
  135. }
  136. if(wc->flags && rate != -1 && rate != wc->rate){
  137. av_log(ctx, AV_LOG_ERROR, "Sampling rate differ, this block: %i, header block: %i\n", rate, wc->rate);
  138. return -1;
  139. }
  140. wc->blksize = size - 24;
  141. return 0;
  142. }
  143. static int wv_read_header(AVFormatContext *s,
  144. AVFormatParameters *ap)
  145. {
  146. ByteIOContext *pb = s->pb;
  147. WVContext *wc = s->priv_data;
  148. AVStream *st;
  149. wc->block_parsed = 0;
  150. if(wv_read_block_header(s, pb) < 0)
  151. return -1;
  152. /* now we are ready: build format streams */
  153. st = av_new_stream(s, 0);
  154. if (!st)
  155. return -1;
  156. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  157. st->codec->codec_id = CODEC_ID_WAVPACK;
  158. st->codec->channels = wc->chan;
  159. st->codec->sample_rate = wc->rate;
  160. st->codec->bits_per_coded_sample = wc->bpp;
  161. av_set_pts_info(st, 64, 1, wc->rate);
  162. st->start_time = 0;
  163. st->duration = wc->samples;
  164. if(!url_is_streamed(s->pb)) {
  165. int64_t cur = url_ftell(s->pb);
  166. ff_ape_parse_tag(s);
  167. if(!av_metadata_get(s->metadata, "", NULL, AV_METADATA_IGNORE_SUFFIX))
  168. ff_id3v1_read(s);
  169. url_fseek(s->pb, cur, SEEK_SET);
  170. }
  171. return 0;
  172. }
  173. static int wv_read_packet(AVFormatContext *s,
  174. AVPacket *pkt)
  175. {
  176. WVContext *wc = s->priv_data;
  177. int ret;
  178. if (url_feof(s->pb))
  179. return AVERROR(EIO);
  180. if(wc->block_parsed){
  181. if(wv_read_block_header(s, s->pb) < 0)
  182. return -1;
  183. }
  184. if(av_new_packet(pkt, wc->blksize + WV_EXTRA_SIZE) < 0)
  185. return AVERROR(ENOMEM);
  186. memcpy(pkt->data, wc->extra, WV_EXTRA_SIZE);
  187. ret = get_buffer(s->pb, pkt->data + WV_EXTRA_SIZE, wc->blksize);
  188. if(ret != wc->blksize){
  189. av_free_packet(pkt);
  190. return AVERROR(EIO);
  191. }
  192. pkt->stream_index = 0;
  193. wc->block_parsed = 1;
  194. pkt->size = ret + WV_EXTRA_SIZE;
  195. pkt->pts = wc->soff;
  196. av_add_index_entry(s->streams[0], wc->pos, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
  197. return 0;
  198. }
  199. static int wv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  200. {
  201. AVStream *st = s->streams[stream_index];
  202. WVContext *wc = s->priv_data;
  203. AVPacket pkt1, *pkt = &pkt1;
  204. int ret;
  205. int index = av_index_search_timestamp(st, timestamp, flags);
  206. int64_t pos, pts;
  207. /* if found, seek there */
  208. if (index >= 0){
  209. wc->block_parsed = 1;
  210. url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET);
  211. return 0;
  212. }
  213. /* if timestamp is out of bounds, return error */
  214. if(timestamp < 0 || timestamp >= s->duration)
  215. return -1;
  216. pos = url_ftell(s->pb);
  217. do{
  218. ret = av_read_frame(s, pkt);
  219. if (ret < 0){
  220. url_fseek(s->pb, pos, SEEK_SET);
  221. return -1;
  222. }
  223. pts = pkt->pts;
  224. av_free_packet(pkt);
  225. }while(pts < timestamp);
  226. return 0;
  227. }
  228. AVInputFormat wv_demuxer = {
  229. "wv",
  230. NULL_IF_CONFIG_SMALL("WavPack"),
  231. sizeof(WVContext),
  232. wv_probe,
  233. wv_read_header,
  234. wv_read_packet,
  235. NULL,
  236. wv_read_seek,
  237. };