From patchwork Sat Dec 14 22:19:19 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 16789 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 901A9448D61 for ; Sun, 15 Dec 2019 00:26:00 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 76E4668981F; Sun, 15 Dec 2019 00:26:00 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f65.google.com (mail-wm1-f65.google.com [209.85.128.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0C6E9688390 for ; Sun, 15 Dec 2019 00:25:51 +0200 (EET) Received: by mail-wm1-f65.google.com with SMTP id p17so2575906wma.1 for ; Sat, 14 Dec 2019 14:25:51 -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=2Cotwht7ORVHDf32R1lYUTEF8h5iMUn2rrqLhAxQjrk=; b=pYJqQwIlskHaIGQ5NEv5bQqAFgXY3pFzEDlTGVcDOtZ65XE1xrwTPAq1dHHTwwK3AP hMyfBgEoFGZ0YyPV668KPEWE7FXGfyisbMjWrdrABWxcx1n2CSeMMWRG9WS8L1s9ZScI 2rxHzkUcD9Zlp4x1MQVpGHWncvTk6in2rZo35Ur5pwVOF2+eYJzbF5m8EBGdFVg0guh0 lGanmNvOYmPIha36ARusiYjCrUID74ydXjjbhpRNTj5+Sv3LapxdMMd90in/b0Hi9j+R oyj+bvc3h3NLXk9A+toTAgjmgULAaKlpzznIrO512xZKJKyHsktcEfxEz8MVAaTJwn1H +HrA== 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=2Cotwht7ORVHDf32R1lYUTEF8h5iMUn2rrqLhAxQjrk=; b=Fj9Q2411bEazn9XBTP4+NmEFjTA9iXwPAv30yIC0pjZwmfYB6ifOAC009jPF4hXB0b l7ra7Nb7aRdYfXhgKimvRf3kmpSvI6DamBIN56Eg0hVLkWp1Mi3yKj2ZovMAzPoKyg1a s53GJMstc0PeQ6lrdPDU3cjTvx4QCmn+7bi/8zO1Cq191t1MvSdfaT42zgeVwvPpnSfl xXVz0Gfp/TGgXmwEUQJGtnVBLuO53ihTT0VFc6xLP9rdOuC/7xF7DXYVgnt1MKleAaJ3 oZvXsCBlzrqgThl6HHTz01TPAKzbii4UIwn3ch7LnmFkTImxhKSLqbJJPsLPB23ETlGT dNYg== X-Gm-Message-State: APjAAAXKkUTvQxmLslIf+cdHO3GkIBJTK4M4rVPBzmU7FU2pGPhPoTxp 6keTKd2oXHuStb+dK1PwNdxJgu96 X-Google-Smtp-Source: APXvYqwLmp/OwmqcxhbQ0pBiqriqll8INR1/kqJYIPJV4xN6rjRAF1P4M4nA5zVx5cVxx9LwaxWD1A== X-Received: by 2002:a7b:c1d8:: with SMTP id a24mr21717581wmj.130.1576362350377; Sat, 14 Dec 2019 14:25:50 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc08e23.dynamic.kabel-deutschland.de. [188.192.142.35]) by smtp.gmail.com with ESMTPSA id v3sm15063771wml.47.2019.12.14.14.25.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 Dec 2019 14:25:49 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 14 Dec 2019 23:19:19 +0100 Message-Id: <20191214221926.16074-7-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191214221926.16074-1-andreas.rheinhardt@gmail.com> References: <20191214221926.16074-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 07/14] h264_mp4toannexb: Simplify extradata insertion 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: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Up until now, h264_mp4toannexb stored the offset of the first SPS and the first PPS in the (output) extradata in its context and used these two numbers together with the size of the extradata and the pointer to the extradata to determine what to insert when inserting extradata. This led to some very long lines like "s->pps_offset != -1 ? s->pps_offset : ctx->par_out->extradata_size - s->sps_offset". Therefore now pointers to SPS and PPS are stored along with their respective sizes, so that e.g. the above line can be changed to "s->sps_size". Signed-off-by: Andreas Rheinhardt --- libavcodec/h264_mp4toannexb_bsf.c | 44 +++++++++++++++---------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/libavcodec/h264_mp4toannexb_bsf.c b/libavcodec/h264_mp4toannexb_bsf.c index cd6d73d118..561126c997 100644 --- a/libavcodec/h264_mp4toannexb_bsf.c +++ b/libavcodec/h264_mp4toannexb_bsf.c @@ -31,8 +31,10 @@ #include "h264.h" typedef struct H264BSFContext { - int32_t sps_offset; - int32_t pps_offset; + uint8_t *sps; + uint8_t *pps; + int sps_size; + int pps_size; uint8_t length_size; uint8_t new_idr; uint8_t idr_sps_seen; @@ -72,12 +74,9 @@ static int h264_extradata_to_annexb(AVBSFContext *ctx, const int padding) GetByteContext ogb, *gb = &ogb; uint16_t unit_size; uint32_t total_size = 0; - uint8_t *out = NULL, unit_nb, sps_done = 0, - sps_seen = 0, pps_seen = 0; + uint8_t *out = NULL, unit_nb, sps_done = 0; static const uint8_t nalu_header[4] = { 0, 0, 0, 1 }; - int length_size; - - s->sps_offset = s->pps_offset = -1; + int length_size, pps_offset = 0; bytestream2_init(gb, ctx->par_in->extradata, ctx->par_in->extradata_size); @@ -90,9 +89,6 @@ static int h264_extradata_to_annexb(AVBSFContext *ctx, const int padding) unit_nb = bytestream2_get_byteu(gb) & 0x1f; /* number of sps unit(s) */ if (!unit_nb) { goto pps; - } else { - s->sps_offset = 0; - sps_seen = 1; } while (unit_nb--) { @@ -115,25 +111,29 @@ static int h264_extradata_to_annexb(AVBSFContext *ctx, const int padding) pps: if (!unit_nb && !sps_done++) { unit_nb = bytestream2_get_byteu(gb); /* number of pps unit(s) */ - if (unit_nb) { - s->pps_offset = total_size; - pps_seen = 1; - } + pps_offset = total_size; } } if (out) memset(out + total_size, 0, padding); - if (!sps_seen) + if (pps_offset) { + s->sps = out; + s->sps_size = pps_offset; + } else { av_log(ctx, AV_LOG_WARNING, "Warning: SPS NALU missing or invalid. " "The resulting stream may not play.\n"); - - if (!pps_seen) + } + if (pps_offset < total_size) { + s->pps = out + pps_offset; + s->pps_size = total_size - pps_offset; + } else { av_log(ctx, AV_LOG_WARNING, "Warning: PPS NALU missing or invalid. " "The resulting stream may not play.\n"); + } av_freep(&ctx->par_out->extradata); ctx->par_out->extradata = out; @@ -220,12 +220,12 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out) s->idr_pps_seen = s->new_idr = 1; /* if SPS has not been seen yet, prepend the AVCC one to PPS */ if (!s->idr_sps_seen) { - if (s->sps_offset == -1) + if (!s->sps_size) av_log(ctx, AV_LOG_WARNING, "SPS not present in the stream, nor in AVCC, stream may be unreadable\n"); else { if ((ret = alloc_and_copy(out, - ctx->par_out->extradata + s->sps_offset, - s->pps_offset != -1 ? s->pps_offset : ctx->par_out->extradata_size - s->sps_offset, + s->sps, + s->sps_size, buf, nal_size, 1)) < 0) goto fail; s->idr_sps_seen = 1; @@ -249,12 +249,12 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out) s->new_idr = 0; /* if only SPS has been seen, also insert PPS */ } else if (s->new_idr && unit_type == H264_NAL_IDR_SLICE && s->idr_sps_seen && !s->idr_pps_seen) { - if (s->pps_offset == -1) { + if (!s->pps_size) { av_log(ctx, AV_LOG_WARNING, "PPS not present in the stream, nor in AVCC, stream may be unreadable\n"); if ((ret = alloc_and_copy(out, NULL, 0, buf, nal_size, 0)) < 0) goto fail; } else if ((ret = alloc_and_copy(out, - ctx->par_out->extradata + s->pps_offset, ctx->par_out->extradata_size - s->pps_offset, + s->pps, s->pps_size, buf, nal_size, 1)) < 0) goto fail; } else {