Skip to content

Commit

Permalink
libavcenc: Modified conversion tables between Qstep & QP
Browse files Browse the repository at this point in the history
avc-qp(QP) scale is logarithmic and Qstep scale is linear.
The translation between log to linear is carried during
rate control modelling.

Look up tables for these translations are modified as per
the literature
  • Loading branch information
ram-mohan committed Sep 30, 2023
1 parent 31fc8ed commit 2e128ba
Showing 1 changed file with 47 additions and 122 deletions.
169 changes: 47 additions & 122 deletions encoder/ih264e_globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,132 +213,57 @@ const WORD8 gi1_mv_pred_condition[8] =
-1, 0, 1, -1, 2, -1, -1, -1
};

/*******************************************************************************
* Translation of MPEG QP to H264 QP
******************************************************************************/
/*
* Note : RC library models QP and bits assuming the QP to be MPEG2.
* Since MPEG qp varies linearly, when the relationship is computed,
* it learns that delta(qp) => delta(bits). Now what we are doing by the
* transation of qp is that
* QPrc = a + b*2^(QPen)
* By not considering the weight matrix in both MPEG and H264 we in effect
* only changing the relation to
* QPrc = c + d*2^(QPen)
* This will only entail changing the RC model parameters, and this will
* not affect rc relation at all
*
* We have MPEG qp which varies from 0-228. The quantization factor has a linear
* relation ship with the size of quantized values
*
* We also have H264 Qp, which varies such that for a change in QP of 6 , we
* double the corresponding scaling factor. Hence the scaling is linear in terms
* of 2^(QPh/6)
*
* Now we want to have translation between QPm and QPh. Hence we can write
*
* QPm = a + b*2^(QPh/6)
*
* Appling boundary condition that
* 1) QPm = 0.625 if QPh = 0
* 2) QPm = 224 if QPh = 51,
*
* we will have
* a = 0.0063, b = 0.6187
*
* Hence the relatiohship is
* QPm = a + b*2^(Qph/6)
* QPh = 6*log((Qpm - a)/b)
*
* Unrounded values for gau1_h264_to_mpeg2_qmap[H264_QP_ELEM] =
*
* 0.625 0.70077 0.78581 0.88127 0.98843 1.10870
* 1.24370 1.39523 1.56533 1.75625 1.97055 2.21110
* 2.48110 2.78417 3.12435 3.50620 3.93480 4.41589
* 4.95590 5.56204 6.24241 7.00609 7.86330 8.82548
* 9.90550 11.11778 12.47851 14.00588 15.72030 17.64467
* 19.80470 22.22925 24.95072 28.00547 31.43430 35.28304
* 39.60310 44.45221 49.89514 56.00463 62.86230 70.55978
* 79.19990 88.89811 99.78398 112.00296 125.71830 141.11325
* 158.39350 177.78992 199.56167 223.99963
*
* Unrounded values for gau1_mpeg2_to_h264_qmap[MPEG2_QP_ELEM]
*
* 0 4.1014 10.1288 13.6477 16.1425 18.0768 19.6568
* 20.9925 22.1493 23.1696 24.0822 24.9078 25.6614 26.3546
* 26.9964 27.5938 28.1527 28.6777 29.1726 29.6408 30.0850
* 30.5074 30.9102 31.2951 31.6636 32.0171 32.3567 32.6834
* 32.9983 33.3021 33.5957 33.8795 34.1544 34.4208 34.6793
* 34.9303 35.1742 35.4114 35.6423 35.8671 36.0863 36.3001
* 36.5087 36.7124 36.9115 37.1060 37.2963 37.4825 37.6648
* 37.8433 38.0182 38.1896 38.3577 38.5226 38.6844 38.8433
* 38.9993 39.1525 39.3031 39.4511 39.5966 39.7397 39.8804
* 40.0189 40.1553 40.2895 40.4217 40.5518 40.6801 40.8065
* 40.9310 41.0538 41.1749 41.2943 41.4121 41.5283 41.6430
* 41.7561 41.8678 41.9781 42.0870 42.1946 42.3008 42.4057
* 42.5094 42.6118 42.7131 42.8132 42.9121 43.0099 43.1066
* 43.2023 43.2969 43.3905 43.4831 43.5747 43.6653 43.7550
* 43.8438 43.9317 44.0187 44.1049 44.1901 44.2746 44.3582
* 44.4411 44.5231 44.6044 44.6849 44.7647 44.8438 44.9221
* 44.9998 45.0767 45.1530 45.2286 45.3035 45.3779 45.4515
* 45.5246 45.5970 45.6689 45.7401 45.8108 45.8809 45.9504
* 46.0194 46.0878 46.1557 46.2231 46.2899 46.3563 46.4221
* 46.4874 46.5523 46.6166 46.6805 46.7439 46.8069 46.8694
* 46.9314 46.9930 47.0542 47.1150 47.1753 47.2352 47.2947
* 47.3538 47.4125 47.4708 47.5287 47.5862 47.6433 47.7001
* 47.7565 47.8125 47.8682 47.9235 47.9785 48.0331 48.0874
* 48.1413 48.1949 48.2482 48.3011 48.3537 48.4060 48.4580
* 48.5097 48.5611 48.6122 48.6629 48.7134 48.7636 48.8135
* 48.8631 48.9124 48.9615 49.0102 49.0587 49.1069 49.1549
* 49.2026 49.2500 49.2972 49.3441 49.3908 49.4372 49.4834
* 49.5293 49.5750 49.6204 49.6656 49.7106 49.7553 49.7998
* 49.8441 49.8882 49.9320 49.9756 50.0190 50.0622 50.1051
* 50.1479 50.1904 50.2327 50.2749 50.3168 50.3585 50.4000
* 50.4413 50.4825 50.5234 50.5641 50.6047 50.6450 50.6852
* 50.7252 50.7650 50.8046 50.8440 50.8833 50.9224 50.9613
* 51.0000
*/
/**
******************************************************************************
* @brief Translation of Qstep <-> QP
* Qstep(QP) = Qstep(QP%6) * (2 ^ floor(QP/6))
* Qstep(QP, n = 0) {0.625, 0.6875, 0.8125, 0.875, 1.0, 1.125} for QP[0-5]
* @remarks QPRange[0-51] & QstepRange[1 - 224].
******************************************************************************
*/
const UWORD8 gau1_h264_to_mpeg2_qmap[H264_QP_ELEM] =
{
1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 3, 3, 4,
4, 4, 5, 6, 6, 7, 8, 9,
10, 11, 12, 14, 16, 18, 20, 22,
25, 28, 31, 35, 40, 44, 50, 56,
63, 71, 79, 89, 100, 112, 126, 141,
158, 178, 200, 224
1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 3, 3, 3, 4,
4, 5, 5, 6, 7, 7, 8, 9,
10, 11, 13, 14, 16, 18, 20, 22,
26, 28, 32, 36, 40, 44, 52, 56,
64, 72, 80, 88, 104, 112, 128, 144,
160, 176, 208, 224,
};

const UWORD8 gau1_mpeg2_to_h264_qmap[MPEG2_QP_ELEM] =
{
0, 4, 10, 14, 16, 18, 20, 21,
22, 23, 24, 25, 26, 26, 27, 28,
28, 29, 29, 30, 30, 31, 31, 31,
32, 32, 32, 33, 33, 33, 34, 34,
34, 34, 35, 35, 35, 35, 36, 36,
36, 36, 37, 37, 37, 37, 37, 37,
38, 38, 38, 38, 38, 39, 39, 39,
39, 39, 39, 39, 40, 40, 40, 40,
40, 40, 40, 41, 41, 41, 41, 41,
41, 41, 41, 42, 42, 42, 42, 42,
42, 42, 42, 42, 43, 43, 43, 43,
43, 43, 43, 43, 43, 43, 43, 44,
44, 44, 44, 44, 44, 44, 44, 44,
44, 44, 45, 45, 45, 45, 45, 45,
45, 45, 45, 45, 45, 45, 45, 46,
46, 46, 46, 46, 46, 46, 46, 46,
46, 46, 46, 46, 46, 46, 47, 47,
47, 47, 47, 47, 47, 47, 47, 47,
47, 47, 47, 47, 47, 47, 48, 48,
48, 48, 48, 48, 48, 48, 48, 48,
48, 48, 48, 48, 48, 48, 48, 48,
49, 49, 49, 49, 49, 49, 49, 49,
49, 49, 49, 49, 49, 49, 49, 49,
49, 49, 49, 49, 49, 50, 50, 50,
50, 50, 50, 50, 50, 50, 50, 50,
50, 50, 50, 50, 50, 50, 50, 50,
50, 50, 50, 50, 51, 51, 51, 51,
51, 51, 51, 51, 51, 51, 51, 51,
51
0, 4, 10, 13, 16, 18, 19, 21,
22, 23, 24, 25, 25, 26, 27, 27,
28, 28, 29, 30, 30, 30, 31, 31,
31, 32, 32, 32, 33, 33, 33, 34,
34, 34, 34, 35, 35, 36, 36, 36,
36, 36, 36, 37, 37, 37, 37, 37,
37, 38, 38, 38, 38, 38, 38, 39,
39, 39, 39, 39, 39, 40, 40, 40,
40, 40, 40, 40, 40, 41, 41, 41,
41, 42, 42, 42, 42, 42, 42, 42,
42, 42, 42, 42, 42, 43, 43, 43,
43, 43, 43, 43, 43, 43, 43, 43,
43, 44, 44, 44, 44, 44, 44, 44,
44, 44, 44, 44, 44, 45, 45, 45,
45, 45, 45, 45, 45, 45, 45, 45,
45, 46, 46, 46, 46, 46, 46, 46,
46, 46, 46, 46, 46, 46, 46, 46,
46, 47, 47, 47, 47, 47, 47, 47,
47, 48, 48, 48, 48, 48, 48, 48,
48, 48, 48, 48, 48, 48, 48, 48,
48, 48, 48, 48, 48, 48, 48, 48,
48, 49, 49, 49, 49, 49, 49, 49,
49, 49, 49, 49, 49, 49, 49, 49,
49, 49, 49, 49, 49, 49, 49, 49,
49, 50, 50, 50, 50, 50, 50, 50,
50, 50, 50, 50, 50, 50, 50, 50,
50, 50, 50, 50, 50, 50, 50, 50,
50, 51, 51, 51, 51, 51, 51, 51,
51, 51, 51, 51, 51, 51, 51, 51,
51, 51, 51, 51, 51, 51, 51, 51,
51, 52, 52, 52, 52, 52, 52, 52,
52, 52, 52, 52, 52, 52, 52, 52,

This comment has been minimized.

Copy link
@harishdm

harishdm Sep 30, 2023

Member

@ram-mohan these values look wrong to me. I don't think it is correct to return a value outside the range.

This comment has been minimized.

Copy link
@ram-mohan

ram-mohan Oct 1, 2023

Author Collaborator

@harishdm, the range QPRange[0-51] is mapped to QstepRange[1 - 224]. Values larger than 224 map to values to larger than 51. The rate control library while initializing the qp bounds, initializes itself to 51. This map to 224. All the values returned by rc are clamped to 224, which during inverse mapping stay at 51. We will never return a value outside the range of h264 qp.

};

0 comments on commit 2e128ba

Please sign in to comment.