150733b5bSPhilipp Zabel /* SPDX-License-Identifier: GPL-2.0-only */ 250733b5bSPhilipp Zabel /* 350733b5bSPhilipp Zabel * V4L2 JPEG helpers header 450733b5bSPhilipp Zabel * 550733b5bSPhilipp Zabel * Copyright (C) 2019 Pengutronix, Philipp Zabel <kernel@pengutronix.de> 650733b5bSPhilipp Zabel * 750733b5bSPhilipp Zabel * For reference, see JPEG ITU-T.81 (ISO/IEC 10918-1) 850733b5bSPhilipp Zabel */ 950733b5bSPhilipp Zabel 1050733b5bSPhilipp Zabel #ifndef _V4L2_JPEG_H 1150733b5bSPhilipp Zabel #define _V4L2_JPEG_H 1250733b5bSPhilipp Zabel 1350733b5bSPhilipp Zabel #include <linux/v4l2-controls.h> 1450733b5bSPhilipp Zabel 1550733b5bSPhilipp Zabel #define V4L2_JPEG_MAX_COMPONENTS 4 1650733b5bSPhilipp Zabel #define V4L2_JPEG_MAX_TABLES 4 1750733b5bSPhilipp Zabel 1850733b5bSPhilipp Zabel /** 1950733b5bSPhilipp Zabel * struct v4l2_jpeg_reference - reference into the JPEG buffer 2050733b5bSPhilipp Zabel * @start: pointer to the start of the referenced segment or table 2150733b5bSPhilipp Zabel * @length: size of the referenced segment or table 2250733b5bSPhilipp Zabel * 2350733b5bSPhilipp Zabel * Wnen referencing marker segments, start points right after the marker code, 2450733b5bSPhilipp Zabel * and length is the size of the segment parameters, excluding the marker code. 2550733b5bSPhilipp Zabel */ 2650733b5bSPhilipp Zabel struct v4l2_jpeg_reference { 2750733b5bSPhilipp Zabel u8 *start; 2850733b5bSPhilipp Zabel size_t length; 2950733b5bSPhilipp Zabel }; 3050733b5bSPhilipp Zabel 3150733b5bSPhilipp Zabel /* B.2.2 Frame header syntax */ 3250733b5bSPhilipp Zabel 3350733b5bSPhilipp Zabel /** 3450733b5bSPhilipp Zabel * struct v4l2_jpeg_frame_component_spec - frame component-specification 3550733b5bSPhilipp Zabel * @component_identifier: C[i] 3650733b5bSPhilipp Zabel * @horizontal_sampling_factor: H[i] 3750733b5bSPhilipp Zabel * @vertical_sampling_factor: V[i] 3850733b5bSPhilipp Zabel * @quantization_table_selector: quantization table destination selector Tq[i] 3950733b5bSPhilipp Zabel */ 4050733b5bSPhilipp Zabel struct v4l2_jpeg_frame_component_spec { 4150733b5bSPhilipp Zabel u8 component_identifier; 4250733b5bSPhilipp Zabel u8 horizontal_sampling_factor; 4350733b5bSPhilipp Zabel u8 vertical_sampling_factor; 4450733b5bSPhilipp Zabel u8 quantization_table_selector; 4550733b5bSPhilipp Zabel }; 4650733b5bSPhilipp Zabel 4750733b5bSPhilipp Zabel /** 4850733b5bSPhilipp Zabel * struct v4l2_jpeg_frame_header - JPEG frame header 4950733b5bSPhilipp Zabel * @height: Y 5050733b5bSPhilipp Zabel * @width: X 5150733b5bSPhilipp Zabel * @precision: P 5250733b5bSPhilipp Zabel * @num_components: Nf 5350733b5bSPhilipp Zabel * @component: component-specification, see v4l2_jpeg_frame_component_spec 5450733b5bSPhilipp Zabel * @subsampling: decoded subsampling from component-specification 5550733b5bSPhilipp Zabel */ 5650733b5bSPhilipp Zabel struct v4l2_jpeg_frame_header { 5750733b5bSPhilipp Zabel u16 height; 5850733b5bSPhilipp Zabel u16 width; 5950733b5bSPhilipp Zabel u8 precision; 6050733b5bSPhilipp Zabel u8 num_components; 6150733b5bSPhilipp Zabel struct v4l2_jpeg_frame_component_spec component[V4L2_JPEG_MAX_COMPONENTS]; 6250733b5bSPhilipp Zabel enum v4l2_jpeg_chroma_subsampling subsampling; 6350733b5bSPhilipp Zabel }; 6450733b5bSPhilipp Zabel 6550733b5bSPhilipp Zabel /* B.2.3 Scan header syntax */ 6650733b5bSPhilipp Zabel 6750733b5bSPhilipp Zabel /** 6850733b5bSPhilipp Zabel * struct v4l2_jpeg_scan_component_spec - scan component-specification 6950733b5bSPhilipp Zabel * @component_selector: Cs[j] 7050733b5bSPhilipp Zabel * @dc_entropy_coding_table_selector: Td[j] 7150733b5bSPhilipp Zabel * @ac_entropy_coding_table_selector: Ta[j] 7250733b5bSPhilipp Zabel */ 7350733b5bSPhilipp Zabel struct v4l2_jpeg_scan_component_spec { 7450733b5bSPhilipp Zabel u8 component_selector; 7550733b5bSPhilipp Zabel u8 dc_entropy_coding_table_selector; 7650733b5bSPhilipp Zabel u8 ac_entropy_coding_table_selector; 7750733b5bSPhilipp Zabel }; 7850733b5bSPhilipp Zabel 7950733b5bSPhilipp Zabel /** 8050733b5bSPhilipp Zabel * struct v4l2_jpeg_scan_header - JPEG scan header 8150733b5bSPhilipp Zabel * @num_components: Ns 8250733b5bSPhilipp Zabel * @component: component-specification, see v4l2_jpeg_scan_component_spec 8350733b5bSPhilipp Zabel */ 8450733b5bSPhilipp Zabel struct v4l2_jpeg_scan_header { 8550733b5bSPhilipp Zabel u8 num_components; /* Ns */ 8650733b5bSPhilipp Zabel struct v4l2_jpeg_scan_component_spec component[V4L2_JPEG_MAX_COMPONENTS]; 8750733b5bSPhilipp Zabel /* Ss, Se, Ah, and Al are not used by any driver */ 8850733b5bSPhilipp Zabel }; 8950733b5bSPhilipp Zabel 9050733b5bSPhilipp Zabel /** 91*b8035f79SMirela Rabulea * enum v4l2_jpeg_app14_tf - APP14 transform flag 92*b8035f79SMirela Rabulea * According to Rec. ITU-T T.872 (06/2012) 6.5.3 93*b8035f79SMirela Rabulea * APP14 segment is for color encoding, it contains a transform flag, 94*b8035f79SMirela Rabulea * which may have values of 0, 1 and 2 and are interpreted as follows: 95*b8035f79SMirela Rabulea * @V4L2_JPEG_APP14_TF_CMYK_RGB: CMYK for images encoded with four components 96*b8035f79SMirela Rabulea * RGB for images encoded with three components 97*b8035f79SMirela Rabulea * @V4L2_JPEG_APP14_TF_YCBCR: an image encoded with three components using YCbCr 98*b8035f79SMirela Rabulea * @V4L2_JPEG_APP14_TF_YCCK: an image encoded with four components using YCCK 99*b8035f79SMirela Rabulea * @V4L2_JPEG_APP14_TF_UNKNOWN: indicate app14 is not present 100*b8035f79SMirela Rabulea */ 101*b8035f79SMirela Rabulea enum v4l2_jpeg_app14_tf { 102*b8035f79SMirela Rabulea V4L2_JPEG_APP14_TF_CMYK_RGB = 0, 103*b8035f79SMirela Rabulea V4L2_JPEG_APP14_TF_YCBCR = 1, 104*b8035f79SMirela Rabulea V4L2_JPEG_APP14_TF_YCCK = 2, 105*b8035f79SMirela Rabulea V4L2_JPEG_APP14_TF_UNKNOWN = -1, 106*b8035f79SMirela Rabulea }; 107*b8035f79SMirela Rabulea 108*b8035f79SMirela Rabulea /** 10950733b5bSPhilipp Zabel * struct v4l2_jpeg_header - parsed JPEG header 11050733b5bSPhilipp Zabel * @sof: pointer to frame header and size 11150733b5bSPhilipp Zabel * @sos: pointer to scan header and size 112f12b81e4SHans Verkuil * @num_dht: number of entries in @dht 11350733b5bSPhilipp Zabel * @dht: pointers to huffman tables and sizes 114f12b81e4SHans Verkuil * @num_dqt: number of entries in @dqt 11550733b5bSPhilipp Zabel * @dqt: pointers to quantization tables and sizes 11650733b5bSPhilipp Zabel * @frame: parsed frame header 11750733b5bSPhilipp Zabel * @scan: pointer to parsed scan header, optional 11850733b5bSPhilipp Zabel * @quantization_tables: references to four quantization tables, optional 11950733b5bSPhilipp Zabel * @huffman_tables: references to four Huffman tables in DC0, DC1, AC0, AC1 12050733b5bSPhilipp Zabel * order, optional 12150733b5bSPhilipp Zabel * @restart_interval: number of MCU per restart interval, Ri 12250733b5bSPhilipp Zabel * @ecs_offset: buffer offset in bytes to the entropy coded segment 123*b8035f79SMirela Rabulea * @app14_tf: transform flag from app14 data 12450733b5bSPhilipp Zabel * 12550733b5bSPhilipp Zabel * When this structure is passed to v4l2_jpeg_parse_header, the optional scan, 12650733b5bSPhilipp Zabel * quantization_tables, and huffman_tables pointers must be initialized to NULL 12750733b5bSPhilipp Zabel * or point at valid memory. 12850733b5bSPhilipp Zabel */ 12950733b5bSPhilipp Zabel struct v4l2_jpeg_header { 13050733b5bSPhilipp Zabel struct v4l2_jpeg_reference sof; 13150733b5bSPhilipp Zabel struct v4l2_jpeg_reference sos; 13250733b5bSPhilipp Zabel unsigned int num_dht; 13350733b5bSPhilipp Zabel struct v4l2_jpeg_reference dht[V4L2_JPEG_MAX_TABLES]; 13450733b5bSPhilipp Zabel unsigned int num_dqt; 13550733b5bSPhilipp Zabel struct v4l2_jpeg_reference dqt[V4L2_JPEG_MAX_TABLES]; 13650733b5bSPhilipp Zabel 13750733b5bSPhilipp Zabel struct v4l2_jpeg_frame_header frame; 13850733b5bSPhilipp Zabel struct v4l2_jpeg_scan_header *scan; 13950733b5bSPhilipp Zabel struct v4l2_jpeg_reference *quantization_tables; 14050733b5bSPhilipp Zabel struct v4l2_jpeg_reference *huffman_tables; 14150733b5bSPhilipp Zabel u16 restart_interval; 14250733b5bSPhilipp Zabel size_t ecs_offset; 143*b8035f79SMirela Rabulea enum v4l2_jpeg_app14_tf app14_tf; 14450733b5bSPhilipp Zabel }; 14550733b5bSPhilipp Zabel 14650733b5bSPhilipp Zabel int v4l2_jpeg_parse_header(void *buf, size_t len, struct v4l2_jpeg_header *out); 14750733b5bSPhilipp Zabel 14850733b5bSPhilipp Zabel int v4l2_jpeg_parse_frame_header(void *buf, size_t len, 14950733b5bSPhilipp Zabel struct v4l2_jpeg_frame_header *frame_header); 15050733b5bSPhilipp Zabel int v4l2_jpeg_parse_scan_header(void *buf, size_t len, 15150733b5bSPhilipp Zabel struct v4l2_jpeg_scan_header *scan_header); 15250733b5bSPhilipp Zabel int v4l2_jpeg_parse_quantization_tables(void *buf, size_t len, u8 precision, 15350733b5bSPhilipp Zabel struct v4l2_jpeg_reference *q_tables); 15450733b5bSPhilipp Zabel int v4l2_jpeg_parse_huffman_tables(void *buf, size_t len, 15550733b5bSPhilipp Zabel struct v4l2_jpeg_reference *huffman_tables); 15650733b5bSPhilipp Zabel 15750733b5bSPhilipp Zabel #endif 158