avcodec/mpegvideo_enc: fix qmat value comments

The comments supposed to track the possible value of the qmat and qmat16
matrices, but they were not updated properly in the long history of the
mpegvideo encoder. Also they wrongly assumed the usage of built-in quantizer
matrices and linear quantization.

Signed-off-by: Marton Balint <cus@passwd.hu>
This commit is contained in:
Marton Balint 2025-01-06 21:18:36 +01:00
parent 7d9f373984
commit ed26812337

View file

@ -133,11 +133,11 @@ void ff_convert_matrix(MpegEncContext *s, int (*qmat)[64],
for (i = 0; i < 64; i++) { for (i = 0; i < 64; i++) {
const int j = s->idsp.idct_permutation[i]; const int j = s->idsp.idct_permutation[i];
int64_t den = (int64_t) qscale2 * quant_matrix[j]; int64_t den = (int64_t) qscale2 * quant_matrix[j];
/* 16 <= qscale * quant_matrix[i] <= 7905 /* 1 * 1 <= qscale2 * quant_matrix[j] <= 112 * 255
* Assume x = ff_aanscales[i] * qscale * quant_matrix[i] * Assume x = qscale2 * quant_matrix[j]
* 19952 <= x <= 249205026 * 1 <= x <= 28560
* (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026 * (1 << 22) / 1 >= (1 << 22) / (x) >= (1 << 22) / 28560
* 3444240 >= (1 << 36) / (x) >= 275 */ * 4194304 >= (1 << 22) / (x) >= 146 */
qmat[qscale][i] = (int)((UINT64_C(2) << QMAT_SHIFT) / den); qmat[qscale][i] = (int)((UINT64_C(2) << QMAT_SHIFT) / den);
} }
@ -145,11 +145,11 @@ void ff_convert_matrix(MpegEncContext *s, int (*qmat)[64],
for (i = 0; i < 64; i++) { for (i = 0; i < 64; i++) {
const int j = s->idsp.idct_permutation[i]; const int j = s->idsp.idct_permutation[i];
int64_t den = ff_aanscales[i] * (int64_t) qscale2 * quant_matrix[j]; int64_t den = ff_aanscales[i] * (int64_t) qscale2 * quant_matrix[j];
/* 16 <= qscale * quant_matrix[i] <= 7905 /* 1247 * 1 * 1 <= ff_aanscales[i] * qscale2 * quant_matrix[j] <= 31521 * 112 * 255
* Assume x = ff_aanscales[i] * qscale * quant_matrix[i] * Assume x = ff_aanscales[i] * qscale2 * quant_matrix[j]
* 19952 <= x <= 249205026 * 1247 <= x <= 900239760
* (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026 * (1 << 36) / 1247 >= (1 << 36) / (x) >= (1 << 36) / 900239760
* 3444240 >= (1 << 36) / (x) >= 275 */ * 55107840 >= (1 << 36) / (x) >= 76 */
qmat[qscale][i] = (int)((UINT64_C(2) << (QMAT_SHIFT + 14)) / den); qmat[qscale][i] = (int)((UINT64_C(2) << (QMAT_SHIFT + 14)) / den);
} }
@ -157,14 +157,17 @@ void ff_convert_matrix(MpegEncContext *s, int (*qmat)[64],
for (i = 0; i < 64; i++) { for (i = 0; i < 64; i++) {
const int j = s->idsp.idct_permutation[i]; const int j = s->idsp.idct_permutation[i];
int64_t den = (int64_t) qscale2 * quant_matrix[j]; int64_t den = (int64_t) qscale2 * quant_matrix[j];
/* We can safely suppose that 16 <= quant_matrix[i] <= 255 /* 1 * 1 <= qscale2 * quant_matrix[j] <= 112 * 255
* Assume x = qscale * quant_matrix[i] * Assume x = qscale2 * quant_matrix[j]
* So 16 <= x <= 7905 * 1 <= x <= 28560
* so (1 << 19) / 16 >= (1 << 19) / (x) >= (1 << 19) / 7905 * (1 << 22) / 1 >= (1 << 22) / (x) >= (1 << 22) / 28560
* so 32768 >= (1 << 19) / (x) >= 67 */ * 4194304 >= (1 << 22) / (x) >= 146
*
* 1 <= x <= 28560
* (1 << 17) / 1 >= (1 << 17) / (x) >= (1 << 17) / 28560
* 131072 >= (1 << 17) / (x) >= 4 */
qmat[qscale][i] = (int)((UINT64_C(2) << QMAT_SHIFT) / den); qmat[qscale][i] = (int)((UINT64_C(2) << QMAT_SHIFT) / den);
//qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) /
// (qscale * quant_matrix[i]);
qmat16[qscale][0][i] = (2 << QMAT_SHIFT_MMX) / den; qmat16[qscale][0][i] = (2 << QMAT_SHIFT_MMX) / den;
if (qmat16[qscale][0][i] == 0 || if (qmat16[qscale][0][i] == 0 ||