162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2019 Pengutronix, Michael Tretter <kernel@pengutronix.de>
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Convert NAL units between raw byte sequence payloads (RBSP) and C structs.
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#ifndef __NAL_HEVC_H__
962306a36Sopenharmony_ci#define __NAL_HEVC_H__
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include <linux/errno.h>
1262306a36Sopenharmony_ci#include <linux/kernel.h>
1362306a36Sopenharmony_ci#include <linux/types.h>
1462306a36Sopenharmony_ci#include <linux/v4l2-controls.h>
1562306a36Sopenharmony_ci#include <linux/videodev2.h>
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_cistruct nal_hevc_profile_tier_level {
1862306a36Sopenharmony_ci	unsigned int general_profile_space;
1962306a36Sopenharmony_ci	unsigned int general_tier_flag;
2062306a36Sopenharmony_ci	unsigned int general_profile_idc;
2162306a36Sopenharmony_ci	unsigned int general_profile_compatibility_flag[32];
2262306a36Sopenharmony_ci	unsigned int general_progressive_source_flag;
2362306a36Sopenharmony_ci	unsigned int general_interlaced_source_flag;
2462306a36Sopenharmony_ci	unsigned int general_non_packed_constraint_flag;
2562306a36Sopenharmony_ci	unsigned int general_frame_only_constraint_flag;
2662306a36Sopenharmony_ci	union {
2762306a36Sopenharmony_ci		struct {
2862306a36Sopenharmony_ci			unsigned int general_max_12bit_constraint_flag;
2962306a36Sopenharmony_ci			unsigned int general_max_10bit_constraint_flag;
3062306a36Sopenharmony_ci			unsigned int general_max_8bit_constraint_flag;
3162306a36Sopenharmony_ci			unsigned int general_max_422chroma_constraint_flag;
3262306a36Sopenharmony_ci			unsigned int general_max_420chroma_constraint_flag;
3362306a36Sopenharmony_ci			unsigned int general_max_monochrome_constraint_flag;
3462306a36Sopenharmony_ci			unsigned int general_intra_constraint_flag;
3562306a36Sopenharmony_ci			unsigned int general_one_picture_only_constraint_flag;
3662306a36Sopenharmony_ci			unsigned int general_lower_bit_rate_constraint_flag;
3762306a36Sopenharmony_ci			union {
3862306a36Sopenharmony_ci				struct {
3962306a36Sopenharmony_ci					unsigned int general_max_14bit_constraint_flag;
4062306a36Sopenharmony_ci					unsigned int general_reserved_zero_33bits;
4162306a36Sopenharmony_ci				};
4262306a36Sopenharmony_ci				unsigned int general_reserved_zero_34bits;
4362306a36Sopenharmony_ci			};
4462306a36Sopenharmony_ci		};
4562306a36Sopenharmony_ci		struct {
4662306a36Sopenharmony_ci			unsigned int general_reserved_zero_7bits;
4762306a36Sopenharmony_ci			/* unsigned int general_one_picture_only_constraint_flag; */
4862306a36Sopenharmony_ci			unsigned int general_reserved_zero_35bits;
4962306a36Sopenharmony_ci		};
5062306a36Sopenharmony_ci		unsigned int general_reserved_zero_43bits;
5162306a36Sopenharmony_ci	};
5262306a36Sopenharmony_ci	union {
5362306a36Sopenharmony_ci		unsigned int general_inbld_flag;
5462306a36Sopenharmony_ci		unsigned int general_reserved_zero_bit;
5562306a36Sopenharmony_ci	};
5662306a36Sopenharmony_ci	unsigned int general_level_idc;
5762306a36Sopenharmony_ci};
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci/*
6062306a36Sopenharmony_ci * struct nal_hevc_vps - Video parameter set
6162306a36Sopenharmony_ci *
6262306a36Sopenharmony_ci * C struct representation of the video parameter set NAL unit as defined by
6362306a36Sopenharmony_ci * Rec. ITU-T H.265 (02/2018) 7.3.2.1 Video parameter set RBSP syntax
6462306a36Sopenharmony_ci */
6562306a36Sopenharmony_cistruct nal_hevc_vps {
6662306a36Sopenharmony_ci	unsigned int video_parameter_set_id;
6762306a36Sopenharmony_ci	unsigned int base_layer_internal_flag;
6862306a36Sopenharmony_ci	unsigned int base_layer_available_flag;
6962306a36Sopenharmony_ci	unsigned int max_layers_minus1;
7062306a36Sopenharmony_ci	unsigned int max_sub_layers_minus1;
7162306a36Sopenharmony_ci	unsigned int temporal_id_nesting_flag;
7262306a36Sopenharmony_ci	struct nal_hevc_profile_tier_level profile_tier_level;
7362306a36Sopenharmony_ci	unsigned int sub_layer_ordering_info_present_flag;
7462306a36Sopenharmony_ci	struct {
7562306a36Sopenharmony_ci		unsigned int max_dec_pic_buffering_minus1[7];
7662306a36Sopenharmony_ci		unsigned int max_num_reorder_pics[7];
7762306a36Sopenharmony_ci		unsigned int max_latency_increase_plus1[7];
7862306a36Sopenharmony_ci	};
7962306a36Sopenharmony_ci	unsigned int max_layer_id;
8062306a36Sopenharmony_ci	unsigned int num_layer_sets_minus1;
8162306a36Sopenharmony_ci	unsigned int layer_id_included_flag[1024][64];
8262306a36Sopenharmony_ci	unsigned int timing_info_present_flag;
8362306a36Sopenharmony_ci	struct {
8462306a36Sopenharmony_ci		unsigned int num_units_in_tick;
8562306a36Sopenharmony_ci		unsigned int time_scale;
8662306a36Sopenharmony_ci		unsigned int poc_proportional_to_timing_flag;
8762306a36Sopenharmony_ci		unsigned int num_ticks_poc_diff_one_minus1;
8862306a36Sopenharmony_ci		unsigned int num_hrd_parameters;
8962306a36Sopenharmony_ci		struct {
9062306a36Sopenharmony_ci			unsigned int hrd_layer_set_idx[0];
9162306a36Sopenharmony_ci			unsigned int cprms_present_flag[0];
9262306a36Sopenharmony_ci		};
9362306a36Sopenharmony_ci		/* hrd_parameters( cprms_present_flag[ i ], max_sub_layers_minus1 ) */
9462306a36Sopenharmony_ci	};
9562306a36Sopenharmony_ci	unsigned int extension_flag;
9662306a36Sopenharmony_ci	unsigned int extension_data_flag;
9762306a36Sopenharmony_ci};
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_cistruct nal_hevc_sub_layer_hrd_parameters {
10062306a36Sopenharmony_ci	unsigned int bit_rate_value_minus1[1];
10162306a36Sopenharmony_ci	unsigned int cpb_size_value_minus1[1];
10262306a36Sopenharmony_ci	unsigned int cbr_flag[1];
10362306a36Sopenharmony_ci};
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_cistruct nal_hevc_hrd_parameters {
10662306a36Sopenharmony_ci	unsigned int nal_hrd_parameters_present_flag;
10762306a36Sopenharmony_ci	unsigned int vcl_hrd_parameters_present_flag;
10862306a36Sopenharmony_ci	struct {
10962306a36Sopenharmony_ci		unsigned int sub_pic_hrd_params_present_flag;
11062306a36Sopenharmony_ci		struct {
11162306a36Sopenharmony_ci			unsigned int tick_divisor_minus2;
11262306a36Sopenharmony_ci			unsigned int du_cpb_removal_delay_increment_length_minus1;
11362306a36Sopenharmony_ci			unsigned int sub_pic_cpb_params_in_pic_timing_sei_flag;
11462306a36Sopenharmony_ci			unsigned int dpb_output_delay_du_length_minus1;
11562306a36Sopenharmony_ci		};
11662306a36Sopenharmony_ci		unsigned int bit_rate_scale;
11762306a36Sopenharmony_ci		unsigned int cpb_size_scale;
11862306a36Sopenharmony_ci		unsigned int cpb_size_du_scale;
11962306a36Sopenharmony_ci		unsigned int initial_cpb_removal_delay_length_minus1;
12062306a36Sopenharmony_ci		unsigned int au_cpb_removal_delay_length_minus1;
12162306a36Sopenharmony_ci		unsigned int dpb_output_delay_length_minus1;
12262306a36Sopenharmony_ci	};
12362306a36Sopenharmony_ci	struct {
12462306a36Sopenharmony_ci		unsigned int fixed_pic_rate_general_flag[1];
12562306a36Sopenharmony_ci		unsigned int fixed_pic_rate_within_cvs_flag[1];
12662306a36Sopenharmony_ci		unsigned int elemental_duration_in_tc_minus1[1];
12762306a36Sopenharmony_ci		unsigned int low_delay_hrd_flag[1];
12862306a36Sopenharmony_ci		unsigned int cpb_cnt_minus1[1];
12962306a36Sopenharmony_ci		struct nal_hevc_sub_layer_hrd_parameters nal_hrd[1];
13062306a36Sopenharmony_ci		struct nal_hevc_sub_layer_hrd_parameters vcl_hrd[1];
13162306a36Sopenharmony_ci	};
13262306a36Sopenharmony_ci};
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci/*
13562306a36Sopenharmony_ci * struct nal_hevc_vui_parameters - VUI parameters
13662306a36Sopenharmony_ci *
13762306a36Sopenharmony_ci * C struct representation of the VUI parameters as defined by Rec. ITU-T
13862306a36Sopenharmony_ci * H.265 (02/2018) E.2.1 VUI parameters syntax.
13962306a36Sopenharmony_ci */
14062306a36Sopenharmony_cistruct nal_hevc_vui_parameters {
14162306a36Sopenharmony_ci	unsigned int aspect_ratio_info_present_flag;
14262306a36Sopenharmony_ci	struct {
14362306a36Sopenharmony_ci		unsigned int aspect_ratio_idc;
14462306a36Sopenharmony_ci		unsigned int sar_width;
14562306a36Sopenharmony_ci		unsigned int sar_height;
14662306a36Sopenharmony_ci	};
14762306a36Sopenharmony_ci	unsigned int overscan_info_present_flag;
14862306a36Sopenharmony_ci	unsigned int overscan_appropriate_flag;
14962306a36Sopenharmony_ci	unsigned int video_signal_type_present_flag;
15062306a36Sopenharmony_ci	struct {
15162306a36Sopenharmony_ci		unsigned int video_format;
15262306a36Sopenharmony_ci		unsigned int video_full_range_flag;
15362306a36Sopenharmony_ci		unsigned int colour_description_present_flag;
15462306a36Sopenharmony_ci		struct {
15562306a36Sopenharmony_ci			unsigned int colour_primaries;
15662306a36Sopenharmony_ci			unsigned int transfer_characteristics;
15762306a36Sopenharmony_ci			unsigned int matrix_coeffs;
15862306a36Sopenharmony_ci		};
15962306a36Sopenharmony_ci	};
16062306a36Sopenharmony_ci	unsigned int chroma_loc_info_present_flag;
16162306a36Sopenharmony_ci	struct {
16262306a36Sopenharmony_ci		unsigned int chroma_sample_loc_type_top_field;
16362306a36Sopenharmony_ci		unsigned int chroma_sample_loc_type_bottom_field;
16462306a36Sopenharmony_ci	};
16562306a36Sopenharmony_ci	unsigned int neutral_chroma_indication_flag;
16662306a36Sopenharmony_ci	unsigned int field_seq_flag;
16762306a36Sopenharmony_ci	unsigned int frame_field_info_present_flag;
16862306a36Sopenharmony_ci	unsigned int default_display_window_flag;
16962306a36Sopenharmony_ci	struct {
17062306a36Sopenharmony_ci		unsigned int def_disp_win_left_offset;
17162306a36Sopenharmony_ci		unsigned int def_disp_win_right_offset;
17262306a36Sopenharmony_ci		unsigned int def_disp_win_top_offset;
17362306a36Sopenharmony_ci		unsigned int def_disp_win_bottom_offset;
17462306a36Sopenharmony_ci	};
17562306a36Sopenharmony_ci	unsigned int vui_timing_info_present_flag;
17662306a36Sopenharmony_ci	struct {
17762306a36Sopenharmony_ci		unsigned int vui_num_units_in_tick;
17862306a36Sopenharmony_ci		unsigned int vui_time_scale;
17962306a36Sopenharmony_ci		unsigned int vui_poc_proportional_to_timing_flag;
18062306a36Sopenharmony_ci		unsigned int vui_num_ticks_poc_diff_one_minus1;
18162306a36Sopenharmony_ci		unsigned int vui_hrd_parameters_present_flag;
18262306a36Sopenharmony_ci		struct nal_hevc_hrd_parameters nal_hrd_parameters;
18362306a36Sopenharmony_ci	};
18462306a36Sopenharmony_ci	unsigned int bitstream_restriction_flag;
18562306a36Sopenharmony_ci	struct {
18662306a36Sopenharmony_ci		unsigned int tiles_fixed_structure_flag;
18762306a36Sopenharmony_ci		unsigned int motion_vectors_over_pic_boundaries_flag;
18862306a36Sopenharmony_ci		unsigned int restricted_ref_pic_lists_flag;
18962306a36Sopenharmony_ci		unsigned int min_spatial_segmentation_idc;
19062306a36Sopenharmony_ci		unsigned int max_bytes_per_pic_denom;
19162306a36Sopenharmony_ci		unsigned int max_bits_per_min_cu_denom;
19262306a36Sopenharmony_ci		unsigned int log2_max_mv_length_horizontal;
19362306a36Sopenharmony_ci		unsigned int log2_max_mv_length_vertical;
19462306a36Sopenharmony_ci	};
19562306a36Sopenharmony_ci};
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_ci/*
19862306a36Sopenharmony_ci * struct nal_hevc_sps - Sequence parameter set
19962306a36Sopenharmony_ci *
20062306a36Sopenharmony_ci * C struct representation of the video parameter set NAL unit as defined by
20162306a36Sopenharmony_ci * Rec. ITU-T H.265 (02/2018) 7.3.2.2 Sequence parameter set RBSP syntax
20262306a36Sopenharmony_ci */
20362306a36Sopenharmony_cistruct nal_hevc_sps {
20462306a36Sopenharmony_ci	unsigned int video_parameter_set_id;
20562306a36Sopenharmony_ci	unsigned int max_sub_layers_minus1;
20662306a36Sopenharmony_ci	unsigned int temporal_id_nesting_flag;
20762306a36Sopenharmony_ci	struct nal_hevc_profile_tier_level profile_tier_level;
20862306a36Sopenharmony_ci	unsigned int seq_parameter_set_id;
20962306a36Sopenharmony_ci	unsigned int chroma_format_idc;
21062306a36Sopenharmony_ci	unsigned int separate_colour_plane_flag;
21162306a36Sopenharmony_ci	unsigned int pic_width_in_luma_samples;
21262306a36Sopenharmony_ci	unsigned int pic_height_in_luma_samples;
21362306a36Sopenharmony_ci	unsigned int conformance_window_flag;
21462306a36Sopenharmony_ci	struct {
21562306a36Sopenharmony_ci		unsigned int conf_win_left_offset;
21662306a36Sopenharmony_ci		unsigned int conf_win_right_offset;
21762306a36Sopenharmony_ci		unsigned int conf_win_top_offset;
21862306a36Sopenharmony_ci		unsigned int conf_win_bottom_offset;
21962306a36Sopenharmony_ci	};
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ci	unsigned int bit_depth_luma_minus8;
22262306a36Sopenharmony_ci	unsigned int bit_depth_chroma_minus8;
22362306a36Sopenharmony_ci	unsigned int log2_max_pic_order_cnt_lsb_minus4;
22462306a36Sopenharmony_ci	unsigned int sub_layer_ordering_info_present_flag;
22562306a36Sopenharmony_ci	struct {
22662306a36Sopenharmony_ci		unsigned int max_dec_pic_buffering_minus1[7];
22762306a36Sopenharmony_ci		unsigned int max_num_reorder_pics[7];
22862306a36Sopenharmony_ci		unsigned int max_latency_increase_plus1[7];
22962306a36Sopenharmony_ci	};
23062306a36Sopenharmony_ci	unsigned int log2_min_luma_coding_block_size_minus3;
23162306a36Sopenharmony_ci	unsigned int log2_diff_max_min_luma_coding_block_size;
23262306a36Sopenharmony_ci	unsigned int log2_min_luma_transform_block_size_minus2;
23362306a36Sopenharmony_ci	unsigned int log2_diff_max_min_luma_transform_block_size;
23462306a36Sopenharmony_ci	unsigned int max_transform_hierarchy_depth_inter;
23562306a36Sopenharmony_ci	unsigned int max_transform_hierarchy_depth_intra;
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_ci	unsigned int scaling_list_enabled_flag;
23862306a36Sopenharmony_ci	unsigned int scaling_list_data_present_flag;
23962306a36Sopenharmony_ci	unsigned int amp_enabled_flag;
24062306a36Sopenharmony_ci	unsigned int sample_adaptive_offset_enabled_flag;
24162306a36Sopenharmony_ci	unsigned int pcm_enabled_flag;
24262306a36Sopenharmony_ci	struct {
24362306a36Sopenharmony_ci		unsigned int pcm_sample_bit_depth_luma_minus1;
24462306a36Sopenharmony_ci		unsigned int pcm_sample_bit_depth_chroma_minus1;
24562306a36Sopenharmony_ci		unsigned int log2_min_pcm_luma_coding_block_size_minus3;
24662306a36Sopenharmony_ci		unsigned int log2_diff_max_min_pcm_luma_coding_block_size;
24762306a36Sopenharmony_ci		unsigned int pcm_loop_filter_disabled_flag;
24862306a36Sopenharmony_ci	};
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_ci	unsigned int num_short_term_ref_pic_sets;
25162306a36Sopenharmony_ci	unsigned int long_term_ref_pics_present_flag;
25262306a36Sopenharmony_ci	unsigned int sps_temporal_mvp_enabled_flag;
25362306a36Sopenharmony_ci	unsigned int strong_intra_smoothing_enabled_flag;
25462306a36Sopenharmony_ci	unsigned int vui_parameters_present_flag;
25562306a36Sopenharmony_ci	struct nal_hevc_vui_parameters vui;
25662306a36Sopenharmony_ci	unsigned int extension_present_flag;
25762306a36Sopenharmony_ci	struct {
25862306a36Sopenharmony_ci		unsigned int sps_range_extension_flag;
25962306a36Sopenharmony_ci		unsigned int sps_multilayer_extension_flag;
26062306a36Sopenharmony_ci		unsigned int sps_3d_extension_flag;
26162306a36Sopenharmony_ci		unsigned int sps_scc_extension_flag;
26262306a36Sopenharmony_ci		unsigned int sps_extension_4bits;
26362306a36Sopenharmony_ci	};
26462306a36Sopenharmony_ci};
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_cistruct nal_hevc_pps {
26762306a36Sopenharmony_ci	unsigned int pps_pic_parameter_set_id;
26862306a36Sopenharmony_ci	unsigned int pps_seq_parameter_set_id;
26962306a36Sopenharmony_ci	unsigned int dependent_slice_segments_enabled_flag;
27062306a36Sopenharmony_ci	unsigned int output_flag_present_flag;
27162306a36Sopenharmony_ci	unsigned int num_extra_slice_header_bits;
27262306a36Sopenharmony_ci	unsigned int sign_data_hiding_enabled_flag;
27362306a36Sopenharmony_ci	unsigned int cabac_init_present_flag;
27462306a36Sopenharmony_ci	unsigned int num_ref_idx_l0_default_active_minus1;
27562306a36Sopenharmony_ci	unsigned int num_ref_idx_l1_default_active_minus1;
27662306a36Sopenharmony_ci	int init_qp_minus26;
27762306a36Sopenharmony_ci	unsigned int constrained_intra_pred_flag;
27862306a36Sopenharmony_ci	unsigned int transform_skip_enabled_flag;
27962306a36Sopenharmony_ci	unsigned int cu_qp_delta_enabled_flag;
28062306a36Sopenharmony_ci	unsigned int diff_cu_qp_delta_depth;
28162306a36Sopenharmony_ci	int pps_cb_qp_offset;
28262306a36Sopenharmony_ci	int pps_cr_qp_offset;
28362306a36Sopenharmony_ci	unsigned int pps_slice_chroma_qp_offsets_present_flag;
28462306a36Sopenharmony_ci	unsigned int weighted_pred_flag;
28562306a36Sopenharmony_ci	unsigned int weighted_bipred_flag;
28662306a36Sopenharmony_ci	unsigned int transquant_bypass_enabled_flag;
28762306a36Sopenharmony_ci	unsigned int tiles_enabled_flag;
28862306a36Sopenharmony_ci	unsigned int entropy_coding_sync_enabled_flag;
28962306a36Sopenharmony_ci	struct {
29062306a36Sopenharmony_ci		unsigned int num_tile_columns_minus1;
29162306a36Sopenharmony_ci		unsigned int num_tile_rows_minus1;
29262306a36Sopenharmony_ci		unsigned int uniform_spacing_flag;
29362306a36Sopenharmony_ci		struct {
29462306a36Sopenharmony_ci			unsigned int column_width_minus1[1];
29562306a36Sopenharmony_ci			unsigned int row_height_minus1[1];
29662306a36Sopenharmony_ci		};
29762306a36Sopenharmony_ci		unsigned int loop_filter_across_tiles_enabled_flag;
29862306a36Sopenharmony_ci	};
29962306a36Sopenharmony_ci	unsigned int pps_loop_filter_across_slices_enabled_flag;
30062306a36Sopenharmony_ci	unsigned int deblocking_filter_control_present_flag;
30162306a36Sopenharmony_ci	struct {
30262306a36Sopenharmony_ci		unsigned int deblocking_filter_override_enabled_flag;
30362306a36Sopenharmony_ci		unsigned int pps_deblocking_filter_disabled_flag;
30462306a36Sopenharmony_ci		struct {
30562306a36Sopenharmony_ci			int pps_beta_offset_div2;
30662306a36Sopenharmony_ci			int pps_tc_offset_div2;
30762306a36Sopenharmony_ci		};
30862306a36Sopenharmony_ci	};
30962306a36Sopenharmony_ci	unsigned int pps_scaling_list_data_present_flag;
31062306a36Sopenharmony_ci	unsigned int lists_modification_present_flag;
31162306a36Sopenharmony_ci	unsigned int log2_parallel_merge_level_minus2;
31262306a36Sopenharmony_ci	unsigned int slice_segment_header_extension_present_flag;
31362306a36Sopenharmony_ci	unsigned int pps_extension_present_flag;
31462306a36Sopenharmony_ci	struct {
31562306a36Sopenharmony_ci		unsigned int pps_range_extension_flag;
31662306a36Sopenharmony_ci		unsigned int pps_multilayer_extension_flag;
31762306a36Sopenharmony_ci		unsigned int pps_3d_extension_flag;
31862306a36Sopenharmony_ci		unsigned int pps_scc_extension_flag;
31962306a36Sopenharmony_ci		unsigned int pps_extension_4bits;
32062306a36Sopenharmony_ci	};
32162306a36Sopenharmony_ci};
32262306a36Sopenharmony_ci
32362306a36Sopenharmony_ci/**
32462306a36Sopenharmony_ci * nal_hevc_profile() - Get profile_idc for v4l2 hevc profile
32562306a36Sopenharmony_ci * @profile: the profile as &enum v4l2_mpeg_video_hevc_profile
32662306a36Sopenharmony_ci *
32762306a36Sopenharmony_ci * Convert the &enum v4l2_mpeg_video_hevc_profile to profile_idc as specified
32862306a36Sopenharmony_ci * in Rec. ITU-T H.265 (02/2018) A.3.
32962306a36Sopenharmony_ci *
33062306a36Sopenharmony_ci * Return: the profile_idc for the passed level
33162306a36Sopenharmony_ci */
33262306a36Sopenharmony_cistatic inline int nal_hevc_profile(enum v4l2_mpeg_video_hevc_profile profile)
33362306a36Sopenharmony_ci{
33462306a36Sopenharmony_ci	switch (profile) {
33562306a36Sopenharmony_ci	case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN:
33662306a36Sopenharmony_ci		return 1;
33762306a36Sopenharmony_ci	case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10:
33862306a36Sopenharmony_ci		return 2;
33962306a36Sopenharmony_ci	case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE:
34062306a36Sopenharmony_ci		return 3;
34162306a36Sopenharmony_ci	default:
34262306a36Sopenharmony_ci		return -EINVAL;
34362306a36Sopenharmony_ci	}
34462306a36Sopenharmony_ci}
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_ci/**
34762306a36Sopenharmony_ci * nal_hevc_tier() - Get tier_flag for v4l2 hevc tier
34862306a36Sopenharmony_ci * @tier: the tier as &enum v4l2_mpeg_video_hevc_tier
34962306a36Sopenharmony_ci *
35062306a36Sopenharmony_ci * Convert the &enum v4l2_mpeg_video_hevc_tier to tier_flag as specified
35162306a36Sopenharmony_ci * in Rec. ITU-T H.265 (02/2018) A.4.1.
35262306a36Sopenharmony_ci *
35362306a36Sopenharmony_ci * Return: the tier_flag for the passed tier
35462306a36Sopenharmony_ci */
35562306a36Sopenharmony_cistatic inline int nal_hevc_tier(enum v4l2_mpeg_video_hevc_tier tier)
35662306a36Sopenharmony_ci{
35762306a36Sopenharmony_ci	switch (tier) {
35862306a36Sopenharmony_ci	case V4L2_MPEG_VIDEO_HEVC_TIER_MAIN:
35962306a36Sopenharmony_ci		return 0;
36062306a36Sopenharmony_ci	case V4L2_MPEG_VIDEO_HEVC_TIER_HIGH:
36162306a36Sopenharmony_ci		return 1;
36262306a36Sopenharmony_ci	default:
36362306a36Sopenharmony_ci		return -EINVAL;
36462306a36Sopenharmony_ci	}
36562306a36Sopenharmony_ci}
36662306a36Sopenharmony_ci
36762306a36Sopenharmony_ci/**
36862306a36Sopenharmony_ci * nal_hevc_level() - Get level_idc for v4l2 hevc level
36962306a36Sopenharmony_ci * @level: the level as &enum v4l2_mpeg_video_hevc_level
37062306a36Sopenharmony_ci *
37162306a36Sopenharmony_ci * Convert the &enum v4l2_mpeg_video_hevc_level to level_idc as specified in
37262306a36Sopenharmony_ci * Rec. ITU-T H.265 (02/2018) A.4.1.
37362306a36Sopenharmony_ci *
37462306a36Sopenharmony_ci * Return: the level_idc for the passed level
37562306a36Sopenharmony_ci */
37662306a36Sopenharmony_cistatic inline int nal_hevc_level(enum v4l2_mpeg_video_hevc_level level)
37762306a36Sopenharmony_ci{
37862306a36Sopenharmony_ci	/*
37962306a36Sopenharmony_ci	 * T-Rec-H.265 p. 280: general_level_idc and sub_layer_level_idc[ i ]
38062306a36Sopenharmony_ci	 * shall be set equal to a value of 30 times the level number
38162306a36Sopenharmony_ci	 * specified in Table A.6.
38262306a36Sopenharmony_ci	 */
38362306a36Sopenharmony_ci	int factor = 30 / 10;
38462306a36Sopenharmony_ci
38562306a36Sopenharmony_ci	switch (level) {
38662306a36Sopenharmony_ci	case V4L2_MPEG_VIDEO_HEVC_LEVEL_1:
38762306a36Sopenharmony_ci		return factor * 10;
38862306a36Sopenharmony_ci	case V4L2_MPEG_VIDEO_HEVC_LEVEL_2:
38962306a36Sopenharmony_ci		return factor * 20;
39062306a36Sopenharmony_ci	case V4L2_MPEG_VIDEO_HEVC_LEVEL_2_1:
39162306a36Sopenharmony_ci		return factor * 21;
39262306a36Sopenharmony_ci	case V4L2_MPEG_VIDEO_HEVC_LEVEL_3:
39362306a36Sopenharmony_ci		return factor * 30;
39462306a36Sopenharmony_ci	case V4L2_MPEG_VIDEO_HEVC_LEVEL_3_1:
39562306a36Sopenharmony_ci		return factor * 31;
39662306a36Sopenharmony_ci	case V4L2_MPEG_VIDEO_HEVC_LEVEL_4:
39762306a36Sopenharmony_ci		return factor * 40;
39862306a36Sopenharmony_ci	case V4L2_MPEG_VIDEO_HEVC_LEVEL_4_1:
39962306a36Sopenharmony_ci		return factor * 41;
40062306a36Sopenharmony_ci	case V4L2_MPEG_VIDEO_HEVC_LEVEL_5:
40162306a36Sopenharmony_ci		return factor * 50;
40262306a36Sopenharmony_ci	case V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1:
40362306a36Sopenharmony_ci		return factor * 51;
40462306a36Sopenharmony_ci	case V4L2_MPEG_VIDEO_HEVC_LEVEL_5_2:
40562306a36Sopenharmony_ci		return factor * 52;
40662306a36Sopenharmony_ci	case V4L2_MPEG_VIDEO_HEVC_LEVEL_6:
40762306a36Sopenharmony_ci		return factor * 60;
40862306a36Sopenharmony_ci	case V4L2_MPEG_VIDEO_HEVC_LEVEL_6_1:
40962306a36Sopenharmony_ci		return factor * 61;
41062306a36Sopenharmony_ci	case V4L2_MPEG_VIDEO_HEVC_LEVEL_6_2:
41162306a36Sopenharmony_ci		return factor * 62;
41262306a36Sopenharmony_ci	default:
41362306a36Sopenharmony_ci		return -EINVAL;
41462306a36Sopenharmony_ci	}
41562306a36Sopenharmony_ci}
41662306a36Sopenharmony_ci
41762306a36Sopenharmony_cistatic inline int nal_hevc_full_range(enum v4l2_quantization quantization)
41862306a36Sopenharmony_ci{
41962306a36Sopenharmony_ci	switch (quantization) {
42062306a36Sopenharmony_ci	case V4L2_QUANTIZATION_FULL_RANGE:
42162306a36Sopenharmony_ci		return 1;
42262306a36Sopenharmony_ci	case V4L2_QUANTIZATION_LIM_RANGE:
42362306a36Sopenharmony_ci		return 0;
42462306a36Sopenharmony_ci	default:
42562306a36Sopenharmony_ci		break;
42662306a36Sopenharmony_ci	}
42762306a36Sopenharmony_ci
42862306a36Sopenharmony_ci	return 0;
42962306a36Sopenharmony_ci}
43062306a36Sopenharmony_ci
43162306a36Sopenharmony_cistatic inline int nal_hevc_color_primaries(enum v4l2_colorspace colorspace)
43262306a36Sopenharmony_ci{
43362306a36Sopenharmony_ci	switch (colorspace) {
43462306a36Sopenharmony_ci	case V4L2_COLORSPACE_SMPTE170M:
43562306a36Sopenharmony_ci		return 6;
43662306a36Sopenharmony_ci	case V4L2_COLORSPACE_SMPTE240M:
43762306a36Sopenharmony_ci		return 7;
43862306a36Sopenharmony_ci	case V4L2_COLORSPACE_REC709:
43962306a36Sopenharmony_ci		return 1;
44062306a36Sopenharmony_ci	case V4L2_COLORSPACE_470_SYSTEM_M:
44162306a36Sopenharmony_ci		return 4;
44262306a36Sopenharmony_ci	case V4L2_COLORSPACE_JPEG:
44362306a36Sopenharmony_ci	case V4L2_COLORSPACE_SRGB:
44462306a36Sopenharmony_ci	case V4L2_COLORSPACE_470_SYSTEM_BG:
44562306a36Sopenharmony_ci		return 5;
44662306a36Sopenharmony_ci	case V4L2_COLORSPACE_BT2020:
44762306a36Sopenharmony_ci		return 9;
44862306a36Sopenharmony_ci	case V4L2_COLORSPACE_DEFAULT:
44962306a36Sopenharmony_ci	case V4L2_COLORSPACE_OPRGB:
45062306a36Sopenharmony_ci	case V4L2_COLORSPACE_RAW:
45162306a36Sopenharmony_ci	case V4L2_COLORSPACE_DCI_P3:
45262306a36Sopenharmony_ci	default:
45362306a36Sopenharmony_ci		return 2;
45462306a36Sopenharmony_ci	}
45562306a36Sopenharmony_ci}
45662306a36Sopenharmony_ci
45762306a36Sopenharmony_cistatic inline int nal_hevc_transfer_characteristics(enum v4l2_colorspace colorspace,
45862306a36Sopenharmony_ci						    enum v4l2_xfer_func xfer_func)
45962306a36Sopenharmony_ci{
46062306a36Sopenharmony_ci	if (xfer_func == V4L2_XFER_FUNC_DEFAULT)
46162306a36Sopenharmony_ci		xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(colorspace);
46262306a36Sopenharmony_ci
46362306a36Sopenharmony_ci	switch (xfer_func) {
46462306a36Sopenharmony_ci	case V4L2_XFER_FUNC_709:
46562306a36Sopenharmony_ci		return 6;
46662306a36Sopenharmony_ci	case V4L2_XFER_FUNC_SMPTE2084:
46762306a36Sopenharmony_ci		return 16;
46862306a36Sopenharmony_ci	case V4L2_XFER_FUNC_SRGB:
46962306a36Sopenharmony_ci	case V4L2_XFER_FUNC_OPRGB:
47062306a36Sopenharmony_ci	case V4L2_XFER_FUNC_NONE:
47162306a36Sopenharmony_ci	case V4L2_XFER_FUNC_DCI_P3:
47262306a36Sopenharmony_ci	case V4L2_XFER_FUNC_SMPTE240M:
47362306a36Sopenharmony_ci	default:
47462306a36Sopenharmony_ci		return 2;
47562306a36Sopenharmony_ci	}
47662306a36Sopenharmony_ci}
47762306a36Sopenharmony_ci
47862306a36Sopenharmony_cistatic inline int nal_hevc_matrix_coeffs(enum v4l2_colorspace colorspace,
47962306a36Sopenharmony_ci					 enum v4l2_ycbcr_encoding ycbcr_encoding)
48062306a36Sopenharmony_ci{
48162306a36Sopenharmony_ci	if (ycbcr_encoding == V4L2_YCBCR_ENC_DEFAULT)
48262306a36Sopenharmony_ci		ycbcr_encoding = V4L2_MAP_YCBCR_ENC_DEFAULT(colorspace);
48362306a36Sopenharmony_ci
48462306a36Sopenharmony_ci	switch (ycbcr_encoding) {
48562306a36Sopenharmony_ci	case V4L2_YCBCR_ENC_601:
48662306a36Sopenharmony_ci	case V4L2_YCBCR_ENC_XV601:
48762306a36Sopenharmony_ci		return 5;
48862306a36Sopenharmony_ci	case V4L2_YCBCR_ENC_709:
48962306a36Sopenharmony_ci	case V4L2_YCBCR_ENC_XV709:
49062306a36Sopenharmony_ci		return 1;
49162306a36Sopenharmony_ci	case V4L2_YCBCR_ENC_BT2020:
49262306a36Sopenharmony_ci		return 9;
49362306a36Sopenharmony_ci	case V4L2_YCBCR_ENC_BT2020_CONST_LUM:
49462306a36Sopenharmony_ci		return 10;
49562306a36Sopenharmony_ci	case V4L2_YCBCR_ENC_SMPTE240M:
49662306a36Sopenharmony_ci	default:
49762306a36Sopenharmony_ci		return 2;
49862306a36Sopenharmony_ci	}
49962306a36Sopenharmony_ci}
50062306a36Sopenharmony_ci
50162306a36Sopenharmony_cissize_t nal_hevc_write_vps(const struct device *dev,
50262306a36Sopenharmony_ci			   void *dest, size_t n, struct nal_hevc_vps *vps);
50362306a36Sopenharmony_cissize_t nal_hevc_read_vps(const struct device *dev,
50462306a36Sopenharmony_ci			  struct nal_hevc_vps *vps, void *src, size_t n);
50562306a36Sopenharmony_ci
50662306a36Sopenharmony_cissize_t nal_hevc_write_sps(const struct device *dev,
50762306a36Sopenharmony_ci			   void *dest, size_t n, struct nal_hevc_sps *sps);
50862306a36Sopenharmony_cissize_t nal_hevc_read_sps(const struct device *dev,
50962306a36Sopenharmony_ci			  struct nal_hevc_sps *sps, void *src, size_t n);
51062306a36Sopenharmony_ci
51162306a36Sopenharmony_cissize_t nal_hevc_write_pps(const struct device *dev,
51262306a36Sopenharmony_ci			   void *dest, size_t n, struct nal_hevc_pps *pps);
51362306a36Sopenharmony_cissize_t nal_hevc_read_pps(const struct device *dev,
51462306a36Sopenharmony_ci			  struct nal_hevc_pps *pps, void *src, size_t n);
51562306a36Sopenharmony_ci
51662306a36Sopenharmony_cissize_t nal_hevc_write_filler(const struct device *dev, void *dest, size_t n);
51762306a36Sopenharmony_cissize_t nal_hevc_read_filler(const struct device *dev, void *src, size_t n);
51862306a36Sopenharmony_ci
51962306a36Sopenharmony_ci#endif /* __NAL_HEVC_H__ */
520