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.

1076 lines
29KB

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