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.

1048 lines
28KB

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