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.

208 lines
5.6KB

  1. /*
  2. * WavPack demuxer
  3. * Copyright (c) 2006 Konstantin Shishkov.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "avformat.h"
  20. #include "allformats.h"
  21. #include "bswap.h"
  22. // specs say that maximum block size is 1Mb
  23. #define WV_BLOCK_LIMIT 1047576
  24. #define WV_EXTRA_SIZE 12
  25. enum WV_FLAGS{
  26. WV_MONO = 0x0004,
  27. WV_HYBRID = 0x0008,
  28. WV_JOINT = 0x0010,
  29. WV_CROSSD = 0x0020,
  30. WV_HSHAPE = 0x0040,
  31. WV_FLOAT = 0x0080,
  32. WV_INT32 = 0x0100,
  33. WV_HBR = 0x0200,
  34. WV_HBAL = 0x0400,
  35. WV_MCINIT = 0x0800,
  36. WV_MCEND = 0x1000,
  37. };
  38. static const int wv_rates[16] = {
  39. 6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000,
  40. 32000, 44100, 48000, 64000, 88200, 96000, 192000, -1
  41. };
  42. typedef struct{
  43. uint32_t blksize, flags;
  44. int rate, chan, bpp;
  45. int block_parsed;
  46. uint8_t extra[WV_EXTRA_SIZE];
  47. }WVContext;
  48. static int wv_probe(AVProbeData *p)
  49. {
  50. /* check file header */
  51. if (p->buf_size <= 32)
  52. return 0;
  53. if (p->buf[0] == 'w' && p->buf[1] == 'v' &&
  54. p->buf[2] == 'p' && p->buf[3] == 'k')
  55. return AVPROBE_SCORE_MAX;
  56. else
  57. return 0;
  58. }
  59. static int wv_read_block_header(AVFormatContext *ctx, ByteIOContext *pb)
  60. {
  61. WVContext *wc = ctx->priv_data;
  62. uint32_t tag, ver;
  63. int size;
  64. int rate, bpp, chan;
  65. tag = get_le32(pb);
  66. if (tag != MKTAG('w', 'v', 'p', 'k'))
  67. return -1;
  68. size = get_le32(pb);
  69. if(size < 24 || size > WV_BLOCK_LIMIT){
  70. av_log(ctx, AV_LOG_ERROR, "Incorrect block size %i\n", size);
  71. return -1;
  72. }
  73. wc->blksize = size;
  74. ver = get_le16(pb);
  75. if(ver < 0x402 || ver > 0x40F){
  76. av_log(ctx, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
  77. return -1;
  78. }
  79. get_byte(pb); // track no
  80. get_byte(pb); // track sub index
  81. get_le32(pb); // total samples in file
  82. get_le32(pb); // offset in samples of current block
  83. get_buffer(pb, wc->extra, WV_EXTRA_SIZE);
  84. wc->flags = LE_32(wc->extra + 4);
  85. //parse flags
  86. if(wc->flags & WV_FLOAT){
  87. av_log(ctx, AV_LOG_ERROR, "Floating point data is not supported\n");
  88. return -1;
  89. }
  90. if(wc->flags & WV_HYBRID){
  91. av_log(ctx, AV_LOG_ERROR, "Hybrid coding mode is not supported\n");
  92. return -1;
  93. }
  94. if(wc->flags & WV_INT32){
  95. av_log(ctx, AV_LOG_ERROR, "Integer point data is not supported\n");
  96. return -1;
  97. }
  98. bpp = ((wc->flags & 3) + 1) << 3;
  99. chan = 1 + !(wc->flags & WV_MONO);
  100. rate = wv_rates[(wc->flags >> 23) & 0xF];
  101. if(rate == -1){
  102. av_log(ctx, AV_LOG_ERROR, "Unknown sampling rate\n");
  103. return -1;
  104. }
  105. if(!wc->bpp) wc->bpp = bpp;
  106. if(!wc->chan) wc->chan = chan;
  107. if(!wc->rate) wc->rate = rate;
  108. if(bpp != wc->bpp){
  109. av_log(ctx, AV_LOG_ERROR, "Bits per sample differ, this block: %i, header block: %i\n", bpp, wc->bpp);
  110. return -1;
  111. }
  112. if(chan != wc->chan){
  113. av_log(ctx, AV_LOG_ERROR, "Channels differ, this block: %i, header block: %i\n", chan, wc->chan);
  114. return -1;
  115. }
  116. if(rate != wc->rate){
  117. av_log(ctx, AV_LOG_ERROR, "Sampling rate differ, this block: %i, header block: %i\n", rate, wc->rate);
  118. return -1;
  119. }
  120. wc->blksize = size - 24;
  121. return 0;
  122. }
  123. static int wv_read_header(AVFormatContext *s,
  124. AVFormatParameters *ap)
  125. {
  126. ByteIOContext *pb = &s->pb;
  127. WVContext *wc = s->priv_data;
  128. AVStream *st;
  129. if(wv_read_block_header(s, pb) < 0)
  130. return -1;
  131. wc->block_parsed = 0;
  132. /* now we are ready: build format streams */
  133. st = av_new_stream(s, 0);
  134. if (!st)
  135. return -1;
  136. st->codec->codec_type = CODEC_TYPE_AUDIO;
  137. st->codec->codec_id = CODEC_ID_WAVPACK;
  138. st->codec->channels = wc->chan;
  139. st->codec->sample_rate = wc->rate;
  140. st->codec->bits_per_sample = wc->bpp;
  141. av_set_pts_info(st, 64, 1, wc->rate);
  142. return 0;
  143. }
  144. static int wv_read_packet(AVFormatContext *s,
  145. AVPacket *pkt)
  146. {
  147. WVContext *wc = s->priv_data;
  148. int ret, samples;
  149. if (url_feof(&s->pb))
  150. return -EIO;
  151. if(wc->block_parsed){
  152. if(wv_read_block_header(s, &s->pb) < 0)
  153. return -1;
  154. }
  155. samples = LE_32(wc->extra);
  156. /* should not happen but who knows */
  157. if(samples * 2 * wc->chan > AVCODEC_MAX_AUDIO_FRAME_SIZE){
  158. av_log(s, AV_LOG_ERROR, "Packet size is too big to be handled in lavc!\n");
  159. return -EIO;
  160. }
  161. if(av_new_packet(pkt, wc->blksize + WV_EXTRA_SIZE) < 0)
  162. return AVERROR_NOMEM;
  163. memcpy(pkt->data, wc->extra, WV_EXTRA_SIZE);
  164. ret = get_buffer(&s->pb, pkt->data + WV_EXTRA_SIZE, wc->blksize);
  165. if(ret != wc->blksize){
  166. av_free_packet(pkt);
  167. return AVERROR_IO;
  168. }
  169. pkt->stream_index = 0;
  170. wc->block_parsed = 1;
  171. pkt->size = ret + WV_EXTRA_SIZE;
  172. return 0;
  173. }
  174. static int wv_read_close(AVFormatContext *s)
  175. {
  176. return 0;
  177. }
  178. AVInputFormat wv_demuxer = {
  179. "wv",
  180. "WavPack",
  181. sizeof(WVContext),
  182. wv_probe,
  183. wv_read_header,
  184. wv_read_packet,
  185. wv_read_close,
  186. pcm_read_seek,
  187. };