From patchwork Wed Feb 17 01:51:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nuo Mi X-Patchwork-Id: 25678 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id BC71B44A2D9 for ; Wed, 17 Feb 2021 04:00:05 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 9CCA168A1C5; Wed, 17 Feb 2021 04:00:05 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-oi1-f170.google.com (mail-oi1-f170.google.com [209.85.167.170]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id F1AF7689FFE for ; Wed, 17 Feb 2021 03:59:58 +0200 (EET) Received: by mail-oi1-f170.google.com with SMTP id f3so13325079oiw.13 for ; Tue, 16 Feb 2021 17:59:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=KTo1nEnXl6sDrA4JJiMPGd64Foh0Jn0GYmNSCVBkceQ=; b=HIos/ObSSBbyADw5wvqbu0PRlc0f4vkMMTI7WiU31Mc3SwZl0CClU35R4yG2JSePl2 73D4n/x2PA3CJ51ZPm1V+72CNmd7vk527CUpdyYVcIAUKzGTMHniSaeTfShpZ9LglHnx vBD/LOJ2OO2NYxrsiDEhV+nsPUUIn52Nud6/CiWUUAAni6rd0GB70ystPRrrJQmT5lf/ PJruFB1keNLopfU5WCiDMgfX4qHIPOwo1kwTKikY9lBUuwL8+l4y+sYPWMD3GRo5FIi/ PEZmKsvcLIS+oYdQFVu3zzTLVrpZoq4xUNHrXnjedHptULXcAaDkjfWBngJTZPAkL5mW iQ8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=KTo1nEnXl6sDrA4JJiMPGd64Foh0Jn0GYmNSCVBkceQ=; b=b8UjwLBdyJGvn6mEA0/pD7u5j9/+4ZkTqtc2kyWfOwtmvrjNKXCA8U/HEdPyHwCTaI y14Zd2CVvwtdmFUEajdY86z1w+bfKFhaJQsQ2H1J0KEeNVmVrzjZPEbbSBZ+J492Ihhk ldm8Lqapg/SxIkZLwthyvuLMybsrsH62OMJb/Gx+0wyQnW6Z+IE7ZxzbcSA2xJJ0L9mc BwAcnryQfxnFoux+kA6kZBipfleYPsEwPghgjd/utACQIotalJWurOC7ZXaXfeB8G02z jBNKuw7Wb+AZAHqfNc0NTSNptJN+YcYsyO6Rf5YwDjrXGGwkPsiU9vZ/8kL4qheojA2I EJ8w== X-Gm-Message-State: AOAM530qV9B/HeDdleQOwPoRe2uqNhyYfNbjlrIZ6i04wcPUkK+iOZWi J9WWQTISkMaVpbf7M/r3V/3LGjlHFI0= X-Google-Smtp-Source: ABdhPJzxmOJvR1eDk7jPPseMSuC3+RYY/gsE1zSe+Q+XqVLzv2a34SiElV7yr2SXgQnyhQvTs0KCew== X-Received: by 2002:a17:90a:c086:: with SMTP id o6mr685494pjs.128.1613526734527; Tue, 16 Feb 2021 17:52:14 -0800 (PST) Received: from Nuomi.localdomain (23.83.245.51.16clouds.com. [23.83.245.51]) by smtp.gmail.com with ESMTPSA id n15sm361125pgl.31.2021.02.16.17.52.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Feb 2021 17:52:14 -0800 (PST) From: Nuo Mi To: ffmpeg-devel@ffmpeg.org Date: Wed, 17 Feb 2021 09:51:38 +0800 Message-Id: <20210217015146.19724-2-nuomi2021@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210217015146.19724-1-nuomi2021@gmail.com> References: <20210211143611.5663-1-nuomi2021@gmail.com> <20210217015146.19724-1-nuomi2021@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v6 1/9] cbs_h2645: refact, use cbs_h2645_replace_ps to replace cbs_h26*_replace_*ps X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Mark Thompson Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Mark Thompson --- libavcodec/cbs_h2645.c | 167 +++++++++++++++++++++++++++++++---------- 1 file changed, 126 insertions(+), 41 deletions(-) diff --git a/libavcodec/cbs_h2645.c b/libavcodec/cbs_h2645.c index 6005d46e0d..57c419aa05 100644 --- a/libavcodec/cbs_h2645.c +++ b/libavcodec/cbs_h2645.c @@ -661,38 +661,123 @@ static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx, return 0; } -#define cbs_h2645_replace_ps(h26n, ps_name, ps_var, id_element) \ -static int cbs_h26 ## h26n ## _replace_ ## ps_var(CodedBitstreamContext *ctx, \ - CodedBitstreamUnit *unit) \ -{ \ - CodedBitstreamH26 ## h26n ## Context *priv = ctx->priv_data; \ - H26 ## h26n ## Raw ## ps_name *ps_var = unit->content; \ - unsigned int id = ps_var->id_element; \ - int err; \ - if (id >= FF_ARRAY_ELEMS(priv->ps_var)) { \ - av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid " #ps_name \ - " id : %d.\n", id); \ - return AVERROR_INVALIDDATA; \ - } \ - err = ff_cbs_make_unit_refcounted(ctx, unit); \ - if (err < 0) \ - return err; \ - if (priv->ps_var[id] == priv->active_ ## ps_var) \ - priv->active_ ## ps_var = NULL ; \ - av_buffer_unref(&priv->ps_var ## _ref[id]); \ - av_assert0(unit->content_ref); \ - priv->ps_var ## _ref[id] = av_buffer_ref(unit->content_ref); \ - if (!priv->ps_var ## _ref[id]) \ - return AVERROR(ENOMEM); \ - priv->ps_var[id] = (H26 ## h26n ## Raw ## ps_name *)priv->ps_var ## _ref[id]->data; \ - return 0; \ -} +static int cbs_h2645_replace_ps(CodedBitstreamContext *ctx, + CodedBitstreamUnit *unit) +{ + typedef struct { + // Codec this parameter set exists in. + enum AVCodecID codec_id; + // The NAL unit type corresponding to this parameter set type. + int nal_unit_type; + // Name of the parameter set. This field is large enough to + // contain a string of the form "XPS". + char name[4]; + // The maximum number of this type of parameter set which might + // be stored. The greatest valid id is also one less than this. + int id_count; + // Offset of the ID field (uint8_t) in the decomposed raw + // parameter set structure. + size_t id_offset; + // Offset of the reference array (AVBufferRef*[]) in the codec + // private context. + size_t ref_array_offset; + // Offset of the pointer array (CodecRawXPS*[]) in the codec + // private context. + size_t ptr_array_offset; + // Offset of the active field (const CodecRawXPS*) in the codec + // private context, or zero if this codec does not have active + // parameter sets. + size_t active_offset; + } PSType; + +#define H26456_PS_TYPE(codec, nal, cname, uname, count, id_off, active_off) { \ + .codec_id = AV_CODEC_ID_ ## codec, \ + .nal_unit_type = nal, \ + .name = #cname, \ + .id_count = count, \ + .id_offset = id_off, \ + .ref_array_offset = offsetof(CodedBitstream ## codec ## Context, \ + uname ## _ref), \ + .ptr_array_offset = offsetof(CodedBitstream ## codec ## Context, \ + uname), \ + .active_offset = active_off, \ + } + +#define H2645_PS_TYPE(codec, nal, cname, uname, count, id_name, active_field) \ + H26456_PS_TYPE(codec, nal, cname, uname, count, \ + offsetof(codec ## Raw ## cname, id_name ## _parameter_set_id), \ + offsetof(CodedBitstream ## codec ## Context, active_field)) + +#define H264_PS_TYPE(cname, uname, id_name) \ + H2645_PS_TYPE(H264, H264_NAL_ ## cname, cname, uname, \ + H264_MAX_ ## cname ## _COUNT, \ + id_name, active_ ## uname) +#define H265_PS_TYPE(cname, uname, id_name) \ + H2645_PS_TYPE(H265, HEVC_NAL_ ## cname, cname, uname, \ + HEVC_MAX_ ## cname ## _COUNT, \ + uname ## _ ## id_name, active_ ## uname) + + static const PSType ps_types[] = { + H264_PS_TYPE(SPS, sps, seq), + H264_PS_TYPE(PPS, pps, pic), + H265_PS_TYPE(VPS, vps, video), + H265_PS_TYPE(SPS, sps, seq), + H265_PS_TYPE(PPS, pps, pic), + }; + + const PSType *ps_type; + AVBufferRef **ref_array; + void **ptr_array; + int err, id = 0, i; + + ps_type = NULL; + for (i = 0; i < FF_ARRAY_ELEMS(ps_types); i++) { + if (ps_types[i].codec_id == ctx->codec->codec_id && + ps_types[i].nal_unit_type == unit->type) { + ps_type = &ps_types[i]; + break; + } + } + av_assert0(ps_type); + + if (ps_type->id_offset) { + id = *((uint8_t*)unit->content + ps_type->id_offset); + + if (id >= ps_type->id_count) { + av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid %s id: %d.\n", + ps_type->name, id); + return AVERROR_INVALIDDATA; + } + } -cbs_h2645_replace_ps(4, SPS, sps, seq_parameter_set_id) -cbs_h2645_replace_ps(4, PPS, pps, pic_parameter_set_id) -cbs_h2645_replace_ps(5, VPS, vps, vps_video_parameter_set_id) -cbs_h2645_replace_ps(5, SPS, sps, sps_seq_parameter_set_id) -cbs_h2645_replace_ps(5, PPS, pps, pps_pic_parameter_set_id) + err = ff_cbs_make_unit_refcounted(ctx, unit); + if (err < 0) + return err; + + ref_array = (AVBufferRef**)((uint8_t*)ctx->priv_data + + ps_type->ref_array_offset); + ptr_array = (void**) ((uint8_t*)ctx->priv_data + + ps_type->ptr_array_offset); + + if (ps_type->active_offset) { + void **active = (void**)((uint8_t*)ctx->priv_data + + ps_type->active_offset); + + if (ptr_array[id] == *active) { + // The old active parameter set is being overwritten, so it can't + // be active after this point. + *active = NULL; + } + } + av_buffer_unref(&ref_array[id]); + + ref_array[id] = av_buffer_ref(unit->content_ref); + if (!ref_array[id]) + return AVERROR(ENOMEM); + ptr_array[id] = ref_array[id]->data; + + return 0; +} static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit) @@ -717,7 +802,7 @@ static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx, if (err < 0) return err; - err = cbs_h264_replace_sps(ctx, unit); + err = cbs_h2645_replace_ps(ctx, unit); if (err < 0) return err; } @@ -739,7 +824,7 @@ static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx, if (err < 0) return err; - err = cbs_h264_replace_pps(ctx, unit); + err = cbs_h2645_replace_ps(ctx, unit); if (err < 0) return err; } @@ -836,7 +921,7 @@ static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx, if (err < 0) return err; - err = cbs_h265_replace_vps(ctx, unit); + err = cbs_h2645_replace_ps(ctx, unit); if (err < 0) return err; } @@ -849,7 +934,7 @@ static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx, if (err < 0) return err; - err = cbs_h265_replace_sps(ctx, unit); + err = cbs_h2645_replace_ps(ctx, unit); if (err < 0) return err; } @@ -863,7 +948,7 @@ static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx, if (err < 0) return err; - err = cbs_h265_replace_pps(ctx, unit); + err = cbs_h2645_replace_ps(ctx, unit); if (err < 0) return err; } @@ -1007,7 +1092,7 @@ static int cbs_h264_write_nal_unit(CodedBitstreamContext *ctx, if (err < 0) return err; - err = cbs_h264_replace_sps(ctx, unit); + err = cbs_h2645_replace_ps(ctx, unit); if (err < 0) return err; } @@ -1031,7 +1116,7 @@ static int cbs_h264_write_nal_unit(CodedBitstreamContext *ctx, if (err < 0) return err; - err = cbs_h264_replace_pps(ctx, unit); + err = cbs_h2645_replace_ps(ctx, unit); if (err < 0) return err; } @@ -1124,7 +1209,7 @@ static int cbs_h265_write_nal_unit(CodedBitstreamContext *ctx, if (err < 0) return err; - err = cbs_h265_replace_vps(ctx, unit); + err = cbs_h2645_replace_ps(ctx, unit); if (err < 0) return err; } @@ -1138,7 +1223,7 @@ static int cbs_h265_write_nal_unit(CodedBitstreamContext *ctx, if (err < 0) return err; - err = cbs_h265_replace_sps(ctx, unit); + err = cbs_h2645_replace_ps(ctx, unit); if (err < 0) return err; } @@ -1152,7 +1237,7 @@ static int cbs_h265_write_nal_unit(CodedBitstreamContext *ctx, if (err < 0) return err; - err = cbs_h265_replace_pps(ctx, unit); + err = cbs_h2645_replace_ps(ctx, unit); if (err < 0) return err; }