FFmpeg
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 #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 static void free_data_planes(AVAudioFifo *afifo, void **output_data)
50 {
51  int i;
52  for (i = 0; i < afifo->nb_buffers; ++i){
54  }
56 }
57 
58 static void ERROR(const char *str)
59 {
60  fprintf(stderr, "%s\n", str);
61  exit(1);
62 }
63 
64 static void print_audio_bytes(const TestStruct *test_sample, void **data_planes, int nb_samples)
65 {
66  int p, b, f;
67  int byte_offset = av_get_bytes_per_sample(test_sample->format);
68  int buffers = av_sample_fmt_is_planar(test_sample->format)
69  ? test_sample->nb_ch : 1;
70  int line_size = (buffers > 1) ? nb_samples * byte_offset
71  : nb_samples * byte_offset * test_sample->nb_ch;
72  for (p = 0; p < buffers; ++p){
73  for(b = 0; b < line_size; b+=byte_offset){
74  for (f = 0; f < byte_offset; f++){
75  int order = !HAVE_BIGENDIAN ? (byte_offset - f - 1) : f;
76  printf("%02x", *((uint8_t*)data_planes[p] + b + order));
77  }
78  if (b + byte_offset < line_size)
79  putchar(' ');
80  }
81  putchar('\n');
82  }
83 }
84 
85 static int read_samples_from_audio_fifo(AVAudioFifo* afifo, void ***output, int nb_samples)
86 {
87  int i;
88  int samples = FFMIN(nb_samples, afifo->nb_samples);
89  int tot_elements = !av_sample_fmt_is_planar(afifo->sample_fmt)
90  ? samples : afifo->channels * samples;
91  void **data_planes = av_malloc_array(afifo->nb_buffers, sizeof(void*));
92  if (!data_planes)
93  ERROR("failed to allocate memory!");
94  if (*output)
95  free_data_planes(afifo, *output);
96  *output = data_planes;
97 
98  for (i = 0; i < afifo->nb_buffers; ++i){
99  data_planes[i] = av_malloc_array(tot_elements, afifo->sample_size);
100  if (!data_planes[i])
101  ERROR("failed to allocate memory!");
102  }
103 
104  return av_audio_fifo_read(afifo, *output, nb_samples);
105 }
106 
107 static int write_samples_to_audio_fifo(AVAudioFifo* afifo, const TestStruct *test_sample,
108  int nb_samples, int offset)
109 {
110  int offset_size, i;
111  void *data_planes[MAX_CHANNELS];
112 
113  if(nb_samples > test_sample->nb_samples_pch - offset){
114  return 0;
115  }
116  if(offset >= test_sample->nb_samples_pch){
117  return 0;
118  }
119  offset_size = offset * afifo->sample_size;
120 
121  for (i = 0; i < afifo->nb_buffers ; ++i){
122  data_planes[i] = (uint8_t*)test_sample->data_planes[i] + offset_size;
123  }
124 
125  return av_audio_fifo_write(afifo, data_planes, nb_samples);
126 }
127 
128 static void test_function(const TestStruct *test_sample)
129 {
130  int ret, i;
131  void **output_data = NULL;
132  AVAudioFifo *afifo = av_audio_fifo_alloc(test_sample->format, test_sample->nb_ch,
133  test_sample->nb_samples_pch);
134  if (!afifo) {
135  ERROR("ERROR: av_audio_fifo_alloc returned NULL!");
136  }
137  ret = write_samples_to_audio_fifo(afifo, test_sample, test_sample->nb_samples_pch, 0);
138  if (ret < 0){
139  ERROR("ERROR: av_audio_fifo_write failed!");
140  }
141  printf("written: %d\n", ret);
142 
143  ret = write_samples_to_audio_fifo(afifo, test_sample, test_sample->nb_samples_pch, 0);
144  if (ret < 0){
145  ERROR("ERROR: av_audio_fifo_write failed!");
146  }
147  printf("written: %d\n", ret);
148  printf("remaining samples in audio_fifo: %d\n\n", av_audio_fifo_size(afifo));
149 
151  if (ret < 0){
152  ERROR("ERROR: av_audio_fifo_read failed!");
153  }
154  printf("read: %d\n", ret);
155  print_audio_bytes(test_sample, output_data, ret);
156  printf("remaining samples in audio_fifo: %d\n\n", av_audio_fifo_size(afifo));
157 
158  /* test av_audio_fifo_peek */
159  ret = av_audio_fifo_peek(afifo, output_data, afifo->nb_samples);
160  if (ret < 0){
161  ERROR("ERROR: av_audio_fifo_peek failed!");
162  }
163  printf("peek:\n");
164  print_audio_bytes(test_sample, output_data, ret);
165  printf("\n");
166 
167  /* test av_audio_fifo_peek_at */
168  printf("peek_at:\n");
169  for (i = 0; i < afifo->nb_samples; ++i){
170  ret = av_audio_fifo_peek_at(afifo, output_data, 1, i);
171  if (ret < 0){
172  ERROR("ERROR: av_audio_fifo_peek_at failed!");
173  }
174  printf("%d:\n", i);
175  print_audio_bytes(test_sample, output_data, ret);
176  }
177  printf("\n");
178 
179  /* test av_audio_fifo_drain */
180  ret = av_audio_fifo_drain(afifo, afifo->nb_samples);
181  if (ret < 0){
182  ERROR("ERROR: av_audio_fifo_drain failed!");
183  }
184  if (afifo->nb_samples){
185  ERROR("drain failed to flush all samples in audio_fifo!");
186  }
187 
188  /* deallocate */
190  av_audio_fifo_free(afifo);
191 }
192 
193 int main(void)
194 {
195  int t, tests = sizeof(test_struct)/sizeof(test_struct[0]);
196 
197  for (t = 0; t < tests; ++t){
198  printf("\nTEST: %d\n\n", t+1);
200  }
201  return 0;
202 }
av_audio_fifo_free
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
Definition: audio_fifo.c:48
main
int main(void)
Definition: audio_fifo.c:193
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:66
printf
__device__ int printf(const char *,...)
av_audio_fifo_write
int av_audio_fifo_write(AVAudioFifo *af, void *const *data, int nb_samples)
Write data to an AVAudioFifo.
Definition: audio_fifo.c:119
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:226
b
#define b
Definition: input.c:42
TestStruct::nb_samples_pch
const int nb_samples_pch
Definition: audio_fifo.c:33
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:1107
TestStruct
Definition: audio_fifo.c:29
AVAudioFifo::nb_samples
int nb_samples
number of samples currently in the FIFO
Definition: audio_fifo.c:40
AVAudioFifo
Context for an Audio FIFO Buffer.
Definition: audio_fifo.c:37
av_audio_fifo_drain
int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples)
Drain data from an AVAudioFifo.
Definition: audio_fifo.c:195
TestStruct::data_planes
const void * data_planes[MAX_CHANNELS]
Definition: audio_fifo.c:32
AVAudioFifo::channels
int channels
number of channels
Definition: audio_fifo.c:43
AVAudioFifo::sample_size
int sample_size
size, in bytes, of one sample in a buffer
Definition: audio_fifo.c:45
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:114
NULL
#define NULL
Definition: coverity.c:32
ERROR
static void ERROR(const char *str)
Definition: audio_fifo.c:58
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:62
MAX_CHANNELS
#define MAX_CHANNELS
Definition: audio_fifo.c:26
test_function
static void test_function(const TestStruct *test_sample)
Definition: audio_fifo.c:128
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:107
TestStruct::format
enum AVSampleFormat format
Definition: audio_fifo.c:30
TestStruct::nb_ch
const int nb_ch
Definition: audio_fifo.c:31
f
f
Definition: af_crystalizer.c:122
AV_SAMPLE_FMT_U8P
@ AV_SAMPLE_FMT_U8P
unsigned 8 bits, planar
Definition: samplefmt.h:63
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:424
av_audio_fifo_read
int av_audio_fifo_read(AVAudioFifo *af, void *const *data, int nb_samples)
Read data from an AVAudioFifo.
Definition: audio_fifo.c:175
av_audio_fifo_peek
int av_audio_fifo_peek(const AVAudioFifo *af, void *const *data, int nb_samples)
Peek data from an AVAudioFifo.
Definition: audio_fifo.c:145
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:222
audio_fifo.c
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:64
data_U8
static const uint8_t data_U8[]
Definition: audio_fifo.c:36
tests
const TestCase tests[]
Definition: fifo_muxer.c:363
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
read_samples_from_audio_fifo
static int read_samples_from_audio_fifo(AVAudioFifo *afifo, void ***output, int nb_samples)
Definition: audio_fifo.c:85
av_get_bytes_per_sample
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:108
AV_SAMPLE_FMT_U8
@ AV_SAMPLE_FMT_U8
unsigned 8 bits
Definition: samplefmt.h:57
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:32
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
data_FLT
static const float data_FLT[]
Definition: audio_fifo.c:38
ret
ret
Definition: filter_design.txt:187
data_S16
static const int16_t data_S16[]
Definition: audio_fifo.c:37
AVAudioFifo::sample_fmt
enum AVSampleFormat sample_fmt
sample format
Definition: audio_fifo.c:44
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
Windows::Graphics::DirectX::Direct3D11::p
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
Definition: vsrc_gfxcapture_winrt.hpp:53
test_struct
static const TestStruct test_struct[]
Definition: audio_fifo.c:40
mem.h
AVAudioFifo::nb_buffers
int nb_buffers
number of buffers
Definition: audio_fifo.c:39
free_data_planes
static void free_data_planes(AVAudioFifo *afifo, void **output_data)
Definition: audio_fifo.c:49
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
print_audio_bytes
static void print_audio_bytes(const TestStruct *test_sample, void **data_planes, int nb_samples)
Definition: audio_fifo.c:64
av_audio_fifo_peek_at
int av_audio_fifo_peek_at(const AVAudioFifo *af, void *const *data, int nb_samples, int offset)
Peek data from an AVAudioFifo.
Definition: audio_fifo.c:150
AV_SAMPLE_FMT_FLT
@ AV_SAMPLE_FMT_FLT
float
Definition: samplefmt.h:60