From patchwork Thu Mar 1 18:58:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rostislav Pehlivanov X-Patchwork-Id: 7780 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.181.170 with SMTP id m39csp5381616jaj; Thu, 1 Mar 2018 10:58:44 -0800 (PST) X-Google-Smtp-Source: AG47ELsSqtaSYUQ74BpeGV6/8gnaVvczTxOkHzTeM88zYPbxDzvTwQbWsMvEq7QTYo1ySZ+WN8Xx X-Received: by 10.28.207.201 with SMTP id f192mr2527898wmg.81.1519930723946; Thu, 01 Mar 2018 10:58:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519930723; cv=none; d=google.com; s=arc-20160816; b=g9iMTbr1iFcOmWOzLSpUtuWi/eM0QU+ALyIBQY05mnSDMfu6sHcIFIYwYm7AGsYaJ2 At1yvqKiauz31DPyvuquB6jsaElQzM95gSmMCoYtO9u6qbuvw8dLCxU3icKfImdBhtEH mo/fPIWDTUyOp3F3q9a5sdqIDjEM46VeNZua9COK2rM4ubzptGk5Xawpa2DEv/vXmdGx ptjUG1nAmvZWG2pa8MthbNeq5PO7WUP/DCyfmdRD/bz/6NAXeG8M9/klWBTtDH3jfRRC MNTJKuMNC9+swqmAdtKuJ2YDbuoFlDmJBt3aRvgwm38RiURqo0e4p+GhBgGyEa4I1W6X tvSQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:dkim-signature :delivered-to:arc-authentication-results; bh=ehoYmAcPQm+q6O+//ukBawM/6dNEnwd1qeLE4J7uqPQ=; b=rvB/Mf3ohr3JfM4v0EHJKtlng2rsBx1uETCt8Mg6P4wH/ZRcAA1b/4sHEJmCFGETBv 1HSzdjIp8tGb41iDM1HWfDU1EsUB5LMle+7lrXQP6iE0bnFPMFAtVEqAJYnuJK7OhJbx oMehUpV55WTa0SPYJKFUjwCEsSGATBpYLGFoYehRMZDYZC+YSfFccRDp56R97YltW1/T 2TjRIJnN+g6B85glehaTGeaL0P/eeUwQRUEuYCayghAneGMCQJvzg0aX1qxpRy529bHI G3m7F92pSGHe0OpcRq2Yx4iUh4nVHFQdwBpLo1+zBPZr0PpFa1MyqZ8DdDWb86/ySScC 0gJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=mzl+Xw8D; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id y39si3199236wrd.117.2018.03.01.10.58.43; Thu, 01 Mar 2018 10:58:43 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=mzl+Xw8D; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1DB7C68A3FE; Thu, 1 Mar 2018 20:58:36 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm0-f65.google.com (mail-wm0-f65.google.com [74.125.82.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 290FC68A202 for ; Thu, 1 Mar 2018 20:58:30 +0200 (EET) Received: by mail-wm0-f65.google.com with SMTP id q83so14149560wme.5 for ; Thu, 01 Mar 2018 10:58:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=frixxvK2XYLhvjdyhviBwByhbSVqdbmJD304xRKj5yw=; b=mzl+Xw8D7JyUy6CtIqX2u5F5iGHPFij/FmyJ/wjeUd3ysVir0wImV10PrEFC83yKBQ uObxhFqP/OAGQsFy9EFN0CGw9afptw7Co6xf3a+pM23OkEgkZVqX8nWp7YzjaT+Qb3dz rXBWDuJpQXJXr0OlUdwxIugnCDYmbujUasgLlDsF5TLCvBZPV0MX15WswBop+Tws8dlG p2sz47SljJecMkG9uNovKWSL4T3Iiya39xs4nfUj+X0A5XXEqIurv2lG2Shi1c0+Zd5e KmdIlDONbNaNGZ/JvzoOKyPEACRppRtntM/ici2K9bh23jHboQ9uSwz3CMnhHldodu+S vxWw== 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; bh=frixxvK2XYLhvjdyhviBwByhbSVqdbmJD304xRKj5yw=; b=OKqei8FnjJ1QY6T5KEEiQF4x4q8PqJjBvdFegngHlut6MFOqlPxXs17xIDheGQczxT nGlTmi7j/lNZfzuRvsjWE9XQdDrAjWbbtovXZGWFQ68qNEjye3hLQIKoDmJ0xSMkDH+b Ze86Sc4Fy15Lzu4FP/lsFB9N6TVysO5UBqZmB5/SiZOLJOkVKHnDrA2RywId3Tvp0uel 2b/8boQwEkhDXNbu+rTn/yGNeMdhEwVFI6dtpK9Jz9MwnblJpt8IoI5txYYmf8YCTxIr pUgcxJmf9by5Klol5fXwx6npPeHDa0bFQpZQfCagvKzDfQQGxeds5xXxNnrYHjaiKBJx LNig== X-Gm-Message-State: AElRT7G2vaRB8AT/lealfhWytc+fbW9BJpXlPjzn/43MYR5BDnsb+zSl LpEuCRrVFxFB22Slq9p2sbP7yjCi X-Received: by 10.28.234.197 with SMTP id g66mr2584852wmi.10.1519930714974; Thu, 01 Mar 2018 10:58:34 -0800 (PST) Received: from moonbase.pars.ee ([2a00:23c4:7c88:af00:28e9:f50d:c7d9:1df0]) by smtp.gmail.com with ESMTPSA id x190sm6668515wme.27.2018.03.01.10.58.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 01 Mar 2018 10:58:33 -0800 (PST) From: Rostislav Pehlivanov To: ffmpeg-devel@ffmpeg.org Date: Thu, 1 Mar 2018 18:58:28 +0000 Message-Id: <20180301185828.4181-1-atomnuker@gmail.com> X-Mailer: git-send-email 2.16.2 Subject: [FFmpeg-devel] [PATCH] frame: add an av_frame_new_side_data_from_buf function 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: Rostislav Pehlivanov MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Also fixes a theoretical memory leak in av_frame_new_side_data_from_buf where if ret was successfully allocated but the realloc call failed, ret wouldn't be freed. Signed-off-by: Rostislav Pehlivanov --- libavutil/frame.c | 42 +++++++++++++++++++++--------------------- libavutil/frame.h | 16 ++++++++++++++++ 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/libavutil/frame.c b/libavutil/frame.c index 662a7e5ab5..4951e48045 100644 --- a/libavutil/frame.c +++ b/libavutil/frame.c @@ -26,11 +26,6 @@ #include "mem.h" #include "samplefmt.h" - -static AVFrameSideData *frame_new_side_data(AVFrame *frame, - enum AVFrameSideDataType type, - AVBufferRef *buf); - #if FF_API_FRAME_GET_SET MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp) MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration) @@ -356,8 +351,10 @@ FF_ENABLE_DEPRECATION_WARNINGS } memcpy(sd_dst->data, sd_src->data, sd_src->size); } else { - sd_dst = frame_new_side_data(dst, sd_src->type, av_buffer_ref(sd_src->buf)); + AVBufferRef *ref = av_buffer_ref(sd_src->buf); + sd_dst = av_frame_new_side_data_from_buf(dst, sd_src->type, ref); if (!sd_dst) { + av_buffer_unref(&ref); wipe_side_data(dst); return AVERROR(ENOMEM); } @@ -642,9 +639,9 @@ AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane) return NULL; } -static AVFrameSideData *frame_new_side_data(AVFrame *frame, - enum AVFrameSideDataType type, - AVBufferRef *buf) +AVFrameSideData *av_frame_new_side_data_from_buf(AVFrame *frame, + enum AVFrameSideDataType type, + AVBufferRef *buf) { AVFrameSideData *ret, **tmp; @@ -652,18 +649,20 @@ static AVFrameSideData *frame_new_side_data(AVFrame *frame, return NULL; if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1) - goto fail; + return NULL; + + ret = av_mallocz(sizeof(*ret)); + if (!ret) + return NULL; tmp = av_realloc(frame->side_data, (frame->nb_side_data + 1) * sizeof(*frame->side_data)); - if (!tmp) - goto fail; + if (!tmp) { + av_free(ret); + return NULL; + } frame->side_data = tmp; - ret = av_mallocz(sizeof(*ret)); - if (!ret) - goto fail; - ret->buf = buf; ret->data = ret->buf->data; ret->size = buf->size; @@ -672,17 +671,18 @@ static AVFrameSideData *frame_new_side_data(AVFrame *frame, frame->side_data[frame->nb_side_data++] = ret; return ret; -fail: - av_buffer_unref(&buf); - return NULL; } AVFrameSideData *av_frame_new_side_data(AVFrame *frame, enum AVFrameSideDataType type, int size) { - - return frame_new_side_data(frame, type, av_buffer_alloc(size)); + AVFrameSideData *ret; + AVBufferRef *buf = av_buffer_alloc(size); + ret = av_frame_new_side_data_from_buf(frame, type, buf); + if (!ret) + av_buffer_unref(&buf); + return ret; } AVFrameSideData *av_frame_get_side_data(const AVFrame *frame, diff --git a/libavutil/frame.h b/libavutil/frame.h index d54bd9a354..59cee8ceab 100644 --- a/libavutil/frame.h +++ b/libavutil/frame.h @@ -800,6 +800,22 @@ AVFrameSideData *av_frame_new_side_data(AVFrame *frame, enum AVFrameSideDataType type, int size); +/** + * Add a new side data to a frame from an existing AVBufferRef + * + * @param frame a frame to which the side data should be added + * @param type the type of the added side data + * @param buf an AVBufferRef to add as side data. The ownership of + * the reference is transferred to the frame. + * + * @return newly added side data on success, NULL on error. On failure + * the frame is unchanged and the AVBufferRef remains owned by + * the caller. + */ +AVFrameSideData *av_frame_new_side_data_from_buf(AVFrame *frame, + enum AVFrameSideDataType type, + AVBufferRef *buf); + /** * @return a pointer to the side data of a given type on success, NULL if there * is no side data with such type in this frame.