1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 * Copyright 2016 Tom aan de Wiel
4 * Copyright 2018 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
10 #include <linux/types.h>
11 #include <linux/bitops.h>
12 #include <asm/byteorder.h>
15 * The compressed format consists of a fwht_cframe_hdr struct followed by the
16 * compressed frame data. The header contains the size of that data.
17 * Each Y, Cb and Cr plane is compressed separately. If the compressed
18 * size of each plane becomes larger than the uncompressed size, then
19 * that plane is stored uncompressed and the corresponding bit is set
20 * in the flags field of the header.
22 * Each compressed plane consists of macroblocks and each macroblock
23 * is run-length-encoded. Each macroblock starts with a 16 bit value.
24 * Bit 15 indicates if this is a P-coded macroblock (1) or not (0).
25 * P-coded macroblocks contain a delta against the previous frame.
27 * Bits 1-12 contain a number. If non-zero, then this same macroblock
28 * repeats that number of times. This results in a high degree of
29 * compression for generated images like colorbars.
31 * Following this macroblock header the MB coefficients are run-length
32 * encoded: the top 12 bits contain the coefficient, the bottom 4 bits
33 * tell how many times this coefficient occurs. The value 0xf indicates
34 * that the remainder of the macroblock should be filled with zeroes.
36 * All 16 and 32 bit values are stored in big-endian (network) order.
38 * Each fwht_cframe_hdr starts with an 8 byte magic header that is
39 * guaranteed not to occur in the compressed frame data. This header
40 * can be used to sync to the next frame.
42 * This codec uses the Fast Walsh Hadamard Transform. Tom aan de Wiel
43 * developed this as part of a university project, specifically for use
44 * with this driver. His project report can be found here:
46 * https://hverkuil.home.xs4all.nl/fwht.pdf
50 * This is a sequence of 8 bytes with the low 4 bits set to 0xf.
52 * This sequence cannot occur in the encoded data
54 * Note that these two magic values are symmetrical so endian issues here.
56 #define FWHT_MAGIC1 0x4f4f4f4f
57 #define FWHT_MAGIC2 0xffffffff
60 * A macro to calculate the needed padding in order to make sure
61 * both luma and chroma components resolutions are rounded up to
64 #define vic_round_dim(dim, div) (round_up((dim) / (div), 8) * (div))
66 struct fwht_cframe_hdr
{
89 struct fwht_raw_frame
{
90 unsigned int width_div
;
91 unsigned int height_div
;
92 unsigned int luma_alpha_step
;
93 unsigned int chroma_step
;
94 unsigned int components_num
;
96 u8
*luma
, *cb
, *cr
, *alpha
;
99 #define FWHT_FRAME_PCODED BIT(0)
100 #define FWHT_FRAME_UNENCODED BIT(1)
101 #define FWHT_LUMA_UNENCODED BIT(2)
102 #define FWHT_CB_UNENCODED BIT(3)
103 #define FWHT_CR_UNENCODED BIT(4)
104 #define FWHT_ALPHA_UNENCODED BIT(5)
106 u32
fwht_encode_frame(struct fwht_raw_frame
*frm
,
107 struct fwht_raw_frame
*ref_frm
,
108 struct fwht_cframe
*cf
,
109 bool is_intra
, bool next_is_intra
,
110 unsigned int width
, unsigned int height
,
111 unsigned int stride
, unsigned int chroma_stride
);
112 bool fwht_decode_frame(struct fwht_cframe
*cf
, u32 hdr_flags
,
113 unsigned int components_num
, unsigned int width
,
114 unsigned int height
, const struct fwht_raw_frame
*ref
,
115 unsigned int ref_stride
, unsigned int ref_chroma_stride
,
116 struct fwht_raw_frame
*dst
, unsigned int dst_stride
,
117 unsigned int dst_chroma_stride
);