| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * AC-3 encoder float/fixed template | ||
| 3 | * Copyright (c) 2000 Fabrice Bellard | ||
| 4 | * Copyright (c) 2006-2011 Justin Ruggles <justin.ruggles@gmail.com> | ||
| 5 | * Copyright (c) 2006-2010 Prakash Punnoor <prakash@punnoor.de> | ||
| 6 | * | ||
| 7 | * This file is part of FFmpeg. | ||
| 8 | * | ||
| 9 | * FFmpeg is free software; you can redistribute it and/or | ||
| 10 | * modify it under the terms of the GNU Lesser General Public | ||
| 11 | * License as published by the Free Software Foundation; either | ||
| 12 | * version 2.1 of the License, or (at your option) any later version. | ||
| 13 | * | ||
| 14 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 17 | * Lesser General Public License for more details. | ||
| 18 | * | ||
| 19 | * You should have received a copy of the GNU Lesser General Public | ||
| 20 | * License along with FFmpeg; if not, write to the Free Software | ||
| 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 22 | */ | ||
| 23 | |||
| 24 | /** | ||
| 25 | * @file | ||
| 26 | * AC-3 encoder float/fixed template | ||
| 27 | */ | ||
| 28 | |||
| 29 | #include "config_components.h" | ||
| 30 | |||
| 31 | #include <stdint.h> | ||
| 32 | |||
| 33 | #include "libavutil/attributes.h" | ||
| 34 | #include "libavutil/avassert.h" | ||
| 35 | #include "libavutil/mem_internal.h" | ||
| 36 | |||
| 37 | #include "audiodsp.h" | ||
| 38 | #include "ac3enc.h" | ||
| 39 | #include "eac3enc.h" | ||
| 40 | |||
| 41 | #if AC3ENC_FLOAT | ||
| 42 | #define RENAME(element) element ## _float | ||
| 43 | #else | ||
| 44 | #define RENAME(element) element ## _fixed | ||
| 45 | #endif | ||
| 46 | |||
| 47 | /* power ratios for the -42 dB band floor and the 12 dB difference margin. */ | ||
| 48 | #define PHASE_BAND_ENERGY_DENOMINATOR (1 << 14) | ||
| 49 | #define PHASE_DIFF_ENERGY_FACTOR (1 << 4) | ||
| 50 | |||
| 51 | /* | ||
| 52 | * Apply the MDCT to input samples to generate frequency coefficients. | ||
| 53 | * This applies the KBD window and normalizes the input to reduce precision | ||
| 54 | * loss due to fixed-point calculations. | ||
| 55 | */ | ||
| 56 | 1432 | static void apply_mdct(AC3EncodeContext *s, uint8_t * const *samples) | |
| 57 | { | ||
| 58 | av_assert1(s->num_blocks > 0); | ||
| 59 | |||
| 60 |
2/2✓ Branch 0 taken 2539 times.
✓ Branch 1 taken 1432 times.
|
3971 | for (int ch = 0; ch < s->channels; ch++) { |
| 61 | 2539 | const SampleType *input_samples0 = (const SampleType*)s->planar_samples[ch]; | |
| 62 | /* Reorder channels from native order to AC-3 order. */ | ||
| 63 | 2539 | const SampleType *input_samples1 = (const SampleType*)samples[s->channel_map[ch]]; | |
| 64 | 2539 | int blk = 0; | |
| 65 | |||
| 66 | do { | ||
| 67 | 15234 | AC3Block *block = &s->blocks[blk]; | |
| 68 | 15234 | SampleType *windowed_samples = s->RENAME(windowed_samples); | |
| 69 | |||
| 70 | 15234 | s->fdsp->vector_fmul(windowed_samples, input_samples0, | |
| 71 | 15234 | s->RENAME(mdct_window), AC3_BLOCK_SIZE); | |
| 72 | 15234 | s->fdsp->vector_fmul_reverse(windowed_samples + AC3_BLOCK_SIZE, | |
| 73 | input_samples1, | ||
| 74 | 15234 | s->RENAME(mdct_window), AC3_BLOCK_SIZE); | |
| 75 | |||
| 76 | 15234 | s->tx_fn(s->tx, block->mdct_coef[ch+1], | |
| 77 | windowed_samples, sizeof(*windowed_samples)); | ||
| 78 | 15234 | input_samples0 = input_samples1; | |
| 79 | 15234 | input_samples1 += AC3_BLOCK_SIZE; | |
| 80 |
2/2✓ Branch 0 taken 12695 times.
✓ Branch 1 taken 2539 times.
|
15234 | } while (++blk < s->num_blocks); |
| 81 | |||
| 82 | /* Store last 256 samples of current frame */ | ||
| 83 | 2539 | memcpy(s->planar_samples[ch], input_samples0, | |
| 84 | AC3_BLOCK_SIZE * sizeof(*input_samples0)); | ||
| 85 | } | ||
| 86 | 1432 | } | |
| 87 | |||
| 88 | |||
| 89 | /* | ||
| 90 | * Calculate coupling channel and coupling coordinates. | ||
| 91 | */ | ||
| 92 | 862 | static void apply_channel_coupling(AC3EncodeContext *s) | |
| 93 | { | ||
| 94 | 862 | LOCAL_ALIGNED_32(CoefType, cpl_coords, [AC3_MAX_BLOCKS], [AC3_MAX_CHANNELS][16]); | |
| 95 | #if AC3ENC_FLOAT | ||
| 96 | 610 | LOCAL_ALIGNED_32(int32_t, fixed_cpl_coords, [AC3_MAX_BLOCKS], [AC3_MAX_CHANNELS][16]); | |
| 97 | #else | ||
| 98 | 252 | int32_t (*fixed_cpl_coords)[AC3_MAX_CHANNELS][16] = cpl_coords; | |
| 99 | #endif | ||
| 100 | 862 | int av_uninit(blk), ch, bnd, i, j; | |
| 101 | 862 | CoefSumType energy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][16] = {{{0}}}; | |
| 102 | int cpl_start, num_cpl_coefs; | ||
| 103 | |||
| 104 | 862 | s->phase_flags_in_use = 0; | |
| 105 | 862 | memset(cpl_coords, 0, AC3_MAX_BLOCKS * sizeof(*cpl_coords)); | |
| 106 | #if AC3ENC_FLOAT | ||
| 107 | 610 | memset(fixed_cpl_coords, 0, AC3_MAX_BLOCKS * sizeof(*cpl_coords)); | |
| 108 | #endif | ||
| 109 | |||
| 110 | /* align start to 16-byte boundary. align length to multiple of 32. | ||
| 111 | note: coupling start bin % 4 will always be 1 */ | ||
| 112 | 862 | cpl_start = s->start_freq[CPL_CH] - 1; | |
| 113 | 862 | num_cpl_coefs = FFALIGN(s->num_cpl_subbands * 12 + 1, 32); | |
| 114 | 862 | cpl_start = FFMIN(256, cpl_start + num_cpl_coefs) - num_cpl_coefs; | |
| 115 | |||
| 116 |
2/2✓ Branch 0 taken 844 times.
✓ Branch 1 taken 18 times.
|
862 | if (s->channel_mode == AC3_CHMODE_STEREO) { |
| 117 | uint8_t phase_flags[AC3_MAX_CPL_BANDS]; | ||
| 118 | 844 | int cpl_blocks = 0; | |
| 119 | |||
| 120 | /* use a single phase strategy for the frame. a difference carrier is | ||
| 121 | * selected only when it has at least 12 dB more energy and the band | ||
| 122 | * is within 42 dB of the coded channel energy in every coupling | ||
| 123 | * block. */ | ||
| 124 | 844 | memset(phase_flags, 1, s->num_cpl_bands); | |
| 125 |
2/2✓ Branch 0 taken 5064 times.
✓ Branch 1 taken 844 times.
|
5908 | for (blk = 0; blk < s->num_blocks; blk++) { |
| 126 | 5064 | AC3Block *block = &s->blocks[blk]; | |
| 127 | CoefSumType sum[AC3_MAX_CPL_BANDS][4]; | ||
| 128 | /* the DSP also returns sum and difference energy in slots 2/3. */ | ||
| 129 | CoefSumType block_energy[4]; | ||
| 130 | CoefSumType max_energy; | ||
| 131 | |||
| 132 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5064 times.
|
5064 | if (!block->cpl_in_use) |
| 133 | ✗ | continue; | |
| 134 | 5064 | cpl_blocks++; | |
| 135 | 5064 | sum_square_butterfly(s, block_energy, | |
| 136 | 5064 | block->mdct_coef[1], block->mdct_coef[2], | |
| 137 | s->start_freq[CPL_CH]); | ||
| 138 | 5064 | i = s->start_freq[CPL_CH]; | |
| 139 |
2/2✓ Branch 0 taken 21636 times.
✓ Branch 1 taken 5064 times.
|
26700 | for (bnd = 0; bnd < s->num_cpl_bands; bnd++) { |
| 140 | 21636 | sum_square_butterfly(s, sum[bnd], | |
| 141 | 21636 | block->mdct_coef[1] + i, | |
| 142 | 21636 | block->mdct_coef[2] + i, | |
| 143 | 21636 | s->cpl_band_sizes[bnd]); | |
| 144 | /* reused by the coupling coordinate calculation below. */ | ||
| 145 | 21636 | energy[blk][1][bnd] = sum[bnd][0]; | |
| 146 | 21636 | energy[blk][2][bnd] = sum[bnd][1]; | |
| 147 | 21636 | block_energy[0] += sum[bnd][0]; | |
| 148 | 21636 | block_energy[1] += sum[bnd][1]; | |
| 149 | 21636 | i += s->cpl_band_sizes[bnd]; | |
| 150 | } | ||
| 151 |
2/2✓ Branch 0 taken 1778 times.
✓ Branch 1 taken 1882 times.
|
5064 | max_energy = FFMAX(block_energy[0], block_energy[1]); |
| 152 |
2/2✓ Branch 0 taken 21636 times.
✓ Branch 1 taken 5064 times.
|
26700 | for (bnd = 0; bnd < s->num_cpl_bands; bnd++) { |
| 153 |
2/2✓ Branch 0 taken 5606 times.
✓ Branch 1 taken 10186 times.
|
21636 | CoefSumType band_energy = FFMAX(sum[bnd][0], sum[bnd][1]); |
| 154 | 21636 | int significant = band_energy > | |
| 155 | 21636 | max_energy / PHASE_BAND_ENERGY_DENOMINATOR; | |
| 156 | |||
| 157 |
2/2✓ Branch 0 taken 14423 times.
✓ Branch 1 taken 7213 times.
|
36059 | phase_flags[bnd] &= significant && |
| 158 |
2/2✓ Branch 0 taken 2853 times.
✓ Branch 1 taken 11570 times.
|
14423 | sum[bnd][3] / PHASE_DIFF_ENERGY_FACTOR > sum[bnd][2]; |
| 159 | } | ||
| 160 | } | ||
| 161 |
2/2✓ Branch 0 taken 3606 times.
✓ Branch 1 taken 844 times.
|
4450 | for (bnd = 0; bnd < s->num_cpl_bands; bnd++) { |
| 162 |
3/4✓ Branch 0 taken 465 times.
✓ Branch 1 taken 3141 times.
✓ Branch 2 taken 465 times.
✗ Branch 3 not taken.
|
3606 | int phase = phase_flags[bnd] && cpl_blocks; |
| 163 | |||
| 164 | 3606 | s->phase_flags[bnd] = phase; | |
| 165 | 3606 | s->phase_flags_in_use |= phase; | |
| 166 | } | ||
| 167 | } | ||
| 168 | |||
| 169 | /* calculate coupling channel from fbw channels */ | ||
| 170 |
2/2✓ Branch 0 taken 5172 times.
✓ Branch 1 taken 862 times.
|
6034 | for (blk = 0; blk < s->num_blocks; blk++) { |
| 171 | 5172 | AC3Block *block = &s->blocks[blk]; | |
| 172 | 5172 | CoefType *cpl_coef = &block->mdct_coef[CPL_CH][cpl_start]; | |
| 173 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5172 times.
|
5172 | if (!block->cpl_in_use) |
| 174 | ✗ | continue; | |
| 175 | 5172 | memset(cpl_coef, 0, num_cpl_coefs * sizeof(*cpl_coef)); | |
| 176 |
2/2✓ Branch 0 taken 10668 times.
✓ Branch 1 taken 5172 times.
|
15840 | for (ch = 1; ch <= s->fbw_channels; ch++) { |
| 177 | 10668 | CoefType *ch_coef = &block->mdct_coef[ch][cpl_start]; | |
| 178 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10668 times.
|
10668 | if (!block->channel_in_cpl[ch]) |
| 179 | ✗ | continue; | |
| 180 |
2/2✓ Branch 0 taken 767616 times.
✓ Branch 1 taken 10668 times.
|
778284 | for (i = 0; i < num_cpl_coefs; i++) |
| 181 | 767616 | cpl_coef[i] += ch_coef[i]; | |
| 182 | } | ||
| 183 | |||
| 184 |
2/2✓ Branch 0 taken 5064 times.
✓ Branch 1 taken 108 times.
|
5172 | if (s->channel_mode == AC3_CHMODE_STEREO) { |
| 185 | 5064 | CoefType *left = block->mdct_coef[1]; | |
| 186 | 5064 | CoefType *right = block->mdct_coef[2]; | |
| 187 | |||
| 188 | 5064 | i = s->start_freq[CPL_CH]; | |
| 189 |
2/2✓ Branch 0 taken 21636 times.
✓ Branch 1 taken 5064 times.
|
26700 | for (bnd = 0; bnd < s->num_cpl_bands; bnd++) { |
| 190 |
2/2✓ Branch 0 taken 2790 times.
✓ Branch 1 taken 18846 times.
|
21636 | if (s->phase_flags[bnd]) { |
| 191 |
2/2✓ Branch 0 taken 40176 times.
✓ Branch 1 taken 2790 times.
|
42966 | for (j = 0; j < s->cpl_band_sizes[bnd]; j++) |
| 192 | 40176 | block->mdct_coef[CPL_CH][i + j] = left[i + j] - right[i + j]; | |
| 193 | } | ||
| 194 | 21636 | i += s->cpl_band_sizes[bnd]; | |
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | /* coefficients must be clipped in order to be encoded */ | ||
| 199 | 5172 | clip_coefficients(&s->adsp, cpl_coef, num_cpl_coefs); | |
| 200 | } | ||
| 201 | |||
| 202 | /* calculate energy in each band in coupling channel and each fbw channel */ | ||
| 203 | /* TODO: possibly use SIMD to speed up energy calculation */ | ||
| 204 | 862 | bnd = 0; | |
| 205 | 862 | i = s->start_freq[CPL_CH]; | |
| 206 |
2/2✓ Branch 0 taken 3642 times.
✓ Branch 1 taken 862 times.
|
4504 | while (i < s->cpl_end_freq) { |
| 207 | 3642 | int band_size = s->cpl_band_sizes[bnd]; | |
| 208 | /* stereo channel energies were filled during phase analysis above. */ | ||
| 209 | 7284 | int last_ch = s->channel_mode == AC3_CHMODE_STEREO ? | |
| 210 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 3606 times.
|
3642 | CPL_CH : s->fbw_channels; |
| 211 |
2/2✓ Branch 0 taken 3822 times.
✓ Branch 1 taken 3642 times.
|
7464 | for (ch = CPL_CH; ch <= last_ch; ch++) { |
| 212 |
2/2✓ Branch 0 taken 22932 times.
✓ Branch 1 taken 3822 times.
|
26754 | for (blk = 0; blk < s->num_blocks; blk++) { |
| 213 | 22932 | AC3Block *block = &s->blocks[blk]; | |
| 214 |
4/6✓ Branch 0 taken 22932 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1080 times.
✓ Branch 3 taken 21852 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1080 times.
|
22932 | if (!block->cpl_in_use || (ch > CPL_CH && !block->channel_in_cpl[ch])) |
| 215 | ✗ | continue; | |
| 216 |
2/2✓ Branch 0 taken 352080 times.
✓ Branch 1 taken 22932 times.
|
375012 | for (j = 0; j < band_size; j++) { |
| 217 | 352080 | CoefType v = block->mdct_coef[ch][i+j]; | |
| 218 | 352080 | MAC_COEF(energy[blk][ch][bnd], v, v); | |
| 219 | } | ||
| 220 | } | ||
| 221 | } | ||
| 222 | 3642 | i += band_size; | |
| 223 | 3642 | bnd++; | |
| 224 | } | ||
| 225 | |||
| 226 | /* calculate coupling coordinates for all blocks for all channels */ | ||
| 227 |
2/2✓ Branch 0 taken 5172 times.
✓ Branch 1 taken 862 times.
|
6034 | for (blk = 0; blk < s->num_blocks; blk++) { |
| 228 | 5172 | AC3Block *block = &s->blocks[blk]; | |
| 229 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5172 times.
|
5172 | if (!block->cpl_in_use) |
| 230 | ✗ | continue; | |
| 231 |
2/2✓ Branch 0 taken 10668 times.
✓ Branch 1 taken 5172 times.
|
15840 | for (ch = 1; ch <= s->fbw_channels; ch++) { |
| 232 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10668 times.
|
10668 | if (!block->channel_in_cpl[ch]) |
| 233 | ✗ | continue; | |
| 234 |
2/2✓ Branch 0 taken 44352 times.
✓ Branch 1 taken 10668 times.
|
55020 | for (bnd = 0; bnd < s->num_cpl_bands; bnd++) { |
| 235 | 44352 | cpl_coords[blk][ch][bnd] = calc_cpl_coord(energy[blk][ch][bnd], | |
| 236 | energy[blk][CPL_CH][bnd]); | ||
| 237 | } | ||
| 238 | } | ||
| 239 | } | ||
| 240 | |||
| 241 | /* determine which blocks to send new coupling coordinates for */ | ||
| 242 |
2/2✓ Branch 0 taken 5172 times.
✓ Branch 1 taken 862 times.
|
6034 | for (blk = 0; blk < s->num_blocks; blk++) { |
| 243 | 5172 | AC3Block *block = &s->blocks[blk]; | |
| 244 |
2/2✓ Branch 0 taken 4310 times.
✓ Branch 1 taken 862 times.
|
5172 | AC3Block *block0 = blk ? &s->blocks[blk-1] : NULL; |
| 245 | |||
| 246 | 5172 | memset(block->new_cpl_coords, 0, sizeof(block->new_cpl_coords)); | |
| 247 | |||
| 248 |
1/2✓ Branch 0 taken 5172 times.
✗ Branch 1 not taken.
|
5172 | if (block->cpl_in_use) { |
| 249 | /* send new coordinates if this is the first block, if previous | ||
| 250 | * block did not use coupling but this block does, the channels | ||
| 251 | * using coupling has changed from the previous block, or the | ||
| 252 | * coordinate difference from the last block for any channel is | ||
| 253 | * greater than a threshold value. */ | ||
| 254 |
3/4✓ Branch 0 taken 4310 times.
✓ Branch 1 taken 862 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4310 times.
|
5172 | if (blk == 0 || !block0->cpl_in_use) { |
| 255 |
2/2✓ Branch 0 taken 1778 times.
✓ Branch 1 taken 862 times.
|
2640 | for (ch = 1; ch <= s->fbw_channels; ch++) |
| 256 | 1778 | block->new_cpl_coords[ch] = 1; | |
| 257 | } else { | ||
| 258 |
2/2✓ Branch 0 taken 8890 times.
✓ Branch 1 taken 4310 times.
|
13200 | for (ch = 1; ch <= s->fbw_channels; ch++) { |
| 259 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8890 times.
|
8890 | if (!block->channel_in_cpl[ch]) |
| 260 | ✗ | continue; | |
| 261 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8890 times.
|
8890 | if (!block0->channel_in_cpl[ch]) { |
| 262 | ✗ | block->new_cpl_coords[ch] = 1; | |
| 263 | } else { | ||
| 264 | 8890 | CoefSumType coord_diff = 0; | |
| 265 |
2/2✓ Branch 0 taken 36960 times.
✓ Branch 1 taken 8890 times.
|
45850 | for (bnd = 0; bnd < s->num_cpl_bands; bnd++) { |
| 266 |
2/2✓ Branch 0 taken 10202 times.
✓ Branch 1 taken 438 times.
|
36960 | coord_diff += FFABS(cpl_coords[blk-1][ch][bnd] - |
| 267 | cpl_coords[blk ][ch][bnd]); | ||
| 268 | } | ||
| 269 | 8890 | coord_diff /= s->num_cpl_bands; | |
| 270 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 8842 times.
|
8890 | if (coord_diff > NEW_CPL_COORD_THRESHOLD) |
| 271 | 48 | block->new_cpl_coords[ch] = 1; | |
| 272 | } | ||
| 273 | } | ||
| 274 | } | ||
| 275 | } | ||
| 276 | } | ||
| 277 | |||
| 278 | av_assert1(s->fbw_channels > 0); | ||
| 279 | |||
| 280 | /* calculate final coupling coordinates, taking into account reusing of | ||
| 281 | coordinates in successive blocks */ | ||
| 282 |
2/2✓ Branch 0 taken 3642 times.
✓ Branch 1 taken 862 times.
|
4504 | for (bnd = 0; bnd < s->num_cpl_bands; bnd++) { |
| 283 | 3642 | blk = 0; | |
| 284 |
2/2✓ Branch 0 taken 3734 times.
✓ Branch 1 taken 3642 times.
|
7376 | while (blk < s->num_blocks) { |
| 285 | 3734 | int av_uninit(blk1); | |
| 286 | 3734 | AC3Block *block = &s->blocks[blk]; | |
| 287 | |||
| 288 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3734 times.
|
3734 | if (!block->cpl_in_use) { |
| 289 | ✗ | blk++; | |
| 290 | ✗ | continue; | |
| 291 | } | ||
| 292 | |||
| 293 |
2/2✓ Branch 0 taken 7588 times.
✓ Branch 1 taken 3734 times.
|
11322 | for (ch = 1; ch <= s->fbw_channels; ch++) { |
| 294 | CoefSumType energy_ch, energy_cpl; | ||
| 295 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7588 times.
|
7588 | if (!block->channel_in_cpl[ch]) |
| 296 | ✗ | continue; | |
| 297 | 7588 | energy_cpl = energy[blk][CPL_CH][bnd]; | |
| 298 | 7588 | energy_ch = energy[blk][ch][bnd]; | |
| 299 | 7588 | blk1 = blk+1; | |
| 300 |
4/4✓ Branch 0 taken 37040 times.
✓ Branch 1 taken 7436 times.
✓ Branch 2 taken 36888 times.
✓ Branch 3 taken 152 times.
|
44476 | while (blk1 < s->num_blocks && !s->blocks[blk1].new_cpl_coords[ch]) { |
| 301 |
1/2✓ Branch 0 taken 36888 times.
✗ Branch 1 not taken.
|
36888 | if (s->blocks[blk1].cpl_in_use) { |
| 302 | 36888 | energy_cpl += energy[blk1][CPL_CH][bnd]; | |
| 303 | 36888 | energy_ch += energy[blk1][ch][bnd]; | |
| 304 | } | ||
| 305 | 36888 | blk1++; | |
| 306 | } | ||
| 307 | 7588 | cpl_coords[blk][ch][bnd] = calc_cpl_coord(energy_ch, energy_cpl); | |
| 308 | } | ||
| 309 | 3734 | blk = blk1; | |
| 310 | } | ||
| 311 | } | ||
| 312 | |||
| 313 | /* calculate exponents/mantissas for coupling coordinates */ | ||
| 314 |
2/2✓ Branch 0 taken 5172 times.
✓ Branch 1 taken 862 times.
|
6034 | for (blk = 0; blk < s->num_blocks; blk++) { |
| 315 | 5172 | AC3Block *block = &s->blocks[blk]; | |
| 316 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5172 times.
|
5172 | if (!block->cpl_in_use) |
| 317 | ✗ | continue; | |
| 318 | |||
| 319 | #if AC3ENC_FLOAT | ||
| 320 | 3660 | s->ac3dsp.float_to_fixed24(fixed_cpl_coords[blk][1], | |
| 321 | 3660 | cpl_coords[blk][1], | |
| 322 | 3660 | s->fbw_channels * 16); | |
| 323 | #endif | ||
| 324 | 5172 | s->ac3dsp.extract_exponents(block->cpl_coord_exp[1], | |
| 325 | 5172 | fixed_cpl_coords[blk][1], | |
| 326 | 5172 | s->fbw_channels * 16); | |
| 327 | |||
| 328 |
2/2✓ Branch 0 taken 10668 times.
✓ Branch 1 taken 5172 times.
|
15840 | for (ch = 1; ch <= s->fbw_channels; ch++) { |
| 329 | int bnd, min_exp, max_exp, master_exp; | ||
| 330 | |||
| 331 |
2/2✓ Branch 0 taken 8842 times.
✓ Branch 1 taken 1826 times.
|
10668 | if (!block->new_cpl_coords[ch]) |
| 332 | 8842 | continue; | |
| 333 | |||
| 334 | /* determine master exponent */ | ||
| 335 | 1826 | min_exp = max_exp = block->cpl_coord_exp[ch][0]; | |
| 336 |
2/2✓ Branch 0 taken 5726 times.
✓ Branch 1 taken 1826 times.
|
7552 | for (bnd = 1; bnd < s->num_cpl_bands; bnd++) { |
| 337 | 5726 | int exp = block->cpl_coord_exp[ch][bnd]; | |
| 338 | 5726 | min_exp = FFMIN(exp, min_exp); | |
| 339 | 5726 | max_exp = FFMAX(exp, max_exp); | |
| 340 | } | ||
| 341 | 1826 | master_exp = ((max_exp - 15) + 2) / 3; | |
| 342 | 1826 | master_exp = FFMAX(master_exp, 0); | |
| 343 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1826 times.
|
1826 | while (min_exp < master_exp * 3) |
| 344 | ✗ | master_exp--; | |
| 345 |
2/2✓ Branch 0 taken 7552 times.
✓ Branch 1 taken 1826 times.
|
9378 | for (bnd = 0; bnd < s->num_cpl_bands; bnd++) { |
| 346 | 7552 | block->cpl_coord_exp[ch][bnd] = av_clip(block->cpl_coord_exp[ch][bnd] - | |
| 347 | 7552 | master_exp * 3, 0, 15); | |
| 348 | } | ||
| 349 | 1826 | block->cpl_master_exp[ch] = master_exp; | |
| 350 | |||
| 351 | /* quantize mantissas */ | ||
| 352 |
2/2✓ Branch 0 taken 7552 times.
✓ Branch 1 taken 1826 times.
|
9378 | for (bnd = 0; bnd < s->num_cpl_bands; bnd++) { |
| 353 | 7552 | int cpl_exp = block->cpl_coord_exp[ch][bnd]; | |
| 354 | 7552 | int cpl_mant = (fixed_cpl_coords[blk][ch][bnd] << (5 + cpl_exp + master_exp * 3)) >> 24; | |
| 355 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7552 times.
|
7552 | if (cpl_exp == 15) |
| 356 | ✗ | cpl_mant >>= 1; | |
| 357 | else | ||
| 358 | 7552 | cpl_mant -= 16; | |
| 359 | |||
| 360 | 7552 | block->cpl_coord_mant[ch][bnd] = cpl_mant; | |
| 361 | } | ||
| 362 | } | ||
| 363 | } | ||
| 364 | |||
| 365 |
2/2✓ Branch 0 taken 305 times.
✓ Branch 1 taken 305 times.
|
610 | if (AC3ENC_FLOAT && CONFIG_EAC3_ENCODER && s->eac3) |
| 366 | 305 | ff_eac3_set_cpl_states(s); | |
| 367 | 862 | } | |
| 368 | |||
| 369 | |||
| 370 | /* | ||
| 371 | * Determine rematrixing flags for each block and band. | ||
| 372 | */ | ||
| 373 | 1432 | static void compute_rematrixing_strategy(AC3EncodeContext *s) | |
| 374 | { | ||
| 375 | int nb_coefs; | ||
| 376 | int blk, bnd; | ||
| 377 | 1432 | AC3Block *block, *block0 = NULL; | |
| 378 | |||
| 379 |
2/2✓ Branch 0 taken 415 times.
✓ Branch 1 taken 1017 times.
|
1432 | if (s->channel_mode != AC3_CHMODE_STEREO) |
| 380 | 415 | return; | |
| 381 | |||
| 382 |
2/2✓ Branch 0 taken 6102 times.
✓ Branch 1 taken 1017 times.
|
7119 | for (blk = 0; blk < s->num_blocks; blk++) { |
| 383 | 6102 | block = &s->blocks[blk]; | |
| 384 | 6102 | block->new_rematrixing_strategy = !blk; | |
| 385 | |||
| 386 | 6102 | block->num_rematrixing_bands = 4; | |
| 387 |
2/2✓ Branch 0 taken 5064 times.
✓ Branch 1 taken 1038 times.
|
6102 | if (block->cpl_in_use) { |
| 388 | 5064 | block->num_rematrixing_bands -= (s->start_freq[CPL_CH] <= 61); | |
| 389 | 5064 | block->num_rematrixing_bands -= (s->start_freq[CPL_CH] == 37); | |
| 390 |
3/4✓ Branch 0 taken 4220 times.
✓ Branch 1 taken 844 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4220 times.
|
5064 | if (blk && block->num_rematrixing_bands != block0->num_rematrixing_bands) |
| 391 | ✗ | block->new_rematrixing_strategy = 1; | |
| 392 | } | ||
| 393 | 6102 | nb_coefs = FFMIN(block->end_freq[1], block->end_freq[2]); | |
| 394 | |||
| 395 |
2/2✓ Branch 0 taken 1038 times.
✓ Branch 1 taken 5064 times.
|
6102 | if (!s->rematrixing_enabled) { |
| 396 | 1038 | block0 = block; | |
| 397 | 1038 | continue; | |
| 398 | } | ||
| 399 | |||
| 400 |
2/2✓ Branch 0 taken 19680 times.
✓ Branch 1 taken 5064 times.
|
24744 | for (bnd = 0; bnd < block->num_rematrixing_bands; bnd++) { |
| 401 | /* calculate sum of squared coeffs for one band in one block */ | ||
| 402 | 19680 | int start = ff_ac3_rematrix_band_tab[bnd]; | |
| 403 | 19680 | int end = FFMIN(nb_coefs, ff_ac3_rematrix_band_tab[bnd+1]); | |
| 404 | CoefSumType sum[4]; | ||
| 405 | 19680 | sum_square_butterfly(s, sum, block->mdct_coef[1] + start, | |
| 406 | 19680 | block->mdct_coef[2] + start, end - start); | |
| 407 | |||
| 408 | /* compare sums to determine if rematrixing will be used for this band */ | ||
| 409 |
6/6✓ Branch 0 taken 14882 times.
✓ Branch 1 taken 4798 times.
✓ Branch 2 taken 5234 times.
✓ Branch 3 taken 9022 times.
✓ Branch 4 taken 8488 times.
✓ Branch 5 taken 5768 times.
|
19680 | if (FFMIN(sum[2], sum[3]) < FFMIN(sum[0], sum[1])) |
| 410 | 11248 | block->rematrixing_flags[bnd] = 1; | |
| 411 | else | ||
| 412 | 8432 | block->rematrixing_flags[bnd] = 0; | |
| 413 | |||
| 414 | /* determine if new rematrixing flags will be sent */ | ||
| 415 |
2/2✓ Branch 0 taken 16400 times.
✓ Branch 1 taken 3280 times.
|
19680 | if (blk && |
| 416 |
2/2✓ Branch 0 taken 1895 times.
✓ Branch 1 taken 14505 times.
|
16400 | block->rematrixing_flags[bnd] != block0->rematrixing_flags[bnd]) { |
| 417 | 1895 | block->new_rematrixing_strategy = 1; | |
| 418 | } | ||
| 419 | } | ||
| 420 | 5064 | block0 = block; | |
| 421 | } | ||
| 422 | } | ||
| 423 | |||
| 424 | 16 | static void copy_input_samples(AC3EncodeContext *s, const AVFrame *frame) | |
| 425 | { | ||
| 426 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
|
16 | int end = frame ? frame->nb_samples : 0; |
| 427 | |||
| 428 | /* copy new samples and zero any remaining samples */ | ||
| 429 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
|
16 | if (frame) { |
| 430 | 12 | av_samples_copy(s->input_samples, frame->extended_data, 0, 0, | |
| 431 | 12 | frame->nb_samples, s->channels, | |
| 432 | 12 | s->avctx->sample_fmt); | |
| 433 | } | ||
| 434 | 16 | av_samples_set_silence(s->input_samples, end, | |
| 435 | 16 | s->avctx->frame_size - end, | |
| 436 | 16 | s->channels, s->avctx->sample_fmt); | |
| 437 | 16 | } | |
| 438 | |||
| 439 | 1432 | static void encode_frame(AC3EncodeContext *s, const AVFrame *frame) | |
| 440 | { | ||
| 441 | uint8_t **samples; | ||
| 442 | |||
| 443 |
4/4✓ Branch 0 taken 1428 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 1416 times.
|
1432 | if (!frame || frame->nb_samples < s->avctx->frame_size) { |
| 444 | 16 | copy_input_samples(s, frame); | |
| 445 | 16 | samples = s->input_samples; | |
| 446 | } else | ||
| 447 | 1416 | samples = frame->extended_data; | |
| 448 | |||
| 449 | 1432 | apply_mdct(s, samples); | |
| 450 | |||
| 451 | 1432 | s->cpl_on = s->cpl_enabled; | |
| 452 | 1432 | ff_ac3_compute_coupling_strategy(s); | |
| 453 | |||
| 454 |
2/2✓ Branch 0 taken 862 times.
✓ Branch 1 taken 570 times.
|
1432 | if (s->cpl_on) |
| 455 | 862 | apply_channel_coupling(s); | |
| 456 | |||
| 457 | 1432 | compute_rematrixing_strategy(s); | |
| 458 | |||
| 459 | #if AC3ENC_FLOAT | ||
| 460 | 610 | scale_coefficients(s); | |
| 461 | #endif | ||
| 462 | 1432 | } | |
| 463 |