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
29static int score_array[MAX_FORMATS];
30static int64_t time_array[MAX_FORMATS];
31static int failures = 0;
32
33#ifndef AV_READ_TIME
34#define AV_READ_TIME(x) 0
35#endif
36
37static 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
62static 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
78int 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        if (!pd.buf) {
114            fprintf(stderr, "out of memory\n");
115            return 1;
116        }
117
118        memset(pd.buf, 0, size + AVPROBE_PADDING_SIZE);
119
120        fprintf(stderr, "testing size=%d\n", size);
121
122        for (retry = 0; retry < retry_count; retry += FFMAX(size, 32)) {
123            for (type = 0; type < 4; type++) {
124                for (p = 0; p < 4096; p++) {
125                    unsigned hist = 0;
126                    init_put_bits(&pb, pd.buf, size);
127                    switch (type) {
128                    case 0:
129                        for (i = 0; i < size * 8; i++)
130                            put_bits(&pb, 1, (av_lfg_get(&state) & 0xFFFFFFFF) > p << 20);
131                        break;
132                    case 1:
133                        for (i = 0; i < size * 8; i++) {
134                            unsigned int p2 = hist ? p & 0x3F : (p >> 6);
135                            unsigned int v  = (av_lfg_get(&state) & 0xFFFFFFFF) > p2 << 26;
136                            put_bits(&pb, 1, v);
137                            hist = v;
138                        }
139                        break;
140                    case 2:
141                        for (i = 0; i < size * 8; i++) {
142                            unsigned int p2 = (p >> (hist * 3)) & 7;
143                            unsigned int v  = (av_lfg_get(&state) & 0xFFFFFFFF) > p2 << 29;
144                            put_bits(&pb, 1, v);
145                            hist = (2 * hist + v) & 3;
146                        }
147                        break;
148                    case 3:
149                        for (i = 0; i < size; i++) {
150                            int c = 0;
151                            while (p & 63) {
152                                c = (av_lfg_get(&state) & 0xFFFFFFFF) >> 24;
153                                if (c >= 'a' && c <= 'z' && (p & 1))
154                                    break;
155                                else if (c >= 'A' && c <= 'Z' && (p & 2))
156                                    break;
157                                else if (c >= '0' && c <= '9' && (p & 4))
158                                    break;
159                                else if (c == ' ' && (p & 8))
160                                    break;
161                                else if (c == 0 && (p & 16))
162                                    break;
163                                else if (c == 1 && (p & 32))
164                                    break;
165                            }
166                            pd.buf[i] = c;
167                        }
168                    }
169                    flush_put_bits(&pb);
170                    probe(&pd, type, p, size);
171                }
172            }
173        }
174    }
175    if(AV_READ_TIME())
176        print_times();
177    return failures;
178}
179