mpeg12framerate.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #include "libavutil/rational.h"
  19. #include "mpeg12.h"
  20. #include "mpeg12data.h"
  21. const AVRational ff_mpeg12_frame_rate_tab[16] = {
  22. { 0, 0},
  23. {24000, 1001},
  24. { 24, 1},
  25. { 25, 1},
  26. {30000, 1001},
  27. { 30, 1},
  28. { 50, 1},
  29. {60000, 1001},
  30. { 60, 1},
  31. // Xing's 15fps: (9)
  32. { 15, 1},
  33. // libmpeg3's "Unofficial economy rates": (10-13)
  34. { 5, 1},
  35. { 10, 1},
  36. { 12, 1},
  37. { 15, 1},
  38. { 0, 0},
  39. };
  40. void ff_mpeg12_find_best_frame_rate(AVRational frame_rate,
  41. int *code, int *ext_n, int *ext_d,
  42. int nonstandard)
  43. {
  44. int mpeg2 = ext_n && ext_d;
  45. int max_code = nonstandard ? 12 : 8;
  46. int c, n, d, best_c, best_n, best_d;
  47. AVRational best_error = { INT_MAX, 1 };
  48. // Default to NTSC if the inputs make no sense.
  49. best_c = 4;
  50. best_n = best_d = 1;
  51. for (c = 1; c <= max_code; c++) {
  52. if (av_cmp_q(frame_rate, ff_mpeg12_frame_rate_tab[c]) == 0) {
  53. best_c = c;
  54. goto found;
  55. }
  56. }
  57. for (c = 1; c <= max_code; c++) {
  58. for (n = 1; n <= (mpeg2 ? 4 : 1); n++) {
  59. for (d = 1; d <= (mpeg2 ? 32 : 1); d++) {
  60. AVRational test, error;
  61. int cmp;
  62. test = av_mul_q(ff_mpeg12_frame_rate_tab[c],
  63. (AVRational) { n, d });
  64. cmp = av_cmp_q(test, frame_rate);
  65. if (cmp == 0) {
  66. best_c = c;
  67. best_n = n;
  68. best_d = d;
  69. goto found;
  70. }
  71. if (cmp < 0)
  72. error = av_div_q(frame_rate, test);
  73. else
  74. error = av_div_q(test, frame_rate);
  75. cmp = av_cmp_q(error, best_error);
  76. if (cmp < 0 || (cmp == 0 && n == 1 && d == 1)) {
  77. best_c = c;
  78. best_n = n;
  79. best_d = d;
  80. best_error = error;
  81. }
  82. }
  83. }
  84. }
  85. found:
  86. *code = best_c;
  87. if (mpeg2) {
  88. *ext_n = best_n - 1;
  89. *ext_d = best_d - 1;
  90. }
  91. }