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.

1073 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 PTL {
  293. int general_profile_space;
  294. uint8_t general_tier_flag;
  295. int general_profile_idc;
  296. int general_profile_compatibility_flag[32];
  297. int general_level_idc;
  298. uint8_t sub_layer_profile_present_flag[MAX_SUB_LAYERS];
  299. uint8_t sub_layer_level_present_flag[MAX_SUB_LAYERS];
  300. int sub_layer_profile_space[MAX_SUB_LAYERS];
  301. uint8_t sub_layer_tier_flag[MAX_SUB_LAYERS];
  302. int sub_layer_profile_idc[MAX_SUB_LAYERS];
  303. uint8_t sub_layer_profile_compatibility_flags[MAX_SUB_LAYERS][32];
  304. int sub_layer_level_idc[MAX_SUB_LAYERS];
  305. } PTL;
  306. typedef struct HEVCVPS {
  307. uint8_t vps_temporal_id_nesting_flag;
  308. int vps_max_layers;
  309. int vps_max_sub_layers; ///< vps_max_temporal_layers_minus1 + 1
  310. PTL ptl;
  311. int vps_sub_layer_ordering_info_present_flag;
  312. unsigned int vps_max_dec_pic_buffering[MAX_SUB_LAYERS];
  313. unsigned int vps_num_reorder_pics[MAX_SUB_LAYERS];
  314. unsigned int vps_max_latency_increase[MAX_SUB_LAYERS];
  315. int vps_max_layer_id;
  316. int vps_num_layer_sets; ///< vps_num_layer_sets_minus1 + 1
  317. uint8_t vps_timing_info_present_flag;
  318. uint32_t vps_num_units_in_tick;
  319. uint32_t vps_time_scale;
  320. uint8_t vps_poc_proportional_to_timing_flag;
  321. int vps_num_ticks_poc_diff_one; ///< vps_num_ticks_poc_diff_one_minus1 + 1
  322. int vps_num_hrd_parameters;
  323. } HEVCVPS;
  324. typedef struct ScalingList {
  325. /* This is a little wasteful, since sizeID 0 only needs 8 coeffs,
  326. * and size ID 3 only has 2 arrays, not 6. */
  327. uint8_t sl[4][6][64];
  328. uint8_t sl_dc[2][6];
  329. } ScalingList;
  330. typedef struct HEVCSPS {
  331. int vps_id;
  332. int chroma_format_idc;
  333. uint8_t separate_colour_plane_flag;
  334. ///< output (i.e. cropped) values
  335. int output_width, output_height;
  336. HEVCWindow output_window;
  337. HEVCWindow pic_conf_win;
  338. int bit_depth;
  339. int pixel_shift;
  340. enum AVPixelFormat pix_fmt;
  341. unsigned int log2_max_poc_lsb;
  342. int pcm_enabled_flag;
  343. int max_sub_layers;
  344. struct {
  345. int max_dec_pic_buffering;
  346. int num_reorder_pics;
  347. int max_latency_increase;
  348. } temporal_layer[MAX_SUB_LAYERS];
  349. VUI vui;
  350. PTL ptl;
  351. uint8_t scaling_list_enable_flag;
  352. ScalingList scaling_list;
  353. unsigned int nb_st_rps;
  354. ShortTermRPS st_rps[MAX_SHORT_TERM_RPS_COUNT];
  355. uint8_t amp_enabled_flag;
  356. uint8_t sao_enabled;
  357. uint8_t long_term_ref_pics_present_flag;
  358. uint16_t lt_ref_pic_poc_lsb_sps[32];
  359. uint8_t used_by_curr_pic_lt_sps_flag[32];
  360. uint8_t num_long_term_ref_pics_sps;
  361. struct {
  362. uint8_t bit_depth;
  363. unsigned int log2_min_pcm_cb_size;
  364. unsigned int log2_max_pcm_cb_size;
  365. uint8_t loop_filter_disable_flag;
  366. } pcm;
  367. uint8_t sps_temporal_mvp_enabled_flag;
  368. uint8_t sps_strong_intra_smoothing_enable_flag;
  369. unsigned int log2_min_cb_size;
  370. unsigned int log2_diff_max_min_coding_block_size;
  371. unsigned int log2_min_tb_size;
  372. unsigned int log2_max_trafo_size;
  373. unsigned int log2_ctb_size;
  374. unsigned int log2_min_pu_size;
  375. int max_transform_hierarchy_depth_inter;
  376. int max_transform_hierarchy_depth_intra;
  377. ///< coded frame dimension in various units
  378. int width;
  379. int height;
  380. int ctb_width;
  381. int ctb_height;
  382. int ctb_size;
  383. int min_cb_width;
  384. int min_cb_height;
  385. int min_tb_width;
  386. int min_tb_height;
  387. int min_pu_width;
  388. int min_pu_height;
  389. int hshift[3];
  390. int vshift[3];
  391. int qp_bd_offset;
  392. } HEVCSPS;
  393. typedef struct HEVCPPS {
  394. int sps_id; ///< seq_parameter_set_id
  395. uint8_t sign_data_hiding_flag;
  396. uint8_t cabac_init_present_flag;
  397. int num_ref_idx_l0_default_active; ///< num_ref_idx_l0_default_active_minus1 + 1
  398. int num_ref_idx_l1_default_active; ///< num_ref_idx_l1_default_active_minus1 + 1
  399. int pic_init_qp_minus26;
  400. uint8_t constrained_intra_pred_flag;
  401. uint8_t transform_skip_enabled_flag;
  402. uint8_t cu_qp_delta_enabled_flag;
  403. int diff_cu_qp_delta_depth;
  404. int cb_qp_offset;
  405. int cr_qp_offset;
  406. uint8_t pic_slice_level_chroma_qp_offsets_present_flag;
  407. uint8_t weighted_pred_flag;
  408. uint8_t weighted_bipred_flag;
  409. uint8_t output_flag_present_flag;
  410. uint8_t transquant_bypass_enable_flag;
  411. uint8_t dependent_slice_segments_enabled_flag;
  412. uint8_t tiles_enabled_flag;
  413. uint8_t entropy_coding_sync_enabled_flag;
  414. int num_tile_columns; ///< num_tile_columns_minus1 + 1
  415. int num_tile_rows; ///< num_tile_rows_minus1 + 1
  416. uint8_t uniform_spacing_flag;
  417. uint8_t loop_filter_across_tiles_enabled_flag;
  418. uint8_t seq_loop_filter_across_slices_enabled_flag;
  419. uint8_t deblocking_filter_control_present_flag;
  420. uint8_t deblocking_filter_override_enabled_flag;
  421. uint8_t disable_dbf;
  422. int beta_offset; ///< beta_offset_div2 * 2
  423. int tc_offset; ///< tc_offset_div2 * 2
  424. uint8_t scaling_list_data_present_flag;
  425. ScalingList scaling_list;
  426. uint8_t lists_modification_present_flag;
  427. int log2_parallel_merge_level; ///< log2_parallel_merge_level_minus2 + 2
  428. int num_extra_slice_header_bits;
  429. uint8_t slice_header_extension_present_flag;
  430. // Inferred parameters
  431. int *column_width; ///< ColumnWidth
  432. int *row_height; ///< RowHeight
  433. int *col_bd; ///< ColBd
  434. int *row_bd; ///< RowBd
  435. int *col_idxX;
  436. int *ctb_addr_rs_to_ts; ///< CtbAddrRSToTS
  437. int *ctb_addr_ts_to_rs; ///< CtbAddrTSToRS
  438. int *tile_id; ///< TileId
  439. int *tile_pos_rs; ///< TilePosRS
  440. int *min_cb_addr_zs; ///< MinCbAddrZS
  441. int *min_tb_addr_zs; ///< MinTbAddrZS
  442. } HEVCPPS;
  443. typedef struct SliceHeader {
  444. int pps_id;
  445. ///< address (in raster order) of the first block in the current slice segment
  446. unsigned int slice_segment_addr;
  447. ///< address (in raster order) of the first block in the current slice
  448. unsigned int slice_addr;
  449. enum SliceType slice_type;
  450. int pic_order_cnt_lsb;
  451. uint8_t first_slice_in_pic_flag;
  452. uint8_t dependent_slice_segment_flag;
  453. uint8_t pic_output_flag;
  454. uint8_t colour_plane_id;
  455. ///< RPS coded in the slice header itself is stored here
  456. ShortTermRPS slice_rps;
  457. const ShortTermRPS *short_term_rps;
  458. LongTermRPS long_term_rps;
  459. unsigned int list_entry_lx[2][32];
  460. uint8_t rpl_modification_flag[2];
  461. uint8_t no_output_of_prior_pics_flag;
  462. uint8_t slice_temporal_mvp_enabled_flag;
  463. unsigned int nb_refs[2];
  464. uint8_t slice_sample_adaptive_offset_flag[3];
  465. uint8_t mvd_l1_zero_flag;
  466. uint8_t cabac_init_flag;
  467. uint8_t disable_deblocking_filter_flag; ///< slice_header_disable_deblocking_filter_flag
  468. uint8_t slice_loop_filter_across_slices_enabled_flag;
  469. uint8_t collocated_list;
  470. unsigned int collocated_ref_idx;
  471. int slice_qp_delta;
  472. int slice_cb_qp_offset;
  473. int slice_cr_qp_offset;
  474. int beta_offset; ///< beta_offset_div2 * 2
  475. int tc_offset; ///< tc_offset_div2 * 2
  476. unsigned int max_num_merge_cand; ///< 5 - 5_minus_max_num_merge_cand
  477. int num_entry_point_offsets;
  478. int8_t slice_qp;
  479. uint8_t luma_log2_weight_denom;
  480. int16_t chroma_log2_weight_denom;
  481. int16_t luma_weight_l0[16];
  482. int16_t chroma_weight_l0[16][2];
  483. int16_t chroma_weight_l1[16][2];
  484. int16_t luma_weight_l1[16];
  485. int16_t luma_offset_l0[16];
  486. int16_t chroma_offset_l0[16][2];
  487. int16_t luma_offset_l1[16];
  488. int16_t chroma_offset_l1[16][2];
  489. int slice_ctb_addr_rs;
  490. } SliceHeader;
  491. typedef struct CodingTree {
  492. int depth; ///< ctDepth
  493. } CodingTree;
  494. typedef struct CodingUnit {
  495. int x;
  496. int y;
  497. enum PredMode pred_mode; ///< PredMode
  498. enum PartMode part_mode; ///< PartMode
  499. uint8_t rqt_root_cbf;
  500. uint8_t pcm_flag;
  501. // Inferred parameters
  502. uint8_t intra_split_flag; ///< IntraSplitFlag
  503. uint8_t max_trafo_depth; ///< MaxTrafoDepth
  504. uint8_t cu_transquant_bypass_flag;
  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. int mpm_idx;
  526. int rem_intra_luma_pred_mode;
  527. uint8_t intra_pred_mode[4];
  528. Mv mvd;
  529. uint8_t merge_flag;
  530. uint8_t intra_pred_mode_c;
  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. int cur_intra_pred_mode;
  543. uint8_t is_cu_qp_delta_coded;
  544. } TransformUnit;
  545. typedef struct SAOParams {
  546. int offset_abs[3][4]; ///< sao_offset_abs
  547. int offset_sign[3][4]; ///< sao_offset_sign
  548. int band_position[3]; ///< sao_band_position
  549. int eo_class[3]; ///< sao_eo_class
  550. int offset_val[3][5]; ///<SaoOffsetVal
  551. uint8_t type_idx[3]; ///< sao_type_idx
  552. } SAOParams;
  553. typedef struct DBParams {
  554. int beta_offset;
  555. int tc_offset;
  556. } DBParams;
  557. #define HEVC_FRAME_FLAG_OUTPUT (1 << 0)
  558. #define HEVC_FRAME_FLAG_SHORT_REF (1 << 1)
  559. #define HEVC_FRAME_FLAG_LONG_REF (1 << 2)
  560. typedef struct HEVCFrame {
  561. AVFrame *frame;
  562. ThreadFrame tf;
  563. MvField *tab_mvf;
  564. RefPicList *refPicList;
  565. RefPicListTab **rpl_tab;
  566. int ctb_count;
  567. int poc;
  568. struct HEVCFrame *collocated_ref;
  569. HEVCWindow window;
  570. AVBufferRef *tab_mvf_buf;
  571. AVBufferRef *rpl_tab_buf;
  572. AVBufferRef *rpl_buf;
  573. /**
  574. * A sequence counter, so that old frames are output first
  575. * after a POC reset
  576. */
  577. uint16_t sequence;
  578. /**
  579. * A combination of HEVC_FRAME_FLAG_*
  580. */
  581. uint8_t flags;
  582. } HEVCFrame;
  583. typedef struct HEVCNAL {
  584. uint8_t *rbsp_buffer;
  585. int rbsp_buffer_size;
  586. int size;
  587. const uint8_t *data;
  588. } HEVCNAL;
  589. typedef struct HEVCDSPContext {
  590. void (*put_pcm)(uint8_t *dst, ptrdiff_t stride, int size,
  591. GetBitContext *gb, int pcm_bit_depth);
  592. void (*transquant_bypass[4])(uint8_t *dst, int16_t *coeffs,
  593. ptrdiff_t stride);
  594. void (*transform_skip)(uint8_t *dst, int16_t *coeffs, ptrdiff_t stride);
  595. void (*transform_4x4_luma_add)(uint8_t *dst, int16_t *coeffs,
  596. ptrdiff_t stride);
  597. void (*transform_add[4])(uint8_t *dst, int16_t *coeffs, ptrdiff_t stride);
  598. void (*sao_band_filter[4])(uint8_t *dst, uint8_t *src, ptrdiff_t stride,
  599. struct SAOParams *sao, int *borders,
  600. int width, int height, int c_idx);
  601. void (*sao_edge_filter[4])(uint8_t *dst, uint8_t *src, ptrdiff_t stride,
  602. struct SAOParams *sao, int *borders, int width,
  603. int height, int c_idx, uint8_t vert_edge,
  604. uint8_t horiz_edge, uint8_t diag_edge);
  605. void (*put_hevc_qpel[4][4])(int16_t *dst, ptrdiff_t dststride, uint8_t *src,
  606. ptrdiff_t srcstride, int width, int height,
  607. int16_t *mcbuffer);
  608. void (*put_hevc_epel[2][2])(int16_t *dst, ptrdiff_t dststride, uint8_t *src,
  609. ptrdiff_t srcstride, int width, int height,
  610. int mx, int my, int16_t *mcbuffer);
  611. void (*put_unweighted_pred)(uint8_t *dst, ptrdiff_t dststride, int16_t *src,
  612. ptrdiff_t srcstride, int width, int height);
  613. void (*put_weighted_pred_avg)(uint8_t *dst, ptrdiff_t dststride,
  614. int16_t *src1, int16_t *src2,
  615. ptrdiff_t srcstride, int width, int height);
  616. void (*weighted_pred)(uint8_t denom, int16_t wlxFlag, int16_t olxFlag,
  617. uint8_t *dst, ptrdiff_t dststride, int16_t *src,
  618. ptrdiff_t srcstride, int width, int height);
  619. void (*weighted_pred_avg)(uint8_t denom, int16_t wl0Flag, int16_t wl1Flag,
  620. int16_t ol0Flag, int16_t ol1Flag, uint8_t *dst,
  621. ptrdiff_t dststride, int16_t *src1, int16_t *src2,
  622. ptrdiff_t srcstride, int width, int height);
  623. void (*hevc_h_loop_filter_luma)(uint8_t *pix, ptrdiff_t stride,
  624. int *beta, int *tc,
  625. uint8_t *no_p, uint8_t *no_q);
  626. void (*hevc_v_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_h_loop_filter_chroma)(uint8_t *pix, ptrdiff_t stride,
  630. int *tc, uint8_t *no_p, uint8_t *no_q);
  631. void (*hevc_v_loop_filter_chroma)(uint8_t *pix, ptrdiff_t stride,
  632. int *tc, uint8_t *no_p, uint8_t *no_q);
  633. void (*hevc_h_loop_filter_luma_c)(uint8_t *pix, ptrdiff_t stride,
  634. int *beta, int *tc,
  635. uint8_t *no_p, uint8_t *no_q);
  636. void (*hevc_v_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_h_loop_filter_chroma_c)(uint8_t *pix, ptrdiff_t stride,
  640. int *tc, uint8_t *no_p,
  641. uint8_t *no_q);
  642. void (*hevc_v_loop_filter_chroma_c)(uint8_t *pix, ptrdiff_t stride,
  643. int *tc, uint8_t *no_p,
  644. uint8_t *no_q);
  645. } HEVCDSPContext;
  646. struct HEVCContext;
  647. typedef struct HEVCPredContext {
  648. void (*intra_pred)(struct HEVCContext *s, int x0, int y0,
  649. int log2_size, int c_idx);
  650. void (*pred_planar[4])(uint8_t *src, const uint8_t *top,
  651. const uint8_t *left, ptrdiff_t stride);
  652. void (*pred_dc)(uint8_t *src, const uint8_t *top, const uint8_t *left,
  653. ptrdiff_t stride, int log2_size, int c_idx);
  654. void (*pred_angular[4])(uint8_t *src, const uint8_t *top,
  655. const uint8_t *left, ptrdiff_t stride,
  656. int c_idx, int mode);
  657. } HEVCPredContext;
  658. typedef struct HEVCLocalContext {
  659. DECLARE_ALIGNED(16, int16_t, mc_buffer[(MAX_PB_SIZE + 7) * MAX_PB_SIZE]);
  660. uint8_t cabac_state[HEVC_CONTEXTS];
  661. uint8_t first_qp_group;
  662. GetBitContext gb;
  663. CABACContext cc;
  664. TransformTree tt;
  665. int8_t qp_y;
  666. int8_t curr_qp_y;
  667. TransformUnit tu;
  668. uint8_t ctb_left_flag;
  669. uint8_t ctb_up_flag;
  670. uint8_t ctb_up_right_flag;
  671. uint8_t ctb_up_left_flag;
  672. int start_of_tiles_x;
  673. int end_of_tiles_x;
  674. int end_of_tiles_y;
  675. uint8_t *edge_emu_buffer;
  676. int edge_emu_buffer_size;
  677. CodingTree ct;
  678. CodingUnit cu;
  679. PredictionUnit pu;
  680. NeighbourAvailable na;
  681. uint8_t slice_or_tiles_left_boundary;
  682. uint8_t slice_or_tiles_up_boundary;
  683. } HEVCLocalContext;
  684. typedef struct HEVCContext {
  685. const AVClass *c; // needed by private avoptions
  686. AVCodecContext *avctx;
  687. HEVCLocalContext HEVClc;
  688. uint8_t cabac_state[HEVC_CONTEXTS];
  689. /** 1 if the independent slice segment header was successfully parsed */
  690. uint8_t slice_initialized;
  691. AVFrame *frame;
  692. AVFrame *sao_frame;
  693. AVFrame *tmp_frame;
  694. AVFrame *output_frame;
  695. const HEVCVPS *vps;
  696. const HEVCSPS *sps;
  697. const HEVCPPS *pps;
  698. AVBufferRef *vps_list[MAX_VPS_COUNT];
  699. AVBufferRef *sps_list[MAX_SPS_COUNT];
  700. AVBufferRef *pps_list[MAX_PPS_COUNT];
  701. AVBufferPool *tab_mvf_pool;
  702. AVBufferPool *rpl_tab_pool;
  703. ///< candidate references for the current frame
  704. RefPicList rps[5];
  705. SliceHeader sh;
  706. SAOParams *sao;
  707. DBParams *deblock;
  708. enum NALUnitType nal_unit_type;
  709. int temporal_id; ///< temporal_id_plus1 - 1
  710. HEVCFrame *ref;
  711. HEVCFrame DPB[32];
  712. int poc;
  713. int pocTid0;
  714. int slice_idx; ///< number of the slice being currently decoded
  715. int eos; ///< current packet contains an EOS/EOB NAL
  716. int max_ra;
  717. int bs_width;
  718. int bs_height;
  719. int is_decoded;
  720. HEVCPredContext hpc;
  721. HEVCDSPContext hevcdsp;
  722. VideoDSPContext vdsp;
  723. DSPContext dsp;
  724. int8_t *qp_y_tab;
  725. uint8_t *split_cu_flag;
  726. uint8_t *horizontal_bs;
  727. uint8_t *vertical_bs;
  728. int32_t *tab_slice_address;
  729. // CU
  730. uint8_t *skip_flag;
  731. uint8_t *tab_ct_depth;
  732. // PU
  733. uint8_t *tab_ipm;
  734. uint8_t *cbf_luma; // cbf_luma of colocated TU
  735. uint8_t *is_pcm;
  736. // CTB-level flags affecting loop filter operation
  737. uint8_t *filter_slice_edges;
  738. /** used on BE to byteswap the lines for checksumming */
  739. uint8_t *checksum_buf;
  740. int checksum_buf_size;
  741. /**
  742. * Sequence counters for decoded and output frames, so that old
  743. * frames are output first after a POC reset
  744. */
  745. uint16_t seq_decode;
  746. uint16_t seq_output;
  747. HEVCNAL *nals;
  748. int nb_nals;
  749. int nals_allocated;
  750. // for checking the frame checksums
  751. struct AVMD5 *md5_ctx;
  752. uint8_t md5[3][16];
  753. uint8_t is_md5;
  754. uint8_t context_initialized;
  755. uint8_t is_nalff; ///< this flag is != 0 if bitstream is encapsulated
  756. ///< as a format defined in 14496-15
  757. int apply_defdispwin;
  758. int nal_length_size; ///< Number of bytes used for nal length (1, 2 or 4)
  759. int nuh_layer_id;
  760. /** frame packing arrangement variables */
  761. int sei_frame_packing_present;
  762. int frame_packing_arrangement_type;
  763. int content_interpretation_type;
  764. int quincunx_subsampling;
  765. } HEVCContext;
  766. int ff_hevc_decode_short_term_rps(HEVCContext *s, ShortTermRPS *rps,
  767. const HEVCSPS *sps, int is_slice_header);
  768. int ff_hevc_decode_nal_vps(HEVCContext *s);
  769. int ff_hevc_decode_nal_sps(HEVCContext *s);
  770. int ff_hevc_decode_nal_pps(HEVCContext *s);
  771. int ff_hevc_decode_nal_sei(HEVCContext *s);
  772. /**
  773. * Mark all frames in DPB as unused for reference.
  774. */
  775. void ff_hevc_clear_refs(HEVCContext *s);
  776. /**
  777. * Drop all frames currently in DPB.
  778. */
  779. void ff_hevc_flush_dpb(HEVCContext *s);
  780. /**
  781. * Compute POC of the current frame and return it.
  782. */
  783. int ff_hevc_compute_poc(HEVCContext *s, int poc_lsb);
  784. RefPicList *ff_hevc_get_ref_list(HEVCContext *s, HEVCFrame *frame,
  785. int x0, int y0);
  786. /**
  787. * Construct the reference picture sets for the current frame.
  788. */
  789. int ff_hevc_frame_rps(HEVCContext *s);
  790. /**
  791. * Construct the reference picture list(s) for the current slice.
  792. */
  793. int ff_hevc_slice_rpl(HEVCContext *s);
  794. void ff_hevc_save_states(HEVCContext *s, int ctb_addr_ts);
  795. void ff_hevc_cabac_init(HEVCContext *s, int ctb_addr_ts);
  796. int ff_hevc_sao_merge_flag_decode(HEVCContext *s);
  797. int ff_hevc_sao_type_idx_decode(HEVCContext *s);
  798. int ff_hevc_sao_band_position_decode(HEVCContext *s);
  799. int ff_hevc_sao_offset_abs_decode(HEVCContext *s);
  800. int ff_hevc_sao_offset_sign_decode(HEVCContext *s);
  801. int ff_hevc_sao_eo_class_decode(HEVCContext *s);
  802. int ff_hevc_end_of_slice_flag_decode(HEVCContext *s);
  803. int ff_hevc_cu_transquant_bypass_flag_decode(HEVCContext *s);
  804. int ff_hevc_skip_flag_decode(HEVCContext *s, int x0, int y0,
  805. int x_cb, int y_cb);
  806. int ff_hevc_pred_mode_decode(HEVCContext *s);
  807. int ff_hevc_split_coding_unit_flag_decode(HEVCContext *s, int ct_depth,
  808. int x0, int y0);
  809. int ff_hevc_part_mode_decode(HEVCContext *s, int log2_cb_size);
  810. int ff_hevc_pcm_flag_decode(HEVCContext *s);
  811. int ff_hevc_prev_intra_luma_pred_flag_decode(HEVCContext *s);
  812. int ff_hevc_mpm_idx_decode(HEVCContext *s);
  813. int ff_hevc_rem_intra_luma_pred_mode_decode(HEVCContext *s);
  814. int ff_hevc_intra_chroma_pred_mode_decode(HEVCContext *s);
  815. int ff_hevc_merge_idx_decode(HEVCContext *s);
  816. int ff_hevc_merge_flag_decode(HEVCContext *s);
  817. int ff_hevc_inter_pred_idc_decode(HEVCContext *s, int nPbW, int nPbH);
  818. int ff_hevc_ref_idx_lx_decode(HEVCContext *s, int num_ref_idx_lx);
  819. int ff_hevc_mvp_lx_flag_decode(HEVCContext *s);
  820. int ff_hevc_no_residual_syntax_flag_decode(HEVCContext *s);
  821. int ff_hevc_abs_mvd_greater0_flag_decode(HEVCContext *s);
  822. int ff_hevc_abs_mvd_greater1_flag_decode(HEVCContext *s);
  823. int ff_hevc_mvd_decode(HEVCContext *s);
  824. int ff_hevc_mvd_sign_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_transform_skip_flag_decode(HEVCContext *s, int c_idx);
  829. int ff_hevc_last_significant_coeff_x_prefix_decode(HEVCContext *s, int c_idx,
  830. int log2_size);
  831. int ff_hevc_last_significant_coeff_y_prefix_decode(HEVCContext *s, int c_idx,
  832. int log2_size);
  833. int ff_hevc_last_significant_coeff_suffix_decode(HEVCContext *s,
  834. int last_significant_coeff_prefix);
  835. int ff_hevc_significant_coeff_group_flag_decode(HEVCContext *s, int c_idx,
  836. int ctx_cg);
  837. int ff_hevc_significant_coeff_flag_decode(HEVCContext *s, int c_idx, int x_c,
  838. int y_c, int log2_trafo_size,
  839. int scan_idx, int prev_sig);
  840. int ff_hevc_coeff_abs_level_greater1_flag_decode(HEVCContext *s, int c_idx,
  841. int ctx_set);
  842. int ff_hevc_coeff_abs_level_greater2_flag_decode(HEVCContext *s, int c_idx,
  843. int inc);
  844. int ff_hevc_coeff_abs_level_remaining(HEVCContext *s, int base_level,
  845. int rc_rice_param);
  846. int ff_hevc_coeff_sign_flag(HEVCContext *s, uint8_t nb);
  847. /**
  848. * Get the number of candidate references for the current frame.
  849. */
  850. int ff_hevc_frame_nb_refs(HEVCContext *s);
  851. int ff_hevc_set_new_ref(HEVCContext *s, AVFrame **frame, int poc);
  852. /**
  853. * Find next frame in output order and put a reference to it in frame.
  854. * @return 1 if a frame was output, 0 otherwise
  855. */
  856. int ff_hevc_output_frame(HEVCContext *s, AVFrame *frame, int flush);
  857. void ff_hevc_unref_frame(HEVCContext *s, HEVCFrame *frame, int flags);
  858. void ff_hevc_set_neighbour_available(HEVCContext *s, int x0, int y0,
  859. int nPbW, int nPbH);
  860. void ff_hevc_luma_mv_merge_mode(HEVCContext *s, int x0, int y0,
  861. int nPbW, int nPbH, int log2_cb_size,
  862. int part_idx, int merge_idx, MvField *mv);
  863. void ff_hevc_luma_mv_mvp_mode(HEVCContext *s, int x0, int y0,
  864. int nPbW, int nPbH, int log2_cb_size,
  865. int part_idx, int merge_idx,
  866. MvField *mv, int mvp_lx_flag, int LX);
  867. void ff_hevc_set_qPy(HEVCContext *s, int xC, int yC, int xBase, int yBase,
  868. int log2_cb_size);
  869. void ff_hevc_deblocking_boundary_strengths(HEVCContext *s, int x0, int y0,
  870. int log2_trafo_size,
  871. int slice_or_tiles_up_boundary,
  872. int slice_or_tiles_left_boundary);
  873. int ff_hevc_cu_qp_delta_sign_flag(HEVCContext *s);
  874. int ff_hevc_cu_qp_delta_abs(HEVCContext *s);
  875. void ff_hevc_hls_filter(HEVCContext *s, int x, int y);
  876. void ff_hevc_hls_filters(HEVCContext *s, int x_ctb, int y_ctb, int ctb_size);
  877. void ff_hevc_pps_free(HEVCPPS **ppps);
  878. void ff_hevc_pred_init(HEVCPredContext *hpc, int bit_depth);
  879. void ff_hevc_dsp_init(HEVCDSPContext *hpc, int bit_depth);
  880. extern const int8_t ff_hevc_epel_filters[7][16];
  881. extern const uint8_t ff_hevc_qpel_extra_before[4];
  882. extern const uint8_t ff_hevc_qpel_extra_after[4];
  883. extern const uint8_t ff_hevc_qpel_extra[4];
  884. extern const uint8_t ff_hevc_diag_scan4x4_x[16];
  885. extern const uint8_t ff_hevc_diag_scan4x4_y[16];
  886. extern const uint8_t ff_hevc_diag_scan8x8_x[64];
  887. extern const uint8_t ff_hevc_diag_scan8x8_y[64];
  888. #endif /* AVCODEC_HEVC_H */