yading@11: /* yading@11: * Copyright (c) 2006 Smartjog S.A.S, Baptiste Coudurier yading@11: * Copyright (c) 2011-2012 Smartjog S.A.S, Clément Bœsch yading@11: * yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: /** yading@11: * @file yading@11: * Timecode helpers yading@11: * @see https://en.wikipedia.org/wiki/SMPTE_time_code yading@11: * @see http://www.dropframetimecode.org yading@11: */ yading@11: yading@11: #include yading@11: #include "timecode.h" yading@11: #include "log.h" yading@11: #include "error.h" yading@11: yading@11: int av_timecode_adjust_ntsc_framenum2(int framenum, int fps) yading@11: { yading@11: /* only works for NTSC 29.97 and 59.94 */ yading@11: int drop_frames = 0; yading@11: int d, m, frames_per_10mins; yading@11: yading@11: if (fps == 30) { yading@11: drop_frames = 2; yading@11: frames_per_10mins = 17982; yading@11: } else if (fps == 60) { yading@11: drop_frames = 4; yading@11: frames_per_10mins = 35964; yading@11: } else yading@11: return framenum; yading@11: yading@11: d = framenum / frames_per_10mins; yading@11: m = framenum % frames_per_10mins; yading@11: yading@11: return framenum + 9 * drop_frames * d + drop_frames * ((m - drop_frames) / (frames_per_10mins / 10)); yading@11: } yading@11: yading@11: uint32_t av_timecode_get_smpte_from_framenum(const AVTimecode *tc, int framenum) yading@11: { yading@11: unsigned fps = tc->fps; yading@11: int drop = !!(tc->flags & AV_TIMECODE_FLAG_DROPFRAME); yading@11: int hh, mm, ss, ff; yading@11: yading@11: framenum += tc->start; yading@11: if (drop) yading@11: framenum = av_timecode_adjust_ntsc_framenum2(framenum, tc->fps); yading@11: ff = framenum % fps; yading@11: ss = framenum / fps % 60; yading@11: mm = framenum / (fps*60) % 60; yading@11: hh = framenum / (fps*3600) % 24; yading@11: return 0 << 31 | // color frame flag (0: unsync mode, 1: sync mode) yading@11: drop << 30 | // drop frame flag (0: non drop, 1: drop) yading@11: (ff / 10) << 28 | // tens of frames yading@11: (ff % 10) << 24 | // units of frames yading@11: 0 << 23 | // PC (NTSC) or BGF0 (PAL) yading@11: (ss / 10) << 20 | // tens of seconds yading@11: (ss % 10) << 16 | // units of seconds yading@11: 0 << 15 | // BGF0 (NTSC) or BGF2 (PAL) yading@11: (mm / 10) << 12 | // tens of minutes yading@11: (mm % 10) << 8 | // units of minutes yading@11: 0 << 7 | // BGF2 (NTSC) or PC (PAL) yading@11: 0 << 6 | // BGF1 yading@11: (hh / 10) << 4 | // tens of hours yading@11: (hh % 10); // units of hours yading@11: } yading@11: yading@11: char *av_timecode_make_string(const AVTimecode *tc, char *buf, int framenum) yading@11: { yading@11: int fps = tc->fps; yading@11: int drop = tc->flags & AV_TIMECODE_FLAG_DROPFRAME; yading@11: int hh, mm, ss, ff, neg = 0; yading@11: yading@11: framenum += tc->start; yading@11: if (drop) yading@11: framenum = av_timecode_adjust_ntsc_framenum2(framenum, fps); yading@11: if (framenum < 0) { yading@11: framenum = -framenum; yading@11: neg = tc->flags & AV_TIMECODE_FLAG_ALLOWNEGATIVE; yading@11: } yading@11: ff = framenum % fps; yading@11: ss = framenum / fps % 60; yading@11: mm = framenum / (fps*60) % 60; yading@11: hh = framenum / (fps*3600); yading@11: if (tc->flags & AV_TIMECODE_FLAG_24HOURSMAX) yading@11: hh = hh % 24; yading@11: snprintf(buf, AV_TIMECODE_STR_SIZE, "%s%02d:%02d:%02d%c%02d", yading@11: neg ? "-" : "", yading@11: hh, mm, ss, drop ? ';' : ':', ff); yading@11: return buf; yading@11: } yading@11: yading@11: static unsigned bcd2uint(uint8_t bcd) yading@11: { yading@11: unsigned low = bcd & 0xf; yading@11: unsigned high = bcd >> 4; yading@11: if (low > 9 || high > 9) yading@11: return 0; yading@11: return low + 10*high; yading@11: } yading@11: yading@11: char *av_timecode_make_smpte_tc_string(char *buf, uint32_t tcsmpte, int prevent_df) yading@11: { yading@11: unsigned hh = bcd2uint(tcsmpte & 0x3f); // 6-bit hours yading@11: unsigned mm = bcd2uint(tcsmpte>>8 & 0x7f); // 7-bit minutes yading@11: unsigned ss = bcd2uint(tcsmpte>>16 & 0x7f); // 7-bit seconds yading@11: unsigned ff = bcd2uint(tcsmpte>>24 & 0x3f); // 6-bit frames yading@11: unsigned drop = tcsmpte & 1<<30 && !prevent_df; // 1-bit drop if not arbitrary bit yading@11: snprintf(buf, AV_TIMECODE_STR_SIZE, "%02u:%02u:%02u%c%02u", yading@11: hh, mm, ss, drop ? ';' : ':', ff); yading@11: return buf; yading@11: } yading@11: yading@11: char *av_timecode_make_mpeg_tc_string(char *buf, uint32_t tc25bit) yading@11: { yading@11: snprintf(buf, AV_TIMECODE_STR_SIZE, "%02u:%02u:%02u%c%02u", yading@11: tc25bit>>19 & 0x1f, // 5-bit hours yading@11: tc25bit>>13 & 0x3f, // 6-bit minutes yading@11: tc25bit>>6 & 0x3f, // 6-bit seconds yading@11: tc25bit & 1<<24 ? ';' : ':', // 1-bit drop flag yading@11: tc25bit & 0x3f); // 6-bit frames yading@11: return buf; yading@11: } yading@11: yading@11: static int check_fps(int fps) yading@11: { yading@11: int i; yading@11: static const int supported_fps[] = {24, 25, 30, 50, 60}; yading@11: yading@11: for (i = 0; i < FF_ARRAY_ELEMS(supported_fps); i++) yading@11: if (fps == supported_fps[i]) yading@11: return 0; yading@11: return -1; yading@11: } yading@11: yading@11: static int check_timecode(void *log_ctx, AVTimecode *tc) yading@11: { yading@11: if (tc->fps <= 0) { yading@11: av_log(log_ctx, AV_LOG_ERROR, "Timecode frame rate must be specified\n"); yading@11: return AVERROR(EINVAL); yading@11: } yading@11: if ((tc->flags & AV_TIMECODE_FLAG_DROPFRAME) && tc->fps != 30 && tc->fps != 60) { yading@11: av_log(log_ctx, AV_LOG_ERROR, "Drop frame is only allowed with 30000/1001 or 60000/1001 FPS\n"); yading@11: return AVERROR(EINVAL); yading@11: } yading@11: if (check_fps(tc->fps) < 0) { yading@11: av_log(log_ctx, AV_LOG_ERROR, "Timecode frame rate %d/%d not supported\n", yading@11: tc->rate.num, tc->rate.den); yading@11: return AVERROR_PATCHWELCOME; yading@11: } yading@11: return 0; yading@11: } yading@11: yading@11: static int fps_from_frame_rate(AVRational rate) yading@11: { yading@11: if (!rate.den || !rate.num) yading@11: return -1; yading@11: return (rate.num + rate.den/2) / rate.den; yading@11: } yading@11: yading@11: int av_timecode_check_frame_rate(AVRational rate) yading@11: { yading@11: return check_fps(fps_from_frame_rate(rate)); yading@11: } yading@11: yading@11: int av_timecode_init(AVTimecode *tc, AVRational rate, int flags, int frame_start, void *log_ctx) yading@11: { yading@11: memset(tc, 0, sizeof(*tc)); yading@11: tc->start = frame_start; yading@11: tc->flags = flags; yading@11: tc->rate = rate; yading@11: tc->fps = fps_from_frame_rate(rate); yading@11: return check_timecode(log_ctx, tc); yading@11: } yading@11: yading@11: int av_timecode_init_from_string(AVTimecode *tc, AVRational rate, const char *str, void *log_ctx) yading@11: { yading@11: char c; yading@11: int hh, mm, ss, ff, ret; yading@11: yading@11: if (sscanf(str, "%d:%d:%d%c%d", &hh, &mm, &ss, &c, &ff) != 5) { yading@11: av_log(log_ctx, AV_LOG_ERROR, "Unable to parse timecode, " yading@11: "syntax: hh:mm:ss[:;.]ff\n"); yading@11: return AVERROR_INVALIDDATA; yading@11: } yading@11: yading@11: memset(tc, 0, sizeof(*tc)); yading@11: tc->flags = c != ':' ? AV_TIMECODE_FLAG_DROPFRAME : 0; // drop if ';', '.', ... yading@11: tc->rate = rate; yading@11: tc->fps = fps_from_frame_rate(rate); yading@11: yading@11: ret = check_timecode(log_ctx, tc); yading@11: if (ret < 0) yading@11: return ret; yading@11: yading@11: tc->start = (hh*3600 + mm*60 + ss) * tc->fps + ff; yading@11: if (tc->flags & AV_TIMECODE_FLAG_DROPFRAME) { /* adjust frame number */ yading@11: int tmins = 60*hh + mm; yading@11: tc->start -= 2 * (tmins - tmins/10); yading@11: } yading@11: return 0; yading@11: }