You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

291 lines
8.7KB

  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 "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. {
  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(AVCodec *enc, AVCodecContext **enc_ctx,
  47. int64_t ch_layout, int sample_rate)
  48. {
  49. AVCodecContext *ctx;
  50. int result;
  51. char name_buff[NAME_BUFF_SIZE];
  52. av_get_channel_layout_string(name_buff, NAME_BUFF_SIZE, 0, ch_layout);
  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. {
  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. ctx->channel_layout = ch_layout;
  63. result = avcodec_open2(ctx, enc, NULL);
  64. if (result < 0)
  65. {
  66. av_log(ctx, AV_LOG_ERROR, "Can't open encoder\n");
  67. return result;
  68. }
  69. *enc_ctx = ctx;
  70. return 0;
  71. }
  72. static int init_decoder(AVCodec *dec, AVCodecContext **dec_ctx,
  73. int64_t ch_layout)
  74. {
  75. AVCodecContext *ctx;
  76. int result;
  77. ctx = avcodec_alloc_context3(dec);
  78. if (!ctx)
  79. {
  80. av_log(NULL, AV_LOG_ERROR , "Can't allocate decoder context\n");
  81. return AVERROR(ENOMEM);
  82. }
  83. ctx->request_sample_fmt = AV_SAMPLE_FMT_S16;
  84. /* XXX: FLAC ignores it for some reason */
  85. ctx->request_channel_layout = ch_layout;
  86. ctx->channel_layout = ch_layout;
  87. result = avcodec_open2(ctx, dec, NULL);
  88. if (result < 0)
  89. {
  90. av_log(ctx, AV_LOG_ERROR, "Can't open decoder\n");
  91. return result;
  92. }
  93. *dec_ctx = ctx;
  94. return 0;
  95. }
  96. static int run_test(AVCodec *enc, AVCodec *dec, AVCodecContext *enc_ctx,
  97. AVCodecContext *dec_ctx)
  98. {
  99. AVPacket enc_pkt;
  100. AVFrame *in_frame, *out_frame;
  101. uint8_t *raw_in = NULL, *raw_out = NULL;
  102. int in_offset = 0, out_offset = 0;
  103. int frame_data_size = 0;
  104. int result = 0;
  105. int got_output = 0;
  106. int i = 0;
  107. in_frame = av_frame_alloc();
  108. if (!in_frame)
  109. {
  110. av_log(NULL, AV_LOG_ERROR, "Can't allocate input frame\n");
  111. return AVERROR(ENOMEM);
  112. }
  113. in_frame->nb_samples = enc_ctx->frame_size;
  114. in_frame->format = enc_ctx->sample_fmt;
  115. in_frame->channel_layout = enc_ctx->channel_layout;
  116. if (av_frame_get_buffer(in_frame, 32) != 0)
  117. {
  118. av_log(NULL, AV_LOG_ERROR, "Can't allocate a buffer for input frame\n");
  119. return AVERROR(ENOMEM);
  120. }
  121. out_frame = av_frame_alloc();
  122. if (!out_frame)
  123. {
  124. av_log(NULL, AV_LOG_ERROR, "Can't allocate output frame\n");
  125. return AVERROR(ENOMEM);
  126. }
  127. raw_in = av_malloc(in_frame->linesize[0] * NUMBER_OF_FRAMES);
  128. if (!raw_in)
  129. {
  130. av_log(NULL, AV_LOG_ERROR, "Can't allocate memory for raw_in\n");
  131. return AVERROR(ENOMEM);
  132. }
  133. raw_out = av_malloc(in_frame->linesize[0] * NUMBER_OF_FRAMES);
  134. if (!raw_out)
  135. {
  136. av_log(NULL, AV_LOG_ERROR, "Can't allocate memory for raw_out\n");
  137. return AVERROR(ENOMEM);
  138. }
  139. for (i = 0; i < NUMBER_OF_FRAMES; i++)
  140. {
  141. av_init_packet(&enc_pkt);
  142. enc_pkt.data = NULL;
  143. enc_pkt.size = 0;
  144. generate_raw_frame((uint16_t*)(in_frame->data[0]), i, enc_ctx->sample_rate,
  145. enc_ctx->channels, enc_ctx->frame_size);
  146. memcpy(raw_in + in_offset, in_frame->data[0], in_frame->linesize[0]);
  147. in_offset += in_frame->linesize[0];
  148. result = avcodec_encode_audio2(enc_ctx, &enc_pkt, in_frame, &got_output);
  149. if (result < 0)
  150. {
  151. av_log(NULL, AV_LOG_ERROR, "Error encoding audio frame\n");
  152. return result;
  153. }
  154. /* if we get an encoded packet, feed it straight to the decoder */
  155. if (got_output)
  156. {
  157. result = avcodec_decode_audio4(dec_ctx, out_frame, &got_output, &enc_pkt);
  158. if (result < 0)
  159. {
  160. av_log(NULL, AV_LOG_ERROR, "Error decoding audio packet\n");
  161. return result;
  162. }
  163. if (got_output)
  164. {
  165. if (result != enc_pkt.size)
  166. {
  167. 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");
  168. return AVERROR_UNKNOWN;
  169. }
  170. if (in_frame->nb_samples != out_frame->nb_samples)
  171. {
  172. av_log(NULL, AV_LOG_ERROR, "Error frames before and after decoding has different number of samples\n");
  173. return AVERROR_UNKNOWN;
  174. }
  175. if (in_frame->channel_layout != out_frame->channel_layout)
  176. {
  177. av_log(NULL, AV_LOG_ERROR, "Error frames before and after decoding has different channel layout\n");
  178. return AVERROR_UNKNOWN;
  179. }
  180. if (in_frame->format != out_frame->format)
  181. {
  182. av_log(NULL, AV_LOG_ERROR, "Error frames before and after decoding has different sample format\n");
  183. return AVERROR_UNKNOWN;
  184. }
  185. memcpy(raw_out + out_offset, out_frame->data[0], out_frame->linesize[0]);
  186. out_offset += out_frame->linesize[0];
  187. }
  188. }
  189. av_free_packet(&enc_pkt);
  190. }
  191. if (memcmp(raw_in, raw_out, frame_data_size * NUMBER_OF_FRAMES) != 0)
  192. {
  193. av_log(NULL, AV_LOG_ERROR, "Output differs\n");
  194. return 1;
  195. }
  196. av_log(NULL, AV_LOG_INFO, "OK\n");
  197. av_freep(&raw_in);
  198. av_freep(&raw_out);
  199. av_frame_free(&in_frame);
  200. av_frame_free(&out_frame);
  201. return 0;
  202. }
  203. static int close_encoder(AVCodecContext *enc_ctx)
  204. {
  205. avcodec_close(enc_ctx);
  206. av_freep(&enc_ctx);
  207. return 0;
  208. }
  209. static int close_decoder(AVCodecContext *dec_ctx)
  210. {
  211. avcodec_close(dec_ctx);
  212. av_freep(&dec_ctx);
  213. return 0;
  214. }
  215. int main(void)
  216. {
  217. AVCodec *enc = NULL, *dec = NULL;
  218. AVCodecContext *enc_ctx = NULL, *dec_ctx = NULL;
  219. uint64_t channel_layouts[] = {AV_CH_LAYOUT_STEREO, AV_CH_LAYOUT_5POINT1_BACK, AV_CH_LAYOUT_SURROUND, AV_CH_LAYOUT_STEREO_DOWNMIX};
  220. int sample_rates[] = {8000, 44100, 48000, 192000};
  221. int cl, sr;
  222. avcodec_register_all();
  223. enc = avcodec_find_encoder(AV_CODEC_ID_FLAC);
  224. if (!enc)
  225. {
  226. av_log(NULL, AV_LOG_ERROR, "Can't find encoder\n");
  227. return 1;
  228. }
  229. dec = avcodec_find_decoder(AV_CODEC_ID_FLAC);
  230. if (!dec)
  231. {
  232. av_log(NULL, AV_LOG_ERROR, "Can't find decoder\n");
  233. return 1;
  234. }
  235. for (cl = 0; cl < FF_ARRAY_ELEMS(channel_layouts); cl++)
  236. {
  237. for (sr = 0; sr < FF_ARRAY_ELEMS(sample_rates); sr++)
  238. {
  239. if (init_encoder(enc, &enc_ctx, channel_layouts[cl], sample_rates[sr]) != 0)
  240. return 1;
  241. if (init_decoder(dec, &dec_ctx, channel_layouts[cl]) != 0)
  242. return 1;
  243. if (run_test(enc, dec, enc_ctx, dec_ctx) != 0)
  244. return 1;
  245. close_encoder(enc_ctx);
  246. close_decoder(dec_ctx);
  247. }
  248. }
  249. return 0;
  250. }