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.

264 lines
8.0KB

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