/* SPDX-License-Identifier: GPL-2.0-only */1/*2* V4L2 JPEG helpers header3*4* Copyright (C) 2019 Pengutronix, Philipp Zabel <[email protected]>5*6* For reference, see JPEG ITU-T.81 (ISO/IEC 10918-1)7*/89#ifndef _V4L2_JPEG_H10#define _V4L2_JPEG_H1112#include <linux/v4l2-controls.h>1314#define V4L2_JPEG_MAX_COMPONENTS 415#define V4L2_JPEG_MAX_TABLES 416/*17* Prefixes used to generate huffman table class and destination identifiers as18* described below:19*20* V4L2_JPEG_LUM_HT | V4L2_JPEG_DC_HT : Prefix for Luma DC coefficients21* huffman table22* V4L2_JPEG_LUM_HT | V4L2_JPEG_AC_HT : Prefix for Luma AC coefficients23* huffman table24* V4L2_JPEG_CHR_HT | V4L2_JPEG_DC_HT : Prefix for Chroma DC coefficients25* huffman table26* V4L2_JPEG_CHR_HT | V4L2_JPEG_AC_HT : Prefix for Chroma AC coefficients27* huffman table28*/29#define V4L2_JPEG_LUM_HT 0x0030#define V4L2_JPEG_CHR_HT 0x0131#define V4L2_JPEG_DC_HT 0x0032#define V4L2_JPEG_AC_HT 0x103334/* Length of reference huffman tables as provided in Table K.3 of ITU-T.81 */35#define V4L2_JPEG_REF_HT_AC_LEN 17836#define V4L2_JPEG_REF_HT_DC_LEN 283738/* Array size for 8x8 block of samples or DCT coefficient */39#define V4L2_JPEG_PIXELS_IN_BLOCK 644041/**42* struct v4l2_jpeg_reference - reference into the JPEG buffer43* @start: pointer to the start of the referenced segment or table44* @length: size of the referenced segment or table45*46* Wnen referencing marker segments, start points right after the marker code,47* and length is the size of the segment parameters, excluding the marker code.48*/49struct v4l2_jpeg_reference {50u8 *start;51size_t length;52};5354/* B.2.2 Frame header syntax */5556/**57* struct v4l2_jpeg_frame_component_spec - frame component-specification58* @component_identifier: C[i]59* @horizontal_sampling_factor: H[i]60* @vertical_sampling_factor: V[i]61* @quantization_table_selector: quantization table destination selector Tq[i]62*/63struct v4l2_jpeg_frame_component_spec {64u8 component_identifier;65u8 horizontal_sampling_factor;66u8 vertical_sampling_factor;67u8 quantization_table_selector;68};6970/**71* struct v4l2_jpeg_frame_header - JPEG frame header72* @height: Y73* @width: X74* @precision: P75* @num_components: Nf76* @component: component-specification, see v4l2_jpeg_frame_component_spec77* @subsampling: decoded subsampling from component-specification78*/79struct v4l2_jpeg_frame_header {80u16 height;81u16 width;82u8 precision;83u8 num_components;84struct v4l2_jpeg_frame_component_spec component[V4L2_JPEG_MAX_COMPONENTS];85enum v4l2_jpeg_chroma_subsampling subsampling;86};8788/* B.2.3 Scan header syntax */8990/**91* struct v4l2_jpeg_scan_component_spec - scan component-specification92* @component_selector: Cs[j]93* @dc_entropy_coding_table_selector: Td[j]94* @ac_entropy_coding_table_selector: Ta[j]95*/96struct v4l2_jpeg_scan_component_spec {97u8 component_selector;98u8 dc_entropy_coding_table_selector;99u8 ac_entropy_coding_table_selector;100};101102/**103* struct v4l2_jpeg_scan_header - JPEG scan header104* @num_components: Ns105* @component: component-specification, see v4l2_jpeg_scan_component_spec106*/107struct v4l2_jpeg_scan_header {108u8 num_components; /* Ns */109struct v4l2_jpeg_scan_component_spec component[V4L2_JPEG_MAX_COMPONENTS];110/* Ss, Se, Ah, and Al are not used by any driver */111};112113/**114* enum v4l2_jpeg_app14_tf - APP14 transform flag115* According to Rec. ITU-T T.872 (06/2012) 6.5.3116* APP14 segment is for color encoding, it contains a transform flag,117* which may have values of 0, 1 and 2 and are interpreted as follows:118* @V4L2_JPEG_APP14_TF_CMYK_RGB: CMYK for images encoded with four components119* RGB for images encoded with three components120* @V4L2_JPEG_APP14_TF_YCBCR: an image encoded with three components using YCbCr121* @V4L2_JPEG_APP14_TF_YCCK: an image encoded with four components using YCCK122* @V4L2_JPEG_APP14_TF_UNKNOWN: indicate app14 is not present123*/124enum v4l2_jpeg_app14_tf {125V4L2_JPEG_APP14_TF_CMYK_RGB = 0,126V4L2_JPEG_APP14_TF_YCBCR = 1,127V4L2_JPEG_APP14_TF_YCCK = 2,128V4L2_JPEG_APP14_TF_UNKNOWN = -1,129};130131/**132* struct v4l2_jpeg_header - parsed JPEG header133* @sof: pointer to frame header and size134* @sos: pointer to scan header and size135* @num_dht: number of entries in @dht136* @dht: pointers to huffman tables and sizes137* @num_dqt: number of entries in @dqt138* @dqt: pointers to quantization tables and sizes139* @frame: parsed frame header140* @scan: pointer to parsed scan header, optional141* @quantization_tables: references to four quantization tables, optional142* @huffman_tables: references to four Huffman tables in DC0, DC1, AC0, AC1143* order, optional144* @restart_interval: number of MCU per restart interval, Ri145* @ecs_offset: buffer offset in bytes to the entropy coded segment146* @app14_tf: transform flag from app14 data147*148* When this structure is passed to v4l2_jpeg_parse_header, the optional scan,149* quantization_tables, and huffman_tables pointers must be initialized to NULL150* or point at valid memory.151*/152struct v4l2_jpeg_header {153struct v4l2_jpeg_reference sof;154struct v4l2_jpeg_reference sos;155unsigned int num_dht;156struct v4l2_jpeg_reference dht[V4L2_JPEG_MAX_TABLES];157unsigned int num_dqt;158struct v4l2_jpeg_reference dqt[V4L2_JPEG_MAX_TABLES];159160struct v4l2_jpeg_frame_header frame;161struct v4l2_jpeg_scan_header *scan;162struct v4l2_jpeg_reference *quantization_tables;163struct v4l2_jpeg_reference *huffman_tables;164u16 restart_interval;165size_t ecs_offset;166enum v4l2_jpeg_app14_tf app14_tf;167};168169int v4l2_jpeg_parse_header(void *buf, size_t len, struct v4l2_jpeg_header *out);170171extern const u8 v4l2_jpeg_zigzag_scan_index[V4L2_JPEG_PIXELS_IN_BLOCK];172extern const u8 v4l2_jpeg_ref_table_luma_qt[V4L2_JPEG_PIXELS_IN_BLOCK];173extern const u8 v4l2_jpeg_ref_table_chroma_qt[V4L2_JPEG_PIXELS_IN_BLOCK];174extern const u8 v4l2_jpeg_ref_table_luma_dc_ht[V4L2_JPEG_REF_HT_DC_LEN];175extern const u8 v4l2_jpeg_ref_table_luma_ac_ht[V4L2_JPEG_REF_HT_AC_LEN];176extern const u8 v4l2_jpeg_ref_table_chroma_dc_ht[V4L2_JPEG_REF_HT_DC_LEN];177extern const u8 v4l2_jpeg_ref_table_chroma_ac_ht[V4L2_JPEG_REF_HT_AC_LEN];178179#endif180181182