timecode.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (c) 2006 Smartjog S.A.S, Baptiste Coudurier <baptiste.coudurier@gmail.com>
  3. * Copyright (c) 2011-2012 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. * @see https://en.wikipedia.org/wiki/SMPTE_time_code
  25. * @see http://www.dropframetimecode.org
  26. */
  27. #include <stdio.h>
  28. #include "timecode.h"
  29. #include "log.h"
  30. #include "error.h"
  31. int av_timecode_adjust_ntsc_framenum(int framenum)
  32. {
  33. /* only works for NTSC 29.97 */
  34. int d = framenum / 17982;
  35. int m = framenum % 17982;
  36. //if (m < 2) m += 2; /* not needed since -2,-1 / 1798 in C returns 0 */
  37. return framenum + 18 * d + 2 * ((m - 2) / 1798);
  38. }
  39. uint32_t av_timecode_get_smpte_from_framenum(const AVTimecode *tc, int framenum)
  40. {
  41. unsigned fps = tc->fps;
  42. int drop = !!(tc->flags & AV_TIMECODE_FLAG_DROPFRAME);
  43. int hh, mm, ss, ff;
  44. framenum += tc->start;
  45. if (drop)
  46. framenum = av_timecode_adjust_ntsc_framenum(framenum);
  47. ff = framenum % fps;
  48. ss = framenum / fps % 60;
  49. mm = framenum / (fps*60) % 60;
  50. hh = framenum / (fps*3600) % 24;
  51. return 0 << 31 | // color frame flag (0: unsync mode, 1: sync mode)
  52. drop << 30 | // drop frame flag (0: non drop, 1: drop)
  53. (ff / 10) << 28 | // tens of frames
  54. (ff % 10) << 24 | // units of frames
  55. 0 << 23 | // PC (NTSC) or BGF0 (PAL)
  56. (ss / 10) << 20 | // tens of seconds
  57. (ss % 10) << 16 | // units of seconds
  58. 0 << 15 | // BGF0 (NTSC) or BGF2 (PAL)
  59. (mm / 10) << 12 | // tens of minutes
  60. (mm % 10) << 8 | // units of minutes
  61. 0 << 7 | // BGF2 (NTSC) or PC (PAL)
  62. 0 << 6 | // BGF1
  63. (hh / 10) << 4 | // tens of hours
  64. (hh % 10); // units of hours
  65. }
  66. char *av_timecode_make_string(const AVTimecode *tc, char *buf, int framenum)
  67. {
  68. int fps = tc->fps;
  69. int drop = tc->flags & AV_TIMECODE_FLAG_DROPFRAME;
  70. int hh, mm, ss, ff, neg = 0;
  71. framenum += tc->start;
  72. if (drop)
  73. framenum = av_timecode_adjust_ntsc_framenum(framenum);
  74. if (framenum < 0) {
  75. framenum = -framenum;
  76. neg = tc->flags & AV_TIMECODE_FLAG_ALLOWNEGATIVE;
  77. }
  78. ff = framenum % fps;
  79. ss = framenum / fps % 60;
  80. mm = framenum / (fps*60) % 60;
  81. hh = framenum / (fps*3600);
  82. if (tc->flags & AV_TIMECODE_FLAG_24HOURSMAX)
  83. hh = hh % 24;
  84. snprintf(buf, AV_TIMECODE_STR_SIZE, "%s%02d:%02d:%02d%c%02d",
  85. neg ? "-" : "",
  86. hh, mm, ss, drop ? ';' : ':', ff);
  87. return buf;
  88. }
  89. static unsigned bcd2uint(uint8_t bcd)
  90. {
  91. unsigned low = bcd & 0xf;
  92. unsigned high = bcd >> 4;
  93. if (low > 9 || high > 9)
  94. return 0;
  95. return low + 10*high;
  96. }
  97. char *av_timecode_make_smpte_tc_string(char *buf, uint32_t tcsmpte, int prevent_df)
  98. {
  99. unsigned hh = bcd2uint(tcsmpte & 0x3f); // 6-bit hours
  100. unsigned mm = bcd2uint(tcsmpte>>8 & 0x7f); // 7-bit minutes
  101. unsigned ss = bcd2uint(tcsmpte>>16 & 0x7f); // 7-bit seconds
  102. unsigned ff = bcd2uint(tcsmpte>>24 & 0x3f); // 6-bit frames
  103. unsigned drop = tcsmpte & 1<<30 && !prevent_df; // 1-bit drop if not arbitrary bit
  104. snprintf(buf, AV_TIMECODE_STR_SIZE, "%02u:%02u:%02u%c%02u",
  105. hh, mm, ss, drop ? ';' : ':', ff);
  106. return buf;
  107. }
  108. char *av_timecode_make_mpeg_tc_string(char *buf, uint32_t tc25bit)
  109. {
  110. snprintf(buf, AV_TIMECODE_STR_SIZE, "%02u:%02u:%02u%c%02u",
  111. tc25bit>>19 & 0x1f, // 5-bit hours
  112. tc25bit>>13 & 0x3f, // 6-bit minutes
  113. tc25bit>>6 & 0x3f, // 6-bit seconds
  114. tc25bit & 1<<24 ? ';' : ':', // 1-bit drop flag
  115. tc25bit & 0x3f); // 6-bit frames
  116. return buf;
  117. }
  118. static int check_timecode(void *log_ctx, AVTimecode *tc)
  119. {
  120. if (tc->fps <= 0) {
  121. av_log(log_ctx, AV_LOG_ERROR, "Timecode frame rate must be specified\n");
  122. return AVERROR(EINVAL);
  123. }
  124. if ((tc->flags & AV_TIMECODE_FLAG_DROPFRAME) && tc->fps != 30) {
  125. av_log(log_ctx, AV_LOG_ERROR, "Drop frame is only allowed with 30000/1001 FPS\n");
  126. return AVERROR(EINVAL);
  127. }
  128. switch (tc->fps) {
  129. case 24:
  130. case 25:
  131. case 30: return 0;
  132. default:
  133. av_log(log_ctx, AV_LOG_ERROR, "Timecode frame rate not supported\n");
  134. return AVERROR_PATCHWELCOME;
  135. }
  136. }
  137. static int fps_from_frame_rate(AVRational rate)
  138. {
  139. if (!rate.den || !rate.num)
  140. return -1;
  141. return (rate.num + rate.den/2) / rate.den;
  142. }
  143. int av_timecode_init(AVTimecode *tc, AVRational rate, int flags, int frame_start, void *log_ctx)
  144. {
  145. memset(tc, 0, sizeof(*tc));
  146. tc->start = frame_start;
  147. tc->flags = flags;
  148. tc->rate = rate;
  149. tc->fps = fps_from_frame_rate(rate);
  150. return check_timecode(log_ctx, tc);
  151. }
  152. int av_timecode_init_from_string(AVTimecode *tc, AVRational rate, const char *str, void *log_ctx)
  153. {
  154. char c;
  155. int hh, mm, ss, ff, ret;
  156. if (sscanf(str, "%d:%d:%d%c%d", &hh, &mm, &ss, &c, &ff) != 5) {
  157. av_log(log_ctx, AV_LOG_ERROR, "Unable to parse timecode, "
  158. "syntax: hh:mm:ss[:;.]ff\n");
  159. return AVERROR_INVALIDDATA;
  160. }
  161. memset(tc, 0, sizeof(*tc));
  162. tc->flags = c != ':' ? AV_TIMECODE_FLAG_DROPFRAME : 0; // drop if ';', '.', ...
  163. tc->rate = rate;
  164. tc->fps = fps_from_frame_rate(rate);
  165. ret = check_timecode(log_ctx, tc);
  166. if (ret < 0)
  167. return ret;
  168. tc->start = (hh*3600 + mm*60 + ss) * tc->fps + ff;
  169. if (tc->flags & AV_TIMECODE_FLAG_DROPFRAME) { /* adjust frame number */
  170. int tmins = 60*hh + mm;
  171. tc->start -= 2 * (tmins - tmins/10);
  172. }
  173. return 0;
  174. }