timecode.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 16
  30. #define AV_TIMECODE_OPTION(ctx, string_field, flags) \
  31. "timecode", "set timecode value following hh:mm:ss[:;.]ff format, " \
  32. "use ';' or '.' before frame number for drop frame", \
  33. offsetof(ctx, string_field), \
  34. AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, flags
  35. enum AVTimecodeFlag {
  36. AV_TIMECODE_FLAG_DROPFRAME = 1<<0, ///< timecode is drop frame
  37. AV_TIMECODE_FLAG_24HOURSMAX = 1<<1, ///< timecode wraps after 24 hours
  38. AV_TIMECODE_FLAG_ALLOWNEGATIVE = 1<<2, ///< negative time values are allowed
  39. };
  40. typedef struct {
  41. int start; ///< timecode frame start (first base frame number)
  42. uint32_t flags; ///< flags such as drop frame, +24 hours support, ...
  43. AVRational rate; ///< frame rate in rational form
  44. unsigned fps; ///< frame per second; must be consistent with the rate field
  45. } AVTimecode;
  46. /**
  47. * Adjust frame number for NTSC drop frame time code.
  48. *
  49. * @param framenum frame number to adjust
  50. * @return adjusted frame number
  51. * @warning adjustment is only valid in NTSC 29.97
  52. */
  53. int av_timecode_adjust_ntsc_framenum(int framenum);
  54. /**
  55. * Convert frame number to SMPTE 12M binary representation.
  56. *
  57. * @param tc timecode data correctly initialized
  58. * @param framenum frame number
  59. * @return the SMPTE binary representation
  60. *
  61. * @note Frame number adjustment is automatically done in case of drop timecode,
  62. * you do NOT have to call av_timecode_adjust_ntsc_framenum().
  63. * @note The frame number is relative to tc->start.
  64. * @note Color frame (CF), binary group flags (BGF) and biphase mark polarity
  65. * correction (PC) bits are set to zero.
  66. */
  67. uint32_t av_timecode_get_smpte_from_framenum(const AVTimecode *tc, int framenum);
  68. /**
  69. * Load timecode string in buf.
  70. *
  71. * @param buf destination buffer, must be at least AV_TIMECODE_STR_SIZE long
  72. * @param tc timecode data correctly initialized
  73. * @param framenum frame number
  74. * @return the buf parameter
  75. *
  76. * @note Timecode representation can be a negative timecode and have more than
  77. * 24 hours, but will only be honored if the flags are correctly set.
  78. * @note The frame number is relative to tc->start.
  79. */
  80. char *av_timecode_make_string(const AVTimecode *tc, char *buf, int framenum);
  81. /**
  82. * Get the timecode string from the SMPTE timecode format.
  83. *
  84. * @param buf destination buffer, must be at least AV_TIMECODE_STR_SIZE long
  85. * @param tcsmpte the 32-bit SMPTE timecode
  86. * @param prevent_df prevent the use of a drop flag when it is known the DF bit
  87. * is arbitrary
  88. * @return the buf parameter
  89. */
  90. char *av_timecode_make_smpte_tc_string(char *buf, uint32_t tcsmpte, int prevent_df);
  91. /**
  92. * Get the timecode string from the 25-bit timecode format (MPEG GOP format).
  93. *
  94. * @param buf destination buffer, must be at least AV_TIMECODE_STR_SIZE long
  95. * @param tc25bit the 25-bits timecode
  96. * @return the buf parameter
  97. */
  98. char *av_timecode_make_mpeg_tc_string(char *buf, uint32_t tc25bit);
  99. /**
  100. * Init a timecode struct with the passed parameters.
  101. *
  102. * @param log_ctx a pointer to an arbitrary struct of which the first field
  103. * is a pointer to an AVClass struct (used for av_log)
  104. * @param tc pointer to an allocated AVTimecode
  105. * @param rate frame rate in rational form
  106. * @param flags miscellaneous flags such as drop frame, +24 hours, ...
  107. * (see AVTimecodeFlag)
  108. * @param frame_start the first frame number
  109. * @return 0 on success, AVERROR otherwise
  110. */
  111. int av_timecode_init(AVTimecode *tc, AVRational rate, int flags, int frame_start, void *log_ctx);
  112. /**
  113. * Parse timecode representation (hh:mm:ss[:;.]ff).
  114. *
  115. * @param log_ctx a pointer to an arbitrary struct of which the first field is a
  116. * pointer to an AVClass struct (used for av_log).
  117. * @param tc pointer to an allocated AVTimecode
  118. * @param rate frame rate in rational form
  119. * @param str timecode string which will determine the frame start
  120. * @return 0 on success, AVERROR otherwise
  121. */
  122. int av_timecode_init_from_string(AVTimecode *tc, AVRational rate, const char *str, void *log_ctx);
  123. #endif /* AVUTIL_TIMECODE_H */