]> git.sesse.net Git - ffmpeg/blob - libavcodec/hevc.h
Add HEVC decoder
[ffmpeg] / libavcodec / hevc.h
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 "internal.h"
37 #include "thread.h"
38 #include "videodsp.h"
39
40 #define MAX_DPB_SIZE 16 // A.4.1
41 #define MAX_REFS 16
42
43 /**
44  * 7.4.2.1
45  */
46 #define MAX_SUB_LAYERS 7
47 #define MAX_VPS_COUNT 16
48 #define MAX_SPS_COUNT 32
49 #define MAX_PPS_COUNT 256
50 #define MAX_SHORT_TERM_RPS_COUNT 64
51 #define MAX_CU_SIZE 128
52
53 //TODO: check if this is really the maximum
54 #define MAX_TRANSFORM_DEPTH 5
55
56 #define MAX_TB_SIZE 32
57 #define MAX_PB_SIZE 64
58 #define MAX_LOG2_CTB_SIZE 6
59 #define MAX_QP 51
60 #define DEFAULT_INTRA_TC_OFFSET 2
61
62 #define HEVC_CONTEXTS 183
63
64 #define MRG_MAX_NUM_CANDS     5
65
66 #define L0 0
67 #define L1 1
68
69 #define EPEL_EXTRA_BEFORE 1
70 #define EPEL_EXTRA_AFTER  2
71 #define EPEL_EXTRA        3
72
73 /**
74  * Value of the luma sample at position (x, y) in the 2D array tab.
75  */
76 #define SAMPLE(tab, x, y) ((tab)[(y) * s->sps->width + (x)])
77 #define SAMPLE_CTB(tab, x, y) ((tab)[(y) * min_cb_width + (x)])
78 #define SAMPLE_CBF(tab, x, y) ((tab)[((y) & ((1<<log2_trafo_size)-1)) * MAX_CU_SIZE + ((x) & ((1<<log2_trafo_size)-1))])
79
80 #define IS_IDR(s) (s->nal_unit_type == NAL_IDR_W_RADL || s->nal_unit_type == NAL_IDR_N_LP)
81 #define IS_BLA(s) (s->nal_unit_type == NAL_BLA_W_RADL || s->nal_unit_type == NAL_BLA_W_LP || \
82                    s->nal_unit_type == NAL_BLA_N_LP)
83 #define IS_IRAP(s) (s->nal_unit_type >= 16 && s->nal_unit_type <= 23)
84
85 /**
86  * Table 7-3: NAL unit type codes
87  */
88 enum NALUnitType {
89     NAL_TRAIL_N    = 0,
90     NAL_TRAIL_R    = 1,
91     NAL_TSA_N      = 2,
92     NAL_TSA_R      = 3,
93     NAL_STSA_N     = 4,
94     NAL_STSA_R     = 5,
95     NAL_RADL_N     = 6,
96     NAL_RADL_R     = 7,
97     NAL_RASL_N     = 8,
98     NAL_RASL_R     = 9,
99     NAL_BLA_W_LP   = 16,
100     NAL_BLA_W_RADL = 17,
101     NAL_BLA_N_LP   = 18,
102     NAL_IDR_W_RADL = 19,
103     NAL_IDR_N_LP   = 20,
104     NAL_CRA_NUT    = 21,
105     NAL_VPS        = 32,
106     NAL_SPS        = 33,
107     NAL_PPS        = 34,
108     NAL_AUD        = 35,
109     NAL_EOS_NUT    = 36,
110     NAL_EOB_NUT    = 37,
111     NAL_FD_NUT     = 38,
112     NAL_SEI_PREFIX = 39,
113     NAL_SEI_SUFFIX = 40,
114 };
115
116 enum RPSType {
117     ST_CURR_BEF = 0,
118     ST_CURR_AFT,
119     ST_FOLL,
120     LT_CURR,
121     LT_FOLL,
122     NB_RPS_TYPE,
123 };
124
125 enum SliceType {
126     B_SLICE = 0,
127     P_SLICE = 1,
128     I_SLICE = 2,
129 };
130
131 enum SyntaxElement {
132     SAO_MERGE_FLAG = 0,
133     SAO_TYPE_IDX,
134     SAO_EO_CLASS,
135     SAO_BAND_POSITION,
136     SAO_OFFSET_ABS,
137     SAO_OFFSET_SIGN,
138     END_OF_SLICE_FLAG,
139     SPLIT_CODING_UNIT_FLAG,
140     CU_TRANSQUANT_BYPASS_FLAG,
141     SKIP_FLAG,
142     CU_QP_DELTA,
143     PRED_MODE_FLAG,
144     PART_MODE,
145     PCM_FLAG,
146     PREV_INTRA_LUMA_PRED_FLAG,
147     MPM_IDX,
148     REM_INTRA_LUMA_PRED_MODE,
149     INTRA_CHROMA_PRED_MODE,
150     MERGE_FLAG,
151     MERGE_IDX,
152     INTER_PRED_IDC,
153     REF_IDX_L0,
154     REF_IDX_L1,
155     ABS_MVD_GREATER0_FLAG,
156     ABS_MVD_GREATER1_FLAG,
157     ABS_MVD_MINUS2,
158     MVD_SIGN_FLAG,
159     MVP_LX_FLAG,
160     NO_RESIDUAL_DATA_FLAG,
161     SPLIT_TRANSFORM_FLAG,
162     CBF_LUMA,
163     CBF_CB_CR,
164     TRANSFORM_SKIP_FLAG,
165     LAST_SIGNIFICANT_COEFF_X_PREFIX,
166     LAST_SIGNIFICANT_COEFF_Y_PREFIX,
167     LAST_SIGNIFICANT_COEFF_X_SUFFIX,
168     LAST_SIGNIFICANT_COEFF_Y_SUFFIX,
169     SIGNIFICANT_COEFF_GROUP_FLAG,
170     SIGNIFICANT_COEFF_FLAG,
171     COEFF_ABS_LEVEL_GREATER1_FLAG,
172     COEFF_ABS_LEVEL_GREATER2_FLAG,
173     COEFF_ABS_LEVEL_REMAINING,
174     COEFF_SIGN_FLAG,
175 };
176
177 enum PartMode {
178     PART_2Nx2N = 0,
179     PART_2NxN  = 1,
180     PART_Nx2N  = 2,
181     PART_NxN   = 3,
182     PART_2NxnU = 4,
183     PART_2NxnD = 5,
184     PART_nLx2N = 6,
185     PART_nRx2N = 7,
186 };
187
188 enum PredMode {
189     MODE_INTER = 0,
190     MODE_INTRA,
191     MODE_SKIP,
192 };
193
194 enum InterPredIdc {
195     PRED_L0 = 0,
196     PRED_L1,
197     PRED_BI,
198 };
199
200 enum IntraPredMode {
201     INTRA_PLANAR = 0,
202     INTRA_DC,
203     INTRA_ANGULAR_2,
204     INTRA_ANGULAR_3,
205     INTRA_ANGULAR_4,
206     INTRA_ANGULAR_5,
207     INTRA_ANGULAR_6,
208     INTRA_ANGULAR_7,
209     INTRA_ANGULAR_8,
210     INTRA_ANGULAR_9,
211     INTRA_ANGULAR_10,
212     INTRA_ANGULAR_11,
213     INTRA_ANGULAR_12,
214     INTRA_ANGULAR_13,
215     INTRA_ANGULAR_14,
216     INTRA_ANGULAR_15,
217     INTRA_ANGULAR_16,
218     INTRA_ANGULAR_17,
219     INTRA_ANGULAR_18,
220     INTRA_ANGULAR_19,
221     INTRA_ANGULAR_20,
222     INTRA_ANGULAR_21,
223     INTRA_ANGULAR_22,
224     INTRA_ANGULAR_23,
225     INTRA_ANGULAR_24,
226     INTRA_ANGULAR_25,
227     INTRA_ANGULAR_26,
228     INTRA_ANGULAR_27,
229     INTRA_ANGULAR_28,
230     INTRA_ANGULAR_29,
231     INTRA_ANGULAR_30,
232     INTRA_ANGULAR_31,
233     INTRA_ANGULAR_32,
234     INTRA_ANGULAR_33,
235     INTRA_ANGULAR_34,
236 };
237
238 enum SAOType {
239     SAO_NOT_APPLIED = 0,
240     SAO_BAND,
241     SAO_EDGE,
242 };
243
244 enum SAOEOClass {
245     SAO_EO_HORIZ = 0,
246     SAO_EO_VERT,
247     SAO_EO_135D,
248     SAO_EO_45D,
249 };
250
251 enum ScanType {
252     SCAN_DIAG = 0,
253     SCAN_HORIZ,
254     SCAN_VERT,
255 };
256
257 typedef struct ShortTermRPS {
258     int num_negative_pics;
259     int num_delta_pocs;
260     int32_t delta_poc[32];
261     uint8_t used[32];
262 } ShortTermRPS;
263
264 typedef struct LongTermRPS {
265     int     poc[32];
266     uint8_t used[32];
267     uint8_t nb_refs;
268 } LongTermRPS;
269
270 typedef struct RefPicList {
271     struct HEVCFrame *ref[MAX_REFS];
272     int list[MAX_REFS];
273     int isLongTerm[MAX_REFS];
274     int nb_refs;
275 } RefPicList;
276
277 typedef struct RefPicListTab {
278     RefPicList refPicList[2];
279 } RefPicListTab;
280
281 typedef struct HEVCWindow {
282     int left_offset;
283     int right_offset;
284     int top_offset;
285     int bottom_offset;
286 } HEVCWindow;
287
288 typedef struct VUI {
289     AVRational sar;
290
291     int overscan_info_present_flag;
292     int overscan_appropriate_flag;
293
294     int video_signal_type_present_flag;
295     int video_format;
296     int video_full_range_flag;
297     int colour_description_present_flag;
298     uint8_t colour_primaries;
299     uint8_t transfer_characteristic;
300     uint8_t matrix_coeffs;
301
302     int chroma_loc_info_present_flag;
303     int chroma_sample_loc_type_top_field;
304     int chroma_sample_loc_type_bottom_field;
305     int neutra_chroma_indication_flag;
306
307     int field_seq_flag;
308     int frame_field_info_present_flag;
309
310     int default_display_window_flag;
311     HEVCWindow def_disp_win;
312
313     int vui_timing_info_present_flag;
314     uint32_t vui_num_units_in_tick;
315     uint32_t vui_time_scale;
316     int vui_poc_proportional_to_timing_flag;
317     int vui_num_ticks_poc_diff_one_minus1;
318     int vui_hrd_parameters_present_flag;
319
320     int bitstream_restriction_flag;
321     int tiles_fixed_structure_flag;
322     int motion_vectors_over_pic_boundaries_flag;
323     int restricted_ref_pic_lists_flag;
324     int min_spatial_segmentation_idc;
325     int max_bytes_per_pic_denom;
326     int max_bits_per_min_cu_denom;
327     int log2_max_mv_length_horizontal;
328     int log2_max_mv_length_vertical;
329 } VUI;
330
331 typedef struct PTL {
332     int general_profile_space;
333     uint8_t general_tier_flag;
334     int general_profile_idc;
335     int general_profile_compatibility_flag[32];
336     int general_level_idc;
337
338     uint8_t sub_layer_profile_present_flag[MAX_SUB_LAYERS];
339     uint8_t sub_layer_level_present_flag[MAX_SUB_LAYERS];
340
341     int sub_layer_profile_space[MAX_SUB_LAYERS];
342     uint8_t sub_layer_tier_flag[MAX_SUB_LAYERS];
343     int sub_layer_profile_idc[MAX_SUB_LAYERS];
344     uint8_t sub_layer_profile_compatibility_flags[MAX_SUB_LAYERS][32];
345     int sub_layer_level_idc[MAX_SUB_LAYERS];
346 } PTL;
347
348 typedef struct HEVCVPS {
349     uint8_t vps_temporal_id_nesting_flag;
350     int vps_max_layers;
351     int vps_max_sub_layers; ///< vps_max_temporal_layers_minus1 + 1
352
353     PTL ptl;
354     int vps_sub_layer_ordering_info_present_flag;
355     unsigned int vps_max_dec_pic_buffering[MAX_SUB_LAYERS];
356     unsigned int vps_num_reorder_pics[MAX_SUB_LAYERS];
357     unsigned int vps_max_latency_increase[MAX_SUB_LAYERS];
358     int vps_max_layer_id;
359     int vps_num_layer_sets; ///< vps_num_layer_sets_minus1 + 1
360     uint8_t vps_timing_info_present_flag;
361     uint32_t vps_num_units_in_tick;
362     uint32_t vps_time_scale;
363     uint8_t vps_poc_proportional_to_timing_flag;
364     int vps_num_ticks_poc_diff_one; ///< vps_num_ticks_poc_diff_one_minus1 + 1
365     int vps_num_hrd_parameters;
366 } HEVCVPS;
367
368 typedef struct ScalingList {
369     /* This is a little wasteful, since sizeID 0 only needs 8 coeffs,
370      * and size ID 3 only has 2 arrays, not 6. */
371     uint8_t sl[4][6][64];
372     uint8_t sl_dc[2][6];
373 } ScalingList;
374
375 typedef struct HEVCSPS {
376     int vps_id;
377     int chroma_format_idc;
378     uint8_t separate_colour_plane_flag;
379
380     ///< output (i.e. cropped) values
381     int output_width, output_height;
382     HEVCWindow output_window;
383
384     HEVCWindow pic_conf_win;
385
386     int bit_depth;
387     int pixel_shift;
388     enum AVPixelFormat pix_fmt;
389
390     unsigned int log2_max_poc_lsb;
391     int pcm_enabled_flag;
392
393     int max_sub_layers;
394     struct {
395         int max_dec_pic_buffering;
396         int num_reorder_pics;
397         int max_latency_increase;
398     } temporal_layer[MAX_SUB_LAYERS];
399
400     VUI vui;
401     PTL ptl;
402
403     uint8_t scaling_list_enable_flag;
404     ScalingList scaling_list;
405
406     unsigned int nb_st_rps;
407     ShortTermRPS st_rps[MAX_SHORT_TERM_RPS_COUNT];
408
409     uint8_t amp_enabled_flag;
410     uint8_t sao_enabled;
411
412     uint8_t long_term_ref_pics_present_flag;
413     uint16_t lt_ref_pic_poc_lsb_sps[32];
414     uint8_t used_by_curr_pic_lt_sps_flag[32];
415     uint8_t num_long_term_ref_pics_sps;
416
417     struct {
418         uint8_t bit_depth;
419         unsigned int log2_min_pcm_cb_size;
420         unsigned int log2_max_pcm_cb_size;
421         uint8_t loop_filter_disable_flag;
422     } pcm;
423     uint8_t sps_temporal_mvp_enabled_flag;
424     uint8_t sps_strong_intra_smoothing_enable_flag;
425
426     unsigned int log2_min_cb_size;
427     unsigned int log2_diff_max_min_coding_block_size;
428     unsigned int log2_min_tb_size;
429     unsigned int log2_max_trafo_size;
430     unsigned int log2_ctb_size;
431     unsigned int log2_min_pu_size;
432
433     int max_transform_hierarchy_depth_inter;
434     int max_transform_hierarchy_depth_intra;
435
436     ///< coded frame dimension in various units
437     int width;
438     int height;
439     int ctb_width;
440     int ctb_height;
441     int ctb_size;
442     int min_cb_width;
443     int min_cb_height;
444     int min_tb_width;
445     int min_tb_height;
446     int min_pu_width;
447     int min_pu_height;
448
449     int hshift[3];
450     int vshift[3];
451
452     int qp_bd_offset;
453 } HEVCSPS;
454
455 typedef struct HEVCPPS {
456     int sps_id; ///< seq_parameter_set_id
457
458     uint8_t sign_data_hiding_flag;
459
460     uint8_t cabac_init_present_flag;
461
462     int num_ref_idx_l0_default_active; ///< num_ref_idx_l0_default_active_minus1 + 1
463     int num_ref_idx_l1_default_active; ///< num_ref_idx_l1_default_active_minus1 + 1
464     int pic_init_qp_minus26;
465
466     uint8_t constrained_intra_pred_flag;
467     uint8_t transform_skip_enabled_flag;
468
469     uint8_t cu_qp_delta_enabled_flag;
470     int diff_cu_qp_delta_depth;
471
472     int cb_qp_offset;
473     int cr_qp_offset;
474     uint8_t pic_slice_level_chroma_qp_offsets_present_flag;
475     uint8_t weighted_pred_flag;
476     uint8_t weighted_bipred_flag;
477     uint8_t output_flag_present_flag;
478     uint8_t transquant_bypass_enable_flag;
479
480     uint8_t dependent_slice_segments_enabled_flag;
481     uint8_t tiles_enabled_flag;
482     uint8_t entropy_coding_sync_enabled_flag;
483
484     int num_tile_columns;   ///< num_tile_columns_minus1 + 1
485     int num_tile_rows;      ///< num_tile_rows_minus1 + 1
486     uint8_t uniform_spacing_flag;
487     uint8_t loop_filter_across_tiles_enabled_flag;
488
489     uint8_t seq_loop_filter_across_slices_enabled_flag;
490
491     uint8_t deblocking_filter_control_present_flag;
492     uint8_t deblocking_filter_override_enabled_flag;
493     uint8_t disable_dbf;
494     int beta_offset;    ///< beta_offset_div2 * 2
495     int tc_offset;      ///< tc_offset_div2 * 2
496
497     uint8_t scaling_list_data_present_flag;
498     ScalingList scaling_list;
499
500     uint8_t lists_modification_present_flag;
501     int log2_parallel_merge_level; ///< log2_parallel_merge_level_minus2 + 2
502     int num_extra_slice_header_bits;
503     uint8_t slice_header_extension_present_flag;
504
505     // Inferred parameters
506     int *column_width;  ///< ColumnWidth
507     int *row_height;    ///< RowHeight
508     int *col_bd;        ///< ColBd
509     int *row_bd;        ///< RowBd
510     int *col_idxX;
511
512     int *ctb_addr_rs_to_ts; ///< CtbAddrRSToTS
513     int *ctb_addr_ts_to_rs; ///< CtbAddrTSToRS
514     int *tile_id;           ///< TileId
515     int *tile_pos_rs;       ///< TilePosRS
516     int *min_cb_addr_zs;    ///< MinCbAddrZS
517     int *min_tb_addr_zs;    ///< MinTbAddrZS
518 } HEVCPPS;
519
520 typedef struct SliceHeader {
521     int pps_id;
522
523     ///< address (in raster order) of the first block in the current slice segment
524     unsigned int   slice_segment_addr;
525     ///< address (in raster order) of the first block in the current slice
526     unsigned int   slice_addr;
527
528     enum SliceType slice_type;
529
530     int pic_order_cnt_lsb;
531
532     uint8_t first_slice_in_pic_flag;
533     uint8_t dependent_slice_segment_flag;
534     uint8_t pic_output_flag;
535     uint8_t colour_plane_id;
536
537     ///< RPS coded in the slice header itself is stored here
538     ShortTermRPS slice_rps;
539     const ShortTermRPS *short_term_rps;
540     LongTermRPS long_term_rps;
541     unsigned int list_entry_lx[2][32];
542
543     uint8_t rpl_modification_flag[2];
544     uint8_t no_output_of_prior_pics_flag;
545     uint8_t slice_temporal_mvp_enabled_flag;
546
547     unsigned int nb_refs[2];
548
549     uint8_t slice_sample_adaptive_offset_flag[3];
550     uint8_t mvd_l1_zero_flag;
551
552     uint8_t cabac_init_flag;
553     uint8_t disable_deblocking_filter_flag; ///< slice_header_disable_deblocking_filter_flag
554     uint8_t slice_loop_filter_across_slices_enabled_flag;
555     uint8_t collocated_list;
556
557     unsigned int collocated_ref_idx;
558
559     int slice_qp_delta;
560     int slice_cb_qp_offset;
561     int slice_cr_qp_offset;
562
563     int beta_offset;    ///< beta_offset_div2 * 2
564     int tc_offset;      ///< tc_offset_div2 * 2
565
566     unsigned int max_num_merge_cand; ///< 5 - 5_minus_max_num_merge_cand
567
568     int num_entry_point_offsets;
569
570     int8_t slice_qp;
571
572     uint8_t luma_log2_weight_denom;
573     int16_t chroma_log2_weight_denom;
574
575     int16_t luma_weight_l0[16];
576     int16_t chroma_weight_l0[16][2];
577     int16_t chroma_weight_l1[16][2];
578     int16_t luma_weight_l1[16];
579
580     int16_t luma_offset_l0[16];
581     int16_t chroma_offset_l0[16][2];
582
583     int16_t luma_offset_l1[16];
584     int16_t chroma_offset_l1[16][2];
585
586     int slice_ctb_addr_rs;
587 } SliceHeader;
588
589 typedef struct CodingTree {
590     int depth; ///< ctDepth
591 } CodingTree;
592
593 typedef struct CodingUnit {
594     int x;
595     int y;
596
597     enum PredMode pred_mode;    ///< PredMode
598     enum PartMode part_mode;    ///< PartMode
599
600     uint8_t rqt_root_cbf;
601
602     uint8_t pcm_flag;
603
604     // Inferred parameters
605     uint8_t intra_split_flag;   ///< IntraSplitFlag
606     uint8_t max_trafo_depth;    ///< MaxTrafoDepth
607     uint8_t cu_transquant_bypass_flag;
608 } CodingUnit;
609
610 typedef struct Mv {
611     int16_t x;  ///< horizontal component of motion vector
612     int16_t y;  ///< vertical component of motion vector
613 } Mv;
614
615 typedef struct MvField {
616     Mv mv[2];
617     int8_t ref_idx[2];
618     int8_t pred_flag[2];
619     uint8_t is_intra;
620 } MvField;
621
622 typedef struct NeighbourAvailable {
623     int cand_bottom_left;
624     int cand_left;
625     int cand_up;
626     int cand_up_left;
627     int cand_up_right;
628     int cand_up_right_sap;
629 } NeighbourAvailable;
630
631 typedef struct PredictionUnit {
632     int mpm_idx;
633     int rem_intra_luma_pred_mode;
634     uint8_t intra_pred_mode[4];
635     Mv mvd;
636     uint8_t merge_flag;
637     uint8_t intra_pred_mode_c;
638 } PredictionUnit;
639
640 typedef struct TransformTree {
641     uint8_t cbf_cb[MAX_TRANSFORM_DEPTH][MAX_CU_SIZE * MAX_CU_SIZE];
642     uint8_t cbf_cr[MAX_TRANSFORM_DEPTH][MAX_CU_SIZE * MAX_CU_SIZE];
643     uint8_t cbf_luma;
644
645     // Inferred parameters
646     uint8_t inter_split_flag;
647 } TransformTree;
648
649 typedef struct TransformUnit {
650     int cu_qp_delta;
651
652     // Inferred parameters;
653     int cur_intra_pred_mode;
654     uint8_t is_cu_qp_delta_coded;
655 } TransformUnit;
656
657 typedef struct SAOParams {
658     int offset_abs[3][4];   ///< sao_offset_abs
659     int offset_sign[3][4];  ///< sao_offset_sign
660
661     int band_position[3];   ///< sao_band_position
662
663     int eo_class[3];        ///< sao_eo_class
664
665     int offset_val[3][5];   ///<SaoOffsetVal
666
667     uint8_t type_idx[3];    ///< sao_type_idx
668 } SAOParams;
669
670 typedef struct DBParams {
671     int beta_offset;
672     int tc_offset;
673 } DBParams;
674
675 #define HEVC_FRAME_FLAG_OUTPUT    (1 << 0)
676 #define HEVC_FRAME_FLAG_SHORT_REF (1 << 1)
677 #define HEVC_FRAME_FLAG_LONG_REF  (1 << 2)
678
679 typedef struct HEVCFrame {
680     AVFrame *frame;
681     ThreadFrame tf;
682     MvField *tab_mvf;
683     RefPicList *refPicList;
684     RefPicListTab **rpl_tab;
685     int ctb_count;
686     int poc;
687     struct HEVCFrame *collocated_ref;
688
689     HEVCWindow window;
690
691     AVBufferRef *tab_mvf_buf;
692     AVBufferRef *rpl_tab_buf;
693     AVBufferRef *rpl_buf;
694
695     /**
696      * A sequence counter, so that old frames are output first
697      * after a POC reset
698      */
699     uint16_t sequence;
700
701     /**
702      * A combination of HEVC_FRAME_FLAG_*
703      */
704     uint8_t flags;
705 } HEVCFrame;
706
707 typedef struct HEVCNAL {
708     uint8_t *rbsp_buffer;
709     int rbsp_buffer_size;
710
711     int size;
712     const uint8_t *data;
713 } HEVCNAL;
714
715 typedef struct HEVCDSPContext {
716     void (*put_pcm)(uint8_t *dst, ptrdiff_t stride, int size,
717                     GetBitContext *gb, int pcm_bit_depth);
718
719     void (*transquant_bypass[4])(uint8_t *dst, int16_t *coeffs,
720                                  ptrdiff_t stride);
721
722     void (*transform_skip)(uint8_t *dst, int16_t *coeffs, ptrdiff_t stride);
723     void (*transform_4x4_luma_add)(uint8_t *dst, int16_t *coeffs,
724                                    ptrdiff_t stride);
725     void (*transform_add[4])(uint8_t *dst, int16_t *coeffs, ptrdiff_t stride);
726
727     void (*sao_band_filter[4])(uint8_t *dst, uint8_t *src, ptrdiff_t stride,
728                                struct SAOParams *sao, int *borders,
729                                int width, int height, int c_idx);
730     void (*sao_edge_filter[4])(uint8_t *dst, uint8_t *src, ptrdiff_t stride,
731                                struct SAOParams *sao, int *borders, int width,
732                                int height, int c_idx, uint8_t vert_edge,
733                                uint8_t horiz_edge, uint8_t diag_edge);
734
735     void (*put_hevc_qpel[4][4])(int16_t *dst, ptrdiff_t dststride, uint8_t *src,
736                                 ptrdiff_t srcstride, int width, int height,
737                                 int16_t *mcbuffer);
738     void (*put_hevc_epel[2][2])(int16_t *dst, ptrdiff_t dststride, uint8_t *src,
739                                 ptrdiff_t srcstride, int width, int height,
740                                 int mx, int my, int16_t *mcbuffer);
741
742     void (*put_unweighted_pred)(uint8_t *dst, ptrdiff_t dststride, int16_t *src,
743                                 ptrdiff_t srcstride, int width, int height);
744     void (*put_weighted_pred_avg)(uint8_t *dst, ptrdiff_t dststride,
745                                   int16_t *src1, int16_t *src2,
746                                   ptrdiff_t srcstride, int width, int height);
747     void (*weighted_pred)(uint8_t denom, int16_t wlxFlag, int16_t olxFlag,
748                           uint8_t *dst, ptrdiff_t dststride, int16_t *src,
749                           ptrdiff_t srcstride, int width, int height);
750     void (*weighted_pred_avg)(uint8_t denom, int16_t wl0Flag, int16_t wl1Flag,
751                               int16_t ol0Flag, int16_t ol1Flag, uint8_t *dst,
752                               ptrdiff_t dststride, int16_t *src1, int16_t *src2,
753                               ptrdiff_t srcstride, int width, int height);
754
755     void (*hevc_h_loop_filter_luma)(uint8_t *pix, ptrdiff_t stride,
756                                     int *beta, int *tc,
757                                     uint8_t *no_p, uint8_t *no_q);
758     void (*hevc_v_loop_filter_luma)(uint8_t *pix, ptrdiff_t stride,
759                                     int *beta, int *tc,
760                                     uint8_t *no_p, uint8_t *no_q);
761     void (*hevc_h_loop_filter_chroma)(uint8_t *pix, ptrdiff_t stride,
762                                       int *tc, uint8_t *no_p, uint8_t *no_q);
763     void (*hevc_v_loop_filter_chroma)(uint8_t *pix, ptrdiff_t stride,
764                                       int *tc, uint8_t *no_p, uint8_t *no_q);
765     void (*hevc_h_loop_filter_luma_c)(uint8_t *pix, ptrdiff_t stride,
766                                       int *beta, int *tc,
767                                       uint8_t *no_p, uint8_t *no_q);
768     void (*hevc_v_loop_filter_luma_c)(uint8_t *pix, ptrdiff_t stride,
769                                       int *beta, int *tc,
770                                       uint8_t *no_p, uint8_t *no_q);
771     void (*hevc_h_loop_filter_chroma_c)(uint8_t *pix, ptrdiff_t stride,
772                                         int *tc, uint8_t *no_p,
773                                         uint8_t *no_q);
774     void (*hevc_v_loop_filter_chroma_c)(uint8_t *pix, ptrdiff_t stride,
775                                         int *tc, uint8_t *no_p,
776                                         uint8_t *no_q);
777 } HEVCDSPContext;
778
779 struct HEVCContext;
780
781 typedef struct HEVCPredContext {
782     void (*intra_pred)(struct HEVCContext *s, int x0, int y0,
783                        int log2_size, int c_idx);
784
785     void (*pred_planar[4])(uint8_t *src, const uint8_t *top,
786                            const uint8_t *left, ptrdiff_t stride);
787     void (*pred_dc)(uint8_t *src, const uint8_t *top, const uint8_t *left,
788                     ptrdiff_t stride, int log2_size, int c_idx);
789     void (*pred_angular[4])(uint8_t *src, const uint8_t *top,
790                             const uint8_t *left, ptrdiff_t stride,
791                             int c_idx, int mode);
792 } HEVCPredContext;
793
794 typedef struct HEVCLocalContext {
795     DECLARE_ALIGNED(16, int16_t, mc_buffer[(MAX_PB_SIZE + 7) * MAX_PB_SIZE]);
796     uint8_t cabac_state[HEVC_CONTEXTS];
797
798     uint8_t first_qp_group;
799
800     GetBitContext gb;
801     CABACContext cc;
802     TransformTree tt;
803
804     int8_t qp_y;
805     int8_t curr_qp_y;
806
807     TransformUnit tu;
808
809     uint8_t ctb_left_flag;
810     uint8_t ctb_up_flag;
811     uint8_t ctb_up_right_flag;
812     uint8_t ctb_up_left_flag;
813     int     start_of_tiles_x;
814     int     end_of_tiles_x;
815     int     end_of_tiles_y;
816     uint8_t *edge_emu_buffer;
817     int      edge_emu_buffer_size;
818     CodingTree ct;
819     CodingUnit cu;
820     PredictionUnit pu;
821     NeighbourAvailable na;
822
823     uint8_t slice_or_tiles_left_boundary;
824     uint8_t slice_or_tiles_up_boundary;
825 } HEVCLocalContext;
826
827 typedef struct HEVCContext {
828     const AVClass *c;  // needed by private avoptions
829     AVCodecContext *avctx;
830
831     HEVCLocalContext HEVClc;
832
833     uint8_t cabac_state[HEVC_CONTEXTS];
834
835     /** 1 if the independent slice segment header was successfully parsed */
836     uint8_t slice_initialized;
837
838     AVFrame *frame;
839     AVFrame *sao_frame;
840     AVFrame *tmp_frame;
841     AVFrame *output_frame;
842
843     const HEVCVPS *vps;
844     const HEVCSPS *sps;
845     const HEVCPPS *pps;
846     HEVCVPS     *vps_list[MAX_VPS_COUNT];
847     AVBufferRef *sps_list[MAX_SPS_COUNT];
848     AVBufferRef *pps_list[MAX_PPS_COUNT];
849
850     AVBufferPool *tab_mvf_pool;
851     AVBufferPool *rpl_tab_pool;
852
853     ///< candidate references for the current frame
854     RefPicList rps[5];
855
856     SliceHeader sh;
857     SAOParams *sao;
858     DBParams *deblock;
859     enum NALUnitType nal_unit_type;
860     int temporal_id;  ///< temporal_id_plus1 - 1
861     HEVCFrame *ref;
862     HEVCFrame DPB[32];
863     int poc;
864     int pocTid0;
865     int slice_idx; ///< number of the slice being currently decoded
866     int eos;       ///< current packet contains an EOS/EOB NAL
867     int max_ra;
868     int bs_width;
869     int bs_height;
870
871     int is_decoded;
872
873     HEVCPredContext hpc;
874     HEVCDSPContext hevcdsp;
875     VideoDSPContext vdsp;
876     DSPContext dsp;
877     int8_t *qp_y_tab;
878     uint8_t *split_cu_flag;
879     uint8_t *horizontal_bs;
880     uint8_t *vertical_bs;
881
882     int32_t *tab_slice_address;
883
884     //  CU
885     uint8_t *skip_flag;
886     uint8_t *tab_ct_depth;
887     // PU
888     uint8_t *tab_ipm;
889
890     uint8_t *cbf_luma; // cbf_luma of colocated TU
891     uint8_t *is_pcm;
892
893     // CTB-level flags affecting loop filter operation
894     uint8_t *filter_slice_edges;
895
896     /** used on BE to byteswap the lines for checksumming */
897     uint8_t *checksum_buf;
898     int      checksum_buf_size;
899
900     /**
901      * Sequence counters for decoded and output frames, so that old
902      * frames are output first after a POC reset
903      */
904     uint16_t seq_decode;
905     uint16_t seq_output;
906
907     HEVCNAL *nals;
908     int nb_nals;
909     int nals_allocated;
910
911     // for checking the frame checksums
912     struct AVMD5 *md5_ctx;
913     uint8_t       md5[3][16];
914     uint8_t is_md5;
915
916     uint8_t context_initialized;
917     uint8_t is_nalff;       ///< this flag is != 0 if bitstream is encapsulated
918                             ///< as a format defined in 14496-15
919     int apply_defdispwin;
920
921     int nal_length_size;    ///< Number of bytes used for nal length (1, 2 or 4)
922     int nuh_layer_id;
923 } HEVCContext;
924
925 int ff_hevc_decode_short_term_rps(HEVCContext *s, ShortTermRPS *rps,
926                                   const HEVCSPS *sps, int is_slice_header);
927 int ff_hevc_decode_nal_vps(HEVCContext *s);
928 int ff_hevc_decode_nal_sps(HEVCContext *s);
929 int ff_hevc_decode_nal_pps(HEVCContext *s);
930 int ff_hevc_decode_nal_sei(HEVCContext *s);
931
932 /**
933  * Mark all frames in DPB as unused for reference.
934  */
935 void ff_hevc_clear_refs(HEVCContext *s);
936
937 /**
938  * Drop all frames currently in DPB.
939  */
940 void ff_hevc_flush_dpb(HEVCContext *s);
941
942 /**
943  * Compute POC of the current frame and return it.
944  */
945 int ff_hevc_compute_poc(HEVCContext *s, int poc_lsb);
946
947 RefPicList *ff_hevc_get_ref_list(HEVCContext *s, HEVCFrame *frame,
948                                  int x0, int y0);
949
950 /**
951  * Construct the reference picture sets for the current frame.
952  */
953 int ff_hevc_frame_rps(HEVCContext *s);
954
955 /**
956  * Construct the reference picture list(s) for the current slice.
957  */
958 int ff_hevc_slice_rpl(HEVCContext *s);
959
960 void ff_hevc_save_states(HEVCContext *s, int ctb_addr_ts);
961 void ff_hevc_cabac_init(HEVCContext *s, int ctb_addr_ts);
962 int ff_hevc_sao_merge_flag_decode(HEVCContext *s);
963 int ff_hevc_sao_type_idx_decode(HEVCContext *s);
964 int ff_hevc_sao_band_position_decode(HEVCContext *s);
965 int ff_hevc_sao_offset_abs_decode(HEVCContext *s);
966 int ff_hevc_sao_offset_sign_decode(HEVCContext *s);
967 int ff_hevc_sao_eo_class_decode(HEVCContext *s);
968 int ff_hevc_end_of_slice_flag_decode(HEVCContext *s);
969 int ff_hevc_cu_transquant_bypass_flag_decode(HEVCContext *s);
970 int ff_hevc_skip_flag_decode(HEVCContext *s, int x0, int y0,
971                              int x_cb, int y_cb);
972 int ff_hevc_pred_mode_decode(HEVCContext *s);
973 int ff_hevc_split_coding_unit_flag_decode(HEVCContext *s, int ct_depth,
974                                           int x0, int y0);
975 int ff_hevc_part_mode_decode(HEVCContext *s, int log2_cb_size);
976 int ff_hevc_pcm_flag_decode(HEVCContext *s);
977 int ff_hevc_prev_intra_luma_pred_flag_decode(HEVCContext *s);
978 int ff_hevc_mpm_idx_decode(HEVCContext *s);
979 int ff_hevc_rem_intra_luma_pred_mode_decode(HEVCContext *s);
980 int ff_hevc_intra_chroma_pred_mode_decode(HEVCContext *s);
981 int ff_hevc_merge_idx_decode(HEVCContext *s);
982 int ff_hevc_merge_flag_decode(HEVCContext *s);
983 int ff_hevc_inter_pred_idc_decode(HEVCContext *s, int nPbW, int nPbH);
984 int ff_hevc_ref_idx_lx_decode(HEVCContext *s, int num_ref_idx_lx);
985 int ff_hevc_mvp_lx_flag_decode(HEVCContext *s);
986 int ff_hevc_no_residual_syntax_flag_decode(HEVCContext *s);
987 int ff_hevc_abs_mvd_greater0_flag_decode(HEVCContext *s);
988 int ff_hevc_abs_mvd_greater1_flag_decode(HEVCContext *s);
989 int ff_hevc_mvd_decode(HEVCContext *s);
990 int ff_hevc_mvd_sign_flag_decode(HEVCContext *s);
991 int ff_hevc_split_transform_flag_decode(HEVCContext *s, int log2_trafo_size);
992 int ff_hevc_cbf_cb_cr_decode(HEVCContext *s, int trafo_depth);
993 int ff_hevc_cbf_luma_decode(HEVCContext *s, int trafo_depth);
994 int ff_hevc_transform_skip_flag_decode(HEVCContext *s, int c_idx);
995 int ff_hevc_last_significant_coeff_x_prefix_decode(HEVCContext *s, int c_idx,
996                                                    int log2_size);
997 int ff_hevc_last_significant_coeff_y_prefix_decode(HEVCContext *s, int c_idx,
998                                                    int log2_size);
999 int ff_hevc_last_significant_coeff_suffix_decode(HEVCContext *s,
1000                                                  int last_significant_coeff_prefix);
1001 int ff_hevc_significant_coeff_group_flag_decode(HEVCContext *s, int c_idx,
1002                                                 int ctx_cg);
1003 int ff_hevc_significant_coeff_flag_decode(HEVCContext *s, int c_idx, int x_c,
1004                                           int y_c, int log2_trafo_size,
1005                                           int scan_idx, int prev_sig);
1006 int ff_hevc_coeff_abs_level_greater1_flag_decode(HEVCContext *s, int c_idx,
1007                                                  int ctx_set);
1008 int ff_hevc_coeff_abs_level_greater2_flag_decode(HEVCContext *s, int c_idx,
1009                                                  int inc);
1010 int ff_hevc_coeff_abs_level_remaining(HEVCContext *s, int base_level,
1011                                       int rc_rice_param);
1012 int ff_hevc_coeff_sign_flag(HEVCContext *s, uint8_t nb);
1013
1014 /**
1015  * Get the number of candidate references for the current frame.
1016  */
1017 int ff_hevc_frame_nb_refs(HEVCContext *s);
1018
1019 int ff_hevc_set_new_ref(HEVCContext *s, AVFrame **frame, int poc);
1020
1021 /**
1022  * Find next frame in output order and put a reference to it in frame.
1023  * @return 1 if a frame was output, 0 otherwise
1024  */
1025 int ff_hevc_output_frame(HEVCContext *s, AVFrame *frame, int flush);
1026
1027 void ff_hevc_unref_frame(HEVCContext *s, HEVCFrame *frame, int flags);
1028
1029 void ff_hevc_set_neighbour_available(HEVCContext *s, int x0, int y0,
1030                                      int nPbW, int nPbH);
1031 void ff_hevc_luma_mv_merge_mode(HEVCContext *s, int x0, int y0,
1032                                 int nPbW, int nPbH, int log2_cb_size,
1033                                 int part_idx, int merge_idx, MvField *mv);
1034 void ff_hevc_luma_mv_mvp_mode(HEVCContext *s, int x0, int y0,
1035                               int nPbW, int nPbH, int log2_cb_size,
1036                               int part_idx, int merge_idx,
1037                               MvField *mv, int mvp_lx_flag, int LX);
1038 void ff_hevc_set_qPy(HEVCContext *s, int xC, int yC, int xBase, int yBase,
1039                      int log2_cb_size);
1040 void ff_hevc_deblocking_boundary_strengths(HEVCContext *s, int x0, int y0,
1041                                            int log2_trafo_size,
1042                                            int slice_or_tiles_up_boundary,
1043                                            int slice_or_tiles_left_boundary);
1044 int ff_hevc_cu_qp_delta_sign_flag(HEVCContext *s);
1045 int ff_hevc_cu_qp_delta_abs(HEVCContext *s);
1046 void ff_hevc_hls_filter(HEVCContext *s, int x, int y);
1047 void ff_hevc_hls_filters(HEVCContext *s, int x_ctb, int y_ctb, int ctb_size);
1048
1049 void ff_hevc_pps_free(HEVCPPS **ppps);
1050
1051 void ff_hevc_pred_init(HEVCPredContext *hpc, int bit_depth);
1052
1053 void ff_hevc_dsp_init(HEVCDSPContext *hpc, int bit_depth);
1054
1055 extern const int8_t ff_hevc_epel_filters[7][16];
1056
1057 extern const uint8_t ff_hevc_qpel_extra_before[4];
1058 extern const uint8_t ff_hevc_qpel_extra_after[4];
1059 extern const uint8_t ff_hevc_qpel_extra[4];
1060
1061 extern const uint8_t ff_hevc_diag_scan4x4_x[16];
1062 extern const uint8_t ff_hevc_diag_scan4x4_y[16];
1063 extern const uint8_t ff_hevc_diag_scan8x8_x[64];
1064 extern const uint8_t ff_hevc_diag_scan8x8_y[64];
1065
1066 #endif /* AVCODEC_HEVC_H */