Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: LGPL-2.1+ */
0002 /*
0003  * Copyright 2016 Tom aan de Wiel
0004  * Copyright 2018 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
0005  */
0006 
0007 #ifndef CODEC_FWHT_H
0008 #define CODEC_FWHT_H
0009 
0010 #include <linux/types.h>
0011 #include <linux/bitops.h>
0012 #include <asm/byteorder.h>
0013 
0014 /*
0015  * The compressed format consists of a fwht_cframe_hdr struct followed by the
0016  * compressed frame data. The header contains the size of that data.
0017  * Each Y, Cb and Cr plane is compressed separately. If the compressed
0018  * size of each plane becomes larger than the uncompressed size, then
0019  * that plane is stored uncompressed and the corresponding bit is set
0020  * in the flags field of the header.
0021  *
0022  * Each compressed plane consists of macroblocks and each macroblock
0023  * is run-length-encoded. Each macroblock starts with a 16 bit value.
0024  * Bit 15 indicates if this is a P-coded macroblock (1) or not (0).
0025  * P-coded macroblocks contain a delta against the previous frame.
0026  *
0027  * Bits 1-12 contain a number. If non-zero, then this same macroblock
0028  * repeats that number of times. This results in a high degree of
0029  * compression for generated images like colorbars.
0030  *
0031  * Following this macroblock header the MB coefficients are run-length
0032  * encoded: the top 12 bits contain the coefficient, the bottom 4 bits
0033  * tell how many times this coefficient occurs. The value 0xf indicates
0034  * that the remainder of the macroblock should be filled with zeroes.
0035  *
0036  * All 16 and 32 bit values are stored in big-endian (network) order.
0037  *
0038  * Each fwht_cframe_hdr starts with an 8 byte magic header that is
0039  * guaranteed not to occur in the compressed frame data. This header
0040  * can be used to sync to the next frame.
0041  *
0042  * This codec uses the Fast Walsh Hadamard Transform. Tom aan de Wiel
0043  * developed this as part of a university project, specifically for use
0044  * with this driver. His project report can be found here:
0045  *
0046  * https://hverkuil.home.xs4all.nl/fwht.pdf
0047  */
0048 
0049 /*
0050  * This is a sequence of 8 bytes with the low 4 bits set to 0xf.
0051  *
0052  * This sequence cannot occur in the encoded data
0053  *
0054  * Note that these two magic values are symmetrical so endian issues here.
0055  */
0056 #define FWHT_MAGIC1 0x4f4f4f4f
0057 #define FWHT_MAGIC2 0xffffffff
0058 
0059 /*
0060  * A macro to calculate the needed padding in order to make sure
0061  * both luma and chroma components resolutions are rounded up to
0062  * a multiple of 8
0063  */
0064 #define vic_round_dim(dim, div) (round_up((dim) / (div), 8) * (div))
0065 
0066 struct fwht_cframe_hdr {
0067     u32 magic1;
0068     u32 magic2;
0069     __be32 version;
0070     __be32 width, height;
0071     __be32 flags;
0072     __be32 colorspace;
0073     __be32 xfer_func;
0074     __be32 ycbcr_enc;
0075     __be32 quantization;
0076     __be32 size;
0077 };
0078 
0079 struct fwht_cframe {
0080     u16 i_frame_qp;
0081     u16 p_frame_qp;
0082     __be16 *rlc_data;
0083     s16 coeffs[8 * 8];
0084     s16 de_coeffs[8 * 8];
0085     s16 de_fwht[8 * 8];
0086     u32 size;
0087 };
0088 
0089 struct fwht_raw_frame {
0090     unsigned int width_div;
0091     unsigned int height_div;
0092     unsigned int luma_alpha_step;
0093     unsigned int chroma_step;
0094     unsigned int components_num;
0095     u8 *buf;
0096     u8 *luma, *cb, *cr, *alpha;
0097 };
0098 
0099 #define FWHT_FRAME_PCODED   BIT(0)
0100 #define FWHT_FRAME_UNENCODED    BIT(1)
0101 #define FWHT_LUMA_UNENCODED BIT(2)
0102 #define FWHT_CB_UNENCODED   BIT(3)
0103 #define FWHT_CR_UNENCODED   BIT(4)
0104 #define FWHT_ALPHA_UNENCODED    BIT(5)
0105 
0106 u32 fwht_encode_frame(struct fwht_raw_frame *frm,
0107               struct fwht_raw_frame *ref_frm,
0108               struct fwht_cframe *cf,
0109               bool is_intra, bool next_is_intra,
0110               unsigned int width, unsigned int height,
0111               unsigned int stride, unsigned int chroma_stride);
0112 bool fwht_decode_frame(struct fwht_cframe *cf, u32 hdr_flags,
0113         unsigned int components_num, unsigned int width,
0114         unsigned int height, const struct fwht_raw_frame *ref,
0115         unsigned int ref_stride, unsigned int ref_chroma_stride,
0116         struct fwht_raw_frame *dst, unsigned int dst_stride,
0117         unsigned int dst_chroma_stride);
0118 #endif