codec-fwht.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #ifndef CODEC_FWHT_H
  7. #define CODEC_FWHT_H
  8. #include <linux/types.h>
  9. #include <linux/bitops.h>
  10. #include <asm/byteorder.h>
  11. /*
  12. * The compressed format consists of a fwht_cframe_hdr struct followed by the
  13. * compressed frame data. The header contains the size of that data.
  14. * Each Y, Cb and Cr plane is compressed separately. If the compressed
  15. * size of each plane becomes larger than the uncompressed size, then
  16. * that plane is stored uncompressed and the corresponding bit is set
  17. * in the flags field of the header.
  18. *
  19. * Each compressed plane consists of macroblocks and each macroblock
  20. * is run-length-encoded. Each macroblock starts with a 16 bit value.
  21. * Bit 15 indicates if this is a P-coded macroblock (1) or not (0).
  22. * P-coded macroblocks contain a delta against the previous frame.
  23. *
  24. * Bits 1-12 contain a number. If non-zero, then this same macroblock
  25. * repeats that number of times. This results in a high degree of
  26. * compression for generated images like colorbars.
  27. *
  28. * Following this macroblock header the MB coefficients are run-length
  29. * encoded: the top 12 bits contain the coefficient, the bottom 4 bits
  30. * tell how many times this coefficient occurs. The value 0xf indicates
  31. * that the remainder of the macroblock should be filled with zeroes.
  32. *
  33. * All 16 and 32 bit values are stored in big-endian (network) order.
  34. *
  35. * Each fwht_cframe_hdr starts with an 8 byte magic header that is
  36. * guaranteed not to occur in the compressed frame data. This header
  37. * can be used to sync to the next frame.
  38. *
  39. * This codec uses the Fast Walsh Hadamard Transform. Tom aan de Wiel
  40. * developed this as part of a university project, specifically for use
  41. * with this driver. His project report can be found here:
  42. *
  43. * https://hverkuil.home.xs4all.nl/fwht.pdf
  44. */
  45. /*
  46. * This is a sequence of 8 bytes with the low 4 bits set to 0xf.
  47. *
  48. * This sequence cannot occur in the encoded data
  49. *
  50. * Note that these two magic values are symmetrical so endian issues here.
  51. */
  52. #define FWHT_MAGIC1 0x4f4f4f4f
  53. #define FWHT_MAGIC2 0xffffffff
  54. /*
  55. * A macro to calculate the needed padding in order to make sure
  56. * both luma and chroma components resolutions are rounded up to
  57. * a multiple of 8
  58. */
  59. #define vic_round_dim(dim, div) (round_up((dim) / (div), 8) * (div))
  60. struct fwht_cframe_hdr {
  61. u32 magic1;
  62. u32 magic2;
  63. __be32 version;
  64. __be32 width, height;
  65. __be32 flags;
  66. __be32 colorspace;
  67. __be32 xfer_func;
  68. __be32 ycbcr_enc;
  69. __be32 quantization;
  70. __be32 size;
  71. };
  72. struct fwht_cframe {
  73. u16 i_frame_qp;
  74. u16 p_frame_qp;
  75. __be16 *rlc_data;
  76. s16 coeffs[8 * 8];
  77. s16 de_coeffs[8 * 8];
  78. s16 de_fwht[8 * 8];
  79. u32 size;
  80. };
  81. struct fwht_raw_frame {
  82. unsigned int width_div;
  83. unsigned int height_div;
  84. unsigned int luma_alpha_step;
  85. unsigned int chroma_step;
  86. unsigned int components_num;
  87. u8 *buf;
  88. u8 *luma, *cb, *cr, *alpha;
  89. };
  90. #define FWHT_FRAME_PCODED BIT(0)
  91. #define FWHT_FRAME_UNENCODED BIT(1)
  92. #define FWHT_LUMA_UNENCODED BIT(2)
  93. #define FWHT_CB_UNENCODED BIT(3)
  94. #define FWHT_CR_UNENCODED BIT(4)
  95. #define FWHT_ALPHA_UNENCODED BIT(5)
  96. u32 fwht_encode_frame(struct fwht_raw_frame *frm,
  97. struct fwht_raw_frame *ref_frm,
  98. struct fwht_cframe *cf,
  99. bool is_intra, bool next_is_intra,
  100. unsigned int width, unsigned int height,
  101. unsigned int stride, unsigned int chroma_stride);
  102. bool fwht_decode_frame(struct fwht_cframe *cf, u32 hdr_flags,
  103. unsigned int components_num, unsigned int width,
  104. unsigned int height, const struct fwht_raw_frame *ref,
  105. unsigned int ref_stride, unsigned int ref_chroma_stride,
  106. struct fwht_raw_frame *dst, unsigned int dst_stride,
  107. unsigned int dst_chroma_stride);
  108. #endif