api-flac-test.c 9.7 KB

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