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