1 // SPDX-License-Identifier: LGPL-2.1+
2 /*
3  * Copyright 2016 Tom aan de Wiel
4  * Copyright 2018 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
5  *
6  * 8x8 Fast Walsh Hadamard Transform in sequency order based on the paper:
7  *
8  * A Recursive Algorithm for Sequency-Ordered Fast Walsh Transforms,
9  * R.D. Brown, 1977
10  */
11 
12 #include <string.h>
13 #include "codec-fwht.h"
14 
15 #define OVERFLOW_BIT BIT(14)
16 
17 /*
18  * Note: bit 0 of the header must always be 0. Otherwise it cannot
19  * be guaranteed that the magic 8 byte sequence (see below) can
20  * never occur in the rlc output.
21  */
22 #define PFRAME_BIT BIT(15)
23 #define DUPS_MASK 0x1ffe
24 
25 #define PBLOCK 0
26 #define IBLOCK 1
27 
28 #define ALL_ZEROS 15
29 
30 static const uint8_t zigzag[64] = {
31 	0,
32 	1,  8,
33 	2,  9, 16,
34 	3, 10, 17, 24,
35 	4, 11, 18, 25, 32,
36 	5, 12, 19, 26, 33, 40,
37 	6, 13, 20, 27, 34, 41, 48,
38 	7, 14, 21, 28, 35, 42, 49, 56,
39 	15, 22, 29, 36, 43, 50, 57,
40 	23, 30, 37, 44, 51, 58,
41 	31, 38, 45, 52, 59,
42 	39, 46, 53, 60,
43 	47, 54, 61,
44 	55, 62,
45 	63,
46 };
47 
48 /*
49  * noinline_for_stack to work around
50  * https://bugs.llvm.org/show_bug.cgi?id=38809
51  */
52 static int noinline_for_stack
rlc(const s16 * in,uint16_t * output,int blocktype)53 rlc(const s16 *in, uint16_t *output, int blocktype)
54 {
55 	s16 block[8 * 8];
56 	s16 *wp = block;
57 	int i = 0;
58 	int x, y;
59 	int ret = 0;
60 
61 	/* read in block from framebuffer */
62 	int lastzero_run = 0;
63 	int to_encode;
64 
65 	for (y = 0; y < 8; y++) {
66 		for (x = 0; x < 8; x++) {
67 			*wp = in[x + y * 8];
68 			wp++;
69 		}
70 	}
71 
72 	/* keep track of amount of trailing zeros */
73 	for (i = 63; i >= 0 && !block[zigzag[i]]; i--)
74 		lastzero_run++;
75 
76 	*output++ = (blocktype == PBLOCK ? htons(PFRAME_BIT) : 0);
77 	ret++;
78 
79 	to_encode = 8 * 8 - (lastzero_run > 14 ? lastzero_run : 0);
80 
81 	i = 0;
82 	while (i < to_encode) {
83 		int cnt = 0;
84 		int tmp;
85 
86 		/* count leading zeros */
87 		while ((tmp = block[zigzag[i]]) == 0 && cnt < 14) {
88 			cnt++;
89 			i++;
90 			if (i == to_encode) {
91 				cnt--;
92 				break;
93 			}
94 		}
95 		/* 4 bits for run, 12 for coefficient (quantization by 4) */
96 		*output++ = htons((cnt | tmp << 4));
97 		i++;
98 		ret++;
99 	}
100 	if (lastzero_run > 14) {
101 		*output = htons(ALL_ZEROS | 0);
102 		ret++;
103 	}
104 
105 	return ret;
106 }
107 
108 /*
109  * This function will worst-case increase rlc_in by 65*2 bytes:
110  * one s16 value for the header and 8 * 8 coefficients of type s16.
111  */
112 static noinline_for_stack u16
derlc(const uint16_t ** rlc_in,s16 * dwht_out,const uint16_t * end_of_input)113 derlc(const uint16_t **rlc_in, s16 *dwht_out, const uint16_t *end_of_input)
114 {
115 	/* header */
116 	const uint16_t *input = *rlc_in;
117 	u16 stat;
118 	int dec_count = 0;
119 	s16 block[8 * 8 + 16];
120 	s16 *wp = block;
121 	int i;
122 
123 	if (input > end_of_input)
124 		return OVERFLOW_BIT;
125 	stat = ntohs(*input++);
126 
127 	/*
128 	 * Now de-compress, it expands one byte to up to 15 bytes
129 	 * (or fills the remainder of the 64 bytes with zeroes if it
130 	 * is the last byte to expand).
131 	 *
132 	 * So block has to be 8 * 8 + 16 bytes, the '+ 16' is to
133 	 * allow for overflow if the incoming data was malformed.
134 	 */
135 	while (dec_count < 8 * 8) {
136 		s16 in;
137 		int length;
138 		int coeff;
139 
140 		if (input > end_of_input)
141 			return OVERFLOW_BIT;
142 		in = ntohs(*input++);
143 		length = in & 0xf;
144 		coeff = in >> 4;
145 
146 		/* fill remainder with zeros */
147 		if (length == 15) {
148 			for (i = 0; i < 64 - dec_count; i++)
149 				*wp++ = 0;
150 			break;
151 		}
152 
153 		for (i = 0; i < length; i++)
154 			*wp++ = 0;
155 		*wp++ = coeff;
156 		dec_count += length + 1;
157 	}
158 
159 	wp = block;
160 
161 	for (i = 0; i < 64; i++) {
162 		int pos = zigzag[i];
163 		int y = pos / 8;
164 		int x = pos % 8;
165 
166 		dwht_out[x + y * 8] = *wp++;
167 	}
168 	*rlc_in = input;
169 	return stat;
170 }
171 
172 static const int quant_table[] = {
173 	2, 2, 2, 2, 2, 2,  2,  2,
174 	2, 2, 2, 2, 2, 2,  2,  2,
175 	2, 2, 2, 2, 2, 2,  2,  3,
176 	2, 2, 2, 2, 2, 2,  3,  6,
177 	2, 2, 2, 2, 2, 3,  6,  6,
178 	2, 2, 2, 2, 3, 6,  6,  6,
179 	2, 2, 2, 3, 6, 6,  6,  6,
180 	2, 2, 3, 6, 6, 6,  6,  8,
181 };
182 
183 static const int quant_table_p[] = {
184 	3, 3, 3, 3, 3, 3,  3,  3,
185 	3, 3, 3, 3, 3, 3,  3,  3,
186 	3, 3, 3, 3, 3, 3,  3,  3,
187 	3, 3, 3, 3, 3, 3,  3,  6,
188 	3, 3, 3, 3, 3, 3,  6,  6,
189 	3, 3, 3, 3, 3, 6,  6,  9,
190 	3, 3, 3, 3, 6, 6,  9,  9,
191 	3, 3, 3, 6, 6, 9,  9,  10,
192 };
193 
quantize_intra(s16 * coeff,s16 * de_coeff,u16 qp)194 static void quantize_intra(s16 *coeff, s16 *de_coeff, u16 qp)
195 {
196 	const int *quant = quant_table;
197 	int i, j;
198 
199 	for (j = 0; j < 8; j++) {
200 		for (i = 0; i < 8; i++, quant++, coeff++, de_coeff++) {
201 			*coeff >>= *quant;
202 			if (*coeff >= -qp && *coeff <= qp)
203 				*coeff = *de_coeff = 0;
204 			else
205 				*de_coeff = *coeff << *quant;
206 		}
207 	}
208 }
209 
dequantize_intra(s16 * coeff)210 static void dequantize_intra(s16 *coeff)
211 {
212 	const int *quant = quant_table;
213 	int i, j;
214 
215 	for (j = 0; j < 8; j++)
216 		for (i = 0; i < 8; i++, quant++, coeff++)
217 			*coeff <<= *quant;
218 }
219 
quantize_inter(s16 * coeff,s16 * de_coeff,u16 qp)220 static void quantize_inter(s16 *coeff, s16 *de_coeff, u16 qp)
221 {
222 	const int *quant = quant_table_p;
223 	int i, j;
224 
225 	for (j = 0; j < 8; j++) {
226 		for (i = 0; i < 8; i++, quant++, coeff++, de_coeff++) {
227 			*coeff >>= *quant;
228 			if (*coeff >= -qp && *coeff <= qp)
229 				*coeff = *de_coeff = 0;
230 			else
231 				*de_coeff = *coeff << *quant;
232 		}
233 	}
234 }
235 
dequantize_inter(s16 * coeff)236 static void dequantize_inter(s16 *coeff)
237 {
238 	const int *quant = quant_table_p;
239 	int i, j;
240 
241 	for (j = 0; j < 8; j++)
242 		for (i = 0; i < 8; i++, quant++, coeff++)
243 			*coeff <<= *quant;
244 }
245 
fwht(const u8 * block,s16 * output_block,unsigned int stride,unsigned int input_step,bool intra)246 static void noinline_for_stack fwht(const u8 *block, s16 *output_block,
247 				    unsigned int stride,
248 				    unsigned int input_step, bool intra)
249 {
250 	/* we'll need more than 8 bits for the transformed coefficients */
251 	s32 workspace1[8], workspace2[8];
252 	const u8 *tmp = block;
253 	s16 *out = output_block;
254 	int add = intra ? 256 : 0;
255 	unsigned int i;
256 
257 	/* stage 1 */
258 	for (i = 0; i < 8; i++, tmp += stride, out += 8) {
259 		switch (input_step) {
260 		case 1:
261 			workspace1[0]  = tmp[0] + tmp[1] - add;
262 			workspace1[1]  = tmp[0] - tmp[1];
263 
264 			workspace1[2]  = tmp[2] + tmp[3] - add;
265 			workspace1[3]  = tmp[2] - tmp[3];
266 
267 			workspace1[4]  = tmp[4] + tmp[5] - add;
268 			workspace1[5]  = tmp[4] - tmp[5];
269 
270 			workspace1[6]  = tmp[6] + tmp[7] - add;
271 			workspace1[7]  = tmp[6] - tmp[7];
272 			break;
273 		case 2:
274 			workspace1[0]  = tmp[0] + tmp[2] - add;
275 			workspace1[1]  = tmp[0] - tmp[2];
276 
277 			workspace1[2]  = tmp[4] + tmp[6] - add;
278 			workspace1[3]  = tmp[4] - tmp[6];
279 
280 			workspace1[4]  = tmp[8] + tmp[10] - add;
281 			workspace1[5]  = tmp[8] - tmp[10];
282 
283 			workspace1[6]  = tmp[12] + tmp[14] - add;
284 			workspace1[7]  = tmp[12] - tmp[14];
285 			break;
286 		case 3:
287 			workspace1[0]  = tmp[0] + tmp[3] - add;
288 			workspace1[1]  = tmp[0] - tmp[3];
289 
290 			workspace1[2]  = tmp[6] + tmp[9] - add;
291 			workspace1[3]  = tmp[6] - tmp[9];
292 
293 			workspace1[4]  = tmp[12] + tmp[15] - add;
294 			workspace1[5]  = tmp[12] - tmp[15];
295 
296 			workspace1[6]  = tmp[18] + tmp[21] - add;
297 			workspace1[7]  = tmp[18] - tmp[21];
298 			break;
299 		default:
300 			workspace1[0]  = tmp[0] + tmp[4] - add;
301 			workspace1[1]  = tmp[0] - tmp[4];
302 
303 			workspace1[2]  = tmp[8] + tmp[12] - add;
304 			workspace1[3]  = tmp[8] - tmp[12];
305 
306 			workspace1[4]  = tmp[16] + tmp[20] - add;
307 			workspace1[5]  = tmp[16] - tmp[20];
308 
309 			workspace1[6]  = tmp[24] + tmp[28] - add;
310 			workspace1[7]  = tmp[24] - tmp[28];
311 			break;
312 		}
313 
314 		/* stage 2 */
315 		workspace2[0] = workspace1[0] + workspace1[2];
316 		workspace2[1] = workspace1[0] - workspace1[2];
317 		workspace2[2] = workspace1[1] - workspace1[3];
318 		workspace2[3] = workspace1[1] + workspace1[3];
319 
320 		workspace2[4] = workspace1[4] + workspace1[6];
321 		workspace2[5] = workspace1[4] - workspace1[6];
322 		workspace2[6] = workspace1[5] - workspace1[7];
323 		workspace2[7] = workspace1[5] + workspace1[7];
324 
325 		/* stage 3 */
326 		out[0] = workspace2[0] + workspace2[4];
327 		out[1] = workspace2[0] - workspace2[4];
328 		out[2] = workspace2[1] - workspace2[5];
329 		out[3] = workspace2[1] + workspace2[5];
330 		out[4] = workspace2[2] + workspace2[6];
331 		out[5] = workspace2[2] - workspace2[6];
332 		out[6] = workspace2[3] - workspace2[7];
333 		out[7] = workspace2[3] + workspace2[7];
334 	}
335 
336 	out = output_block;
337 
338 	for (i = 0; i < 8; i++, out++) {
339 		/* stage 1 */
340 		workspace1[0]  = out[0] + out[1 * 8];
341 		workspace1[1]  = out[0] - out[1 * 8];
342 
343 		workspace1[2]  = out[2 * 8] + out[3 * 8];
344 		workspace1[3]  = out[2 * 8] - out[3 * 8];
345 
346 		workspace1[4]  = out[4 * 8] + out[5 * 8];
347 		workspace1[5]  = out[4 * 8] - out[5 * 8];
348 
349 		workspace1[6]  = out[6 * 8] + out[7 * 8];
350 		workspace1[7]  = out[6 * 8] - out[7 * 8];
351 
352 		/* stage 2 */
353 		workspace2[0] = workspace1[0] + workspace1[2];
354 		workspace2[1] = workspace1[0] - workspace1[2];
355 		workspace2[2] = workspace1[1] - workspace1[3];
356 		workspace2[3] = workspace1[1] + workspace1[3];
357 
358 		workspace2[4] = workspace1[4] + workspace1[6];
359 		workspace2[5] = workspace1[4] - workspace1[6];
360 		workspace2[6] = workspace1[5] - workspace1[7];
361 		workspace2[7] = workspace1[5] + workspace1[7];
362 		/* stage 3 */
363 		out[0 * 8] = workspace2[0] + workspace2[4];
364 		out[1 * 8] = workspace2[0] - workspace2[4];
365 		out[2 * 8] = workspace2[1] - workspace2[5];
366 		out[3 * 8] = workspace2[1] + workspace2[5];
367 		out[4 * 8] = workspace2[2] + workspace2[6];
368 		out[5 * 8] = workspace2[2] - workspace2[6];
369 		out[6 * 8] = workspace2[3] - workspace2[7];
370 		out[7 * 8] = workspace2[3] + workspace2[7];
371 	}
372 }
373 
374 /*
375  * Not the nicest way of doing it, but P-blocks get twice the range of
376  * that of the I-blocks. Therefore we need a type bigger than 8 bits.
377  * Furthermore values can be negative... This is just a version that
378  * works with 16 signed data
379  */
380 static void noinline_for_stack
fwht16(const s16 * block,s16 * output_block,int stride,int intra)381 fwht16(const s16 *block, s16 *output_block, int stride, int intra)
382 {
383 	/* we'll need more than 8 bits for the transformed coefficients */
384 	s32 workspace1[8], workspace2[8];
385 	const s16 *tmp = block;
386 	s16 *out = output_block;
387 	int i;
388 
389 	for (i = 0; i < 8; i++, tmp += stride, out += 8) {
390 		/* stage 1 */
391 		workspace1[0]  = tmp[0] + tmp[1];
392 		workspace1[1]  = tmp[0] - tmp[1];
393 
394 		workspace1[2]  = tmp[2] + tmp[3];
395 		workspace1[3]  = tmp[2] - tmp[3];
396 
397 		workspace1[4]  = tmp[4] + tmp[5];
398 		workspace1[5]  = tmp[4] - tmp[5];
399 
400 		workspace1[6]  = tmp[6] + tmp[7];
401 		workspace1[7]  = tmp[6] - tmp[7];
402 
403 		/* stage 2 */
404 		workspace2[0] = workspace1[0] + workspace1[2];
405 		workspace2[1] = workspace1[0] - workspace1[2];
406 		workspace2[2] = workspace1[1] - workspace1[3];
407 		workspace2[3] = workspace1[1] + workspace1[3];
408 
409 		workspace2[4] = workspace1[4] + workspace1[6];
410 		workspace2[5] = workspace1[4] - workspace1[6];
411 		workspace2[6] = workspace1[5] - workspace1[7];
412 		workspace2[7] = workspace1[5] + workspace1[7];
413 
414 		/* stage 3 */
415 		out[0] = workspace2[0] + workspace2[4];
416 		out[1] = workspace2[0] - workspace2[4];
417 		out[2] = workspace2[1] - workspace2[5];
418 		out[3] = workspace2[1] + workspace2[5];
419 		out[4] = workspace2[2] + workspace2[6];
420 		out[5] = workspace2[2] - workspace2[6];
421 		out[6] = workspace2[3] - workspace2[7];
422 		out[7] = workspace2[3] + workspace2[7];
423 	}
424 
425 	out = output_block;
426 
427 	for (i = 0; i < 8; i++, out++) {
428 		/* stage 1 */
429 		workspace1[0]  = out[0] + out[1*8];
430 		workspace1[1]  = out[0] - out[1*8];
431 
432 		workspace1[2]  = out[2*8] + out[3*8];
433 		workspace1[3]  = out[2*8] - out[3*8];
434 
435 		workspace1[4]  = out[4*8] + out[5*8];
436 		workspace1[5]  = out[4*8] - out[5*8];
437 
438 		workspace1[6]  = out[6*8] + out[7*8];
439 		workspace1[7]  = out[6*8] - out[7*8];
440 
441 		/* stage 2 */
442 		workspace2[0] = workspace1[0] + workspace1[2];
443 		workspace2[1] = workspace1[0] - workspace1[2];
444 		workspace2[2] = workspace1[1] - workspace1[3];
445 		workspace2[3] = workspace1[1] + workspace1[3];
446 
447 		workspace2[4] = workspace1[4] + workspace1[6];
448 		workspace2[5] = workspace1[4] - workspace1[6];
449 		workspace2[6] = workspace1[5] - workspace1[7];
450 		workspace2[7] = workspace1[5] + workspace1[7];
451 
452 		/* stage 3 */
453 		out[0*8] = workspace2[0] + workspace2[4];
454 		out[1*8] = workspace2[0] - workspace2[4];
455 		out[2*8] = workspace2[1] - workspace2[5];
456 		out[3*8] = workspace2[1] + workspace2[5];
457 		out[4*8] = workspace2[2] + workspace2[6];
458 		out[5*8] = workspace2[2] - workspace2[6];
459 		out[6*8] = workspace2[3] - workspace2[7];
460 		out[7*8] = workspace2[3] + workspace2[7];
461 	}
462 }
463 
464 static noinline_for_stack void
ifwht(const s16 * block,s16 * output_block,int intra)465 ifwht(const s16 *block, s16 *output_block, int intra)
466 {
467 	/*
468 	 * we'll need more than 8 bits for the transformed coefficients
469 	 * use native unit of cpu
470 	 */
471 	int workspace1[8], workspace2[8];
472 	int inter = intra ? 0 : 1;
473 	const s16 *tmp = block;
474 	s16 *out = output_block;
475 	int i;
476 
477 	for (i = 0; i < 8; i++, tmp += 8, out += 8) {
478 		/* stage 1 */
479 		workspace1[0]  = tmp[0] + tmp[1];
480 		workspace1[1]  = tmp[0] - tmp[1];
481 
482 		workspace1[2]  = tmp[2] + tmp[3];
483 		workspace1[3]  = tmp[2] - tmp[3];
484 
485 		workspace1[4]  = tmp[4] + tmp[5];
486 		workspace1[5]  = tmp[4] - tmp[5];
487 
488 		workspace1[6]  = tmp[6] + tmp[7];
489 		workspace1[7]  = tmp[6] - tmp[7];
490 
491 		/* stage 2 */
492 		workspace2[0] = workspace1[0] + workspace1[2];
493 		workspace2[1] = workspace1[0] - workspace1[2];
494 		workspace2[2] = workspace1[1] - workspace1[3];
495 		workspace2[3] = workspace1[1] + workspace1[3];
496 
497 		workspace2[4] = workspace1[4] + workspace1[6];
498 		workspace2[5] = workspace1[4] - workspace1[6];
499 		workspace2[6] = workspace1[5] - workspace1[7];
500 		workspace2[7] = workspace1[5] + workspace1[7];
501 
502 		/* stage 3 */
503 		out[0] = workspace2[0] + workspace2[4];
504 		out[1] = workspace2[0] - workspace2[4];
505 		out[2] = workspace2[1] - workspace2[5];
506 		out[3] = workspace2[1] + workspace2[5];
507 		out[4] = workspace2[2] + workspace2[6];
508 		out[5] = workspace2[2] - workspace2[6];
509 		out[6] = workspace2[3] - workspace2[7];
510 		out[7] = workspace2[3] + workspace2[7];
511 	}
512 
513 	out = output_block;
514 
515 	for (i = 0; i < 8; i++, out++) {
516 		/* stage 1 */
517 		workspace1[0]  = out[0] + out[1 * 8];
518 		workspace1[1]  = out[0] - out[1 * 8];
519 
520 		workspace1[2]  = out[2 * 8] + out[3 * 8];
521 		workspace1[3]  = out[2 * 8] - out[3 * 8];
522 
523 		workspace1[4]  = out[4 * 8] + out[5 * 8];
524 		workspace1[5]  = out[4 * 8] - out[5 * 8];
525 
526 		workspace1[6]  = out[6 * 8] + out[7 * 8];
527 		workspace1[7]  = out[6 * 8] - out[7 * 8];
528 
529 		/* stage 2 */
530 		workspace2[0] = workspace1[0] + workspace1[2];
531 		workspace2[1] = workspace1[0] - workspace1[2];
532 		workspace2[2] = workspace1[1] - workspace1[3];
533 		workspace2[3] = workspace1[1] + workspace1[3];
534 
535 		workspace2[4] = workspace1[4] + workspace1[6];
536 		workspace2[5] = workspace1[4] - workspace1[6];
537 		workspace2[6] = workspace1[5] - workspace1[7];
538 		workspace2[7] = workspace1[5] + workspace1[7];
539 
540 		/* stage 3 */
541 		if (inter) {
542 			int d;
543 
544 			out[0 * 8] = workspace2[0] + workspace2[4];
545 			out[1 * 8] = workspace2[0] - workspace2[4];
546 			out[2 * 8] = workspace2[1] - workspace2[5];
547 			out[3 * 8] = workspace2[1] + workspace2[5];
548 			out[4 * 8] = workspace2[2] + workspace2[6];
549 			out[5 * 8] = workspace2[2] - workspace2[6];
550 			out[6 * 8] = workspace2[3] - workspace2[7];
551 			out[7 * 8] = workspace2[3] + workspace2[7];
552 
553 			for (d = 0; d < 8; d++)
554 				out[8 * d] >>= 6;
555 		} else {
556 			int d;
557 
558 			out[0 * 8] = workspace2[0] + workspace2[4];
559 			out[1 * 8] = workspace2[0] - workspace2[4];
560 			out[2 * 8] = workspace2[1] - workspace2[5];
561 			out[3 * 8] = workspace2[1] + workspace2[5];
562 			out[4 * 8] = workspace2[2] + workspace2[6];
563 			out[5 * 8] = workspace2[2] - workspace2[6];
564 			out[6 * 8] = workspace2[3] - workspace2[7];
565 			out[7 * 8] = workspace2[3] + workspace2[7];
566 
567 			for (d = 0; d < 8; d++) {
568 				out[8 * d] >>= 6;
569 				out[8 * d] += 128;
570 			}
571 		}
572 	}
573 }
574 
fill_encoder_block(const u8 * input,s16 * dst,unsigned int stride,unsigned int input_step)575 static void fill_encoder_block(const u8 *input, s16 *dst,
576 			       unsigned int stride, unsigned int input_step)
577 {
578 	int i, j;
579 
580 	for (i = 0; i < 8; i++) {
581 		for (j = 0; j < 8; j++, input += input_step)
582 			*dst++ = *input;
583 		input += stride - 8 * input_step;
584 	}
585 }
586 
var_intra(const s16 * input)587 static int var_intra(const s16 *input)
588 {
589 	int32_t mean = 0;
590 	int32_t ret = 0;
591 	const s16 *tmp = input;
592 	int i;
593 
594 	for (i = 0; i < 8 * 8; i++, tmp++)
595 		mean += *tmp;
596 	mean /= 64;
597 	tmp = input;
598 	for (i = 0; i < 8 * 8; i++, tmp++)
599 		ret += (*tmp - mean) < 0 ? -(*tmp - mean) : (*tmp - mean);
600 	return ret;
601 }
602 
var_inter(const s16 * old,const s16 * new)603 static int var_inter(const s16 *old, const s16 *new)
604 {
605 	int32_t ret = 0;
606 	int i;
607 
608 	for (i = 0; i < 8 * 8; i++, old++, new++)
609 		ret += (*old - *new) < 0 ? -(*old - *new) : (*old - *new);
610 	return ret;
611 }
612 
613 static noinline_for_stack int
decide_blocktype(const u8 * cur,const u8 * reference,s16 * deltablock,unsigned int stride,unsigned int input_step)614 decide_blocktype(const u8 *cur, const u8 *reference, s16 *deltablock,
615 		 unsigned int stride, unsigned int input_step)
616 {
617 	s16 tmp[64];
618 	s16 old[64];
619 	s16 *work = tmp;
620 	unsigned int k, l;
621 	int vari;
622 	int vard;
623 
624 	fill_encoder_block(cur, tmp, stride, input_step);
625 	fill_encoder_block(reference, old, 8, 1);
626 	vari = var_intra(tmp);
627 
628 	for (k = 0; k < 8; k++) {
629 		for (l = 0; l < 8; l++) {
630 			*deltablock = *work - *reference;
631 			deltablock++;
632 			work++;
633 			reference++;
634 		}
635 	}
636 	deltablock -= 64;
637 	vard = var_inter(old, tmp);
638 	return vari <= vard ? IBLOCK : PBLOCK;
639 }
640 
fill_decoder_block(u8 * dst,const s16 * input,int stride,unsigned int dst_step)641 static void fill_decoder_block(u8 *dst, const s16 *input, int stride,
642 			       unsigned int dst_step)
643 {
644 	int i, j;
645 
646 	for (i = 0; i < 8; i++) {
647 		for (j = 0; j < 8; j++, input++, dst += dst_step) {
648 			if (*input < 0)
649 				*dst = 0;
650 			else if (*input > 255)
651 				*dst = 255;
652 			else
653 				*dst = *input;
654 		}
655 		dst += stride - (8 * dst_step);
656 	}
657 }
658 
add_deltas(s16 * deltas,const u8 * ref,int stride,unsigned int ref_step)659 static void add_deltas(s16 *deltas, const u8 *ref, int stride,
660 		       unsigned int ref_step)
661 {
662 	int k, l;
663 
664 	for (k = 0; k < 8; k++) {
665 		for (l = 0; l < 8; l++) {
666 			*deltas += *ref;
667 			ref += ref_step;
668 			/*
669 			 * Due to quantizing, it might possible that the
670 			 * decoded coefficients are slightly out of range
671 			 */
672 			if (*deltas < 0)
673 				*deltas = 0;
674 			else if (*deltas > 255)
675 				*deltas = 255;
676 			deltas++;
677 		}
678 		ref += stride - (8 * ref_step);
679 	}
680 }
681 
encode_plane(u8 * input,u8 * refp,uint16_t ** rlco,uint16_t * rlco_max,struct fwht_cframe * cf,u32 height,u32 width,u32 stride,unsigned int input_step,bool is_intra,bool next_is_intra)682 static u32 encode_plane(u8 *input, u8 *refp, uint16_t **rlco, uint16_t *rlco_max,
683 			struct fwht_cframe *cf, u32 height, u32 width,
684 			u32 stride, unsigned int input_step,
685 			bool is_intra, bool next_is_intra)
686 {
687 	u8 *input_start = input;
688 	uint16_t *rlco_start = *rlco;
689 	s16 deltablock[64];
690 	uint16_t pframe_bit = htons(PFRAME_BIT);
691 	u32 encoding = 0;
692 	unsigned int last_size = 0;
693 	unsigned int i, j;
694 
695 	width = round_up(width, 8);
696 	height = round_up(height, 8);
697 
698 	for (j = 0; j < height / 8; j++) {
699 		input = input_start + j * 8 * stride;
700 		for (i = 0; i < width / 8; i++) {
701 			/* intra code, first frame is always intra coded. */
702 			int blocktype = IBLOCK;
703 			unsigned int size;
704 
705 			if (!is_intra)
706 				blocktype = decide_blocktype(input, refp,
707 					deltablock, stride, input_step);
708 			if (blocktype == IBLOCK) {
709 				fwht(input, cf->coeffs, stride, input_step, 1);
710 				quantize_intra(cf->coeffs, cf->de_coeffs,
711 					       cf->i_frame_qp);
712 			} else {
713 				/* inter code */
714 				encoding |= FWHT_FRAME_PCODED;
715 				fwht16(deltablock, cf->coeffs, 8, 0);
716 				quantize_inter(cf->coeffs, cf->de_coeffs,
717 					       cf->p_frame_qp);
718 			}
719 			if (!next_is_intra) {
720 				ifwht(cf->de_coeffs, cf->de_fwht, blocktype);
721 
722 				if (blocktype == PBLOCK)
723 					add_deltas(cf->de_fwht, refp, 8, 1);
724 				fill_decoder_block(refp, cf->de_fwht, 8, 1);
725 			}
726 
727 			input += 8 * input_step;
728 			refp += 8 * 8;
729 
730 			size = rlc(cf->coeffs, *rlco, blocktype);
731 			if (last_size == size &&
732 			    !memcmp(*rlco + 1, *rlco - size + 1, 2 * size - 2)) {
733 				uint16_t *last_rlco = *rlco - size;
734 				s16 hdr = ntohs(*last_rlco);
735 
736 				if (!((*last_rlco ^ **rlco) & pframe_bit) &&
737 				    (hdr & DUPS_MASK) < DUPS_MASK)
738 					*last_rlco = htons(hdr + 2);
739 				else
740 					*rlco += size;
741 			} else {
742 				*rlco += size;
743 			}
744 			if (*rlco >= rlco_max) {
745 				encoding |= FWHT_FRAME_UNENCODED;
746 				goto exit_loop;
747 			}
748 			last_size = size;
749 		}
750 	}
751 
752 exit_loop:
753 	if (encoding & FWHT_FRAME_UNENCODED) {
754 		u8 *out = (u8 *)rlco_start;
755 		u8 *p;
756 
757 		input = input_start;
758 		/*
759 		 * The compressed stream should never contain the magic
760 		 * header, so when we copy the YUV data we replace 0xff
761 		 * by 0xfe. Since YUV is limited range such values
762 		 * shouldn't appear anyway.
763 		 */
764 		for (j = 0; j < height; j++) {
765 			for (i = 0, p = input; i < width; i++, p += input_step)
766 				*out++ = (*p == 0xff) ? 0xfe : *p;
767 			input += stride;
768 		}
769 		*rlco = (uint16_t *)out;
770 		encoding &= ~FWHT_FRAME_PCODED;
771 	}
772 	return encoding;
773 }
774 
fwht_encode_frame(struct fwht_raw_frame * frm,struct fwht_raw_frame * ref_frm,struct fwht_cframe * cf,bool is_intra,bool next_is_intra,unsigned int width,unsigned int height,unsigned int stride,unsigned int chroma_stride)775 u32 fwht_encode_frame(struct fwht_raw_frame *frm,
776 		      struct fwht_raw_frame *ref_frm,
777 		      struct fwht_cframe *cf,
778 		      bool is_intra, bool next_is_intra,
779 		      unsigned int width, unsigned int height,
780 		      unsigned int stride, unsigned int chroma_stride)
781 {
782 	unsigned int size = height * width;
783 	uint16_t *rlco = cf->rlc_data;
784 	uint16_t *rlco_max;
785 	u32 encoding;
786 
787 	rlco_max = rlco + size / 2 - 256;
788 	encoding = encode_plane(frm->luma, ref_frm->luma, &rlco, rlco_max, cf,
789 				height, width, stride,
790 				frm->luma_alpha_step, is_intra, next_is_intra);
791 	if (encoding & FWHT_FRAME_UNENCODED)
792 		encoding |= FWHT_LUMA_UNENCODED;
793 	encoding &= ~FWHT_FRAME_UNENCODED;
794 
795 	if (frm->components_num >= 3) {
796 		u32 chroma_h = height / frm->height_div;
797 		u32 chroma_w = width / frm->width_div;
798 		unsigned int chroma_size = chroma_h * chroma_w;
799 
800 		rlco_max = rlco + chroma_size / 2 - 256;
801 		encoding |= encode_plane(frm->cb, ref_frm->cb, &rlco, rlco_max,
802 					 cf, chroma_h, chroma_w,
803 					 chroma_stride, frm->chroma_step,
804 					 is_intra, next_is_intra);
805 		if (encoding & FWHT_FRAME_UNENCODED)
806 			encoding |= FWHT_CB_UNENCODED;
807 		encoding &= ~FWHT_FRAME_UNENCODED;
808 		rlco_max = rlco + chroma_size / 2 - 256;
809 		encoding |= encode_plane(frm->cr, ref_frm->cr, &rlco, rlco_max,
810 					 cf, chroma_h, chroma_w,
811 					 chroma_stride, frm->chroma_step,
812 					 is_intra, next_is_intra);
813 		if (encoding & FWHT_FRAME_UNENCODED)
814 			encoding |= FWHT_CR_UNENCODED;
815 		encoding &= ~FWHT_FRAME_UNENCODED;
816 	}
817 
818 	if (frm->components_num == 4) {
819 		rlco_max = rlco + size / 2 - 256;
820 		encoding |= encode_plane(frm->alpha, ref_frm->alpha, &rlco,
821 					 rlco_max, cf, height, width,
822 					 stride, frm->luma_alpha_step,
823 					 is_intra, next_is_intra);
824 		if (encoding & FWHT_FRAME_UNENCODED)
825 			encoding |= FWHT_ALPHA_UNENCODED;
826 		encoding &= ~FWHT_FRAME_UNENCODED;
827 	}
828 
829 	cf->size = (rlco - cf->rlc_data) * sizeof(*rlco);
830 	return encoding;
831 }
832 
decode_plane(struct fwht_cframe * cf,const uint16_t ** rlco,u32 height,u32 width,const u8 * ref,u32 ref_stride,unsigned int ref_step,u8 * dst,unsigned int dst_stride,unsigned int dst_step,bool uncompressed,const uint16_t * end_of_rlco_buf)833 static bool decode_plane(struct fwht_cframe *cf, const uint16_t **rlco,
834 			 u32 height, u32 width, const u8 *ref, u32 ref_stride,
835 			 unsigned int ref_step, u8 *dst,
836 			 unsigned int dst_stride, unsigned int dst_step,
837 			 bool uncompressed, const uint16_t *end_of_rlco_buf)
838 {
839 	unsigned int copies = 0;
840 	s16 copy[8 * 8];
841 	u16 stat;
842 	unsigned int i, j;
843 	bool is_intra = !ref;
844 
845 	width = round_up(width, 8);
846 	height = round_up(height, 8);
847 
848 	if (uncompressed) {
849 		int i;
850 
851 		if (end_of_rlco_buf + 1 < *rlco + width * height / 2)
852 			return false;
853 		for (i = 0; i < height; i++) {
854 			memcpy(dst, *rlco, width);
855 			dst += dst_stride;
856 			*rlco += width / 2;
857 		}
858 		return true;
859 	}
860 
861 	/*
862 	 * When decoding each macroblock the rlco pointer will be increased
863 	 * by 65 * 2 bytes worst-case.
864 	 * To avoid overflow the buffer has to be 65/64th of the actual raw
865 	 * image size, just in case someone feeds it malicious data.
866 	 */
867 	for (j = 0; j < height / 8; j++) {
868 		for (i = 0; i < width / 8; i++) {
869 			const u8 *refp = ref + j * 8 * ref_stride +
870 				i * 8 * ref_step;
871 			u8 *dstp = dst + j * 8 * dst_stride + i * 8 * dst_step;
872 
873 			if (copies) {
874 				memcpy(cf->de_fwht, copy, sizeof(copy));
875 				if ((stat & PFRAME_BIT) && !is_intra)
876 					add_deltas(cf->de_fwht, refp,
877 						   ref_stride, ref_step);
878 				fill_decoder_block(dstp, cf->de_fwht,
879 						   dst_stride, dst_step);
880 				copies--;
881 				continue;
882 			}
883 
884 			stat = derlc(rlco, cf->coeffs, end_of_rlco_buf);
885 			if (stat & OVERFLOW_BIT)
886 				return false;
887 			if ((stat & PFRAME_BIT) && !is_intra)
888 				dequantize_inter(cf->coeffs);
889 			else
890 				dequantize_intra(cf->coeffs);
891 
892 			ifwht(cf->coeffs, cf->de_fwht,
893 			      ((stat & PFRAME_BIT) && !is_intra) ? 0 : 1);
894 
895 			copies = (stat & DUPS_MASK) >> 1;
896 			if (copies)
897 				memcpy(copy, cf->de_fwht, sizeof(copy));
898 			if ((stat & PFRAME_BIT) && !is_intra)
899 				add_deltas(cf->de_fwht, refp,
900 					   ref_stride, ref_step);
901 			fill_decoder_block(dstp, cf->de_fwht, dst_stride,
902 					   dst_step);
903 		}
904 	}
905 	return true;
906 }
907 
fwht_decode_frame(struct fwht_cframe * cf,u32 hdr_flags,unsigned int components_num,unsigned int width,unsigned int height,const struct fwht_raw_frame * ref,unsigned int ref_stride,unsigned int ref_chroma_stride,struct fwht_raw_frame * dst,unsigned int dst_stride,unsigned int dst_chroma_stride)908 bool fwht_decode_frame(struct fwht_cframe *cf, u32 hdr_flags,
909 		       unsigned int components_num, unsigned int width,
910 		       unsigned int height, const struct fwht_raw_frame *ref,
911 		       unsigned int ref_stride, unsigned int ref_chroma_stride,
912 		       struct fwht_raw_frame *dst, unsigned int dst_stride,
913 		       unsigned int dst_chroma_stride)
914 {
915 	const uint16_t *rlco = cf->rlc_data;
916 	const uint16_t *end_of_rlco_buf = cf->rlc_data +
917 			(cf->size / sizeof(*rlco)) - 1;
918 
919 	if (!decode_plane(cf, &rlco, height, width, ref->luma, ref_stride,
920 			  ref->luma_alpha_step, dst->luma, dst_stride,
921 			  dst->luma_alpha_step,
922 			  hdr_flags & FWHT_FL_LUMA_IS_UNCOMPRESSED,
923 			  end_of_rlco_buf))
924 		return false;
925 
926 	if (components_num >= 3) {
927 		u32 h = height;
928 		u32 w = width;
929 
930 		if (!(hdr_flags & FWHT_FL_CHROMA_FULL_HEIGHT))
931 			h /= 2;
932 		if (!(hdr_flags & FWHT_FL_CHROMA_FULL_WIDTH))
933 			w /= 2;
934 
935 		if (!decode_plane(cf, &rlco, h, w, ref->cb, ref_chroma_stride,
936 				  ref->chroma_step, dst->cb, dst_chroma_stride,
937 				  dst->chroma_step,
938 				  hdr_flags & FWHT_FL_CB_IS_UNCOMPRESSED,
939 				  end_of_rlco_buf))
940 			return false;
941 		if (!decode_plane(cf, &rlco, h, w, ref->cr, ref_chroma_stride,
942 				  ref->chroma_step, dst->cr, dst_chroma_stride,
943 				  dst->chroma_step,
944 				  hdr_flags & FWHT_FL_CR_IS_UNCOMPRESSED,
945 				  end_of_rlco_buf))
946 			return false;
947 	}
948 
949 	if (components_num == 4)
950 		if (!decode_plane(cf, &rlco, height, width, ref->alpha, ref_stride,
951 				  ref->luma_alpha_step, dst->alpha, dst_stride,
952 				  dst->luma_alpha_step,
953 				  hdr_flags & FWHT_FL_ALPHA_IS_UNCOMPRESSED,
954 				  end_of_rlco_buf))
955 			return false;
956 	return true;
957 }
958