You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1076 lines
31KB

  1. /*
  2. * HEVC video decoder
  3. *
  4. * Copyright (C) 2012 - 2013 Guillaume Martres
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #ifndef AVCODEC_HEVC_H
  23. #define AVCODEC_HEVC_H
  24. #include <stddef.h>
  25. #include <stdint.h>
  26. #include "libavutil/buffer.h"
  27. #include "libavutil/md5.h"
  28. #include "avcodec.h"
  29. #include "cabac.h"
  30. #include "dsputil.h"
  31. #include "get_bits.h"
  32. #include "internal.h"
  33. #include "thread.h"
  34. #include "videodsp.h"
  35. #define MAX_DPB_SIZE 16 // A.4.1
  36. #define MAX_REFS 16
  37. /**
  38. * 7.4.2.1
  39. */
  40. #define MAX_SUB_LAYERS 7
  41. #define MAX_VPS_COUNT 16
  42. #define MAX_SPS_COUNT 32
  43. #define MAX_PPS_COUNT 256
  44. #define MAX_SHORT_TERM_RPS_COUNT 64
  45. #define MAX_CU_SIZE 128
  46. //TODO: check if this is really the maximum
  47. #define MAX_TRANSFORM_DEPTH 5
  48. #define MAX_TB_SIZE 32
  49. #define MAX_PB_SIZE 64
  50. #define MAX_LOG2_CTB_SIZE 6
  51. #define MAX_QP 51
  52. #define DEFAULT_INTRA_TC_OFFSET 2
  53. #define HEVC_CONTEXTS 183
  54. #define MRG_MAX_NUM_CANDS 5
  55. #define L0 0
  56. #define L1 1
  57. #define EPEL_EXTRA_BEFORE 1
  58. #define EPEL_EXTRA_AFTER 2
  59. #define EPEL_EXTRA 3
  60. /**
  61. * Value of the luma sample at position (x, y) in the 2D array tab.
  62. */
  63. #define SAMPLE(tab, x, y) ((tab)[(y) * s->sps->width + (x)])
  64. #define SAMPLE_CTB(tab, x, y) ((tab)[(y) * min_cb_width + (x)])
  65. #define SAMPLE_CBF(tab, x, y) ((tab)[((y) & ((1<<log2_trafo_size)-1)) * MAX_CU_SIZE + ((x) & ((1<<log2_trafo_size)-1))])
  66. #define IS_IDR(s) (s->nal_unit_type == NAL_IDR_W_RADL || s->nal_unit_type == NAL_IDR_N_LP)
  67. #define IS_BLA(s) (s->nal_unit_type == NAL_BLA_W_RADL || s->nal_unit_type == NAL_BLA_W_LP || \
  68. s->nal_unit_type == NAL_BLA_N_LP)
  69. #define IS_IRAP(s) (s->nal_unit_type >= 16 && s->nal_unit_type <= 23)
  70. /**
  71. * Table 7-3: NAL unit type codes
  72. */
  73. enum NALUnitType {
  74. NAL_TRAIL_N = 0,
  75. NAL_TRAIL_R = 1,
  76. NAL_TSA_N = 2,
  77. NAL_TSA_R = 3,
  78. NAL_STSA_N = 4,
  79. NAL_STSA_R = 5,
  80. NAL_RADL_N = 6,
  81. NAL_RADL_R = 7,
  82. NAL_RASL_N = 8,
  83. NAL_RASL_R = 9,
  84. NAL_BLA_W_LP = 16,
  85. NAL_BLA_W_RADL = 17,
  86. NAL_BLA_N_LP = 18,
  87. NAL_IDR_W_RADL = 19,
  88. NAL_IDR_N_LP = 20,
  89. NAL_CRA_NUT = 21,
  90. NAL_VPS = 32,
  91. NAL_SPS = 33,
  92. NAL_PPS = 34,
  93. NAL_AUD = 35,
  94. NAL_EOS_NUT = 36,
  95. NAL_EOB_NUT = 37,
  96. NAL_FD_NUT = 38,
  97. NAL_SEI_PREFIX = 39,
  98. NAL_SEI_SUFFIX = 40,
  99. };
  100. enum RPSType {
  101. ST_CURR_BEF = 0,
  102. ST_CURR_AFT,
  103. ST_FOLL,
  104. LT_CURR,
  105. LT_FOLL,
  106. NB_RPS_TYPE,
  107. };
  108. enum SliceType {
  109. B_SLICE = 0,
  110. P_SLICE = 1,
  111. I_SLICE = 2,
  112. };
  113. enum SyntaxElement {
  114. SAO_MERGE_FLAG = 0,
  115. SAO_TYPE_IDX,
  116. SAO_EO_CLASS,
  117. SAO_BAND_POSITION,
  118. SAO_OFFSET_ABS,
  119. SAO_OFFSET_SIGN,
  120. END_OF_SLICE_FLAG,
  121. SPLIT_CODING_UNIT_FLAG,
  122. CU_TRANSQUANT_BYPASS_FLAG,
  123. SKIP_FLAG,
  124. CU_QP_DELTA,
  125. PRED_MODE_FLAG,
  126. PART_MODE,
  127. PCM_FLAG,
  128. PREV_INTRA_LUMA_PRED_FLAG,
  129. MPM_IDX,
  130. REM_INTRA_LUMA_PRED_MODE,
  131. INTRA_CHROMA_PRED_MODE,
  132. MERGE_FLAG,
  133. MERGE_IDX,
  134. INTER_PRED_IDC,
  135. REF_IDX_L0,
  136. REF_IDX_L1,
  137. ABS_MVD_GREATER0_FLAG,
  138. ABS_MVD_GREATER1_FLAG,
  139. ABS_MVD_MINUS2,
  140. MVD_SIGN_FLAG,
  141. MVP_LX_FLAG,
  142. NO_RESIDUAL_DATA_FLAG,
  143. SPLIT_TRANSFORM_FLAG,
  144. CBF_LUMA,
  145. CBF_CB_CR,
  146. TRANSFORM_SKIP_FLAG,
  147. LAST_SIGNIFICANT_COEFF_X_PREFIX,
  148. LAST_SIGNIFICANT_COEFF_Y_PREFIX,
  149. LAST_SIGNIFICANT_COEFF_X_SUFFIX,
  150. LAST_SIGNIFICANT_COEFF_Y_SUFFIX,
  151. SIGNIFICANT_COEFF_GROUP_FLAG,
  152. SIGNIFICANT_COEFF_FLAG,
  153. COEFF_ABS_LEVEL_GREATER1_FLAG,
  154. COEFF_ABS_LEVEL_GREATER2_FLAG,
  155. COEFF_ABS_LEVEL_REMAINING,
  156. COEFF_SIGN_FLAG,
  157. };
  158. enum PartMode {
  159. PART_2Nx2N = 0,
  160. PART_2NxN = 1,
  161. PART_Nx2N = 2,
  162. PART_NxN = 3,
  163. PART_2NxnU = 4,
  164. PART_2NxnD = 5,
  165. PART_nLx2N = 6,
  166. PART_nRx2N = 7,
  167. };
  168. enum PredMode {
  169. MODE_INTER = 0,
  170. MODE_INTRA,
  171. MODE_SKIP,
  172. };
  173. enum InterPredIdc {
  174. PRED_L0 = 0,
  175. PRED_L1,
  176. PRED_BI,
  177. };
  178. enum IntraPredMode {
  179. INTRA_PLANAR = 0,
  180. INTRA_DC,
  181. INTRA_ANGULAR_2,
  182. INTRA_ANGULAR_3,
  183. INTRA_ANGULAR_4,
  184. INTRA_ANGULAR_5,
  185. INTRA_ANGULAR_6,
  186. INTRA_ANGULAR_7,
  187. INTRA_ANGULAR_8,
  188. INTRA_ANGULAR_9,
  189. INTRA_ANGULAR_10,
  190. INTRA_ANGULAR_11,
  191. INTRA_ANGULAR_12,
  192. INTRA_ANGULAR_13,
  193. INTRA_ANGULAR_14,
  194. INTRA_ANGULAR_15,
  195. INTRA_ANGULAR_16,
  196. INTRA_ANGULAR_17,
  197. INTRA_ANGULAR_18,
  198. INTRA_ANGULAR_19,
  199. INTRA_ANGULAR_20,
  200. INTRA_ANGULAR_21,
  201. INTRA_ANGULAR_22,
  202. INTRA_ANGULAR_23,
  203. INTRA_ANGULAR_24,
  204. INTRA_ANGULAR_25,
  205. INTRA_ANGULAR_26,
  206. INTRA_ANGULAR_27,
  207. INTRA_ANGULAR_28,
  208. INTRA_ANGULAR_29,
  209. INTRA_ANGULAR_30,
  210. INTRA_ANGULAR_31,
  211. INTRA_ANGULAR_32,
  212. INTRA_ANGULAR_33,
  213. INTRA_ANGULAR_34,
  214. };
  215. enum SAOType {
  216. SAO_NOT_APPLIED = 0,
  217. SAO_BAND,
  218. SAO_EDGE,
  219. };
  220. enum SAOEOClass {
  221. SAO_EO_HORIZ = 0,
  222. SAO_EO_VERT,
  223. SAO_EO_135D,
  224. SAO_EO_45D,
  225. };
  226. enum ScanType {
  227. SCAN_DIAG = 0,
  228. SCAN_HORIZ,
  229. SCAN_VERT,
  230. };
  231. typedef struct ShortTermRPS {
  232. int num_negative_pics;
  233. int num_delta_pocs;
  234. int32_t delta_poc[32];
  235. uint8_t used[32];
  236. } ShortTermRPS;
  237. typedef struct LongTermRPS {
  238. int poc[32];
  239. uint8_t used[32];
  240. uint8_t nb_refs;
  241. } LongTermRPS;
  242. typedef struct RefPicList {
  243. struct HEVCFrame *ref[MAX_REFS];
  244. int list[MAX_REFS];
  245. int isLongTerm[MAX_REFS];
  246. int nb_refs;
  247. } RefPicList;
  248. typedef struct RefPicListTab {
  249. RefPicList refPicList[2];
  250. } RefPicListTab;
  251. typedef struct HEVCWindow {
  252. int left_offset;
  253. int right_offset;
  254. int top_offset;
  255. int bottom_offset;
  256. } HEVCWindow;
  257. typedef struct VUI {
  258. AVRational sar;
  259. int overscan_info_present_flag;
  260. int overscan_appropriate_flag;
  261. int video_signal_type_present_flag;
  262. int video_format;
  263. int video_full_range_flag;
  264. int colour_description_present_flag;
  265. uint8_t colour_primaries;
  266. uint8_t transfer_characteristic;
  267. uint8_t matrix_coeffs;
  268. int chroma_loc_info_present_flag;
  269. int chroma_sample_loc_type_top_field;
  270. int chroma_sample_loc_type_bottom_field;
  271. int neutra_chroma_indication_flag;
  272. int field_seq_flag;
  273. int frame_field_info_present_flag;
  274. int default_display_window_flag;
  275. HEVCWindow def_disp_win;
  276. int vui_timing_info_present_flag;
  277. uint32_t vui_num_units_in_tick;
  278. uint32_t vui_time_scale;
  279. int vui_poc_proportional_to_timing_flag;
  280. int vui_num_ticks_poc_diff_one_minus1;
  281. int vui_hrd_parameters_present_flag;
  282. int bitstream_restriction_flag;
  283. int tiles_fixed_structure_flag;
  284. int motion_vectors_over_pic_boundaries_flag;
  285. int restricted_ref_pic_lists_flag;
  286. int min_spatial_segmentation_idc;
  287. int max_bytes_per_pic_denom;
  288. int max_bits_per_min_cu_denom;
  289. int log2_max_mv_length_horizontal;
  290. int log2_max_mv_length_vertical;
  291. } VUI;
  292. typedef struct PTLCommon {
  293. uint8_t profile_space;
  294. uint8_t tier_flag;
  295. uint8_t profile_idc;
  296. uint8_t profile_compatibility_flag[32];
  297. uint8_t level_idc;
  298. uint8_t progressive_source_flag;
  299. uint8_t interlaced_source_flag;
  300. uint8_t non_packed_constraint_flag;
  301. uint8_t frame_only_constraint_flag;
  302. } PTLCommon;
  303. typedef struct PTL {
  304. PTLCommon general_ptl;
  305. PTLCommon sub_layer_ptl[MAX_SUB_LAYERS];
  306. uint8_t sub_layer_profile_present_flag[MAX_SUB_LAYERS];
  307. uint8_t sub_layer_level_present_flag[MAX_SUB_LAYERS];
  308. } PTL;
  309. typedef struct HEVCVPS {
  310. uint8_t vps_temporal_id_nesting_flag;
  311. int vps_max_layers;
  312. int vps_max_sub_layers; ///< vps_max_temporal_layers_minus1 + 1
  313. PTL ptl;
  314. int vps_sub_layer_ordering_info_present_flag;
  315. unsigned int vps_max_dec_pic_buffering[MAX_SUB_LAYERS];
  316. unsigned int vps_num_reorder_pics[MAX_SUB_LAYERS];
  317. unsigned int vps_max_latency_increase[MAX_SUB_LAYERS];
  318. int vps_max_layer_id;
  319. int vps_num_layer_sets; ///< vps_num_layer_sets_minus1 + 1
  320. uint8_t vps_timing_info_present_flag;
  321. uint32_t vps_num_units_in_tick;
  322. uint32_t vps_time_scale;
  323. uint8_t vps_poc_proportional_to_timing_flag;
  324. int vps_num_ticks_poc_diff_one; ///< vps_num_ticks_poc_diff_one_minus1 + 1
  325. int vps_num_hrd_parameters;
  326. } HEVCVPS;
  327. typedef struct ScalingList {
  328. /* This is a little wasteful, since sizeID 0 only needs 8 coeffs,
  329. * and size ID 3 only has 2 arrays, not 6. */
  330. uint8_t sl[4][6][64];
  331. uint8_t sl_dc[2][6];
  332. } ScalingList;
  333. typedef struct HEVCSPS {
  334. int vps_id;
  335. int chroma_format_idc;
  336. uint8_t separate_colour_plane_flag;
  337. ///< output (i.e. cropped) values
  338. int output_width, output_height;
  339. HEVCWindow output_window;
  340. HEVCWindow pic_conf_win;
  341. int bit_depth;
  342. int pixel_shift;
  343. enum AVPixelFormat pix_fmt;
  344. unsigned int log2_max_poc_lsb;
  345. int pcm_enabled_flag;
  346. int max_sub_layers;
  347. struct {
  348. int max_dec_pic_buffering;
  349. int num_reorder_pics;
  350. int max_latency_increase;
  351. } temporal_layer[MAX_SUB_LAYERS];
  352. VUI vui;
  353. PTL ptl;
  354. uint8_t scaling_list_enable_flag;
  355. ScalingList scaling_list;
  356. unsigned int nb_st_rps;
  357. ShortTermRPS st_rps[MAX_SHORT_TERM_RPS_COUNT];
  358. uint8_t amp_enabled_flag;
  359. uint8_t sao_enabled;
  360. uint8_t long_term_ref_pics_present_flag;
  361. uint16_t lt_ref_pic_poc_lsb_sps[32];
  362. uint8_t used_by_curr_pic_lt_sps_flag[32];
  363. uint8_t num_long_term_ref_pics_sps;
  364. struct {
  365. uint8_t bit_depth;
  366. unsigned int log2_min_pcm_cb_size;
  367. unsigned int log2_max_pcm_cb_size;
  368. uint8_t loop_filter_disable_flag;
  369. } pcm;
  370. uint8_t sps_temporal_mvp_enabled_flag;
  371. uint8_t sps_strong_intra_smoothing_enable_flag;
  372. unsigned int log2_min_cb_size;
  373. unsigned int log2_diff_max_min_coding_block_size;
  374. unsigned int log2_min_tb_size;
  375. unsigned int log2_max_trafo_size;
  376. unsigned int log2_ctb_size;
  377. unsigned int log2_min_pu_size;
  378. int max_transform_hierarchy_depth_inter;
  379. int max_transform_hierarchy_depth_intra;
  380. ///< coded frame dimension in various units
  381. int width;
  382. int height;
  383. int ctb_width;
  384. int ctb_height;
  385. int ctb_size;
  386. int min_cb_width;
  387. int min_cb_height;
  388. int min_tb_width;
  389. int min_tb_height;
  390. int min_pu_width;
  391. int min_pu_height;
  392. int hshift[3];
  393. int vshift[3];
  394. int qp_bd_offset;
  395. } HEVCSPS;
  396. typedef struct HEVCPPS {
  397. int sps_id; ///< seq_parameter_set_id
  398. uint8_t sign_data_hiding_flag;
  399. uint8_t cabac_init_present_flag;
  400. int num_ref_idx_l0_default_active; ///< num_ref_idx_l0_default_active_minus1 + 1
  401. int num_ref_idx_l1_default_active; ///< num_ref_idx_l1_default_active_minus1 + 1
  402. int pic_init_qp_minus26;
  403. uint8_t constrained_intra_pred_flag;
  404. uint8_t transform_skip_enabled_flag;
  405. uint8_t cu_qp_delta_enabled_flag;
  406. int diff_cu_qp_delta_depth;
  407. int cb_qp_offset;
  408. int cr_qp_offset;
  409. uint8_t pic_slice_level_chroma_qp_offsets_present_flag;
  410. uint8_t weighted_pred_flag;
  411. uint8_t weighted_bipred_flag;
  412. uint8_t output_flag_present_flag;
  413. uint8_t transquant_bypass_enable_flag;
  414. uint8_t dependent_slice_segments_enabled_flag;
  415. uint8_t tiles_enabled_flag;
  416. uint8_t entropy_coding_sync_enabled_flag;
  417. int num_tile_columns; ///< num_tile_columns_minus1 + 1
  418. int num_tile_rows; ///< num_tile_rows_minus1 + 1
  419. uint8_t uniform_spacing_flag;
  420. uint8_t loop_filter_across_tiles_enabled_flag;
  421. uint8_t seq_loop_filter_across_slices_enabled_flag;
  422. uint8_t deblocking_filter_control_present_flag;
  423. uint8_t deblocking_filter_override_enabled_flag;
  424. uint8_t disable_dbf;
  425. int beta_offset; ///< beta_offset_div2 * 2
  426. int tc_offset; ///< tc_offset_div2 * 2
  427. uint8_t scaling_list_data_present_flag;
  428. ScalingList scaling_list;
  429. uint8_t lists_modification_present_flag;
  430. int log2_parallel_merge_level; ///< log2_parallel_merge_level_minus2 + 2
  431. int num_extra_slice_header_bits;
  432. uint8_t slice_header_extension_present_flag;
  433. // Inferred parameters
  434. int *column_width; ///< ColumnWidth
  435. int *row_height; ///< RowHeight
  436. int *col_bd; ///< ColBd
  437. int *row_bd; ///< RowBd
  438. int *col_idxX;
  439. int *ctb_addr_rs_to_ts; ///< CtbAddrRSToTS
  440. int *ctb_addr_ts_to_rs; ///< CtbAddrTSToRS
  441. int *tile_id; ///< TileId
  442. int *tile_pos_rs; ///< TilePosRS
  443. int *min_cb_addr_zs; ///< MinCbAddrZS
  444. int *min_tb_addr_zs; ///< MinTbAddrZS
  445. } HEVCPPS;
  446. typedef struct SliceHeader {
  447. int pps_id;
  448. ///< address (in raster order) of the first block in the current slice segment
  449. unsigned int slice_segment_addr;
  450. ///< address (in raster order) of the first block in the current slice
  451. unsigned int slice_addr;
  452. enum SliceType slice_type;
  453. int pic_order_cnt_lsb;
  454. uint8_t first_slice_in_pic_flag;
  455. uint8_t dependent_slice_segment_flag;
  456. uint8_t pic_output_flag;
  457. uint8_t colour_plane_id;
  458. ///< RPS coded in the slice header itself is stored here
  459. ShortTermRPS slice_rps;
  460. const ShortTermRPS *short_term_rps;
  461. LongTermRPS long_term_rps;
  462. unsigned int list_entry_lx[2][32];
  463. uint8_t rpl_modification_flag[2];
  464. uint8_t no_output_of_prior_pics_flag;
  465. uint8_t slice_temporal_mvp_enabled_flag;
  466. unsigned int nb_refs[2];
  467. uint8_t slice_sample_adaptive_offset_flag[3];
  468. uint8_t mvd_l1_zero_flag;
  469. uint8_t cabac_init_flag;
  470. uint8_t disable_deblocking_filter_flag; ///< slice_header_disable_deblocking_filter_flag
  471. uint8_t slice_loop_filter_across_slices_enabled_flag;
  472. uint8_t collocated_list;
  473. unsigned int collocated_ref_idx;
  474. int slice_qp_delta;
  475. int slice_cb_qp_offset;
  476. int slice_cr_qp_offset;
  477. int beta_offset; ///< beta_offset_div2 * 2
  478. int tc_offset; ///< tc_offset_div2 * 2
  479. unsigned int max_num_merge_cand; ///< 5 - 5_minus_max_num_merge_cand
  480. int num_entry_point_offsets;
  481. int8_t slice_qp;
  482. uint8_t luma_log2_weight_denom;
  483. int16_t chroma_log2_weight_denom;
  484. int16_t luma_weight_l0[16];
  485. int16_t chroma_weight_l0[16][2];
  486. int16_t chroma_weight_l1[16][2];
  487. int16_t luma_weight_l1[16];
  488. int16_t luma_offset_l0[16];
  489. int16_t chroma_offset_l0[16][2];
  490. int16_t luma_offset_l1[16];
  491. int16_t chroma_offset_l1[16][2];
  492. int slice_ctb_addr_rs;
  493. } SliceHeader;
  494. typedef struct CodingTree {
  495. int depth; ///< ctDepth
  496. } CodingTree;
  497. typedef struct CodingUnit {
  498. int x;
  499. int y;
  500. enum PredMode pred_mode; ///< PredMode
  501. enum PartMode part_mode; ///< PartMode
  502. uint8_t rqt_root_cbf;
  503. uint8_t pcm_flag;
  504. // Inferred parameters
  505. uint8_t intra_split_flag; ///< IntraSplitFlag
  506. uint8_t max_trafo_depth; ///< MaxTrafoDepth
  507. uint8_t cu_transquant_bypass_flag;
  508. } CodingUnit;
  509. typedef struct Mv {
  510. int16_t x; ///< horizontal component of motion vector
  511. int16_t y; ///< vertical component of motion vector
  512. } Mv;
  513. typedef struct MvField {
  514. Mv mv[2];
  515. int8_t ref_idx[2];
  516. int8_t pred_flag[2];
  517. uint8_t is_intra;
  518. } MvField;
  519. typedef struct NeighbourAvailable {
  520. int cand_bottom_left;
  521. int cand_left;
  522. int cand_up;
  523. int cand_up_left;
  524. int cand_up_right;
  525. int cand_up_right_sap;
  526. } NeighbourAvailable;
  527. typedef struct PredictionUnit {
  528. int mpm_idx;
  529. int rem_intra_luma_pred_mode;
  530. uint8_t intra_pred_mode[4];
  531. Mv mvd;
  532. uint8_t merge_flag;
  533. uint8_t intra_pred_mode_c;
  534. } PredictionUnit;
  535. typedef struct TransformTree {
  536. uint8_t cbf_cb[MAX_TRANSFORM_DEPTH][MAX_CU_SIZE * MAX_CU_SIZE];
  537. uint8_t cbf_cr[MAX_TRANSFORM_DEPTH][MAX_CU_SIZE * MAX_CU_SIZE];
  538. uint8_t cbf_luma;
  539. // Inferred parameters
  540. uint8_t inter_split_flag;
  541. } TransformTree;
  542. typedef struct TransformUnit {
  543. int cu_qp_delta;
  544. // Inferred parameters;
  545. int cur_intra_pred_mode;
  546. uint8_t is_cu_qp_delta_coded;
  547. } TransformUnit;
  548. typedef struct SAOParams {
  549. int offset_abs[3][4]; ///< sao_offset_abs
  550. int offset_sign[3][4]; ///< sao_offset_sign
  551. int band_position[3]; ///< sao_band_position
  552. int eo_class[3]; ///< sao_eo_class
  553. int offset_val[3][5]; ///<SaoOffsetVal
  554. uint8_t type_idx[3]; ///< sao_type_idx
  555. } SAOParams;
  556. typedef struct DBParams {
  557. int beta_offset;
  558. int tc_offset;
  559. } DBParams;
  560. #define HEVC_FRAME_FLAG_OUTPUT (1 << 0)
  561. #define HEVC_FRAME_FLAG_SHORT_REF (1 << 1)
  562. #define HEVC_FRAME_FLAG_LONG_REF (1 << 2)
  563. typedef struct HEVCFrame {
  564. AVFrame *frame;
  565. ThreadFrame tf;
  566. MvField *tab_mvf;
  567. RefPicList *refPicList;
  568. RefPicListTab **rpl_tab;
  569. int ctb_count;
  570. int poc;
  571. struct HEVCFrame *collocated_ref;
  572. HEVCWindow window;
  573. AVBufferRef *tab_mvf_buf;
  574. AVBufferRef *rpl_tab_buf;
  575. AVBufferRef *rpl_buf;
  576. /**
  577. * A sequence counter, so that old frames are output first
  578. * after a POC reset
  579. */
  580. uint16_t sequence;
  581. /**
  582. * A combination of HEVC_FRAME_FLAG_*
  583. */
  584. uint8_t flags;
  585. } HEVCFrame;
  586. typedef struct HEVCNAL {
  587. uint8_t *rbsp_buffer;
  588. int rbsp_buffer_size;
  589. int size;
  590. const uint8_t *data;
  591. } HEVCNAL;
  592. typedef struct HEVCDSPContext {
  593. void (*put_pcm)(uint8_t *dst, ptrdiff_t stride, int size,
  594. GetBitContext *gb, int pcm_bit_depth);
  595. void (*transquant_bypass[4])(uint8_t *dst, int16_t *coeffs,
  596. ptrdiff_t stride);
  597. void (*transform_skip)(uint8_t *dst, int16_t *coeffs, ptrdiff_t stride);
  598. void (*transform_4x4_luma_add)(uint8_t *dst, int16_t *coeffs,
  599. ptrdiff_t stride);
  600. void (*transform_add[4])(uint8_t *dst, int16_t *coeffs, ptrdiff_t stride);
  601. void (*sao_band_filter[4])(uint8_t *dst, uint8_t *src, ptrdiff_t stride,
  602. struct SAOParams *sao, int *borders,
  603. int width, int height, int c_idx);
  604. void (*sao_edge_filter[4])(uint8_t *dst, uint8_t *src, ptrdiff_t stride,
  605. struct SAOParams *sao, int *borders, int width,
  606. int height, int c_idx, uint8_t vert_edge,
  607. uint8_t horiz_edge, uint8_t diag_edge);
  608. void (*put_hevc_qpel[4][4])(int16_t *dst, ptrdiff_t dststride, uint8_t *src,
  609. ptrdiff_t srcstride, int width, int height,
  610. int16_t *mcbuffer);
  611. void (*put_hevc_epel[2][2])(int16_t *dst, ptrdiff_t dststride, uint8_t *src,
  612. ptrdiff_t srcstride, int width, int height,
  613. int mx, int my, int16_t *mcbuffer);
  614. void (*put_unweighted_pred)(uint8_t *dst, ptrdiff_t dststride, int16_t *src,
  615. ptrdiff_t srcstride, int width, int height);
  616. void (*put_weighted_pred_avg)(uint8_t *dst, ptrdiff_t dststride,
  617. int16_t *src1, int16_t *src2,
  618. ptrdiff_t srcstride, int width, int height);
  619. void (*weighted_pred)(uint8_t denom, int16_t wlxFlag, int16_t olxFlag,
  620. uint8_t *dst, ptrdiff_t dststride, int16_t *src,
  621. ptrdiff_t srcstride, int width, int height);
  622. void (*weighted_pred_avg)(uint8_t denom, int16_t wl0Flag, int16_t wl1Flag,
  623. int16_t ol0Flag, int16_t ol1Flag, uint8_t *dst,
  624. ptrdiff_t dststride, int16_t *src1, int16_t *src2,
  625. ptrdiff_t srcstride, int width, int height);
  626. void (*hevc_h_loop_filter_luma)(uint8_t *pix, ptrdiff_t stride,
  627. int *beta, int *tc,
  628. uint8_t *no_p, uint8_t *no_q);
  629. void (*hevc_v_loop_filter_luma)(uint8_t *pix, ptrdiff_t stride,
  630. int *beta, int *tc,
  631. uint8_t *no_p, uint8_t *no_q);
  632. void (*hevc_h_loop_filter_chroma)(uint8_t *pix, ptrdiff_t stride,
  633. int *tc, uint8_t *no_p, uint8_t *no_q);
  634. void (*hevc_v_loop_filter_chroma)(uint8_t *pix, ptrdiff_t stride,
  635. int *tc, uint8_t *no_p, uint8_t *no_q);
  636. void (*hevc_h_loop_filter_luma_c)(uint8_t *pix, ptrdiff_t stride,
  637. int *beta, int *tc,
  638. uint8_t *no_p, uint8_t *no_q);
  639. void (*hevc_v_loop_filter_luma_c)(uint8_t *pix, ptrdiff_t stride,
  640. int *beta, int *tc,
  641. uint8_t *no_p, uint8_t *no_q);
  642. void (*hevc_h_loop_filter_chroma_c)(uint8_t *pix, ptrdiff_t stride,
  643. int *tc, uint8_t *no_p,
  644. uint8_t *no_q);
  645. void (*hevc_v_loop_filter_chroma_c)(uint8_t *pix, ptrdiff_t stride,
  646. int *tc, uint8_t *no_p,
  647. uint8_t *no_q);
  648. } HEVCDSPContext;
  649. struct HEVCContext;
  650. typedef struct HEVCPredContext {
  651. void (*intra_pred)(struct HEVCContext *s, int x0, int y0,
  652. int log2_size, int c_idx);
  653. void (*pred_planar[4])(uint8_t *src, const uint8_t *top,
  654. const uint8_t *left, ptrdiff_t stride);
  655. void (*pred_dc)(uint8_t *src, const uint8_t *top, const uint8_t *left,
  656. ptrdiff_t stride, int log2_size, int c_idx);
  657. void (*pred_angular[4])(uint8_t *src, const uint8_t *top,
  658. const uint8_t *left, ptrdiff_t stride,
  659. int c_idx, int mode);
  660. } HEVCPredContext;
  661. typedef struct HEVCLocalContext {
  662. DECLARE_ALIGNED(16, int16_t, mc_buffer[(MAX_PB_SIZE + 7) * MAX_PB_SIZE]);
  663. uint8_t cabac_state[HEVC_CONTEXTS];
  664. uint8_t first_qp_group;
  665. GetBitContext gb;
  666. CABACContext cc;
  667. TransformTree tt;
  668. int8_t qp_y;
  669. int8_t curr_qp_y;
  670. TransformUnit tu;
  671. uint8_t ctb_left_flag;
  672. uint8_t ctb_up_flag;
  673. uint8_t ctb_up_right_flag;
  674. uint8_t ctb_up_left_flag;
  675. int start_of_tiles_x;
  676. int end_of_tiles_x;
  677. int end_of_tiles_y;
  678. uint8_t *edge_emu_buffer;
  679. int edge_emu_buffer_size;
  680. CodingTree ct;
  681. CodingUnit cu;
  682. PredictionUnit pu;
  683. NeighbourAvailable na;
  684. uint8_t slice_or_tiles_left_boundary;
  685. uint8_t slice_or_tiles_up_boundary;
  686. } HEVCLocalContext;
  687. typedef struct HEVCContext {
  688. const AVClass *c; // needed by private avoptions
  689. AVCodecContext *avctx;
  690. HEVCLocalContext HEVClc;
  691. uint8_t cabac_state[HEVC_CONTEXTS];
  692. /** 1 if the independent slice segment header was successfully parsed */
  693. uint8_t slice_initialized;
  694. AVFrame *frame;
  695. AVFrame *sao_frame;
  696. AVFrame *tmp_frame;
  697. AVFrame *output_frame;
  698. const HEVCVPS *vps;
  699. const HEVCSPS *sps;
  700. const HEVCPPS *pps;
  701. AVBufferRef *vps_list[MAX_VPS_COUNT];
  702. AVBufferRef *sps_list[MAX_SPS_COUNT];
  703. AVBufferRef *pps_list[MAX_PPS_COUNT];
  704. AVBufferPool *tab_mvf_pool;
  705. AVBufferPool *rpl_tab_pool;
  706. ///< candidate references for the current frame
  707. RefPicList rps[5];
  708. SliceHeader sh;
  709. SAOParams *sao;
  710. DBParams *deblock;
  711. enum NALUnitType nal_unit_type;
  712. int temporal_id; ///< temporal_id_plus1 - 1
  713. HEVCFrame *ref;
  714. HEVCFrame DPB[32];
  715. int poc;
  716. int pocTid0;
  717. int slice_idx; ///< number of the slice being currently decoded
  718. int eos; ///< current packet contains an EOS/EOB NAL
  719. int max_ra;
  720. int bs_width;
  721. int bs_height;
  722. int is_decoded;
  723. HEVCPredContext hpc;
  724. HEVCDSPContext hevcdsp;
  725. VideoDSPContext vdsp;
  726. DSPContext dsp;
  727. int8_t *qp_y_tab;
  728. uint8_t *split_cu_flag;
  729. uint8_t *horizontal_bs;
  730. uint8_t *vertical_bs;
  731. int32_t *tab_slice_address;
  732. // CU
  733. uint8_t *skip_flag;
  734. uint8_t *tab_ct_depth;
  735. // PU
  736. uint8_t *tab_ipm;
  737. uint8_t *cbf_luma; // cbf_luma of colocated TU
  738. uint8_t *is_pcm;
  739. // CTB-level flags affecting loop filter operation
  740. uint8_t *filter_slice_edges;
  741. /** used on BE to byteswap the lines for checksumming */
  742. uint8_t *checksum_buf;
  743. int checksum_buf_size;
  744. /**
  745. * Sequence counters for decoded and output frames, so that old
  746. * frames are output first after a POC reset
  747. */
  748. uint16_t seq_decode;
  749. uint16_t seq_output;
  750. HEVCNAL *nals;
  751. int nb_nals;
  752. int nals_allocated;
  753. // for checking the frame checksums
  754. struct AVMD5 *md5_ctx;
  755. uint8_t md5[3][16];
  756. uint8_t is_md5;
  757. uint8_t context_initialized;
  758. uint8_t is_nalff; ///< this flag is != 0 if bitstream is encapsulated
  759. ///< as a format defined in 14496-15
  760. int apply_defdispwin;
  761. int nal_length_size; ///< Number of bytes used for nal length (1, 2 or 4)
  762. int nuh_layer_id;
  763. /** frame packing arrangement variables */
  764. int sei_frame_packing_present;
  765. int frame_packing_arrangement_type;
  766. int content_interpretation_type;
  767. int quincunx_subsampling;
  768. } HEVCContext;
  769. int ff_hevc_decode_short_term_rps(HEVCContext *s, ShortTermRPS *rps,
  770. const HEVCSPS *sps, int is_slice_header);
  771. int ff_hevc_decode_nal_vps(HEVCContext *s);
  772. int ff_hevc_decode_nal_sps(HEVCContext *s);
  773. int ff_hevc_decode_nal_pps(HEVCContext *s);
  774. int ff_hevc_decode_nal_sei(HEVCContext *s);
  775. /**
  776. * Mark all frames in DPB as unused for reference.
  777. */
  778. void ff_hevc_clear_refs(HEVCContext *s);
  779. /**
  780. * Drop all frames currently in DPB.
  781. */
  782. void ff_hevc_flush_dpb(HEVCContext *s);
  783. /**
  784. * Compute POC of the current frame and return it.
  785. */
  786. int ff_hevc_compute_poc(HEVCContext *s, int poc_lsb);
  787. RefPicList *ff_hevc_get_ref_list(HEVCContext *s, HEVCFrame *frame,
  788. int x0, int y0);
  789. /**
  790. * Construct the reference picture sets for the current frame.
  791. */
  792. int ff_hevc_frame_rps(HEVCContext *s);
  793. /**
  794. * Construct the reference picture list(s) for the current slice.
  795. */
  796. int ff_hevc_slice_rpl(HEVCContext *s);
  797. void ff_hevc_save_states(HEVCContext *s, int ctb_addr_ts);
  798. void ff_hevc_cabac_init(HEVCContext *s, int ctb_addr_ts);
  799. int ff_hevc_sao_merge_flag_decode(HEVCContext *s);
  800. int ff_hevc_sao_type_idx_decode(HEVCContext *s);
  801. int ff_hevc_sao_band_position_decode(HEVCContext *s);
  802. int ff_hevc_sao_offset_abs_decode(HEVCContext *s);
  803. int ff_hevc_sao_offset_sign_decode(HEVCContext *s);
  804. int ff_hevc_sao_eo_class_decode(HEVCContext *s);
  805. int ff_hevc_end_of_slice_flag_decode(HEVCContext *s);
  806. int ff_hevc_cu_transquant_bypass_flag_decode(HEVCContext *s);
  807. int ff_hevc_skip_flag_decode(HEVCContext *s, int x0, int y0,
  808. int x_cb, int y_cb);
  809. int ff_hevc_pred_mode_decode(HEVCContext *s);
  810. int ff_hevc_split_coding_unit_flag_decode(HEVCContext *s, int ct_depth,
  811. int x0, int y0);
  812. int ff_hevc_part_mode_decode(HEVCContext *s, int log2_cb_size);
  813. int ff_hevc_pcm_flag_decode(HEVCContext *s);
  814. int ff_hevc_prev_intra_luma_pred_flag_decode(HEVCContext *s);
  815. int ff_hevc_mpm_idx_decode(HEVCContext *s);
  816. int ff_hevc_rem_intra_luma_pred_mode_decode(HEVCContext *s);
  817. int ff_hevc_intra_chroma_pred_mode_decode(HEVCContext *s);
  818. int ff_hevc_merge_idx_decode(HEVCContext *s);
  819. int ff_hevc_merge_flag_decode(HEVCContext *s);
  820. int ff_hevc_inter_pred_idc_decode(HEVCContext *s, int nPbW, int nPbH);
  821. int ff_hevc_ref_idx_lx_decode(HEVCContext *s, int num_ref_idx_lx);
  822. int ff_hevc_mvp_lx_flag_decode(HEVCContext *s);
  823. int ff_hevc_no_residual_syntax_flag_decode(HEVCContext *s);
  824. int ff_hevc_abs_mvd_greater0_flag_decode(HEVCContext *s);
  825. int ff_hevc_abs_mvd_greater1_flag_decode(HEVCContext *s);
  826. int ff_hevc_mvd_decode(HEVCContext *s);
  827. int ff_hevc_mvd_sign_flag_decode(HEVCContext *s);
  828. int ff_hevc_split_transform_flag_decode(HEVCContext *s, int log2_trafo_size);
  829. int ff_hevc_cbf_cb_cr_decode(HEVCContext *s, int trafo_depth);
  830. int ff_hevc_cbf_luma_decode(HEVCContext *s, int trafo_depth);
  831. int ff_hevc_transform_skip_flag_decode(HEVCContext *s, int c_idx);
  832. int ff_hevc_last_significant_coeff_x_prefix_decode(HEVCContext *s, int c_idx,
  833. int log2_size);
  834. int ff_hevc_last_significant_coeff_y_prefix_decode(HEVCContext *s, int c_idx,
  835. int log2_size);
  836. int ff_hevc_last_significant_coeff_suffix_decode(HEVCContext *s,
  837. int last_significant_coeff_prefix);
  838. int ff_hevc_significant_coeff_group_flag_decode(HEVCContext *s, int c_idx,
  839. int ctx_cg);
  840. int ff_hevc_significant_coeff_flag_decode(HEVCContext *s, int c_idx, int x_c,
  841. int y_c, int log2_trafo_size,
  842. int scan_idx, int prev_sig);
  843. int ff_hevc_coeff_abs_level_greater1_flag_decode(HEVCContext *s, int c_idx,
  844. int ctx_set);
  845. int ff_hevc_coeff_abs_level_greater2_flag_decode(HEVCContext *s, int c_idx,
  846. int inc);
  847. int ff_hevc_coeff_abs_level_remaining(HEVCContext *s, int base_level,
  848. int rc_rice_param);
  849. int ff_hevc_coeff_sign_flag(HEVCContext *s, uint8_t nb);
  850. /**
  851. * Get the number of candidate references for the current frame.
  852. */
  853. int ff_hevc_frame_nb_refs(HEVCContext *s);
  854. int ff_hevc_set_new_ref(HEVCContext *s, AVFrame **frame, int poc);
  855. /**
  856. * Find next frame in output order and put a reference to it in frame.
  857. * @return 1 if a frame was output, 0 otherwise
  858. */
  859. int ff_hevc_output_frame(HEVCContext *s, AVFrame *frame, int flush);
  860. void ff_hevc_unref_frame(HEVCContext *s, HEVCFrame *frame, int flags);
  861. void ff_hevc_set_neighbour_available(HEVCContext *s, int x0, int y0,
  862. int nPbW, int nPbH);
  863. void ff_hevc_luma_mv_merge_mode(HEVCContext *s, int x0, int y0,
  864. int nPbW, int nPbH, int log2_cb_size,
  865. int part_idx, int merge_idx, MvField *mv);
  866. void ff_hevc_luma_mv_mvp_mode(HEVCContext *s, int x0, int y0,
  867. int nPbW, int nPbH, int log2_cb_size,
  868. int part_idx, int merge_idx,
  869. MvField *mv, int mvp_lx_flag, int LX);
  870. void ff_hevc_set_qPy(HEVCContext *s, int xC, int yC, int xBase, int yBase,
  871. int log2_cb_size);
  872. void ff_hevc_deblocking_boundary_strengths(HEVCContext *s, int x0, int y0,
  873. int log2_trafo_size,
  874. int slice_or_tiles_up_boundary,
  875. int slice_or_tiles_left_boundary);
  876. int ff_hevc_cu_qp_delta_sign_flag(HEVCContext *s);
  877. int ff_hevc_cu_qp_delta_abs(HEVCContext *s);
  878. void ff_hevc_hls_filter(HEVCContext *s, int x, int y);
  879. void ff_hevc_hls_filters(HEVCContext *s, int x_ctb, int y_ctb, int ctb_size);
  880. void ff_hevc_pps_free(HEVCPPS **ppps);
  881. void ff_hevc_pred_init(HEVCPredContext *hpc, int bit_depth);
  882. void ff_hevc_dsp_init(HEVCDSPContext *hpc, int bit_depth);
  883. extern const int8_t ff_hevc_epel_filters[7][16];
  884. extern const uint8_t ff_hevc_qpel_extra_before[4];
  885. extern const uint8_t ff_hevc_qpel_extra_after[4];
  886. extern const uint8_t ff_hevc_qpel_extra[4];
  887. extern const uint8_t ff_hevc_diag_scan4x4_x[16];
  888. extern const uint8_t ff_hevc_diag_scan4x4_y[16];
  889. extern const uint8_t ff_hevc_diag_scan8x8_x[64];
  890. extern const uint8_t ff_hevc_diag_scan8x8_y[64];
  891. #endif /* AVCODEC_HEVC_H */