Libav
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 Libav.
7  *
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.
12  *
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.
17  *
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
21  */
22 
23 #ifndef AVCODEC_HEVC_H
24 #define AVCODEC_HEVC_H
25 
26 #include <stddef.h>
27 #include <stdint.h>
28 
29 #include "libavutil/buffer.h"
30 #include "libavutil/md5.h"
31 
32 #include "avcodec.h"
33 #include "cabac.h"
34 #include "dsputil.h"
35 #include "get_bits.h"
36 #include "hevcdsp.h"
37 #include "internal.h"
38 #include "thread.h"
39 #include "videodsp.h"
40 
41 #define MAX_DPB_SIZE 16 // A.4.1
42 #define MAX_REFS 16
43 
47 #define MAX_SUB_LAYERS 7
48 #define MAX_VPS_COUNT 16
49 #define MAX_SPS_COUNT 32
50 #define MAX_PPS_COUNT 256
51 #define MAX_SHORT_TERM_RPS_COUNT 64
52 #define MAX_CU_SIZE 128
53 
54 //TODO: check if this is really the maximum
55 #define MAX_TRANSFORM_DEPTH 5
56 
57 #define MAX_TB_SIZE 32
58 #define MAX_PB_SIZE 64
59 #define MAX_LOG2_CTB_SIZE 6
60 #define MAX_QP 51
61 #define DEFAULT_INTRA_TC_OFFSET 2
62 
63 #define HEVC_CONTEXTS 183
64 
65 #define MRG_MAX_NUM_CANDS 5
66 
67 #define L0 0
68 #define L1 1
69 
70 #define EPEL_EXTRA_BEFORE 1
71 #define EPEL_EXTRA_AFTER 2
72 #define EPEL_EXTRA 3
73 
74 #define EDGE_EMU_BUFFER_STRIDE 80
75 
79 #define SAMPLE(tab, x, y) ((tab)[(y) * s->sps->width + (x)])
80 #define SAMPLE_CTB(tab, x, y) ((tab)[(y) * min_cb_width + (x)])
81 #define SAMPLE_CBF(tab, x, y) ((tab)[((y) & ((1<<log2_trafo_size)-1)) * MAX_CU_SIZE + ((x) & ((1<<log2_trafo_size)-1))])
82 
83 #define IS_IDR(s) (s->nal_unit_type == NAL_IDR_W_RADL || s->nal_unit_type == NAL_IDR_N_LP)
84 #define IS_BLA(s) (s->nal_unit_type == NAL_BLA_W_RADL || s->nal_unit_type == NAL_BLA_W_LP || \
85  s->nal_unit_type == NAL_BLA_N_LP)
86 #define IS_IRAP(s) (s->nal_unit_type >= 16 && s->nal_unit_type <= 23)
87 
88 #define FFUDIV(a,b) (((a) > 0 ? (a) : (a) - (b) + 1) / (b))
89 #define FFUMOD(a,b) ((a) - (b) * FFUDIV(a,b))
90 
97  NAL_TSA_N = 2,
98  NAL_TSA_R = 3,
111  NAL_VPS = 32,
112  NAL_SPS = 33,
113  NAL_PPS = 34,
114  NAL_AUD = 35,
120 };
121 
122 enum RPSType {
129 };
130 
131 enum SliceType {
132  B_SLICE = 0,
133  P_SLICE = 1,
134  I_SLICE = 2,
135 };
136 
181 };
182 
183 enum PartMode {
187  PART_NxN = 3,
192 };
193 
194 enum PredMode {
198 };
199 
201  PRED_L0 = 0,
204 };
205 
242 };
243 
244 enum SAOType {
248 };
249 
255 };
256 
257 enum ScanType {
261 };
262 
263 typedef struct ShortTermRPS {
264  unsigned int num_negative_pics;
268 } ShortTermRPS;
269 
270 typedef struct LongTermRPS {
271  int poc[32];
274 } LongTermRPS;
275 
276 typedef struct RefPicList {
280  int nb_refs;
281 } RefPicList;
282 
283 typedef struct RefPicListTab {
285 } RefPicListTab;
286 
287 typedef struct HEVCWindow {
292 } HEVCWindow;
293 
294 typedef struct VUI {
296 
299 
307 
312 
315 
318 
321  uint32_t vui_time_scale;
325 
335 } VUI;
336 
337 typedef struct PTLCommon {
347 } PTLCommon;
348 
349 typedef struct PTL {
352 
355 } PTL;
356 
357 typedef struct HEVCVPS {
361 
371  uint32_t vps_time_scale;
375 } HEVCVPS;
376 
377 typedef struct ScalingList {
378  /* This is a little wasteful, since sizeID 0 only needs 8 coeffs,
379  * and size ID 3 only has 2 arrays, not 6. */
380  uint8_t sl[4][6][64];
381  uint8_t sl_dc[2][6];
382 } ScalingList;
383 
384 typedef struct HEVCSPS {
385  int vps_id;
388 
392 
394 
398 
399  unsigned int log2_max_poc_lsb;
401 
403  struct {
408 
411 
414 
415  unsigned int nb_st_rps;
417 
420 
425 
426  struct {
429  unsigned int log2_min_pcm_cb_size;
430  unsigned int log2_max_pcm_cb_size;
432  } pcm;
435 
436  unsigned int log2_min_cb_size;
438  unsigned int log2_min_tb_size;
439  unsigned int log2_max_trafo_size;
440  unsigned int log2_ctb_size;
441  unsigned int log2_min_pu_size;
442 
445 
447  int width;
448  int height;
451  int ctb_size;
458 
459  int hshift[3];
460  int vshift[3];
461 
463 } HEVCSPS;
464 
465 typedef struct HEVCPPS {
466  unsigned int sps_id;
467 
469 
471 
475 
478 
481 
489 
493 
498 
500 
505  int tc_offset;
506 
509 
514 
515  // Inferred parameters
516  unsigned int *column_width;
517  unsigned int *row_height;
518  unsigned int *col_bd;
519  unsigned int *row_bd;
520  int *col_idxX;
521 
524  int *tile_id;
525  int *tile_pos_rs;
528 } HEVCPPS;
529 
530 typedef struct SliceHeader {
531  unsigned int pps_id;
532 
534  unsigned int slice_segment_addr;
536  unsigned int slice_addr;
537 
539 
541 
546 
551  unsigned int list_entry_lx[2][32];
552 
556 
557  unsigned int nb_refs[2];
558 
561 
566 
567  unsigned int collocated_ref_idx;
568 
572 
574  int tc_offset;
575 
576  unsigned int max_num_merge_cand;
577 
579 
580  int8_t slice_qp;
581 
584 
585  int16_t luma_weight_l0[16];
586  int16_t chroma_weight_l0[16][2];
587  int16_t chroma_weight_l1[16][2];
588  int16_t luma_weight_l1[16];
589 
590  int16_t luma_offset_l0[16];
591  int16_t chroma_offset_l0[16][2];
592 
593  int16_t luma_offset_l1[16];
594  int16_t chroma_offset_l1[16][2];
595 
597 } SliceHeader;
598 
599 typedef struct CodingTree {
600  int depth;
601 } CodingTree;
602 
603 typedef struct CodingUnit {
604  int x;
605  int y;
606 
609 
611 
613 
614  // Inferred parameters
618 } CodingUnit;
619 
620 typedef struct Mv {
621  int16_t x;
622  int16_t y;
623 } Mv;
624 
625 typedef struct MvField {
626  Mv mv[2];
627  int8_t ref_idx[2];
628  int8_t pred_flag[2];
630 } MvField;
631 
632 typedef struct NeighbourAvailable {
635  int cand_up;
640 
641 typedef struct PredictionUnit {
642  int mpm_idx;
649 
650 typedef struct TransformTree {
654 
655  // Inferred parameters
657 } TransformTree;
658 
659 typedef struct TransformUnit {
661 
662  // Inferred parameters;
665 } TransformUnit;
666 
667 typedef struct DBParams {
670 } DBParams;
671 
672 #define HEVC_FRAME_FLAG_OUTPUT (1 << 0)
673 #define HEVC_FRAME_FLAG_SHORT_REF (1 << 1)
674 #define HEVC_FRAME_FLAG_LONG_REF (1 << 2)
675 
676 typedef struct HEVCFrame {
683  int poc;
685 
687 
691 
696  uint16_t sequence;
697 
702 } HEVCFrame;
703 
704 typedef struct HEVCNAL {
707 
708  int size;
709  const uint8_t *data;
710 } HEVCNAL;
711 
712 struct HEVCContext;
713 
714 typedef struct HEVCPredContext {
715  void (*intra_pred)(struct HEVCContext *s, int x0, int y0,
716  int log2_size, int c_idx);
717 
718  void (*pred_planar[4])(uint8_t *src, const uint8_t *top,
719  const uint8_t *left, ptrdiff_t stride);
720  void (*pred_dc)(uint8_t *src, const uint8_t *top, const uint8_t *left,
721  ptrdiff_t stride, int log2_size, int c_idx);
722  void (*pred_angular[4])(uint8_t *src, const uint8_t *top,
723  const uint8_t *left, ptrdiff_t stride,
724  int c_idx, int mode);
726 
727 typedef struct HEVCLocalContext {
730 
732 
736 
737  int8_t qp_y;
738  int8_t curr_qp_y;
739 
741 
749  /* +7 is for subpixel interpolation, *2 for high bit depths */
755 
759 
760 typedef struct HEVCContext {
761  const AVClass *c; // needed by private avoptions
763 
765 
767 
770 
775 
776  const HEVCVPS *vps;
777  const HEVCSPS *sps;
778  const HEVCPPS *pps;
782 
785 
788 
796  int poc;
797  int pocTid0;
798  int slice_idx;
799  int eos;
800  int max_ra;
801  int bs_width;
803 
805 
810  int8_t *qp_y_tab;
814 
816 
817  // CU
820  // PU
822 
823  uint8_t *cbf_luma; // cbf_luma of colocated TU
825 
826  // CTB-level flags affecting loop filter operation
828 
832 
837  uint16_t seq_decode;
838  uint16_t seq_output;
839 
841  int nb_nals;
843  // type of the first VCL NAL of the current frame
845 
846  // for checking the frame checksums
847  struct AVMD5 *md5_ctx;
848  uint8_t md5[3][16];
850 
853 
855 
858 
864 } HEVCContext;
865 
867  const HEVCSPS *sps, int is_slice_header);
872 
877 
882 
886 int ff_hevc_compute_poc(HEVCContext *s, int poc_lsb);
887 
889  int x0, int y0);
890 
895 
900 
901 void ff_hevc_save_states(HEVCContext *s, int ctb_addr_ts);
902 void ff_hevc_cabac_init(HEVCContext *s, int ctb_addr_ts);
911 int ff_hevc_skip_flag_decode(HEVCContext *s, int x0, int y0,
912  int x_cb, int y_cb);
915  int x0, int y0);
916 int ff_hevc_part_mode_decode(HEVCContext *s, int log2_cb_size);
924 int ff_hevc_inter_pred_idc_decode(HEVCContext *s, int nPbW, int nPbH);
925 int ff_hevc_ref_idx_lx_decode(HEVCContext *s, int num_ref_idx_lx);
932 int ff_hevc_split_transform_flag_decode(HEVCContext *s, int log2_trafo_size);
933 int ff_hevc_cbf_cb_cr_decode(HEVCContext *s, int trafo_depth);
934 int ff_hevc_cbf_luma_decode(HEVCContext *s, int trafo_depth);
937  int log2_size);
939  int log2_size);
941  int last_significant_coeff_prefix);
943  int ctx_cg);
944 int ff_hevc_significant_coeff_flag_decode(HEVCContext *s, int c_idx, int x_c,
945  int y_c, int log2_trafo_size,
946  int scan_idx, int prev_sig);
948  int ctx_set);
950  int inc);
951 int ff_hevc_coeff_abs_level_remaining(HEVCContext *s, int base_level,
952  int rc_rice_param);
954 
959 
960 int ff_hevc_set_new_ref(HEVCContext *s, AVFrame **frame, int poc);
961 
966 int ff_hevc_output_frame(HEVCContext *s, AVFrame *frame, int flush);
967 
968 void ff_hevc_unref_frame(HEVCContext *s, HEVCFrame *frame, int flags);
969 
970 void ff_hevc_set_neighbour_available(HEVCContext *s, int x0, int y0,
971  int nPbW, int nPbH);
972 void ff_hevc_luma_mv_merge_mode(HEVCContext *s, int x0, int y0,
973  int nPbW, int nPbH, int log2_cb_size,
974  int part_idx, int merge_idx, MvField *mv);
975 void ff_hevc_luma_mv_mvp_mode(HEVCContext *s, int x0, int y0,
976  int nPbW, int nPbH, int log2_cb_size,
977  int part_idx, int merge_idx,
978  MvField *mv, int mvp_lx_flag, int LX);
979 void ff_hevc_set_qPy(HEVCContext *s, int xC, int yC, int xBase, int yBase,
980  int log2_cb_size);
981 void ff_hevc_deblocking_boundary_strengths(HEVCContext *s, int x0, int y0,
982  int log2_trafo_size,
983  int slice_or_tiles_up_boundary,
984  int slice_or_tiles_left_boundary);
987 void ff_hevc_hls_filter(HEVCContext *s, int x, int y);
988 void ff_hevc_hls_filters(HEVCContext *s, int x_ctb, int y_ctb, int ctb_size);
989 
990 void ff_hevc_pps_free(HEVCPPS **ppps);
991 
992 void ff_hevc_pred_init(HEVCPredContext *hpc, int bit_depth);
993 
994 extern const uint8_t ff_hevc_qpel_extra_before[4];
995 extern const uint8_t ff_hevc_qpel_extra_after[4];
996 extern const uint8_t ff_hevc_qpel_extra[4];
997 
998 extern const uint8_t ff_hevc_diag_scan4x4_x[16];
999 extern const uint8_t ff_hevc_diag_scan4x4_y[16];
1000 extern const uint8_t ff_hevc_diag_scan8x8_x[64];
1001 extern const uint8_t ff_hevc_diag_scan8x8_y[64];
1002 
1003 #endif /* AVCODEC_HEVC_H */