more aggressive and asymmetrical Chroma compression

This commit is contained in:
minjaesong
2025-08-22 23:12:56 +09:00
parent b720e786a9
commit 3c135e48e0
3 changed files with 331 additions and 162 deletions

View File

@@ -173,80 +173,158 @@ static const uint8_t QUANT_TABLES_Y[8][256] = {
14, 15, 16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25, 26, 27}
};
// Quality settings for quantization (Chroma channels - 8x8)
static const uint8_t QUANT_TABLES_C[8][64] = {
// Quality 0 (lowest)
{120, 90, 75, 120, 180, 255, 255, 255,
83, 90, 105, 143, 195, 255, 255, 255,
105, 98, 120, 180, 255, 255, 255, 255,
105, 128, 165, 218, 255, 255, 255, 255,
135, 165, 255, 255, 255, 255, 255, 255,
180, 255, 255, 255, 255, 255, 255, 255,
// Quality settings for quantization (Co channel - 8x8)
static const uint8_t QUANT_TABLES_CO[8][64] = {
// Quality 0 (lowest) - 2x more aggressive than before
{240, 180, 150, 240, 255, 255, 255, 255,
166, 180, 210, 255, 255, 255, 255, 255,
210, 196, 240, 255, 255, 255, 255, 255,
210, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255},
// Quality 1
{60, 45, 38, 60, 90, 150, 192, 225,
42, 45, 53, 72, 98, 192, 225, 255,
53, 49, 60, 90, 150, 192, 225, 255,
53, 64, 83, 109, 192, 225, 255, 255,
68, 83, 139, 192, 225, 255, 255, 255,
90, 132, 192, 225, 255, 255, 255, 255,
185, 192, 225, 255, 255, 255, 255, 255,
192, 225, 255, 255, 255, 255, 255, 255},
// Quality 2
{30, 23, 19, 30, 45, 75, 96, 113,
21, 23, 27, 36, 49, 96, 113, 135,
27, 25, 30, 45, 75, 96, 113, 135,
27, 32, 42, 55, 96, 113, 135, 150,
34, 42, 70, 96, 113, 135, 150, 165,
45, 66, 96, 113, 135, 150, 165, 180,
93, 96, 113, 135, 150, 165, 180, 188,
96, 113, 135, 150, 165, 180, 188, 192},
// Quality 3
{24, 18, 15, 24, 36, 60, 77, 90,
17, 18, 21, 29, 39, 77, 90, 108,
21, 20, 24, 36, 60, 77, 90, 108,
21, 26, 33, 44, 77, 90, 108, 120,
27, 33, 56, 77, 90, 108, 120, 132,
36, 53, 77, 90, 108, 120, 132, 144,
74, 77, 90, 108, 120, 132, 144, 150,
77, 90, 108, 120, 132, 144, 150, 154},
// Quality 4
{18, 14, 12, 18, 27, 45, 57, 68,
13, 14, 16, 22, 30, 57, 68, 81,
16, 15, 18, 27, 45, 57, 68, 81,
16, 20, 25, 33, 57, 68, 81, 90,
20, 25, 42, 57, 68, 81, 90, 99,
27, 39, 57, 68, 81, 90, 99, 108,
56, 57, 68, 81, 90, 99, 108, 113,
57, 68, 81, 90, 99, 108, 113, 116},
// Quality 5
{15, 11, 9, 15, 23, 38, 48, 57,
11, 11, 13, 18, 24, 48, 57, 68,
13, 12, 15, 23, 38, 48, 57, 68,
13, 16, 21, 28, 48, 57, 68, 75,
17, 21, 35, 48, 57, 68, 75, 83,
23, 33, 48, 57, 68, 75, 83, 90,
46, 48, 57, 68, 75, 83, 90, 94,
48, 57, 68, 75, 83, 90, 94, 96},
// Quality 6
{12, 9, 8, 12, 18, 30, 39, 45,
9, 9, 11, 14, 20, 39, 45, 54,
11, 10, 12, 18, 30, 39, 45, 54,
11, 13, 17, 22, 39, 45, 54, 60,
14, 17, 28, 39, 45, 54, 60, 66,
18, 26, 39, 45, 54, 60, 66, 72,
38, 39, 45, 54, 60, 66, 72, 75,
39, 45, 54, 60, 66, 72, 75, 77},
// Quality 7 (highest) - much finer quantization for better quality
{1, 1, 1, 1, 1, 2, 2, 3,
1, 1, 1, 1, 2, 2, 3, 4,
1, 1, 1, 2, 2, 3, 4, 5,
1, 1, 2, 2, 3, 4, 5, 6,
1, 2, 2, 3, 4, 5, 6, 7,
2, 2, 3, 4, 5, 6, 7, 8,
2, 3, 4, 5, 6, 7, 8, 9,
3, 4, 5, 6, 7, 8, 9, 10}
// Quality 1 - 1.8x more aggressive
{108, 81, 68, 108, 162, 255, 255, 255,
76, 81, 95, 130, 176, 255, 255, 255,
95, 88, 108, 162, 255, 255, 255, 255,
95, 115, 149, 196, 255, 255, 255, 255,
122, 149, 250, 255, 255, 255, 255, 255,
162, 238, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255},
// Quality 2 - 1.7x more aggressive
{51, 39, 32, 51, 77, 128, 163, 192,
36, 39, 46, 61, 83, 163, 192, 230,
46, 43, 51, 77, 128, 163, 192, 230,
46, 54, 71, 94, 163, 192, 230, 255,
58, 71, 119, 163, 192, 230, 255, 255,
77, 112, 163, 192, 230, 255, 255, 255,
158, 163, 192, 230, 255, 255, 255, 255,
163, 192, 230, 255, 255, 255, 255, 255},
// Quality 3 - 1.6x more aggressive
{38, 29, 24, 38, 58, 96, 123, 144,
27, 29, 34, 46, 62, 123, 144, 173,
34, 32, 38, 58, 96, 123, 144, 173,
34, 42, 53, 70, 123, 144, 173, 192,
43, 53, 90, 123, 144, 173, 192, 211,
58, 85, 123, 144, 173, 192, 211, 230,
118, 123, 144, 173, 192, 211, 230, 240,
123, 144, 173, 192, 211, 230, 240, 246},
// Quality 4 - 1.5x more aggressive
{27, 21, 18, 27, 41, 68, 86, 102,
20, 21, 24, 33, 45, 86, 102, 122,
24, 23, 27, 41, 68, 86, 102, 122,
24, 30, 38, 50, 86, 102, 122, 135,
30, 38, 63, 86, 102, 122, 135, 149,
41, 59, 86, 102, 122, 135, 149, 162,
84, 86, 102, 122, 135, 149, 162, 170,
86, 102, 122, 135, 149, 162, 170, 174},
// Quality 5 - 1.4x more aggressive
{21, 15, 13, 21, 32, 53, 67, 80,
15, 15, 18, 25, 34, 67, 80, 95,
18, 17, 21, 32, 53, 67, 80, 95,
18, 22, 29, 39, 67, 80, 95, 105,
24, 29, 49, 67, 80, 95, 105, 116,
32, 46, 67, 80, 95, 105, 116, 126,
64, 67, 80, 95, 105, 116, 126, 132,
67, 80, 95, 105, 116, 126, 132, 134},
// Quality 6 - 1.3x more aggressive
{16, 12, 10, 16, 23, 39, 51, 59,
12, 12, 14, 18, 26, 51, 59, 70,
14, 13, 16, 23, 39, 51, 59, 70,
14, 17, 22, 29, 51, 59, 70, 78,
18, 22, 36, 51, 59, 70, 78, 86,
23, 34, 51, 59, 70, 78, 86, 94,
49, 51, 59, 70, 78, 86, 94, 98,
51, 59, 70, 78, 86, 94, 98, 100},
// Quality 7 (highest) - 1.2x more aggressive for subtle improvement
{1, 1, 1, 1, 1, 2, 2, 4,
1, 1, 1, 1, 2, 3, 4, 5,
1, 1, 1, 2, 3, 4, 5, 6,
1, 1, 2, 3, 4, 5, 6, 7,
1, 2, 3, 4, 5, 6, 7, 8,
2, 3, 4, 5, 6, 7, 8, 10,
3, 4, 5, 6, 7, 8, 10, 11,
4, 5, 6, 7, 8, 10, 11, 12}
};
// Quality settings for quantization (Cg channel - 8x8, much more aggressive than Co)
// Similar to NTSC Q channel reduction - exploit reduced human sensitivity to green-magenta
// Now using 3x-5x more aggressive quantization, similar to actual NTSC Q bandwidth reduction
static const uint8_t QUANT_TABLES_CG[8][64] = {
// Quality 0 (lowest) - 5x more aggressive than Co, maximum compression
{255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255},
// Quality 1 - 4.5x more aggressive, very low bandwidth
{255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255},
// Quality 2 - 4x more aggressive, preserve only DC and very low frequencies
{120, 180, 240, 255, 255, 255, 255, 255,
180, 240, 255, 255, 255, 255, 255, 255,
240, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255},
// Quality 3 - 3.5x more aggressive, basic low frequency preservation
{84, 108, 144, 192, 255, 255, 255, 255,
108, 144, 192, 255, 255, 255, 255, 255,
144, 192, 255, 255, 255, 255, 255, 255,
192, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255},
// Quality 4 - 3x more aggressive, moderate compression
{54, 72, 96, 128, 180, 255, 255, 255,
72, 96, 128, 180, 255, 255, 255, 255,
96, 128, 180, 255, 255, 255, 255, 255,
128, 180, 255, 255, 255, 255, 255, 255,
180, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255},
// Quality 5 - 2.8x more aggressive, balanced compression
{42, 56, 75, 100, 140, 200, 255, 255,
56, 75, 100, 140, 200, 255, 255, 255,
75, 100, 140, 200, 255, 255, 255, 255,
100, 140, 200, 255, 255, 255, 255, 255,
140, 200, 255, 255, 255, 255, 255, 255,
200, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255},
// Quality 6 - 2.5x more aggressive, good quality but still compressed
{30, 40, 54, 72, 100, 144, 200, 255,
40, 54, 72, 100, 144, 200, 255, 255,
54, 72, 100, 144, 200, 255, 255, 255,
72, 100, 144, 200, 255, 255, 255, 255,
100, 144, 200, 255, 255, 255, 255, 255,
144, 200, 255, 255, 255, 255, 255, 255,
200, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255},
// Quality 7 (highest) - 2x more aggressive, preserve more detail
{2, 3, 4, 6, 8, 12, 16, 20,
3, 4, 6, 8, 12, 16, 20, 24,
4, 6, 8, 12, 16, 20, 24, 28,
6, 8, 12, 16, 20, 24, 28, 32,
8, 12, 16, 20, 24, 28, 32, 36,
12, 16, 20, 24, 28, 32, 36, 40,
16, 20, 24, 28, 32, 36, 40, 44,
20, 24, 28, 32, 36, 40, 44, 48}
};
// Audio constants (reuse MP2 from existing system)
@@ -890,18 +968,19 @@ static void encode_block(tev_encoder_t *enc, int block_x, int block_y, int is_ke
// Apply fast DCT transform to chroma - 4x performance improvement
dct_8x8_fast(enc->co_workspace, enc->dct_workspace);
// Quantize Co coefficients (chroma)
const uint8_t *c_quant = QUANT_TABLES_C[enc->quality];
// Quantize Co coefficients (chroma - orange-blue)
const uint8_t *co_quant = QUANT_TABLES_CO[enc->quality];
for (int i = 0; i < 64; i++) {
block->co_coeffs[i] = quantize_coeff(enc->dct_workspace[i], c_quant[i], i == 0, 1);
block->co_coeffs[i] = quantize_coeff(enc->dct_workspace[i], co_quant[i], i == 0, 1);
}
// Apply fast DCT transform to Cg - 4x performance improvement
dct_8x8_fast(enc->cg_workspace, enc->dct_workspace);
// Quantize Cg coefficients (chroma)
// Quantize Cg coefficients (chroma - green-magenta, more aggressive like NTSC Q)
const uint8_t *cg_quant = QUANT_TABLES_CG[enc->quality];
for (int i = 0; i < 64; i++) {
block->cg_coeffs[i] = quantize_coeff(enc->dct_workspace[i], c_quant[i], i == 0, 1);
block->cg_coeffs[i] = quantize_coeff(enc->dct_workspace[i], cg_quant[i], i == 0, 1);
}
// Set CBP (simplified - always encode all channels)