| @@ -21,32 +21,6 @@ · 20 unmodified lines |
| 21 | #include "evc.h" | 21 | #include "evc.h" |
| 22 | #include "evc_parse.h" | 22 | #include "evc_parse.h" |
| 23 | | 23 | |
| 24 | #define NUM_CHROMA_FORMATS 4 // @see ISO_IEC_23094-1 section 6.2 table 2 | | |
| 25 | | | |
| 26 | static const enum AVPixelFormat pix_fmts_8bit[NUM_CHROMA_FORMATS] = { | | |
| 27 | AV_PIX_FMT_GRAY8, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P | | |
| 28 | }; | | |
| 29 | | | |
| 30 | static const enum AVPixelFormat pix_fmts_9bit[NUM_CHROMA_FORMATS] = { | | |
| 31 | AV_PIX_FMT_GRAY9, AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9 | | |
| 32 | }; | | |
| 33 | | | |
| 34 | static const enum AVPixelFormat pix_fmts_10bit[NUM_CHROMA_FORMATS] = { | | |
| 35 | AV_PIX_FMT_GRAY10, AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10 | | |
| 36 | }; | | |
| 37 | | | |
| 38 | static const enum AVPixelFormat pix_fmts_12bit[NUM_CHROMA_FORMATS] = { | | |
| 39 | AV_PIX_FMT_GRAY12, AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12 | | |
| 40 | }; | | |
| 41 | | | |
| 42 | static const enum AVPixelFormat pix_fmts_14bit[NUM_CHROMA_FORMATS] = { | | |
| 43 | AV_PIX_FMT_GRAY14, AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14 | | |
| 44 | }; | | |
| 45 | | | |
| 46 | static const enum AVPixelFormat pix_fmts_16bit[NUM_CHROMA_FORMATS] = { | | |
| 47 | AV_PIX_FMT_GRAY16, AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16 | | |
| 48 | }; | | |
| 49 | | | |
| 50 | // nuh_temporal_id specifies a temporal identifier for the NAL unit | 24 | // nuh_temporal_id specifies a temporal identifier for the NAL unit |
| 51 | int ff_evc_get_temporal_id(const uint8_t *bits, int bits_size, void *logctx) | 25 | int ff_evc_get_temporal_id(const uint8_t *bits, int bits_size, void *logctx) |
| 52 | { | 26 | { |
| @@ -258,159 +232,3 @@ int ff_evc_derive_poc(const EVCParamSets *ps, const EVCParserSliceHeader *sh, · 205 unmodified lines |
| 258 | | 232 | |
| 259 | return 0; | 233 | return 0; |
| 260 | } | 234 | } |
| 261 | | | |
| 262 | int ff_evc_parse_nal_unit(EVCParserContext *ctx, const uint8_t *buf, int buf_size, void *logctx) | | |
| 263 | { | | |
| 264 | int nalu_type, nalu_size; | | |
| 265 | int tid; | | |
| 266 | const uint8_t *data = buf; | | |
| 267 | int data_size = buf_size; | | |
| 268 | | | |
| 269 | // ctx->picture_structure = AV_PICTURE_STRUCTURE_FRAME; | | |
| 270 | ctx->key_frame = -1; | | |
| 271 | | | |
| 272 | nalu_size = buf_size; | | |
| 273 | if (nalu_size <= 0) { | | |
| 274 | av_log(logctx, AV_LOG_ERROR, "Invalid NAL unit size: (%d)\n", nalu_size); | | |
| 275 | return AVERROR_INVALIDDATA; | | |
| 276 | } | | |
| 277 | | | |
| 278 | // @see ISO_IEC_23094-1_2020, 7.4.2.2 NAL unit header semantic (Table 4 - NAL unit type codes and NAL unit type classes) | | |
| 279 | // @see enum EVCNALUnitType in evc.h | | |
| 280 | nalu_type = evc_get_nalu_type(data, data_size, logctx); | | |
| 281 | if (nalu_type < EVC_NOIDR_NUT || nalu_type > EVC_UNSPEC_NUT62) { | | |
| 282 | av_log(logctx, AV_LOG_ERROR, "Invalid NAL unit type: (%d)\n", nalu_type); | | |
| 283 | return AVERROR_INVALIDDATA; | | |
| 284 | } | | |
| 285 | ctx->nalu_type = nalu_type; | | |
| 286 | | | |
| 287 | tid = ff_evc_get_temporal_id(data, data_size, logctx); | | |
| 288 | if (tid < 0) { | | |
| 289 | av_log(logctx, AV_LOG_ERROR, "Invalid temporial id: (%d)\n", tid); | | |
| 290 | return AVERROR_INVALIDDATA; | | |
| 291 | } | | |
| 292 | ctx->nuh_temporal_id = tid; | | |
| 293 | | | |
| 294 | data += EVC_NALU_HEADER_SIZE; | | |
| 295 | data_size -= EVC_NALU_HEADER_SIZE; | | |
| 296 | | | |
| 297 | switch(nalu_type) { | | |
| 298 | case EVC_SPS_NUT: { | | |
| 299 | EVCParserSPS *sps; | | |
| 300 | int bit_depth; | | |
| 301 | | | |
| 302 | sps = ff_evc_parse_sps(&ctx->ps, data, nalu_size); | | |
| 303 | if (!sps) { | | |
| 304 | av_log(logctx, AV_LOG_ERROR, "SPS parsing error\n"); | | |
| 305 | return AVERROR_INVALIDDATA; | | |
| 306 | } | | |
| 307 | | | |
| 308 | ctx->coded_width = sps->pic_width_in_luma_samples; | | |
| 309 | ctx->coded_height = sps->pic_height_in_luma_samples; | | |
| 310 | | | |
| 311 | if(sps->picture_cropping_flag) { | | |
| 312 | ctx->width = sps->pic_width_in_luma_samples - sps->picture_crop_left_offset - sps->picture_crop_right_offset; | | |
| 313 | ctx->height = sps->pic_height_in_luma_samples - sps->picture_crop_top_offset - sps->picture_crop_bottom_offset; | | |
| 314 | } else { | | |
| 315 | ctx->width = sps->pic_width_in_luma_samples; | | |
| 316 | ctx->height = sps->pic_height_in_luma_samples; | | |
| 317 | } | | |
| 318 | | | |
| 319 | if (sps->profile_idc == 1) ctx->profile = FF_PROFILE_EVC_MAIN; | | |
| 320 | else ctx->profile = FF_PROFILE_EVC_BASELINE; | | |
| 321 | | | |
| 322 | if (sps->vui_parameters_present_flag && sps->vui_parameters.timing_info_present_flag) { | | |
| 323 | int64_t num = sps->vui_parameters.num_units_in_tick; | | |
| 324 | int64_t den = sps->vui_parameters.time_scale; | | |
| 325 | if (num != 0 && den != 0) | | |
| 326 | av_reduce(&ctx->framerate.den, &ctx->framerate.num, num, den, 1 << 30); | | |
| 327 | } else | | |
| 328 | ctx->framerate = (AVRational) { 0, 1 }; | | |
| 329 | | | |
| 330 | bit_depth = sps->bit_depth_chroma_minus8 + 8; | | |
| 331 | ctx->format = AV_PIX_FMT_NONE; | | |
| 332 | | | |
| 333 | switch (bit_depth) { | | |
| 334 | case 8: | | |
| 335 | ctx->format = pix_fmts_8bit[sps->chroma_format_idc]; | | |
| 336 | break; | | |
| 337 | case 9: | | |
| 338 | ctx->format = pix_fmts_9bit[sps->chroma_format_idc]; | | |
| 339 | break; | | |
| 340 | case 10: | | |
| 341 | ctx->format = pix_fmts_10bit[sps->chroma_format_idc]; | | |
| 342 | break; | | |
| 343 | case 12: | | |
| 344 | ctx->format = pix_fmts_12bit[sps->chroma_format_idc]; | | |
| 345 | break; | | |
| 346 | case 14: | | |
| 347 | ctx->format = pix_fmts_14bit[sps->chroma_format_idc]; | | |
| 348 | break; | | |
| 349 | case 16: | | |
| 350 | ctx->format = pix_fmts_16bit[sps->chroma_format_idc]; | | |
| 351 | break; | | |
| 352 | } | | |
| 353 | av_assert0(ctx->format != AV_PIX_FMT_NONE); | | |
| 354 | | | |
| 355 | break; | | |
| 356 | } | | |
| 357 | case EVC_PPS_NUT: { | | |
| 358 | EVCParserPPS *pps; | | |
| 359 | | | |
| 360 | pps = ff_evc_parse_pps(&ctx->ps, data, nalu_size); | | |
| 361 | if (!pps) { | | |
| 362 | av_log(logctx, AV_LOG_ERROR, "PPS parsing error\n"); | | |
| 363 | return AVERROR_INVALIDDATA; | | |
| 364 | } | | |
| 365 | break; | | |
| 366 | } | | |
| 367 | case EVC_SEI_NUT: // Supplemental Enhancement Information | | |
| 368 | case EVC_APS_NUT: // Adaptation parameter set | | |
| 369 | case EVC_FD_NUT: // Filler data | | |
| 370 | break; | | |
| 371 | case EVC_IDR_NUT: // Coded slice of a IDR or non-IDR picture | | |
| 372 | case EVC_NOIDR_NUT: { | | |
| 373 | EVCParserSliceHeader sh; | | |
| 374 | int ret; | | |
| 375 | | | |
| 376 | ret = ff_evc_parse_slice_header(&sh, &ctx->ps, nalu_type, data, nalu_size); | | |
| 377 | if (ret < 0) { | | |
| 378 | av_log(logctx, AV_LOG_ERROR, "Slice header parsing error\n"); | | |
| 379 | return ret; | | |
| 380 | } | | |
| 381 | | | |
| 382 | switch (sh.slice_type) { | | |
| 383 | case EVC_SLICE_TYPE_B: { | | |
| 384 | ctx->pict_type = AV_PICTURE_TYPE_B; | | |
| 385 | break; | | |
| 386 | } | | |
| 387 | case EVC_SLICE_TYPE_P: { | | |
| 388 | ctx->pict_type = AV_PICTURE_TYPE_P; | | |
| 389 | break; | | |
| 390 | } | | |
| 391 | case EVC_SLICE_TYPE_I: { | | |
| 392 | ctx->pict_type = AV_PICTURE_TYPE_I; | | |
| 393 | break; | | |
| 394 | } | | |
| 395 | default: { | | |
| 396 | ctx->pict_type = AV_PICTURE_TYPE_NONE; | | |
| 397 | } | | |
| 398 | } | | |
| 399 | | | |
| 400 | ctx->key_frame = (nalu_type == EVC_IDR_NUT) ? 1 : 0; | | |
| 401 | | | |
| 402 | // POC (picture order count of the current picture) derivation | | |
| 403 | // @see ISO/IEC 23094-1:2020(E) 8.3.1 Decoding process for picture order count | | |
| 404 | ret = ff_evc_derive_poc(&ctx->ps, &sh, &ctx->poc, nalu_type, tid); | | |
| 405 | if (ret < 0) | | |
| 406 | return ret; | | |
| 407 | | | |
| 408 | ctx->output_picture_number = ctx->poc.PicOrderCntVal; | | |
| 409 | ctx->key_frame = (nalu_type == EVC_IDR_NUT) ? 1 : 0; | | |
| 410 | | | |
| 411 | break; | | |
| 412 | } | | |
| 413 | } | | |
| 414 | | | |
| 415 | return 0; | | |
| 416 | } | | |