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
33
34 #define NUMBER_OF_FRAMES 200
35 #define NAME_BUFF_SIZE 100
36
37 /* generate i-th frame of test audio */
40 {
41 int j, k;
42
44 frame_data[channels * j] = 10000 * ((j / 10 * i) % 2);
46 frame_data[channels * j + k] = frame_data[channels * j] * (k + 1);
47 }
48 return 0;
49 }
50
53 {
55 int result;
57
60
62 if (!ctx) {
65 }
66
70
72 if (result < 0) {
74 return result;
75 }
76
78 return 0;
79 }
80
82 int64_t ch_layout)
83 {
85 int result;
86
88 if (!ctx) {
91 }
92
94 /* XXX: FLAC ignores it for some reason */
97
99 if (result < 0) {
101 return result;
102 }
103
105 return 0;
106 }
107
110 {
114 int in_offset = 0, out_offset = 0;
115 int result = 0;
116 int got_output = 0;
117 int i = 0;
118 int in_frame_bytes, out_frame_bytes;
119
121 if (!in_frame) {
124 }
125
132 }
133
135 if (!out_frame) {
138 }
139
141 if (!raw_in) {
144 }
145
147 if (!raw_out) {
150 }
151
156
160 if (in_frame_bytes > in_frame->
linesize[0]) {
162 return 1;
163 }
164 memcpy(raw_in + in_offset, in_frame->
data[0], in_frame_bytes);
165 in_offset += in_frame_bytes;
167 if (result < 0) {
169 return result;
170 }
171
172 /* if we get an encoded packet, feed it straight to the decoder */
173 if (got_output) {
175 if (result < 0) {
177 return result;
178 }
179
180 if (got_output) {
181 if (result != enc_pkt.
size) {
182 av_log(
NULL,
AV_LOG_INFO,
"Decoder consumed only part of a packet, it is allowed to do so -- need to update this test\n");
184 }
185
189 }
190
194 }
195
199 }
201 if (out_frame_bytes > out_frame->
linesize[0]) {
203 return 1;
204 }
205 memcpy(raw_out + out_offset, out_frame->
data[0], out_frame_bytes);
206 out_offset += out_frame_bytes;
207 }
208 }
210 }
211
212 if (memcmp(raw_in, raw_out, out_frame_bytes * NUMBER_OF_FRAMES) != 0) {
214 return 1;
215 }
216
218
223 return 0;
224 }
225
227 {
230 return 0;
231 }
232
234 {
237 return 0;
238 }
239
241 {
246 int cl, sr;
247
249 if (!enc) {
251 return 1;
252 }
253
255 if (!dec) {
257 return 1;
258 }
259
262 if (
init_encoder(enc, &enc_ctx, channel_layouts[cl], sample_rates[sr]) != 0)
263 return 1;
264 if (
init_decoder(dec, &dec_ctx, channel_layouts[cl]) != 0)
265 return 1;
266 if (
run_test(enc, dec, enc_ctx, dec_ctx) != 0)
267 return 1;
270 }
271 }
272
273 return 0;
274 }
This structure describes decoded (raw) audio or video data.
AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
static int run_test(AVCodec *enc, AVCodec *dec, AVCodecContext *enc_ctx, AVCodecContext *dec_ctx)
#define AV_CH_LAYOUT_SURROUND
attribute_deprecated int avcodec_encode_audio2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Encode a frame of audio.
attribute_deprecated int avcodec_decode_audio4(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, const AVPacket *avpkt)
Decode the audio frame of size avpkt->size from avpkt->data into frame.
#define AV_CH_LAYOUT_STEREO
enum AVSampleFormat sample_fmt
audio sample format
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
int avcodec_close(AVCodecContext *avctx)
Close a given AVCodecContext and free all the data associated with it (but not the AVCodecContext its...
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
enum AVSampleFormat request_sample_fmt
desired sample format
uint64_t channel_layout
Audio channel layout.
uint64_t channel_layout
Channel layout of the audio data.
int channels
number of audio channels, only used for audio.
#define AV_CH_LAYOUT_STEREO_DOWNMIX
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
#define AV_CH_LAYOUT_5POINT1_BACK
#define FF_ARRAY_ELEMS(a)
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.
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
int frame_size
Number of samples per channel in an audio frame.
#define AV_LOG_INFO
Standard information.
Libavcodec external API header.
int sample_rate
samples per second
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
static int close_encoder(AVCodecContext **enc_ctx)
main external API structure.
AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
static const uint16_t channel_layouts[7]
static int init_encoder(AVCodec *enc, AVCodecContext **enc_ctx, int64_t ch_layout, int sample_rate)
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
common internal and external API header
static AVCodecContext * dec_ctx
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
static int generate_raw_frame(uint16_t *frame_data, int i, int sample_rate, int channels, int frame_size)
int channels
number of audio channels
static int close_decoder(AVCodecContext **dec_ctx)
static int init_decoder(AVCodec *dec, AVCodecContext **dec_ctx, int64_t ch_layout)
uint64_t request_channel_layout
Request decoder to use this channel layout if it can (0 for default)
This structure stores compressed data.
int nb_samples
number of audio samples (per channel) described by this frame