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 uint64_t u64; 28 typedef uint32_t u32; 29 typedef uint16_t u16; 30 typedef int16_t s16; 31 typedef int32_t s32; 32 typedef uint8_t 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 #define FWHT_VERSION 3 80 81 /* Set if this is an interlaced format */ 82 #define FWHT_FL_IS_INTERLACED BIT(0) 83 /* Set if this is a bottom-first (NTSC) interlaced format */ 84 #define FWHT_FL_IS_BOTTOM_FIRST BIT(1) 85 /* Set if each 'frame' contains just one field */ 86 #define FWHT_FL_IS_ALTERNATE BIT(2) 87 /* 88 * If FWHT_FL_IS_ALTERNATE was set, then this is set if this 89 * 'frame' is the bottom field, else it is the top field. 90 */ 91 #define FWHT_FL_IS_BOTTOM_FIELD BIT(3) 92 /* Set if this frame is uncompressed */ 93 #define FWHT_FL_LUMA_IS_UNCOMPRESSED BIT(4) 94 #define FWHT_FL_CB_IS_UNCOMPRESSED BIT(5) 95 #define FWHT_FL_CR_IS_UNCOMPRESSED BIT(6) 96 #define FWHT_FL_CHROMA_FULL_HEIGHT BIT(7) 97 #define FWHT_FL_CHROMA_FULL_WIDTH BIT(8) 98 #define FWHT_FL_ALPHA_IS_UNCOMPRESSED BIT(9) 99 #define FWHT_FL_I_FRAME BIT(10) 100 101 /* A 4-values flag - the number of components - 1 */ 102 #define FWHT_FL_COMPONENTS_NUM_MSK GENMASK(18, 16) 103 #define FWHT_FL_COMPONENTS_NUM_OFFSET 16 104 105 #define FWHT_FL_PIXENC_MSK GENMASK(20, 19) 106 #define FWHT_FL_PIXENC_OFFSET 19 107 #define FWHT_FL_PIXENC_YUV (1 << FWHT_FL_PIXENC_OFFSET) 108 #define FWHT_FL_PIXENC_RGB (2 << FWHT_FL_PIXENC_OFFSET) 109 #define FWHT_FL_PIXENC_HSV (3 << FWHT_FL_PIXENC_OFFSET) 110 111 /* 112 * A macro to calculate the needed padding in order to make sure 113 * both luma and chroma components resolutions are rounded up to 114 * a multiple of 8 115 */ 116 #define vic_round_dim(dim, div) (round_up((dim) / (div), 8) * (div)) 117 118 struct fwht_cframe_hdr { 119 u32 magic1; 120 u32 magic2; 121 uint32_t version; 122 uint32_t width, height; 123 uint32_t flags; 124 uint32_t colorspace; 125 uint32_t xfer_func; 126 uint32_t ycbcr_enc; 127 uint32_t quantization; 128 uint32_t size; 129 }; 130 131 struct fwht_cframe { 132 u16 i_frame_qp; 133 u16 p_frame_qp; 134 uint16_t *rlc_data; 135 s16 coeffs[8 * 8]; 136 s16 de_coeffs[8 * 8]; 137 s16 de_fwht[8 * 8]; 138 u32 size; 139 }; 140 141 struct fwht_raw_frame { 142 unsigned int width_div; 143 unsigned int height_div; 144 unsigned int luma_alpha_step; 145 unsigned int chroma_step; 146 unsigned int components_num; 147 u8 *buf; 148 u8 *luma, *cb, *cr, *alpha; 149 }; 150 151 #define FWHT_FRAME_PCODED BIT(0) 152 #define FWHT_FRAME_UNENCODED BIT(1) 153 #define FWHT_LUMA_UNENCODED BIT(2) 154 #define FWHT_CB_UNENCODED BIT(3) 155 #define FWHT_CR_UNENCODED BIT(4) 156 #define FWHT_ALPHA_UNENCODED BIT(5) 157 158 u32 fwht_encode_frame(struct fwht_raw_frame *frm, 159 struct fwht_raw_frame *ref_frm, 160 struct fwht_cframe *cf, 161 bool is_intra, bool next_is_intra, 162 unsigned int width, unsigned int height, 163 unsigned int stride, unsigned int chroma_stride); 164 bool fwht_decode_frame(struct fwht_cframe *cf, u32 hdr_flags, 165 unsigned int components_num, unsigned int width, 166 unsigned int height, const struct fwht_raw_frame *ref, 167 unsigned int ref_stride, unsigned int ref_chroma_stride, 168 struct fwht_raw_frame *dst, unsigned int dst_stride, 169 unsigned int dst_chroma_stride); 170 #endif 171