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.

280 lines
9.4KB

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