Libav 0.7.1
|
00001 /* 00002 * AC-3 DSP utils 00003 * Copyright (c) 2011 Justin Ruggles 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * Libav is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with Libav; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00022 #include "libavutil/avassert.h" 00023 #include "avcodec.h" 00024 #include "ac3.h" 00025 #include "ac3dsp.h" 00026 00027 static void ac3_exponent_min_c(uint8_t *exp, int num_reuse_blocks, int nb_coefs) 00028 { 00029 int blk, i; 00030 00031 if (!num_reuse_blocks) 00032 return; 00033 00034 for (i = 0; i < nb_coefs; i++) { 00035 uint8_t min_exp = *exp; 00036 uint8_t *exp1 = exp + 256; 00037 for (blk = 0; blk < num_reuse_blocks; blk++) { 00038 uint8_t next_exp = *exp1; 00039 if (next_exp < min_exp) 00040 min_exp = next_exp; 00041 exp1 += 256; 00042 } 00043 *exp++ = min_exp; 00044 } 00045 } 00046 00047 static int ac3_max_msb_abs_int16_c(const int16_t *src, int len) 00048 { 00049 int i, v = 0; 00050 for (i = 0; i < len; i++) 00051 v |= abs(src[i]); 00052 return v; 00053 } 00054 00055 static void ac3_lshift_int16_c(int16_t *src, unsigned int len, 00056 unsigned int shift) 00057 { 00058 uint32_t *src32 = (uint32_t *)src; 00059 const uint32_t mask = ~(((1 << shift) - 1) << 16); 00060 int i; 00061 len >>= 1; 00062 for (i = 0; i < len; i += 8) { 00063 src32[i ] = (src32[i ] << shift) & mask; 00064 src32[i+1] = (src32[i+1] << shift) & mask; 00065 src32[i+2] = (src32[i+2] << shift) & mask; 00066 src32[i+3] = (src32[i+3] << shift) & mask; 00067 src32[i+4] = (src32[i+4] << shift) & mask; 00068 src32[i+5] = (src32[i+5] << shift) & mask; 00069 src32[i+6] = (src32[i+6] << shift) & mask; 00070 src32[i+7] = (src32[i+7] << shift) & mask; 00071 } 00072 } 00073 00074 static void ac3_rshift_int32_c(int32_t *src, unsigned int len, 00075 unsigned int shift) 00076 { 00077 do { 00078 *src++ >>= shift; 00079 *src++ >>= shift; 00080 *src++ >>= shift; 00081 *src++ >>= shift; 00082 *src++ >>= shift; 00083 *src++ >>= shift; 00084 *src++ >>= shift; 00085 *src++ >>= shift; 00086 len -= 8; 00087 } while (len > 0); 00088 } 00089 00090 static void float_to_fixed24_c(int32_t *dst, const float *src, unsigned int len) 00091 { 00092 const float scale = 1 << 24; 00093 do { 00094 *dst++ = lrintf(*src++ * scale); 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 len -= 8; 00103 } while (len > 0); 00104 } 00105 00106 static void ac3_bit_alloc_calc_bap_c(int16_t *mask, int16_t *psd, 00107 int start, int end, 00108 int snr_offset, int floor, 00109 const uint8_t *bap_tab, uint8_t *bap) 00110 { 00111 int bin, band; 00112 00113 /* special case, if snr offset is -960, set all bap's to zero */ 00114 if (snr_offset == -960) { 00115 memset(bap, 0, AC3_MAX_COEFS); 00116 return; 00117 } 00118 00119 bin = start; 00120 band = ff_ac3_bin_to_band_tab[start]; 00121 do { 00122 int m = (FFMAX(mask[band] - snr_offset - floor, 0) & 0x1FE0) + floor; 00123 int band_end = FFMIN(ff_ac3_band_start_tab[band+1], end); 00124 for (; bin < band_end; bin++) { 00125 int address = av_clip((psd[bin] - m) >> 5, 0, 63); 00126 bap[bin] = bap_tab[address]; 00127 } 00128 } while (end > ff_ac3_band_start_tab[band++]); 00129 } 00130 00131 static void ac3_update_bap_counts_c(uint16_t mant_cnt[16], uint8_t *bap, 00132 int len) 00133 { 00134 while (len-- > 0) 00135 mant_cnt[bap[len]]++; 00136 } 00137 00138 DECLARE_ALIGNED(16, const uint16_t, ff_ac3_bap_bits)[16] = { 00139 0, 0, 0, 3, 0, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16 00140 }; 00141 00142 static int ac3_compute_mantissa_size_c(uint16_t mant_cnt[6][16]) 00143 { 00144 int blk, bap; 00145 int bits = 0; 00146 00147 for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) { 00148 // bap=1 : 3 mantissas in 5 bits 00149 bits += (mant_cnt[blk][1] / 3) * 5; 00150 // bap=2 : 3 mantissas in 7 bits 00151 // bap=4 : 2 mantissas in 7 bits 00152 bits += ((mant_cnt[blk][2] / 3) + (mant_cnt[blk][4] >> 1)) * 7; 00153 // bap=3 : 1 mantissa in 3 bits 00154 bits += mant_cnt[blk][3] * 3; 00155 // bap=5 to 15 : get bits per mantissa from table 00156 for (bap = 5; bap < 16; bap++) 00157 bits += mant_cnt[blk][bap] * ff_ac3_bap_bits[bap]; 00158 } 00159 return bits; 00160 } 00161 00162 static void ac3_extract_exponents_c(uint8_t *exp, int32_t *coef, int nb_coefs) 00163 { 00164 int i; 00165 00166 for (i = 0; i < nb_coefs; i++) { 00167 int e; 00168 int v = abs(coef[i]); 00169 if (v == 0) 00170 e = 24; 00171 else { 00172 e = 23 - av_log2(v); 00173 if (e >= 24) { 00174 e = 24; 00175 coef[i] = 0; 00176 } else if (e < 0) { 00177 e = 0; 00178 coef[i] = av_clip(coef[i], -16777215, 16777215); 00179 } 00180 } 00181 exp[i] = e; 00182 } 00183 } 00184 00185 av_cold void ff_ac3dsp_init(AC3DSPContext *c, int bit_exact) 00186 { 00187 c->ac3_exponent_min = ac3_exponent_min_c; 00188 c->ac3_max_msb_abs_int16 = ac3_max_msb_abs_int16_c; 00189 c->ac3_lshift_int16 = ac3_lshift_int16_c; 00190 c->ac3_rshift_int32 = ac3_rshift_int32_c; 00191 c->float_to_fixed24 = float_to_fixed24_c; 00192 c->bit_alloc_calc_bap = ac3_bit_alloc_calc_bap_c; 00193 c->update_bap_counts = ac3_update_bap_counts_c; 00194 c->compute_mantissa_size = ac3_compute_mantissa_size_c; 00195 c->extract_exponents = ac3_extract_exponents_c; 00196 00197 if (ARCH_ARM) 00198 ff_ac3dsp_init_arm(c, bit_exact); 00199 if (HAVE_MMX) 00200 ff_ac3dsp_init_x86(c, bit_exact); 00201 }