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.

1023 lines
27KB

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