2 * This file is part of FFmpeg.
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.
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.
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
19 #include "libavutil/rational.h"
22 #include "mpeg12data.h"
24 const AVRational ff_mpeg12_frame_rate_tab[16] = {
36 // libmpeg3's "Unofficial economy rates": (10-13)
44 void ff_mpeg12_find_best_frame_rate(AVRational frame_rate,
45 int *code, int *ext_n, int *ext_d,
48 int mpeg2 = ext_n && ext_d;
49 int max_code = nonstandard ? 12 : 8;
50 int c, n, d, best_c, best_n, best_d;
51 AVRational best_error = { INT_MAX, 1 };
53 // Default to NTSC if the inputs make no sense.
57 for (c = 1; c <= max_code; c++) {
58 if (av_cmp_q(frame_rate, ff_mpeg12_frame_rate_tab[c]) == 0) {
64 for (c = 1; c <= max_code; c++) {
65 for (n = 1; n <= (mpeg2 ? 4 : 1); n++) {
66 for (d = 1; d <= (mpeg2 ? 32 : 1); d++) {
67 AVRational test, error;
70 test = av_mul_q(ff_mpeg12_frame_rate_tab[c],
71 (AVRational) { n, d });
73 cmp = av_cmp_q(test, frame_rate);
82 error = av_div_q(frame_rate, test);
84 error = av_div_q(test, frame_rate);
86 cmp = av_cmp_q(error, best_error);
87 if (cmp < 0 || (cmp == 0 && n == 1 && d == 1)) {