FFmpeg
mpeg12framerate.c
Go to the documentation of this file.
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 <stddef.h>
20 
21 #include "libavutil/log.h"
22 #include "libavcodec/mpeg12.h"
23 #include "libavcodec/mpeg12data.h"
24 
25 int main(void)
26 {
27  int i;
28 
29 #define TEST_MATCH(frame_rate, code, ext_n, ext_d) do { \
30  AVRational fr = frame_rate; \
31  int c, n, d; \
32  ff_mpeg12_find_best_frame_rate(fr, &c, &n, &d, 0); \
33  if (c != code || n != ext_n || d != ext_d) { \
34  av_log(NULL, AV_LOG_ERROR, "Failed to match %d/%d: " \
35  "code = %d, ext_n = %d, ext_d = %d.\n", \
36  fr.num, fr.den, c, n, d); \
37  return 1; \
38  } \
39  } while (0)
40 #define TEST_EXACT(frn, frd) do { \
41  AVRational fr = (AVRational) { frn, frd }; \
42  int c, n, d; \
43  ff_mpeg12_find_best_frame_rate(fr, &c, &n, &d, 0); \
44  if (av_cmp_q(fr, av_mul_q(ff_mpeg12_frame_rate_tab[c], \
45  (AVRational) { n + 1, d + 1 })) != 0) { \
46  av_log(NULL, AV_LOG_ERROR, "Failed to find exact %d/%d: " \
47  "code = %d, ext_n = %d, ext_d = %d.\n", \
48  fr.num, fr.den, c, n, d); \
49  return 1; \
50  } \
51  } while (0)
52 
53  // Framerates in the table must be chosen exactly.
54  for (i = 1; i <= 8; i++)
56 
57  // As should the same ones with small perturbations.
58  // (1/1000 used here to be smaller than half the difference
59  // between 24 and 24000/1001.)
60  for (i = 1; i <= 8; i++) {
62  (AVRational) { 1, 1000 }), i, 0, 0);
64  (AVRational) { 1, 1000 }), i, 0, 0);
65  }
66 
67  // Exactly constructable framerates should be exact. Note that some
68  // values can be made in multiple ways (e.g. 12 = 24 / 2 == 60 / 5),
69  // and there is no reason to favour any particular choice.
70  TEST_EXACT( 1, 1);
71  TEST_EXACT( 2, 1);
72  TEST_EXACT( 12, 1);
73  TEST_EXACT( 15000, 1001);
74  TEST_EXACT( 15, 1);
75  TEST_EXACT( 120, 1);
76  TEST_EXACT(120000, 1001);
77  TEST_EXACT( 200, 1);
78  TEST_EXACT( 240, 1);
79 
80  // Values higher than 240 (the highest representable, as 60 * 4 / 1)
81  // should be mapped to 240.
82  for (i = 240; i < 1000; i += 10)
83  TEST_MATCH(((AVRational) { i, 1 }), 8, 3, 0);
84  // Values lower than 24000/32032 (the lowest representable, as
85  // 24000/1001 * 1 / 32) should be mapped to 24000/32032.
86  for (i = 74; i > 0; i--)
87  TEST_MATCH(((AVRational) { i, 100 }), 1, 0, 31);
88 
89  return 0;
90 }
av_sub_q
AVRational av_sub_q(AVRational b, AVRational c)
Subtract one rational from another.
Definition: rational.c:101
mpeg12.h
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
TEST_EXACT
#define TEST_EXACT(frn, frd)
TEST_MATCH
#define TEST_MATCH(frame_rate, code, ext_n, ext_d)
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
mpeg12data.h
main
int main(void)
Definition: mpeg12framerate.c:25
av_add_q
AVRational av_add_q(AVRational b, AVRational c)
Add two rationals.
Definition: rational.c:93
ff_mpeg12_frame_rate_tab
const AVRational ff_mpeg12_frame_rate_tab[16]
Definition: mpeg12framerate.c:24