From patchwork Tue Dec 20 02:39:03 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Turner X-Patchwork-Id: 1867 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.65.86 with SMTP id o83csp1461410vsa; Mon, 19 Dec 2016 18:45:08 -0800 (PST) X-Received: by 10.28.62.141 with SMTP id l135mr17424919wma.20.1482201908489; Mon, 19 Dec 2016 18:45:08 -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 n11si20696652wjw.117.2016.12.19.18.45.08; Mon, 19 Dec 2016 18:45:08 -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 EC4F768A14C; Tue, 20 Dec 2016 04:44:57 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf0-f195.google.com (mail-pf0-f195.google.com [209.85.192.195]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A401468A0B7 for ; Tue, 20 Dec 2016 04:44:51 +0200 (EET) Received: by mail-pf0-f195.google.com with SMTP id 144so8149663pfv.0 for ; Mon, 19 Dec 2016 18:44:59 -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=J2E2JcMelDYQDrrNH+/BkqXilNGILWJ+C773JaIactM=; b=e/fhJa0hdm3AYWA4lfk1Ii5deSLRgGUsa0LBR7nExEX8nGvpECEb0kCsOo6t9vVitW RvBWrR3DV0/VyRxD3U6F14zGbVX44V6NySedCd2Vao4vqEYxeyhoUfPZKNVjlPWWnIuK CI7+Vso3ROoQT6lZuWWRDrSfMOnDtLCPyLhPT06BWENdrAV1dVigvUDVEA+p6645bnUt Ef41Q1520HsKSAR4LV2psCgvNAQ/GUud+2Jz0wA3kF1xGD6E4T9+AB8xN3v2yj1j8i9/ uI1gr4ADNDS/85w3zFmfwpO4x8Vjt2zc0vQvVbZVC1TQj/zPKQfYKzEcyRqc55od3mrl wykQ== 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=J2E2JcMelDYQDrrNH+/BkqXilNGILWJ+C773JaIactM=; b=WJsbCTD3PSk3y73acPczBPe9VUv+2peWNlGf8jdwXlgr1UE4smbItvNIDdtRkjATTz 2d0vmfbcIBEx7oEEOQfkrSgZerjFBOXGQ0XFzleYKjVDz8Sb+9WTErrqTWPNk/FvDwqc bVIBSwk966UW+K17gI0pQoOAu/n/IMGkUcpYekbqHhClEmHKeESvGS47MkD5Fmlgbk/G AXx9lDyRsfxnctxbzl2XCVkE9+rLtJ/Legvu7X7rA0nQdDM+qFB6wi2o1Yw324vcoHMK 1NYIT8gBbUjT6ZQRyfux/h1GsyNhXDOpqHUY8jZTcBfqDLeHjN348wACMbNWqdzoIZPO TqbQ== X-Gm-Message-State: AIkVDXJ2bsCvDbJICbxg6y2IgBlvqNgYJFH+IVYgPMh8K1N0c8X3YmL0epb+k4ZJj7nAJw== X-Received: by 10.84.209.134 with SMTP id y6mr21522140plh.32.1482201545788; Mon, 19 Dec 2016 18:39:05 -0800 (PST) Received: from Zany.fios-router.home ([47.147.141.23]) by smtp.gmail.com with ESMTPSA id n8sm13236053pgc.0.2016.12.19.18.39.04 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 19 Dec 2016 18:39:05 -0800 (PST) From: Thomas Turner To: ffmpeg-devel@ffmpeg.org Date: Mon, 19 Dec 2016 18:39:03 -0800 Message-Id: <1482201543-6796-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 | 195 ++++++++++++++++++++++++++++++++++++ tests/fate/libavutil.mak | 4 + tests/ref/fate/audio_fifo | 228 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 428 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..7e166b1 --- /dev/null +++ b/libavutil/tests/audio_fifo.c @@ -0,0 +1,195 @@ +/* + * 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 +#include "libavutil/audio_fifo.c" + +#define MAX_CHANNELS 32 + +#define ERROR(str) \ + fprintf(stderr, "%s\n", str); \ + exit(1); + +typedef struct TestStruct { + const enum AVSampleFormat format; + const int nb_ch; + void const *data_planes[MAX_CHANNELS]; + int nb_samples_pch; +} TestStruct; + +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_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 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 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)); + } + 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, const 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(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); + if (!afifo) { + ERROR("ERROR: av_audio_fifo_alloc returned NULL!"); + } + ret = write_samples_to_audio_fifo(afifo, test_sample, test_sample.nb_samples_pch, 0); + if (ret < 0){ + ERROR("ERROR: av_audio_fifo_write failed!"); + } + printf("written: %d\n", ret); + + ret = write_samples_to_audio_fifo(afifo, test_sample, test_sample.nb_samples_pch, 0); + if (ret < 0){ + ERROR("ERROR: av_audio_fifo_write failed!"); + } + 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){ + ERROR("ERROR: av_audio_fifo_write failed!"); + } + 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){ + ERROR("ERROR: av_audio_fifo_peek failed!"); + } + 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){ + ERROR("ERROR: av_audio_fifo_peek failed!"); + } + 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){ + ERROR("ERROR: av_audio_fifo_drain failed!"); + } + if (afifo->nb_samples){ + ERROR("drain failed to flush all samples in audio_fifo!"); + } + + /* 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..5987a83 100644 --- a/tests/fate/libavutil.mak +++ b/tests/fate/libavutil.mak @@ -23,6 +23,10 @@ fate-atomic: libavutil/tests/atomic$(EXESUF) fate-atomic: CMD = run libavutil/tests/atomic fate-atomic: 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-avstring fate-avstring: libavutil/tests/avstring$(EXESUF) fate-avstring: CMD = run libavutil/tests/avstring 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 +