probetest.c 6.5 KB

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