1*4882a593Smuzhiyun /* SPDX-License-Identifier: LGPL-2.1+ */ 2*4882a593Smuzhiyun /* 3*4882a593Smuzhiyun * Copyright 2016 Tom aan de Wiel 4*4882a593Smuzhiyun * Copyright 2018 Cisco Systems, Inc. and/or its affiliates. All rights reserved. 5*4882a593Smuzhiyun */ 6*4882a593Smuzhiyun 7*4882a593Smuzhiyun #ifndef CODEC_FWHT_H 8*4882a593Smuzhiyun #define CODEC_FWHT_H 9*4882a593Smuzhiyun 10*4882a593Smuzhiyun #include <linux/types.h> 11*4882a593Smuzhiyun #include <linux/bitops.h> 12*4882a593Smuzhiyun #include <asm/byteorder.h> 13*4882a593Smuzhiyun 14*4882a593Smuzhiyun /* 15*4882a593Smuzhiyun * The compressed format consists of a fwht_cframe_hdr struct followed by the 16*4882a593Smuzhiyun * compressed frame data. The header contains the size of that data. 17*4882a593Smuzhiyun * Each Y, Cb and Cr plane is compressed separately. If the compressed 18*4882a593Smuzhiyun * size of each plane becomes larger than the uncompressed size, then 19*4882a593Smuzhiyun * that plane is stored uncompressed and the corresponding bit is set 20*4882a593Smuzhiyun * in the flags field of the header. 21*4882a593Smuzhiyun * 22*4882a593Smuzhiyun * Each compressed plane consists of macroblocks and each macroblock 23*4882a593Smuzhiyun * is run-length-encoded. Each macroblock starts with a 16 bit value. 24*4882a593Smuzhiyun * Bit 15 indicates if this is a P-coded macroblock (1) or not (0). 25*4882a593Smuzhiyun * P-coded macroblocks contain a delta against the previous frame. 26*4882a593Smuzhiyun * 27*4882a593Smuzhiyun * Bits 1-12 contain a number. If non-zero, then this same macroblock 28*4882a593Smuzhiyun * repeats that number of times. This results in a high degree of 29*4882a593Smuzhiyun * compression for generated images like colorbars. 30*4882a593Smuzhiyun * 31*4882a593Smuzhiyun * Following this macroblock header the MB coefficients are run-length 32*4882a593Smuzhiyun * encoded: the top 12 bits contain the coefficient, the bottom 4 bits 33*4882a593Smuzhiyun * tell how many times this coefficient occurs. The value 0xf indicates 34*4882a593Smuzhiyun * that the remainder of the macroblock should be filled with zeroes. 35*4882a593Smuzhiyun * 36*4882a593Smuzhiyun * All 16 and 32 bit values are stored in big-endian (network) order. 37*4882a593Smuzhiyun * 38*4882a593Smuzhiyun * Each fwht_cframe_hdr starts with an 8 byte magic header that is 39*4882a593Smuzhiyun * guaranteed not to occur in the compressed frame data. This header 40*4882a593Smuzhiyun * can be used to sync to the next frame. 41*4882a593Smuzhiyun * 42*4882a593Smuzhiyun * This codec uses the Fast Walsh Hadamard Transform. Tom aan de Wiel 43*4882a593Smuzhiyun * developed this as part of a university project, specifically for use 44*4882a593Smuzhiyun * with this driver. His project report can be found here: 45*4882a593Smuzhiyun * 46*4882a593Smuzhiyun * https://hverkuil.home.xs4all.nl/fwht.pdf 47*4882a593Smuzhiyun */ 48*4882a593Smuzhiyun 49*4882a593Smuzhiyun /* 50*4882a593Smuzhiyun * This is a sequence of 8 bytes with the low 4 bits set to 0xf. 51*4882a593Smuzhiyun * 52*4882a593Smuzhiyun * This sequence cannot occur in the encoded data 53*4882a593Smuzhiyun * 54*4882a593Smuzhiyun * Note that these two magic values are symmetrical so endian issues here. 55*4882a593Smuzhiyun */ 56*4882a593Smuzhiyun #define FWHT_MAGIC1 0x4f4f4f4f 57*4882a593Smuzhiyun #define FWHT_MAGIC2 0xffffffff 58*4882a593Smuzhiyun 59*4882a593Smuzhiyun #define FWHT_VERSION 3 60*4882a593Smuzhiyun 61*4882a593Smuzhiyun /* Set if this is an interlaced format */ 62*4882a593Smuzhiyun #define FWHT_FL_IS_INTERLACED BIT(0) 63*4882a593Smuzhiyun /* Set if this is a bottom-first (NTSC) interlaced format */ 64*4882a593Smuzhiyun #define FWHT_FL_IS_BOTTOM_FIRST BIT(1) 65*4882a593Smuzhiyun /* Set if each 'frame' contains just one field */ 66*4882a593Smuzhiyun #define FWHT_FL_IS_ALTERNATE BIT(2) 67*4882a593Smuzhiyun /* 68*4882a593Smuzhiyun * If FWHT_FL_IS_ALTERNATE was set, then this is set if this 69*4882a593Smuzhiyun * 'frame' is the bottom field, else it is the top field. 70*4882a593Smuzhiyun */ 71*4882a593Smuzhiyun #define FWHT_FL_IS_BOTTOM_FIELD BIT(3) 72*4882a593Smuzhiyun /* Set if this frame is uncompressed */ 73*4882a593Smuzhiyun #define FWHT_FL_LUMA_IS_UNCOMPRESSED BIT(4) 74*4882a593Smuzhiyun #define FWHT_FL_CB_IS_UNCOMPRESSED BIT(5) 75*4882a593Smuzhiyun #define FWHT_FL_CR_IS_UNCOMPRESSED BIT(6) 76*4882a593Smuzhiyun #define FWHT_FL_CHROMA_FULL_HEIGHT BIT(7) 77*4882a593Smuzhiyun #define FWHT_FL_CHROMA_FULL_WIDTH BIT(8) 78*4882a593Smuzhiyun #define FWHT_FL_ALPHA_IS_UNCOMPRESSED BIT(9) 79*4882a593Smuzhiyun #define FWHT_FL_I_FRAME BIT(10) 80*4882a593Smuzhiyun 81*4882a593Smuzhiyun /* A 4-values flag - the number of components - 1 */ 82*4882a593Smuzhiyun #define FWHT_FL_COMPONENTS_NUM_MSK GENMASK(18, 16) 83*4882a593Smuzhiyun #define FWHT_FL_COMPONENTS_NUM_OFFSET 16 84*4882a593Smuzhiyun 85*4882a593Smuzhiyun #define FWHT_FL_PIXENC_MSK GENMASK(20, 19) 86*4882a593Smuzhiyun #define FWHT_FL_PIXENC_OFFSET 19 87*4882a593Smuzhiyun #define FWHT_FL_PIXENC_YUV (1 << FWHT_FL_PIXENC_OFFSET) 88*4882a593Smuzhiyun #define FWHT_FL_PIXENC_RGB (2 << FWHT_FL_PIXENC_OFFSET) 89*4882a593Smuzhiyun #define FWHT_FL_PIXENC_HSV (3 << FWHT_FL_PIXENC_OFFSET) 90*4882a593Smuzhiyun 91*4882a593Smuzhiyun /* 92*4882a593Smuzhiyun * A macro to calculate the needed padding in order to make sure 93*4882a593Smuzhiyun * both luma and chroma components resolutions are rounded up to 94*4882a593Smuzhiyun * a multiple of 8 95*4882a593Smuzhiyun */ 96*4882a593Smuzhiyun #define vic_round_dim(dim, div) (round_up((dim) / (div), 8) * (div)) 97*4882a593Smuzhiyun 98*4882a593Smuzhiyun struct fwht_cframe_hdr { 99*4882a593Smuzhiyun u32 magic1; 100*4882a593Smuzhiyun u32 magic2; 101*4882a593Smuzhiyun __be32 version; 102*4882a593Smuzhiyun __be32 width, height; 103*4882a593Smuzhiyun __be32 flags; 104*4882a593Smuzhiyun __be32 colorspace; 105*4882a593Smuzhiyun __be32 xfer_func; 106*4882a593Smuzhiyun __be32 ycbcr_enc; 107*4882a593Smuzhiyun __be32 quantization; 108*4882a593Smuzhiyun __be32 size; 109*4882a593Smuzhiyun }; 110*4882a593Smuzhiyun 111*4882a593Smuzhiyun struct fwht_cframe { 112*4882a593Smuzhiyun u16 i_frame_qp; 113*4882a593Smuzhiyun u16 p_frame_qp; 114*4882a593Smuzhiyun __be16 *rlc_data; 115*4882a593Smuzhiyun s16 coeffs[8 * 8]; 116*4882a593Smuzhiyun s16 de_coeffs[8 * 8]; 117*4882a593Smuzhiyun s16 de_fwht[8 * 8]; 118*4882a593Smuzhiyun u32 size; 119*4882a593Smuzhiyun }; 120*4882a593Smuzhiyun 121*4882a593Smuzhiyun struct fwht_raw_frame { 122*4882a593Smuzhiyun unsigned int width_div; 123*4882a593Smuzhiyun unsigned int height_div; 124*4882a593Smuzhiyun unsigned int luma_alpha_step; 125*4882a593Smuzhiyun unsigned int chroma_step; 126*4882a593Smuzhiyun unsigned int components_num; 127*4882a593Smuzhiyun u8 *buf; 128*4882a593Smuzhiyun u8 *luma, *cb, *cr, *alpha; 129*4882a593Smuzhiyun }; 130*4882a593Smuzhiyun 131*4882a593Smuzhiyun #define FWHT_FRAME_PCODED BIT(0) 132*4882a593Smuzhiyun #define FWHT_FRAME_UNENCODED BIT(1) 133*4882a593Smuzhiyun #define FWHT_LUMA_UNENCODED BIT(2) 134*4882a593Smuzhiyun #define FWHT_CB_UNENCODED BIT(3) 135*4882a593Smuzhiyun #define FWHT_CR_UNENCODED BIT(4) 136*4882a593Smuzhiyun #define FWHT_ALPHA_UNENCODED BIT(5) 137*4882a593Smuzhiyun 138*4882a593Smuzhiyun u32 fwht_encode_frame(struct fwht_raw_frame *frm, 139*4882a593Smuzhiyun struct fwht_raw_frame *ref_frm, 140*4882a593Smuzhiyun struct fwht_cframe *cf, 141*4882a593Smuzhiyun bool is_intra, bool next_is_intra, 142*4882a593Smuzhiyun unsigned int width, unsigned int height, 143*4882a593Smuzhiyun unsigned int stride, unsigned int chroma_stride); 144*4882a593Smuzhiyun bool fwht_decode_frame(struct fwht_cframe *cf, u32 hdr_flags, 145*4882a593Smuzhiyun unsigned int components_num, unsigned int width, 146*4882a593Smuzhiyun unsigned int height, const struct fwht_raw_frame *ref, 147*4882a593Smuzhiyun unsigned int ref_stride, unsigned int ref_chroma_stride, 148*4882a593Smuzhiyun struct fwht_raw_frame *dst, unsigned int dst_stride, 149*4882a593Smuzhiyun unsigned int dst_chroma_stride); 150*4882a593Smuzhiyun #endif 151