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" 31#include "libavutil/channel_layout.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 */ 39static 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 52static int init_encoder(const AVCodec *enc, AVCodecContext **enc_ctx, 53 const AVChannelLayout *ch_layout, int sample_rate) 54{ 55 AVCodecContext *ctx; 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 62 ctx = avcodec_alloc_context3(enc); 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 82static int init_decoder(const AVCodec *dec, AVCodecContext **dec_ctx, 83 const AVChannelLayout *ch_layout) 84{ 85 AVCodecContext *ctx; 86 int result; 87 88 ctx = avcodec_alloc_context3(dec); 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 107static int run_test(const AVCodec *enc, const AVCodec *dec, 108 AVCodecContext *enc_ctx, AVCodecContext *dec_ctx) 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 246int main(void) 247{ 248 const AVCodec *enc = NULL, *dec = NULL; 249 AVCodecContext *enc_ctx = NULL, *dec_ctx = NULL; 250 const AVChannelLayout channel_layouts[] = { AV_CHANNEL_LAYOUT_STEREO, 251 AV_CHANNEL_LAYOUT_5POINT1_BACK, 252 AV_CHANNEL_LAYOUT_SURROUND, 253 AV_CHANNEL_LAYOUT_STEREO_DOWNMIX }; 254 int sample_rates[] = {8000, 44100, 48000, 192000}; 255 int cl, sr; 256 257 enc = avcodec_find_encoder(AV_CODEC_ID_FLAC); 258 if (!enc) { 259 av_log(NULL, AV_LOG_ERROR, "Can't find encoder\n"); 260 return 1; 261 } 262 263 dec = avcodec_find_decoder(AV_CODEC_ID_FLAC); 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); 278 avcodec_free_context(&dec_ctx); 279 } 280 } 281 282 return 0; 283} 284