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"
36 #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 LongTermRPS {
236 typedef struct RefPicList {
237 struct HEVCFrame *ref[HEVC_MAX_REFS];
238 int list[HEVC_MAX_REFS];
239 int isLongTerm[HEVC_MAX_REFS];
243 typedef struct RefPicListTab {
244 RefPicList refPicList[2];
247 typedef struct SliceHeader {
250 ///< address (in raster order) of the first block in the current slice segment
251 unsigned int slice_segment_addr;
252 ///< address (in raster order) of the first block in the current slice
253 unsigned int slice_addr;
255 enum HEVCSliceType slice_type;
257 int pic_order_cnt_lsb;
259 uint8_t first_slice_in_pic_flag;
260 uint8_t dependent_slice_segment_flag;
261 uint8_t pic_output_flag;
262 uint8_t colour_plane_id;
264 ///< RPS coded in the slice header itself is stored here
265 int short_term_ref_pic_set_sps_flag;
266 int short_term_ref_pic_set_size;
267 ShortTermRPS slice_rps;
268 const ShortTermRPS *short_term_rps;
269 int long_term_ref_pic_set_size;
270 LongTermRPS long_term_rps;
271 unsigned int list_entry_lx[2][32];
273 uint8_t rpl_modification_flag[2];
274 uint8_t no_output_of_prior_pics_flag;
275 uint8_t slice_temporal_mvp_enabled_flag;
277 unsigned int nb_refs[2];
279 uint8_t slice_sample_adaptive_offset_flag[3];
280 uint8_t mvd_l1_zero_flag;
282 uint8_t cabac_init_flag;
283 uint8_t disable_deblocking_filter_flag; ///< slice_header_disable_deblocking_filter_flag
284 uint8_t slice_loop_filter_across_slices_enabled_flag;
285 uint8_t collocated_list;
287 unsigned int collocated_ref_idx;
290 int slice_cb_qp_offset;
291 int slice_cr_qp_offset;
293 uint8_t cu_chroma_qp_offset_enabled_flag;
295 int beta_offset; ///< beta_offset_div2 * 2
296 int tc_offset; ///< tc_offset_div2 * 2
298 unsigned int max_num_merge_cand; ///< 5 - 5_minus_max_num_merge_cand
300 unsigned *entry_point_offset;
303 int num_entry_point_offsets;
307 uint8_t luma_log2_weight_denom;
308 int16_t chroma_log2_weight_denom;
310 int16_t luma_weight_l0[16];
311 int16_t chroma_weight_l0[16][2];
312 int16_t chroma_weight_l1[16][2];
313 int16_t luma_weight_l1[16];
315 int16_t luma_offset_l0[16];
316 int16_t chroma_offset_l0[16][2];
318 int16_t luma_offset_l1[16];
319 int16_t chroma_offset_l1[16][2];
321 int slice_ctb_addr_rs;
324 typedef struct CodingUnit {
328 enum PredMode pred_mode; ///< PredMode
329 enum PartMode part_mode; ///< PartMode
331 // Inferred parameters
332 uint8_t intra_split_flag; ///< IntraSplitFlag
333 uint8_t max_trafo_depth; ///< MaxTrafoDepth
334 uint8_t cu_transquant_bypass_flag;
338 int16_t x; ///< horizontal component of motion vector
339 int16_t y; ///< vertical component of motion vector
342 typedef struct MvField {
343 DECLARE_ALIGNED(4, Mv, mv)[2];
348 typedef struct NeighbourAvailable {
349 int cand_bottom_left;
354 int cand_up_right_sap;
355 } NeighbourAvailable;
357 typedef struct PredictionUnit {
359 int rem_intra_luma_pred_mode;
360 uint8_t intra_pred_mode[4];
363 uint8_t intra_pred_mode_c[4];
364 uint8_t chroma_mode_c[4];
367 typedef struct TransformUnit {
372 // Inferred parameters;
374 int intra_pred_mode_c;
376 uint8_t is_cu_qp_delta_coded;
377 uint8_t is_cu_chroma_qp_offset_coded;
378 int8_t cu_qp_offset_cb;
379 int8_t cu_qp_offset_cr;
383 typedef struct DBParams {
388 #define HEVC_FRAME_FLAG_OUTPUT (1 << 0)
389 #define HEVC_FRAME_FLAG_SHORT_REF (1 << 1)
390 #define HEVC_FRAME_FLAG_LONG_REF (1 << 2)
391 #define HEVC_FRAME_FLAG_BUMPING (1 << 3)
393 typedef struct HEVCFrame {
397 RefPicList *refPicList;
398 RefPicListTab **rpl_tab;
401 struct HEVCFrame *collocated_ref;
405 AVBufferRef *tab_mvf_buf;
406 AVBufferRef *rpl_tab_buf;
407 AVBufferRef *rpl_buf;
409 AVBufferRef *hwaccel_priv_buf;
410 void *hwaccel_picture_private;
413 * A sequence counter, so that old frames are output first
419 * A combination of HEVC_FRAME_FLAG_*
424 typedef struct HEVCLocalContext {
425 uint8_t cabac_state[HEVC_CONTEXTS];
427 uint8_t stat_coeff[4];
429 uint8_t first_qp_group;
441 uint8_t ctb_left_flag;
443 uint8_t ctb_up_right_flag;
444 uint8_t ctb_up_left_flag;
447 /* +7 is for subpixel interpolation, *2 for high bit depths */
448 DECLARE_ALIGNED(32, uint8_t, edge_emu_buffer)[(MAX_PB_SIZE + 7) * EDGE_EMU_BUFFER_STRIDE * 2];
449 /* The extended size between the new edge emu buffer is abused by SAO */
450 DECLARE_ALIGNED(32, uint8_t, edge_emu_buffer2)[(MAX_PB_SIZE + 7) * EDGE_EMU_BUFFER_STRIDE * 2];
451 DECLARE_ALIGNED(32, int16_t, tmp [MAX_PB_SIZE * MAX_PB_SIZE]);
456 NeighbourAvailable na;
458 #define BOUNDARY_LEFT_SLICE (1 << 0)
459 #define BOUNDARY_LEFT_TILE (1 << 1)
460 #define BOUNDARY_UPPER_SLICE (1 << 2)
461 #define BOUNDARY_UPPER_TILE (1 << 3)
462 /* properties of the boundary of the current CTB for the purposes
463 * of the deblocking filter */
467 typedef struct HEVCContext {
468 const AVClass *c; // needed by private avoptions
469 AVCodecContext *avctx;
471 struct HEVCContext *sList[MAX_NB_THREADS];
473 HEVCLocalContext *HEVClcList[MAX_NB_THREADS];
474 HEVCLocalContext *HEVClc;
476 uint8_t threads_type;
477 uint8_t threads_number;
482 uint8_t *cabac_state;
484 /** 1 if the independent slice segment header was successfully parsed */
485 uint8_t slice_initialized;
488 AVFrame *output_frame;
489 uint8_t *sao_pixel_buffer_h[3];
490 uint8_t *sao_pixel_buffer_v[3];
494 AVBufferPool *tab_mvf_pool;
495 AVBufferPool *rpl_tab_pool;
497 ///< candidate references for the current frame
503 enum HEVCNALUnitType nal_unit_type;
504 int temporal_id; ///< temporal_id_plus1 - 1
509 int slice_idx; ///< number of the slice being currently decoded
510 int eos; ///< current packet contains an EOS/EOB NAL
511 int last_eos; ///< last packet contains an EOS/EOB NAL
517 int no_rasl_output_flag;
520 HEVCDSPContext hevcdsp;
521 VideoDSPContext vdsp;
522 BswapDSPContext bdsp;
524 uint8_t *horizontal_bs;
525 uint8_t *vertical_bs;
527 int32_t *tab_slice_address;
531 uint8_t *tab_ct_depth;
535 uint8_t *cbf_luma; // cbf_luma of colocated TU
538 // CTB-level flags affecting loop filter operation
539 uint8_t *filter_slice_edges;
541 /** used on BE to byteswap the lines for checksumming */
542 uint8_t *checksum_buf;
543 int checksum_buf_size;
546 * Sequence counters for decoded and output frames, so that old
547 * frames are output first after a POC reset
552 int enable_parallel_tiles;
558 // type of the first VCL NAL of the current frame
559 enum HEVCNALUnitType first_nal_type;
561 // for checking the frame checksums
562 struct AVMD5 *md5_ctx;
566 uint8_t context_initialized;
567 uint8_t is_nalff; ///< this flag is != 0 if bitstream is encapsulated
568 ///< as a format defined in 14496-15
569 int apply_defdispwin;
571 int active_seq_parameter_set_id;
573 int nal_length_size; ///< Number of bytes used for nal length (1, 2 or 4)
576 /** frame packing arrangement variables */
577 int sei_frame_packing_present;
578 int frame_packing_arrangement_type;
579 int content_interpretation_type;
580 int quincunx_subsampling;
582 /** display orientation */
583 int sei_display_orientation_present;
584 int sei_anticlockwise_rotation;
585 int sei_hflip, sei_vflip;
589 uint8_t* a53_caption;
590 int a53_caption_size;
592 /** mastering display */
593 int sei_mastering_display_info_present;
594 uint16_t display_primaries[3][2];
595 uint16_t white_point[2];
596 uint32_t max_mastering_luminance;
597 uint32_t min_mastering_luminance;
599 /* content light level */
600 int sei_content_light_present;
601 uint16_t max_content_light_level;
602 uint16_t max_pic_average_light_level;
606 int ff_hevc_decode_nal_sei(HEVCContext *s);
609 * Mark all frames in DPB as unused for reference.
611 void ff_hevc_clear_refs(HEVCContext *s);
614 * Drop all frames currently in DPB.
616 void ff_hevc_flush_dpb(HEVCContext *s);
619 * Compute POC of the current frame and return it.
621 int ff_hevc_compute_poc(HEVCContext *s, int poc_lsb);
623 RefPicList *ff_hevc_get_ref_list(HEVCContext *s, HEVCFrame *frame,
627 * Construct the reference picture sets for the current frame.
629 int ff_hevc_frame_rps(HEVCContext *s);
632 * Construct the reference picture list(s) for the current slice.
634 int ff_hevc_slice_rpl(HEVCContext *s);
636 void ff_hevc_save_states(HEVCContext *s, int ctb_addr_ts);
637 void ff_hevc_cabac_init(HEVCContext *s, int ctb_addr_ts);
638 int ff_hevc_sao_merge_flag_decode(HEVCContext *s);
639 int ff_hevc_sao_type_idx_decode(HEVCContext *s);
640 int ff_hevc_sao_band_position_decode(HEVCContext *s);
641 int ff_hevc_sao_offset_abs_decode(HEVCContext *s);
642 int ff_hevc_sao_offset_sign_decode(HEVCContext *s);
643 int ff_hevc_sao_eo_class_decode(HEVCContext *s);
644 int ff_hevc_end_of_slice_flag_decode(HEVCContext *s);
645 int ff_hevc_cu_transquant_bypass_flag_decode(HEVCContext *s);
646 int ff_hevc_skip_flag_decode(HEVCContext *s, int x0, int y0,
648 int ff_hevc_pred_mode_decode(HEVCContext *s);
649 int ff_hevc_split_coding_unit_flag_decode(HEVCContext *s, int ct_depth,
651 int ff_hevc_part_mode_decode(HEVCContext *s, int log2_cb_size);
652 int ff_hevc_pcm_flag_decode(HEVCContext *s);
653 int ff_hevc_prev_intra_luma_pred_flag_decode(HEVCContext *s);
654 int ff_hevc_mpm_idx_decode(HEVCContext *s);
655 int ff_hevc_rem_intra_luma_pred_mode_decode(HEVCContext *s);
656 int ff_hevc_intra_chroma_pred_mode_decode(HEVCContext *s);
657 int ff_hevc_merge_idx_decode(HEVCContext *s);
658 int ff_hevc_merge_flag_decode(HEVCContext *s);
659 int ff_hevc_inter_pred_idc_decode(HEVCContext *s, int nPbW, int nPbH);
660 int ff_hevc_ref_idx_lx_decode(HEVCContext *s, int num_ref_idx_lx);
661 int ff_hevc_mvp_lx_flag_decode(HEVCContext *s);
662 int ff_hevc_no_residual_syntax_flag_decode(HEVCContext *s);
663 int ff_hevc_split_transform_flag_decode(HEVCContext *s, int log2_trafo_size);
664 int ff_hevc_cbf_cb_cr_decode(HEVCContext *s, int trafo_depth);
665 int ff_hevc_cbf_luma_decode(HEVCContext *s, int trafo_depth);
666 int ff_hevc_log2_res_scale_abs(HEVCContext *s, int idx);
667 int ff_hevc_res_scale_sign_flag(HEVCContext *s, int idx);
670 * Get the number of candidate references for the current frame.
672 int ff_hevc_frame_nb_refs(HEVCContext *s);
674 int ff_hevc_set_new_ref(HEVCContext *s, AVFrame **frame, int poc);
677 * Find next frame in output order and put a reference to it in frame.
678 * @return 1 if a frame was output, 0 otherwise
680 int ff_hevc_output_frame(HEVCContext *s, AVFrame *frame, int flush);
682 void ff_hevc_bump_frame(HEVCContext *s);
684 void ff_hevc_unref_frame(HEVCContext *s, HEVCFrame *frame, int flags);
686 void ff_hevc_set_neighbour_available(HEVCContext *s, int x0, int y0,
688 void ff_hevc_luma_mv_merge_mode(HEVCContext *s, int x0, int y0,
689 int nPbW, int nPbH, int log2_cb_size,
690 int part_idx, int merge_idx, MvField *mv);
691 void ff_hevc_luma_mv_mvp_mode(HEVCContext *s, int x0, int y0,
692 int nPbW, int nPbH, int log2_cb_size,
693 int part_idx, int merge_idx,
694 MvField *mv, int mvp_lx_flag, int LX);
695 void ff_hevc_set_qPy(HEVCContext *s, int xBase, int yBase,
697 void ff_hevc_deblocking_boundary_strengths(HEVCContext *s, int x0, int y0,
698 int log2_trafo_size);
699 int ff_hevc_cu_qp_delta_sign_flag(HEVCContext *s);
700 int ff_hevc_cu_qp_delta_abs(HEVCContext *s);
701 int ff_hevc_cu_chroma_qp_offset_flag(HEVCContext *s);
702 int ff_hevc_cu_chroma_qp_offset_idx(HEVCContext *s);
703 void ff_hevc_hls_filter(HEVCContext *s, int x, int y, int ctb_size);
704 void ff_hevc_hls_filters(HEVCContext *s, int x_ctb, int y_ctb, int ctb_size);
705 void ff_hevc_hls_residual_coding(HEVCContext *s, int x0, int y0,
706 int log2_trafo_size, enum ScanType scan_idx,
709 void ff_hevc_hls_mvd_coding(HEVCContext *s, int x0, int y0, int log2_cb_size);
712 * Reset SEI values that are stored on the Context.
713 * e.g. Caption data that was extracted during NAL
716 * @param s HEVCContext.
718 void ff_hevc_reset_sei(HEVCContext *s);
720 extern const uint8_t ff_hevc_qpel_extra_before[4];
721 extern const uint8_t ff_hevc_qpel_extra_after[4];
722 extern const uint8_t ff_hevc_qpel_extra[4];
724 #endif /* AVCODEC_HEVCDEC_H */