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