hevc_ps.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * HEVC parameter set parsing
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #ifndef AVCODEC_HEVC_PS_H
  21. #define AVCODEC_HEVC_PS_H
  22. #include <stdint.h>
  23. #include "libavutil/buffer.h"
  24. #include "libavutil/pixfmt.h"
  25. #include "libavutil/rational.h"
  26. #include "avcodec.h"
  27. #include "get_bits.h"
  28. #include "hevc.h"
  29. typedef struct ShortTermRPS {
  30. unsigned int num_negative_pics;
  31. int num_delta_pocs;
  32. int rps_idx_num_delta_pocs;
  33. int32_t delta_poc[32];
  34. uint8_t used[32];
  35. } ShortTermRPS;
  36. typedef struct HEVCWindow {
  37. unsigned int left_offset;
  38. unsigned int right_offset;
  39. unsigned int top_offset;
  40. unsigned int bottom_offset;
  41. } HEVCWindow;
  42. typedef struct VUI {
  43. AVRational sar;
  44. int overscan_info_present_flag;
  45. int overscan_appropriate_flag;
  46. int video_signal_type_present_flag;
  47. int video_format;
  48. int video_full_range_flag;
  49. int colour_description_present_flag;
  50. uint8_t colour_primaries;
  51. uint8_t transfer_characteristic;
  52. uint8_t matrix_coeffs;
  53. int chroma_loc_info_present_flag;
  54. int chroma_sample_loc_type_top_field;
  55. int chroma_sample_loc_type_bottom_field;
  56. int neutra_chroma_indication_flag;
  57. int field_seq_flag;
  58. int frame_field_info_present_flag;
  59. int default_display_window_flag;
  60. HEVCWindow def_disp_win;
  61. int vui_timing_info_present_flag;
  62. uint32_t vui_num_units_in_tick;
  63. uint32_t vui_time_scale;
  64. int vui_poc_proportional_to_timing_flag;
  65. int vui_num_ticks_poc_diff_one_minus1;
  66. int vui_hrd_parameters_present_flag;
  67. int bitstream_restriction_flag;
  68. int tiles_fixed_structure_flag;
  69. int motion_vectors_over_pic_boundaries_flag;
  70. int restricted_ref_pic_lists_flag;
  71. int min_spatial_segmentation_idc;
  72. int max_bytes_per_pic_denom;
  73. int max_bits_per_min_cu_denom;
  74. int log2_max_mv_length_horizontal;
  75. int log2_max_mv_length_vertical;
  76. } VUI;
  77. typedef struct PTLCommon {
  78. uint8_t profile_space;
  79. uint8_t tier_flag;
  80. uint8_t profile_idc;
  81. uint8_t profile_compatibility_flag[32];
  82. uint8_t progressive_source_flag;
  83. uint8_t interlaced_source_flag;
  84. uint8_t non_packed_constraint_flag;
  85. uint8_t frame_only_constraint_flag;
  86. uint8_t max_12bit_constraint_flag;
  87. uint8_t max_10bit_constraint_flag;
  88. uint8_t max_8bit_constraint_flag;
  89. uint8_t max_422chroma_constraint_flag;
  90. uint8_t max_420chroma_constraint_flag;
  91. uint8_t max_monochrome_constraint_flag;
  92. uint8_t intra_constraint_flag;
  93. uint8_t one_picture_only_constraint_flag;
  94. uint8_t lower_bit_rate_constraint_flag;
  95. uint8_t max_14bit_constraint_flag;
  96. uint8_t inbld_flag;
  97. uint8_t level_idc;
  98. } PTLCommon;
  99. typedef struct PTL {
  100. PTLCommon general_ptl;
  101. PTLCommon sub_layer_ptl[HEVC_MAX_SUB_LAYERS];
  102. uint8_t sub_layer_profile_present_flag[HEVC_MAX_SUB_LAYERS];
  103. uint8_t sub_layer_level_present_flag[HEVC_MAX_SUB_LAYERS];
  104. } PTL;
  105. typedef struct HEVCVPS {
  106. uint8_t vps_temporal_id_nesting_flag;
  107. int vps_max_layers;
  108. int vps_max_sub_layers; ///< vps_max_temporal_layers_minus1 + 1
  109. PTL ptl;
  110. int vps_sub_layer_ordering_info_present_flag;
  111. unsigned int vps_max_dec_pic_buffering[HEVC_MAX_SUB_LAYERS];
  112. unsigned int vps_num_reorder_pics[HEVC_MAX_SUB_LAYERS];
  113. unsigned int vps_max_latency_increase[HEVC_MAX_SUB_LAYERS];
  114. int vps_max_layer_id;
  115. int vps_num_layer_sets; ///< vps_num_layer_sets_minus1 + 1
  116. uint8_t vps_timing_info_present_flag;
  117. uint32_t vps_num_units_in_tick;
  118. uint32_t vps_time_scale;
  119. uint8_t vps_poc_proportional_to_timing_flag;
  120. int vps_num_ticks_poc_diff_one; ///< vps_num_ticks_poc_diff_one_minus1 + 1
  121. int vps_num_hrd_parameters;
  122. uint8_t data[4096];
  123. int data_size;
  124. } HEVCVPS;
  125. typedef struct ScalingList {
  126. /* This is a little wasteful, since sizeID 0 only needs 8 coeffs,
  127. * and size ID 3 only has 2 arrays, not 6. */
  128. uint8_t sl[4][6][64];
  129. uint8_t sl_dc[2][6];
  130. } ScalingList;
  131. typedef struct HEVCSPS {
  132. unsigned vps_id;
  133. int chroma_format_idc;
  134. uint8_t separate_colour_plane_flag;
  135. HEVCWindow output_window;
  136. HEVCWindow pic_conf_win;
  137. int bit_depth;
  138. int bit_depth_chroma;
  139. int pixel_shift;
  140. enum AVPixelFormat pix_fmt;
  141. unsigned int log2_max_poc_lsb;
  142. int pcm_enabled_flag;
  143. int max_sub_layers;
  144. struct {
  145. int max_dec_pic_buffering;
  146. int num_reorder_pics;
  147. int max_latency_increase;
  148. } temporal_layer[HEVC_MAX_SUB_LAYERS];
  149. uint8_t temporal_id_nesting_flag;
  150. VUI vui;
  151. PTL ptl;
  152. uint8_t scaling_list_enable_flag;
  153. ScalingList scaling_list;
  154. unsigned int nb_st_rps;
  155. ShortTermRPS st_rps[HEVC_MAX_SHORT_TERM_REF_PIC_SETS];
  156. uint8_t amp_enabled_flag;
  157. uint8_t sao_enabled;
  158. uint8_t long_term_ref_pics_present_flag;
  159. uint16_t lt_ref_pic_poc_lsb_sps[HEVC_MAX_LONG_TERM_REF_PICS];
  160. uint8_t used_by_curr_pic_lt_sps_flag[HEVC_MAX_LONG_TERM_REF_PICS];
  161. uint8_t num_long_term_ref_pics_sps;
  162. struct {
  163. uint8_t bit_depth;
  164. uint8_t bit_depth_chroma;
  165. unsigned int log2_min_pcm_cb_size;
  166. unsigned int log2_max_pcm_cb_size;
  167. uint8_t loop_filter_disable_flag;
  168. } pcm;
  169. uint8_t sps_temporal_mvp_enabled_flag;
  170. uint8_t sps_strong_intra_smoothing_enable_flag;
  171. unsigned int log2_min_cb_size;
  172. unsigned int log2_diff_max_min_coding_block_size;
  173. unsigned int log2_min_tb_size;
  174. unsigned int log2_max_trafo_size;
  175. unsigned int log2_ctb_size;
  176. unsigned int log2_min_pu_size;
  177. int max_transform_hierarchy_depth_inter;
  178. int max_transform_hierarchy_depth_intra;
  179. int sps_range_extension_flag;
  180. int transform_skip_rotation_enabled_flag;
  181. int transform_skip_context_enabled_flag;
  182. int implicit_rdpcm_enabled_flag;
  183. int explicit_rdpcm_enabled_flag;
  184. int extended_precision_processing_flag;
  185. int intra_smoothing_disabled_flag;
  186. int high_precision_offsets_enabled_flag;
  187. int persistent_rice_adaptation_enabled_flag;
  188. int cabac_bypass_alignment_enabled_flag;
  189. ///< coded frame dimension in various units
  190. int width;
  191. int height;
  192. int ctb_width;
  193. int ctb_height;
  194. int ctb_size;
  195. int min_cb_width;
  196. int min_cb_height;
  197. int min_tb_width;
  198. int min_tb_height;
  199. int min_pu_width;
  200. int min_pu_height;
  201. int tb_mask;
  202. int hshift[3];
  203. int vshift[3];
  204. int qp_bd_offset;
  205. uint8_t data[4096];
  206. int data_size;
  207. } HEVCSPS;
  208. typedef struct HEVCPPS {
  209. unsigned int sps_id; ///< seq_parameter_set_id
  210. uint8_t sign_data_hiding_flag;
  211. uint8_t cabac_init_present_flag;
  212. int num_ref_idx_l0_default_active; ///< num_ref_idx_l0_default_active_minus1 + 1
  213. int num_ref_idx_l1_default_active; ///< num_ref_idx_l1_default_active_minus1 + 1
  214. int pic_init_qp_minus26;
  215. uint8_t constrained_intra_pred_flag;
  216. uint8_t transform_skip_enabled_flag;
  217. uint8_t cu_qp_delta_enabled_flag;
  218. int diff_cu_qp_delta_depth;
  219. int cb_qp_offset;
  220. int cr_qp_offset;
  221. uint8_t pic_slice_level_chroma_qp_offsets_present_flag;
  222. uint8_t weighted_pred_flag;
  223. uint8_t weighted_bipred_flag;
  224. uint8_t output_flag_present_flag;
  225. uint8_t transquant_bypass_enable_flag;
  226. uint8_t dependent_slice_segments_enabled_flag;
  227. uint8_t tiles_enabled_flag;
  228. uint8_t entropy_coding_sync_enabled_flag;
  229. uint16_t num_tile_columns; ///< num_tile_columns_minus1 + 1
  230. uint16_t num_tile_rows; ///< num_tile_rows_minus1 + 1
  231. uint8_t uniform_spacing_flag;
  232. uint8_t loop_filter_across_tiles_enabled_flag;
  233. uint8_t seq_loop_filter_across_slices_enabled_flag;
  234. uint8_t deblocking_filter_control_present_flag;
  235. uint8_t deblocking_filter_override_enabled_flag;
  236. uint8_t disable_dbf;
  237. int beta_offset; ///< beta_offset_div2 * 2
  238. int tc_offset; ///< tc_offset_div2 * 2
  239. uint8_t scaling_list_data_present_flag;
  240. ScalingList scaling_list;
  241. uint8_t lists_modification_present_flag;
  242. int log2_parallel_merge_level; ///< log2_parallel_merge_level_minus2 + 2
  243. int num_extra_slice_header_bits;
  244. uint8_t slice_header_extension_present_flag;
  245. uint8_t log2_max_transform_skip_block_size;
  246. uint8_t pps_range_extensions_flag;
  247. uint8_t cross_component_prediction_enabled_flag;
  248. uint8_t chroma_qp_offset_list_enabled_flag;
  249. uint8_t diff_cu_chroma_qp_offset_depth;
  250. uint8_t chroma_qp_offset_list_len_minus1;
  251. int8_t cb_qp_offset_list[6];
  252. int8_t cr_qp_offset_list[6];
  253. uint8_t log2_sao_offset_scale_luma;
  254. uint8_t log2_sao_offset_scale_chroma;
  255. // Inferred parameters
  256. unsigned int *column_width; ///< ColumnWidth
  257. unsigned int *row_height; ///< RowHeight
  258. unsigned int *col_bd; ///< ColBd
  259. unsigned int *row_bd; ///< RowBd
  260. int *col_idxX;
  261. int *ctb_addr_rs_to_ts; ///< CtbAddrRSToTS
  262. int *ctb_addr_ts_to_rs; ///< CtbAddrTSToRS
  263. int *tile_id; ///< TileId
  264. int *tile_pos_rs; ///< TilePosRS
  265. int *min_tb_addr_zs; ///< MinTbAddrZS
  266. int *min_tb_addr_zs_tab;///< MinTbAddrZS
  267. uint8_t data[4096];
  268. int data_size;
  269. } HEVCPPS;
  270. typedef struct HEVCParamSets {
  271. AVBufferRef *vps_list[HEVC_MAX_VPS_COUNT];
  272. AVBufferRef *sps_list[HEVC_MAX_SPS_COUNT];
  273. AVBufferRef *pps_list[HEVC_MAX_PPS_COUNT];
  274. /* currently active parameter sets */
  275. const HEVCVPS *vps;
  276. const HEVCSPS *sps;
  277. const HEVCPPS *pps;
  278. } HEVCParamSets;
  279. /**
  280. * Parse the SPS from the bitstream into the provided HEVCSPS struct.
  281. *
  282. * @param sps_id the SPS id will be written here
  283. * @param apply_defdispwin if set 1, the default display window from the VUI
  284. * will be applied to the video dimensions
  285. * @param vps_list if non-NULL, this function will validate that the SPS refers
  286. * to an existing VPS
  287. */
  288. int ff_hevc_parse_sps(HEVCSPS *sps, GetBitContext *gb, unsigned int *sps_id,
  289. int apply_defdispwin, AVBufferRef **vps_list, AVCodecContext *avctx);
  290. int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx,
  291. HEVCParamSets *ps);
  292. int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx,
  293. HEVCParamSets *ps, int apply_defdispwin);
  294. int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx,
  295. HEVCParamSets *ps);
  296. void ff_hevc_ps_uninit(HEVCParamSets *ps);
  297. int ff_hevc_decode_short_term_rps(GetBitContext *gb, AVCodecContext *avctx,
  298. ShortTermRPS *rps, const HEVCSPS *sps, int is_slice_header);
  299. int ff_hevc_encode_nal_vps(HEVCVPS *vps, unsigned int id,
  300. uint8_t *buf, int buf_size);
  301. /**
  302. * Compute POC of the current frame and return it.
  303. */
  304. int ff_hevc_compute_poc(const HEVCSPS *sps, int pocTid0, int poc_lsb, int nal_unit_type);
  305. #endif /* AVCODEC_HEVC_PS_H */