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"
29 #include "libavutil/md5.h"
30 #include "libavutil/mem_internal.h"
37 #include "h2645_parse.h"
46 #define SHIFT_CTB_WPP 2
48 //TODO: check if this is really the maximum
49 #define MAX_TRANSFORM_DEPTH 5
51 #define MAX_TB_SIZE 32
53 #define DEFAULT_INTRA_TC_OFFSET 2
55 #define HEVC_CONTEXTS 199
56 #define HEVC_STAT_COEFFS 4
58 #define MRG_MAX_NUM_CANDS 5
63 #define EPEL_EXTRA_BEFORE 1
64 #define EPEL_EXTRA_AFTER 2
66 #define QPEL_EXTRA_BEFORE 3
67 #define QPEL_EXTRA_AFTER 4
70 #define EDGE_EMU_BUFFER_STRIDE 80
73 * Value of the luma sample at position (x, y) in the 2D array tab.
75 #define SAMPLE(tab, x, y) ((tab)[(y) * s->sps->width + (x)])
76 #define SAMPLE_CTB(tab, x, y) ((tab)[(y) * min_cb_width + (x)])
78 #define IS_IDR(s) ((s)->nal_unit_type == HEVC_NAL_IDR_W_RADL || (s)->nal_unit_type == HEVC_NAL_IDR_N_LP)
79 #define IS_BLA(s) ((s)->nal_unit_type == HEVC_NAL_BLA_W_RADL || (s)->nal_unit_type == HEVC_NAL_BLA_W_LP || \
80 (s)->nal_unit_type == HEVC_NAL_BLA_N_LP)
81 #define IS_IRAP(s) ((s)->nal_unit_type >= 16 && (s)->nal_unit_type <= 23)
100 SPLIT_CODING_UNIT_FLAG,
101 CU_TRANSQUANT_BYPASS_FLAG,
107 PREV_INTRA_LUMA_PRED_FLAG,
109 REM_INTRA_LUMA_PRED_MODE,
110 INTRA_CHROMA_PRED_MODE,
116 ABS_MVD_GREATER0_FLAG,
117 ABS_MVD_GREATER1_FLAG,
121 NO_RESIDUAL_DATA_FLAG,
122 SPLIT_TRANSFORM_FLAG,
127 EXPLICIT_RDPCM_DIR_FLAG,
128 LAST_SIGNIFICANT_COEFF_X_PREFIX,
129 LAST_SIGNIFICANT_COEFF_Y_PREFIX,
130 LAST_SIGNIFICANT_COEFF_X_SUFFIX,
131 LAST_SIGNIFICANT_COEFF_Y_SUFFIX,
132 SIGNIFICANT_COEFF_GROUP_FLAG,
133 SIGNIFICANT_COEFF_FLAG,
134 COEFF_ABS_LEVEL_GREATER1_FLAG,
135 COEFF_ABS_LEVEL_GREATER2_FLAG,
136 COEFF_ABS_LEVEL_REMAINING,
140 CU_CHROMA_QP_OFFSET_FLAG,
141 CU_CHROMA_QP_OFFSET_IDX,
232 typedef struct LongTermRPS {
234 uint8_t poc_msb_present[32];
239 typedef struct RefPicList {
240 struct HEVCFrame *ref[HEVC_MAX_REFS];
241 int list[HEVC_MAX_REFS];
242 int isLongTerm[HEVC_MAX_REFS];
246 typedef struct RefPicListTab {
247 RefPicList refPicList[2];
250 typedef struct SliceHeader {
253 ///< address (in raster order) of the first block in the current slice segment
254 unsigned int slice_segment_addr;
255 ///< address (in raster order) of the first block in the current slice
256 unsigned int slice_addr;
258 enum HEVCSliceType slice_type;
260 int pic_order_cnt_lsb;
262 uint8_t first_slice_in_pic_flag;
263 uint8_t dependent_slice_segment_flag;
264 uint8_t pic_output_flag;
265 uint8_t colour_plane_id;
267 ///< RPS coded in the slice header itself is stored here
268 int short_term_ref_pic_set_sps_flag;
269 int short_term_ref_pic_set_size;
270 ShortTermRPS slice_rps;
271 const ShortTermRPS *short_term_rps;
272 int long_term_ref_pic_set_size;
273 LongTermRPS long_term_rps;
274 unsigned int list_entry_lx[2][32];
276 uint8_t rpl_modification_flag[2];
277 uint8_t no_output_of_prior_pics_flag;
278 uint8_t slice_temporal_mvp_enabled_flag;
280 unsigned int nb_refs[2];
282 uint8_t slice_sample_adaptive_offset_flag[3];
283 uint8_t mvd_l1_zero_flag;
285 uint8_t cabac_init_flag;
286 uint8_t disable_deblocking_filter_flag; ///< slice_header_disable_deblocking_filter_flag
287 uint8_t slice_loop_filter_across_slices_enabled_flag;
288 uint8_t collocated_list;
290 unsigned int collocated_ref_idx;
293 int slice_cb_qp_offset;
294 int slice_cr_qp_offset;
296 uint8_t cu_chroma_qp_offset_enabled_flag;
298 int beta_offset; ///< beta_offset_div2 * 2
299 int tc_offset; ///< tc_offset_div2 * 2
301 unsigned int max_num_merge_cand; ///< 5 - 5_minus_max_num_merge_cand
303 unsigned *entry_point_offset;
306 int num_entry_point_offsets;
310 uint8_t luma_log2_weight_denom;
311 int16_t chroma_log2_weight_denom;
313 int16_t luma_weight_l0[16];
314 int16_t chroma_weight_l0[16][2];
315 int16_t chroma_weight_l1[16][2];
316 int16_t luma_weight_l1[16];
318 int16_t luma_offset_l0[16];
319 int16_t chroma_offset_l0[16][2];
321 int16_t luma_offset_l1[16];
322 int16_t chroma_offset_l1[16][2];
324 int slice_ctb_addr_rs;
327 typedef struct CodingUnit {
331 enum PredMode pred_mode; ///< PredMode
332 enum PartMode part_mode; ///< PartMode
334 // Inferred parameters
335 uint8_t intra_split_flag; ///< IntraSplitFlag
336 uint8_t max_trafo_depth; ///< MaxTrafoDepth
337 uint8_t cu_transquant_bypass_flag;
341 int16_t x; ///< horizontal component of motion vector
342 int16_t y; ///< vertical component of motion vector
345 typedef struct MvField {
346 DECLARE_ALIGNED(4, Mv, mv)[2];
351 typedef struct NeighbourAvailable {
352 int cand_bottom_left;
357 int cand_up_right_sap;
358 } NeighbourAvailable;
360 typedef struct PredictionUnit {
362 int rem_intra_luma_pred_mode;
363 uint8_t intra_pred_mode[4];
366 uint8_t intra_pred_mode_c[4];
367 uint8_t chroma_mode_c[4];
370 typedef struct TransformUnit {
375 // Inferred parameters;
377 int intra_pred_mode_c;
379 uint8_t is_cu_qp_delta_coded;
380 uint8_t is_cu_chroma_qp_offset_coded;
381 int8_t cu_qp_offset_cb;
382 int8_t cu_qp_offset_cr;
386 typedef struct DBParams {
391 #define HEVC_FRAME_FLAG_OUTPUT (1 << 0)
392 #define HEVC_FRAME_FLAG_SHORT_REF (1 << 1)
393 #define HEVC_FRAME_FLAG_LONG_REF (1 << 2)
394 #define HEVC_FRAME_FLAG_BUMPING (1 << 3)
396 typedef struct HEVCFrame {
400 RefPicList *refPicList;
401 RefPicListTab **rpl_tab;
404 struct HEVCFrame *collocated_ref;
406 AVBufferRef *tab_mvf_buf;
407 AVBufferRef *rpl_tab_buf;
408 AVBufferRef *rpl_buf;
410 AVBufferRef *hwaccel_priv_buf;
411 void *hwaccel_picture_private;
414 * A sequence counter, so that old frames are output first
420 * A combination of HEVC_FRAME_FLAG_*
425 typedef struct HEVCLocalContext {
426 uint8_t cabac_state[HEVC_CONTEXTS];
428 uint8_t stat_coeff[HEVC_STAT_COEFFS];
430 uint8_t first_qp_group;
442 uint8_t ctb_left_flag;
444 uint8_t ctb_up_right_flag;
445 uint8_t ctb_up_left_flag;
448 /* +7 is for subpixel interpolation, *2 for high bit depths */
449 DECLARE_ALIGNED(32, uint8_t, edge_emu_buffer)[(MAX_PB_SIZE + 7) * EDGE_EMU_BUFFER_STRIDE * 2];
450 /* The extended size between the new edge emu buffer is abused by SAO */
451 DECLARE_ALIGNED(32, uint8_t, edge_emu_buffer2)[(MAX_PB_SIZE + 7) * EDGE_EMU_BUFFER_STRIDE * 2];
452 DECLARE_ALIGNED(32, int16_t, tmp)[MAX_PB_SIZE * MAX_PB_SIZE];
457 NeighbourAvailable na;
459 #define BOUNDARY_LEFT_SLICE (1 << 0)
460 #define BOUNDARY_LEFT_TILE (1 << 1)
461 #define BOUNDARY_UPPER_SLICE (1 << 2)
462 #define BOUNDARY_UPPER_TILE (1 << 3)
463 /* properties of the boundary of the current CTB for the purposes
464 * of the deblocking filter */
468 typedef struct HEVCContext {
469 const AVClass *c; // needed by private avoptions
470 AVCodecContext *avctx;
472 struct HEVCContext **sList;
474 HEVCLocalContext **HEVClcList;
475 HEVCLocalContext *HEVClc;
477 uint8_t threads_type;
478 uint8_t threads_number;
483 uint8_t *cabac_state;
484 uint8_t stat_coeff[HEVC_STAT_COEFFS];
486 /** 1 if the independent slice segment header was successfully parsed */
487 uint8_t slice_initialized;
490 AVFrame *output_frame;
491 uint8_t *sao_pixel_buffer_h[3];
492 uint8_t *sao_pixel_buffer_v[3];
496 struct AVMD5 *md5_ctx;
498 AVBufferPool *tab_mvf_pool;
499 AVBufferPool *rpl_tab_pool;
501 ///< candidate references for the current frame
507 enum HEVCNALUnitType nal_unit_type;
508 int temporal_id; ///< temporal_id_plus1 - 1
513 int slice_idx; ///< number of the slice being currently decoded
514 int eos; ///< current packet contains an EOS/EOB NAL
515 int last_eos; ///< last packet contains an EOS/EOB NAL
522 int no_rasl_output_flag;
525 HEVCDSPContext hevcdsp;
526 VideoDSPContext vdsp;
527 BswapDSPContext bdsp;
529 uint8_t *horizontal_bs;
530 uint8_t *vertical_bs;
532 int32_t *tab_slice_address;
536 uint8_t *tab_ct_depth;
540 uint8_t *cbf_luma; // cbf_luma of colocated TU
543 // CTB-level flags affecting loop filter operation
544 uint8_t *filter_slice_edges;
546 /** used on BE to byteswap the lines for checksumming */
547 uint8_t *checksum_buf;
548 int checksum_buf_size;
551 * Sequence counters for decoded and output frames, so that old
552 * frames are output first after a POC reset
557 int enable_parallel_tiles;
563 // type of the first VCL NAL of the current frame
564 enum HEVCNALUnitType first_nal_type;
566 uint8_t context_initialized;
567 int is_nalff; ///< this flag is != 0 if bitstream is encapsulated
568 ///< as a format defined in 14496-15
569 int apply_defdispwin;
571 int nal_length_size; ///< Number of bytes used for nal length (1, 2 or 4)
576 * Mark all frames in DPB as unused for reference.
578 void ff_hevc_clear_refs(HEVCContext *s);
581 * Drop all frames currently in DPB.
583 void ff_hevc_flush_dpb(HEVCContext *s);
585 RefPicList *ff_hevc_get_ref_list(HEVCContext *s, HEVCFrame *frame,
589 * Construct the reference picture sets for the current frame.
591 int ff_hevc_frame_rps(HEVCContext *s);
594 * Construct the reference picture list(s) for the current slice.
596 int ff_hevc_slice_rpl(HEVCContext *s);
598 void ff_hevc_save_states(HEVCContext *s, int ctb_addr_ts);
599 int ff_hevc_cabac_init(HEVCContext *s, int ctb_addr_ts, int thread);
600 int ff_hevc_sao_merge_flag_decode(HEVCContext *s);
601 int ff_hevc_sao_type_idx_decode(HEVCContext *s);
602 int ff_hevc_sao_band_position_decode(HEVCContext *s);
603 int ff_hevc_sao_offset_abs_decode(HEVCContext *s);
604 int ff_hevc_sao_offset_sign_decode(HEVCContext *s);
605 int ff_hevc_sao_eo_class_decode(HEVCContext *s);
606 int ff_hevc_end_of_slice_flag_decode(HEVCContext *s);
607 int ff_hevc_cu_transquant_bypass_flag_decode(HEVCContext *s);
608 int ff_hevc_skip_flag_decode(HEVCContext *s, int x0, int y0,
610 int ff_hevc_pred_mode_decode(HEVCContext *s);
611 int ff_hevc_split_coding_unit_flag_decode(HEVCContext *s, int ct_depth,
613 int ff_hevc_part_mode_decode(HEVCContext *s, int log2_cb_size);
614 int ff_hevc_pcm_flag_decode(HEVCContext *s);
615 int ff_hevc_prev_intra_luma_pred_flag_decode(HEVCContext *s);
616 int ff_hevc_mpm_idx_decode(HEVCContext *s);
617 int ff_hevc_rem_intra_luma_pred_mode_decode(HEVCContext *s);
618 int ff_hevc_intra_chroma_pred_mode_decode(HEVCContext *s);
619 int ff_hevc_merge_idx_decode(HEVCContext *s);
620 int ff_hevc_merge_flag_decode(HEVCContext *s);
621 int ff_hevc_inter_pred_idc_decode(HEVCContext *s, int nPbW, int nPbH);
622 int ff_hevc_ref_idx_lx_decode(HEVCContext *s, int num_ref_idx_lx);
623 int ff_hevc_mvp_lx_flag_decode(HEVCContext *s);
624 int ff_hevc_no_residual_syntax_flag_decode(HEVCContext *s);
625 int ff_hevc_split_transform_flag_decode(HEVCContext *s, int log2_trafo_size);
626 int ff_hevc_cbf_cb_cr_decode(HEVCContext *s, int trafo_depth);
627 int ff_hevc_cbf_luma_decode(HEVCContext *s, int trafo_depth);
628 int ff_hevc_log2_res_scale_abs(HEVCContext *s, int idx);
629 int ff_hevc_res_scale_sign_flag(HEVCContext *s, int idx);
632 * Get the number of candidate references for the current frame.
634 int ff_hevc_frame_nb_refs(const HEVCContext *s);
636 int ff_hevc_set_new_ref(HEVCContext *s, AVFrame **frame, int poc);
638 static av_always_inline int ff_hevc_nal_is_nonref(enum HEVCNALUnitType type)
641 case HEVC_NAL_TRAIL_N:
643 case HEVC_NAL_STSA_N:
644 case HEVC_NAL_RADL_N:
645 case HEVC_NAL_RASL_N:
646 case HEVC_NAL_VCL_N10:
647 case HEVC_NAL_VCL_N12:
648 case HEVC_NAL_VCL_N14:
656 * Find next frame in output order and put a reference to it in frame.
657 * @return 1 if a frame was output, 0 otherwise
659 int ff_hevc_output_frame(HEVCContext *s, AVFrame *frame, int flush);
661 void ff_hevc_bump_frame(HEVCContext *s);
663 void ff_hevc_unref_frame(HEVCContext *s, HEVCFrame *frame, int flags);
665 void ff_hevc_set_neighbour_available(HEVCContext *s, int x0, int y0,
667 void ff_hevc_luma_mv_merge_mode(HEVCContext *s, int x0, int y0,
668 int nPbW, int nPbH, int log2_cb_size,
669 int part_idx, int merge_idx, MvField *mv);
670 void ff_hevc_luma_mv_mvp_mode(HEVCContext *s, int x0, int y0,
671 int nPbW, int nPbH, int log2_cb_size,
672 int part_idx, int merge_idx,
673 MvField *mv, int mvp_lx_flag, int LX);
674 void ff_hevc_set_qPy(HEVCContext *s, int xBase, int yBase,
676 void ff_hevc_deblocking_boundary_strengths(HEVCContext *s, int x0, int y0,
677 int log2_trafo_size);
678 int ff_hevc_cu_qp_delta_sign_flag(HEVCContext *s);
679 int ff_hevc_cu_qp_delta_abs(HEVCContext *s);
680 int ff_hevc_cu_chroma_qp_offset_flag(HEVCContext *s);
681 int ff_hevc_cu_chroma_qp_offset_idx(HEVCContext *s);
682 void ff_hevc_hls_filter(HEVCContext *s, int x, int y, int ctb_size);
683 void ff_hevc_hls_filters(HEVCContext *s, int x_ctb, int y_ctb, int ctb_size);
684 void ff_hevc_hls_residual_coding(HEVCContext *s, int x0, int y0,
685 int log2_trafo_size, enum ScanType scan_idx,
688 void ff_hevc_hls_mvd_coding(HEVCContext *s, int x0, int y0, int log2_cb_size);
690 extern const uint8_t ff_hevc_qpel_extra_before[4];
691 extern const uint8_t ff_hevc_qpel_extra_after[4];
692 extern const uint8_t ff_hevc_qpel_extra[4];
694 #endif /* AVCODEC_HEVCDEC_H */