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.

1051 lines
29KB

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