xref: /linux/include/media/v4l2-jpeg.h (revision 32daa5d7899e03433429bedf9e20d7963179703a)
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 
10 #ifndef _V4L2_JPEG_H
11 #define _V4L2_JPEG_H
12 
13 #include <linux/v4l2-controls.h>
14 
15 #define V4L2_JPEG_MAX_COMPONENTS	4
16 #define V4L2_JPEG_MAX_TABLES		4
17 
18 /**
19  * struct v4l2_jpeg_reference - reference into the JPEG buffer
20  * @start: pointer to the start of the referenced segment or table
21  * @length: size of the referenced segment or table
22  *
23  * Wnen referencing marker segments, start points right after the marker code,
24  * and length is the size of the segment parameters, excluding the marker code.
25  */
26 struct v4l2_jpeg_reference {
27 	u8 *start;
28 	size_t length;
29 };
30 
31 /* B.2.2 Frame header syntax */
32 
33 /**
34  * struct v4l2_jpeg_frame_component_spec - frame component-specification
35  * @component_identifier: C[i]
36  * @horizontal_sampling_factor: H[i]
37  * @vertical_sampling_factor: V[i]
38  * @quantization_table_selector: quantization table destination selector Tq[i]
39  */
40 struct v4l2_jpeg_frame_component_spec {
41 	u8 component_identifier;
42 	u8 horizontal_sampling_factor;
43 	u8 vertical_sampling_factor;
44 	u8 quantization_table_selector;
45 };
46 
47 /**
48  * struct v4l2_jpeg_frame_header - JPEG frame header
49  * @height: Y
50  * @width: X
51  * @precision: P
52  * @num_components: Nf
53  * @component: component-specification, see v4l2_jpeg_frame_component_spec
54  * @subsampling: decoded subsampling from component-specification
55  */
56 struct v4l2_jpeg_frame_header {
57 	u16 height;
58 	u16 width;
59 	u8 precision;
60 	u8 num_components;
61 	struct v4l2_jpeg_frame_component_spec component[V4L2_JPEG_MAX_COMPONENTS];
62 	enum v4l2_jpeg_chroma_subsampling subsampling;
63 };
64 
65 /* B.2.3 Scan header syntax */
66 
67 /**
68  * struct v4l2_jpeg_scan_component_spec - scan component-specification
69  * @component_selector: Cs[j]
70  * @dc_entropy_coding_table_selector: Td[j]
71  * @ac_entropy_coding_table_selector: Ta[j]
72  */
73 struct v4l2_jpeg_scan_component_spec {
74 	u8 component_selector;
75 	u8 dc_entropy_coding_table_selector;
76 	u8 ac_entropy_coding_table_selector;
77 };
78 
79 /**
80  * struct v4l2_jpeg_scan_header - JPEG scan header
81  * @num_components: Ns
82  * @component: component-specification, see v4l2_jpeg_scan_component_spec
83  */
84 struct v4l2_jpeg_scan_header {
85 	u8 num_components;				/* Ns */
86 	struct v4l2_jpeg_scan_component_spec component[V4L2_JPEG_MAX_COMPONENTS];
87 	/* Ss, Se, Ah, and Al are not used by any driver */
88 };
89 
90 /**
91  * struct v4l2_jpeg_header - parsed JPEG header
92  * @sof: pointer to frame header and size
93  * @sos: pointer to scan header and size
94  * @dht: pointers to huffman tables and sizes
95  * @dqt: pointers to quantization tables and sizes
96  * @frame: parsed frame header
97  * @scan: pointer to parsed scan header, optional
98  * @quantization_tables: references to four quantization tables, optional
99  * @huffman_tables: references to four Huffman tables in DC0, DC1, AC0, AC1
100  *                  order, optional
101  * @restart_interval: number of MCU per restart interval, Ri
102  * @ecs_offset: buffer offset in bytes to the entropy coded segment
103  *
104  * When this structure is passed to v4l2_jpeg_parse_header, the optional scan,
105  * quantization_tables, and huffman_tables pointers must be initialized to NULL
106  * or point at valid memory.
107  */
108 struct v4l2_jpeg_header {
109 	struct v4l2_jpeg_reference sof;
110 	struct v4l2_jpeg_reference sos;
111 	unsigned int num_dht;
112 	struct v4l2_jpeg_reference dht[V4L2_JPEG_MAX_TABLES];
113 	unsigned int num_dqt;
114 	struct v4l2_jpeg_reference dqt[V4L2_JPEG_MAX_TABLES];
115 
116 	struct v4l2_jpeg_frame_header frame;
117 	struct v4l2_jpeg_scan_header *scan;
118 	struct v4l2_jpeg_reference *quantization_tables;
119 	struct v4l2_jpeg_reference *huffman_tables;
120 	u16 restart_interval;
121 	size_t ecs_offset;
122 };
123 
124 int v4l2_jpeg_parse_header(void *buf, size_t len, struct v4l2_jpeg_header *out);
125 
126 int v4l2_jpeg_parse_frame_header(void *buf, size_t len,
127 				 struct v4l2_jpeg_frame_header *frame_header);
128 int v4l2_jpeg_parse_scan_header(void *buf, size_t len,
129 				struct v4l2_jpeg_scan_header *scan_header);
130 int v4l2_jpeg_parse_quantization_tables(void *buf, size_t len, u8 precision,
131 					struct v4l2_jpeg_reference *q_tables);
132 int v4l2_jpeg_parse_huffman_tables(void *buf, size_t len,
133 				   struct v4l2_jpeg_reference *huffman_tables);
134 
135 #endif
136