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