00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00029 #include <stdio.h>
00030 #include "timecode.h"
00031 #include "log.h"
00032 #include "error.h"
00033
00034 int av_timecode_adjust_ntsc_framenum2(int framenum, int fps)
00035 {
00036
00037 int drop_frames = 0;
00038 int d = framenum / 17982;
00039 int m = framenum % 17982;
00040
00041 if (fps == 30)
00042 drop_frames = 2;
00043 else if (fps == 60)
00044 drop_frames = 4;
00045 else
00046 return framenum;
00047
00048
00049 return framenum + 9 * drop_frames * d + drop_frames * ((m - 2) / 1798);
00050 }
00051
00052 uint32_t av_timecode_get_smpte_from_framenum(const AVTimecode *tc, int framenum)
00053 {
00054 unsigned fps = tc->fps;
00055 int drop = !!(tc->flags & AV_TIMECODE_FLAG_DROPFRAME);
00056 int hh, mm, ss, ff;
00057
00058 framenum += tc->start;
00059 if (drop)
00060 framenum = av_timecode_adjust_ntsc_framenum2(framenum, tc->fps);
00061 ff = framenum % fps;
00062 ss = framenum / fps % 60;
00063 mm = framenum / (fps*60) % 60;
00064 hh = framenum / (fps*3600) % 24;
00065 return 0 << 31 |
00066 drop << 30 |
00067 (ff / 10) << 28 |
00068 (ff % 10) << 24 |
00069 0 << 23 |
00070 (ss / 10) << 20 |
00071 (ss % 10) << 16 |
00072 0 << 15 |
00073 (mm / 10) << 12 |
00074 (mm % 10) << 8 |
00075 0 << 7 |
00076 0 << 6 |
00077 (hh / 10) << 4 |
00078 (hh % 10);
00079 }
00080
00081 char *av_timecode_make_string(const AVTimecode *tc, char *buf, int framenum)
00082 {
00083 int fps = tc->fps;
00084 int drop = tc->flags & AV_TIMECODE_FLAG_DROPFRAME;
00085 int hh, mm, ss, ff, neg = 0;
00086
00087 framenum += tc->start;
00088 if (drop)
00089 framenum = av_timecode_adjust_ntsc_framenum2(framenum, fps);
00090 if (framenum < 0) {
00091 framenum = -framenum;
00092 neg = tc->flags & AV_TIMECODE_FLAG_ALLOWNEGATIVE;
00093 }
00094 ff = framenum % fps;
00095 ss = framenum / fps % 60;
00096 mm = framenum / (fps*60) % 60;
00097 hh = framenum / (fps*3600);
00098 if (tc->flags & AV_TIMECODE_FLAG_24HOURSMAX)
00099 hh = hh % 24;
00100 snprintf(buf, AV_TIMECODE_STR_SIZE, "%s%02d:%02d:%02d%c%02d",
00101 neg ? "-" : "",
00102 hh, mm, ss, drop ? ';' : ':', ff);
00103 return buf;
00104 }
00105
00106 static unsigned bcd2uint(uint8_t bcd)
00107 {
00108 unsigned low = bcd & 0xf;
00109 unsigned high = bcd >> 4;
00110 if (low > 9 || high > 9)
00111 return 0;
00112 return low + 10*high;
00113 }
00114
00115 char *av_timecode_make_smpte_tc_string(char *buf, uint32_t tcsmpte, int prevent_df)
00116 {
00117 unsigned hh = bcd2uint(tcsmpte & 0x3f);
00118 unsigned mm = bcd2uint(tcsmpte>>8 & 0x7f);
00119 unsigned ss = bcd2uint(tcsmpte>>16 & 0x7f);
00120 unsigned ff = bcd2uint(tcsmpte>>24 & 0x3f);
00121 unsigned drop = tcsmpte & 1<<30 && !prevent_df;
00122 snprintf(buf, AV_TIMECODE_STR_SIZE, "%02u:%02u:%02u%c%02u",
00123 hh, mm, ss, drop ? ';' : ':', ff);
00124 return buf;
00125 }
00126
00127 char *av_timecode_make_mpeg_tc_string(char *buf, uint32_t tc25bit)
00128 {
00129 snprintf(buf, AV_TIMECODE_STR_SIZE, "%02u:%02u:%02u%c%02u",
00130 tc25bit>>19 & 0x1f,
00131 tc25bit>>13 & 0x3f,
00132 tc25bit>>6 & 0x3f,
00133 tc25bit & 1<<24 ? ';' : ':',
00134 tc25bit & 0x3f);
00135 return buf;
00136 }
00137
00138 static int check_fps(int fps)
00139 {
00140 int i;
00141 static const int supported_fps[] = {24, 25, 30, 50, 60};
00142
00143 for (i = 0; i < FF_ARRAY_ELEMS(supported_fps); i++)
00144 if (fps == supported_fps[i])
00145 return 0;
00146 return -1;
00147 }
00148
00149 static int check_timecode(void *log_ctx, AVTimecode *tc)
00150 {
00151 if (tc->fps <= 0) {
00152 av_log(log_ctx, AV_LOG_ERROR, "Timecode frame rate must be specified\n");
00153 return AVERROR(EINVAL);
00154 }
00155 if ((tc->flags & AV_TIMECODE_FLAG_DROPFRAME) && tc->fps != 30) {
00156 av_log(log_ctx, AV_LOG_ERROR, "Drop frame is only allowed with 30000/1001 FPS\n");
00157 return AVERROR(EINVAL);
00158 }
00159 if (check_fps(tc->fps) < 0) {
00160 av_log(log_ctx, AV_LOG_ERROR, "Timecode frame rate %d/%d not supported\n",
00161 tc->rate.num, tc->rate.den);
00162 return AVERROR_PATCHWELCOME;
00163 }
00164 return 0;
00165 }
00166
00167 static int fps_from_frame_rate(AVRational rate)
00168 {
00169 if (!rate.den || !rate.num)
00170 return -1;
00171 return (rate.num + rate.den/2) / rate.den;
00172 }
00173
00174 int av_timecode_check_frame_rate(AVRational rate)
00175 {
00176 return check_fps(fps_from_frame_rate(rate));
00177 }
00178
00179 int av_timecode_init(AVTimecode *tc, AVRational rate, int flags, int frame_start, void *log_ctx)
00180 {
00181 memset(tc, 0, sizeof(*tc));
00182 tc->start = frame_start;
00183 tc->flags = flags;
00184 tc->rate = rate;
00185 tc->fps = fps_from_frame_rate(rate);
00186 return check_timecode(log_ctx, tc);
00187 }
00188
00189 int av_timecode_init_from_string(AVTimecode *tc, AVRational rate, const char *str, void *log_ctx)
00190 {
00191 char c;
00192 int hh, mm, ss, ff, ret;
00193
00194 if (sscanf(str, "%d:%d:%d%c%d", &hh, &mm, &ss, &c, &ff) != 5) {
00195 av_log(log_ctx, AV_LOG_ERROR, "Unable to parse timecode, "
00196 "syntax: hh:mm:ss[:;.]ff\n");
00197 return AVERROR_INVALIDDATA;
00198 }
00199
00200 memset(tc, 0, sizeof(*tc));
00201 tc->flags = c != ':' ? AV_TIMECODE_FLAG_DROPFRAME : 0;
00202 tc->rate = rate;
00203 tc->fps = fps_from_frame_rate(rate);
00204
00205 ret = check_timecode(log_ctx, tc);
00206 if (ret < 0)
00207 return ret;
00208
00209 tc->start = (hh*3600 + mm*60 + ss) * tc->fps + ff;
00210 if (tc->flags & AV_TIMECODE_FLAG_DROPFRAME) {
00211 int tmins = 60*hh + mm;
00212 tc->start -= 2 * (tmins - tmins/10);
00213 }
00214 return 0;
00215 }