From patchwork Thu Oct 17 08:29:39 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 15808 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 14E87449FA2 for ; Thu, 17 Oct 2019 11:30:58 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 04CE768A7EB; Thu, 17 Oct 2019 11:30:58 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f68.google.com (mail-wr1-f68.google.com [209.85.221.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A72BE68A753 for ; Thu, 17 Oct 2019 11:30:44 +0300 (EEST) Received: by mail-wr1-f68.google.com with SMTP id w18so718793wrt.3 for ; Thu, 17 Oct 2019 01:30:44 -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=MvYx6NKfnNGS3aJ3CZVuwDyMVswX7p9UmCXYJ5c54BE=; b=PCW/zhYscSrv7JMT5T9FsSA5h1ULvxFO6rIVtb16uGp0MaKYKNb7KGDfA/mMPr72gT RkUAM3kCWzVrTcnbprV1J9RBw06dcx1gucPvXj8Hb/ryyyUtGaPn3DTvRmVJeJyio22F b5kHc/HBA/joW6te6d1KWmuqIG9pZpBuAsG7J7bbol4iQdbsbOFVJD83IB12WbyEUlQ6 pKsMnavFkrCbs+9xIF4+yCJMsyc802kBrf/e1i6OHZs6xx9eqniz3j/EyZneG4TJTqvE IriNgDpefShGfN5OeK7hiSqo1tAd7Qc31efMFL8s69aGIL2C6sumAM/zWWY1+Idhrgub fWxg== 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=MvYx6NKfnNGS3aJ3CZVuwDyMVswX7p9UmCXYJ5c54BE=; b=l3alqtLcTlOlQaxVdNbdSOiJ3Q9ijTtSo01IGqB0T3wgmmJ9crpaWBhOfJqiZ7S+KP 5EkL20a8IRY2B81sI5QMx7BD0R+hxogOPkN7HplUM1fGlGKI7v7uLydUj7YTJnJeSKcn HqI2QsxcdJZ7kXRN/igy6giaXJ7N6p3vv35jcTRq3RCKxb7Z0PE1yVKsfK9pu/P6Miot AlrW22A4EdOREHWsUMd+/9Zj5IjHHteBermAPLdtB0mbBiSMQL0l/3JWcf3HzX6mjzYE 3CabG6cjsaXqenO5ZN2se8h1sbElpXppN4SwiW6d5a6WRN8iSH+5HA0dKDH2k1JhYlPS byFA== X-Gm-Message-State: APjAAAVHtg4gPDAnUzehWIAZ6IvxZszU2/sJWhz96vD+FOKuaDrVLpO+ XpXCGaB+9DSSdj5MgmSKVauReVIZ X-Google-Smtp-Source: APXvYqx2ndTJaCmnS3J/yP84LpQARdeKt7BVcSHJPLgZj/U0eTcMmisHIh+DfIlEL3+EENXFDUKLJg== X-Received: by 2002:a5d:65c1:: with SMTP id e1mr1895362wrw.364.1571301043902; Thu, 17 Oct 2019 01:30:43 -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.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Oct 2019 01:30:43 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 17 Oct 2019 10:29:39 +0200 Message-Id: <20191017082945.13534-9-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 09/15] h264_mp4toannexb: Copy one NAL unit at a time 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" If processing an input NAL unit triggers the insertion of data from extradata in front of said NAL unit, the output packet is grown (i.e. reallocated) once to accomodate both the new extradata as well as the input NAL unit itself; this has been changed: In such a situation, the packet is now grown twice. While this is bad for performance, it allows to simplify the code and ultimately to stop reallocating the packet altogether. Signed-off-by: Andreas Rheinhardt --- libavcodec/h264_mp4toannexb_bsf.c | 37 +++++++++++++------------------ 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/libavcodec/h264_mp4toannexb_bsf.c b/libavcodec/h264_mp4toannexb_bsf.c index 718878c9f2..7f46644294 100644 --- a/libavcodec/h264_mp4toannexb_bsf.c +++ b/libavcodec/h264_mp4toannexb_bsf.c @@ -43,26 +43,23 @@ typedef struct H264BSFContext { } H264BSFContext; static int alloc_and_copy(AVPacket *out, - const uint8_t *sps_pps, uint32_t sps_pps_size, const uint8_t *in, uint32_t in_size, int ps) { uint32_t offset = out->size; - uint8_t start_code_size = offset == 0 && sps_pps_size == 0 || ps ? 4 : 3; + uint8_t start_code_size = ps < 0 ? 0 : offset == 0 || ps ? 4 : 3; int err; - err = av_grow_packet(out, sps_pps_size + in_size + start_code_size); + err = av_grow_packet(out, in_size + start_code_size); if (err < 0) return err; - if (sps_pps) - memcpy(out->data + offset, sps_pps, sps_pps_size); - memcpy(out->data + sps_pps_size + start_code_size + offset, in, in_size); + memcpy(out->data + start_code_size + offset, in, in_size); if (start_code_size == 4) { - AV_WB32(out->data + offset + sps_pps_size, 1); - } else { - (out->data + offset + sps_pps_size)[0] = - (out->data + offset + sps_pps_size)[1] = 0; - (out->data + offset + sps_pps_size)[2] = 1; + AV_WB32(out->data + offset, 1); + } else if (start_code_size) { + (out->data + offset)[0] = + (out->data + offset)[1] = 0; + (out->data + offset)[2] = 1; } return 0; @@ -226,10 +223,9 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out) if ((ret = alloc_and_copy(out, s->sps, s->sps_size, - buf, nal_size, 1)) < 0) + -1)) < 0) goto fail; s->idr_sps_seen = 1; - goto next_nal; } } } @@ -242,32 +238,29 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out) /* prepend only to the first type 5 NAL unit of an IDR picture, if no sps/pps are already present */ if (s->new_idr && unit_type == H264_NAL_IDR_SLICE && !s->idr_sps_seen && !s->idr_pps_seen) { - if ((ret=alloc_and_copy(out, + if (ctx->par_out->extradata && (ret=alloc_and_copy(out, ctx->par_out->extradata, ctx->par_out->extradata_size, - buf, nal_size, 0)) < 0) + -1)) < 0) goto fail; 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_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, s->pps, s->pps_size, - buf, nal_size, 0)) < 0) + -1)) < 0) goto fail; - } else { - if ((ret=alloc_and_copy(out, NULL, 0, buf, nal_size, unit_type == H264_NAL_SPS || unit_type == H264_NAL_PPS)) < 0) + } + + if ((ret=alloc_and_copy(out, buf, nal_size, unit_type == H264_NAL_SPS || unit_type == H264_NAL_PPS)) < 0) goto fail; if (!s->new_idr && unit_type == H264_NAL_SLICE) { s->new_idr = 1; s->idr_sps_seen = 0; s->idr_pps_seen = 0; } - } -next_nal: buf += nal_size; cumul_size += nal_size + s->length_size; } while (cumul_size < buf_size);