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