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.

259 lines
7.8KB

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