From patchwork Mon Nov 26 13:39:10 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 11168 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 C167244D285 for ; Mon, 26 Nov 2018 15:39:57 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4519868A649; Mon, 26 Nov 2018 15:39:58 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f66.google.com (mail-wm1-f66.google.com [209.85.128.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 05B6868A643 for ; Mon, 26 Nov 2018 15:39:52 +0200 (EET) Received: by mail-wm1-f66.google.com with SMTP id g67so5036200wmd.2 for ; Mon, 26 Nov 2018 05:39:56 -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=KRrpP4ikljT12Nz9qiMTnoiuKZAIqI5s+8yeZMiQoMY=; b=HyhSLcjbz17OxaztzAfwEzHC849rMZsCuRwVEZe7+xZF05uBLuiHtiCODpEkNXuU++ iq8YSgVNqF6WzkBu0+TTf3wKMrY7IEegKNdPaXYDZLTpzBe0iOAX/DSWaNLntZFwkD6K rKaTT+DR6AWfUJelPFhlKz8FwucNbgj3inRBYK4Rd1+5+W/fJDCB1E8lH/Bd1eCrVd0j Aley8utBQZW2mkIjas8XBpnJP/wBdmWS6xTtKyTXPe/AjnTVhcK/eqz7Z831+cKWirF/ KkBH3zcCCyDHIrSTHv190WKlxmOClgugzzEdHgy09vc1nEMX07pdn/8nHLwduO/idZ5k XthA== 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=KRrpP4ikljT12Nz9qiMTnoiuKZAIqI5s+8yeZMiQoMY=; b=hO+GumMNiBL+NX+qrG5bu/2RyLN99qiQL2uK481692WW70VDLMssUsOLqGxJPUpmTl iV65Swqf29jyyKdVXT3A7iGJ0+BgbSaxQ4YEWEZmH6+QOwlu1QDprzCwbGaiHENDWHGm 725/tjERO5N2xyKzj5mtP7pUHL5dtkDrAEOvXckhG2L7D630JG5so7TC1iQyUNuStAuG E9lepInLN2yVoGb43oPhgT/M5g3uG/Vgs4SSR8UBzobQz3HHONFQIzkKfHS5ubYnsl/3 ELEjHxEBVHpyXqcykxcY5ijH2zfejOilmsSmCN61aw5ewfWO7AMyc+AS7GF/9s8IJwkE kzbQ== X-Gm-Message-State: AA+aEWZxxepzmbmsE9Sf486r9lw7e7NytC3VgPgd6QqSX/dmqDPkVoE+ DzgoHEJK0nhp3CdSyrqf/AHF8maQ X-Google-Smtp-Source: AJdET5eaBRKezzzeIu3CdHjXn+Ig171Lb6StL3g03UJUicOv4aH3TpJfjKGMd1wrVWF1Qw+fWYhulg== X-Received: by 2002:a1c:aa92:: with SMTP id t140mr23605826wme.36.1543239595852; Mon, 26 Nov 2018 05:39:55 -0800 (PST) Received: from localhost.localdomain (ipbcc08c44.dynamic.kabel-deutschland.de. [188.192.140.68]) by smtp.googlemail.com with ESMTPSA id 142sm1184169wmw.27.2018.11.26.05.39.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 26 Nov 2018 05:39:55 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 26 Nov 2018 14:39:10 +0100 Message-Id: <20181126133913.2356-2-andreas.rheinhardt@googlemail.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: References: MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/5] cbs: Add a macro to create functions for deep copying 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" Some structs in cbs (most importantly some of the parameter sets of H.264/HEVC) can contain external buffers so that shallow copies are not enough for them; furthermore, they need a special free-callback function. The macro provided in this commit can be used to easily create functions both for copying as well as freeing such structures. Signed-off-by: Andreas Rheinhardt --- libavcodec/cbs_internal.h | 53 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/libavcodec/cbs_internal.h b/libavcodec/cbs_internal.h index 62a836af90..dd45748c61 100644 --- a/libavcodec/cbs_internal.h +++ b/libavcodec/cbs_internal.h @@ -98,4 +98,57 @@ extern const CodedBitstreamType ff_cbs_type_mpeg2; extern const CodedBitstreamType ff_cbs_type_vp9; +enum { + BITS, + BYTES +}; + +// The following macro automatically creates both (deep) copy and +// free functions for structs with exactly one internal buffer. + +#define cbs_copy_free(codec, type, var, buffer, size_element, size_offset, size_unit) \ +static void cbs_ ## codec ## _free_ ## var(void *unit, uint8_t *content) \ +{ \ + type *var = (type *)content; \ + \ + av_buffer_unref(&var->buffer ## _ref); \ + av_freep(&var); \ +} \ + \ +static AVBufferRef *cbs_ ## codec ## _copy_ ## var(const type *source) \ +{ \ + AVBufferRef *copy_ref; \ + type *copy; \ + \ + copy = av_malloc(sizeof(type)); \ + if (!copy) \ + return NULL; \ + memcpy(copy, source, sizeof(type)); \ + \ + copy_ref = av_buffer_create((uint8_t*)copy, sizeof(type), \ + &cbs_ ## codec ## _free_ ## var, \ + NULL, 0); \ + if (!copy_ref) { \ + av_free(copy); \ + return NULL; \ + } \ + \ + if (source->buffer) { \ + size_t size = (size_t)source->size_element + size_offset; \ + if (size_unit == BITS) \ + size = (size + 7) / 8; \ + \ + copy->buffer ## _ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE); \ + if (!copy->buffer ## _ref) {\ + av_buffer_unref(©_ref); \ + return NULL; \ + } \ + copy->buffer = copy->buffer ## _ref->data; \ + memcpy(copy->buffer, source->buffer, size); \ + memset(copy->buffer + size, 0, AV_INPUT_BUFFER_PADDING_SIZE); \ + } \ + \ + return copy_ref; \ +} + #endif /* AVCODEC_CBS_INTERNAL_H */