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.

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