/src/FreeRDP/libfreerdp/codec/rfx_quantization.c
Line | Count | Source |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * RemoteFX Codec Library - Quantization |
4 | | * |
5 | | * Copyright 2011 Vic Lee |
6 | | * |
7 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | | * you may not use this file except in compliance with the License. |
9 | | * You may obtain a copy of the License at |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
13 | | * Unless required by applicable law or agreed to in writing, software |
14 | | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | | * See the License for the specific language governing permissions and |
17 | | * limitations under the License. |
18 | | */ |
19 | | |
20 | | #include <winpr/assert.h> |
21 | | #include <winpr/cast.h> |
22 | | |
23 | | #include <freerdp/config.h> |
24 | | |
25 | | #include <freerdp/primitives.h> |
26 | | |
27 | | #include "rfx_quantization.h" |
28 | | |
29 | | /* |
30 | | * Band Offset Dimensions Size |
31 | | * |
32 | | * HL1 0 32x32 1024 |
33 | | * LH1 1024 32x32 1024 |
34 | | * HH1 2048 32x32 1024 |
35 | | * |
36 | | * HL2 3072 16x16 256 |
37 | | * LH2 3328 16x16 256 |
38 | | * HH2 3584 16x16 256 |
39 | | * |
40 | | * HL3 3840 8x8 64 |
41 | | * LH3 3904 8x8 64 |
42 | | * HH3 3968 8x8 64 |
43 | | * |
44 | | * LL3 4032 8x8 64 |
45 | | */ |
46 | | |
47 | | static inline BOOL rfx_quantization_decode_block(const primitives_t* WINPR_RESTRICT prims, |
48 | | INT16* WINPR_RESTRICT buffer, UINT32 buffer_size, |
49 | | UINT32 factor) |
50 | 0 | { |
51 | 0 | if (factor == 0) |
52 | 0 | return TRUE; |
53 | | |
54 | 0 | return prims->lShiftC_16s_inplace(buffer, factor, buffer_size) == PRIMITIVES_SUCCESS; |
55 | 0 | } |
56 | | |
57 | | BOOL rfx_quantization_decode(INT16* WINPR_RESTRICT buffer, |
58 | | const UINT32* WINPR_RESTRICT quantization_values, size_t nrQuantValues) |
59 | 0 | { |
60 | 0 | const primitives_t* prims = primitives_get(); |
61 | 0 | WINPR_ASSERT(buffer); |
62 | 0 | WINPR_ASSERT(quantization_values); |
63 | 0 | WINPR_ASSERT(NR_QUANT_VALUES == nrQuantValues); |
64 | |
|
65 | 0 | for (size_t x = 0; x < nrQuantValues; x++) |
66 | 0 | { |
67 | 0 | const UINT32 val = quantization_values[x]; |
68 | |
|
69 | 0 | if (val < 1) |
70 | 0 | return FALSE; |
71 | 0 | } |
72 | | |
73 | 0 | rfx_quantization_decode_block(prims, &buffer[0], 1024, quantization_values[8] - 1); /* HL1 */ |
74 | 0 | rfx_quantization_decode_block(prims, &buffer[1024], 1024, quantization_values[7] - 1); /* LH1 */ |
75 | 0 | rfx_quantization_decode_block(prims, &buffer[2048], 1024, quantization_values[9] - 1); /* HH1 */ |
76 | 0 | rfx_quantization_decode_block(prims, &buffer[3072], 256, quantization_values[5] - 1); /* HL2 */ |
77 | 0 | rfx_quantization_decode_block(prims, &buffer[3328], 256, quantization_values[4] - 1); /* LH2 */ |
78 | 0 | rfx_quantization_decode_block(prims, &buffer[3584], 256, quantization_values[6] - 1); /* HH2 */ |
79 | 0 | rfx_quantization_decode_block(prims, &buffer[3840], 64, quantization_values[2] - 1); /* HL3 */ |
80 | 0 | rfx_quantization_decode_block(prims, &buffer[3904], 64, quantization_values[1] - 1); /* LH3 */ |
81 | 0 | rfx_quantization_decode_block(prims, &buffer[3968], 64, quantization_values[3] - 1); /* HH3 */ |
82 | 0 | rfx_quantization_decode_block(prims, &buffer[4032], 64, quantization_values[0] - 1); /* LL3 */ |
83 | 0 | return TRUE; |
84 | 0 | } |
85 | | |
86 | | static inline void rfx_quantization_encode_block(INT16* WINPR_RESTRICT buffer, size_t buffer_size, |
87 | | UINT32 factor) |
88 | 0 | { |
89 | 0 | if (factor == 0) |
90 | 0 | return; |
91 | | |
92 | 0 | const INT16 half = WINPR_ASSERTING_INT_CAST(INT16, 1u << (factor - 1)); |
93 | | /* Could probably use prims->rShiftC_16s(dst+half, factor, dst, buffer_size); */ |
94 | 0 | for (INT16* dst = buffer; buffer_size > 0; dst++, buffer_size--) |
95 | 0 | { |
96 | 0 | *dst = WINPR_ASSERTING_INT_CAST(INT16, (*dst + half) >> factor); |
97 | 0 | } |
98 | 0 | } |
99 | | |
100 | | WINPR_ATTR_NODISCARD |
101 | | BOOL rfx_quantization_encode(INT16* WINPR_RESTRICT buffer, |
102 | | const UINT32* WINPR_RESTRICT quantization_values, size_t nrQuantValues) |
103 | 0 | { |
104 | 0 | WINPR_ASSERT(buffer); |
105 | 0 | WINPR_ASSERT(quantization_values); |
106 | 0 | WINPR_ASSERT(nrQuantValues == NR_QUANT_VALUES); |
107 | |
|
108 | 0 | for (size_t x = 0; x < nrQuantValues; x++) |
109 | 0 | { |
110 | 0 | const UINT32 val = quantization_values[x]; |
111 | 0 | if (val < 6) |
112 | 0 | return FALSE; |
113 | 0 | } |
114 | | |
115 | 0 | rfx_quantization_encode_block(buffer, 1024, quantization_values[8] - 6); /* HL1 */ |
116 | 0 | rfx_quantization_encode_block(buffer + 1024, 1024, quantization_values[7] - 6); /* LH1 */ |
117 | 0 | rfx_quantization_encode_block(buffer + 2048, 1024, quantization_values[9] - 6); /* HH1 */ |
118 | 0 | rfx_quantization_encode_block(buffer + 3072, 256, quantization_values[5] - 6); /* HL2 */ |
119 | 0 | rfx_quantization_encode_block(buffer + 3328, 256, quantization_values[4] - 6); /* LH2 */ |
120 | 0 | rfx_quantization_encode_block(buffer + 3584, 256, quantization_values[6] - 6); /* HH2 */ |
121 | 0 | rfx_quantization_encode_block(buffer + 3840, 64, quantization_values[2] - 6); /* HL3 */ |
122 | 0 | rfx_quantization_encode_block(buffer + 3904, 64, quantization_values[1] - 6); /* LH3 */ |
123 | 0 | rfx_quantization_encode_block(buffer + 3968, 64, quantization_values[3] - 6); /* HH3 */ |
124 | 0 | rfx_quantization_encode_block(buffer + 4032, 64, quantization_values[0] - 6); /* LL3 */ |
125 | | |
126 | | /* The coefficients are scaled by << 5 at RGB->YCbCr phase, so we round it back here */ |
127 | 0 | rfx_quantization_encode_block(buffer, 4096, 5); |
128 | 0 | return TRUE; |
129 | 0 | } |