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.

240 lines
7.3KB

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