timecode.c 7.3 KB

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