From patchwork Tue Dec 27 02:21:18 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Turner X-Patchwork-Id: 1938 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.89.21 with SMTP id n21csp1290254vsb; Mon, 26 Dec 2016 18:27:36 -0800 (PST) X-Received: by 10.28.188.87 with SMTP id m84mr25166520wmf.14.1482805656148; Mon, 26 Dec 2016 18:27:36 -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 l123si44379713wmb.151.2016.12.26.18.27.35; Mon, 26 Dec 2016 18:27:36 -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=@googlemail.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=QUARANTINE dis=NONE) header.from=googlemail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 2163F689C8E; Tue, 27 Dec 2016 04:27:31 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pg0-f68.google.com (mail-pg0-f68.google.com [74.125.83.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 81260689BAA for ; Tue, 27 Dec 2016 04:27:23 +0200 (EET) Received: by mail-pg0-f68.google.com with SMTP id b1so10855556pgc.1 for ; Mon, 26 Dec 2016 18:27:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20161025; h=from:to:subject:date:message-id; bh=JSqlFxkABbMe/Qi0HmAKjnA75mGu5aCZ2ylNZRdeaCU=; b=A25bAg3VyQCizX73FtGI+k9WCqlk+PJ2HshYrUlE8poqd+negXqcX2P6JUGu5+mQWx ggQFNzJjTKRc8NokkJt+GRAD1wfnOx76+AGA5pM3+gT08VDvqZUMtbduuqSfm4HFXI6p 4qggUK4UgYTwoq7BPo08DUwo3cv8yUghVA0O2EsOxf8P5N8cWP+V6aHxjgeESUVn8Zch p9udk7xyWWV3tkh8pVQtWH8TXJfevixlu7V6pGoPkmFhS5mhmHxNpUHX+NfGvgOp2iXA /zQV8uVARZa0+xbb3dFmArIbUvcO7pyVcTc3kAYHNxTpVBUSBsbMtqnBC4CZ+aq31Rr4 d5Bw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=JSqlFxkABbMe/Qi0HmAKjnA75mGu5aCZ2ylNZRdeaCU=; b=SnbyLcOB4Xfcj3GhTQs1cMUrvAflLTlnTK9YSf/TosURff1wzXyLslBwMsfFipuBaJ Wpk6Tq9kW+PYdNkBWlYVUnBsQY+Gs5QeWKcx4b2ktW4oQSiPlPnHWukZQEnN42pfFi6v 7UKdgkzgBjagvnZ9W4xXg4AkVx+cbamE8AZ/7Gh+TpWpiGTfLaLolQ9gDF2Xa7y4GdEW lzd+mqrAaK0C7nHzXXUdOhyzBBuUMvCainWYx5SZfC2DYsR4cddnW4vE64Mcbq4xlmfB Y4VD90AHirQJQ1SrS/1aqkbMlRn2A+GHZ9b0m9TdpVEbl8LXaT6BpW9b61/PoiPSLb1S ByJA== X-Gm-Message-State: AIkVDXLVSicq0AvPSVHi6H4IOwZwVptlUaQ5YbQ3VUZOnBUbjEE4HPqh/lUeBVc2CTqZZw== X-Received: by 10.99.160.1 with SMTP id r1mr54260543pge.107.1482805280966; Mon, 26 Dec 2016 18:21:20 -0800 (PST) Received: from Zany.fios-router.home ([47.147.141.23]) by smtp.gmail.com with ESMTPSA id t20sm84869424pfk.48.2016.12.26.18.21.20 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 26 Dec 2016 18:21:20 -0800 (PST) From: Thomas Turner To: ffmpeg-devel@ffmpeg.org Date: Mon, 26 Dec 2016 18:21:18 -0800 Message-Id: <1482805278-5678-1-git-send-email-thomastdt@googlemail.com> X-Mailer: git-send-email 1.9.1 Subject: [FFmpeg-devel] [PATCH] avutil/tests: Replacement selftest for audio_fifo.c 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Thomas Turner --- libavutil/tests/audio_fifo.c | 181 ++++++++++++++++++++++++++++--------------- tests/ref/fate/audio_fifo | 34 +++----- 2 files changed, 130 insertions(+), 85 deletions(-) diff --git a/libavutil/tests/audio_fifo.c b/libavutil/tests/audio_fifo.c index 34c8573..a5c3344 100644 --- a/libavutil/tests/audio_fifo.c +++ b/libavutil/tests/audio_fifo.c @@ -19,6 +19,7 @@ #include #include #include +#include "libavutil/mem.h" #include "libavutil/audio_fifo.c" #define MAX_CHANNELS 32 @@ -31,12 +32,25 @@ typedef struct TestStruct { int nb_samples_pch; } TestStruct; +typedef struct Audio_Data { + uint8_t *data[MAX_CHANNELS]; /**< data plane pointers */ + int nb_samples_pch; /**< number of samples per channel */ + int allocated_samples; /**< number of samples per channel the buffer can hold */ + enum AVSampleFormat sample_fmt; /**< sample format */ + int channels; /**< channel count */ + int is_planar; /**< sample format is planar */ + int planes; /**< number of data planes */ + int sample_size; /**< bytes per sample */ + int stride; /**< sample byte offset within a plane */ + int line_size; /**< size of data buffer per plane */ +} Audio_Data; + static const uint8_t data_U8 [] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; static const int16_t data_S16[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; static const float data_FLT[] = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0}; static const TestStruct test_struct[] = { - {.format = AV_SAMPLE_FMT_U8 , .nb_ch = 1, .data_planes = {data_U8 , }, .nb_samples_pch = 12}, + {.format = AV_SAMPLE_FMT_U8 , .nb_ch = 2, .data_planes = {data_U8 , }, .nb_samples_pch = 6 }, {.format = AV_SAMPLE_FMT_U8P , .nb_ch = 2, .data_planes = {data_U8 , data_U8 +6, }, .nb_samples_pch = 6 }, {.format = AV_SAMPLE_FMT_S16 , .nb_ch = 1, .data_planes = {data_S16, }, .nb_samples_pch = 12}, {.format = AV_SAMPLE_FMT_S16P , .nb_ch = 2, .data_planes = {data_S16, data_S16+6, }, .nb_samples_pch = 6 }, @@ -44,34 +58,81 @@ static const TestStruct test_struct[] = { {.format = AV_SAMPLE_FMT_FLTP , .nb_ch = 2, .data_planes = {data_FLT, data_FLT+6, }, .nb_samples_pch = 6 } }; -static void ERROR(const char *str) +static void free_audio(Audio_Data *a) { - fprintf(stderr, "%s\n", str); - exit(1); + int i; + for (i = 0; i < a->planes; ++i){ + av_freep(&a->data[i]); + } + av_freep(&a); } -static void* allocate_memory(size_t size) +static Audio_Data* set_audio_data(const TestStruct test_sample) { - void *ptr = malloc(size); - if (ptr == NULL){ - ERROR("failed to allocate memory!"); + Audio_Data *a; + int buf_size, i; + + a = av_mallocz(sizeof(*a)); + if (!a) + return NULL; + + a->nb_samples_pch = test_sample.nb_samples_pch; + a->sample_fmt = test_sample.format; + a->channels = test_sample.nb_ch; + a->is_planar = av_sample_fmt_is_planar(a->sample_fmt); + a->planes = a->is_planar ? a->channels : 1; + a->sample_size = av_get_bytes_per_sample(a->sample_fmt); + + if (av_samples_get_buffer_size(&buf_size, a->channels, a->nb_samples_pch, a->sample_fmt, 1) < 0) + goto error; + + a->stride = buf_size / a->nb_samples_pch; + a->line_size = buf_size; + + for (i = 0; i < a->planes; ++i){ + a->data[i] = av_malloc_array(1, buf_size); + if (!a->data[i]) + goto error; + memcpy(a->data[i], test_sample.data_planes[i] ,buf_size); } - return ptr; + + a->allocated_samples = a->nb_samples_pch; + + return a; + +error: + free_audio(a); + return NULL; } -static void print_audio_bytes(const TestStruct *test_sample, void **data_planes, int nb_samples) +static void ERROR(const char *str, AVAudioFifo *afifo, Audio_Data *adata) { - int p, b, f; - int byte_offset = av_get_bytes_per_sample(test_sample->format); - int buffers = av_sample_fmt_is_planar(test_sample->format) - ? test_sample->nb_ch : 1; - int line_size = (buffers > 1) ? nb_samples * byte_offset - : nb_samples * byte_offset * test_sample->nb_ch; - for (p = 0; p < buffers; ++p){ - for(b = 0; b < line_size; b+=byte_offset){ - for (f = 0; f < byte_offset; f++){ - int order = !HAVE_BIGENDIAN ? (byte_offset - f - 1) : f; - printf("%02x", *((uint8_t*)data_planes[p] + b + order)); + fprintf(stderr, "%s\n", str); + + if (afifo) + av_audio_fifo_free(afifo); + if (adata) + free_audio(adata); + + exit(1); +} + +static void print_audio_bytes(Audio_Data *a, uint8_t **data_planes, int nb_samples, int output) +{ + int p, b, f, linesize; + + if (!output) + data_planes = a->data; + + if (!data_planes || !a || nb_samples <= 0 ) + return; + + linesize = a->stride * nb_samples; + for (p = 0; p < a->planes; ++p){ + for(b = 0; b < linesize; b+=a->sample_size){ + for (f = 0; f < a->sample_size; f++){ + int order = !HAVE_BIGENDIAN ? (a->sample_size - f - 1) : f; + printf("%02x", *(data_planes[p] + b + order)); } putchar(' '); } @@ -79,38 +140,31 @@ static void print_audio_bytes(const TestStruct *test_sample, void **data_planes, } } -static int read_samples_from_audio_fifo(AVAudioFifo* afifo, void ***output, int nb_samples) +static int read_samples_from_audio_fifo(AVAudioFifo* afifo, Audio_Data *a, int nb_samples) { - int i; - int samples = FFMIN(nb_samples, afifo->nb_samples); - int tot_elements = !av_sample_fmt_is_planar(afifo->sample_fmt) - ? samples : afifo->channels * samples; - void **data_planes = allocate_memory(sizeof(void*) * afifo->nb_buffers); - *output = data_planes; - - for (i = 0; i < afifo->nb_buffers; ++i){ - data_planes[i] = allocate_memory(afifo->sample_size * tot_elements); - } + if (nb_samples <= 0) + return 0; + nb_samples = FFMIN(a->allocated_samples, nb_samples); - return av_audio_fifo_read(afifo, *output, nb_samples); + return av_audio_fifo_read(afifo, (void **)a->data, nb_samples); } -static int write_samples_to_audio_fifo(AVAudioFifo* afifo, const TestStruct test_sample, +static int write_samples_to_audio_fifo(AVAudioFifo *afifo, Audio_Data *a, int nb_samples, int offset) { int offset_size, i; void *data_planes[MAX_CHANNELS]; - if(nb_samples > test_sample.nb_samples_pch - offset){ + if(nb_samples > a->nb_samples_pch - offset){ return 0; } - if(offset >= test_sample.nb_samples_pch){ + if(offset >= a->nb_samples_pch){ return 0; } - offset_size = offset * afifo->sample_size; + offset_size = offset * a->stride; - for (i = 0; i < afifo->nb_buffers ; ++i){ - data_planes[i] = (uint8_t*)test_sample.data_planes[i] + offset_size; + for (i = 0; i < a->planes ; ++i){ + data_planes[i] = a->data[i] + offset_size; } return av_audio_fifo_write(afifo, data_planes, nb_samples); @@ -119,68 +173,71 @@ static int write_samples_to_audio_fifo(AVAudioFifo* afifo, const TestStruct test static void test_function(const TestStruct test_sample) { int ret, i; - void **output_data = NULL; - AVAudioFifo *afifo = av_audio_fifo_alloc(test_sample.format, test_sample.nb_ch, - test_sample.nb_samples_pch); + Audio_Data *a; + AVAudioFifo *afifo; + + a = set_audio_data(test_sample); + if (!a) { + ERROR("ERROR: set_audio_data returned NULL!", NULL, NULL); + } + afifo = av_audio_fifo_alloc(a->sample_fmt, a->channels, a->nb_samples_pch); if (!afifo) { - ERROR("ERROR: av_audio_fifo_alloc returned NULL!"); + ERROR("ERROR: av_audio_fifo_alloc returned NULL!", NULL, a); } - ret = write_samples_to_audio_fifo(afifo, test_sample, test_sample.nb_samples_pch, 0); + + ret = write_samples_to_audio_fifo(afifo, a, a->nb_samples_pch, 0); if (ret < 0){ - ERROR("ERROR: av_audio_fifo_write failed!"); + ERROR("ERROR: av_audio_fifo_write failed!", afifo, a); } printf("written: %d\n", ret); - ret = write_samples_to_audio_fifo(afifo, test_sample, test_sample.nb_samples_pch, 0); + ret = write_samples_to_audio_fifo(afifo, a, a->nb_samples_pch, 0); if (ret < 0){ - ERROR("ERROR: av_audio_fifo_write failed!"); + ERROR("ERROR: av_audio_fifo_write failed!", afifo, a); } printf("written: %d\n", ret); printf("remaining samples in audio_fifo: %d\n\n", av_audio_fifo_size(afifo)); - ret = read_samples_from_audio_fifo(afifo, &output_data, test_sample.nb_samples_pch); + ret = read_samples_from_audio_fifo(afifo, a, a->allocated_samples); if (ret < 0){ - ERROR("ERROR: av_audio_fifo_write failed!"); + ERROR("ERROR: av_audio_fifo_write failed!", afifo, a); } printf("read: %d\n", ret); - print_audio_bytes(&test_sample, output_data, ret); + print_audio_bytes(a, NULL, ret, 0); printf("remaining samples in audio_fifo: %d\n\n", av_audio_fifo_size(afifo)); /* test av_audio_fifo_peek */ - ret = av_audio_fifo_peek(afifo, output_data, afifo->nb_samples); + ret = av_audio_fifo_peek(afifo, (void **)a->data, a->allocated_samples); if (ret < 0){ - ERROR("ERROR: av_audio_fifo_peek failed!"); + ERROR("ERROR: av_audio_fifo_peek failed!", afifo, a); } printf("peek:\n"); - print_audio_bytes(&test_sample, output_data, ret); + print_audio_bytes(a, NULL, ret, 0); printf("\n"); /* test av_audio_fifo_peek_at */ printf("peek_at:\n"); for (i = 0; i < afifo->nb_samples; ++i){ - ret = av_audio_fifo_peek_at(afifo, output_data, 1, i); + ret = av_audio_fifo_peek_at(afifo, (void **)a->data, 1, i); if (ret < 0){ - ERROR("ERROR: av_audio_fifo_peek failed!"); + ERROR("ERROR: av_audio_fifo_peek failed!", afifo, a); } printf("%d:\n", i); - print_audio_bytes(&test_sample, output_data, ret); + print_audio_bytes(a, NULL, ret, 0); } printf("\n"); /* test av_audio_fifo_drain */ ret = av_audio_fifo_drain(afifo, afifo->nb_samples); if (ret < 0){ - ERROR("ERROR: av_audio_fifo_drain failed!"); + ERROR("ERROR: av_audio_fifo_drain failed!", afifo, a); } if (afifo->nb_samples){ - ERROR("drain failed to flush all samples in audio_fifo!"); + ERROR("drain failed to flush all samples in audio_fifo!", afifo, a); } /* deallocate */ - for (i = 0; i < afifo->nb_buffers; ++i){ - free(output_data[i]); - } - free(output_data); + free_audio(a); av_audio_fifo_free(afifo); } diff --git a/tests/ref/fate/audio_fifo b/tests/ref/fate/audio_fifo index 6c6538b..3906984 100644 --- a/tests/ref/fate/audio_fifo +++ b/tests/ref/fate/audio_fifo @@ -1,42 +1,30 @@ TEST: 1 -written: 12 -written: 12 -remaining samples in audio_fifo: 24 +written: 6 +written: 6 +remaining samples in audio_fifo: 12 -read: 12 +read: 6 00 01 02 03 04 05 06 07 08 09 0a 0b -remaining samples in audio_fifo: 12 +remaining samples in audio_fifo: 6 peek: 00 01 02 03 04 05 06 07 08 09 0a 0b peek_at: 0: -00 +00 01 1: -01 +02 03 2: -02 +04 05 3: -03 +06 07 4: -04 +08 09 5: -05 -6: -06 -7: -07 -8: -08 -9: -09 -10: -0a -11: -0b +0a 0b TEST: 2