[FFmpeg-devel] avutil/tests: Replacement selftest for audio_fifo.c

Submitted by Thomas Turner on Dec. 27, 2016, 2:21 a.m.

Details

Message ID 1482805278-5678-1-git-send-email-thomastdt@googlemail.com
State New
Headers show

Commit Message

Thomas Turner Dec. 27, 2016, 2:21 a.m.
Signed-off-by: Thomas Turner <thomastdt@googlemail.com>
---
 libavutil/tests/audio_fifo.c | 181 ++++++++++++++++++++++++++++---------------
 tests/ref/fate/audio_fifo    |  34 +++-----
 2 files changed, 130 insertions(+), 85 deletions(-)

Comments

Michael Niedermayer Dec. 27, 2016, 4:23 p.m.
On Mon, Dec 26, 2016 at 06:21:18PM -0800, Thomas Turner wrote:
> Signed-off-by: Thomas Turner <thomastdt@googlemail.com>
> ---
>  libavutil/tests/audio_fifo.c | 181 ++++++++++++++++++++++++++++---------------
>  tests/ref/fate/audio_fifo    |  34 +++-----
>  2 files changed, 130 insertions(+), 85 deletions(-)

The commit message is a bit terse, please describe what/why things
are changed

[...]
> +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);
> +}

the indention depth seems inconsistent compared to the other functions

[...]

Patch hide | download patch | download mbox

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 <stdlib.h>
 #include <stdio.h>
 #include <inttypes.h>
+#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