FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
hevc.h
Go to the documentation of this file.
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 
23 #ifndef AVCODEC_HEVC_H
24 #define AVCODEC_HEVC_H
25 
26 #include "libavutil/buffer.h"
27 #include "libavutil/md5.h"
28 
29 #include "avcodec.h"
30 #include "bswapdsp.h"
31 #include "cabac.h"
32 #include "get_bits.h"
33 #include "hevcpred.h"
34 #include "hevcdsp.h"
35 #include "internal.h"
36 #include "thread.h"
37 #include "videodsp.h"
38 
39 #define MAX_DPB_SIZE 16 // A.4.1
40 #define MAX_REFS 16
41 
42 #define MAX_NB_THREADS 16
43 #define SHIFT_CTB_WPP 2
44 
45 /**
46  * 7.4.2.1
47  */
48 #define MAX_SUB_LAYERS 7
49 #define MAX_VPS_COUNT 16
50 #define MAX_SPS_COUNT 32
51 #define MAX_PPS_COUNT 256
52 #define MAX_SHORT_TERM_RPS_COUNT 64
53 #define MAX_CU_SIZE 128
54 
55 //TODO: check if this is really the maximum
56 #define MAX_TRANSFORM_DEPTH 5
57 
58 #define MAX_TB_SIZE 32
59 #define MAX_PB_SIZE 64
60 #define MAX_LOG2_CTB_SIZE 6
61 #define MAX_QP 51
62 #define DEFAULT_INTRA_TC_OFFSET 2
63 
64 #define HEVC_CONTEXTS 199
65 
66 #define MRG_MAX_NUM_CANDS 5
67 
68 #define L0 0
69 #define L1 1
70 
71 #define EPEL_EXTRA_BEFORE 1
72 #define EPEL_EXTRA_AFTER 2
73 #define EPEL_EXTRA 3
74 #define QPEL_EXTRA_BEFORE 3
75 #define QPEL_EXTRA_AFTER 4
76 #define QPEL_EXTRA 7
77 
78 #define EDGE_EMU_BUFFER_STRIDE 80
79 
80 /**
81  * Value of the luma sample at position (x, y) in the 2D array tab.
82  */
83 #define SAMPLE(tab, x, y) ((tab)[(y) * s->sps->width + (x)])
84 #define SAMPLE_CTB(tab, x, y) ((tab)[(y) * min_cb_width + (x)])
85 #define SAMPLE_CBF(tab, x, y) ((tab)[((y) & ((1<<log2_trafo_size)-1)) * MAX_CU_SIZE + ((x) & ((1<<log2_trafo_size)-1))])
86 #define SAMPLE_CBF2(tab, x, y) ((tab)[(y) * MAX_CU_SIZE + (x)])
87 
88 #define IS_IDR(s) ((s)->nal_unit_type == NAL_IDR_W_RADL || (s)->nal_unit_type == NAL_IDR_N_LP)
89 #define IS_BLA(s) ((s)->nal_unit_type == NAL_BLA_W_RADL || (s)->nal_unit_type == NAL_BLA_W_LP || \
90  (s)->nal_unit_type == NAL_BLA_N_LP)
91 #define IS_IRAP(s) ((s)->nal_unit_type >= 16 && (s)->nal_unit_type <= 23)
92 
93 /**
94  * Table 7-3: NAL unit type codes
95  */
99  NAL_TSA_N = 2,
113  NAL_VPS = 32,
114  NAL_SPS = 33,
115  NAL_PPS = 34,
116  NAL_AUD = 35,
122 };
123 
124 enum RPSType {
131 };
132 
133 enum SliceType {
134  B_SLICE = 0,
135  P_SLICE = 1,
136  I_SLICE = 2,
137 };
138 
189 };
190 
191 enum PartMode {
195  PART_NxN = 3,
200 };
201 
202 enum PredMode {
206 };
207 
209  PRED_L0 = 0,
212 };
213 
214 enum PredFlag {
215  PF_INTRA = 0,
219 };
220 
257 };
258 
259 enum SAOType {
263 };
264 
270 };
271 
272 enum ScanType {
276 };
277 
278 typedef struct ShortTermRPS {
279  unsigned int num_negative_pics;
283 } ShortTermRPS;
284 
285 typedef struct LongTermRPS {
286  int poc[32];
289 } LongTermRPS;
290 
291 typedef struct RefPicList {
295  int nb_refs;
296 } RefPicList;
297 
298 typedef struct RefPicListTab {
300 } RefPicListTab;
301 
302 typedef struct HEVCWindow {
307 } HEVCWindow;
308 
309 typedef struct VUI {
311 
314 
322 
327 
330 
333 
336  uint32_t vui_time_scale;
340 
350 } VUI;
351 
352 typedef struct PTLCommon {
362 } PTLCommon;
363 
364 typedef struct PTL {
367 
370 } PTL;
371 
372 typedef struct HEVCVPS {
375  int vps_max_sub_layers; ///< vps_max_temporal_layers_minus1 + 1
376 
383  int vps_num_layer_sets; ///< vps_num_layer_sets_minus1 + 1
386  uint32_t vps_time_scale;
388  int vps_num_ticks_poc_diff_one; ///< vps_num_ticks_poc_diff_one_minus1 + 1
390 } HEVCVPS;
391 
392 typedef struct ScalingList {
393  /* This is a little wasteful, since sizeID 0 only needs 8 coeffs,
394  * and size ID 3 only has 2 arrays, not 6. */
395  uint8_t sl[4][6][64];
396  uint8_t sl_dc[2][6];
397 } ScalingList;
398 
399 typedef struct HEVCSPS {
400  unsigned vps_id;
403 
404  ///< output (i.e. cropped) values
407 
409 
413 
414  unsigned int log2_max_poc_lsb;
416 
418  struct {
423 
426 
429 
430  unsigned int nb_st_rps;
432 
435 
440 
441  struct {
444  unsigned int log2_min_pcm_cb_size;
445  unsigned int log2_max_pcm_cb_size;
447  } pcm;
450 
451  unsigned int log2_min_cb_size;
453  unsigned int log2_min_tb_size;
454  unsigned int log2_max_trafo_size;
455  unsigned int log2_ctb_size;
456  unsigned int log2_min_pu_size;
457 
460 
467 
468  ///< coded frame dimension in various units
469  int width;
470  int height;
473  int ctb_size;
480  int tb_mask;
481 
482  int hshift[3];
483  int vshift[3];
484 
486 } HEVCSPS;
487 
488 typedef struct HEVCPPS {
489  unsigned int sps_id; ///< seq_parameter_set_id
490 
492 
494 
495  int num_ref_idx_l0_default_active; ///< num_ref_idx_l0_default_active_minus1 + 1
496  int num_ref_idx_l1_default_active; ///< num_ref_idx_l1_default_active_minus1 + 1
498 
501 
504 
512 
516 
517  int num_tile_columns; ///< num_tile_columns_minus1 + 1
518  int num_tile_rows; ///< num_tile_rows_minus1 + 1
521 
523 
527  int beta_offset; ///< beta_offset_div2 * 2
528  int tc_offset; ///< tc_offset_div2 * 2
529 
532 
534  int log2_parallel_merge_level; ///< log2_parallel_merge_level_minus2 + 2
542  int8_t cb_qp_offset_list[5];
543  int8_t cr_qp_offset_list[5];
546 
547  // Inferred parameters
548  unsigned int *column_width; ///< ColumnWidth
549  unsigned int *row_height; ///< RowHeight
550  unsigned int *col_bd; ///< ColBd
551  unsigned int *row_bd; ///< RowBd
552  int *col_idxX;
553 
554  int *ctb_addr_rs_to_ts; ///< CtbAddrRSToTS
555  int *ctb_addr_ts_to_rs; ///< CtbAddrTSToRS
556  int *tile_id; ///< TileId
557  int *tile_pos_rs; ///< TilePosRS
558  int *min_tb_addr_zs; ///< MinTbAddrZS
559  int *min_tb_addr_zs_tab;///< MinTbAddrZS
560 } HEVCPPS;
561 
562 typedef struct SliceHeader {
563  unsigned int pps_id;
564 
565  ///< address (in raster order) of the first block in the current slice segment
566  unsigned int slice_segment_addr;
567  ///< address (in raster order) of the first block in the current slice
568  unsigned int slice_addr;
569 
571 
573 
578 
579  ///< RPS coded in the slice header itself is stored here
583  unsigned int list_entry_lx[2][32];
584 
588 
589  unsigned int nb_refs[2];
590 
593 
595  uint8_t disable_deblocking_filter_flag; ///< slice_header_disable_deblocking_filter_flag
598 
599  unsigned int collocated_ref_idx;
600 
604 
606 
607  int beta_offset; ///< beta_offset_div2 * 2
608  int tc_offset; ///< tc_offset_div2 * 2
609 
610  unsigned int max_num_merge_cand; ///< 5 - 5_minus_max_num_merge_cand
611 
613  int * offset;
614  int * size;
616 
617  int8_t slice_qp;
618 
621 
622  int16_t luma_weight_l0[16];
623  int16_t chroma_weight_l0[16][2];
624  int16_t chroma_weight_l1[16][2];
625  int16_t luma_weight_l1[16];
626 
627  int16_t luma_offset_l0[16];
628  int16_t chroma_offset_l0[16][2];
629 
630  int16_t luma_offset_l1[16];
631  int16_t chroma_offset_l1[16][2];
632 
634 } SliceHeader;
635 
636 typedef struct CodingTree {
637  int depth; ///< ctDepth
638 } CodingTree;
639 
640 typedef struct CodingUnit {
641  int x;
642  int y;
643 
644  enum PredMode pred_mode; ///< PredMode
645  enum PartMode part_mode; ///< PartMode
646 
648 
650 
651  // Inferred parameters
652  uint8_t intra_split_flag; ///< IntraSplitFlag
653  uint8_t max_trafo_depth; ///< MaxTrafoDepth
655 } CodingUnit;
656 
657 typedef struct Mv {
658  int16_t x; ///< horizontal component of motion vector
659  int16_t y; ///< vertical component of motion vector
660 } Mv;
661 
662 typedef struct MvField {
663  Mv mv[2];
664  int8_t ref_idx[2];
665  int8_t pred_flag;
666 } MvField;
667 
668 typedef struct NeighbourAvailable {
671  int cand_up;
676 
677 typedef struct PredictionUnit {
678  int mpm_idx;
686 
687 typedef struct TransformTree {
691 
692  // Inferred parameters
694 } TransformTree;
695 
696 typedef struct TransformUnit {
699 
701 
702  // Inferred parameters;
711 } TransformUnit;
712 
713 typedef struct DBParams {
716 } DBParams;
717 
718 #define HEVC_FRAME_FLAG_OUTPUT (1 << 0)
719 #define HEVC_FRAME_FLAG_SHORT_REF (1 << 1)
720 #define HEVC_FRAME_FLAG_LONG_REF (1 << 2)
721 
722 typedef struct HEVCFrame {
729  int poc;
731 
733 
737 
738  /**
739  * A sequence counter, so that old frames are output first
740  * after a POC reset
741  */
742  uint16_t sequence;
743 
744  /**
745  * A combination of HEVC_FRAME_FLAG_*
746  */
748 } HEVCFrame;
749 
750 typedef struct HEVCNAL {
753 
754  int size;
755  const uint8_t *data;
756 } HEVCNAL;
757 
758 typedef struct HEVCLocalContext {
761 
763 
765 
769 
770  int8_t qp_y;
771  int8_t curr_qp_y;
772 
773  int qPy_pred;
774 
776 
783  /* +7 is for subpixel interpolation, *2 for high bit depths */
786 
791 
795 
796 typedef struct HEVCContext {
797  const AVClass *c; // needed by private avoptions
799 
801 
804 
807 
808  int width;
809  int height;
810 
812 
813  /** 1 if the independent slice segment header was successfully parsed */
815 
820 
821  const HEVCVPS *vps;
822  const HEVCSPS *sps;
823  const HEVCPPS *pps;
827 
829 
832 
833  ///< candidate references for the current frame
835 
840  int temporal_id; ///< temporal_id_plus1 - 1
843  int poc;
844  int pocTid0;
845  int slice_idx; ///< number of the slice being currently decoded
846  int eos; ///< current packet contains an EOS/EOB NAL
847  int last_eos; ///< last packet contains an EOS/EOB NAL
848  int max_ra;
849  int bs_width;
851 
853 
858  int8_t *qp_y_tab;
861 
863 
864  // CU
867  // PU
869 
870  uint8_t *cbf_luma; // cbf_luma of colocated TU
872 
873  // CTB-level flags affecting loop filter operation
875 
876  /** used on BE to byteswap the lines for checksumming */
879 
880  /**
881  * Sequence counters for decoded and output frames, so that old
882  * frames are output first after a POC reset
883  */
884  uint16_t seq_decode;
885  uint16_t seq_output;
886 
888  int wpp_err;
892 
896 
897  const uint8_t *data;
898 
900  int nb_nals;
902  // type of the first VCL NAL of the current frame
904 
905  // for checking the frame checksums
906  struct AVMD5 *md5_ctx;
907  uint8_t md5[3][16];
909 
911  uint8_t is_nalff; ///< this flag is != 0 if bitstream is encapsulated
912  ///< as a format defined in 14496-15
914 
916 
917  int nal_length_size; ///< Number of bytes used for nal length (1, 2 or 4)
919 
920  /** frame packing arrangement variables */
925 
926  /** display orientation */
930 
932 } HEVCContext;
933 
935  const HEVCSPS *sps, int is_slice_header);
940 
942  HEVCNAL *nal);
943 
944 /**
945  * Mark all frames in DPB as unused for reference.
946  */
948 
949 /**
950  * Drop all frames currently in DPB.
951  */
953 
954 /**
955  * Compute POC of the current frame and return it.
956  */
957 int ff_hevc_compute_poc(HEVCContext *s, int poc_lsb);
958 
960  int x0, int y0);
961 
962 /**
963  * Construct the reference picture sets for the current frame.
964  */
966 
967 /**
968  * Construct the reference picture list(s) for the current slice.
969  */
971 
972 void ff_hevc_save_states(HEVCContext *s, int ctb_addr_ts);
973 void ff_hevc_cabac_init(HEVCContext *s, int ctb_addr_ts);
982 int ff_hevc_skip_flag_decode(HEVCContext *s, int x0, int y0,
983  int x_cb, int y_cb);
986  int x0, int y0);
987 int ff_hevc_part_mode_decode(HEVCContext *s, int log2_cb_size);
995 int ff_hevc_inter_pred_idc_decode(HEVCContext *s, int nPbW, int nPbH);
996 int ff_hevc_ref_idx_lx_decode(HEVCContext *s, int num_ref_idx_lx);
999 int ff_hevc_split_transform_flag_decode(HEVCContext *s, int log2_trafo_size);
1000 int ff_hevc_cbf_cb_cr_decode(HEVCContext *s, int trafo_depth);
1001 int ff_hevc_cbf_luma_decode(HEVCContext *s, int trafo_depth);
1004 
1005 /**
1006  * Get the number of candidate references for the current frame.
1007  */
1009 
1010 int ff_hevc_set_new_ref(HEVCContext *s, AVFrame **frame, int poc);
1011 
1012 /**
1013  * Find next frame in output order and put a reference to it in frame.
1014  * @return 1 if a frame was output, 0 otherwise
1015  */
1017 
1019 
1020 void ff_hevc_set_neighbour_available(HEVCContext *s, int x0, int y0,
1021  int nPbW, int nPbH);
1022 void ff_hevc_luma_mv_merge_mode(HEVCContext *s, int x0, int y0,
1023  int nPbW, int nPbH, int log2_cb_size,
1024  int part_idx, int merge_idx, MvField *mv);
1025 void ff_hevc_luma_mv_mvp_mode(HEVCContext *s, int x0, int y0,
1026  int nPbW, int nPbH, int log2_cb_size,
1027  int part_idx, int merge_idx,
1028  MvField *mv, int mvp_lx_flag, int LX);
1029 void ff_hevc_set_qPy(HEVCContext *s, int xC, int yC, int xBase, int yBase,
1030  int log2_cb_size);
1032  int log2_trafo_size);
1037 void ff_hevc_hls_filter(HEVCContext *s, int x, int y, int ctb_size);
1038 void ff_hevc_hls_filters(HEVCContext *s, int x_ctb, int y_ctb, int ctb_size);
1039 void ff_hevc_hls_residual_coding(HEVCContext *s, int x0, int y0,
1040  int log2_trafo_size, enum ScanType scan_idx,
1041  int c_idx);
1042 
1043 void ff_hevc_hls_mvd_coding(HEVCContext *s, int x0, int y0, int log2_cb_size);
1044 
1045 
1046 extern const uint8_t ff_hevc_qpel_extra_before[4];
1047 extern const uint8_t ff_hevc_qpel_extra_after[4];
1048 extern const uint8_t ff_hevc_qpel_extra[4];
1049 
1050 extern const uint8_t ff_hevc_diag_scan4x4_x[16];
1051 extern const uint8_t ff_hevc_diag_scan4x4_y[16];
1052 extern const uint8_t ff_hevc_diag_scan8x8_x[64];
1053 extern const uint8_t ff_hevc_diag_scan8x8_y[64];
1054 
1055 #endif /* AVCODEC_HEVC_H */