From patchwork Thu Jan 5 11:11:33 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthieu Bouron X-Patchwork-Id: 2063 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.89.21 with SMTP id n21csp5281876vsb; Thu, 5 Jan 2017 03:18:54 -0800 (PST) X-Received: by 10.28.58.14 with SMTP id h14mr66135231wma.7.1483615134608; Thu, 05 Jan 2017 03:18:54 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id m80si54979457wma.56.2017.01.05.03.18.53; Thu, 05 Jan 2017 03:18:54 -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; 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 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 6D8CD689C13; Thu, 5 Jan 2017 13:18:38 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wj0-f193.google.com (mail-wj0-f193.google.com [209.85.210.193]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C5AC26891F7 for ; Thu, 5 Jan 2017 13:18:31 +0200 (EET) Received: by mail-wj0-f193.google.com with SMTP id kp2so78753725wjc.0 for ; Thu, 05 Jan 2017 03:18:37 -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:in-reply-to:references; bh=7X6i+4xhrfF61L5RV0xheaJjAbJ/fcR6d02WUp78uhg=; b=fV+eyUUYvJevTu6qk5hnz5diAc/XiWGICjrkSmXfkDL4IUk5VkybRIimKqFqqAk3in Au0sGyHBzjMByf2xQ0ss7GT6NHTqZ7DZhs3qaR8SJv28ahClwvaNJnSfCbTtkJhXs1NR EKPy0YocA/ugwbBx3mUOPcy2pgOObDVBElMZ5cGXbemr1Q2JcJNuOGpNL1PNQ5oXqr7J pxPUGwfJ2VU6XLijc999oSlgCb1yNZSwgYBzWq0qPDAOcN/ElZt9AfV1ohnDDYDqLyuP FS/euCNnKGIBuGACPLe93c9pKyvICYHmC/3muHHOS0W3beYtNtfTp3X/RUtFsG6DConB Kylw== 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; bh=7X6i+4xhrfF61L5RV0xheaJjAbJ/fcR6d02WUp78uhg=; b=dAgLg/+wh5hoRmUvg7ElERAZjcGQi02mUWSYM4Z4cATI+JXC0EjHzUp37InfUMrHxo bU/93Kb7ls69vxdJoaaXn5J6VqnTYPBexjbjPIzTvAmN5fSaa1knNdPTg/D/Z7k8akXm Gq2NjIvt3fNlhy8lvj6x1iYmzYlQi+qfZe9SZXqBobpg75bY/e54WZ76C2od39FWLDoW 6YIILoswnZR3ehVBwHFwHW0fDt36se/rdDfTnJZftHqx5zhJnlOHJSGAPrkgeASeGJXx L3UQFYmRUYTYF0ZysYsclgzrEkOjk4I3bU1+oI0fNjYXL6TdcepZ80Bjx2y5UkRs49U/ jdIg== X-Gm-Message-State: AIkVDXLmH7s+g/oBwfIVwXD8/t2UcpmWnehvSRSXieCYtRibv4aNs7Ap9T2X2gHFpGyyNw== X-Received: by 10.194.99.38 with SMTP id en6mr61619706wjb.184.1483614680686; Thu, 05 Jan 2017 03:11:20 -0800 (PST) Received: from boken.gopro.lcl (LPuteaux-656-1-74-135.w80-12.abo.wanadoo.fr. [80.12.80.135]) by smtp.gmail.com with ESMTPSA id j6sm103326617wjk.25.2017.01.05.03.11.19 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 05 Jan 2017 03:11:20 -0800 (PST) From: Matthieu Bouron To: ffmpeg-devel@ffmpeg.org Date: Thu, 5 Jan 2017 12:11:33 +0100 Message-Id: <20170105111136.31854-3-matthieu.bouron@gmail.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170105111136.31854-1-matthieu.bouron@gmail.com> References: <1462895161-21148-1-git-send-email-matthieu.bouron@gmail.com> <20170105111136.31854-1-matthieu.bouron@gmail.com> Subject: [FFmpeg-devel] [PATCH 2/5] lavfi/framepool: add audio support 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: Matthieu Bouron MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" --- libavfilter/framepool.c | 109 ++++++++++++++++++++++++++++++++++++++++++++++++ libavfilter/framepool.h | 36 +++++++++++++++- 2 files changed, 144 insertions(+), 1 deletion(-) diff --git a/libavfilter/framepool.c b/libavfilter/framepool.c index 36c6e8fd04..588dcb6da2 100644 --- a/libavfilter/framepool.c +++ b/libavfilter/framepool.c @@ -20,6 +20,7 @@ #include "framepool.h" #include "libavutil/avassert.h" +#include "libavutil/avutil.h" #include "libavutil/buffer.h" #include "libavutil/frame.h" #include "libavutil/imgutils.h" @@ -28,8 +29,18 @@ struct FFFramePool { + enum AVMediaType type; + + /* video */ int width; int height; + + /* audio */ + int planes; + int channels; + int nb_samples; + + /* common */ int format; int align; int linesize[4]; @@ -54,6 +65,7 @@ FFFramePool *ff_frame_pool_video_init(AVBufferRef* (*alloc)(int size), if (!pool) return NULL; + pool->type = AVMEDIA_TYPE_VIDEO; pool->width = width; pool->height = height; pool->format = format; @@ -104,6 +116,44 @@ fail: return NULL; } +FFFramePool *ff_frame_pool_audio_init(AVBufferRef* (*alloc)(int size), + int channels, + int nb_samples, + enum AVSampleFormat format, + int align) +{ + int ret, planar; + FFFramePool *pool; + + pool = av_mallocz(sizeof(FFFramePool)); + if (!pool) + return NULL; + + planar = av_sample_fmt_is_planar(format); + + pool->type = AVMEDIA_TYPE_AUDIO; + pool->planes = planar ? channels : 1; + pool->channels = channels; + pool->nb_samples = nb_samples; + pool->format = format; + pool->align = align; + + ret = av_samples_get_buffer_size(&pool->linesize[0], channels, + nb_samples, format, 0); + if (ret < 0) + goto fail; + + pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL); + if (!pool->pools[0]) + goto fail; + + return pool; + +fail: + ff_frame_pool_uninit(&pool); + return NULL; +} + int ff_frame_pool_get_video_config(FFFramePool *pool, int *width, int *height, @@ -113,6 +163,8 @@ int ff_frame_pool_get_video_config(FFFramePool *pool, if (!pool) return AVERROR(EINVAL); + av_assert0(pool->type == AVMEDIA_TYPE_VIDEO); + *width = pool->width; *height = pool->height; *format = pool->format; @@ -121,6 +173,24 @@ int ff_frame_pool_get_video_config(FFFramePool *pool, return 0; } +int ff_frame_pool_get_audio_config(FFFramePool *pool, + int *channels, + int *nb_samples, + enum AVSampleFormat *format, + int *align) +{ + if (!pool) + return AVERROR(EINVAL); + + av_assert0(pool->type == AVMEDIA_TYPE_AUDIO); + + *channels = pool->channels; + *nb_samples = pool->nb_samples; + *format = pool->format; + *align = pool->align; + + return 0; +} AVFrame *ff_frame_pool_get(FFFramePool *pool) { @@ -133,6 +203,8 @@ AVFrame *ff_frame_pool_get(FFFramePool *pool) return NULL; } + switch(pool->type) { + case AVMEDIA_TYPE_VIDEO: desc = av_pix_fmt_desc_get(pool->format); if (!desc) { goto fail; @@ -167,6 +239,43 @@ AVFrame *ff_frame_pool_get(FFFramePool *pool) } frame->extended_data = frame->data; + break; + case AVMEDIA_TYPE_AUDIO: + frame->nb_samples = pool->nb_samples; + av_frame_set_channels(frame, pool->channels); + frame->format = pool->format; + frame->linesize[0] = pool->linesize[0]; + + if (pool->planes > AV_NUM_DATA_POINTERS) { + frame->extended_data = av_mallocz_array(pool->planes, + sizeof(*frame->extended_data)); + frame->nb_extended_buf = pool->planes - AV_NUM_DATA_POINTERS; + frame->extended_buf = av_mallocz_array(frame->nb_extended_buf, + sizeof(*frame->extended_buf)); + if (!frame->extended_data || !frame->extended_buf) + goto fail; + } else { + frame->extended_data = frame->data; + av_assert0(frame->nb_extended_buf == 0); + } + + for (i = 0; i < FFMIN(pool->planes, AV_NUM_DATA_POINTERS); i++) { + frame->buf[i] = av_buffer_pool_get(pool->pools[0]); + if (!frame->buf[i]) + goto fail; + frame->extended_data[i] = frame->data[i] = frame->buf[i]->data; + } + for (i = 0; i < frame->nb_extended_buf; i++) { + frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]); + if (!frame->extended_buf[i]) + goto fail; + frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data; + } + + break; + default: + av_assert0(0); + } return frame; fail: diff --git a/libavfilter/framepool.h b/libavfilter/framepool.h index 4824824894..e5560e4c6e 100644 --- a/libavfilter/framepool.h +++ b/libavfilter/framepool.h @@ -41,7 +41,7 @@ typedef struct FFFramePool FFFramePool; * @param height height of each frame in this pool * @param format format of each frame in this pool * @param align buffers alignement of each frame in this pool - * @return newly created frame pool on success, NULL on error. + * @return newly created video frame pool on success, NULL on error. */ FFFramePool *ff_frame_pool_video_init(AVBufferRef* (*alloc)(int size), int width, @@ -50,6 +50,24 @@ FFFramePool *ff_frame_pool_video_init(AVBufferRef* (*alloc)(int size), int align); /** + * Allocate and initialize an audio frame pool. + * + * @param alloc a function that will be used to allocate new frame buffers when + * the pool is empty. May be NULL, then the default allocator will be used + * (av_buffer_alloc()). + * @param channels channels of each frame in this pool + * @param nb_samples number of samples of each frame in this pool + * @param format format of each frame in this pool + * @param align buffers alignement of each frame in this pool + * @return newly created audio frame pool on success, NULL on error. + */ +FFFramePool *ff_frame_pool_audio_init(AVBufferRef* (*alloc)(int size), + int channels, + int samples, + enum AVSampleFormat format, + int align); + +/** * Deallocate the frame pool. It is safe to call this function while * some of the allocated frame are still in use. * @@ -73,6 +91,22 @@ int ff_frame_pool_get_video_config(FFFramePool *pool, int *align); /** + * Get the audio frame pool configuration. + * + * @param channels channels of each frame in this pool + * @param nb_samples number of samples of each frame in this pool + * @param format format of each frame in this pool + * @param align buffers alignement of each frame in this pool + * @return 0 on success, a negative AVERROR otherwise. + */ +int ff_frame_pool_get_audio_config(FFFramePool *pool, + int *channels, + int *nb_samples, + enum AVSampleFormat *format, + int *align); + + +/** * Allocate a new AVFrame, reussing old buffers from the pool when available. * This function may be called simultaneously from multiple threads. *