Team Fortress 2 Source Code as on 22/4/2020
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.

124 lines
3.8 KiB

  1. #include <assert.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #define STB_VORBIS_HEADER_ONLY
  6. #include "stb_vorbis.c"
  7. #define SAMPLES_TO_TEST 3000
  8. int test_count [5] = { 5000, 3000, 2000, 50000, 50000 };
  9. int test_spacing[5] = { 1, 111, 3337, 7779, 72717 };
  10. int try_seeking(stb_vorbis *v, unsigned int pos, short *output, unsigned int num_samples)
  11. {
  12. int count;
  13. short samples[SAMPLES_TO_TEST*2];
  14. assert(pos <= num_samples);
  15. if (!stb_vorbis_seek(v, pos)) {
  16. fprintf(stderr, "Seek to %u returned error from stb_vorbis\n", pos);
  17. return 0;
  18. }
  19. count = stb_vorbis_get_samples_short_interleaved(v, 2, samples, SAMPLES_TO_TEST*2);
  20. if (count > (int) (num_samples - pos)) {
  21. fprintf(stderr, "Seek to %u allowed decoding %d samples when only %d should have been valid.\n",
  22. pos, count, (int) (num_samples - pos));
  23. return 0;
  24. }
  25. if (count < SAMPLES_TO_TEST && count < (int) (num_samples - pos)) {
  26. fprintf(stderr, "Seek to %u only decoded %d samples of %d attempted when at least %d should have been valid.\n",
  27. pos, count, SAMPLES_TO_TEST, num_samples - pos);
  28. return 0;
  29. }
  30. if (0 != memcmp(samples, output + pos*2, count*2)) {
  31. int k;
  32. for (k=0; k < SAMPLES_TO_TEST*2; ++k) {
  33. if (samples[k] != output[k]) {
  34. fprintf(stderr, "Seek to %u produced incorrect samples starting at sample %u (short #%d in buffer).\n",
  35. pos, pos + (k/2), k);
  36. break;
  37. }
  38. }
  39. assert(k != SAMPLES_TO_TEST*2);
  40. return 0;
  41. }
  42. return 1;
  43. }
  44. int main(int argc, char **argv)
  45. {
  46. int num_chan, samprate;
  47. int i, j, test, phase;
  48. short *output;
  49. if (argc == 1) {
  50. fprintf(stderr, "Usage: vorbseek {vorbisfile} [{vorbisfile]*]\n");
  51. fprintf(stderr, "Tests various seek offsets to make sure they're sample exact.\n");
  52. return 0;
  53. }
  54. #if 0
  55. {
  56. // check that outofmem occurs correctly
  57. stb_vorbis_alloc va;
  58. va.alloc_buffer = malloc(1024*1024);
  59. for (i=0; i < 1024*1024; i += 10) {
  60. int error=0;
  61. stb_vorbis *v;
  62. va.alloc_buffer_length_in_bytes = i;
  63. v = stb_vorbis_open_filename(argv[1], &error, &va);
  64. if (v != NULL)
  65. break;
  66. printf("Error %d at %d\n", error, i);
  67. }
  68. }
  69. #endif
  70. for (j=1; j < argc; ++j) {
  71. unsigned int successes=0, attempts = 0;
  72. unsigned int num_samples = stb_vorbis_decode_filename(argv[j], &num_chan, &samprate, &output);
  73. break;
  74. if (num_samples == 0xffffffff) {
  75. fprintf(stderr, "Error: couldn't open file or not vorbis file: %s\n", argv[j]);
  76. goto fail;
  77. }
  78. if (num_chan != 2) {
  79. fprintf(stderr, "vorbseek testing only works with files with 2 channels, %s has %d\n", argv[j], num_chan);
  80. goto fail;
  81. }
  82. for (test=0; test < 5; ++test) {
  83. int error;
  84. stb_vorbis *v = stb_vorbis_open_filename(argv[j], &error, NULL);
  85. if (v == NULL) {
  86. fprintf(stderr, "Couldn't re-open %s for test #%d\n", argv[j], test);
  87. goto fail;
  88. }
  89. for (phase=0; phase < 3; ++phase) {
  90. unsigned int base = phase == 0 ? 0 : phase == 1 ? num_samples - test_count[test]*test_spacing[test] : num_samples/3;
  91. for (i=0; i < test_count[test]; ++i) {
  92. unsigned int pos = base + i*test_spacing[test];
  93. if (pos > num_samples) // this also catches underflows
  94. continue;
  95. successes += try_seeking(v, pos, output, num_samples);
  96. attempts += 1;
  97. }
  98. }
  99. stb_vorbis_close(v);
  100. }
  101. printf("%d of %d seeks failed in %s (%d samples)\n", attempts-successes, attempts, argv[j], num_samples);
  102. free(output);
  103. }
  104. return 0;
  105. fail:
  106. return 1;
  107. }