wv.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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/bswap.h"
  22. #include "avformat.h"
  23. // specs say that maximum block size is 1Mb
  24. #define WV_BLOCK_LIMIT 1047576
  25. #define WV_EXTRA_SIZE 12
  26. enum WV_FLAGS{
  27. WV_MONO = 0x0004,
  28. WV_HYBRID = 0x0008,
  29. WV_JOINT = 0x0010,
  30. WV_CROSSD = 0x0020,
  31. WV_HSHAPE = 0x0040,
  32. WV_FLOAT = 0x0080,
  33. WV_INT32 = 0x0100,
  34. WV_HBR = 0x0200,
  35. WV_HBAL = 0x0400,
  36. WV_MCINIT = 0x0800,
  37. WV_MCEND = 0x1000,
  38. };
  39. static const int wv_rates[16] = {
  40. 6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000,
  41. 32000, 44100, 48000, 64000, 88200, 96000, 192000, -1
  42. };
  43. typedef struct{
  44. uint32_t blksize, flags;
  45. int rate, chan, bpp;
  46. uint32_t samples, soff;
  47. int block_parsed;
  48. uint8_t extra[WV_EXTRA_SIZE];
  49. int64_t pos;
  50. }WVContext;
  51. static int wv_probe(AVProbeData *p)
  52. {
  53. /* check file header */
  54. if (p->buf_size <= 32)
  55. return 0;
  56. if (p->buf[0] == 'w' && p->buf[1] == 'v' &&
  57. p->buf[2] == 'p' && p->buf[3] == 'k')
  58. return AVPROBE_SCORE_MAX;
  59. else
  60. return 0;
  61. }
  62. static int wv_read_block_header(AVFormatContext *ctx, ByteIOContext *pb)
  63. {
  64. WVContext *wc = ctx->priv_data;
  65. uint32_t tag, ver;
  66. int size;
  67. int rate, bpp, chan;
  68. wc->pos = url_ftell(pb);
  69. tag = get_le32(pb);
  70. if (tag != MKTAG('w', 'v', 'p', 'k'))
  71. return -1;
  72. size = get_le32(pb);
  73. if(size < 24 || size > WV_BLOCK_LIMIT){
  74. av_log(ctx, AV_LOG_ERROR, "Incorrect block size %i\n", size);
  75. return -1;
  76. }
  77. wc->blksize = size;
  78. ver = get_le16(pb);
  79. if(ver < 0x402 || ver > 0x410){
  80. av_log(ctx, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
  81. return -1;
  82. }
  83. get_byte(pb); // track no
  84. get_byte(pb); // track sub index
  85. wc->samples = get_le32(pb); // total samples in file
  86. wc->soff = get_le32(pb); // offset in samples of current block
  87. get_buffer(pb, wc->extra, WV_EXTRA_SIZE);
  88. wc->flags = AV_RL32(wc->extra + 4);
  89. //parse flags
  90. if(wc->flags & WV_FLOAT){
  91. av_log(ctx, AV_LOG_ERROR, "Floating point data is not supported\n");
  92. return -1;
  93. }
  94. if(wc->flags & WV_HYBRID){
  95. av_log(ctx, AV_LOG_ERROR, "Hybrid coding mode 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(wc->flags && 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(wc->flags && 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(wc->flags && 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_coded_sample = wc->bpp;
  141. av_set_pts_info(st, 64, 1, wc->rate);
  142. s->start_time = 0;
  143. s->duration = (int64_t)wc->samples * AV_TIME_BASE / st->codec->sample_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 AVERROR(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(ENOMEM);
  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(EIO);
  164. }
  165. pkt->stream_index = 0;
  166. wc->block_parsed = 1;
  167. pkt->size = ret + WV_EXTRA_SIZE;
  168. pkt->pts = wc->soff;
  169. av_add_index_entry(s->streams[0], wc->pos, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
  170. return 0;
  171. }
  172. static int wv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  173. {
  174. AVStream *st = s->streams[stream_index];
  175. WVContext *wc = s->priv_data;
  176. AVPacket pkt1, *pkt = &pkt1;
  177. int ret;
  178. int index = av_index_search_timestamp(st, timestamp, flags);
  179. int64_t pos, pts;
  180. /* if found, seek there */
  181. if (index >= 0){
  182. wc->block_parsed = 1;
  183. url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET);
  184. return 0;
  185. }
  186. /* if timestamp is out of bounds, return error */
  187. if(timestamp < 0 || timestamp >= s->duration)
  188. return -1;
  189. pos = url_ftell(s->pb);
  190. do{
  191. ret = av_read_frame(s, pkt);
  192. if (ret < 0){
  193. url_fseek(s->pb, pos, SEEK_SET);
  194. return -1;
  195. }
  196. pts = pkt->pts;
  197. av_free_packet(pkt);
  198. }while(pts < timestamp);
  199. return 0;
  200. }
  201. AVInputFormat wv_demuxer = {
  202. "wv",
  203. NULL_IF_CONFIG_SMALL("WavPack"),
  204. sizeof(WVContext),
  205. wv_probe,
  206. wv_read_header,
  207. wv_read_packet,
  208. NULL,
  209. wv_read_seek,
  210. };