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