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.

962 lines
25KB

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