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