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.

203 lines
5.4KB

  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. int block_parsed;
  48. uint8_t extra[WV_EXTRA_SIZE];
  49. }WVContext;
  50. static int wv_probe(AVProbeData *p)
  51. {
  52. /* check file header */
  53. if (p->buf_size <= 32)
  54. return 0;
  55. if (p->buf[0] == 'w' && p->buf[1] == 'v' &&
  56. p->buf[2] == 'p' && p->buf[3] == 'k')
  57. return AVPROBE_SCORE_MAX;
  58. else
  59. return 0;
  60. }
  61. static int wv_read_block_header(AVFormatContext *ctx, ByteIOContext *pb)
  62. {
  63. WVContext *wc = ctx->priv_data;
  64. uint32_t tag, ver;
  65. int size;
  66. int rate, bpp, chan;
  67. tag = get_le32(pb);
  68. if (tag != MKTAG('w', 'v', 'p', 'k'))
  69. return -1;
  70. size = get_le32(pb);
  71. if(size < 24 || size > WV_BLOCK_LIMIT){
  72. av_log(ctx, AV_LOG_ERROR, "Incorrect block size %i\n", size);
  73. return -1;
  74. }
  75. wc->blksize = size;
  76. ver = get_le16(pb);
  77. if(ver < 0x402 || ver > 0x40F){
  78. av_log(ctx, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
  79. return -1;
  80. }
  81. get_byte(pb); // track no
  82. get_byte(pb); // track sub index
  83. get_le32(pb); // total samples in file
  84. get_le32(pb); // offset in samples of current block
  85. get_buffer(pb, wc->extra, WV_EXTRA_SIZE);
  86. wc->flags = AV_RL32(wc->extra + 4);
  87. //parse flags
  88. if(wc->flags & WV_FLOAT){
  89. av_log(ctx, AV_LOG_ERROR, "Floating point data is not supported\n");
  90. return -1;
  91. }
  92. if(wc->flags & WV_HYBRID){
  93. av_log(ctx, AV_LOG_ERROR, "Hybrid coding mode is not supported\n");
  94. return -1;
  95. }
  96. if(wc->flags & WV_INT32){
  97. av_log(ctx, AV_LOG_ERROR, "Integer point data is not supported\n");
  98. return -1;
  99. }
  100. bpp = ((wc->flags & 3) + 1) << 3;
  101. chan = 1 + !(wc->flags & WV_MONO);
  102. rate = wv_rates[(wc->flags >> 23) & 0xF];
  103. if(rate == -1){
  104. av_log(ctx, AV_LOG_ERROR, "Unknown sampling rate\n");
  105. return -1;
  106. }
  107. if(!wc->bpp) wc->bpp = bpp;
  108. if(!wc->chan) wc->chan = chan;
  109. if(!wc->rate) wc->rate = rate;
  110. if(wc->flags && bpp != wc->bpp){
  111. av_log(ctx, AV_LOG_ERROR, "Bits per sample differ, this block: %i, header block: %i\n", bpp, wc->bpp);
  112. return -1;
  113. }
  114. if(wc->flags && chan != wc->chan){
  115. av_log(ctx, AV_LOG_ERROR, "Channels differ, this block: %i, header block: %i\n", chan, wc->chan);
  116. return -1;
  117. }
  118. if(wc->flags && rate != wc->rate){
  119. av_log(ctx, AV_LOG_ERROR, "Sampling rate differ, this block: %i, header block: %i\n", rate, wc->rate);
  120. return -1;
  121. }
  122. wc->blksize = size - 24;
  123. return 0;
  124. }
  125. static int wv_read_header(AVFormatContext *s,
  126. AVFormatParameters *ap)
  127. {
  128. ByteIOContext *pb = &s->pb;
  129. WVContext *wc = s->priv_data;
  130. AVStream *st;
  131. if(wv_read_block_header(s, pb) < 0)
  132. return -1;
  133. wc->block_parsed = 0;
  134. /* now we are ready: build format streams */
  135. st = av_new_stream(s, 0);
  136. if (!st)
  137. return -1;
  138. st->codec->codec_type = CODEC_TYPE_AUDIO;
  139. st->codec->codec_id = CODEC_ID_WAVPACK;
  140. st->codec->channels = wc->chan;
  141. st->codec->sample_rate = wc->rate;
  142. st->codec->bits_per_sample = wc->bpp;
  143. av_set_pts_info(st, 64, 1, wc->rate);
  144. return 0;
  145. }
  146. static int wv_read_packet(AVFormatContext *s,
  147. AVPacket *pkt)
  148. {
  149. WVContext *wc = s->priv_data;
  150. int ret;
  151. if (url_feof(&s->pb))
  152. return -EIO;
  153. if(wc->block_parsed){
  154. if(wv_read_block_header(s, &s->pb) < 0)
  155. return -1;
  156. }
  157. if(av_new_packet(pkt, wc->blksize + WV_EXTRA_SIZE) < 0)
  158. return AVERROR_NOMEM;
  159. memcpy(pkt->data, wc->extra, WV_EXTRA_SIZE);
  160. ret = get_buffer(&s->pb, pkt->data + WV_EXTRA_SIZE, wc->blksize);
  161. if(ret != wc->blksize){
  162. av_free_packet(pkt);
  163. return AVERROR_IO;
  164. }
  165. pkt->stream_index = 0;
  166. wc->block_parsed = 1;
  167. pkt->size = ret + WV_EXTRA_SIZE;
  168. return 0;
  169. }
  170. static int wv_read_close(AVFormatContext *s)
  171. {
  172. return 0;
  173. }
  174. AVInputFormat wv_demuxer = {
  175. "wv",
  176. "WavPack",
  177. sizeof(WVContext),
  178. wv_probe,
  179. wv_read_header,
  180. wv_read_packet,
  181. wv_read_close,
  182. };