| Line | Branch | Exec | Source |
|---|---|---|---|
| 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_HEVCDEC_H | ||
| 24 | #define AVCODEC_HEVC_HEVCDEC_H | ||
| 25 | |||
| 26 | #include <stdatomic.h> | ||
| 27 | |||
| 28 | #include "libavutil/buffer.h" | ||
| 29 | #include "libavutil/mem_internal.h" | ||
| 30 | |||
| 31 | #include "libavcodec/avcodec.h" | ||
| 32 | #include "libavcodec/bswapdsp.h" | ||
| 33 | #include "libavcodec/cabac.h" | ||
| 34 | #include "libavcodec/dovi_rpu.h" | ||
| 35 | #include "libavcodec/h2645_parse.h" | ||
| 36 | #include "libavcodec/progressframe.h" | ||
| 37 | #include "libavcodec/videodsp.h" | ||
| 38 | |||
| 39 | #include "dsp.h" | ||
| 40 | #include "hevc.h" | ||
| 41 | #include "pred.h" | ||
| 42 | #include "ps.h" | ||
| 43 | #include "sei.h" | ||
| 44 | |||
| 45 | #define SHIFT_CTB_WPP 2 | ||
| 46 | |||
| 47 | #define MAX_TB_SIZE 32 | ||
| 48 | #define MAX_QP 51 | ||
| 49 | #define DEFAULT_INTRA_TC_OFFSET 2 | ||
| 50 | |||
| 51 | #define HEVC_CONTEXTS 199 | ||
| 52 | #define HEVC_STAT_COEFFS 4 | ||
| 53 | |||
| 54 | #define MRG_MAX_NUM_CANDS 5 | ||
| 55 | |||
| 56 | #define L0 0 | ||
| 57 | #define L1 1 | ||
| 58 | |||
| 59 | #define EPEL_EXTRA_BEFORE 1 | ||
| 60 | #define EPEL_EXTRA_AFTER 2 | ||
| 61 | #define EPEL_EXTRA 3 | ||
| 62 | #define QPEL_EXTRA_BEFORE 3 | ||
| 63 | #define QPEL_EXTRA_AFTER 4 | ||
| 64 | #define QPEL_EXTRA 7 | ||
| 65 | |||
| 66 | #define EDGE_EMU_BUFFER_STRIDE 80 | ||
| 67 | |||
| 68 | /** | ||
| 69 | * Value of the luma sample at position (x, y) in the 2D array tab. | ||
| 70 | */ | ||
| 71 | #define SAMPLE(tab, x, y) ((tab)[(y) * s->sps->width + (x)]) | ||
| 72 | #define SAMPLE_CTB(tab, x, y) ((tab)[(y) * min_cb_width + (x)]) | ||
| 73 | |||
| 74 | #define IS_IDR_TYPE(t) ((t) == HEVC_NAL_IDR_W_RADL || (t) == HEVC_NAL_IDR_N_LP) | ||
| 75 | #define IS_IDR(s) IS_IDR_TYPE((s)->nal_unit_type) | ||
| 76 | #define IS_BLA(s) ((s)->nal_unit_type == HEVC_NAL_BLA_W_RADL || (s)->nal_unit_type == HEVC_NAL_BLA_W_LP || \ | ||
| 77 | (s)->nal_unit_type == HEVC_NAL_BLA_N_LP) | ||
| 78 | #define IS_IRAP(s) ((s)->nal_unit_type >= HEVC_NAL_BLA_W_LP && (s)->nal_unit_type <= HEVC_NAL_RSV_IRAP_VCL23) | ||
| 79 | |||
| 80 | #define HEVC_RECOVERY_UNSPECIFIED INT_MAX | ||
| 81 | #define HEVC_RECOVERY_END INT_MIN | ||
| 82 | #define HEVC_IS_RECOVERING(s) ((s)->recovery_poc != HEVC_RECOVERY_UNSPECIFIED && (s)->recovery_poc != HEVC_RECOVERY_END) | ||
| 83 | |||
| 84 | enum RPSType { | ||
| 85 | ST_CURR_BEF = 0, | ||
| 86 | ST_CURR_AFT, | ||
| 87 | ST_FOLL, | ||
| 88 | LT_CURR, | ||
| 89 | LT_FOLL, | ||
| 90 | INTER_LAYER0, | ||
| 91 | INTER_LAYER1, | ||
| 92 | NB_RPS_TYPE, | ||
| 93 | }; | ||
| 94 | |||
| 95 | enum PartMode { | ||
| 96 | PART_2Nx2N = 0, | ||
| 97 | PART_2NxN = 1, | ||
| 98 | PART_Nx2N = 2, | ||
| 99 | PART_NxN = 3, | ||
| 100 | PART_2NxnU = 4, | ||
| 101 | PART_2NxnD = 5, | ||
| 102 | PART_nLx2N = 6, | ||
| 103 | PART_nRx2N = 7, | ||
| 104 | }; | ||
| 105 | |||
| 106 | enum PredMode { | ||
| 107 | MODE_INTER = 0, | ||
| 108 | MODE_INTRA, | ||
| 109 | MODE_SKIP, | ||
| 110 | }; | ||
| 111 | |||
| 112 | enum InterPredIdc { | ||
| 113 | PRED_L0 = 0, | ||
| 114 | PRED_L1, | ||
| 115 | PRED_BI, | ||
| 116 | }; | ||
| 117 | |||
| 118 | enum PredFlag { | ||
| 119 | PF_INTRA = 0, | ||
| 120 | PF_L0, | ||
| 121 | PF_L1, | ||
| 122 | PF_BI, | ||
| 123 | }; | ||
| 124 | |||
| 125 | enum IntraPredMode { | ||
| 126 | INTRA_PLANAR = 0, | ||
| 127 | INTRA_DC, | ||
| 128 | INTRA_ANGULAR_2, | ||
| 129 | INTRA_ANGULAR_3, | ||
| 130 | INTRA_ANGULAR_4, | ||
| 131 | INTRA_ANGULAR_5, | ||
| 132 | INTRA_ANGULAR_6, | ||
| 133 | INTRA_ANGULAR_7, | ||
| 134 | INTRA_ANGULAR_8, | ||
| 135 | INTRA_ANGULAR_9, | ||
| 136 | INTRA_ANGULAR_10, | ||
| 137 | INTRA_ANGULAR_11, | ||
| 138 | INTRA_ANGULAR_12, | ||
| 139 | INTRA_ANGULAR_13, | ||
| 140 | INTRA_ANGULAR_14, | ||
| 141 | INTRA_ANGULAR_15, | ||
| 142 | INTRA_ANGULAR_16, | ||
| 143 | INTRA_ANGULAR_17, | ||
| 144 | INTRA_ANGULAR_18, | ||
| 145 | INTRA_ANGULAR_19, | ||
| 146 | INTRA_ANGULAR_20, | ||
| 147 | INTRA_ANGULAR_21, | ||
| 148 | INTRA_ANGULAR_22, | ||
| 149 | INTRA_ANGULAR_23, | ||
| 150 | INTRA_ANGULAR_24, | ||
| 151 | INTRA_ANGULAR_25, | ||
| 152 | INTRA_ANGULAR_26, | ||
| 153 | INTRA_ANGULAR_27, | ||
| 154 | INTRA_ANGULAR_28, | ||
| 155 | INTRA_ANGULAR_29, | ||
| 156 | INTRA_ANGULAR_30, | ||
| 157 | INTRA_ANGULAR_31, | ||
| 158 | INTRA_ANGULAR_32, | ||
| 159 | INTRA_ANGULAR_33, | ||
| 160 | INTRA_ANGULAR_34, | ||
| 161 | }; | ||
| 162 | |||
| 163 | enum SAOType { | ||
| 164 | SAO_NOT_APPLIED = 0, | ||
| 165 | SAO_BAND, | ||
| 166 | SAO_EDGE, | ||
| 167 | SAO_APPLIED | ||
| 168 | }; | ||
| 169 | |||
| 170 | enum SAOEOClass { | ||
| 171 | SAO_EO_HORIZ = 0, | ||
| 172 | SAO_EO_VERT, | ||
| 173 | SAO_EO_135D, | ||
| 174 | SAO_EO_45D, | ||
| 175 | }; | ||
| 176 | |||
| 177 | enum ScanType { | ||
| 178 | SCAN_DIAG = 0, | ||
| 179 | SCAN_HORIZ, | ||
| 180 | SCAN_VERT, | ||
| 181 | }; | ||
| 182 | |||
| 183 | typedef struct HEVCCABACState { | ||
| 184 | uint8_t state[HEVC_CONTEXTS]; | ||
| 185 | uint8_t stat_coeff[HEVC_STAT_COEFFS]; | ||
| 186 | } HEVCCABACState; | ||
| 187 | |||
| 188 | typedef struct LongTermRPS { | ||
| 189 | int poc[32]; | ||
| 190 | uint8_t poc_msb_present[32]; | ||
| 191 | uint8_t used[32]; | ||
| 192 | uint8_t nb_refs; | ||
| 193 | } LongTermRPS; | ||
| 194 | |||
| 195 | typedef struct RefPicList { | ||
| 196 | struct HEVCFrame *ref[HEVC_MAX_REFS]; | ||
| 197 | int list[HEVC_MAX_REFS]; | ||
| 198 | int isLongTerm[HEVC_MAX_REFS]; | ||
| 199 | int nb_refs; | ||
| 200 | } RefPicList; | ||
| 201 | |||
| 202 | typedef struct RefPicListTab { | ||
| 203 | RefPicList refPicList[2]; | ||
| 204 | } RefPicListTab; | ||
| 205 | |||
| 206 | typedef struct SliceHeader { | ||
| 207 | unsigned int pps_id; | ||
| 208 | |||
| 209 | /// address (in raster order) of the first block in the current slice segment | ||
| 210 | unsigned int slice_segment_addr; | ||
| 211 | /// address (in raster order) of the first block in the current slice | ||
| 212 | unsigned int slice_addr; | ||
| 213 | |||
| 214 | enum HEVCSliceType slice_type; | ||
| 215 | |||
| 216 | int pic_order_cnt_lsb; | ||
| 217 | int poc; | ||
| 218 | |||
| 219 | uint8_t first_slice_in_pic_flag; | ||
| 220 | uint8_t dependent_slice_segment_flag; | ||
| 221 | uint8_t pic_output_flag; | ||
| 222 | uint8_t colour_plane_id; | ||
| 223 | uint8_t inter_layer_pred; | ||
| 224 | |||
| 225 | /// RPS coded in the slice header itself is stored here | ||
| 226 | int short_term_ref_pic_set_sps_flag; | ||
| 227 | int short_term_ref_pic_set_size; | ||
| 228 | ShortTermRPS slice_rps; | ||
| 229 | const ShortTermRPS *short_term_rps; | ||
| 230 | int long_term_ref_pic_set_size; | ||
| 231 | LongTermRPS long_term_rps; | ||
| 232 | unsigned int list_entry_lx[2][32]; | ||
| 233 | |||
| 234 | uint8_t rpl_modification_flag[2]; | ||
| 235 | uint8_t no_output_of_prior_pics_flag; | ||
| 236 | uint8_t slice_temporal_mvp_enabled_flag; | ||
| 237 | |||
| 238 | unsigned int nb_refs[2]; | ||
| 239 | |||
| 240 | uint8_t slice_sample_adaptive_offset_flag[3]; | ||
| 241 | uint8_t mvd_l1_zero_flag; | ||
| 242 | |||
| 243 | uint8_t cabac_init_flag; | ||
| 244 | uint8_t disable_deblocking_filter_flag; ///< slice_header_disable_deblocking_filter_flag | ||
| 245 | uint8_t slice_loop_filter_across_slices_enabled_flag; | ||
| 246 | uint8_t collocated_list; | ||
| 247 | |||
| 248 | unsigned int collocated_ref_idx; | ||
| 249 | |||
| 250 | int slice_qp_delta; | ||
| 251 | int slice_cb_qp_offset; | ||
| 252 | int slice_cr_qp_offset; | ||
| 253 | |||
| 254 | int slice_act_y_qp_offset; | ||
| 255 | int slice_act_cb_qp_offset; | ||
| 256 | int slice_act_cr_qp_offset; | ||
| 257 | |||
| 258 | uint8_t cu_chroma_qp_offset_enabled_flag; | ||
| 259 | |||
| 260 | int beta_offset; ///< beta_offset_div2 * 2 | ||
| 261 | int tc_offset; ///< tc_offset_div2 * 2 | ||
| 262 | |||
| 263 | uint8_t max_num_merge_cand; ///< 5 - 5_minus_max_num_merge_cand | ||
| 264 | uint8_t use_integer_mv_flag; | ||
| 265 | |||
| 266 | unsigned *entry_point_offset; | ||
| 267 | int * offset; | ||
| 268 | int * size; | ||
| 269 | int num_entry_point_offsets; | ||
| 270 | |||
| 271 | int8_t slice_qp; | ||
| 272 | |||
| 273 | uint8_t luma_log2_weight_denom; | ||
| 274 | int16_t chroma_log2_weight_denom; | ||
| 275 | |||
| 276 | int16_t luma_weight_l0[16]; | ||
| 277 | int16_t chroma_weight_l0[16][2]; | ||
| 278 | int16_t chroma_weight_l1[16][2]; | ||
| 279 | int16_t luma_weight_l1[16]; | ||
| 280 | |||
| 281 | int16_t luma_offset_l0[16]; | ||
| 282 | int16_t chroma_offset_l0[16][2]; | ||
| 283 | |||
| 284 | int16_t luma_offset_l1[16]; | ||
| 285 | int16_t chroma_offset_l1[16][2]; | ||
| 286 | |||
| 287 | int slice_ctb_addr_rs; | ||
| 288 | unsigned data_offset; | ||
| 289 | } SliceHeader; | ||
| 290 | |||
| 291 | typedef struct CodingUnit { | ||
| 292 | int x; | ||
| 293 | int y; | ||
| 294 | |||
| 295 | enum PredMode pred_mode; ///< PredMode | ||
| 296 | enum PartMode part_mode; ///< PartMode | ||
| 297 | |||
| 298 | // Inferred parameters | ||
| 299 | uint8_t intra_split_flag; ///< IntraSplitFlag | ||
| 300 | uint8_t max_trafo_depth; ///< MaxTrafoDepth | ||
| 301 | uint8_t cu_transquant_bypass_flag; | ||
| 302 | } CodingUnit; | ||
| 303 | |||
| 304 | typedef struct Mv { | ||
| 305 | int16_t x; ///< horizontal component of motion vector | ||
| 306 | int16_t y; ///< vertical component of motion vector | ||
| 307 | } Mv; | ||
| 308 | |||
| 309 | typedef struct MvField { | ||
| 310 | DECLARE_ALIGNED(4, Mv, mv)[2]; | ||
| 311 | int8_t ref_idx[2]; | ||
| 312 | int8_t pred_flag; | ||
| 313 | } MvField; | ||
| 314 | |||
| 315 | typedef struct NeighbourAvailable { | ||
| 316 | int cand_bottom_left; | ||
| 317 | int cand_left; | ||
| 318 | int cand_up; | ||
| 319 | int cand_up_left; | ||
| 320 | int cand_up_right; | ||
| 321 | int cand_up_right_sap; | ||
| 322 | } NeighbourAvailable; | ||
| 323 | |||
| 324 | typedef struct PredictionUnit { | ||
| 325 | int mpm_idx; | ||
| 326 | int rem_intra_luma_pred_mode; | ||
| 327 | uint8_t intra_pred_mode[4]; | ||
| 328 | Mv mvd; | ||
| 329 | uint8_t merge_flag; | ||
| 330 | uint8_t intra_pred_mode_c[4]; | ||
| 331 | uint8_t chroma_mode_c[4]; | ||
| 332 | } PredictionUnit; | ||
| 333 | |||
| 334 | typedef struct TransformUnit { | ||
| 335 | int cu_qp_delta; | ||
| 336 | |||
| 337 | int res_scale_val; | ||
| 338 | |||
| 339 | // Inferred parameters; | ||
| 340 | int intra_pred_mode; | ||
| 341 | int intra_pred_mode_c; | ||
| 342 | int chroma_mode_c; | ||
| 343 | uint8_t is_cu_qp_delta_coded; | ||
| 344 | uint8_t is_cu_chroma_qp_offset_coded; | ||
| 345 | uint8_t cross_pf; | ||
| 346 | } TransformUnit; | ||
| 347 | |||
| 348 | typedef struct DBParams { | ||
| 349 | int beta_offset; | ||
| 350 | int tc_offset; | ||
| 351 | } DBParams; | ||
| 352 | |||
| 353 | #define HEVC_FRAME_FLAG_OUTPUT (1 << 0) | ||
| 354 | #define HEVC_FRAME_FLAG_SHORT_REF (1 << 1) | ||
| 355 | #define HEVC_FRAME_FLAG_LONG_REF (1 << 2) | ||
| 356 | #define HEVC_FRAME_FLAG_UNAVAILABLE (1 << 3) | ||
| 357 | #define HEVC_FRAME_FLAG_CORRUPT (1 << 4) | ||
| 358 | |||
| 359 | typedef struct HEVCFrame { | ||
| 360 | union { | ||
| 361 | struct { | ||
| 362 | AVFrame *f; | ||
| 363 | }; | ||
| 364 | ProgressFrame tf; | ||
| 365 | }; | ||
| 366 | AVFrame *frame_grain; | ||
| 367 | int needs_fg; /* 1 if grain needs to be applied by the decoder */ | ||
| 368 | MvField *tab_mvf; ///< RefStruct reference | ||
| 369 | RefPicList *refPicList; | ||
| 370 | RefPicListTab **rpl_tab; ///< RefStruct reference | ||
| 371 | int ctb_count; | ||
| 372 | int poc; | ||
| 373 | |||
| 374 | const HEVCPPS *pps; ///< RefStruct reference | ||
| 375 | RefPicListTab *rpl; ///< RefStruct reference | ||
| 376 | int nb_rpl_elems; | ||
| 377 | |||
| 378 | void *hwaccel_picture_private; ///< RefStruct reference | ||
| 379 | |||
| 380 | // for secondary-layer frames, this is the DPB index of the base-layer frame | ||
| 381 | // from the same AU, if it exists, otherwise -1 | ||
| 382 | int base_layer_frame; | ||
| 383 | |||
| 384 | /** | ||
| 385 | * A combination of HEVC_FRAME_FLAG_* | ||
| 386 | */ | ||
| 387 | uint8_t flags; | ||
| 388 | } HEVCFrame; | ||
| 389 | |||
| 390 | /* State carried from one CTB to the next within a substream: one per tile | ||
| 391 | * column, or one per row for WPP. */ | ||
| 392 | typedef struct HEVCSubstream { | ||
| 393 | const uint8_t *data; ///< CABAC bytes of this substream | ||
| 394 | size_t size; | ||
| 395 | /* 0: open CABAC on data at first use, 1: already open, continue in it */ | ||
| 396 | uint8_t opened; | ||
| 397 | |||
| 398 | CABACContext cc; | ||
| 399 | uint8_t cabac_state[HEVC_CONTEXTS]; | ||
| 400 | uint8_t stat_coeff[HEVC_STAT_COEFFS]; | ||
| 401 | |||
| 402 | int8_t qp_y; | ||
| 403 | int qPy_pred; | ||
| 404 | uint8_t first_qp_group; | ||
| 405 | int8_t cu_qp_offset_cb; | ||
| 406 | int8_t cu_qp_offset_cr; | ||
| 407 | } HEVCSubstream; | ||
| 408 | |||
| 409 | typedef struct HEVCLocalContext { | ||
| 410 | HEVCSubstream sub; | ||
| 411 | |||
| 412 | void *logctx; | ||
| 413 | const struct HEVCContext *parent; | ||
| 414 | |||
| 415 | /* Slice being decoded, read instead of HEVCContext so that tiles of | ||
| 416 | * different slices can be decoded interleaved. */ | ||
| 417 | const SliceHeader *sh; | ||
| 418 | const RefPicList *rpl; ///< reference lists of that slice, NULL for I slices | ||
| 419 | const HEVCFrame *collocated_ref; ///< picture read by the temporal MVP, NULL if none | ||
| 420 | |||
| 421 | /** | ||
| 422 | * This is a pointer to the common CABAC state. | ||
| 423 | * In case entropy_coding_sync_enabled_flag is set, | ||
| 424 | * the CABAC state after decoding the second CTU in a row is | ||
| 425 | * stored here and used to initialize the CABAC state before | ||
| 426 | * decoding the first CTU in the next row. | ||
| 427 | * This is the basis for WPP and in case slice-threading is used, | ||
| 428 | * the next row is decoded by another thread making this state | ||
| 429 | * shared between threads. | ||
| 430 | */ | ||
| 431 | HEVCCABACState *common_cabac_state; | ||
| 432 | |||
| 433 | TransformUnit tu; | ||
| 434 | |||
| 435 | uint8_t ctb_left_flag; | ||
| 436 | uint8_t ctb_up_flag; | ||
| 437 | uint8_t ctb_up_right_flag; | ||
| 438 | uint8_t ctb_up_left_flag; | ||
| 439 | int end_of_tiles_x; | ||
| 440 | int end_of_tiles_y; | ||
| 441 | /* +7 is for subpixel interpolation, *2 for high bit depths */ | ||
| 442 | DECLARE_ALIGNED(32, uint8_t, edge_emu_buffer)[(MAX_PB_SIZE + 7) * EDGE_EMU_BUFFER_STRIDE * 2]; | ||
| 443 | /* The extended size between the new edge emu buffer is abused by SAO */ | ||
| 444 | DECLARE_ALIGNED(32, uint8_t, edge_emu_buffer2)[(MAX_PB_SIZE + 7) * EDGE_EMU_BUFFER_STRIDE * 2]; | ||
| 445 | DECLARE_ALIGNED(32, int16_t, tmp)[MAX_PB_SIZE * MAX_PB_SIZE]; | ||
| 446 | |||
| 447 | int ct_depth; | ||
| 448 | CodingUnit cu; | ||
| 449 | PredictionUnit pu; | ||
| 450 | NeighbourAvailable na; | ||
| 451 | |||
| 452 | #define BOUNDARY_LEFT_SLICE (1 << 0) | ||
| 453 | #define BOUNDARY_LEFT_TILE (1 << 1) | ||
| 454 | #define BOUNDARY_UPPER_SLICE (1 << 2) | ||
| 455 | #define BOUNDARY_UPPER_TILE (1 << 3) | ||
| 456 | /* properties of the boundary of the current CTB for the purposes | ||
| 457 | * of the deblocking filter */ | ||
| 458 | int boundary_flags; | ||
| 459 | |||
| 460 | /* decoding tiles in parallel: defer tile-boundary BS to a serial pass */ | ||
| 461 | int tile_bs_defer; | ||
| 462 | int slice_bs_defer; | ||
| 463 | |||
| 464 | // an array of these structs is used for per-thread state - pad its size | ||
| 465 | // to avoid false sharing | ||
| 466 | char padding[128]; | ||
| 467 | } HEVCLocalContext; | ||
| 468 | |||
| 469 | typedef struct HEVCLayerContext { | ||
| 470 | HEVCFrame DPB[32]; | ||
| 471 | HEVCFrame *cur_frame; | ||
| 472 | |||
| 473 | const HEVCSPS *sps; // RefStruct reference | ||
| 474 | |||
| 475 | int bs_width; | ||
| 476 | int bs_height; | ||
| 477 | |||
| 478 | SAOParams *sao; | ||
| 479 | DBParams *deblock; | ||
| 480 | |||
| 481 | // CU | ||
| 482 | uint8_t *skip_flag; | ||
| 483 | uint8_t *tab_ct_depth; | ||
| 484 | |||
| 485 | // PU | ||
| 486 | uint8_t *cbf_luma; // cbf_luma of colocated TU | ||
| 487 | uint8_t *tab_ipm; | ||
| 488 | uint8_t *is_pcm; | ||
| 489 | |||
| 490 | // CTB-level flags affecting loop filter operation | ||
| 491 | uint8_t *filter_slice_edges; | ||
| 492 | |||
| 493 | int32_t *tab_slice_address; | ||
| 494 | |||
| 495 | int8_t *qp_y_tab; | ||
| 496 | |||
| 497 | uint8_t *horizontal_bs; | ||
| 498 | uint8_t *vertical_bs; | ||
| 499 | |||
| 500 | uint8_t *sao_pixel_buffer_h[3]; | ||
| 501 | uint8_t *sao_pixel_buffer_v[3]; | ||
| 502 | |||
| 503 | struct AVRefStructPool *tab_mvf_pool; | ||
| 504 | struct AVRefStructPool *rpl_tab_pool; | ||
| 505 | } HEVCLayerContext; | ||
| 506 | |||
| 507 | /* A slice deferred to be decoded with the rest of its picture. sh is a | ||
| 508 | * shallow copy with the heap arrays cleared, see defer_tile_slice(). */ | ||
| 509 | typedef struct HEVCTileSlice { | ||
| 510 | SliceHeader sh; | ||
| 511 | const uint8_t *data; | ||
| 512 | int size; | ||
| 513 | int start_ts; | ||
| 514 | int end_ts; | ||
| 515 | int qp_y_seed; ///< lc[0].qp_y before deferring, the initial QP predictor | ||
| 516 | const RefPicList *rpl; ///< reference lists of this slice | ||
| 517 | const HEVCFrame *collocated_ref; ///< temporal MVP target of this slice | ||
| 518 | } HEVCTileSlice; | ||
| 519 | |||
| 520 | /* Start and state of one tile, for tiles bound by entry points and for one | ||
| 521 | * slice per tile alike. */ | ||
| 522 | typedef struct HEVCTileStream { | ||
| 523 | const SliceHeader *sh; ///< header of the slice this tile belongs to | ||
| 524 | const RefPicList *rpl; ///< reference lists of that slice, NULL for I slices | ||
| 525 | const HEVCFrame *collocated_ref; ///< temporal MVP target of that slice | ||
| 526 | const uint8_t *data; ///< start of the CABAC substream | ||
| 527 | size_t size; | ||
| 528 | int qp_y_seed; | ||
| 529 | int start_ts; ///< first CTB of the tile, in tile scan order | ||
| 530 | int ended; ///< the substream signalled its end (damaged input) | ||
| 531 | } HEVCTileStream; | ||
| 532 | |||
| 533 | typedef struct HEVCContext { | ||
| 534 | const AVClass *c; // needed by private avoptions | ||
| 535 | AVCodecContext *avctx; | ||
| 536 | |||
| 537 | HEVCLocalContext *local_ctx; | ||
| 538 | unsigned nb_local_ctx; | ||
| 539 | |||
| 540 | // per-layer decoding state, addressed by VPS layer indices | ||
| 541 | HEVCLayerContext layers[HEVC_VPS_MAX_LAYERS]; | ||
| 542 | // VPS index of the layer currently being decoded | ||
| 543 | unsigned cur_layer; | ||
| 544 | // bitmask of layer indices that are active for decoding/output | ||
| 545 | unsigned layers_active_decode; | ||
| 546 | unsigned layers_active_output; | ||
| 547 | |||
| 548 | /** 1 if the independent slice segment header was successfully parsed */ | ||
| 549 | uint8_t slice_initialized; | ||
| 550 | |||
| 551 | struct AVContainerFifo *output_fifo; | ||
| 552 | |||
| 553 | HEVCParamSets ps; | ||
| 554 | HEVCSEI sei; | ||
| 555 | struct AVMD5 *md5_ctx; | ||
| 556 | |||
| 557 | /// candidate references for the current frame | ||
| 558 | RefPicList rps[NB_RPS_TYPE]; | ||
| 559 | |||
| 560 | const HEVCVPS *vps; ///< RefStruct reference | ||
| 561 | const HEVCPPS *pps; ///< RefStruct reference | ||
| 562 | SliceHeader sh; | ||
| 563 | enum HEVCNALUnitType nal_unit_type; | ||
| 564 | int temporal_id; ///< temporal_id_plus1 - 1 | ||
| 565 | HEVCFrame *cur_frame; | ||
| 566 | HEVCFrame *collocated_ref; | ||
| 567 | int poc; | ||
| 568 | int poc_tid0; | ||
| 569 | int slice_idx; ///< number of the slice being currently decoded | ||
| 570 | int eos; ///< current packet contains an EOS/EOB NAL | ||
| 571 | int last_eos; ///< last packet contains an EOS/EOB NAL | ||
| 572 | int recovery_poc; | ||
| 573 | |||
| 574 | // NoRaslOutputFlag associated with the last IRAP frame | ||
| 575 | int no_rasl_output_flag; | ||
| 576 | |||
| 577 | // The first slice of the current picture was skipped (undecodable RASL | ||
| 578 | // or avctx->skip_frame), so drop its remaining slices too | ||
| 579 | int skipping_frame; | ||
| 580 | |||
| 581 | HEVCPredContext hpc; | ||
| 582 | HEVCDSPContext hevcdsp; | ||
| 583 | VideoDSPContext vdsp; | ||
| 584 | BswapDSPContext bdsp; | ||
| 585 | |||
| 586 | /** used on BE to byteswap the lines for checksumming */ | ||
| 587 | uint8_t *checksum_buf; | ||
| 588 | int checksum_buf_size; | ||
| 589 | |||
| 590 | /** The target for the common_cabac_state of the local contexts. */ | ||
| 591 | HEVCCABACState cabac; | ||
| 592 | |||
| 593 | struct ThreadProgress *wpp_progress; | ||
| 594 | unsigned nb_wpp_progress; | ||
| 595 | |||
| 596 | atomic_int wpp_err; | ||
| 597 | |||
| 598 | const uint8_t *data; | ||
| 599 | |||
| 600 | H2645Packet pkt; | ||
| 601 | // type of the first VCL NAL of the current frame | ||
| 602 | enum HEVCNALUnitType first_nal_type; | ||
| 603 | // index in pkt.nals of the NAL unit after which we can call | ||
| 604 | // ff_thread_finish_setup() | ||
| 605 | unsigned finish_setup_nal_idx; | ||
| 606 | |||
| 607 | int is_nalff; ///< this flag is != 0 if bitstream is encapsulated | ||
| 608 | ///< as a format defined in 14496-15 | ||
| 609 | int apply_defdispwin; | ||
| 610 | |||
| 611 | // multi-layer AVOptions | ||
| 612 | int *view_ids; | ||
| 613 | unsigned nb_view_ids; | ||
| 614 | |||
| 615 | unsigned *view_ids_available; | ||
| 616 | unsigned nb_view_ids_available; | ||
| 617 | |||
| 618 | unsigned *view_pos_available; | ||
| 619 | unsigned nb_view_pos_available; | ||
| 620 | |||
| 621 | int nal_length_size; ///< Number of bytes used for nal length (1, 2 or 4) | ||
| 622 | int nuh_layer_id; | ||
| 623 | |||
| 624 | int film_grain_warning_shown; | ||
| 625 | |||
| 626 | // dts of the packet currently being decoded | ||
| 627 | int64_t pkt_dts; | ||
| 628 | |||
| 629 | AVBufferRef *rpu_buf; ///< 0 or 1 Dolby Vision RPUs. | ||
| 630 | DOVIContext dovi_ctx; ///< Dolby Vision decoding context | ||
| 631 | |||
| 632 | /* Slices deferred when every tile carries its own slice, or when the | ||
| 633 | * slices of an untiled picture are decoded in parallel. Frame and layer | ||
| 634 | * are kept so that leftovers are dropped, not decoded into another | ||
| 635 | * frame. */ | ||
| 636 | HEVCTileSlice *tile_slices; | ||
| 637 | unsigned nb_tile_slices; | ||
| 638 | unsigned tile_slices_alloc; | ||
| 639 | const HEVCFrame *tile_slices_frame; | ||
| 640 | int tile_slices_layer; | ||
| 641 | |||
| 642 | /* Per-tile substream table of the picture, one entry per tile */ | ||
| 643 | HEVCTileStream *tile_streams; | ||
| 644 | unsigned tile_streams_alloc; | ||
| 645 | |||
| 646 | /* Substream state per tile column, only ever grows */ | ||
| 647 | HEVCSubstream *substreams; | ||
| 648 | unsigned nb_substreams; | ||
| 649 | |||
| 650 | int tile_interleave; | ||
| 651 | } HEVCContext; | ||
| 652 | |||
| 653 | /** | ||
| 654 | * Mark all frames in DPB as unused for reference. | ||
| 655 | */ | ||
| 656 | void ff_hevc_clear_refs(HEVCLayerContext *l); | ||
| 657 | |||
| 658 | /** | ||
| 659 | * Drop all frames currently in DPB. | ||
| 660 | */ | ||
| 661 | void ff_hevc_flush_dpb(HEVCContext *s); | ||
| 662 | |||
| 663 | const RefPicList *ff_hevc_get_ref_list(const HEVCFrame *frame, int x0, int y0); | ||
| 664 | |||
| 665 | /** | ||
| 666 | * Construct the reference picture sets for the current frame. | ||
| 667 | */ | ||
| 668 | int ff_hevc_frame_rps(HEVCContext *s, HEVCLayerContext *l); | ||
| 669 | |||
| 670 | /** | ||
| 671 | * Construct the reference picture list(s) for the current slice. | ||
| 672 | */ | ||
| 673 | int ff_hevc_slice_rpl(HEVCContext *s); | ||
| 674 | |||
| 675 | void ff_hevc_save_states(HEVCLocalContext *lc, const HEVCPPS *pps, | ||
| 676 | int ctb_addr_ts); | ||
| 677 | int ff_hevc_cabac_init(HEVCLocalContext *lc, const HEVCPPS *pps, | ||
| 678 | int ctb_addr_ts); | ||
| 679 | int ff_hevc_sao_merge_flag_decode(HEVCLocalContext *lc); | ||
| 680 | int ff_hevc_sao_type_idx_decode(HEVCLocalContext *lc); | ||
| 681 | int ff_hevc_sao_band_position_decode(HEVCLocalContext *lc); | ||
| 682 | int ff_hevc_sao_offset_abs_decode(HEVCLocalContext *lc, int bit_depth); | ||
| 683 | int ff_hevc_sao_offset_sign_decode(HEVCLocalContext *lc); | ||
| 684 | int ff_hevc_sao_eo_class_decode(HEVCLocalContext *lc); | ||
| 685 | int ff_hevc_end_of_slice_flag_decode(HEVCLocalContext *lc); | ||
| 686 | int ff_hevc_cu_transquant_bypass_flag_decode(HEVCLocalContext *lc); | ||
| 687 | int ff_hevc_skip_flag_decode(HEVCLocalContext *lc, uint8_t *skip_flag, | ||
| 688 | int x0, int y0, int x_cb, int y_cb, int min_cb_width); | ||
| 689 | int ff_hevc_pred_mode_decode(HEVCLocalContext *lc); | ||
| 690 | int ff_hevc_split_coding_unit_flag_decode(HEVCLocalContext *lc, uint8_t *tab_ct_depth, | ||
| 691 | const HEVCSPS *sps, | ||
| 692 | int ct_depth, int x0, int y0); | ||
| 693 | int ff_hevc_part_mode_decode(HEVCLocalContext *lc, const HEVCSPS *sps, int log2_cb_size); | ||
| 694 | int ff_hevc_pcm_flag_decode(HEVCLocalContext *lc); | ||
| 695 | int ff_hevc_prev_intra_luma_pred_flag_decode(HEVCLocalContext *lc); | ||
| 696 | int ff_hevc_mpm_idx_decode(HEVCLocalContext *lc); | ||
| 697 | int ff_hevc_rem_intra_luma_pred_mode_decode(HEVCLocalContext *lc); | ||
| 698 | int ff_hevc_intra_chroma_pred_mode_decode(HEVCLocalContext *lc); | ||
| 699 | int ff_hevc_merge_idx_decode(HEVCLocalContext *lc); | ||
| 700 | int ff_hevc_merge_flag_decode(HEVCLocalContext *lc); | ||
| 701 | int ff_hevc_inter_pred_idc_decode(HEVCLocalContext *lc, int nPbW, int nPbH); | ||
| 702 | int ff_hevc_ref_idx_lx_decode(HEVCLocalContext *lc, int num_ref_idx_lx); | ||
| 703 | int ff_hevc_mvp_lx_flag_decode(HEVCLocalContext *lc); | ||
| 704 | int ff_hevc_no_residual_syntax_flag_decode(HEVCLocalContext *lc); | ||
| 705 | int ff_hevc_split_transform_flag_decode(HEVCLocalContext *lc, int log2_trafo_size); | ||
| 706 | int ff_hevc_cbf_cb_cr_decode(HEVCLocalContext *lc, int trafo_depth); | ||
| 707 | int ff_hevc_cbf_luma_decode(HEVCLocalContext *lc, int trafo_depth); | ||
| 708 | int ff_hevc_log2_res_scale_abs(HEVCLocalContext *lc, int idx); | ||
| 709 | int ff_hevc_res_scale_sign_flag(HEVCLocalContext *lc, int idx); | ||
| 710 | |||
| 711 | /** | ||
| 712 | * Get the number of candidate references for the current frame. | ||
| 713 | */ | ||
| 714 | int ff_hevc_frame_nb_refs(const SliceHeader *sh, const HEVCPPS *pps, | ||
| 715 | unsigned layer_idx); | ||
| 716 | |||
| 717 | int ff_hevc_set_new_ref(HEVCContext *s, HEVCLayerContext *l, int poc); | ||
| 718 | |||
| 719 | 1629 | static av_always_inline int ff_hevc_nal_is_nonref(enum HEVCNALUnitType type) | |
| 720 | { | ||
| 721 |
2/2✓ Branch 0 taken 505 times.
✓ Branch 1 taken 1124 times.
|
1629 | switch (type) { |
| 722 | 505 | case HEVC_NAL_TRAIL_N: | |
| 723 | case HEVC_NAL_TSA_N: | ||
| 724 | case HEVC_NAL_STSA_N: | ||
| 725 | case HEVC_NAL_RADL_N: | ||
| 726 | case HEVC_NAL_RASL_N: | ||
| 727 | case HEVC_NAL_VCL_N10: | ||
| 728 | case HEVC_NAL_VCL_N12: | ||
| 729 | case HEVC_NAL_VCL_N14: | ||
| 730 | 505 | return 1; | |
| 731 | 1124 | default: break; | |
| 732 | } | ||
| 733 | 1124 | return 0; | |
| 734 | } | ||
| 735 | |||
| 736 | /** | ||
| 737 | * Find frames in the DPB that are ready for output and either write them to the | ||
| 738 | * output FIFO or drop their output flag, depending on the value of discard. | ||
| 739 | * | ||
| 740 | * @param max_output maximum number of AUs with an output-pending frame in at | ||
| 741 | * least one layer that can be present in the DPB before output | ||
| 742 | * is triggered | ||
| 743 | * @param max_dpb maximum number of any frames that can be present in the DPB | ||
| 744 | * for any layer before output is triggered | ||
| 745 | */ | ||
| 746 | int ff_hevc_output_frames(HEVCContext *s, | ||
| 747 | unsigned layers_active_decode, unsigned layers_active_output, | ||
| 748 | unsigned max_output, unsigned max_dpb, int discard); | ||
| 749 | |||
| 750 | void ff_hevc_unref_frame(HEVCFrame *frame, int flags); | ||
| 751 | |||
| 752 | void ff_hevc_set_neighbour_available(HEVCLocalContext *lc, int x0, int y0, | ||
| 753 | int nPbW, int nPbH, int log2_ctb_size); | ||
| 754 | void ff_hevc_luma_mv_merge_mode(HEVCLocalContext *lc, const HEVCPPS *pps, | ||
| 755 | int x0, int y0, | ||
| 756 | int nPbW, int nPbH, int log2_cb_size, | ||
| 757 | int part_idx, int merge_idx, MvField *mv); | ||
| 758 | void ff_hevc_luma_mv_mvp_mode(HEVCLocalContext *lc, const HEVCPPS *pps, | ||
| 759 | int x0, int y0, | ||
| 760 | int nPbW, int nPbH, int log2_cb_size, | ||
| 761 | int part_idx, int merge_idx, | ||
| 762 | MvField *mv, int mvp_lx_flag, int LX); | ||
| 763 | void ff_hevc_hls_filter(HEVCLocalContext *lc, const HEVCLayerContext *l, | ||
| 764 | const HEVCPPS *pps, | ||
| 765 | int x, int y, int ctb_size); | ||
| 766 | void ff_hevc_hls_filters(HEVCLocalContext *lc, const HEVCLayerContext *l, | ||
| 767 | const HEVCPPS *pps, | ||
| 768 | int x_ctb, int y_ctb, int ctb_size); | ||
| 769 | void ff_hevc_set_qPy(HEVCLocalContext *lc, | ||
| 770 | const HEVCLayerContext *l, const HEVCPPS *pps, | ||
| 771 | int xBase, int yBase, int log2_cb_size); | ||
| 772 | void ff_hevc_deblocking_boundary_strengths(HEVCLocalContext *lc, const HEVCLayerContext *l, | ||
| 773 | const HEVCPPS *pps, | ||
| 774 | int x0, int y0, int log2_trafo_size); | ||
| 775 | void ff_hevc_ctb_edge_bs(HEVCLocalContext *lc, const HEVCLayerContext *l, | ||
| 776 | const HEVCPPS *pps, int x0, int y0, unsigned edges); | ||
| 777 | int ff_hevc_cu_qp_delta_sign_flag(HEVCLocalContext *lc); | ||
| 778 | int ff_hevc_cu_qp_delta_abs(HEVCLocalContext *lc); | ||
| 779 | int ff_hevc_cu_chroma_qp_offset_flag(HEVCLocalContext *lc); | ||
| 780 | int ff_hevc_cu_chroma_qp_offset_idx(HEVCLocalContext *lc, int chroma_qp_offset_list_len_minus1); | ||
| 781 | void ff_hevc_hls_residual_coding(HEVCLocalContext *lc, const HEVCPPS *pps, | ||
| 782 | int x0, int y0, | ||
| 783 | int log2_trafo_size, enum ScanType scan_idx, | ||
| 784 | int c_idx); | ||
| 785 | |||
| 786 | void ff_hevc_hls_mvd_coding(HEVCLocalContext *lc, int x0, int y0, int log2_cb_size); | ||
| 787 | |||
| 788 | int ff_hevc_is_alpha_video(const HEVCContext *s); | ||
| 789 | |||
| 790 | /** | ||
| 791 | * Resolve the caller's view selection into layers. | ||
| 792 | * | ||
| 793 | * Depends only on the requested view IDs and the VPS, so unlike | ||
| 794 | * layers_active_decode it is already valid before setup_multilayer() runs and | ||
| 795 | * can be used from a hwaccel frame_params()/init() callback. Auxiliary alpha | ||
| 796 | * video is not covered here, as its layers do not follow view selection. | ||
| 797 | * | ||
| 798 | * @param active_output receives the mask of layers to output | ||
| 799 | * @return number of layers that will be decoded, or a negative error code | ||
| 800 | */ | ||
| 801 | int ff_hevc_requested_layers(const HEVCContext *s, const HEVCVPS *vps, | ||
| 802 | unsigned *active_output); | ||
| 803 | |||
| 804 | extern const uint8_t ff_hevc_qpel_extra_before[4]; | ||
| 805 | extern const uint8_t ff_hevc_qpel_extra_after[4]; | ||
| 806 | extern const uint8_t ff_hevc_qpel_extra[4]; | ||
| 807 | |||
| 808 | #endif /* AVCODEC_HEVC_HEVCDEC_H */ | ||
| 809 |