]> git.sesse.net Git - ffmpeg/blob - libavcodec/timecode.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / timecode.c
1 /*
2  * Copyright (C) 2006 Smartjog S.A.S, Baptiste Coudurier <baptiste.coudurier@gmail.com>
3  * Copyright (C) 2011 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
25  */
26
27 #include <stdio.h>
28 #include "timecode.h"
29 #include "libavutil/log.h"
30
31 int avpriv_framenum_to_drop_timecode(int frame_num)
32 {
33     /* only works for NTSC 29.97 */
34     int d = frame_num / 17982;
35     int m = frame_num % 17982;
36     //if (m < 2) m += 2; /* not needed since -2,-1 / 1798 in C returns 0 */
37     return frame_num + 18 * d + 2 * ((m - 2) / 1798);
38 }
39
40 uint32_t avpriv_framenum_to_smpte_timecode(unsigned frame, int fps, int drop)
41 {
42     return (0                                    << 31) | // color frame flag
43            (drop                                 << 30) | // drop  frame flag
44            ( ((frame % fps) / 10)                << 28) | // tens  of frames
45            ( ((frame % fps) % 10)                << 24) | // units of frames
46            (0                                    << 23) | // field phase (NTSC), b0 (PAL)
47            ((((frame / fps) % 60) / 10)          << 20) | // tens  of seconds
48            ((((frame / fps) % 60) % 10)          << 16) | // units of seconds
49            (0                                    << 15) | // b0 (NTSC), b2 (PAL)
50            ((((frame / (fps * 60)) % 60) / 10)   << 12) | // tens  of minutes
51            ((((frame / (fps * 60)) % 60) % 10)   <<  8) | // units of minutes
52            (0                                    <<  7) | // b1
53            (0                                    <<  6) | // b2 (NTSC), field phase (PAL)
54            ((((frame / (fps * 3600) % 24)) / 10) <<  4) | // tens  of hours
55            (  (frame / (fps * 3600) % 24)) % 10;          // units of hours
56 }
57
58 int avpriv_check_timecode_rate(void *avcl, AVRational rate, int drop)
59 {
60     int fps;
61
62     if (!rate.num || !rate.den) {
63         av_log(avcl, AV_LOG_ERROR, "Timecode frame rate must be specified\n");
64         return -1;
65     }
66     fps = (rate.num + rate.den/2) / rate.den;
67     if (drop && fps != 30) {
68         av_log(avcl, AV_LOG_ERROR, "Drop frame is only allowed with 30000/1001 FPS\n");
69         return -2;
70     }
71     switch (fps) {
72     case 24:
73     case 25:
74     case 30: return  0;
75
76     default:
77         av_log(avcl, AV_LOG_ERROR, "Timecode frame rate not supported\n");
78         return -3;
79     }
80 }
81
82 char *avpriv_timecode_to_string(char *buf, const struct ff_timecode *tc, unsigned frame)
83 {
84     int frame_num = tc->start + frame;
85     int fps = (tc->rate.num + tc->rate.den/2) / tc->rate.den;
86     int hh, mm, ss, ff, neg = 0;
87
88     if (tc->drop)
89         frame_num = avpriv_framenum_to_drop_timecode(frame_num);
90     if (frame_num < 0) {
91         frame_num = -frame_num;
92         neg = 1;
93     }
94     ff = frame_num % fps;
95     ss = frame_num / fps        % 60;
96     mm = frame_num / (fps*60)   % 60;
97     hh = frame_num / (fps*3600);
98     snprintf(buf, 16, "%s%02d:%02d:%02d%c%02d",
99              neg ? "-" : "",
100              hh, mm, ss, tc->drop ? ';' : ':', ff);
101     return buf;
102 }
103
104 int avpriv_init_smpte_timecode(void *avcl, struct ff_timecode *tc)
105 {
106     int hh, mm, ss, ff, fps, ret;
107     char c;
108
109     if (sscanf(tc->str, "%d:%d:%d%c%d", &hh, &mm, &ss, &c, &ff) != 5) {
110         av_log(avcl, AV_LOG_ERROR, "unable to parse timecode, "
111                                    "syntax: hh:mm:ss[:;.]ff\n");
112         return -1;
113     }
114
115     tc->drop  = c != ':'; // drop if ';', '.', ...
116
117     ret = avpriv_check_timecode_rate(avcl, tc->rate, tc->drop);
118     if (ret < 0)
119         return ret;
120
121     fps       = (tc->rate.num + tc->rate.den/2) / tc->rate.den;
122     tc->start = (hh*3600 + mm*60 + ss) * fps + ff;
123
124     if (tc->drop) { /* adjust frame number */
125         int tmins = 60*hh + mm;
126         tc->start -= 2 * (tmins - tmins/10);
127     }
128     return 0;
129 }
130
131 #if FF_API_OLD_TIMECODE
132 int ff_framenum_to_drop_timecode(int frame_num)
133 {
134     return avpriv_framenum_to_drop_timecode(frame_num);
135 }
136
137 uint32_t ff_framenum_to_smtpe_timecode(unsigned frame, int fps, int drop)
138 {
139     return avpriv_framenum_to_smpte_timecode(frame, fps, drop);
140 }
141
142 int ff_init_smtpe_timecode(void *avcl, struct ff_timecode *tc)
143 {
144     return avpriv_init_smpte_timecode(avcl, tc);
145 }
146 #endif