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.

197 lines
6.7KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <inttypes.h>
  21. #include "libavutil/audio_fifo.c"
  22. #define MAX_CHANNELS 32
  23. typedef struct TestStruct {
  24. const enum AVSampleFormat format;
  25. const int nb_ch;
  26. void const *data_planes[MAX_CHANNELS];
  27. int nb_samples_pch;
  28. } TestStruct;
  29. static const uint8_t data_U8 [] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
  30. static const int16_t data_S16[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
  31. static const float data_FLT[] = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0};
  32. static const TestStruct test_struct[] = {
  33. {.format = AV_SAMPLE_FMT_U8 , .nb_ch = 1, .data_planes = {data_U8 , }, .nb_samples_pch = 12},
  34. {.format = AV_SAMPLE_FMT_U8P , .nb_ch = 2, .data_planes = {data_U8 , data_U8 +6, }, .nb_samples_pch = 6 },
  35. {.format = AV_SAMPLE_FMT_S16 , .nb_ch = 1, .data_planes = {data_S16, }, .nb_samples_pch = 12},
  36. {.format = AV_SAMPLE_FMT_S16P , .nb_ch = 2, .data_planes = {data_S16, data_S16+6, }, .nb_samples_pch = 6 },
  37. {.format = AV_SAMPLE_FMT_FLT , .nb_ch = 1, .data_planes = {data_FLT, }, .nb_samples_pch = 12},
  38. {.format = AV_SAMPLE_FMT_FLTP , .nb_ch = 2, .data_planes = {data_FLT, data_FLT+6, }, .nb_samples_pch = 6 }
  39. };
  40. static void ERROR(const char *str)
  41. {
  42. fprintf(stderr, "%s\n", str);
  43. exit(1);
  44. }
  45. static void* allocate_memory(size_t size)
  46. {
  47. void *ptr = malloc(size);
  48. if (ptr == NULL){
  49. ERROR("failed to allocate memory!");
  50. }
  51. return ptr;
  52. }
  53. static void print_audio_bytes(const TestStruct *test_sample, void **data_planes, int nb_samples)
  54. {
  55. int p, b, f;
  56. int byte_offset = av_get_bytes_per_sample(test_sample->format);
  57. int buffers = av_sample_fmt_is_planar(test_sample->format)
  58. ? test_sample->nb_ch : 1;
  59. int line_size = (buffers > 1) ? nb_samples * byte_offset
  60. : nb_samples * byte_offset * test_sample->nb_ch;
  61. for (p = 0; p < buffers; ++p){
  62. for(b = 0; b < line_size; b+=byte_offset){
  63. for (f = 0; f < byte_offset; f++){
  64. int order = !HAVE_BIGENDIAN ? (byte_offset - f - 1) : f;
  65. printf("%02x", *((uint8_t*)data_planes[p] + b + order));
  66. }
  67. putchar(' ');
  68. }
  69. putchar('\n');
  70. }
  71. }
  72. static int read_samples_from_audio_fifo(AVAudioFifo* afifo, void ***output, int nb_samples)
  73. {
  74. int i;
  75. int samples = FFMIN(nb_samples, afifo->nb_samples);
  76. int tot_elements = !av_sample_fmt_is_planar(afifo->sample_fmt)
  77. ? samples : afifo->channels * samples;
  78. void **data_planes = allocate_memory(sizeof(void*) * afifo->nb_buffers);
  79. *output = data_planes;
  80. for (i = 0; i < afifo->nb_buffers; ++i){
  81. data_planes[i] = allocate_memory(afifo->sample_size * tot_elements);
  82. }
  83. return av_audio_fifo_read(afifo, *output, nb_samples);
  84. }
  85. static int write_samples_to_audio_fifo(AVAudioFifo* afifo, const TestStruct test_sample,
  86. int nb_samples, int offset)
  87. {
  88. int offset_size, i;
  89. void *data_planes[MAX_CHANNELS];
  90. if(nb_samples > test_sample.nb_samples_pch - offset){
  91. return 0;
  92. }
  93. if(offset >= test_sample.nb_samples_pch){
  94. return 0;
  95. }
  96. offset_size = offset * afifo->sample_size;
  97. for (i = 0; i < afifo->nb_buffers ; ++i){
  98. data_planes[i] = (uint8_t*)test_sample.data_planes[i] + offset_size;
  99. }
  100. return av_audio_fifo_write(afifo, data_planes, nb_samples);
  101. }
  102. static void test_function(const TestStruct test_sample)
  103. {
  104. int ret, i;
  105. void **output_data = NULL;
  106. AVAudioFifo *afifo = av_audio_fifo_alloc(test_sample.format, test_sample.nb_ch,
  107. test_sample.nb_samples_pch);
  108. if (!afifo) {
  109. ERROR("ERROR: av_audio_fifo_alloc returned NULL!");
  110. }
  111. ret = write_samples_to_audio_fifo(afifo, test_sample, test_sample.nb_samples_pch, 0);
  112. if (ret < 0){
  113. ERROR("ERROR: av_audio_fifo_write failed!");
  114. }
  115. printf("written: %d\n", ret);
  116. ret = write_samples_to_audio_fifo(afifo, test_sample, test_sample.nb_samples_pch, 0);
  117. if (ret < 0){
  118. ERROR("ERROR: av_audio_fifo_write failed!");
  119. }
  120. printf("written: %d\n", ret);
  121. printf("remaining samples in audio_fifo: %d\n\n", av_audio_fifo_size(afifo));
  122. ret = read_samples_from_audio_fifo(afifo, &output_data, test_sample.nb_samples_pch);
  123. if (ret < 0){
  124. ERROR("ERROR: av_audio_fifo_read failed!");
  125. }
  126. printf("read: %d\n", ret);
  127. print_audio_bytes(&test_sample, output_data, ret);
  128. printf("remaining samples in audio_fifo: %d\n\n", av_audio_fifo_size(afifo));
  129. /* test av_audio_fifo_peek */
  130. ret = av_audio_fifo_peek(afifo, output_data, afifo->nb_samples);
  131. if (ret < 0){
  132. ERROR("ERROR: av_audio_fifo_peek failed!");
  133. }
  134. printf("peek:\n");
  135. print_audio_bytes(&test_sample, output_data, ret);
  136. printf("\n");
  137. /* test av_audio_fifo_peek_at */
  138. printf("peek_at:\n");
  139. for (i = 0; i < afifo->nb_samples; ++i){
  140. ret = av_audio_fifo_peek_at(afifo, output_data, 1, i);
  141. if (ret < 0){
  142. ERROR("ERROR: av_audio_fifo_peek_at failed!");
  143. }
  144. printf("%d:\n", i);
  145. print_audio_bytes(&test_sample, output_data, ret);
  146. }
  147. printf("\n");
  148. /* test av_audio_fifo_drain */
  149. ret = av_audio_fifo_drain(afifo, afifo->nb_samples);
  150. if (ret < 0){
  151. ERROR("ERROR: av_audio_fifo_drain failed!");
  152. }
  153. if (afifo->nb_samples){
  154. ERROR("drain failed to flush all samples in audio_fifo!");
  155. }
  156. /* deallocate */
  157. for (i = 0; i < afifo->nb_buffers; ++i){
  158. free(output_data[i]);
  159. }
  160. free(output_data);
  161. av_audio_fifo_free(afifo);
  162. }
  163. int main(void)
  164. {
  165. int t, tests = sizeof(test_struct)/sizeof(test_struct[0]);
  166. for (t = 0; t < tests; ++t){
  167. printf("\nTEST: %d\n\n", t+1);
  168. test_function(test_struct[t]);
  169. }
  170. return 0;
  171. }