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.

164 lines
4.2KB

  1. /*
  2. * WavPack decoder/encoder common code
  3. * Copyright (c) 2006,2011 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. #ifndef AVCODEC_WAVPACK_H
  22. #define AVCODEC_WAVPACK_H
  23. #include "libavutil/common.h"
  24. #define MAX_TERMS 16
  25. #define MAX_TERM 8
  26. #define WV_HEADER_SIZE 32
  27. #define WV_MONO 0x00000004
  28. #define WV_JOINT_STEREO 0x00000010
  29. #define WV_CROSS_DECORR 0x00000020
  30. #define WV_FLOAT_DATA 0x00000080
  31. #define WV_INT32_DATA 0x00000100
  32. #define WV_FALSE_STEREO 0x40000000
  33. #define WV_DSD_DATA 0x80000000
  34. #define WV_HYBRID_MODE 0x00000008
  35. #define WV_HYBRID_SHAPE 0x00000008
  36. #define WV_HYBRID_BITRATE 0x00000200
  37. #define WV_HYBRID_BALANCE 0x00000400
  38. #define WV_INITIAL_BLOCK 0x00000800
  39. #define WV_FINAL_BLOCK 0x00001000
  40. #define WV_MONO_DATA (WV_MONO | WV_FALSE_STEREO)
  41. #define WV_SINGLE_BLOCK (WV_INITIAL_BLOCK | WV_FINAL_BLOCK)
  42. #define WV_FLT_SHIFT_ONES 0x01
  43. #define WV_FLT_SHIFT_SAME 0x02
  44. #define WV_FLT_SHIFT_SENT 0x04
  45. #define WV_FLT_ZERO_SENT 0x08
  46. #define WV_FLT_ZERO_SIGN 0x10
  47. #define WV_MAX_SAMPLES 150000
  48. enum WP_ID_Flags {
  49. WP_IDF_MASK = 0x3F,
  50. WP_IDF_IGNORE = 0x20,
  51. WP_IDF_ODD = 0x40,
  52. WP_IDF_LONG = 0x80
  53. };
  54. enum WP_ID {
  55. WP_ID_DUMMY = 0,
  56. WP_ID_ENCINFO,
  57. WP_ID_DECTERMS,
  58. WP_ID_DECWEIGHTS,
  59. WP_ID_DECSAMPLES,
  60. WP_ID_ENTROPY,
  61. WP_ID_HYBRID,
  62. WP_ID_SHAPING,
  63. WP_ID_FLOATINFO,
  64. WP_ID_INT32INFO,
  65. WP_ID_DATA,
  66. WP_ID_CORR,
  67. WP_ID_EXTRABITS,
  68. WP_ID_CHANINFO,
  69. WP_ID_DSD_DATA,
  70. WP_ID_SAMPLE_RATE = 0x27,
  71. };
  72. typedef struct Decorr {
  73. int delta;
  74. int value;
  75. int weightA;
  76. int weightB;
  77. int samplesA[MAX_TERM];
  78. int samplesB[MAX_TERM];
  79. int sumA;
  80. int sumB;
  81. } Decorr;
  82. typedef struct WvChannel {
  83. int median[3];
  84. int slow_level, error_limit;
  85. unsigned bitrate_acc, bitrate_delta;
  86. } WvChannel;
  87. // macros for manipulating median values
  88. #define GET_MED(n) ((c->median[n] >> 4) + 1)
  89. #define DEC_MED(n) c->median[n] -= ((int)(c->median[n] + (128U >> (n)) - 2) / (128 >> (n))) * 2U
  90. #define INC_MED(n) c->median[n] += ((int)(c->median[n] + (128U >> (n)) ) / (128 >> (n))) * 5U
  91. // macros for applying weight
  92. #define UPDATE_WEIGHT_CLIP(weight, delta, samples, in) \
  93. if ((samples) && (in)) { \
  94. if (((samples) ^ (in)) < 0) { \
  95. (weight) -= (delta); \
  96. if ((weight) < -1024) \
  97. (weight) = -1024; \
  98. } else { \
  99. (weight) += (delta); \
  100. if ((weight) > 1024) \
  101. (weight) = 1024; \
  102. } \
  103. }
  104. static const int wv_rates[16] = {
  105. 6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000,
  106. 32000, 44100, 48000, 64000, 88200, 96000, 192000, 0
  107. };
  108. // exponent table copied from WavPack source
  109. extern const uint8_t ff_wp_exp2_table[256];
  110. extern const uint8_t ff_wp_log2_table[256];
  111. static av_always_inline int wp_exp2(int16_t val)
  112. {
  113. int res, neg = 0;
  114. if (val < 0) {
  115. val = -val;
  116. neg = 1;
  117. }
  118. res = ff_wp_exp2_table[val & 0xFF] | 0x100;
  119. val >>= 8;
  120. if (val > 31U)
  121. return INT_MIN;
  122. res = (val > 9) ? (res << (val - 9)) : (res >> (9 - val));
  123. return neg ? -res : res;
  124. }
  125. static av_always_inline int wp_log2(uint32_t val)
  126. {
  127. int bits;
  128. if (!val)
  129. return 0;
  130. if (val == 1)
  131. return 256;
  132. val += val >> 9;
  133. bits = av_log2(val) + 1;
  134. if (bits < 9)
  135. return (bits << 8) + ff_wp_log2_table[(val << (9 - bits)) & 0xFF];
  136. else
  137. return (bits << 8) + ff_wp_log2_table[(val >> (bits - 9)) & 0xFF];
  138. }
  139. #endif /* AVCODEC_WAVPACK_H */