timecode.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 header
  24. */
  25. #ifndef AVUTIL_TIMECODE_H
  26. #define AVUTIL_TIMECODE_H
  27. #include <stdint.h>
  28. #include "rational.h"
  29. #define AV_TIMECODE_STR_SIZE 23
  30. enum AVTimecodeFlag {
  31. AV_TIMECODE_FLAG_DROPFRAME = 1<<0, ///< timecode is drop frame
  32. AV_TIMECODE_FLAG_24HOURSMAX = 1<<1, ///< timecode wraps after 24 hours
  33. AV_TIMECODE_FLAG_ALLOWNEGATIVE = 1<<2, ///< negative time values are allowed
  34. };
  35. typedef struct {
  36. int start; ///< timecode frame start (first base frame number)
  37. uint32_t flags; ///< flags such as drop frame, +24 hours support, ...
  38. AVRational rate; ///< frame rate in rational form
  39. unsigned fps; ///< frame per second; must be consistent with the rate field
  40. } AVTimecode;
  41. /**
  42. * Adjust frame number for NTSC drop frame time code.
  43. *
  44. * @param framenum frame number to adjust
  45. * @param fps frame per second, 30 or 60
  46. * @return adjusted frame number
  47. * @warning adjustment is only valid in NTSC 29.97 and 59.94
  48. */
  49. int av_timecode_adjust_ntsc_framenum2(int framenum, int fps);
  50. /**
  51. * Convert frame number to SMPTE 12M binary representation.
  52. *
  53. * @param tc timecode data correctly initialized
  54. * @param framenum frame number
  55. * @return the SMPTE binary representation
  56. *
  57. * See SMPTE ST 314M-2005 Sec 4.4.2.2.1 "Time code pack (TC)"
  58. * the format description as follows:
  59. * bits 0-5: hours, in BCD(6bits)
  60. * bits 6: BGF1
  61. * bits 7: BGF2 (NTSC) or PC (PAL)
  62. * bits 8-14: minutes, in BCD(7bits)
  63. * bits 15: BGF0 (NTSC) or BGF2 (PAL)
  64. * bits 16-22: seconds, in BCD(7bits)
  65. * bits 23: PC (NTSC) or BGF0 (PAL)
  66. * bits 24-29: frames, in BCD(6bits)
  67. * bits 30: drop frame flag (0: non drop, 1: drop)
  68. * bits 31: color frame flag (0: unsync mode, 1: sync mode)
  69. * @note BCD numbers (6 or 7 bits): 4 or 5 lower bits for units, 2 higher bits for tens.
  70. * @note Frame number adjustment is automatically done in case of drop timecode,
  71. * you do NOT have to call av_timecode_adjust_ntsc_framenum2().
  72. * @note The frame number is relative to tc->start.
  73. * @note Color frame (CF), binary group flags (BGF) and biphase mark polarity
  74. * correction (PC) bits are set to zero.
  75. */
  76. uint32_t av_timecode_get_smpte_from_framenum(const AVTimecode *tc, int framenum);
  77. /**
  78. * Convert sei info to SMPTE 12M binary representation.
  79. *
  80. * @param rate frame rate in rational form
  81. * @param drop drop flag
  82. * @param hh hour
  83. * @param mm minute
  84. * @param ss second
  85. * @param ff frame number
  86. * @return the SMPTE binary representation
  87. */
  88. uint32_t av_timecode_get_smpte(AVRational rate, int drop, int hh, int mm, int ss, int ff);
  89. /**
  90. * Load timecode string in buf.
  91. *
  92. * @param buf destination buffer, must be at least AV_TIMECODE_STR_SIZE long
  93. * @param tc timecode data correctly initialized
  94. * @param framenum frame number
  95. * @return the buf parameter
  96. *
  97. * @note Timecode representation can be a negative timecode and have more than
  98. * 24 hours, but will only be honored if the flags are correctly set.
  99. * @note The frame number is relative to tc->start.
  100. */
  101. char *av_timecode_make_string(const AVTimecode *tc, char *buf, int framenum);
  102. /**
  103. * Get the timecode string from the SMPTE timecode format.
  104. *
  105. * @param buf destination buffer, must be at least AV_TIMECODE_STR_SIZE long
  106. * @param tcsmpte the 32-bit SMPTE timecode
  107. * @param prevent_df prevent the use of a drop flag when it is known the DF bit
  108. * is arbitrary
  109. * @return the buf parameter
  110. */
  111. char *av_timecode_make_smpte_tc_string(char *buf, uint32_t tcsmpte, int prevent_df);
  112. /**
  113. * Get the timecode string from the 25-bit timecode format (MPEG GOP format).
  114. *
  115. * @param buf destination buffer, must be at least AV_TIMECODE_STR_SIZE long
  116. * @param tc25bit the 25-bits timecode
  117. * @return the buf parameter
  118. */
  119. char *av_timecode_make_mpeg_tc_string(char *buf, uint32_t tc25bit);
  120. /**
  121. * Init a timecode struct with the passed parameters.
  122. *
  123. * @param log_ctx a pointer to an arbitrary struct of which the first field
  124. * is a pointer to an AVClass struct (used for av_log)
  125. * @param tc pointer to an allocated AVTimecode
  126. * @param rate frame rate in rational form
  127. * @param flags miscellaneous flags such as drop frame, +24 hours, ...
  128. * (see AVTimecodeFlag)
  129. * @param frame_start the first frame number
  130. * @return 0 on success, AVERROR otherwise
  131. */
  132. int av_timecode_init(AVTimecode *tc, AVRational rate, int flags, int frame_start, void *log_ctx);
  133. /**
  134. * Parse timecode representation (hh:mm:ss[:;.]ff).
  135. *
  136. * @param log_ctx a pointer to an arbitrary struct of which the first field is a
  137. * pointer to an AVClass struct (used for av_log).
  138. * @param tc pointer to an allocated AVTimecode
  139. * @param rate frame rate in rational form
  140. * @param str timecode string which will determine the frame start
  141. * @return 0 on success, AVERROR otherwise
  142. */
  143. int av_timecode_init_from_string(AVTimecode *tc, AVRational rate, const char *str, void *log_ctx);
  144. /**
  145. * Check if the timecode feature is available for the given frame rate
  146. *
  147. * @return 0 if supported, <0 otherwise
  148. */
  149. int av_timecode_check_frame_rate(AVRational rate);
  150. #endif /* AVUTIL_TIMECODE_H */