1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include "libavcodec/mpeg12.h"
20#include "libavcodec/mpeg12data.h"
21
22int main(void)
23{
24    int i;
25
26#define TEST_MATCH(frame_rate, code, ext_n, ext_d) do { \
27        AVRational fr = frame_rate; \
28        int c, n, d; \
29        ff_mpeg12_find_best_frame_rate(fr, &c, &n, &d, 0); \
30        if (c != code || n != ext_n || d != ext_d) { \
31            av_log(NULL, AV_LOG_ERROR, "Failed to match %d/%d: " \
32                   "code = %d, ext_n = %d, ext_d = %d.\n", \
33                   fr.num, fr.den, c, n, d); \
34            return 1; \
35        } \
36    } while (0)
37#define TEST_EXACT(frn, frd) do { \
38        AVRational fr = (AVRational) { frn, frd }; \
39        int c, n, d; \
40        ff_mpeg12_find_best_frame_rate(fr, &c, &n, &d, 0); \
41        if (av_cmp_q(fr, av_mul_q(ff_mpeg12_frame_rate_tab[c], \
42                                  (AVRational) { n + 1, d + 1 })) != 0) { \
43            av_log(NULL, AV_LOG_ERROR, "Failed to find exact %d/%d: " \
44                   "code = %d, ext_n = %d, ext_d = %d.\n", \
45                   fr.num, fr.den, c, n, d); \
46            return 1; \
47        } \
48    } while (0)
49
50    // Framerates in the table must be chosen exactly.
51    for (i = 1; i <= 8; i++)
52        TEST_MATCH(ff_mpeg12_frame_rate_tab[i], i, 0, 0);
53
54    // As should the same ones with small perturbations.
55    // (1/1000 used here to be smaller than half the difference
56    // between 24 and 24000/1001.)
57    for (i = 1; i <= 8; i++) {
58        TEST_MATCH(av_sub_q(ff_mpeg12_frame_rate_tab[i],
59                            (AVRational) { 1, 1000 }), i, 0, 0);
60        TEST_MATCH(av_add_q(ff_mpeg12_frame_rate_tab[i],
61                            (AVRational) { 1, 1000 }), i, 0, 0);
62    }
63
64    // Exactly constructable framerates should be exact.  Note that some
65    // values can be made in multiple ways (e.g. 12 = 24 / 2 == 60 / 5),
66    // and there is no reason to favour any particular choice.
67    TEST_EXACT(     1,    1);
68    TEST_EXACT(     2,    1);
69    TEST_EXACT(    12,    1);
70    TEST_EXACT( 15000, 1001);
71    TEST_EXACT(    15,    1);
72    TEST_EXACT(   120,    1);
73    TEST_EXACT(120000, 1001);
74    TEST_EXACT(   200,    1);
75    TEST_EXACT(   240,    1);
76
77    // Values higher than 240 (the highest representable, as 60 * 4 / 1)
78    // should be mapped to 240.
79    for (i = 240; i < 1000; i += 10)
80        TEST_MATCH(((AVRational) { i, 1 }), 8, 3, 0);
81    // Values lower than 24000/32032 (the lowest representable, as
82    // 24000/1001 * 1 / 32) should be mapped to 24000/32032.
83    for (i = 74; i > 0; i--)
84        TEST_MATCH(((AVRational) { i, 100 }), 1, 0, 31);
85
86    return 0;
87}
88