Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 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 | /** |
Diego Biurrun | ba87f08 | 2010-04-20 14:45:34 | [diff] [blame] | 23 | * @file |
Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 24 | * AAC encoder |
| 25 | */ |
| 26 | |
| 27 | /*********************************** |
| 28 | * TODOs: |
Kostya Shishkov | 817015e | 2008-08-16 05:47:18 | [diff] [blame] | 29 | * add sane pulse detection |
Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 30 | ***********************************/ |
Michael Niedermayer | a7a7f32 | 2021-05-28 19:37:26 | [diff] [blame] | 31 | #include <float.h> |
Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 32 | |
Andreas Rheinhardt | 1be3d8a | 2021-06-11 23:10:58 | [diff] [blame] | 33 | #include "libavutil/channel_layout.h" |
Ganesh Ajjanagadde | 2e4fd16 | 2016-01-14 22:55:56 | [diff] [blame] | 34 | #include "libavutil/libm.h" |
Justin Ruggles | d5a7229 | 2012-05-21 16:58:41 | [diff] [blame] | 35 | #include "libavutil/float_dsp.h" |
Nathan Caldwell | cc9947f | 2011-06-01 05:38:09 | [diff] [blame] | 36 | #include "libavutil/opt.h" |
Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 37 | #include "avcodec.h" |
Andreas Rheinhardt | a688f3c | 2022-03-16 17:18:28 | [diff] [blame] | 38 | #include "codec_internal.h" |
Andreas Rheinhardt | 56e9e02 | 2021-05-11 13:17:13 | [diff] [blame] | 39 | #include "encode.h" |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 40 | #include "put_bits.h" |
Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 41 | #include "mpeg4audio.h" |
Mans Rullgard | 4538729 | 2011-03-19 23:44:00 | [diff] [blame] | 42 | #include "sinewin.h" |
Marton Balint | aee036c | 2020-05-10 18:38:11 | [diff] [blame] | 43 | #include "profiles.h" |
Martin Storsjö | f2da2e1 | 2022-02-23 10:55:45 | [diff] [blame] | 44 | #include "version.h" |
Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 45 | |
Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 46 | #include "aac.h" |
| 47 | #include "aactab.h" |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 48 | #include "aacenc.h" |
Rostislav Pehlivanov | c47c781 | 2015-07-29 04:44:26 | [diff] [blame] | 49 | #include "aacenctab.h" |
Rostislav Pehlivanov | ef8e5a6 | 2015-07-29 04:44:27 | [diff] [blame] | 50 | #include "aacenc_utils.h" |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 51 | |
| 52 | #include "psymodel.h" |
Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 53 | |
Andreas Rheinhardt | c488ee6 | 2022-08-01 03:42:06 | [diff] [blame] | 54 | /** |
| 55 | * List of PCE (Program Configuration Element) for the channel layouts listed |
| 56 | * in channel_layout.h |
| 57 | * |
| 58 | * For those wishing in the future to add other layouts: |
| 59 | * |
| 60 | * - num_ele: number of elements in each group of front, side, back, lfe channels |
| 61 | * (an element is of type SCE (single channel), CPE (channel pair) for |
| 62 | * the first 3 groups; and is LFE for LFE group). |
| 63 | * |
| 64 | * - pairing: 0 for an SCE element or 1 for a CPE; does not apply to LFE group |
| 65 | * |
| 66 | * - index: there are three independent indices for SCE, CPE and LFE; |
| 67 | * they are incremented irrespective of the group to which the element belongs; |
| 68 | * they are not reset when going from one group to another |
| 69 | * |
| 70 | * Example: for 7.0 channel layout, |
| 71 | * .pairing = { { 1, 0 }, { 1 }, { 1 }, }, (3 CPE and 1 SCE in front group) |
| 72 | * .index = { { 0, 0 }, { 1 }, { 2 }, }, |
| 73 | * (index is 0 for the single SCE but goes from 0 to 2 for the CPEs) |
| 74 | * |
| 75 | * The index order impacts the channel ordering. But is otherwise arbitrary |
| 76 | * (the sequence could have been 2, 0, 1 instead of 0, 1, 2). |
| 77 | * |
| 78 | * Spec allows for discontinuous indices, e.g. if one has a total of two SCE, |
| 79 | * SCE.0 SCE.15 is OK per spec; BUT it won't be decoded by our AAC decoder |
| 80 | * which at this time requires that indices fully cover some range starting |
| 81 | * from 0 (SCE.1 SCE.0 is OK but not SCE.0 SCE.15). |
| 82 | * |
| 83 | * - config_map: total number of elements and their types. Beware, the way the |
| 84 | * types are ordered impacts the final channel ordering. |
| 85 | * |
| 86 | * - reorder_map: reorders the channels. |
| 87 | * |
| 88 | */ |
| 89 | static const AACPCEInfo aac_pce_configs[] = { |
| 90 | { |
| 91 | .layout = AV_CHANNEL_LAYOUT_MONO, |
| 92 | .num_ele = { 1, 0, 0, 0 }, |
| 93 | .pairing = { { 0 }, }, |
| 94 | .index = { { 0 }, }, |
| 95 | .config_map = { 1, TYPE_SCE, }, |
| 96 | .reorder_map = { 0 }, |
| 97 | }, |
| 98 | { |
| 99 | .layout = AV_CHANNEL_LAYOUT_STEREO, |
| 100 | .num_ele = { 1, 0, 0, 0 }, |
| 101 | .pairing = { { 1 }, }, |
| 102 | .index = { { 0 }, }, |
| 103 | .config_map = { 1, TYPE_CPE, }, |
| 104 | .reorder_map = { 0, 1 }, |
| 105 | }, |
| 106 | { |
| 107 | .layout = AV_CHANNEL_LAYOUT_2POINT1, |
| 108 | .num_ele = { 1, 0, 0, 1 }, |
| 109 | .pairing = { { 1 }, }, |
| 110 | .index = { { 0 },{ 0 },{ 0 },{ 0 } }, |
| 111 | .config_map = { 2, TYPE_CPE, TYPE_LFE }, |
| 112 | .reorder_map = { 0, 1, 2 }, |
| 113 | }, |
| 114 | { |
| 115 | .layout = AV_CHANNEL_LAYOUT_2_1, |
| 116 | .num_ele = { 1, 0, 1, 0 }, |
| 117 | .pairing = { { 1 },{ 0 },{ 0 } }, |
| 118 | .index = { { 0 },{ 0 },{ 0 }, }, |
| 119 | .config_map = { 2, TYPE_CPE, TYPE_SCE }, |
| 120 | .reorder_map = { 0, 1, 2 }, |
| 121 | }, |
| 122 | { |
| 123 | .layout = AV_CHANNEL_LAYOUT_SURROUND, |
| 124 | .num_ele = { 2, 0, 0, 0 }, |
| 125 | .pairing = { { 1, 0 }, }, |
| 126 | .index = { { 0, 0 }, }, |
| 127 | .config_map = { 2, TYPE_CPE, TYPE_SCE, }, |
| 128 | .reorder_map = { 0, 1, 2 }, |
| 129 | }, |
| 130 | { |
| 131 | .layout = AV_CHANNEL_LAYOUT_3POINT1, |
| 132 | .num_ele = { 2, 0, 0, 1 }, |
| 133 | .pairing = { { 1, 0 }, }, |
| 134 | .index = { { 0, 0 }, { 0 }, { 0 }, { 0 }, }, |
| 135 | .config_map = { 3, TYPE_CPE, TYPE_SCE, TYPE_LFE }, |
| 136 | .reorder_map = { 0, 1, 2, 3 }, |
| 137 | }, |
| 138 | { |
| 139 | .layout = AV_CHANNEL_LAYOUT_4POINT0, |
| 140 | .num_ele = { 2, 0, 1, 0 }, |
| 141 | .pairing = { { 1, 0 }, { 0 }, { 0 }, }, |
| 142 | .index = { { 0, 0 }, { 0 }, { 1 } }, |
| 143 | .config_map = { 3, TYPE_CPE, TYPE_SCE, TYPE_SCE }, |
| 144 | .reorder_map = { 0, 1, 2, 3 }, |
| 145 | }, |
| 146 | { |
| 147 | .layout = AV_CHANNEL_LAYOUT_4POINT1, |
| 148 | .num_ele = { 2, 1, 1, 0 }, |
| 149 | .pairing = { { 1, 0 }, { 0 }, { 0 }, }, |
| 150 | .index = { { 0, 0 }, { 1 }, { 2 }, { 0 } }, |
| 151 | .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_SCE }, |
| 152 | .reorder_map = { 0, 1, 2, 3, 4 }, |
| 153 | }, |
| 154 | { |
| 155 | .layout = AV_CHANNEL_LAYOUT_2_2, |
| 156 | .num_ele = { 1, 1, 0, 0 }, |
| 157 | .pairing = { { 1 }, { 1 }, }, |
| 158 | .index = { { 0 }, { 1 }, }, |
| 159 | .config_map = { 2, TYPE_CPE, TYPE_CPE }, |
| 160 | .reorder_map = { 0, 1, 2, 3 }, |
| 161 | }, |
| 162 | { |
| 163 | .layout = AV_CHANNEL_LAYOUT_QUAD, |
| 164 | .num_ele = { 1, 0, 1, 0 }, |
| 165 | .pairing = { { 1 }, { 0 }, { 1 }, }, |
| 166 | .index = { { 0 }, { 0 }, { 1 } }, |
| 167 | .config_map = { 2, TYPE_CPE, TYPE_CPE }, |
| 168 | .reorder_map = { 0, 1, 2, 3 }, |
| 169 | }, |
| 170 | { |
| 171 | .layout = AV_CHANNEL_LAYOUT_5POINT0, |
| 172 | .num_ele = { 2, 1, 0, 0 }, |
| 173 | .pairing = { { 1, 0 }, { 1 }, }, |
| 174 | .index = { { 0, 0 }, { 1 } }, |
| 175 | .config_map = { 3, TYPE_CPE, TYPE_SCE, TYPE_CPE }, |
| 176 | .reorder_map = { 0, 1, 2, 3, 4 }, |
| 177 | }, |
| 178 | { |
| 179 | .layout = AV_CHANNEL_LAYOUT_5POINT1, |
| 180 | .num_ele = { 2, 1, 1, 0 }, |
| 181 | .pairing = { { 1, 0 }, { 0 }, { 1 }, }, |
| 182 | .index = { { 0, 0 }, { 1 }, { 1 } }, |
| 183 | .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE }, |
| 184 | .reorder_map = { 0, 1, 2, 3, 4, 5 }, |
| 185 | }, |
| 186 | { |
| 187 | .layout = AV_CHANNEL_LAYOUT_5POINT0_BACK, |
| 188 | .num_ele = { 2, 0, 1, 0 }, |
| 189 | .pairing = { { 1, 0 }, { 0 }, { 1 } }, |
| 190 | .index = { { 0, 0 }, { 0 }, { 1 } }, |
| 191 | .config_map = { 3, TYPE_CPE, TYPE_SCE, TYPE_CPE }, |
| 192 | .reorder_map = { 0, 1, 2, 3, 4 }, |
| 193 | }, |
| 194 | { |
| 195 | .layout = AV_CHANNEL_LAYOUT_5POINT1_BACK, |
| 196 | .num_ele = { 2, 1, 1, 0 }, |
| 197 | .pairing = { { 1, 0 }, { 0 }, { 1 }, }, |
| 198 | .index = { { 0, 0 }, { 1 }, { 1 } }, |
| 199 | .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE }, |
| 200 | .reorder_map = { 0, 1, 2, 3, 4, 5 }, |
| 201 | }, |
| 202 | { |
| 203 | .layout = AV_CHANNEL_LAYOUT_6POINT0, |
| 204 | .num_ele = { 2, 1, 1, 0 }, |
| 205 | .pairing = { { 1, 0 }, { 1 }, { 0 }, }, |
| 206 | .index = { { 0, 0 }, { 1 }, { 1 } }, |
| 207 | .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE }, |
| 208 | .reorder_map = { 0, 1, 2, 3, 4, 5 }, |
| 209 | }, |
| 210 | { |
| 211 | .layout = AV_CHANNEL_LAYOUT_6POINT0_FRONT, |
| 212 | .num_ele = { 2, 1, 0, 0 }, |
| 213 | .pairing = { { 1, 1 }, { 1 } }, |
| 214 | .index = { { 1, 0 }, { 2 }, }, |
| 215 | .config_map = { 3, TYPE_CPE, TYPE_CPE, TYPE_CPE, }, |
| 216 | .reorder_map = { 0, 1, 2, 3, 4, 5 }, |
| 217 | }, |
| 218 | { |
| 219 | .layout = AV_CHANNEL_LAYOUT_HEXAGONAL, |
| 220 | .num_ele = { 2, 0, 2, 0 }, |
| 221 | .pairing = { { 1, 0 },{ 0 },{ 1, 0 }, }, |
| 222 | .index = { { 0, 0 },{ 0 },{ 1, 1 } }, |
| 223 | .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE, }, |
| 224 | .reorder_map = { 0, 1, 2, 3, 4, 5 }, |
| 225 | }, |
| 226 | { |
| 227 | .layout = AV_CHANNEL_LAYOUT_6POINT1, |
| 228 | .num_ele = { 2, 1, 2, 0 }, |
| 229 | .pairing = { { 1, 0 },{ 0 },{ 1, 0 }, }, |
| 230 | .index = { { 0, 0 },{ 1 },{ 1, 2 } }, |
| 231 | .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_SCE }, |
| 232 | .reorder_map = { 0, 1, 2, 3, 4, 5, 6 }, |
| 233 | }, |
| 234 | { |
| 235 | .layout = AV_CHANNEL_LAYOUT_6POINT1_BACK, |
| 236 | .num_ele = { 2, 1, 2, 0 }, |
| 237 | .pairing = { { 1, 0 }, { 0 }, { 1, 0 }, }, |
| 238 | .index = { { 0, 0 }, { 1 }, { 1, 2 } }, |
| 239 | .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_SCE }, |
| 240 | .reorder_map = { 0, 1, 2, 3, 4, 5, 6 }, |
| 241 | }, |
| 242 | { |
| 243 | .layout = AV_CHANNEL_LAYOUT_6POINT1_FRONT, |
| 244 | .num_ele = { 2, 1, 2, 0 }, |
| 245 | .pairing = { { 1, 0 }, { 0 }, { 1, 0 }, }, |
| 246 | .index = { { 0, 0 }, { 1 }, { 1, 2 } }, |
| 247 | .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_SCE }, |
| 248 | .reorder_map = { 0, 1, 2, 3, 4, 5, 6 }, |
| 249 | }, |
| 250 | { |
| 251 | .layout = AV_CHANNEL_LAYOUT_7POINT0, |
| 252 | .num_ele = { 2, 1, 1, 0 }, |
| 253 | .pairing = { { 1, 0 }, { 1 }, { 1 }, }, |
| 254 | .index = { { 0, 0 }, { 1 }, { 2 }, }, |
| 255 | .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_CPE }, |
| 256 | .reorder_map = { 0, 1, 2, 3, 4, 5, 6 }, |
| 257 | }, |
| 258 | { |
| 259 | .layout = AV_CHANNEL_LAYOUT_7POINT0_FRONT, |
| 260 | .num_ele = { 2, 1, 1, 0 }, |
| 261 | .pairing = { { 1, 0 }, { 1 }, { 1 }, }, |
| 262 | .index = { { 0, 0 }, { 1 }, { 2 }, }, |
| 263 | .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_CPE }, |
| 264 | .reorder_map = { 0, 1, 2, 3, 4, 5, 6 }, |
| 265 | }, |
| 266 | { |
| 267 | .layout = AV_CHANNEL_LAYOUT_7POINT1, |
| 268 | .num_ele = { 2, 1, 2, 0 }, |
| 269 | .pairing = { { 1, 0 }, { 0 }, { 1, 1 }, }, |
| 270 | .index = { { 0, 0 }, { 1 }, { 1, 2 }, { 0 } }, |
| 271 | .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_CPE }, |
| 272 | .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 }, |
| 273 | }, |
| 274 | { |
| 275 | .layout = AV_CHANNEL_LAYOUT_7POINT1_WIDE, |
| 276 | .num_ele = { 2, 1, 2, 0 }, |
| 277 | .pairing = { { 1, 0 }, { 0 },{ 1, 1 }, }, |
| 278 | .index = { { 0, 0 }, { 1 }, { 1, 2 }, { 0 } }, |
| 279 | .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_CPE }, |
| 280 | .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 }, |
| 281 | }, |
| 282 | { |
| 283 | .layout = AV_CHANNEL_LAYOUT_7POINT1_WIDE_BACK, |
| 284 | .num_ele = { 2, 1, 2, 0 }, |
| 285 | .pairing = { { 1, 0 }, { 0 }, { 1, 1 }, }, |
| 286 | .index = { { 0, 0 }, { 1 }, { 1, 2 }, { 0 } }, |
| 287 | .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_CPE }, |
| 288 | .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 }, |
| 289 | }, |
| 290 | { |
| 291 | .layout = AV_CHANNEL_LAYOUT_OCTAGONAL, |
| 292 | .num_ele = { 2, 1, 2, 0 }, |
| 293 | .pairing = { { 1, 0 }, { 1 }, { 1, 0 }, }, |
| 294 | .index = { { 0, 0 }, { 1 }, { 2, 1 } }, |
| 295 | .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_SCE }, |
| 296 | .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 }, |
| 297 | }, |
| 298 | { /* Meant for order 2/mixed ambisonics */ |
| 299 | .layout = { .order = AV_CHANNEL_ORDER_NATIVE, .nb_channels = 9, |
| 300 | .u.mask = AV_CH_LAYOUT_OCTAGONAL | AV_CH_TOP_CENTER }, |
| 301 | .num_ele = { 2, 2, 2, 0 }, |
| 302 | .pairing = { { 1, 0 }, { 1, 0 }, { 1, 0 }, }, |
| 303 | .index = { { 0, 0 }, { 1, 1 }, { 2, 2 } }, |
| 304 | .config_map = { 6, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE }, |
| 305 | .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, |
| 306 | }, |
| 307 | { /* Meant for order 2/mixed ambisonics */ |
| 308 | .layout = { .order = AV_CHANNEL_ORDER_NATIVE, .nb_channels = 10, |
| 309 | .u.mask = AV_CH_LAYOUT_6POINT0_FRONT | AV_CH_BACK_CENTER | |
| 310 | AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT | AV_CH_TOP_CENTER }, |
| 311 | .num_ele = { 2, 2, 2, 0 }, |
| 312 | .pairing = { { 1, 1 }, { 1, 0 }, { 1, 0 }, }, |
| 313 | .index = { { 0, 1 }, { 2, 0 }, { 3, 1 } }, |
| 314 | .config_map = { 6, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE }, |
| 315 | .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, |
| 316 | }, |
| 317 | { |
| 318 | .layout = AV_CHANNEL_LAYOUT_HEXADECAGONAL, |
| 319 | .num_ele = { 4, 2, 4, 0 }, |
| 320 | .pairing = { { 1, 0, 1, 0 }, { 1, 1 }, { 1, 0, 1, 0 }, }, |
| 321 | .index = { { 0, 0, 1, 1 }, { 2, 3 }, { 4, 2, 5, 3 } }, |
| 322 | .config_map = { 10, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE }, |
| 323 | .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, |
| 324 | }, |
| 325 | }; |
| 326 | |
Rostislav Pehlivanov | fbf295e | 2016-10-03 18:53:11 | [diff] [blame] | 327 | static void put_pce(PutBitContext *pb, AVCodecContext *avctx) |
| 328 | { |
| 329 | int i, j; |
| 330 | AACEncContext *s = avctx->priv_data; |
| 331 | AACPCEInfo *pce = &s->pce; |
Rostislav Pehlivanov | 7b7775a | 2017-11-09 04:30:07 | [diff] [blame] | 332 | const int bitexact = avctx->flags & AV_CODEC_FLAG_BITEXACT; |
| 333 | const char *aux_data = bitexact ? "Lavc" : LIBAVCODEC_IDENT; |
Rostislav Pehlivanov | fbf295e | 2016-10-03 18:53:11 | [diff] [blame] | 334 | |
| 335 | put_bits(pb, 4, 0); |
| 336 | |
| 337 | put_bits(pb, 2, avctx->profile); |
| 338 | put_bits(pb, 4, s->samplerate_index); |
| 339 | |
| 340 | put_bits(pb, 4, pce->num_ele[0]); /* Front */ |
| 341 | put_bits(pb, 4, pce->num_ele[1]); /* Side */ |
| 342 | put_bits(pb, 4, pce->num_ele[2]); /* Back */ |
| 343 | put_bits(pb, 2, pce->num_ele[3]); /* LFE */ |
| 344 | put_bits(pb, 3, 0); /* Assoc data */ |
| 345 | put_bits(pb, 4, 0); /* CCs */ |
| 346 | |
| 347 | put_bits(pb, 1, 0); /* Stereo mixdown */ |
| 348 | put_bits(pb, 1, 0); /* Mono mixdown */ |
| 349 | put_bits(pb, 1, 0); /* Something else */ |
| 350 | |
| 351 | for (i = 0; i < 4; i++) { |
| 352 | for (j = 0; j < pce->num_ele[i]; j++) { |
| 353 | if (i < 3) |
| 354 | put_bits(pb, 1, pce->pairing[i][j]); |
| 355 | put_bits(pb, 4, pce->index[i][j]); |
| 356 | } |
| 357 | } |
| 358 | |
Anton Khirnov | 717503f | 2020-10-26 12:36:06 | [diff] [blame] | 359 | align_put_bits(pb); |
Rostislav Pehlivanov | 7b7775a | 2017-11-09 04:30:07 | [diff] [blame] | 360 | put_bits(pb, 8, strlen(aux_data)); |
Anton Khirnov | 944ba30 | 2020-10-26 12:41:39 | [diff] [blame] | 361 | ff_put_string(pb, aux_data, 0); |
Rostislav Pehlivanov | fbf295e | 2016-10-03 18:53:11 | [diff] [blame] | 362 | } |
| 363 | |
Nathan Caldwell | 9b8e2a8 | 2011-12-15 02:43:56 | [diff] [blame] | 364 | /** |
Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 365 | * Make AAC audio config object. |
| 366 | * @see 1.6.2.1 "Syntax - AudioSpecificConfig" |
| 367 | */ |
Rostislav Pehlivanov | 7b7775a | 2017-11-09 04:30:07 | [diff] [blame] | 368 | static int put_audio_specific_config(AVCodecContext *avctx) |
Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 369 | { |
| 370 | PutBitContext pb; |
| 371 | AACEncContext *s = avctx->priv_data; |
Rostislav Pehlivanov | fbf295e | 2016-10-03 18:53:11 | [diff] [blame] | 372 | int channels = (!s->needs_pce)*(s->channels - (s->channels == 8 ? 1 : 0)); |
Rostislav Pehlivanov | 7b7775a | 2017-11-09 04:30:07 | [diff] [blame] | 373 | const int max_size = 32; |
Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 374 | |
Rostislav Pehlivanov | 7b7775a | 2017-11-09 04:30:07 | [diff] [blame] | 375 | avctx->extradata = av_mallocz(max_size); |
| 376 | if (!avctx->extradata) |
| 377 | return AVERROR(ENOMEM); |
| 378 | |
| 379 | init_put_bits(&pb, avctx->extradata, max_size); |
Rostislav Pehlivanov | eab12d0 | 2015-08-21 18:20:22 | [diff] [blame] | 380 | put_bits(&pb, 5, s->profile+1); //profile |
Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 381 | put_bits(&pb, 4, s->samplerate_index); //sample rate index |
Rostislav Pehlivanov | b3deaec | 2015-10-12 14:53:17 | [diff] [blame] | 382 | put_bits(&pb, 4, channels); |
Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 383 | //GASpecificConfig |
| 384 | put_bits(&pb, 1, 0); //frame length - 1024 samples |
| 385 | put_bits(&pb, 1, 0); //does not depend on core coder |
| 386 | put_bits(&pb, 1, 0); //is not extension |
Rostislav Pehlivanov | fbf295e | 2016-10-03 18:53:11 | [diff] [blame] | 387 | if (s->needs_pce) |
| 388 | put_pce(&pb, avctx); |
Alex Converse | d67a6aa | 2011-01-22 05:23:43 | [diff] [blame] | 389 | |
| 390 | //Explicitly Mark SBR absent |
Janne Grunau | 604eb152 | 2011-01-23 14:45:19 | [diff] [blame] | 391 | put_bits(&pb, 11, 0x2b7); //sync extension |
Alex Converse | d67a6aa | 2011-01-22 05:23:43 | [diff] [blame] | 392 | put_bits(&pb, 5, AOT_SBR); |
| 393 | put_bits(&pb, 1, 0); |
Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 394 | flush_put_bits(&pb); |
Andreas Rheinhardt | c81b8e0 | 2021-03-25 11:52:56 | [diff] [blame] | 395 | avctx->extradata_size = put_bytes_output(&pb); |
Rostislav Pehlivanov | 7b7775a | 2017-11-09 04:30:07 | [diff] [blame] | 396 | |
| 397 | return 0; |
Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 398 | } |
| 399 | |
Claudio Freire | b629c67 | 2015-10-12 06:56:22 | [diff] [blame] | 400 | void ff_quantize_band_cost_cache_init(struct AACEncContext *s) |
| 401 | { |
Reimar Döffinger | b91e376 | 2016-03-06 16:28:42 | [diff] [blame] | 402 | ++s->quantize_band_cost_cache_generation; |
| 403 | if (s->quantize_band_cost_cache_generation == 0) { |
| 404 | memset(s->quantize_band_cost_cache, 0, sizeof(s->quantize_band_cost_cache)); |
| 405 | s->quantize_band_cost_cache_generation = 1; |
Claudio Freire | b629c67 | 2015-10-12 06:56:22 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | |
Nathan Caldwell | 9292fe4 | 2011-12-24 00:36:15 | [diff] [blame] | 409 | #define WINDOW_FUNC(type) \ |
Ronald S. Bultje | 42d3246 | 2013-01-20 21:20:30 | [diff] [blame] | 410 | static void apply_ ##type ##_window(AVFloatDSPContext *fdsp, \ |
Justin Ruggles | d5a7229 | 2012-05-21 16:58:41 | [diff] [blame] | 411 | SingleChannelElement *sce, \ |
| 412 | const float *audio) |
Nathan Caldwell | 9292fe4 | 2011-12-24 00:36:15 | [diff] [blame] | 413 | |
| 414 | WINDOW_FUNC(only_long) |
Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 415 | { |
Nathan Caldwell | 9292fe4 | 2011-12-24 00:36:15 | [diff] [blame] | 416 | const float *lwindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024; |
| 417 | const float *pwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024; |
Michael Niedermayer | 59b68ee | 2012-11-26 14:15:02 | [diff] [blame] | 418 | float *out = sce->ret_buf; |
Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 419 | |
Ronald S. Bultje | 42d3246 | 2013-01-20 21:20:30 | [diff] [blame] | 420 | fdsp->vector_fmul (out, audio, lwindow, 1024); |
| 421 | fdsp->vector_fmul_reverse(out + 1024, audio + 1024, pwindow, 1024); |
Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 422 | } |
| 423 | |
Nathan Caldwell | 9292fe4 | 2011-12-24 00:36:15 | [diff] [blame] | 424 | WINDOW_FUNC(long_start) |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 425 | { |
Nathan Caldwell | 9292fe4 | 2011-12-24 00:36:15 | [diff] [blame] | 426 | const float *lwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024; |
| 427 | const float *swindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128; |
Michael Niedermayer | 59b68ee | 2012-11-26 14:15:02 | [diff] [blame] | 428 | float *out = sce->ret_buf; |
Nathan Caldwell | 9292fe4 | 2011-12-24 00:36:15 | [diff] [blame] | 429 | |
Justin Ruggles | d5a7229 | 2012-05-21 16:58:41 | [diff] [blame] | 430 | fdsp->vector_fmul(out, audio, lwindow, 1024); |
Nathan Caldwell | 2e626dd | 2012-01-28 05:23:41 | [diff] [blame] | 431 | memcpy(out + 1024, audio + 1024, sizeof(out[0]) * 448); |
Ronald S. Bultje | 42d3246 | 2013-01-20 21:20:30 | [diff] [blame] | 432 | fdsp->vector_fmul_reverse(out + 1024 + 448, audio + 1024 + 448, swindow, 128); |
Nathan Caldwell | 9292fe4 | 2011-12-24 00:36:15 | [diff] [blame] | 433 | memset(out + 1024 + 576, 0, sizeof(out[0]) * 448); |
| 434 | } |
| 435 | |
| 436 | WINDOW_FUNC(long_stop) |
| 437 | { |
| 438 | const float *lwindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024; |
| 439 | const float *swindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128; |
Michael Niedermayer | 59b68ee | 2012-11-26 14:15:02 | [diff] [blame] | 440 | float *out = sce->ret_buf; |
Nathan Caldwell | 9292fe4 | 2011-12-24 00:36:15 | [diff] [blame] | 441 | |
| 442 | memset(out, 0, sizeof(out[0]) * 448); |
Justin Ruggles | d5a7229 | 2012-05-21 16:58:41 | [diff] [blame] | 443 | fdsp->vector_fmul(out + 448, audio + 448, swindow, 128); |
Nathan Caldwell | 9292fe4 | 2011-12-24 00:36:15 | [diff] [blame] | 444 | memcpy(out + 576, audio + 576, sizeof(out[0]) * 448); |
Ronald S. Bultje | 42d3246 | 2013-01-20 21:20:30 | [diff] [blame] | 445 | fdsp->vector_fmul_reverse(out + 1024, audio + 1024, lwindow, 1024); |
Nathan Caldwell | 9292fe4 | 2011-12-24 00:36:15 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | WINDOW_FUNC(eight_short) |
| 449 | { |
| 450 | const float *swindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128; |
| 451 | const float *pwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128; |
| 452 | const float *in = audio + 448; |
Michael Niedermayer | 59b68ee | 2012-11-26 14:15:02 | [diff] [blame] | 453 | float *out = sce->ret_buf; |
Mans Rullgard | 3715d841 | 2012-01-29 20:55:10 | [diff] [blame] | 454 | int w; |
Nathan Caldwell | 9292fe4 | 2011-12-24 00:36:15 | [diff] [blame] | 455 | |
Mans Rullgard | 3715d841 | 2012-01-29 20:55:10 | [diff] [blame] | 456 | for (w = 0; w < 8; w++) { |
Ronald S. Bultje | 42d3246 | 2013-01-20 21:20:30 | [diff] [blame] | 457 | fdsp->vector_fmul (out, in, w ? pwindow : swindow, 128); |
Nathan Caldwell | 9292fe4 | 2011-12-24 00:36:15 | [diff] [blame] | 458 | out += 128; |
| 459 | in += 128; |
Ronald S. Bultje | 42d3246 | 2013-01-20 21:20:30 | [diff] [blame] | 460 | fdsp->vector_fmul_reverse(out, in, swindow, 128); |
Nathan Caldwell | 9292fe4 | 2011-12-24 00:36:15 | [diff] [blame] | 461 | out += 128; |
| 462 | } |
| 463 | } |
| 464 | |
Ronald S. Bultje | 42d3246 | 2013-01-20 21:20:30 | [diff] [blame] | 465 | static void (*const apply_window[4])(AVFloatDSPContext *fdsp, |
Justin Ruggles | d5a7229 | 2012-05-21 16:58:41 | [diff] [blame] | 466 | SingleChannelElement *sce, |
| 467 | const float *audio) = { |
Nathan Caldwell | 9292fe4 | 2011-12-24 00:36:15 | [diff] [blame] | 468 | [ONLY_LONG_SEQUENCE] = apply_only_long_window, |
| 469 | [LONG_START_SEQUENCE] = apply_long_start_window, |
| 470 | [EIGHT_SHORT_SEQUENCE] = apply_eight_short_window, |
| 471 | [LONG_STOP_SEQUENCE] = apply_long_stop_window |
| 472 | }; |
| 473 | |
Nathan Caldwell | 04af2ef | 2011-12-07 22:20:10 | [diff] [blame] | 474 | static void apply_window_and_mdct(AACEncContext *s, SingleChannelElement *sce, |
| 475 | float *audio) |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 476 | { |
Nathan Caldwell | 9292fe4 | 2011-12-24 00:36:15 | [diff] [blame] | 477 | int i; |
Lynne | 8f3e062 | 2022-10-29 12:01:57 | [diff] [blame] | 478 | float *output = sce->ret_buf; |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 479 | |
Michael Niedermayer | 14285c3 | 2014-11-29 17:58:13 | [diff] [blame] | 480 | apply_window[sce->ics.window_sequence[0]](s->fdsp, sce, audio); |
Nathan Caldwell | 9292fe4 | 2011-12-24 00:36:15 | [diff] [blame] | 481 | |
| 482 | if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) |
Lynne | 8f3e062 | 2022-10-29 12:01:57 | [diff] [blame] | 483 | s->mdct1024_fn(s->mdct1024, sce->coeffs, output, sizeof(float)); |
Nathan Caldwell | 9292fe4 | 2011-12-24 00:36:15 | [diff] [blame] | 484 | else |
| 485 | for (i = 0; i < 1024; i += 128) |
Lynne | 8f3e062 | 2022-10-29 12:01:57 | [diff] [blame] | 486 | s->mdct128_fn(s->mdct128, &sce->coeffs[i], output + i*2, sizeof(float)); |
Nathan Caldwell | 9292fe4 | 2011-12-24 00:36:15 | [diff] [blame] | 487 | memcpy(audio, audio + 1024, sizeof(audio[0]) * 1024); |
Claudio Freire | 6394aca | 2015-03-03 06:43:06 | [diff] [blame] | 488 | memcpy(sce->pcoeffs, sce->coeffs, sizeof(sce->pcoeffs)); |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 489 | } |
| 490 | |
Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 491 | /** |
| 492 | * Encode ics_info element. |
| 493 | * @see Table 4.6 (syntax of ics_info) |
| 494 | */ |
Kostya Shishkov | e43b0a7 | 2008-08-24 05:56:23 | [diff] [blame] | 495 | static void put_ics_info(AACEncContext *s, IndividualChannelStream *info) |
Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 496 | { |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 497 | int w; |
Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 498 | |
| 499 | put_bits(&s->pb, 1, 0); // ics_reserved bit |
| 500 | put_bits(&s->pb, 2, info->window_sequence[0]); |
| 501 | put_bits(&s->pb, 1, info->use_kb_window[0]); |
Alex Converse | fd257dc | 2009-07-08 20:36:45 | [diff] [blame] | 502 | if (info->window_sequence[0] != EIGHT_SHORT_SEQUENCE) { |
Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 503 | put_bits(&s->pb, 6, info->max_sfb); |
Rostislav Pehlivanov | 76b81b1 | 2015-08-21 18:38:05 | [diff] [blame] | 504 | put_bits(&s->pb, 1, !!info->predictor_present); |
Alex Converse | fd257dc | 2009-07-08 20:36:45 | [diff] [blame] | 505 | } else { |
Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 506 | put_bits(&s->pb, 4, info->max_sfb); |
Diego Biurrun | c8f47d8 | 2009-07-08 21:36:33 | [diff] [blame] | 507 | for (w = 1; w < 8; w++) |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 508 | put_bits(&s->pb, 1, !info->group_len[w]); |
Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | |
| 512 | /** |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 513 | * Encode MS data. |
| 514 | * @see 4.6.8.1 "Joint Coding - M/S Stereo" |
Kostya Shishkov | e43b0a7 | 2008-08-24 05:56:23 | [diff] [blame] | 515 | */ |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 516 | static void encode_ms_info(PutBitContext *pb, ChannelElement *cpe) |
Kostya Shishkov | e43b0a7 | 2008-08-24 05:56:23 | [diff] [blame] | 517 | { |
Kostya Shishkov | e43b0a7 | 2008-08-24 05:56:23 | [diff] [blame] | 518 | int i, w; |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 519 | |
| 520 | put_bits(pb, 2, cpe->ms_mode); |
Diego Biurrun | c8f47d8 | 2009-07-08 21:36:33 | [diff] [blame] | 521 | if (cpe->ms_mode == 1) |
| 522 | for (w = 0; w < cpe->ch[0].ics.num_windows; w += cpe->ch[0].ics.group_len[w]) |
Alex Converse | fd257dc | 2009-07-08 20:36:45 | [diff] [blame] | 523 | for (i = 0; i < cpe->ch[0].ics.max_sfb; i++) |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 524 | put_bits(pb, 1, cpe->ms_mask[w*16 + i]); |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | /** |
| 528 | * Produce integer coefficients from scalefactors provided by the model. |
| 529 | */ |
Diego Biurrun | 72c758f | 2012-10-24 17:16:08 | [diff] [blame] | 530 | static void adjust_frame_information(ChannelElement *cpe, int chans) |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 531 | { |
| 532 | int i, w, w2, g, ch; |
Rostislav Pehlivanov | 0b23390 | 2015-07-02 18:13:06 | [diff] [blame] | 533 | int maxsfb, cmaxsfb; |
Rostislav Pehlivanov | 0b23390 | 2015-07-02 18:13:06 | [diff] [blame] | 534 | |
| 535 | for (ch = 0; ch < chans; ch++) { |
| 536 | IndividualChannelStream *ics = &cpe->ch[ch].ics; |
| 537 | maxsfb = 0; |
| 538 | cpe->ch[ch].pulse.num_pulse = 0; |
| 539 | for (w = 0; w < ics->num_windows; w += ics->group_len[w]) { |
| 540 | for (w2 = 0; w2 < ics->group_len[w]; w2++) { |
Claudio Freire | 6394aca | 2015-03-03 06:43:06 | [diff] [blame] | 541 | for (cmaxsfb = ics->num_swb; cmaxsfb > 0 && cpe->ch[ch].zeroes[w*16+cmaxsfb-1]; cmaxsfb--) |
| 542 | ; |
| 543 | maxsfb = FFMAX(maxsfb, cmaxsfb); |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 544 | } |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 545 | } |
| 546 | ics->max_sfb = maxsfb; |
| 547 | |
| 548 | //adjust zero bands for window groups |
Alex Converse | fd257dc | 2009-07-08 20:36:45 | [diff] [blame] | 549 | for (w = 0; w < ics->num_windows; w += ics->group_len[w]) { |
| 550 | for (g = 0; g < ics->max_sfb; g++) { |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 551 | i = 1; |
Alex Converse | fd257dc | 2009-07-08 20:36:45 | [diff] [blame] | 552 | for (w2 = w; w2 < w + ics->group_len[w]; w2++) { |
| 553 | if (!cpe->ch[ch].zeroes[w2*16 + g]) { |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 554 | i = 0; |
| 555 | break; |
| 556 | } |
| 557 | } |
| 558 | cpe->ch[ch].zeroes[w*16 + g] = i; |
| 559 | } |
| 560 | } |
| 561 | } |
| 562 | |
Alex Converse | fd257dc | 2009-07-08 20:36:45 | [diff] [blame] | 563 | if (chans > 1 && cpe->common_window) { |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 564 | IndividualChannelStream *ics0 = &cpe->ch[0].ics; |
| 565 | IndividualChannelStream *ics1 = &cpe->ch[1].ics; |
| 566 | int msc = 0; |
| 567 | ics0->max_sfb = FFMAX(ics0->max_sfb, ics1->max_sfb); |
| 568 | ics1->max_sfb = ics0->max_sfb; |
Alex Converse | fd257dc | 2009-07-08 20:36:45 | [diff] [blame] | 569 | for (w = 0; w < ics0->num_windows*16; w += 16) |
| 570 | for (i = 0; i < ics0->max_sfb; i++) |
Diego Biurrun | c8f47d8 | 2009-07-08 21:36:33 | [diff] [blame] | 571 | if (cpe->ms_mask[w+i]) |
| 572 | msc++; |
Diego Biurrun | 99d61d3 | 2009-07-08 21:16:06 | [diff] [blame] | 573 | if (msc == 0 || ics0->max_sfb == 0) |
| 574 | cpe->ms_mode = 0; |
| 575 | else |
Nathan Caldwell | 98add74 | 2011-06-29 03:11:39 | [diff] [blame] | 576 | cpe->ms_mode = msc < ics0->max_sfb * ics0->num_windows ? 1 : 2; |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | |
Rostislav Pehlivanov | 20dc527 | 2015-09-02 05:26:45 | [diff] [blame] | 580 | static void apply_intensity_stereo(ChannelElement *cpe) |
| 581 | { |
| 582 | int w, w2, g, i; |
| 583 | IndividualChannelStream *ics = &cpe->ch[0].ics; |
| 584 | if (!cpe->common_window) |
| 585 | return; |
| 586 | for (w = 0; w < ics->num_windows; w += ics->group_len[w]) { |
| 587 | for (w2 = 0; w2 < ics->group_len[w]; w2++) { |
| 588 | int start = (w+w2) * 128; |
| 589 | for (g = 0; g < ics->num_swb; g++) { |
| 590 | int p = -1 + 2 * (cpe->ch[1].band_type[w*16+g] - 14); |
| 591 | float scale = cpe->ch[0].is_ener[w*16+g]; |
| 592 | if (!cpe->is_mask[w*16 + g]) { |
| 593 | start += ics->swb_sizes[g]; |
| 594 | continue; |
| 595 | } |
Claudio Freire | 01ecb71 | 2015-10-11 20:29:50 | [diff] [blame] | 596 | if (cpe->ms_mask[w*16 + g]) |
| 597 | p *= -1; |
Rostislav Pehlivanov | 20dc527 | 2015-09-02 05:26:45 | [diff] [blame] | 598 | for (i = 0; i < ics->swb_sizes[g]; i++) { |
| 599 | float sum = (cpe->ch[0].coeffs[start+i] + p*cpe->ch[1].coeffs[start+i])*scale; |
| 600 | cpe->ch[0].coeffs[start+i] = sum; |
| 601 | cpe->ch[1].coeffs[start+i] = 0.0f; |
| 602 | } |
| 603 | start += ics->swb_sizes[g]; |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | static void apply_mid_side_stereo(ChannelElement *cpe) |
| 610 | { |
| 611 | int w, w2, g, i; |
| 612 | IndividualChannelStream *ics = &cpe->ch[0].ics; |
| 613 | if (!cpe->common_window) |
| 614 | return; |
| 615 | for (w = 0; w < ics->num_windows; w += ics->group_len[w]) { |
| 616 | for (w2 = 0; w2 < ics->group_len[w]; w2++) { |
| 617 | int start = (w+w2) * 128; |
| 618 | for (g = 0; g < ics->num_swb; g++) { |
Claudio Freire | fc36d85 | 2015-11-26 06:27:06 | [diff] [blame] | 619 | /* ms_mask can be used for other purposes in PNS and I/S, |
| 620 | * so must not apply M/S if any band uses either, even if |
| 621 | * ms_mask is set. |
| 622 | */ |
| 623 | if (!cpe->ms_mask[w*16 + g] || cpe->is_mask[w*16 + g] |
Claudio Freire | 509f168 | 2016-01-08 08:04:37 | [diff] [blame] | 624 | || cpe->ch[0].band_type[w*16 + g] >= NOISE_BT |
| 625 | || cpe->ch[1].band_type[w*16 + g] >= NOISE_BT) { |
Rostislav Pehlivanov | 20dc527 | 2015-09-02 05:26:45 | [diff] [blame] | 626 | start += ics->swb_sizes[g]; |
| 627 | continue; |
| 628 | } |
| 629 | for (i = 0; i < ics->swb_sizes[g]; i++) { |
| 630 | float L = (cpe->ch[0].coeffs[start+i] + cpe->ch[1].coeffs[start+i]) * 0.5f; |
| 631 | float R = L - cpe->ch[1].coeffs[start+i]; |
| 632 | cpe->ch[0].coeffs[start+i] = L; |
| 633 | cpe->ch[1].coeffs[start+i] = R; |
| 634 | } |
| 635 | start += ics->swb_sizes[g]; |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 | |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 641 | /** |
| 642 | * Encode scalefactor band coding type. |
| 643 | */ |
| 644 | static void encode_band_info(AACEncContext *s, SingleChannelElement *sce) |
| 645 | { |
| 646 | int w; |
| 647 | |
Rostislav Pehlivanov | 20dc527 | 2015-09-02 05:26:45 | [diff] [blame] | 648 | if (s->coder->set_special_band_scalefactors) |
| 649 | s->coder->set_special_band_scalefactors(s, sce); |
| 650 | |
Diego Biurrun | c8f47d8 | 2009-07-08 21:36:33 | [diff] [blame] | 651 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 652 | s->coder->encode_window_bands_info(s, sce, w, sce->ics.group_len[w], s->lambda); |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | /** |
| 656 | * Encode scalefactors. |
| 657 | */ |
Diego Biurrun | 99d61d3 | 2009-07-08 21:16:06 | [diff] [blame] | 658 | static void encode_scale_factors(AVCodecContext *avctx, AACEncContext *s, |
| 659 | SingleChannelElement *sce) |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 660 | { |
Rostislav Pehlivanov | 013498b | 2015-04-13 23:33:51 | [diff] [blame] | 661 | int diff, off_sf = sce->sf_idx[0], off_pns = sce->sf_idx[0] - NOISE_OFFSET; |
Rostislav Pehlivanov | 7c10b87 | 2015-06-26 20:16:34 | [diff] [blame] | 662 | int off_is = 0, noise_flag = 1; |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 663 | int i, w; |
| 664 | |
Alex Converse | fd257dc | 2009-07-08 20:36:45 | [diff] [blame] | 665 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
| 666 | for (i = 0; i < sce->ics.max_sfb; i++) { |
| 667 | if (!sce->zeroes[w*16 + i]) { |
Rostislav Pehlivanov | f7f71b5 | 2015-04-12 04:50:34 | [diff] [blame] | 668 | if (sce->band_type[w*16 + i] == NOISE_BT) { |
| 669 | diff = sce->sf_idx[w*16 + i] - off_pns; |
| 670 | off_pns = sce->sf_idx[w*16 + i]; |
| 671 | if (noise_flag-- > 0) { |
| 672 | put_bits(&s->pb, NOISE_PRE_BITS, diff + NOISE_PRE); |
| 673 | continue; |
| 674 | } |
Rostislav Pehlivanov | 7c10b87 | 2015-06-26 20:16:34 | [diff] [blame] | 675 | } else if (sce->band_type[w*16 + i] == INTENSITY_BT || |
| 676 | sce->band_type[w*16 + i] == INTENSITY_BT2) { |
| 677 | diff = sce->sf_idx[w*16 + i] - off_is; |
| 678 | off_is = sce->sf_idx[w*16 + i]; |
Rostislav Pehlivanov | f7f71b5 | 2015-04-12 04:50:34 | [diff] [blame] | 679 | } else { |
| 680 | diff = sce->sf_idx[w*16 + i] - off_sf; |
| 681 | off_sf = sce->sf_idx[w*16 + i]; |
| 682 | } |
| 683 | diff += SCALE_DIFF_ZERO; |
Michael Niedermayer | f69f9b3 | 2012-10-26 00:11:20 | [diff] [blame] | 684 | av_assert0(diff >= 0 && diff <= 120); |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 685 | put_bits(&s->pb, ff_aac_scalefactor_bits[diff], ff_aac_scalefactor_code[diff]); |
| 686 | } |
| 687 | } |
| 688 | } |
Kostya Shishkov | e43b0a7 | 2008-08-24 05:56:23 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | /** |
Kostya Shishkov | 817015e | 2008-08-16 05:47:18 | [diff] [blame] | 692 | * Encode pulse data. |
| 693 | */ |
Kostya Shishkov | cda00de | 2008-08-23 15:25:57 | [diff] [blame] | 694 | static void encode_pulses(AACEncContext *s, Pulse *pulse) |
Kostya Shishkov | 817015e | 2008-08-16 05:47:18 | [diff] [blame] | 695 | { |
| 696 | int i; |
| 697 | |
| 698 | put_bits(&s->pb, 1, !!pulse->num_pulse); |
Diego Biurrun | 99d61d3 | 2009-07-08 21:16:06 | [diff] [blame] | 699 | if (!pulse->num_pulse) |
| 700 | return; |
Kostya Shishkov | 817015e | 2008-08-16 05:47:18 | [diff] [blame] | 701 | |
| 702 | put_bits(&s->pb, 2, pulse->num_pulse - 1); |
| 703 | put_bits(&s->pb, 6, pulse->start); |
Alex Converse | fd257dc | 2009-07-08 20:36:45 | [diff] [blame] | 704 | for (i = 0; i < pulse->num_pulse; i++) { |
Kostya Shishkov | f5c3eae | 2008-08-16 11:59:36 | [diff] [blame] | 705 | put_bits(&s->pb, 5, pulse->pos[i]); |
Kostya Shishkov | 817015e | 2008-08-16 05:47:18 | [diff] [blame] | 706 | put_bits(&s->pb, 4, pulse->amp[i]); |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | /** |
| 711 | * Encode spectral coefficients processed by psychoacoustic model. |
| 712 | */ |
Kostya Shishkov | cda00de | 2008-08-23 15:25:57 | [diff] [blame] | 713 | static void encode_spectral_coeffs(AACEncContext *s, SingleChannelElement *sce) |
Kostya Shishkov | 817015e | 2008-08-16 05:47:18 | [diff] [blame] | 714 | { |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 715 | int start, i, w, w2; |
Kostya Shishkov | 817015e | 2008-08-16 05:47:18 | [diff] [blame] | 716 | |
Alex Converse | fd257dc | 2009-07-08 20:36:45 | [diff] [blame] | 717 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
Kostya Shishkov | 817015e | 2008-08-16 05:47:18 | [diff] [blame] | 718 | start = 0; |
Alex Converse | fd257dc | 2009-07-08 20:36:45 | [diff] [blame] | 719 | for (i = 0; i < sce->ics.max_sfb; i++) { |
| 720 | if (sce->zeroes[w*16 + i]) { |
Kostya Shishkov | cda00de | 2008-08-23 15:25:57 | [diff] [blame] | 721 | start += sce->ics.swb_sizes[i]; |
Kostya Shishkov | 817015e | 2008-08-16 05:47:18 | [diff] [blame] | 722 | continue; |
| 723 | } |
Rostislav Pehlivanov | 44ddee9 | 2015-08-29 05:34:08 | [diff] [blame] | 724 | for (w2 = w; w2 < w + sce->ics.group_len[w]; w2++) { |
Rostislav Pehlivanov | 43b378a | 2015-08-21 17:53:14 | [diff] [blame] | 725 | s->coder->quantize_and_encode_band(s, &s->pb, |
| 726 | &sce->coeffs[start + w2*128], |
Rostislav Pehlivanov | 44ddee9 | 2015-08-29 05:34:08 | [diff] [blame] | 727 | NULL, sce->ics.swb_sizes[i], |
Diego Biurrun | 99d61d3 | 2009-07-08 21:16:06 | [diff] [blame] | 728 | sce->sf_idx[w*16 + i], |
| 729 | sce->band_type[w*16 + i], |
Rostislav Pehlivanov | 43b378a | 2015-08-21 17:53:14 | [diff] [blame] | 730 | s->lambda, |
| 731 | sce->ics.window_clipping[w]); |
Rostislav Pehlivanov | 44ddee9 | 2015-08-29 05:34:08 | [diff] [blame] | 732 | } |
Kostya Shishkov | cda00de | 2008-08-23 15:25:57 | [diff] [blame] | 733 | start += sce->ics.swb_sizes[i]; |
Kostya Shishkov | 817015e | 2008-08-16 05:47:18 | [diff] [blame] | 734 | } |
Kostya Shishkov | 817015e | 2008-08-16 05:47:18 | [diff] [blame] | 735 | } |
| 736 | } |
| 737 | |
| 738 | /** |
Claudio Freire | 59216e0 | 2015-07-21 01:53:24 | [diff] [blame] | 739 | * Downscale spectral coefficients for near-clipping windows to avoid artifacts |
| 740 | */ |
| 741 | static void avoid_clipping(AACEncContext *s, SingleChannelElement *sce) |
| 742 | { |
| 743 | int start, i, j, w; |
| 744 | |
| 745 | if (sce->ics.clip_avoidance_factor < 1.0f) { |
| 746 | for (w = 0; w < sce->ics.num_windows; w++) { |
| 747 | start = 0; |
| 748 | for (i = 0; i < sce->ics.max_sfb; i++) { |
Rostislav Pehlivanov | 32be264 | 2015-08-21 17:30:51 | [diff] [blame] | 749 | float *swb_coeffs = &sce->coeffs[start + w*128]; |
Claudio Freire | 59216e0 | 2015-07-21 01:53:24 | [diff] [blame] | 750 | for (j = 0; j < sce->ics.swb_sizes[i]; j++) |
| 751 | swb_coeffs[j] *= sce->ics.clip_avoidance_factor; |
| 752 | start += sce->ics.swb_sizes[i]; |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | /** |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 759 | * Encode one channel of audio data. |
| 760 | */ |
Diego Biurrun | 99d61d3 | 2009-07-08 21:16:06 | [diff] [blame] | 761 | static int encode_individual_channel(AVCodecContext *avctx, AACEncContext *s, |
| 762 | SingleChannelElement *sce, |
| 763 | int common_window) |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 764 | { |
| 765 | put_bits(&s->pb, 8, sce->sf_idx[0]); |
Rostislav Pehlivanov | 76b81b1 | 2015-08-21 18:38:05 | [diff] [blame] | 766 | if (!common_window) { |
Diego Biurrun | 99d61d3 | 2009-07-08 21:16:06 | [diff] [blame] | 767 | put_ics_info(s, &sce->ics); |
Rostislav Pehlivanov | 76b81b1 | 2015-08-21 18:38:05 | [diff] [blame] | 768 | if (s->coder->encode_main_pred) |
| 769 | s->coder->encode_main_pred(s, sce); |
Rostislav Pehlivanov | 27d23ae | 2015-10-17 01:22:51 | [diff] [blame] | 770 | if (s->coder->encode_ltp_info) |
| 771 | s->coder->encode_ltp_info(s, sce, 0); |
Rostislav Pehlivanov | 76b81b1 | 2015-08-21 18:38:05 | [diff] [blame] | 772 | } |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 773 | encode_band_info(s, sce); |
| 774 | encode_scale_factors(avctx, s, sce); |
| 775 | encode_pulses(s, &sce->pulse); |
Rostislav Pehlivanov | f20b671 | 2015-08-29 05:47:31 | [diff] [blame] | 776 | put_bits(&s->pb, 1, !!sce->tns.present); |
Rostislav Pehlivanov | a1c487e | 2015-08-21 18:27:38 | [diff] [blame] | 777 | if (s->coder->encode_tns_info) |
| 778 | s->coder->encode_tns_info(s, sce); |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 779 | put_bits(&s->pb, 1, 0); //ssr |
| 780 | encode_spectral_coeffs(s, sce); |
| 781 | return 0; |
| 782 | } |
| 783 | |
| 784 | /** |
Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 785 | * Write some auxiliary information about the created AAC file. |
| 786 | */ |
Diego Biurrun | 72c758f | 2012-10-24 17:16:08 | [diff] [blame] | 787 | static void put_bitstream_info(AACEncContext *s, const char *name) |
Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 788 | { |
| 789 | int i, namelen, padbits; |
| 790 | |
| 791 | namelen = strlen(name) + 2; |
Kostya Shishkov | f5c3eae | 2008-08-16 11:59:36 | [diff] [blame] | 792 | put_bits(&s->pb, 3, TYPE_FIL); |
Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 793 | put_bits(&s->pb, 4, FFMIN(namelen, 15)); |
Alex Converse | fd257dc | 2009-07-08 20:36:45 | [diff] [blame] | 794 | if (namelen >= 15) |
Michael Niedermayer | 018a664 | 2012-01-22 00:40:10 | [diff] [blame] | 795 | put_bits(&s->pb, 8, namelen - 14); |
Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 796 | put_bits(&s->pb, 4, 0); //extension type - filler |
Alex Converse | efe6807 | 2012-01-23 21:08:32 | [diff] [blame] | 797 | padbits = -put_bits_count(&s->pb) & 7; |
Anton Khirnov | 717503f | 2020-10-26 12:36:06 | [diff] [blame] | 798 | align_put_bits(&s->pb); |
Alex Converse | fd257dc | 2009-07-08 20:36:45 | [diff] [blame] | 799 | for (i = 0; i < namelen - 2; i++) |
Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 800 | put_bits(&s->pb, 8, name[i]); |
| 801 | put_bits(&s->pb, 12 - padbits, 0); |
| 802 | } |
| 803 | |
Nathan Caldwell | 9b8e2a8 | 2011-12-15 02:43:56 | [diff] [blame] | 804 | /* |
Justin Ruggles | f3e2d68 | 2012-08-25 17:04:33 | [diff] [blame] | 805 | * Copy input samples. |
Michael Niedermayer | 4b4d3d7 | 2012-02-01 02:46:11 | [diff] [blame] | 806 | * Channels are reordered from libavcodec's default order to AAC order. |
Nathan Caldwell | 9b8e2a8 | 2011-12-15 02:43:56 | [diff] [blame] | 807 | */ |
Justin Ruggles | f3e2d68 | 2012-08-25 17:04:33 | [diff] [blame] | 808 | static void copy_input_samples(AACEncContext *s, const AVFrame *frame) |
Nathan Caldwell | 9b8e2a8 | 2011-12-15 02:43:56 | [diff] [blame] | 809 | { |
Justin Ruggles | f3e2d68 | 2012-08-25 17:04:33 | [diff] [blame] | 810 | int ch; |
| 811 | int end = 2048 + (frame ? frame->nb_samples : 0); |
Rostislav Pehlivanov | fbf295e | 2016-10-03 18:53:11 | [diff] [blame] | 812 | const uint8_t *channel_map = s->reorder_map; |
Nathan Caldwell | 9b8e2a8 | 2011-12-15 02:43:56 | [diff] [blame] | 813 | |
Justin Ruggles | f3e2d68 | 2012-08-25 17:04:33 | [diff] [blame] | 814 | /* copy and remap input samples */ |
| 815 | for (ch = 0; ch < s->channels; ch++) { |
Nathan Caldwell | 9b8e2a8 | 2011-12-15 02:43:56 | [diff] [blame] | 816 | /* copy last 1024 samples of previous frame to the start of the current frame */ |
Nathan Caldwell | dc7e7d4 | 2012-01-28 05:23:40 | [diff] [blame] | 817 | memcpy(&s->planar_samples[ch][1024], &s->planar_samples[ch][2048], 1024 * sizeof(s->planar_samples[0][0])); |
Nathan Caldwell | 9b8e2a8 | 2011-12-15 02:43:56 | [diff] [blame] | 818 | |
Justin Ruggles | f3e2d68 | 2012-08-25 17:04:33 | [diff] [blame] | 819 | /* copy new samples and zero any remaining samples */ |
Justin Ruggles | ad95307 | 2012-01-28 17:28:01 | [diff] [blame] | 820 | if (frame) { |
Justin Ruggles | f3e2d68 | 2012-08-25 17:04:33 | [diff] [blame] | 821 | memcpy(&s->planar_samples[ch][2048], |
| 822 | frame->extended_data[channel_map[ch]], |
| 823 | frame->nb_samples * sizeof(s->planar_samples[0][0])); |
Nathan Caldwell | 9b8e2a8 | 2011-12-15 02:43:56 | [diff] [blame] | 824 | } |
Justin Ruggles | f3e2d68 | 2012-08-25 17:04:33 | [diff] [blame] | 825 | memset(&s->planar_samples[ch][end], 0, |
| 826 | (3072 - end) * sizeof(s->planar_samples[0][0])); |
Nathan Caldwell | 9b8e2a8 | 2011-12-15 02:43:56 | [diff] [blame] | 827 | } |
| 828 | } |
| 829 | |
Justin Ruggles | ad95307 | 2012-01-28 17:28:01 | [diff] [blame] | 830 | static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, |
| 831 | const AVFrame *frame, int *got_packet_ptr) |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 832 | { |
| 833 | AACEncContext *s = avctx->priv_data; |
Nathan Caldwell | 7946a5a | 2011-12-24 22:24:16 | [diff] [blame] | 834 | float **samples = s->planar_samples, *samples2, *la, *overlap; |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 835 | ChannelElement *cpe; |
Rostislav Pehlivanov | d1ca714 | 2015-08-21 18:13:26 | [diff] [blame] | 836 | SingleChannelElement *sce; |
Rostislav Pehlivanov | 2d9b5ae | 2015-10-17 01:13:00 | [diff] [blame] | 837 | IndividualChannelStream *ics; |
Claudio Freire | 7ec74ae | 2015-09-23 05:13:56 | [diff] [blame] | 838 | int i, its, ch, w, chans, tag, start_ch, ret, frame_bits; |
Claudio Freire | 01ecb71 | 2015-10-11 20:29:50 | [diff] [blame] | 839 | int target_bits, rate_bits, too_many_bits, too_few_bits; |
Rostislav Pehlivanov | d1ca714 | 2015-08-21 18:13:26 | [diff] [blame] | 840 | int ms_mode = 0, is_mode = 0, tns_mode = 0, pred_mode = 0; |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 841 | int chan_el_counter[4]; |
Måns Rullgård | 86e41bc3 | 2010-07-06 00:06:15 | [diff] [blame] | 842 | FFPsyWindowInfo windows[AAC_MAX_CHANNELS]; |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 843 | |
Justin Ruggles | ad95307 | 2012-01-28 17:28:01 | [diff] [blame] | 844 | /* add current frame to queue */ |
| 845 | if (frame) { |
Michael Niedermayer | 98fed59 | 2013-01-13 23:02:50 | [diff] [blame] | 846 | if ((ret = ff_af_queue_add(&s->afq, frame)) < 0) |
Justin Ruggles | ad95307 | 2012-01-28 17:28:01 | [diff] [blame] | 847 | return ret; |
Rostislav Pehlivanov | 0cf6853 | 2016-11-07 02:11:50 | [diff] [blame] | 848 | } else { |
| 849 | if (!s->afq.remaining_samples || (!s->afq.frame_alloc && !s->afq.frame_count)) |
| 850 | return 0; |
Justin Ruggles | ad95307 | 2012-01-28 17:28:01 | [diff] [blame] | 851 | } |
| 852 | |
Justin Ruggles | f3e2d68 | 2012-08-25 17:04:33 | [diff] [blame] | 853 | copy_input_samples(s, frame); |
Justin Ruggles | 89eea6d | 2012-01-28 22:51:22 | [diff] [blame] | 854 | if (s->psypp) |
| 855 | ff_psy_preprocess(s->psypp, s->planar_samples, s->channels); |
Nathan Caldwell | 9b8e2a8 | 2011-12-15 02:43:56 | [diff] [blame] | 856 | |
Marton Balint | 6b6f7db | 2023-01-23 23:35:54 | [diff] [blame] | 857 | if (!avctx->frame_num) |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 858 | return 0; |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 859 | |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 860 | start_ch = 0; |
Nathan Caldwell | 1bb5204 | 2011-06-20 04:29:37 | [diff] [blame] | 861 | for (i = 0; i < s->chan_map[0]; i++) { |
Alex Converse | 5962f6b | 2009-07-08 23:10:13 | [diff] [blame] | 862 | FFPsyWindowInfo* wi = windows + start_ch; |
Nathan Caldwell | 1bb5204 | 2011-06-20 04:29:37 | [diff] [blame] | 863 | tag = s->chan_map[i+1]; |
Diego Biurrun | 99d61d3 | 2009-07-08 21:16:06 | [diff] [blame] | 864 | chans = tag == TYPE_CPE ? 2 : 1; |
| 865 | cpe = &s->cpe[i]; |
Nathan Caldwell | 5b29af6 | 2011-05-06 07:19:51 | [diff] [blame] | 866 | for (ch = 0; ch < chans; ch++) { |
Michael Niedermayer | 2cb8ede | 2016-01-20 14:05:38 | [diff] [blame] | 867 | int k; |
Michael Niedermayer | c38a607 | 2015-10-17 11:40:04 | [diff] [blame] | 868 | float clip_avoidance_factor; |
Rostislav Pehlivanov | 2d9b5ae | 2015-10-17 01:13:00 | [diff] [blame] | 869 | sce = &cpe->ch[ch]; |
| 870 | ics = &sce->ics; |
| 871 | s->cur_channel = start_ch + ch; |
Rostislav Pehlivanov | 2d9b5ae | 2015-10-17 01:13:00 | [diff] [blame] | 872 | overlap = &samples[s->cur_channel][0]; |
Nathan Caldwell | 7946a5a | 2011-12-24 22:24:16 | [diff] [blame] | 873 | samples2 = overlap + 1024; |
Nathan Caldwell | 9b8e2a8 | 2011-12-15 02:43:56 | [diff] [blame] | 874 | la = samples2 + (448+64); |
Justin Ruggles | ad95307 | 2012-01-28 17:28:01 | [diff] [blame] | 875 | if (!frame) |
Nathan Caldwell | 2bb1d0e | 2010-07-19 18:22:44 | [diff] [blame] | 876 | la = NULL; |
Alex Converse | 03d5d9b | 2010-07-07 21:48:51 | [diff] [blame] | 877 | if (tag == TYPE_LFE) { |
Claudio Freire | 8005b6d | 2016-04-06 02:13:44 | [diff] [blame] | 878 | wi[ch].window_type[0] = wi[ch].window_type[1] = ONLY_LONG_SEQUENCE; |
Nathan Caldwell | 5b29af6 | 2011-05-06 07:19:51 | [diff] [blame] | 879 | wi[ch].window_shape = 0; |
| 880 | wi[ch].num_windows = 1; |
| 881 | wi[ch].grouping[0] = 1; |
Claudio Freire | 8005b6d | 2016-04-06 02:13:44 | [diff] [blame] | 882 | wi[ch].clipping[0] = 0; |
Nathan Caldwell | 24efdea | 2011-07-29 20:49:04 | [diff] [blame] | 883 | |
| 884 | /* Only the lowest 12 coefficients are used in a LFE channel. |
| 885 | * The expression below results in only the bottom 8 coefficients |
| 886 | * being used for 11.025kHz to 16kHz sample rates. |
| 887 | */ |
| 888 | ics->num_swb = s->samplerate_index >= 8 ? 1 : 3; |
Alex Converse | 03d5d9b | 2010-07-07 21:48:51 | [diff] [blame] | 889 | } else { |
Rostislav Pehlivanov | 2d9b5ae | 2015-10-17 01:13:00 | [diff] [blame] | 890 | wi[ch] = s->psy.model->window(&s->psy, samples2, la, s->cur_channel, |
Alex Converse | 2678438 | 2010-07-07 21:50:50 | [diff] [blame] | 891 | ics->window_sequence[0]); |
Alex Converse | 03d5d9b | 2010-07-07 21:48:51 | [diff] [blame] | 892 | } |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 893 | ics->window_sequence[1] = ics->window_sequence[0]; |
Nathan Caldwell | 5b29af6 | 2011-05-06 07:19:51 | [diff] [blame] | 894 | ics->window_sequence[0] = wi[ch].window_type[0]; |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 895 | ics->use_kb_window[1] = ics->use_kb_window[0]; |
Nathan Caldwell | 5b29af6 | 2011-05-06 07:19:51 | [diff] [blame] | 896 | ics->use_kb_window[0] = wi[ch].window_shape; |
| 897 | ics->num_windows = wi[ch].num_windows; |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 898 | ics->swb_sizes = s->psy.bands [ics->num_windows == 8]; |
Nathan Caldwell | 24efdea | 2011-07-29 20:49:04 | [diff] [blame] | 899 | ics->num_swb = tag == TYPE_LFE ? ics->num_swb : s->psy.num_bands[ics->num_windows == 8]; |
Andreas Cadhalpun | 5b0da69 | 2015-12-06 20:35:08 | [diff] [blame] | 900 | ics->max_sfb = FFMIN(ics->max_sfb, ics->num_swb); |
Rostislav Pehlivanov | 23e786b | 2015-08-21 17:40:44 | [diff] [blame] | 901 | ics->swb_offset = wi[ch].window_type[0] == EIGHT_SHORT_SEQUENCE ? |
| 902 | ff_swb_offset_128 [s->samplerate_index]: |
| 903 | ff_swb_offset_1024[s->samplerate_index]; |
Rostislav Pehlivanov | 5ed5ca7 | 2015-09-01 05:20:24 | [diff] [blame] | 904 | ics->tns_max_bands = wi[ch].window_type[0] == EIGHT_SHORT_SEQUENCE ? |
| 905 | ff_tns_max_bands_128 [s->samplerate_index]: |
| 906 | ff_tns_max_bands_1024[s->samplerate_index]; |
Claudio Freire | 8005b6d | 2016-04-06 02:13:44 | [diff] [blame] | 907 | |
Nathan Caldwell | 5b29af6 | 2011-05-06 07:19:51 | [diff] [blame] | 908 | for (w = 0; w < ics->num_windows; w++) |
| 909 | ics->group_len[w] = wi[ch].grouping[w]; |
Claudio Freire | 8005b6d | 2016-04-06 02:13:44 | [diff] [blame] | 910 | |
| 911 | /* Calculate input sample maximums and evaluate clipping risk */ |
| 912 | clip_avoidance_factor = 0.0f; |
| 913 | for (w = 0; w < ics->num_windows; w++) { |
| 914 | const float *wbuf = overlap + w * 128; |
| 915 | const int wlen = 2048 / ics->num_windows; |
| 916 | float max = 0; |
| 917 | int j; |
| 918 | /* mdct input is 2 * output */ |
| 919 | for (j = 0; j < wlen; j++) |
| 920 | max = FFMAX(max, fabsf(wbuf[j])); |
| 921 | wi[ch].clipping[w] = max; |
| 922 | } |
Claudio Freire | 59216e0 | 2015-07-21 01:53:24 | [diff] [blame] | 923 | for (w = 0; w < ics->num_windows; w++) { |
| 924 | if (wi[ch].clipping[w] > CLIP_AVOIDANCE_FACTOR) { |
| 925 | ics->window_clipping[w] = 1; |
| 926 | clip_avoidance_factor = FFMAX(clip_avoidance_factor, wi[ch].clipping[w]); |
| 927 | } else { |
| 928 | ics->window_clipping[w] = 0; |
| 929 | } |
| 930 | } |
| 931 | if (clip_avoidance_factor > CLIP_AVOIDANCE_FACTOR) { |
| 932 | ics->clip_avoidance_factor = CLIP_AVOIDANCE_FACTOR / clip_avoidance_factor; |
| 933 | } else { |
| 934 | ics->clip_avoidance_factor = 1.0f; |
| 935 | } |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 936 | |
Rostislav Pehlivanov | 2d9b5ae | 2015-10-17 01:13:00 | [diff] [blame] | 937 | apply_window_and_mdct(s, sce, overlap); |
Rostislav Pehlivanov | 27d23ae | 2015-10-17 01:22:51 | [diff] [blame] | 938 | |
| 939 | if (s->options.ltp && s->coder->update_ltp) { |
| 940 | s->coder->update_ltp(s, sce); |
| 941 | apply_window[sce->ics.window_sequence[0]](s->fdsp, sce, &sce->ltp_state[0]); |
Lynne | 8f3e062 | 2022-10-29 12:01:57 | [diff] [blame] | 942 | s->mdct1024_fn(s->mdct1024, sce->lcoeffs, sce->ret_buf, sizeof(float)); |
Rostislav Pehlivanov | 27d23ae | 2015-10-17 01:22:51 | [diff] [blame] | 943 | } |
| 944 | |
Michael Niedermayer | 2cb8ede | 2016-01-20 14:05:38 | [diff] [blame] | 945 | for (k = 0; k < 1024; k++) { |
Michael Niedermayer | 77bf96b | 2016-08-23 09:00:29 | [diff] [blame] | 946 | if (!(fabs(cpe->ch[ch].coeffs[k]) < 1E16)) { // Ensure headroom for energy calculation |
| 947 | av_log(avctx, AV_LOG_ERROR, "Input contains (near) NaN/+-Inf\n"); |
Michael Niedermayer | 2cb8ede | 2016-01-20 14:05:38 | [diff] [blame] | 948 | return AVERROR(EINVAL); |
| 949 | } |
Michael Niedermayer | f9fa560 | 2014-11-08 22:32:39 | [diff] [blame] | 950 | } |
Rostislav Pehlivanov | 2d9b5ae | 2015-10-17 01:13:00 | [diff] [blame] | 951 | avoid_clipping(s, sce); |
Alex Converse | 5962f6b | 2009-07-08 23:10:13 | [diff] [blame] | 952 | } |
| 953 | start_ch += chans; |
| 954 | } |
Andreas Rheinhardt | 56e9e02 | 2021-05-11 13:17:13 | [diff] [blame] | 955 | if ((ret = ff_alloc_packet(avctx, avpkt, 8192 * s->channels)) < 0) |
Reimar Döffinger | ecd7455 | 2012-04-06 13:25:05 | [diff] [blame] | 956 | return ret; |
Claudio Freire | 7ec74ae | 2015-09-23 05:13:56 | [diff] [blame] | 957 | frame_bits = its = 0; |
Alex Converse | 48d20c1 | 2009-07-08 23:12:53 | [diff] [blame] | 958 | do { |
Justin Ruggles | ad95307 | 2012-01-28 17:28:01 | [diff] [blame] | 959 | init_put_bits(&s->pb, avpkt->data, avpkt->size); |
| 960 | |
Marton Balint | 6b6f7db | 2023-01-23 23:35:54 | [diff] [blame] | 961 | if ((avctx->frame_num & 0xFF)==1 && !(avctx->flags & AV_CODEC_FLAG_BITEXACT)) |
Diego Biurrun | 72c758f | 2012-10-24 17:16:08 | [diff] [blame] | 962 | put_bitstream_info(s, LIBAVCODEC_IDENT); |
Alex Converse | f11bfe3 | 2009-07-08 23:14:47 | [diff] [blame] | 963 | start_ch = 0; |
Claudio Freire | 7ec74ae | 2015-09-23 05:13:56 | [diff] [blame] | 964 | target_bits = 0; |
Alex Converse | f11bfe3 | 2009-07-08 23:14:47 | [diff] [blame] | 965 | memset(chan_el_counter, 0, sizeof(chan_el_counter)); |
Nathan Caldwell | 1bb5204 | 2011-06-20 04:29:37 | [diff] [blame] | 966 | for (i = 0; i < s->chan_map[0]; i++) { |
Alex Converse | f11bfe3 | 2009-07-08 23:14:47 | [diff] [blame] | 967 | FFPsyWindowInfo* wi = windows + start_ch; |
Nathan Caldwell | 01344fe | 2011-05-19 05:14:59 | [diff] [blame] | 968 | const float *coeffs[2]; |
Nathan Caldwell | 1bb5204 | 2011-06-20 04:29:37 | [diff] [blame] | 969 | tag = s->chan_map[i+1]; |
Alex Converse | f11bfe3 | 2009-07-08 23:14:47 | [diff] [blame] | 970 | chans = tag == TYPE_CPE ? 2 : 1; |
| 971 | cpe = &s->cpe[i]; |
Rostislav Pehlivanov | a0079aa | 2015-09-01 06:00:10 | [diff] [blame] | 972 | cpe->common_window = 0; |
Rostislav Pehlivanov | 9f4f578 | 2015-07-02 18:13:03 | [diff] [blame] | 973 | memset(cpe->is_mask, 0, sizeof(cpe->is_mask)); |
| 974 | memset(cpe->ms_mask, 0, sizeof(cpe->ms_mask)); |
Alex Converse | 8e4c11e | 2010-08-30 23:52:03 | [diff] [blame] | 975 | put_bits(&s->pb, 3, tag); |
| 976 | put_bits(&s->pb, 4, chan_el_counter[tag]++); |
Rostislav Pehlivanov | e6c9f3a | 2015-08-21 17:36:09 | [diff] [blame] | 977 | for (ch = 0; ch < chans; ch++) { |
| 978 | sce = &cpe->ch[ch]; |
| 979 | coeffs[ch] = sce->coeffs; |
Rostislav Pehlivanov | 76b81b1 | 2015-08-21 18:38:05 | [diff] [blame] | 980 | sce->ics.predictor_present = 0; |
Rostislav Pehlivanov | 27d23ae | 2015-10-17 01:22:51 | [diff] [blame] | 981 | sce->ics.ltp.present = 0; |
| 982 | memset(sce->ics.ltp.used, 0, sizeof(sce->ics.ltp.used)); |
Rostislav Pehlivanov | 3f3be1c | 2015-10-17 01:14:10 | [diff] [blame] | 983 | memset(sce->ics.prediction_used, 0, sizeof(sce->ics.prediction_used)); |
Rostislav Pehlivanov | a1c487e | 2015-08-21 18:27:38 | [diff] [blame] | 984 | memset(&sce->tns, 0, sizeof(TemporalNoiseShaping)); |
Rostislav Pehlivanov | e6c9f3a | 2015-08-21 17:36:09 | [diff] [blame] | 985 | for (w = 0; w < 128; w++) |
| 986 | if (sce->band_type[w] > RESERVED_BT) |
| 987 | sce->band_type[w] = 0; |
| 988 | } |
Claudio Freire | 7ec74ae | 2015-09-23 05:13:56 | [diff] [blame] | 989 | s->psy.bitres.alloc = -1; |
Rostislav Pehlivanov | 4386f17 | 2015-12-18 14:27:13 | [diff] [blame] | 990 | s->psy.bitres.bits = s->last_frame_pb_count / s->channels; |
Nathan Caldwell | d3a6c2a | 2011-05-19 05:23:22 | [diff] [blame] | 991 | s->psy.model->analyze(&s->psy, start_ch, coeffs, wi); |
Claudio Freire | 7ec74ae | 2015-09-23 05:13:56 | [diff] [blame] | 992 | if (s->psy.bitres.alloc > 0) { |
| 993 | /* Lambda unused here on purpose, we need to take psy's unscaled allocation */ |
Claudio Freire | 01ecb71 | 2015-10-11 20:29:50 | [diff] [blame] | 994 | target_bits += s->psy.bitres.alloc |
| 995 | * (s->lambda / (avctx->global_quality ? avctx->global_quality : 120)); |
Claudio Freire | 7ec74ae | 2015-09-23 05:13:56 | [diff] [blame] | 996 | s->psy.bitres.alloc /= chans; |
| 997 | } |
| 998 | s->cur_type = tag; |
Nathan Caldwell | 5b29af6 | 2011-05-06 07:19:51 | [diff] [blame] | 999 | for (ch = 0; ch < chans; ch++) { |
Claudio Freire | e41cd3c | 2013-05-12 07:38:40 | [diff] [blame] | 1000 | s->cur_channel = start_ch + ch; |
Claudio Freire | 01ecb71 | 2015-10-11 20:29:50 | [diff] [blame] | 1001 | if (s->options.pns && s->coder->mark_pns) |
| 1002 | s->coder->mark_pns(s, avctx, &cpe->ch[ch]); |
Nathan Caldwell | 5b29af6 | 2011-05-06 07:19:51 | [diff] [blame] | 1003 | s->coder->search_for_quantizers(avctx, s, &cpe->ch[ch], s->lambda); |
Alex Converse | f11bfe3 | 2009-07-08 23:14:47 | [diff] [blame] | 1004 | } |
Alex Converse | f11bfe3 | 2009-07-08 23:14:47 | [diff] [blame] | 1005 | if (chans > 1 |
| 1006 | && wi[0].window_type[0] == wi[1].window_type[0] |
| 1007 | && wi[0].window_shape == wi[1].window_shape) { |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 1008 | |
Alex Converse | f11bfe3 | 2009-07-08 23:14:47 | [diff] [blame] | 1009 | cpe->common_window = 1; |
Nathan Caldwell | 5b29af6 | 2011-05-06 07:19:51 | [diff] [blame] | 1010 | for (w = 0; w < wi[0].num_windows; w++) { |
| 1011 | if (wi[0].grouping[w] != wi[1].grouping[w]) { |
Alex Converse | f11bfe3 | 2009-07-08 23:14:47 | [diff] [blame] | 1012 | cpe->common_window = 0; |
| 1013 | break; |
| 1014 | } |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 1015 | } |
| 1016 | } |
Rostislav Pehlivanov | 20dc527 | 2015-09-02 05:26:45 | [diff] [blame] | 1017 | for (ch = 0; ch < chans; ch++) { /* TNS and PNS */ |
Rostislav Pehlivanov | a1c487e | 2015-08-21 18:27:38 | [diff] [blame] | 1018 | sce = &cpe->ch[ch]; |
| 1019 | s->cur_channel = start_ch + ch; |
Rostislav Pehlivanov | a1c487e | 2015-08-21 18:27:38 | [diff] [blame] | 1020 | if (s->options.tns && s->coder->search_for_tns) |
| 1021 | s->coder->search_for_tns(s, sce); |
Rostislav Pehlivanov | f20b671 | 2015-08-29 05:47:31 | [diff] [blame] | 1022 | if (s->options.tns && s->coder->apply_tns_filt) |
Rostislav Pehlivanov | f3f6c6b | 2015-09-01 05:44:07 | [diff] [blame] | 1023 | s->coder->apply_tns_filt(s, sce); |
Rostislav Pehlivanov | a1c487e | 2015-08-21 18:27:38 | [diff] [blame] | 1024 | if (sce->tns.present) |
| 1025 | tns_mode = 1; |
Rostislav Pehlivanov | b32e989 | 2015-12-06 13:32:40 | [diff] [blame] | 1026 | if (s->options.pns && s->coder->search_for_pns) |
| 1027 | s->coder->search_for_pns(s, avctx, sce); |
Rostislav Pehlivanov | 38fd4c2 | 2015-07-02 18:13:05 | [diff] [blame] | 1028 | } |
Claudio Freire | e41cd3c | 2013-05-12 07:38:40 | [diff] [blame] | 1029 | s->cur_channel = start_ch; |
Rostislav Pehlivanov | 20dc527 | 2015-09-02 05:26:45 | [diff] [blame] | 1030 | if (s->options.intensity_stereo) { /* Intensity Stereo */ |
| 1031 | if (s->coder->search_for_is) |
| 1032 | s->coder->search_for_is(s, avctx, cpe); |
Rostislav Pehlivanov | e8576dc | 2015-07-02 18:13:07 | [diff] [blame] | 1033 | if (cpe->is_mode) is_mode = 1; |
Rostislav Pehlivanov | 20dc527 | 2015-09-02 05:26:45 | [diff] [blame] | 1034 | apply_intensity_stereo(cpe); |
Rostislav Pehlivanov | e8576dc | 2015-07-02 18:13:07 | [diff] [blame] | 1035 | } |
Rostislav Pehlivanov | 20dc527 | 2015-09-02 05:26:45 | [diff] [blame] | 1036 | if (s->options.pred) { /* Prediction */ |
| 1037 | for (ch = 0; ch < chans; ch++) { |
| 1038 | sce = &cpe->ch[ch]; |
| 1039 | s->cur_channel = start_ch + ch; |
| 1040 | if (s->options.pred && s->coder->search_for_pred) |
| 1041 | s->coder->search_for_pred(s, sce); |
| 1042 | if (cpe->ch[ch].ics.predictor_present) pred_mode = 1; |
| 1043 | } |
Rostislav Pehlivanov | 93e6b23 | 2015-10-12 22:33:07 | [diff] [blame] | 1044 | if (s->coder->adjust_common_pred) |
| 1045 | s->coder->adjust_common_pred(s, cpe); |
Rostislav Pehlivanov | 20dc527 | 2015-09-02 05:26:45 | [diff] [blame] | 1046 | for (ch = 0; ch < chans; ch++) { |
| 1047 | sce = &cpe->ch[ch]; |
| 1048 | s->cur_channel = start_ch + ch; |
| 1049 | if (s->options.pred && s->coder->apply_main_pred) |
| 1050 | s->coder->apply_main_pred(s, sce); |
| 1051 | } |
| 1052 | s->cur_channel = start_ch; |
| 1053 | } |
Rostislav Pehlivanov | 0f4334d | 2015-10-12 15:50:10 | [diff] [blame] | 1054 | if (s->options.mid_side) { /* Mid/Side stereo */ |
| 1055 | if (s->options.mid_side == -1 && s->coder->search_for_ms) |
Rostislav Pehlivanov | 20dc527 | 2015-09-02 05:26:45 | [diff] [blame] | 1056 | s->coder->search_for_ms(s, cpe); |
| 1057 | else if (cpe->common_window) |
| 1058 | memset(cpe->ms_mask, 1, sizeof(cpe->ms_mask)); |
Rostislav Pehlivanov | 20dc527 | 2015-09-02 05:26:45 | [diff] [blame] | 1059 | apply_mid_side_stereo(cpe); |
| 1060 | } |
Diego Biurrun | 72c758f | 2012-10-24 17:16:08 | [diff] [blame] | 1061 | adjust_frame_information(cpe, chans); |
Rostislav Pehlivanov | 27d23ae | 2015-10-17 01:22:51 | [diff] [blame] | 1062 | if (s->options.ltp) { /* LTP */ |
| 1063 | for (ch = 0; ch < chans; ch++) { |
| 1064 | sce = &cpe->ch[ch]; |
| 1065 | s->cur_channel = start_ch + ch; |
| 1066 | if (s->coder->search_for_ltp) |
| 1067 | s->coder->search_for_ltp(s, sce, cpe->common_window); |
| 1068 | if (sce->ics.ltp.present) pred_mode = 1; |
| 1069 | } |
| 1070 | s->cur_channel = start_ch; |
| 1071 | if (s->coder->adjust_common_ltp) |
| 1072 | s->coder->adjust_common_ltp(s, cpe); |
| 1073 | } |
Alex Converse | f11bfe3 | 2009-07-08 23:14:47 | [diff] [blame] | 1074 | if (chans == 2) { |
| 1075 | put_bits(&s->pb, 1, cpe->common_window); |
| 1076 | if (cpe->common_window) { |
| 1077 | put_ics_info(s, &cpe->ch[0].ics); |
Rostislav Pehlivanov | 76b81b1 | 2015-08-21 18:38:05 | [diff] [blame] | 1078 | if (s->coder->encode_main_pred) |
| 1079 | s->coder->encode_main_pred(s, &cpe->ch[0]); |
Rostislav Pehlivanov | 27d23ae | 2015-10-17 01:22:51 | [diff] [blame] | 1080 | if (s->coder->encode_ltp_info) |
| 1081 | s->coder->encode_ltp_info(s, &cpe->ch[0], 1); |
Alex Converse | f11bfe3 | 2009-07-08 23:14:47 | [diff] [blame] | 1082 | encode_ms_info(&s->pb, cpe); |
Claudio Freire | 6394aca | 2015-03-03 06:43:06 | [diff] [blame] | 1083 | if (cpe->ms_mode) ms_mode = 1; |
Alex Converse | f11bfe3 | 2009-07-08 23:14:47 | [diff] [blame] | 1084 | } |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 1085 | } |
Nathan Caldwell | 5b29af6 | 2011-05-06 07:19:51 | [diff] [blame] | 1086 | for (ch = 0; ch < chans; ch++) { |
| 1087 | s->cur_channel = start_ch + ch; |
| 1088 | encode_individual_channel(avctx, s, &cpe->ch[ch], cpe->common_window); |
Alex Converse | f11bfe3 | 2009-07-08 23:14:47 | [diff] [blame] | 1089 | } |
| 1090 | start_ch += chans; |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 1091 | } |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 1092 | |
James Almer | f5c8d00 | 2017-03-26 00:35:15 | [diff] [blame] | 1093 | if (avctx->flags & AV_CODEC_FLAG_QSCALE) { |
Claudio Freire | 7ec74ae | 2015-09-23 05:13:56 | [diff] [blame] | 1094 | /* When using a constant Q-scale, don't mess with lambda */ |
Alex Converse | 48d20c1 | 2009-07-08 23:12:53 | [diff] [blame] | 1095 | break; |
Nathan Caldwell | 230c1a9 | 2011-01-05 08:32:16 | [diff] [blame] | 1096 | } |
Claudio Freire | 7ec74ae | 2015-09-23 05:13:56 | [diff] [blame] | 1097 | |
| 1098 | /* rate control stuff |
Claudio Freire | 01ecb71 | 2015-10-11 20:29:50 | [diff] [blame] | 1099 | * allow between the nominal bitrate, and what psy's bit reservoir says to target |
| 1100 | * but drift towards the nominal bitrate always |
Claudio Freire | 7ec74ae | 2015-09-23 05:13:56 | [diff] [blame] | 1101 | */ |
Claudio Freire | 7ec74ae | 2015-09-23 05:13:56 | [diff] [blame] | 1102 | frame_bits = put_bits_count(&s->pb); |
Claudio Freire | 01ecb71 | 2015-10-11 20:29:50 | [diff] [blame] | 1103 | rate_bits = avctx->bit_rate * 1024 / avctx->sample_rate; |
| 1104 | rate_bits = FFMIN(rate_bits, 6144 * s->channels - 3); |
| 1105 | too_many_bits = FFMAX(target_bits, rate_bits); |
| 1106 | too_many_bits = FFMIN(too_many_bits, 6144 * s->channels - 3); |
| 1107 | too_few_bits = FFMIN(FFMAX(rate_bits - rate_bits/4, target_bits), too_many_bits); |
Claudio Freire | 7ec74ae | 2015-09-23 05:13:56 | [diff] [blame] | 1108 | |
Jeremy Wu | b92af7b | 2023-04-25 06:09:52 | [diff] [blame] | 1109 | /* When strict bit-rate control is demanded */ |
| 1110 | if (avctx->bit_rate_tolerance == 0) { |
| 1111 | if (rate_bits < frame_bits) { |
| 1112 | float ratio = ((float)rate_bits) / frame_bits; |
| 1113 | s->lambda *= FFMIN(0.9f, ratio); |
| 1114 | continue; |
| 1115 | } |
| 1116 | /* reset lambda when solution is found */ |
| 1117 | s->lambda = avctx->global_quality > 0 ? avctx->global_quality : 120; |
| 1118 | break; |
| 1119 | } |
| 1120 | |
Claudio Freire | 7ec74ae | 2015-09-23 05:13:56 | [diff] [blame] | 1121 | /* When using ABR, be strict (but only for increasing) */ |
Claudio Freire | 01ecb71 | 2015-10-11 20:29:50 | [diff] [blame] | 1122 | too_few_bits = too_few_bits - too_few_bits/8; |
| 1123 | too_many_bits = too_many_bits + too_many_bits/2; |
Claudio Freire | 7ec74ae | 2015-09-23 05:13:56 | [diff] [blame] | 1124 | |
| 1125 | if ( its == 0 /* for steady-state Q-scale tracking */ |
| 1126 | || (its < 5 && (frame_bits < too_few_bits || frame_bits > too_many_bits)) |
| 1127 | || frame_bits >= 6144 * s->channels - 3 ) |
| 1128 | { |
Claudio Freire | 01ecb71 | 2015-10-11 20:29:50 | [diff] [blame] | 1129 | float ratio = ((float)rate_bits) / frame_bits; |
Claudio Freire | 7ec74ae | 2015-09-23 05:13:56 | [diff] [blame] | 1130 | |
| 1131 | if (frame_bits >= too_few_bits && frame_bits <= too_many_bits) { |
| 1132 | /* |
| 1133 | * This path is for steady-state Q-scale tracking |
| 1134 | * When frame bits fall within the stable range, we still need to adjust |
| 1135 | * lambda to maintain it like so in a stable fashion (large jumps in lambda |
| 1136 | * create artifacts and should be avoided), but slowly |
| 1137 | */ |
| 1138 | ratio = sqrtf(sqrtf(ratio)); |
| 1139 | ratio = av_clipf(ratio, 0.9f, 1.1f); |
| 1140 | } else { |
| 1141 | /* Not so fast though */ |
| 1142 | ratio = sqrtf(ratio); |
Claudio Freire | 6394aca | 2015-03-03 06:43:06 | [diff] [blame] | 1143 | } |
Michael Niedermayer | 4b89cf7 | 2021-06-01 08:07:05 | [diff] [blame] | 1144 | s->lambda = av_clipf(s->lambda * ratio, FLT_EPSILON, 65536.f); |
Claudio Freire | 7ec74ae | 2015-09-23 05:13:56 | [diff] [blame] | 1145 | |
| 1146 | /* Keep iterating if we must reduce and lambda is in the sky */ |
Claudio Freire | ca203e9 | 2015-12-01 06:28:36 | [diff] [blame] | 1147 | if (ratio > 0.9f && ratio < 1.1f) { |
Claudio Freire | 7ec74ae | 2015-09-23 05:13:56 | [diff] [blame] | 1148 | break; |
| 1149 | } else { |
| 1150 | if (is_mode || ms_mode || tns_mode || pred_mode) { |
| 1151 | for (i = 0; i < s->chan_map[0]; i++) { |
| 1152 | // Must restore coeffs |
| 1153 | chans = tag == TYPE_CPE ? 2 : 1; |
| 1154 | cpe = &s->cpe[i]; |
| 1155 | for (ch = 0; ch < chans; ch++) |
| 1156 | memcpy(cpe->ch[ch].coeffs, cpe->ch[ch].pcoeffs, sizeof(cpe->ch[ch].coeffs)); |
| 1157 | } |
| 1158 | } |
| 1159 | its++; |
| 1160 | } |
| 1161 | } else { |
| 1162 | break; |
Claudio Freire | 6394aca | 2015-03-03 06:43:06 | [diff] [blame] | 1163 | } |
Alex Converse | 48d20c1 | 2009-07-08 23:12:53 | [diff] [blame] | 1164 | } while (1); |
| 1165 | |
Rostislav Pehlivanov | 27d23ae | 2015-10-17 01:22:51 | [diff] [blame] | 1166 | if (s->options.ltp && s->coder->ltp_insert_new_frame) |
| 1167 | s->coder->ltp_insert_new_frame(s); |
| 1168 | |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 1169 | put_bits(&s->pb, 3, TYPE_END); |
| 1170 | flush_put_bits(&s->pb); |
Hendrik Leppkes | 362028c | 2015-12-18 13:39:15 | [diff] [blame] | 1171 | |
Rostislav Pehlivanov | 4386f17 | 2015-12-18 14:27:13 | [diff] [blame] | 1172 | s->last_frame_pb_count = put_bits_count(&s->pb); |
Andreas Rheinhardt | c81b8e0 | 2021-03-25 11:52:56 | [diff] [blame] | 1173 | avpkt->size = put_bytes_output(&s->pb); |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 1174 | |
Claudio Freire | 01ecb71 | 2015-10-11 20:29:50 | [diff] [blame] | 1175 | s->lambda_sum += s->lambda; |
| 1176 | s->lambda_count++; |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 1177 | |
Justin Ruggles | ad95307 | 2012-01-28 17:28:01 | [diff] [blame] | 1178 | ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts, |
| 1179 | &avpkt->duration); |
| 1180 | |
Justin Ruggles | ad95307 | 2012-01-28 17:28:01 | [diff] [blame] | 1181 | *got_packet_ptr = 1; |
| 1182 | return 0; |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 1183 | } |
| 1184 | |
Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 1185 | static av_cold int aac_encode_end(AVCodecContext *avctx) |
| 1186 | { |
| 1187 | AACEncContext *s = avctx->priv_data; |
| 1188 | |
Michael Niedermayer | c520b98 | 2021-05-29 15:49:22 | [diff] [blame] | 1189 | av_log(avctx, AV_LOG_INFO, "Qavg: %.3f\n", s->lambda_count ? s->lambda_sum / s->lambda_count : NAN); |
Claudio Freire | 01ecb71 | 2015-10-11 20:29:50 | [diff] [blame] | 1190 | |
Lynne | 8f3e062 | 2022-10-29 12:01:57 | [diff] [blame] | 1191 | av_tx_uninit(&s->mdct1024); |
| 1192 | av_tx_uninit(&s->mdct128); |
Alex Converse | 78e65cd | 2009-07-08 20:01:31 | [diff] [blame] | 1193 | ff_psy_end(&s->psy); |
Rostislav Pehlivanov | b47a1e5 | 2015-08-21 17:43:09 | [diff] [blame] | 1194 | ff_lpc_end(&s->lpc); |
Nathan Caldwell | 5310704 | 2011-12-15 02:50:23 | [diff] [blame] | 1195 | if (s->psypp) |
| 1196 | ff_psy_preprocess_end(s->psypp); |
Nathan Caldwell | 9b8e2a8 | 2011-12-15 02:43:56 | [diff] [blame] | 1197 | av_freep(&s->buffer.samples); |
Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 1198 | av_freep(&s->cpe); |
Michael Niedermayer | 14285c3 | 2014-11-29 17:58:13 | [diff] [blame] | 1199 | av_freep(&s->fdsp); |
Justin Ruggles | ad95307 | 2012-01-28 17:28:01 | [diff] [blame] | 1200 | ff_af_queue_close(&s->afq); |
Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 1201 | return 0; |
| 1202 | } |
| 1203 | |
Nathan Caldwell | 5310704 | 2011-12-15 02:50:23 | [diff] [blame] | 1204 | static av_cold int dsp_init(AVCodecContext *avctx, AACEncContext *s) |
| 1205 | { |
| 1206 | int ret = 0; |
Lynne | 8f3e062 | 2022-10-29 12:01:57 | [diff] [blame] | 1207 | float scale = 32768.0f; |
Nathan Caldwell | 5310704 | 2011-12-15 02:50:23 | [diff] [blame] | 1208 | |
Michael Niedermayer | 94d68a4 | 2015-07-27 19:14:31 | [diff] [blame] | 1209 | s->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT); |
Michael Niedermayer | 14285c3 | 2014-11-29 17:58:13 | [diff] [blame] | 1210 | if (!s->fdsp) |
| 1211 | return AVERROR(ENOMEM); |
Nathan Caldwell | 5310704 | 2011-12-15 02:50:23 | [diff] [blame] | 1212 | |
| 1213 | // window init |
Andreas Rheinhardt | 86b8c25 | 2020-11-22 22:33:03 | [diff] [blame] | 1214 | ff_aac_float_common_init(); |
Nathan Caldwell | 5310704 | 2011-12-15 02:50:23 | [diff] [blame] | 1215 | |
Lynne | 8f3e062 | 2022-10-29 12:01:57 | [diff] [blame] | 1216 | if ((ret = av_tx_init(&s->mdct1024, &s->mdct1024_fn, AV_TX_FLOAT_MDCT, 0, |
| 1217 | 1024, &scale, 0)) < 0) |
Nathan Caldwell | 5310704 | 2011-12-15 02:50:23 | [diff] [blame] | 1218 | return ret; |
Lynne | 8f3e062 | 2022-10-29 12:01:57 | [diff] [blame] | 1219 | if ((ret = av_tx_init(&s->mdct128, &s->mdct128_fn, AV_TX_FLOAT_MDCT, 0, |
| 1220 | 128, &scale, 0)) < 0) |
Nathan Caldwell | 5310704 | 2011-12-15 02:50:23 | [diff] [blame] | 1221 | return ret; |
| 1222 | |
| 1223 | return 0; |
| 1224 | } |
| 1225 | |
| 1226 | static av_cold int alloc_buffers(AVCodecContext *avctx, AACEncContext *s) |
| 1227 | { |
Mans Rullgard | 3715d841 | 2012-01-29 20:55:10 | [diff] [blame] | 1228 | int ch; |
Limin Wang | ebf2a8a | 2020-06-01 14:48:28 | [diff] [blame] | 1229 | if (!FF_ALLOCZ_TYPED_ARRAY(s->buffer.samples, s->channels * 3 * 1024) || |
| 1230 | !FF_ALLOCZ_TYPED_ARRAY(s->cpe, s->chan_map[0])) |
| 1231 | return AVERROR(ENOMEM); |
Nathan Caldwell | 5310704 | 2011-12-15 02:50:23 | [diff] [blame] | 1232 | |
Mans Rullgard | 3715d841 | 2012-01-29 20:55:10 | [diff] [blame] | 1233 | for(ch = 0; ch < s->channels; ch++) |
Nathan Caldwell | 7946a5a | 2011-12-24 22:24:16 | [diff] [blame] | 1234 | s->planar_samples[ch] = s->buffer.samples + 3 * 1024 * ch; |
Nathan Caldwell | 9b8e2a8 | 2011-12-15 02:43:56 | [diff] [blame] | 1235 | |
Nathan Caldwell | 5310704 | 2011-12-15 02:50:23 | [diff] [blame] | 1236 | return 0; |
Nathan Caldwell | 5310704 | 2011-12-15 02:50:23 | [diff] [blame] | 1237 | } |
| 1238 | |
| 1239 | static av_cold int aac_encode_init(AVCodecContext *avctx) |
| 1240 | { |
| 1241 | AACEncContext *s = avctx->priv_data; |
| 1242 | int i, ret = 0; |
| 1243 | const uint8_t *sizes[2]; |
| 1244 | uint8_t grouping[AAC_MAX_CHANNELS]; |
| 1245 | int lengths[2]; |
| 1246 | |
Rostislav Pehlivanov | f0a8212 | 2016-02-12 18:34:18 | [diff] [blame] | 1247 | /* Constants */ |
Rostislav Pehlivanov | 4386f17 | 2015-12-18 14:27:13 | [diff] [blame] | 1248 | s->last_frame_pb_count = 0; |
Nathan Caldwell | 5310704 | 2011-12-15 02:50:23 | [diff] [blame] | 1249 | avctx->frame_size = 1024; |
Rostislav Pehlivanov | 0f4334d | 2015-10-12 15:50:10 | [diff] [blame] | 1250 | avctx->initial_padding = 1024; |
Rostislav Pehlivanov | f0a8212 | 2016-02-12 18:34:18 | [diff] [blame] | 1251 | s->lambda = avctx->global_quality > 0 ? avctx->global_quality : 120; |
Nathan Caldwell | 5310704 | 2011-12-15 02:50:23 | [diff] [blame] | 1252 | |
Rostislav Pehlivanov | f0a8212 | 2016-02-12 18:34:18 | [diff] [blame] | 1253 | /* Channel map and unspecified bitrate guessing */ |
Anton Khirnov | 494760f | 2013-05-07 05:20:32 | [diff] [blame] | 1254 | s->channels = avctx->ch_layout.nb_channels; |
Rostislav Pehlivanov | fbf295e | 2016-10-03 18:53:11 | [diff] [blame] | 1255 | |
| 1256 | s->needs_pce = 1; |
| 1257 | for (i = 0; i < FF_ARRAY_ELEMS(aac_normal_chan_layouts); i++) { |
Anton Khirnov | 494760f | 2013-05-07 05:20:32 | [diff] [blame] | 1258 | if (!av_channel_layout_compare(&avctx->ch_layout, &aac_normal_chan_layouts[i])) { |
Rostislav Pehlivanov | fbf295e | 2016-10-03 18:53:11 | [diff] [blame] | 1259 | s->needs_pce = s->options.pce; |
| 1260 | break; |
| 1261 | } |
| 1262 | } |
| 1263 | |
| 1264 | if (s->needs_pce) { |
Moritz Barsnick | 1693a68 | 2018-07-06 21:49:50 | [diff] [blame] | 1265 | char buf[64]; |
Rostislav Pehlivanov | fbf295e | 2016-10-03 18:53:11 | [diff] [blame] | 1266 | for (i = 0; i < FF_ARRAY_ELEMS(aac_pce_configs); i++) |
Anton Khirnov | 494760f | 2013-05-07 05:20:32 | [diff] [blame] | 1267 | if (!av_channel_layout_compare(&avctx->ch_layout, &aac_pce_configs[i].layout)) |
Rostislav Pehlivanov | fbf295e | 2016-10-03 18:53:11 | [diff] [blame] | 1268 | break; |
Anton Khirnov | 494760f | 2013-05-07 05:20:32 | [diff] [blame] | 1269 | av_channel_layout_describe(&avctx->ch_layout, buf, sizeof(buf)); |
| 1270 | if (i == FF_ARRAY_ELEMS(aac_pce_configs)) { |
| 1271 | av_log(avctx, AV_LOG_ERROR, "Unsupported channel layout \"%s\"\n", buf); |
| 1272 | return AVERROR(EINVAL); |
| 1273 | } |
Moritz Barsnick | 1693a68 | 2018-07-06 21:49:50 | [diff] [blame] | 1274 | av_log(avctx, AV_LOG_INFO, "Using a PCE to encode channel layout \"%s\"\n", buf); |
Rostislav Pehlivanov | fbf295e | 2016-10-03 18:53:11 | [diff] [blame] | 1275 | s->pce = aac_pce_configs[i]; |
| 1276 | s->reorder_map = s->pce.reorder_map; |
| 1277 | s->chan_map = s->pce.config_map; |
| 1278 | } else { |
| 1279 | s->reorder_map = aac_chan_maps[s->channels - 1]; |
| 1280 | s->chan_map = aac_chan_configs[s->channels - 1]; |
| 1281 | } |
| 1282 | |
Rostislav Pehlivanov | f0a8212 | 2016-02-12 18:34:18 | [diff] [blame] | 1283 | if (!avctx->bit_rate) { |
| 1284 | for (i = 1; i <= s->chan_map[0]; i++) { |
| 1285 | avctx->bit_rate += s->chan_map[i] == TYPE_CPE ? 128000 : /* Pair */ |
| 1286 | s->chan_map[i] == TYPE_LFE ? 16000 : /* LFE */ |
| 1287 | 69000 ; /* SCE */ |
| 1288 | } |
| 1289 | } |
| 1290 | |
| 1291 | /* Samplerate */ |
Nathan Caldwell | 5310704 | 2011-12-15 02:50:23 | [diff] [blame] | 1292 | for (i = 0; i < 16; i++) |
Andreas Rheinhardt | 49bf945 | 2021-02-10 18:37:37 | [diff] [blame] | 1293 | if (avctx->sample_rate == ff_mpeg4audio_sample_rates[i]) |
Nathan Caldwell | 5310704 | 2011-12-15 02:50:23 | [diff] [blame] | 1294 | break; |
Rostislav Pehlivanov | 0f4334d | 2015-10-12 15:50:10 | [diff] [blame] | 1295 | s->samplerate_index = i; |
Rostislav Pehlivanov | 0f4334d | 2015-10-12 15:50:10 | [diff] [blame] | 1296 | ERROR_IF(s->samplerate_index == 16 || |
| 1297 | s->samplerate_index >= ff_aac_swb_size_1024_len || |
| 1298 | s->samplerate_index >= ff_aac_swb_size_128_len, |
Nathan Caldwell | 5310704 | 2011-12-15 02:50:23 | [diff] [blame] | 1299 | "Unsupported sample rate %d\n", avctx->sample_rate); |
Rostislav Pehlivanov | f0a8212 | 2016-02-12 18:34:18 | [diff] [blame] | 1300 | |
| 1301 | /* Bitrate limiting */ |
Claudio Freire | 6dbbb98 | 2015-03-06 07:05:32 | [diff] [blame] | 1302 | WARN_IF(1024.0 * avctx->bit_rate / avctx->sample_rate > 6144 * s->channels, |
Hendrik Leppkes | 92186f2 | 2015-12-08 08:59:45 | [diff] [blame] | 1303 | "Too many bits %f > %d per frame requested, clamping to max\n", |
Luca Barbato | b805482 | 2015-11-28 11:28:21 | [diff] [blame] | 1304 | 1024.0 * avctx->bit_rate / avctx->sample_rate, |
| 1305 | 6144 * s->channels); |
Rostislav Pehlivanov | f0a8212 | 2016-02-12 18:34:18 | [diff] [blame] | 1306 | avctx->bit_rate = (int64_t)FFMIN(6144 * s->channels / 1024.0 * avctx->sample_rate, |
| 1307 | avctx->bit_rate); |
Claudio Freire | 6dbbb98 | 2015-03-06 07:05:32 | [diff] [blame] | 1308 | |
Rostislav Pehlivanov | f0a8212 | 2016-02-12 18:34:18 | [diff] [blame] | 1309 | /* Profile and option setting */ |
Andreas Rheinhardt | 8238bc0 | 2023-09-02 12:57:41 | [diff] [blame^] | 1310 | avctx->profile = avctx->profile == AV_PROFILE_UNKNOWN ? AV_PROFILE_AAC_LOW : |
Rostislav Pehlivanov | f0a8212 | 2016-02-12 18:34:18 | [diff] [blame] | 1311 | avctx->profile; |
Rostislav Pehlivanov | e9299df | 2015-10-17 02:15:44 | [diff] [blame] | 1312 | for (i = 0; i < FF_ARRAY_ELEMS(aacenc_profiles); i++) |
| 1313 | if (avctx->profile == aacenc_profiles[i]) |
Rostislav Pehlivanov | 0f4334d | 2015-10-12 15:50:10 | [diff] [blame] | 1314 | break; |
Andreas Rheinhardt | 8238bc0 | 2023-09-02 12:57:41 | [diff] [blame^] | 1315 | if (avctx->profile == AV_PROFILE_MPEG2_AAC_LOW) { |
| 1316 | avctx->profile = AV_PROFILE_AAC_LOW; |
Rostislav Pehlivanov | e9299df | 2015-10-17 02:15:44 | [diff] [blame] | 1317 | ERROR_IF(s->options.pred, |
| 1318 | "Main prediction unavailable in the \"mpeg2_aac_low\" profile\n"); |
| 1319 | ERROR_IF(s->options.ltp, |
| 1320 | "LTP prediction unavailable in the \"mpeg2_aac_low\" profile\n"); |
| 1321 | WARN_IF(s->options.pns, |
| 1322 | "PNS unavailable in the \"mpeg2_aac_low\" profile, turning off\n"); |
| 1323 | s->options.pns = 0; |
Andreas Rheinhardt | 8238bc0 | 2023-09-02 12:57:41 | [diff] [blame^] | 1324 | } else if (avctx->profile == AV_PROFILE_AAC_LTP) { |
Rostislav Pehlivanov | e9299df | 2015-10-17 02:15:44 | [diff] [blame] | 1325 | s->options.ltp = 1; |
| 1326 | ERROR_IF(s->options.pred, |
| 1327 | "Main prediction unavailable in the \"aac_ltp\" profile\n"); |
Andreas Rheinhardt | 8238bc0 | 2023-09-02 12:57:41 | [diff] [blame^] | 1328 | } else if (avctx->profile == AV_PROFILE_AAC_MAIN) { |
Rostislav Pehlivanov | e9299df | 2015-10-17 02:15:44 | [diff] [blame] | 1329 | s->options.pred = 1; |
| 1330 | ERROR_IF(s->options.ltp, |
| 1331 | "LTP prediction unavailable in the \"aac_main\" profile\n"); |
| 1332 | } else if (s->options.ltp) { |
Andreas Rheinhardt | 8238bc0 | 2023-09-02 12:57:41 | [diff] [blame^] | 1333 | avctx->profile = AV_PROFILE_AAC_LTP; |
Rostislav Pehlivanov | e9299df | 2015-10-17 02:15:44 | [diff] [blame] | 1334 | WARN_IF(1, |
| 1335 | "Chainging profile to \"aac_ltp\"\n"); |
| 1336 | ERROR_IF(s->options.pred, |
| 1337 | "Main prediction unavailable in the \"aac_ltp\" profile\n"); |
| 1338 | } else if (s->options.pred) { |
Andreas Rheinhardt | 8238bc0 | 2023-09-02 12:57:41 | [diff] [blame^] | 1339 | avctx->profile = AV_PROFILE_AAC_MAIN; |
Rostislav Pehlivanov | e9299df | 2015-10-17 02:15:44 | [diff] [blame] | 1340 | WARN_IF(1, |
| 1341 | "Chainging profile to \"aac_main\"\n"); |
Rostislav Pehlivanov | 3112501 | 2015-12-05 18:43:17 | [diff] [blame] | 1342 | ERROR_IF(s->options.ltp, |
Rostislav Pehlivanov | e9299df | 2015-10-17 02:15:44 | [diff] [blame] | 1343 | "LTP prediction unavailable in the \"aac_main\" profile\n"); |
Rostislav Pehlivanov | 0f4334d | 2015-10-12 15:50:10 | [diff] [blame] | 1344 | } |
Rostislav Pehlivanov | e9299df | 2015-10-17 02:15:44 | [diff] [blame] | 1345 | s->profile = avctx->profile; |
Rostislav Pehlivanov | 0f4334d | 2015-10-12 15:50:10 | [diff] [blame] | 1346 | |
Rostislav Pehlivanov | f0a8212 | 2016-02-12 18:34:18 | [diff] [blame] | 1347 | /* Coder limitations */ |
| 1348 | s->coder = &ff_aac_coders[s->options.coder]; |
Rostislav Pehlivanov | fb0abb3 | 2016-08-06 23:49:34 | [diff] [blame] | 1349 | if (s->options.coder == AAC_CODER_ANMR) { |
Rostislav Pehlivanov | b270ec9 | 2015-12-05 14:41:41 | [diff] [blame] | 1350 | ERROR_IF(avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL, |
Rostislav Pehlivanov | 6612d04 | 2016-08-13 16:34:58 | [diff] [blame] | 1351 | "The ANMR coder is considered experimental, add -strict -2 to enable!\n"); |
Rostislav Pehlivanov | 8ffe1cb | 2015-09-01 11:07:00 | [diff] [blame] | 1352 | s->options.intensity_stereo = 0; |
| 1353 | s->options.pns = 0; |
| 1354 | } |
Rostislav Pehlivanov | a72b1ea | 2016-01-20 16:49:55 | [diff] [blame] | 1355 | ERROR_IF(s->options.ltp && avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL, |
| 1356 | "The LPT profile requires experimental compliance, add -strict -2 to enable!\n"); |
| 1357 | |
Rostislav Pehlivanov | 0fe0e21 | 2016-02-13 12:23:22 | [diff] [blame] | 1358 | /* M/S introduces horrible artifacts with multichannel files, this is temporary */ |
| 1359 | if (s->channels > 3) |
| 1360 | s->options.mid_side = 0; |
| 1361 | |
Vittorio Giovara | 971099f | 2014-12-17 13:53:43 | [diff] [blame] | 1362 | if ((ret = dsp_init(avctx, s)) < 0) |
Limin Wang | ebf2a8a | 2020-06-01 14:48:28 | [diff] [blame] | 1363 | return ret; |
Nathan Caldwell | 5310704 | 2011-12-15 02:50:23 | [diff] [blame] | 1364 | |
Vittorio Giovara | 971099f | 2014-12-17 13:53:43 | [diff] [blame] | 1365 | if ((ret = alloc_buffers(avctx, s)) < 0) |
Limin Wang | ebf2a8a | 2020-06-01 14:48:28 | [diff] [blame] | 1366 | return ret; |
Nathan Caldwell | 5310704 | 2011-12-15 02:50:23 | [diff] [blame] | 1367 | |
Rostislav Pehlivanov | 7b7775a | 2017-11-09 04:30:07 | [diff] [blame] | 1368 | if ((ret = put_audio_specific_config(avctx))) |
Limin Wang | ebf2a8a | 2020-06-01 14:48:28 | [diff] [blame] | 1369 | return ret; |
Nathan Caldwell | 5310704 | 2011-12-15 02:50:23 | [diff] [blame] | 1370 | |
Rostislav Pehlivanov | 0f4334d | 2015-10-12 15:50:10 | [diff] [blame] | 1371 | sizes[0] = ff_aac_swb_size_1024[s->samplerate_index]; |
| 1372 | sizes[1] = ff_aac_swb_size_128[s->samplerate_index]; |
| 1373 | lengths[0] = ff_aac_num_swb_1024[s->samplerate_index]; |
| 1374 | lengths[1] = ff_aac_num_swb_128[s->samplerate_index]; |
Nathan Caldwell | 5310704 | 2011-12-15 02:50:23 | [diff] [blame] | 1375 | for (i = 0; i < s->chan_map[0]; i++) |
| 1376 | grouping[i] = s->chan_map[i + 1] == TYPE_CPE; |
Vittorio Giovara | 971099f | 2014-12-17 13:53:43 | [diff] [blame] | 1377 | if ((ret = ff_psy_init(&s->psy, avctx, 2, sizes, lengths, |
| 1378 | s->chan_map[0], grouping)) < 0) |
Limin Wang | ebf2a8a | 2020-06-01 14:48:28 | [diff] [blame] | 1379 | return ret; |
Nathan Caldwell | 5310704 | 2011-12-15 02:50:23 | [diff] [blame] | 1380 | s->psypp = ff_psy_preprocess_init(avctx); |
Rostislav Pehlivanov | d09f9c4 | 2015-09-01 05:40:12 | [diff] [blame] | 1381 | ff_lpc_init(&s->lpc, 2*avctx->frame_size, TNS_MAX_ORDER, FF_LPC_TYPE_LEVINSON); |
Rostislav Pehlivanov | 230178d | 2016-10-08 00:47:04 | [diff] [blame] | 1382 | s->random_state = 0x1f2e3d4c; |
Nathan Caldwell | 5310704 | 2011-12-15 02:50:23 | [diff] [blame] | 1383 | |
Rostislav Pehlivanov | d2ae5f7 | 2016-10-08 14:59:14 | [diff] [blame] | 1384 | s->abs_pow34 = abs_pow34_v; |
| 1385 | s->quant_bands = quantize_bands; |
| 1386 | |
Andreas Rheinhardt | 40e6575 | 2022-06-12 03:51:12 | [diff] [blame] | 1387 | #if ARCH_X86 |
| 1388 | ff_aac_dsp_init_x86(s); |
| 1389 | #endif |
Rostislav Pehlivanov | d2ae5f7 | 2016-10-08 14:59:14 | [diff] [blame] | 1390 | |
Andreas Rheinhardt | 40e6575 | 2022-06-12 03:51:12 | [diff] [blame] | 1391 | #if HAVE_MIPSDSP |
| 1392 | ff_aac_coder_init_mips(s); |
| 1393 | #endif |
Bojan Zivkovic | 26f3924 | 2013-03-06 13:55:05 | [diff] [blame] | 1394 | |
Justin Ruggles | ad95307 | 2012-01-28 17:28:01 | [diff] [blame] | 1395 | ff_af_queue_init(avctx, &s->afq); |
Andreas Rheinhardt | 195d8ce | 2020-11-19 15:54:45 | [diff] [blame] | 1396 | ff_aac_tableinit(); |
Justin Ruggles | ad95307 | 2012-01-28 17:28:01 | [diff] [blame] | 1397 | |
Nathan Caldwell | 5310704 | 2011-12-15 02:50:23 | [diff] [blame] | 1398 | return 0; |
Nathan Caldwell | 5310704 | 2011-12-15 02:50:23 | [diff] [blame] | 1399 | } |
| 1400 | |
Nathan Caldwell | cc9947f | 2011-06-01 05:38:09 | [diff] [blame] | 1401 | #define AACENC_FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM |
| 1402 | static const AVOption aacenc_options[] = { |
Lynne | 660d1d8 | 2021-05-21 15:39:36 | [diff] [blame] | 1403 | {"aac_coder", "Coding algorithm", offsetof(AACEncContext, options.coder), AV_OPT_TYPE_INT, {.i64 = AAC_CODER_TWOLOOP}, 0, AAC_CODER_NB-1, AACENC_FLAGS, "coder"}, |
Rostislav Pehlivanov | 0f4334d | 2015-10-12 15:50:10 | [diff] [blame] | 1404 | {"anmr", "ANMR method", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_ANMR}, INT_MIN, INT_MAX, AACENC_FLAGS, "coder"}, |
| 1405 | {"twoloop", "Two loop searching method", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_TWOLOOP}, INT_MIN, INT_MAX, AACENC_FLAGS, "coder"}, |
Rostislav Pehlivanov | fcb681a | 2018-01-13 11:46:29 | [diff] [blame] | 1406 | {"fast", "Default fast search", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_FAST}, INT_MIN, INT_MAX, AACENC_FLAGS, "coder"}, |
Rostislav Pehlivanov | 83900c0 | 2015-10-17 01:20:13 | [diff] [blame] | 1407 | {"aac_ms", "Force M/S stereo coding", offsetof(AACEncContext, options.mid_side), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, AACENC_FLAGS}, |
Rostislav Pehlivanov | e9299df | 2015-10-17 02:15:44 | [diff] [blame] | 1408 | {"aac_is", "Intensity stereo coding", offsetof(AACEncContext, options.intensity_stereo), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AACENC_FLAGS}, |
| 1409 | {"aac_pns", "Perceptual noise substitution", offsetof(AACEncContext, options.pns), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AACENC_FLAGS}, |
Rostislav Pehlivanov | dfba1be | 2015-10-17 10:06:05 | [diff] [blame] | 1410 | {"aac_tns", "Temporal noise shaping", offsetof(AACEncContext, options.tns), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AACENC_FLAGS}, |
Rostislav Pehlivanov | e9299df | 2015-10-17 02:15:44 | [diff] [blame] | 1411 | {"aac_ltp", "Long term prediction", offsetof(AACEncContext, options.ltp), AV_OPT_TYPE_BOOL, {.i64 = 0}, -1, 1, AACENC_FLAGS}, |
| 1412 | {"aac_pred", "AAC-Main prediction", offsetof(AACEncContext, options.pred), AV_OPT_TYPE_BOOL, {.i64 = 0}, -1, 1, AACENC_FLAGS}, |
Rostislav Pehlivanov | fbf295e | 2016-10-03 18:53:11 | [diff] [blame] | 1413 | {"aac_pce", "Forces the use of PCEs", offsetof(AACEncContext, options.pce), AV_OPT_TYPE_BOOL, {.i64 = 0}, -1, 1, AACENC_FLAGS}, |
Marton Balint | aee036c | 2020-05-10 18:38:11 | [diff] [blame] | 1414 | FF_AAC_PROFILE_OPTS |
Nathan Caldwell | cc9947f | 2011-06-01 05:38:09 | [diff] [blame] | 1415 | {NULL} |
| 1416 | }; |
| 1417 | |
| 1418 | static const AVClass aacenc_class = { |
Diego Biurrun | 97cfe1d | 2017-06-10 14:45:06 | [diff] [blame] | 1419 | .class_name = "AAC encoder", |
| 1420 | .item_name = av_default_item_name, |
| 1421 | .option = aacenc_options, |
| 1422 | .version = LIBAVUTIL_VERSION_INT, |
Nathan Caldwell | cc9947f | 2011-06-01 05:38:09 | [diff] [blame] | 1423 | }; |
| 1424 | |
Andreas Rheinhardt | 5aabb25 | 2022-03-16 20:26:11 | [diff] [blame] | 1425 | static const FFCodecDefault aac_encode_defaults[] = { |
Rostislav Pehlivanov | f0a8212 | 2016-02-12 18:34:18 | [diff] [blame] | 1426 | { "b", "0" }, |
| 1427 | { NULL } |
| 1428 | }; |
| 1429 | |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 1430 | const FFCodec ff_aac_encoder = { |
| 1431 | .p.name = "aac", |
Andreas Rheinhardt | 48286d4 | 2022-08-29 11:38:02 | [diff] [blame] | 1432 | CODEC_LONG_NAME("AAC (Advanced Audio Coding)"), |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 1433 | .p.type = AVMEDIA_TYPE_AUDIO, |
| 1434 | .p.id = AV_CODEC_ID_AAC, |
Andreas Rheinhardt | a499b43 | 2021-05-11 18:52:13 | [diff] [blame] | 1435 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | |
| 1436 | AV_CODEC_CAP_SMALL_LAST_FRAME, |
Anton Khirnov | ec6402b | 2011-07-17 10:54:31 | [diff] [blame] | 1437 | .priv_data_size = sizeof(AACEncContext), |
| 1438 | .init = aac_encode_init, |
Andreas Rheinhardt | 4243da4 | 2022-03-30 21:28:24 | [diff] [blame] | 1439 | FF_CODEC_ENCODE_CB(aac_encode_frame), |
Anton Khirnov | ec6402b | 2011-07-17 10:54:31 | [diff] [blame] | 1440 | .close = aac_encode_end, |
Rostislav Pehlivanov | f0a8212 | 2016-02-12 18:34:18 | [diff] [blame] | 1441 | .defaults = aac_encode_defaults, |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 1442 | .p.supported_samplerates = ff_mpeg4audio_sample_rates, |
Andreas Rheinhardt | 21b23ce | 2022-07-09 22:05:45 | [diff] [blame] | 1443 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 1444 | .p.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP, |
Martin Storsjö | 00c3b67 | 2012-04-06 16:19:39 | [diff] [blame] | 1445 | AV_SAMPLE_FMT_NONE }, |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 1446 | .p.priv_class = &aacenc_class, |
Kostya Shishkov | c03d9d0 | 2008-08-14 05:52:29 | [diff] [blame] | 1447 | }; |