• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
7 #ifndef CODEC_FWHT_H
8 #define CODEC_FWHT_H
9 
10 #include <linux/types.h>
11 #include <string.h>
12 #include <errno.h>
13 #include <arpa/inet.h>
14 #include <stdbool.h>
15 #include <stdint.h>
16 
17 #define BIT(x) (1 << (x))
18 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
19 #define GENMASK(h, l) \
20 	(((~0UL) - (1UL << (l)) + 1) & (~0UL >> ((8 * sizeof(long)) - 1 - (h))))
21 #define pr_err(arg...)
22 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
23 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
24 #define noinline_for_stack
25 
26 
27 typedef __u64 u64;
28 typedef __u32 u32;
29 typedef __u16 u16;
30 typedef __s16 s16;
31 typedef __s32 s32;
32 typedef __u8 u8;
33 
34 /*
35  * The compressed format consists of a fwht_cframe_hdr struct followed by the
36  * compressed frame data. The header contains the size of that data.
37  * Each Y, Cb and Cr plane is compressed separately. If the compressed
38  * size of each plane becomes larger than the uncompressed size, then
39  * that plane is stored uncompressed and the corresponding bit is set
40  * in the flags field of the header.
41  *
42  * Each compressed plane consists of macroblocks and each macroblock
43  * is run-length-encoded. Each macroblock starts with a 16 bit value.
44  * Bit 15 indicates if this is a P-coded macroblock (1) or not (0).
45  * P-coded macroblocks contain a delta against the previous frame.
46  *
47  * Bits 1-12 contain a number. If non-zero, then this same macroblock
48  * repeats that number of times. This results in a high degree of
49  * compression for generated images like colorbars.
50  *
51  * Following this macroblock header the MB coefficients are run-length
52  * encoded: the top 12 bits contain the coefficient, the bottom 4 bits
53  * tell how many times this coefficient occurs. The value 0xf indicates
54  * that the remainder of the macroblock should be filled with zeroes.
55  *
56  * All 16 and 32 bit values are stored in big-endian (network) order.
57  *
58  * Each fwht_cframe_hdr starts with an 8 byte magic header that is
59  * guaranteed not to occur in the compressed frame data. This header
60  * can be used to sync to the next frame.
61  *
62  * This codec uses the Fast Walsh Hadamard Transform. Tom aan de Wiel
63  * developed this as part of a university project, specifically for use
64  * with this driver. His project report can be found here:
65  *
66  * https://hverkuil.home.xs4all.nl/fwht.pdf
67  */
68 
69 /*
70  * This is a sequence of 8 bytes with the low 4 bits set to 0xf.
71  *
72  * This sequence cannot occur in the encoded data
73  *
74  * Note that these two magic values are symmetrical so endian issues here.
75  */
76 #define FWHT_MAGIC1 0x4f4f4f4f
77 #define FWHT_MAGIC2 0xffffffff
78 
79 /*
80  * A macro to calculate the needed padding in order to make sure
81  * both luma and chroma components resolutions are rounded up to
82  * a multiple of 8
83  */
84 #define vic_round_dim(dim, div) (round_up((dim) / (div), 8) * (div))
85 
86 struct fwht_cframe_hdr {
87 	u32 magic1;
88 	u32 magic2;
89 	__be32 version;
90 	__be32 width, height;
91 	__be32 flags;
92 	__be32 colorspace;
93 	__be32 xfer_func;
94 	__be32 ycbcr_enc;
95 	__be32 quantization;
96 	__be32 size;
97 };
98 
99 struct fwht_cframe {
100 	u16 i_frame_qp;
101 	u16 p_frame_qp;
102 	__be16 *rlc_data;
103 	s16 coeffs[8 * 8];
104 	s16 de_coeffs[8 * 8];
105 	s16 de_fwht[8 * 8];
106 	u32 size;
107 };
108 
109 struct fwht_raw_frame {
110 	unsigned int width_div;
111 	unsigned int height_div;
112 	unsigned int luma_alpha_step;
113 	unsigned int chroma_step;
114 	unsigned int components_num;
115 	u8 *buf;
116 	u8 *luma, *cb, *cr, *alpha;
117 };
118 
119 #define FWHT_FRAME_PCODED	BIT(0)
120 #define FWHT_FRAME_UNENCODED	BIT(1)
121 #define FWHT_LUMA_UNENCODED	BIT(2)
122 #define FWHT_CB_UNENCODED	BIT(3)
123 #define FWHT_CR_UNENCODED	BIT(4)
124 #define FWHT_ALPHA_UNENCODED	BIT(5)
125 
126 u32 fwht_encode_frame(struct fwht_raw_frame *frm,
127 		      struct fwht_raw_frame *ref_frm,
128 		      struct fwht_cframe *cf,
129 		      bool is_intra, bool next_is_intra,
130 		      unsigned int width, unsigned int height,
131 		      unsigned int stride, unsigned int chroma_stride);
132 bool fwht_decode_frame(struct fwht_cframe *cf, u32 hdr_flags,
133 		unsigned int components_num, unsigned int width,
134 		unsigned int height, const struct fwht_raw_frame *ref,
135 		unsigned int ref_stride, unsigned int ref_chroma_stride,
136 		struct fwht_raw_frame *dst, unsigned int dst_stride,
137 		unsigned int dst_chroma_stride);
138 #endif
139