4 * Copyright (C) 2012 - 2013 Guillaume Martres
6 * This file is part of FFmpeg.
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.
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.
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
23 #ifndef AVCODEC_HEVCDEC_H
24 #define AVCODEC_HEVCDEC_H
26 #include <stdatomic.h>
28 #include "libavutil/buffer.h"
35 #include "h2645_parse.h"
44 #define MAX_NB_THREADS 16
45 #define SHIFT_CTB_WPP 2
47 //TODO: check if this is really the maximum
48 #define MAX_TRANSFORM_DEPTH 5
50 #define MAX_TB_SIZE 32
52 #define DEFAULT_INTRA_TC_OFFSET 2
54 #define HEVC_CONTEXTS 199
56 #define MRG_MAX_NUM_CANDS 5
61 #define EPEL_EXTRA_BEFORE 1
62 #define EPEL_EXTRA_AFTER 2
64 #define QPEL_EXTRA_BEFORE 3
65 #define QPEL_EXTRA_AFTER 4
68 #define EDGE_EMU_BUFFER_STRIDE 80
71 * Value of the luma sample at position (x, y) in the 2D array tab.
73 #define SAMPLE(tab, x, y) ((tab)[(y) * s->sps->width + (x)])
74 #define SAMPLE_CTB(tab, x, y) ((tab)[(y) * min_cb_width + (x)])
76 #define IS_IDR(s) ((s)->nal_unit_type == HEVC_NAL_IDR_W_RADL || (s)->nal_unit_type == HEVC_NAL_IDR_N_LP)
77 #define IS_BLA(s) ((s)->nal_unit_type == HEVC_NAL_BLA_W_RADL || (s)->nal_unit_type == HEVC_NAL_BLA_W_LP || \
78 (s)->nal_unit_type == HEVC_NAL_BLA_N_LP)
79 #define IS_IRAP(s) ((s)->nal_unit_type >= 16 && (s)->nal_unit_type <= 23)
98 SPLIT_CODING_UNIT_FLAG,
99 CU_TRANSQUANT_BYPASS_FLAG,
105 PREV_INTRA_LUMA_PRED_FLAG,
107 REM_INTRA_LUMA_PRED_MODE,
108 INTRA_CHROMA_PRED_MODE,
114 ABS_MVD_GREATER0_FLAG,
115 ABS_MVD_GREATER1_FLAG,
119 NO_RESIDUAL_DATA_FLAG,
120 SPLIT_TRANSFORM_FLAG,
125 EXPLICIT_RDPCM_DIR_FLAG,
126 LAST_SIGNIFICANT_COEFF_X_PREFIX,
127 LAST_SIGNIFICANT_COEFF_Y_PREFIX,
128 LAST_SIGNIFICANT_COEFF_X_SUFFIX,
129 LAST_SIGNIFICANT_COEFF_Y_SUFFIX,
130 SIGNIFICANT_COEFF_GROUP_FLAG,
131 SIGNIFICANT_COEFF_FLAG,
132 COEFF_ABS_LEVEL_GREATER1_FLAG,
133 COEFF_ABS_LEVEL_GREATER2_FLAG,
134 COEFF_ABS_LEVEL_REMAINING,
138 CU_CHROMA_QP_OFFSET_FLAG,
139 CU_CHROMA_QP_OFFSET_IDX,
230 typedef struct RefPicList {
231 struct HEVCFrame *ref[HEVC_MAX_REFS];
232 int list[HEVC_MAX_REFS];
233 int isLongTerm[HEVC_MAX_REFS];
237 typedef struct RefPicListTab {
238 RefPicList refPicList[2];
241 typedef struct CodingUnit {
245 enum PredMode pred_mode; ///< PredMode
246 enum PartMode part_mode; ///< PartMode
248 // Inferred parameters
249 uint8_t intra_split_flag; ///< IntraSplitFlag
250 uint8_t max_trafo_depth; ///< MaxTrafoDepth
251 uint8_t cu_transquant_bypass_flag;
255 int16_t x; ///< horizontal component of motion vector
256 int16_t y; ///< vertical component of motion vector
259 typedef struct MvField {
260 DECLARE_ALIGNED(4, Mv, mv)[2];
265 typedef struct NeighbourAvailable {
266 int cand_bottom_left;
271 int cand_up_right_sap;
272 } NeighbourAvailable;
274 typedef struct PredictionUnit {
276 int rem_intra_luma_pred_mode;
277 uint8_t intra_pred_mode[4];
280 uint8_t intra_pred_mode_c[4];
281 uint8_t chroma_mode_c[4];
284 typedef struct TransformUnit {
289 // Inferred parameters;
291 int intra_pred_mode_c;
293 uint8_t is_cu_qp_delta_coded;
294 uint8_t is_cu_chroma_qp_offset_coded;
295 int8_t cu_qp_offset_cb;
296 int8_t cu_qp_offset_cr;
300 typedef struct DBParams {
305 #define HEVC_FRAME_FLAG_OUTPUT (1 << 0)
306 #define HEVC_FRAME_FLAG_SHORT_REF (1 << 1)
307 #define HEVC_FRAME_FLAG_LONG_REF (1 << 2)
308 #define HEVC_FRAME_FLAG_BUMPING (1 << 3)
310 typedef struct HEVCFrame {
314 RefPicList *refPicList;
315 RefPicListTab **rpl_tab;
318 struct HEVCFrame *collocated_ref;
320 AVBufferRef *tab_mvf_buf;
321 AVBufferRef *rpl_tab_buf;
322 AVBufferRef *rpl_buf;
324 AVBufferRef *hwaccel_priv_buf;
325 void *hwaccel_picture_private;
328 * A sequence counter, so that old frames are output first
334 * A combination of HEVC_FRAME_FLAG_*
339 typedef struct HEVCLocalContext {
340 uint8_t cabac_state[HEVC_CONTEXTS];
342 uint8_t stat_coeff[4];
344 uint8_t first_qp_group;
356 uint8_t ctb_left_flag;
358 uint8_t ctb_up_right_flag;
359 uint8_t ctb_up_left_flag;
362 /* +7 is for subpixel interpolation, *2 for high bit depths */
363 DECLARE_ALIGNED(32, uint8_t, edge_emu_buffer)[(MAX_PB_SIZE + 7) * EDGE_EMU_BUFFER_STRIDE * 2];
364 /* The extended size between the new edge emu buffer is abused by SAO */
365 DECLARE_ALIGNED(32, uint8_t, edge_emu_buffer2)[(MAX_PB_SIZE + 7) * EDGE_EMU_BUFFER_STRIDE * 2];
366 DECLARE_ALIGNED(32, int16_t, tmp)[MAX_PB_SIZE * MAX_PB_SIZE];
371 NeighbourAvailable na;
373 #define BOUNDARY_LEFT_SLICE (1 << 0)
374 #define BOUNDARY_LEFT_TILE (1 << 1)
375 #define BOUNDARY_UPPER_SLICE (1 << 2)
376 #define BOUNDARY_UPPER_TILE (1 << 3)
377 /* properties of the boundary of the current CTB for the purposes
378 * of the deblocking filter */
382 typedef struct HEVCContext {
383 const AVClass *c; // needed by private avoptions
384 AVCodecContext *avctx;
386 struct HEVCContext *sList[MAX_NB_THREADS];
388 HEVCLocalContext *HEVClcList[MAX_NB_THREADS];
389 HEVCLocalContext *HEVClc;
391 uint8_t threads_type;
392 uint8_t threads_number;
397 uint8_t *cabac_state;
399 /** 1 if the independent slice segment header was successfully parsed */
400 uint8_t slice_initialized;
403 AVFrame *output_frame;
404 uint8_t *sao_pixel_buffer_h[3];
405 uint8_t *sao_pixel_buffer_v[3];
409 AVBufferPool *tab_mvf_pool;
410 AVBufferPool *rpl_tab_pool;
412 ///< candidate references for the current frame
418 enum HEVCNALUnitType nal_unit_type;
419 int temporal_id; ///< temporal_id_plus1 - 1
424 int slice_idx; ///< number of the slice being currently decoded
425 int eos; ///< current packet contains an EOS/EOB NAL
426 int last_eos; ///< last packet contains an EOS/EOB NAL
432 int no_rasl_output_flag;
435 HEVCDSPContext hevcdsp;
436 VideoDSPContext vdsp;
437 BswapDSPContext bdsp;
439 uint8_t *horizontal_bs;
440 uint8_t *vertical_bs;
442 int32_t *tab_slice_address;
446 uint8_t *tab_ct_depth;
450 uint8_t *cbf_luma; // cbf_luma of colocated TU
453 // CTB-level flags affecting loop filter operation
454 uint8_t *filter_slice_edges;
456 /** used on BE to byteswap the lines for checksumming */
457 uint8_t *checksum_buf;
458 int checksum_buf_size;
461 * Sequence counters for decoded and output frames, so that old
462 * frames are output first after a POC reset
467 int enable_parallel_tiles;
473 // type of the first VCL NAL of the current frame
474 enum HEVCNALUnitType first_nal_type;
476 uint8_t context_initialized;
477 int is_nalff; ///< this flag is != 0 if bitstream is encapsulated
478 ///< as a format defined in 14496-15
479 int apply_defdispwin;
481 int nal_length_size; ///< Number of bytes used for nal length (1, 2 or 4)
488 * Mark all frames in DPB as unused for reference.
490 void ff_hevc_clear_refs(HEVCContext *s);
493 * Drop all frames currently in DPB.
495 void ff_hevc_flush_dpb(HEVCContext *s);
497 RefPicList *ff_hevc_get_ref_list(HEVCContext *s, HEVCFrame *frame,
501 * Construct the reference picture sets for the current frame.
503 int ff_hevc_frame_rps(HEVCContext *s);
506 * Construct the reference picture list(s) for the current slice.
508 int ff_hevc_slice_rpl(HEVCContext *s);
510 void ff_hevc_save_states(HEVCContext *s, int ctb_addr_ts);
511 int ff_hevc_cabac_init(HEVCContext *s, int ctb_addr_ts);
512 int ff_hevc_sao_merge_flag_decode(HEVCContext *s);
513 int ff_hevc_sao_type_idx_decode(HEVCContext *s);
514 int ff_hevc_sao_band_position_decode(HEVCContext *s);
515 int ff_hevc_sao_offset_abs_decode(HEVCContext *s);
516 int ff_hevc_sao_offset_sign_decode(HEVCContext *s);
517 int ff_hevc_sao_eo_class_decode(HEVCContext *s);
518 int ff_hevc_end_of_slice_flag_decode(HEVCContext *s);
519 int ff_hevc_cu_transquant_bypass_flag_decode(HEVCContext *s);
520 int ff_hevc_skip_flag_decode(HEVCContext *s, int x0, int y0,
522 int ff_hevc_pred_mode_decode(HEVCContext *s);
523 int ff_hevc_split_coding_unit_flag_decode(HEVCContext *s, int ct_depth,
525 int ff_hevc_part_mode_decode(HEVCContext *s, int log2_cb_size);
526 int ff_hevc_pcm_flag_decode(HEVCContext *s);
527 int ff_hevc_prev_intra_luma_pred_flag_decode(HEVCContext *s);
528 int ff_hevc_mpm_idx_decode(HEVCContext *s);
529 int ff_hevc_rem_intra_luma_pred_mode_decode(HEVCContext *s);
530 int ff_hevc_intra_chroma_pred_mode_decode(HEVCContext *s);
531 int ff_hevc_merge_idx_decode(HEVCContext *s);
532 int ff_hevc_merge_flag_decode(HEVCContext *s);
533 int ff_hevc_inter_pred_idc_decode(HEVCContext *s, int nPbW, int nPbH);
534 int ff_hevc_ref_idx_lx_decode(HEVCContext *s, int num_ref_idx_lx);
535 int ff_hevc_mvp_lx_flag_decode(HEVCContext *s);
536 int ff_hevc_no_residual_syntax_flag_decode(HEVCContext *s);
537 int ff_hevc_split_transform_flag_decode(HEVCContext *s, int log2_trafo_size);
538 int ff_hevc_cbf_cb_cr_decode(HEVCContext *s, int trafo_depth);
539 int ff_hevc_cbf_luma_decode(HEVCContext *s, int trafo_depth);
540 int ff_hevc_log2_res_scale_abs(HEVCContext *s, int idx);
541 int ff_hevc_res_scale_sign_flag(HEVCContext *s, int idx);
544 * Get the number of candidate references for the current frame.
546 int ff_hevc_frame_nb_refs(HEVCContext *s);
548 int ff_hevc_set_new_ref(HEVCContext *s, AVFrame **frame, int poc);
551 * Find next frame in output order and put a reference to it in frame.
552 * @return 1 if a frame was output, 0 otherwise
554 int ff_hevc_output_frame(HEVCContext *s, AVFrame *frame, int flush);
556 void ff_hevc_bump_frame(HEVCContext *s);
558 void ff_hevc_unref_frame(HEVCContext *s, HEVCFrame *frame, int flags);
560 void ff_hevc_set_neighbour_available(HEVCContext *s, int x0, int y0,
562 void ff_hevc_luma_mv_merge_mode(HEVCContext *s, int x0, int y0,
563 int nPbW, int nPbH, int log2_cb_size,
564 int part_idx, int merge_idx, MvField *mv);
565 void ff_hevc_luma_mv_mvp_mode(HEVCContext *s, int x0, int y0,
566 int nPbW, int nPbH, int log2_cb_size,
567 int part_idx, int merge_idx,
568 MvField *mv, int mvp_lx_flag, int LX);
569 void ff_hevc_set_qPy(HEVCContext *s, int xBase, int yBase,
571 void ff_hevc_deblocking_boundary_strengths(HEVCContext *s, int x0, int y0,
572 int log2_trafo_size);
573 int ff_hevc_cu_qp_delta_sign_flag(HEVCContext *s);
574 int ff_hevc_cu_qp_delta_abs(HEVCContext *s);
575 int ff_hevc_cu_chroma_qp_offset_flag(HEVCContext *s);
576 int ff_hevc_cu_chroma_qp_offset_idx(HEVCContext *s);
577 void ff_hevc_hls_filter(HEVCContext *s, int x, int y, int ctb_size);
578 void ff_hevc_hls_filters(HEVCContext *s, int x_ctb, int y_ctb, int ctb_size);
579 void ff_hevc_hls_residual_coding(HEVCContext *s, int x0, int y0,
580 int log2_trafo_size, enum ScanType scan_idx,
583 void ff_hevc_hls_mvd_coding(HEVCContext *s, int x0, int y0, int log2_cb_size);
585 extern const uint8_t ff_hevc_qpel_extra_before[4];
586 extern const uint8_t ff_hevc_qpel_extra_after[4];
587 extern const uint8_t ff_hevc_qpel_extra[4];
589 #endif /* AVCODEC_HEVCDEC_H */