From patchwork Sat May 30 16:05:28 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 20005 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 299A144A6AD for ; Sat, 30 May 2020 19:06:51 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1336C68B075; Sat, 30 May 2020 19:06:51 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7120D68B06C for ; Sat, 30 May 2020 19:06:40 +0300 (EEST) Received: by mail-wr1-f53.google.com with SMTP id q11so7235032wrp.3 for ; Sat, 30 May 2020 09:06:40 -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=Q6BXeLowgHVOEAzvS3zejF0hX4h0QSs0WpSTmQGYoKc=; b=HuTPDTQ9bD4Ppe7MxlB8u8x9ZNnIbTZIGFpzVFUWbHZCIO4AM5rEpXhN1t0NEqizh5 oDFn63Im2cG8AV+R+lv2kOS+V02aYunpfdmpxLqseAMvD7WC75SV9yxWLgnWPv71b/O4 uwi8L404tyFnTW0LJ1zf+JYBQbu80J2NpFJa56Bs+DfhQMIfZWCW9cXMajK3Epfk2MD2 fehzAj6RsXkSvP+tp4o8ZFHr6/W+QWyfSUQDjelEhJyzB3Wzqrr+rJzmgwhATUHxPEh0 5B6fEIJqKBO7Xd8NxOFsOmnAwri23q5ffENrXfIcywZjPp7lv54E9w59kIa6IqnukAYa jauQ== 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=Q6BXeLowgHVOEAzvS3zejF0hX4h0QSs0WpSTmQGYoKc=; b=FNcJDcF0t9XVWvfSUxf7v0YraC2f0bUNma/JWbOkNMga4zV4VopCZhsmPh5xha+urH AM8vWUUl04iccg1wxKGxywTsZVC8y2WCj8sDPA9FW5ODNEbr74+BSlORv3QJjsfcFt/p FGr1KtLynK6hmq6MQ+OntNhMx4ahI+xD3DpQ1zkjyhopg6wv5CwbgvkF5/MEpXv7SvjV CfP8R5b76TzNo2X+a540lH3z6GnWSTKLDqztk9iIZZOqw550xXjoHfUiO7ympKgZ0INf w4wH9hYwIV+0Y5Xts2DWSZ1jkqIJBSysMc3InGSfDDQF4EQUyklAD9DGneKxMJ+IF7Wa KLqQ== X-Gm-Message-State: AOAM533PIMD0Z7i1+QtRVf8O29baKZbpXGXy+lMXpE4fchSOXy2WAOPZ CN6v0lwjOK9SdwUQoI3DVwDJuZgM X-Google-Smtp-Source: ABdhPJwD1xEGQb2PUTfr+Fs++4QcgaDxoz+MPHTEVcWdRnfeompgiPgCSajCUbdrgu8xCjb4RX9IhA== X-Received: by 2002:adf:ecce:: with SMTP id s14mr2004890wro.154.1590854799206; Sat, 30 May 2020 09:06:39 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1ab57.dynamic.kabel-deutschland.de. [188.193.171.87]) by smtp.gmail.com with ESMTPSA id v27sm15186517wrv.81.2020.05.30.09.06.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 30 May 2020 09:06:38 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 30 May 2020 18:05:28 +0200 Message-Id: <20200530160541.29517-23-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200530160541.29517-1-andreas.rheinhardt@gmail.com> References: <20200530160541.29517-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 23/36] avcodec/utils: Add utility functions for bsf 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" Several bitstream filters (e.g. dump_extradata, imxdump, mjpeg2jpeg, mjpegadump, mp3decomp, ...) don't buffer packets; instead, they just modify the buffer of one packet and don't change any other of the packet's non-buffer properties. The usual approach of these bitstream filters is to use separate packets for in- and output as follows: 1. Get the input packet via ff_bsf_get_packet() which entails an allocation. 2. Use av_new_packet() to allocate a big enough buffer in the output packet. 3. Perform the actual work of the bitstream filter, i.e. fill the output buffer. 4. Use av_packet_copy_props() to copy the non-buffer fields of the input packet to the output packet. 5. Free the input packet and return. This commit adds two utility functions that allow a different approach: A function to (re)allocate a refcounted buffer with zeroed padding and a function to replace a packet's buffer and the buffer-related fields with information from an AVBufferRef. This allows to modify the bitstream filters as follows: 1. Get the packet via ff_bsf_get_packet_ref(). 2. Use ff_buffer_padded_realloc() to get a big enough refcounted buffer. 3. Perform the actual work of the bitstream filter. 4. Use ff_packet_replace_buffer() to replace the old data in the packet with the modified one and return. The first of these functions is just packet_alloc() from avpacket.c which has been made non-static. Signed-off-by: Andreas Rheinhardt --- An earlier version put the declarations to internal.h, but James suggested putting them into bsf_internal.h (or actually, into bsf.h because bsf_internal.h didn't exist back then), so I went with this. There is currently no user that actually makes use of the reallocation feature; it was initially thought for hevc_mp4toannexb, but then I decided to make it no longer reallocate the output buffer at all any more. But I kept this functionality. Might be useful some day. libavcodec/avpacket.c | 21 ++++++++++++++++----- libavcodec/bsf_internal.h | 27 +++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/libavcodec/avpacket.c b/libavcodec/avpacket.c index 033f2d8f26..c8f3b0cf7a 100644 --- a/libavcodec/avpacket.c +++ b/libavcodec/avpacket.c @@ -27,6 +27,7 @@ #include "libavutil/mathematics.h" #include "libavutil/mem.h" +#include "bsf_internal.h" #include "bytestream.h" #include "internal.h" #include "packet.h" @@ -69,7 +70,7 @@ void av_packet_free(AVPacket **pkt) av_freep(pkt); } -static int packet_alloc(AVBufferRef **buf, int size) +int ff_buffer_padded_realloc(AVBufferRef **buf, int size) { int ret; if (size < 0 || size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) @@ -87,7 +88,7 @@ static int packet_alloc(AVBufferRef **buf, int size) int av_new_packet(AVPacket *pkt, int size) { AVBufferRef *buf = NULL; - int ret = packet_alloc(&buf, size); + int ret = ff_buffer_padded_realloc(&buf, size); if (ret < 0) return ret; @@ -621,7 +622,7 @@ int av_packet_ref(AVPacket *dst, const AVPacket *src) goto fail; if (!src->buf) { - ret = packet_alloc(&dst->buf, src->size); + ret = ff_buffer_padded_realloc(&dst->buf, src->size); if (ret < 0) goto fail; av_assert1(!src->size || src->data); @@ -674,7 +675,7 @@ int av_packet_make_refcounted(AVPacket *pkt) if (pkt->buf) return 0; - ret = packet_alloc(&pkt->buf, pkt->size); + ret = ff_buffer_padded_realloc(&pkt->buf, pkt->size); if (ret < 0) return ret; av_assert1(!pkt->size || pkt->data); @@ -694,7 +695,7 @@ int av_packet_make_writable(AVPacket *pkt) if (pkt->buf && av_buffer_is_writable(pkt->buf)) return 0; - ret = packet_alloc(&buf, pkt->size); + ret = ff_buffer_padded_realloc(&buf, pkt->size); if (ret < 0) return ret; av_assert1(!pkt->size || pkt->data); @@ -770,3 +771,13 @@ int ff_side_data_set_prft(AVPacket *pkt, int64_t timestamp) return 0; } + +void ff_packet_replace_buffer(AVPacket *pkt, AVBufferRef *buf) +{ + av_buffer_unref(&pkt->buf); + + pkt->buf = buf; + pkt->data = buf->data; + av_assert1(buf->size >= AV_INPUT_BUFFER_PADDING_SIZE); + pkt->size = buf->size - AV_INPUT_BUFFER_PADDING_SIZE; +} diff --git a/libavcodec/bsf_internal.h b/libavcodec/bsf_internal.h index fefd5b8905..edaacaa2dd 100644 --- a/libavcodec/bsf_internal.h +++ b/libavcodec/bsf_internal.h @@ -19,6 +19,7 @@ #ifndef AVCODEC_BSF_INTERNAL_H #define AVCODEC_BSF_INTERNAL_H +#include "libavutil/buffer.h" #include "libavutil/log.h" #include "bsf.h" @@ -42,6 +43,32 @@ int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt); */ int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt); +/** + * (Re)allocate an AVBufferRef to an effective size of size. In addition, + * the buffer will have AV_BUFFER_INPUT_PADDING_SIZE bytes of zeroed padding + * at the end. + * + * @param buf Pointer to pointer to an AVBufferRef. Must not be NULL; + * *buf will be reallocated to the desired size; if *buf is NULL, + * it will be allocated, otherwise it must be writable. + * @param size Desired usable size. + * @return Zero on success, negative error code on failure. + * *buf is unchanged on error. + */ +int ff_buffer_padded_realloc(AVBufferRef **buf, int size); + +/** + * Unreference the packet's buf and replace it with the given buf. + * The packet's data and size fields will be updated with the information + * from buf, too. + * + * @param pkt Pointer to a packet to modify. Must not be NULL. + * @param buf Pointer to an AVBufferRef. Must not be NULL. + * buf will be owned by pkt afterwards. Its size must include + * AV_INPUT_BUFFER_PADDING_SIZE bytes of padding at the end. + */ +void ff_packet_replace_buffer(AVPacket *pkt, AVBufferRef *buf); + const AVClass *ff_bsf_child_class_next(const AVClass *prev); #endif /* AVCODEC_BSF_INTERNAL_H */