• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/ac3.c

Go to the documentation of this file.
00001 /*
00002  * Common code between the AC-3 encoder and decoder
00003  * Copyright (c) 2000 Fabrice Bellard
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00027 #include "avcodec.h"
00028 #include "ac3.h"
00029 #include "get_bits.h"
00030 
00031 #if CONFIG_HARDCODED_TABLES
00032 
00036 static const uint8_t band_start_tab[51] = {
00037       0,  1,   2,   3,   4,   5,   6,   7,   8,   9,
00038      10,  11, 12,  13,  14,  15,  16,  17,  18,  19,
00039      20,  21, 22,  23,  24,  25,  26,  27,  28,  31,
00040      34,  37, 40,  43,  46,  49,  55,  61,  67,  73,
00041      79,  85, 97, 109, 121, 133, 157, 181, 205, 229, 253
00042 };
00043 
00047 static const uint8_t bin_to_band_tab[253] = {
00048      0,
00049      1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12,
00050     13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
00051     25, 26, 27, 28, 28, 28, 29, 29, 29, 30, 30, 30,
00052     31, 31, 31, 32, 32, 32, 33, 33, 33, 34, 34, 34,
00053     35, 35, 35, 35, 35, 35, 36, 36, 36, 36, 36, 36,
00054     37, 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, 38,
00055     39, 39, 39, 39, 39, 39, 40, 40, 40, 40, 40, 40,
00056     41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41,
00057     42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42,
00058     43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43,
00059     44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44,
00060     45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45,
00061     45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45,
00062     46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46,
00063     46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46,
00064     47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47,
00065     47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47,
00066     48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
00067     48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
00068     49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49,
00069     49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49
00070 };
00071 
00072 #else /* CONFIG_HARDCODED_TABLES */
00073 static uint8_t band_start_tab[51];
00074 static uint8_t bin_to_band_tab[253];
00075 #endif
00076 
00077 static inline int calc_lowcomp1(int a, int b0, int b1, int c)
00078 {
00079     if ((b0 + 256) == b1) {
00080         a = c;
00081     } else if (b0 > b1) {
00082         a = FFMAX(a - 64, 0);
00083     }
00084     return a;
00085 }
00086 
00087 static inline int calc_lowcomp(int a, int b0, int b1, int bin)
00088 {
00089     if (bin < 7) {
00090         return calc_lowcomp1(a, b0, b1, 384);
00091     } else if (bin < 20) {
00092         return calc_lowcomp1(a, b0, b1, 320);
00093     } else {
00094         return FFMAX(a - 128, 0);
00095     }
00096 }
00097 
00098 void ff_ac3_bit_alloc_calc_psd(int8_t *exp, int start, int end, int16_t *psd,
00099                                int16_t *band_psd)
00100 {
00101     int bin, band;
00102 
00103     /* exponent mapping to PSD */
00104     for (bin = start; bin < end; bin++) {
00105         psd[bin]=(3072 - (exp[bin] << 7));
00106     }
00107 
00108     /* PSD integration */
00109     bin  = start;
00110     band = bin_to_band_tab[start];
00111     do {
00112         int v = psd[bin++];
00113         int band_end = FFMIN(band_start_tab[band+1], end);
00114         for (; bin < band_end; bin++) {
00115             int max = FFMAX(v, psd[bin]);
00116             /* logadd */
00117             int adr = FFMIN(max - ((v + psd[bin] + 1) >> 1), 255);
00118             v = max + ff_ac3_log_add_tab[adr];
00119         }
00120         band_psd[band++] = v;
00121     } while (end > band_start_tab[band]);
00122 }
00123 
00124 int ff_ac3_bit_alloc_calc_mask(AC3BitAllocParameters *s, int16_t *band_psd,
00125                                int start, int end, int fast_gain, int is_lfe,
00126                                int dba_mode, int dba_nsegs, uint8_t *dba_offsets,
00127                                uint8_t *dba_lengths, uint8_t *dba_values,
00128                                int16_t *mask)
00129 {
00130     int16_t excite[50]; /* excitation */
00131     int band;
00132     int band_start, band_end, begin, end1;
00133     int lowcomp, fastleak, slowleak;
00134 
00135     /* excitation function */
00136     band_start = bin_to_band_tab[start];
00137     band_end   = bin_to_band_tab[end-1] + 1;
00138 
00139     if (band_start == 0) {
00140         lowcomp = 0;
00141         lowcomp = calc_lowcomp1(lowcomp, band_psd[0], band_psd[1], 384);
00142         excite[0] = band_psd[0] - fast_gain - lowcomp;
00143         lowcomp = calc_lowcomp1(lowcomp, band_psd[1], band_psd[2], 384);
00144         excite[1] = band_psd[1] - fast_gain - lowcomp;
00145         begin = 7;
00146         for (band = 2; band < 7; band++) {
00147             if (!(is_lfe && band == 6))
00148                 lowcomp = calc_lowcomp1(lowcomp, band_psd[band], band_psd[band+1], 384);
00149             fastleak = band_psd[band] - fast_gain;
00150             slowleak = band_psd[band] - s->slow_gain;
00151             excite[band] = fastleak - lowcomp;
00152             if (!(is_lfe && band == 6)) {
00153                 if (band_psd[band] <= band_psd[band+1]) {
00154                     begin = band + 1;
00155                     break;
00156                 }
00157             }
00158         }
00159 
00160         end1 = FFMIN(band_end, 22);
00161         for (band = begin; band < end1; band++) {
00162             if (!(is_lfe && band == 6))
00163                 lowcomp = calc_lowcomp(lowcomp, band_psd[band], band_psd[band+1], band);
00164             fastleak = FFMAX(fastleak - s->fast_decay, band_psd[band] - fast_gain);
00165             slowleak = FFMAX(slowleak - s->slow_decay, band_psd[band] - s->slow_gain);
00166             excite[band] = FFMAX(fastleak - lowcomp, slowleak);
00167         }
00168         begin = 22;
00169     } else {
00170         /* coupling channel */
00171         begin = band_start;
00172         fastleak = (s->cpl_fast_leak << 8) + 768;
00173         slowleak = (s->cpl_slow_leak << 8) + 768;
00174     }
00175 
00176     for (band = begin; band < band_end; band++) {
00177         fastleak = FFMAX(fastleak - s->fast_decay, band_psd[band] - fast_gain);
00178         slowleak = FFMAX(slowleak - s->slow_decay, band_psd[band] - s->slow_gain);
00179         excite[band] = FFMAX(fastleak, slowleak);
00180     }
00181 
00182     /* compute masking curve */
00183 
00184     for (band = band_start; band < band_end; band++) {
00185         int tmp = s->db_per_bit - band_psd[band];
00186         if (tmp > 0) {
00187             excite[band] += tmp >> 2;
00188         }
00189         mask[band] = FFMAX(ff_ac3_hearing_threshold_tab[band >> s->sr_shift][s->sr_code], excite[band]);
00190     }
00191 
00192     /* delta bit allocation */
00193 
00194     if (dba_mode == DBA_REUSE || dba_mode == DBA_NEW) {
00195         int i, seg, delta;
00196         if (dba_nsegs >= 8)
00197             return -1;
00198         band = 0;
00199         for (seg = 0; seg < dba_nsegs; seg++) {
00200             band += dba_offsets[seg];
00201             if (band >= 50 || dba_lengths[seg] > 50-band)
00202                 return -1;
00203             if (dba_values[seg] >= 4) {
00204                 delta = (dba_values[seg] - 3) << 7;
00205             } else {
00206                 delta = (dba_values[seg] - 4) << 7;
00207             }
00208             for (i = 0; i < dba_lengths[seg]; i++) {
00209                 mask[band++] += delta;
00210             }
00211         }
00212     }
00213     return 0;
00214 }
00215 
00216 void ff_ac3_bit_alloc_calc_bap(int16_t *mask, int16_t *psd, int start, int end,
00217                                int snr_offset, int floor,
00218                                const uint8_t *bap_tab, uint8_t *bap)
00219 {
00220     int bin, band;
00221 
00222     /* special case, if snr offset is -960, set all bap's to zero */
00223     if (snr_offset == -960) {
00224         memset(bap, 0, 256);
00225         return;
00226     }
00227 
00228     bin  = start;
00229     band = bin_to_band_tab[start];
00230     do {
00231         int m = (FFMAX(mask[band] - snr_offset - floor, 0) & 0x1FE0) + floor;
00232         int band_end = FFMIN(band_start_tab[band+1], end);
00233         for (; bin < band_end; bin++) {
00234             int address = av_clip((psd[bin] - m) >> 5, 0, 63);
00235             bap[bin] = bap_tab[address];
00236         }
00237     } while (end > band_start_tab[band++]);
00238 }
00239 
00240 /* AC-3 bit allocation. The algorithm is the one described in the AC-3
00241    spec. */
00242 void ac3_parametric_bit_allocation(AC3BitAllocParameters *s, uint8_t *bap,
00243                                    int8_t *exp, int start, int end,
00244                                    int snr_offset, int fast_gain, int is_lfe,
00245                                    int dba_mode, int dba_nsegs,
00246                                    uint8_t *dba_offsets, uint8_t *dba_lengths,
00247                                    uint8_t *dba_values)
00248 {
00249     int16_t psd[256];   /* scaled exponents */
00250     int16_t band_psd[50]; /* interpolated exponents */
00251     int16_t mask[50];   /* masking value */
00252 
00253     ff_ac3_bit_alloc_calc_psd(exp, start, end, psd, band_psd);
00254 
00255     ff_ac3_bit_alloc_calc_mask(s, band_psd, start, end, fast_gain, is_lfe,
00256                                dba_mode, dba_nsegs, dba_offsets, dba_lengths,
00257                                dba_values, mask);
00258 
00259     ff_ac3_bit_alloc_calc_bap(mask, psd, start, end, snr_offset, s->floor,
00260                               ff_ac3_bap_tab, bap);
00261 }
00262 
00268 av_cold void ac3_common_init(void)
00269 {
00270 #if !CONFIG_HARDCODED_TABLES
00271     /* compute bndtab and masktab from bandsz */
00272     int bin = 0, band;
00273     for (band = 0; band < 50; band++) {
00274         int band_end = bin + ff_ac3_critical_band_size_tab[band];
00275         band_start_tab[band] = bin;
00276         while (bin < band_end)
00277             bin_to_band_tab[bin++] = band;
00278     }
00279     band_start_tab[50] = bin;
00280 #endif /* !CONFIG_HARDCODED_TABLES */
00281 }

Generated on Fri Sep 16 2011 17:17:33 for FFmpeg by  doxygen 1.7.1