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.

277 lines
9.3KB

  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_AUDIO_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 i = 0;
  100. int in_frame_bytes, out_frame_bytes;
  101. in_frame = av_frame_alloc();
  102. if (!in_frame) {
  103. av_log(NULL, AV_LOG_ERROR, "Can't allocate input frame\n");
  104. return AVERROR(ENOMEM);
  105. }
  106. in_frame->nb_samples = enc_ctx->frame_size;
  107. in_frame->format = enc_ctx->sample_fmt;
  108. in_frame->channel_layout = enc_ctx->channel_layout;
  109. if (av_frame_get_buffer(in_frame, 0) != 0) {
  110. av_log(NULL, AV_LOG_ERROR, "Can't allocate a buffer for input frame\n");
  111. return AVERROR(ENOMEM);
  112. }
  113. out_frame = av_frame_alloc();
  114. if (!out_frame) {
  115. av_log(NULL, AV_LOG_ERROR, "Can't allocate output frame\n");
  116. return AVERROR(ENOMEM);
  117. }
  118. raw_in = av_malloc(in_frame->linesize[0] * NUMBER_OF_AUDIO_FRAMES);
  119. if (!raw_in) {
  120. av_log(NULL, AV_LOG_ERROR, "Can't allocate memory for raw_in\n");
  121. return AVERROR(ENOMEM);
  122. }
  123. raw_out = av_malloc(in_frame->linesize[0] * NUMBER_OF_AUDIO_FRAMES);
  124. if (!raw_out) {
  125. av_log(NULL, AV_LOG_ERROR, "Can't allocate memory for raw_out\n");
  126. return AVERROR(ENOMEM);
  127. }
  128. for (i = 0; i < NUMBER_OF_AUDIO_FRAMES; i++) {
  129. av_init_packet(&enc_pkt);
  130. enc_pkt.data = NULL;
  131. enc_pkt.size = 0;
  132. result = av_frame_make_writable(in_frame);
  133. if (result < 0)
  134. return result;
  135. generate_raw_frame((uint16_t*)(in_frame->data[0]), i, enc_ctx->sample_rate,
  136. enc_ctx->channels, enc_ctx->frame_size);
  137. in_frame_bytes = in_frame->nb_samples * in_frame->channels * sizeof(uint16_t);
  138. if (in_frame_bytes > in_frame->linesize[0]) {
  139. av_log(NULL, AV_LOG_ERROR, "Incorrect value of input frame linesize\n");
  140. return 1;
  141. }
  142. memcpy(raw_in + in_offset, in_frame->data[0], in_frame_bytes);
  143. in_offset += in_frame_bytes;
  144. result = avcodec_send_frame(enc_ctx, in_frame);
  145. if (result < 0) {
  146. av_log(NULL, AV_LOG_ERROR, "Error submitting a frame for encoding\n");
  147. return result;
  148. }
  149. while (result >= 0) {
  150. result = avcodec_receive_packet(enc_ctx, &enc_pkt);
  151. if (result == AVERROR(EAGAIN))
  152. break;
  153. else if (result < 0 && result != AVERROR_EOF) {
  154. av_log(NULL, AV_LOG_ERROR, "Error encoding audio frame\n");
  155. return result;
  156. }
  157. /* if we get an encoded packet, feed it straight to the decoder */
  158. result = avcodec_send_packet(dec_ctx, &enc_pkt);
  159. av_packet_unref(&enc_pkt);
  160. if (result < 0) {
  161. av_log(NULL, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
  162. return result;
  163. }
  164. result = avcodec_receive_frame(dec_ctx, out_frame);
  165. if (result == AVERROR(EAGAIN)) {
  166. result = 0;
  167. continue;
  168. } else if (result == AVERROR(EOF)) {
  169. result = 0;
  170. break;
  171. } else if (result < 0) {
  172. av_log(NULL, AV_LOG_ERROR, "Error decoding audio packet\n");
  173. return result;
  174. }
  175. if (in_frame->nb_samples != out_frame->nb_samples) {
  176. av_log(NULL, AV_LOG_ERROR, "Error frames before and after decoding has different number of samples\n");
  177. return AVERROR_UNKNOWN;
  178. }
  179. if (in_frame->channel_layout != out_frame->channel_layout) {
  180. av_log(NULL, AV_LOG_ERROR, "Error frames before and after decoding has different channel layout\n");
  181. return AVERROR_UNKNOWN;
  182. }
  183. if (in_frame->format != out_frame->format) {
  184. av_log(NULL, AV_LOG_ERROR, "Error frames before and after decoding has different sample format\n");
  185. return AVERROR_UNKNOWN;
  186. }
  187. out_frame_bytes = out_frame->nb_samples * out_frame->channels * sizeof(uint16_t);
  188. if (out_frame_bytes > out_frame->linesize[0]) {
  189. av_log(NULL, AV_LOG_ERROR, "Incorrect value of output frame linesize\n");
  190. return 1;
  191. }
  192. memcpy(raw_out + out_offset, out_frame->data[0], out_frame_bytes);
  193. out_offset += out_frame_bytes;
  194. }
  195. }
  196. if (memcmp(raw_in, raw_out, out_frame_bytes * NUMBER_OF_AUDIO_FRAMES) != 0) {
  197. av_log(NULL, AV_LOG_ERROR, "Output differs\n");
  198. return 1;
  199. }
  200. av_log(NULL, AV_LOG_INFO, "OK\n");
  201. av_freep(&raw_in);
  202. av_freep(&raw_out);
  203. av_frame_free(&in_frame);
  204. av_frame_free(&out_frame);
  205. return 0;
  206. }
  207. int main(void)
  208. {
  209. AVCodec *enc = NULL, *dec = NULL;
  210. AVCodecContext *enc_ctx = NULL, *dec_ctx = NULL;
  211. uint64_t channel_layouts[] = {AV_CH_LAYOUT_STEREO, AV_CH_LAYOUT_5POINT1_BACK, AV_CH_LAYOUT_SURROUND, AV_CH_LAYOUT_STEREO_DOWNMIX};
  212. int sample_rates[] = {8000, 44100, 48000, 192000};
  213. int cl, sr;
  214. enc = avcodec_find_encoder(AV_CODEC_ID_FLAC);
  215. if (!enc) {
  216. av_log(NULL, AV_LOG_ERROR, "Can't find encoder\n");
  217. return 1;
  218. }
  219. dec = avcodec_find_decoder(AV_CODEC_ID_FLAC);
  220. if (!dec) {
  221. av_log(NULL, AV_LOG_ERROR, "Can't find decoder\n");
  222. return 1;
  223. }
  224. for (cl = 0; cl < FF_ARRAY_ELEMS(channel_layouts); cl++) {
  225. for (sr = 0; sr < FF_ARRAY_ELEMS(sample_rates); sr++) {
  226. if (init_encoder(enc, &enc_ctx, channel_layouts[cl], sample_rates[sr]) != 0)
  227. return 1;
  228. if (init_decoder(dec, &dec_ctx, channel_layouts[cl]) != 0)
  229. return 1;
  230. if (run_test(enc, dec, enc_ctx, dec_ctx) != 0)
  231. return 1;
  232. avcodec_free_context(&enc_ctx);
  233. avcodec_free_context(&dec_ctx);
  234. }
  235. }
  236. return 0;
  237. }