[14963] | 1 | /* |
---|
| 2 | * The simplest mpeg audio layer 2 encoder |
---|
| 3 | * Copyright (c) 2000, 2001 Fabrice Bellard. |
---|
| 4 | * |
---|
| 5 | * This routines are normaly part of FFmpeg and had been isolated |
---|
| 6 | * for use in DreamDVD by Seddi. |
---|
| 7 | * |
---|
| 8 | * FFmpeg is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU Lesser General Public |
---|
| 10 | * License as published by the Free Software Foundation; either |
---|
| 11 | * version 2.1 of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * FFmpeg is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 16 | * Lesser General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU Lesser General Public |
---|
| 19 | * License along with FFmpeg; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 21 | * |
---|
| 22 | * part of libdreamdvd |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | #ifndef __MPEGAUDIO_ENC_H__ |
---|
| 26 | |
---|
| 27 | #define __MPEGAUDIO_ENC_H__ |
---|
| 28 | |
---|
| 29 | #include <stdint.h> |
---|
| 30 | #include <inttypes.h> |
---|
| 31 | #include <stdlib.h> |
---|
| 32 | #include <stdio.h> |
---|
| 33 | #include <string.h> |
---|
| 34 | #include <assert.h> |
---|
| 35 | #include <byteswap.h> |
---|
| 36 | #include <math.h> |
---|
| 37 | |
---|
| 38 | #define FLOOR(a) ((int)(a) - ((a) < 0 && (a) != (int)(a))) |
---|
| 39 | #define SQRT2 1.41421356237309514547 |
---|
| 40 | #define FRAC_BITS 15 /* fractional bits for sb_samples and dct */ |
---|
| 41 | #define WFRAC_BITS 14 /* fractional bits for window */ |
---|
| 42 | #define FRAC_ONE (1 << FRAC_BITS) |
---|
| 43 | #define MUL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS) |
---|
| 44 | #define FIX(a) ((int)((a) * FRAC_ONE)) |
---|
| 45 | #define WSHIFT (WFRAC_BITS + 15 - FRAC_BITS) |
---|
| 46 | #define MPA_MAX_CHANNELS 2 |
---|
| 47 | |
---|
| 48 | #define SBLIMIT 32 /* number of subbands */ |
---|
| 49 | /* max frame size, in samples */ |
---|
| 50 | #define MPA_FRAME_SIZE 1152 |
---|
| 51 | |
---|
| 52 | /* max compressed frame size */ |
---|
| 53 | #define MPA_MAX_CODED_FRAME_SIZE 1792 |
---|
| 54 | #define SAMPLES_BUF_SIZE 4096 |
---|
| 55 | #define NB_CHANNELS 2 |
---|
| 56 | #define MPA_MAX_CODED_FRAME_SIZE 1792 |
---|
| 57 | #define MPA_STEREO 0 |
---|
| 58 | #define MPA_MONO 3 |
---|
| 59 | |
---|
| 60 | static const int ddvd_mpa_costab32[30] = { |
---|
| 61 | FIX(0.54119610014619701222), |
---|
| 62 | FIX(1.3065629648763763537), |
---|
| 63 | |
---|
| 64 | FIX(0.50979557910415917998), |
---|
| 65 | FIX(2.5629154477415054814), |
---|
| 66 | FIX(0.89997622313641556513), |
---|
| 67 | FIX(0.60134488693504528634), |
---|
| 68 | |
---|
| 69 | FIX(0.5024192861881556782), |
---|
| 70 | FIX(5.1011486186891552563), |
---|
| 71 | FIX(0.78815462345125020249), |
---|
| 72 | FIX(0.64682178335999007679), |
---|
| 73 | FIX(0.56694403481635768927), |
---|
| 74 | FIX(1.0606776859903470633), |
---|
| 75 | FIX(1.7224470982383341955), |
---|
| 76 | FIX(0.52249861493968885462), |
---|
| 77 | |
---|
| 78 | FIX(10.19000812354803287), |
---|
| 79 | FIX(0.674808341455005678), |
---|
| 80 | FIX(1.1694399334328846596), |
---|
| 81 | FIX(0.53104259108978413284), |
---|
| 82 | FIX(2.0577810099534108446), |
---|
| 83 | FIX(0.58293496820613388554), |
---|
| 84 | FIX(0.83934964541552681272), |
---|
| 85 | FIX(0.50547095989754364798), |
---|
| 86 | FIX(3.4076084184687189804), |
---|
| 87 | FIX(0.62250412303566482475), |
---|
| 88 | FIX(0.97256823786196078263), |
---|
| 89 | FIX(0.51544730992262455249), |
---|
| 90 | FIX(1.4841646163141661852), |
---|
| 91 | FIX(0.5531038960344445421), |
---|
| 92 | FIX(0.74453627100229857749), |
---|
| 93 | FIX(0.5006029982351962726), |
---|
| 94 | }; |
---|
| 95 | |
---|
| 96 | static const int ddvd_mpa_bitinv32[32] = { |
---|
| 97 | 0, 16, 8, 24, 4, 20, 12, 28, |
---|
| 98 | 2, 18, 10, 26, 6, 22, 14, 30, |
---|
| 99 | 1, 17, 9, 25, 5, 21, 13, 29, |
---|
| 100 | 3, 19, 11, 27, 7, 23, 15, 31 |
---|
| 101 | }; |
---|
| 102 | |
---|
| 103 | |
---|
| 104 | static int16_t ddvd_mpa_filter_bank[512]; |
---|
| 105 | |
---|
| 106 | const uint16_t ddvd_mpa_ff_mpa_freq_tab[3] = { 44100, 48000, 32000 }; |
---|
| 107 | |
---|
| 108 | const int ddvd_mpa_ff_mpa_quant_steps[17] = { |
---|
| 109 | 3, 5, 7, 9, 15, |
---|
| 110 | 31, 63, 127, 255, 511, |
---|
| 111 | 1023, 2047, 4095, 8191, 16383, |
---|
| 112 | 32767, 65535 |
---|
| 113 | }; |
---|
| 114 | |
---|
| 115 | const uint16_t ddvd_mpa_ff_mpa_bitrate_tab[2][3][15] = { |
---|
| 116 | { {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448 }, |
---|
| 117 | {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384 }, |
---|
| 118 | {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320 } }, |
---|
| 119 | { {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256}, |
---|
| 120 | {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}, |
---|
| 121 | {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160} |
---|
| 122 | } |
---|
| 123 | }; |
---|
| 124 | |
---|
| 125 | int ddvd_mpa_ff_mpa_l2_select_table(int bitrate, int nb_channels, int freq, int lsf) |
---|
| 126 | { |
---|
| 127 | int ch_bitrate, table; |
---|
| 128 | |
---|
| 129 | ch_bitrate = bitrate / nb_channels; |
---|
| 130 | if (!lsf) { |
---|
| 131 | if ((freq == 48000 && ch_bitrate >= 56) || |
---|
| 132 | (ch_bitrate >= 56 && ch_bitrate <= 80)) |
---|
| 133 | table = 0; |
---|
| 134 | else if (freq != 48000 && ch_bitrate >= 96) |
---|
| 135 | table = 1; |
---|
| 136 | else if (freq != 32000 && ch_bitrate <= 48) |
---|
| 137 | table = 2; |
---|
| 138 | else |
---|
| 139 | table = 3; |
---|
| 140 | } else { |
---|
| 141 | table = 4; |
---|
| 142 | } |
---|
| 143 | return table; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | const int ddvd_mpa_ff_mpa_sblimit_table[5] = { 27 , 30 , 8, 12 , 30 }; |
---|
| 147 | |
---|
| 148 | /* encoding tables which give the quantization index. Note how it is |
---|
| 149 | possible to store them efficiently ! */ |
---|
| 150 | static const unsigned char ddvd_mpa_alloc_table_0[] = { |
---|
| 151 | 4, 0, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, |
---|
| 152 | 4, 0, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, |
---|
| 153 | 4, 0, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, |
---|
| 154 | 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16, |
---|
| 155 | 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16, |
---|
| 156 | 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16, |
---|
| 157 | 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16, |
---|
| 158 | 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16, |
---|
| 159 | 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16, |
---|
| 160 | 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16, |
---|
| 161 | 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16, |
---|
| 162 | 3, 0, 1, 2, 3, 4, 5, 16, |
---|
| 163 | 3, 0, 1, 2, 3, 4, 5, 16, |
---|
| 164 | 3, 0, 1, 2, 3, 4, 5, 16, |
---|
| 165 | 3, 0, 1, 2, 3, 4, 5, 16, |
---|
| 166 | 3, 0, 1, 2, 3, 4, 5, 16, |
---|
| 167 | 3, 0, 1, 2, 3, 4, 5, 16, |
---|
| 168 | 3, 0, 1, 2, 3, 4, 5, 16, |
---|
| 169 | 3, 0, 1, 2, 3, 4, 5, 16, |
---|
| 170 | 3, 0, 1, 2, 3, 4, 5, 16, |
---|
| 171 | 3, 0, 1, 2, 3, 4, 5, 16, |
---|
| 172 | 3, 0, 1, 2, 3, 4, 5, 16, |
---|
| 173 | 3, 0, 1, 2, 3, 4, 5, 16, |
---|
| 174 | 2, 0, 1, 16, |
---|
| 175 | 2, 0, 1, 16, |
---|
| 176 | 2, 0, 1, 16, |
---|
| 177 | 2, 0, 1, 16, |
---|
| 178 | }; |
---|
| 179 | |
---|
| 180 | static const unsigned char ddvd_mpa_alloc_table_1[] = { |
---|
| 181 | 4, 0, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, |
---|
| 182 | 4, 0, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, |
---|
| 183 | 4, 0, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, |
---|
| 184 | 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16, |
---|
| 185 | 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16, |
---|
| 186 | 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16, |
---|
| 187 | 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16, |
---|
| 188 | 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16, |
---|
| 189 | 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16, |
---|
| 190 | 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16, |
---|
| 191 | 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16, |
---|
| 192 | 3, 0, 1, 2, 3, 4, 5, 16, |
---|
| 193 | 3, 0, 1, 2, 3, 4, 5, 16, |
---|
| 194 | 3, 0, 1, 2, 3, 4, 5, 16, |
---|
| 195 | 3, 0, 1, 2, 3, 4, 5, 16, |
---|
| 196 | 3, 0, 1, 2, 3, 4, 5, 16, |
---|
| 197 | 3, 0, 1, 2, 3, 4, 5, 16, |
---|
| 198 | 3, 0, 1, 2, 3, 4, 5, 16, |
---|
| 199 | 3, 0, 1, 2, 3, 4, 5, 16, |
---|
| 200 | 3, 0, 1, 2, 3, 4, 5, 16, |
---|
| 201 | 3, 0, 1, 2, 3, 4, 5, 16, |
---|
| 202 | 3, 0, 1, 2, 3, 4, 5, 16, |
---|
| 203 | 3, 0, 1, 2, 3, 4, 5, 16, |
---|
| 204 | 2, 0, 1, 16, |
---|
| 205 | 2, 0, 1, 16, |
---|
| 206 | 2, 0, 1, 16, |
---|
| 207 | 2, 0, 1, 16, |
---|
| 208 | 2, 0, 1, 16, |
---|
| 209 | 2, 0, 1, 16, |
---|
| 210 | 2, 0, 1, 16, |
---|
| 211 | }; |
---|
| 212 | |
---|
| 213 | static const unsigned char ddvd_mpa_alloc_table_2[] = { |
---|
| 214 | 4, 0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, |
---|
| 215 | 4, 0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, |
---|
| 216 | 3, 0, 1, 3, 4, 5, 6, 7, |
---|
| 217 | 3, 0, 1, 3, 4, 5, 6, 7, |
---|
| 218 | 3, 0, 1, 3, 4, 5, 6, 7, |
---|
| 219 | 3, 0, 1, 3, 4, 5, 6, 7, |
---|
| 220 | 3, 0, 1, 3, 4, 5, 6, 7, |
---|
| 221 | 3, 0, 1, 3, 4, 5, 6, 7, |
---|
| 222 | }; |
---|
| 223 | |
---|
| 224 | static const unsigned char ddvd_mpa_alloc_table_3[] = { |
---|
| 225 | 4, 0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, |
---|
| 226 | 4, 0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, |
---|
| 227 | 3, 0, 1, 3, 4, 5, 6, 7, |
---|
| 228 | 3, 0, 1, 3, 4, 5, 6, 7, |
---|
| 229 | 3, 0, 1, 3, 4, 5, 6, 7, |
---|
| 230 | 3, 0, 1, 3, 4, 5, 6, 7, |
---|
| 231 | 3, 0, 1, 3, 4, 5, 6, 7, |
---|
| 232 | 3, 0, 1, 3, 4, 5, 6, 7, |
---|
| 233 | 3, 0, 1, 3, 4, 5, 6, 7, |
---|
| 234 | 3, 0, 1, 3, 4, 5, 6, 7, |
---|
| 235 | 3, 0, 1, 3, 4, 5, 6, 7, |
---|
| 236 | 3, 0, 1, 3, 4, 5, 6, 7, |
---|
| 237 | }; |
---|
| 238 | |
---|
| 239 | static const unsigned char ddvd_mpa_alloc_table_4[] = { |
---|
| 240 | 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
---|
| 241 | 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
---|
| 242 | 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
---|
| 243 | 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
---|
| 244 | 3, 0, 1, 3, 4, 5, 6, 7, |
---|
| 245 | 3, 0, 1, 3, 4, 5, 6, 7, |
---|
| 246 | 3, 0, 1, 3, 4, 5, 6, 7, |
---|
| 247 | 3, 0, 1, 3, 4, 5, 6, 7, |
---|
| 248 | 3, 0, 1, 3, 4, 5, 6, 7, |
---|
| 249 | 3, 0, 1, 3, 4, 5, 6, 7, |
---|
| 250 | 3, 0, 1, 3, 4, 5, 6, 7, |
---|
| 251 | 2, 0, 1, 3, |
---|
| 252 | 2, 0, 1, 3, |
---|
| 253 | 2, 0, 1, 3, |
---|
| 254 | 2, 0, 1, 3, |
---|
| 255 | 2, 0, 1, 3, |
---|
| 256 | 2, 0, 1, 3, |
---|
| 257 | 2, 0, 1, 3, |
---|
| 258 | 2, 0, 1, 3, |
---|
| 259 | 2, 0, 1, 3, |
---|
| 260 | 2, 0, 1, 3, |
---|
| 261 | 2, 0, 1, 3, |
---|
| 262 | 2, 0, 1, 3, |
---|
| 263 | 2, 0, 1, 3, |
---|
| 264 | 2, 0, 1, 3, |
---|
| 265 | 2, 0, 1, 3, |
---|
| 266 | 2, 0, 1, 3, |
---|
| 267 | 2, 0, 1, 3, |
---|
| 268 | 2, 0, 1, 3, |
---|
| 269 | 2, 0, 1, 3, |
---|
| 270 | }; |
---|
| 271 | |
---|
| 272 | const unsigned char *ddvd_mpa_ff_mpa_alloc_tables[5] = |
---|
| 273 | { ddvd_mpa_alloc_table_0, ddvd_mpa_alloc_table_1, ddvd_mpa_alloc_table_2, ddvd_mpa_alloc_table_3, ddvd_mpa_alloc_table_4, }; |
---|
| 274 | |
---|
| 275 | |
---|
| 276 | const int32_t ddvd_mpa_ff_mpa_enwindow[257] = { |
---|
| 277 | 0, -1, -1, -1, -1, -1, -1, -2, |
---|
| 278 | -2, -2, -2, -3, -3, -4, -4, -5, |
---|
| 279 | -5, -6, -7, -7, -8, -9, -10, -11, |
---|
| 280 | -13, -14, -16, -17, -19, -21, -24, -26, |
---|
| 281 | -29, -31, -35, -38, -41, -45, -49, -53, |
---|
| 282 | -58, -63, -68, -73, -79, -85, -91, -97, |
---|
| 283 | -104, -111, -117, -125, -132, -139, -147, -154, |
---|
| 284 | -161, -169, -176, -183, -190, -196, -202, -208, |
---|
| 285 | 213, 218, 222, 225, 227, 228, 228, 227, |
---|
| 286 | 224, 221, 215, 208, 200, 189, 177, 163, |
---|
| 287 | 146, 127, 106, 83, 57, 29, -2, -36, |
---|
| 288 | -72, -111, -153, -197, -244, -294, -347, -401, |
---|
| 289 | -459, -519, -581, -645, -711, -779, -848, -919, |
---|
| 290 | -991, -1064, -1137, -1210, -1283, -1356, -1428, -1498, |
---|
| 291 | -1567, -1634, -1698, -1759, -1817, -1870, -1919, -1962, |
---|
| 292 | -2001, -2032, -2057, -2075, -2085, -2087, -2080, -2063, |
---|
| 293 | 2037, 2000, 1952, 1893, 1822, 1739, 1644, 1535, |
---|
| 294 | 1414, 1280, 1131, 970, 794, 605, 402, 185, |
---|
| 295 | -45, -288, -545, -814, -1095, -1388, -1692, -2006, |
---|
| 296 | -2330, -2663, -3004, -3351, -3705, -4063, -4425, -4788, |
---|
| 297 | -5153, -5517, -5879, -6237, -6589, -6935, -7271, -7597, |
---|
| 298 | -7910, -8209, -8491, -8755, -8998, -9219, -9416, -9585, |
---|
| 299 | -9727, -9838, -9916, -9959, -9966, -9935, -9863, -9750, |
---|
| 300 | -9592, -9389, -9139, -8840, -8492, -8092, -7640, -7134, |
---|
| 301 | 6574, 5959, 5288, 4561, 3776, 2935, 2037, 1082, |
---|
| 302 | 70, -998, -2122, -3300, -4533, -5818, -7154, -8540, |
---|
| 303 | -9975,-11455,-12980,-14548,-16155,-17799,-19478,-21189, |
---|
| 304 | -22929,-24694,-26482,-28289,-30112,-31947,-33791,-35640, |
---|
| 305 | -37489,-39336,-41176,-43006,-44821,-46617,-48390,-50137, |
---|
| 306 | -51853,-53534,-55178,-56778,-58333,-59838,-61289,-62684, |
---|
| 307 | -64019,-65290,-66494,-67629,-68692,-69679,-70590,-71420, |
---|
| 308 | -72169,-72835,-73415,-73908,-74313,-74630,-74856,-74992, |
---|
| 309 | 75038, |
---|
| 310 | }; |
---|
| 311 | |
---|
| 312 | |
---|
| 313 | static int ddvd_mpa_scale_factor_table[64]; |
---|
| 314 | static unsigned char ddvd_mpa_scale_diff_table[128]; |
---|
| 315 | static int8_t ddvd_mpa_scale_factor_shift[64]; |
---|
| 316 | static unsigned short ddvd_mpa_scale_factor_mult[64]; |
---|
| 317 | |
---|
| 318 | const uint8_t ddvd_mpa_ff_log2_tab[256]={ |
---|
| 319 | 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4, |
---|
| 320 | 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, |
---|
| 321 | 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, |
---|
| 322 | 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, |
---|
| 323 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, |
---|
| 324 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, |
---|
| 325 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, |
---|
| 326 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7 |
---|
| 327 | }; |
---|
| 328 | |
---|
| 329 | static inline int ddvd_mpa_av_log2(unsigned int v) |
---|
| 330 | { |
---|
| 331 | int n; |
---|
| 332 | |
---|
| 333 | n = 0; |
---|
| 334 | if (v & 0xffff0000) { |
---|
| 335 | v >>= 16; |
---|
| 336 | n += 16; |
---|
| 337 | } |
---|
| 338 | if (v & 0xff00) { |
---|
| 339 | v >>= 8; |
---|
| 340 | n += 8; |
---|
| 341 | } |
---|
| 342 | n += ddvd_mpa_ff_log2_tab[v]; |
---|
| 343 | |
---|
| 344 | return n; |
---|
| 345 | } |
---|
| 346 | |
---|
| 347 | /* fixed psycho acoustic model. Values of SNR taken from the 'toolame' |
---|
| 348 | project */ |
---|
| 349 | static const float ddvd_mpa_fixed_smr[SBLIMIT] = { |
---|
| 350 | 30, 17, 16, 10, 3, 12, 8, 2.5, |
---|
| 351 | 5, 5, 6, 6, 5, 6, 10, 6, |
---|
| 352 | -4, -10, -21, -30, -42, -55, -68, -75, |
---|
| 353 | -75, -75, -75, -75, -91, -107, -110, -108 |
---|
| 354 | }; |
---|
| 355 | |
---|
| 356 | #define SB_NOTALLOCATED 0 |
---|
| 357 | #define SB_ALLOCATED 1 |
---|
| 358 | #define SB_NOMORE 2 |
---|
| 359 | |
---|
| 360 | |
---|
| 361 | |
---|
| 362 | /* total number of bits per allocation group */ |
---|
| 363 | static unsigned short ddvd_mpa_total_quant_bits[17]; |
---|
| 364 | |
---|
| 365 | static const unsigned short ddvd_mpa_quant_snr[17] = { |
---|
| 366 | 70, 110, 160, 208, |
---|
| 367 | 253, 316, 378, 439, |
---|
| 368 | 499, 559, 620, 680, |
---|
| 369 | 740, 800, 861, 920, |
---|
| 370 | 980 |
---|
| 371 | }; |
---|
| 372 | |
---|
| 373 | static const unsigned char ddvd_mpa_nb_scale_factors[4] = { 3, 2, 1, 2 }; |
---|
| 374 | |
---|
| 375 | const int ddvd_mpa_ff_mpa_quant_bits[17] = { |
---|
| 376 | -5, -7, 3, -10, 4, |
---|
| 377 | 5, 6, 7, 8, 9, |
---|
| 378 | 10, 11, 12, 13, 14, |
---|
| 379 | 15, 16 |
---|
| 380 | }; |
---|
| 381 | |
---|
| 382 | |
---|
| 383 | #if BYTE_ORDER == BIG_ENDIAN |
---|
| 384 | #define be2me_16(x) (x) |
---|
| 385 | #define be2me_32(x) (x) |
---|
| 386 | #define be2me_64(x) (x) |
---|
| 387 | #define le2me_16(x) bswap_16(x) |
---|
| 388 | #define le2me_32(x) bswap_32(x) |
---|
| 389 | #define le2me_64(x) bswap_64(x) |
---|
| 390 | #else |
---|
| 391 | #define be2me_16(x) bswap_16(x) |
---|
| 392 | #define be2me_32(x) bswap_32(x) |
---|
| 393 | #define be2me_64(x) bswap_64(x) |
---|
| 394 | #define le2me_16(x) (x) |
---|
| 395 | #define le2me_32(x) (x) |
---|
| 396 | #define le2me_64(x) (x) |
---|
| 397 | #endif |
---|
| 398 | |
---|
| 399 | typedef struct ddvd_mpa_PutBitContext { |
---|
| 400 | uint32_t bit_buf; |
---|
| 401 | int bit_left; |
---|
| 402 | uint8_t *buf, *buf_ptr, *buf_end; |
---|
| 403 | } ddvd_mpa_PutBitContext; |
---|
| 404 | |
---|
| 405 | |
---|
| 406 | static inline void ddvd_mpa_init_put_bits(ddvd_mpa_PutBitContext *s, uint8_t *buffer, int buffer_size) |
---|
| 407 | { |
---|
| 408 | if(buffer_size < 0) { |
---|
| 409 | buffer_size = 0; |
---|
| 410 | buffer = NULL; |
---|
| 411 | } |
---|
| 412 | |
---|
| 413 | s->buf = buffer; |
---|
| 414 | s->buf_end = s->buf + buffer_size; |
---|
| 415 | s->buf_ptr = s->buf; |
---|
| 416 | s->bit_left=32; |
---|
| 417 | s->bit_buf=0; |
---|
| 418 | } |
---|
| 419 | |
---|
| 420 | static inline uint8_t* ddvd_mpa_pbBufPtr(ddvd_mpa_PutBitContext *s) |
---|
| 421 | { |
---|
| 422 | return s->buf_ptr; |
---|
| 423 | } |
---|
| 424 | |
---|
| 425 | static inline void ddvd_mpa_flush_put_bits(ddvd_mpa_PutBitContext *s) |
---|
| 426 | { |
---|
| 427 | s->bit_buf<<= s->bit_left; |
---|
| 428 | while (s->bit_left < 32) { |
---|
| 429 | /* XXX: should test end of buffer */ |
---|
| 430 | *s->buf_ptr++=s->bit_buf >> 24; |
---|
| 431 | s->bit_buf<<=8; |
---|
| 432 | s->bit_left+=8; |
---|
| 433 | } |
---|
| 434 | s->bit_left=32; |
---|
| 435 | s->bit_buf=0; |
---|
| 436 | } |
---|
| 437 | |
---|
| 438 | |
---|
| 439 | |
---|
| 440 | static inline void ddvd_mpa_put_bits(ddvd_mpa_PutBitContext *s, int n, unsigned int value) |
---|
| 441 | { |
---|
| 442 | unsigned int bit_buf; |
---|
| 443 | int bit_left; |
---|
| 444 | |
---|
| 445 | // printf("put_bits=%d %x\n", n, value); |
---|
| 446 | assert(n == 32 || value < (1U << n)); |
---|
| 447 | |
---|
| 448 | bit_buf = s->bit_buf; |
---|
| 449 | bit_left = s->bit_left; |
---|
| 450 | |
---|
| 451 | // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf); |
---|
| 452 | /* XXX: optimize */ |
---|
| 453 | if (n < bit_left) { |
---|
| 454 | bit_buf = (bit_buf<<n) | value; |
---|
| 455 | bit_left-=n; |
---|
| 456 | } else { |
---|
| 457 | bit_buf<<=bit_left; |
---|
| 458 | bit_buf |= value >> (n - bit_left); |
---|
| 459 | *(uint32_t *)s->buf_ptr = be2me_32(bit_buf); |
---|
| 460 | //printf("bitbuf = %08x\n", bit_buf); |
---|
| 461 | s->buf_ptr+=4; |
---|
| 462 | bit_left+=32 - n; |
---|
| 463 | bit_buf = value; |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | s->bit_buf = bit_buf; |
---|
| 467 | s->bit_left = bit_left; |
---|
| 468 | } |
---|
| 469 | |
---|
| 470 | |
---|
| 471 | int ddvd_mpa_samples_offset[MPA_MAX_CHANNELS]; /* offset in samples_buf */ |
---|
| 472 | int ddvd_mpa_sb_samples[MPA_MAX_CHANNELS][3][12][SBLIMIT]; |
---|
| 473 | short ddvd_mpa_samples_buf[MPA_MAX_CHANNELS][SAMPLES_BUF_SIZE]; /* buffer for filter */ |
---|
| 474 | int ddvd_mpa_sblimit; |
---|
| 475 | unsigned char ddvd_mpa_scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3]; /* scale factors */ |
---|
| 476 | /* code to group 3 scale factors */ |
---|
| 477 | unsigned char ddvd_mpa_scale_code[MPA_MAX_CHANNELS][SBLIMIT]; |
---|
| 478 | int ddvd_mpa_frame_size; /* frame size, in bits, without padding */ |
---|
| 479 | int ddvd_mpa_frame_frac, ddvd_mpa_frame_frac_incr, ddvd_mpa_do_padding; |
---|
| 480 | const unsigned char *ddvd_mpa_alloc_table; |
---|
| 481 | ddvd_mpa_PutBitContext pb; |
---|
| 482 | int ddvd_mpa_lsf; /* 1 if mpeg2 low bitrate selected */ |
---|
| 483 | int ddvd_mpa_bitrate_index; /* bit rate */ |
---|
| 484 | int ddvd_mpa_freq_index; |
---|
| 485 | int ddvd_mpa_freq; |
---|
| 486 | int ddvd_mpa_bit_rate; |
---|
| 487 | int64_t ddvd_mpa_nb_samples; |
---|
| 488 | |
---|
| 489 | #endif |
---|