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