FFmpeg
api-flac-test.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Ludmila Glinskih
3  * Copyright (c) 2001 Fabrice Bellard
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21  * THE SOFTWARE.
22  */
23 
24 /*
25  * FLAC codec test.
26  * Encodes raw data to FLAC format and decodes it back to raw. Compares raw-data
27  * after that.
28  */
29 
30 #include "libavcodec/avcodec.h"
32 #include "libavutil/common.h"
33 #include "libavutil/samplefmt.h"
34 
35 #define NUMBER_OF_AUDIO_FRAMES 200
36 #define NAME_BUFF_SIZE 100
37 
38 /* generate i-th frame of test audio */
39 static int generate_raw_frame(uint16_t *frame_data, int i, int sample_rate,
40  int channels, int frame_size)
41 {
42  int j, k;
43 
44  for (j = 0; j < frame_size; j++) {
45  frame_data[channels * j] = 10000 * ((j / 10 * i) % 2);
46  for (k = 1; k < channels; k++)
47  frame_data[channels * j + k] = frame_data[channels * j] * (k + 1);
48  }
49  return 0;
50 }
51 
52 static int init_encoder(const AVCodec *enc, AVCodecContext **enc_ctx,
53  const AVChannelLayout *ch_layout, int sample_rate)
54 {
56  int result;
57  char name_buff[NAME_BUFF_SIZE];
58 
59  av_channel_layout_describe(ch_layout, name_buff, NAME_BUFF_SIZE);
60  av_log(NULL, AV_LOG_INFO, "channel layout: %s, sample rate: %i\n", name_buff, sample_rate);
61 
63  if (!ctx) {
64  av_log(NULL, AV_LOG_ERROR, "Can't allocate encoder context\n");
65  return AVERROR(ENOMEM);
66  }
67 
68  ctx->sample_fmt = AV_SAMPLE_FMT_S16;
69  ctx->sample_rate = sample_rate;
70  av_channel_layout_copy(&ctx->ch_layout, ch_layout);
71 
72  result = avcodec_open2(ctx, enc, NULL);
73  if (result < 0) {
74  av_log(ctx, AV_LOG_ERROR, "Can't open encoder\n");
75  return result;
76  }
77 
78  *enc_ctx = ctx;
79  return 0;
80 }
81 
82 static int init_decoder(const AVCodec *dec, AVCodecContext **dec_ctx,
83  const AVChannelLayout *ch_layout)
84 {
86  int result;
87 
89  if (!ctx) {
90  av_log(NULL, AV_LOG_ERROR , "Can't allocate decoder context\n");
91  return AVERROR(ENOMEM);
92  }
93 
94  ctx->request_sample_fmt = AV_SAMPLE_FMT_S16;
95  av_channel_layout_copy(&ctx->ch_layout, ch_layout);
96 
97  result = avcodec_open2(ctx, dec, NULL);
98  if (result < 0) {
99  av_log(ctx, AV_LOG_ERROR, "Can't open decoder\n");
100  return result;
101  }
102 
103  *dec_ctx = ctx;
104  return 0;
105 }
106 
107 static int run_test(const AVCodec *enc, const AVCodec *dec,
109 {
110  AVPacket *enc_pkt;
111  AVFrame *in_frame, *out_frame;
112  uint8_t *raw_in = NULL, *raw_out = NULL;
113  int in_offset = 0, out_offset = 0;
114  int result = 0;
115  int i = 0;
116  int in_frame_bytes, out_frame_bytes;
117 
118  enc_pkt = av_packet_alloc();
119  if (!enc_pkt) {
120  av_log(NULL, AV_LOG_ERROR, "Can't allocate output packet\n");
121  return AVERROR(ENOMEM);
122  }
123 
124  in_frame = av_frame_alloc();
125  if (!in_frame) {
126  av_log(NULL, AV_LOG_ERROR, "Can't allocate input frame\n");
127  return AVERROR(ENOMEM);
128  }
129 
130  in_frame->nb_samples = enc_ctx->frame_size;
131  in_frame->format = enc_ctx->sample_fmt;
132  result = av_channel_layout_copy(&in_frame->ch_layout, &enc_ctx->ch_layout);
133  if (result < 0)
134  return result;
135  if (av_frame_get_buffer(in_frame, 0) != 0) {
136  av_log(NULL, AV_LOG_ERROR, "Can't allocate a buffer for input frame\n");
137  return AVERROR(ENOMEM);
138  }
139 
140  out_frame = av_frame_alloc();
141  if (!out_frame) {
142  av_log(NULL, AV_LOG_ERROR, "Can't allocate output frame\n");
143  return AVERROR(ENOMEM);
144  }
145 
146  raw_in = av_malloc(in_frame->linesize[0] * NUMBER_OF_AUDIO_FRAMES);
147  if (!raw_in) {
148  av_log(NULL, AV_LOG_ERROR, "Can't allocate memory for raw_in\n");
149  return AVERROR(ENOMEM);
150  }
151 
152  raw_out = av_malloc(in_frame->linesize[0] * NUMBER_OF_AUDIO_FRAMES);
153  if (!raw_out) {
154  av_log(NULL, AV_LOG_ERROR, "Can't allocate memory for raw_out\n");
155  return AVERROR(ENOMEM);
156  }
157 
158  for (i = 0; i < NUMBER_OF_AUDIO_FRAMES; i++) {
159  result = av_frame_make_writable(in_frame);
160  if (result < 0)
161  return result;
162 
163  generate_raw_frame((uint16_t*)(in_frame->data[0]), i, enc_ctx->sample_rate,
164  enc_ctx->ch_layout.nb_channels, enc_ctx->frame_size);
165  in_frame_bytes = in_frame->nb_samples * in_frame->ch_layout.nb_channels * sizeof(uint16_t);
166  if (in_frame_bytes > in_frame->linesize[0]) {
167  av_log(NULL, AV_LOG_ERROR, "Incorrect value of input frame linesize\n");
168  return 1;
169  }
170  memcpy(raw_in + in_offset, in_frame->data[0], in_frame_bytes);
171  in_offset += in_frame_bytes;
172  result = avcodec_send_frame(enc_ctx, in_frame);
173  if (result < 0) {
174  av_log(NULL, AV_LOG_ERROR, "Error submitting a frame for encoding\n");
175  return result;
176  }
177 
178  while (result >= 0) {
179  result = avcodec_receive_packet(enc_ctx, enc_pkt);
180  if (result == AVERROR(EAGAIN))
181  break;
182  else if (result < 0 && result != AVERROR_EOF) {
183  av_log(NULL, AV_LOG_ERROR, "Error encoding audio frame\n");
184  return result;
185  }
186 
187  /* if we get an encoded packet, feed it straight to the decoder */
188  result = avcodec_send_packet(dec_ctx, enc_pkt);
189  av_packet_unref(enc_pkt);
190  if (result < 0) {
191  av_log(NULL, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
192  return result;
193  }
194 
195  result = avcodec_receive_frame(dec_ctx, out_frame);
196  if (result == AVERROR(EAGAIN)) {
197  result = 0;
198  continue;
199  } else if (result == AVERROR(EOF)) {
200  result = 0;
201  break;
202  } else if (result < 0) {
203  av_log(NULL, AV_LOG_ERROR, "Error decoding audio packet\n");
204  return result;
205  }
206 
207  if (in_frame->nb_samples != out_frame->nb_samples) {
208  av_log(NULL, AV_LOG_ERROR, "Error frames before and after decoding has different number of samples\n");
209  return AVERROR_UNKNOWN;
210  }
211 
212  if (av_channel_layout_compare(&in_frame->ch_layout, &out_frame->ch_layout)) {
213  av_log(NULL, AV_LOG_ERROR, "Error frames before and after decoding has different channel layout\n");
214  return AVERROR_UNKNOWN;
215  }
216 
217  if (in_frame->format != out_frame->format) {
218  av_log(NULL, AV_LOG_ERROR, "Error frames before and after decoding has different sample format\n");
219  return AVERROR_UNKNOWN;
220  }
221  out_frame_bytes = out_frame->nb_samples * out_frame->ch_layout.nb_channels * sizeof(uint16_t);
222  if (out_frame_bytes > out_frame->linesize[0]) {
223  av_log(NULL, AV_LOG_ERROR, "Incorrect value of output frame linesize\n");
224  return 1;
225  }
226  memcpy(raw_out + out_offset, out_frame->data[0], out_frame_bytes);
227  out_offset += out_frame_bytes;
228  }
229  }
230 
231  if (memcmp(raw_in, raw_out, out_frame_bytes * NUMBER_OF_AUDIO_FRAMES) != 0) {
232  av_log(NULL, AV_LOG_ERROR, "Output differs\n");
233  return 1;
234  }
235 
236  av_log(NULL, AV_LOG_INFO, "OK\n");
237 
238  av_freep(&raw_in);
239  av_freep(&raw_out);
240  av_packet_free(&enc_pkt);
241  av_frame_free(&in_frame);
242  av_frame_free(&out_frame);
243  return 0;
244 }
245 
246 int main(void)
247 {
248  const AVCodec *enc = NULL, *dec = NULL;
249  AVCodecContext *enc_ctx = NULL, *dec_ctx = NULL;
254  int sample_rates[] = {8000, 44100, 48000, 192000};
255  int cl, sr;
256 
258  if (!enc) {
259  av_log(NULL, AV_LOG_ERROR, "Can't find encoder\n");
260  return 1;
261  }
262 
264  if (!dec) {
265  av_log(NULL, AV_LOG_ERROR, "Can't find decoder\n");
266  return 1;
267  }
268 
269  for (cl = 0; cl < FF_ARRAY_ELEMS(channel_layouts); cl++) {
270  for (sr = 0; sr < FF_ARRAY_ELEMS(sample_rates); sr++) {
271  if (init_encoder(enc, &enc_ctx, &channel_layouts[cl], sample_rates[sr]) != 0)
272  return 1;
273  if (init_decoder(dec, &dec_ctx, &channel_layouts[cl]) != 0)
274  return 1;
275  if (run_test(enc, dec, enc_ctx, dec_ctx) != 0)
276  return 1;
277  avcodec_free_context(&enc_ctx);
279  }
280  }
281 
282  return 0;
283 }
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1092
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:423
AVCodec
AVCodec.
Definition: codec.h:187
AV_CHANNEL_LAYOUT_STEREO_DOWNMIX
#define AV_CHANNEL_LAYOUT_STEREO_DOWNMIX
Definition: channel_layout.h:415
dec_ctx
static AVCodecContext * dec_ctx
Definition: decode_filter_audio.c:46
avcodec_receive_packet
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
Read encoded data from the encoder.
Definition: encode.c:556
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
av_frame_get_buffer
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition: frame.c:243
AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_STEREO
Definition: channel_layout.h:383
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1064
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:100
avcodec_find_encoder
const AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
Definition: allcodecs.c:970
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
av_frame_make_writable
int av_frame_make_writable(AVFrame *frame)
Ensure that the frame data is writable, avoiding data copy if possible.
Definition: frame.c:683
AV_CODEC_ID_FLAC
@ AV_CODEC_ID_FLAC
Definition: codec_id.h:454
AVERROR_UNKNOWN
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:73
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:317
sample_rate
sample_rate
Definition: ffmpeg_filter.c:368
av_packet_free
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:74
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:361
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2107
samplefmt.h
AVFrame::ch_layout
AVChannelLayout ch_layout
Channel layout of the audio data.
Definition: frame.h:802
AV_CHANNEL_LAYOUT_SURROUND
#define AV_CHANNEL_LAYOUT_SURROUND
Definition: channel_layout.h:386
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:88
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
init_decoder
static int init_decoder(const AVCodec *dec, AVCodecContext **dec_ctx, const AVChannelLayout *ch_layout)
Definition: api-flac-test.c:82
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_channel_layout_describe
int av_channel_layout_describe(const AVChannelLayout *channel_layout, char *buf, size_t buf_size)
Get a human-readable string describing the channel layout properties.
Definition: channel_layout.c:786
avcodec_alloc_context3
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:153
frame_size
int frame_size
Definition: mxfenc.c:2311
avcodec_receive_frame
int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Return decoded output data from a decoder or encoder (when the AV_CODEC_FLAG_RECON_FRAME flag is used...
Definition: avcodec.c:713
ctx
AVFormatContext * ctx
Definition: movenc.c:48
channels
channels
Definition: aptx.h:31
init_encoder
static int init_encoder(const AVCodec *enc, AVCodecContext **enc_ctx, const AVChannelLayout *ch_layout, int sample_rate)
Definition: api-flac-test.c:52
result
and forward the result(frame or status change) to the corresponding input. If nothing is possible
NULL
#define NULL
Definition: coverity.c:32
avcodec_free_context
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer.
Definition: options.c:168
avcodec_open2
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: avcodec.c:128
avcodec_find_decoder
const AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition: allcodecs.c:975
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:307
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1080
NAME_BUFF_SIZE
#define NAME_BUFF_SIZE
Definition: api-flac-test.c:36
run_test
static int run_test(const AVCodec *enc, const AVCodec *dec, AVCodecContext *enc_ctx, AVCodecContext *dec_ctx)
Definition: api-flac-test.c:107
AVFrame::format
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:427
frame_data
FrameData * frame_data(AVFrame *frame)
Get our axiliary frame data attached to the frame, allocating it if needed.
Definition: ffmpeg.c:429
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:63
av_channel_layout_compare
int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout *chl1)
Check whether two channel layouts are semantically the same, i.e.
Definition: channel_layout.c:942
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
sample_rates
sample_rates
Definition: ffmpeg_filter.c:368
NUMBER_OF_AUDIO_FRAMES
#define NUMBER_OF_AUDIO_FRAMES
Definition: api-flac-test.c:35
avcodec_send_packet
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
Definition: decode.c:709
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:420
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
common.h
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
avcodec_send_frame
int avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
Supply a raw video or audio frame to the encoder.
Definition: encode.c:518
avcodec.h
main
int main(void)
Definition: api-flac-test.c:246
AVCodecContext
main external API structure.
Definition: avcodec.h:441
channel_layout.h
av_channel_layout_copy
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
Definition: channel_layout.c:647
AVPacket
This structure stores compressed data.
Definition: packet.h:468
channel_layouts
static const uint16_t channel_layouts[7]
Definition: dca_lbr.c:111
generate_raw_frame
static int generate_raw_frame(uint16_t *frame_data, int i, int sample_rate, int channels, int frame_size)
Definition: api-flac-test.c:39
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
AV_CHANNEL_LAYOUT_5POINT1_BACK
#define AV_CHANNEL_LAYOUT_5POINT1_BACK
Definition: channel_layout.h:395
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:385
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27