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.

978 lines
26KB

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