api-flac-test.c 9.1 KB

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