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