probetest.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * copyright (c) 2009 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdlib.h>
  21. #include "libavformat/avformat.h"
  22. #include "libavcodec/put_bits.h"
  23. #include "libavutil/lfg.h"
  24. #include "libavutil/timer.h"
  25. #define MAX_FORMATS 1000 //this must be larger than the number of formats
  26. static int score_array[MAX_FORMATS];
  27. static int64_t time_array[MAX_FORMATS];
  28. static int failures = 0;
  29. #ifndef AV_READ_TIME
  30. #define AV_READ_TIME(x) 0
  31. #endif
  32. static void probe(AVProbeData *pd, int type, int p, int size)
  33. {
  34. int i = 0;
  35. AVInputFormat *fmt = NULL;
  36. while ((fmt = av_iformat_next(fmt))) {
  37. if (fmt->flags & AVFMT_NOFILE)
  38. continue;
  39. if (fmt->read_probe) {
  40. int score;
  41. int64_t start = AV_READ_TIME();
  42. score = fmt->read_probe(pd);
  43. time_array[i] += AV_READ_TIME() - start;
  44. if (score > score_array[i] && score > AVPROBE_SCORE_MAX / 4) {
  45. score_array[i] = score;
  46. fprintf(stderr,
  47. "Failure of %s probing code with score=%d type=%d p=%X size=%d\n",
  48. fmt->name, score, type, p, size);
  49. failures++;
  50. }
  51. }
  52. i++;
  53. }
  54. }
  55. static void print_times(void)
  56. {
  57. int i = 0;
  58. AVInputFormat *fmt = NULL;
  59. while ((fmt = av_iformat_next(fmt))) {
  60. if (fmt->flags & AVFMT_NOFILE)
  61. continue;
  62. if (time_array[i] > 1000000) {
  63. fprintf(stderr, "%12"PRIu64" cycles, %12s\n",
  64. time_array[i], fmt->name);
  65. }
  66. i++;
  67. }
  68. }
  69. int main(int argc, char **argv)
  70. {
  71. unsigned int p, i, type, size, retry;
  72. AVProbeData pd = { 0 };
  73. AVLFG state;
  74. PutBitContext pb;
  75. int retry_count= 4097;
  76. int max_size = 65537;
  77. if(argc >= 2)
  78. retry_count = atoi(argv[1]);
  79. if(argc >= 3)
  80. max_size = atoi(argv[2]);
  81. if (max_size > 1000000000U/8) {
  82. fprintf(stderr, "max_size out of bounds\n");
  83. return 1;
  84. }
  85. if (retry_count > 1000000000U) {
  86. fprintf(stderr, "retry_count out of bounds\n");
  87. return 1;
  88. }
  89. avcodec_register_all();
  90. av_register_all();
  91. av_lfg_init(&state, 0xdeadbeef);
  92. pd.buf = NULL;
  93. for (size = 1; size < max_size; size *= 2) {
  94. pd.buf_size = size;
  95. pd.buf = av_realloc(pd.buf, size + AVPROBE_PADDING_SIZE);
  96. pd.filename = "";
  97. if (!pd.buf) {
  98. fprintf(stderr, "out of memory\n");
  99. return 1;
  100. }
  101. memset(pd.buf, 0, size + AVPROBE_PADDING_SIZE);
  102. fprintf(stderr, "testing size=%d\n", size);
  103. for (retry = 0; retry < retry_count; retry += FFMAX(size, 32)) {
  104. for (type = 0; type < 4; type++) {
  105. for (p = 0; p < 4096; p++) {
  106. unsigned hist = 0;
  107. init_put_bits(&pb, pd.buf, size);
  108. switch (type) {
  109. case 0:
  110. for (i = 0; i < size * 8; i++)
  111. put_bits(&pb, 1, (av_lfg_get(&state) & 0xFFFFFFFF) > p << 20);
  112. break;
  113. case 1:
  114. for (i = 0; i < size * 8; i++) {
  115. unsigned int p2 = hist ? p & 0x3F : (p >> 6);
  116. unsigned int v = (av_lfg_get(&state) & 0xFFFFFFFF) > p2 << 26;
  117. put_bits(&pb, 1, v);
  118. hist = v;
  119. }
  120. break;
  121. case 2:
  122. for (i = 0; i < size * 8; i++) {
  123. unsigned int p2 = (p >> (hist * 3)) & 7;
  124. unsigned int v = (av_lfg_get(&state) & 0xFFFFFFFF) > p2 << 29;
  125. put_bits(&pb, 1, v);
  126. hist = (2 * hist + v) & 3;
  127. }
  128. break;
  129. case 3:
  130. for (i = 0; i < size; i++) {
  131. int c = 0;
  132. while (p & 63) {
  133. c = (av_lfg_get(&state) & 0xFFFFFFFF) >> 24;
  134. if (c >= 'a' && c <= 'z' && (p & 1))
  135. break;
  136. else if (c >= 'A' && c <= 'Z' && (p & 2))
  137. break;
  138. else if (c >= '0' && c <= '9' && (p & 4))
  139. break;
  140. else if (c == ' ' && (p & 8))
  141. break;
  142. else if (c == 0 && (p & 16))
  143. break;
  144. else if (c == 1 && (p & 32))
  145. break;
  146. }
  147. pd.buf[i] = c;
  148. }
  149. }
  150. flush_put_bits(&pb);
  151. probe(&pd, type, p, size);
  152. }
  153. }
  154. }
  155. }
  156. if(AV_READ_TIME())
  157. print_times();
  158. return failures;
  159. }