From patchwork Fri May 19 16:41:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhao Zhili X-Patchwork-Id: 41721 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:ba91:b0:105:feb:71f2 with SMTP id fb17csp1130106pzb; Fri, 19 May 2023 01:41:48 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4lDjXyw3C/ycADE+tmTNVOuj+RoegLuMT+RLSOSAj0pXcI0cSOevwTTiJyBts0TpsONdSw X-Received: by 2002:a05:6402:35a:b0:50b:c4d5:3da1 with SMTP id r26-20020a056402035a00b0050bc4d53da1mr1759195edw.8.1684485707932; Fri, 19 May 2023 01:41:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684485707; cv=none; d=google.com; s=arc-20160816; b=mWLVavMKmO5lGVyGch50tThugK+boFQx2+ucs467NfUxQ+HZB2iFnVfeNQMuEl5UTy zxlc3ndL8KJ1b0bSfWZUNvizSntpTj8MqEUewG+gMFaIuTmI6OFU2gxdWdy0rAoFozVI XJflv/3iBW0Iw1ta78duMj1OwPRLA2ubNoSDPO+M+hNCHaCLVI6aBUuT2PO8ivKvX79s djm+9lbce9w02ag+cFKUh2j9bPxkifyY1DCz3eczPPKczi4kw1q/DkfWVXlXPFVx/VgN asqQXcZ185ZD6GMpn4skbWkRcCZBbPZojqupT3H5BRNNaWuwRcGms8JR5zbjGPf+kQAj 23xQ== 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:date:to:from:message-id :dkim-signature:delivered-to; bh=kLPfbk7yKzhKNCVYTmtL4DdY3rEtDdsEgMmobl2fEdM=; b=lgdBuL27aiHE6QKUA+Cs+y7xuwejNU9Doki+rARw0uiy64AS4lqjnEF5k6r5l3780l Wu1icrQOKU38pIzaUFzFw+s0IfnQnHIviDSfSlr6CghpCih70Gy2Zj3DJaZ2/CQHoPjB TfiQH/TU25L2L/jWz3Nh39MT7xF9CI3+hMObo7O/82dF4Hpz1r1rx2nwY+K5Q971t0hN rN3IlmkkNVsiglVLBvL81kVGHIQsO4s7HnzNCh8OA0HuQ3Z5cbEWD1Vf679ggSP5XnZt BBJCzZJM0h07oPGBtb54uk/Nw85LkKhzV9icPnG8fUv0OLfTH4RwLgy/TrZFrCoNNt8a X62A== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@foxmail.com header.s=s201512 header.b=fNXg97K4; 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 d14-20020aa7ce0e000000b0050bc56c1ff8si2427015edv.184.2023.05.19.01.41.47; Fri, 19 May 2023 01:41:47 -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=fNXg97K4; 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 DCFDD68C13A; Fri, 19 May 2023 11:41:42 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from out203-205-221-240.mail.qq.com (out203-205-221-240.mail.qq.com [203.205.221.240]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4C28E68C033 for ; Fri, 19 May 2023 11:41:35 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1684485688; bh=/6PgLCBuvjpceDHseb9tM7knBRzhCcIvMmBA9pCIsfA=; h=From:To:Cc:Subject:Date; b=fNXg97K4A3wr0Tz5nUiMpQ3V2TDwzCxkNzt0+BgsTDOgVjRm5tvHCEEHmD6ke27+D AAYY+C3PqoJ5TQvIYVHuVnjvcsyhRK19ie1FCQYVSpW5Y77ftdC4V1Z/trENTJ3ET4 x4lyXpXqXhq7jEsVfZozzUoH5X/JiDm17lSKyh00= Received: from localhost.localdomain ([119.147.10.201]) by newxmesmtplogicsvrszc2-0.qq.com (NewEsmtp) with SMTP id A5B2749A; Fri, 19 May 2023 16:41:27 +0800 X-QQ-mid: xmsmtpt1684485687txk1z1i1p Message-ID: X-QQ-XMAILINFO: OCfk4Ew1/b2Lu/iiFlDI8aClYJZxZg0TwU+VMFJlm3vrWsmH2/2M+6/2zr+c9j 6EsK7e/UkNL3f5bDlNIh1hSXWx5PgFn9Jf44VR4yi8FOguxyMqCv9jK9H3x+58t3IPEyQ/xy6U7p f3Z0sWeW3sD+X8EPp7GDd1PuKevwFcniEjcqQhRI/jszqljKse3UFYKT3z+Q9Ah6Ioc0uZ9hrqiv IAvJJVsjadIE0Uxt2QG6Zif+tPk/ez94NFbdauLFFlWclt2USgqlcfDStpy6KZGpOoiYoGk0dKwk 05D/xHm1eP7BXWnVtn06QH3+DlvHjMorIHt8uJjWupWT05P9mVf4ReQ2abNwuKFqp1NxPtuxbMYz mXH4+tQ7IlS716c3WB8KRU6aSkiAZJsmkeu4YMwPH4Hi4PU9e7RNRy8JFm8Ho2EQj5/9QrF0NZnU CIQM3EXzTC5YFwuDe8MoEFALSD2hByTiddquUhYcerHOmj06MHvj9dptAFzv5SFDirTNITrSzK9Y gw12+z5IspaJiQEz9JtgKS280OJjZsJ9RPNxKzDQr3RnNXI7yoMArxHoXFk/ji+S0DMj1ADP6zch QPOaKFcS/E0bIs6OJmj3ypqYdvnbsS0TsoA+uj9nHpwoJbsmo9z3ZW4uwMK7h/nSyk+6+Kkw4yzc 9FMSRUHo8Vqv2ttIuofIKoX6qevsvpaCfiKu2PuB2q5uaBLdrhPGSjxVnr1N8Uk6Xfhg+bQ+9gvy chbXZ3cYBpuxi/rtDbiYB6y1phK1C2Gi5iKC0XMX64kepfZmw0C1clp1my5YUGDmS65zh15wzSrD 2kt3qY4NpdqeBEZg/xbh/rAGGumXFdHeae5eFIeMck8FDwfhMtkHCuiSwWjEjxFVPja2IvDftq+/ fuTpXG2ZWgTuCuK2aNdFaxBkSpCdhZRT1DlvkKZpiFJ1K5cc+4dFo899QzYrGXJsqDZjdUKvEi7H +CqZC2CJBHQHiNzzjqA0BZfv4Cz81YgXetY9FKyZPki8EHo006WA== From: Zhao Zhili To: ffmpeg-devel@ffmpeg.org Date: Sat, 20 May 2023 00:41:29 +0800 X-OQ-MSGID: <20230519164133.3278597-1-quinkblack@foxmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 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: vE+RrRyzUOUY 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;