LCOV - code coverage report
Current view: top level - libavutil/tests - audio_fifo.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 78 94 83.0 %
Date: 2017-10-18 21:45:51 Functions: 6 7 85.7 %

          Line data    Source code
       1             : /*
       2             :  * This file is part of FFmpeg.
       3             :  *
       4             :  * FFmpeg is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Lesser General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2.1 of the License, or (at your option) any later version.
       8             :  *
       9             :  * FFmpeg is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Lesser General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Lesser General Public
      15             :  * License along with FFmpeg; if not, write to the Free Software
      16             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      17             :  */
      18             : 
      19             : #include <stdlib.h>
      20             : #include <stdio.h>
      21             : #include <inttypes.h>
      22             : #include "libavutil/mem.h"
      23             : #include "libavutil/audio_fifo.c"
      24             : 
      25             : #define MAX_CHANNELS    32
      26             : 
      27             : 
      28             : typedef struct TestStruct {
      29             :     const enum AVSampleFormat format;
      30             :     const int nb_ch;
      31             :     void const *data_planes[MAX_CHANNELS];
      32             :     const int nb_samples_pch;
      33             : } TestStruct;
      34             : 
      35             : static const uint8_t data_U8 [] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11                        };
      36             : static const int16_t data_S16[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11                        };
      37             : 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};
      38             : 
      39             : static const TestStruct test_struct[] = {
      40             :     {.format = AV_SAMPLE_FMT_U8   , .nb_ch = 1, .data_planes = {data_U8 ,             }, .nb_samples_pch = 12},
      41             :     {.format = AV_SAMPLE_FMT_U8P  , .nb_ch = 2, .data_planes = {data_U8 , data_U8 +6, }, .nb_samples_pch = 6 },
      42             :     {.format = AV_SAMPLE_FMT_S16  , .nb_ch = 1, .data_planes = {data_S16,             }, .nb_samples_pch = 12},
      43             :     {.format = AV_SAMPLE_FMT_S16P , .nb_ch = 2, .data_planes = {data_S16, data_S16+6, }, .nb_samples_pch = 6 },
      44             :     {.format = AV_SAMPLE_FMT_FLT  , .nb_ch = 1, .data_planes = {data_FLT,             }, .nb_samples_pch = 12},
      45             :     {.format = AV_SAMPLE_FMT_FLTP , .nb_ch = 2, .data_planes = {data_FLT, data_FLT+6, }, .nb_samples_pch = 6 }
      46             : };
      47             : 
      48           6 : static void free_data_planes(AVAudioFifo *afifo, void **output_data)
      49             : {
      50             :     int i;
      51          15 :     for (i = 0; i < afifo->nb_buffers; ++i){
      52           9 :         av_freep(&output_data[i]);
      53             :     }
      54           6 :     av_freep(&output_data);
      55           6 : }
      56             : 
      57           0 : static void ERROR(const char *str)
      58             : {
      59           0 :     fprintf(stderr, "%s\n", str);
      60           0 :     exit(1);
      61             : }
      62             : 
      63          66 : static void print_audio_bytes(const TestStruct *test_sample, void **data_planes, int nb_samples)
      64             : {
      65             :     int p, b, f;
      66          66 :     int byte_offset      = av_get_bytes_per_sample(test_sample->format);
      67         132 :     int buffers          = av_sample_fmt_is_planar(test_sample->format)
      68          66 :                                          ? test_sample->nb_ch : 1;
      69          66 :     int line_size        = (buffers > 1) ? nb_samples * byte_offset
      70          66 :                                          : nb_samples * byte_offset * test_sample->nb_ch;
      71         156 :     for (p = 0; p < buffers; ++p){
      72         306 :         for(b = 0; b < line_size; b+=byte_offset){
      73         720 :             for (f = 0; f < byte_offset; f++){
      74         504 :                 int order = !HAVE_BIGENDIAN ? (byte_offset - f - 1) : f;
      75         504 :                 printf("%02x", *((uint8_t*)data_planes[p] + b + order));
      76             :             }
      77         216 :             putchar(' ');
      78             :         }
      79          90 :         putchar('\n');
      80             :     }
      81          66 : }
      82             : 
      83           6 : static int read_samples_from_audio_fifo(AVAudioFifo* afifo, void ***output, int nb_samples)
      84             : {
      85             :     int i;
      86           6 :     int samples        = FFMIN(nb_samples, afifo->nb_samples);
      87          12 :     int tot_elements   = !av_sample_fmt_is_planar(afifo->sample_fmt)
      88           6 :                          ? samples : afifo->channels * samples;
      89           6 :     void **data_planes = av_malloc_array(afifo->nb_buffers, sizeof(void*));
      90           6 :     if (!data_planes)
      91           0 :         ERROR("failed to allocate memory!");
      92           6 :     if (*output)
      93           0 :         free_data_planes(afifo, *output);
      94           6 :     *output            = data_planes;
      95             : 
      96          15 :     for (i = 0; i < afifo->nb_buffers; ++i){
      97           9 :         data_planes[i] = av_malloc_array(tot_elements, afifo->sample_size);
      98           9 :         if (!data_planes[i])
      99           0 :             ERROR("failed to allocate memory!");
     100             :     }
     101             : 
     102           6 :     return av_audio_fifo_read(afifo, *output, nb_samples);
     103             : }
     104             : 
     105          12 : static int write_samples_to_audio_fifo(AVAudioFifo* afifo, const TestStruct *test_sample,
     106             :                                        int nb_samples, int offset)
     107             : {
     108             :     int offset_size, i;
     109             :     void *data_planes[MAX_CHANNELS];
     110             : 
     111          12 :     if(nb_samples > test_sample->nb_samples_pch - offset){
     112           0 :         return 0;
     113             :     }
     114          12 :     if(offset >= test_sample->nb_samples_pch){
     115           0 :         return 0;
     116             :     }
     117          12 :     offset_size  = offset * afifo->sample_size;
     118             : 
     119          30 :     for (i = 0; i < afifo->nb_buffers ; ++i){
     120          18 :         data_planes[i] = (uint8_t*)test_sample->data_planes[i] + offset_size;
     121             :     }
     122             : 
     123          12 :     return av_audio_fifo_write(afifo, data_planes, nb_samples);
     124             : }
     125             : 
     126           6 : static void test_function(const TestStruct *test_sample)
     127             : {
     128             :     int ret, i;
     129           6 :     void **output_data  = NULL;
     130           6 :     AVAudioFifo *afifo  = av_audio_fifo_alloc(test_sample->format, test_sample->nb_ch,
     131             :                                             test_sample->nb_samples_pch);
     132           6 :     if (!afifo) {
     133           0 :         ERROR("ERROR: av_audio_fifo_alloc returned NULL!");
     134             :     }
     135           6 :     ret = write_samples_to_audio_fifo(afifo, test_sample, test_sample->nb_samples_pch, 0);
     136           6 :     if (ret < 0){
     137           0 :         ERROR("ERROR: av_audio_fifo_write failed!");
     138             :     }
     139           6 :     printf("written: %d\n", ret);
     140             : 
     141           6 :     ret = write_samples_to_audio_fifo(afifo, test_sample, test_sample->nb_samples_pch, 0);
     142           6 :     if (ret < 0){
     143           0 :         ERROR("ERROR: av_audio_fifo_write failed!");
     144             :     }
     145           6 :     printf("written: %d\n", ret);
     146           6 :     printf("remaining samples in audio_fifo: %d\n\n", av_audio_fifo_size(afifo));
     147             : 
     148           6 :     ret = read_samples_from_audio_fifo(afifo, &output_data, test_sample->nb_samples_pch);
     149           6 :     if (ret < 0){
     150           0 :         ERROR("ERROR: av_audio_fifo_read failed!");
     151             :     }
     152           6 :     printf("read: %d\n", ret);
     153           6 :     print_audio_bytes(test_sample, output_data, ret);
     154           6 :     printf("remaining samples in audio_fifo: %d\n\n", av_audio_fifo_size(afifo));
     155             : 
     156             :     /* test av_audio_fifo_peek */
     157           6 :     ret = av_audio_fifo_peek(afifo, output_data, afifo->nb_samples);
     158           6 :     if (ret < 0){
     159           0 :         ERROR("ERROR: av_audio_fifo_peek failed!");
     160             :     }
     161           6 :     printf("peek:\n");
     162           6 :     print_audio_bytes(test_sample, output_data, ret);
     163           6 :     printf("\n");
     164             : 
     165             :     /* test av_audio_fifo_peek_at */
     166           6 :     printf("peek_at:\n");
     167          60 :     for (i = 0; i < afifo->nb_samples; ++i){
     168          54 :         ret = av_audio_fifo_peek_at(afifo, output_data, 1, i);
     169          54 :         if (ret < 0){
     170           0 :             ERROR("ERROR: av_audio_fifo_peek_at failed!");
     171             :         }
     172          54 :         printf("%d:\n", i);
     173          54 :         print_audio_bytes(test_sample, output_data, ret);
     174             :     }
     175           6 :     printf("\n");
     176             : 
     177             :     /* test av_audio_fifo_drain */
     178           6 :     ret = av_audio_fifo_drain(afifo, afifo->nb_samples);
     179           6 :     if (ret < 0){
     180           0 :         ERROR("ERROR: av_audio_fifo_drain failed!");
     181             :     }
     182           6 :     if (afifo->nb_samples){
     183           0 :         ERROR("drain failed to flush all samples in audio_fifo!");
     184             :     }
     185             : 
     186             :     /* deallocate */
     187           6 :     free_data_planes(afifo, output_data);
     188           6 :     av_audio_fifo_free(afifo);
     189           6 : }
     190             : 
     191           1 : int main(void)
     192             : {
     193           1 :     int t, tests = sizeof(test_struct)/sizeof(test_struct[0]);
     194             : 
     195           7 :     for (t = 0; t < tests; ++t){
     196           6 :         printf("\nTEST: %d\n\n", t+1);
     197           6 :         test_function(&test_struct[t]);
     198             :     }
     199           1 :     return 0;
     200             : }

Generated by: LCOV version 1.13