v4l2-jpeg.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * V4L2 JPEG helpers header
  4. *
  5. * Copyright (C) 2019 Pengutronix, Philipp Zabel <[email protected]>
  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. * struct v4l2_jpeg_reference - reference into the JPEG buffer
  16. * @start: pointer to the start of the referenced segment or table
  17. * @length: size of the referenced segment or table
  18. *
  19. * Wnen referencing marker segments, start points right after the marker code,
  20. * and length is the size of the segment parameters, excluding the marker code.
  21. */
  22. struct v4l2_jpeg_reference {
  23. u8 *start;
  24. size_t length;
  25. };
  26. /* B.2.2 Frame header syntax */
  27. /**
  28. * struct v4l2_jpeg_frame_component_spec - frame component-specification
  29. * @component_identifier: C[i]
  30. * @horizontal_sampling_factor: H[i]
  31. * @vertical_sampling_factor: V[i]
  32. * @quantization_table_selector: quantization table destination selector Tq[i]
  33. */
  34. struct v4l2_jpeg_frame_component_spec {
  35. u8 component_identifier;
  36. u8 horizontal_sampling_factor;
  37. u8 vertical_sampling_factor;
  38. u8 quantization_table_selector;
  39. };
  40. /**
  41. * struct v4l2_jpeg_frame_header - JPEG frame header
  42. * @height: Y
  43. * @width: X
  44. * @precision: P
  45. * @num_components: Nf
  46. * @component: component-specification, see v4l2_jpeg_frame_component_spec
  47. * @subsampling: decoded subsampling from component-specification
  48. */
  49. struct v4l2_jpeg_frame_header {
  50. u16 height;
  51. u16 width;
  52. u8 precision;
  53. u8 num_components;
  54. struct v4l2_jpeg_frame_component_spec component[V4L2_JPEG_MAX_COMPONENTS];
  55. enum v4l2_jpeg_chroma_subsampling subsampling;
  56. };
  57. /* B.2.3 Scan header syntax */
  58. /**
  59. * struct v4l2_jpeg_scan_component_spec - scan component-specification
  60. * @component_selector: Cs[j]
  61. * @dc_entropy_coding_table_selector: Td[j]
  62. * @ac_entropy_coding_table_selector: Ta[j]
  63. */
  64. struct v4l2_jpeg_scan_component_spec {
  65. u8 component_selector;
  66. u8 dc_entropy_coding_table_selector;
  67. u8 ac_entropy_coding_table_selector;
  68. };
  69. /**
  70. * struct v4l2_jpeg_scan_header - JPEG scan header
  71. * @num_components: Ns
  72. * @component: component-specification, see v4l2_jpeg_scan_component_spec
  73. */
  74. struct v4l2_jpeg_scan_header {
  75. u8 num_components; /* Ns */
  76. struct v4l2_jpeg_scan_component_spec component[V4L2_JPEG_MAX_COMPONENTS];
  77. /* Ss, Se, Ah, and Al are not used by any driver */
  78. };
  79. /**
  80. * enum v4l2_jpeg_app14_tf - APP14 transform flag
  81. * According to Rec. ITU-T T.872 (06/2012) 6.5.3
  82. * APP14 segment is for color encoding, it contains a transform flag,
  83. * which may have values of 0, 1 and 2 and are interpreted as follows:
  84. * @V4L2_JPEG_APP14_TF_CMYK_RGB: CMYK for images encoded with four components
  85. * RGB for images encoded with three components
  86. * @V4L2_JPEG_APP14_TF_YCBCR: an image encoded with three components using YCbCr
  87. * @V4L2_JPEG_APP14_TF_YCCK: an image encoded with four components using YCCK
  88. * @V4L2_JPEG_APP14_TF_UNKNOWN: indicate app14 is not present
  89. */
  90. enum v4l2_jpeg_app14_tf {
  91. V4L2_JPEG_APP14_TF_CMYK_RGB = 0,
  92. V4L2_JPEG_APP14_TF_YCBCR = 1,
  93. V4L2_JPEG_APP14_TF_YCCK = 2,
  94. V4L2_JPEG_APP14_TF_UNKNOWN = -1,
  95. };
  96. /**
  97. * struct v4l2_jpeg_header - parsed JPEG header
  98. * @sof: pointer to frame header and size
  99. * @sos: pointer to scan header and size
  100. * @num_dht: number of entries in @dht
  101. * @dht: pointers to huffman tables and sizes
  102. * @num_dqt: number of entries in @dqt
  103. * @dqt: pointers to quantization tables and sizes
  104. * @frame: parsed frame header
  105. * @scan: pointer to parsed scan header, optional
  106. * @quantization_tables: references to four quantization tables, optional
  107. * @huffman_tables: references to four Huffman tables in DC0, DC1, AC0, AC1
  108. * order, optional
  109. * @restart_interval: number of MCU per restart interval, Ri
  110. * @ecs_offset: buffer offset in bytes to the entropy coded segment
  111. * @app14_tf: transform flag from app14 data
  112. *
  113. * When this structure is passed to v4l2_jpeg_parse_header, the optional scan,
  114. * quantization_tables, and huffman_tables pointers must be initialized to NULL
  115. * or point at valid memory.
  116. */
  117. struct v4l2_jpeg_header {
  118. struct v4l2_jpeg_reference sof;
  119. struct v4l2_jpeg_reference sos;
  120. unsigned int num_dht;
  121. struct v4l2_jpeg_reference dht[V4L2_JPEG_MAX_TABLES];
  122. unsigned int num_dqt;
  123. struct v4l2_jpeg_reference dqt[V4L2_JPEG_MAX_TABLES];
  124. struct v4l2_jpeg_frame_header frame;
  125. struct v4l2_jpeg_scan_header *scan;
  126. struct v4l2_jpeg_reference *quantization_tables;
  127. struct v4l2_jpeg_reference *huffman_tables;
  128. u16 restart_interval;
  129. size_t ecs_offset;
  130. enum v4l2_jpeg_app14_tf app14_tf;
  131. };
  132. int v4l2_jpeg_parse_header(void *buf, size_t len, struct v4l2_jpeg_header *out);
  133. int v4l2_jpeg_parse_frame_header(void *buf, size_t len,
  134. struct v4l2_jpeg_frame_header *frame_header);
  135. int v4l2_jpeg_parse_scan_header(void *buf, size_t len,
  136. struct v4l2_jpeg_scan_header *scan_header);
  137. int v4l2_jpeg_parse_quantization_tables(void *buf, size_t len, u8 precision,
  138. struct v4l2_jpeg_reference *q_tables);
  139. int v4l2_jpeg_parse_huffman_tables(void *buf, size_t len,
  140. struct v4l2_jpeg_reference *huffman_tables);
  141. #endif