4 * Copyright (C) 2012 - 2013 Guillaume Martres
6 * This file is part of Libav.
8 * Libav 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 * Libav 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 Libav; 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
29 #include "libavutil/buffer.h"
30 #include "libavutil/md5.h"
36 #include "h2645_parse.h"
44 //TODO: check if this is really the maximum
45 #define MAX_TRANSFORM_DEPTH 5
47 #define MAX_TB_SIZE 32
48 #define MAX_PB_SIZE 64
50 #define DEFAULT_INTRA_TC_OFFSET 2
52 #define HEVC_CONTEXTS 183
54 #define MRG_MAX_NUM_CANDS 5
59 #define EPEL_EXTRA_BEFORE 1
60 #define EPEL_EXTRA_AFTER 2
63 #define EDGE_EMU_BUFFER_STRIDE 80
66 * Value of the luma sample at position (x, y) in the 2D array tab.
68 #define SAMPLE(tab, x, y) ((tab)[(y) * s->sps->width + (x)])
69 #define SAMPLE_CTB(tab, x, y) ((tab)[(y) * min_cb_width + (x)])
71 #define IS_IDR(s) (s->nal_unit_type == HEVC_NAL_IDR_W_RADL || s->nal_unit_type == HEVC_NAL_IDR_N_LP)
72 #define IS_BLA(s) (s->nal_unit_type == HEVC_NAL_BLA_W_RADL || s->nal_unit_type == HEVC_NAL_BLA_W_LP || \
73 s->nal_unit_type == HEVC_NAL_BLA_N_LP)
74 #define IS_IRAP(s) (s->nal_unit_type >= 16 && s->nal_unit_type <= 23)
76 #define FFUDIV(a,b) (((a) > 0 ? (a) : (a) - (b) + 1) / (b))
77 #define FFUMOD(a,b) ((a) - (b) * FFUDIV(a,b))
96 SPLIT_CODING_UNIT_FLAG,
97 CU_TRANSQUANT_BYPASS_FLAG,
103 PREV_INTRA_LUMA_PRED_FLAG,
105 REM_INTRA_LUMA_PRED_MODE,
106 INTRA_CHROMA_PRED_MODE,
112 ABS_MVD_GREATER0_FLAG,
113 ABS_MVD_GREATER1_FLAG,
117 NO_RESIDUAL_DATA_FLAG,
118 SPLIT_TRANSFORM_FLAG,
122 LAST_SIGNIFICANT_COEFF_X_PREFIX,
123 LAST_SIGNIFICANT_COEFF_Y_PREFIX,
124 LAST_SIGNIFICANT_COEFF_X_SUFFIX,
125 LAST_SIGNIFICANT_COEFF_Y_SUFFIX,
126 SIGNIFICANT_COEFF_GROUP_FLAG,
127 SIGNIFICANT_COEFF_FLAG,
128 COEFF_ABS_LEVEL_GREATER1_FLAG,
129 COEFF_ABS_LEVEL_GREATER2_FLAG,
130 COEFF_ABS_LEVEL_REMAINING,
214 typedef struct LongTermRPS {
220 typedef struct RefPicList {
221 struct HEVCFrame *ref[HEVC_MAX_REFS];
222 int list[HEVC_MAX_REFS];
223 int isLongTerm[HEVC_MAX_REFS];
227 typedef struct RefPicListTab {
228 RefPicList refPicList[2];
231 typedef struct SliceHeader {
234 ///< address (in raster order) of the first block in the current slice segment
235 unsigned int slice_segment_addr;
236 ///< address (in raster order) of the first block in the current slice
237 unsigned int slice_addr;
239 enum HEVCSliceType slice_type;
241 int pic_order_cnt_lsb;
243 uint8_t first_slice_in_pic_flag;
244 uint8_t dependent_slice_segment_flag;
245 uint8_t pic_output_flag;
246 uint8_t colour_plane_id;
248 ///< RPS coded in the slice header itself is stored here
249 int short_term_ref_pic_set_sps_flag;
250 int short_term_ref_pic_set_size;
251 ShortTermRPS slice_rps;
252 const ShortTermRPS *short_term_rps;
253 int long_term_ref_pic_set_size;
254 LongTermRPS long_term_rps;
255 unsigned int list_entry_lx[2][32];
257 uint8_t rpl_modification_flag[2];
258 uint8_t no_output_of_prior_pics_flag;
259 uint8_t slice_temporal_mvp_enabled_flag;
261 unsigned int nb_refs[2];
263 uint8_t slice_sample_adaptive_offset_flag[3];
264 uint8_t mvd_l1_zero_flag;
266 uint8_t cabac_init_flag;
267 uint8_t disable_deblocking_filter_flag; ///< slice_header_disable_deblocking_filter_flag
268 uint8_t slice_loop_filter_across_slices_enabled_flag;
269 uint8_t collocated_list;
271 unsigned int collocated_ref_idx;
274 int slice_cb_qp_offset;
275 int slice_cr_qp_offset;
277 int beta_offset; ///< beta_offset_div2 * 2
278 int tc_offset; ///< tc_offset_div2 * 2
280 unsigned int max_num_merge_cand; ///< 5 - 5_minus_max_num_merge_cand
282 int num_entry_point_offsets;
286 uint8_t luma_log2_weight_denom;
287 int16_t chroma_log2_weight_denom;
289 int16_t luma_weight_l0[16];
290 int16_t chroma_weight_l0[16][2];
291 int16_t chroma_weight_l1[16][2];
292 int16_t luma_weight_l1[16];
294 int16_t luma_offset_l0[16];
295 int16_t chroma_offset_l0[16][2];
297 int16_t luma_offset_l1[16];
298 int16_t chroma_offset_l1[16][2];
300 int slice_ctb_addr_rs;
303 typedef struct CodingTree {
304 int depth; ///< ctDepth
307 typedef struct CodingUnit {
311 enum PredMode pred_mode; ///< PredMode
312 enum PartMode part_mode; ///< PartMode
314 // Inferred parameters
315 uint8_t intra_split_flag; ///< IntraSplitFlag
316 uint8_t max_trafo_depth; ///< MaxTrafoDepth
317 uint8_t cu_transquant_bypass_flag;
321 int16_t x; ///< horizontal component of motion vector
322 int16_t y; ///< vertical component of motion vector
325 typedef struct MvField {
326 DECLARE_ALIGNED(4, Mv, mv)[2];
332 typedef struct NeighbourAvailable {
333 int cand_bottom_left;
338 int cand_up_right_sap;
339 } NeighbourAvailable;
341 typedef struct PredictionUnit {
343 int rem_intra_luma_pred_mode;
344 uint8_t intra_pred_mode[4];
347 uint8_t intra_pred_mode_c;
350 typedef struct TransformUnit {
353 // Inferred parameters;
354 int cur_intra_pred_mode;
355 uint8_t is_cu_qp_delta_coded;
358 typedef struct DBParams {
363 #define HEVC_FRAME_FLAG_OUTPUT (1 << 0)
364 #define HEVC_FRAME_FLAG_SHORT_REF (1 << 1)
365 #define HEVC_FRAME_FLAG_LONG_REF (1 << 2)
367 typedef struct HEVCFrame {
371 RefPicList *refPicList;
372 RefPicListTab **rpl_tab;
375 struct HEVCFrame *collocated_ref;
379 AVBufferRef *tab_mvf_buf;
380 AVBufferRef *rpl_tab_buf;
381 AVBufferRef *rpl_buf;
383 AVBufferRef *hwaccel_priv_buf;
384 void *hwaccel_picture_private;
387 * A sequence counter, so that old frames are output first
393 * A combination of HEVC_FRAME_FLAG_*
400 typedef struct HEVCPredContext {
401 void (*intra_pred[4])(struct HEVCContext *s, int x0, int y0, int c_idx);
403 void (*pred_planar[4])(uint8_t *src, const uint8_t *top,
404 const uint8_t *left, ptrdiff_t stride);
405 void (*pred_dc)(uint8_t *src, const uint8_t *top, const uint8_t *left,
406 ptrdiff_t stride, int log2_size, int c_idx);
407 void (*pred_angular[4])(uint8_t *src, const uint8_t *top,
408 const uint8_t *left, ptrdiff_t stride,
409 int c_idx, int mode);
412 typedef struct HEVCLocalContext {
413 DECLARE_ALIGNED(16, int16_t, mc_buffer[(MAX_PB_SIZE + 24) * MAX_PB_SIZE]);
414 uint8_t cabac_state[HEVC_CONTEXTS];
416 uint8_t first_qp_group;
426 uint8_t ctb_left_flag;
428 uint8_t ctb_up_right_flag;
429 uint8_t ctb_up_left_flag;
430 int start_of_tiles_x;
433 /* +7 is for subpixel interpolation, *2 for high bit depths */
434 DECLARE_ALIGNED(32, uint8_t, edge_emu_buffer)[(MAX_PB_SIZE + 7) * EDGE_EMU_BUFFER_STRIDE * 2];
438 NeighbourAvailable na;
440 #define BOUNDARY_LEFT_SLICE (1 << 0)
441 #define BOUNDARY_LEFT_TILE (1 << 1)
442 #define BOUNDARY_UPPER_SLICE (1 << 2)
443 #define BOUNDARY_UPPER_TILE (1 << 3)
444 /* properties of the boundary of the current CTB for the purposes
445 * of the deblocking filter */
449 typedef struct HEVCContext {
450 const AVClass *c; // needed by private avoptions
451 AVCodecContext *avctx;
453 HEVCLocalContext HEVClc;
455 uint8_t cabac_state[HEVC_CONTEXTS];
457 /** 1 if the independent slice segment header was successfully parsed */
458 uint8_t slice_initialized;
463 AVFrame *output_frame;
467 AVBufferPool *tab_mvf_pool;
468 AVBufferPool *rpl_tab_pool;
470 ///< candidate references for the current frame
476 enum HEVCNALUnitType nal_unit_type;
477 int temporal_id; ///< temporal_id_plus1 - 1
482 int slice_idx; ///< number of the slice being currently decoded
483 int eos; ///< current packet contains an EOS/EOB NAL
491 HEVCDSPContext hevcdsp;
492 VideoDSPContext vdsp;
493 BswapDSPContext bdsp;
495 uint8_t *horizontal_bs;
496 uint8_t *vertical_bs;
498 int32_t *tab_slice_address;
502 uint8_t *tab_ct_depth;
506 uint8_t *cbf_luma; // cbf_luma of colocated TU
509 // CTB-level flags affecting loop filter operation
510 uint8_t *filter_slice_edges;
512 /** used on BE to byteswap the lines for checksumming */
513 uint8_t *checksum_buf;
514 int checksum_buf_size;
517 * Sequence counters for decoded and output frames, so that old
518 * frames are output first after a POC reset
524 // type of the first VCL NAL of the current frame
525 enum HEVCNALUnitType first_nal_type;
527 // for checking the frame checksums
528 struct AVMD5 *md5_ctx;
532 uint8_t context_initialized;
533 uint8_t is_nalff; ///< this flag is != 0 if bitstream is encapsulated
534 ///< as a format defined in 14496-15
535 int apply_defdispwin;
537 int nal_length_size; ///< Number of bytes used for nal length (1, 2 or 4)
540 /** frame packing arrangement variables */
541 int sei_frame_packing_present;
542 int frame_packing_arrangement_type;
543 int content_interpretation_type;
544 int quincunx_subsampling;
546 /** display orientation */
547 int sei_display_orientation_present;
548 int sei_anticlockwise_rotation;
549 int sei_hflip, sei_vflip;
552 int ff_hevc_decode_nal_sei(HEVCContext *s);
555 * Mark all frames in DPB as unused for reference.
557 void ff_hevc_clear_refs(HEVCContext *s);
560 * Drop all frames currently in DPB.
562 void ff_hevc_flush_dpb(HEVCContext *s);
565 * Compute POC of the current frame and return it.
567 int ff_hevc_compute_poc(HEVCContext *s, int poc_lsb);
569 RefPicList *ff_hevc_get_ref_list(HEVCContext *s, HEVCFrame *frame,
573 * Construct the reference picture sets for the current frame.
575 int ff_hevc_frame_rps(HEVCContext *s);
578 * Construct the reference picture list(s) for the current slice.
580 int ff_hevc_slice_rpl(HEVCContext *s);
582 void ff_hevc_save_states(HEVCContext *s, int ctb_addr_ts);
583 void ff_hevc_cabac_init(HEVCContext *s, int ctb_addr_ts);
584 int ff_hevc_sao_merge_flag_decode(HEVCContext *s);
585 int ff_hevc_sao_type_idx_decode(HEVCContext *s);
586 int ff_hevc_sao_band_position_decode(HEVCContext *s);
587 int ff_hevc_sao_offset_abs_decode(HEVCContext *s);
588 int ff_hevc_sao_offset_sign_decode(HEVCContext *s);
589 int ff_hevc_sao_eo_class_decode(HEVCContext *s);
590 int ff_hevc_end_of_slice_flag_decode(HEVCContext *s);
591 int ff_hevc_cu_transquant_bypass_flag_decode(HEVCContext *s);
592 int ff_hevc_skip_flag_decode(HEVCContext *s, int x0, int y0,
594 int ff_hevc_pred_mode_decode(HEVCContext *s);
595 int ff_hevc_split_coding_unit_flag_decode(HEVCContext *s, int ct_depth,
597 int ff_hevc_part_mode_decode(HEVCContext *s, int log2_cb_size);
598 int ff_hevc_pcm_flag_decode(HEVCContext *s);
599 int ff_hevc_prev_intra_luma_pred_flag_decode(HEVCContext *s);
600 int ff_hevc_mpm_idx_decode(HEVCContext *s);
601 int ff_hevc_rem_intra_luma_pred_mode_decode(HEVCContext *s);
602 int ff_hevc_intra_chroma_pred_mode_decode(HEVCContext *s);
603 int ff_hevc_merge_idx_decode(HEVCContext *s);
604 int ff_hevc_merge_flag_decode(HEVCContext *s);
605 int ff_hevc_inter_pred_idc_decode(HEVCContext *s, int nPbW, int nPbH);
606 int ff_hevc_ref_idx_lx_decode(HEVCContext *s, int num_ref_idx_lx);
607 int ff_hevc_mvp_lx_flag_decode(HEVCContext *s);
608 int ff_hevc_no_residual_syntax_flag_decode(HEVCContext *s);
609 int ff_hevc_abs_mvd_greater0_flag_decode(HEVCContext *s);
610 int ff_hevc_abs_mvd_greater1_flag_decode(HEVCContext *s);
611 int ff_hevc_mvd_decode(HEVCContext *s);
612 int ff_hevc_mvd_sign_flag_decode(HEVCContext *s);
613 int ff_hevc_split_transform_flag_decode(HEVCContext *s, int log2_trafo_size);
614 int ff_hevc_cbf_cb_cr_decode(HEVCContext *s, int trafo_depth);
615 int ff_hevc_cbf_luma_decode(HEVCContext *s, int trafo_depth);
616 int ff_hevc_transform_skip_flag_decode(HEVCContext *s, int c_idx);
617 int ff_hevc_last_significant_coeff_x_prefix_decode(HEVCContext *s, int c_idx,
619 int ff_hevc_last_significant_coeff_y_prefix_decode(HEVCContext *s, int c_idx,
621 int ff_hevc_last_significant_coeff_suffix_decode(HEVCContext *s,
622 int last_significant_coeff_prefix);
623 int ff_hevc_significant_coeff_group_flag_decode(HEVCContext *s, int c_idx,
625 int ff_hevc_significant_coeff_flag_decode(HEVCContext *s, int c_idx, int x_c,
626 int y_c, int log2_trafo_size,
627 int scan_idx, int prev_sig);
628 int ff_hevc_coeff_abs_level_greater1_flag_decode(HEVCContext *s, int c_idx,
630 int ff_hevc_coeff_abs_level_greater2_flag_decode(HEVCContext *s, int c_idx,
632 int ff_hevc_coeff_abs_level_remaining(HEVCContext *s, int base_level,
634 int ff_hevc_coeff_sign_flag(HEVCContext *s, uint8_t nb);
637 * Get the number of candidate references for the current frame.
639 int ff_hevc_frame_nb_refs(HEVCContext *s);
641 int ff_hevc_set_new_ref(HEVCContext *s, AVFrame **frame, int poc);
644 * Find next frame in output order and put a reference to it in frame.
645 * @return 1 if a frame was output, 0 otherwise
647 int ff_hevc_output_frame(HEVCContext *s, AVFrame *frame, int flush);
649 void ff_hevc_unref_frame(HEVCContext *s, HEVCFrame *frame, int flags);
651 void ff_hevc_set_neighbour_available(HEVCContext *s, int x0, int y0,
653 void ff_hevc_luma_mv_merge_mode(HEVCContext *s, int x0, int y0,
654 int nPbW, int nPbH, int log2_cb_size,
655 int part_idx, int merge_idx, MvField *mv);
656 void ff_hevc_luma_mv_mvp_mode(HEVCContext *s, int x0, int y0,
657 int nPbW, int nPbH, int log2_cb_size,
658 int part_idx, int merge_idx,
659 MvField *mv, int mvp_lx_flag, int LX);
660 void ff_hevc_set_qPy(HEVCContext *s, int xC, int yC, int xBase, int yBase,
662 void ff_hevc_deblocking_boundary_strengths(HEVCContext *s, int x0, int y0,
663 int log2_trafo_size);
664 int ff_hevc_cu_qp_delta_sign_flag(HEVCContext *s);
665 int ff_hevc_cu_qp_delta_abs(HEVCContext *s);
666 void ff_hevc_hls_filter(HEVCContext *s, int x, int y);
667 void ff_hevc_hls_filters(HEVCContext *s, int x_ctb, int y_ctb, int ctb_size);
669 void ff_hevc_pred_init(HEVCPredContext *hpc, int bit_depth);
671 extern const uint8_t ff_hevc_qpel_extra_before[4];
672 extern const uint8_t ff_hevc_qpel_extra_after[4];
673 extern const uint8_t ff_hevc_qpel_extra[4];
675 #endif /* AVCODEC_HEVCDEC_H */