vicodec-codec.h 3.9 KB

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