FreeRDP
rfx_quantization.c
1 
20 #include <freerdp/config.h>
21 
22 #include <freerdp/primitives.h>
23 
24 #include "rfx_quantization.h"
25 
26 /*
27  * Band Offset Dimensions Size
28  *
29  * HL1 0 32x32 1024
30  * LH1 1024 32x32 1024
31  * HH1 2048 32x32 1024
32  *
33  * HL2 3072 16x16 256
34  * LH2 3328 16x16 256
35  * HH2 3584 16x16 256
36  *
37  * HL3 3840 8x8 64
38  * LH3 3904 8x8 64
39  * HH3 3968 8x8 64
40  *
41  * LL3 4032 8x8 64
42  */
43 
44 static INLINE void rfx_quantization_decode_block(const primitives_t* WINPR_RESTRICT prims,
45  INT16* WINPR_RESTRICT buffer, UINT32 buffer_size,
46  UINT32 factor)
47 {
48  if (factor == 0)
49  return;
50 
51  prims->lShiftC_16s_inplace(buffer, factor, buffer_size);
52 }
53 
54 void rfx_quantization_decode(INT16* WINPR_RESTRICT buffer, const UINT32* WINPR_RESTRICT quantVals)
55 {
56  const primitives_t* prims = primitives_get();
57  WINPR_ASSERT(buffer);
58  WINPR_ASSERT(quantVals);
59 
60  rfx_quantization_decode_block(prims, &buffer[0], 1024, quantVals[8] - 1); /* HL1 */
61  rfx_quantization_decode_block(prims, &buffer[1024], 1024, quantVals[7] - 1); /* LH1 */
62  rfx_quantization_decode_block(prims, &buffer[2048], 1024, quantVals[9] - 1); /* HH1 */
63  rfx_quantization_decode_block(prims, &buffer[3072], 256, quantVals[5] - 1); /* HL2 */
64  rfx_quantization_decode_block(prims, &buffer[3328], 256, quantVals[4] - 1); /* LH2 */
65  rfx_quantization_decode_block(prims, &buffer[3584], 256, quantVals[6] - 1); /* HH2 */
66  rfx_quantization_decode_block(prims, &buffer[3840], 64, quantVals[2] - 1); /* HL3 */
67  rfx_quantization_decode_block(prims, &buffer[3904], 64, quantVals[1] - 1); /* LH3 */
68  rfx_quantization_decode_block(prims, &buffer[3968], 64, quantVals[3] - 1); /* HH3 */
69  rfx_quantization_decode_block(prims, &buffer[4032], 64, quantVals[0] - 1); /* LL3 */
70 }
71 
72 static INLINE void rfx_quantization_encode_block(INT16* WINPR_RESTRICT buffer, size_t buffer_size,
73  UINT32 factor)
74 {
75  INT16 half = 0;
76 
77  if (factor == 0)
78  return;
79 
80  half = (1 << (factor - 1));
81  /* Could probably use prims->rShiftC_16s(dst+half, factor, dst, buffer_size); */
82  for (INT16* dst = buffer; buffer_size > 0; dst++, buffer_size--)
83  {
84  *dst = (*dst + half) >> factor;
85  }
86 }
87 
88 void rfx_quantization_encode(INT16* WINPR_RESTRICT buffer,
89  const UINT32* WINPR_RESTRICT quantization_values)
90 {
91  WINPR_ASSERT(buffer);
92  WINPR_ASSERT(quantization_values);
93 
94  rfx_quantization_encode_block(buffer, 1024, quantization_values[8] - 6); /* HL1 */
95  rfx_quantization_encode_block(buffer + 1024, 1024, quantization_values[7] - 6); /* LH1 */
96  rfx_quantization_encode_block(buffer + 2048, 1024, quantization_values[9] - 6); /* HH1 */
97  rfx_quantization_encode_block(buffer + 3072, 256, quantization_values[5] - 6); /* HL2 */
98  rfx_quantization_encode_block(buffer + 3328, 256, quantization_values[4] - 6); /* LH2 */
99  rfx_quantization_encode_block(buffer + 3584, 256, quantization_values[6] - 6); /* HH2 */
100  rfx_quantization_encode_block(buffer + 3840, 64, quantization_values[2] - 6); /* HL3 */
101  rfx_quantization_encode_block(buffer + 3904, 64, quantization_values[1] - 6); /* LH3 */
102  rfx_quantization_encode_block(buffer + 3968, 64, quantization_values[3] - 6); /* HH3 */
103  rfx_quantization_encode_block(buffer + 4032, 64, quantization_values[0] - 6); /* LL3 */
104 
105  /* The coefficients are scaled by << 5 at RGB->YCbCr phase, so we round it back here */
106  rfx_quantization_encode_block(buffer, 4096, 5);
107 }