libavcodec/timecode.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006 Smartjog S.A.S, Baptiste Coudurier <baptiste.coudurier@gmail.com>
00003  * Copyright (C) 2011 Smartjog S.A.S, Clément Bœsch      <clement.boesch@smartjog.com>
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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     /* only works for NTSC 29.97 */
00034     int d = frame_num / 17982;
00035     int m = frame_num % 17982;
00036     //if (m < 2) m += 2; /* not needed since -2,-1 / 1798 in C returns 0 */
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) | // color frame flag
00043            (drop                                 << 30) | // drop  frame flag
00044            ( ((frame % fps) / 10)                << 28) | // tens  of frames
00045            ( ((frame % fps) % 10)                << 24) | // units of frames
00046            (0                                    << 23) | // field phase (NTSC), b0 (PAL)
00047            ((((frame / fps) % 60) / 10)          << 20) | // tens  of seconds
00048            ((((frame / fps) % 60) % 10)          << 16) | // units of seconds
00049            (0                                    << 15) | // b0 (NTSC), b2 (PAL)
00050            ((((frame / (fps * 60)) % 60) / 10)   << 12) | // tens  of minutes
00051            ((((frame / (fps * 60)) % 60) % 10)   <<  8) | // units of minutes
00052            (0                                    <<  7) | // b1
00053            (0                                    <<  6) | // b2 (NTSC), field phase (PAL)
00054            ((((frame / (fps * 3600) % 24)) / 10) <<  4) | // tens  of hours
00055            (  (frame / (fps * 3600) % 24)) % 10;          // units of hours
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 != ':'; // drop if ';', '.', ...
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) { /* adjust frame number */
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