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.

1060 lines
28KB

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