/ src / library / av_processing / AacTables.php
<?php
/**
 * SeekQuarry/Yioop --
 * Open Source Pure PHP Search Engine, Crawler, and Indexer
 *
 * Copyright (C) 2009 - 2026  Chris Pollett chris@pollett.org
 *
 * LICENSE:
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * END LICENSE
 *
 * @author Chris Pollett chris@pollett.org
 * @license https://www.gnu.org/licenses/ GPL3
 * @link https://www.seekquarry.com/
 * @copyright 2009 - 2026
 * @filesource
 */
namespace seekquarry\yioop\library\av_processing;

/**
 * AacTables the two code tables an AAC file is written with. Compressed sound
 * is written as codes of uneven length: a value that turns up often gets a
 * short code and a rare one a long code, so the whole comes to fewer bits than
 * fixed width numbers would. Which code stands for which value is fixed by the
 * standard rather than worked out for each file, so both sides already agree
 * and nothing has to be sent to explain them. The standard offers eleven tables
 * for the sound itself, each suited to a different range of values. Only the
 * widest is used here. It takes a pair of values at a time, each from nothing
 * to sixteen, and where a value reaches sixteen the rest of it is written
 * separately. Using one table rather than choosing the best of the eleven for
 * each run of bands costs a few percent in size and is work for later. The
 * second table is for the loudness settings, which are written as steps from
 * the setting before since they change slowly. The numbers here come from the
 * AAC standard, ISO/IEC 14496-3.
 */
class AacTables
{
    /**
     * PAIR_RANGE is how many values the widest sound table holds along each
     * side.
     */
    const PAIR_RANGE = 17;
    /**
     * ESCAPE_AT is the value at which a code stops carrying the whole number
     * and the rest is written separately.
     */
    const ESCAPE_AT = 16;
    /**
     * SOUND_TABLE is which of the eleven tables for sound this is.
     */
    const SOUND_TABLE = 11;
    /**
     * LOUDNESS_SPAN is how far a loudness setting may step from the one before.
     */
    const LOUDNESS_SPAN = 60;
    /**
     * SOUND_CODES is the codes for each pair of sound values.
     * @var array
     */
    const SOUND_CODES = [
        0, 6, 25, 61, 156, 198, 423, 912, 962, 991, 2022, 2035, 4091, 2028,
        4090, 4094, 910, 5, 1, 8, 20, 55, 66, 146, 175, 401, 421, 437, 926,
        960, 930, 973, 2006, 174, 23, 7, 9, 24, 57, 64, 142, 163, 184, 409,
        428, 449, 945, 918, 958, 970, 157, 60, 21, 22, 26, 59, 68, 145, 165,
        190, 406, 430, 441, 929, 913, 933, 981, 148, 154, 54, 56, 58, 65, 140,
        155, 176, 195, 414, 427, 444, 927, 911, 937, 975, 147, 191, 62, 63,
        67, 69, 158, 167, 185, 404, 418, 442, 451, 934, 935, 955, 980, 159,
        416, 143, 141, 144, 152, 166, 182, 196, 415, 431, 447, 921, 959, 948,
        969, 999, 168, 438, 171, 164, 170, 178, 194, 197, 408, 420, 440, 908,
        932, 964, 966, 989, 1000, 173, 943, 402, 189, 188, 398, 407, 410, 419,
        433, 909, 920, 951, 979, 977, 987, 2013, 180, 990, 425, 411, 412, 417,
        426, 429, 435, 907, 946, 952, 974, 993, 992, 2002, 2021, 183, 2019,
        443, 424, 422, 432, 434, 439, 923, 922, 954, 949, 982, 2007, 996,
        2008, 2026, 186, 2024, 928, 445, 436, 906, 452, 914, 938, 944, 956,
        983, 2004, 2012, 2011, 2005, 2032, 193, 2043, 968, 931, 917, 925, 940,
        942, 965, 984, 994, 998, 2020, 2023, 2016, 2025, 2039, 400, 2034, 915,
        446, 448, 916, 919, 941, 963, 961, 978, 2010, 2009, 2015, 2027, 2036,
        2042, 405, 2040, 957, 924, 939, 936, 947, 953, 976, 995, 997, 2018,
        2014, 2029, 2033, 2041, 2044, 403, 4093, 988, 950, 967, 972, 971, 985,
        986, 2003, 2017, 2030, 2031, 2037, 2038, 4092, 4095, 413, 450, 181,
        161, 150, 151, 149, 153, 160, 162, 172, 169, 177, 179, 187, 192, 399,
        4
    ];
    /**
     * SOUND_CODE_BITS is how many bits each of those codes takes.
     * @var array
     */
    const SOUND_CODE_BITS = [
        4, 5, 6, 7, 8, 8, 9, 10, 10, 10, 11, 11, 12, 11, 12, 12, 10, 5, 4, 5,
        6, 7, 7, 8, 8, 9, 9, 9, 10, 10, 10, 10, 11, 8, 6, 5, 5, 6, 7, 7, 8, 8,
        8, 9, 9, 9, 10, 10, 10, 10, 8, 7, 6, 6, 6, 7, 7, 8, 8, 8, 9, 9, 9, 10,
        10, 10, 10, 8, 8, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 10, 10, 10, 10, 8,
        8, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 8, 9, 8, 8, 8, 8,
        8, 8, 8, 9, 9, 9, 10, 10, 10, 10, 10, 8, 9, 8, 8, 8, 8, 8, 8, 9, 9, 9,
        10, 10, 10, 10, 10, 10, 8, 10, 9, 8, 8, 9, 9, 9, 9, 9, 10, 10, 10, 10,
        10, 10, 11, 8, 10, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 11,
        11, 8, 11, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 11, 10, 11, 11, 8,
        11, 10, 9, 9, 10, 9, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 8, 11,
        10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 9, 11, 10,
        9, 9, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 9, 11, 10, 10,
        10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 9, 12, 10, 10, 10,
        10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 12, 12, 9, 9, 8, 8, 8, 8, 8,
        8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 5
    ];
    /**
     * LOUDNESS_CODES is the codes for each step a loudness setting may take.
     * @var array
     */
    const LOUDNESS_CODES = [
        262120, 262118, 262119, 262117, 524277, 524273, 524269, 524278,
        524270, 524271, 524272, 524284, 524285, 524287, 524286, 524279,
        524280, 524283, 524281, 262116, 524282, 262115, 131055, 131056, 65525,
        131054, 65522, 65523, 65524, 65521, 32758, 32759, 16377, 16373, 16375,
        16371, 16374, 16370, 8183, 8181, 4089, 4087, 4086, 2041, 4084, 2040,
        1017, 1015, 1013, 504, 503, 250, 248, 246, 121, 58, 56, 26, 11, 4, 0,
        10, 12, 27, 57, 59, 120, 122, 247, 249, 502, 505, 1012, 1014, 1016,
        2037, 2036, 2038, 2039, 4085, 4088, 8180, 8182, 8184, 16376, 16372,
        65520, 32756, 65526, 32757, 262114, 524249, 524250, 524251, 524252,
        524253, 524254, 524248, 524242, 524243, 524244, 524245, 524246,
        524274, 524255, 524263, 524264, 524265, 524266, 524267, 524262,
        524256, 524257, 524258, 524259, 524260, 524261, 524247, 524268,
        524276, 524275
    ];
    /**
     * LOUDNESS_CODE_BITS is how many bits each of those codes takes.
     * @var array
     */
    const LOUDNESS_CODE_BITS = [
        18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
        19, 19, 18, 19, 18, 17, 17, 16, 17, 16, 16, 16, 16, 15, 15, 14, 14,
        14, 14, 14, 14, 13, 13, 12, 12, 12, 11, 12, 11, 10, 10, 10, 9, 9, 8,
        8, 8, 7, 6, 6, 5, 4, 3, 1, 4, 4, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10,
        10, 11, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 16, 15, 16, 15, 18,
        19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
        19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19
    ];
}
X