18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * V4L2 JPEG helpers header
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2019 Pengutronix, Philipp Zabel <kernel@pengutronix.de>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * For reference, see JPEG ITU-T.81 (ISO/IEC 10918-1)
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#ifndef _V4L2_JPEG_H
118c2ecf20Sopenharmony_ci#define _V4L2_JPEG_H
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/v4l2-controls.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#define V4L2_JPEG_MAX_COMPONENTS	4
168c2ecf20Sopenharmony_ci#define V4L2_JPEG_MAX_TABLES		4
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci/**
198c2ecf20Sopenharmony_ci * struct v4l2_jpeg_reference - reference into the JPEG buffer
208c2ecf20Sopenharmony_ci * @start: pointer to the start of the referenced segment or table
218c2ecf20Sopenharmony_ci * @length: size of the referenced segment or table
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci * Wnen referencing marker segments, start points right after the marker code,
248c2ecf20Sopenharmony_ci * and length is the size of the segment parameters, excluding the marker code.
258c2ecf20Sopenharmony_ci */
268c2ecf20Sopenharmony_cistruct v4l2_jpeg_reference {
278c2ecf20Sopenharmony_ci	u8 *start;
288c2ecf20Sopenharmony_ci	size_t length;
298c2ecf20Sopenharmony_ci};
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci/* B.2.2 Frame header syntax */
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/**
348c2ecf20Sopenharmony_ci * struct v4l2_jpeg_frame_component_spec - frame component-specification
358c2ecf20Sopenharmony_ci * @component_identifier: C[i]
368c2ecf20Sopenharmony_ci * @horizontal_sampling_factor: H[i]
378c2ecf20Sopenharmony_ci * @vertical_sampling_factor: V[i]
388c2ecf20Sopenharmony_ci * @quantization_table_selector: quantization table destination selector Tq[i]
398c2ecf20Sopenharmony_ci */
408c2ecf20Sopenharmony_cistruct v4l2_jpeg_frame_component_spec {
418c2ecf20Sopenharmony_ci	u8 component_identifier;
428c2ecf20Sopenharmony_ci	u8 horizontal_sampling_factor;
438c2ecf20Sopenharmony_ci	u8 vertical_sampling_factor;
448c2ecf20Sopenharmony_ci	u8 quantization_table_selector;
458c2ecf20Sopenharmony_ci};
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci/**
488c2ecf20Sopenharmony_ci * struct v4l2_jpeg_frame_header - JPEG frame header
498c2ecf20Sopenharmony_ci * @height: Y
508c2ecf20Sopenharmony_ci * @width: X
518c2ecf20Sopenharmony_ci * @precision: P
528c2ecf20Sopenharmony_ci * @num_components: Nf
538c2ecf20Sopenharmony_ci * @component: component-specification, see v4l2_jpeg_frame_component_spec
548c2ecf20Sopenharmony_ci * @subsampling: decoded subsampling from component-specification
558c2ecf20Sopenharmony_ci */
568c2ecf20Sopenharmony_cistruct v4l2_jpeg_frame_header {
578c2ecf20Sopenharmony_ci	u16 height;
588c2ecf20Sopenharmony_ci	u16 width;
598c2ecf20Sopenharmony_ci	u8 precision;
608c2ecf20Sopenharmony_ci	u8 num_components;
618c2ecf20Sopenharmony_ci	struct v4l2_jpeg_frame_component_spec component[V4L2_JPEG_MAX_COMPONENTS];
628c2ecf20Sopenharmony_ci	enum v4l2_jpeg_chroma_subsampling subsampling;
638c2ecf20Sopenharmony_ci};
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci/* B.2.3 Scan header syntax */
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci/**
688c2ecf20Sopenharmony_ci * struct v4l2_jpeg_scan_component_spec - scan component-specification
698c2ecf20Sopenharmony_ci * @component_selector: Cs[j]
708c2ecf20Sopenharmony_ci * @dc_entropy_coding_table_selector: Td[j]
718c2ecf20Sopenharmony_ci * @ac_entropy_coding_table_selector: Ta[j]
728c2ecf20Sopenharmony_ci */
738c2ecf20Sopenharmony_cistruct v4l2_jpeg_scan_component_spec {
748c2ecf20Sopenharmony_ci	u8 component_selector;
758c2ecf20Sopenharmony_ci	u8 dc_entropy_coding_table_selector;
768c2ecf20Sopenharmony_ci	u8 ac_entropy_coding_table_selector;
778c2ecf20Sopenharmony_ci};
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci/**
808c2ecf20Sopenharmony_ci * struct v4l2_jpeg_scan_header - JPEG scan header
818c2ecf20Sopenharmony_ci * @num_components: Ns
828c2ecf20Sopenharmony_ci * @component: component-specification, see v4l2_jpeg_scan_component_spec
838c2ecf20Sopenharmony_ci */
848c2ecf20Sopenharmony_cistruct v4l2_jpeg_scan_header {
858c2ecf20Sopenharmony_ci	u8 num_components;				/* Ns */
868c2ecf20Sopenharmony_ci	struct v4l2_jpeg_scan_component_spec component[V4L2_JPEG_MAX_COMPONENTS];
878c2ecf20Sopenharmony_ci	/* Ss, Se, Ah, and Al are not used by any driver */
888c2ecf20Sopenharmony_ci};
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci/**
918c2ecf20Sopenharmony_ci * struct v4l2_jpeg_header - parsed JPEG header
928c2ecf20Sopenharmony_ci * @sof: pointer to frame header and size
938c2ecf20Sopenharmony_ci * @sos: pointer to scan header and size
948c2ecf20Sopenharmony_ci * @dht: pointers to huffman tables and sizes
958c2ecf20Sopenharmony_ci * @dqt: pointers to quantization tables and sizes
968c2ecf20Sopenharmony_ci * @frame: parsed frame header
978c2ecf20Sopenharmony_ci * @scan: pointer to parsed scan header, optional
988c2ecf20Sopenharmony_ci * @quantization_tables: references to four quantization tables, optional
998c2ecf20Sopenharmony_ci * @huffman_tables: references to four Huffman tables in DC0, DC1, AC0, AC1
1008c2ecf20Sopenharmony_ci *                  order, optional
1018c2ecf20Sopenharmony_ci * @restart_interval: number of MCU per restart interval, Ri
1028c2ecf20Sopenharmony_ci * @ecs_offset: buffer offset in bytes to the entropy coded segment
1038c2ecf20Sopenharmony_ci *
1048c2ecf20Sopenharmony_ci * When this structure is passed to v4l2_jpeg_parse_header, the optional scan,
1058c2ecf20Sopenharmony_ci * quantization_tables, and huffman_tables pointers must be initialized to NULL
1068c2ecf20Sopenharmony_ci * or point at valid memory.
1078c2ecf20Sopenharmony_ci */
1088c2ecf20Sopenharmony_cistruct v4l2_jpeg_header {
1098c2ecf20Sopenharmony_ci	struct v4l2_jpeg_reference sof;
1108c2ecf20Sopenharmony_ci	struct v4l2_jpeg_reference sos;
1118c2ecf20Sopenharmony_ci	unsigned int num_dht;
1128c2ecf20Sopenharmony_ci	struct v4l2_jpeg_reference dht[V4L2_JPEG_MAX_TABLES];
1138c2ecf20Sopenharmony_ci	unsigned int num_dqt;
1148c2ecf20Sopenharmony_ci	struct v4l2_jpeg_reference dqt[V4L2_JPEG_MAX_TABLES];
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	struct v4l2_jpeg_frame_header frame;
1178c2ecf20Sopenharmony_ci	struct v4l2_jpeg_scan_header *scan;
1188c2ecf20Sopenharmony_ci	struct v4l2_jpeg_reference *quantization_tables;
1198c2ecf20Sopenharmony_ci	struct v4l2_jpeg_reference *huffman_tables;
1208c2ecf20Sopenharmony_ci	u16 restart_interval;
1218c2ecf20Sopenharmony_ci	size_t ecs_offset;
1228c2ecf20Sopenharmony_ci};
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ciint v4l2_jpeg_parse_header(void *buf, size_t len, struct v4l2_jpeg_header *out);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ciint v4l2_jpeg_parse_frame_header(void *buf, size_t len,
1278c2ecf20Sopenharmony_ci				 struct v4l2_jpeg_frame_header *frame_header);
1288c2ecf20Sopenharmony_ciint v4l2_jpeg_parse_scan_header(void *buf, size_t len,
1298c2ecf20Sopenharmony_ci				struct v4l2_jpeg_scan_header *scan_header);
1308c2ecf20Sopenharmony_ciint v4l2_jpeg_parse_quantization_tables(void *buf, size_t len, u8 precision,
1318c2ecf20Sopenharmony_ci					struct v4l2_jpeg_reference *q_tables);
1328c2ecf20Sopenharmony_ciint v4l2_jpeg_parse_huffman_tables(void *buf, size_t len,
1338c2ecf20Sopenharmony_ci				   struct v4l2_jpeg_reference *huffman_tables);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci#endif
136