api-flac-test.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. * FLAC codec test.
  25. * Encodes raw data to FLAC format and decodes it back to raw. Compares raw-data
  26. * after that.
  27. */
  28. #include "libavcodec/avcodec.h"
  29. #include "libavutil/channel_layout.h"
  30. #include "libavutil/common.h"
  31. #include "libavutil/mem.h"
  32. #include "libavutil/samplefmt.h"
  33. #define NUMBER_OF_AUDIO_FRAMES 200
  34. #define NAME_BUFF_SIZE 100
  35. /* generate i-th frame of test audio */
  36. static int generate_raw_frame(uint16_t *frame_data, int i, int sample_rate,
  37. int channels, int frame_size)
  38. {
  39. int j, k;
  40. for (j = 0; j < frame_size; j++) {
  41. frame_data[channels * j] = 10000 * ((j / 10 * i) % 2);
  42. for (k = 1; k < channels; k++)
  43. frame_data[channels * j + k] = frame_data[channels * j] * (k + 1);
  44. }
  45. return 0;
  46. }
  47. static int init_encoder(const AVCodec *enc, AVCodecContext **enc_ctx,
  48. const AVChannelLayout *ch_layout, int sample_rate)
  49. {
  50. AVCodecContext *ctx;
  51. int result;
  52. char name_buff[NAME_BUFF_SIZE];
  53. av_channel_layout_describe(ch_layout, name_buff, NAME_BUFF_SIZE);
  54. av_log(NULL, AV_LOG_INFO, "channel layout: %s, sample rate: %i\n", name_buff, sample_rate);
  55. ctx = avcodec_alloc_context3(enc);
  56. if (!ctx) {
  57. av_log(NULL, AV_LOG_ERROR, "Can't allocate encoder context\n");
  58. return AVERROR(ENOMEM);
  59. }
  60. ctx->sample_fmt = AV_SAMPLE_FMT_S16;
  61. ctx->sample_rate = sample_rate;
  62. av_channel_layout_copy(&ctx->ch_layout, ch_layout);
  63. result = avcodec_open2(ctx, enc, NULL);
  64. if (result < 0) {
  65. av_log(ctx, AV_LOG_ERROR, "Can't open encoder\n");
  66. return result;
  67. }
  68. *enc_ctx = ctx;
  69. return 0;
  70. }
  71. static int init_decoder(const AVCodec *dec, AVCodecContext **dec_ctx,
  72. const AVChannelLayout *ch_layout)
  73. {
  74. AVCodecContext *ctx;
  75. int result;
  76. ctx = avcodec_alloc_context3(dec);
  77. if (!ctx) {
  78. av_log(NULL, AV_LOG_ERROR , "Can't allocate decoder context\n");
  79. return AVERROR(ENOMEM);
  80. }
  81. ctx->request_sample_fmt = AV_SAMPLE_FMT_S16;
  82. av_channel_layout_copy(&ctx->ch_layout, ch_layout);
  83. result = avcodec_open2(ctx, dec, NULL);
  84. if (result < 0) {
  85. av_log(ctx, AV_LOG_ERROR, "Can't open decoder\n");
  86. return result;
  87. }
  88. *dec_ctx = ctx;
  89. return 0;
  90. }
  91. static int run_test(const AVCodec *enc, const AVCodec *dec,
  92. AVCodecContext *enc_ctx, AVCodecContext *dec_ctx)
  93. {
  94. AVPacket *enc_pkt;
  95. AVFrame *in_frame, *out_frame;
  96. uint8_t *raw_in = NULL, *raw_out = NULL;
  97. int in_offset = 0, out_offset = 0;
  98. int result = 0;
  99. int i = 0;
  100. int in_frame_bytes, out_frame_bytes;
  101. enc_pkt = av_packet_alloc();
  102. if (!enc_pkt) {
  103. av_log(NULL, AV_LOG_ERROR, "Can't allocate output packet\n");
  104. return AVERROR(ENOMEM);
  105. }
  106. in_frame = av_frame_alloc();
  107. if (!in_frame) {
  108. av_log(NULL, AV_LOG_ERROR, "Can't allocate input frame\n");
  109. return AVERROR(ENOMEM);
  110. }
  111. in_frame->nb_samples = enc_ctx->frame_size;
  112. in_frame->format = enc_ctx->sample_fmt;
  113. result = av_channel_layout_copy(&in_frame->ch_layout, &enc_ctx->ch_layout);
  114. if (result < 0)
  115. return result;
  116. if (av_frame_get_buffer(in_frame, 0) != 0) {
  117. av_log(NULL, AV_LOG_ERROR, "Can't allocate a buffer for input frame\n");
  118. return AVERROR(ENOMEM);
  119. }
  120. out_frame = av_frame_alloc();
  121. if (!out_frame) {
  122. av_log(NULL, AV_LOG_ERROR, "Can't allocate output frame\n");
  123. return AVERROR(ENOMEM);
  124. }
  125. raw_in = av_malloc(in_frame->linesize[0] * NUMBER_OF_AUDIO_FRAMES);
  126. if (!raw_in) {
  127. av_log(NULL, AV_LOG_ERROR, "Can't allocate memory for raw_in\n");
  128. return AVERROR(ENOMEM);
  129. }
  130. raw_out = av_malloc(in_frame->linesize[0] * NUMBER_OF_AUDIO_FRAMES);
  131. if (!raw_out) {
  132. av_log(NULL, AV_LOG_ERROR, "Can't allocate memory for raw_out\n");
  133. return AVERROR(ENOMEM);
  134. }
  135. for (i = 0; i < NUMBER_OF_AUDIO_FRAMES; i++) {
  136. result = av_frame_make_writable(in_frame);
  137. if (result < 0)
  138. return result;
  139. generate_raw_frame((uint16_t*)(in_frame->data[0]), i, enc_ctx->sample_rate,
  140. enc_ctx->ch_layout.nb_channels, enc_ctx->frame_size);
  141. in_frame_bytes = in_frame->nb_samples * in_frame->ch_layout.nb_channels * sizeof(uint16_t);
  142. if (in_frame_bytes > in_frame->linesize[0]) {
  143. av_log(NULL, AV_LOG_ERROR, "Incorrect value of input frame linesize\n");
  144. return 1;
  145. }
  146. memcpy(raw_in + in_offset, in_frame->data[0], in_frame_bytes);
  147. in_offset += in_frame_bytes;
  148. result = avcodec_send_frame(enc_ctx, in_frame);
  149. if (result < 0) {
  150. av_log(NULL, AV_LOG_ERROR, "Error submitting a frame for encoding\n");
  151. return result;
  152. }
  153. while (result >= 0) {
  154. result = avcodec_receive_packet(enc_ctx, enc_pkt);
  155. if (result == AVERROR(EAGAIN))
  156. break;
  157. else if (result < 0 && result != AVERROR_EOF) {
  158. av_log(NULL, AV_LOG_ERROR, "Error encoding audio frame\n");
  159. return result;
  160. }
  161. /* if we get an encoded packet, feed it straight to the decoder */
  162. result = avcodec_send_packet(dec_ctx, enc_pkt);
  163. av_packet_unref(enc_pkt);
  164. if (result < 0) {
  165. av_log(NULL, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
  166. return result;
  167. }
  168. result = avcodec_receive_frame(dec_ctx, out_frame);
  169. if (result == AVERROR(EAGAIN)) {
  170. result = 0;
  171. continue;
  172. } else if (result == AVERROR(EOF)) {
  173. result = 0;
  174. break;
  175. } else if (result < 0) {
  176. av_log(NULL, AV_LOG_ERROR, "Error decoding audio packet\n");
  177. return result;
  178. }
  179. if (in_frame->nb_samples != out_frame->nb_samples) {
  180. av_log(NULL, AV_LOG_ERROR, "Error frames before and after decoding has different number of samples\n");
  181. return AVERROR_UNKNOWN;
  182. }
  183. if (av_channel_layout_compare(&in_frame->ch_layout, &out_frame->ch_layout)) {
  184. av_log(NULL, AV_LOG_ERROR, "Error frames before and after decoding has different channel layout\n");
  185. return AVERROR_UNKNOWN;
  186. }
  187. if (in_frame->format != out_frame->format) {
  188. av_log(NULL, AV_LOG_ERROR, "Error frames before and after decoding has different sample format\n");
  189. return AVERROR_UNKNOWN;
  190. }
  191. out_frame_bytes = out_frame->nb_samples * out_frame->ch_layout.nb_channels * sizeof(uint16_t);
  192. if (out_frame_bytes > out_frame->linesize[0]) {
  193. av_log(NULL, AV_LOG_ERROR, "Incorrect value of output frame linesize\n");
  194. return 1;
  195. }
  196. memcpy(raw_out + out_offset, out_frame->data[0], out_frame_bytes);
  197. out_offset += out_frame_bytes;
  198. }
  199. }
  200. if (memcmp(raw_in, raw_out, out_frame_bytes * NUMBER_OF_AUDIO_FRAMES) != 0) {
  201. av_log(NULL, AV_LOG_ERROR, "Output differs\n");
  202. return 1;
  203. }
  204. av_log(NULL, AV_LOG_INFO, "OK\n");
  205. av_freep(&raw_in);
  206. av_freep(&raw_out);
  207. av_packet_free(&enc_pkt);
  208. av_frame_free(&in_frame);
  209. av_frame_free(&out_frame);
  210. return 0;
  211. }
  212. int main(void)
  213. {
  214. const AVCodec *enc = NULL, *dec = NULL;
  215. AVCodecContext *enc_ctx = NULL, *dec_ctx = NULL;
  216. const AVChannelLayout channel_layouts[] = { AV_CHANNEL_LAYOUT_STEREO,
  217. AV_CHANNEL_LAYOUT_5POINT1_BACK,
  218. AV_CHANNEL_LAYOUT_SURROUND,
  219. AV_CHANNEL_LAYOUT_STEREO_DOWNMIX };
  220. int sample_rates[] = {8000, 44100, 48000, 192000};
  221. int cl, sr;
  222. enc = avcodec_find_encoder(AV_CODEC_ID_FLAC);
  223. if (!enc) {
  224. av_log(NULL, AV_LOG_ERROR, "Can't find encoder\n");
  225. return 1;
  226. }
  227. dec = avcodec_find_decoder(AV_CODEC_ID_FLAC);
  228. if (!dec) {
  229. av_log(NULL, AV_LOG_ERROR, "Can't find decoder\n");
  230. return 1;
  231. }
  232. for (cl = 0; cl < FF_ARRAY_ELEMS(channel_layouts); cl++) {
  233. for (sr = 0; sr < FF_ARRAY_ELEMS(sample_rates); sr++) {
  234. if (init_encoder(enc, &enc_ctx, &channel_layouts[cl], sample_rates[sr]) != 0)
  235. return 1;
  236. if (init_decoder(dec, &dec_ctx, &channel_layouts[cl]) != 0)
  237. return 1;
  238. if (run_test(enc, dec, enc_ctx, dec_ctx) != 0)
  239. return 1;
  240. avcodec_free_context(&enc_ctx);
  241. avcodec_free_context(&dec_ctx);
  242. }
  243. }
  244. return 0;
  245. }