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.

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