blob: 6e174a7801ffcf4bb9ab37a7b01103082f5738af [file] [log] [blame]
Reimar Döffinger062777b2010-03-14 19:30:251/*
2 * Header file for hardcoded QDM2 tables
3 *
4 * Copyright (c) 2010 Reimar Döffinger <[email protected]>
5 *
6 * This file is part of FFmpeg.
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
Diego Biurrun153382e2011-05-17 14:58:0423#ifndef AVCODEC_QDM2_TABLEGEN_H
24#define AVCODEC_QDM2_TABLEGEN_H
Reimar Döffinger062777b2010-03-14 19:30:2525
26#include <stdint.h>
27#include <math.h>
Måns Rullgård69d12902010-06-26 20:28:4428#include "libavutil/attributes.h"
Reimar Döffinger70d80ed2014-09-07 11:55:2329#include "qdm2data.h"
Reimar Döffinger062777b2010-03-14 19:30:2530
31#define SOFTCLIP_THRESHOLD 27600
32#define HARDCLIP_THRESHOLD 35716
33
34#if CONFIG_HARDCODED_TABLES
35#define softclip_table_init()
36#define rnd_table_init()
37#define init_noise_samples()
Reimar Döffinger70d80ed2014-09-07 11:55:2338#define qdm2_init_vlc()
Reimar Döffinger062777b2010-03-14 19:30:2539#include "libavcodec/qdm2_tables.h"
40#else
41static uint16_t softclip_table[HARDCLIP_THRESHOLD - SOFTCLIP_THRESHOLD + 1];
Michael Niedermayer8c4aebb2013-01-28 18:34:5542static float noise_table[4096 + 20];
Reimar Döffinger062777b2010-03-14 19:30:2543static uint8_t random_dequant_index[256][5];
44static uint8_t random_dequant_type24[128][3];
45static float noise_samples[128];
46
47static av_cold void softclip_table_init(void) {
48 int i;
49 double dfl = SOFTCLIP_THRESHOLD - 32767;
50 float delta = 1.0 / -dfl;
51 for (i = 0; i < HARDCLIP_THRESHOLD - SOFTCLIP_THRESHOLD + 1; i++)
52 softclip_table[i] = SOFTCLIP_THRESHOLD - ((int)(sin((float)i * delta) * dfl) & 0x0000FFFF);
53}
54
55
56// random generated table
57static av_cold void rnd_table_init(void) {
58 int i,j;
Michael Niedermayer5c8ffba2013-03-06 03:56:3359 uint32_t ldw;
Reimar Döffinger062777b2010-03-14 19:30:2560 uint64_t random_seed = 0;
61 float delta = 1.0 / 16384.0;
62 for(i = 0; i < 4096 ;i++) {
63 random_seed = random_seed * 214013 + 2531011;
64 noise_table[i] = (delta * (float)(((int32_t)random_seed >> 16) & 0x00007FFF)- 1.0) * 1.3;
65 }
66
67 for (i = 0; i < 256 ;i++) {
68 random_seed = 81;
69 ldw = i;
70 for (j = 0; j < 5 ;j++) {
Michael Niedermayer5c8ffba2013-03-06 03:56:3371 random_dequant_index[i][j] = ldw / random_seed;
72 ldw %= random_seed;
73 random_seed /= 3;
Reimar Döffinger062777b2010-03-14 19:30:2574 }
75 }
76 for (i = 0; i < 128 ;i++) {
77 random_seed = 25;
78 ldw = i;
79 for (j = 0; j < 3 ;j++) {
Michael Niedermayer5c8ffba2013-03-06 03:56:3380 random_dequant_type24[i][j] = ldw / random_seed;
81 ldw %= random_seed;
82 random_seed /= 5;
Reimar Döffinger062777b2010-03-14 19:30:2583 }
84 }
85}
86
87
88static av_cold void init_noise_samples(void) {
89 int i;
Mans Rullgardc6825142011-10-11 16:45:5590 unsigned random_seed = 0;
Reimar Döffinger062777b2010-03-14 19:30:2591 float delta = 1.0 / 16384.0;
92 for (i = 0; i < 128;i++) {
93 random_seed = random_seed * 214013 + 2531011;
94 noise_samples[i] = (delta * (float)((random_seed >> 16) & 0x00007fff) - 1.0);
95 }
96}
Reimar Döffinger70d80ed2014-09-07 11:55:2397
98static VLC vlc_tab_level;
99static VLC vlc_tab_diff;
100static VLC vlc_tab_run;
101static VLC fft_level_exp_alt_vlc;
102static VLC fft_level_exp_vlc;
103static VLC fft_stereo_exp_vlc;
104static VLC fft_stereo_phase_vlc;
105static VLC vlc_tab_tone_level_idx_hi1;
106static VLC vlc_tab_tone_level_idx_mid;
107static VLC vlc_tab_tone_level_idx_hi2;
108static VLC vlc_tab_type30;
109static VLC vlc_tab_type34;
110static VLC vlc_tab_fft_tone_offset[5];
111
Andreas Rheinhardt2d764062022-06-13 21:02:57112static VLCElem qdm2_table[3838];
Reimar Döffinger70d80ed2014-09-07 11:55:23113
Andreas Rheinhardt03112cd2020-11-03 22:16:47114static av_cold void build_vlc(VLC *vlc, int nb_bits, int nb_codes,
115 unsigned *offset, const uint8_t tab[][2])
Andreas Rheinhardt682b0e42020-10-31 16:33:28116{
Andreas Rheinhardt03112cd2020-11-03 22:16:47117 vlc->table = &qdm2_table[*offset];
118 vlc->table_allocated = FF_ARRAY_ELEMS(qdm2_table) - *offset;
Andreas Rheinhardt9cdf82c2023-09-08 13:18:12119 ff_vlc_init_from_lengths(vlc, nb_bits, nb_codes,
Andreas Rheinhardt682b0e42020-10-31 16:33:28120 &tab[0][1], 2, &tab[0][0], 2, 1,
Andreas Rheinhardt9cdf82c2023-09-08 13:18:12121 -1, VLC_INIT_STATIC_OVERLONG | VLC_INIT_LE, NULL);
Andreas Rheinhardt03112cd2020-11-03 22:16:47122 *offset += vlc->table_size;
Andreas Rheinhardt682b0e42020-10-31 16:33:28123}
124
Reimar Döffinger70d80ed2014-09-07 11:55:23125static av_cold void qdm2_init_vlc(void)
126{
Andreas Rheinhardtbcdf67c2020-10-31 18:26:39127 const uint8_t (*tab)[2] = tab_fft_tone_offset;
Andreas Rheinhardt03112cd2020-11-03 22:16:47128 unsigned offset = 0;
Andreas Rheinhardtbcdf67c2020-10-31 18:26:39129
Andreas Rheinhardt03112cd2020-11-03 22:16:47130 build_vlc(&vlc_tab_level, 8, 24, &offset, tab_level);
131 build_vlc(&vlc_tab_diff, 8, 33, &offset, tab_diff);
132 build_vlc(&vlc_tab_run, 5, 6, &offset, tab_run);
Reimar Döffinger70d80ed2014-09-07 11:55:23133
Andreas Rheinhardt03112cd2020-11-03 22:16:47134 build_vlc(&fft_level_exp_alt_vlc, 8, 28, &offset, fft_level_exp_alt);
135 build_vlc(&fft_level_exp_vlc, 8, 20, &offset, fft_level_exp);
Reimar Döffinger70d80ed2014-09-07 11:55:23136
Andreas Rheinhardt03112cd2020-11-03 22:16:47137 build_vlc(&fft_stereo_exp_vlc, 6, 7, &offset, fft_stereo_exp);
138 build_vlc(&fft_stereo_phase_vlc, 6, 9, &offset, fft_stereo_phase);
Reimar Döffinger70d80ed2014-09-07 11:55:23139
Andreas Rheinhardt03112cd2020-11-03 22:16:47140 build_vlc(&vlc_tab_tone_level_idx_hi1, 8, 20, &offset, tab_tone_level_idx_hi1);
141 build_vlc(&vlc_tab_tone_level_idx_mid, 8, 13, &offset, tab_tone_level_idx_mid);
142 build_vlc(&vlc_tab_tone_level_idx_hi2, 8, 18, &offset, tab_tone_level_idx_hi2);
Reimar Döffinger70d80ed2014-09-07 11:55:23143
Andreas Rheinhardt03112cd2020-11-03 22:16:47144 build_vlc(&vlc_tab_type30, 6, 9, &offset, tab_type30);
145 build_vlc(&vlc_tab_type34, 5, 10, &offset, tab_type34);
Reimar Döffinger70d80ed2014-09-07 11:55:23146
Andreas Rheinhardtbcdf67c2020-10-31 18:26:39147 for (int i = 0; i < 5; i++) {
148 build_vlc(&vlc_tab_fft_tone_offset[i], 8, tab_fft_tone_offset_sizes[i],
Andreas Rheinhardt03112cd2020-11-03 22:16:47149 &offset, tab);
Andreas Rheinhardtbcdf67c2020-10-31 18:26:39150 tab += tab_fft_tone_offset_sizes[i];
151 }
Reimar Döffinger70d80ed2014-09-07 11:55:23152}
153
Reimar Döffinger062777b2010-03-14 19:30:25154#endif /* CONFIG_HARDCODED_TABLES */
155
Diego Biurrun153382e2011-05-17 14:58:04156#endif /* AVCODEC_QDM2_TABLEGEN_H */