00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include "version.h"
00029
00030 #if FF_API_OLD_TIMECODE
00031
00032 #include <stdio.h>
00033 #include "timecode.h"
00034 #include "libavutil/log.h"
00035
00036 int avpriv_framenum_to_drop_timecode(int frame_num)
00037 {
00038
00039 int d = frame_num / 17982;
00040 int m = frame_num % 17982;
00041
00042 return frame_num + 18 * d + 2 * ((m - 2) / 1798);
00043 }
00044
00045 uint32_t avpriv_framenum_to_smpte_timecode(unsigned frame, int fps, int drop)
00046 {
00047 return (0 << 31) |
00048 (drop << 30) |
00049 ( ((frame % fps) / 10) << 28) |
00050 ( ((frame % fps) % 10) << 24) |
00051 (0 << 23) |
00052 ((((frame / fps) % 60) / 10) << 20) |
00053 ((((frame / fps) % 60) % 10) << 16) |
00054 (0 << 15) |
00055 ((((frame / (fps * 60)) % 60) / 10) << 12) |
00056 ((((frame / (fps * 60)) % 60) % 10) << 8) |
00057 (0 << 7) |
00058 (0 << 6) |
00059 ((((frame / (fps * 3600) % 24)) / 10) << 4) |
00060 ( (frame / (fps * 3600) % 24)) % 10;
00061 }
00062
00063 int avpriv_check_timecode_rate(void *avcl, AVRational rate, int drop)
00064 {
00065 int fps;
00066
00067 if (!rate.num || !rate.den) {
00068 av_log(avcl, AV_LOG_ERROR, "Timecode frame rate must be specified\n");
00069 return -1;
00070 }
00071 fps = (rate.num + rate.den/2) / rate.den;
00072 if (drop && fps != 30) {
00073 av_log(avcl, AV_LOG_ERROR, "Drop frame is only allowed with 30000/1001 FPS\n");
00074 return -2;
00075 }
00076 switch (fps) {
00077 case 24:
00078 case 25:
00079 case 30: return 0;
00080
00081 default:
00082 av_log(avcl, AV_LOG_ERROR, "Timecode frame rate not supported\n");
00083 return -3;
00084 }
00085 }
00086
00087 char *avpriv_timecode_to_string(char *buf, const struct ff_timecode *tc, unsigned frame)
00088 {
00089 int frame_num = tc->start + frame;
00090 int fps = (tc->rate.num + tc->rate.den/2) / tc->rate.den;
00091 int hh, mm, ss, ff, neg = 0;
00092
00093 if (tc->drop)
00094 frame_num = avpriv_framenum_to_drop_timecode(frame_num);
00095 if (frame_num < 0) {
00096 frame_num = -frame_num;
00097 neg = 1;
00098 }
00099 ff = frame_num % fps;
00100 ss = frame_num / fps % 60;
00101 mm = frame_num / (fps*60) % 60;
00102 hh = frame_num / (fps*3600);
00103 snprintf(buf, 16, "%s%02d:%02d:%02d%c%02d",
00104 neg ? "-" : "",
00105 hh, mm, ss, tc->drop ? ';' : ':', ff);
00106 return buf;
00107 }
00108
00109 int avpriv_init_smpte_timecode(void *avcl, struct ff_timecode *tc)
00110 {
00111 int hh, mm, ss, ff, fps, ret;
00112 char c;
00113
00114 if (sscanf(tc->str, "%d:%d:%d%c%d", &hh, &mm, &ss, &c, &ff) != 5) {
00115 av_log(avcl, AV_LOG_ERROR, "unable to parse timecode, "
00116 "syntax: hh:mm:ss[:;.]ff\n");
00117 return -1;
00118 }
00119
00120 tc->drop = c != ':';
00121
00122 ret = avpriv_check_timecode_rate(avcl, tc->rate, tc->drop);
00123 if (ret < 0)
00124 return ret;
00125
00126 fps = (tc->rate.num + tc->rate.den/2) / tc->rate.den;
00127 tc->start = (hh*3600 + mm*60 + ss) * fps + ff;
00128
00129 if (tc->drop) {
00130 int tmins = 60*hh + mm;
00131 tc->start -= 2 * (tmins - tmins/10);
00132 }
00133 return 0;
00134 }
00135
00136 int ff_framenum_to_drop_timecode(int frame_num)
00137 {
00138 return avpriv_framenum_to_drop_timecode(frame_num);
00139 }
00140
00141 uint32_t ff_framenum_to_smtpe_timecode(unsigned frame, int fps, int drop)
00142 {
00143 return avpriv_framenum_to_smpte_timecode(frame, fps, drop);
00144 }
00145
00146 int ff_init_smtpe_timecode(void *avcl, struct ff_timecode *tc)
00147 {
00148 return avpriv_init_smpte_timecode(avcl, tc);
00149 }
00150 #endif