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.

975 lines
28KB

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