1/* SPDX-License-Identifier: GPL-2.0+ */ 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 VICODEC_RLC_H 8#define VICODEC_RLC_H 9 10#include <linux/types.h> 11#include <linux/bitops.h> 12#include <asm/byteorder.h> 13 14/* 15 * The compressed format consists of a 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. 21 * 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. 26 * 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. 30 * 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. 35 * 36 * All 16 and 32 bit values are stored in big-endian (network) order. 37 * 38 * Each 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. 41 * 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: 45 * 46 * https://hverkuil.home.xs4all.nl/fwht.pdf 47 */ 48 49/* 50 * Note: bit 0 of the header must always be 0. Otherwise it cannot 51 * be guaranteed that the magic 8 byte sequence (see below) can 52 * never occur in the rlc output. 53 */ 54#define PFRAME_BIT (1 << 15) 55#define DUPS_MASK 0x1ffe 56 57/* 58 * This is a sequence of 8 bytes with the low 4 bits set to 0xf. 59 * 60 * This sequence cannot occur in the encoded data 61 */ 62#define VICODEC_MAGIC1 0x4f4f4f4f 63#define VICODEC_MAGIC2 0xffffffff 64 65#define VICODEC_VERSION 1 66 67#define VICODEC_MAX_WIDTH 3840 68#define VICODEC_MAX_HEIGHT 2160 69#define VICODEC_MIN_WIDTH 640 70#define VICODEC_MIN_HEIGHT 480 71 72#define PBLOCK 0 73#define IBLOCK 1 74 75/* Set if this is an interlaced format */ 76#define VICODEC_FL_IS_INTERLACED BIT(0) 77/* Set if this is a bottom-first (NTSC) interlaced format */ 78#define VICODEC_FL_IS_BOTTOM_FIRST BIT(1) 79/* Set if each 'frame' contains just one field */ 80#define VICODEC_FL_IS_ALTERNATE BIT(2) 81/* 82 * If VICODEC_FL_IS_ALTERNATE was set, then this is set if this 83 * 'frame' is the bottom field, else it is the top field. 84 */ 85#define VICODEC_FL_IS_BOTTOM_FIELD BIT(3) 86/* Set if this frame is uncompressed */ 87#define VICODEC_FL_LUMA_IS_UNCOMPRESSED BIT(4) 88#define VICODEC_FL_CB_IS_UNCOMPRESSED BIT(5) 89#define VICODEC_FL_CR_IS_UNCOMPRESSED BIT(6) 90 91struct cframe_hdr { 92 u32 magic1; 93 u32 magic2; 94 __be32 version; 95 __be32 width, height; 96 __be32 flags; 97 __be32 colorspace; 98 __be32 xfer_func; 99 __be32 ycbcr_enc; 100 __be32 quantization; 101 __be32 size; 102}; 103 104struct cframe { 105 unsigned int width, height; 106 __be16 *rlc_data; 107 s16 coeffs[8 * 8]; 108 s16 de_coeffs[8 * 8]; 109 s16 de_fwht[8 * 8]; 110 u32 size; 111}; 112 113struct raw_frame { 114 unsigned int width, height; 115 unsigned int chroma_step; 116 u8 *luma, *cb, *cr; 117}; 118 119#define FRAME_PCODED BIT(0) 120#define FRAME_UNENCODED BIT(1) 121#define LUMA_UNENCODED BIT(2) 122#define CB_UNENCODED BIT(3) 123#define CR_UNENCODED BIT(4) 124 125u32 encode_frame(struct raw_frame *frm, struct raw_frame *ref_frm, 126 struct cframe *cf, bool is_intra, bool next_is_intra); 127void decode_frame(struct cframe *cf, struct raw_frame *ref, u32 hdr_flags); 128 129#endif 130