probetest.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 "libavformat/demux.h"
  23. #include "libavcodec/put_bits.h"
  24. #include "libavutil/lfg.h"
  25. #include "libavutil/mem.h"
  26. #include "libavutil/timer.h"
  27. #define MAX_FORMATS 1000 //this must be larger than the number of formats
  28. static int score_array[MAX_FORMATS];
  29. static int64_t time_array[MAX_FORMATS];
  30. static int failures = 0;
  31. static const char *single_format;
  32. #ifndef AV_READ_TIME
  33. #define AV_READ_TIME(x) 0
  34. #endif
  35. static void probe(AVProbeData *pd, int type, int p, int size)
  36. {
  37. int i = 0;
  38. const AVInputFormat *fmt = NULL;
  39. void *fmt_opaque = NULL;
  40. while ((fmt = av_demuxer_iterate(&fmt_opaque))) {
  41. if (fmt->flags & AVFMT_NOFILE)
  42. continue;
  43. if (ffifmt(fmt)->read_probe &&
  44. (!single_format || !strcmp(single_format, fmt->name))
  45. ) {
  46. int score;
  47. int64_t start = AV_READ_TIME();
  48. score = ffifmt(fmt)->read_probe(pd);
  49. time_array[i] += AV_READ_TIME() - start;
  50. if (score > score_array[i] && score > AVPROBE_SCORE_MAX / 4) {
  51. score_array[i] = score;
  52. fprintf(stderr,
  53. "Failure of %s probing code with score=%d type=%d p=%X size=%d\n",
  54. fmt->name, score, type, p, size);
  55. failures++;
  56. }
  57. }
  58. i++;
  59. }
  60. }
  61. static void print_times(void)
  62. {
  63. int i = 0;
  64. const AVInputFormat *fmt = NULL;
  65. void *fmt_opaque = NULL;
  66. while ((fmt = av_demuxer_iterate(&fmt_opaque))) {
  67. if (fmt->flags & AVFMT_NOFILE)
  68. continue;
  69. if (time_array[i] > 1000000) {
  70. fprintf(stderr, "%12"PRIu64" cycles, %12s\n",
  71. time_array[i], fmt->name);
  72. }
  73. i++;
  74. }
  75. }
  76. static int read_int(char *arg) {
  77. int ret;
  78. if (!arg || !*arg)
  79. return -1;
  80. ret = strtol(arg, &arg, 0);
  81. if (*arg)
  82. return -1;
  83. return ret;
  84. }
  85. int main(int argc, char **argv)
  86. {
  87. unsigned int p, i, type, size, retry;
  88. AVProbeData pd = { 0 };
  89. AVLFG state;
  90. PutBitContext pb;
  91. int retry_count= 4097;
  92. int max_size = 65537;
  93. int j;
  94. for (j = i = 1; i<argc; i++) {
  95. if (!strcmp(argv[i], "-f") && i+1<argc && !single_format) {
  96. single_format = argv[++i];
  97. } else if (read_int(argv[i])>0 && j == 1) {
  98. retry_count = read_int(argv[i]);
  99. j++;
  100. } else if (read_int(argv[i])>0 && j == 2) {
  101. max_size = read_int(argv[i]);
  102. j++;
  103. } else {
  104. fprintf(stderr, "probetest [-f <input format>] [<retry_count> [<max_size>]]\n");
  105. return 1;
  106. }
  107. }
  108. if (max_size > 1000000000U/8) {
  109. fprintf(stderr, "max_size out of bounds\n");
  110. return 1;
  111. }
  112. if (retry_count > 1000000000U) {
  113. fprintf(stderr, "retry_count out of bounds\n");
  114. return 1;
  115. }
  116. av_lfg_init(&state, 0xdeadbeef);
  117. pd.buf = NULL;
  118. for (size = 1; size < max_size; size *= 2) {
  119. pd.buf_size = size;
  120. pd.buf = av_realloc(pd.buf, size + AVPROBE_PADDING_SIZE);
  121. pd.filename = "";
  122. if (!pd.buf) {
  123. fprintf(stderr, "out of memory\n");
  124. return 1;
  125. }
  126. memset(pd.buf, 0, size + AVPROBE_PADDING_SIZE);
  127. fprintf(stderr, "testing size=%d\n", size);
  128. for (retry = 0; retry < retry_count; retry += FFMAX(size, 32)) {
  129. for (type = 0; type < 4; type++) {
  130. for (p = 0; p < 4096; p++) {
  131. unsigned hist = 0;
  132. init_put_bits(&pb, pd.buf, size);
  133. switch (type) {
  134. case 0:
  135. for (i = 0; i < size * 8; i++)
  136. put_bits(&pb, 1, (av_lfg_get(&state) & 0xFFFFFFFF) > p << 20);
  137. break;
  138. case 1:
  139. for (i = 0; i < size * 8; i++) {
  140. unsigned int p2 = hist ? p & 0x3F : (p >> 6);
  141. unsigned int v = (av_lfg_get(&state) & 0xFFFFFFFF) > p2 << 26;
  142. put_bits(&pb, 1, v);
  143. hist = v;
  144. }
  145. break;
  146. case 2:
  147. for (i = 0; i < size * 8; i++) {
  148. unsigned int p2 = (p >> (hist * 3)) & 7;
  149. unsigned int v = (av_lfg_get(&state) & 0xFFFFFFFF) > p2 << 29;
  150. put_bits(&pb, 1, v);
  151. hist = (2 * hist + v) & 3;
  152. }
  153. break;
  154. case 3:
  155. for (i = 0; i < size; i++) {
  156. int c = 0;
  157. while (p & 63) {
  158. c = (av_lfg_get(&state) & 0xFFFFFFFF) >> 24;
  159. if (c >= 'a' && c <= 'z' && (p & 1))
  160. break;
  161. else if (c >= 'A' && c <= 'Z' && (p & 2))
  162. break;
  163. else if (c >= '0' && c <= '9' && (p & 4))
  164. break;
  165. else if (c == ' ' && (p & 8))
  166. break;
  167. else if (c == 0 && (p & 16))
  168. break;
  169. else if (c == 1 && (p & 32))
  170. break;
  171. }
  172. pd.buf[i] = c;
  173. }
  174. }
  175. flush_put_bits(&pb);
  176. probe(&pd, type, p, size);
  177. }
  178. }
  179. }
  180. }
  181. if(AV_READ_TIME())
  182. print_times();
  183. return failures;
  184. }