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 module ffmpeg.libavutil.timecode;
27 
28 import ffmpeg.libavutil;
29 
30 extern (C) @nogc nothrow:
31 
32 enum AV_TIMECODE_STR_SIZE = 23;
33 
34 enum AVTimecodeFlag
35 {
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 
41 struct AVTimecode
42 {
43     int start; ///< timecode frame start (first base frame number)
44     uint flags; ///< flags such as drop frame, +24 hours support, ...
45     AVRational rate; ///< frame rate in rational form
46     uint fps; ///< frame per second; must be consistent with the rate field
47 }
48 
49 /**
50  * Adjust frame number for NTSC drop frame time code.
51  *
52  * @param framenum frame number to adjust
53  * @param fps      frame per second, multiples of 30
54  * @return         adjusted frame number
55  * @warning        adjustment is only valid for multiples of NTSC 29.97
56  */
57 int av_timecode_adjust_ntsc_framenum2 (int framenum, int fps);
58 
59 /**
60  * Convert frame number to SMPTE 12M binary representation.
61  *
62  * @param tc       timecode data correctly initialized
63  * @param framenum frame number
64  * @return         the SMPTE binary representation
65  *
66  * See SMPTE ST 314M-2005 Sec 4.4.2.2.1 "Time code pack (TC)"
67  * the format description as follows:
68  * bits 0-5:   hours, in BCD(6bits)
69  * bits 6:     BGF1
70  * bits 7:     BGF2 (NTSC) or FIELD (PAL)
71  * bits 8-14:  minutes, in BCD(7bits)
72  * bits 15:    BGF0 (NTSC) or BGF2 (PAL)
73  * bits 16-22: seconds, in BCD(7bits)
74  * bits 23:    FIELD (NTSC) or BGF0 (PAL)
75  * bits 24-29: frames, in BCD(6bits)
76  * bits 30:    drop  frame flag (0: non drop,    1: drop)
77  * bits 31:    color frame flag (0: unsync mode, 1: sync mode)
78  * @note BCD numbers (6 or 7 bits): 4 or 5 lower bits for units, 2 higher bits for tens.
79  * @note Frame number adjustment is automatically done in case of drop timecode,
80  *       you do NOT have to call av_timecode_adjust_ntsc_framenum2().
81  * @note The frame number is relative to tc->start.
82  * @note Color frame (CF) and binary group flags (BGF) bits are set to zero.
83  */
84 uint av_timecode_get_smpte_from_framenum (const(AVTimecode)* tc, int framenum);
85 
86 /**
87  * Convert sei info to SMPTE 12M binary representation.
88  *
89  * @param rate     frame rate in rational form
90  * @param drop     drop flag
91  * @param hh       hour
92  * @param mm       minute
93  * @param ss       second
94  * @param ff       frame number
95  * @return         the SMPTE binary representation
96  */
97 uint av_timecode_get_smpte (AVRational rate, int drop, int hh, int mm, int ss, int ff);
98 
99 /**
100  * Load timecode string in buf.
101  *
102  * @param buf      destination buffer, must be at least AV_TIMECODE_STR_SIZE long
103  * @param tc       timecode data correctly initialized
104  * @param framenum frame number
105  * @return         the buf parameter
106  *
107  * @note Timecode representation can be a negative timecode and have more than
108  *       24 hours, but will only be honored if the flags are correctly set.
109  * @note The frame number is relative to tc->start.
110  */
111 char* av_timecode_make_string (const(AVTimecode)* tc, char* buf, int framenum);
112 
113 /**
114  * Get the timecode string from the SMPTE timecode format.
115  *
116  * In contrast to av_timecode_make_smpte_tc_string this function supports 50/60
117  * fps timecodes by using the field bit.
118  *
119  * @param buf        destination buffer, must be at least AV_TIMECODE_STR_SIZE long
120  * @param rate       frame rate of the timecode
121  * @param tcsmpte    the 32-bit SMPTE timecode
122  * @param prevent_df prevent the use of a drop flag when it is known the DF bit
123  *                   is arbitrary
124  * @param skip_field prevent the use of a field flag when it is known the field
125  *                   bit is arbitrary (e.g. because it is used as PC flag)
126  * @return           the buf parameter
127  */
128 char* av_timecode_make_smpte_tc_string2 (char* buf, AVRational rate, uint tcsmpte, int prevent_df, int skip_field);
129 
130 /**
131  * Get the timecode string from the SMPTE timecode format.
132  *
133  * @param buf        destination buffer, must be at least AV_TIMECODE_STR_SIZE long
134  * @param tcsmpte    the 32-bit SMPTE timecode
135  * @param prevent_df prevent the use of a drop flag when it is known the DF bit
136  *                   is arbitrary
137  * @return           the buf parameter
138  */
139 char* av_timecode_make_smpte_tc_string (char* buf, uint tcsmpte, int prevent_df);
140 
141 /**
142  * Get the timecode string from the 25-bit timecode format (MPEG GOP format).
143  *
144  * @param buf     destination buffer, must be at least AV_TIMECODE_STR_SIZE long
145  * @param tc25bit the 25-bits timecode
146  * @return        the buf parameter
147  */
148 char* av_timecode_make_mpeg_tc_string (char* buf, uint tc25bit);
149 
150 /**
151  * Init a timecode struct with the passed parameters.
152  *
153  * @param log_ctx     a pointer to an arbitrary struct of which the first field
154  *                    is a pointer to an AVClass struct (used for av_log)
155  * @param tc          pointer to an allocated AVTimecode
156  * @param rate        frame rate in rational form
157  * @param flags       miscellaneous flags such as drop frame, +24 hours, ...
158  *                    (see AVTimecodeFlag)
159  * @param frame_start the first frame number
160  * @return            0 on success, AVERROR otherwise
161  */
162 int av_timecode_init (AVTimecode* tc, AVRational rate, int flags, int frame_start, void* log_ctx);
163 
164 /**
165  * Init a timecode struct from the passed timecode components.
166  *
167  * @param log_ctx     a pointer to an arbitrary struct of which the first field
168  *                    is a pointer to an AVClass struct (used for av_log)
169  * @param tc          pointer to an allocated AVTimecode
170  * @param rate        frame rate in rational form
171  * @param flags       miscellaneous flags such as drop frame, +24 hours, ...
172  *                    (see AVTimecodeFlag)
173  * @param hh          hours
174  * @param mm          minutes
175  * @param ss          seconds
176  * @param ff          frames
177  * @return            0 on success, AVERROR otherwise
178  */
179 int av_timecode_init_from_components (AVTimecode* tc, AVRational rate, int flags, int hh, int mm, int ss, int ff, void* log_ctx);
180 
181 /**
182  * Parse timecode representation (hh:mm:ss[:;.]ff).
183  *
184  * @param log_ctx a pointer to an arbitrary struct of which the first field is a
185  *                pointer to an AVClass struct (used for av_log).
186  * @param tc      pointer to an allocated AVTimecode
187  * @param rate    frame rate in rational form
188  * @param str     timecode string which will determine the frame start
189  * @return        0 on success, AVERROR otherwise
190  */
191 int av_timecode_init_from_string (AVTimecode* tc, AVRational rate, const(char)* str, void* log_ctx);
192 
193 /**
194  * Check if the timecode feature is available for the given frame rate
195  *
196  * @return 0 if supported, <0 otherwise
197  */
198 int av_timecode_check_frame_rate (AVRational rate);
199 
200 /* AVUTIL_TIMECODE_H */