00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/avassert.h"
00023 #include "avcodec.h"
00024 #include "ac3.h"
00025 #include "ac3dsp.h"
00026 #include "mathops.h"
00027
00028 static void ac3_exponent_min_c(uint8_t *exp, int num_reuse_blocks, int nb_coefs)
00029 {
00030 int blk, i;
00031
00032 if (!num_reuse_blocks)
00033 return;
00034
00035 for (i = 0; i < nb_coefs; i++) {
00036 uint8_t min_exp = *exp;
00037 uint8_t *exp1 = exp + 256;
00038 for (blk = 0; blk < num_reuse_blocks; blk++) {
00039 uint8_t next_exp = *exp1;
00040 if (next_exp < min_exp)
00041 min_exp = next_exp;
00042 exp1 += 256;
00043 }
00044 *exp++ = min_exp;
00045 }
00046 }
00047
00048 static int ac3_max_msb_abs_int16_c(const int16_t *src, int len)
00049 {
00050 int i, v = 0;
00051 for (i = 0; i < len; i++)
00052 v |= abs(src[i]);
00053 return v;
00054 }
00055
00056 static void ac3_lshift_int16_c(int16_t *src, unsigned int len,
00057 unsigned int shift)
00058 {
00059 uint32_t *src32 = (uint32_t *)src;
00060 const uint32_t mask = ~(((1 << shift) - 1) << 16);
00061 int i;
00062 len >>= 1;
00063 for (i = 0; i < len; i += 8) {
00064 src32[i ] = (src32[i ] << shift) & mask;
00065 src32[i+1] = (src32[i+1] << shift) & mask;
00066 src32[i+2] = (src32[i+2] << shift) & mask;
00067 src32[i+3] = (src32[i+3] << shift) & mask;
00068 src32[i+4] = (src32[i+4] << shift) & mask;
00069 src32[i+5] = (src32[i+5] << shift) & mask;
00070 src32[i+6] = (src32[i+6] << shift) & mask;
00071 src32[i+7] = (src32[i+7] << shift) & mask;
00072 }
00073 }
00074
00075 static void ac3_rshift_int32_c(int32_t *src, unsigned int len,
00076 unsigned int shift)
00077 {
00078 do {
00079 *src++ >>= shift;
00080 *src++ >>= shift;
00081 *src++ >>= shift;
00082 *src++ >>= shift;
00083 *src++ >>= shift;
00084 *src++ >>= shift;
00085 *src++ >>= shift;
00086 *src++ >>= shift;
00087 len -= 8;
00088 } while (len > 0);
00089 }
00090
00091 static void float_to_fixed24_c(int32_t *dst, const float *src, unsigned int len)
00092 {
00093 const float scale = 1 << 24;
00094 do {
00095 *dst++ = lrintf(*src++ * scale);
00096 *dst++ = lrintf(*src++ * scale);
00097 *dst++ = lrintf(*src++ * scale);
00098 *dst++ = lrintf(*src++ * scale);
00099 *dst++ = lrintf(*src++ * scale);
00100 *dst++ = lrintf(*src++ * scale);
00101 *dst++ = lrintf(*src++ * scale);
00102 *dst++ = lrintf(*src++ * scale);
00103 len -= 8;
00104 } while (len > 0);
00105 }
00106
00107 static void ac3_bit_alloc_calc_bap_c(int16_t *mask, int16_t *psd,
00108 int start, int end,
00109 int snr_offset, int floor,
00110 const uint8_t *bap_tab, uint8_t *bap)
00111 {
00112 int bin, band, band_end;
00113
00114
00115 if (snr_offset == -960) {
00116 memset(bap, 0, AC3_MAX_COEFS);
00117 return;
00118 }
00119
00120 bin = start;
00121 band = ff_ac3_bin_to_band_tab[start];
00122 do {
00123 int m = (FFMAX(mask[band] - snr_offset - floor, 0) & 0x1FE0) + floor;
00124 band_end = ff_ac3_band_start_tab[++band];
00125 band_end = FFMIN(band_end, end);
00126
00127 for (; bin < band_end; bin++) {
00128 int address = av_clip((psd[bin] - m) >> 5, 0, 63);
00129 bap[bin] = bap_tab[address];
00130 }
00131 } while (end > band_end);
00132 }
00133
00134 static void ac3_update_bap_counts_c(uint16_t mant_cnt[16], uint8_t *bap,
00135 int len)
00136 {
00137 while (len-- > 0)
00138 mant_cnt[bap[len]]++;
00139 }
00140
00141 DECLARE_ALIGNED(16, const uint16_t, ff_ac3_bap_bits)[16] = {
00142 0, 0, 0, 3, 0, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16
00143 };
00144
00145 static int ac3_compute_mantissa_size_c(uint16_t mant_cnt[6][16])
00146 {
00147 int blk, bap;
00148 int bits = 0;
00149
00150 for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
00151
00152 bits += (mant_cnt[blk][1] / 3) * 5;
00153
00154
00155 bits += ((mant_cnt[blk][2] / 3) + (mant_cnt[blk][4] >> 1)) * 7;
00156
00157 bits += mant_cnt[blk][3] * 3;
00158
00159 for (bap = 5; bap < 16; bap++)
00160 bits += mant_cnt[blk][bap] * ff_ac3_bap_bits[bap];
00161 }
00162 return bits;
00163 }
00164
00165 static void ac3_extract_exponents_c(uint8_t *exp, int32_t *coef, int nb_coefs)
00166 {
00167 int i;
00168
00169 for (i = 0; i < nb_coefs; i++) {
00170 int v = abs(coef[i]);
00171 exp[i] = v ? 23 - av_log2(v) : 24;
00172 }
00173 }
00174
00175 static void ac3_sum_square_butterfly_int32_c(int64_t sum[4],
00176 const int32_t *coef0,
00177 const int32_t *coef1,
00178 int len)
00179 {
00180 int i;
00181
00182 sum[0] = sum[1] = sum[2] = sum[3] = 0;
00183
00184 for (i = 0; i < len; i++) {
00185 int lt = coef0[i];
00186 int rt = coef1[i];
00187 int md = lt + rt;
00188 int sd = lt - rt;
00189 MAC64(sum[0], lt, lt);
00190 MAC64(sum[1], rt, rt);
00191 MAC64(sum[2], md, md);
00192 MAC64(sum[3], sd, sd);
00193 }
00194 }
00195
00196 static void ac3_sum_square_butterfly_float_c(float sum[4],
00197 const float *coef0,
00198 const float *coef1,
00199 int len)
00200 {
00201 int i;
00202
00203 sum[0] = sum[1] = sum[2] = sum[3] = 0;
00204
00205 for (i = 0; i < len; i++) {
00206 float lt = coef0[i];
00207 float rt = coef1[i];
00208 float md = lt + rt;
00209 float sd = lt - rt;
00210 sum[0] += lt * lt;
00211 sum[1] += rt * rt;
00212 sum[2] += md * md;
00213 sum[3] += sd * sd;
00214 }
00215 }
00216
00217 av_cold void ff_ac3dsp_init(AC3DSPContext *c, int bit_exact)
00218 {
00219 c->ac3_exponent_min = ac3_exponent_min_c;
00220 c->ac3_max_msb_abs_int16 = ac3_max_msb_abs_int16_c;
00221 c->ac3_lshift_int16 = ac3_lshift_int16_c;
00222 c->ac3_rshift_int32 = ac3_rshift_int32_c;
00223 c->float_to_fixed24 = float_to_fixed24_c;
00224 c->bit_alloc_calc_bap = ac3_bit_alloc_calc_bap_c;
00225 c->update_bap_counts = ac3_update_bap_counts_c;
00226 c->compute_mantissa_size = ac3_compute_mantissa_size_c;
00227 c->extract_exponents = ac3_extract_exponents_c;
00228 c->sum_square_butterfly_int32 = ac3_sum_square_butterfly_int32_c;
00229 c->sum_square_butterfly_float = ac3_sum_square_butterfly_float_c;
00230
00231 if (ARCH_ARM)
00232 ff_ac3dsp_init_arm(c, bit_exact);
00233 if (HAVE_MMX)
00234 ff_ac3dsp_init_x86(c, bit_exact);
00235 }