| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * AAC encoder | ||
| 3 | * Copyright (C) 2008 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 | |||
| 22 | /** | ||
| 23 | * @file | ||
| 24 | * AAC encoder | ||
| 25 | */ | ||
| 26 | |||
| 27 | /*********************************** | ||
| 28 | * TODOs: | ||
| 29 | * add sane pulse detection | ||
| 30 | ***********************************/ | ||
| 31 | #include <float.h> | ||
| 32 | #include <math.h> | ||
| 33 | |||
| 34 | #include "libavutil/channel_layout.h" | ||
| 35 | #include "libavutil/crc.h" | ||
| 36 | #include "libavutil/float_dsp.h" | ||
| 37 | #include "libavutil/mem.h" | ||
| 38 | #include "libavutil/opt.h" | ||
| 39 | #include "avcodec.h" | ||
| 40 | #include "codec_internal.h" | ||
| 41 | #include "encode.h" | ||
| 42 | #include "put_bits.h" | ||
| 43 | #include "mpeg4audio.h" | ||
| 44 | #include "sinewin.h" | ||
| 45 | #include "profiles.h" | ||
| 46 | #include "version.h" | ||
| 47 | |||
| 48 | #include "aac.h" | ||
| 49 | #include "aactab.h" | ||
| 50 | #include "aacenc.h" | ||
| 51 | #include "aacenctab.h" | ||
| 52 | #include "aacenc_utils.h" | ||
| 53 | |||
| 54 | #include "psymodel.h" | ||
| 55 | |||
| 56 | /** | ||
| 57 | * List of PCE (Program Configuration Element) for the channel layouts listed | ||
| 58 | * in channel_layout.h | ||
| 59 | * | ||
| 60 | * For those wishing in the future to add other layouts: | ||
| 61 | * | ||
| 62 | * - num_ele: number of elements in each group of front, side, back, lfe channels | ||
| 63 | * (an element is of type SCE (single channel), CPE (channel pair) for | ||
| 64 | * the first 3 groups; and is LFE for LFE group). | ||
| 65 | * | ||
| 66 | * - pairing: 0 for an SCE element or 1 for a CPE; does not apply to LFE group | ||
| 67 | * | ||
| 68 | * - index: there are three independent indices for SCE, CPE and LFE; | ||
| 69 | * they are incremented irrespective of the group to which the element belongs; | ||
| 70 | * they are not reset when going from one group to another | ||
| 71 | * | ||
| 72 | * Example: for 7.0 channel layout, | ||
| 73 | * .pairing = { { 1, 0 }, { 1 }, { 1 }, }, (3 CPE and 1 SCE in front group) | ||
| 74 | * .index = { { 0, 0 }, { 1 }, { 2 }, }, | ||
| 75 | * (index is 0 for the single SCE but goes from 0 to 2 for the CPEs) | ||
| 76 | * | ||
| 77 | * The index order impacts the channel ordering. But is otherwise arbitrary | ||
| 78 | * (the sequence could have been 2, 0, 1 instead of 0, 1, 2). | ||
| 79 | * | ||
| 80 | * Spec allows for discontinuous indices, e.g. if one has a total of two SCE, | ||
| 81 | * SCE.0 SCE.15 is OK per spec; BUT it won't be decoded by our AAC decoder | ||
| 82 | * which at this time requires that indices fully cover some range starting | ||
| 83 | * from 0 (SCE.1 SCE.0 is OK but not SCE.0 SCE.15). | ||
| 84 | * | ||
| 85 | * - height: 0 for a base layer element, 1 for a top layer element, 2 for a bottom | ||
| 86 | * layer element. | ||
| 87 | * | ||
| 88 | * - config_map: total number of elements and their types. Beware, the way the | ||
| 89 | * types are ordered impacts the final channel ordering. | ||
| 90 | * | ||
| 91 | * - reorder_map: reorders the channels. | ||
| 92 | * | ||
| 93 | */ | ||
| 94 | static const AACPCEInfo aac_pce_configs[] = { | ||
| 95 | { | ||
| 96 | .layout = AV_CHANNEL_LAYOUT_MONO, | ||
| 97 | .num_ele = { 1, 0, 0, 0 }, | ||
| 98 | .pairing = { { 0 }, }, | ||
| 99 | .index = { { 0 }, }, | ||
| 100 | .config_map = { 1, TYPE_SCE, }, | ||
| 101 | .reorder_map = { 0 }, | ||
| 102 | }, | ||
| 103 | { | ||
| 104 | .layout = AV_CHANNEL_LAYOUT_STEREO, | ||
| 105 | .num_ele = { 1, 0, 0, 0 }, | ||
| 106 | .pairing = { { 1 }, }, | ||
| 107 | .index = { { 0 }, }, | ||
| 108 | .config_map = { 1, TYPE_CPE, }, | ||
| 109 | .reorder_map = { 0, 1 }, | ||
| 110 | }, | ||
| 111 | { | ||
| 112 | .layout = AV_CHANNEL_LAYOUT_2POINT1, | ||
| 113 | .num_ele = { 1, 0, 0, 1 }, | ||
| 114 | .pairing = { { 1 }, }, | ||
| 115 | .index = { { 0 },{ 0 },{ 0 },{ 0 } }, | ||
| 116 | .config_map = { 2, TYPE_CPE, TYPE_LFE }, | ||
| 117 | .reorder_map = { 0, 1, 2 }, | ||
| 118 | }, | ||
| 119 | { | ||
| 120 | .layout = AV_CHANNEL_LAYOUT_2_1, | ||
| 121 | .num_ele = { 1, 0, 1, 0 }, | ||
| 122 | .pairing = { { 1 },{ 0 },{ 0 } }, | ||
| 123 | .index = { { 0 },{ 0 },{ 0 }, }, | ||
| 124 | .config_map = { 2, TYPE_CPE, TYPE_SCE }, | ||
| 125 | .reorder_map = { 0, 1, 2 }, | ||
| 126 | }, | ||
| 127 | { | ||
| 128 | .layout = AV_CHANNEL_LAYOUT_SURROUND, | ||
| 129 | .num_ele = { 2, 0, 0, 0 }, | ||
| 130 | .pairing = { { 0, 1 }, }, | ||
| 131 | .index = { { 0, 0 }, }, | ||
| 132 | .config_map = { 2, TYPE_SCE, TYPE_CPE }, | ||
| 133 | .reorder_map = { 2, 0, 1 }, | ||
| 134 | }, | ||
| 135 | { | ||
| 136 | .layout = AV_CHANNEL_LAYOUT_3POINT1, | ||
| 137 | .num_ele = { 2, 0, 0, 1 }, | ||
| 138 | .pairing = { { 0, 1 }, }, | ||
| 139 | .index = { { 0, 0 }, { 0 }, { 0 }, { 0 }, }, | ||
| 140 | .config_map = { 3, TYPE_SCE, TYPE_CPE, TYPE_LFE }, | ||
| 141 | .reorder_map = { 2, 0, 1, 3 }, | ||
| 142 | }, | ||
| 143 | { | ||
| 144 | .layout = AV_CHANNEL_LAYOUT_4POINT0, | ||
| 145 | .num_ele = { 2, 0, 1, 0 }, | ||
| 146 | .pairing = { { 0, 1 }, { 0 }, { 0 }, }, | ||
| 147 | .index = { { 0, 0 }, { 0 }, { 1 } }, | ||
| 148 | .config_map = { 3, TYPE_SCE, TYPE_CPE, TYPE_SCE }, | ||
| 149 | .reorder_map = { 2, 0, 1, 3 }, | ||
| 150 | }, | ||
| 151 | { | ||
| 152 | .layout = AV_CHANNEL_LAYOUT_4POINT1, | ||
| 153 | .num_ele = { 2, 0, 1, 1 }, | ||
| 154 | .pairing = { { 0, 1 }, { 0 }, { 0 }, }, | ||
| 155 | .index = { { 0, 0 }, { 0 }, { 1 }, { 0 } }, | ||
| 156 | .config_map = { 4, TYPE_SCE, TYPE_CPE, TYPE_SCE, TYPE_LFE }, | ||
| 157 | .reorder_map = { 2, 0, 1, 4, 3 }, | ||
| 158 | }, | ||
| 159 | { | ||
| 160 | .layout = AV_CHANNEL_LAYOUT_2_2, | ||
| 161 | .num_ele = { 1, 0, 1, 0 }, | ||
| 162 | .pairing = { { 1 }, { 0 }, { 1 }, }, | ||
| 163 | .index = { { 0 }, { 0 }, { 1 } }, | ||
| 164 | .config_map = { 2, TYPE_CPE, TYPE_CPE }, | ||
| 165 | .reorder_map = { 0, 1, 2, 3 }, | ||
| 166 | }, | ||
| 167 | { | ||
| 168 | .layout = AV_CHANNEL_LAYOUT_QUAD, | ||
| 169 | .num_ele = { 1, 0, 1, 0 }, | ||
| 170 | .pairing = { { 1 }, { 0 }, { 1 }, }, | ||
| 171 | .index = { { 0 }, { 0 }, { 1 } }, | ||
| 172 | .config_map = { 2, TYPE_CPE, TYPE_CPE }, | ||
| 173 | .reorder_map = { 0, 1, 2, 3 }, | ||
| 174 | }, | ||
| 175 | { | ||
| 176 | .layout = AV_CHANNEL_LAYOUT_5POINT0, | ||
| 177 | .num_ele = { 2, 0, 1, 0 }, | ||
| 178 | .pairing = { { 0, 1 }, { 0 }, { 1 } }, | ||
| 179 | .index = { { 0, 0 }, { 0 }, { 1 } }, | ||
| 180 | .config_map = { 3, TYPE_SCE, TYPE_CPE, TYPE_CPE }, | ||
| 181 | .reorder_map = { 2, 0, 1, 3, 4 }, | ||
| 182 | }, | ||
| 183 | { | ||
| 184 | .layout = AV_CHANNEL_LAYOUT_5POINT1, | ||
| 185 | .num_ele = { 2, 0, 1, 1 }, | ||
| 186 | .pairing = { { 0, 1 }, { 0 }, { 1 }, }, | ||
| 187 | .index = { { 0, 0 }, { 0 }, { 1 }, { 0 } }, | ||
| 188 | .config_map = { 4, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE }, | ||
| 189 | .reorder_map = { 2, 0, 1, 4, 5, 3 }, | ||
| 190 | }, | ||
| 191 | { | ||
| 192 | .layout = AV_CHANNEL_LAYOUT_5POINT0_BACK, | ||
| 193 | .num_ele = { 2, 0, 1, 0 }, | ||
| 194 | .pairing = { { 0, 1 }, { 0 }, { 1 } }, | ||
| 195 | .index = { { 0, 0 }, { 0 }, { 1 } }, | ||
| 196 | .config_map = { 3, TYPE_SCE, TYPE_CPE, TYPE_CPE }, | ||
| 197 | .reorder_map = { 2, 0, 1, 3, 4 }, | ||
| 198 | }, | ||
| 199 | { | ||
| 200 | .layout = AV_CHANNEL_LAYOUT_5POINT1_BACK, | ||
| 201 | .num_ele = { 2, 0, 1, 1 }, | ||
| 202 | .pairing = { { 0, 1 }, { 0 }, { 1 }, }, | ||
| 203 | .index = { { 0, 0 }, { 0 }, { 1 }, { 0 } }, | ||
| 204 | .config_map = { 4, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE }, | ||
| 205 | .reorder_map = { 2, 0, 1, 4, 5, 3 }, | ||
| 206 | }, | ||
| 207 | { | ||
| 208 | .layout = AV_CHANNEL_LAYOUT_6POINT0, | ||
| 209 | .num_ele = { 2, 0, 2, 0 }, | ||
| 210 | .pairing = { { 0, 1 }, { 0 }, { 1, 0 } }, | ||
| 211 | .index = { { 0, 0 }, { 0 }, { 1, 1 } }, | ||
| 212 | .config_map = { 4, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_SCE }, | ||
| 213 | .reorder_map = { 2, 0, 1, 4, 5, 3 }, | ||
| 214 | }, | ||
| 215 | { | ||
| 216 | .layout = AV_CHANNEL_LAYOUT_6POINT0_FRONT, | ||
| 217 | .num_ele = { 2, 0, 1, 0 }, | ||
| 218 | .pairing = { { 1, 1 }, { 0 }, { 1 } }, | ||
| 219 | .index = { { 0, 1 }, { 0 }, { 2 }, }, | ||
| 220 | .config_map = { 3, TYPE_CPE, TYPE_CPE, TYPE_CPE, }, | ||
| 221 | .reorder_map = { 2, 3, 0, 1, 4, 5 }, | ||
| 222 | }, | ||
| 223 | { | ||
| 224 | .layout = AV_CHANNEL_LAYOUT_HEXAGONAL, | ||
| 225 | .num_ele = { 2, 0, 2, 0 }, | ||
| 226 | .pairing = { { 0, 1 }, { 0 }, { 1, 0 } }, | ||
| 227 | .index = { { 0, 0 }, { 0 }, { 1, 1 } }, | ||
| 228 | .config_map = { 4, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_SCE }, | ||
| 229 | .reorder_map = { 2, 0, 1, 3, 4, 5 }, | ||
| 230 | }, | ||
| 231 | { | ||
| 232 | .layout = AV_CHANNEL_LAYOUT_6POINT1, | ||
| 233 | .num_ele = { 2, 0, 2, 1 }, | ||
| 234 | .pairing = { { 0, 1 }, { 0 }, { 1, 0 }, }, | ||
| 235 | .index = { { 0, 0 }, { 0 }, { 1, 1 }, { 0 } }, | ||
| 236 | .config_map = { 5, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_SCE, TYPE_LFE }, | ||
| 237 | .reorder_map = { 2, 0, 1, 5, 6, 4, 3 }, | ||
| 238 | }, | ||
| 239 | { | ||
| 240 | .layout = AV_CHANNEL_LAYOUT_6POINT1_BACK, | ||
| 241 | .num_ele = { 2, 0, 2, 1 }, | ||
| 242 | .pairing = { { 0, 1 },{ 0 },{ 1, 0 }, }, | ||
| 243 | .index = { { 0, 0 },{ 0 },{ 1, 1 },{ 0 } }, | ||
| 244 | .config_map = { 5, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_SCE, TYPE_LFE }, | ||
| 245 | .reorder_map = { 2, 0, 1, 4, 5, 6, 3 }, | ||
| 246 | }, | ||
| 247 | { | ||
| 248 | .layout = AV_CHANNEL_LAYOUT_6POINT1_FRONT, | ||
| 249 | .num_ele = { 2, 0, 1, 1 }, | ||
| 250 | .pairing = { { 1, 1 }, { 0 }, { 1 }, }, | ||
| 251 | .index = { { 0, 1 }, { 0 }, { 2 }, { 0 }, }, | ||
| 252 | .config_map = { 4, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_LFE, }, | ||
| 253 | .reorder_map = { 3, 4, 0, 1, 5, 6, 2 }, | ||
| 254 | }, | ||
| 255 | { | ||
| 256 | .layout = AV_CHANNEL_LAYOUT_7POINT0, | ||
| 257 | .num_ele = { 2, 0, 2, 0 }, | ||
| 258 | .pairing = { { 0, 1 }, { 0 }, { 1, 1 }, }, | ||
| 259 | .index = { { 0, 0 }, { 0 }, { 2, 1 }, }, | ||
| 260 | .config_map = { 4, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE }, | ||
| 261 | .reorder_map = { 2, 0, 1, 3, 4, 5, 6 }, | ||
| 262 | }, | ||
| 263 | { | ||
| 264 | .layout = AV_CHANNEL_LAYOUT_7POINT0_FRONT, | ||
| 265 | .num_ele = { 3, 0, 1, 0 }, | ||
| 266 | .pairing = { { 0, 1, 1 }, { 0 }, { 1 }, }, | ||
| 267 | .index = { { 0, 0, 1 }, { 0 }, { 2 }, }, | ||
| 268 | .config_map = { 4, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE }, | ||
| 269 | .reorder_map = { 2, 3, 4, 0, 1, 5, 6 }, | ||
| 270 | }, | ||
| 271 | { | ||
| 272 | .layout = AV_CHANNEL_LAYOUT_7POINT1, | ||
| 273 | .num_ele = { 2, 0, 2, 1 }, | ||
| 274 | .pairing = { { 0, 1 }, { 0 }, { 1, 1 }, }, | ||
| 275 | .index = { { 0, 0 }, { 0 }, { 2, 1 }, { 0 } }, | ||
| 276 | .config_map = { 5, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_LFE }, | ||
| 277 | .reorder_map = { 2, 0, 1, 4, 5, 6, 7, 3 }, | ||
| 278 | }, | ||
| 279 | { | ||
| 280 | .layout = AV_CHANNEL_LAYOUT_7POINT1_WIDE, | ||
| 281 | .num_ele = { 3, 0, 1, 1 }, | ||
| 282 | .pairing = { { 0, 1, 1 }, { 0 }, { 1 }, }, | ||
| 283 | .index = { { 0, 0, 1 }, { 0 }, { 2 }, { 0 }, }, | ||
| 284 | .config_map = { 5, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_LFE }, | ||
| 285 | .reorder_map = { 2, 4, 5, 0, 1, 6, 7, 3 }, | ||
| 286 | }, | ||
| 287 | { | ||
| 288 | .layout = AV_CHANNEL_LAYOUT_7POINT1_WIDE_BACK, | ||
| 289 | .num_ele = { 3, 0, 1, 1 }, | ||
| 290 | .pairing = { { 0, 1, 1 }, { 0 }, { 1 } }, | ||
| 291 | .index = { { 0, 0, 1 }, { 0 }, { 2 }, { 0 } }, | ||
| 292 | .config_map = { 5, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_LFE }, | ||
| 293 | .reorder_map = { 2, 6, 7, 0, 1, 4, 5, 3 }, | ||
| 294 | }, | ||
| 295 | { | ||
| 296 | .layout = AV_CHANNEL_LAYOUT_OCTAGONAL, | ||
| 297 | .num_ele = { 2, 0, 3, 0 }, | ||
| 298 | .pairing = { { 0, 1 }, { 0 }, { 1, 1, 0 }, }, | ||
| 299 | .index = { { 0, 0 }, { 0 }, { 1, 2, 1 }, }, | ||
| 300 | .config_map = { 5, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_SCE }, | ||
| 301 | .reorder_map = { 2, 0, 1, 6, 7, 3, 4, 5 }, | ||
| 302 | }, | ||
| 303 | { | ||
| 304 | .layout = AV_CHANNEL_LAYOUT_5POINT1POINT2, | ||
| 305 | .num_ele = { 3, 0, 1, 1 }, | ||
| 306 | .pairing = { { 0, 1, 1 }, { 0 }, { 1 }, }, | ||
| 307 | .index = { { 0, 0, 2 }, { 0 }, { 1 }, { 0 }, }, | ||
| 308 | .height = { { 0, 0, 1 }, { 0 }, { 0 } }, | ||
| 309 | .config_map = { 5, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE, TYPE_CPE }, | ||
| 310 | .reorder_map = { 2, 0, 1, 4, 5, 3, 6, 7 }, | ||
| 311 | }, | ||
| 312 | { | ||
| 313 | // ITU-R BS.2051-3 Sound System C | ||
| 314 | .layout = AV_CHANNEL_LAYOUT_5POINT1POINT2_BACK, | ||
| 315 | .num_ele = { 3, 0, 1, 1 }, | ||
| 316 | .pairing = { { 0, 1, 1 }, { 0 }, { 1 }, }, | ||
| 317 | .index = { { 0, 0, 2 }, { 0 }, { 1 }, { 0 }, }, | ||
| 318 | .height = { { 0, 0, 1 }, { 0 }, { 0 } }, | ||
| 319 | .config_map = { 5, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE, TYPE_CPE }, | ||
| 320 | .reorder_map = { 2, 0, 1, 4, 5, 3, 6, 7 }, | ||
| 321 | }, | ||
| 322 | { | ||
| 323 | .layout = AV_CHANNEL_LAYOUT_5POINT1POINT4, | ||
| 324 | .num_ele = { 3, 0, 2, 1 }, | ||
| 325 | .pairing = { { 0, 1, 1 }, { 0 }, { 1, 1 }, }, | ||
| 326 | .index = { { 0, 0, 2 }, { 0 }, { 1, 3 }, { 0 }, }, | ||
| 327 | .height = { { 0, 0, 1 }, { 0 }, { 0, 1 } }, | ||
| 328 | .config_map = { 6, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE, TYPE_CPE, TYPE_CPE }, | ||
| 329 | .reorder_map = { 2, 0, 1, 4, 5, 3, 6, 7, 8, 9 }, | ||
| 330 | }, | ||
| 331 | // ITU-R BS.2051-3 Sound System D | ||
| 332 | { | ||
| 333 | .layout = { | ||
| 334 | .nb_channels = 10, | ||
| 335 | .order = AV_CHANNEL_ORDER_NATIVE, | ||
| 336 | .u.mask = AV_CH_LAYOUT_5POINT1POINT2_BACK | AV_CH_TOP_BACK_LEFT | AV_CH_TOP_BACK_RIGHT, | ||
| 337 | }, | ||
| 338 | .num_ele = { 3, 0, 2, 1 }, | ||
| 339 | .pairing = { { 0, 1, 1 }, { 0 }, { 1, 1 }, }, | ||
| 340 | .index = { { 0, 0, 2 }, { 0 }, { 1, 3 }, { 0 }, }, | ||
| 341 | .height = { { 0, 0, 1 }, { 0 }, { 0, 1 } }, | ||
| 342 | .config_map = { 6, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE, TYPE_CPE, TYPE_CPE }, | ||
| 343 | .reorder_map = { 2, 0, 1, 4, 5, 3, 6, 7, 8, 9 }, | ||
| 344 | }, | ||
| 345 | { | ||
| 346 | // ITU-R BS.2051-3 Sound System E | ||
| 347 | .layout = { | ||
| 348 | .nb_channels = 11, | ||
| 349 | .order = AV_CHANNEL_ORDER_NATIVE, | ||
| 350 | .u.mask = AV_CH_LAYOUT_5POINT1POINT4 | AV_CH_BOTTOM_FRONT_CENTER, | ||
| 351 | }, | ||
| 352 | .num_ele = { 4, 0, 2, 1 }, | ||
| 353 | .pairing = { { 0, 1, 1, 0 }, { 0 }, { 1, 1 }, }, | ||
| 354 | .index = { { 0, 0, 2, 1 }, { 0 }, { 1, 3 }, { 0 }, }, | ||
| 355 | .height = { { 0, 0, 1, 2 }, { 0 }, { 0, 1 } }, | ||
| 356 | .config_map = { 7, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE, TYPE_CPE, TYPE_CPE, TYPE_SCE }, | ||
| 357 | .reorder_map = { 2, 0, 1, 4, 5, 3, 6, 7, 8, 9, 10 }, | ||
| 358 | }, | ||
| 359 | { | ||
| 360 | .layout = AV_CHANNEL_LAYOUT_5POINT1POINT6, | ||
| 361 | .num_ele = { 4, 1, 2, 1 }, | ||
| 362 | .pairing = { { 0, 1, 0, 1 }, { 0 }, { 1, 1 }, }, | ||
| 363 | .index = { { 0, 0, 1, 2 }, { 2 }, { 1, 3 }, { 0 }, }, | ||
| 364 | .height = { { 0, 0, 1, 1 }, { 1 }, { 0, 1 } }, | ||
| 365 | .config_map = { 8, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE, TYPE_SCE, TYPE_CPE, TYPE_SCE, TYPE_CPE }, | ||
| 366 | .reorder_map = { 2, 0, 1, 4, 5, 3, 8, 7, 9, 6, 10, 11 }, | ||
| 367 | }, | ||
| 368 | { | ||
| 369 | .layout = { | ||
| 370 | .nb_channels = 12, | ||
| 371 | .order = AV_CHANNEL_ORDER_NATIVE, | ||
| 372 | .u.mask = AV_CH_LAYOUT_5POINT1POINT2_BACK | AV_CH_TOP_BACK_LEFT | AV_CH_TOP_BACK_RIGHT | | ||
| 373 | AV_CH_TOP_CENTER | AV_CH_TOP_FRONT_CENTER, | ||
| 374 | }, | ||
| 375 | .num_ele = { 4, 1, 2, 1 }, | ||
| 376 | .pairing = { { 0, 1, 0, 1 }, { 0 }, { 1, 1 }, }, | ||
| 377 | .index = { { 0, 0, 1, 2 }, { 2 }, { 1, 3 }, { 0 }, }, | ||
| 378 | .height = { { 0, 0, 1, 1 }, { 1 }, { 0, 1 } }, | ||
| 379 | .config_map = { 8, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE, TYPE_SCE, TYPE_CPE, TYPE_SCE, TYPE_CPE }, | ||
| 380 | .reorder_map = { 2, 0, 1, 4, 5, 3, 8, 7, 9, 6, 10, 11 }, | ||
| 381 | }, | ||
| 382 | { | ||
| 383 | .layout = AV_CHANNEL_LAYOUT_7POINT1POINT2, | ||
| 384 | .num_ele = { 3, 0, 2, 1 }, | ||
| 385 | .pairing = { { 0, 1, 1 }, { 0 }, { 1, 1 }, }, | ||
| 386 | .index = { { 0, 0, 3 }, { 0 }, { 2, 1 }, { 0 } }, | ||
| 387 | .height = { { 0, 0, 1 }, { 0 }, { 0, 0 } }, | ||
| 388 | .config_map = { 6, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_LFE, TYPE_CPE }, | ||
| 389 | .reorder_map = { 2, 0, 1, 4, 5, 6, 7, 3, 8, 9 }, | ||
| 390 | }, | ||
| 391 | { | ||
| 392 | // ITU-R BS.2051-3 Sound System F | ||
| 393 | .layout = AV_CHANNEL_LAYOUT_7POINT2POINT3, | ||
| 394 | .num_ele = { 3, 0, 3, 2 }, | ||
| 395 | .pairing = { { 0, 1, 1 }, { 0 }, { 1, 1, 0 }, }, | ||
| 396 | .index = { { 0, 0, 3 }, { 0 }, { 2, 1, 1 }, { 0, 1 } }, | ||
| 397 | .height = { { 0, 0, 1 }, { 0 }, { 0, 0, 1 } }, | ||
| 398 | .config_map = { 8, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_LFE, TYPE_LFE, TYPE_CPE, TYPE_SCE }, | ||
| 399 | .reorder_map = { 2, 0, 1, 4, 5, 6, 7, 3, 11, 8, 9, 10 }, | ||
| 400 | }, | ||
| 401 | { | ||
| 402 | // ITU-R BS.2051-3 Sound System J | ||
| 403 | .layout = AV_CHANNEL_LAYOUT_7POINT1POINT4, | ||
| 404 | .num_ele = { 3, 0, 3, 1 }, | ||
| 405 | .pairing = { { 0, 1, 1 }, { 0 }, { 1, 1, 1 }, }, | ||
| 406 | .index = { { 0, 0, 3 }, { 0 }, { 2, 1, 4 }, { 0 } }, | ||
| 407 | .height = { { 0, 0, 1 }, { 0 }, { 0, 0, 1 } }, | ||
| 408 | .config_map = { 7, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_LFE, TYPE_CPE, TYPE_CPE }, | ||
| 409 | .reorder_map = { 2, 0, 1, 4, 5, 6, 7, 3, 8, 9, 10, 11 }, | ||
| 410 | }, | ||
| 411 | { | ||
| 412 | .layout = AV_CHANNEL_LAYOUT_7POINT1POINT6, | ||
| 413 | .num_ele = { 4, 1, 3, 1 }, | ||
| 414 | .pairing = { { 0, 1, 0, 1 }, { 0 }, { 1, 1, 1 }, }, | ||
| 415 | .index = { { 0, 0, 1, 3 }, { 2 }, { 2, 1, 4 }, { 0 } }, | ||
| 416 | .height = { { 0, 0, 1, 1 }, { 1 }, { 0, 0, 1 } }, | ||
| 417 | .config_map = { 9, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_LFE, TYPE_SCE, TYPE_CPE, TYPE_SCE, TYPE_CPE }, | ||
| 418 | .reorder_map = { 2, 0, 1, 4, 5, 6, 7, 3, 10, 9, 11, 8, 12, 13 }, | ||
| 419 | }, | ||
| 420 | { | ||
| 421 | // ITU-R BS.2051-3 Sound System G | ||
| 422 | .layout = AV_CHANNEL_LAYOUT_9POINT1POINT4, | ||
| 423 | .num_ele = { 4, 0, 3, 1 }, | ||
| 424 | .pairing = { { 0, 1, 1, 1 }, { 0 }, { 1, 1, 1 }, }, | ||
| 425 | .index = { { 0, 0, 1, 4 }, { 0 }, { 2, 3, 5 }, { 0 } }, | ||
| 426 | .height = { { 0, 0, 0, 1 }, { 0 }, { 0, 0, 1 } }, | ||
| 427 | .config_map = { 8, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_LFE, TYPE_CPE, TYPE_CPE }, | ||
| 428 | .reorder_map = { 2, 6, 7, 0, 1, 8, 9, 4, 5, 3, 10, 11, 12, 13 }, | ||
| 429 | }, | ||
| 430 | { | ||
| 431 | .layout = AV_CHANNEL_LAYOUT_9POINT1POINT6, | ||
| 432 | .num_ele = { 4, 1, 3, 1 }, | ||
| 433 | .pairing = { { 0, 1, 1, 1 }, { 1 }, { 1, 1, 1 }, }, | ||
| 434 | .index = { { 0, 0, 1, 4 }, { 5 }, { 2, 3, 6 }, { 0 } }, | ||
| 435 | .height = { { 0, 0, 0, 1 }, { 1 }, { 0, 0, 1 } }, | ||
| 436 | .config_map = { 9, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_LFE, TYPE_CPE, TYPE_CPE, TYPE_CPE }, | ||
| 437 | .reorder_map = { 2, 6, 7, 0, 1, 8, 9, 4, 5, 3, 10, 11, 14, 15, 12, 13 }, | ||
| 438 | }, | ||
| 439 | { | ||
| 440 | .layout = AV_CHANNEL_LAYOUT_AMBISONIC_FIRST_ORDER, | ||
| 441 | .num_ele = { 1, 0, 1, 0 }, | ||
| 442 | .pairing = { { 1 }, { 0 }, { 1 }, }, | ||
| 443 | .index = { { 0 }, { 0 }, { 1 } }, | ||
| 444 | .config_map = { 2, TYPE_CPE, TYPE_CPE }, | ||
| 445 | .reorder_map = { 0, 1, 2, 3 }, | ||
| 446 | }, | ||
| 447 | { | ||
| 448 | .layout = { .order = AV_CHANNEL_ORDER_AMBISONIC, .nb_channels = 9 }, | ||
| 449 | .num_ele = { 3, 0, 2, 0 }, | ||
| 450 | .pairing = { { 0, 1, 1 }, { 0 }, { 1, 1 }, }, | ||
| 451 | .index = { { 0, 0, 1 }, { 0 }, { 2, 3 }, }, | ||
| 452 | .config_map = { 5, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_CPE }, | ||
| 453 | .reorder_map = { 2, 5, 6, 0, 1, 7, 8, 3, 4 }, | ||
| 454 | }, | ||
| 455 | { | ||
| 456 | .layout = { .order = AV_CHANNEL_ORDER_AMBISONIC, .nb_channels = 16 }, | ||
| 457 | .num_ele = { 4, 0, 4, 0 }, | ||
| 458 | .pairing = { { 1, 1, 1, 1 }, { 0 }, { 1, 1, 1, 1 }, }, | ||
| 459 | .index = { { 0, 1, 2, 3 }, { 0 }, { 4, 5, 6, 7 }, }, | ||
| 460 | .config_map = { 8, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_CPE }, | ||
| 461 | .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, | ||
| 462 | }, | ||
| 463 | }; | ||
| 464 | |||
| 465 | 9 | static void put_pce(PutBitContext *pb, AVCodecContext *avctx) | |
| 466 | { | ||
| 467 | int i, j; | ||
| 468 | 9 | AACEncContext *s = avctx->priv_data; | |
| 469 | 9 | AACPCEInfo *pce = &s->pce; | |
| 470 | 9 | const int bitexact = avctx->flags & AV_CODEC_FLAG_BITEXACT; | |
| 471 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | const char *aux_data = bitexact ? "Lavc" : LIBAVCODEC_IDENT; |
| 472 | |||
| 473 | 9 | put_bits(pb, 4, 0); | |
| 474 | |||
| 475 | 9 | put_bits(pb, 2, avctx->profile); | |
| 476 | 9 | put_bits(pb, 4, s->samplerate_index); | |
| 477 | |||
| 478 | 9 | put_bits(pb, 4, pce->num_ele[0]); /* Front */ | |
| 479 | 9 | put_bits(pb, 4, pce->num_ele[1]); /* Side */ | |
| 480 | 9 | put_bits(pb, 4, pce->num_ele[2]); /* Back */ | |
| 481 | 9 | put_bits(pb, 2, pce->num_ele[3]); /* LFE */ | |
| 482 | 9 | put_bits(pb, 3, 0); /* Assoc data */ | |
| 483 | 9 | put_bits(pb, 4, 0); /* CCs */ | |
| 484 | |||
| 485 | 9 | put_bits(pb, 1, 0); /* Stereo mixdown */ | |
| 486 | 9 | put_bits(pb, 1, 0); /* Mono mixdown */ | |
| 487 | 9 | put_bits(pb, 1, 0); /* Something else */ | |
| 488 | |||
| 489 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 9 times.
|
45 | for (i = 0; i < 4; i++) { |
| 490 |
2/2✓ Branch 0 taken 66 times.
✓ Branch 1 taken 36 times.
|
102 | for (j = 0; j < pce->num_ele[i]; j++) { |
| 491 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 10 times.
|
66 | if (i < 3) |
| 492 | 56 | put_bits(pb, 1, pce->pairing[i][j]); | |
| 493 | 66 | put_bits(pb, 4, pce->index[i][j]); | |
| 494 | } | ||
| 495 | } | ||
| 496 | |||
| 497 | 9 | align_put_bits(pb); | |
| 498 |
1/2✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
|
9 | if (s->needs_height_ext) { |
| 499 | 9 | const AVCRC *crc_ctx = av_crc_get_table(AV_CRC_8_ATM); | |
| 500 | PutBitContext height_pb; | ||
| 501 | uint8_t buf[16]; | ||
| 502 | 9 | int bits = 8 + pce->num_ele[0] * 2 + pce->num_ele[1] * 2 + pce->num_ele[2] * 2; | |
| 503 | 9 | int bytes = (bits + 7) / 8; | |
| 504 | |||
| 505 | 9 | init_put_bits(&height_pb, buf, bytes); | |
| 506 | 9 | put_bits(&height_pb, 8, 0xAC); | |
| 507 |
2/2✓ Branch 0 taken 27 times.
✓ Branch 1 taken 9 times.
|
36 | for (i = 0; i < 3; i++) |
| 508 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 27 times.
|
83 | for (j = 0; j < pce->num_ele[i]; j++) |
| 509 | 56 | put_bits(&height_pb, 2, pce->height[i][j]); | |
| 510 | 9 | flush_put_bits(&height_pb); | |
| 511 | |||
| 512 | 9 | put_bits(pb, 8, bytes + 1); | |
| 513 | 9 | ff_copy_bits(pb, buf, bits); | |
| 514 | 9 | align_put_bits(pb); | |
| 515 | 9 | put_bits(pb, 8, av_crc(crc_ctx, 0xFF, buf, bytes)); | |
| 516 | } else { | ||
| 517 | ✗ | put_bits(pb, 8, strlen(aux_data)); | |
| 518 | ✗ | ff_put_string(pb, aux_data, 0); | |
| 519 | } | ||
| 520 | 9 | } | |
| 521 | |||
| 522 | /** | ||
| 523 | * Make AAC audio config object. | ||
| 524 | * @see 1.6.2.1 "Syntax - AudioSpecificConfig" | ||
| 525 | */ | ||
| 526 | 27 | static int put_audio_specific_config(AVCodecContext *avctx, int chcfg) | |
| 527 | { | ||
| 528 | PutBitContext pb; | ||
| 529 | 27 | AACEncContext *s = avctx->priv_data; | |
| 530 | 27 | const int max_size = 32; | |
| 531 | |||
| 532 | 27 | avctx->extradata = av_mallocz(max_size); | |
| 533 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 | if (!avctx->extradata) |
| 534 | ✗ | return AVERROR(ENOMEM); | |
| 535 | |||
| 536 | 27 | init_put_bits(&pb, avctx->extradata, max_size); | |
| 537 | 27 | put_bits(&pb, 5, s->profile+1); //profile | |
| 538 | 27 | put_bits(&pb, 4, s->samplerate_index); //sample rate index | |
| 539 | 27 | put_bits(&pb, 4, chcfg); | |
| 540 | //GASpecificConfig | ||
| 541 | 27 | put_bits(&pb, 1, 0); //frame length - 1024 samples | |
| 542 | 27 | put_bits(&pb, 1, 0); //does not depend on core coder | |
| 543 | 27 | put_bits(&pb, 1, 0); //is not extension | |
| 544 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 18 times.
|
27 | if (s->needs_pce) |
| 545 | 9 | put_pce(&pb, avctx); | |
| 546 | |||
| 547 | //Explicitly Mark SBR absent | ||
| 548 | 27 | put_bits(&pb, 11, 0x2b7); //sync extension | |
| 549 | 27 | put_bits(&pb, 5, AOT_SBR); | |
| 550 | 27 | put_bits(&pb, 1, 0); | |
| 551 | 27 | flush_put_bits(&pb); | |
| 552 | 27 | avctx->extradata_size = put_bytes_output(&pb); | |
| 553 | |||
| 554 | 27 | return 0; | |
| 555 | } | ||
| 556 | |||
| 557 | 14544 | void ff_quantize_band_cost_cache_init(struct AACEncContext *s) | |
| 558 | { | ||
| 559 | 14544 | ++s->quantize_band_cost_cache_generation; | |
| 560 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14544 times.
|
14544 | if (s->quantize_band_cost_cache_generation == 0) { |
| 561 | ✗ | memset(s->quantize_band_cost_cache, 0, sizeof(s->quantize_band_cost_cache)); | |
| 562 | ✗ | s->quantize_band_cost_cache_generation = 1; | |
| 563 | } | ||
| 564 | 14544 | } | |
| 565 | |||
| 566 | #define WINDOW_FUNC(type) \ | ||
| 567 | static void apply_ ##type ##_window(AVFloatDSPContext *fdsp, \ | ||
| 568 | SingleChannelElement *sce, \ | ||
| 569 | const float *audio) | ||
| 570 | |||
| 571 | 7785 | WINDOW_FUNC(only_long) | |
| 572 | { | ||
| 573 |
2/2✓ Branch 0 taken 7647 times.
✓ Branch 1 taken 138 times.
|
7785 | const float *lwindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024; |
| 574 |
2/2✓ Branch 0 taken 7632 times.
✓ Branch 1 taken 153 times.
|
7785 | const float *pwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024; |
| 575 | 7785 | float *out = sce->ret_buf; | |
| 576 | |||
| 577 | 7785 | fdsp->vector_fmul (out, audio, lwindow, 1024); | |
| 578 | 7785 | fdsp->vector_fmul_reverse(out + 1024, audio + 1024, pwindow, 1024); | |
| 579 | 7785 | } | |
| 580 | |||
| 581 | 424 | WINDOW_FUNC(long_start) | |
| 582 | { | ||
| 583 |
2/2✓ Branch 0 taken 303 times.
✓ Branch 1 taken 121 times.
|
424 | const float *lwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024; |
| 584 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 424 times.
|
424 | const float *swindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128; |
| 585 | 424 | float *out = sce->ret_buf; | |
| 586 | |||
| 587 | 424 | fdsp->vector_fmul(out, audio, lwindow, 1024); | |
| 588 | 424 | memcpy(out + 1024, audio + 1024, sizeof(out[0]) * 448); | |
| 589 | 424 | fdsp->vector_fmul_reverse(out + 1024 + 448, audio + 1024 + 448, swindow, 128); | |
| 590 | 424 | memset(out + 1024 + 576, 0, sizeof(out[0]) * 448); | |
| 591 | 424 | } | |
| 592 | |||
| 593 | 424 | WINDOW_FUNC(long_stop) | |
| 594 | { | ||
| 595 |
1/2✓ Branch 0 taken 424 times.
✗ Branch 1 not taken.
|
424 | const float *lwindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024; |
| 596 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 424 times.
|
424 | const float *swindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128; |
| 597 | 424 | float *out = sce->ret_buf; | |
| 598 | |||
| 599 | 424 | memset(out, 0, sizeof(out[0]) * 448); | |
| 600 | 424 | fdsp->vector_fmul(out + 448, audio + 448, swindow, 128); | |
| 601 | 424 | memcpy(out + 576, audio + 576, sizeof(out[0]) * 448); | |
| 602 | 424 | fdsp->vector_fmul_reverse(out + 1024, audio + 1024, lwindow, 1024); | |
| 603 | 424 | } | |
| 604 | |||
| 605 | 515 | WINDOW_FUNC(eight_short) | |
| 606 | { | ||
| 607 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 515 times.
|
515 | const float *swindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128; |
| 608 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 515 times.
|
515 | const float *pwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128; |
| 609 | 515 | const float *in = audio + 448; | |
| 610 | 515 | float *out = sce->ret_buf; | |
| 611 | int w; | ||
| 612 | |||
| 613 |
2/2✓ Branch 0 taken 4120 times.
✓ Branch 1 taken 515 times.
|
4635 | for (w = 0; w < 8; w++) { |
| 614 |
2/2✓ Branch 0 taken 3605 times.
✓ Branch 1 taken 515 times.
|
4120 | fdsp->vector_fmul (out, in, w ? pwindow : swindow, 128); |
| 615 | 4120 | out += 128; | |
| 616 | 4120 | in += 128; | |
| 617 | 4120 | fdsp->vector_fmul_reverse(out, in, swindow, 128); | |
| 618 | 4120 | out += 128; | |
| 619 | } | ||
| 620 | 515 | } | |
| 621 | |||
| 622 | static void (*const apply_window[4])(AVFloatDSPContext *fdsp, | ||
| 623 | SingleChannelElement *sce, | ||
| 624 | const float *audio) = { | ||
| 625 | [ONLY_LONG_SEQUENCE] = apply_only_long_window, | ||
| 626 | [LONG_START_SEQUENCE] = apply_long_start_window, | ||
| 627 | [EIGHT_SHORT_SEQUENCE] = apply_eight_short_window, | ||
| 628 | [LONG_STOP_SEQUENCE] = apply_long_stop_window | ||
| 629 | }; | ||
| 630 | |||
| 631 | 9148 | static void apply_window_and_mdct(AACEncContext *s, SingleChannelElement *sce, | |
| 632 | float *audio) | ||
| 633 | { | ||
| 634 | int i; | ||
| 635 | 9148 | float *output = sce->ret_buf; | |
| 636 | |||
| 637 | 9148 | apply_window[sce->ics.window_sequence[0]](s->fdsp, sce, audio); | |
| 638 | |||
| 639 |
2/2✓ Branch 0 taken 8633 times.
✓ Branch 1 taken 515 times.
|
9148 | if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) |
| 640 | 8633 | s->mdct1024_fn(s->mdct1024, sce->coeffs, output, sizeof(float)); | |
| 641 | else | ||
| 642 |
2/2✓ Branch 0 taken 4120 times.
✓ Branch 1 taken 515 times.
|
4635 | for (i = 0; i < 1024; i += 128) |
| 643 | 4120 | s->mdct128_fn(s->mdct128, &sce->coeffs[i], output + i*2, sizeof(float)); | |
| 644 | 9148 | memcpy(audio, audio + 1024, sizeof(audio[0]) * 1024); | |
| 645 | 9148 | memcpy(sce->pcoeffs, sce->coeffs, sizeof(sce->pcoeffs)); | |
| 646 | 9148 | } | |
| 647 | |||
| 648 | /** | ||
| 649 | * Encode ics_info element. | ||
| 650 | * @see Table 4.6 (syntax of ics_info) | ||
| 651 | */ | ||
| 652 | 6122 | static void put_ics_info(AACEncContext *s, IndividualChannelStream *info) | |
| 653 | { | ||
| 654 | int w; | ||
| 655 | |||
| 656 | 6122 | put_bits(&s->pb, 1, 0); // ics_reserved bit | |
| 657 | 6122 | put_bits(&s->pb, 2, info->window_sequence[0]); | |
| 658 | 6122 | put_bits(&s->pb, 1, info->use_kb_window[0]); | |
| 659 |
2/2✓ Branch 0 taken 5851 times.
✓ Branch 1 taken 271 times.
|
6122 | if (info->window_sequence[0] != EIGHT_SHORT_SEQUENCE) { |
| 660 | 5851 | put_bits(&s->pb, 6, info->max_sfb); | |
| 661 | 5851 | put_bits(&s->pb, 1, 0); /* No predictor present */ | |
| 662 | } else { | ||
| 663 | 271 | put_bits(&s->pb, 4, info->max_sfb); | |
| 664 |
2/2✓ Branch 0 taken 1897 times.
✓ Branch 1 taken 271 times.
|
2168 | for (w = 1; w < 8; w++) |
| 665 | 1897 | put_bits(&s->pb, 1, !info->group_len[w]); | |
| 666 | } | ||
| 667 | 6122 | } | |
| 668 | |||
| 669 | /** | ||
| 670 | * Encode MS data. | ||
| 671 | * @see 4.6.8.1 "Joint Coding - M/S Stereo" | ||
| 672 | */ | ||
| 673 | 5252 | static void encode_ms_info(PutBitContext *pb, ChannelElement *cpe) | |
| 674 | { | ||
| 675 | int i, w; | ||
| 676 | |||
| 677 | 5252 | put_bits(pb, 2, cpe->ms_mode); | |
| 678 |
2/2✓ Branch 0 taken 1239 times.
✓ Branch 1 taken 4013 times.
|
5252 | if (cpe->ms_mode == 1) |
| 679 |
2/2✓ Branch 0 taken 1334 times.
✓ Branch 1 taken 1239 times.
|
2573 | for (w = 0; w < cpe->ch[0].ics.num_windows; w += cpe->ch[0].ics.group_len[w]) |
| 680 |
2/2✓ Branch 0 taken 56309 times.
✓ Branch 1 taken 1334 times.
|
57643 | for (i = 0; i < cpe->ch[0].ics.max_sfb; i++) |
| 681 | 56309 | put_bits(pb, 1, cpe->ms_mask[w*16 + i]); | |
| 682 | 5252 | } | |
| 683 | |||
| 684 | /** | ||
| 685 | * Produce integer coefficients from scalefactors provided by the model. | ||
| 686 | */ | ||
| 687 | 6122 | static void adjust_frame_information(ChannelElement *cpe, int chans) | |
| 688 | { | ||
| 689 | int i, w, w2, g, ch; | ||
| 690 | int maxsfb, cmaxsfb; | ||
| 691 | |||
| 692 |
2/2✓ Branch 0 taken 11374 times.
✓ Branch 1 taken 6122 times.
|
17496 | for (ch = 0; ch < chans; ch++) { |
| 693 | 11374 | IndividualChannelStream *ics = &cpe->ch[ch].ics; | |
| 694 | 11374 | maxsfb = 0; | |
| 695 | 11374 | cpe->ch[ch].pulse.num_pulse = 0; | |
| 696 |
2/2✓ Branch 0 taken 12089 times.
✓ Branch 1 taken 11374 times.
|
23463 | for (w = 0; w < ics->num_windows; w += ics->group_len[w]) { |
| 697 |
4/4✓ Branch 0 taken 31358 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 19297 times.
✓ Branch 3 taken 12061 times.
|
31386 | for (cmaxsfb = ics->num_swb; cmaxsfb > 0 && cpe->ch[ch].zeroes[w*16+cmaxsfb-1]; cmaxsfb--) |
| 698 | ; | ||
| 699 | 12089 | maxsfb = FFMAX(maxsfb, cmaxsfb); | |
| 700 | } | ||
| 701 | 11374 | ics->max_sfb = maxsfb; | |
| 702 | |||
| 703 | //adjust zero bands for window groups | ||
| 704 |
2/2✓ Branch 0 taken 12089 times.
✓ Branch 1 taken 11374 times.
|
23463 | for (w = 0; w < ics->num_windows; w += ics->group_len[w]) { |
| 705 |
2/2✓ Branch 0 taken 522474 times.
✓ Branch 1 taken 12089 times.
|
534563 | for (g = 0; g < ics->max_sfb; g++) { |
| 706 | 522474 | i = 1; | |
| 707 |
2/2✓ Branch 0 taken 522584 times.
✓ Branch 1 taken 4444 times.
|
527028 | for (w2 = w; w2 < w + ics->group_len[w]; w2++) { |
| 708 |
2/2✓ Branch 0 taken 518030 times.
✓ Branch 1 taken 4554 times.
|
522584 | if (!cpe->ch[ch].zeroes[w2*16 + g]) { |
| 709 | 518030 | i = 0; | |
| 710 | 518030 | break; | |
| 711 | } | ||
| 712 | } | ||
| 713 | 522474 | cpe->ch[ch].zeroes[w*16 + g] = i; | |
| 714 | } | ||
| 715 | } | ||
| 716 | } | ||
| 717 | |||
| 718 |
3/4✓ Branch 0 taken 5252 times.
✓ Branch 1 taken 870 times.
✓ Branch 2 taken 5252 times.
✗ Branch 3 not taken.
|
6122 | if (chans > 1 && cpe->common_window) { |
| 719 | 5252 | IndividualChannelStream *ics0 = &cpe->ch[0].ics; | |
| 720 | 5252 | IndividualChannelStream *ics1 = &cpe->ch[1].ics; | |
| 721 | 5252 | int msc = 0; | |
| 722 | 5252 | ics0->max_sfb = FFMAX(ics0->max_sfb, ics1->max_sfb); | |
| 723 | 5252 | ics1->max_sfb = ics0->max_sfb; | |
| 724 |
2/2✓ Branch 0 taken 7030 times.
✓ Branch 1 taken 5252 times.
|
12282 | for (w = 0; w < ics0->num_windows*16; w += 16) |
| 725 |
2/2✓ Branch 0 taken 265298 times.
✓ Branch 1 taken 7030 times.
|
272328 | for (i = 0; i < ics0->max_sfb; i++) |
| 726 |
2/2✓ Branch 0 taken 50542 times.
✓ Branch 1 taken 214756 times.
|
265298 | if (cpe->ms_mask[w+i]) |
| 727 | 50542 | msc++; | |
| 728 |
3/4✓ Branch 0 taken 1657 times.
✓ Branch 1 taken 3595 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1657 times.
|
5252 | if (msc == 0 || ics0->max_sfb == 0) |
| 729 | 3595 | cpe->ms_mode = 0; | |
| 730 | else | ||
| 731 |
2/2✓ Branch 0 taken 1239 times.
✓ Branch 1 taken 418 times.
|
1657 | cpe->ms_mode = msc < ics0->max_sfb * ics0->num_windows ? 1 : 2; |
| 732 | } | ||
| 733 | 6122 | } | |
| 734 | |||
| 735 | 684 | static void apply_intensity_stereo(ChannelElement *cpe) | |
| 736 | { | ||
| 737 | int w, w2, g, i; | ||
| 738 | 684 | IndividualChannelStream *ics = &cpe->ch[0].ics; | |
| 739 |
2/2✓ Branch 0 taken 136 times.
✓ Branch 1 taken 548 times.
|
684 | if (!cpe->common_window) |
| 740 | 136 | return; | |
| 741 |
2/2✓ Branch 0 taken 585 times.
✓ Branch 1 taken 548 times.
|
1133 | for (w = 0; w < ics->num_windows; w += ics->group_len[w]) { |
| 742 |
2/2✓ Branch 0 taken 723 times.
✓ Branch 1 taken 585 times.
|
1308 | for (w2 = 0; w2 < ics->group_len[w]; w2++) { |
| 743 | 723 | int start = (w+w2) * 128; | |
| 744 |
2/2✓ Branch 0 taken 28427 times.
✓ Branch 1 taken 723 times.
|
29150 | for (g = 0; g < ics->num_swb; g++) { |
| 745 | 28427 | int p = -1 + 2 * (cpe->ch[1].band_type[w*16+g] - 14); | |
| 746 | 28427 | float scale = cpe->ch[0].is_ener[w*16+g]; | |
| 747 |
2/2✓ Branch 0 taken 19153 times.
✓ Branch 1 taken 9274 times.
|
28427 | if (!cpe->is_mask[w*16 + g]) { |
| 748 | 19153 | start += ics->swb_sizes[g]; | |
| 749 | 19153 | continue; | |
| 750 | } | ||
| 751 |
2/2✓ Branch 0 taken 2501 times.
✓ Branch 1 taken 6773 times.
|
9274 | if (cpe->ms_mask[w*16 + g]) |
| 752 | 2501 | p *= -1; | |
| 753 |
2/2✓ Branch 0 taken 296820 times.
✓ Branch 1 taken 9274 times.
|
306094 | for (i = 0; i < ics->swb_sizes[g]; i++) { |
| 754 | 296820 | float sum = (cpe->ch[0].coeffs[start+i] + p*cpe->ch[1].coeffs[start+i])*scale; | |
| 755 | 296820 | cpe->ch[0].coeffs[start+i] = sum; | |
| 756 | 296820 | cpe->ch[1].coeffs[start+i] = 0.0f; | |
| 757 | } | ||
| 758 | 9274 | start += ics->swb_sizes[g]; | |
| 759 | } | ||
| 760 | } | ||
| 761 | } | ||
| 762 | } | ||
| 763 | |||
| 764 | /* I/S acceptance level for the image-error EMA at full rate pressure */ | ||
| 765 | #define NMR_IS_IMG_GATE 8000.0f | ||
| 766 | |||
| 767 | /* Frequency in Hz for the lower limit of intensity stereo */ | ||
| 768 | #define NMR_IS_LOW_LIMIT 6100 | ||
| 769 | |||
| 770 | /* M/S adoption: es < 0.5*em, content-driven and rate-free */ | ||
| 771 | #define NMR_MS_EQUIV 0.5f | ||
| 772 | #define NMR_MS_MASK 0.0f | ||
| 773 | |||
| 774 | /* Pair decouple threshold on the joint-tool candidacy fraction EMA: pairs | ||
| 775 | * whose joint tools are mostly dead (diffuse decorrelated content) window | ||
| 776 | * per-channel and skip M/S; recouple above 1.3x. */ | ||
| 777 | #define NMR_DECORR_LO 0.20f | ||
| 778 | |||
| 779 | /* Stereo-decision hysteresis: leaving a joint mode costs a margin. */ | ||
| 780 | #define NMR_STICKY 2.0f | ||
| 781 | |||
| 782 | /* Decision statistics are EMA-smoothed across frames. */ | ||
| 783 | #define NMR_SDEC_EMA 0.75f | ||
| 784 | |||
| 785 | /* PNS-stereo gate: substitute only clearly-decorrelated (wide) bands. */ | ||
| 786 | #define NMR_PNS_STEREO_DECORR 0.6f | ||
| 787 | |||
| 788 | /* M/S balance gate: no M/S on bands panned harder than this energy ratio */ | ||
| 789 | #define NMR_MS_BALANCE 0.25f | ||
| 790 | |||
| 791 | /* Perceptual I/S: highly correlated long-window bands above this frequency | ||
| 792 | * take I/S at this image-error budget regardless of rate pressure */ | ||
| 793 | #define NMR_IS_PERC_FREQ 8000.0f | ||
| 794 | #define NMR_IS_PERC_CORR 0.85f | ||
| 795 | #define NMR_IS_PERC_GATE 50.0f | ||
| 796 | |||
| 797 | /* Recode one band's window group as mid+side in place. */ | ||
| 798 | 29576 | static void nmr_apply_ms_band(AACEncContext *s, ChannelElement *cpe, | |
| 799 | int w, int g, int start, int len, int gl) | ||
| 800 | { | ||
| 801 | 29576 | SingleChannelElement *sce0 = &cpe->ch[0]; | |
| 802 | 29576 | SingleChannelElement *sce1 = &cpe->ch[1]; | |
| 803 | 29576 | cpe->ms_mask[w*16+g] = 1; | |
| 804 |
2/2✓ Branch 0 taken 33483 times.
✓ Branch 1 taken 29576 times.
|
63059 | for (int w2 = 0; w2 < gl; w2++) { |
| 805 | 33483 | FFPsyBand *b0 = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g]; | |
| 806 | 33483 | FFPsyBand *b1 = &s->psy.ch[s->cur_channel+1].psy_bands[(w+w2)*16+g]; | |
| 807 | 33483 | float *L = sce0->coeffs + start + (w+w2)*128; | |
| 808 | 33483 | float *R = sce1->coeffs + start + (w+w2)*128; | |
| 809 | 33483 | float em = 0.0f, es = 0.0f; | |
| 810 |
2/2✓ Branch 0 taken 582996 times.
✓ Branch 1 taken 33483 times.
|
616479 | for (int i = 0; i < len; i++) { |
| 811 | 582996 | float m = (L[i] + R[i]) * 0.5f; | |
| 812 | 582996 | R[i] = m - R[i]; L[i] = m; | |
| 813 | 582996 | em += L[i]*L[i]; es += R[i]*R[i]; | |
| 814 | } | ||
| 815 |
2/2✓ Branch 0 taken 12881 times.
✓ Branch 1 taken 20602 times.
|
33483 | b0->threshold = FFMIN(b0->threshold, b1->threshold) * 0.5f; |
| 816 | 33483 | b1->threshold = b0->threshold; | |
| 817 | 33483 | b0->energy = em; b1->energy = es; | |
| 818 | } | ||
| 819 | 29576 | } | |
| 820 | |||
| 821 | /* I/S perceptual test: reconstruction image error vs the pair's masks. */ | ||
| 822 | 26653 | static int nmr_is_image_masked(AACEncContext *s, ChannelElement *cpe, | |
| 823 | int w, int g, int start, int len, int gl, | ||
| 824 | float ener0, float ener1, float dot, | ||
| 825 | float minthr0, float minthr1, float *ratio_out, | ||
| 826 | float *scale_out, float *sr_out, int *p_out) | ||
| 827 | { | ||
| 828 |
2/2✓ Branch 0 taken 26492 times.
✓ Branch 1 taken 161 times.
|
26653 | int p = dot >= 0.0f ? 1 : -1; |
| 829 | 26653 | float ener01 = ener0 + ener1 + 2*p*dot; /* energy of L + p*R */ | |
| 830 | 26653 | *ratio_out = FLT_MAX; | |
| 831 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 26653 times.
|
26653 | if (ener01 <= FLT_MIN) |
| 832 | ✗ | return 0; | |
| 833 | 26653 | float scale = sqrtf(ener0 / ener01); /* carrier = (L + p*R)*scale */ | |
| 834 | 26653 | float sr_ = sqrtf(ener1 / ener0); /* decoder: R = p*sr_*carrier */ | |
| 835 | 26653 | float img0 = 0.0f, img1 = 0.0f; | |
| 836 |
2/2✓ Branch 0 taken 30639 times.
✓ Branch 1 taken 26653 times.
|
57292 | for (int w2 = 0; w2 < gl; w2++) { |
| 837 | 30639 | const float *L = cpe->ch[0].coeffs + start + (w+w2)*128; | |
| 838 | 30639 | const float *R = cpe->ch[1].coeffs + start + (w+w2)*128; | |
| 839 |
2/2✓ Branch 0 taken 949984 times.
✓ Branch 1 taken 30639 times.
|
980623 | for (int i = 0; i < len; i++) { |
| 840 | 949984 | float c = (L[i] + p*R[i]) * scale; | |
| 841 | 949984 | float dl = L[i] - c, dr = R[i] - p*sr_*c; | |
| 842 | 949984 | img0 += dl*dl; img1 += dr*dr; | |
| 843 | } | ||
| 844 | } | ||
| 845 |
9/10✓ Branch 0 taken 20149 times.
✓ Branch 1 taken 6504 times.
✓ Branch 2 taken 20149 times.
✓ Branch 3 taken 6504 times.
✓ Branch 4 taken 8317 times.
✓ Branch 5 taken 18336 times.
✓ Branch 6 taken 8317 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 11832 times.
✓ Branch 9 taken 6504 times.
|
26653 | *ratio_out = FFMAX(img0 / FFMAX(minthr0 * gl, FLT_MIN), |
| 846 | img1 / FFMAX(minthr1 * gl, FLT_MIN)); | ||
| 847 | 26653 | *scale_out = scale; *sr_out = sr_; *p_out = p; | |
| 848 | 26653 | return 1; | |
| 849 | } | ||
| 850 | |||
| 851 | /* Recode one band's window group as intensity stereo in place: replace L with the | ||
| 852 | * carrier, zero R, signal the phase via the side channel's band type, and fold the | ||
| 853 | * pair's masking into the surviving (carrier) channel. */ | ||
| 854 | 14340 | static void nmr_apply_is_band(AACEncContext *s, ChannelElement *cpe, | |
| 855 | int w, int g, int start, int len, int gl, | ||
| 856 | float scale, float sr_, int p, | ||
| 857 | float ener0, float ener1) | ||
| 858 | { | ||
| 859 | 14340 | cpe->is_mask[w*16+g] = 1; | |
| 860 | 14340 | cpe->ch[0].is_ener[w*16+g] = scale; | |
| 861 | 14340 | cpe->ch[1].is_ener[w*16+g] = ener0 / ener1; | |
| 862 |
2/2✓ Branch 0 taken 14324 times.
✓ Branch 1 taken 16 times.
|
14340 | cpe->ch[1].band_type[w*16+g] = p > 0 ? INTENSITY_BT : INTENSITY_BT2; |
| 863 |
2/2✓ Branch 0 taken 14340 times.
✓ Branch 1 taken 14340 times.
|
28680 | for (int w2 = 0; w2 < gl; w2++) { |
| 864 | 14340 | FFPsyBand *b0 = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g]; | |
| 865 | 14340 | FFPsyBand *b1 = &s->psy.ch[s->cur_channel+1].psy_bands[(w+w2)*16+g]; | |
| 866 | 14340 | float *L = cpe->ch[0].coeffs + start + (w+w2)*128; | |
| 867 | 14340 | float *R = cpe->ch[1].coeffs + start + (w+w2)*128; | |
| 868 | 14340 | float ec = 0.0f; | |
| 869 |
2/2✓ Branch 0 taken 497884 times.
✓ Branch 1 taken 14340 times.
|
512224 | for (int i = 0; i < len; i++) { |
| 870 | 497884 | L[i] = (L[i] + p*R[i]) * scale; | |
| 871 | 497884 | R[i] = 0.0f; | |
| 872 | 497884 | ec += L[i]*L[i]; | |
| 873 | } | ||
| 874 |
4/6✓ Branch 0 taken 14340 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1915 times.
✓ Branch 3 taken 12425 times.
✓ Branch 4 taken 1915 times.
✗ Branch 5 not taken.
|
14340 | b0->threshold = FFMIN(b0->threshold, b1->threshold / FFMAX(sr_*sr_, 1e-9f)); |
| 875 | 14340 | b0->energy = ec; b1->energy = 0.0f; | |
| 876 | } | ||
| 877 | 14340 | } | |
| 878 | |||
| 879 | /* | ||
| 880 | * Per-band stereo-mode decision (L/R vs M/S vs intensity) for the NMR coder, | ||
| 881 | * made before quantization from the psychoacoustic model alone, so the | ||
| 882 | * quantizer search allocates natively on the spectra that are actually coded. | ||
| 883 | */ | ||
| 884 | 1337 | static void nmr_decide_stereo(AACEncContext *s, ChannelElement *cpe) | |
| 885 | { | ||
| 886 | 1337 | SingleChannelElement *sce0 = &cpe->ch[0]; | |
| 887 | 1337 | SingleChannelElement *sce1 = &cpe->ch[1]; | |
| 888 | 1337 | IndividualChannelStream *ics = &sce0->ics; | |
| 889 | 1337 | const AVCodecContext *avctx = s->psy.avctx; | |
| 890 | 1337 | const float freq_mult = avctx->sample_rate / (1024.0f / ics->num_windows) / 2.0f; | |
| 891 | 1337 | int is_count = 0; | |
| 892 | |||
| 893 |
1/2✓ Branch 0 taken 1337 times.
✗ Branch 1 not taken.
|
1337 | if (s->nmr) { |
| 894 | 1337 | int pi = (s->cur_channel >> 1) & 7; | |
| 895 | 1337 | pi = pi * 2 + (ics->num_windows == 8); /* per-grid state bank */ | |
| 896 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 1241 times.
|
1337 | if (!s->nmr->sinit[pi]) { |
| 897 | /* one-time init; per-grid banks persist across window switches | ||
| 898 | * (wiping them churned stereo modes audibly) */ | ||
| 899 | 96 | memset(s->nmr->smode[pi], 0, sizeof(s->nmr->smode[pi])); | |
| 900 |
2/2✓ Branch 0 taken 12288 times.
✓ Branch 1 taken 96 times.
|
12384 | for (int b = 0; b < 128; b++) { |
| 901 | 12288 | s->nmr->sema_em[pi][b] = 0.0f; | |
| 902 | 12288 | s->nmr->sema_img[pi][b] = -1.0f; | |
| 903 | } | ||
| 904 | 96 | s->nmr->sinit[pi] = 1; | |
| 905 | } | ||
| 906 | } | ||
| 907 | |||
| 908 | /* Per-band stereo decision (L/R vs M/S vs I/S), made pre-quantization from | ||
| 909 | * the psy model so the trellis allocates on the coded spectra. */ | ||
| 910 | |||
| 911 | /* I/S engages under SUSTAINED strain only: rate pressure gated by the | ||
| 912 | * lambda floor (pressure spikes at a comfortable operating point must | ||
| 913 | * not admit it). Unengaged candidates fall back to M/S. */ | ||
| 914 | 4011 | float is_ramp = s->nmr ? s->nmr->press * | |
| 915 |
1/2✓ Branch 0 taken 1337 times.
✗ Branch 1 not taken.
|
1337 | av_clipf((s->nmr->lam_floor - 40.0f) / (120.0f - 40.0f), 0.0f, 1.0f) : 0.0f; |
| 916 | /* perceptual I/S (below) makes candidacy pressure-independent */ | ||
| 917 | 1337 | const int allow_is = s->options.intensity_stereo; | |
| 918 | |||
| 919 | 1337 | const int pidx = (s->cur_channel >> 1) & 15; | |
| 920 | 1337 | const int decoupled = s->psy.pair_decoupled[pidx]; | |
| 921 | 1337 | int njoint = 0, nbands = 0; /* joint-tool candidacy census, decouple feed */ | |
| 922 | |||
| 923 |
2/2✓ Branch 0 taken 1460 times.
✓ Branch 1 taken 1337 times.
|
2797 | for (int w = 0; w < ics->num_windows; w += ics->group_len[w]) { |
| 924 | 1460 | int start = 0; | |
| 925 |
2/2✓ Branch 0 taken 63770 times.
✓ Branch 1 taken 1460 times.
|
65230 | for (int g = 0; g < ics->num_swb; start += ics->swb_sizes[g++]) { |
| 926 | 63770 | int len = ics->swb_sizes[g], gl = ics->group_len[w]; | |
| 927 | 63770 | float ener0 = 0.0f, ener1 = 0.0f, dot = 0.0f, es_tot = 0.0f, em_tot = 0.0f; | |
| 928 | 63770 | float minthr0 = FLT_MAX, minthr1 = FLT_MAX; | |
| 929 | |||
| 930 | 63770 | cpe->is_mask[w*16+g] = 0; | |
| 931 | 63770 | cpe->ms_mask[w*16+g] = 0; | |
| 932 | |||
| 933 |
2/2✓ Branch 0 taken 71750 times.
✓ Branch 1 taken 63770 times.
|
135520 | for (int w2 = 0; w2 < gl; w2++) { |
| 934 | 71750 | FFPsyBand *b0 = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g]; | |
| 935 | 71750 | FFPsyBand *b1 = &s->psy.ch[s->cur_channel+1].psy_bands[(w+w2)*16+g]; | |
| 936 | 71750 | const float *L = sce0->coeffs + start + (w+w2)*128; | |
| 937 | 71750 | const float *R = sce1->coeffs + start + (w+w2)*128; | |
| 938 | 71750 | float el = 0.0f, er = 0.0f, em = 0.0f, es = 0.0f, d = 0.0f; | |
| 939 |
2/2✓ Branch 0 taken 1369088 times.
✓ Branch 1 taken 71750 times.
|
1440838 | for (int i = 0; i < len; i++) { |
| 940 | 1369088 | float m = (L[i] + R[i]) * 0.5f; | |
| 941 | 1369088 | float sv = m - R[i]; | |
| 942 | 1369088 | el += L[i]*L[i]; er += R[i]*R[i]; | |
| 943 | 1369088 | em += m*m; es += sv*sv; d += L[i]*R[i]; | |
| 944 | } | ||
| 945 | 71750 | ener0 += el; ener1 += er; dot += d; es_tot += es; em_tot += em; | |
| 946 |
2/2✓ Branch 0 taken 67518 times.
✓ Branch 1 taken 4232 times.
|
71750 | minthr0 = FFMIN(minthr0, b0->threshold); |
| 947 |
2/2✓ Branch 0 taken 67435 times.
✓ Branch 1 taken 4315 times.
|
71750 | minthr1 = FFMIN(minthr1, b1->threshold); |
| 948 | } | ||
| 949 |
2/2✓ Branch 0 taken 15508 times.
✓ Branch 1 taken 48262 times.
|
63770 | float thr_g = FFMIN(minthr0, minthr1) * gl; /* group masking budget */ |
| 950 | |||
| 951 | /* PNS-stereo reservation: keep clearly-wide noise bands for PNS. */ | ||
| 952 | 63770 | const int sidx = w*16+g; | |
| 953 | { | ||
| 954 | 63770 | float es_w = es_tot, em_w = em_tot; | |
| 955 |
1/2✓ Branch 0 taken 63770 times.
✗ Branch 1 not taken.
|
63770 | if (s->nmr) { |
| 956 | 63770 | int pi_ = ((s->cur_channel >> 1) & 7) * 2 + (cpe->ch[0].ics.num_windows == 8); | |
| 957 | 63770 | float pe = s->nmr->sema_es[pi_][sidx]; | |
| 958 | 63770 | float pm = s->nmr->sema_em[pi_][sidx]; | |
| 959 |
2/2✓ Branch 0 taken 59222 times.
✓ Branch 1 taken 4548 times.
|
63770 | if (pm > 0.0f) { |
| 960 | 59222 | es_w = NMR_SDEC_EMA * pe + (1.0f - NMR_SDEC_EMA) * es_tot; | |
| 961 | 59222 | em_w = NMR_SDEC_EMA * pm + (1.0f - NMR_SDEC_EMA) * em_tot; | |
| 962 | } | ||
| 963 | } | ||
| 964 |
3/4✓ Branch 0 taken 24553 times.
✓ Branch 1 taken 39217 times.
✓ Branch 2 taken 24553 times.
✗ Branch 3 not taken.
|
63770 | if (cpe->ch[0].can_pns[w*16+g] && cpe->ch[1].can_pns[w*16+g] && |
| 965 |
2/2✓ Branch 0 taken 1029 times.
✓ Branch 1 taken 23524 times.
|
24553 | es_w > NMR_PNS_STEREO_DECORR * em_w) |
| 966 | 1029 | continue; | |
| 967 | } | ||
| 968 | 62741 | cpe->ch[0].can_pns[w*16+g] = cpe->ch[1].can_pns[w*16+g] = 0; | |
| 969 | |||
| 970 | 62741 | int pi = ((s->cur_channel >> 1) & 7) * 2 + (cpe->ch[0].ics.num_windows == 8); | |
| 971 |
1/2✓ Branch 0 taken 62741 times.
✗ Branch 1 not taken.
|
62741 | uint8_t *pmode = s->nmr ? s->nmr->smode[pi] : NULL; |
| 972 |
1/2✓ Branch 0 taken 62741 times.
✗ Branch 1 not taken.
|
62741 | int prev = pmode ? pmode[sidx] : 0; |
| 973 |
2/2✓ Branch 0 taken 29191 times.
✓ Branch 1 taken 33550 times.
|
62741 | float eqgate = NMR_MS_EQUIV * (prev == 1 ? 1.5f : 1.0f); /* stay-until es>0.75em */ |
| 974 | /* I/S = lossy economy: image-error budget scales with pressure */ | ||
| 975 |
2/2✓ Branch 0 taken 13550 times.
✓ Branch 1 taken 49191 times.
|
62741 | float imgate = NMR_IS_IMG_GATE * is_ramp * (prev == 2 ? NMR_STICKY : 1.0f); |
| 976 | /* Perceptual I/S (metric-blind by design - Zimtohrli penalizes | ||
| 977 | * even sub-mask image error, ears above ~8k do not hear | ||
| 978 | * interaural fine structure): engage on genuinely intensity- | ||
| 979 | * panned HF - high inter-channel correlation, long windows, | ||
| 980 | * sticky - regardless of rate pressure. Freed bits are judged | ||
| 981 | * by the sub-8k spectrum; the image itself is judged by ears. */ | ||
| 982 |
5/6✓ Branch 0 taken 59639 times.
✓ Branch 1 taken 3102 times.
✓ Branch 2 taken 59345 times.
✓ Branch 3 taken 294 times.
✓ Branch 4 taken 59345 times.
✗ Branch 5 not taken.
|
62741 | if (cpe->ch[0].ics.num_windows != 8 && ener0 > FLT_MIN && ener1 > FLT_MIN) { |
| 983 | 59345 | float corr = dot / sqrtf(ener0 * ener1); | |
| 984 |
2/2✓ Branch 0 taken 21760 times.
✓ Branch 1 taken 37585 times.
|
59345 | if (start * freq_mult > NMR_IS_PERC_FREQ && |
| 985 |
4/4✓ Branch 0 taken 13523 times.
✓ Branch 1 taken 8237 times.
✓ Branch 2 taken 15406 times.
✓ Branch 3 taken 6354 times.
|
21760 | fabsf(corr) > NMR_IS_PERC_CORR * (prev == 2 ? 0.9f : 1.0f)) |
| 986 |
6/6✓ Branch 0 taken 12728 times.
✓ Branch 1 taken 2678 times.
✓ Branch 2 taken 162 times.
✓ Branch 3 taken 15244 times.
✓ Branch 4 taken 12728 times.
✓ Branch 5 taken 2516 times.
|
15406 | imgate = FFMAX(imgate, NMR_IS_PERC_GATE * (prev == 2 ? NMR_STICKY : 1.0f)); |
| 987 | } | ||
| 988 | 62741 | float es_d = es_tot, em_d = em_tot; | |
| 989 |
1/2✓ Branch 0 taken 62741 times.
✗ Branch 1 not taken.
|
62741 | if (s->nmr) { |
| 990 | 62741 | float *ees = &s->nmr->sema_es[pi][sidx]; | |
| 991 | 62741 | float *eem = &s->nmr->sema_em[pi][sidx]; | |
| 992 |
2/2✓ Branch 0 taken 4137 times.
✓ Branch 1 taken 58604 times.
|
62741 | if (*eem <= 0.0f) { *ees = es_tot; *eem = em_tot; } |
| 993 | else { | ||
| 994 | 58604 | *ees = NMR_SDEC_EMA * *ees + (1.0f - NMR_SDEC_EMA) * es_tot; | |
| 995 | 58604 | *eem = NMR_SDEC_EMA * *eem + (1.0f - NMR_SDEC_EMA) * em_tot; | |
| 996 | } | ||
| 997 | 62741 | es_d = *ees; em_d = *eem; | |
| 998 | } | ||
| 999 | /* Balance gate: M/S has no coding gain on a hard-panned band | ||
| 1000 | * (|S| ~ |M|), and M/S quantization noise decorrelates across | ||
| 1001 | * the unfold, smearing the panned source into the far channel. */ | ||
| 1002 |
4/4✓ Branch 0 taken 16664 times.
✓ Branch 1 taken 46077 times.
✓ Branch 2 taken 16664 times.
✓ Branch 3 taken 46077 times.
|
62741 | int bal_ok = FFMIN(ener0, ener1) > NMR_MS_BALANCE * FFMAX(ener0, ener1); |
| 1003 |
2/2✓ Branch 0 taken 45101 times.
✓ Branch 1 taken 17640 times.
|
107842 | int ms_would = s->options.mid_side && |
| 1004 |
1/2✓ Branch 0 taken 45101 times.
✗ Branch 1 not taken.
|
45101 | (s->options.mid_side == 1 || |
| 1005 |
2/2✓ Branch 0 taken 4982 times.
✓ Branch 1 taken 40119 times.
|
45101 | ((es_d < eqgate * em_d || |
| 1006 |
3/4✗ Branch 0 not taken.
✓ Branch 1 taken 4982 times.
✓ Branch 2 taken 37808 times.
✓ Branch 3 taken 2311 times.
|
45101 | es_tot < NMR_MS_MASK * thr_g) && bal_ok)); |
| 1007 |
3/4✓ Branch 0 taken 37808 times.
✓ Branch 1 taken 24933 times.
✓ Branch 2 taken 37808 times.
✗ Branch 3 not taken.
|
62741 | int ms_ok = ms_would && !decoupled; |
| 1008 | float scale, sr_, imgratio; int p; | ||
| 1009 | /* I/S competes with M/S above the frequency limit (candidacy must | ||
| 1010 | * not be gated on !ms_ok - that leaves only unrenderable bands) */ | ||
| 1011 |
2/2✓ Branch 0 taken 26653 times.
✓ Branch 1 taken 126 times.
|
26779 | int is_cand = start * freq_mult > NMR_IS_LOW_LIMIT && |
| 1012 |
4/6✓ Branch 0 taken 26779 times.
✓ Branch 1 taken 35962 times.
✓ Branch 2 taken 26653 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 26653 times.
✗ Branch 5 not taken.
|
116173 | ener0 > FLT_MIN && ener1 > FLT_MIN && |
| 1013 | 26653 | nmr_is_image_masked(s, cpe, w, g, start, len, gl, | |
| 1014 | ener0, ener1, dot, minthr0, minthr1, | ||
| 1015 | &imgratio, &scale, &sr_, &p); | ||
| 1016 | 62741 | int is_ok = is_cand; | |
| 1017 |
3/4✓ Branch 0 taken 62741 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 26779 times.
✓ Branch 3 taken 35962 times.
|
62741 | if (s->nmr && start * freq_mult > NMR_IS_LOW_LIMIT) { |
| 1018 | /* smoothed image-error; updated only while candidate (fail-value | ||
| 1019 | * feeding jammed it permanently high) */ | ||
| 1020 | 26779 | float *eim = &s->nmr->sema_img[pi][sidx]; | |
| 1021 |
2/2✓ Branch 0 taken 26653 times.
✓ Branch 1 taken 126 times.
|
26779 | if (is_cand) { |
| 1022 | /* seed from first measurement; freeze when not candidate */ | ||
| 1023 |
2/2✓ Branch 0 taken 1734 times.
✓ Branch 1 taken 24919 times.
|
26653 | if (*eim < 0.0f) *eim = imgratio; |
| 1024 |
2/2✓ Branch 0 taken 3326 times.
✓ Branch 1 taken 21593 times.
|
24919 | else *eim = NMR_SDEC_EMA * *eim + (1.0f - NMR_SDEC_EMA) * FFMIN(imgratio, 100.0f * NMR_IS_IMG_GATE); |
| 1025 | } | ||
| 1026 |
5/6✓ Branch 0 taken 26653 times.
✓ Branch 1 taken 126 times.
✓ Branch 2 taken 26653 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14340 times.
✓ Branch 5 taken 12313 times.
|
26779 | is_ok = is_cand && *eim >= 0.0f && *eim < imgate; |
| 1027 | } | ||
| 1028 | |||
| 1029 |
4/4✓ Branch 0 taken 24933 times.
✓ Branch 1 taken 37808 times.
✓ Branch 2 taken 6108 times.
✓ Branch 3 taken 18825 times.
|
62741 | njoint += ms_would || is_ok; nbands++; |
| 1030 |
1/2✓ Branch 0 taken 62741 times.
✗ Branch 1 not taken.
|
62741 | if (pmode) { |
| 1031 |
6/8✓ Branch 0 taken 14340 times.
✓ Branch 1 taken 48401 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14340 times.
✓ Branch 4 taken 18825 times.
✓ Branch 5 taken 29576 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 18825 times.
|
62741 | int m_ = (is_ok && allow_is) ? 2 : ms_ok ? 1 : |
| 1032 | ✗ | (is_ok && s->options.mid_side) ? 1 : 0; | |
| 1033 | 62741 | pmode[sidx] = m_; | |
| 1034 | 62741 | s->nmr->smode_band[(s->cur_channel >> 1) & 7][w*16+g] = m_; | |
| 1035 | } | ||
| 1036 |
3/4✓ Branch 0 taken 14340 times.
✓ Branch 1 taken 48401 times.
✓ Branch 2 taken 14340 times.
✗ Branch 3 not taken.
|
62741 | if (is_ok && allow_is) { |
| 1037 | 14340 | nmr_apply_is_band(s, cpe, w, g, start, len, gl, | |
| 1038 | scale, sr_, p, ener0, ener1); | ||
| 1039 | 14340 | is_count++; | |
| 1040 |
3/6✓ Branch 0 taken 18825 times.
✓ Branch 1 taken 29576 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18825 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
48401 | } else if (ms_ok || (is_ok && s->options.mid_side)) { |
| 1041 | 29576 | nmr_apply_ms_band(s, cpe, w, g, start, len, gl); | |
| 1042 | } | ||
| 1043 | /* else: keep full L/R stereo */ | ||
| 1044 | } | ||
| 1045 | } | ||
| 1046 | 1337 | cpe->is_mode = !!is_count; | |
| 1047 | |||
| 1048 |
1/2✓ Branch 0 taken 1337 times.
✗ Branch 1 not taken.
|
1337 | if (nbands > 0) { |
| 1049 | /* Pair joint-tool value, read next frame by the psy pair-synced window | ||
| 1050 | * decision and the M/S candidacy above. Measured as CANDIDACY (not | ||
| 1051 | * adoption) so decoupling cannot starve its own signal and self-lock. */ | ||
| 1052 | 1337 | float r = (float)njoint / nbands; | |
| 1053 | 1337 | float *pj = &s->psy.pair_joint[pidx]; | |
| 1054 |
2/2✓ Branch 0 taken 1281 times.
✓ Branch 1 taken 56 times.
|
1337 | *pj = *pj > 0.0f ? 0.95f * *pj + 0.05f * r : r; |
| 1055 | 1337 | s->psy.pair_decoupled[pidx] = *pj < | |
| 1056 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1332 times.
|
1337 | (s->psy.pair_decoupled[pidx] ? 1.3f * NMR_DECORR_LO : NMR_DECORR_LO); |
| 1057 | } | ||
| 1058 | 1337 | } | |
| 1059 | |||
| 1060 | 412 | static void apply_mid_side_stereo(ChannelElement *cpe) | |
| 1061 | { | ||
| 1062 | int w, w2, g, i; | ||
| 1063 | 412 | IndividualChannelStream *ics = &cpe->ch[0].ics; | |
| 1064 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 412 times.
|
412 | if (!cpe->common_window) |
| 1065 | ✗ | return; | |
| 1066 |
2/2✓ Branch 0 taken 447 times.
✓ Branch 1 taken 412 times.
|
859 | for (w = 0; w < ics->num_windows; w += ics->group_len[w]) { |
| 1067 |
2/2✓ Branch 0 taken 580 times.
✓ Branch 1 taken 447 times.
|
1027 | for (w2 = 0; w2 < ics->group_len[w]; w2++) { |
| 1068 | 580 | int start = (w+w2) * 128; | |
| 1069 |
2/2✓ Branch 0 taken 21700 times.
✓ Branch 1 taken 580 times.
|
22280 | for (g = 0; g < ics->num_swb; g++) { |
| 1070 | /* ms_mask can be used for other purposes in PNS and I/S, | ||
| 1071 | * so must not apply M/S if any band uses either, even if | ||
| 1072 | * ms_mask is set. | ||
| 1073 | */ | ||
| 1074 |
2/4✓ Branch 0 taken 21700 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21700 times.
✗ Branch 3 not taken.
|
21700 | if (!cpe->ms_mask[w*16 + g] || cpe->is_mask[w*16 + g] |
| 1075 |
1/2✓ Branch 0 taken 21700 times.
✗ Branch 1 not taken.
|
21700 | || cpe->ch[0].band_type[w*16 + g] >= NOISE_BT |
| 1076 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21700 times.
|
21700 | || cpe->ch[1].band_type[w*16 + g] >= NOISE_BT) { |
| 1077 | ✗ | start += ics->swb_sizes[g]; | |
| 1078 | ✗ | continue; | |
| 1079 | } | ||
| 1080 |
2/2✓ Branch 0 taken 421888 times.
✓ Branch 1 taken 21700 times.
|
443588 | for (i = 0; i < ics->swb_sizes[g]; i++) { |
| 1081 | 421888 | float L = (cpe->ch[0].coeffs[start+i] + cpe->ch[1].coeffs[start+i]) * 0.5f; | |
| 1082 | 421888 | float R = L - cpe->ch[1].coeffs[start+i]; | |
| 1083 | 421888 | cpe->ch[0].coeffs[start+i] = L; | |
| 1084 | 421888 | cpe->ch[1].coeffs[start+i] = R; | |
| 1085 | } | ||
| 1086 | 21700 | start += ics->swb_sizes[g]; | |
| 1087 | } | ||
| 1088 | } | ||
| 1089 | } | ||
| 1090 | } | ||
| 1091 | |||
| 1092 | /** | ||
| 1093 | * Encode scalefactor band coding type. | ||
| 1094 | */ | ||
| 1095 | 11374 | static void encode_band_info(AACEncContext *s, SingleChannelElement *sce) | |
| 1096 | { | ||
| 1097 | int w; | ||
| 1098 | |||
| 1099 |
1/2✓ Branch 0 taken 11374 times.
✗ Branch 1 not taken.
|
11374 | if (s->coder->set_special_band_scalefactors) |
| 1100 | 11374 | s->coder->set_special_band_scalefactors(s, sce); | |
| 1101 | |||
| 1102 |
2/2✓ Branch 0 taken 12089 times.
✓ Branch 1 taken 11374 times.
|
23463 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) |
| 1103 | { | ||
| 1104 | /* the sectioning trellis must trade section bits against | ||
| 1105 | * spectral bits at the coder's REAL operating lambda; the | ||
| 1106 | * NMR outer-loop lambda is a static 120 */ | ||
| 1107 | 12089 | float slam = s->lambda; | |
| 1108 |
3/4✓ Branch 0 taken 3671 times.
✓ Branch 1 taken 8418 times.
✓ Branch 2 taken 3671 times.
✗ Branch 3 not taken.
|
12089 | if (s->options.coder == AAC_CODER_NMR && s->nmr && |
| 1109 |
2/2✓ Branch 0 taken 3659 times.
✓ Branch 1 taken 12 times.
|
3671 | s->nmr->lam_slew > 0.0f) |
| 1110 | 3659 | slam = s->nmr->lam_slew; | |
| 1111 | 12089 | s->coder->encode_window_bands_info(s, sce, w, sce->ics.group_len[w], slam); | |
| 1112 | } | ||
| 1113 | 11374 | } | |
| 1114 | |||
| 1115 | /** | ||
| 1116 | * Encode scalefactors. | ||
| 1117 | */ | ||
| 1118 | 11374 | static void encode_scale_factors(AVCodecContext *avctx, AACEncContext *s, | |
| 1119 | SingleChannelElement *sce) | ||
| 1120 | { | ||
| 1121 | 11374 | int diff, off_sf = sce->sf_idx[0], off_pns = sce->sf_idx[0] - NOISE_OFFSET; | |
| 1122 | 11374 | int off_is = 0, noise_flag = 1; | |
| 1123 | int i, w; | ||
| 1124 | |||
| 1125 |
2/2✓ Branch 0 taken 12089 times.
✓ Branch 1 taken 11374 times.
|
23463 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
| 1126 |
2/2✓ Branch 0 taken 525643 times.
✓ Branch 1 taken 12089 times.
|
537732 | for (i = 0; i < sce->ics.max_sfb; i++) { |
| 1127 |
2/2✓ Branch 0 taken 513344 times.
✓ Branch 1 taken 12299 times.
|
525643 | if (!sce->zeroes[w*16 + i]) { |
| 1128 |
2/2✓ Branch 0 taken 3416 times.
✓ Branch 1 taken 509928 times.
|
513344 | if (sce->band_type[w*16 + i] == NOISE_BT) { |
| 1129 | 3416 | diff = sce->sf_idx[w*16 + i] - off_pns; | |
| 1130 | 3416 | off_pns = sce->sf_idx[w*16 + i]; | |
| 1131 |
2/2✓ Branch 0 taken 842 times.
✓ Branch 1 taken 2574 times.
|
3416 | if (noise_flag-- > 0) { |
| 1132 | 842 | put_bits(&s->pb, NOISE_PRE_BITS, diff + NOISE_PRE); | |
| 1133 | 842 | continue; | |
| 1134 | } | ||
| 1135 |
2/2✓ Branch 0 taken 489532 times.
✓ Branch 1 taken 20396 times.
|
509928 | } else if (sce->band_type[w*16 + i] == INTENSITY_BT || |
| 1136 |
2/2✓ Branch 0 taken 2390 times.
✓ Branch 1 taken 487142 times.
|
489532 | sce->band_type[w*16 + i] == INTENSITY_BT2) { |
| 1137 | 22786 | diff = sce->sf_idx[w*16 + i] - off_is; | |
| 1138 | 22786 | off_is = sce->sf_idx[w*16 + i]; | |
| 1139 | } else { | ||
| 1140 | 487142 | diff = sce->sf_idx[w*16 + i] - off_sf; | |
| 1141 | 487142 | off_sf = sce->sf_idx[w*16 + i]; | |
| 1142 | } | ||
| 1143 | 512502 | diff += SCALE_DIFF_ZERO; | |
| 1144 |
2/4✓ Branch 0 taken 512502 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 512502 times.
|
512502 | av_assert0(diff >= 0 && diff <= 120); |
| 1145 | 512502 | put_bits(&s->pb, ff_aac_scalefactor_bits[diff], ff_aac_scalefactor_code[diff]); | |
| 1146 | } | ||
| 1147 | } | ||
| 1148 | } | ||
| 1149 | 11374 | } | |
| 1150 | |||
| 1151 | /** | ||
| 1152 | * Encode pulse data. | ||
| 1153 | */ | ||
| 1154 | 11374 | static void encode_pulses(AACEncContext *s, Pulse *pulse) | |
| 1155 | { | ||
| 1156 | int i; | ||
| 1157 | |||
| 1158 | 11374 | put_bits(&s->pb, 1, !!pulse->num_pulse); | |
| 1159 |
1/2✓ Branch 0 taken 11374 times.
✗ Branch 1 not taken.
|
11374 | if (!pulse->num_pulse) |
| 1160 | 11374 | return; | |
| 1161 | |||
| 1162 | ✗ | put_bits(&s->pb, 2, pulse->num_pulse - 1); | |
| 1163 | ✗ | put_bits(&s->pb, 6, pulse->start); | |
| 1164 | ✗ | for (i = 0; i < pulse->num_pulse; i++) { | |
| 1165 | ✗ | put_bits(&s->pb, 5, pulse->pos[i]); | |
| 1166 | ✗ | put_bits(&s->pb, 4, pulse->amp[i]); | |
| 1167 | } | ||
| 1168 | } | ||
| 1169 | |||
| 1170 | /** | ||
| 1171 | * Encode spectral coefficients processed by psychoacoustic model. | ||
| 1172 | */ | ||
| 1173 | 11374 | static void encode_spectral_coeffs(AACEncContext *s, SingleChannelElement *sce) | |
| 1174 | { | ||
| 1175 | int start, i, w, w2; | ||
| 1176 | |||
| 1177 |
2/2✓ Branch 0 taken 12089 times.
✓ Branch 1 taken 11374 times.
|
23463 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
| 1178 | 12089 | start = 0; | |
| 1179 |
2/2✓ Branch 0 taken 525643 times.
✓ Branch 1 taken 12089 times.
|
537732 | for (i = 0; i < sce->ics.max_sfb; i++) { |
| 1180 |
2/2✓ Branch 0 taken 12299 times.
✓ Branch 1 taken 513344 times.
|
525643 | if (sce->zeroes[w*16 + i]) { |
| 1181 | 12299 | start += sce->ics.swb_sizes[i]; | |
| 1182 | 12299 | continue; | |
| 1183 | } | ||
| 1184 |
2/2✓ Branch 0 taken 546259 times.
✓ Branch 1 taken 513344 times.
|
1059603 | for (w2 = w; w2 < w + sce->ics.group_len[w]; w2++) { |
| 1185 | 546259 | s->coder->quantize_and_encode_band(s, &s->pb, | |
| 1186 | 546259 | &sce->coeffs[start + w2*128], | |
| 1187 | 546259 | NULL, sce->ics.swb_sizes[i], | |
| 1188 | 546259 | sce->sf_idx[w*16 + i], | |
| 1189 | 546259 | sce->band_type[w*16 + i], | |
| 1190 | s->lambda, | ||
| 1191 | 546259 | sce->ics.window_clipping[w]); | |
| 1192 | } | ||
| 1193 | 513344 | start += sce->ics.swb_sizes[i]; | |
| 1194 | } | ||
| 1195 | } | ||
| 1196 | 11374 | } | |
| 1197 | |||
| 1198 | /** | ||
| 1199 | * Downscale spectral coefficients for near-clipping windows to avoid artifacts | ||
| 1200 | */ | ||
| 1201 | 9148 | static void avoid_clipping(AACEncContext *s, SingleChannelElement *sce) | |
| 1202 | { | ||
| 1203 | int start, i, j, w; | ||
| 1204 | |||
| 1205 |
2/2✓ Branch 0 taken 113 times.
✓ Branch 1 taken 9035 times.
|
9148 | if (sce->ics.clip_avoidance_factor < 1.0f) { |
| 1206 |
2/2✓ Branch 0 taken 169 times.
✓ Branch 1 taken 113 times.
|
282 | for (w = 0; w < sce->ics.num_windows; w++) { |
| 1207 | 169 | start = 0; | |
| 1208 |
2/2✓ Branch 0 taken 5686 times.
✓ Branch 1 taken 169 times.
|
5855 | for (i = 0; i < sce->ics.max_sfb; i++) { |
| 1209 | 5686 | float *swb_coeffs = &sce->coeffs[start + w*128]; | |
| 1210 |
2/2✓ Branch 0 taken 105544 times.
✓ Branch 1 taken 5686 times.
|
111230 | for (j = 0; j < sce->ics.swb_sizes[i]; j++) |
| 1211 | 105544 | swb_coeffs[j] *= sce->ics.clip_avoidance_factor; | |
| 1212 | 5686 | start += sce->ics.swb_sizes[i]; | |
| 1213 | } | ||
| 1214 | } | ||
| 1215 | } | ||
| 1216 | 9148 | } | |
| 1217 | |||
| 1218 | /** | ||
| 1219 | * Encode one channel of audio data. | ||
| 1220 | */ | ||
| 1221 | 11374 | static int encode_individual_channel(AVCodecContext *avctx, AACEncContext *s, | |
| 1222 | SingleChannelElement *sce, | ||
| 1223 | int common_window) | ||
| 1224 | { | ||
| 1225 | 11374 | put_bits(&s->pb, 8, sce->sf_idx[0]); | |
| 1226 |
2/2✓ Branch 0 taken 870 times.
✓ Branch 1 taken 10504 times.
|
11374 | if (!common_window) |
| 1227 | 870 | put_ics_info(s, &sce->ics); | |
| 1228 | 11374 | encode_band_info(s, sce); | |
| 1229 | 11374 | encode_scale_factors(avctx, s, sce); | |
| 1230 | 11374 | encode_pulses(s, &sce->pulse); | |
| 1231 | 11374 | put_bits(&s->pb, 1, !!sce->tns.present); | |
| 1232 |
1/2✓ Branch 0 taken 11374 times.
✗ Branch 1 not taken.
|
11374 | if (s->coder->encode_tns_info) |
| 1233 | 11374 | s->coder->encode_tns_info(s, sce); | |
| 1234 | 11374 | put_bits(&s->pb, 1, 0); //ssr | |
| 1235 | 11374 | encode_spectral_coeffs(s, sce); | |
| 1236 | 11374 | return 0; | |
| 1237 | } | ||
| 1238 | |||
| 1239 | /** | ||
| 1240 | * Write some auxiliary information about the created AAC file. | ||
| 1241 | */ | ||
| 1242 | 9 | static void put_bitstream_info(AACEncContext *s, const char *name) | |
| 1243 | { | ||
| 1244 | int i, namelen, padbits; | ||
| 1245 | |||
| 1246 | 9 | namelen = strlen(name) + 2; | |
| 1247 | 9 | put_bits(&s->pb, 3, TYPE_FIL); | |
| 1248 | 9 | put_bits(&s->pb, 4, FFMIN(namelen, 15)); | |
| 1249 |
1/2✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
|
9 | if (namelen >= 15) |
| 1250 | 9 | put_bits(&s->pb, 8, namelen - 14); | |
| 1251 | 9 | put_bits(&s->pb, 4, 0); //extension type - filler | |
| 1252 | 9 | padbits = -put_bits_count(&s->pb) & 7; | |
| 1253 | 9 | align_put_bits(&s->pb); | |
| 1254 |
2/2✓ Branch 0 taken 117 times.
✓ Branch 1 taken 9 times.
|
126 | for (i = 0; i < namelen - 2; i++) |
| 1255 | 117 | put_bits(&s->pb, 8, name[i]); | |
| 1256 | 9 | put_bits(&s->pb, 12 - padbits, 0); | |
| 1257 | 9 | } | |
| 1258 | |||
| 1259 | /* | ||
| 1260 | * Copy input samples. | ||
| 1261 | * Channels are reordered from libavcodec's default order to AAC order. | ||
| 1262 | */ | ||
| 1263 | 4359 | static void copy_input_samples(AACEncContext *s, const AVFrame *frame) | |
| 1264 | { | ||
| 1265 | int ch; | ||
| 1266 |
2/2✓ Branch 0 taken 4305 times.
✓ Branch 1 taken 54 times.
|
4359 | int end = 2048 + (frame ? frame->nb_samples : 0); |
| 1267 | 4359 | const uint8_t *channel_map = s->reorder_map; | |
| 1268 | |||
| 1269 | /* copy and remap input samples */ | ||
| 1270 |
2/2✓ Branch 0 taken 9295 times.
✓ Branch 1 taken 4359 times.
|
13654 | for (ch = 0; ch < s->channels; ch++) { |
| 1271 | /* copy last 1024 samples of previous frame to the start of the current frame */ | ||
| 1272 | 9295 | memcpy(&s->planar_samples[ch][1024], &s->planar_samples[ch][2048], 1024 * sizeof(s->planar_samples[0][0])); | |
| 1273 | |||
| 1274 | /* copy new samples and zero any remaining samples */ | ||
| 1275 |
2/2✓ Branch 0 taken 9001 times.
✓ Branch 1 taken 294 times.
|
9295 | if (frame) { |
| 1276 | 9001 | memcpy(&s->planar_samples[ch][2048], | |
| 1277 | 9001 | frame->extended_data[channel_map[ch]], | |
| 1278 | 9001 | frame->nb_samples * sizeof(s->planar_samples[0][0])); | |
| 1279 | } | ||
| 1280 | 9295 | memset(&s->planar_samples[ch][end], 0, | |
| 1281 | 9295 | (3072 - end) * sizeof(s->planar_samples[0][0])); | |
| 1282 | } | ||
| 1283 | 4359 | } | |
| 1284 | |||
| 1285 | 4386 | static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, | |
| 1286 | const AVFrame *frame, int *got_packet_ptr) | ||
| 1287 | { | ||
| 1288 | 4386 | AACEncContext *s = avctx->priv_data; | |
| 1289 | 4386 | float **samples = s->planar_samples, *samples2, *la, *overlap; | |
| 1290 | ChannelElement *cpe; | ||
| 1291 | SingleChannelElement *sce; | ||
| 1292 | IndividualChannelStream *ics; | ||
| 1293 | int i, its, ch, w, chans, tag, start_ch, ret, frame_bits; | ||
| 1294 | int target_bits, rate_bits, too_many_bits, too_few_bits; | ||
| 1295 | 4386 | int ms_mode = 0, is_mode = 0, tns_mode = 0, pred_mode = 0; | |
| 1296 | int chan_el_counter[4]; | ||
| 1297 | FFPsyWindowInfo windows[AAC_MAX_CHANNELS]; | ||
| 1298 | |||
| 1299 | /* add current frame to queue */ | ||
| 1300 |
2/2✓ Branch 0 taken 4305 times.
✓ Branch 1 taken 81 times.
|
4386 | if (frame) { |
| 1301 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4305 times.
|
4305 | if ((ret = ff_af_queue_add(&s->afq, frame)) < 0) |
| 1302 | ✗ | return ret; | |
| 1303 | } else { | ||
| 1304 |
3/6✓ Branch 0 taken 54 times.
✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 54 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
81 | if (!s->afq.remaining_samples || (!s->afq.frame_alloc && !s->afq.frame_count)) |
| 1305 | 27 | return 0; | |
| 1306 | } | ||
| 1307 | |||
| 1308 | 4359 | copy_input_samples(s, frame); | |
| 1309 | |||
| 1310 |
2/2✓ Branch 0 taken 27 times.
✓ Branch 1 taken 4332 times.
|
4359 | if (!avctx->frame_num) |
| 1311 | 27 | return 0; | |
| 1312 | |||
| 1313 | 4332 | start_ch = 0; | |
| 1314 |
2/2✓ Branch 0 taken 4989 times.
✓ Branch 1 taken 4332 times.
|
9321 | for (i = 0; i < s->chan_map[0]; i++) { |
| 1315 | 4989 | FFPsyWindowInfo* wi = windows + start_ch; | |
| 1316 | 4989 | tag = s->chan_map[i+1]; | |
| 1317 |
2/2✓ Branch 0 taken 4159 times.
✓ Branch 1 taken 830 times.
|
4989 | chans = tag == TYPE_CPE ? 2 : 1; |
| 1318 | 4989 | cpe = &s->cpe[i]; | |
| 1319 | { | ||
| 1320 | 4989 | int wi_paired = 0; | |
| 1321 | /* Synced pair windows: decide both channels of a CPE together so | ||
| 1322 | * their block switching never diverges (see psy window_pair). */ | ||
| 1323 |
6/8✓ Branch 0 taken 4159 times.
✓ Branch 1 taken 830 times.
✓ Branch 2 taken 4159 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4159 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4039 times.
✓ Branch 7 taken 120 times.
|
4989 | if (chans == 2 && tag != TYPE_LFE && s->psy.model->window_pair && frame) { |
| 1324 | 4039 | const float *ov0 = &samples[start_ch][0], *ov1 = &samples[start_ch + 1][0]; | |
| 1325 | 4039 | s->psy.model->window_pair(&s->psy, | |
| 1326 | ov0 + 1024, ov0 + 1024 + 448 + 64, | ||
| 1327 | ov1 + 1024, ov1 + 1024 + 448 + 64, | ||
| 1328 | start_ch, start_ch + 1, | ||
| 1329 | 4039 | cpe->ch[0].ics.window_sequence[0], | |
| 1330 | 4039 | cpe->ch[1].ics.window_sequence[0], | |
| 1331 | wi); | ||
| 1332 | 4039 | wi_paired = 1; | |
| 1333 | } | ||
| 1334 |
2/2✓ Branch 0 taken 9148 times.
✓ Branch 1 taken 4989 times.
|
14137 | for (ch = 0; ch < chans; ch++) { |
| 1335 | int k; | ||
| 1336 | float clip_avoidance_factor; | ||
| 1337 | 9148 | sce = &cpe->ch[ch]; | |
| 1338 | 9148 | ics = &sce->ics; | |
| 1339 | 9148 | s->cur_channel = start_ch + ch; | |
| 1340 | 9148 | overlap = &samples[s->cur_channel][0]; | |
| 1341 | 9148 | samples2 = overlap + 1024; | |
| 1342 | 9148 | la = samples2 + (448+64); | |
| 1343 |
2/2✓ Branch 0 taken 294 times.
✓ Branch 1 taken 8854 times.
|
9148 | if (!frame) |
| 1344 | 294 | la = NULL; | |
| 1345 |
2/2✓ Branch 0 taken 138 times.
✓ Branch 1 taken 9010 times.
|
9148 | if (tag == TYPE_LFE) { |
| 1346 | 138 | wi[ch].window_type[0] = wi[ch].window_type[1] = ONLY_LONG_SEQUENCE; | |
| 1347 | 138 | wi[ch].window_shape = 0; | |
| 1348 | 138 | wi[ch].num_windows = 1; | |
| 1349 | 138 | wi[ch].grouping[0] = 1; | |
| 1350 | 138 | wi[ch].clipping[0] = 0; | |
| 1351 | |||
| 1352 | /* Only the lowest 12 coefficients are used in a LFE channel. | ||
| 1353 | * The expression below results in only the bottom 8 coefficients | ||
| 1354 | * being used for 11.025kHz to 16kHz sample rates. | ||
| 1355 | */ | ||
| 1356 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 138 times.
|
138 | ics->num_swb = s->samplerate_index >= 8 ? 1 : 3; |
| 1357 |
2/2✓ Branch 0 taken 932 times.
✓ Branch 1 taken 8078 times.
|
9010 | } else if (!wi_paired) { |
| 1358 | 932 | wi[ch] = s->psy.model->window(&s->psy, samples2, la, s->cur_channel, | |
| 1359 | 932 | ics->window_sequence[0]); | |
| 1360 | } | ||
| 1361 | 9148 | ics->window_sequence[1] = ics->window_sequence[0]; | |
| 1362 | 9148 | ics->window_sequence[0] = wi[ch].window_type[0]; | |
| 1363 | 9148 | ics->use_kb_window[1] = ics->use_kb_window[0]; | |
| 1364 | 9148 | ics->use_kb_window[0] = wi[ch].window_shape; | |
| 1365 | 9148 | ics->num_windows = wi[ch].num_windows; | |
| 1366 |
2/2✓ Branch 0 taken 515 times.
✓ Branch 1 taken 8633 times.
|
9148 | ics->swb_sizes = s->psy.bands [ics->num_windows == 8]; |
| 1367 |
4/4✓ Branch 0 taken 138 times.
✓ Branch 1 taken 9010 times.
✓ Branch 2 taken 515 times.
✓ Branch 3 taken 8495 times.
|
9148 | ics->num_swb = tag == TYPE_LFE ? ics->num_swb : s->psy.num_bands[ics->num_windows == 8]; |
| 1368 | 9148 | ics->max_sfb = FFMIN(ics->max_sfb, ics->num_swb); | |
| 1369 | 18296 | ics->swb_offset = wi[ch].window_type[0] == EIGHT_SHORT_SEQUENCE ? | |
| 1370 |
2/2✓ Branch 0 taken 515 times.
✓ Branch 1 taken 8633 times.
|
9148 | ff_swb_offset_128 [s->samplerate_index]: |
| 1371 | 8633 | ff_swb_offset_1024[s->samplerate_index]; | |
| 1372 | 18296 | ics->tns_max_bands = wi[ch].window_type[0] == EIGHT_SHORT_SEQUENCE ? | |
| 1373 |
2/2✓ Branch 0 taken 515 times.
✓ Branch 1 taken 8633 times.
|
9148 | ff_tns_max_bands_128 [s->samplerate_index]: |
| 1374 | 8633 | ff_tns_max_bands_1024[s->samplerate_index]; | |
| 1375 | |||
| 1376 |
2/2✓ Branch 0 taken 12753 times.
✓ Branch 1 taken 9148 times.
|
21901 | for (w = 0; w < ics->num_windows; w++) |
| 1377 | 12753 | ics->group_len[w] = wi[ch].grouping[w]; | |
| 1378 | |||
| 1379 | /* Calculate input sample maximums and evaluate clipping risk */ | ||
| 1380 | 9148 | clip_avoidance_factor = 0.0f; | |
| 1381 |
2/2✓ Branch 0 taken 12753 times.
✓ Branch 1 taken 9148 times.
|
21901 | for (w = 0; w < ics->num_windows; w++) { |
| 1382 | 12753 | const float *wbuf = overlap + w * 128; | |
| 1383 | 12753 | const int wlen = 2048 / ics->num_windows; | |
| 1384 | 12753 | float max = 0; | |
| 1385 | int j; | ||
| 1386 | /* mdct input is 2 * output */ | ||
| 1387 |
2/2✓ Branch 0 taken 18735104 times.
✓ Branch 1 taken 12753 times.
|
18747857 | for (j = 0; j < wlen; j++) |
| 1388 |
2/2✓ Branch 0 taken 18221625 times.
✓ Branch 1 taken 513479 times.
|
18735104 | max = FFMAX(max, fabsf(wbuf[j])); |
| 1389 | 12753 | wi[ch].clipping[w] = max; | |
| 1390 | } | ||
| 1391 | /* Pre-attenuating hot frames costs 0.45 dB of level accuracy on | ||
| 1392 | * loud masters; float decoders don't clip, so the NMR coder | ||
| 1393 | * keeps levels exact. */ | ||
| 1394 |
2/2✓ Branch 0 taken 3408 times.
✓ Branch 1 taken 5740 times.
|
9148 | if (s->options.coder == AAC_CODER_NMR) |
| 1395 |
2/2✓ Branch 0 taken 4913 times.
✓ Branch 1 taken 3408 times.
|
8321 | for (w = 0; w < ics->num_windows; w++) |
| 1396 | 4913 | wi[ch].clipping[w] = 0; | |
| 1397 |
2/2✓ Branch 0 taken 12753 times.
✓ Branch 1 taken 9148 times.
|
21901 | for (w = 0; w < ics->num_windows; w++) { |
| 1398 |
2/2✓ Branch 0 taken 125 times.
✓ Branch 1 taken 12628 times.
|
12753 | if (wi[ch].clipping[w] > CLIP_AVOIDANCE_FACTOR) { |
| 1399 | 125 | ics->window_clipping[w] = 1; | |
| 1400 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 123 times.
|
125 | clip_avoidance_factor = FFMAX(clip_avoidance_factor, wi[ch].clipping[w]); |
| 1401 | } else { | ||
| 1402 | 12628 | ics->window_clipping[w] = 0; | |
| 1403 | } | ||
| 1404 | } | ||
| 1405 |
2/2✓ Branch 0 taken 113 times.
✓ Branch 1 taken 9035 times.
|
9148 | if (clip_avoidance_factor > CLIP_AVOIDANCE_FACTOR) { |
| 1406 | 113 | ics->clip_avoidance_factor = CLIP_AVOIDANCE_FACTOR / clip_avoidance_factor; | |
| 1407 | } else { | ||
| 1408 | 9035 | ics->clip_avoidance_factor = 1.0f; | |
| 1409 | } | ||
| 1410 | |||
| 1411 | 9148 | apply_window_and_mdct(s, sce, overlap); | |
| 1412 | |||
| 1413 |
2/2✓ Branch 0 taken 9367552 times.
✓ Branch 1 taken 9148 times.
|
9376700 | for (k = 0; k < 1024; k++) { |
| 1414 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9367552 times.
|
9367552 | if (!(fabs(cpe->ch[ch].coeffs[k]) < 1E16)) { // Ensure headroom for energy calculation |
| 1415 | ✗ | av_log(avctx, AV_LOG_ERROR, "Input contains (near) NaN/+-Inf\n"); | |
| 1416 | ✗ | return AVERROR(EINVAL); | |
| 1417 | } | ||
| 1418 | } | ||
| 1419 | 9148 | avoid_clipping(s, sce); | |
| 1420 | } | ||
| 1421 | } | ||
| 1422 | 4989 | start_ch += chans; | |
| 1423 | } | ||
| 1424 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4332 times.
|
4332 | if ((ret = ff_alloc_packet(avctx, avpkt, 8192 * s->channels)) < 0) |
| 1425 | ✗ | return ret; | |
| 1426 | 4332 | frame_bits = its = 0; | |
| 1427 | do { | ||
| 1428 | 5405 | init_put_bits(&s->pb, avpkt->data, avpkt->size); | |
| 1429 | |||
| 1430 |
4/4✓ Branch 0 taken 48 times.
✓ Branch 1 taken 5357 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 39 times.
|
5405 | if ((avctx->frame_num & 0xFF)==1 && !(avctx->flags & AV_CODEC_FLAG_BITEXACT)) |
| 1431 | 9 | put_bitstream_info(s, LIBAVCODEC_IDENT); | |
| 1432 | 5405 | start_ch = 0; | |
| 1433 | 5405 | target_bits = 0; | |
| 1434 | 5405 | memset(chan_el_counter, 0, sizeof(chan_el_counter)); | |
| 1435 |
2/2✓ Branch 0 taken 6122 times.
✓ Branch 1 taken 5405 times.
|
11527 | for (i = 0; i < s->chan_map[0]; i++) { |
| 1436 | 6122 | FFPsyWindowInfo* wi = windows + start_ch; | |
| 1437 | const float *coeffs[2]; | ||
| 1438 | 6122 | tag = s->chan_map[i+1]; | |
| 1439 |
2/2✓ Branch 0 taken 5252 times.
✓ Branch 1 taken 870 times.
|
6122 | chans = tag == TYPE_CPE ? 2 : 1; |
| 1440 | 6122 | cpe = &s->cpe[i]; | |
| 1441 | 6122 | cpe->common_window = 0; | |
| 1442 | 6122 | memset(cpe->is_mask, 0, sizeof(cpe->is_mask)); | |
| 1443 | 6122 | memset(cpe->ms_mask, 0, sizeof(cpe->ms_mask)); | |
| 1444 | 6122 | put_bits(&s->pb, 3, tag); | |
| 1445 | 6122 | put_bits(&s->pb, 4, chan_el_counter[tag]++); | |
| 1446 |
2/2✓ Branch 0 taken 11374 times.
✓ Branch 1 taken 6122 times.
|
17496 | for (ch = 0; ch < chans; ch++) { |
| 1447 | 11374 | sce = &cpe->ch[ch]; | |
| 1448 | 11374 | coeffs[ch] = sce->coeffs; | |
| 1449 | 11374 | memset(&sce->tns, 0, sizeof(TemporalNoiseShaping)); | |
| 1450 |
2/2✓ Branch 0 taken 1455872 times.
✓ Branch 1 taken 11374 times.
|
1467246 | for (w = 0; w < 128; w++) |
| 1451 |
2/2✓ Branch 0 taken 25358 times.
✓ Branch 1 taken 1430514 times.
|
1455872 | if (sce->band_type[w] > RESERVED_BT) |
| 1452 | 25358 | sce->band_type[w] = 0; | |
| 1453 | } | ||
| 1454 | 6122 | s->psy.bitres.alloc = -1; | |
| 1455 | 6122 | s->psy.bitres.bits = s->last_frame_pb_count / s->channels; | |
| 1456 | 6122 | s->psy.model->analyze(&s->psy, start_ch, coeffs, wi); | |
| 1457 |
2/2✓ Branch 0 taken 4198 times.
✓ Branch 1 taken 1924 times.
|
6122 | if (s->psy.bitres.alloc > 0) { |
| 1458 | /* Lambda unused here on purpose, we need to take psy's unscaled allocation */ | ||
| 1459 | 8396 | target_bits += s->psy.bitres.alloc | |
| 1460 |
2/2✓ Branch 0 taken 411 times.
✓ Branch 1 taken 3787 times.
|
4198 | * (s->lambda / (avctx->global_quality ? avctx->global_quality : 120)); |
| 1461 | 4198 | s->psy.bitres.alloc /= chans; | |
| 1462 | } | ||
| 1463 | 6122 | s->cur_type = tag; | |
| 1464 |
2/2✓ Branch 0 taken 5252 times.
✓ Branch 1 taken 870 times.
|
6122 | if (chans > 1 |
| 1465 |
1/2✓ Branch 0 taken 5252 times.
✗ Branch 1 not taken.
|
5252 | && wi[0].window_type[0] == wi[1].window_type[0] |
| 1466 |
1/2✓ Branch 0 taken 5252 times.
✗ Branch 1 not taken.
|
5252 | && wi[0].window_shape == wi[1].window_shape) { |
| 1467 | |||
| 1468 | 5252 | cpe->common_window = 1; | |
| 1469 |
2/2✓ Branch 0 taken 7030 times.
✓ Branch 1 taken 5252 times.
|
12282 | for (w = 0; w < wi[0].num_windows; w++) { |
| 1470 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7030 times.
|
7030 | if (wi[0].grouping[w] != wi[1].grouping[w]) { |
| 1471 | ✗ | cpe->common_window = 0; | |
| 1472 | ✗ | break; | |
| 1473 | } | ||
| 1474 | } | ||
| 1475 | } | ||
| 1476 | |||
| 1477 |
3/4✓ Branch 0 taken 2754 times.
✓ Branch 1 taken 3368 times.
✓ Branch 2 taken 2754 times.
✗ Branch 3 not taken.
|
8876 | const int use_tns = s->options.tns && s->coder->search_for_tns && |
| 1478 |
1/2✓ Branch 0 taken 2754 times.
✗ Branch 1 not taken.
|
2754 | s->coder->apply_tns_filt; |
| 1479 | |||
| 1480 | /* The NMR coder rate-controls itself and never re-quantizes, so TNS must run | ||
| 1481 | * before the quantizer */ | ||
| 1482 | 6122 | const int tns_first = s->options.coder == AAC_CODER_NMR; | |
| 1483 |
3/4✓ Branch 0 taken 2071 times.
✓ Branch 1 taken 4051 times.
✓ Branch 2 taken 2071 times.
✗ Branch 3 not taken.
|
6122 | if (tns_first && use_tns) { |
| 1484 |
2/2✓ Branch 0 taken 3408 times.
✓ Branch 1 taken 2071 times.
|
5479 | for (ch = 0; ch < chans; ch++) { |
| 1485 | 3408 | sce = &cpe->ch[ch]; | |
| 1486 | 3408 | s->cur_channel = start_ch + ch; | |
| 1487 | /* mono: mark_pns before TNS so the region cap sees PNS bands. Stereo | ||
| 1488 | * PNS is marked in its own block (below) after the stereo decision. */ | ||
| 1489 |
4/6✓ Branch 0 taken 734 times.
✓ Branch 1 taken 2674 times.
✓ Branch 2 taken 734 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 734 times.
✗ Branch 5 not taken.
|
3408 | if (chans == 1 && s->options.pns && s->coder->mark_pns) |
| 1490 | 734 | s->coder->mark_pns(s, avctx, sce); | |
| 1491 | 3408 | s->coder->search_for_tns(s, sce); | |
| 1492 | 3408 | s->coder->apply_tns_filt(s, sce); | |
| 1493 |
2/2✓ Branch 0 taken 68 times.
✓ Branch 1 taken 3340 times.
|
3408 | if (sce->tns.present) |
| 1494 | 68 | tns_mode = 1; | |
| 1495 | } | ||
| 1496 | } | ||
| 1497 | |||
| 1498 | /* NMR stereo PNS (imaging-safe). Mark each channel's noise-like bands on the | ||
| 1499 | * original L/R psy, then keep PNS only where BOTH channels are noise-like. */ | ||
| 1500 |
5/6✓ Branch 0 taken 5252 times.
✓ Branch 1 taken 870 times.
✓ Branch 2 taken 5252 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1337 times.
✓ Branch 5 taken 3915 times.
|
6122 | if (chans == 2 && cpe->common_window && tns_first && |
| 1501 |
2/4✓ Branch 0 taken 1337 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1337 times.
✗ Branch 3 not taken.
|
1337 | s->options.pns && s->coder->mark_pns) { |
| 1502 | 1337 | s->cur_channel = start_ch; s->coder->mark_pns(s, avctx, &cpe->ch[0]); | |
| 1503 | 1337 | s->cur_channel = start_ch + 1; s->coder->mark_pns(s, avctx, &cpe->ch[1]); | |
| 1504 |
2/2✓ Branch 0 taken 171136 times.
✓ Branch 1 taken 1337 times.
|
172473 | for (int b = 0; b < 128; b++) |
| 1505 |
4/4✓ Branch 0 taken 24710 times.
✓ Branch 1 taken 146426 times.
✓ Branch 2 taken 98 times.
✓ Branch 3 taken 24612 times.
|
171136 | if (!cpe->ch[0].can_pns[b] || !cpe->ch[1].can_pns[b]) |
| 1506 | 146524 | cpe->ch[0].can_pns[b] = cpe->ch[1].can_pns[b] = 0; | |
| 1507 | } | ||
| 1508 | |||
| 1509 | /* The NMR coder decides I/S and M/S BEFORE quantization, from the psy model, | ||
| 1510 | * and the trellis then allocates natively on the coeffs actually coded. */ | ||
| 1511 |
5/6✓ Branch 0 taken 5252 times.
✓ Branch 1 taken 870 times.
✓ Branch 2 taken 5252 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1337 times.
✓ Branch 5 taken 3915 times.
|
6122 | if (chans == 2 && cpe->common_window && s->options.coder == AAC_CODER_NMR && |
| 1512 |
3/4✓ Branch 0 taken 378 times.
✓ Branch 1 taken 959 times.
✓ Branch 2 taken 378 times.
✗ Branch 3 not taken.
|
1337 | (s->options.mid_side || s->options.intensity_stereo)) { |
| 1513 | 1337 | s->cur_channel = start_ch; | |
| 1514 | 1337 | nmr_decide_stereo(s, cpe); | |
| 1515 | } | ||
| 1516 | /* NMR pools the CPE bit budget: both channels of a pair are solved | ||
| 1517 | * jointly under one shared lambda (see aaccoder_nmr.h). */ | ||
| 1518 |
3/4✓ Branch 0 taken 2071 times.
✓ Branch 1 taken 4051 times.
✓ Branch 2 taken 2071 times.
✗ Branch 3 not taken.
|
6122 | if (s->options.coder == AAC_CODER_NMR && s->nmr) |
| 1519 | 2071 | s->nmr->pair = (chans == 2); | |
| 1520 |
2/2✓ Branch 0 taken 11374 times.
✓ Branch 1 taken 6122 times.
|
17496 | for (ch = 0; ch < chans; ch++) { |
| 1521 | 11374 | s->cur_channel = start_ch + ch; | |
| 1522 | /* NMR PNS is mono-only */ | ||
| 1523 |
5/6✓ Branch 0 taken 4638 times.
✓ Branch 1 taken 6736 times.
✓ Branch 2 taken 4638 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1230 times.
✓ Branch 5 taken 3408 times.
|
11374 | if (s->options.pns && s->coder->mark_pns && !tns_first) |
| 1524 | 1230 | s->coder->mark_pns(s, avctx, &cpe->ch[ch]); | |
| 1525 | 11374 | s->coder->search_for_quantizers(avctx, s, &cpe->ch[ch], s->lambda); | |
| 1526 | } | ||
| 1527 |
2/2✓ Branch 0 taken 11374 times.
✓ Branch 1 taken 6122 times.
|
17496 | for (ch = 0; ch < chans; ch++) { /* TNS (non-NMR) and PNS */ |
| 1528 | 11374 | sce = &cpe->ch[ch]; | |
| 1529 | 11374 | s->cur_channel = start_ch + ch; | |
| 1530 |
4/4✓ Branch 0 taken 7966 times.
✓ Branch 1 taken 3408 times.
✓ Branch 2 taken 1230 times.
✓ Branch 3 taken 6736 times.
|
11374 | if (!tns_first && use_tns) { |
| 1531 | 1230 | s->coder->search_for_tns(s, sce); | |
| 1532 | 1230 | s->coder->apply_tns_filt(s, sce); | |
| 1533 |
2/2✓ Branch 0 taken 41 times.
✓ Branch 1 taken 1189 times.
|
1230 | if (sce->tns.present) |
| 1534 | 41 | tns_mode = 1; | |
| 1535 | } | ||
| 1536 |
4/4✓ Branch 0 taken 4638 times.
✓ Branch 1 taken 6736 times.
✓ Branch 2 taken 1230 times.
✓ Branch 3 taken 3408 times.
|
11374 | if (s->options.pns && s->coder->search_for_pns) |
| 1537 | 1230 | s->coder->search_for_pns(s, avctx, sce); | |
| 1538 | } | ||
| 1539 | 6122 | s->cur_channel = start_ch; | |
| 1540 |
2/2✓ Branch 0 taken 2755 times.
✓ Branch 1 taken 3367 times.
|
6122 | if (s->options.intensity_stereo) { /* Intensity Stereo */ |
| 1541 |
2/2✓ Branch 0 taken 684 times.
✓ Branch 1 taken 2071 times.
|
2755 | if (s->options.coder != AAC_CODER_NMR) { /* NMR: decided pre-search */ |
| 1542 |
1/2✓ Branch 0 taken 684 times.
✗ Branch 1 not taken.
|
684 | if (s->coder->search_for_is) |
| 1543 | 684 | s->coder->search_for_is(s, avctx, cpe); | |
| 1544 | 684 | apply_intensity_stereo(cpe); | |
| 1545 | } | ||
| 1546 |
2/2✓ Branch 0 taken 1513 times.
✓ Branch 1 taken 1242 times.
|
2755 | if (cpe->is_mode) is_mode = 1; |
| 1547 | } | ||
| 1548 |
4/4✓ Branch 0 taken 1889 times.
✓ Branch 1 taken 4233 times.
✓ Branch 2 taken 412 times.
✓ Branch 3 taken 1477 times.
|
6122 | if (s->options.mid_side && s->options.coder != AAC_CODER_NMR) { /* Mid/Side stereo */ |
| 1549 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 412 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
412 | if (s->options.mid_side == -1 && s->coder->search_for_ms) |
| 1550 | ✗ | s->coder->search_for_ms(s, cpe); | |
| 1551 |
1/2✓ Branch 0 taken 412 times.
✗ Branch 1 not taken.
|
412 | else if (cpe->common_window) |
| 1552 | 412 | memset(cpe->ms_mask, 1, sizeof(cpe->ms_mask)); | |
| 1553 | 412 | apply_mid_side_stereo(cpe); | |
| 1554 | } | ||
| 1555 | 6122 | adjust_frame_information(cpe, chans); | |
| 1556 |
2/2✓ Branch 0 taken 5252 times.
✓ Branch 1 taken 870 times.
|
6122 | if (chans == 2) { |
| 1557 | 5252 | put_bits(&s->pb, 1, cpe->common_window); | |
| 1558 |
1/2✓ Branch 0 taken 5252 times.
✗ Branch 1 not taken.
|
5252 | if (cpe->common_window) { |
| 1559 | 5252 | put_ics_info(s, &cpe->ch[0].ics); | |
| 1560 | 5252 | encode_ms_info(&s->pb, cpe); | |
| 1561 |
2/2✓ Branch 0 taken 1657 times.
✓ Branch 1 taken 3595 times.
|
5252 | if (cpe->ms_mode) ms_mode = 1; |
| 1562 | } | ||
| 1563 | } | ||
| 1564 |
2/2✓ Branch 0 taken 11374 times.
✓ Branch 1 taken 6122 times.
|
17496 | for (ch = 0; ch < chans; ch++) { |
| 1565 | 11374 | s->cur_channel = start_ch + ch; | |
| 1566 | 11374 | encode_individual_channel(avctx, s, &cpe->ch[ch], cpe->common_window); | |
| 1567 | } | ||
| 1568 | 6122 | start_ch += chans; | |
| 1569 | } | ||
| 1570 | |||
| 1571 |
2/2✓ Branch 0 taken 411 times.
✓ Branch 1 taken 4994 times.
|
5405 | if (avctx->flags & AV_CODEC_FLAG_QSCALE) { |
| 1572 | /* When using a constant Q-scale, don't mess with lambda, unless | ||
| 1573 | * the frame does not fit the decoder buffer: retry coarser (the | ||
| 1574 | * coders' legality caps shrink with lambda) */ | ||
| 1575 | 411 | frame_bits = put_bits_count(&s->pb); | |
| 1576 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 411 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
411 | if (frame_bits < 6144 * s->channels - 3 || its >= 16) |
| 1577 | break; | ||
| 1578 | ✗ | s->lambda *= FFMIN(0.9f, (6144.0f * s->channels - 3) / frame_bits); | |
| 1579 | ✗ | for (i = 0; i < s->chan_map[0]; i++) { | |
| 1580 | ✗ | chans = s->chan_map[i + 1] == TYPE_CPE ? 2 : 1; | |
| 1581 | ✗ | for (ch = 0; ch < chans; ch++) | |
| 1582 | ✗ | memcpy(s->cpe[i].ch[ch].coeffs, s->cpe[i].ch[ch].pcoeffs, | |
| 1583 | sizeof(s->cpe[i].ch[ch].coeffs)); | ||
| 1584 | } | ||
| 1585 | ✗ | its++; | |
| 1586 | ✗ | continue; | |
| 1587 | } | ||
| 1588 | |||
| 1589 | 4994 | frame_bits = put_bits_count(&s->pb); | |
| 1590 | |||
| 1591 | /* The NMR coder rate-controls itself (global-lambda reservoir servo): | ||
| 1592 | * per-frame bits intentionally float around the nominal rate, so skip | ||
| 1593 | * the lambda rate loop and only intervene on a hard overflow. */ | ||
| 1594 |
3/4✓ Branch 0 taken 1147 times.
✓ Branch 1 taken 3847 times.
✓ Branch 2 taken 1147 times.
✗ Branch 3 not taken.
|
4994 | if (s->options.coder == AAC_CODER_NMR && avctx->bit_rate_tolerance != 0 && |
| 1595 |
1/2✓ Branch 0 taken 1147 times.
✗ Branch 1 not taken.
|
1147 | frame_bits < 6144 * s->channels - 3) |
| 1596 | 1147 | break; | |
| 1597 | |||
| 1598 | /* rate control stuff | ||
| 1599 | * allow between the nominal bitrate, and what psy's bit reservoir says to target | ||
| 1600 | * but drift towards the nominal bitrate always | ||
| 1601 | */ | ||
| 1602 | 3847 | rate_bits = avctx->bit_rate * 1024 / avctx->sample_rate; | |
| 1603 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3847 times.
|
3847 | rate_bits = FFMIN(rate_bits, 6144 * s->channels - 3); |
| 1604 | 3847 | too_many_bits = FFMAX(target_bits, rate_bits); | |
| 1605 |
2/2✓ Branch 0 taken 866 times.
✓ Branch 1 taken 2981 times.
|
3847 | too_many_bits = FFMIN(too_many_bits, 6144 * s->channels - 3); |
| 1606 | 3847 | too_few_bits = FFMIN(FFMAX(rate_bits - rate_bits/4, target_bits), too_many_bits); | |
| 1607 | |||
| 1608 | /* When strict bit-rate control is demanded */ | ||
| 1609 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3847 times.
|
3847 | if (avctx->bit_rate_tolerance == 0) { |
| 1610 | ✗ | if (rate_bits < frame_bits) { | |
| 1611 | ✗ | float ratio = ((float)rate_bits) / frame_bits; | |
| 1612 | ✗ | s->lambda *= FFMIN(0.9f, ratio); | |
| 1613 | ✗ | continue; | |
| 1614 | } | ||
| 1615 | /* reset lambda when solution is found */ | ||
| 1616 | ✗ | s->lambda = avctx->global_quality > 0 ? avctx->global_quality : 120; | |
| 1617 | ✗ | break; | |
| 1618 | } | ||
| 1619 | |||
| 1620 | /* When using ABR, be strict (but only for increasing) */ | ||
| 1621 | 3847 | too_few_bits = too_few_bits - too_few_bits/8; | |
| 1622 | 3847 | too_many_bits = too_many_bits + too_many_bits/2; | |
| 1623 | |||
| 1624 |
2/2✓ Branch 0 taken 1073 times.
✓ Branch 1 taken 2774 times.
|
3847 | if ( its == 0 /* for steady-state Q-scale tracking */ |
| 1625 |
6/6✓ Branch 0 taken 859 times.
✓ Branch 1 taken 214 times.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 848 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 8 times.
|
1073 | || (its < 5 && (frame_bits < too_few_bits || frame_bits > too_many_bits)) |
| 1626 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 217 times.
|
217 | || frame_bits >= 6144 * s->channels - 3 ) |
| 1627 | { | ||
| 1628 | 3630 | float ratio = ((float)rate_bits) / frame_bits; | |
| 1629 | |||
| 1630 |
4/4✓ Branch 0 taken 2516 times.
✓ Branch 1 taken 1114 times.
✓ Branch 2 taken 2506 times.
✓ Branch 3 taken 10 times.
|
3630 | if (frame_bits >= too_few_bits && frame_bits <= too_many_bits) { |
| 1631 | /* | ||
| 1632 | * This path is for steady-state Q-scale tracking | ||
| 1633 | * When frame bits fall within the stable range, we still need to adjust | ||
| 1634 | * lambda to maintain it like so in a stable fashion (large jumps in lambda | ||
| 1635 | * create artifacts and should be avoided), but slowly | ||
| 1636 | */ | ||
| 1637 | 2506 | ratio = sqrtf(sqrtf(ratio)); | |
| 1638 | 2506 | ratio = av_clipf(ratio, 0.9f, 1.1f); | |
| 1639 | } else { | ||
| 1640 | /* Not so fast though */ | ||
| 1641 | 1124 | ratio = sqrtf(ratio); | |
| 1642 | } | ||
| 1643 | 3630 | s->lambda = av_clipf(s->lambda * ratio, FLT_EPSILON, 65536.f); | |
| 1644 | |||
| 1645 | /* Keep iterating if we must reduce and lambda is in the sky */ | ||
| 1646 |
4/4✓ Branch 0 taken 3615 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 1058 times.
✓ Branch 3 taken 2557 times.
|
3630 | if (ratio > 0.9f && ratio < 1.1f) { |
| 1647 | break; | ||
| 1648 | } else { | ||
| 1649 |
7/8✓ Branch 0 taken 1072 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1071 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1066 times.
✓ Branch 5 taken 5 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1066 times.
|
1073 | if (is_mode || ms_mode || tns_mode || pred_mode) { |
| 1650 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 7 times.
|
29 | for (i = 0; i < s->chan_map[0]; i++) { |
| 1651 | // Must restore coeffs | ||
| 1652 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 20 times.
|
22 | chans = tag == TYPE_CPE ? 2 : 1; |
| 1653 | 22 | cpe = &s->cpe[i]; | |
| 1654 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 22 times.
|
46 | for (ch = 0; ch < chans; ch++) |
| 1655 | 24 | memcpy(cpe->ch[ch].coeffs, cpe->ch[ch].pcoeffs, sizeof(cpe->ch[ch].coeffs)); | |
| 1656 | } | ||
| 1657 | } | ||
| 1658 | 1073 | its++; | |
| 1659 | } | ||
| 1660 | } else { | ||
| 1661 | break; | ||
| 1662 | } | ||
| 1663 | } while (1); | ||
| 1664 |
2/2✓ Branch 0 taken 411 times.
✓ Branch 1 taken 3921 times.
|
4332 | if (avctx->flags & AV_CODEC_FLAG_QSCALE) |
| 1665 |
1/2✓ Branch 0 taken 411 times.
✗ Branch 1 not taken.
|
411 | s->lambda = avctx->global_quality > 0 ? avctx->global_quality : 120; |
| 1666 | |||
| 1667 | /* tool-usage stats over the final per-band decisions of this frame */ | ||
| 1668 |
2/2✓ Branch 0 taken 4989 times.
✓ Branch 1 taken 4332 times.
|
9321 | for (i = 0; i < s->chan_map[0]; i++) { |
| 1669 |
2/2✓ Branch 0 taken 4159 times.
✓ Branch 1 taken 830 times.
|
4989 | int etag = s->chan_map[i + 1], echans = etag == TYPE_CPE ? 2 : 1; |
| 1670 | 4989 | ChannelElement *ce = &s->cpe[i]; | |
| 1671 | 4989 | IndividualChannelStream *ics = &ce->ch[0].ics; | |
| 1672 |
2/2✓ Branch 0 taken 9148 times.
✓ Branch 1 taken 4989 times.
|
14137 | for (ch = 0; ch < echans; ch++) { /* per-channel frame stats */ |
| 1673 | 9148 | int is_short = ce->ch[ch].ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE; | |
| 1674 | 9148 | s->stat_chans++; | |
| 1675 |
2/2✓ Branch 0 taken 515 times.
✓ Branch 1 taken 8633 times.
|
9148 | if (is_short) |
| 1676 | 515 | s->stat_short++; | |
| 1677 |
2/2✓ Branch 0 taken 104 times.
✓ Branch 1 taken 9044 times.
|
9148 | if (ce->ch[ch].tns.present) { |
| 1678 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
|
104 | if (is_short) s->stat_tns_short++; |
| 1679 | 104 | else s->stat_tns_long++; | |
| 1680 | } | ||
| 1681 | } | ||
| 1682 |
2/2✓ Branch 0 taken 5350 times.
✓ Branch 1 taken 4989 times.
|
10339 | for (w = 0; w < ics->num_windows; w += ics->group_len[w]) { |
| 1683 |
2/2✓ Branch 0 taken 233857 times.
✓ Branch 1 taken 5350 times.
|
239207 | for (int g = 0; g < ics->num_swb; g++) { |
| 1684 | 233857 | int idx = w*16 + g, coded = 0; | |
| 1685 |
2/2✓ Branch 0 taken 433749 times.
✓ Branch 1 taken 233857 times.
|
667606 | for (ch = 0; ch < echans; ch++) { |
| 1686 | 433749 | SingleChannelElement *sce = &ce->ch[ch]; | |
| 1687 |
4/4✓ Branch 0 taken 26589 times.
✓ Branch 1 taken 407160 times.
✓ Branch 2 taken 24711 times.
✓ Branch 3 taken 1878 times.
|
433749 | if (sce->zeroes[idx] && sce->band_type[idx] == 0) |
| 1688 | 24711 | continue; | |
| 1689 | 409038 | s->stat_ch_bands++; | |
| 1690 |
2/2✓ Branch 0 taken 3141 times.
✓ Branch 1 taken 405897 times.
|
409038 | if (sce->band_type[idx] == NOISE_BT) |
| 1691 | 3141 | s->stat_pns++; | |
| 1692 | 409038 | coded = 1; | |
| 1693 | } | ||
| 1694 |
4/4✓ Branch 0 taken 199892 times.
✓ Branch 1 taken 33965 times.
✓ Branch 2 taken 193322 times.
✓ Branch 3 taken 6570 times.
|
233857 | if (etag == TYPE_CPE && coded) { |
| 1695 | 193322 | s->stat_cpe_bands++; | |
| 1696 |
2/2✓ Branch 0 taken 47576 times.
✓ Branch 1 taken 145746 times.
|
193322 | if (ce->ms_mask[idx]) s->stat_ms++; |
| 1697 |
2/2✓ Branch 0 taken 22764 times.
✓ Branch 1 taken 170558 times.
|
193322 | if (ce->is_mask[idx]) s->stat_is++; |
| 1698 | } | ||
| 1699 | } | ||
| 1700 | } | ||
| 1701 | } | ||
| 1702 | |||
| 1703 | 4332 | put_bits(&s->pb, 3, TYPE_END); | |
| 1704 | 4332 | flush_put_bits(&s->pb); | |
| 1705 | |||
| 1706 | 4332 | s->last_frame_pb_count = put_bits_count(&s->pb); | |
| 1707 | |||
| 1708 | /* NMR rate accounting: how many bits the frame really took beyond what the | ||
| 1709 | * trellis counted; feeds the next frame's budget correction */ | ||
| 1710 |
2/2✓ Branch 0 taken 1558 times.
✓ Branch 1 taken 2774 times.
|
4332 | if (s->nmr) { |
| 1711 | 1558 | int counted = 0; | |
| 1712 |
2/2✓ Branch 0 taken 3408 times.
✓ Branch 1 taken 1558 times.
|
4966 | for (i = 0; i < s->channels; i++) |
| 1713 | 3408 | counted += s->nmr->counted[i]; | |
| 1714 |
2/2✓ Branch 0 taken 1552 times.
✓ Branch 1 taken 6 times.
|
1558 | if (counted > 0) { |
| 1715 | 1552 | float side = (float)s->last_frame_pb_count - counted; | |
| 1716 |
2/2✓ Branch 0 taken 1534 times.
✓ Branch 1 taken 18 times.
|
1552 | if (s->nmr->side_inited) { |
| 1717 | 1534 | s->nmr->side_ema += 0.125f * (side - s->nmr->side_ema); | |
| 1718 | } else { | ||
| 1719 | 18 | s->nmr->side_ema = side; | |
| 1720 | 18 | s->nmr->side_inited = 1; | |
| 1721 | } | ||
| 1722 | } | ||
| 1723 | } | ||
| 1724 | 4332 | avpkt->size = put_bytes_output(&s->pb); | |
| 1725 | |||
| 1726 | /* NMR reports its real operating lambda: the corridor centre in CBR, | ||
| 1727 | * the quality-mode slew state in VBR/ABR - the lambda of the last long | ||
| 1728 | * operating-point solve, which short frames and legality re-solves do | ||
| 1729 | * not update (the outer-loop s->lambda is never touched for this coder | ||
| 1730 | * and would pin Qavg at its 120 init) */ | ||
| 1731 |
2/2✓ Branch 0 taken 1552 times.
✓ Branch 1 taken 6 times.
|
1558 | s->lambda_sum += (s->nmr && s->nmr->lam_slew > 0.0f && |
| 1732 |
4/4✓ Branch 0 taken 1141 times.
✓ Branch 1 taken 411 times.
✓ Branch 2 taken 411 times.
✓ Branch 3 taken 730 times.
|
1552 | ((avctx->flags & AV_CODEC_FLAG_QSCALE) || s->options.rc == 1)) ? |
| 1733 |
2/2✓ Branch 0 taken 1558 times.
✓ Branch 1 taken 2774 times.
|
8664 | s->nmr->lam_slew : |
| 1734 |
4/4✓ Branch 0 taken 736 times.
✓ Branch 1 taken 2774 times.
✓ Branch 2 taken 730 times.
✓ Branch 3 taken 6 times.
|
3510 | (s->nmr && s->nmr->lam_rc > 0.0f) ? s->nmr->lam_rc : s->lambda; |
| 1735 | 4332 | s->lambda_count++; | |
| 1736 | |||
| 1737 | 4332 | ret = ff_af_queue_remove(&s->afq, avctx->frame_size, avpkt); | |
| 1738 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4332 times.
|
4332 | if (ret < 0) |
| 1739 | ✗ | return ret; | |
| 1740 | |||
| 1741 | 4332 | avpkt->flags |= AV_PKT_FLAG_KEY; | |
| 1742 | |||
| 1743 | 4332 | *got_packet_ptr = 1; | |
| 1744 | 4332 | return 0; | |
| 1745 | } | ||
| 1746 | |||
| 1747 | 27 | static av_cold int aac_encode_end(AVCodecContext *avctx) | |
| 1748 | { | ||
| 1749 | 27 | AACEncContext *s = avctx->priv_data; | |
| 1750 | |||
| 1751 | 38 | av_log(avctx, AV_LOG_INFO, | |
| 1752 | "Qavg: %.3f Tr: %.1f%% TNS(L): %.1f%% TNS(S): %.1f%% M/S: %.1f%% I/S: %.1f%% PNS: %.1f%%\n", | ||
| 1753 |
1/2✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
|
27 | s->lambda_count ? s->lambda_sum / s->lambda_count : NAN, |
| 1754 |
1/2✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
|
27 | s->stat_chans ? 100.0 * s->stat_short / s->stat_chans : 0.0, |
| 1755 |
1/2✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
|
27 | s->stat_chans - s->stat_short ? 100.0 * s->stat_tns_long / (s->stat_chans - s->stat_short) : 0.0, |
| 1756 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 6 times.
|
27 | s->stat_short ? 100.0 * s->stat_tns_short / s->stat_short : 0.0, |
| 1757 |
2/2✓ Branch 0 taken 25 times.
✓ Branch 1 taken 2 times.
|
27 | s->stat_cpe_bands ? 100.0 * s->stat_ms / s->stat_cpe_bands : 0.0, |
| 1758 |
2/2✓ Branch 0 taken 25 times.
✓ Branch 1 taken 2 times.
|
27 | s->stat_cpe_bands ? 100.0 * s->stat_is / s->stat_cpe_bands : 0.0, |
| 1759 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 1 times.
|
27 | s->stat_ch_bands ? 100.0 * s->stat_pns / s->stat_ch_bands : 0.0); |
| 1760 | |||
| 1761 | 27 | av_tx_uninit(&s->mdct1024); | |
| 1762 | 27 | av_tx_uninit(&s->mdct128); | |
| 1763 | 27 | ff_psy_end(&s->psy); | |
| 1764 | 27 | ff_lpc_end(&s->lpc); | |
| 1765 | 27 | av_freep(&s->buffer.samples); | |
| 1766 | 27 | av_freep(&s->cpe); | |
| 1767 | 27 | av_freep(&s->fdsp); | |
| 1768 | 27 | av_freep(&s->nmr); | |
| 1769 | 27 | ff_af_queue_close(&s->afq); | |
| 1770 | 27 | return 0; | |
| 1771 | } | ||
| 1772 | |||
| 1773 | 27 | static av_cold int dsp_init(AVCodecContext *avctx, AACEncContext *s) | |
| 1774 | { | ||
| 1775 | 27 | int ret = 0; | |
| 1776 | 27 | float scale = 32768.0f; | |
| 1777 | |||
| 1778 | 27 | s->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT); | |
| 1779 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 | if (!s->fdsp) |
| 1780 | ✗ | return AVERROR(ENOMEM); | |
| 1781 | |||
| 1782 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
|
27 | if ((ret = av_tx_init(&s->mdct1024, &s->mdct1024_fn, AV_TX_FLOAT_MDCT, 0, |
| 1783 | 1024, &scale, 0)) < 0) | ||
| 1784 | ✗ | return ret; | |
| 1785 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
|
27 | if ((ret = av_tx_init(&s->mdct128, &s->mdct128_fn, AV_TX_FLOAT_MDCT, 0, |
| 1786 | 128, &scale, 0)) < 0) | ||
| 1787 | ✗ | return ret; | |
| 1788 | |||
| 1789 | 27 | return 0; | |
| 1790 | } | ||
| 1791 | |||
| 1792 | 27 | static av_cold int alloc_buffers(AVCodecContext *avctx, AACEncContext *s) | |
| 1793 | { | ||
| 1794 | int ch; | ||
| 1795 |
1/2✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
|
27 | if (!FF_ALLOCZ_TYPED_ARRAY(s->buffer.samples, s->channels * 3 * 1024) || |
| 1796 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
|
27 | !FF_ALLOCZ_TYPED_ARRAY(s->cpe, s->chan_map[0])) |
| 1797 | ✗ | return AVERROR(ENOMEM); | |
| 1798 | |||
| 1799 |
2/2✓ Branch 0 taken 147 times.
✓ Branch 1 taken 27 times.
|
174 | for(ch = 0; ch < s->channels; ch++) |
| 1800 | 147 | s->planar_samples[ch] = s->buffer.samples + 3 * 1024 * ch; | |
| 1801 | |||
| 1802 |
2/2✓ Branch 0 taken 19 times.
✓ Branch 1 taken 8 times.
|
27 | if (s->options.coder == AAC_CODER_NMR) { |
| 1803 | 19 | s->nmr = av_mallocz(sizeof(*s->nmr)); | |
| 1804 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
|
19 | if (!s->nmr) |
| 1805 | ✗ | return AVERROR(ENOMEM); | |
| 1806 | } | ||
| 1807 | |||
| 1808 | 27 | return 0; | |
| 1809 | } | ||
| 1810 | |||
| 1811 | 9 | static av_cold int check_height_ext(AVCodecContext *avctx, AACEncContext *s) | |
| 1812 | { | ||
| 1813 |
1/2✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
|
81 | for (int i = 0; i < avctx->ch_layout.nb_channels; i++) { |
| 1814 | 81 | enum AVChannel ch = av_channel_layout_channel_from_index(&avctx->ch_layout, i); | |
| 1815 |
3/4✓ Branch 0 taken 9 times.
✓ Branch 1 taken 72 times.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
81 | if (ch >= AV_CHAN_TOP_FRONT_LEFT && ch <= AV_CHAN_TOP_BACK_RIGHT) |
| 1816 | 9 | return 1; | |
| 1817 | // Layouts with TOP_SIDE channels also include the above. | ||
| 1818 | } | ||
| 1819 | |||
| 1820 | ✗ | return 0; | |
| 1821 | } | ||
| 1822 | |||
| 1823 | 27 | static av_cold int aac_encode_init(AVCodecContext *avctx) | |
| 1824 | { | ||
| 1825 | 27 | AACEncContext *s = avctx->priv_data; | |
| 1826 | 27 | int i, ret = 0; | |
| 1827 | int chcfg; | ||
| 1828 | const uint8_t *sizes[2]; | ||
| 1829 | uint8_t grouping[AAC_MAX_CHANNELS]; | ||
| 1830 | int lengths[2]; | ||
| 1831 | |||
| 1832 | /* Constants */ | ||
| 1833 | 27 | s->last_frame_pb_count = 0; | |
| 1834 | 27 | avctx->frame_size = 1024; | |
| 1835 | 27 | avctx->initial_padding = 1024; | |
| 1836 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 26 times.
|
27 | s->lambda = avctx->global_quality > 0 ? avctx->global_quality : 120; |
| 1837 | |||
| 1838 | /* Channel map and unspecified bitrate guessing */ | ||
| 1839 | 27 | s->channels = avctx->ch_layout.nb_channels; | |
| 1840 | |||
| 1841 | 27 | s->needs_pce = 1; | |
| 1842 |
2/2✓ Branch 0 taken 165 times.
✓ Branch 1 taken 8 times.
|
173 | for (chcfg = 1; chcfg < FF_ARRAY_ELEMS(aac_normal_chan_layouts); chcfg++) { |
| 1843 |
2/2✓ Branch 1 taken 19 times.
✓ Branch 2 taken 146 times.
|
165 | if (!av_channel_layout_compare(&avctx->ch_layout, &aac_normal_chan_layouts[chcfg])) { |
| 1844 | 19 | s->needs_pce = s->options.pce; | |
| 1845 | 19 | break; | |
| 1846 | } | ||
| 1847 | } | ||
| 1848 | |||
| 1849 |
3/6✓ Branch 0 taken 18 times.
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
27 | if (!s->needs_pce && chcfg == 7 /* 7.1(wide) */ && !s->options.allow_71wide) { |
| 1850 | /** | ||
| 1851 | * FFmpeg used to produce out-of-spec AAC files that mistagged 7.1 | ||
| 1852 | * as 7.1(wide), and this wark-around is still enabled by default in | ||
| 1853 | * aacdec.c, so avoid producing such files in the rare case that the | ||
| 1854 | * user correctly passed 7.1(wide) channel layout content. | ||
| 1855 | */ | ||
| 1856 | ✗ | av_log(avctx, AV_LOG_INFO, "Forcing the use of PCE to encode 7.1(wide) " | |
| 1857 | "channel layout to avoid ambiguity. Set -aac_allow_71wide 1 to " | ||
| 1858 | "override this behavior and force the use of spec-compliant " | ||
| 1859 | "channel configuration ID.\n"); | ||
| 1860 | ✗ | s->needs_pce = 1; | |
| 1861 | } | ||
| 1862 | |||
| 1863 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 18 times.
|
27 | if (s->needs_pce) { |
| 1864 | char buf[64]; | ||
| 1865 |
1/2✓ Branch 0 taken 308 times.
✗ Branch 1 not taken.
|
308 | for (i = 0; i < FF_ARRAY_ELEMS(aac_pce_configs); i++) |
| 1866 |
2/2✓ Branch 1 taken 9 times.
✓ Branch 2 taken 299 times.
|
308 | if (!av_channel_layout_compare(&avctx->ch_layout, &aac_pce_configs[i].layout)) |
| 1867 | 9 | break; | |
| 1868 | 9 | av_channel_layout_describe(&avctx->ch_layout, buf, sizeof(buf)); | |
| 1869 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | if (i == FF_ARRAY_ELEMS(aac_pce_configs)) { |
| 1870 | ✗ | av_log(avctx, AV_LOG_ERROR, "Unsupported channel layout \"%s\"\n", buf); | |
| 1871 | ✗ | return AVERROR(EINVAL); | |
| 1872 | } | ||
| 1873 | 9 | av_log(avctx, AV_LOG_INFO, "Using a PCE to encode channel layout \"%s\"\n", buf); | |
| 1874 | 9 | s->pce = aac_pce_configs[i]; | |
| 1875 | 9 | s->reorder_map = s->pce.reorder_map; | |
| 1876 | 9 | s->chan_map = s->pce.config_map; | |
| 1877 | 9 | s->needs_height_ext = check_height_ext(avctx, s); | |
| 1878 | 9 | chcfg = 0; | |
| 1879 | } else { | ||
| 1880 | 18 | s->reorder_map = aac_chan_maps[chcfg - 1]; | |
| 1881 | 18 | s->chan_map = aac_chan_configs[chcfg - 1]; | |
| 1882 | } | ||
| 1883 | |||
| 1884 |
2/2✓ Branch 0 taken 19 times.
✓ Branch 1 taken 8 times.
|
27 | if (!avctx->bit_rate) { |
| 1885 |
2/2✓ Branch 0 taken 79 times.
✓ Branch 1 taken 19 times.
|
98 | for (i = 1; i <= s->chan_map[0]; i++) { |
| 1886 |
2/2✓ Branch 0 taken 27 times.
✓ Branch 1 taken 52 times.
|
106 | avctx->bit_rate += s->chan_map[i] == TYPE_CPE ? 128000 : /* Pair */ |
| 1887 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 16 times.
|
27 | s->chan_map[i] == TYPE_LFE ? 16000 : /* LFE */ |
| 1888 | 69000 ; /* SCE */ | ||
| 1889 | } | ||
| 1890 | } | ||
| 1891 | |||
| 1892 | /* Samplerate */ | ||
| 1893 | 27 | for (int i = 0;; i++) { | |
| 1894 | 107 | av_assert1(i < 13); | |
| 1895 |
2/2✓ Branch 0 taken 27 times.
✓ Branch 1 taken 107 times.
|
134 | if (avctx->sample_rate == ff_mpeg4audio_sample_rates[i]) { |
| 1896 | 27 | s->samplerate_index = i; | |
| 1897 | 27 | break; | |
| 1898 | } | ||
| 1899 | } | ||
| 1900 | |||
| 1901 | /* Bitrate limiting */ | ||
| 1902 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 | WARN_IF(1024.0 * avctx->bit_rate / avctx->sample_rate > 6144 * s->channels, |
| 1903 | "Too many bits %f > %d per frame requested, clamping to max\n", | ||
| 1904 | 1024.0 * avctx->bit_rate / avctx->sample_rate, | ||
| 1905 | 6144 * s->channels); | ||
| 1906 |
1/2✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
|
27 | avctx->bit_rate = (int64_t)FFMIN(6144 * s->channels / 1024.0 * avctx->sample_rate, |
| 1907 | avctx->bit_rate); | ||
| 1908 | |||
| 1909 | /* Profile and option setting */ | ||
| 1910 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 | avctx->profile = avctx->profile == AV_PROFILE_UNKNOWN ? AV_PROFILE_AAC_LOW : |
| 1911 | avctx->profile; | ||
| 1912 |
1/2✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
|
27 | for (i = 0; i < FF_ARRAY_ELEMS(aacenc_profiles); i++) |
| 1913 |
1/2✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
|
27 | if (avctx->profile == aacenc_profiles[i]) |
| 1914 | 27 | break; | |
| 1915 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 | ERROR_IF(i == FF_ARRAY_ELEMS(aacenc_profiles), "Profile not supported!\n"); |
| 1916 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 | if (avctx->profile == AV_PROFILE_MPEG2_AAC_LOW) { |
| 1917 | ✗ | avctx->profile = AV_PROFILE_AAC_LOW; | |
| 1918 | ✗ | WARN_IF(s->options.pns, | |
| 1919 | "PNS unavailable in the \"mpeg2_aac_low\" profile, turning off\n"); | ||
| 1920 | ✗ | s->options.pns = 0; | |
| 1921 | } | ||
| 1922 | 27 | s->profile = avctx->profile; | |
| 1923 | |||
| 1924 | /* Coder limitations */ | ||
| 1925 | 27 | s->coder = &ff_aac_coders[s->options.coder]; | |
| 1926 | |||
| 1927 | /* M/S introduces horrible artifacts with multichannel files, this is temporary */ | ||
| 1928 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 17 times.
|
27 | if (s->channels > 3) |
| 1929 | 10 | s->options.mid_side = 0; | |
| 1930 | |||
| 1931 | /* Coding bandwidth, fixed at init time */ | ||
| 1932 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 22 times.
|
27 | if (avctx->cutoff > 0) { |
| 1933 | 5 | s->bandwidth = avctx->cutoff; | |
| 1934 | } else { | ||
| 1935 | int frame_br; | ||
| 1936 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 21 times.
|
22 | if (avctx->flags & AV_CODEC_FLAG_QSCALE) { |
| 1937 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (s->options.coder == AAC_CODER_NMR) { |
| 1938 | /* nd-target VBR: expected per-channel rate from the quality | ||
| 1939 | * ladder (measured: q=1 ~ 64.5 kbps/ch, x1.27 per doubling) */ | ||
| 1940 | 2 | float q = avctx->global_quality > 0 ? | |
| 1941 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | avctx->global_quality / (float)FF_QP2LAMBDA : 1.0f; |
| 1942 | 1 | frame_br = 66000 * powf(q, 0.29f); | |
| 1943 | } else { | ||
| 1944 | ✗ | frame_br = avctx->bit_rate / 2.0f * (s->lambda / 120.f) * 1.5f; | |
| 1945 | } | ||
| 1946 | } else { | ||
| 1947 | 21 | frame_br = avctx->bit_rate / avctx->ch_layout.nb_channels; | |
| 1948 | } | ||
| 1949 | |||
| 1950 |
3/4✓ Branch 0 taken 19 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
|
22 | if (s->options.coder == AAC_CODER_NMR && frame_br >= 24000) { |
| 1951 | /* Ear-tuned, not metric-tuned: Zim rewards HF presence and cannot | ||
| 1952 | * hear HF graininess, so metric sweeps push this table wide. At | ||
| 1953 | * these rates coarse HF reads as beat-synchronous crunch (velvet); | ||
| 1954 | * FDK sits at 14k and Apple at ~16k for 64 kbps/ch. */ | ||
| 1955 | static const int rates[] = { 24000, 32000, 48000, 64000, 96000, 192000 }; | ||
| 1956 | static const int bws[] = { 14000, 14000, 15000, 16000, 19500, 22000 }; | ||
| 1957 | 19 | int bw_i = 0; | |
| 1958 |
4/4✓ Branch 0 taken 58 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 40 times.
✓ Branch 3 taken 18 times.
|
59 | for (; bw_i < FF_ARRAY_ELEMS(rates) - 2 && frame_br > rates[bw_i + 1]; bw_i++); |
| 1959 | 19 | s->bandwidth = bws[bw_i] + (int)((int64_t)(bws[bw_i + 1] - bws[bw_i]) * | |
| 1960 | 19 | (frame_br - rates[bw_i]) / (rates[bw_i + 1] - rates[bw_i])); | |
| 1961 | 19 | s->bandwidth = FFMIN3(s->bandwidth, 22000, avctx->sample_rate / 2); | |
| 1962 | } else { | ||
| 1963 |
3/4✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
3 | if (s->options.pns || s->options.intensity_stereo) |
| 1964 | 1 | frame_br *= 1.15f; | |
| 1965 |
5/10✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 3 times.
|
3 | s->bandwidth = FFMAX(3000, AAC_CUTOFF_FROM_BITRATE(frame_br, 1, |
| 1966 | avctx->sample_rate)); | ||
| 1967 | } | ||
| 1968 | |||
| 1969 | 22 | s->bandwidth = FFMIN(FFMAX(s->bandwidth, 8000), avctx->sample_rate / 2); | |
| 1970 | } | ||
| 1971 | |||
| 1972 |
3/4✓ Branch 0 taken 26 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
|
27 | if (!(avctx->flags & AV_CODEC_FLAG_QSCALE) && avctx->bit_rate > 0) { |
| 1973 | 26 | int bpc = avctx->bit_rate / avctx->ch_layout.nb_channels; | |
| 1974 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
26 | if (bpc <= 32000 && avctx->sample_rate > 32000) |
| 1975 | ✗ | av_log(avctx, AV_LOG_INFO, | |
| 1976 | "%d kb/s per channel at %d Hz: consider resampling the " | ||
| 1977 | "input to 32000 Hz or lower for better quality.\n", | ||
| 1978 | bpc / 1000, avctx->sample_rate); | ||
| 1979 | } | ||
| 1980 | |||
| 1981 | // Initialize static tables | ||
| 1982 | 27 | ff_aac_float_common_init(); | |
| 1983 | |||
| 1984 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
|
27 | if ((ret = dsp_init(avctx, s)) < 0) |
| 1985 | ✗ | return ret; | |
| 1986 | |||
| 1987 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
|
27 | if ((ret = alloc_buffers(avctx, s)) < 0) |
| 1988 | ✗ | return ret; | |
| 1989 | |||
| 1990 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
|
27 | if ((ret = put_audio_specific_config(avctx, chcfg))) |
| 1991 | ✗ | return ret; | |
| 1992 | |||
| 1993 | 27 | sizes[0] = ff_aac_swb_size_1024[s->samplerate_index]; | |
| 1994 | 27 | sizes[1] = ff_aac_swb_size_128[s->samplerate_index]; | |
| 1995 | 27 | lengths[0] = ff_aac_num_swb_1024[s->samplerate_index]; | |
| 1996 | 27 | lengths[1] = ff_aac_num_swb_128[s->samplerate_index]; | |
| 1997 |
2/2✓ Branch 0 taken 87 times.
✓ Branch 1 taken 27 times.
|
114 | for (i = 0; i < s->chan_map[0]; i++) |
| 1998 | 87 | grouping[i] = s->chan_map[i + 1] == TYPE_CPE; | |
| 1999 |
4/4✓ Branch 0 taken 26 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 25 times.
|
29 | s->psy.unbounded_pe = ((avctx->flags & AV_CODEC_FLAG_QSCALE) || s->options.rc == 1) && |
| 2000 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | s->options.coder == AAC_CODER_NMR; |
| 2001 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 | if ((ret = ff_psy_init(&s->psy, avctx, 2, sizes, lengths, |
| 2002 | 27 | s->chan_map[0], grouping, s->bandwidth)) < 0) | |
| 2003 | ✗ | return ret; | |
| 2004 | |||
| 2005 | 27 | ff_lpc_init(&s->lpc, 2*avctx->frame_size, TNS_MAX_ORDER, FF_LPC_TYPE_LEVINSON); | |
| 2006 | 27 | s->random_state = 0x1f2e3d4c; | |
| 2007 | |||
| 2008 | 27 | ff_aacenc_dsp_init(&s->aacdsp); | |
| 2009 | |||
| 2010 | 27 | ff_af_queue_init(avctx, &s->afq); | |
| 2011 | |||
| 2012 | 27 | return 0; | |
| 2013 | } | ||
| 2014 | |||
| 2015 | #define AACENC_FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM | ||
| 2016 | static const AVOption aacenc_options[] = { | ||
| 2017 | {"aac_coder", "Coding algorithm", offsetof(AACEncContext, options.coder), AV_OPT_TYPE_INT, {.i64 = AAC_CODER_NMR}, 0, AAC_CODER_NB-1, AACENC_FLAGS, .unit = "coder"}, | ||
| 2018 | {"twoloop", "Two loop searching method", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_TWOLOOP}, INT_MIN, INT_MAX, AACENC_FLAGS, .unit = "coder"}, | ||
| 2019 | {"fast", "Fast search", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_FAST}, INT_MIN, INT_MAX, AACENC_FLAGS, .unit = "coder"}, | ||
| 2020 | {"nmr", "Noise-to-mask ratio scalefactor trellis", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_NMR}, INT_MIN, INT_MAX, AACENC_FLAGS, .unit = "coder"}, | ||
| 2021 | {"aac_ms", "Force M/S stereo coding", offsetof(AACEncContext, options.mid_side), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, AACENC_FLAGS}, | ||
| 2022 | {"aac_is", "Intensity stereo coding", offsetof(AACEncContext, options.intensity_stereo), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AACENC_FLAGS}, | ||
| 2023 | {"aac_pns", "Perceptual noise substitution", offsetof(AACEncContext, options.pns), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AACENC_FLAGS}, | ||
| 2024 | {"aac_tns", "Temporal noise shaping", offsetof(AACEncContext, options.tns), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AACENC_FLAGS}, | ||
| 2025 | {"aac_pce", "Forces the use of PCEs", offsetof(AACEncContext, options.pce), AV_OPT_TYPE_BOOL, {.i64 = 0}, -1, 1, AACENC_FLAGS}, | ||
| 2026 | {"aac_rc", "Rate-control mode (NMR coder)", offsetof(AACEncContext, options.rc), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, AACENC_FLAGS, .unit = "aac_rc"}, | ||
| 2027 | {"cbr", "Constant bitrate (corridor + leaky bucket)", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, INT_MIN, INT_MAX, AACENC_FLAGS, .unit = "aac_rc"}, | ||
| 2028 | {"abr", "Average bitrate (constant-quality target, slow rate servo)", 0, AV_OPT_TYPE_CONST, {.i64 = 1}, INT_MIN, INT_MAX, AACENC_FLAGS, .unit = "aac_rc"}, | ||
| 2029 | {"aac_nmr_speed", "NMR coder speed level: 0 = slowest/best, higher trades quality for speed", offsetof(AACEncContext, options.nmr_speed), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 4, AACENC_FLAGS}, | ||
| 2030 | {"aac_allow_71wide", "Allow non-PCE use of 7.1(wide) channel layout", offsetof(AACEncContext, options.allow_71wide), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AACENC_FLAGS}, | ||
| 2031 | FF_AAC_PROFILE_OPTS | ||
| 2032 | {NULL} | ||
| 2033 | }; | ||
| 2034 | |||
| 2035 | static const AVClass aacenc_class = { | ||
| 2036 | .class_name = "AAC encoder", | ||
| 2037 | .item_name = av_default_item_name, | ||
| 2038 | .option = aacenc_options, | ||
| 2039 | .version = LIBAVUTIL_VERSION_INT, | ||
| 2040 | }; | ||
| 2041 | |||
| 2042 | static const FFCodecDefault aac_encode_defaults[] = { | ||
| 2043 | { "b", "0" }, | ||
| 2044 | { NULL } | ||
| 2045 | }; | ||
| 2046 | |||
| 2047 | const FFCodec ff_aac_encoder = { | ||
| 2048 | .p.name = "aac", | ||
| 2049 | CODEC_LONG_NAME("AAC (Advanced Audio Coding)"), | ||
| 2050 | .p.type = AVMEDIA_TYPE_AUDIO, | ||
| 2051 | .p.id = AV_CODEC_ID_AAC, | ||
| 2052 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | | ||
| 2053 | AV_CODEC_CAP_SMALL_LAST_FRAME, | ||
| 2054 | .priv_data_size = sizeof(AACEncContext), | ||
| 2055 | .init = aac_encode_init, | ||
| 2056 | FF_CODEC_ENCODE_CB(aac_encode_frame), | ||
| 2057 | .close = aac_encode_end, | ||
| 2058 | .defaults = aac_encode_defaults, | ||
| 2059 | CODEC_SAMPLERATES_ARRAY(ff_mpeg4audio_sample_rates), | ||
| 2060 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
| 2061 | CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_FLTP), | ||
| 2062 | .p.priv_class = &aacenc_class, | ||
| 2063 | }; | ||
| 2064 |