v4l2-jpeg.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * V4L2 JPEG helpers header
  4. *
  5. * Copyright (C) 2019 Pengutronix, Philipp Zabel <kernel@pengutronix.de>
  6. *
  7. * For reference, see JPEG ITU-T.81 (ISO/IEC 10918-1)
  8. */
  9. #ifndef _V4L2_JPEG_H
  10. #define _V4L2_JPEG_H
  11. #include <linux/v4l2-controls.h>
  12. #define V4L2_JPEG_MAX_COMPONENTS 4
  13. #define V4L2_JPEG_MAX_TABLES 4
  14. /*
  15. * Prefixes used to generate huffman table class and destination identifiers as
  16. * described below:
  17. *
  18. * V4L2_JPEG_LUM_HT | V4L2_JPEG_DC_HT : Prefix for Luma DC coefficients
  19. * huffman table
  20. * V4L2_JPEG_LUM_HT | V4L2_JPEG_AC_HT : Prefix for Luma AC coefficients
  21. * huffman table
  22. * V4L2_JPEG_CHR_HT | V4L2_JPEG_DC_HT : Prefix for Chroma DC coefficients
  23. * huffman table
  24. * V4L2_JPEG_CHR_HT | V4L2_JPEG_AC_HT : Prefix for Chroma AC coefficients
  25. * huffman table
  26. */
  27. #define V4L2_JPEG_LUM_HT 0x00
  28. #define V4L2_JPEG_CHR_HT 0x01
  29. #define V4L2_JPEG_DC_HT 0x00
  30. #define V4L2_JPEG_AC_HT 0x10
  31. /* Length of reference huffman tables as provided in Table K.3 of ITU-T.81 */
  32. #define V4L2_JPEG_REF_HT_AC_LEN 178
  33. #define V4L2_JPEG_REF_HT_DC_LEN 28
  34. /* Array size for 8x8 block of samples or DCT coefficient */
  35. #define V4L2_JPEG_PIXELS_IN_BLOCK 64
  36. /**
  37. * struct v4l2_jpeg_reference - reference into the JPEG buffer
  38. * @start: pointer to the start of the referenced segment or table
  39. * @length: size of the referenced segment or table
  40. *
  41. * Wnen referencing marker segments, start points right after the marker code,
  42. * and length is the size of the segment parameters, excluding the marker code.
  43. */
  44. struct v4l2_jpeg_reference {
  45. u8 *start;
  46. size_t length;
  47. };
  48. /* B.2.2 Frame header syntax */
  49. /**
  50. * struct v4l2_jpeg_frame_component_spec - frame component-specification
  51. * @component_identifier: C[i]
  52. * @horizontal_sampling_factor: H[i]
  53. * @vertical_sampling_factor: V[i]
  54. * @quantization_table_selector: quantization table destination selector Tq[i]
  55. */
  56. struct v4l2_jpeg_frame_component_spec {
  57. u8 component_identifier;
  58. u8 horizontal_sampling_factor;
  59. u8 vertical_sampling_factor;
  60. u8 quantization_table_selector;
  61. };
  62. /**
  63. * struct v4l2_jpeg_frame_header - JPEG frame header
  64. * @height: Y
  65. * @width: X
  66. * @precision: P
  67. * @num_components: Nf
  68. * @component: component-specification, see v4l2_jpeg_frame_component_spec
  69. * @subsampling: decoded subsampling from component-specification
  70. */
  71. struct v4l2_jpeg_frame_header {
  72. u16 height;
  73. u16 width;
  74. u8 precision;
  75. u8 num_components;
  76. struct v4l2_jpeg_frame_component_spec component[V4L2_JPEG_MAX_COMPONENTS];
  77. enum v4l2_jpeg_chroma_subsampling subsampling;
  78. };
  79. /* B.2.3 Scan header syntax */
  80. /**
  81. * struct v4l2_jpeg_scan_component_spec - scan component-specification
  82. * @component_selector: Cs[j]
  83. * @dc_entropy_coding_table_selector: Td[j]
  84. * @ac_entropy_coding_table_selector: Ta[j]
  85. */
  86. struct v4l2_jpeg_scan_component_spec {
  87. u8 component_selector;
  88. u8 dc_entropy_coding_table_selector;
  89. u8 ac_entropy_coding_table_selector;
  90. };
  91. /**
  92. * struct v4l2_jpeg_scan_header - JPEG scan header
  93. * @num_components: Ns
  94. * @component: component-specification, see v4l2_jpeg_scan_component_spec
  95. */
  96. struct v4l2_jpeg_scan_header {
  97. u8 num_components; /* Ns */
  98. struct v4l2_jpeg_scan_component_spec component[V4L2_JPEG_MAX_COMPONENTS];
  99. /* Ss, Se, Ah, and Al are not used by any driver */
  100. };
  101. /**
  102. * enum v4l2_jpeg_app14_tf - APP14 transform flag
  103. * According to Rec. ITU-T T.872 (06/2012) 6.5.3
  104. * APP14 segment is for color encoding, it contains a transform flag,
  105. * which may have values of 0, 1 and 2 and are interpreted as follows:
  106. * @V4L2_JPEG_APP14_TF_CMYK_RGB: CMYK for images encoded with four components
  107. * RGB for images encoded with three components
  108. * @V4L2_JPEG_APP14_TF_YCBCR: an image encoded with three components using YCbCr
  109. * @V4L2_JPEG_APP14_TF_YCCK: an image encoded with four components using YCCK
  110. * @V4L2_JPEG_APP14_TF_UNKNOWN: indicate app14 is not present
  111. */
  112. enum v4l2_jpeg_app14_tf {
  113. V4L2_JPEG_APP14_TF_CMYK_RGB = 0,
  114. V4L2_JPEG_APP14_TF_YCBCR = 1,
  115. V4L2_JPEG_APP14_TF_YCCK = 2,
  116. V4L2_JPEG_APP14_TF_UNKNOWN = -1,
  117. };
  118. /**
  119. * struct v4l2_jpeg_header - parsed JPEG header
  120. * @sof: pointer to frame header and size
  121. * @sos: pointer to scan header and size
  122. * @num_dht: number of entries in @dht
  123. * @dht: pointers to huffman tables and sizes
  124. * @num_dqt: number of entries in @dqt
  125. * @dqt: pointers to quantization tables and sizes
  126. * @frame: parsed frame header
  127. * @scan: pointer to parsed scan header, optional
  128. * @quantization_tables: references to four quantization tables, optional
  129. * @huffman_tables: references to four Huffman tables in DC0, DC1, AC0, AC1
  130. * order, optional
  131. * @restart_interval: number of MCU per restart interval, Ri
  132. * @ecs_offset: buffer offset in bytes to the entropy coded segment
  133. * @app14_tf: transform flag from app14 data
  134. *
  135. * When this structure is passed to v4l2_jpeg_parse_header, the optional scan,
  136. * quantization_tables, and huffman_tables pointers must be initialized to NULL
  137. * or point at valid memory.
  138. */
  139. struct v4l2_jpeg_header {
  140. struct v4l2_jpeg_reference sof;
  141. struct v4l2_jpeg_reference sos;
  142. unsigned int num_dht;
  143. struct v4l2_jpeg_reference dht[V4L2_JPEG_MAX_TABLES];
  144. unsigned int num_dqt;
  145. struct v4l2_jpeg_reference dqt[V4L2_JPEG_MAX_TABLES];
  146. struct v4l2_jpeg_frame_header frame;
  147. struct v4l2_jpeg_scan_header *scan;
  148. struct v4l2_jpeg_reference *quantization_tables;
  149. struct v4l2_jpeg_reference *huffman_tables;
  150. u16 restart_interval;
  151. size_t ecs_offset;
  152. enum v4l2_jpeg_app14_tf app14_tf;
  153. };
  154. int v4l2_jpeg_parse_header(void *buf, size_t len, struct v4l2_jpeg_header *out);
  155. extern const u8 v4l2_jpeg_zigzag_scan_index[V4L2_JPEG_PIXELS_IN_BLOCK];
  156. extern const u8 v4l2_jpeg_ref_table_luma_qt[V4L2_JPEG_PIXELS_IN_BLOCK];
  157. extern const u8 v4l2_jpeg_ref_table_chroma_qt[V4L2_JPEG_PIXELS_IN_BLOCK];
  158. extern const u8 v4l2_jpeg_ref_table_luma_dc_ht[V4L2_JPEG_REF_HT_DC_LEN];
  159. extern const u8 v4l2_jpeg_ref_table_luma_ac_ht[V4L2_JPEG_REF_HT_AC_LEN];
  160. extern const u8 v4l2_jpeg_ref_table_chroma_dc_ht[V4L2_JPEG_REF_HT_DC_LEN];
  161. extern const u8 v4l2_jpeg_ref_table_chroma_ac_ht[V4L2_JPEG_REF_HT_AC_LEN];
  162. #endif