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 "cabac.h"
31 #include "dsputil.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 183
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 
75 #define EDGE_EMU_BUFFER_STRIDE 80
76 
77 /**
78  * Value of the luma sample at position (x, y) in the 2D array tab.
79  */
80 #define SAMPLE(tab, x, y) ((tab)[(y) * s->sps->width + (x)])
81 #define SAMPLE_CTB(tab, x, y) ((tab)[(y) * min_cb_width + (x)])
82 #define SAMPLE_CBF(tab, x, y) ((tab)[((y) & ((1<<log2_trafo_size)-1)) * MAX_CU_SIZE + ((x) & ((1<<log2_trafo_size)-1))])
83 
84 #define IS_IDR(s) (s->nal_unit_type == NAL_IDR_W_RADL || s->nal_unit_type == NAL_IDR_N_LP)
85 #define IS_BLA(s) (s->nal_unit_type == NAL_BLA_W_RADL || s->nal_unit_type == NAL_BLA_W_LP || \
86  s->nal_unit_type == NAL_BLA_N_LP)
87 #define IS_IRAP(s) (s->nal_unit_type >= 16 && s->nal_unit_type <= 23)
88 
89 /**
90  * Table 7-3: NAL unit type codes
91  */
95  NAL_TSA_N = 2,
96  NAL_TSA_R = 3,
109  NAL_VPS = 32,
110  NAL_SPS = 33,
111  NAL_PPS = 34,
112  NAL_AUD = 35,
118 };
119 
120 enum RPSType {
127 };
128 
129 enum SliceType {
130  B_SLICE = 0,
131  P_SLICE = 1,
132  I_SLICE = 2,
133 };
134 
179 };
180 
181 enum PartMode {
185  PART_NxN = 3,
190 };
191 
192 enum PredMode {
196 };
197 
199  PRED_L0 = 0,
202 };
203 
240 };
241 
242 enum SAOType {
246 };
247 
253 };
254 
255 enum ScanType {
259 };
260 
261 typedef struct ShortTermRPS {
262  unsigned int num_negative_pics;
266 } ShortTermRPS;
267 
268 typedef struct LongTermRPS {
269  int poc[32];
272 } LongTermRPS;
273 
274 typedef struct RefPicList {
278  int nb_refs;
279 } RefPicList;
280 
281 typedef struct RefPicListTab {
283 } RefPicListTab;
284 
285 typedef struct HEVCWindow {
290 } HEVCWindow;
291 
292 typedef struct VUI {
294 
297 
305 
310 
313 
316 
319  uint32_t vui_time_scale;
323 
333 } VUI;
334 
335 typedef struct PTLCommon {
345 } PTLCommon;
346 
347 typedef struct PTL {
350 
353 } PTL;
354 
355 typedef struct HEVCVPS {
358  int vps_max_sub_layers; ///< vps_max_temporal_layers_minus1 + 1
359 
366  int vps_num_layer_sets; ///< vps_num_layer_sets_minus1 + 1
369  uint32_t vps_time_scale;
371  int vps_num_ticks_poc_diff_one; ///< vps_num_ticks_poc_diff_one_minus1 + 1
373 } HEVCVPS;
374 
375 typedef struct ScalingList {
376  /* This is a little wasteful, since sizeID 0 only needs 8 coeffs,
377  * and size ID 3 only has 2 arrays, not 6. */
378  uint8_t sl[4][6][64];
379  uint8_t sl_dc[2][6];
380 } ScalingList;
381 
382 typedef struct HEVCSPS {
383  unsigned vps_id;
386 
387  ///< output (i.e. cropped) values
390 
392 
396 
397  unsigned int log2_max_poc_lsb;
399 
401  struct {
406 
409 
412 
413  unsigned int nb_st_rps;
415 
418 
423 
424  struct {
427  unsigned int log2_min_pcm_cb_size;
428  unsigned int log2_max_pcm_cb_size;
430  } pcm;
433 
434  unsigned int log2_min_cb_size;
436  unsigned int log2_min_tb_size;
437  unsigned int log2_max_trafo_size;
438  unsigned int log2_ctb_size;
439  unsigned int log2_min_pu_size;
440 
443 
444  ///< coded frame dimension in various units
445  int width;
446  int height;
449  int ctb_size;
456 
457  int hshift[3];
458  int vshift[3];
459 
461 } HEVCSPS;
462 
463 typedef struct HEVCPPS {
464  unsigned int sps_id; ///< seq_parameter_set_id
465 
467 
469 
470  int num_ref_idx_l0_default_active; ///< num_ref_idx_l0_default_active_minus1 + 1
471  int num_ref_idx_l1_default_active; ///< num_ref_idx_l1_default_active_minus1 + 1
473 
476 
479 
487 
491 
492  int num_tile_columns; ///< num_tile_columns_minus1 + 1
493  int num_tile_rows; ///< num_tile_rows_minus1 + 1
496 
498 
502  int beta_offset; ///< beta_offset_div2 * 2
503  int tc_offset; ///< tc_offset_div2 * 2
504 
507 
509  int log2_parallel_merge_level; ///< log2_parallel_merge_level_minus2 + 2
512 
513  // Inferred parameters
514  unsigned int *column_width; ///< ColumnWidth
515  unsigned int *row_height; ///< RowHeight
516  unsigned int *col_bd; ///< ColBd
517  unsigned int *row_bd; ///< RowBd
518  int *col_idxX;
519 
520  int *ctb_addr_rs_to_ts; ///< CtbAddrRSToTS
521  int *ctb_addr_ts_to_rs; ///< CtbAddrTSToRS
522  int *tile_id; ///< TileId
523  int *tile_pos_rs; ///< TilePosRS
524  int *min_cb_addr_zs; ///< MinCbAddrZS
525  int *min_tb_addr_zs; ///< MinTbAddrZS
526 } HEVCPPS;
527 
528 typedef struct SliceHeader {
529  unsigned int pps_id;
530 
531  ///< address (in raster order) of the first block in the current slice segment
532  unsigned int slice_segment_addr;
533  ///< address (in raster order) of the first block in the current slice
534  unsigned int slice_addr;
535 
537 
539 
544 
545  ///< RPS coded in the slice header itself is stored here
549  unsigned int list_entry_lx[2][32];
550 
554 
555  unsigned int nb_refs[2];
556 
559 
561  uint8_t disable_deblocking_filter_flag; ///< slice_header_disable_deblocking_filter_flag
564 
565  unsigned int collocated_ref_idx;
566 
570 
571  int beta_offset; ///< beta_offset_div2 * 2
572  int tc_offset; ///< tc_offset_div2 * 2
573 
574  unsigned int max_num_merge_cand; ///< 5 - 5_minus_max_num_merge_cand
575 
577  int * offset;
578  int * size;
580 
581  int8_t slice_qp;
582 
585 
586  int16_t luma_weight_l0[16];
587  int16_t chroma_weight_l0[16][2];
588  int16_t chroma_weight_l1[16][2];
589  int16_t luma_weight_l1[16];
590 
591  int16_t luma_offset_l0[16];
592  int16_t chroma_offset_l0[16][2];
593 
594  int16_t luma_offset_l1[16];
595  int16_t chroma_offset_l1[16][2];
596 
598 } SliceHeader;
599 
600 typedef struct CodingTree {
601  int depth; ///< ctDepth
602 } CodingTree;
603 
604 typedef struct CodingUnit {
605  int x;
606  int y;
607 
608  enum PredMode pred_mode; ///< PredMode
609  enum PartMode part_mode; ///< PartMode
610 
612 
614 
615  // Inferred parameters
616  uint8_t intra_split_flag; ///< IntraSplitFlag
617  uint8_t max_trafo_depth; ///< MaxTrafoDepth
619 } CodingUnit;
620 
621 typedef struct Mv {
622  int16_t x; ///< horizontal component of motion vector
623  int16_t y; ///< vertical component of motion vector
624 } Mv;
625 
626 typedef struct MvField {
627  Mv mv[2];
628  int8_t ref_idx[2];
629  int8_t pred_flag[2];
631 } MvField;
632 
633 typedef struct NeighbourAvailable {
636  int cand_up;
641 
642 typedef struct PredictionUnit {
643  int mpm_idx;
650 
651 typedef struct TransformTree {
655 
656  // Inferred parameters
658 } TransformTree;
659 
660 typedef struct TransformUnit {
662 
663  // Inferred parameters;
666 } TransformUnit;
667 
668 typedef struct DBParams {
671 } DBParams;
672 
673 #define HEVC_FRAME_FLAG_OUTPUT (1 << 0)
674 #define HEVC_FRAME_FLAG_SHORT_REF (1 << 1)
675 #define HEVC_FRAME_FLAG_LONG_REF (1 << 2)
676 
677 typedef struct HEVCFrame {
684  int poc;
686 
688 
692 
693  /**
694  * A sequence counter, so that old frames are output first
695  * after a POC reset
696  */
697  uint16_t sequence;
698 
699  /**
700  * A combination of HEVC_FRAME_FLAG_*
701  */
703 } HEVCFrame;
704 
705 typedef struct HEVCNAL {
708 
709  int size;
710  const uint8_t *data;
711 } HEVCNAL;
712 
713 typedef struct HEVCLocalContext {
716 
718 
722 
723  int8_t qp_y;
724  int8_t curr_qp_y;
725 
726  int qPy_pred;
727 
729 
736  /* +7 is for subpixel interpolation, *2 for high bit depths */
742 
746 
747 typedef struct HEVCContext {
748  const AVClass *c; // needed by private avoptions
750 
752 
755 
758 
759  int width;
760  int height;
761 
763 
764  /** 1 if the independent slice segment header was successfully parsed */
766 
771 
772  const HEVCVPS *vps;
773  const HEVCSPS *sps;
774  const HEVCPPS *pps;
778 
781 
782  ///< candidate references for the current frame
784 
789  int temporal_id; ///< temporal_id_plus1 - 1
792  int poc;
793  int pocTid0;
794  int slice_idx; ///< number of the slice being currently decoded
795  int eos; ///< current packet contains an EOS/EOB NAL
796  int max_ra;
797  int bs_width;
799 
801 
806  int8_t *qp_y_tab;
810 
812 
813  // CU
816  // PU
818 
819  uint8_t *cbf_luma; // cbf_luma of colocated TU
821 
822  // CTB-level flags affecting loop filter operation
824 
825  /** used on BE to byteswap the lines for checksumming */
828 
829  /**
830  * Sequence counters for decoded and output frames, so that old
831  * frames are output first after a POC reset
832  */
833  uint16_t seq_decode;
834  uint16_t seq_output;
835 
837  int wpp_err;
841 
845 
847 
849  int nb_nals;
851  // type of the first VCL NAL of the current frame
853 
854  // for checking the frame checksums
855  struct AVMD5 *md5_ctx;
856  uint8_t md5[3][16];
858 
860  uint8_t is_nalff; ///< this flag is != 0 if bitstream is encapsulated
861  ///< as a format defined in 14496-15
863 
865 
866  int nal_length_size; ///< Number of bytes used for nal length (1, 2 or 4)
868 
869  /** frame packing arrangement variables */
874 
876 } HEVCContext;
877 
879  const HEVCSPS *sps, int is_slice_header);
884 
886  HEVCNAL *nal);
887 
888 /**
889  * Mark all frames in DPB as unused for reference.
890  */
892 
893 /**
894  * Drop all frames currently in DPB.
895  */
897 
898 /**
899  * Compute POC of the current frame and return it.
900  */
901 int ff_hevc_compute_poc(HEVCContext *s, int poc_lsb);
902 
904  int x0, int y0);
905 
906 /**
907  * Construct the reference picture sets for the current frame.
908  */
910 
911 /**
912  * Construct the reference picture list(s) for the current slice.
913  */
915 
916 void ff_hevc_save_states(HEVCContext *s, int ctb_addr_ts);
917 void ff_hevc_cabac_init(HEVCContext *s, int ctb_addr_ts);
926 int ff_hevc_skip_flag_decode(HEVCContext *s, int x0, int y0,
927  int x_cb, int y_cb);
930  int x0, int y0);
931 int ff_hevc_part_mode_decode(HEVCContext *s, int log2_cb_size);
939 int ff_hevc_inter_pred_idc_decode(HEVCContext *s, int nPbW, int nPbH);
940 int ff_hevc_ref_idx_lx_decode(HEVCContext *s, int num_ref_idx_lx);
943 int ff_hevc_split_transform_flag_decode(HEVCContext *s, int log2_trafo_size);
944 int ff_hevc_cbf_cb_cr_decode(HEVCContext *s, int trafo_depth);
945 int ff_hevc_cbf_luma_decode(HEVCContext *s, int trafo_depth);
947 
948 /**
949  * Get the number of candidate references for the current frame.
950  */
952 
953 int ff_hevc_set_new_ref(HEVCContext *s, AVFrame **frame, int poc);
954 
955 /**
956  * Find next frame in output order and put a reference to it in frame.
957  * @return 1 if a frame was output, 0 otherwise
958  */
960 
962 
963 void ff_hevc_set_neighbour_available(HEVCContext *s, int x0, int y0,
964  int nPbW, int nPbH);
965 void ff_hevc_luma_mv_merge_mode(HEVCContext *s, int x0, int y0,
966  int nPbW, int nPbH, int log2_cb_size,
967  int part_idx, int merge_idx, MvField *mv);
968 void ff_hevc_luma_mv_mvp_mode(HEVCContext *s, int x0, int y0,
969  int nPbW, int nPbH, int log2_cb_size,
970  int part_idx, int merge_idx,
971  MvField *mv, int mvp_lx_flag, int LX);
972 void ff_hevc_set_qPy(HEVCContext *s, int xC, int yC, int xBase, int yBase,
973  int log2_cb_size);
975  int log2_trafo_size,
976  int slice_or_tiles_up_boundary,
977  int slice_or_tiles_left_boundary);
980 void ff_hevc_hls_filter(HEVCContext *s, int x, int y);
981 void ff_hevc_hls_filters(HEVCContext *s, int x_ctb, int y_ctb, int ctb_size);
982 void ff_hevc_hls_residual_coding(HEVCContext *s, int x0, int y0,
983  int log2_trafo_size, enum ScanType scan_idx,
984  int c_idx);
985 
986 void ff_hevc_hls_mvd_coding(HEVCContext *s, int x0, int y0, int log2_cb_size);
987 
988 
989 extern const uint8_t ff_hevc_qpel_extra_before[4];
990 extern const uint8_t ff_hevc_qpel_extra_after[4];
991 extern const uint8_t ff_hevc_qpel_extra[4];
992 
993 extern const uint8_t ff_hevc_diag_scan4x4_x[16];
994 extern const uint8_t ff_hevc_diag_scan4x4_y[16];
995 extern const uint8_t ff_hevc_diag_scan8x8_x[64];
996 extern const uint8_t ff_hevc_diag_scan8x8_y[64];
997 
998 #endif /* AVCODEC_HEVC_H */