libandroidplugin added
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / ARDroneLib / VLIB / P264 / p264_Qp.c
1 #include <VLIB/P264/p264_Qp.h>
2
3 // define QP_core_table used to scale p264 4x4 transform coefficients
4 // QP is the quantizer parameter varying from 0 to 51.
5 // p264 rescaling rules :
6 // - luma 4x4 residual block -> use Coeff*QP%6_core_table * 2^(floor(QP/6))
7 //
8 // - 4x4 luma DC block       -> use Coeff*QP%6_core_table[0] * 2^(floor(QP/6)-2)                     (Qp>=12)
9 //                           -> use [Coeff*QP%6_core_table[0] + 2^(1-floor(QP/6)] >> (2-floor(QP/6)) (Qp<12)
10 //
11 // - 2x2 chroma DC block     -> use Coeff*QP%6_core_table[0] / 2                  (Qp<6)
12 //                           -> use Coeff*QP%6_core_table[0] * 2^(floor(QP/6)-1)  (Qp>=6)
13
14 static uint16_t QP_core_table[6][16] = {
15 // QP0 : Qstep0 * Si (* 2^6)
16 {
17  10, 13, 10, 13,
18  13, 16, 13, 16,
19  10, 13, 10, 13,
20  13, 16, 13, 16
21 },
22 // QP1 : Qstep1 * Si (* 2^6)
23 {
24  11, 14, 11, 14,
25  14, 18, 14, 18,
26  11, 14, 11, 14,
27  14, 18, 14, 18
28 },
29 // QP2 : Qstep2 * Si (* 2^6)
30 {
31   13, 16, 13, 16,
32   16, 20, 16, 20,
33   13, 16, 13, 16,
34   16, 20, 16, 20
35 },
36 // QP3 : Qstep3 * Si (* 2^6)
37 {
38   14, 18, 14, 18,
39   18, 23, 18, 23,
40   14, 18, 14, 18,
41   18, 23, 18, 23
42 },
43 // QP4 : Qstep4 * Si (* 2^6)
44 {
45   16, 20, 16, 20,
46   20, 25, 20, 25,
47   16, 20, 16, 20,
48   20, 25, 20, 25
49 },
50 // QP5 : Qstep5 * Si (* 2^6)
51 {
52   18, 23, 18, 23,
53   23, 29, 23, 29,
54   18, 23, 18, 23,
55   23, 29, 23, 29
56 }
57 };
58
59
60 void p264_4x4_residual_scale(int16_t* in, int16_t* out, uint32_t Qp)
61 // validate for several Qp on one value block
62 {
63   // rescaling rules :
64   // - luma 4x4 residual block -> use Coeff*QP%6_core_table * 2^(floor(QP/6))
65   uint32_t shift = Qp/6;
66   uint16_t* p_Qp_core_table = QP_core_table[Qp%6];
67   uint32_t i = 16;
68
69   while(i--)
70   {
71     *out++ = (((int32_t)(*in++))*((int32_t)*p_Qp_core_table++))<<shift;
72   }
73 }
74
75 void p264_2x2_chromaDC_scale(int16_t* in, int16_t* out, uint32_t Qp)
76 // validate for several Qp on one value block
77 {
78   // in and out could be the same
79   // - 2x2 chroma DC block     -> use Coeff*QP%6_core_table[0] / 2                  (Qp<6)
80   //                           -> use Coeff*QP%6_core_table[0] * 2^(floor(QP/6)-1)  (Qp>=6)
81   uint32_t shift = Qp/6-1;
82   uint32_t scale = QP_core_table[Qp%6][0];
83   if (Qp < 6)
84   {
85     *out++ = (((int32_t)(*in++))*scale)>>1;
86     *out++ = (((int32_t)(*in++))*scale)>>1;
87     *out++ = (((int32_t)(*in++))*scale)>>1;
88     *out++ = (((int32_t)(*in++))*scale)>>1;
89   }
90   else
91   {
92     *out++ = (((int32_t)(*in++))*scale)<<shift;
93     *out++ = (((int32_t)(*in++))*scale)<<shift;
94     *out++ = (((int32_t)(*in++))*scale)<<shift;
95     *out++ = (((int32_t)(*in++))*scale)<<shift;
96   }
97 }
98
99 void p264_4x4_lumaDC_scale(int16_t* in, int16_t* out, uint32_t Qp)
100 {
101   // - 4x4 luma DC block       -> use Coeff*QP%6_core_table[0] * 2^(floor(QP/6)-2)                     (Qp>=12)
102   //                           -> use [Coeff*QP%6_core_table[0] + 2^(1-floor(QP/6)] >> (2-floor(QP/6)) (Qp<12)
103   uint32_t shift;
104   int32_t round;
105   uint32_t scale = QP_core_table[Qp%6][0];
106   uint32_t i = 16;
107   if (Qp < 12)
108   {
109         //  [QP%6_core_table[0] + 2^(1-floor(QP/6)] >> (2-floor(QP/6))
110     shift = 2-Qp/6;
111     round = 2<<(1-Qp/6);
112     while(i--)
113     {
114           *out++ = (((int32_t)(*in++))*scale + round )>>shift;
115     }
116   }
117   else
118   {
119     shift = Qp/6-2;
120     while(i--)
121     {
122         *out++ = (((int32_t)(*in++))*scale)<<shift;
123     }
124   }
125 }
126