From patchwork Tue Feb 5 20:08:51 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 11977 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 2C710448ED3 for ; Tue, 5 Feb 2019 22:18:39 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 137EA68AB11; Tue, 5 Feb 2019 22:18:39 +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 CABEB68A9CE for ; Tue, 5 Feb 2019 22:18:32 +0200 (EET) Received: by mail-wm1-f65.google.com with SMTP id q21so288574wmc.5 for ; Tue, 05 Feb 2019 12:18:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=dGq4Xfi1ohBfRyHChVUw/NiotqPzwV/MnDOgIwWMsE4=; b=Vmui/Nccg0sznFQFfdsnFxLTSEVC+fjYeOwJjlsdFDy7Gk9x7VUT//ARo2j52M21e3 MmADewkCs28OAE/2sj81EtkoN3OXVhTYj0+2s0mEy4HfaEyYgpxQn1wP6bNIoZM+5c2T C4vfzYIaQQ/0OMRd0kg4e3N308YKQEjli0Ik78qFkxZU+pWFCHhSdDhJymqxw/vAaOlO EiXSnNopPptfsMvOvG9v/NyYLtVaVwOv/SLEtKWMfIPUalEqPqXNqKXhEqI58YxUm0L0 kCZHyT1WHBMP40jfKdkRd/YGV3wJEmI9EWKHxskaIPvq8//z81iKsP3CysR6mKl0XLQS 7tLQ== 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=dGq4Xfi1ohBfRyHChVUw/NiotqPzwV/MnDOgIwWMsE4=; b=bBblIUI9tF5003Ip4bxnfAvTM79gNpGwQwaYjefCmYorTDrEg79ZxQUXcBt/Syh6+9 n0BKrj2nmYzanJa2M4CoWrTG1/BZVUb3LjobZDBbC6gKxi6tfeOrxfzNbtYkQISegxu2 0gTpiyM+sq4n+TGt3vzQ/S3+RGvA+PoPDK64d5CScfIx9BSYCNqxleI+Xn/N783ve+eZ aWA3NbdWe3LhkRl8l9pM1FMRu/ytkj61KxTffNGCcjsR0ZP1h4Fua8Q0D3aJrfEuq2Wh apexKC+gFKde7SBfGLBO2swp8UDaAmK03W74ME2WDUykEJnkmQ1zp+yeXapDTdbixVSi s/uQ== X-Gm-Message-State: AHQUAuaNgojdYCdvrwldBXKqwMYxMhuWjvWfizxFshgvR0jaKH9EoBJx 5m/XQlTYla6ifAA25zlBCj057i0+o+4= X-Google-Smtp-Source: AHgI3IYcjcLkokQ/ziiULI+/b3HpIiTcOwlUXGW/5XjW4+HXoLWOWQ0Z3KxafR3ZodglnGKTl6nWiA== X-Received: by 2002:a7b:c5d1:: with SMTP id n17mr318851wmk.152.1549397422403; Tue, 05 Feb 2019 12:10:22 -0800 (PST) Received: from localhost.localdomain (ipbcc08c44.dynamic.kabel-deutschland.de. [188.192.140.68]) by smtp.googlemail.com with ESMTPSA id x3sm25570144wrd.19.2019.02.05.12.10.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 05 Feb 2019 12:10:21 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Tue, 5 Feb 2019 21:08:51 +0100 Message-Id: <20190205200852.2160-3-andreas.rheinhardt@googlemail.com> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20190205200852.2160-1-andreas.rheinhardt@googlemail.com> References: <20190205200852.2160-1-andreas.rheinhardt@googlemail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/3] libavcodec/cbs: Don't zero twice 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, a fragment that got reused was zeroed twice: Once during uninit and once during reading the next packet/extradata/buffer. The second zeroing has now been made optional. This is also in preparation of actually reusing a fragment's units array. Otherwise it would be lost during reading. Signed-off-by: Andreas Rheinhardt --- libavcodec/av1_metadata_bsf.c | 4 ++-- libavcodec/av1_parser.c | 4 ++-- libavcodec/cbs.c | 17 +++++++++++------ libavcodec/cbs.h | 20 +++++++++++++++++--- libavcodec/filter_units_bsf.c | 4 ++-- libavcodec/h264_metadata_bsf.c | 4 ++-- libavcodec/h264_redundant_pps_bsf.c | 4 ++-- libavcodec/h265_metadata_bsf.c | 4 ++-- libavcodec/mpeg2_metadata_bsf.c | 4 ++-- libavcodec/trace_headers_bsf.c | 4 ++-- libavcodec/vp9_metadata_bsf.c | 2 +- 11 files changed, 45 insertions(+), 26 deletions(-) diff --git a/libavcodec/av1_metadata_bsf.c b/libavcodec/av1_metadata_bsf.c index c3c56afeab..b08a1379e7 100644 --- a/libavcodec/av1_metadata_bsf.c +++ b/libavcodec/av1_metadata_bsf.c @@ -126,7 +126,7 @@ static int av1_metadata_filter(AVBSFContext *bsf, AVPacket *out) if (err < 0) return err; - err = ff_cbs_read_packet(ctx->cbc, frag, in); + err = ff_cbs_read_packet(ctx->cbc, frag, in, 1); if (err < 0) { av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n"); goto fail; @@ -191,7 +191,7 @@ static int av1_metadata_init(AVBSFContext *bsf) return err; if (bsf->par_in->extradata) { - err = ff_cbs_read_extradata(ctx->cbc, frag, bsf->par_in); + err = ff_cbs_read_extradata(ctx->cbc, frag, bsf->par_in, 1); if (err < 0) { av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n"); goto fail; diff --git a/libavcodec/av1_parser.c b/libavcodec/av1_parser.c index d78e4b3f3a..071227eed6 100644 --- a/libavcodec/av1_parser.c +++ b/libavcodec/av1_parser.c @@ -66,7 +66,7 @@ static int av1_parser_parse(AVCodecParserContext *ctx, if (avctx->extradata_size && !s->parsed_extradata) { s->parsed_extradata = 1; - ret = ff_cbs_read(s->cbc, td, avctx->extradata, avctx->extradata_size); + ret = ff_cbs_read(s->cbc, td, avctx->extradata, avctx->extradata_size, 1); if (ret < 0) { av_log(avctx, AV_LOG_ERROR, "Failed to parse extradata.\n"); goto end; @@ -75,7 +75,7 @@ static int av1_parser_parse(AVCodecParserContext *ctx, ff_cbs_fragment_uninit(s->cbc, td, 1); } - ret = ff_cbs_read(s->cbc, td, data, size); + ret = ff_cbs_read(s->cbc, td, data, size, 1); if (ret < 0) { av_log(avctx, AV_LOG_ERROR, "Failed to parse temporal unit.\n"); goto end; diff --git a/libavcodec/cbs.c b/libavcodec/cbs.c index b61dedb1eb..71f9fcbe32 100644 --- a/libavcodec/cbs.c +++ b/libavcodec/cbs.c @@ -217,11 +217,13 @@ static int cbs_fill_fragment_data(CodedBitstreamContext *ctx, int ff_cbs_read_extradata(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, - const AVCodecParameters *par) + const AVCodecParameters *par, + int reuse) { int err; - memset(frag, 0, sizeof(*frag)); + if (!reuse) + memset(frag, 0, sizeof(*frag)); err = cbs_fill_fragment_data(ctx, frag, par->extradata, par->extradata_size); @@ -237,11 +239,12 @@ int ff_cbs_read_extradata(CodedBitstreamContext *ctx, int ff_cbs_read_packet(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, - const AVPacket *pkt) + const AVPacket *pkt, int reuse) { int err; - memset(frag, 0, sizeof(*frag)); + if (!reuse) + memset(frag, 0, sizeof(*frag)); if (pkt->buf) { frag->data_ref = av_buffer_ref(pkt->buf); @@ -266,11 +269,13 @@ int ff_cbs_read_packet(CodedBitstreamContext *ctx, int ff_cbs_read(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, - const uint8_t *data, size_t size) + const uint8_t *data, size_t size, + int reuse) { int err; - memset(frag, 0, sizeof(*frag)); + if (!reuse) + memset(frag, 0, sizeof(*frag)); err = cbs_fill_fragment_data(ctx, frag, data, size); if (err < 0) diff --git a/libavcodec/cbs.h b/libavcodec/cbs.h index 229cb129aa..2265b5d5bd 100644 --- a/libavcodec/cbs.h +++ b/libavcodec/cbs.h @@ -240,10 +240,15 @@ void ff_cbs_close(CodedBitstreamContext **ctx); * This also updates the internal state, so will need to be called for * codecs with extradata to read parameter sets necessary for further * parsing even if the fragment itself is not desired. + * + * If reuse is not set, the fragment will be zeroed before usage; + * otherwise, the fragment's units_allocated and units members must + * be valid and all the other members have to be zero/NULL. */ int ff_cbs_read_extradata(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, - const AVCodecParameters *par); + const AVCodecParameters *par, + int reuse); /** * Read the data bitstream from a packet into a fragment, then @@ -252,10 +257,14 @@ int ff_cbs_read_extradata(CodedBitstreamContext *ctx, * This also updates the internal state of the coded bitstream context * with any persistent data from the fragment which may be required to * read following fragments (e.g. parameter sets). + * + * If reuse is not set, the fragment will be zeroed before usage; + * otherwise, the fragment's units_allocated and units members must + * be valid and all the other members have to be zero/NULL. */ int ff_cbs_read_packet(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, - const AVPacket *pkt); + const AVPacket *pkt, int reuse); /** * Read a bitstream from a memory region into a fragment, then @@ -264,10 +273,15 @@ int ff_cbs_read_packet(CodedBitstreamContext *ctx, * This also updates the internal state of the coded bitstream context * with any persistent data from the fragment which may be required to * read following fragments (e.g. parameter sets). + * + * If reuse is not set, the fragment will be zeroed before usage; + * otherwise, the fragment's units_allocated and units members must + * be valid and all the other members have to be zero/NULL. */ int ff_cbs_read(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, - const uint8_t *data, size_t size); + const uint8_t *data, size_t size, + int reuse); /** diff --git a/libavcodec/filter_units_bsf.c b/libavcodec/filter_units_bsf.c index a3b25cb944..9980601ce5 100644 --- a/libavcodec/filter_units_bsf.c +++ b/libavcodec/filter_units_bsf.c @@ -116,7 +116,7 @@ static int filter_units_filter(AVBSFContext *bsf, AVPacket *out) return 0; } - err = ff_cbs_read_packet(ctx->cbc, frag, in); + err = ff_cbs_read_packet(ctx->cbc, frag, in, 1); if (err < 0) { av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n"); goto fail; @@ -201,7 +201,7 @@ static int filter_units_init(AVBSFContext *bsf) if (bsf->par_in->extradata) { CodedBitstreamFragment ps; - err = ff_cbs_read_extradata(ctx->cbc, &ps, bsf->par_in); + err = ff_cbs_read_extradata(ctx->cbc, &ps, bsf->par_in, 0); if (err < 0) { av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n"); } else { diff --git a/libavcodec/h264_metadata_bsf.c b/libavcodec/h264_metadata_bsf.c index c4cfc6094f..53ba623b70 100644 --- a/libavcodec/h264_metadata_bsf.c +++ b/libavcodec/h264_metadata_bsf.c @@ -291,7 +291,7 @@ static int h264_metadata_filter(AVBSFContext *bsf, AVPacket *out) if (err < 0) return err; - err = ff_cbs_read_packet(ctx->cbc, au, in); + err = ff_cbs_read_packet(ctx->cbc, au, in, 1); if (err < 0) { av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n"); goto fail; @@ -625,7 +625,7 @@ static int h264_metadata_init(AVBSFContext *bsf) return err; if (bsf->par_in->extradata) { - err = ff_cbs_read_extradata(ctx->cbc, au, bsf->par_in); + err = ff_cbs_read_extradata(ctx->cbc, au, bsf->par_in, 1); if (err < 0) { av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n"); goto fail; diff --git a/libavcodec/h264_redundant_pps_bsf.c b/libavcodec/h264_redundant_pps_bsf.c index 1c929cd44b..1df93dd121 100644 --- a/libavcodec/h264_redundant_pps_bsf.c +++ b/libavcodec/h264_redundant_pps_bsf.c @@ -78,7 +78,7 @@ static int h264_redundant_pps_filter(AVBSFContext *bsf, AVPacket *out) if (err < 0) return err; - err = ff_cbs_read_packet(ctx->input, au, in); + err = ff_cbs_read_packet(ctx->input, au, in, 1); if (err < 0) goto fail; @@ -143,7 +143,7 @@ static int h264_redundant_pps_init(AVBSFContext *bsf) ctx->global_pic_init_qp = 26; if (bsf->par_in->extradata) { - err = ff_cbs_read_extradata(ctx->input, au, bsf->par_in); + err = ff_cbs_read_extradata(ctx->input, au, bsf->par_in, 1); if (err < 0) { av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n"); goto fail; diff --git a/libavcodec/h265_metadata_bsf.c b/libavcodec/h265_metadata_bsf.c index 0ad2ea80b8..53d35f0e09 100644 --- a/libavcodec/h265_metadata_bsf.c +++ b/libavcodec/h265_metadata_bsf.c @@ -241,7 +241,7 @@ static int h265_metadata_filter(AVBSFContext *bsf, AVPacket *out) if (err < 0) return err; - err = ff_cbs_read_packet(ctx->cbc, au, in); + err = ff_cbs_read_packet(ctx->cbc, au, in, 1); if (err < 0) { av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n"); goto fail; @@ -342,7 +342,7 @@ static int h265_metadata_init(AVBSFContext *bsf) return err; if (bsf->par_in->extradata) { - err = ff_cbs_read_extradata(ctx->cbc, au, bsf->par_in); + err = ff_cbs_read_extradata(ctx->cbc, au, bsf->par_in, 1); if (err < 0) { av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n"); goto fail; diff --git a/libavcodec/mpeg2_metadata_bsf.c b/libavcodec/mpeg2_metadata_bsf.c index 420b620f6a..43e6ac8c15 100644 --- a/libavcodec/mpeg2_metadata_bsf.c +++ b/libavcodec/mpeg2_metadata_bsf.c @@ -190,7 +190,7 @@ static int mpeg2_metadata_filter(AVBSFContext *bsf, AVPacket *out) if (err < 0) return err; - err = ff_cbs_read_packet(ctx->cbc, frag, in); + err = ff_cbs_read_packet(ctx->cbc, frag, in, 1); if (err < 0) { av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n"); goto fail; @@ -234,7 +234,7 @@ static int mpeg2_metadata_init(AVBSFContext *bsf) return err; if (bsf->par_in->extradata) { - err = ff_cbs_read_extradata(ctx->cbc, frag, bsf->par_in); + err = ff_cbs_read_extradata(ctx->cbc, frag, bsf->par_in, 1); if (err < 0) { av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n"); goto fail; diff --git a/libavcodec/trace_headers_bsf.c b/libavcodec/trace_headers_bsf.c index f9667f0456..c6a47e6d6d 100644 --- a/libavcodec/trace_headers_bsf.c +++ b/libavcodec/trace_headers_bsf.c @@ -48,7 +48,7 @@ static int trace_headers_init(AVBSFContext *bsf) av_log(bsf, AV_LOG_INFO, "Extradata\n"); - err = ff_cbs_read_extradata(ctx->cbc, &ps, bsf->par_in); + err = ff_cbs_read_extradata(ctx->cbc, &ps, bsf->par_in, 0); ff_cbs_fragment_uninit(ctx->cbc, &ps, 1); } @@ -92,7 +92,7 @@ static int trace_headers(AVBSFContext *bsf, AVPacket *pkt) av_log(bsf, AV_LOG_INFO, "Packet: %d bytes%s.\n", pkt->size, tmp); - err = ff_cbs_read_packet(ctx->cbc, &au, pkt); + err = ff_cbs_read_packet(ctx->cbc, &au, pkt, 0); ff_cbs_fragment_uninit(ctx->cbc, &au, 1); diff --git a/libavcodec/vp9_metadata_bsf.c b/libavcodec/vp9_metadata_bsf.c index b275a07800..26f1e65f20 100644 --- a/libavcodec/vp9_metadata_bsf.c +++ b/libavcodec/vp9_metadata_bsf.c @@ -48,7 +48,7 @@ static int vp9_metadata_filter(AVBSFContext *bsf, AVPacket *out) if (err < 0) return err; - err = ff_cbs_read_packet(ctx->cbc, frag, in); + err = ff_cbs_read_packet(ctx->cbc, frag, in, 1); if (err < 0) { av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n"); goto fail;