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.

238 lines
6.5KB

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