[FFmpeg-devel] [PATCH] avutil: Added selftest for libavutil/audio_fifo.c

Thomas Turner thomastdt at googlemail.com
Sat Dec 17 13:16:30 EET 2016


Signed-off-by: Thomas Turner <thomastdt at googlemail.com>
---
 libavutil/Makefile           |   1 +
 libavutil/tests/audio_fifo.c | 196 +++++++++++++++++++++++++++++++++++++
 tests/fate/libavutil.mak     |   4 +
 tests/ref/fate/audio_fifo    | 228 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 429 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..6dffe85
--- /dev/null
+++ b/libavutil/tests/audio_fifo.c
@@ -0,0 +1,196 @@
+/*
+ * 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 <stdlib.h>
+#include <inttypes.h>
+#include "libavutil/audio_fifo.c"
+#include "libavutil/audio_fifo.h"
+#include "libavutil/error.h"
+
+#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..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
+
-- 
1.9.1



More information about the ffmpeg-devel mailing list