From patchwork Fri Dec 16 03:55:57 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Turner X-Patchwork-Id: 1828 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.65.86 with SMTP id o83csp1122312vsa; Thu, 15 Dec 2016 20:04:22 -0800 (PST) X-Received: by 10.194.43.164 with SMTP id x4mr648179wjl.155.1481861062670; Thu, 15 Dec 2016 20:04:22 -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 i2si1497160wma.140.2016.12.15.20.04.21; Thu, 15 Dec 2016 20:04:22 -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 80F3E689B20; Fri, 16 Dec 2016 06:04:12 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-oi0-f68.google.com (mail-oi0-f68.google.com [209.85.218.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 249EA680CF2 for ; Fri, 16 Dec 2016 06:04:05 +0200 (EET) Received: by mail-oi0-f68.google.com with SMTP id u15so8527429oie.3 for ; Thu, 15 Dec 2016 20:04:11 -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=ss+H4srD1/huPHdRVK9pPZ3WT4Ob0S5rqypOKgZXLZ0=; b=X1eHLiNiejIdF+1n+NNGsbjTGjlbhqI4Oqwfm7jh+Ks4KnfHb4WT7hX0LHYDBbLQBa suHaohQP9BD+4+KxTpJHLh2FpWG4JFJ3VX+2rrYQnVfV9By70kYK7JwbCKUkw3pOG52o Nlx1IvMLvU/BuUSn0JMGDa+SBClMr5vKDZnZ1BmUCS9OJtObGzonjSoCzDq11nF9UEF0 CO/VqiRFzYRFTF590EDNXfaJM8MzWwUdOFYZypIgKtZtwphIXQ3hThmrblWflV3U3bsc siGKPOQgf5TqJq12kijRwM3MmRY1I8I0yROGRecfWbphG9UiyC51sa7pT4gc85DCdVb+ 71JA== 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=ss+H4srD1/huPHdRVK9pPZ3WT4Ob0S5rqypOKgZXLZ0=; b=jgxIfCkdRqG+w8jzUavrbnYXF8GAnSqyFqeeql2uXXGOKAt5fxEjgWXIGL1y0AaU4/ DcyBHx4RqEuZI1Emsq2gY1Ucc80LqUrZdxYMLgsFOw4Wm9wj71+EuHJbY8FHZF2YLjYB Ox24hdNI6ZT1cAKVmlRUkp6xWvsUDxz5kSqHDGz7y5twQQwQXj8nyPGG8yE57+jPNNdS WwP3vol47Il4GyiPxhp/iAJptfh9sfCeMGwQEIed4MEMa6bqxmlAzCJMwOJZAFVPHPfZ rWgWeNQQMLOkeC7UqEd4sgNXPED/lAy7AmiEeU9vmpp/Cxtp5xy2MI8B6nJr1gbtdON9 5vow== X-Gm-Message-State: AIkVDXJYdl+2Do+2VVqsHX6QNuQghtkLIabMtdNmsw/59HAVBwN5PSbTGoAe/Z45vsj0Ug== X-Received: by 10.157.5.209 with SMTP id 75mr833007otd.229.1481860560771; Thu, 15 Dec 2016 19:56:00 -0800 (PST) Received: from Zany.attlocal.net (76-252-225-111.lightspeed.sndgca.sbcglobal.net. [76.252.225.111]) by smtp.gmail.com with ESMTPSA id r102sm1808611ota.11.2016.12.15.19.56.00 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 15 Dec 2016 19:56:00 -0800 (PST) From: Thomas Turner To: ffmpeg-devel@ffmpeg.org Date: Thu, 15 Dec 2016 19:55:57 -0800 Message-Id: <1481860557-4751-1-git-send-email-thomastdt@googlemail.com> X-Mailer: git-send-email 1.9.1 Subject: [FFmpeg-devel] [PATCH] avutil: Added selftest for libavutil/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/Makefile | 1 + libavutil/tests/audio_fifo.c | 210 +++++++++++++++++++++++++++++++++++++++ tests/fate/libavutil.mak | 4 + tests/ref/fate/audio_fifo | 228 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 443 insertions(+) create mode 100644 libavutil/tests/audio_fifo.c create mode 100644 tests/ref/fate/audio_fifo diff --git a/libavutil/Makefile b/libavutil/Makefile index 9841645..2dd91b8 100644 --- a/libavutil/Makefile +++ b/libavutil/Makefile @@ -182,6 +182,7 @@ SKIPHEADERS-$(CONFIG_OPENCL) += opencl.h TESTPROGS = adler32 \ aes \ atomic \ + audio_fifo \ avstring \ base64 \ blowfish \ diff --git a/libavutil/tests/audio_fifo.c b/libavutil/tests/audio_fifo.c new file mode 100644 index 0000000..e3a3484 --- /dev/null +++ b/libavutil/tests/audio_fifo.c @@ -0,0 +1,210 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +//#include "libavutil/audio_fifo.c" +#include "../audio_fifo.c" +#include "libavutil/audio_fifo.h" +#include "libavutil/error.h" + +#define MAX_CHANNELS 32 + + +typedef struct TestStruct { + const char *message; + const enum AVSampleFormat format; + const int nb_ch; + void *data_planes[MAX_CHANNELS]; + int nb_samples_pch; +} TestStruct; + +static uint8_t data_U8 [] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; +static int16_t data_S16[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; +static 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_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 }, + {.format = AV_SAMPLE_FMT_FLT , .nb_ch = 1, .data_planes = {data_FLT, }, .nb_samples_pch = 12}, + {.format = AV_SAMPLE_FMT_FLTP , .nb_ch = 2, .data_planes = {data_FLT, data_FLT+6, }, .nb_samples_pch = 6 } +}; + +static int is_little_endian(void) +{ + const unsigned int i = 1; + return *(uint8_t*)&i; +} + +static void* allocate_memory(size_t size) +{ + void *ptr = malloc(size); + if (ptr == NULL){ + fprintf(stderr, "failed to allocate memory!\n"); + exit(1); + } + return ptr; +} + +static void print_audio_bytes(const TestStruct *test_sample, void **data_planes, int nb_samples) +{ + int p, b, f; + int e = is_little_endian(); + 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 = e ? (byte_offset - f - 1) : f; + printf("%02x", *((uint8_t*)data_planes[p] + b + order)); + } + putchar(' '); + } + putchar('\n'); + } +} + +static int read_samples_from_audio_fifo(AVAudioFifo* afifo, void ***output, int nb_samples) +{ + int i, planes; + int samples = FFMIN(nb_samples, afifo->nb_samples); + int tot_elements = !(planes = av_sample_fmt_is_planar(afifo->sample_fmt)) + ? samples : afifo->channels * samples; + void **data_planes = allocate_memory(sizeof(void*) * planes); + *output = data_planes; + + for (i = 0; i < afifo->nb_buffers; ++i){ + data_planes[i] = allocate_memory(afifo->sample_size * tot_elements); + } + + return av_audio_fifo_read(afifo, *output, nb_samples); +} + +static int write_samples_to_audio_fifo(AVAudioFifo* afifo, TestStruct test_sample, + int nb_samples, int offset) +{ + int offset_size, i; + void *data_planes[MAX_CHANNELS]; + + if(nb_samples > test_sample.nb_samples_pch - offset){ + return 0; + } + if(offset >= test_sample.nb_samples_pch){ + return 0; + } + offset_size = offset * afifo->sample_size; + + for (i = 0; i < afifo->nb_buffers ; ++i){ + data_planes[i] = (uint8_t*)test_sample.data_planes[i] + offset_size; + } + + return av_audio_fifo_write(afifo, data_planes, nb_samples); +} + +static void test_function(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); + if (!afifo) { + fprintf(stderr, "ERROR: av_audio_fifo_alloc returned NULL!\n"); + exit(1); + } + ret = write_samples_to_audio_fifo(afifo, test_sample, test_sample.nb_samples_pch, 0); + if (ret < 0){ + fprintf(stderr, "ERROR: av_audio_fifo_write failed!\n"); + exit(1); + } + printf("written: %d\n", ret); + + ret = write_samples_to_audio_fifo(afifo, test_sample, test_sample.nb_samples_pch, 0); + if (ret < 0){ + fprintf(stderr, "ERROR: av_audio_fifo_write failed!\n"); + exit(1); + } + 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); + if (ret < 0){ + fprintf(stderr, "ERROR: av_audio_fifo_write failed!\n"); + exit(1); + } + printf("read: %d\n", ret); + print_audio_bytes(&test_sample, output_data, ret); + 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); + if (ret < 0){ + fprintf(stderr, "ERROR: av_audio_fifo_peek failed!\n"); + exit(1); + } + printf("peek:\n"); + print_audio_bytes(&test_sample, output_data, ret); + 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); + if (ret < 0){ + fprintf(stderr, "ERROR: av_audio_fifo_peek failed!\n"); + exit(1); + } + printf("%d:\n", i); + print_audio_bytes(&test_sample, output_data, ret); + } + printf("\n"); + + /* test av_audio_fifo_drain */ + ret = av_audio_fifo_drain(afifo, afifo->nb_samples); + if (ret < 0){ + fprintf(stderr, "ERROR: av_audio_fifo_drain failed!\n"); + exit(1); + } + if (afifo->nb_samples){ + printf("drain failed to flush all samples in audio_fifo!\n"); + exit(1); + } + + /* deallocate */ + for (i = 0; i < afifo->nb_buffers; ++i){ + free(output_data[i]); + } + free(output_data); + av_audio_fifo_free(afifo); +} + +int main(void) +{ + int t, tests = sizeof(test_struct)/sizeof(test_struct[0]); + + for (t = 0; t < tests; ++t){ + printf("\nTEST: %d\n\n", t+1); + test_function(test_struct[t]); + } + return 0; +} \ No newline at end of file diff --git a/tests/fate/libavutil.mak b/tests/fate/libavutil.mak index 06f968c..932ef71 100644 --- a/tests/fate/libavutil.mak +++ b/tests/fate/libavutil.mak @@ -8,6 +8,10 @@ fate-aes: libavutil/tests/aes$(EXESUF) fate-aes: CMD = run libavutil/tests/aes fate-aes: REF = /dev/null +FATE_LIBAVUTIL += fate-audio_fifo +fate-audio_fifo: libavutil/tests/audio_fifo$(EXESUF) +fate-audio_fifo: CMD = run libavutil/tests/audio_fifo + FATE_LIBAVUTIL += fate-camellia fate-camellia: libavutil/tests/camellia$(EXESUF) fate-camellia: CMD = run libavutil/tests/camellia diff --git a/tests/ref/fate/audio_fifo b/tests/ref/fate/audio_fifo new file mode 100644 index 0000000..6c6538b --- /dev/null +++ b/tests/ref/fate/audio_fifo @@ -0,0 +1,228 @@ + +TEST: 1 + +written: 12 +written: 12 +remaining samples in audio_fifo: 24 + +read: 12 +00 01 02 03 04 05 06 07 08 09 0a 0b +remaining samples in audio_fifo: 12 + +peek: +00 01 02 03 04 05 06 07 08 09 0a 0b + +peek_at: +0: +00 +1: +01 +2: +02 +3: +03 +4: +04 +5: +05 +6: +06 +7: +07 +8: +08 +9: +09 +10: +0a +11: +0b + + +TEST: 2 + +written: 6 +written: 6 +remaining samples in audio_fifo: 12 + +read: 6 +00 01 02 03 04 05 +06 07 08 09 0a 0b +remaining samples in audio_fifo: 6 + +peek: +00 01 02 03 04 05 +06 07 08 09 0a 0b + +peek_at: +0: +00 +06 +1: +01 +07 +2: +02 +08 +3: +03 +09 +4: +04 +0a +5: +05 +0b + + +TEST: 3 + +written: 12 +written: 12 +remaining samples in audio_fifo: 24 + +read: 12 +0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 000a 000b +remaining samples in audio_fifo: 12 + +peek: +0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 000a 000b + +peek_at: +0: +0000 +1: +0001 +2: +0002 +3: +0003 +4: +0004 +5: +0005 +6: +0006 +7: +0007 +8: +0008 +9: +0009 +10: +000a +11: +000b + + +TEST: 4 + +written: 6 +written: 6 +remaining samples in audio_fifo: 12 + +read: 6 +0000 0001 0002 0003 0004 0005 +0006 0007 0008 0009 000a 000b +remaining samples in audio_fifo: 6 + +peek: +0000 0001 0002 0003 0004 0005 +0006 0007 0008 0009 000a 000b + +peek_at: +0: +0000 +0006 +1: +0001 +0007 +2: +0002 +0008 +3: +0003 +0009 +4: +0004 +000a +5: +0005 +000b + + +TEST: 5 + +written: 12 +written: 12 +remaining samples in audio_fifo: 24 + +read: 12 +00000000 3f800000 40000000 40400000 40800000 40a00000 40c00000 40e00000 41000000 41100000 41200000 41300000 +remaining samples in audio_fifo: 12 + +peek: +00000000 3f800000 40000000 40400000 40800000 40a00000 40c00000 40e00000 41000000 41100000 41200000 41300000 + +peek_at: +0: +00000000 +1: +3f800000 +2: +40000000 +3: +40400000 +4: +40800000 +5: +40a00000 +6: +40c00000 +7: +40e00000 +8: +41000000 +9: +41100000 +10: +41200000 +11: +41300000 + + +TEST: 6 + +written: 6 +written: 6 +remaining samples in audio_fifo: 12 + +read: 6 +00000000 3f800000 40000000 40400000 40800000 40a00000 +40c00000 40e00000 41000000 41100000 41200000 41300000 +remaining samples in audio_fifo: 6 + +peek: +00000000 3f800000 40000000 40400000 40800000 40a00000 +40c00000 40e00000 41000000 41100000 41200000 41300000 + +peek_at: +0: +00000000 +40c00000 +1: +3f800000 +40e00000 +2: +40000000 +41000000 +3: +40400000 +41100000 +4: +40800000 +41200000 +5: +40a00000 +41300000 +