FFmpeg  4.3
audio_fifo.c
Go to the documentation of this file.
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 static void free_data_planes(AVAudioFifo *afifo, void **output_data)
49 {
50  int i;
51  for (i = 0; i < afifo->nb_buffers; ++i){
53  }
55 }
56 
57 static void ERROR(const char *str)
58 {
59  fprintf(stderr, "%s\n", str);
60  exit(1);
61 }
62 
63 static void print_audio_bytes(const TestStruct *test_sample, void **data_planes, int nb_samples)
64 {
65  int p, b, f;
66  int byte_offset = av_get_bytes_per_sample(test_sample->format);
67  int buffers = av_sample_fmt_is_planar(test_sample->format)
68  ? test_sample->nb_ch : 1;
69  int line_size = (buffers > 1) ? nb_samples * byte_offset
70  : nb_samples * byte_offset * test_sample->nb_ch;
71  for (p = 0; p < buffers; ++p){
72  for(b = 0; b < line_size; b+=byte_offset){
73  for (f = 0; f < byte_offset; f++){
74  int order = !HAVE_BIGENDIAN ? (byte_offset - f - 1) : f;
75  printf("%02x", *((uint8_t*)data_planes[p] + b + order));
76  }
77  putchar(' ');
78  }
79  putchar('\n');
80  }
81 }
82 
83 static int read_samples_from_audio_fifo(AVAudioFifo* afifo, void ***output, int nb_samples)
84 {
85  int i;
86  int samples = FFMIN(nb_samples, afifo->nb_samples);
87  int tot_elements = !av_sample_fmt_is_planar(afifo->sample_fmt)
88  ? samples : afifo->channels * samples;
89  void **data_planes = av_malloc_array(afifo->nb_buffers, sizeof(void*));
90  if (!data_planes)
91  ERROR("failed to allocate memory!");
92  if (*output)
93  free_data_planes(afifo, *output);
94  *output = data_planes;
95 
96  for (i = 0; i < afifo->nb_buffers; ++i){
97  data_planes[i] = av_malloc_array(tot_elements, afifo->sample_size);
98  if (!data_planes[i])
99  ERROR("failed to allocate memory!");
100  }
101 
102  return av_audio_fifo_read(afifo, *output, nb_samples);
103 }
104 
105 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  if(nb_samples > test_sample->nb_samples_pch - offset){
112  return 0;
113  }
114  if(offset >= test_sample->nb_samples_pch){
115  return 0;
116  }
117  offset_size = offset * afifo->sample_size;
118 
119  for (i = 0; i < afifo->nb_buffers ; ++i){
120  data_planes[i] = (uint8_t*)test_sample->data_planes[i] + offset_size;
121  }
122 
123  return av_audio_fifo_write(afifo, data_planes, nb_samples);
124 }
125 
126 static void test_function(const TestStruct *test_sample)
127 {
128  int ret, i;
129  void **output_data = NULL;
130  AVAudioFifo *afifo = av_audio_fifo_alloc(test_sample->format, test_sample->nb_ch,
131  test_sample->nb_samples_pch);
132  if (!afifo) {
133  ERROR("ERROR: av_audio_fifo_alloc returned NULL!");
134  }
135  ret = write_samples_to_audio_fifo(afifo, test_sample, test_sample->nb_samples_pch, 0);
136  if (ret < 0){
137  ERROR("ERROR: av_audio_fifo_write failed!");
138  }
139  printf("written: %d\n", ret);
140 
141  ret = write_samples_to_audio_fifo(afifo, test_sample, test_sample->nb_samples_pch, 0);
142  if (ret < 0){
143  ERROR("ERROR: av_audio_fifo_write failed!");
144  }
145  printf("written: %d\n", ret);
146  printf("remaining samples in audio_fifo: %d\n\n", av_audio_fifo_size(afifo));
147 
149  if (ret < 0){
150  ERROR("ERROR: av_audio_fifo_read failed!");
151  }
152  printf("read: %d\n", ret);
153  print_audio_bytes(test_sample, output_data, ret);
154  printf("remaining samples in audio_fifo: %d\n\n", av_audio_fifo_size(afifo));
155 
156  /* test av_audio_fifo_peek */
157  ret = av_audio_fifo_peek(afifo, output_data, afifo->nb_samples);
158  if (ret < 0){
159  ERROR("ERROR: av_audio_fifo_peek failed!");
160  }
161  printf("peek:\n");
162  print_audio_bytes(test_sample, output_data, ret);
163  printf("\n");
164 
165  /* test av_audio_fifo_peek_at */
166  printf("peek_at:\n");
167  for (i = 0; i < afifo->nb_samples; ++i){
168  ret = av_audio_fifo_peek_at(afifo, output_data, 1, i);
169  if (ret < 0){
170  ERROR("ERROR: av_audio_fifo_peek_at failed!");
171  }
172  printf("%d:\n", i);
173  print_audio_bytes(test_sample, output_data, ret);
174  }
175  printf("\n");
176 
177  /* test av_audio_fifo_drain */
178  ret = av_audio_fifo_drain(afifo, afifo->nb_samples);
179  if (ret < 0){
180  ERROR("ERROR: av_audio_fifo_drain failed!");
181  }
182  if (afifo->nb_samples){
183  ERROR("drain failed to flush all samples in audio_fifo!");
184  }
185 
186  /* deallocate */
188  av_audio_fifo_free(afifo);
189 }
190 
191 int main(void)
192 {
193  int t, tests = sizeof(test_struct)/sizeof(test_struct[0]);
194 
195  for (t = 0; t < tests; ++t){
196  printf("\nTEST: %d\n\n", t+1);
198  }
199  return 0;
200 }
av_audio_fifo_free
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
Definition: audio_fifo.c:45
main
int main(void)
Definition: audio_fifo.c:191
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:69
output
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
Definition: filter_design.txt:225
b
#define b
Definition: input.c:41
TestStruct::nb_samples_pch
const int nb_samples_pch
Definition: audio_fifo.c:32
output_data
static int output_data(MLPDecodeContext *m, unsigned int substr, AVFrame *frame, int *got_frame_ptr)
Write the audio data into the output buffer.
Definition: mlpdec.c:1062
samples
FFmpeg Automated Testing Environment ************************************Introduction Using FATE from your FFmpeg source directory Submitting the results to the FFmpeg result aggregation server Uploading new samples to the fate suite FATE makefile targets and variables Makefile targets Makefile variables Examples Introduction **************FATE is an extended regression suite on the client side and a means for results aggregation and presentation on the server side The first part of this document explains how you can use FATE from your FFmpeg source directory to test your ffmpeg binary The second part describes how you can run FATE to submit the results to FFmpeg’s FATE server In any way you can have a look at the publicly viewable FATE results by visiting this as it can be seen if some test on some platform broke with their recent contribution This usually happens on the platforms the developers could not test on The second part of this document describes how you can run FATE to submit your results to FFmpeg’s FATE server If you want to submit your results be sure to check that your combination of OS and compiler is not already listed on the above mentioned website In the third part you can find a comprehensive listing of FATE makefile targets and variables Using FATE from your FFmpeg source directory **********************************************If you want to run FATE on your machine you need to have the samples in place You can get the samples via the build target fate rsync Use this command from the top level source this will cause FATE to fail NOTE To use a custom wrapper to run the pass ‘ target exec’ to ‘configure’ or set the TARGET_EXEC Make variable Submitting the results to the FFmpeg result aggregation server ****************************************************************To submit your results to the server you should run fate through the shell script ‘tests fate sh’ from the FFmpeg sources This script needs to be invoked with a configuration file as its first argument tests fate sh path to fate_config A configuration file template with comments describing the individual configuration variables can be found at ‘doc fate_config sh template’ Create a configuration that suits your based on the configuration template The ‘slot’ configuration variable can be any string that is not yet but it is suggested that you name it adhering to the following pattern ‘ARCH OS COMPILER COMPILER VERSION’ The configuration file itself will be sourced in a shell therefore all shell features may be used This enables you to setup the environment as you need it for your build For your first test runs the ‘fate_recv’ variable should be empty or commented out This will run everything as normal except that it will omit the submission of the results to the server The following files should be present in $workdir as specified in the configuration it may help to try out the ‘ssh’ command with one or more ‘ v’ options You should get detailed output concerning your SSH configuration and the authentication process The only thing left is to automate the execution of the fate sh script and the synchronisation of the samples directory Uploading new samples to the fate suite *****************************************If you need a sample uploaded send a mail to samples request This is for developers who have an account on the fate suite server If you upload new samples
Definition: fate.txt:139
TestStruct
Definition: audio_fifo.c:28
AVAudioFifo::nb_samples
int nb_samples
number of samples currently in the FIFO
Definition: audio_fifo.c:37
AVAudioFifo
Context for an Audio FIFO Buffer.
Definition: audio_fifo.c:34
av_audio_fifo_drain
int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples)
Drain data from an AVAudioFifo.
Definition: audio_fifo.c:201
TestStruct::data_planes
const void * data_planes[MAX_CHANNELS]
Definition: audio_fifo.c:31
AVAudioFifo::channels
int channels
number of channels
Definition: audio_fifo.c:40
AVAudioFifo::sample_size
int sample_size
size, in bytes, of one sample in a buffer
Definition: audio_fifo.c:42
av_audio_fifo_write
int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
Write data to an AVAudioFifo.
Definition: audio_fifo.c:112
av_sample_fmt_is_planar
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
Definition: samplefmt.c:112
f
#define f(width, name)
Definition: cbs_vp9.c:255
NULL
#define NULL
Definition: coverity.c:32
ERROR
static void ERROR(const char *str)
Definition: audio_fifo.c:57
av_audio_fifo_alloc
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
Definition: audio_fifo.c:59
MAX_CHANNELS
#define MAX_CHANNELS
Definition: audio_fifo.c:25
test_function
static void test_function(const TestStruct *test_sample)
Definition: audio_fifo.c:126
AV_SAMPLE_FMT_U8
AV_SAMPLE_FMT_U8
Definition: audio_convert.c:194
write_samples_to_audio_fifo
static int write_samples_to_audio_fifo(AVAudioFifo *afifo, const TestStruct *test_sample, int nb_samples, int offset)
Definition: audio_fifo.c:105
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469
TestStruct::format
enum AVSampleFormat format
Definition: audio_fifo.c:29
TestStruct::nb_ch
const int nb_ch
Definition: audio_fifo.c:30
av_audio_fifo_peek_at
int av_audio_fifo_peek_at(AVAudioFifo *af, void **data, int nb_samples, int offset)
Peek data from an AVAudioFifo.
Definition: audio_fifo.c:157
AV_SAMPLE_FMT_U8P
@ AV_SAMPLE_FMT_U8P
unsigned 8 bits, planar
Definition: samplefmt.h:66
tests
FFmpeg Automated Testing Environment ************************************Introduction Using FATE from your FFmpeg source directory Submitting the results to the FFmpeg result aggregation server Uploading new samples to the fate suite FATE makefile targets and variables Makefile targets Makefile variables Examples Introduction **************FATE is an extended regression suite on the client side and a means for results aggregation and presentation on the server side The first part of this document explains how you can use FATE from your FFmpeg source directory to test your ffmpeg binary The second part describes how you can run FATE to submit the results to FFmpeg’s FATE server In any way you can have a look at the publicly viewable FATE results by visiting this as it can be seen if some test on some platform broke with their recent contribution This usually happens on the platforms the developers could not test on The second part of this document describes how you can run FATE to submit your results to FFmpeg’s FATE server If you want to submit your results be sure to check that your combination of OS and compiler is not already listed on the above mentioned website In the third part you can find a comprehensive listing of FATE makefile targets and variables Using FATE from your FFmpeg source directory **********************************************If you want to run FATE on your machine you need to have the samples in place You can get the samples via the build target fate rsync Use this command from the top level source this will cause FATE to fail NOTE To use a custom wrapper to run the pass ‘ target exec’ to ‘configure’ or set the TARGET_EXEC Make variable Submitting the results to the FFmpeg result aggregation server ****************************************************************To submit your results to the server you should run fate through the shell script ‘tests fate sh’ from the FFmpeg sources This script needs to be invoked with a configuration file as its first argument tests fate sh path to fate_config A configuration file template with comments describing the individual configuration variables can be found at ‘doc fate_config sh template’ Create a configuration that suits your based on the configuration template The ‘slot’ configuration variable can be any string that is not yet but it is suggested that you name it adhering to the following pattern ‘ARCH OS COMPILER COMPILER VERSION’ The configuration file itself will be sourced in a shell therefore all shell features may be used This enables you to setup the environment as you need it for your build For your first test runs the ‘fate_recv’ variable should be empty or commented out This will run everything as normal except that it will omit the submission of the results to the server The following files should be present in $workdir as specified in the configuration it may help to try out the ‘ssh’ command with one or more ‘ v’ options You should get detailed output concerning your SSH configuration and the authentication process The only thing left is to automate the execution of the fate sh script and the synchronisation of the samples directory Uploading new samples to the fate suite *****************************************If you need a sample uploaded send a mail to samples request This is for developers who have an account on the fate suite server If you upload new please make sure they are as small as space on each network bandwidth and so on benefit from smaller test cases Also keep in mind older checkouts use existing sample that means in practice generally do not remove or overwrite files as it likely would break older checkouts or releases Also all needed samples for a commit should be ideally before the push If you need an account for frequently uploading samples or you wish to help others by doing that send a mail to ffmpeg devel rsync vauL Duo ug o o X fate suite ffmpeg Duo ug o o X fate suite fate suite ffmpeg Duo ug o o X fate suite fate suite ffmpeg can be set or it has a meaning only while running the regression tests ‘THREADS’ Specify how many threads to use while running regression tests
Definition: fate.txt:188
HAVE_BIGENDIAN
#define HAVE_BIGENDIAN
Definition: config.h:199
printf
printf("static const uint8_t my_array[100] = {\n")
FFMIN
#define FFMIN(a, b)
Definition: common.h:96
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
av_audio_fifo_size
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
Definition: audio_fifo.c:228
audio_fifo.c
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:67
data_U8
static const uint8_t data_U8[]
Definition: audio_fifo.c:35
av_audio_fifo_read
int av_audio_fifo_read(AVAudioFifo *af, void **data, int nb_samples)
Read data from an AVAudioFifo.
Definition: audio_fifo.c:181
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
read_samples_from_audio_fifo
static int read_samples_from_audio_fifo(AVAudioFifo *afifo, void ***output, int nb_samples)
Definition: audio_fifo.c:83
av_get_bytes_per_sample
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:106
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:32
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
uint8_t
uint8_t
Definition: audio_convert.c:194
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:61
data_FLT
static const float data_FLT[]
Definition: audio_fifo.c:37
ret
ret
Definition: filter_design.txt:187
data_S16
static const int16_t data_S16[]
Definition: audio_fifo.c:36
AVAudioFifo::sample_fmt
enum AVSampleFormat sample_fmt
sample format
Definition: audio_fifo.c:41
test_struct
static const TestStruct test_struct[]
Definition: audio_fifo.c:39
mem.h
AVAudioFifo::nb_buffers
int nb_buffers
number of buffers
Definition: audio_fifo.c:36
free_data_planes
static void free_data_planes(AVAudioFifo *afifo, void **output_data)
Definition: audio_fifo.c:48
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
convert_header.str
string str
Definition: convert_header.py:20
print_audio_bytes
static void print_audio_bytes(const TestStruct *test_sample, void **data_planes, int nb_samples)
Definition: audio_fifo.c:63
av_audio_fifo_peek
int av_audio_fifo_peek(AVAudioFifo *af, void **data, int nb_samples)
Peek data from an AVAudioFifo.
Definition: audio_fifo.c:138
AV_SAMPLE_FMT_FLT
@ AV_SAMPLE_FMT_FLT
float
Definition: samplefmt.h:63