FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/tests/audio_fifo.c
Date: 2024-07-24 19:24:46
Exec Total Coverage
Lines: 79 95 83.2%
Functions: 6 7 85.7%
Branches: 35 48 72.9%

Line Branch Exec Source
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 #include "config.h"
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <inttypes.h>
23 #include "libavutil/mem.h"
24 #include "libavutil/audio_fifo.c"
25
26 #define MAX_CHANNELS 32
27
28
29 typedef struct TestStruct {
30 const enum AVSampleFormat format;
31 const int nb_ch;
32 void const *data_planes[MAX_CHANNELS];
33 const int nb_samples_pch;
34 } TestStruct;
35
36 static const uint8_t data_U8 [] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
37 static const int16_t data_S16[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
38 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};
39
40 static const TestStruct test_struct[] = {
41 {.format = AV_SAMPLE_FMT_U8 , .nb_ch = 1, .data_planes = {data_U8 , }, .nb_samples_pch = 12},
42 {.format = AV_SAMPLE_FMT_U8P , .nb_ch = 2, .data_planes = {data_U8 , data_U8 +6, }, .nb_samples_pch = 6 },
43 {.format = AV_SAMPLE_FMT_S16 , .nb_ch = 1, .data_planes = {data_S16, }, .nb_samples_pch = 12},
44 {.format = AV_SAMPLE_FMT_S16P , .nb_ch = 2, .data_planes = {data_S16, data_S16+6, }, .nb_samples_pch = 6 },
45 {.format = AV_SAMPLE_FMT_FLT , .nb_ch = 1, .data_planes = {data_FLT, }, .nb_samples_pch = 12},
46 {.format = AV_SAMPLE_FMT_FLTP , .nb_ch = 2, .data_planes = {data_FLT, data_FLT+6, }, .nb_samples_pch = 6 }
47 };
48
49 6 static void free_data_planes(AVAudioFifo *afifo, void **output_data)
50 {
51 int i;
52
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 6 times.
15 for (i = 0; i < afifo->nb_buffers; ++i){
53 9 av_freep(&output_data[i]);
54 }
55 6 av_freep(&output_data);
56 6 }
57
58 static void ERROR(const char *str)
59 {
60 fprintf(stderr, "%s\n", str);
61 exit(1);
62 }
63
64 66 static void print_audio_bytes(const TestStruct *test_sample, void **data_planes, int nb_samples)
65 {
66 int p, b, f;
67 66 int byte_offset = av_get_bytes_per_sample(test_sample->format);
68 66 int buffers = av_sample_fmt_is_planar(test_sample->format)
69
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 42 times.
66 ? test_sample->nb_ch : 1;
70 66 int line_size = (buffers > 1) ? nb_samples * byte_offset
71
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 42 times.
66 : nb_samples * byte_offset * test_sample->nb_ch;
72
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 66 times.
156 for (p = 0; p < buffers; ++p){
73
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 90 times.
306 for(b = 0; b < line_size; b+=byte_offset){
74
2/2
✓ Branch 0 taken 504 times.
✓ Branch 1 taken 216 times.
720 for (f = 0; f < byte_offset; f++){
75 504 int order = !HAVE_BIGENDIAN ? (byte_offset - f - 1) : f;
76 504 printf("%02x", *((uint8_t*)data_planes[p] + b + order));
77 }
78 216 putchar(' ');
79 }
80 90 putchar('\n');
81 }
82 66 }
83
84 6 static int read_samples_from_audio_fifo(AVAudioFifo* afifo, void ***output, int nb_samples)
85 {
86 int i;
87 6 int samples = FFMIN(nb_samples, afifo->nb_samples);
88 6 int tot_elements = !av_sample_fmt_is_planar(afifo->sample_fmt)
89
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 ? samples : afifo->channels * samples;
90 6 void **data_planes = av_malloc_array(afifo->nb_buffers, sizeof(void*));
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!data_planes)
92 ERROR("failed to allocate memory!");
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (*output)
94 free_data_planes(afifo, *output);
95 6 *output = data_planes;
96
97
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 6 times.
15 for (i = 0; i < afifo->nb_buffers; ++i){
98 9 data_planes[i] = av_malloc_array(tot_elements, afifo->sample_size);
99
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (!data_planes[i])
100 ERROR("failed to allocate memory!");
101 }
102
103 6 return av_audio_fifo_read(afifo, *output, nb_samples);
104 }
105
106 12 static int write_samples_to_audio_fifo(AVAudioFifo* afifo, const TestStruct *test_sample,
107 int nb_samples, int offset)
108 {
109 int offset_size, i;
110 void *data_planes[MAX_CHANNELS];
111
112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if(nb_samples > test_sample->nb_samples_pch - offset){
113 return 0;
114 }
115
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if(offset >= test_sample->nb_samples_pch){
116 return 0;
117 }
118 12 offset_size = offset * afifo->sample_size;
119
120
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 12 times.
30 for (i = 0; i < afifo->nb_buffers ; ++i){
121 18 data_planes[i] = (uint8_t*)test_sample->data_planes[i] + offset_size;
122 }
123
124 12 return av_audio_fifo_write(afifo, data_planes, nb_samples);
125 }
126
127 6 static void test_function(const TestStruct *test_sample)
128 {
129 int ret, i;
130 6 void **output_data = NULL;
131 6 AVAudioFifo *afifo = av_audio_fifo_alloc(test_sample->format, test_sample->nb_ch,
132 6 test_sample->nb_samples_pch);
133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!afifo) {
134 ERROR("ERROR: av_audio_fifo_alloc returned NULL!");
135 }
136 6 ret = write_samples_to_audio_fifo(afifo, test_sample, test_sample->nb_samples_pch, 0);
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0){
138 ERROR("ERROR: av_audio_fifo_write failed!");
139 }
140 6 printf("written: %d\n", ret);
141
142 6 ret = write_samples_to_audio_fifo(afifo, test_sample, test_sample->nb_samples_pch, 0);
143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0){
144 ERROR("ERROR: av_audio_fifo_write failed!");
145 }
146 6 printf("written: %d\n", ret);
147 6 printf("remaining samples in audio_fifo: %d\n\n", av_audio_fifo_size(afifo));
148
149 6 ret = read_samples_from_audio_fifo(afifo, &output_data, test_sample->nb_samples_pch);
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0){
151 ERROR("ERROR: av_audio_fifo_read failed!");
152 }
153 6 printf("read: %d\n", ret);
154 6 print_audio_bytes(test_sample, output_data, ret);
155 6 printf("remaining samples in audio_fifo: %d\n\n", av_audio_fifo_size(afifo));
156
157 /* test av_audio_fifo_peek */
158 6 ret = av_audio_fifo_peek(afifo, output_data, afifo->nb_samples);
159
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0){
160 ERROR("ERROR: av_audio_fifo_peek failed!");
161 }
162 6 printf("peek:\n");
163 6 print_audio_bytes(test_sample, output_data, ret);
164 6 printf("\n");
165
166 /* test av_audio_fifo_peek_at */
167 6 printf("peek_at:\n");
168
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 6 times.
60 for (i = 0; i < afifo->nb_samples; ++i){
169 54 ret = av_audio_fifo_peek_at(afifo, output_data, 1, i);
170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
54 if (ret < 0){
171 ERROR("ERROR: av_audio_fifo_peek_at failed!");
172 }
173 54 printf("%d:\n", i);
174 54 print_audio_bytes(test_sample, output_data, ret);
175 }
176 6 printf("\n");
177
178 /* test av_audio_fifo_drain */
179 6 ret = av_audio_fifo_drain(afifo, afifo->nb_samples);
180
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0){
181 ERROR("ERROR: av_audio_fifo_drain failed!");
182 }
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (afifo->nb_samples){
184 ERROR("drain failed to flush all samples in audio_fifo!");
185 }
186
187 /* deallocate */
188 6 free_data_planes(afifo, output_data);
189 6 av_audio_fifo_free(afifo);
190 6 }
191
192 1 int main(void)
193 {
194 1 int t, tests = sizeof(test_struct)/sizeof(test_struct[0]);
195
196
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
7 for (t = 0; t < tests; ++t){
197 6 printf("\nTEST: %d\n\n", t+1);
198 6 test_function(&test_struct[t]);
199 }
200 1 return 0;
201 }
202