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.

589 lines
15KB

  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_HEVCDEC_H
  23. #define AVCODEC_HEVCDEC_H
  24. #include <stdatomic.h>
  25. #include "libavutil/buffer.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 "hevc.h"
  33. #include "hevc_ps.h"
  34. #include "hevc_sei.h"
  35. #include "hevcdsp.h"
  36. #include "internal.h"
  37. #include "thread.h"
  38. #include "videodsp.h"
  39. #define MAX_NB_THREADS 16
  40. #define SHIFT_CTB_WPP 2
  41. //TODO: check if this is really the maximum
  42. #define MAX_TRANSFORM_DEPTH 5
  43. #define MAX_TB_SIZE 32
  44. #define MAX_QP 51
  45. #define DEFAULT_INTRA_TC_OFFSET 2
  46. #define HEVC_CONTEXTS 199
  47. #define MRG_MAX_NUM_CANDS 5
  48. #define L0 0
  49. #define L1 1
  50. #define EPEL_EXTRA_BEFORE 1
  51. #define EPEL_EXTRA_AFTER 2
  52. #define EPEL_EXTRA 3
  53. #define QPEL_EXTRA_BEFORE 3
  54. #define QPEL_EXTRA_AFTER 4
  55. #define QPEL_EXTRA 7
  56. #define EDGE_EMU_BUFFER_STRIDE 80
  57. /**
  58. * Value of the luma sample at position (x, y) in the 2D array tab.
  59. */
  60. #define SAMPLE(tab, x, y) ((tab)[(y) * s->sps->width + (x)])
  61. #define SAMPLE_CTB(tab, x, y) ((tab)[(y) * min_cb_width + (x)])
  62. #define IS_IDR(s) ((s)->nal_unit_type == HEVC_NAL_IDR_W_RADL || (s)->nal_unit_type == HEVC_NAL_IDR_N_LP)
  63. #define IS_BLA(s) ((s)->nal_unit_type == HEVC_NAL_BLA_W_RADL || (s)->nal_unit_type == HEVC_NAL_BLA_W_LP || \
  64. (s)->nal_unit_type == HEVC_NAL_BLA_N_LP)
  65. #define IS_IRAP(s) ((s)->nal_unit_type >= 16 && (s)->nal_unit_type <= 23)
  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 SyntaxElement {
  75. SAO_MERGE_FLAG = 0,
  76. SAO_TYPE_IDX,
  77. SAO_EO_CLASS,
  78. SAO_BAND_POSITION,
  79. SAO_OFFSET_ABS,
  80. SAO_OFFSET_SIGN,
  81. END_OF_SLICE_FLAG,
  82. SPLIT_CODING_UNIT_FLAG,
  83. CU_TRANSQUANT_BYPASS_FLAG,
  84. SKIP_FLAG,
  85. CU_QP_DELTA,
  86. PRED_MODE_FLAG,
  87. PART_MODE,
  88. PCM_FLAG,
  89. PREV_INTRA_LUMA_PRED_FLAG,
  90. MPM_IDX,
  91. REM_INTRA_LUMA_PRED_MODE,
  92. INTRA_CHROMA_PRED_MODE,
  93. MERGE_FLAG,
  94. MERGE_IDX,
  95. INTER_PRED_IDC,
  96. REF_IDX_L0,
  97. REF_IDX_L1,
  98. ABS_MVD_GREATER0_FLAG,
  99. ABS_MVD_GREATER1_FLAG,
  100. ABS_MVD_MINUS2,
  101. MVD_SIGN_FLAG,
  102. MVP_LX_FLAG,
  103. NO_RESIDUAL_DATA_FLAG,
  104. SPLIT_TRANSFORM_FLAG,
  105. CBF_LUMA,
  106. CBF_CB_CR,
  107. TRANSFORM_SKIP_FLAG,
  108. EXPLICIT_RDPCM_FLAG,
  109. EXPLICIT_RDPCM_DIR_FLAG,
  110. LAST_SIGNIFICANT_COEFF_X_PREFIX,
  111. LAST_SIGNIFICANT_COEFF_Y_PREFIX,
  112. LAST_SIGNIFICANT_COEFF_X_SUFFIX,
  113. LAST_SIGNIFICANT_COEFF_Y_SUFFIX,
  114. SIGNIFICANT_COEFF_GROUP_FLAG,
  115. SIGNIFICANT_COEFF_FLAG,
  116. COEFF_ABS_LEVEL_GREATER1_FLAG,
  117. COEFF_ABS_LEVEL_GREATER2_FLAG,
  118. COEFF_ABS_LEVEL_REMAINING,
  119. COEFF_SIGN_FLAG,
  120. LOG2_RES_SCALE_ABS,
  121. RES_SCALE_SIGN_FLAG,
  122. CU_CHROMA_QP_OFFSET_FLAG,
  123. CU_CHROMA_QP_OFFSET_IDX,
  124. };
  125. enum PartMode {
  126. PART_2Nx2N = 0,
  127. PART_2NxN = 1,
  128. PART_Nx2N = 2,
  129. PART_NxN = 3,
  130. PART_2NxnU = 4,
  131. PART_2NxnD = 5,
  132. PART_nLx2N = 6,
  133. PART_nRx2N = 7,
  134. };
  135. enum PredMode {
  136. MODE_INTER = 0,
  137. MODE_INTRA,
  138. MODE_SKIP,
  139. };
  140. enum InterPredIdc {
  141. PRED_L0 = 0,
  142. PRED_L1,
  143. PRED_BI,
  144. };
  145. enum PredFlag {
  146. PF_INTRA = 0,
  147. PF_L0,
  148. PF_L1,
  149. PF_BI,
  150. };
  151. enum IntraPredMode {
  152. INTRA_PLANAR = 0,
  153. INTRA_DC,
  154. INTRA_ANGULAR_2,
  155. INTRA_ANGULAR_3,
  156. INTRA_ANGULAR_4,
  157. INTRA_ANGULAR_5,
  158. INTRA_ANGULAR_6,
  159. INTRA_ANGULAR_7,
  160. INTRA_ANGULAR_8,
  161. INTRA_ANGULAR_9,
  162. INTRA_ANGULAR_10,
  163. INTRA_ANGULAR_11,
  164. INTRA_ANGULAR_12,
  165. INTRA_ANGULAR_13,
  166. INTRA_ANGULAR_14,
  167. INTRA_ANGULAR_15,
  168. INTRA_ANGULAR_16,
  169. INTRA_ANGULAR_17,
  170. INTRA_ANGULAR_18,
  171. INTRA_ANGULAR_19,
  172. INTRA_ANGULAR_20,
  173. INTRA_ANGULAR_21,
  174. INTRA_ANGULAR_22,
  175. INTRA_ANGULAR_23,
  176. INTRA_ANGULAR_24,
  177. INTRA_ANGULAR_25,
  178. INTRA_ANGULAR_26,
  179. INTRA_ANGULAR_27,
  180. INTRA_ANGULAR_28,
  181. INTRA_ANGULAR_29,
  182. INTRA_ANGULAR_30,
  183. INTRA_ANGULAR_31,
  184. INTRA_ANGULAR_32,
  185. INTRA_ANGULAR_33,
  186. INTRA_ANGULAR_34,
  187. };
  188. enum SAOType {
  189. SAO_NOT_APPLIED = 0,
  190. SAO_BAND,
  191. SAO_EDGE,
  192. SAO_APPLIED
  193. };
  194. enum SAOEOClass {
  195. SAO_EO_HORIZ = 0,
  196. SAO_EO_VERT,
  197. SAO_EO_135D,
  198. SAO_EO_45D,
  199. };
  200. enum ScanType {
  201. SCAN_DIAG = 0,
  202. SCAN_HORIZ,
  203. SCAN_VERT,
  204. };
  205. typedef struct RefPicList {
  206. struct HEVCFrame *ref[HEVC_MAX_REFS];
  207. int list[HEVC_MAX_REFS];
  208. int isLongTerm[HEVC_MAX_REFS];
  209. int nb_refs;
  210. } RefPicList;
  211. typedef struct RefPicListTab {
  212. RefPicList refPicList[2];
  213. } RefPicListTab;
  214. typedef struct CodingUnit {
  215. int x;
  216. int y;
  217. enum PredMode pred_mode; ///< PredMode
  218. enum PartMode part_mode; ///< PartMode
  219. // Inferred parameters
  220. uint8_t intra_split_flag; ///< IntraSplitFlag
  221. uint8_t max_trafo_depth; ///< MaxTrafoDepth
  222. uint8_t cu_transquant_bypass_flag;
  223. } CodingUnit;
  224. typedef struct Mv {
  225. int16_t x; ///< horizontal component of motion vector
  226. int16_t y; ///< vertical component of motion vector
  227. } Mv;
  228. typedef struct MvField {
  229. DECLARE_ALIGNED(4, Mv, mv)[2];
  230. int8_t ref_idx[2];
  231. int8_t pred_flag;
  232. } MvField;
  233. typedef struct NeighbourAvailable {
  234. int cand_bottom_left;
  235. int cand_left;
  236. int cand_up;
  237. int cand_up_left;
  238. int cand_up_right;
  239. int cand_up_right_sap;
  240. } NeighbourAvailable;
  241. typedef struct PredictionUnit {
  242. int mpm_idx;
  243. int rem_intra_luma_pred_mode;
  244. uint8_t intra_pred_mode[4];
  245. Mv mvd;
  246. uint8_t merge_flag;
  247. uint8_t intra_pred_mode_c[4];
  248. uint8_t chroma_mode_c[4];
  249. } PredictionUnit;
  250. typedef struct TransformUnit {
  251. int cu_qp_delta;
  252. int res_scale_val;
  253. // Inferred parameters;
  254. int intra_pred_mode;
  255. int intra_pred_mode_c;
  256. int chroma_mode_c;
  257. uint8_t is_cu_qp_delta_coded;
  258. uint8_t is_cu_chroma_qp_offset_coded;
  259. int8_t cu_qp_offset_cb;
  260. int8_t cu_qp_offset_cr;
  261. uint8_t cross_pf;
  262. } TransformUnit;
  263. typedef struct DBParams {
  264. int beta_offset;
  265. int tc_offset;
  266. } DBParams;
  267. #define HEVC_FRAME_FLAG_OUTPUT (1 << 0)
  268. #define HEVC_FRAME_FLAG_SHORT_REF (1 << 1)
  269. #define HEVC_FRAME_FLAG_LONG_REF (1 << 2)
  270. #define HEVC_FRAME_FLAG_BUMPING (1 << 3)
  271. typedef struct HEVCFrame {
  272. AVFrame *frame;
  273. ThreadFrame tf;
  274. MvField *tab_mvf;
  275. RefPicList *refPicList;
  276. RefPicListTab **rpl_tab;
  277. int ctb_count;
  278. int poc;
  279. struct HEVCFrame *collocated_ref;
  280. AVBufferRef *tab_mvf_buf;
  281. AVBufferRef *rpl_tab_buf;
  282. AVBufferRef *rpl_buf;
  283. AVBufferRef *hwaccel_priv_buf;
  284. void *hwaccel_picture_private;
  285. /**
  286. * A sequence counter, so that old frames are output first
  287. * after a POC reset
  288. */
  289. uint16_t sequence;
  290. /**
  291. * A combination of HEVC_FRAME_FLAG_*
  292. */
  293. uint8_t flags;
  294. } HEVCFrame;
  295. typedef struct HEVCLocalContext {
  296. uint8_t cabac_state[HEVC_CONTEXTS];
  297. uint8_t stat_coeff[4];
  298. uint8_t first_qp_group;
  299. GetBitContext gb;
  300. CABACContext cc;
  301. int8_t qp_y;
  302. int8_t curr_qp_y;
  303. int qPy_pred;
  304. TransformUnit tu;
  305. uint8_t ctb_left_flag;
  306. uint8_t ctb_up_flag;
  307. uint8_t ctb_up_right_flag;
  308. uint8_t ctb_up_left_flag;
  309. int end_of_tiles_x;
  310. int end_of_tiles_y;
  311. /* +7 is for subpixel interpolation, *2 for high bit depths */
  312. DECLARE_ALIGNED(32, uint8_t, edge_emu_buffer)[(MAX_PB_SIZE + 7) * EDGE_EMU_BUFFER_STRIDE * 2];
  313. /* The extended size between the new edge emu buffer is abused by SAO */
  314. DECLARE_ALIGNED(32, uint8_t, edge_emu_buffer2)[(MAX_PB_SIZE + 7) * EDGE_EMU_BUFFER_STRIDE * 2];
  315. DECLARE_ALIGNED(32, int16_t, tmp)[MAX_PB_SIZE * MAX_PB_SIZE];
  316. int ct_depth;
  317. CodingUnit cu;
  318. PredictionUnit pu;
  319. NeighbourAvailable na;
  320. #define BOUNDARY_LEFT_SLICE (1 << 0)
  321. #define BOUNDARY_LEFT_TILE (1 << 1)
  322. #define BOUNDARY_UPPER_SLICE (1 << 2)
  323. #define BOUNDARY_UPPER_TILE (1 << 3)
  324. /* properties of the boundary of the current CTB for the purposes
  325. * of the deblocking filter */
  326. int boundary_flags;
  327. } HEVCLocalContext;
  328. typedef struct HEVCContext {
  329. const AVClass *c; // needed by private avoptions
  330. AVCodecContext *avctx;
  331. struct HEVCContext *sList[MAX_NB_THREADS];
  332. HEVCLocalContext *HEVClcList[MAX_NB_THREADS];
  333. HEVCLocalContext *HEVClc;
  334. uint8_t threads_type;
  335. uint8_t threads_number;
  336. int width;
  337. int height;
  338. uint8_t *cabac_state;
  339. /** 1 if the independent slice segment header was successfully parsed */
  340. uint8_t slice_initialized;
  341. AVFrame *frame;
  342. AVFrame *output_frame;
  343. uint8_t *sao_pixel_buffer_h[3];
  344. uint8_t *sao_pixel_buffer_v[3];
  345. HEVCParamSets ps;
  346. HEVCSEI sei;
  347. AVBufferPool *tab_mvf_pool;
  348. AVBufferPool *rpl_tab_pool;
  349. ///< candidate references for the current frame
  350. RefPicList rps[5];
  351. SliceHeader sh;
  352. SAOParams *sao;
  353. DBParams *deblock;
  354. enum HEVCNALUnitType nal_unit_type;
  355. int temporal_id; ///< temporal_id_plus1 - 1
  356. HEVCFrame *ref;
  357. HEVCFrame DPB[32];
  358. int poc;
  359. int pocTid0;
  360. int slice_idx; ///< number of the slice being currently decoded
  361. int eos; ///< current packet contains an EOS/EOB NAL
  362. int last_eos; ///< last packet contains an EOS/EOB NAL
  363. int max_ra;
  364. int bs_width;
  365. int bs_height;
  366. int is_decoded;
  367. int no_rasl_output_flag;
  368. HEVCPredContext hpc;
  369. HEVCDSPContext hevcdsp;
  370. VideoDSPContext vdsp;
  371. BswapDSPContext bdsp;
  372. int8_t *qp_y_tab;
  373. uint8_t *horizontal_bs;
  374. uint8_t *vertical_bs;
  375. int32_t *tab_slice_address;
  376. // CU
  377. uint8_t *skip_flag;
  378. uint8_t *tab_ct_depth;
  379. // PU
  380. uint8_t *tab_ipm;
  381. uint8_t *cbf_luma; // cbf_luma of colocated TU
  382. uint8_t *is_pcm;
  383. // CTB-level flags affecting loop filter operation
  384. uint8_t *filter_slice_edges;
  385. /** used on BE to byteswap the lines for checksumming */
  386. uint8_t *checksum_buf;
  387. int checksum_buf_size;
  388. /**
  389. * Sequence counters for decoded and output frames, so that old
  390. * frames are output first after a POC reset
  391. */
  392. uint16_t seq_decode;
  393. uint16_t seq_output;
  394. int enable_parallel_tiles;
  395. atomic_int wpp_err;
  396. const uint8_t *data;
  397. H2645Packet pkt;
  398. // type of the first VCL NAL of the current frame
  399. enum HEVCNALUnitType first_nal_type;
  400. uint8_t context_initialized;
  401. int is_nalff; ///< this flag is != 0 if bitstream is encapsulated
  402. ///< as a format defined in 14496-15
  403. int apply_defdispwin;
  404. int nal_length_size; ///< Number of bytes used for nal length (1, 2 or 4)
  405. int nuh_layer_id;
  406. } HEVCContext;
  407. /**
  408. * Mark all frames in DPB as unused for reference.
  409. */
  410. void ff_hevc_clear_refs(HEVCContext *s);
  411. /**
  412. * Drop all frames currently in DPB.
  413. */
  414. void ff_hevc_flush_dpb(HEVCContext *s);
  415. RefPicList *ff_hevc_get_ref_list(HEVCContext *s, HEVCFrame *frame,
  416. int x0, int y0);
  417. /**
  418. * Construct the reference picture sets for the current frame.
  419. */
  420. int ff_hevc_frame_rps(HEVCContext *s);
  421. /**
  422. * Construct the reference picture list(s) for the current slice.
  423. */
  424. int ff_hevc_slice_rpl(HEVCContext *s);
  425. void ff_hevc_save_states(HEVCContext *s, int ctb_addr_ts);
  426. int ff_hevc_cabac_init(HEVCContext *s, int ctb_addr_ts);
  427. int ff_hevc_sao_merge_flag_decode(HEVCContext *s);
  428. int ff_hevc_sao_type_idx_decode(HEVCContext *s);
  429. int ff_hevc_sao_band_position_decode(HEVCContext *s);
  430. int ff_hevc_sao_offset_abs_decode(HEVCContext *s);
  431. int ff_hevc_sao_offset_sign_decode(HEVCContext *s);
  432. int ff_hevc_sao_eo_class_decode(HEVCContext *s);
  433. int ff_hevc_end_of_slice_flag_decode(HEVCContext *s);
  434. int ff_hevc_cu_transquant_bypass_flag_decode(HEVCContext *s);
  435. int ff_hevc_skip_flag_decode(HEVCContext *s, int x0, int y0,
  436. int x_cb, int y_cb);
  437. int ff_hevc_pred_mode_decode(HEVCContext *s);
  438. int ff_hevc_split_coding_unit_flag_decode(HEVCContext *s, int ct_depth,
  439. int x0, int y0);
  440. int ff_hevc_part_mode_decode(HEVCContext *s, int log2_cb_size);
  441. int ff_hevc_pcm_flag_decode(HEVCContext *s);
  442. int ff_hevc_prev_intra_luma_pred_flag_decode(HEVCContext *s);
  443. int ff_hevc_mpm_idx_decode(HEVCContext *s);
  444. int ff_hevc_rem_intra_luma_pred_mode_decode(HEVCContext *s);
  445. int ff_hevc_intra_chroma_pred_mode_decode(HEVCContext *s);
  446. int ff_hevc_merge_idx_decode(HEVCContext *s);
  447. int ff_hevc_merge_flag_decode(HEVCContext *s);
  448. int ff_hevc_inter_pred_idc_decode(HEVCContext *s, int nPbW, int nPbH);
  449. int ff_hevc_ref_idx_lx_decode(HEVCContext *s, int num_ref_idx_lx);
  450. int ff_hevc_mvp_lx_flag_decode(HEVCContext *s);
  451. int ff_hevc_no_residual_syntax_flag_decode(HEVCContext *s);
  452. int ff_hevc_split_transform_flag_decode(HEVCContext *s, int log2_trafo_size);
  453. int ff_hevc_cbf_cb_cr_decode(HEVCContext *s, int trafo_depth);
  454. int ff_hevc_cbf_luma_decode(HEVCContext *s, int trafo_depth);
  455. int ff_hevc_log2_res_scale_abs(HEVCContext *s, int idx);
  456. int ff_hevc_res_scale_sign_flag(HEVCContext *s, int idx);
  457. /**
  458. * Get the number of candidate references for the current frame.
  459. */
  460. int ff_hevc_frame_nb_refs(HEVCContext *s);
  461. int ff_hevc_set_new_ref(HEVCContext *s, AVFrame **frame, int poc);
  462. /**
  463. * Find next frame in output order and put a reference to it in frame.
  464. * @return 1 if a frame was output, 0 otherwise
  465. */
  466. int ff_hevc_output_frame(HEVCContext *s, AVFrame *frame, int flush);
  467. void ff_hevc_bump_frame(HEVCContext *s);
  468. void ff_hevc_unref_frame(HEVCContext *s, HEVCFrame *frame, int flags);
  469. void ff_hevc_set_neighbour_available(HEVCContext *s, int x0, int y0,
  470. int nPbW, int nPbH);
  471. void ff_hevc_luma_mv_merge_mode(HEVCContext *s, int x0, int y0,
  472. int nPbW, int nPbH, int log2_cb_size,
  473. int part_idx, int merge_idx, MvField *mv);
  474. void ff_hevc_luma_mv_mvp_mode(HEVCContext *s, int x0, int y0,
  475. int nPbW, int nPbH, int log2_cb_size,
  476. int part_idx, int merge_idx,
  477. MvField *mv, int mvp_lx_flag, int LX);
  478. void ff_hevc_set_qPy(HEVCContext *s, int xBase, int yBase,
  479. int log2_cb_size);
  480. void ff_hevc_deblocking_boundary_strengths(HEVCContext *s, int x0, int y0,
  481. int log2_trafo_size);
  482. int ff_hevc_cu_qp_delta_sign_flag(HEVCContext *s);
  483. int ff_hevc_cu_qp_delta_abs(HEVCContext *s);
  484. int ff_hevc_cu_chroma_qp_offset_flag(HEVCContext *s);
  485. int ff_hevc_cu_chroma_qp_offset_idx(HEVCContext *s);
  486. void ff_hevc_hls_filter(HEVCContext *s, int x, int y, int ctb_size);
  487. void ff_hevc_hls_filters(HEVCContext *s, int x_ctb, int y_ctb, int ctb_size);
  488. void ff_hevc_hls_residual_coding(HEVCContext *s, int x0, int y0,
  489. int log2_trafo_size, enum ScanType scan_idx,
  490. int c_idx);
  491. void ff_hevc_hls_mvd_coding(HEVCContext *s, int x0, int y0, int log2_cb_size);
  492. extern const uint8_t ff_hevc_qpel_extra_before[4];
  493. extern const uint8_t ff_hevc_qpel_extra_after[4];
  494. extern const uint8_t ff_hevc_qpel_extra[4];
  495. #endif /* AVCODEC_HEVCDEC_H */