From patchwork Mon Jun 17 03:42:06 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 13571 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 8B9DB447BCA for ; Mon, 17 Jun 2019 06:49:37 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 6F69868A191; Mon, 17 Jun 2019 06:49:37 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f65.google.com (mail-wr1-f65.google.com [209.85.221.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D2C926801A4 for ; Mon, 17 Jun 2019 06:49:36 +0300 (EEST) Received: by mail-wr1-f65.google.com with SMTP id p11so8250446wre.7 for ; Sun, 16 Jun 2019 20:49:36 -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=WGpkK9IX+8elu1seY6QTEYF6PPl87hZARViEH3k6NK8=; b=rg/dmiFWgRsTKmfmzQiV5gKetOPfR3sAAknDl5e00aSds3NrSVfc73RbLK8Tq1kSeV oH35DjJga2brchmx9HVt26riIcll1uPI2y3qXbfs1UQ0S/sxE+Uz+QGcvtuGzWTgDE3D hXEVpmxoxeVdRJXiRsn8UZR1pVAT81COM8gbQ6uShif3XG8rdsOUfe1g+S4p952u+yQH ILproNQC8ZAPKk4I3+GMMD3Zlb8+i2m2kddqAp7GY/bqiyLPLdrK1b8M9f2zJJxeMQNd 8Thjz2vTZrPwKBWhyDQBrJQQu04OqWRyJIs+sE8XV1RMtQ2tToye3f2t9B2K149D/jML 4fKQ== 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=WGpkK9IX+8elu1seY6QTEYF6PPl87hZARViEH3k6NK8=; b=BZW1QtenVAcBOMFNsyP84h8TpdnB/OTfL0Y3zMUVoHP++5QQ8SilW7MKdZ98MzQEbw r5UofsotycpFywD87LX6CdfBp8NfE+Lz17IUsMcDiDf2fiN2Q4ejNZJfjN377tR2MOhC 3HtXB1vM/HUpCdYvQymj1diFv+07HcyIf4SMSdCSIwgiI8tvfCLeyBfWAIt3QM5DPoYW YR8KxVmznevcdsxIDWzsUjgvlhO+p54M7yoRxy9dYh8f0YWQ84uZc+NnsgPNx7lTI8+J 6Ds5p3RLYV0itdJZ4b4Uw1Mtqc5VPiu5s8F7iUKHl1emOVPRMOCCc60IfET/3soSJwlI Cuhg== X-Gm-Message-State: APjAAAUQdqyPbv3T728CF/WT3DInGBEMT1OHyUP4oZvEzraiIdpB5Rh/ egCFDmG9u96XExpcWLy958K9mFpu X-Google-Smtp-Source: APXvYqzmSU6Jaw7VlgUr+egI3az6DM4w93tb9e67McX2zM4gAFX0lnlKO6khlz65oGMN1NEXh0CKZQ== X-Received: by 2002:a5d:4843:: with SMTP id n3mr22295448wrs.77.1560743033733; Sun, 16 Jun 2019 20:43:53 -0700 (PDT) Received: from localhost.localdomain (ipbcc063db.dynamic.kabel-deutschland.de. [188.192.99.219]) by smtp.gmail.com with ESMTPSA id x83sm9632889wmb.42.2019.06.16.20.43.52 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Sun, 16 Jun 2019 20:43:53 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 17 Jun 2019 05:42:06 +0200 Message-Id: <20190617034223.21195-2-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190617034223.21195-1-andreas.rheinhardt@gmail.com> References: <20190617034223.21195-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 01/18] cbs: Allow non-blank packets in ff_cbs_write_packet 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, ff_cbs_write_packet always initialized the packet structure it received without documenting this behaviour; furthermore, the packet's buffer would (on success) be overwritten with the new buffer without unreferencing the old. This meant that the input packet had to be either clean (otherwise there would be memleaks) in which case the initialization is redundant or uninitialized. ff_cbs_write_packet was never used with uninitialized packets, so the initialization was redundant. Worse yet, it forced callers to use more than one packet and made it difficult to add side-data to a packet designated for output, because said side-data could only be attached after the call to ff_cbs_write_packet. This has been changed. It is now allowed to use a non-blank packet. The currently existing buffer will be unreferenced and replaced by the new one, as will be the accompanying fields (i.e. data and size). The rest isn't touched at all. This change will enable us to use only one packet in the bitstream filters that rely on CBS. This commit also updates the documentation of ff_cbs_write_extradata and ff_cbs_write_packet (to better describe existing behaviour and in the latter case to also describe the new behaviour). Signed-off-by: Andreas Rheinhardt --- I could also have made it unref the packet's buffer at the beginning; this would have the advantage that the packet's buffer would be freed after the units have been rewritten (if they are rewritten) and after the fragment's buffer has been unreferenced, so that maximum memory consumption would decrease. It would also be in line with all current callers of ff_cbs_write_packet, but maybe it wouldn't be what a future caller wants. What do you think? libavcodec/cbs.c | 3 ++- libavcodec/cbs.h | 10 +++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/libavcodec/cbs.c b/libavcodec/cbs.c index 0260ba6f67..47679eca1b 100644 --- a/libavcodec/cbs.c +++ b/libavcodec/cbs.c @@ -357,7 +357,8 @@ int ff_cbs_write_packet(CodedBitstreamContext *ctx, if (!buf) return AVERROR(ENOMEM); - av_init_packet(pkt); + av_buffer_unref(&pkt->buf); + pkt->buf = buf; pkt->data = frag->data; pkt->size = frag->data_size; diff --git a/libavcodec/cbs.h b/libavcodec/cbs.h index 967dcd1468..5260a39c63 100644 --- a/libavcodec/cbs.h +++ b/libavcodec/cbs.h @@ -297,7 +297,8 @@ int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx, /** * Write the bitstream of a fragment to the extradata in codec parameters. * - * This replaces any existing extradata in the structure. + * Modifies context and fragment as ff_cbs_write_fragment_data does and + * replaces any existing extradata in the structure. */ int ff_cbs_write_extradata(CodedBitstreamContext *ctx, AVCodecParameters *par, @@ -305,6 +306,13 @@ int ff_cbs_write_extradata(CodedBitstreamContext *ctx, /** * Write the bitstream of a fragment to a packet. + * + * Modifies context and fragment as ff_cbs_write_fragment_data does. + * + * On success, the packet's buf is unreferenced and its buf, data and + * size fields are set to the corresponding values from the newly updated + * fragment; other fields are not touched. On failure, the packet is not + * touched at all. */ int ff_cbs_write_packet(CodedBitstreamContext *ctx, AVPacket *pkt,