| 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_7POINT1POINT2, | ||
| 361 | .num_ele = { 3, 0, 2, 1 }, | ||
| 362 | .pairing = { { 0, 1, 1 }, { 0 }, { 1, 1 }, }, | ||
| 363 | .index = { { 0, 0, 3 }, { 0 }, { 2, 1 }, { 0 } }, | ||
| 364 | .height = { { 0, 0, 1 }, { 0 }, { 0, 0 } }, | ||
| 365 | .config_map = { 6, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_LFE, TYPE_CPE }, | ||
| 366 | .reorder_map = { 2, 0, 1, 4, 5, 6, 7, 3, 8, 9 }, | ||
| 367 | }, | ||
| 368 | { | ||
| 369 | // ITU-R BS.2051-3 Sound System F | ||
| 370 | .layout = AV_CHANNEL_LAYOUT_7POINT2POINT3, | ||
| 371 | .num_ele = { 3, 0, 3, 2 }, | ||
| 372 | .pairing = { { 0, 1, 1 }, { 0 }, { 1, 1, 0 }, }, | ||
| 373 | .index = { { 0, 0, 3 }, { 0 }, { 2, 1, 1 }, { 0, 1 } }, | ||
| 374 | .height = { { 0, 0, 1 }, { 0 }, { 0, 0, 1 } }, | ||
| 375 | .config_map = { 8, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_LFE, TYPE_LFE, TYPE_CPE, TYPE_SCE }, | ||
| 376 | .reorder_map = { 2, 0, 1, 4, 5, 6, 7, 3, 11, 8, 9, 10 }, | ||
| 377 | }, | ||
| 378 | { | ||
| 379 | // ITU-R BS.2051-3 Sound System J | ||
| 380 | .layout = AV_CHANNEL_LAYOUT_7POINT1POINT4, | ||
| 381 | .num_ele = { 3, 0, 3, 1 }, | ||
| 382 | .pairing = { { 0, 1, 1 }, { 0 }, { 1, 1, 1 }, }, | ||
| 383 | .index = { { 0, 0, 3 }, { 0 }, { 2, 1, 4 }, { 0 } }, | ||
| 384 | .height = { { 0, 0, 1 }, { 0 }, { 0, 0, 1 } }, | ||
| 385 | .config_map = { 7, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_LFE, TYPE_CPE, TYPE_CPE }, | ||
| 386 | .reorder_map = { 2, 0, 1, 4, 5, 6, 7, 3, 8, 9, 10, 11 }, | ||
| 387 | }, | ||
| 388 | { | ||
| 389 | // ITU-R BS.2051-3 Sound System G | ||
| 390 | .layout = AV_CHANNEL_LAYOUT_9POINT1POINT4, | ||
| 391 | .num_ele = { 4, 0, 3, 1 }, | ||
| 392 | .pairing = { { 0, 1, 1, 1 }, { 0 }, { 1, 1, 1 }, }, | ||
| 393 | .index = { { 0, 0, 1, 4 }, { 0 }, { 2, 3, 5 }, { 0 } }, | ||
| 394 | .height = { { 0, 0, 0, 1 }, { 0 }, { 0, 0, 1 } }, | ||
| 395 | .config_map = { 8, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_LFE, TYPE_CPE, TYPE_CPE }, | ||
| 396 | .reorder_map = { 2, 6, 7, 0, 1, 8, 9, 4, 5, 3, 10, 11, 12, 13 }, | ||
| 397 | }, | ||
| 398 | { | ||
| 399 | .layout = AV_CHANNEL_LAYOUT_9POINT1POINT6, | ||
| 400 | .num_ele = { 4, 1, 3, 1 }, | ||
| 401 | .pairing = { { 0, 1, 1, 1 }, { 1 }, { 1, 1, 1 }, }, | ||
| 402 | .index = { { 0, 0, 1, 4 }, { 5 }, { 2, 3, 6 }, { 0 } }, | ||
| 403 | .height = { { 0, 0, 0, 1 }, { 1 }, { 0, 0, 1 } }, | ||
| 404 | .config_map = { 9, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_LFE, TYPE_CPE, TYPE_CPE, TYPE_CPE }, | ||
| 405 | .reorder_map = { 2, 6, 7, 0, 1, 8, 9, 4, 5, 3, 10, 11, 14, 15, 12, 13 }, | ||
| 406 | }, | ||
| 407 | { | ||
| 408 | .layout = AV_CHANNEL_LAYOUT_AMBISONIC_FIRST_ORDER, | ||
| 409 | .num_ele = { 1, 0, 1, 0 }, | ||
| 410 | .pairing = { { 1 }, { 0 }, { 1 }, }, | ||
| 411 | .index = { { 0 }, { 0 }, { 1 } }, | ||
| 412 | .config_map = { 2, TYPE_CPE, TYPE_CPE }, | ||
| 413 | .reorder_map = { 0, 1, 2, 3 }, | ||
| 414 | }, | ||
| 415 | { | ||
| 416 | .layout = { .order = AV_CHANNEL_ORDER_AMBISONIC, .nb_channels = 9 }, | ||
| 417 | .num_ele = { 3, 0, 2, 0 }, | ||
| 418 | .pairing = { { 0, 1, 1 }, { 0 }, { 1, 1 }, }, | ||
| 419 | .index = { { 0, 0, 1 }, { 0 }, { 2, 3 }, }, | ||
| 420 | .config_map = { 5, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_CPE }, | ||
| 421 | .reorder_map = { 2, 5, 6, 0, 1, 7, 8, 3, 4 }, | ||
| 422 | }, | ||
| 423 | { | ||
| 424 | .layout = { .order = AV_CHANNEL_ORDER_AMBISONIC, .nb_channels = 16 }, | ||
| 425 | .num_ele = { 4, 0, 4, 0 }, | ||
| 426 | .pairing = { { 1, 1, 1, 1 }, { 0 }, { 1, 1, 1, 1 }, }, | ||
| 427 | .index = { { 0, 1, 2, 3 }, { 0 }, { 4, 5, 6, 7 }, }, | ||
| 428 | .config_map = { 8, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_CPE }, | ||
| 429 | .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, | ||
| 430 | }, | ||
| 431 | }; | ||
| 432 | |||
| 433 | 7 | static void put_pce(PutBitContext *pb, AVCodecContext *avctx) | |
| 434 | { | ||
| 435 | int i, j; | ||
| 436 | 7 | AACEncContext *s = avctx->priv_data; | |
| 437 | 7 | AACPCEInfo *pce = &s->pce; | |
| 438 | 7 | const int bitexact = avctx->flags & AV_CODEC_FLAG_BITEXACT; | |
| 439 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | const char *aux_data = bitexact ? "Lavc" : LIBAVCODEC_IDENT; |
| 440 | |||
| 441 | 7 | put_bits(pb, 4, 0); | |
| 442 | |||
| 443 | 7 | put_bits(pb, 2, avctx->profile); | |
| 444 | 7 | put_bits(pb, 4, s->samplerate_index); | |
| 445 | |||
| 446 | 7 | put_bits(pb, 4, pce->num_ele[0]); /* Front */ | |
| 447 | 7 | put_bits(pb, 4, pce->num_ele[1]); /* Side */ | |
| 448 | 7 | put_bits(pb, 4, pce->num_ele[2]); /* Back */ | |
| 449 | 7 | put_bits(pb, 2, pce->num_ele[3]); /* LFE */ | |
| 450 | 7 | put_bits(pb, 3, 0); /* Assoc data */ | |
| 451 | 7 | put_bits(pb, 4, 0); /* CCs */ | |
| 452 | |||
| 453 | 7 | put_bits(pb, 1, 0); /* Stereo mixdown */ | |
| 454 | 7 | put_bits(pb, 1, 0); /* Mono mixdown */ | |
| 455 | 7 | put_bits(pb, 1, 0); /* Something else */ | |
| 456 | |||
| 457 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 7 times.
|
35 | for (i = 0; i < 4; i++) { |
| 458 |
2/2✓ Branch 0 taken 49 times.
✓ Branch 1 taken 28 times.
|
77 | for (j = 0; j < pce->num_ele[i]; j++) { |
| 459 |
2/2✓ Branch 0 taken 41 times.
✓ Branch 1 taken 8 times.
|
49 | if (i < 3) |
| 460 | 41 | put_bits(pb, 1, pce->pairing[i][j]); | |
| 461 | 49 | put_bits(pb, 4, pce->index[i][j]); | |
| 462 | } | ||
| 463 | } | ||
| 464 | |||
| 465 | 7 | align_put_bits(pb); | |
| 466 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 | if (s->needs_height_ext) { |
| 467 | 7 | const AVCRC *crc_ctx = av_crc_get_table(AV_CRC_8_ATM); | |
| 468 | PutBitContext height_pb; | ||
| 469 | uint8_t buf[16]; | ||
| 470 | 7 | int bits = 8 + pce->num_ele[0] * 2 + pce->num_ele[1] * 2 + pce->num_ele[2] * 2; | |
| 471 | 7 | int bytes = (bits + 7) / 8; | |
| 472 | |||
| 473 | 7 | init_put_bits(&height_pb, buf, bytes); | |
| 474 | 7 | put_bits(&height_pb, 8, 0xAC); | |
| 475 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 7 times.
|
28 | for (i = 0; i < 3; i++) |
| 476 |
2/2✓ Branch 0 taken 41 times.
✓ Branch 1 taken 21 times.
|
62 | for (j = 0; j < pce->num_ele[i]; j++) |
| 477 | 41 | put_bits(&height_pb, 2, pce->height[i][j]); | |
| 478 | 7 | flush_put_bits(&height_pb); | |
| 479 | |||
| 480 | 7 | put_bits(pb, 8, bytes + 1); | |
| 481 | 7 | ff_copy_bits(pb, buf, bits); | |
| 482 | 7 | align_put_bits(pb); | |
| 483 | 7 | put_bits(pb, 8, av_crc(crc_ctx, 0xFF, buf, bytes)); | |
| 484 | } else { | ||
| 485 | ✗ | put_bits(pb, 8, strlen(aux_data)); | |
| 486 | ✗ | ff_put_string(pb, aux_data, 0); | |
| 487 | } | ||
| 488 | 7 | } | |
| 489 | |||
| 490 | /** | ||
| 491 | * Make AAC audio config object. | ||
| 492 | * @see 1.6.2.1 "Syntax - AudioSpecificConfig" | ||
| 493 | */ | ||
| 494 | 23 | static int put_audio_specific_config(AVCodecContext *avctx, int chcfg) | |
| 495 | { | ||
| 496 | PutBitContext pb; | ||
| 497 | 23 | AACEncContext *s = avctx->priv_data; | |
| 498 | 23 | const int max_size = 32; | |
| 499 | |||
| 500 | 23 | avctx->extradata = av_mallocz(max_size); | |
| 501 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
|
23 | if (!avctx->extradata) |
| 502 | ✗ | return AVERROR(ENOMEM); | |
| 503 | |||
| 504 | 23 | init_put_bits(&pb, avctx->extradata, max_size); | |
| 505 | 23 | put_bits(&pb, 5, s->profile+1); //profile | |
| 506 | 23 | put_bits(&pb, 4, s->samplerate_index); //sample rate index | |
| 507 | 23 | put_bits(&pb, 4, chcfg); | |
| 508 | //GASpecificConfig | ||
| 509 | 23 | put_bits(&pb, 1, 0); //frame length - 1024 samples | |
| 510 | 23 | put_bits(&pb, 1, 0); //does not depend on core coder | |
| 511 | 23 | put_bits(&pb, 1, 0); //is not extension | |
| 512 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 16 times.
|
23 | if (s->needs_pce) |
| 513 | 7 | put_pce(&pb, avctx); | |
| 514 | |||
| 515 | //Explicitly Mark SBR absent | ||
| 516 | 23 | put_bits(&pb, 11, 0x2b7); //sync extension | |
| 517 | 23 | put_bits(&pb, 5, AOT_SBR); | |
| 518 | 23 | put_bits(&pb, 1, 0); | |
| 519 | 23 | flush_put_bits(&pb); | |
| 520 | 23 | avctx->extradata_size = put_bytes_output(&pb); | |
| 521 | |||
| 522 | 23 | return 0; | |
| 523 | } | ||
| 524 | |||
| 525 | 11488 | void ff_quantize_band_cost_cache_init(struct AACEncContext *s) | |
| 526 | { | ||
| 527 | 11488 | ++s->quantize_band_cost_cache_generation; | |
| 528 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11488 times.
|
11488 | if (s->quantize_band_cost_cache_generation == 0) { |
| 529 | ✗ | memset(s->quantize_band_cost_cache, 0, sizeof(s->quantize_band_cost_cache)); | |
| 530 | ✗ | s->quantize_band_cost_cache_generation = 1; | |
| 531 | } | ||
| 532 | 11488 | } | |
| 533 | |||
| 534 | #define WINDOW_FUNC(type) \ | ||
| 535 | static void apply_ ##type ##_window(AVFloatDSPContext *fdsp, \ | ||
| 536 | SingleChannelElement *sce, \ | ||
| 537 | const float *audio) | ||
| 538 | |||
| 539 | 6401 | WINDOW_FUNC(only_long) | |
| 540 | { | ||
| 541 |
2/2✓ Branch 0 taken 6281 times.
✓ Branch 1 taken 120 times.
|
6401 | const float *lwindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024; |
| 542 |
2/2✓ Branch 0 taken 6276 times.
✓ Branch 1 taken 125 times.
|
6401 | const float *pwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024; |
| 543 | 6401 | float *out = sce->ret_buf; | |
| 544 | |||
| 545 | 6401 | fdsp->vector_fmul (out, audio, lwindow, 1024); | |
| 546 | 6401 | fdsp->vector_fmul_reverse(out + 1024, audio + 1024, pwindow, 1024); | |
| 547 | 6401 | } | |
| 548 | |||
| 549 | 278 | WINDOW_FUNC(long_start) | |
| 550 | { | ||
| 551 |
2/2✓ Branch 0 taken 175 times.
✓ Branch 1 taken 103 times.
|
278 | const float *lwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024; |
| 552 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 278 times.
|
278 | const float *swindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128; |
| 553 | 278 | float *out = sce->ret_buf; | |
| 554 | |||
| 555 | 278 | fdsp->vector_fmul(out, audio, lwindow, 1024); | |
| 556 | 278 | memcpy(out + 1024, audio + 1024, sizeof(out[0]) * 448); | |
| 557 | 278 | fdsp->vector_fmul_reverse(out + 1024 + 448, audio + 1024 + 448, swindow, 128); | |
| 558 | 278 | memset(out + 1024 + 576, 0, sizeof(out[0]) * 448); | |
| 559 | 278 | } | |
| 560 | |||
| 561 | 278 | WINDOW_FUNC(long_stop) | |
| 562 | { | ||
| 563 |
1/2✓ Branch 0 taken 278 times.
✗ Branch 1 not taken.
|
278 | const float *lwindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024; |
| 564 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 278 times.
|
278 | const float *swindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128; |
| 565 | 278 | float *out = sce->ret_buf; | |
| 566 | |||
| 567 | 278 | memset(out, 0, sizeof(out[0]) * 448); | |
| 568 | 278 | fdsp->vector_fmul(out + 448, audio + 448, swindow, 128); | |
| 569 | 278 | memcpy(out + 576, audio + 576, sizeof(out[0]) * 448); | |
| 570 | 278 | fdsp->vector_fmul_reverse(out + 1024, audio + 1024, lwindow, 1024); | |
| 571 | 278 | } | |
| 572 | |||
| 573 | 313 | WINDOW_FUNC(eight_short) | |
| 574 | { | ||
| 575 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 313 times.
|
313 | const float *swindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128; |
| 576 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 313 times.
|
313 | const float *pwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128; |
| 577 | 313 | const float *in = audio + 448; | |
| 578 | 313 | float *out = sce->ret_buf; | |
| 579 | int w; | ||
| 580 | |||
| 581 |
2/2✓ Branch 0 taken 2504 times.
✓ Branch 1 taken 313 times.
|
2817 | for (w = 0; w < 8; w++) { |
| 582 |
2/2✓ Branch 0 taken 2191 times.
✓ Branch 1 taken 313 times.
|
2504 | fdsp->vector_fmul (out, in, w ? pwindow : swindow, 128); |
| 583 | 2504 | out += 128; | |
| 584 | 2504 | in += 128; | |
| 585 | 2504 | fdsp->vector_fmul_reverse(out, in, swindow, 128); | |
| 586 | 2504 | out += 128; | |
| 587 | } | ||
| 588 | 313 | } | |
| 589 | |||
| 590 | static void (*const apply_window[4])(AVFloatDSPContext *fdsp, | ||
| 591 | SingleChannelElement *sce, | ||
| 592 | const float *audio) = { | ||
| 593 | [ONLY_LONG_SEQUENCE] = apply_only_long_window, | ||
| 594 | [LONG_START_SEQUENCE] = apply_long_start_window, | ||
| 595 | [EIGHT_SHORT_SEQUENCE] = apply_eight_short_window, | ||
| 596 | [LONG_STOP_SEQUENCE] = apply_long_stop_window | ||
| 597 | }; | ||
| 598 | |||
| 599 | 7270 | static void apply_window_and_mdct(AACEncContext *s, SingleChannelElement *sce, | |
| 600 | float *audio) | ||
| 601 | { | ||
| 602 | int i; | ||
| 603 | 7270 | float *output = sce->ret_buf; | |
| 604 | |||
| 605 | 7270 | apply_window[sce->ics.window_sequence[0]](s->fdsp, sce, audio); | |
| 606 | |||
| 607 |
2/2✓ Branch 0 taken 6957 times.
✓ Branch 1 taken 313 times.
|
7270 | if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) |
| 608 | 6957 | s->mdct1024_fn(s->mdct1024, sce->coeffs, output, sizeof(float)); | |
| 609 | else | ||
| 610 |
2/2✓ Branch 0 taken 2504 times.
✓ Branch 1 taken 313 times.
|
2817 | for (i = 0; i < 1024; i += 128) |
| 611 | 2504 | s->mdct128_fn(s->mdct128, &sce->coeffs[i], output + i*2, sizeof(float)); | |
| 612 | 7270 | memcpy(audio, audio + 1024, sizeof(audio[0]) * 1024); | |
| 613 | 7270 | memcpy(sce->pcoeffs, sce->coeffs, sizeof(sce->pcoeffs)); | |
| 614 | 7270 | } | |
| 615 | |||
| 616 | /** | ||
| 617 | * Encode ics_info element. | ||
| 618 | * @see Table 4.6 (syntax of ics_info) | ||
| 619 | */ | ||
| 620 | 5511 | static void put_ics_info(AACEncContext *s, IndividualChannelStream *info) | |
| 621 | { | ||
| 622 | int w; | ||
| 623 | |||
| 624 | 5511 | put_bits(&s->pb, 1, 0); // ics_reserved bit | |
| 625 | 5511 | put_bits(&s->pb, 2, info->window_sequence[0]); | |
| 626 | 5511 | put_bits(&s->pb, 1, info->use_kb_window[0]); | |
| 627 |
2/2✓ Branch 0 taken 5343 times.
✓ Branch 1 taken 168 times.
|
5511 | if (info->window_sequence[0] != EIGHT_SHORT_SEQUENCE) { |
| 628 | 5343 | put_bits(&s->pb, 6, info->max_sfb); | |
| 629 | 5343 | put_bits(&s->pb, 1, 0); /* No predictor present */ | |
| 630 | } else { | ||
| 631 | 168 | put_bits(&s->pb, 4, info->max_sfb); | |
| 632 |
2/2✓ Branch 0 taken 1176 times.
✓ Branch 1 taken 168 times.
|
1344 | for (w = 1; w < 8; w++) |
| 633 | 1176 | put_bits(&s->pb, 1, !info->group_len[w]); | |
| 634 | } | ||
| 635 | 5511 | } | |
| 636 | |||
| 637 | /** | ||
| 638 | * Encode MS data. | ||
| 639 | * @see 4.6.8.1 "Joint Coding - M/S Stereo" | ||
| 640 | */ | ||
| 641 | 4685 | static void encode_ms_info(PutBitContext *pb, ChannelElement *cpe) | |
| 642 | { | ||
| 643 | int i, w; | ||
| 644 | |||
| 645 | 4685 | put_bits(pb, 2, cpe->ms_mode); | |
| 646 |
2/2✓ Branch 0 taken 366 times.
✓ Branch 1 taken 4319 times.
|
4685 | if (cpe->ms_mode == 1) |
| 647 |
2/2✓ Branch 0 taken 419 times.
✓ Branch 1 taken 366 times.
|
785 | for (w = 0; w < cpe->ch[0].ics.num_windows; w += cpe->ch[0].ics.group_len[w]) |
| 648 |
2/2✓ Branch 0 taken 18021 times.
✓ Branch 1 taken 419 times.
|
18440 | for (i = 0; i < cpe->ch[0].ics.max_sfb; i++) |
| 649 | 18021 | put_bits(pb, 1, cpe->ms_mask[w*16 + i]); | |
| 650 | 4685 | } | |
| 651 | |||
| 652 | /** | ||
| 653 | * Produce integer coefficients from scalefactors provided by the model. | ||
| 654 | */ | ||
| 655 | 5511 | static void adjust_frame_information(ChannelElement *cpe, int chans) | |
| 656 | { | ||
| 657 | int i, w, w2, g, ch; | ||
| 658 | int maxsfb, cmaxsfb; | ||
| 659 | |||
| 660 |
2/2✓ Branch 0 taken 10196 times.
✓ Branch 1 taken 5511 times.
|
15707 | for (ch = 0; ch < chans; ch++) { |
| 661 | 10196 | IndividualChannelStream *ics = &cpe->ch[ch].ics; | |
| 662 | 10196 | maxsfb = 0; | |
| 663 | 10196 | cpe->ch[ch].pulse.num_pulse = 0; | |
| 664 |
2/2✓ Branch 0 taken 11136 times.
✓ Branch 1 taken 10196 times.
|
21332 | for (w = 0; w < ics->num_windows; w += ics->group_len[w]) { |
| 665 |
4/4✓ Branch 0 taken 22486 times.
✓ Branch 1 taken 103 times.
✓ Branch 2 taken 11453 times.
✓ Branch 3 taken 11033 times.
|
22589 | for (cmaxsfb = ics->num_swb; cmaxsfb > 0 && cpe->ch[ch].zeroes[w*16+cmaxsfb-1]; cmaxsfb--) |
| 666 | ; | ||
| 667 | 11136 | maxsfb = FFMAX(maxsfb, cmaxsfb); | |
| 668 | } | ||
| 669 | 10196 | ics->max_sfb = maxsfb; | |
| 670 | |||
| 671 | //adjust zero bands for window groups | ||
| 672 |
2/2✓ Branch 0 taken 11136 times.
✓ Branch 1 taken 10196 times.
|
21332 | for (w = 0; w < ics->num_windows; w += ics->group_len[w]) { |
| 673 |
2/2✓ Branch 0 taken 483155 times.
✓ Branch 1 taken 11136 times.
|
494291 | for (g = 0; g < ics->max_sfb; g++) { |
| 674 | 483155 | i = 1; | |
| 675 |
2/2✓ Branch 0 taken 483245 times.
✓ Branch 1 taken 2261 times.
|
485506 | for (w2 = w; w2 < w + ics->group_len[w]; w2++) { |
| 676 |
2/2✓ Branch 0 taken 480894 times.
✓ Branch 1 taken 2351 times.
|
483245 | if (!cpe->ch[ch].zeroes[w2*16 + g]) { |
| 677 | 480894 | i = 0; | |
| 678 | 480894 | break; | |
| 679 | } | ||
| 680 | } | ||
| 681 | 483155 | cpe->ch[ch].zeroes[w*16 + g] = i; | |
| 682 | } | ||
| 683 | } | ||
| 684 | } | ||
| 685 | |||
| 686 |
3/4✓ Branch 0 taken 4685 times.
✓ Branch 1 taken 826 times.
✓ Branch 2 taken 4685 times.
✗ Branch 3 not taken.
|
5511 | if (chans > 1 && cpe->common_window) { |
| 687 | 4685 | IndividualChannelStream *ics0 = &cpe->ch[0].ics; | |
| 688 | 4685 | IndividualChannelStream *ics1 = &cpe->ch[1].ics; | |
| 689 | 4685 | int msc = 0; | |
| 690 | 4685 | ics0->max_sfb = FFMAX(ics0->max_sfb, ics1->max_sfb); | |
| 691 | 4685 | ics1->max_sfb = ics0->max_sfb; | |
| 692 |
2/2✓ Branch 0 taken 5784 times.
✓ Branch 1 taken 4685 times.
|
10469 | for (w = 0; w < ics0->num_windows*16; w += 16) |
| 693 |
2/2✓ Branch 0 taken 235119 times.
✓ Branch 1 taken 5784 times.
|
240903 | for (i = 0; i < ics0->max_sfb; i++) |
| 694 |
2/2✓ Branch 0 taken 28564 times.
✓ Branch 1 taken 206555 times.
|
235119 | if (cpe->ms_mask[w+i]) |
| 695 | 28564 | msc++; | |
| 696 |
3/4✓ Branch 0 taken 852 times.
✓ Branch 1 taken 3833 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 852 times.
|
4685 | if (msc == 0 || ics0->max_sfb == 0) |
| 697 | 3833 | cpe->ms_mode = 0; | |
| 698 | else | ||
| 699 |
2/2✓ Branch 0 taken 366 times.
✓ Branch 1 taken 486 times.
|
852 | cpe->ms_mode = msc < ics0->max_sfb * ics0->num_windows ? 1 : 2; |
| 700 | } | ||
| 701 | 5511 | } | |
| 702 | |||
| 703 | 740 | static void apply_intensity_stereo(ChannelElement *cpe) | |
| 704 | { | ||
| 705 | int w, w2, g, i; | ||
| 706 | 740 | IndividualChannelStream *ics = &cpe->ch[0].ics; | |
| 707 |
2/2✓ Branch 0 taken 164 times.
✓ Branch 1 taken 576 times.
|
740 | if (!cpe->common_window) |
| 708 | 164 | return; | |
| 709 |
2/2✓ Branch 0 taken 633 times.
✓ Branch 1 taken 576 times.
|
1209 | for (w = 0; w < ics->num_windows; w += ics->group_len[w]) { |
| 710 |
2/2✓ Branch 0 taken 716 times.
✓ Branch 1 taken 633 times.
|
1349 | for (w2 = 0; w2 < ics->group_len[w]; w2++) { |
| 711 | 716 | int start = (w+w2) * 128; | |
| 712 |
2/2✓ Branch 0 taken 29484 times.
✓ Branch 1 taken 716 times.
|
30200 | for (g = 0; g < ics->num_swb; g++) { |
| 713 | 29484 | int p = -1 + 2 * (cpe->ch[1].band_type[w*16+g] - 14); | |
| 714 | 29484 | float scale = cpe->ch[0].is_ener[w*16+g]; | |
| 715 |
2/2✓ Branch 0 taken 20331 times.
✓ Branch 1 taken 9153 times.
|
29484 | if (!cpe->is_mask[w*16 + g]) { |
| 716 | 20331 | start += ics->swb_sizes[g]; | |
| 717 | 20331 | continue; | |
| 718 | } | ||
| 719 |
2/2✓ Branch 0 taken 2365 times.
✓ Branch 1 taken 6788 times.
|
9153 | if (cpe->ms_mask[w*16 + g]) |
| 720 | 2365 | p *= -1; | |
| 721 |
2/2✓ Branch 0 taken 297772 times.
✓ Branch 1 taken 9153 times.
|
306925 | for (i = 0; i < ics->swb_sizes[g]; i++) { |
| 722 | 297772 | float sum = (cpe->ch[0].coeffs[start+i] + p*cpe->ch[1].coeffs[start+i])*scale; | |
| 723 | 297772 | cpe->ch[0].coeffs[start+i] = sum; | |
| 724 | 297772 | cpe->ch[1].coeffs[start+i] = 0.0f; | |
| 725 | } | ||
| 726 | 9153 | start += ics->swb_sizes[g]; | |
| 727 | } | ||
| 728 | } | ||
| 729 | } | ||
| 730 | } | ||
| 731 | |||
| 732 | /* I/S acceptance level for the image-error EMA at full rate pressure */ | ||
| 733 | #define NMR_IS_IMG_GATE 8000.0f | ||
| 734 | |||
| 735 | /* Frequency in Hz for the lower limit of intensity stereo */ | ||
| 736 | #define NMR_IS_LOW_LIMIT 6100 | ||
| 737 | |||
| 738 | /* M/S adoption: es < 0.5*em, content-driven and rate-free */ | ||
| 739 | #define NMR_MS_EQUIV 0.5f | ||
| 740 | #define NMR_MS_MASK 0.0f | ||
| 741 | |||
| 742 | /* Pair decouple threshold on the joint-tool candidacy fraction EMA: pairs | ||
| 743 | * whose joint tools are mostly dead (diffuse decorrelated content) window | ||
| 744 | * per-channel and skip M/S; recouple above 1.3x. */ | ||
| 745 | #define NMR_DECORR_LO 0.20f | ||
| 746 | |||
| 747 | /* Stereo-decision hysteresis: leaving a joint mode costs a margin. */ | ||
| 748 | #define NMR_STICKY 2.0f | ||
| 749 | |||
| 750 | /* Decision statistics are EMA-smoothed across frames. */ | ||
| 751 | #define NMR_SDEC_EMA 0.75f | ||
| 752 | |||
| 753 | /* PNS-stereo gate: substitute only clearly-decorrelated (wide) bands. */ | ||
| 754 | #define NMR_PNS_STEREO_DECORR 0.6f | ||
| 755 | |||
| 756 | /* Recode one band's window group as mid+side in place. */ | ||
| 757 | 5063 | static void nmr_apply_ms_band(AACEncContext *s, ChannelElement *cpe, | |
| 758 | int w, int g, int start, int len, int gl) | ||
| 759 | { | ||
| 760 | 5063 | SingleChannelElement *sce0 = &cpe->ch[0]; | |
| 761 | 5063 | SingleChannelElement *sce1 = &cpe->ch[1]; | |
| 762 | 5063 | cpe->ms_mask[w*16+g] = 1; | |
| 763 |
2/2✓ Branch 0 taken 5161 times.
✓ Branch 1 taken 5063 times.
|
10224 | for (int w2 = 0; w2 < gl; w2++) { |
| 764 | 5161 | FFPsyBand *b0 = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g]; | |
| 765 | 5161 | FFPsyBand *b1 = &s->psy.ch[s->cur_channel+1].psy_bands[(w+w2)*16+g]; | |
| 766 | 5161 | float *L = sce0->coeffs + start + (w+w2)*128; | |
| 767 | 5161 | float *R = sce1->coeffs + start + (w+w2)*128; | |
| 768 | 5161 | float em = 0.0f, es = 0.0f; | |
| 769 |
2/2✓ Branch 0 taken 95768 times.
✓ Branch 1 taken 5161 times.
|
100929 | for (int i = 0; i < len; i++) { |
| 770 | 95768 | float m = (L[i] + R[i]) * 0.5f; | |
| 771 | 95768 | R[i] = m - R[i]; L[i] = m; | |
| 772 | 95768 | em += L[i]*L[i]; es += R[i]*R[i]; | |
| 773 | } | ||
| 774 |
2/2✓ Branch 0 taken 340 times.
✓ Branch 1 taken 4821 times.
|
5161 | b0->threshold = FFMIN(b0->threshold, b1->threshold) * 0.5f; |
| 775 | 5161 | b1->threshold = b0->threshold; | |
| 776 | 5161 | b0->energy = em; b1->energy = es; | |
| 777 | } | ||
| 778 | 5063 | } | |
| 779 | |||
| 780 | /* I/S perceptual test: reconstruction image error vs the pair's masks. */ | ||
| 781 | 8911 | static int nmr_is_image_masked(AACEncContext *s, ChannelElement *cpe, | |
| 782 | int w, int g, int start, int len, int gl, | ||
| 783 | float ener0, float ener1, float dot, | ||
| 784 | float minthr0, float minthr1, float *ratio_out, | ||
| 785 | float *scale_out, float *sr_out, int *p_out) | ||
| 786 | { | ||
| 787 |
2/2✓ Branch 0 taken 8851 times.
✓ Branch 1 taken 60 times.
|
8911 | int p = dot >= 0.0f ? 1 : -1; |
| 788 | 8911 | float ener01 = ener0 + ener1 + 2*p*dot; /* energy of L + p*R */ | |
| 789 | 8911 | *ratio_out = FLT_MAX; | |
| 790 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8911 times.
|
8911 | if (ener01 <= FLT_MIN) |
| 791 | ✗ | return 0; | |
| 792 | 8911 | float scale = sqrtf(ener0 / ener01); /* carrier = (L + p*R)*scale */ | |
| 793 | 8911 | float sr_ = sqrtf(ener1 / ener0); /* decoder: R = p*sr_*carrier */ | |
| 794 | 8911 | float img0 = 0.0f, img1 = 0.0f; | |
| 795 |
2/2✓ Branch 0 taken 10003 times.
✓ Branch 1 taken 8911 times.
|
18914 | for (int w2 = 0; w2 < gl; w2++) { |
| 796 | 10003 | const float *L = cpe->ch[0].coeffs + start + (w+w2)*128; | |
| 797 | 10003 | const float *R = cpe->ch[1].coeffs + start + (w+w2)*128; | |
| 798 |
2/2✓ Branch 0 taken 303112 times.
✓ Branch 1 taken 10003 times.
|
313115 | for (int i = 0; i < len; i++) { |
| 799 | 303112 | float c = (L[i] + p*R[i]) * scale; | |
| 800 | 303112 | float dl = L[i] - c, dr = R[i] - p*sr_*c; | |
| 801 | 303112 | img0 += dl*dl; img1 += dr*dr; | |
| 802 | } | ||
| 803 | } | ||
| 804 |
8/10✓ Branch 0 taken 8906 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 8911 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 285 times.
✓ Branch 5 taken 8626 times.
✓ Branch 6 taken 280 times.
✓ Branch 7 taken 5 times.
✓ Branch 8 taken 8626 times.
✗ Branch 9 not taken.
|
8911 | *ratio_out = FFMAX(img0 / FFMAX(minthr0 * gl, FLT_MIN), |
| 805 | img1 / FFMAX(minthr1 * gl, FLT_MIN)); | ||
| 806 | 8911 | *scale_out = scale; *sr_out = sr_; *p_out = p; | |
| 807 | 8911 | return 1; | |
| 808 | } | ||
| 809 | |||
| 810 | /* Recode one band's window group as intensity stereo in place: replace L with the | ||
| 811 | * carrier, zero R, signal the phase via the side channel's band type, and fold the | ||
| 812 | * pair's masking into the surviving (carrier) channel. */ | ||
| 813 | 700 | static void nmr_apply_is_band(AACEncContext *s, ChannelElement *cpe, | |
| 814 | int w, int g, int start, int len, int gl, | ||
| 815 | float scale, float sr_, int p, | ||
| 816 | float ener0, float ener1) | ||
| 817 | { | ||
| 818 | 700 | cpe->is_mask[w*16+g] = 1; | |
| 819 | 700 | cpe->ch[0].is_ener[w*16+g] = scale; | |
| 820 | 700 | cpe->ch[1].is_ener[w*16+g] = ener0 / ener1; | |
| 821 |
1/2✓ Branch 0 taken 700 times.
✗ Branch 1 not taken.
|
700 | cpe->ch[1].band_type[w*16+g] = p > 0 ? INTENSITY_BT : INTENSITY_BT2; |
| 822 |
2/2✓ Branch 0 taken 728 times.
✓ Branch 1 taken 700 times.
|
1428 | for (int w2 = 0; w2 < gl; w2++) { |
| 823 | 728 | FFPsyBand *b0 = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g]; | |
| 824 | 728 | FFPsyBand *b1 = &s->psy.ch[s->cur_channel+1].psy_bands[(w+w2)*16+g]; | |
| 825 | 728 | float *L = cpe->ch[0].coeffs + start + (w+w2)*128; | |
| 826 | 728 | float *R = cpe->ch[1].coeffs + start + (w+w2)*128; | |
| 827 | 728 | float ec = 0.0f; | |
| 828 |
2/2✓ Branch 0 taken 24160 times.
✓ Branch 1 taken 728 times.
|
24888 | for (int i = 0; i < len; i++) { |
| 829 | 24160 | L[i] = (L[i] + p*R[i]) * scale; | |
| 830 | 24160 | R[i] = 0.0f; | |
| 831 | 24160 | ec += L[i]*L[i]; | |
| 832 | } | ||
| 833 |
4/6✓ Branch 0 taken 728 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 717 times.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
|
728 | b0->threshold = FFMIN(b0->threshold, b1->threshold / FFMAX(sr_*sr_, 1e-9f)); |
| 834 | 728 | b0->energy = ec; b1->energy = 0.0f; | |
| 835 | } | ||
| 836 | 700 | } | |
| 837 | |||
| 838 | /* | ||
| 839 | * Per-band stereo-mode decision (L/R vs M/S vs intensity) for the NMR coder, | ||
| 840 | * made before quantization from the psychoacoustic model alone, so the | ||
| 841 | * quantizer search allocates natively on the spectra that are actually coded. | ||
| 842 | */ | ||
| 843 | 434 | static void nmr_decide_stereo(AACEncContext *s, ChannelElement *cpe) | |
| 844 | { | ||
| 845 | 434 | SingleChannelElement *sce0 = &cpe->ch[0]; | |
| 846 | 434 | SingleChannelElement *sce1 = &cpe->ch[1]; | |
| 847 | 434 | IndividualChannelStream *ics = &sce0->ics; | |
| 848 | 434 | const AVCodecContext *avctx = s->psy.avctx; | |
| 849 | 434 | const float freq_mult = avctx->sample_rate / (1024.0f / ics->num_windows) / 2.0f; | |
| 850 | 434 | int is_count = 0; | |
| 851 | |||
| 852 |
1/2✓ Branch 0 taken 434 times.
✗ Branch 1 not taken.
|
434 | if (s->nmr) { |
| 853 | 434 | int pi = (s->cur_channel >> 1) & 7; | |
| 854 | 434 | pi = pi * 2 + (ics->num_windows == 8); /* per-grid state bank */ | |
| 855 |
2/2✓ Branch 0 taken 79 times.
✓ Branch 1 taken 355 times.
|
434 | if (!s->nmr->sinit[pi]) { |
| 856 | /* one-time init; per-grid banks persist across window switches | ||
| 857 | * (wiping them churned stereo modes audibly) */ | ||
| 858 | 79 | memset(s->nmr->smode[pi], 0, sizeof(s->nmr->smode[pi])); | |
| 859 |
2/2✓ Branch 0 taken 10112 times.
✓ Branch 1 taken 79 times.
|
10191 | for (int b = 0; b < 128; b++) { |
| 860 | 10112 | s->nmr->sema_em[pi][b] = 0.0f; | |
| 861 | 10112 | s->nmr->sema_img[pi][b] = -1.0f; | |
| 862 | } | ||
| 863 | 79 | s->nmr->sinit[pi] = 1; | |
| 864 | } | ||
| 865 | } | ||
| 866 | |||
| 867 | /* Per-band stereo decision (L/R vs M/S vs I/S), made pre-quantization from | ||
| 868 | * the psy model so the trellis allocates on the coded spectra. */ | ||
| 869 | |||
| 870 | /* I/S engages under SUSTAINED strain only: rate pressure gated by the | ||
| 871 | * lambda floor (pressure spikes at a comfortable operating point must | ||
| 872 | * not admit it). Unengaged candidates fall back to M/S. */ | ||
| 873 | 1302 | float is_ramp = s->nmr ? s->nmr->press * | |
| 874 |
1/2✓ Branch 0 taken 434 times.
✗ Branch 1 not taken.
|
434 | av_clipf((s->nmr->lam_floor - 40.0f) / (120.0f - 40.0f), 0.0f, 1.0f) : 0.0f; |
| 875 |
3/4✓ Branch 0 taken 434 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 401 times.
|
434 | const int allow_is = s->options.intensity_stereo && is_ramp > 0.0f; |
| 876 | |||
| 877 | 434 | const int pidx = (s->cur_channel >> 1) & 15; | |
| 878 | 434 | const int decoupled = s->psy.pair_decoupled[pidx]; | |
| 879 | 434 | int njoint = 0, nbands = 0; /* joint-tool candidacy census, decouple feed */ | |
| 880 | |||
| 881 |
2/2✓ Branch 0 taken 551 times.
✓ Branch 1 taken 434 times.
|
985 | for (int w = 0; w < ics->num_windows; w += ics->group_len[w]) { |
| 882 | 551 | int start = 0; | |
| 883 |
2/2✓ Branch 0 taken 21539 times.
✓ Branch 1 taken 551 times.
|
22090 | for (int g = 0; g < ics->num_swb; start += ics->swb_sizes[g++]) { |
| 884 | 21539 | int len = ics->swb_sizes[g], gl = ics->group_len[w]; | |
| 885 | 21539 | float ener0 = 0.0f, ener1 = 0.0f, dot = 0.0f, es_tot = 0.0f, em_tot = 0.0f; | |
| 886 | 21539 | float minthr0 = FLT_MAX, minthr1 = FLT_MAX; | |
| 887 | |||
| 888 | 21539 | cpe->is_mask[w*16+g] = 0; | |
| 889 | 21539 | cpe->ms_mask[w*16+g] = 0; | |
| 890 | |||
| 891 |
2/2✓ Branch 0 taken 23723 times.
✓ Branch 1 taken 21539 times.
|
45262 | for (int w2 = 0; w2 < gl; w2++) { |
| 892 | 23723 | FFPsyBand *b0 = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g]; | |
| 893 | 23723 | FFPsyBand *b1 = &s->psy.ch[s->cur_channel+1].psy_bands[(w+w2)*16+g]; | |
| 894 | 23723 | const float *L = sce0->coeffs + start + (w+w2)*128; | |
| 895 | 23723 | const float *R = sce1->coeffs + start + (w+w2)*128; | |
| 896 | 23723 | float el = 0.0f, er = 0.0f, em = 0.0f, es = 0.0f, d = 0.0f; | |
| 897 |
2/2✓ Branch 0 taken 444416 times.
✓ Branch 1 taken 23723 times.
|
468139 | for (int i = 0; i < len; i++) { |
| 898 | 444416 | float m = (L[i] + R[i]) * 0.5f; | |
| 899 | 444416 | float sv = m - R[i]; | |
| 900 | 444416 | el += L[i]*L[i]; er += R[i]*R[i]; | |
| 901 | 444416 | em += m*m; es += sv*sv; d += L[i]*R[i]; | |
| 902 | } | ||
| 903 | 23723 | ener0 += el; ener1 += er; dot += d; es_tot += es; em_tot += em; | |
| 904 |
2/2✓ Branch 0 taken 22599 times.
✓ Branch 1 taken 1124 times.
|
23723 | minthr0 = FFMIN(minthr0, b0->threshold); |
| 905 |
2/2✓ Branch 0 taken 22673 times.
✓ Branch 1 taken 1050 times.
|
23723 | minthr1 = FFMIN(minthr1, b1->threshold); |
| 906 | } | ||
| 907 |
2/2✓ Branch 0 taken 682 times.
✓ Branch 1 taken 20857 times.
|
21539 | float thr_g = FFMIN(minthr0, minthr1) * gl; /* group masking budget */ |
| 908 | |||
| 909 | /* PNS-stereo reservation: keep clearly-wide noise bands for PNS. */ | ||
| 910 | 21539 | const int sidx = w*16+g; | |
| 911 | { | ||
| 912 | 21539 | float es_w = es_tot, em_w = em_tot; | |
| 913 |
1/2✓ Branch 0 taken 21539 times.
✗ Branch 1 not taken.
|
21539 | if (s->nmr) { |
| 914 | 21539 | int pi_ = ((s->cur_channel >> 1) & 7) * 2 + (cpe->ch[0].ics.num_windows == 8); | |
| 915 | 21539 | float pe = s->nmr->sema_es[pi_][sidx]; | |
| 916 | 21539 | float pm = s->nmr->sema_em[pi_][sidx]; | |
| 917 |
2/2✓ Branch 0 taken 16955 times.
✓ Branch 1 taken 4584 times.
|
21539 | if (pm > 0.0f) { |
| 918 | 16955 | es_w = NMR_SDEC_EMA * pe + (1.0f - NMR_SDEC_EMA) * es_tot; | |
| 919 | 16955 | em_w = NMR_SDEC_EMA * pm + (1.0f - NMR_SDEC_EMA) * em_tot; | |
| 920 | } | ||
| 921 | } | ||
| 922 |
3/4✓ Branch 0 taken 9879 times.
✓ Branch 1 taken 11660 times.
✓ Branch 2 taken 9879 times.
✗ Branch 3 not taken.
|
21539 | if (cpe->ch[0].can_pns[w*16+g] && cpe->ch[1].can_pns[w*16+g] && |
| 923 |
2/2✓ Branch 0 taken 395 times.
✓ Branch 1 taken 9484 times.
|
9879 | es_w > NMR_PNS_STEREO_DECORR * em_w) |
| 924 | 395 | continue; | |
| 925 | } | ||
| 926 | 21144 | cpe->ch[0].can_pns[w*16+g] = cpe->ch[1].can_pns[w*16+g] = 0; | |
| 927 | |||
| 928 | 21144 | int pi = ((s->cur_channel >> 1) & 7) * 2 + (cpe->ch[0].ics.num_windows == 8); | |
| 929 |
1/2✓ Branch 0 taken 21144 times.
✗ Branch 1 not taken.
|
21144 | uint8_t *pmode = s->nmr ? s->nmr->smode[pi] : NULL; |
| 930 |
1/2✓ Branch 0 taken 21144 times.
✗ Branch 1 not taken.
|
21144 | int prev = pmode ? pmode[sidx] : 0; |
| 931 |
2/2✓ Branch 0 taken 4736 times.
✓ Branch 1 taken 16408 times.
|
21144 | float eqgate = NMR_MS_EQUIV * (prev == 1 ? 1.5f : 1.0f); /* stay-until es>0.75em */ |
| 932 | /* I/S = lossy economy: image-error budget scales with pressure */ | ||
| 933 |
2/2✓ Branch 0 taken 672 times.
✓ Branch 1 taken 20472 times.
|
21144 | float imgate = NMR_IS_IMG_GATE * is_ramp * (prev == 2 ? NMR_STICKY : 1.0f); |
| 934 | 21144 | float es_d = es_tot, em_d = em_tot; | |
| 935 |
1/2✓ Branch 0 taken 21144 times.
✗ Branch 1 not taken.
|
21144 | if (s->nmr) { |
| 936 | 21144 | float *ees = &s->nmr->sema_es[pi][sidx]; | |
| 937 | 21144 | float *eem = &s->nmr->sema_em[pi][sidx]; | |
| 938 |
2/2✓ Branch 0 taken 4314 times.
✓ Branch 1 taken 16830 times.
|
21144 | if (*eem <= 0.0f) { *ees = es_tot; *eem = em_tot; } |
| 939 | else { | ||
| 940 | 16830 | *ees = NMR_SDEC_EMA * *ees + (1.0f - NMR_SDEC_EMA) * es_tot; | |
| 941 | 16830 | *eem = NMR_SDEC_EMA * *eem + (1.0f - NMR_SDEC_EMA) * em_tot; | |
| 942 | } | ||
| 943 | 21144 | es_d = *ees; em_d = *eem; | |
| 944 | } | ||
| 945 |
2/2✓ Branch 0 taken 6360 times.
✓ Branch 1 taken 14784 times.
|
27504 | int ms_would = s->options.mid_side && |
| 946 |
1/2✓ Branch 0 taken 6360 times.
✗ Branch 1 not taken.
|
6360 | (s->options.mid_side == 1 || |
| 947 |
2/2✓ Branch 0 taken 744 times.
✓ Branch 1 taken 5616 times.
|
6360 | es_d < eqgate * em_d || |
| 948 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 744 times.
|
744 | es_tot < NMR_MS_MASK * thr_g); |
| 949 |
3/4✓ Branch 0 taken 5616 times.
✓ Branch 1 taken 15528 times.
✓ Branch 2 taken 5616 times.
✗ Branch 3 not taken.
|
21144 | int ms_ok = ms_would && !decoupled; |
| 950 | float scale, sr_, imgratio; int p; | ||
| 951 | /* I/S competes with M/S above the frequency limit (candidacy must | ||
| 952 | * not be gated on !ms_ok - that leaves only unrenderable bands) */ | ||
| 953 |
2/2✓ Branch 0 taken 8911 times.
✓ Branch 1 taken 126 times.
|
9037 | int is_cand = start * freq_mult > NMR_IS_LOW_LIMIT && |
| 954 |
4/6✓ Branch 0 taken 9037 times.
✓ Branch 1 taken 12107 times.
✓ Branch 2 taken 8911 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8911 times.
✗ Branch 5 not taken.
|
39092 | ener0 > FLT_MIN && ener1 > FLT_MIN && |
| 955 | 8911 | nmr_is_image_masked(s, cpe, w, g, start, len, gl, | |
| 956 | ener0, ener1, dot, minthr0, minthr1, | ||
| 957 | &imgratio, &scale, &sr_, &p); | ||
| 958 | 21144 | int is_ok = is_cand; | |
| 959 |
3/4✓ Branch 0 taken 21144 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9037 times.
✓ Branch 3 taken 12107 times.
|
21144 | if (s->nmr && start * freq_mult > NMR_IS_LOW_LIMIT) { |
| 960 | /* smoothed image-error; updated only while candidate (fail-value | ||
| 961 | * feeding jammed it permanently high) */ | ||
| 962 | 9037 | float *eim = &s->nmr->sema_img[pi][sidx]; | |
| 963 |
2/2✓ Branch 0 taken 8911 times.
✓ Branch 1 taken 126 times.
|
9037 | if (is_cand) { |
| 964 | /* seed from first measurement; freeze when not candidate */ | ||
| 965 |
2/2✓ Branch 0 taken 1841 times.
✓ Branch 1 taken 7070 times.
|
8911 | if (*eim < 0.0f) *eim = imgratio; |
| 966 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 7065 times.
|
7070 | else *eim = NMR_SDEC_EMA * *eim + (1.0f - NMR_SDEC_EMA) * FFMIN(imgratio, 100.0f * NMR_IS_IMG_GATE); |
| 967 | } | ||
| 968 |
5/6✓ Branch 0 taken 8911 times.
✓ Branch 1 taken 126 times.
✓ Branch 2 taken 8911 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 700 times.
✓ Branch 5 taken 8211 times.
|
9037 | is_ok = is_cand && *eim >= 0.0f && *eim < imgate; |
| 969 | } | ||
| 970 | |||
| 971 |
4/4✓ Branch 0 taken 15528 times.
✓ Branch 1 taken 5616 times.
✓ Branch 2 taken 147 times.
✓ Branch 3 taken 15381 times.
|
21144 | njoint += ms_would || is_ok; nbands++; |
| 972 |
1/2✓ Branch 0 taken 21144 times.
✗ Branch 1 not taken.
|
21144 | if (pmode) { |
| 973 |
6/8✓ Branch 0 taken 700 times.
✓ Branch 1 taken 20444 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 700 times.
✓ Branch 4 taken 15381 times.
✓ Branch 5 taken 5063 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 15381 times.
|
21144 | int m_ = (is_ok && allow_is) ? 2 : ms_ok ? 1 : |
| 974 | ✗ | (is_ok && s->options.mid_side) ? 1 : 0; | |
| 975 | 21144 | pmode[sidx] = m_; | |
| 976 | 21144 | s->nmr->smode_band[(s->cur_channel >> 1) & 7][w*16+g] = m_; | |
| 977 | } | ||
| 978 |
3/4✓ Branch 0 taken 700 times.
✓ Branch 1 taken 20444 times.
✓ Branch 2 taken 700 times.
✗ Branch 3 not taken.
|
21144 | if (is_ok && allow_is) { |
| 979 | 700 | nmr_apply_is_band(s, cpe, w, g, start, len, gl, | |
| 980 | scale, sr_, p, ener0, ener1); | ||
| 981 | 700 | is_count++; | |
| 982 |
3/6✓ Branch 0 taken 15381 times.
✓ Branch 1 taken 5063 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 15381 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
20444 | } else if (ms_ok || (is_ok && s->options.mid_side)) { |
| 983 | 5063 | nmr_apply_ms_band(s, cpe, w, g, start, len, gl); | |
| 984 | } | ||
| 985 | /* else: keep full L/R stereo */ | ||
| 986 | } | ||
| 987 | } | ||
| 988 | 434 | cpe->is_mode = !!is_count; | |
| 989 | |||
| 990 |
1/2✓ Branch 0 taken 434 times.
✗ Branch 1 not taken.
|
434 | if (nbands > 0) { |
| 991 | /* Pair joint-tool value, read next frame by the psy pair-synced window | ||
| 992 | * decision and the M/S candidacy above. Measured as CANDIDACY (not | ||
| 993 | * adoption) so decoupling cannot starve its own signal and self-lock. */ | ||
| 994 | 434 | float r = (float)njoint / nbands; | |
| 995 | 434 | float *pj = &s->psy.pair_joint[pidx]; | |
| 996 |
2/2✓ Branch 0 taken 181 times.
✓ Branch 1 taken 253 times.
|
434 | *pj = *pj > 0.0f ? 0.95f * *pj + 0.05f * r : r; |
| 997 | 434 | s->psy.pair_decoupled[pidx] = *pj < | |
| 998 |
2/2✓ Branch 0 taken 213 times.
✓ Branch 1 taken 221 times.
|
434 | (s->psy.pair_decoupled[pidx] ? 1.3f * NMR_DECORR_LO : NMR_DECORR_LO); |
| 999 | } | ||
| 1000 | 434 | } | |
| 1001 | |||
| 1002 | 411 | static void apply_mid_side_stereo(ChannelElement *cpe) | |
| 1003 | { | ||
| 1004 | int w, w2, g, i; | ||
| 1005 | 411 | IndividualChannelStream *ics = &cpe->ch[0].ics; | |
| 1006 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 411 times.
|
411 | if (!cpe->common_window) |
| 1007 | ✗ | return; | |
| 1008 |
2/2✓ Branch 0 taken 462 times.
✓ Branch 1 taken 411 times.
|
873 | for (w = 0; w < ics->num_windows; w += ics->group_len[w]) { |
| 1009 |
2/2✓ Branch 0 taken 537 times.
✓ Branch 1 taken 462 times.
|
999 | for (w2 = 0; w2 < ics->group_len[w]; w2++) { |
| 1010 | 537 | int start = (w+w2) * 128; | |
| 1011 |
2/2✓ Branch 0 taken 21273 times.
✓ Branch 1 taken 537 times.
|
21810 | for (g = 0; g < ics->num_swb; g++) { |
| 1012 | /* ms_mask can be used for other purposes in PNS and I/S, | ||
| 1013 | * so must not apply M/S if any band uses either, even if | ||
| 1014 | * ms_mask is set. | ||
| 1015 | */ | ||
| 1016 |
2/4✓ Branch 0 taken 21273 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21273 times.
✗ Branch 3 not taken.
|
21273 | if (!cpe->ms_mask[w*16 + g] || cpe->is_mask[w*16 + g] |
| 1017 |
1/2✓ Branch 0 taken 21273 times.
✗ Branch 1 not taken.
|
21273 | || cpe->ch[0].band_type[w*16 + g] >= NOISE_BT |
| 1018 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21273 times.
|
21273 | || cpe->ch[1].band_type[w*16 + g] >= NOISE_BT) { |
| 1019 | ✗ | start += ics->swb_sizes[g]; | |
| 1020 | ✗ | continue; | |
| 1021 | } | ||
| 1022 |
2/2✓ Branch 0 taken 420864 times.
✓ Branch 1 taken 21273 times.
|
442137 | for (i = 0; i < ics->swb_sizes[g]; i++) { |
| 1023 | 420864 | float L = (cpe->ch[0].coeffs[start+i] + cpe->ch[1].coeffs[start+i]) * 0.5f; | |
| 1024 | 420864 | float R = L - cpe->ch[1].coeffs[start+i]; | |
| 1025 | 420864 | cpe->ch[0].coeffs[start+i] = L; | |
| 1026 | 420864 | cpe->ch[1].coeffs[start+i] = R; | |
| 1027 | } | ||
| 1028 | 21273 | start += ics->swb_sizes[g]; | |
| 1029 | } | ||
| 1030 | } | ||
| 1031 | } | ||
| 1032 | } | ||
| 1033 | |||
| 1034 | /** | ||
| 1035 | * Encode scalefactor band coding type. | ||
| 1036 | */ | ||
| 1037 | 10196 | static void encode_band_info(AACEncContext *s, SingleChannelElement *sce) | |
| 1038 | { | ||
| 1039 | int w; | ||
| 1040 | |||
| 1041 |
1/2✓ Branch 0 taken 10196 times.
✗ Branch 1 not taken.
|
10196 | if (s->coder->set_special_band_scalefactors) |
| 1042 | 10196 | s->coder->set_special_band_scalefactors(s, sce); | |
| 1043 | |||
| 1044 |
2/2✓ Branch 0 taken 11136 times.
✓ Branch 1 taken 10196 times.
|
21332 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) |
| 1045 | 11136 | s->coder->encode_window_bands_info(s, sce, w, sce->ics.group_len[w], s->lambda); | |
| 1046 | 10196 | } | |
| 1047 | |||
| 1048 | /** | ||
| 1049 | * Encode scalefactors. | ||
| 1050 | */ | ||
| 1051 | 10196 | static void encode_scale_factors(AVCodecContext *avctx, AACEncContext *s, | |
| 1052 | SingleChannelElement *sce) | ||
| 1053 | { | ||
| 1054 | 10196 | int diff, off_sf = sce->sf_idx[0], off_pns = sce->sf_idx[0] - NOISE_OFFSET; | |
| 1055 | 10196 | int off_is = 0, noise_flag = 1; | |
| 1056 | int i, w; | ||
| 1057 | |||
| 1058 |
2/2✓ Branch 0 taken 11136 times.
✓ Branch 1 taken 10196 times.
|
21332 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
| 1059 |
2/2✓ Branch 0 taken 486799 times.
✓ Branch 1 taken 11136 times.
|
497935 | for (i = 0; i < sce->ics.max_sfb; i++) { |
| 1060 |
2/2✓ Branch 0 taken 474389 times.
✓ Branch 1 taken 12410 times.
|
486799 | if (!sce->zeroes[w*16 + i]) { |
| 1061 |
2/2✓ Branch 0 taken 7039 times.
✓ Branch 1 taken 467350 times.
|
474389 | if (sce->band_type[w*16 + i] == NOISE_BT) { |
| 1062 | 7039 | diff = sce->sf_idx[w*16 + i] - off_pns; | |
| 1063 | 7039 | off_pns = sce->sf_idx[w*16 + i]; | |
| 1064 |
2/2✓ Branch 0 taken 1074 times.
✓ Branch 1 taken 5965 times.
|
7039 | if (noise_flag-- > 0) { |
| 1065 | 1074 | put_bits(&s->pb, NOISE_PRE_BITS, diff + NOISE_PRE); | |
| 1066 | 1074 | continue; | |
| 1067 | } | ||
| 1068 |
2/2✓ Branch 0 taken 460079 times.
✓ Branch 1 taken 7271 times.
|
467350 | } else if (sce->band_type[w*16 + i] == INTENSITY_BT || |
| 1069 |
2/2✓ Branch 0 taken 2079 times.
✓ Branch 1 taken 458000 times.
|
460079 | sce->band_type[w*16 + i] == INTENSITY_BT2) { |
| 1070 | 9350 | diff = sce->sf_idx[w*16 + i] - off_is; | |
| 1071 | 9350 | off_is = sce->sf_idx[w*16 + i]; | |
| 1072 | } else { | ||
| 1073 | 458000 | diff = sce->sf_idx[w*16 + i] - off_sf; | |
| 1074 | 458000 | off_sf = sce->sf_idx[w*16 + i]; | |
| 1075 | } | ||
| 1076 | 473315 | diff += SCALE_DIFF_ZERO; | |
| 1077 |
2/4✓ Branch 0 taken 473315 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 473315 times.
|
473315 | av_assert0(diff >= 0 && diff <= 120); |
| 1078 | 473315 | put_bits(&s->pb, ff_aac_scalefactor_bits[diff], ff_aac_scalefactor_code[diff]); | |
| 1079 | } | ||
| 1080 | } | ||
| 1081 | } | ||
| 1082 | 10196 | } | |
| 1083 | |||
| 1084 | /** | ||
| 1085 | * Encode pulse data. | ||
| 1086 | */ | ||
| 1087 | 10196 | static void encode_pulses(AACEncContext *s, Pulse *pulse) | |
| 1088 | { | ||
| 1089 | int i; | ||
| 1090 | |||
| 1091 | 10196 | put_bits(&s->pb, 1, !!pulse->num_pulse); | |
| 1092 |
1/2✓ Branch 0 taken 10196 times.
✗ Branch 1 not taken.
|
10196 | if (!pulse->num_pulse) |
| 1093 | 10196 | return; | |
| 1094 | |||
| 1095 | ✗ | put_bits(&s->pb, 2, pulse->num_pulse - 1); | |
| 1096 | ✗ | put_bits(&s->pb, 6, pulse->start); | |
| 1097 | ✗ | for (i = 0; i < pulse->num_pulse; i++) { | |
| 1098 | ✗ | put_bits(&s->pb, 5, pulse->pos[i]); | |
| 1099 | ✗ | put_bits(&s->pb, 4, pulse->amp[i]); | |
| 1100 | } | ||
| 1101 | } | ||
| 1102 | |||
| 1103 | /** | ||
| 1104 | * Encode spectral coefficients processed by psychoacoustic model. | ||
| 1105 | */ | ||
| 1106 | 10196 | static void encode_spectral_coeffs(AACEncContext *s, SingleChannelElement *sce) | |
| 1107 | { | ||
| 1108 | int start, i, w, w2; | ||
| 1109 | |||
| 1110 |
2/2✓ Branch 0 taken 11136 times.
✓ Branch 1 taken 10196 times.
|
21332 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
| 1111 | 11136 | start = 0; | |
| 1112 |
2/2✓ Branch 0 taken 486799 times.
✓ Branch 1 taken 11136 times.
|
497935 | for (i = 0; i < sce->ics.max_sfb; i++) { |
| 1113 |
2/2✓ Branch 0 taken 12410 times.
✓ Branch 1 taken 474389 times.
|
486799 | if (sce->zeroes[w*16 + i]) { |
| 1114 | 12410 | start += sce->ics.swb_sizes[i]; | |
| 1115 | 12410 | continue; | |
| 1116 | } | ||
| 1117 |
2/2✓ Branch 0 taken 489164 times.
✓ Branch 1 taken 474389 times.
|
963553 | for (w2 = w; w2 < w + sce->ics.group_len[w]; w2++) { |
| 1118 | 489164 | s->coder->quantize_and_encode_band(s, &s->pb, | |
| 1119 | 489164 | &sce->coeffs[start + w2*128], | |
| 1120 | 489164 | NULL, sce->ics.swb_sizes[i], | |
| 1121 | 489164 | sce->sf_idx[w*16 + i], | |
| 1122 | 489164 | sce->band_type[w*16 + i], | |
| 1123 | s->lambda, | ||
| 1124 | 489164 | sce->ics.window_clipping[w]); | |
| 1125 | } | ||
| 1126 | 474389 | start += sce->ics.swb_sizes[i]; | |
| 1127 | } | ||
| 1128 | } | ||
| 1129 | 10196 | } | |
| 1130 | |||
| 1131 | /** | ||
| 1132 | * Downscale spectral coefficients for near-clipping windows to avoid artifacts | ||
| 1133 | */ | ||
| 1134 | 7270 | static void avoid_clipping(AACEncContext *s, SingleChannelElement *sce) | |
| 1135 | { | ||
| 1136 | int start, i, j, w; | ||
| 1137 | |||
| 1138 |
2/2✓ Branch 0 taken 157 times.
✓ Branch 1 taken 7113 times.
|
7270 | if (sce->ics.clip_avoidance_factor < 1.0f) { |
| 1139 |
2/2✓ Branch 0 taken 220 times.
✓ Branch 1 taken 157 times.
|
377 | for (w = 0; w < sce->ics.num_windows; w++) { |
| 1140 | 220 | start = 0; | |
| 1141 |
2/2✓ Branch 0 taken 8115 times.
✓ Branch 1 taken 220 times.
|
8335 | for (i = 0; i < sce->ics.max_sfb; i++) { |
| 1142 | 8115 | float *swb_coeffs = &sce->coeffs[start + w*128]; | |
| 1143 |
2/2✓ Branch 0 taken 156312 times.
✓ Branch 1 taken 8115 times.
|
164427 | for (j = 0; j < sce->ics.swb_sizes[i]; j++) |
| 1144 | 156312 | swb_coeffs[j] *= sce->ics.clip_avoidance_factor; | |
| 1145 | 8115 | start += sce->ics.swb_sizes[i]; | |
| 1146 | } | ||
| 1147 | } | ||
| 1148 | } | ||
| 1149 | 7270 | } | |
| 1150 | |||
| 1151 | /** | ||
| 1152 | * Encode one channel of audio data. | ||
| 1153 | */ | ||
| 1154 | 10196 | static int encode_individual_channel(AVCodecContext *avctx, AACEncContext *s, | |
| 1155 | SingleChannelElement *sce, | ||
| 1156 | int common_window) | ||
| 1157 | { | ||
| 1158 | 10196 | put_bits(&s->pb, 8, sce->sf_idx[0]); | |
| 1159 |
2/2✓ Branch 0 taken 826 times.
✓ Branch 1 taken 9370 times.
|
10196 | if (!common_window) |
| 1160 | 826 | put_ics_info(s, &sce->ics); | |
| 1161 | 10196 | encode_band_info(s, sce); | |
| 1162 | 10196 | encode_scale_factors(avctx, s, sce); | |
| 1163 | 10196 | encode_pulses(s, &sce->pulse); | |
| 1164 | 10196 | put_bits(&s->pb, 1, !!sce->tns.present); | |
| 1165 |
1/2✓ Branch 0 taken 10196 times.
✗ Branch 1 not taken.
|
10196 | if (s->coder->encode_tns_info) |
| 1166 | 10196 | s->coder->encode_tns_info(s, sce); | |
| 1167 | 10196 | put_bits(&s->pb, 1, 0); //ssr | |
| 1168 | 10196 | encode_spectral_coeffs(s, sce); | |
| 1169 | 10196 | return 0; | |
| 1170 | } | ||
| 1171 | |||
| 1172 | /** | ||
| 1173 | * Write some auxiliary information about the created AAC file. | ||
| 1174 | */ | ||
| 1175 | 7 | static void put_bitstream_info(AACEncContext *s, const char *name) | |
| 1176 | { | ||
| 1177 | int i, namelen, padbits; | ||
| 1178 | |||
| 1179 | 7 | namelen = strlen(name) + 2; | |
| 1180 | 7 | put_bits(&s->pb, 3, TYPE_FIL); | |
| 1181 | 7 | put_bits(&s->pb, 4, FFMIN(namelen, 15)); | |
| 1182 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 | if (namelen >= 15) |
| 1183 | 7 | put_bits(&s->pb, 8, namelen - 14); | |
| 1184 | 7 | put_bits(&s->pb, 4, 0); //extension type - filler | |
| 1185 | 7 | padbits = -put_bits_count(&s->pb) & 7; | |
| 1186 | 7 | align_put_bits(&s->pb); | |
| 1187 |
2/2✓ Branch 0 taken 91 times.
✓ Branch 1 taken 7 times.
|
98 | for (i = 0; i < namelen - 2; i++) |
| 1188 | 91 | put_bits(&s->pb, 8, name[i]); | |
| 1189 | 7 | put_bits(&s->pb, 12 - padbits, 0); | |
| 1190 | 7 | } | |
| 1191 | |||
| 1192 | /* | ||
| 1193 | * Copy input samples. | ||
| 1194 | * Channels are reordered from libavcodec's default order to AAC order. | ||
| 1195 | */ | ||
| 1196 | 3515 | static void copy_input_samples(AACEncContext *s, const AVFrame *frame) | |
| 1197 | { | ||
| 1198 | int ch; | ||
| 1199 |
2/2✓ Branch 0 taken 3469 times.
✓ Branch 1 taken 46 times.
|
3515 | int end = 2048 + (frame ? frame->nb_samples : 0); |
| 1200 | 3515 | const uint8_t *channel_map = s->reorder_map; | |
| 1201 | |||
| 1202 | /* copy and remap input samples */ | ||
| 1203 |
2/2✓ Branch 0 taken 7387 times.
✓ Branch 1 taken 3515 times.
|
10902 | for (ch = 0; ch < s->channels; ch++) { |
| 1204 | /* copy last 1024 samples of previous frame to the start of the current frame */ | ||
| 1205 | 7387 | memcpy(&s->planar_samples[ch][1024], &s->planar_samples[ch][2048], 1024 * sizeof(s->planar_samples[0][0])); | |
| 1206 | |||
| 1207 | /* copy new samples and zero any remaining samples */ | ||
| 1208 |
2/2✓ Branch 0 taken 7153 times.
✓ Branch 1 taken 234 times.
|
7387 | if (frame) { |
| 1209 | 7153 | memcpy(&s->planar_samples[ch][2048], | |
| 1210 | 7153 | frame->extended_data[channel_map[ch]], | |
| 1211 | 7153 | frame->nb_samples * sizeof(s->planar_samples[0][0])); | |
| 1212 | } | ||
| 1213 | 7387 | memset(&s->planar_samples[ch][end], 0, | |
| 1214 | 7387 | (3072 - end) * sizeof(s->planar_samples[0][0])); | |
| 1215 | } | ||
| 1216 | 3515 | } | |
| 1217 | |||
| 1218 | 3538 | static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, | |
| 1219 | const AVFrame *frame, int *got_packet_ptr) | ||
| 1220 | { | ||
| 1221 | 3538 | AACEncContext *s = avctx->priv_data; | |
| 1222 | 3538 | float **samples = s->planar_samples, *samples2, *la, *overlap; | |
| 1223 | ChannelElement *cpe; | ||
| 1224 | SingleChannelElement *sce; | ||
| 1225 | IndividualChannelStream *ics; | ||
| 1226 | int i, its, ch, w, chans, tag, start_ch, ret, frame_bits; | ||
| 1227 | int target_bits, rate_bits, too_many_bits, too_few_bits; | ||
| 1228 | 3538 | int ms_mode = 0, is_mode = 0, tns_mode = 0, pred_mode = 0; | |
| 1229 | int chan_el_counter[4]; | ||
| 1230 | FFPsyWindowInfo windows[AAC_MAX_CHANNELS]; | ||
| 1231 | |||
| 1232 | /* add current frame to queue */ | ||
| 1233 |
2/2✓ Branch 0 taken 3469 times.
✓ Branch 1 taken 69 times.
|
3538 | if (frame) { |
| 1234 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3469 times.
|
3469 | if ((ret = ff_af_queue_add(&s->afq, frame)) < 0) |
| 1235 | ✗ | return ret; | |
| 1236 | } else { | ||
| 1237 |
3/6✓ Branch 0 taken 46 times.
✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 46 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
69 | if (!s->afq.remaining_samples || (!s->afq.frame_alloc && !s->afq.frame_count)) |
| 1238 | 23 | return 0; | |
| 1239 | } | ||
| 1240 | |||
| 1241 | 3515 | copy_input_samples(s, frame); | |
| 1242 | |||
| 1243 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 3492 times.
|
3515 | if (!avctx->frame_num) |
| 1244 | 23 | return 0; | |
| 1245 | |||
| 1246 | 3492 | start_ch = 0; | |
| 1247 |
2/2✓ Branch 0 taken 4014 times.
✓ Branch 1 taken 3492 times.
|
7506 | for (i = 0; i < s->chan_map[0]; i++) { |
| 1248 | 4014 | FFPsyWindowInfo* wi = windows + start_ch; | |
| 1249 | 4014 | tag = s->chan_map[i+1]; | |
| 1250 |
2/2✓ Branch 0 taken 3256 times.
✓ Branch 1 taken 758 times.
|
4014 | chans = tag == TYPE_CPE ? 2 : 1; |
| 1251 | 4014 | cpe = &s->cpe[i]; | |
| 1252 | { | ||
| 1253 | 4014 | int wi_paired = 0; | |
| 1254 | /* Synced pair windows: decide both channels of a CPE together so | ||
| 1255 | * their block switching never diverges (see psy window_pair). */ | ||
| 1256 |
6/8✓ Branch 0 taken 3256 times.
✓ Branch 1 taken 758 times.
✓ Branch 2 taken 3256 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3256 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3158 times.
✓ Branch 7 taken 98 times.
|
4014 | if (chans == 2 && tag != TYPE_LFE && s->psy.model->window_pair && frame) { |
| 1257 | 3158 | const float *ov0 = &samples[start_ch][0], *ov1 = &samples[start_ch + 1][0]; | |
| 1258 | 3158 | s->psy.model->window_pair(&s->psy, | |
| 1259 | ov0 + 1024, ov0 + 1024 + 448 + 64, | ||
| 1260 | ov1 + 1024, ov1 + 1024 + 448 + 64, | ||
| 1261 | start_ch, start_ch + 1, | ||
| 1262 | 3158 | cpe->ch[0].ics.window_sequence[0], | |
| 1263 | 3158 | cpe->ch[1].ics.window_sequence[0], | |
| 1264 | wi); | ||
| 1265 | 3158 | wi_paired = 1; | |
| 1266 | } | ||
| 1267 |
2/2✓ Branch 0 taken 7270 times.
✓ Branch 1 taken 4014 times.
|
11284 | for (ch = 0; ch < chans; ch++) { |
| 1268 | int k; | ||
| 1269 | float clip_avoidance_factor; | ||
| 1270 | 7270 | sce = &cpe->ch[ch]; | |
| 1271 | 7270 | ics = &sce->ics; | |
| 1272 | 7270 | s->cur_channel = start_ch + ch; | |
| 1273 | 7270 | overlap = &samples[s->cur_channel][0]; | |
| 1274 | 7270 | samples2 = overlap + 1024; | |
| 1275 | 7270 | la = samples2 + (448+64); | |
| 1276 |
2/2✓ Branch 0 taken 234 times.
✓ Branch 1 taken 7036 times.
|
7270 | if (!frame) |
| 1277 | 234 | la = NULL; | |
| 1278 |
2/2✓ Branch 0 taken 120 times.
✓ Branch 1 taken 7150 times.
|
7270 | if (tag == TYPE_LFE) { |
| 1279 | 120 | wi[ch].window_type[0] = wi[ch].window_type[1] = ONLY_LONG_SEQUENCE; | |
| 1280 | 120 | wi[ch].window_shape = 0; | |
| 1281 | 120 | wi[ch].num_windows = 1; | |
| 1282 | 120 | wi[ch].grouping[0] = 1; | |
| 1283 | 120 | wi[ch].clipping[0] = 0; | |
| 1284 | |||
| 1285 | /* Only the lowest 12 coefficients are used in a LFE channel. | ||
| 1286 | * The expression below results in only the bottom 8 coefficients | ||
| 1287 | * being used for 11.025kHz to 16kHz sample rates. | ||
| 1288 | */ | ||
| 1289 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 120 times.
|
120 | ics->num_swb = s->samplerate_index >= 8 ? 1 : 3; |
| 1290 |
2/2✓ Branch 0 taken 834 times.
✓ Branch 1 taken 6316 times.
|
7150 | } else if (!wi_paired) { |
| 1291 | 834 | wi[ch] = s->psy.model->window(&s->psy, samples2, la, s->cur_channel, | |
| 1292 | 834 | ics->window_sequence[0]); | |
| 1293 | } | ||
| 1294 | 7270 | ics->window_sequence[1] = ics->window_sequence[0]; | |
| 1295 | 7270 | ics->window_sequence[0] = wi[ch].window_type[0]; | |
| 1296 | 7270 | ics->use_kb_window[1] = ics->use_kb_window[0]; | |
| 1297 | 7270 | ics->use_kb_window[0] = wi[ch].window_shape; | |
| 1298 | 7270 | ics->num_windows = wi[ch].num_windows; | |
| 1299 |
2/2✓ Branch 0 taken 313 times.
✓ Branch 1 taken 6957 times.
|
7270 | ics->swb_sizes = s->psy.bands [ics->num_windows == 8]; |
| 1300 |
4/4✓ Branch 0 taken 120 times.
✓ Branch 1 taken 7150 times.
✓ Branch 2 taken 313 times.
✓ Branch 3 taken 6837 times.
|
7270 | ics->num_swb = tag == TYPE_LFE ? ics->num_swb : s->psy.num_bands[ics->num_windows == 8]; |
| 1301 | 7270 | ics->max_sfb = FFMIN(ics->max_sfb, ics->num_swb); | |
| 1302 | 14540 | ics->swb_offset = wi[ch].window_type[0] == EIGHT_SHORT_SEQUENCE ? | |
| 1303 |
2/2✓ Branch 0 taken 313 times.
✓ Branch 1 taken 6957 times.
|
7270 | ff_swb_offset_128 [s->samplerate_index]: |
| 1304 | 6957 | ff_swb_offset_1024[s->samplerate_index]; | |
| 1305 | 14540 | ics->tns_max_bands = wi[ch].window_type[0] == EIGHT_SHORT_SEQUENCE ? | |
| 1306 |
2/2✓ Branch 0 taken 313 times.
✓ Branch 1 taken 6957 times.
|
7270 | ff_tns_max_bands_128 [s->samplerate_index]: |
| 1307 | 6957 | ff_tns_max_bands_1024[s->samplerate_index]; | |
| 1308 | |||
| 1309 |
2/2✓ Branch 0 taken 9461 times.
✓ Branch 1 taken 7270 times.
|
16731 | for (w = 0; w < ics->num_windows; w++) |
| 1310 | 9461 | ics->group_len[w] = wi[ch].grouping[w]; | |
| 1311 | |||
| 1312 | /* Calculate input sample maximums and evaluate clipping risk */ | ||
| 1313 | 7270 | clip_avoidance_factor = 0.0f; | |
| 1314 |
2/2✓ Branch 0 taken 9461 times.
✓ Branch 1 taken 7270 times.
|
16731 | for (w = 0; w < ics->num_windows; w++) { |
| 1315 | 9461 | const float *wbuf = overlap + w * 128; | |
| 1316 | 9461 | const int wlen = 2048 / ics->num_windows; | |
| 1317 | 9461 | float max = 0; | |
| 1318 | int j; | ||
| 1319 | /* mdct input is 2 * output */ | ||
| 1320 |
2/2✓ Branch 0 taken 14888960 times.
✓ Branch 1 taken 9461 times.
|
14898421 | for (j = 0; j < wlen; j++) |
| 1321 |
2/2✓ Branch 0 taken 14471409 times.
✓ Branch 1 taken 417551 times.
|
14888960 | max = FFMAX(max, fabsf(wbuf[j])); |
| 1322 | 9461 | wi[ch].clipping[w] = max; | |
| 1323 | } | ||
| 1324 |
2/2✓ Branch 0 taken 9461 times.
✓ Branch 1 taken 7270 times.
|
16731 | for (w = 0; w < ics->num_windows; w++) { |
| 1325 |
2/2✓ Branch 0 taken 175 times.
✓ Branch 1 taken 9286 times.
|
9461 | if (wi[ch].clipping[w] > CLIP_AVOIDANCE_FACTOR) { |
| 1326 | 175 | ics->window_clipping[w] = 1; | |
| 1327 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 170 times.
|
175 | clip_avoidance_factor = FFMAX(clip_avoidance_factor, wi[ch].clipping[w]); |
| 1328 | } else { | ||
| 1329 | 9286 | ics->window_clipping[w] = 0; | |
| 1330 | } | ||
| 1331 | } | ||
| 1332 |
2/2✓ Branch 0 taken 157 times.
✓ Branch 1 taken 7113 times.
|
7270 | if (clip_avoidance_factor > CLIP_AVOIDANCE_FACTOR) { |
| 1333 | 157 | ics->clip_avoidance_factor = CLIP_AVOIDANCE_FACTOR / clip_avoidance_factor; | |
| 1334 | } else { | ||
| 1335 | 7113 | ics->clip_avoidance_factor = 1.0f; | |
| 1336 | } | ||
| 1337 | |||
| 1338 | 7270 | apply_window_and_mdct(s, sce, overlap); | |
| 1339 | |||
| 1340 |
2/2✓ Branch 0 taken 7444480 times.
✓ Branch 1 taken 7270 times.
|
7451750 | for (k = 0; k < 1024; k++) { |
| 1341 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7444480 times.
|
7444480 | if (!(fabs(cpe->ch[ch].coeffs[k]) < 1E16)) { // Ensure headroom for energy calculation |
| 1342 | ✗ | av_log(avctx, AV_LOG_ERROR, "Input contains (near) NaN/+-Inf\n"); | |
| 1343 | ✗ | return AVERROR(EINVAL); | |
| 1344 | } | ||
| 1345 | } | ||
| 1346 | 7270 | avoid_clipping(s, sce); | |
| 1347 | } | ||
| 1348 | } | ||
| 1349 | 4014 | start_ch += chans; | |
| 1350 | } | ||
| 1351 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3492 times.
|
3492 | if ((ret = ff_alloc_packet(avctx, avpkt, 8192 * s->channels)) < 0) |
| 1352 | ✗ | return ret; | |
| 1353 | 3492 | frame_bits = its = 0; | |
| 1354 | do { | ||
| 1355 | 4887 | init_put_bits(&s->pb, avpkt->data, avpkt->size); | |
| 1356 | |||
| 1357 |
4/4✓ Branch 0 taken 42 times.
✓ Branch 1 taken 4845 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 35 times.
|
4887 | if ((avctx->frame_num & 0xFF)==1 && !(avctx->flags & AV_CODEC_FLAG_BITEXACT)) |
| 1358 | 7 | put_bitstream_info(s, LIBAVCODEC_IDENT); | |
| 1359 | 4887 | start_ch = 0; | |
| 1360 | 4887 | target_bits = 0; | |
| 1361 | 4887 | memset(chan_el_counter, 0, sizeof(chan_el_counter)); | |
| 1362 |
2/2✓ Branch 0 taken 5511 times.
✓ Branch 1 taken 4887 times.
|
10398 | for (i = 0; i < s->chan_map[0]; i++) { |
| 1363 | 5511 | FFPsyWindowInfo* wi = windows + start_ch; | |
| 1364 | const float *coeffs[2]; | ||
| 1365 | 5511 | tag = s->chan_map[i+1]; | |
| 1366 |
2/2✓ Branch 0 taken 4685 times.
✓ Branch 1 taken 826 times.
|
5511 | chans = tag == TYPE_CPE ? 2 : 1; |
| 1367 | 5511 | cpe = &s->cpe[i]; | |
| 1368 | 5511 | cpe->common_window = 0; | |
| 1369 | 5511 | memset(cpe->is_mask, 0, sizeof(cpe->is_mask)); | |
| 1370 | 5511 | memset(cpe->ms_mask, 0, sizeof(cpe->ms_mask)); | |
| 1371 | 5511 | put_bits(&s->pb, 3, tag); | |
| 1372 | 5511 | put_bits(&s->pb, 4, chan_el_counter[tag]++); | |
| 1373 |
2/2✓ Branch 0 taken 10196 times.
✓ Branch 1 taken 5511 times.
|
15707 | for (ch = 0; ch < chans; ch++) { |
| 1374 | 10196 | sce = &cpe->ch[ch]; | |
| 1375 | 10196 | coeffs[ch] = sce->coeffs; | |
| 1376 | 10196 | memset(&sce->tns, 0, sizeof(TemporalNoiseShaping)); | |
| 1377 |
2/2✓ Branch 0 taken 1305088 times.
✓ Branch 1 taken 10196 times.
|
1315284 | for (w = 0; w < 128; w++) |
| 1378 |
2/2✓ Branch 0 taken 16157 times.
✓ Branch 1 taken 1288931 times.
|
1305088 | if (sce->band_type[w] > RESERVED_BT) |
| 1379 | 16157 | sce->band_type[w] = 0; | |
| 1380 | } | ||
| 1381 | 5511 | s->psy.bitres.alloc = -1; | |
| 1382 | 5511 | s->psy.bitres.bits = s->last_frame_pb_count / s->channels; | |
| 1383 | 5511 | s->psy.model->analyze(&s->psy, start_ch, coeffs, wi); | |
| 1384 |
1/2✓ Branch 0 taken 5511 times.
✗ Branch 1 not taken.
|
5511 | if (s->psy.bitres.alloc > 0) { |
| 1385 | /* Lambda unused here on purpose, we need to take psy's unscaled allocation */ | ||
| 1386 | 11022 | target_bits += s->psy.bitres.alloc | |
| 1387 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5511 times.
|
5511 | * (s->lambda / (avctx->global_quality ? avctx->global_quality : 120)); |
| 1388 | 5511 | s->psy.bitres.alloc /= chans; | |
| 1389 | } | ||
| 1390 | 5511 | s->cur_type = tag; | |
| 1391 |
2/2✓ Branch 0 taken 4685 times.
✓ Branch 1 taken 826 times.
|
5511 | if (chans > 1 |
| 1392 |
1/2✓ Branch 0 taken 4685 times.
✗ Branch 1 not taken.
|
4685 | && wi[0].window_type[0] == wi[1].window_type[0] |
| 1393 |
1/2✓ Branch 0 taken 4685 times.
✗ Branch 1 not taken.
|
4685 | && wi[0].window_shape == wi[1].window_shape) { |
| 1394 | |||
| 1395 | 4685 | cpe->common_window = 1; | |
| 1396 |
2/2✓ Branch 0 taken 5784 times.
✓ Branch 1 taken 4685 times.
|
10469 | for (w = 0; w < wi[0].num_windows; w++) { |
| 1397 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5784 times.
|
5784 | if (wi[0].grouping[w] != wi[1].grouping[w]) { |
| 1398 | ✗ | cpe->common_window = 0; | |
| 1399 | ✗ | break; | |
| 1400 | } | ||
| 1401 | } | ||
| 1402 | } | ||
| 1403 | |||
| 1404 |
3/4✓ Branch 0 taken 1835 times.
✓ Branch 1 taken 3676 times.
✓ Branch 2 taken 1835 times.
✗ Branch 3 not taken.
|
7346 | const int use_tns = s->options.tns && s->coder->search_for_tns && |
| 1405 |
1/2✓ Branch 0 taken 1835 times.
✗ Branch 1 not taken.
|
1835 | s->coder->apply_tns_filt; |
| 1406 | |||
| 1407 | /* The NMR coder rate-controls itself and never re-quantizes, so TNS must run | ||
| 1408 | * before the quantizer */ | ||
| 1409 | 5511 | const int tns_first = s->options.coder == AAC_CODER_NMR; | |
| 1410 |
3/4✓ Branch 0 taken 1096 times.
✓ Branch 1 taken 4415 times.
✓ Branch 2 taken 1096 times.
✗ Branch 3 not taken.
|
5511 | if (tns_first && use_tns) { |
| 1411 |
2/2✓ Branch 0 taken 1530 times.
✓ Branch 1 taken 1096 times.
|
2626 | for (ch = 0; ch < chans; ch++) { |
| 1412 | 1530 | sce = &cpe->ch[ch]; | |
| 1413 | 1530 | s->cur_channel = start_ch + ch; | |
| 1414 | /* mono: mark_pns before TNS so the region cap sees PNS bands. Stereo | ||
| 1415 | * PNS is marked in its own block (below) after the stereo decision. */ | ||
| 1416 |
4/6✓ Branch 0 taken 662 times.
✓ Branch 1 taken 868 times.
✓ Branch 2 taken 662 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 662 times.
✗ Branch 5 not taken.
|
1530 | if (chans == 1 && s->options.pns && s->coder->mark_pns) |
| 1417 | 662 | s->coder->mark_pns(s, avctx, sce); | |
| 1418 | 1530 | s->coder->search_for_tns(s, sce); | |
| 1419 | 1530 | s->coder->apply_tns_filt(s, sce); | |
| 1420 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1521 times.
|
1530 | if (sce->tns.present) |
| 1421 | 9 | tns_mode = 1; | |
| 1422 | } | ||
| 1423 | } | ||
| 1424 | |||
| 1425 | /* NMR stereo PNS (imaging-safe). Mark each channel's noise-like bands on the | ||
| 1426 | * original L/R psy, then keep PNS only where BOTH channels are noise-like. */ | ||
| 1427 |
5/6✓ Branch 0 taken 4685 times.
✓ Branch 1 taken 826 times.
✓ Branch 2 taken 4685 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 434 times.
✓ Branch 5 taken 4251 times.
|
5511 | if (chans == 2 && cpe->common_window && tns_first && |
| 1428 |
2/4✓ Branch 0 taken 434 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 434 times.
✗ Branch 3 not taken.
|
434 | s->options.pns && s->coder->mark_pns) { |
| 1429 | 434 | s->cur_channel = start_ch; s->coder->mark_pns(s, avctx, &cpe->ch[0]); | |
| 1430 | 434 | s->cur_channel = start_ch + 1; s->coder->mark_pns(s, avctx, &cpe->ch[1]); | |
| 1431 |
2/2✓ Branch 0 taken 55552 times.
✓ Branch 1 taken 434 times.
|
55986 | for (int b = 0; b < 128; b++) |
| 1432 |
4/4✓ Branch 0 taken 10014 times.
✓ Branch 1 taken 45538 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 10009 times.
|
55552 | if (!cpe->ch[0].can_pns[b] || !cpe->ch[1].can_pns[b]) |
| 1433 | 45543 | cpe->ch[0].can_pns[b] = cpe->ch[1].can_pns[b] = 0; | |
| 1434 | } | ||
| 1435 | |||
| 1436 | /* The NMR coder decides I/S and M/S BEFORE quantization, from the psy model, | ||
| 1437 | * and the trellis then allocates natively on the coeffs actually coded. */ | ||
| 1438 |
5/6✓ Branch 0 taken 4685 times.
✓ Branch 1 taken 826 times.
✓ Branch 2 taken 4685 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 434 times.
✓ Branch 5 taken 4251 times.
|
5511 | if (chans == 2 && cpe->common_window && s->options.coder == AAC_CODER_NMR && |
| 1439 |
3/4✓ Branch 0 taken 297 times.
✓ Branch 1 taken 137 times.
✓ Branch 2 taken 297 times.
✗ Branch 3 not taken.
|
434 | (s->options.mid_side || s->options.intensity_stereo)) { |
| 1440 | 434 | s->cur_channel = start_ch; | |
| 1441 | 434 | nmr_decide_stereo(s, cpe); | |
| 1442 | } | ||
| 1443 | /* NMR pools the CPE bit budget: both channels of a pair are solved | ||
| 1444 | * jointly under one shared lambda (see aaccoder_nmr.h). */ | ||
| 1445 |
3/4✓ Branch 0 taken 1096 times.
✓ Branch 1 taken 4415 times.
✓ Branch 2 taken 1096 times.
✗ Branch 3 not taken.
|
5511 | if (s->options.coder == AAC_CODER_NMR && s->nmr) |
| 1446 | 1096 | s->nmr->pair = (chans == 2); | |
| 1447 |
2/2✓ Branch 0 taken 10196 times.
✓ Branch 1 taken 5511 times.
|
15707 | for (ch = 0; ch < chans; ch++) { |
| 1448 | 10196 | s->cur_channel = start_ch + ch; | |
| 1449 | /* NMR PNS is mono-only */ | ||
| 1450 |
5/6✓ Branch 0 taken 2844 times.
✓ Branch 1 taken 7352 times.
✓ Branch 2 taken 2844 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1314 times.
✓ Branch 5 taken 1530 times.
|
10196 | if (s->options.pns && s->coder->mark_pns && !tns_first) |
| 1451 | 1314 | s->coder->mark_pns(s, avctx, &cpe->ch[ch]); | |
| 1452 | 10196 | s->coder->search_for_quantizers(avctx, s, &cpe->ch[ch], s->lambda); | |
| 1453 | } | ||
| 1454 |
2/2✓ Branch 0 taken 10196 times.
✓ Branch 1 taken 5511 times.
|
15707 | for (ch = 0; ch < chans; ch++) { /* TNS (non-NMR) and PNS */ |
| 1455 | 10196 | sce = &cpe->ch[ch]; | |
| 1456 | 10196 | s->cur_channel = start_ch + ch; | |
| 1457 |
4/4✓ Branch 0 taken 8666 times.
✓ Branch 1 taken 1530 times.
✓ Branch 2 taken 1314 times.
✓ Branch 3 taken 7352 times.
|
10196 | if (!tns_first && use_tns) { |
| 1458 | 1314 | s->coder->search_for_tns(s, sce); | |
| 1459 | 1314 | s->coder->apply_tns_filt(s, sce); | |
| 1460 |
2/2✓ Branch 0 taken 54 times.
✓ Branch 1 taken 1260 times.
|
1314 | if (sce->tns.present) |
| 1461 | 54 | tns_mode = 1; | |
| 1462 | } | ||
| 1463 |
4/4✓ Branch 0 taken 2844 times.
✓ Branch 1 taken 7352 times.
✓ Branch 2 taken 1314 times.
✓ Branch 3 taken 1530 times.
|
10196 | if (s->options.pns && s->coder->search_for_pns) |
| 1464 | 1314 | s->coder->search_for_pns(s, avctx, sce); | |
| 1465 | } | ||
| 1466 | 5511 | s->cur_channel = start_ch; | |
| 1467 |
2/2✓ Branch 0 taken 1836 times.
✓ Branch 1 taken 3675 times.
|
5511 | if (s->options.intensity_stereo) { /* Intensity Stereo */ |
| 1468 |
2/2✓ Branch 0 taken 740 times.
✓ Branch 1 taken 1096 times.
|
1836 | if (s->options.coder != AAC_CODER_NMR) { /* NMR: decided pre-search */ |
| 1469 |
1/2✓ Branch 0 taken 740 times.
✗ Branch 1 not taken.
|
740 | if (s->coder->search_for_is) |
| 1470 | 740 | s->coder->search_for_is(s, avctx, cpe); | |
| 1471 | 740 | apply_intensity_stereo(cpe); | |
| 1472 | } | ||
| 1473 |
2/2✓ Branch 0 taken 449 times.
✓ Branch 1 taken 1387 times.
|
1836 | if (cpe->is_mode) is_mode = 1; |
| 1474 | } | ||
| 1475 |
4/4✓ Branch 0 taken 1066 times.
✓ Branch 1 taken 4445 times.
✓ Branch 2 taken 411 times.
✓ Branch 3 taken 655 times.
|
5511 | if (s->options.mid_side && s->options.coder != AAC_CODER_NMR) { /* Mid/Side stereo */ |
| 1476 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 411 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
411 | if (s->options.mid_side == -1 && s->coder->search_for_ms) |
| 1477 | ✗ | s->coder->search_for_ms(s, cpe); | |
| 1478 |
1/2✓ Branch 0 taken 411 times.
✗ Branch 1 not taken.
|
411 | else if (cpe->common_window) |
| 1479 | 411 | memset(cpe->ms_mask, 1, sizeof(cpe->ms_mask)); | |
| 1480 | 411 | apply_mid_side_stereo(cpe); | |
| 1481 | } | ||
| 1482 | 5511 | adjust_frame_information(cpe, chans); | |
| 1483 |
2/2✓ Branch 0 taken 4685 times.
✓ Branch 1 taken 826 times.
|
5511 | if (chans == 2) { |
| 1484 | 4685 | put_bits(&s->pb, 1, cpe->common_window); | |
| 1485 |
1/2✓ Branch 0 taken 4685 times.
✗ Branch 1 not taken.
|
4685 | if (cpe->common_window) { |
| 1486 | 4685 | put_ics_info(s, &cpe->ch[0].ics); | |
| 1487 | 4685 | encode_ms_info(&s->pb, cpe); | |
| 1488 |
2/2✓ Branch 0 taken 852 times.
✓ Branch 1 taken 3833 times.
|
4685 | if (cpe->ms_mode) ms_mode = 1; |
| 1489 | } | ||
| 1490 | } | ||
| 1491 |
2/2✓ Branch 0 taken 10196 times.
✓ Branch 1 taken 5511 times.
|
15707 | for (ch = 0; ch < chans; ch++) { |
| 1492 | 10196 | s->cur_channel = start_ch + ch; | |
| 1493 | 10196 | encode_individual_channel(avctx, s, &cpe->ch[ch], cpe->common_window); | |
| 1494 | } | ||
| 1495 | 5511 | start_ch += chans; | |
| 1496 | } | ||
| 1497 | |||
| 1498 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4887 times.
|
4887 | if (avctx->flags & AV_CODEC_FLAG_QSCALE) { |
| 1499 | /* When using a constant Q-scale, don't mess with lambda */ | ||
| 1500 | ✗ | break; | |
| 1501 | } | ||
| 1502 | |||
| 1503 | 4887 | frame_bits = put_bits_count(&s->pb); | |
| 1504 | |||
| 1505 | /* The NMR coder rate-controls itself (global-lambda reservoir servo): | ||
| 1506 | * per-frame bits intentionally float around the nominal rate, so skip | ||
| 1507 | * the lambda rate loop and only intervene on a hard overflow. */ | ||
| 1508 |
3/4✓ Branch 0 taken 718 times.
✓ Branch 1 taken 4169 times.
✓ Branch 2 taken 718 times.
✗ Branch 3 not taken.
|
4887 | if (s->options.coder == AAC_CODER_NMR && avctx->bit_rate_tolerance != 0 && |
| 1509 |
1/2✓ Branch 0 taken 718 times.
✗ Branch 1 not taken.
|
718 | frame_bits < 6144 * s->channels - 3) |
| 1510 | 718 | break; | |
| 1511 | |||
| 1512 | /* rate control stuff | ||
| 1513 | * allow between the nominal bitrate, and what psy's bit reservoir says to target | ||
| 1514 | * but drift towards the nominal bitrate always | ||
| 1515 | */ | ||
| 1516 | 4169 | rate_bits = avctx->bit_rate * 1024 / avctx->sample_rate; | |
| 1517 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4169 times.
|
4169 | rate_bits = FFMIN(rate_bits, 6144 * s->channels - 3); |
| 1518 | 4169 | too_many_bits = FFMAX(target_bits, rate_bits); | |
| 1519 |
2/2✓ Branch 0 taken 2025 times.
✓ Branch 1 taken 2144 times.
|
4169 | too_many_bits = FFMIN(too_many_bits, 6144 * s->channels - 3); |
| 1520 | 4169 | too_few_bits = FFMIN(FFMAX(rate_bits - rate_bits/4, target_bits), too_many_bits); | |
| 1521 | |||
| 1522 | /* When strict bit-rate control is demanded */ | ||
| 1523 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4169 times.
|
4169 | if (avctx->bit_rate_tolerance == 0) { |
| 1524 | ✗ | if (rate_bits < frame_bits) { | |
| 1525 | ✗ | float ratio = ((float)rate_bits) / frame_bits; | |
| 1526 | ✗ | s->lambda *= FFMIN(0.9f, ratio); | |
| 1527 | ✗ | continue; | |
| 1528 | } | ||
| 1529 | /* reset lambda when solution is found */ | ||
| 1530 | ✗ | s->lambda = avctx->global_quality > 0 ? avctx->global_quality : 120; | |
| 1531 | ✗ | break; | |
| 1532 | } | ||
| 1533 | |||
| 1534 | /* When using ABR, be strict (but only for increasing) */ | ||
| 1535 | 4169 | too_few_bits = too_few_bits - too_few_bits/8; | |
| 1536 | 4169 | too_many_bits = too_many_bits + too_many_bits/2; | |
| 1537 | |||
| 1538 |
2/2✓ Branch 0 taken 1395 times.
✓ Branch 1 taken 2774 times.
|
4169 | if ( its == 0 /* for steady-state Q-scale tracking */ |
| 1539 |
5/6✓ Branch 0 taken 1117 times.
✓ Branch 1 taken 278 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1116 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
1395 | || (its < 5 && (frame_bits < too_few_bits || frame_bits > too_many_bits)) |
| 1540 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 279 times.
|
279 | || frame_bits >= 6144 * s->channels - 3 ) |
| 1541 | { | ||
| 1542 | 3890 | float ratio = ((float)rate_bits) / frame_bits; | |
| 1543 | |||
| 1544 |
3/4✓ Branch 0 taken 2165 times.
✓ Branch 1 taken 1725 times.
✓ Branch 2 taken 2165 times.
✗ Branch 3 not taken.
|
3890 | if (frame_bits >= too_few_bits && frame_bits <= too_many_bits) { |
| 1545 | /* | ||
| 1546 | * This path is for steady-state Q-scale tracking | ||
| 1547 | * When frame bits fall within the stable range, we still need to adjust | ||
| 1548 | * lambda to maintain it like so in a stable fashion (large jumps in lambda | ||
| 1549 | * create artifacts and should be avoided), but slowly | ||
| 1550 | */ | ||
| 1551 | 2165 | ratio = sqrtf(sqrtf(ratio)); | |
| 1552 | 2165 | ratio = av_clipf(ratio, 0.9f, 1.1f); | |
| 1553 | } else { | ||
| 1554 | /* Not so fast though */ | ||
| 1555 | 1725 | ratio = sqrtf(ratio); | |
| 1556 | } | ||
| 1557 | 3890 | s->lambda = av_clipf(s->lambda * ratio, FLT_EPSILON, 65536.f); | |
| 1558 | |||
| 1559 | /* Keep iterating if we must reduce and lambda is in the sky */ | ||
| 1560 |
4/4✓ Branch 0 taken 3862 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 1367 times.
✓ Branch 3 taken 2495 times.
|
3890 | if (ratio > 0.9f && ratio < 1.1f) { |
| 1561 | break; | ||
| 1562 | } else { | ||
| 1563 |
6/8✓ Branch 0 taken 1394 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1394 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1382 times.
✓ Branch 5 taken 12 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1382 times.
|
1395 | if (is_mode || ms_mode || tns_mode || pred_mode) { |
| 1564 |
2/2✓ Branch 0 taken 49 times.
✓ Branch 1 taken 13 times.
|
62 | for (i = 0; i < s->chan_map[0]; i++) { |
| 1565 | // Must restore coeffs | ||
| 1566 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 48 times.
|
49 | chans = tag == TYPE_CPE ? 2 : 1; |
| 1567 | 49 | cpe = &s->cpe[i]; | |
| 1568 |
2/2✓ Branch 0 taken 50 times.
✓ Branch 1 taken 49 times.
|
99 | for (ch = 0; ch < chans; ch++) |
| 1569 | 50 | memcpy(cpe->ch[ch].coeffs, cpe->ch[ch].pcoeffs, sizeof(cpe->ch[ch].coeffs)); | |
| 1570 | } | ||
| 1571 | } | ||
| 1572 | 1395 | its++; | |
| 1573 | } | ||
| 1574 | } else { | ||
| 1575 | break; | ||
| 1576 | } | ||
| 1577 | } while (1); | ||
| 1578 | |||
| 1579 | /* tool-usage stats over the final per-band decisions of this frame */ | ||
| 1580 |
2/2✓ Branch 0 taken 4014 times.
✓ Branch 1 taken 3492 times.
|
7506 | for (i = 0; i < s->chan_map[0]; i++) { |
| 1581 |
2/2✓ Branch 0 taken 3256 times.
✓ Branch 1 taken 758 times.
|
4014 | int etag = s->chan_map[i + 1], echans = etag == TYPE_CPE ? 2 : 1; |
| 1582 | 4014 | ChannelElement *ce = &s->cpe[i]; | |
| 1583 | 4014 | IndividualChannelStream *ics = &ce->ch[0].ics; | |
| 1584 |
2/2✓ Branch 0 taken 7270 times.
✓ Branch 1 taken 4014 times.
|
11284 | for (ch = 0; ch < echans; ch++) { /* per-channel frame stats */ |
| 1585 | 7270 | int is_short = ce->ch[ch].ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE; | |
| 1586 | 7270 | s->stat_chans++; | |
| 1587 |
2/2✓ Branch 0 taken 313 times.
✓ Branch 1 taken 6957 times.
|
7270 | if (is_short) |
| 1588 | 313 | s->stat_short++; | |
| 1589 |
2/2✓ Branch 0 taken 49 times.
✓ Branch 1 taken 7221 times.
|
7270 | if (ce->ch[ch].tns.present) { |
| 1590 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
|
49 | if (is_short) s->stat_tns_short++; |
| 1591 | 49 | else s->stat_tns_long++; | |
| 1592 | } | ||
| 1593 | } | ||
| 1594 |
2/2✓ Branch 0 taken 4482 times.
✓ Branch 1 taken 4014 times.
|
8496 | for (w = 0; w < ics->num_windows; w += ics->group_len[w]) { |
| 1595 |
2/2✓ Branch 0 taken 192048 times.
✓ Branch 1 taken 4482 times.
|
196530 | for (int g = 0; g < ics->num_swb; g++) { |
| 1596 | 192048 | int idx = w*16 + g, coded = 0; | |
| 1597 |
2/2✓ Branch 0 taken 352411 times.
✓ Branch 1 taken 192048 times.
|
544459 | for (ch = 0; ch < echans; ch++) { |
| 1598 | 352411 | SingleChannelElement *sce = &ce->ch[ch]; | |
| 1599 |
4/4✓ Branch 0 taken 15994 times.
✓ Branch 1 taken 336417 times.
✓ Branch 2 taken 14112 times.
✓ Branch 3 taken 1882 times.
|
352411 | if (sce->zeroes[idx] && sce->band_type[idx] == 0) |
| 1600 | 14112 | continue; | |
| 1601 | 338299 | s->stat_ch_bands++; | |
| 1602 |
2/2✓ Branch 0 taken 6345 times.
✓ Branch 1 taken 331954 times.
|
338299 | if (sce->band_type[idx] == NOISE_BT) |
| 1603 | 6345 | s->stat_pns++; | |
| 1604 | 338299 | coded = 1; | |
| 1605 | } | ||
| 1606 |
4/4✓ Branch 0 taken 160363 times.
✓ Branch 1 taken 31685 times.
✓ Branch 2 taken 156363 times.
✓ Branch 3 taken 4000 times.
|
192048 | if (etag == TYPE_CPE && coded) { |
| 1607 | 156363 | s->stat_cpe_bands++; | |
| 1608 |
2/2✓ Branch 0 taken 26309 times.
✓ Branch 1 taken 130054 times.
|
156363 | if (ce->ms_mask[idx]) s->stat_ms++; |
| 1609 |
2/2✓ Branch 0 taken 9326 times.
✓ Branch 1 taken 147037 times.
|
156363 | if (ce->is_mask[idx]) s->stat_is++; |
| 1610 | } | ||
| 1611 | } | ||
| 1612 | } | ||
| 1613 | } | ||
| 1614 | |||
| 1615 | 3492 | put_bits(&s->pb, 3, TYPE_END); | |
| 1616 | 3492 | flush_put_bits(&s->pb); | |
| 1617 | |||
| 1618 | 3492 | s->last_frame_pb_count = put_bits_count(&s->pb); | |
| 1619 | |||
| 1620 | /* NMR rate accounting: how many bits the frame really took beyond what the | ||
| 1621 | * trellis counted; feeds the next frame's budget correction */ | ||
| 1622 |
2/2✓ Branch 0 taken 718 times.
✓ Branch 1 taken 2774 times.
|
3492 | if (s->nmr) { |
| 1623 | 718 | int counted = 0; | |
| 1624 |
2/2✓ Branch 0 taken 1530 times.
✓ Branch 1 taken 718 times.
|
2248 | for (i = 0; i < s->channels; i++) |
| 1625 | 1530 | counted += s->nmr->counted[i]; | |
| 1626 |
2/2✓ Branch 0 taken 712 times.
✓ Branch 1 taken 6 times.
|
718 | if (counted > 0) { |
| 1627 | 712 | float side = (float)s->last_frame_pb_count - counted; | |
| 1628 |
2/2✓ Branch 0 taken 698 times.
✓ Branch 1 taken 14 times.
|
712 | if (s->nmr->side_inited) { |
| 1629 | 698 | s->nmr->side_ema += 0.125f * (side - s->nmr->side_ema); | |
| 1630 | } else { | ||
| 1631 | 14 | s->nmr->side_ema = side; | |
| 1632 | 14 | s->nmr->side_inited = 1; | |
| 1633 | } | ||
| 1634 | } | ||
| 1635 | } | ||
| 1636 | 3492 | avpkt->size = put_bytes_output(&s->pb); | |
| 1637 | |||
| 1638 |
4/4✓ Branch 0 taken 718 times.
✓ Branch 1 taken 2774 times.
✓ Branch 2 taken 712 times.
✓ Branch 3 taken 6 times.
|
3492 | s->lambda_sum += (s->nmr && s->nmr->lam_rc > 0.0f) ? s->nmr->lam_rc : s->lambda; |
| 1639 | 3492 | s->lambda_count++; | |
| 1640 | |||
| 1641 | 3492 | ret = ff_af_queue_remove(&s->afq, avctx->frame_size, avpkt); | |
| 1642 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3492 times.
|
3492 | if (ret < 0) |
| 1643 | ✗ | return ret; | |
| 1644 | |||
| 1645 | 3492 | avpkt->flags |= AV_PKT_FLAG_KEY; | |
| 1646 | |||
| 1647 | 3492 | *got_packet_ptr = 1; | |
| 1648 | 3492 | return 0; | |
| 1649 | } | ||
| 1650 | |||
| 1651 | 23 | static av_cold int aac_encode_end(AVCodecContext *avctx) | |
| 1652 | { | ||
| 1653 | 23 | AACEncContext *s = avctx->priv_data; | |
| 1654 | |||
| 1655 | 29 | av_log(avctx, AV_LOG_INFO, | |
| 1656 | "Qavg: %.3f Tr: %.1f%% TNS(L): %.1f%% TNS(S): %.1f%% M/S: %.1f%% I/S: %.1f%% PNS: %.1f%%\n", | ||
| 1657 |
1/2✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
|
23 | s->lambda_count ? s->lambda_sum / s->lambda_count : NAN, |
| 1658 |
1/2✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
|
23 | s->stat_chans ? 100.0 * s->stat_short / s->stat_chans : 0.0, |
| 1659 |
1/2✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
|
23 | s->stat_chans - s->stat_short ? 100.0 * s->stat_tns_long / (s->stat_chans - s->stat_short) : 0.0, |
| 1660 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 1 times.
|
23 | s->stat_short ? 100.0 * s->stat_tns_short / s->stat_short : 0.0, |
| 1661 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 2 times.
|
23 | s->stat_cpe_bands ? 100.0 * s->stat_ms / s->stat_cpe_bands : 0.0, |
| 1662 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 2 times.
|
23 | s->stat_cpe_bands ? 100.0 * s->stat_is / s->stat_cpe_bands : 0.0, |
| 1663 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 1 times.
|
23 | s->stat_ch_bands ? 100.0 * s->stat_pns / s->stat_ch_bands : 0.0); |
| 1664 | |||
| 1665 | 23 | av_tx_uninit(&s->mdct1024); | |
| 1666 | 23 | av_tx_uninit(&s->mdct128); | |
| 1667 | 23 | ff_psy_end(&s->psy); | |
| 1668 | 23 | ff_lpc_end(&s->lpc); | |
| 1669 | 23 | av_freep(&s->buffer.samples); | |
| 1670 | 23 | av_freep(&s->cpe); | |
| 1671 | 23 | av_freep(&s->fdsp); | |
| 1672 | 23 | av_freep(&s->nmr); | |
| 1673 | 23 | ff_af_queue_close(&s->afq); | |
| 1674 | 23 | return 0; | |
| 1675 | } | ||
| 1676 | |||
| 1677 | 23 | static av_cold int dsp_init(AVCodecContext *avctx, AACEncContext *s) | |
| 1678 | { | ||
| 1679 | 23 | int ret = 0; | |
| 1680 | 23 | float scale = 32768.0f; | |
| 1681 | |||
| 1682 | 23 | s->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT); | |
| 1683 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
|
23 | if (!s->fdsp) |
| 1684 | ✗ | return AVERROR(ENOMEM); | |
| 1685 | |||
| 1686 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
|
23 | if ((ret = av_tx_init(&s->mdct1024, &s->mdct1024_fn, AV_TX_FLOAT_MDCT, 0, |
| 1687 | 1024, &scale, 0)) < 0) | ||
| 1688 | ✗ | return ret; | |
| 1689 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
|
23 | if ((ret = av_tx_init(&s->mdct128, &s->mdct128_fn, AV_TX_FLOAT_MDCT, 0, |
| 1690 | 128, &scale, 0)) < 0) | ||
| 1691 | ✗ | return ret; | |
| 1692 | |||
| 1693 | 23 | return 0; | |
| 1694 | } | ||
| 1695 | |||
| 1696 | 23 | static av_cold int alloc_buffers(AVCodecContext *avctx, AACEncContext *s) | |
| 1697 | { | ||
| 1698 | int ch; | ||
| 1699 |
1/2✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
|
23 | if (!FF_ALLOCZ_TYPED_ARRAY(s->buffer.samples, s->channels * 3 * 1024) || |
| 1700 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
|
23 | !FF_ALLOCZ_TYPED_ARRAY(s->cpe, s->chan_map[0])) |
| 1701 | ✗ | return AVERROR(ENOMEM); | |
| 1702 | |||
| 1703 |
2/2✓ Branch 0 taken 117 times.
✓ Branch 1 taken 23 times.
|
140 | for(ch = 0; ch < s->channels; ch++) |
| 1704 | 117 | s->planar_samples[ch] = s->buffer.samples + 3 * 1024 * ch; | |
| 1705 | |||
| 1706 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 8 times.
|
23 | if (s->options.coder == AAC_CODER_NMR) { |
| 1707 | 15 | s->nmr = av_mallocz(sizeof(*s->nmr)); | |
| 1708 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | if (!s->nmr) |
| 1709 | ✗ | return AVERROR(ENOMEM); | |
| 1710 | } | ||
| 1711 | |||
| 1712 | 23 | return 0; | |
| 1713 | } | ||
| 1714 | |||
| 1715 | 7 | static av_cold int check_height_ext(AVCodecContext *avctx, AACEncContext *s) | |
| 1716 | { | ||
| 1717 |
1/2✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
|
63 | for (int i = 0; i < avctx->ch_layout.nb_channels; i++) { |
| 1718 | 63 | enum AVChannel ch = av_channel_layout_channel_from_index(&avctx->ch_layout, i); | |
| 1719 |
3/4✓ Branch 0 taken 7 times.
✓ Branch 1 taken 56 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
63 | if (ch >= AV_CHAN_TOP_FRONT_LEFT && ch <= AV_CHAN_TOP_BACK_RIGHT) |
| 1720 | 7 | return 1; | |
| 1721 | // Layouts with TOP_SIDE channels also include the above. | ||
| 1722 | } | ||
| 1723 | |||
| 1724 | ✗ | return 0; | |
| 1725 | } | ||
| 1726 | |||
| 1727 | 23 | static av_cold int aac_encode_init(AVCodecContext *avctx) | |
| 1728 | { | ||
| 1729 | 23 | AACEncContext *s = avctx->priv_data; | |
| 1730 | 23 | int i, ret = 0; | |
| 1731 | int chcfg; | ||
| 1732 | const uint8_t *sizes[2]; | ||
| 1733 | uint8_t grouping[AAC_MAX_CHANNELS]; | ||
| 1734 | int lengths[2]; | ||
| 1735 | |||
| 1736 | /* Constants */ | ||
| 1737 | 23 | s->last_frame_pb_count = 0; | |
| 1738 | 23 | avctx->frame_size = 1024; | |
| 1739 | 23 | avctx->initial_padding = 1024; | |
| 1740 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
|
23 | s->lambda = avctx->global_quality > 0 ? avctx->global_quality : 120; |
| 1741 | |||
| 1742 | /* Channel map and unspecified bitrate guessing */ | ||
| 1743 | 23 | s->channels = avctx->ch_layout.nb_channels; | |
| 1744 | |||
| 1745 | 23 | s->needs_pce = 1; | |
| 1746 |
2/2✓ Branch 0 taken 133 times.
✓ Branch 1 taken 6 times.
|
139 | for (chcfg = 1; chcfg < FF_ARRAY_ELEMS(aac_normal_chan_layouts); chcfg++) { |
| 1747 |
2/2✓ Branch 1 taken 17 times.
✓ Branch 2 taken 116 times.
|
133 | if (!av_channel_layout_compare(&avctx->ch_layout, &aac_normal_chan_layouts[chcfg])) { |
| 1748 | 17 | s->needs_pce = s->options.pce; | |
| 1749 | 17 | break; | |
| 1750 | } | ||
| 1751 | } | ||
| 1752 | |||
| 1753 |
3/6✓ Branch 0 taken 16 times.
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
23 | if (!s->needs_pce && chcfg == 7 /* 7.1(wide) */ && !s->options.allow_71wide) { |
| 1754 | /** | ||
| 1755 | * FFmpeg used to produce out-of-spec AAC files that mistagged 7.1 | ||
| 1756 | * as 7.1(wide), and this wark-around is still enabled by default in | ||
| 1757 | * aacdec.c, so avoid producing such files in the rare case that the | ||
| 1758 | * user correctly passed 7.1(wide) channel layout content. | ||
| 1759 | */ | ||
| 1760 | ✗ | av_log(avctx, AV_LOG_INFO, "Forcing the use of PCE to encode 7.1(wide) " | |
| 1761 | "channel layout to avoid ambiguity. Set -aac_allow_71wide 1 to " | ||
| 1762 | "override this behavior and force the use of spec-compliant " | ||
| 1763 | "channel configuration ID.\n"); | ||
| 1764 | ✗ | s->needs_pce = 1; | |
| 1765 | } | ||
| 1766 | |||
| 1767 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 16 times.
|
23 | if (s->needs_pce) { |
| 1768 | char buf[64]; | ||
| 1769 |
1/2✓ Branch 0 taken 227 times.
✗ Branch 1 not taken.
|
227 | for (i = 0; i < FF_ARRAY_ELEMS(aac_pce_configs); i++) |
| 1770 |
2/2✓ Branch 1 taken 7 times.
✓ Branch 2 taken 220 times.
|
227 | if (!av_channel_layout_compare(&avctx->ch_layout, &aac_pce_configs[i].layout)) |
| 1771 | 7 | break; | |
| 1772 | 7 | av_channel_layout_describe(&avctx->ch_layout, buf, sizeof(buf)); | |
| 1773 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | if (i == FF_ARRAY_ELEMS(aac_pce_configs)) { |
| 1774 | ✗ | av_log(avctx, AV_LOG_ERROR, "Unsupported channel layout \"%s\"\n", buf); | |
| 1775 | ✗ | return AVERROR(EINVAL); | |
| 1776 | } | ||
| 1777 | 7 | av_log(avctx, AV_LOG_INFO, "Using a PCE to encode channel layout \"%s\"\n", buf); | |
| 1778 | 7 | s->pce = aac_pce_configs[i]; | |
| 1779 | 7 | s->reorder_map = s->pce.reorder_map; | |
| 1780 | 7 | s->chan_map = s->pce.config_map; | |
| 1781 | 7 | s->needs_height_ext = check_height_ext(avctx, s); | |
| 1782 | 7 | chcfg = 0; | |
| 1783 | } else { | ||
| 1784 | 16 | s->reorder_map = aac_chan_maps[chcfg - 1]; | |
| 1785 | 16 | s->chan_map = aac_chan_configs[chcfg - 1]; | |
| 1786 | } | ||
| 1787 | |||
| 1788 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 7 times.
|
23 | if (!avctx->bit_rate) { |
| 1789 |
2/2✓ Branch 0 taken 61 times.
✓ Branch 1 taken 16 times.
|
77 | for (i = 1; i <= s->chan_map[0]; i++) { |
| 1790 |
2/2✓ Branch 0 taken 19 times.
✓ Branch 1 taken 42 times.
|
80 | avctx->bit_rate += s->chan_map[i] == TYPE_CPE ? 128000 : /* Pair */ |
| 1791 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 10 times.
|
19 | s->chan_map[i] == TYPE_LFE ? 16000 : /* LFE */ |
| 1792 | 69000 ; /* SCE */ | ||
| 1793 | } | ||
| 1794 | } | ||
| 1795 | |||
| 1796 | /* Samplerate */ | ||
| 1797 | 23 | for (int i = 0;; i++) { | |
| 1798 | 91 | av_assert1(i < 13); | |
| 1799 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 91 times.
|
114 | if (avctx->sample_rate == ff_mpeg4audio_sample_rates[i]) { |
| 1800 | 23 | s->samplerate_index = i; | |
| 1801 | 23 | break; | |
| 1802 | } | ||
| 1803 | } | ||
| 1804 | |||
| 1805 | /* Bitrate limiting */ | ||
| 1806 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
|
23 | WARN_IF(1024.0 * avctx->bit_rate / avctx->sample_rate > 6144 * s->channels, |
| 1807 | "Too many bits %f > %d per frame requested, clamping to max\n", | ||
| 1808 | 1024.0 * avctx->bit_rate / avctx->sample_rate, | ||
| 1809 | 6144 * s->channels); | ||
| 1810 |
1/2✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
|
23 | avctx->bit_rate = (int64_t)FFMIN(6144 * s->channels / 1024.0 * avctx->sample_rate, |
| 1811 | avctx->bit_rate); | ||
| 1812 | |||
| 1813 | /* Profile and option setting */ | ||
| 1814 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
|
23 | avctx->profile = avctx->profile == AV_PROFILE_UNKNOWN ? AV_PROFILE_AAC_LOW : |
| 1815 | avctx->profile; | ||
| 1816 |
1/2✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
|
23 | for (i = 0; i < FF_ARRAY_ELEMS(aacenc_profiles); i++) |
| 1817 |
1/2✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
|
23 | if (avctx->profile == aacenc_profiles[i]) |
| 1818 | 23 | break; | |
| 1819 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
|
23 | ERROR_IF(i == FF_ARRAY_ELEMS(aacenc_profiles), "Profile not supported!\n"); |
| 1820 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
|
23 | if (avctx->profile == AV_PROFILE_MPEG2_AAC_LOW) { |
| 1821 | ✗ | avctx->profile = AV_PROFILE_AAC_LOW; | |
| 1822 | ✗ | WARN_IF(s->options.pns, | |
| 1823 | "PNS unavailable in the \"mpeg2_aac_low\" profile, turning off\n"); | ||
| 1824 | ✗ | s->options.pns = 0; | |
| 1825 | } | ||
| 1826 | 23 | s->profile = avctx->profile; | |
| 1827 | |||
| 1828 | /* Coder limitations */ | ||
| 1829 | 23 | s->coder = &ff_aac_coders[s->options.coder]; | |
| 1830 | |||
| 1831 | /* M/S introduces horrible artifacts with multichannel files, this is temporary */ | ||
| 1832 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 15 times.
|
23 | if (s->channels > 3) |
| 1833 | 8 | s->options.mid_side = 0; | |
| 1834 | |||
| 1835 | /* Coding bandwidth, fixed at init time */ | ||
| 1836 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 18 times.
|
23 | if (avctx->cutoff > 0) { |
| 1837 | 5 | s->bandwidth = avctx->cutoff; | |
| 1838 | } else { | ||
| 1839 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
|
18 | int frame_br = (avctx->flags & AV_CODEC_FLAG_QSCALE) ? |
| 1840 | ✗ | (avctx->bit_rate / 2.0f * (s->lambda / 120.f) * 1.5f) : | |
| 1841 | 18 | (avctx->bit_rate / avctx->ch_layout.nb_channels); | |
| 1842 | |||
| 1843 |
3/4✓ Branch 0 taken 15 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
|
18 | if (s->options.coder == AAC_CODER_NMR && frame_br >= 24000) { |
| 1844 | static const int rates[] = { 24000, 32000, 48000, 64000, 96000, 192000 }; | ||
| 1845 | static const int bws[] = { 14000, 14000, 18500, 20000, 21000, 22000 }; | ||
| 1846 | 15 | int bw_i = 0; | |
| 1847 |
3/4✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
✓ Branch 3 taken 15 times.
|
46 | for (; bw_i < FF_ARRAY_ELEMS(rates) - 2 && frame_br > rates[bw_i + 1]; bw_i++); |
| 1848 | 15 | s->bandwidth = bws[bw_i] + (int)((int64_t)(bws[bw_i + 1] - bws[bw_i]) * | |
| 1849 | 15 | (frame_br - rates[bw_i]) / (rates[bw_i + 1] - rates[bw_i])); | |
| 1850 | 15 | s->bandwidth = FFMIN3(s->bandwidth, 22000, avctx->sample_rate / 2); | |
| 1851 | } else { | ||
| 1852 |
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) |
| 1853 | 1 | frame_br *= 1.15f; | |
| 1854 |
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, |
| 1855 | avctx->sample_rate)); | ||
| 1856 | } | ||
| 1857 | |||
| 1858 | 18 | s->bandwidth = FFMIN(FFMAX(s->bandwidth, 8000), avctx->sample_rate / 2); | |
| 1859 | } | ||
| 1860 | |||
| 1861 |
2/4✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
✗ Branch 3 not taken.
|
23 | if (!(avctx->flags & AV_CODEC_FLAG_QSCALE) && avctx->bit_rate > 0) { |
| 1862 | 23 | int bpc = avctx->bit_rate / avctx->ch_layout.nb_channels; | |
| 1863 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
23 | if (bpc <= 32000 && avctx->sample_rate > 32000) |
| 1864 | ✗ | av_log(avctx, AV_LOG_INFO, | |
| 1865 | "%d kb/s per channel at %d Hz: consider resampling the " | ||
| 1866 | "input to 32000 Hz or lower for better quality.\n", | ||
| 1867 | bpc / 1000, avctx->sample_rate); | ||
| 1868 | } | ||
| 1869 | |||
| 1870 | // Initialize static tables | ||
| 1871 | 23 | ff_aac_float_common_init(); | |
| 1872 | |||
| 1873 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
|
23 | if ((ret = dsp_init(avctx, s)) < 0) |
| 1874 | ✗ | return ret; | |
| 1875 | |||
| 1876 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
|
23 | if ((ret = alloc_buffers(avctx, s)) < 0) |
| 1877 | ✗ | return ret; | |
| 1878 | |||
| 1879 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
|
23 | if ((ret = put_audio_specific_config(avctx, chcfg))) |
| 1880 | ✗ | return ret; | |
| 1881 | |||
| 1882 | 23 | sizes[0] = ff_aac_swb_size_1024[s->samplerate_index]; | |
| 1883 | 23 | sizes[1] = ff_aac_swb_size_128[s->samplerate_index]; | |
| 1884 | 23 | lengths[0] = ff_aac_num_swb_1024[s->samplerate_index]; | |
| 1885 | 23 | lengths[1] = ff_aac_num_swb_128[s->samplerate_index]; | |
| 1886 |
2/2✓ Branch 0 taken 68 times.
✓ Branch 1 taken 23 times.
|
91 | for (i = 0; i < s->chan_map[0]; i++) |
| 1887 | 68 | grouping[i] = s->chan_map[i + 1] == TYPE_CPE; | |
| 1888 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
|
23 | if ((ret = ff_psy_init(&s->psy, avctx, 2, sizes, lengths, |
| 1889 | 23 | s->chan_map[0], grouping, s->bandwidth)) < 0) | |
| 1890 | ✗ | return ret; | |
| 1891 | 23 | ff_lpc_init(&s->lpc, 2*avctx->frame_size, TNS_MAX_ORDER, FF_LPC_TYPE_LEVINSON); | |
| 1892 | 23 | s->random_state = 0x1f2e3d4c; | |
| 1893 | |||
| 1894 | 23 | ff_aacenc_dsp_init(&s->aacdsp); | |
| 1895 | |||
| 1896 | 23 | ff_af_queue_init(avctx, &s->afq); | |
| 1897 | |||
| 1898 | 23 | return 0; | |
| 1899 | } | ||
| 1900 | |||
| 1901 | #define AACENC_FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM | ||
| 1902 | static const AVOption aacenc_options[] = { | ||
| 1903 | {"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"}, | ||
| 1904 | {"twoloop", "Two loop searching method", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_TWOLOOP}, INT_MIN, INT_MAX, AACENC_FLAGS, .unit = "coder"}, | ||
| 1905 | {"fast", "Fast search", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_FAST}, INT_MIN, INT_MAX, AACENC_FLAGS, .unit = "coder"}, | ||
| 1906 | {"nmr", "Noise-to-mask ratio scalefactor trellis", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_NMR}, INT_MIN, INT_MAX, AACENC_FLAGS, .unit = "coder"}, | ||
| 1907 | {"aac_ms", "Force M/S stereo coding", offsetof(AACEncContext, options.mid_side), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, AACENC_FLAGS}, | ||
| 1908 | {"aac_is", "Intensity stereo coding", offsetof(AACEncContext, options.intensity_stereo), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AACENC_FLAGS}, | ||
| 1909 | {"aac_pns", "Perceptual noise substitution", offsetof(AACEncContext, options.pns), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AACENC_FLAGS}, | ||
| 1910 | {"aac_tns", "Temporal noise shaping", offsetof(AACEncContext, options.tns), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AACENC_FLAGS}, | ||
| 1911 | {"aac_pce", "Forces the use of PCEs", offsetof(AACEncContext, options.pce), AV_OPT_TYPE_BOOL, {.i64 = 0}, -1, 1, AACENC_FLAGS}, | ||
| 1912 | {"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}, | ||
| 1913 | {"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}, | ||
| 1914 | FF_AAC_PROFILE_OPTS | ||
| 1915 | {NULL} | ||
| 1916 | }; | ||
| 1917 | |||
| 1918 | static const AVClass aacenc_class = { | ||
| 1919 | .class_name = "AAC encoder", | ||
| 1920 | .item_name = av_default_item_name, | ||
| 1921 | .option = aacenc_options, | ||
| 1922 | .version = LIBAVUTIL_VERSION_INT, | ||
| 1923 | }; | ||
| 1924 | |||
| 1925 | static const FFCodecDefault aac_encode_defaults[] = { | ||
| 1926 | { "b", "0" }, | ||
| 1927 | { NULL } | ||
| 1928 | }; | ||
| 1929 | |||
| 1930 | const FFCodec ff_aac_encoder = { | ||
| 1931 | .p.name = "aac", | ||
| 1932 | CODEC_LONG_NAME("AAC (Advanced Audio Coding)"), | ||
| 1933 | .p.type = AVMEDIA_TYPE_AUDIO, | ||
| 1934 | .p.id = AV_CODEC_ID_AAC, | ||
| 1935 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | | ||
| 1936 | AV_CODEC_CAP_SMALL_LAST_FRAME, | ||
| 1937 | .priv_data_size = sizeof(AACEncContext), | ||
| 1938 | .init = aac_encode_init, | ||
| 1939 | FF_CODEC_ENCODE_CB(aac_encode_frame), | ||
| 1940 | .close = aac_encode_end, | ||
| 1941 | .defaults = aac_encode_defaults, | ||
| 1942 | CODEC_SAMPLERATES_ARRAY(ff_mpeg4audio_sample_rates), | ||
| 1943 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
| 1944 | CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_FLTP), | ||
| 1945 | .p.priv_class = &aacenc_class, | ||
| 1946 | }; | ||
| 1947 |