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.

267 lines
8.6KB

  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 frame_data_size = 0;
  99. int result = 0;
  100. int got_output = 0;
  101. int i = 0;
  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. memcpy(raw_in + in_offset, in_frame->data[0], in_frame->linesize[0]);
  136. in_offset += in_frame->linesize[0];
  137. result = avcodec_encode_audio2(enc_ctx, &enc_pkt, in_frame, &got_output);
  138. if (result < 0) {
  139. av_log(NULL, AV_LOG_ERROR, "Error encoding audio frame\n");
  140. return result;
  141. }
  142. /* if we get an encoded packet, feed it straight to the decoder */
  143. if (got_output) {
  144. result = avcodec_decode_audio4(dec_ctx, out_frame, &got_output, &enc_pkt);
  145. if (result < 0) {
  146. av_log(NULL, AV_LOG_ERROR, "Error decoding audio packet\n");
  147. return result;
  148. }
  149. if (got_output) {
  150. if (result != enc_pkt.size) {
  151. 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");
  152. return AVERROR_UNKNOWN;
  153. }
  154. if (in_frame->nb_samples != out_frame->nb_samples) {
  155. av_log(NULL, AV_LOG_ERROR, "Error frames before and after decoding has different number of samples\n");
  156. return AVERROR_UNKNOWN;
  157. }
  158. if (in_frame->channel_layout != out_frame->channel_layout) {
  159. av_log(NULL, AV_LOG_ERROR, "Error frames before and after decoding has different channel layout\n");
  160. return AVERROR_UNKNOWN;
  161. }
  162. if (in_frame->format != out_frame->format) {
  163. av_log(NULL, AV_LOG_ERROR, "Error frames before and after decoding has different sample format\n");
  164. return AVERROR_UNKNOWN;
  165. }
  166. memcpy(raw_out + out_offset, out_frame->data[0], out_frame->linesize[0]);
  167. out_offset += out_frame->linesize[0];
  168. }
  169. }
  170. av_free_packet(&enc_pkt);
  171. }
  172. if (memcmp(raw_in, raw_out, frame_data_size * NUMBER_OF_FRAMES) != 0) {
  173. av_log(NULL, AV_LOG_ERROR, "Output differs\n");
  174. return 1;
  175. }
  176. av_log(NULL, AV_LOG_INFO, "OK\n");
  177. av_freep(&raw_in);
  178. av_freep(&raw_out);
  179. av_frame_free(&in_frame);
  180. av_frame_free(&out_frame);
  181. return 0;
  182. }
  183. static int close_encoder(AVCodecContext **enc_ctx)
  184. {
  185. avcodec_close(*enc_ctx);
  186. av_freep(enc_ctx);
  187. return 0;
  188. }
  189. static int close_decoder(AVCodecContext **dec_ctx)
  190. {
  191. avcodec_close(*dec_ctx);
  192. av_freep(dec_ctx);
  193. return 0;
  194. }
  195. int main(void)
  196. {
  197. AVCodec *enc = NULL, *dec = NULL;
  198. AVCodecContext *enc_ctx = NULL, *dec_ctx = NULL;
  199. uint64_t channel_layouts[] = {AV_CH_LAYOUT_STEREO, AV_CH_LAYOUT_5POINT1_BACK, AV_CH_LAYOUT_SURROUND, AV_CH_LAYOUT_STEREO_DOWNMIX};
  200. int sample_rates[] = {8000, 44100, 48000, 192000};
  201. int cl, sr;
  202. avcodec_register_all();
  203. enc = avcodec_find_encoder(AV_CODEC_ID_FLAC);
  204. if (!enc) {
  205. av_log(NULL, AV_LOG_ERROR, "Can't find encoder\n");
  206. return 1;
  207. }
  208. dec = avcodec_find_decoder(AV_CODEC_ID_FLAC);
  209. if (!dec) {
  210. av_log(NULL, AV_LOG_ERROR, "Can't find decoder\n");
  211. return 1;
  212. }
  213. for (cl = 0; cl < FF_ARRAY_ELEMS(channel_layouts); cl++) {
  214. for (sr = 0; sr < FF_ARRAY_ELEMS(sample_rates); sr++) {
  215. if (init_encoder(enc, &enc_ctx, channel_layouts[cl], sample_rates[sr]) != 0)
  216. return 1;
  217. if (init_decoder(dec, &dec_ctx, channel_layouts[cl]) != 0)
  218. return 1;
  219. if (run_test(enc, dec, enc_ctx, dec_ctx) != 0)
  220. return 1;
  221. close_encoder(&enc_ctx);
  222. close_decoder(&dec_ctx);
  223. }
  224. }
  225. return 0;
  226. }