X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Fmpeg12framerate.c;h=ab3d351173bc1fffe501cae8612e2ea2bce7e1b9;hb=c17f638565c60c1763a8d5b2cdc034bda184e8e5;hp=094cd180a5f6e048123e948f493e6a22a4b93d30;hpb=6c5d6baeede1aa57bada4c019c01ab9538ed77d2;p=ffmpeg diff --git a/libavcodec/mpeg12framerate.c b/libavcodec/mpeg12framerate.c index 094cd180a5f..ab3d351173b 100644 --- a/libavcodec/mpeg12framerate.c +++ b/libavcodec/mpeg12framerate.c @@ -18,6 +18,9 @@ #include "libavutil/rational.h" +#include "mpeg12.h" +#include "mpeg12data.h" + const AVRational ff_mpeg12_frame_rate_tab[16] = { { 0, 0}, {24000, 1001}, @@ -37,3 +40,64 @@ const AVRational ff_mpeg12_frame_rate_tab[16] = { { 15, 1}, { 0, 0}, }; + +void ff_mpeg12_find_best_frame_rate(AVRational frame_rate, + int *code, int *ext_n, int *ext_d, + int nonstandard) +{ + int mpeg2 = ext_n && ext_d; + int max_code = nonstandard ? 12 : 8; + int c, n, d, best_c, best_n, best_d; + AVRational best_error = { INT_MAX, 1 }; + + // Default to NTSC if the inputs make no sense. + best_c = 4; + best_n = best_d = 1; + + for (c = 1; c <= max_code; c++) { + if (av_cmp_q(frame_rate, ff_mpeg12_frame_rate_tab[c]) == 0) { + best_c = c; + goto found; + } + } + + for (c = 1; c <= max_code; c++) { + for (n = 1; n <= (mpeg2 ? 4 : 1); n++) { + for (d = 1; d <= (mpeg2 ? 32 : 1); d++) { + AVRational test, error; + int cmp; + + test = av_mul_q(ff_mpeg12_frame_rate_tab[c], + (AVRational) { n, d }); + + cmp = av_cmp_q(test, frame_rate); + if (cmp == 0) { + best_c = c; + best_n = n; + best_d = d; + goto found; + } + + if (cmp < 0) + error = av_div_q(frame_rate, test); + else + error = av_div_q(test, frame_rate); + + cmp = av_cmp_q(error, best_error); + if (cmp < 0 || (cmp == 0 && n == 1 && d == 1)) { + best_c = c; + best_n = n; + best_d = d; + best_error = error; + } + } + } + } + +found: + *code = best_c; + if (mpeg2) { + *ext_n = best_n - 1; + *ext_d = best_d - 1; + } +}