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  int64_t ch_layout, int sample_rate)
54 {
56  int result;
57  char name_buff[NAME_BUFF_SIZE];
58 
59  av_get_channel_layout_string(name_buff, NAME_BUFF_SIZE, 0, ch_layout);
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  ctx->channel_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  int64_t 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  /* XXX: FLAC ignores it for some reason */
96  ctx->request_channel_layout = ch_layout;
97  ctx->channel_layout = ch_layout;
98 
99  result = avcodec_open2(ctx, dec, NULL);
100  if (result < 0) {
101  av_log(ctx, AV_LOG_ERROR, "Can't open decoder\n");
102  return result;
103  }
104 
105  *dec_ctx = ctx;
106  return 0;
107 }
108 
109 static int run_test(const AVCodec *enc, const AVCodec *dec,
111 {
112  AVPacket *enc_pkt;
113  AVFrame *in_frame, *out_frame;
114  uint8_t *raw_in = NULL, *raw_out = NULL;
115  int in_offset = 0, out_offset = 0;
116  int result = 0;
117  int i = 0;
118  int in_frame_bytes, out_frame_bytes;
119 
120  enc_pkt = av_packet_alloc();
121  if (!enc_pkt) {
122  av_log(NULL, AV_LOG_ERROR, "Can't allocate output packet\n");
123  return AVERROR(ENOMEM);
124  }
125 
126  in_frame = av_frame_alloc();
127  if (!in_frame) {
128  av_log(NULL, AV_LOG_ERROR, "Can't allocate input frame\n");
129  return AVERROR(ENOMEM);
130  }
131 
132  in_frame->nb_samples = enc_ctx->frame_size;
133  in_frame->format = enc_ctx->sample_fmt;
134  in_frame->channel_layout = enc_ctx->channel_layout;
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->channels, enc_ctx->frame_size);
165  in_frame_bytes = in_frame->nb_samples * in_frame->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 (in_frame->channel_layout != out_frame->channel_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->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;
251  int sample_rates[] = {8000, 44100, 48000, 192000};
252  int cl, sr;
253 
255  if (!enc) {
256  av_log(NULL, AV_LOG_ERROR, "Can't find encoder\n");
257  return 1;
258  }
259 
261  if (!dec) {
262  av_log(NULL, AV_LOG_ERROR, "Can't find decoder\n");
263  return 1;
264  }
265 
266  for (cl = 0; cl < FF_ARRAY_ELEMS(channel_layouts); cl++) {
267  for (sr = 0; sr < FF_ARRAY_ELEMS(sample_rates); sr++) {
268  if (init_encoder(enc, &enc_ctx, channel_layouts[cl], sample_rates[sr]) != 0)
269  return 1;
270  if (init_decoder(dec, &dec_ctx, channel_layouts[cl]) != 0)
271  return 1;
272  if (run_test(enc, dec, enc_ctx, dec_ctx) != 0)
273  return 1;
274  avcodec_free_context(&enc_ctx);
276  }
277  }
278 
279  return 0;
280 }
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1008
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:404
AVCodec
AVCodec.
Definition: codec.h:197
avcodec_receive_packet
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
Read encoded data from the encoder.
Definition: encode.c:392
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
AVCodecContext::channel_layout
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1039
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:246
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:988
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:112
avcodec_find_encoder
const AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
Definition: allcodecs.c:910
av_get_channel_layout_string
void av_get_channel_layout_string(char *buf, int buf_size, int nb_channels, uint64_t channel_layout)
Return a description of a channel layout.
Definition: channel_layout.c:217
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
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:490
AV_CODEC_ID_FLAC
@ AV_CODEC_ID_FLAC
Definition: codec_id.h:434
AVERROR_UNKNOWN
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:73
sample_rate
sample_rate
Definition: ffmpeg_filter.c:156
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:70
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:317
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
samplefmt.h
AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_STEREO
Definition: channel_layout.h:91
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:99
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
avcodec_alloc_context3
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:141
AVFrame::channels
int channels
number of audio channels, only used for audio.
Definition: frame.h:592
frame_size
int frame_size
Definition: mxfenc.c:2206
AV_CH_LAYOUT_STEREO_DOWNMIX
#define AV_CH_LAYOUT_STEREO_DOWNMIX
Definition: channel_layout.h:117
avcodec_receive_frame
int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Return decoded output data from a decoder.
Definition: decode.c:639
ctx
AVFormatContext * ctx
Definition: movenc.c:48
channels
channels
Definition: aptx.h:33
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:156
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:137
avcodec_find_decoder
const AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition: allcodecs.c:915
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:996
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:109
AVFrame::format
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:376
AVFrame::channel_layout
uint64_t channel_layout
Channel layout of the audio data.
Definition: frame.h:463
AV_CH_LAYOUT_5POINT1_BACK
#define AV_CH_LAYOUT_5POINT1_BACK
Definition: channel_layout.h:103
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:59
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
sample_rates
sample_rates
Definition: ffmpeg_filter.c:156
NUMBER_OF_AUDIO_FRAMES
#define NUMBER_OF_AUDIO_FRAMES
Definition: api-flac-test.c:35
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:989
avcodec_send_packet
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
Definition: decode.c:576
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:369
i
int i
Definition: input.c:406
common.h
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:61
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:361
avcodec.h
AV_CH_LAYOUT_SURROUND
#define AV_CH_LAYOUT_SURROUND
Definition: channel_layout.h:94
main
int main(void)
Definition: api-flac-test.c:246
init_encoder
static int init_encoder(const AVCodec *enc, AVCodecContext **enc_ctx, int64_t ch_layout, int sample_rate)
Definition: api-flac-test.c:52
init_decoder
static int init_decoder(const AVCodec *dec, AVCodecContext **dec_ctx, int64_t ch_layout)
Definition: api-flac-test.c:82
AVCodecContext
main external API structure.
Definition: avcodec.h:379
channel_layout.h
AVPacket
This structure stores compressed data.
Definition: packet.h:350
channel_layouts
static const uint16_t channel_layouts[7]
Definition: dca_lbr.c:114
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:35
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:334
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
dec_ctx
static AVCodecContext * dec_ctx
Definition: filtering_audio.c:44