From patchwork Thu Oct 17 08:29:37 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 15803 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 F28B4449FA2 for ; Thu, 17 Oct 2019 11:30:52 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D942968A768; Thu, 17 Oct 2019 11:30:52 +0300 (EEST) 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 0482868A702 for ; Thu, 17 Oct 2019 11:30:43 +0300 (EEST) Received: by mail-wm1-f65.google.com with SMTP id y21so1564685wmi.0 for ; Thu, 17 Oct 2019 01:30:42 -0700 (PDT) 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=Cv0q90brXRcZwk1VYf3ASwIUZ4lN7h7UpJwDh0j21bM=; b=jtZ2oRpLZ5Fn4PvWhmWlqKn7797Zs/JxF8TwLpi7+XDHgIQzqSMBj9ZUj8sReup/Bf UiFcIw4K/BNAQaPRcWBt9HhBq7yXiGqzKVZiUoAs94cibHsR2SKidwwnjoLhFn1FK1Lh 1LBUrQYvB86e4uHEoTfyEv4+L5lEa4mJUOROc4GIDCychH+cpwSu0/6FBivJ6Wpy2AST 73lq0oLTp+qzfu0D4i7fHLcLDJOX/u0FMGQveZG1fZpbCBHitJU6e84bwT8RsmJhi5sG jeHarpg2LzQ7dI3OQAn5ZMIR0fYJp4YUsIX4DTYA8s169svdtipMXsWNWcv6usWcILB/ /7kA== 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=Cv0q90brXRcZwk1VYf3ASwIUZ4lN7h7UpJwDh0j21bM=; b=Hz2ntbmYNATl9FwcIWxujYxlcpUvuAyRZwrgaAmTdmtVDInb5GjUyTyqSjFTbubfkc zKU04FOdgilX6LzlPFk1gRl5S6ygHlVGb22CIshGA2N28FSRKzERgI6TYR6YbifwDQwL BOxQod9fkLeHfhkxPY+xFCgfyI+W3oFwpnHDxqTMTiE4L8Y6dyIlwBg/a87YLI63YpqD C57zkzgdZvsPd0XPiCDtJJbUCdbQ7TNoSXH/EAR7Dp3fP9UyYaqBYof6dBnkDnECFHSk es7HtEs7oiKmpv4UHGsc99+IUlQxE09XYKVPzCgrv1O5LxQRIXcvLY8CUA21GMXhDwKX O6Aw== X-Gm-Message-State: APjAAAW9K0fDh+vI/U65MKymguGuDE0n493Bki1mKaAUmjLVTtibvObI QvAQMk+tHHobhUb9+x5USfkIM3Np X-Google-Smtp-Source: APXvYqwRw3rFeJJz3ztkuo0cTwgOgcrDbn1q+nkOyC/P5e3VOHSA/YRKxpd2vShWIEPfp9w1Z+AQAg== X-Received: by 2002:a1c:ac02:: with SMTP id v2mr1699783wme.85.1571301042218; Thu, 17 Oct 2019 01:30:42 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc08937.dynamic.kabel-deutschland.de. [188.192.137.55]) by smtp.gmail.com with ESMTPSA id l7sm1369273wrv.77.2019.10.17.01.30.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Oct 2019 01:30:41 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 17 Oct 2019 10:29:37 +0200 Message-Id: <20191017082945.13534-7-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191017082945.13534-1-andreas.rheinhardt@gmail.com> References: <20191017082945.13534-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 07/15] 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 270287dc1e..7db26595f0 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 {