]> git.sesse.net Git - ffmpeg/blob - libavcodec/hevcdec.h
Merge commit 'e16b20782a597e36a9c7488487c3179375a25b97'
[ffmpeg] / libavcodec / hevcdec.h
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_HEVCDEC_H
24 #define AVCODEC_HEVCDEC_H
25
26 #include <stdatomic.h>
27
28 #include "libavutil/buffer.h"
29 #include "libavutil/md5.h"
30
31 #include "avcodec.h"
32 #include "bswapdsp.h"
33 #include "cabac.h"
34 #include "get_bits.h"
35 #include "hevcpred.h"
36 #include "h2645_parse.h"
37 #include "hevc.h"
38 #include "hevc_ps.h"
39 #include "hevc_sei.h"
40 #include "hevcdsp.h"
41 #include "internal.h"
42 #include "thread.h"
43 #include "videodsp.h"
44
45 #define MAX_NB_THREADS 16
46 #define SHIFT_CTB_WPP 2
47
48 //TODO: check if this is really the maximum
49 #define MAX_TRANSFORM_DEPTH 5
50
51 #define MAX_TB_SIZE 32
52 #define MAX_QP 51
53 #define DEFAULT_INTRA_TC_OFFSET 2
54
55 #define HEVC_CONTEXTS 199
56
57 #define MRG_MAX_NUM_CANDS     5
58
59 #define L0 0
60 #define L1 1
61
62 #define EPEL_EXTRA_BEFORE 1
63 #define EPEL_EXTRA_AFTER  2
64 #define EPEL_EXTRA        3
65 #define QPEL_EXTRA_BEFORE 3
66 #define QPEL_EXTRA_AFTER  4
67 #define QPEL_EXTRA        7
68
69 #define EDGE_EMU_BUFFER_STRIDE 80
70
71 /**
72  * Value of the luma sample at position (x, y) in the 2D array tab.
73  */
74 #define SAMPLE(tab, x, y) ((tab)[(y) * s->sps->width + (x)])
75 #define SAMPLE_CTB(tab, x, y) ((tab)[(y) * min_cb_width + (x)])
76
77 #define IS_IDR(s) ((s)->nal_unit_type == HEVC_NAL_IDR_W_RADL || (s)->nal_unit_type == HEVC_NAL_IDR_N_LP)
78 #define IS_BLA(s) ((s)->nal_unit_type == HEVC_NAL_BLA_W_RADL || (s)->nal_unit_type == HEVC_NAL_BLA_W_LP || \
79                    (s)->nal_unit_type == HEVC_NAL_BLA_N_LP)
80 #define IS_IRAP(s) ((s)->nal_unit_type >= 16 && (s)->nal_unit_type <= 23)
81
82 enum RPSType {
83     ST_CURR_BEF = 0,
84     ST_CURR_AFT,
85     ST_FOLL,
86     LT_CURR,
87     LT_FOLL,
88     NB_RPS_TYPE,
89 };
90
91 enum SyntaxElement {
92     SAO_MERGE_FLAG = 0,
93     SAO_TYPE_IDX,
94     SAO_EO_CLASS,
95     SAO_BAND_POSITION,
96     SAO_OFFSET_ABS,
97     SAO_OFFSET_SIGN,
98     END_OF_SLICE_FLAG,
99     SPLIT_CODING_UNIT_FLAG,
100     CU_TRANSQUANT_BYPASS_FLAG,
101     SKIP_FLAG,
102     CU_QP_DELTA,
103     PRED_MODE_FLAG,
104     PART_MODE,
105     PCM_FLAG,
106     PREV_INTRA_LUMA_PRED_FLAG,
107     MPM_IDX,
108     REM_INTRA_LUMA_PRED_MODE,
109     INTRA_CHROMA_PRED_MODE,
110     MERGE_FLAG,
111     MERGE_IDX,
112     INTER_PRED_IDC,
113     REF_IDX_L0,
114     REF_IDX_L1,
115     ABS_MVD_GREATER0_FLAG,
116     ABS_MVD_GREATER1_FLAG,
117     ABS_MVD_MINUS2,
118     MVD_SIGN_FLAG,
119     MVP_LX_FLAG,
120     NO_RESIDUAL_DATA_FLAG,
121     SPLIT_TRANSFORM_FLAG,
122     CBF_LUMA,
123     CBF_CB_CR,
124     TRANSFORM_SKIP_FLAG,
125     EXPLICIT_RDPCM_FLAG,
126     EXPLICIT_RDPCM_DIR_FLAG,
127     LAST_SIGNIFICANT_COEFF_X_PREFIX,
128     LAST_SIGNIFICANT_COEFF_Y_PREFIX,
129     LAST_SIGNIFICANT_COEFF_X_SUFFIX,
130     LAST_SIGNIFICANT_COEFF_Y_SUFFIX,
131     SIGNIFICANT_COEFF_GROUP_FLAG,
132     SIGNIFICANT_COEFF_FLAG,
133     COEFF_ABS_LEVEL_GREATER1_FLAG,
134     COEFF_ABS_LEVEL_GREATER2_FLAG,
135     COEFF_ABS_LEVEL_REMAINING,
136     COEFF_SIGN_FLAG,
137     LOG2_RES_SCALE_ABS,
138     RES_SCALE_SIGN_FLAG,
139     CU_CHROMA_QP_OFFSET_FLAG,
140     CU_CHROMA_QP_OFFSET_IDX,
141 };
142
143 enum PartMode {
144     PART_2Nx2N = 0,
145     PART_2NxN  = 1,
146     PART_Nx2N  = 2,
147     PART_NxN   = 3,
148     PART_2NxnU = 4,
149     PART_2NxnD = 5,
150     PART_nLx2N = 6,
151     PART_nRx2N = 7,
152 };
153
154 enum PredMode {
155     MODE_INTER = 0,
156     MODE_INTRA,
157     MODE_SKIP,
158 };
159
160 enum InterPredIdc {
161     PRED_L0 = 0,
162     PRED_L1,
163     PRED_BI,
164 };
165
166 enum PredFlag {
167     PF_INTRA = 0,
168     PF_L0,
169     PF_L1,
170     PF_BI,
171 };
172
173 enum IntraPredMode {
174     INTRA_PLANAR = 0,
175     INTRA_DC,
176     INTRA_ANGULAR_2,
177     INTRA_ANGULAR_3,
178     INTRA_ANGULAR_4,
179     INTRA_ANGULAR_5,
180     INTRA_ANGULAR_6,
181     INTRA_ANGULAR_7,
182     INTRA_ANGULAR_8,
183     INTRA_ANGULAR_9,
184     INTRA_ANGULAR_10,
185     INTRA_ANGULAR_11,
186     INTRA_ANGULAR_12,
187     INTRA_ANGULAR_13,
188     INTRA_ANGULAR_14,
189     INTRA_ANGULAR_15,
190     INTRA_ANGULAR_16,
191     INTRA_ANGULAR_17,
192     INTRA_ANGULAR_18,
193     INTRA_ANGULAR_19,
194     INTRA_ANGULAR_20,
195     INTRA_ANGULAR_21,
196     INTRA_ANGULAR_22,
197     INTRA_ANGULAR_23,
198     INTRA_ANGULAR_24,
199     INTRA_ANGULAR_25,
200     INTRA_ANGULAR_26,
201     INTRA_ANGULAR_27,
202     INTRA_ANGULAR_28,
203     INTRA_ANGULAR_29,
204     INTRA_ANGULAR_30,
205     INTRA_ANGULAR_31,
206     INTRA_ANGULAR_32,
207     INTRA_ANGULAR_33,
208     INTRA_ANGULAR_34,
209 };
210
211 enum SAOType {
212     SAO_NOT_APPLIED = 0,
213     SAO_BAND,
214     SAO_EDGE,
215     SAO_APPLIED
216 };
217
218 enum SAOEOClass {
219     SAO_EO_HORIZ = 0,
220     SAO_EO_VERT,
221     SAO_EO_135D,
222     SAO_EO_45D,
223 };
224
225 enum ScanType {
226     SCAN_DIAG = 0,
227     SCAN_HORIZ,
228     SCAN_VERT,
229 };
230
231 typedef struct RefPicList {
232     struct HEVCFrame *ref[HEVC_MAX_REFS];
233     int list[HEVC_MAX_REFS];
234     int isLongTerm[HEVC_MAX_REFS];
235     int nb_refs;
236 } RefPicList;
237
238 typedef struct RefPicListTab {
239     RefPicList refPicList[2];
240 } RefPicListTab;
241
242 typedef struct CodingUnit {
243     int x;
244     int y;
245
246     enum PredMode pred_mode;    ///< PredMode
247     enum PartMode part_mode;    ///< PartMode
248
249     // Inferred parameters
250     uint8_t intra_split_flag;   ///< IntraSplitFlag
251     uint8_t max_trafo_depth;    ///< MaxTrafoDepth
252     uint8_t cu_transquant_bypass_flag;
253 } CodingUnit;
254
255 typedef struct Mv {
256     int16_t x;  ///< horizontal component of motion vector
257     int16_t y;  ///< vertical component of motion vector
258 } Mv;
259
260 typedef struct MvField {
261     DECLARE_ALIGNED(4, Mv, mv)[2];
262     int8_t ref_idx[2];
263     int8_t pred_flag;
264 } MvField;
265
266 typedef struct NeighbourAvailable {
267     int cand_bottom_left;
268     int cand_left;
269     int cand_up;
270     int cand_up_left;
271     int cand_up_right;
272     int cand_up_right_sap;
273 } NeighbourAvailable;
274
275 typedef struct PredictionUnit {
276     int mpm_idx;
277     int rem_intra_luma_pred_mode;
278     uint8_t intra_pred_mode[4];
279     Mv mvd;
280     uint8_t merge_flag;
281     uint8_t intra_pred_mode_c[4];
282     uint8_t chroma_mode_c[4];
283 } PredictionUnit;
284
285 typedef struct TransformUnit {
286     int cu_qp_delta;
287
288     int res_scale_val;
289
290     // Inferred parameters;
291     int intra_pred_mode;
292     int intra_pred_mode_c;
293     int chroma_mode_c;
294     uint8_t is_cu_qp_delta_coded;
295     uint8_t is_cu_chroma_qp_offset_coded;
296     int8_t  cu_qp_offset_cb;
297     int8_t  cu_qp_offset_cr;
298     uint8_t cross_pf;
299 } TransformUnit;
300
301 typedef struct DBParams {
302     int beta_offset;
303     int tc_offset;
304 } DBParams;
305
306 #define HEVC_FRAME_FLAG_OUTPUT    (1 << 0)
307 #define HEVC_FRAME_FLAG_SHORT_REF (1 << 1)
308 #define HEVC_FRAME_FLAG_LONG_REF  (1 << 2)
309 #define HEVC_FRAME_FLAG_BUMPING   (1 << 3)
310
311 typedef struct HEVCFrame {
312     AVFrame *frame;
313     ThreadFrame tf;
314     MvField *tab_mvf;
315     RefPicList *refPicList;
316     RefPicListTab **rpl_tab;
317     int ctb_count;
318     int poc;
319     struct HEVCFrame *collocated_ref;
320
321     AVBufferRef *tab_mvf_buf;
322     AVBufferRef *rpl_tab_buf;
323     AVBufferRef *rpl_buf;
324
325     AVBufferRef *hwaccel_priv_buf;
326     void *hwaccel_picture_private;
327
328     /**
329      * A sequence counter, so that old frames are output first
330      * after a POC reset
331      */
332     uint16_t sequence;
333
334     /**
335      * A combination of HEVC_FRAME_FLAG_*
336      */
337     uint8_t flags;
338 } HEVCFrame;
339
340 typedef struct HEVCLocalContext {
341     uint8_t cabac_state[HEVC_CONTEXTS];
342
343     uint8_t stat_coeff[4];
344
345     uint8_t first_qp_group;
346
347     GetBitContext gb;
348     CABACContext cc;
349
350     int8_t qp_y;
351     int8_t curr_qp_y;
352
353     int qPy_pred;
354
355     TransformUnit tu;
356
357     uint8_t ctb_left_flag;
358     uint8_t ctb_up_flag;
359     uint8_t ctb_up_right_flag;
360     uint8_t ctb_up_left_flag;
361     int     end_of_tiles_x;
362     int     end_of_tiles_y;
363     /* +7 is for subpixel interpolation, *2 for high bit depths */
364     DECLARE_ALIGNED(32, uint8_t, edge_emu_buffer)[(MAX_PB_SIZE + 7) * EDGE_EMU_BUFFER_STRIDE * 2];
365     /* The extended size between the new edge emu buffer is abused by SAO */
366     DECLARE_ALIGNED(32, uint8_t, edge_emu_buffer2)[(MAX_PB_SIZE + 7) * EDGE_EMU_BUFFER_STRIDE * 2];
367     DECLARE_ALIGNED(32, int16_t, tmp)[MAX_PB_SIZE * MAX_PB_SIZE];
368
369     int ct_depth;
370     CodingUnit cu;
371     PredictionUnit pu;
372     NeighbourAvailable na;
373
374 #define BOUNDARY_LEFT_SLICE     (1 << 0)
375 #define BOUNDARY_LEFT_TILE      (1 << 1)
376 #define BOUNDARY_UPPER_SLICE    (1 << 2)
377 #define BOUNDARY_UPPER_TILE     (1 << 3)
378     /* properties of the boundary of the current CTB for the purposes
379      * of the deblocking filter */
380     int boundary_flags;
381 } HEVCLocalContext;
382
383 typedef struct HEVCContext {
384     const AVClass *c;  // needed by private avoptions
385     AVCodecContext *avctx;
386
387     struct HEVCContext  *sList[MAX_NB_THREADS];
388
389     HEVCLocalContext    *HEVClcList[MAX_NB_THREADS];
390     HEVCLocalContext    *HEVClc;
391
392     uint8_t             threads_type;
393     uint8_t             threads_number;
394
395     int                 width;
396     int                 height;
397
398     uint8_t *cabac_state;
399
400     /** 1 if the independent slice segment header was successfully parsed */
401     uint8_t slice_initialized;
402
403     AVFrame *frame;
404     AVFrame *output_frame;
405     uint8_t *sao_pixel_buffer_h[3];
406     uint8_t *sao_pixel_buffer_v[3];
407
408     HEVCParamSets ps;
409     HEVCSEI sei;
410     struct AVMD5 *md5_ctx;
411
412     AVBufferPool *tab_mvf_pool;
413     AVBufferPool *rpl_tab_pool;
414
415     ///< candidate references for the current frame
416     RefPicList rps[5];
417
418     SliceHeader sh;
419     SAOParams *sao;
420     DBParams *deblock;
421     enum HEVCNALUnitType nal_unit_type;
422     int temporal_id;  ///< temporal_id_plus1 - 1
423     HEVCFrame *ref;
424     HEVCFrame DPB[32];
425     int poc;
426     int pocTid0;
427     int slice_idx; ///< number of the slice being currently decoded
428     int eos;       ///< current packet contains an EOS/EOB NAL
429     int last_eos;  ///< last packet contains an EOS/EOB NAL
430     int max_ra;
431     int bs_width;
432     int bs_height;
433
434     int is_decoded;
435     int no_rasl_output_flag;
436
437     HEVCPredContext hpc;
438     HEVCDSPContext hevcdsp;
439     VideoDSPContext vdsp;
440     BswapDSPContext bdsp;
441     int8_t *qp_y_tab;
442     uint8_t *horizontal_bs;
443     uint8_t *vertical_bs;
444
445     int32_t *tab_slice_address;
446
447     //  CU
448     uint8_t *skip_flag;
449     uint8_t *tab_ct_depth;
450     // PU
451     uint8_t *tab_ipm;
452
453     uint8_t *cbf_luma; // cbf_luma of colocated TU
454     uint8_t *is_pcm;
455
456     // CTB-level flags affecting loop filter operation
457     uint8_t *filter_slice_edges;
458
459     /** used on BE to byteswap the lines for checksumming */
460     uint8_t *checksum_buf;
461     int      checksum_buf_size;
462
463     /**
464      * Sequence counters for decoded and output frames, so that old
465      * frames are output first after a POC reset
466      */
467     uint16_t seq_decode;
468     uint16_t seq_output;
469
470     int enable_parallel_tiles;
471     atomic_int wpp_err;
472
473     const uint8_t *data;
474
475     H2645Packet pkt;
476     // type of the first VCL NAL of the current frame
477     enum HEVCNALUnitType first_nal_type;
478
479     uint8_t context_initialized;
480     int is_nalff;           ///< this flag is != 0 if bitstream is encapsulated
481                             ///< as a format defined in 14496-15
482     int apply_defdispwin;
483
484     int nal_length_size;    ///< Number of bytes used for nal length (1, 2 or 4)
485     int nuh_layer_id;
486 } HEVCContext;
487
488 /**
489  * Mark all frames in DPB as unused for reference.
490  */
491 void ff_hevc_clear_refs(HEVCContext *s);
492
493 /**
494  * Drop all frames currently in DPB.
495  */
496 void ff_hevc_flush_dpb(HEVCContext *s);
497
498 RefPicList *ff_hevc_get_ref_list(HEVCContext *s, HEVCFrame *frame,
499                                  int x0, int y0);
500
501 /**
502  * Construct the reference picture sets for the current frame.
503  */
504 int ff_hevc_frame_rps(HEVCContext *s);
505
506 /**
507  * Construct the reference picture list(s) for the current slice.
508  */
509 int ff_hevc_slice_rpl(HEVCContext *s);
510
511 void ff_hevc_save_states(HEVCContext *s, int ctb_addr_ts);
512 int ff_hevc_cabac_init(HEVCContext *s, int ctb_addr_ts);
513 int ff_hevc_sao_merge_flag_decode(HEVCContext *s);
514 int ff_hevc_sao_type_idx_decode(HEVCContext *s);
515 int ff_hevc_sao_band_position_decode(HEVCContext *s);
516 int ff_hevc_sao_offset_abs_decode(HEVCContext *s);
517 int ff_hevc_sao_offset_sign_decode(HEVCContext *s);
518 int ff_hevc_sao_eo_class_decode(HEVCContext *s);
519 int ff_hevc_end_of_slice_flag_decode(HEVCContext *s);
520 int ff_hevc_cu_transquant_bypass_flag_decode(HEVCContext *s);
521 int ff_hevc_skip_flag_decode(HEVCContext *s, int x0, int y0,
522                              int x_cb, int y_cb);
523 int ff_hevc_pred_mode_decode(HEVCContext *s);
524 int ff_hevc_split_coding_unit_flag_decode(HEVCContext *s, int ct_depth,
525                                           int x0, int y0);
526 int ff_hevc_part_mode_decode(HEVCContext *s, int log2_cb_size);
527 int ff_hevc_pcm_flag_decode(HEVCContext *s);
528 int ff_hevc_prev_intra_luma_pred_flag_decode(HEVCContext *s);
529 int ff_hevc_mpm_idx_decode(HEVCContext *s);
530 int ff_hevc_rem_intra_luma_pred_mode_decode(HEVCContext *s);
531 int ff_hevc_intra_chroma_pred_mode_decode(HEVCContext *s);
532 int ff_hevc_merge_idx_decode(HEVCContext *s);
533 int ff_hevc_merge_flag_decode(HEVCContext *s);
534 int ff_hevc_inter_pred_idc_decode(HEVCContext *s, int nPbW, int nPbH);
535 int ff_hevc_ref_idx_lx_decode(HEVCContext *s, int num_ref_idx_lx);
536 int ff_hevc_mvp_lx_flag_decode(HEVCContext *s);
537 int ff_hevc_no_residual_syntax_flag_decode(HEVCContext *s);
538 int ff_hevc_split_transform_flag_decode(HEVCContext *s, int log2_trafo_size);
539 int ff_hevc_cbf_cb_cr_decode(HEVCContext *s, int trafo_depth);
540 int ff_hevc_cbf_luma_decode(HEVCContext *s, int trafo_depth);
541 int ff_hevc_log2_res_scale_abs(HEVCContext *s, int idx);
542 int ff_hevc_res_scale_sign_flag(HEVCContext *s, int idx);
543
544 /**
545  * Get the number of candidate references for the current frame.
546  */
547 int ff_hevc_frame_nb_refs(const HEVCContext *s);
548
549 int ff_hevc_set_new_ref(HEVCContext *s, AVFrame **frame, int poc);
550
551 static av_always_inline int ff_hevc_nal_is_nonref(enum HEVCNALUnitType type)
552 {
553     switch (type) {
554     case HEVC_NAL_TRAIL_N:
555     case HEVC_NAL_TSA_N:
556     case HEVC_NAL_STSA_N:
557     case HEVC_NAL_RADL_N:
558     case HEVC_NAL_RASL_N:
559     case HEVC_NAL_VCL_N10:
560     case HEVC_NAL_VCL_N12:
561     case HEVC_NAL_VCL_N14:
562     case HEVC_NAL_BLA_N_LP:
563     case HEVC_NAL_IDR_N_LP:
564         return 1;
565         break;
566     default: break;
567     }
568     return 0;
569 }
570
571 /**
572  * Find next frame in output order and put a reference to it in frame.
573  * @return 1 if a frame was output, 0 otherwise
574  */
575 int ff_hevc_output_frame(HEVCContext *s, AVFrame *frame, int flush);
576
577 void ff_hevc_bump_frame(HEVCContext *s);
578
579 void ff_hevc_unref_frame(HEVCContext *s, HEVCFrame *frame, int flags);
580
581 void ff_hevc_set_neighbour_available(HEVCContext *s, int x0, int y0,
582                                      int nPbW, int nPbH);
583 void ff_hevc_luma_mv_merge_mode(HEVCContext *s, int x0, int y0,
584                                 int nPbW, int nPbH, int log2_cb_size,
585                                 int part_idx, int merge_idx, MvField *mv);
586 void ff_hevc_luma_mv_mvp_mode(HEVCContext *s, int x0, int y0,
587                               int nPbW, int nPbH, int log2_cb_size,
588                               int part_idx, int merge_idx,
589                               MvField *mv, int mvp_lx_flag, int LX);
590 void ff_hevc_set_qPy(HEVCContext *s, int xBase, int yBase,
591                      int log2_cb_size);
592 void ff_hevc_deblocking_boundary_strengths(HEVCContext *s, int x0, int y0,
593                                            int log2_trafo_size);
594 int ff_hevc_cu_qp_delta_sign_flag(HEVCContext *s);
595 int ff_hevc_cu_qp_delta_abs(HEVCContext *s);
596 int ff_hevc_cu_chroma_qp_offset_flag(HEVCContext *s);
597 int ff_hevc_cu_chroma_qp_offset_idx(HEVCContext *s);
598 void ff_hevc_hls_filter(HEVCContext *s, int x, int y, int ctb_size);
599 void ff_hevc_hls_filters(HEVCContext *s, int x_ctb, int y_ctb, int ctb_size);
600 void ff_hevc_hls_residual_coding(HEVCContext *s, int x0, int y0,
601                                  int log2_trafo_size, enum ScanType scan_idx,
602                                  int c_idx);
603
604 void ff_hevc_hls_mvd_coding(HEVCContext *s, int x0, int y0, int log2_cb_size);
605
606 extern const uint8_t ff_hevc_qpel_extra_before[4];
607 extern const uint8_t ff_hevc_qpel_extra_after[4];
608 extern const uint8_t ff_hevc_qpel_extra[4];
609
610 #endif /* AVCODEC_HEVCDEC_H */