You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
4.8KB

  1. /*
  2. * Copyright (C) 2006 Smartjog S.A.S, Baptiste Coudurier <baptiste.coudurier@gmail.com>
  3. * Copyright (C) 2011 Smartjog S.A.S, Clément Bœsch <clement.boesch@smartjog.com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Timecode helpers
  24. */
  25. #include <stdio.h>
  26. #include "timecode.h"
  27. #include "libavutil/log.h"
  28. int avpriv_framenum_to_drop_timecode(int frame_num)
  29. {
  30. /* only works for NTSC 29.97 */
  31. int d = frame_num / 17982;
  32. int m = frame_num % 17982;
  33. //if (m < 2) m += 2; /* not needed since -2,-1 / 1798 in C returns 0 */
  34. return frame_num + 18 * d + 2 * ((m - 2) / 1798);
  35. }
  36. uint32_t avpriv_framenum_to_smpte_timecode(unsigned frame, int fps, int drop)
  37. {
  38. return (0 << 31) | // color frame flag
  39. (drop << 30) | // drop frame flag
  40. ( ((frame % fps) / 10) << 28) | // tens of frames
  41. ( ((frame % fps) % 10) << 24) | // units of frames
  42. (0 << 23) | // field phase (NTSC), b0 (PAL)
  43. ((((frame / fps) % 60) / 10) << 20) | // tens of seconds
  44. ((((frame / fps) % 60) % 10) << 16) | // units of seconds
  45. (0 << 15) | // b0 (NTSC), b2 (PAL)
  46. ((((frame / (fps * 60)) % 60) / 10) << 12) | // tens of minutes
  47. ((((frame / (fps * 60)) % 60) % 10) << 8) | // units of minutes
  48. (0 << 7) | // b1
  49. (0 << 6) | // b2 (NTSC), field phase (PAL)
  50. ((((frame / (fps * 3600) % 24)) / 10) << 4) | // tens of hours
  51. ( (frame / (fps * 3600) % 24)) % 10; // units of hours
  52. }
  53. int avpriv_check_timecode_rate(void *avcl, AVRational rate, int drop)
  54. {
  55. int fps;
  56. if (!rate.num || !rate.den) {
  57. av_log(avcl, AV_LOG_ERROR, "Timecode frame rate must be specified\n");
  58. return -1;
  59. }
  60. fps = (rate.num + rate.den/2) / rate.den;
  61. if (drop && fps != 30) {
  62. av_log(avcl, AV_LOG_ERROR, "Drop frame is only allowed with 30000/1001 FPS\n");
  63. return -2;
  64. }
  65. switch (fps) {
  66. case 24:
  67. case 25:
  68. case 30: return 0;
  69. default:
  70. av_log(avcl, AV_LOG_ERROR, "Timecode frame rate not supported\n");
  71. return -3;
  72. }
  73. }
  74. char *avpriv_timecode_to_string(char *buf, const struct ff_timecode *tc, unsigned frame)
  75. {
  76. int frame_num = tc->start + frame;
  77. int fps = (tc->rate.num + tc->rate.den/2) / tc->rate.den;
  78. int hh, mm, ss, ff, neg = 0;
  79. if (tc->drop)
  80. frame_num = avpriv_framenum_to_drop_timecode(frame_num);
  81. if (frame_num < 0) {
  82. frame_num = -frame_num;
  83. neg = 1;
  84. }
  85. ff = frame_num % fps;
  86. ss = frame_num / fps % 60;
  87. mm = frame_num / (fps*60) % 60;
  88. hh = frame_num / (fps*3600);
  89. snprintf(buf, 16, "%s%02d:%02d:%02d%c%02d",
  90. neg ? "-" : "",
  91. hh, mm, ss, tc->drop ? ';' : ':', ff);
  92. return buf;
  93. }
  94. int avpriv_init_smpte_timecode(void *avcl, struct ff_timecode *tc)
  95. {
  96. int hh, mm, ss, ff, fps, ret;
  97. char c;
  98. if (sscanf(tc->str, "%d:%d:%d%c%d", &hh, &mm, &ss, &c, &ff) != 5) {
  99. av_log(avcl, AV_LOG_ERROR, "unable to parse timecode, "
  100. "syntax: hh:mm:ss[:;.]ff\n");
  101. return -1;
  102. }
  103. tc->drop = c != ':'; // drop if ';', '.', ...
  104. ret = avpriv_check_timecode_rate(avcl, tc->rate, tc->drop);
  105. if (ret < 0)
  106. return ret;
  107. fps = (tc->rate.num + tc->rate.den/2) / tc->rate.den;
  108. tc->start = (hh*3600 + mm*60 + ss) * fps + ff;
  109. if (tc->drop) { /* adjust frame number */
  110. int tmins = 60*hh + mm;
  111. tc->start -= 2 * (tmins - tmins/10);
  112. }
  113. return 0;
  114. }
  115. #if FF_API_OLD_TIMECODE
  116. int ff_framenum_to_drop_timecode(int frame_num)
  117. {
  118. return avpriv_framenum_to_drop_timecode(frame_num);
  119. }
  120. uint32_t ff_framenum_to_smtpe_timecode(unsigned frame, int fps, int drop)
  121. {
  122. return avpriv_framenum_to_smpte_timecode(frame, fps, drop);
  123. }
  124. int ff_init_smtpe_timecode(void *avcl, struct ff_timecode *tc)
  125. {
  126. return avpriv_init_smpte_timecode(avcl, tc);
  127. }
  128. #endif