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