From patchwork Thu Jul 6 16:19:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhao Zhili X-Patchwork-Id: 42460 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3b1e:b0:12b:9ae3:586d with SMTP id c30csp6353769pzh; Thu, 6 Jul 2023 01:26:25 -0700 (PDT) X-Google-Smtp-Source: APBJJlFct3TSvj6BlGhmcZ4/L3RzX2DYxUauYSSEwLAOynvr5X8516frXoyCs9OPvZIxGZjFoZn8 X-Received: by 2002:a17:907:3da4:b0:990:8807:8f88 with SMTP id he36-20020a1709073da400b0099088078f88mr1116080ejc.55.1688631984995; Thu, 06 Jul 2023 01:26:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688631984; cv=none; d=google.com; s=arc-20160816; b=TAu2Gih+vk8YPcRObfvsmurFBPqgmHLpFCHfcWbbI35DFTsAaNbMaBak5/NViDydZw h9dvlT6oQduBOEbNYYPxDZjF5Dfn9yTapHtuquzqB9dIbyymoVO0i7Kfb3pYsI5JQAjK B6vjG1OlEkpqGdw17SwvLnqx9B26/OcT/9hqLq4m4Iz4bmw2DtEA/Y70eR+sn/6K/5BT N/G5RVUvOTRtdNI5zt8ugZ5nuHuY8LvIhUnejuimPY2t85tIeE2k/iZ+bs2PGXwIKZu8 f8GvdUSbBH8p1lY4GXJPgNSZewX3nmI3O80HXVOq/HK//4wi8FfeYhKixjvdn2xbPPde I1Uw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to:date :to:from:message-id:dkim-signature:delivered-to; bh=kLPfbk7yKzhKNCVYTmtL4DdY3rEtDdsEgMmobl2fEdM=; fh=fgsEC5uuZOuzV7r9mTXgvURIGbxpRO08JRnNlycBIXw=; b=DZSBskUm03XUR/CO3hQfV1wot4tky7pxN58M2FaR4PTCEdjMxR3g5TR6P326WWqKk1 IcYK/+m7QRlbS5z4k2W336KAQTiUJFv56Wq/mIFngdY5T+qbpnCeNsCmK4w0cKIre0o0 rUdoukyurJOApPL/73XlRMck60KZbo541BFZUDEIT6OGQImeMNV25sEPo4MUu1+xfv/g 5D8O5YxpcgnmNpFy45edQAXWGabBOXciAJUxhnP5CHUWm78nIwm2ho3goAM6Da58ykEi 26zHRj+/kby8CTNR6jIheqrQkG2UYMfQUPQEdH6/D+Dte3lMh1mYZQ2t77E010Yn/W4i p08g== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@foxmail.com header.s=s201512 header.b=DLftEs3K; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=foxmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id t10-20020a1709066bca00b00991f96678cesi544550ejs.615.2023.07.06.01.26.24; Thu, 06 Jul 2023 01:26:24 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@foxmail.com header.s=s201512 header.b=DLftEs3K; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=foxmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id DFBDE68C6C5; Thu, 6 Jul 2023 11:26:21 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from out203-205-221-149.mail.qq.com (out203-205-221-149.mail.qq.com [203.205.221.149]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A0B3B68C672 for ; Thu, 6 Jul 2023 11:26:14 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1688631971; bh=/6PgLCBuvjpceDHseb9tM7knBRzhCcIvMmBA9pCIsfA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=DLftEs3KedrMdCX7ENxdhzfcYXzCc1YO2BNz31ehi3EaoWGLDTuBTN0j8SfNlUiGw tHeR8rkGCt1pjo8fT8l4+aNlnk5QPz352RBTR0w6UG+p2VYlyiyvnEUk1KiHfpbY2U DcO4ADnvZs/FqnmP1lHSXnaNnemGRhajl0c/M+0k= Received: from localhost.localdomain ([113.108.77.56]) by newxmesmtplogicsvrszc2-0.qq.com (NewEsmtp) with SMTP id 4F8BA662; Thu, 06 Jul 2023 16:19:56 +0800 X-QQ-mid: xmsmtpt1688631596tko0zmabz Message-ID: X-QQ-XMAILINFO: NsLYjltVP0elqDI1X0oKYnRX/djjuf11l4izikSIF+2b/dXqY/4dlPlibiBqfi skx1jaPoOMJu7BzK5bqhiDKCItsMT8NND8yJYKVaCGyOorswbnpogTPWEFoEHj/mcldg6EhDigB8 C9I7buLgdXsxwzjVdnO1fq980R7pzjzgFI85iT81KJPZnzcS4r4sAbkubc2MSD5wqfXpINyXCqvx 0kocPk+HVWZA/nk5ONHt2RqDaNdJMrK9F0jX1kSjrsy7AcWFutc3daeUVDTdIK/4D3Xih/WBeIpv PQCuLYwvRn+xq21nUG3hcrFvo/SQw9nQcUpOZJIxKLLaHJS5CV35g1XvceK3AIG4j3TPetS+Bh6y uDdQyjVtQNhzOHMlY2di9QUHVrUREGhIa1b3s6OsqzWlorpL1QHzTnFM57rQ8sGK+2sZvn3TD9W3 laHh3UBrB6NGqPrKTP7okSREFz+/qUmJu7jk6MAuMh/noaWCRG3Ks/YvU6mFbbQfRsDTWA0jVvKU 01OmgYEG6sHUuFEz76mO6H00ae8CgPRwEiG7hiyp2YrQ59IDOKkt+cH20XZgICobUXznKDoKdCkk ESHEmWKv3LiOoaH2fClfStzUpnZeFGJiJz2bxy+oqYQK5vN15KHSpoggfyAfp0fF6bVWKAxvYsEL A5FXvAYKzsE6HOAO4HOBmJMVXWH5HMiawZZPBXlGfiGv1mkh0sOYvRf6jLy/ECldRtceQJeF30IF Kfjx5CzlxTwcvq8HBPp/q2B0OddIAmoKjIFlMLQM4R4jC296wHE9G0XBq8UiNYzUoUlGHpLSoKyi m6wbrHQMS7/Cht65N6C4Tthmyd117IWMagk534wFCgwXd4buM//eFkDv6VL4zyM0LAQTDkp4jBju aU3+jugpGEOqUY05XpYOexvC1i5lAS7l+2uPtklgORDMd5aiHW1137rljgdL07+ivNmZEWhkzwOb /794pH/Xy2Cth2WlcYEfZGAsdYQo7023fXAam8YP94gAnAommVdw== X-QQ-XMRINFO: MPJ6Tf5t3I/ycC2BItcBVIA= From: Zhao Zhili To: ffmpeg-devel@ffmpeg.org Date: Fri, 7 Jul 2023 00:19:53 +0800 X-OQ-MSGID: <20230706161957.1758621-1-quinkblack@foxmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 1/5] avcodec/h264_mp4toannexb_bsf: refactor start_code_size handling X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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: Zhao Zhili Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 36VRRr8y5ZJ5 From: Zhao Zhili start_code_size depends on whether PS comes from out-of-band or in-band. Make the code more readable. --- libavcodec/h264_mp4toannexb_bsf.c | 34 ++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/libavcodec/h264_mp4toannexb_bsf.c b/libavcodec/h264_mp4toannexb_bsf.c index d11be455c2..7dce1ae9b6 100644 --- a/libavcodec/h264_mp4toannexb_bsf.c +++ b/libavcodec/h264_mp4toannexb_bsf.c @@ -43,10 +43,26 @@ typedef struct H264BSFContext { int extradata_parsed; } H264BSFContext; +enum PsSource { + PS_OUT_OF_BAND = -1, + PS_NONE = 0, + PS_IN_BAND = 1, +}; + static void count_or_copy(uint8_t **out, uint64_t *out_size, - const uint8_t *in, int in_size, int ps, int copy) + const uint8_t *in, int in_size, enum PsSource ps, int copy) { - uint8_t start_code_size = ps < 0 ? 0 : *out_size == 0 || ps ? 4 : 3; + uint8_t start_code_size; + + if (ps == PS_OUT_OF_BAND) + /* start code already present in out-of-band ps data, so don't need to + * add it manually again + */ + start_code_size = 0; + else if (ps == PS_IN_BAND || *out_size == 0) + start_code_size = 4; + else + start_code_size = 3; if (copy) { memcpy(*out + start_code_size, in, in_size); @@ -202,6 +218,7 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *opkt) do { uint32_t nal_size = 0; + enum PsSource ps; /* possible overread ok due to padding */ for (int i = 0; i < s->length_size; i++) @@ -230,7 +247,7 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *opkt) if (!s->sps_size) { LOG_ONCE(ctx, AV_LOG_WARNING, "SPS not present in the stream, nor in AVCC, stream may be unreadable\n"); } else { - count_or_copy(&out, &out_size, s->sps, s->sps_size, -1, j); + count_or_copy(&out, &out_size, s->sps, s->sps_size, PS_OUT_OF_BAND, j); sps_seen = 1; } } @@ -246,19 +263,22 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *opkt) if (new_idr && unit_type == H264_NAL_IDR_SLICE && !sps_seen && !pps_seen) { if (ctx->par_out->extradata) count_or_copy(&out, &out_size, ctx->par_out->extradata, - ctx->par_out->extradata_size, -1, j); + ctx->par_out->extradata_size, PS_OUT_OF_BAND, j); new_idr = 0; /* if only SPS has been seen, also insert PPS */ } else if (new_idr && unit_type == H264_NAL_IDR_SLICE && sps_seen && !pps_seen) { if (!s->pps_size) { LOG_ONCE(ctx, AV_LOG_WARNING, "PPS not present in the stream, nor in AVCC, stream may be unreadable\n"); } else { - count_or_copy(&out, &out_size, s->pps, s->pps_size, -1, j); + count_or_copy(&out, &out_size, s->pps, s->pps_size, PS_OUT_OF_BAND, j); } } - count_or_copy(&out, &out_size, buf, nal_size, - unit_type == H264_NAL_SPS || unit_type == H264_NAL_PPS, j); + if (unit_type == H264_NAL_SPS || unit_type == H264_NAL_PPS) + ps = PS_IN_BAND; + else + ps = PS_NONE; + count_or_copy(&out, &out_size, buf, nal_size, ps, j); if (!new_idr && unit_type == H264_NAL_SLICE) { new_idr = 1; sps_seen = 0;