LCOV - code coverage report
Current view: top level - src/libavutil - timecode.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 95 119 79.8 %
Date: 2017-01-24 04:42:20 Functions: 11 12 91.7 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2006 Smartjog S.A.S, Baptiste Coudurier <baptiste.coudurier@gmail.com>
       3             :  * Copyright (c) 2011-2012 Smartjog S.A.S, Clément Bœsch <clement.boesch@smartjog.com>
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or
       8             :  * modify it under the terms of the GNU Lesser General Public
       9             :  * License as published by the Free Software Foundation; either
      10             :  * version 2.1 of the License, or (at your option) any later version.
      11             :  *
      12             :  * FFmpeg is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :  * Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : /**
      23             :  * @file
      24             :  * Timecode helpers
      25             :  * @see https://en.wikipedia.org/wiki/SMPTE_time_code
      26             :  * @see http://www.dropframetimecode.org
      27             :  */
      28             : 
      29             : #include <stdio.h>
      30             : #include "timecode.h"
      31             : #include "log.h"
      32             : #include "error.h"
      33             : 
      34        3525 : int av_timecode_adjust_ntsc_framenum2(int framenum, int fps)
      35             : {
      36             :     /* only works for NTSC 29.97 and 59.94 */
      37        3525 :     int drop_frames = 0;
      38             :     int d, m, frames_per_10mins;
      39             : 
      40        3525 :     if (fps == 30) {
      41        3525 :         drop_frames = 2;
      42        3525 :         frames_per_10mins = 17982;
      43           0 :     } else if (fps == 60) {
      44           0 :         drop_frames = 4;
      45           0 :         frames_per_10mins = 35964;
      46             :     } else
      47           0 :         return framenum;
      48             : 
      49        3525 :     d = framenum / frames_per_10mins;
      50        3525 :     m = framenum % frames_per_10mins;
      51             : 
      52        3525 :     return framenum + 9 * drop_frames * d + drop_frames * ((m - drop_frames) / (frames_per_10mins / 10));
      53             : }
      54             : 
      55       97203 : uint32_t av_timecode_get_smpte_from_framenum(const AVTimecode *tc, int framenum)
      56             : {
      57       97203 :     unsigned fps = tc->fps;
      58       97203 :     int drop = !!(tc->flags & AV_TIMECODE_FLAG_DROPFRAME);
      59             :     int hh, mm, ss, ff;
      60             : 
      61       97203 :     framenum += tc->start;
      62       97203 :     if (drop)
      63        3510 :         framenum = av_timecode_adjust_ntsc_framenum2(framenum, tc->fps);
      64       97203 :     ff = framenum % fps;
      65       97203 :     ss = framenum / fps      % 60;
      66       97203 :     mm = framenum / (fps*60) % 60;
      67       97203 :     hh = framenum / (fps*3600) % 24;
      68       97203 :     return 0         << 31 | // color frame flag (0: unsync mode, 1: sync mode)
      69       97203 :            drop      << 30 | // drop  frame flag (0: non drop,    1: drop)
      70       97203 :            (ff / 10) << 28 | // tens  of frames
      71       97203 :            (ff % 10) << 24 | // units of frames
      72       97203 :            0         << 23 | // PC (NTSC) or BGF0 (PAL)
      73       97203 :            (ss / 10) << 20 | // tens  of seconds
      74       97203 :            (ss % 10) << 16 | // units of seconds
      75       97203 :            0         << 15 | // BGF0 (NTSC) or BGF2 (PAL)
      76       97203 :            (mm / 10) << 12 | // tens  of minutes
      77       97203 :            (mm % 10) <<  8 | // units of minutes
      78       97203 :            0         <<  7 | // BGF2 (NTSC) or PC (PAL)
      79       97203 :            0         <<  6 | // BGF1
      80      194406 :            (hh / 10) <<  4 | // tens  of hours
      81       97203 :            (hh % 10);        // units of hours
      82             : }
      83             : 
      84         100 : char *av_timecode_make_string(const AVTimecode *tc, char *buf, int framenum)
      85             : {
      86         100 :     int fps = tc->fps;
      87         100 :     int drop = tc->flags & AV_TIMECODE_FLAG_DROPFRAME;
      88         100 :     int hh, mm, ss, ff, neg = 0;
      89             : 
      90         100 :     framenum += tc->start;
      91         100 :     if (drop)
      92           6 :         framenum = av_timecode_adjust_ntsc_framenum2(framenum, fps);
      93         100 :     if (framenum < 0) {
      94           0 :         framenum = -framenum;
      95           0 :         neg = tc->flags & AV_TIMECODE_FLAG_ALLOWNEGATIVE;
      96             :     }
      97         100 :     ff = framenum % fps;
      98         100 :     ss = framenum / fps        % 60;
      99         100 :     mm = framenum / (fps*60)   % 60;
     100         100 :     hh = framenum / (fps*3600);
     101         100 :     if (tc->flags & AV_TIMECODE_FLAG_24HOURSMAX)
     102           0 :         hh = hh % 24;
     103         100 :     snprintf(buf, AV_TIMECODE_STR_SIZE, "%s%02d:%02d:%02d%c%02d",
     104             :              neg ? "-" : "",
     105             :              hh, mm, ss, drop ? ';' : ':', ff);
     106         100 :     return buf;
     107             : }
     108             : 
     109          68 : static unsigned bcd2uint(uint8_t bcd)
     110             : {
     111          68 :    unsigned low  = bcd & 0xf;
     112          68 :    unsigned high = bcd >> 4;
     113          68 :    if (low > 9 || high > 9)
     114           0 :        return 0;
     115          68 :    return low + 10*high;
     116             : }
     117             : 
     118          17 : char *av_timecode_make_smpte_tc_string(char *buf, uint32_t tcsmpte, int prevent_df)
     119             : {
     120          17 :     unsigned hh   = bcd2uint(tcsmpte     & 0x3f);    // 6-bit hours
     121          17 :     unsigned mm   = bcd2uint(tcsmpte>>8  & 0x7f);    // 7-bit minutes
     122          17 :     unsigned ss   = bcd2uint(tcsmpte>>16 & 0x7f);    // 7-bit seconds
     123          17 :     unsigned ff   = bcd2uint(tcsmpte>>24 & 0x3f);    // 6-bit frames
     124          17 :     unsigned drop = tcsmpte & 1<<30 && !prevent_df;  // 1-bit drop if not arbitrary bit
     125          17 :     snprintf(buf, AV_TIMECODE_STR_SIZE, "%02u:%02u:%02u%c%02u",
     126             :              hh, mm, ss, drop ? ';' : ':', ff);
     127          17 :     return buf;
     128             : }
     129             : 
     130           0 : char *av_timecode_make_mpeg_tc_string(char *buf, uint32_t tc25bit)
     131             : {
     132           0 :     snprintf(buf, AV_TIMECODE_STR_SIZE, "%02u:%02u:%02u%c%02u",
     133           0 :              tc25bit>>19 & 0x1f,              // 5-bit hours
     134           0 :              tc25bit>>13 & 0x3f,              // 6-bit minutes
     135           0 :              tc25bit>>6  & 0x3f,              // 6-bit seconds
     136           0 :              tc25bit     & 1<<24 ? ';' : ':', // 1-bit drop flag
     137             :              tc25bit     & 0x3f);             // 6-bit frames
     138           0 :     return buf;
     139             : }
     140             : 
     141         148 : static int check_fps(int fps)
     142             : {
     143             :     int i;
     144             :     static const int supported_fps[] = {
     145             :         24, 25, 30, 48, 50, 60, 100, 120, 150,
     146             :     };
     147             : 
     148         335 :     for (i = 0; i < FF_ARRAY_ELEMS(supported_fps); i++)
     149         333 :         if (fps == supported_fps[i])
     150         146 :             return 0;
     151           2 :     return -1;
     152             : }
     153             : 
     154         134 : static int check_timecode(void *log_ctx, AVTimecode *tc)
     155             : {
     156         134 :     if ((int)tc->fps <= 0) {
     157           0 :         av_log(log_ctx, AV_LOG_ERROR, "Timecode frame rate must be specified\n");
     158           0 :         return AVERROR(EINVAL);
     159             :     }
     160         134 :     if ((tc->flags & AV_TIMECODE_FLAG_DROPFRAME) && tc->fps != 30 && tc->fps != 60) {
     161           0 :         av_log(log_ctx, AV_LOG_ERROR, "Drop frame is only allowed with 30000/1001 or 60000/1001 FPS\n");
     162           0 :         return AVERROR(EINVAL);
     163             :     }
     164         134 :     if (check_fps(tc->fps) < 0) {
     165           0 :         av_log(log_ctx, AV_LOG_WARNING, "Using non-standard frame rate %d/%d\n",
     166             :                tc->rate.num, tc->rate.den);
     167             :     }
     168         134 :     return 0;
     169             : }
     170             : 
     171         148 : static int fps_from_frame_rate(AVRational rate)
     172             : {
     173         148 :     if (!rate.den || !rate.num)
     174           0 :         return -1;
     175         148 :     return (rate.num + rate.den/2) / rate.den;
     176             : }
     177             : 
     178          14 : int av_timecode_check_frame_rate(AVRational rate)
     179             : {
     180          14 :     return check_fps(fps_from_frame_rate(rate));
     181             : }
     182             : 
     183         115 : int av_timecode_init(AVTimecode *tc, AVRational rate, int flags, int frame_start, void *log_ctx)
     184             : {
     185         115 :     memset(tc, 0, sizeof(*tc));
     186         115 :     tc->start = frame_start;
     187         115 :     tc->flags = flags;
     188         115 :     tc->rate  = rate;
     189         115 :     tc->fps   = fps_from_frame_rate(rate);
     190         115 :     return check_timecode(log_ctx, tc);
     191             : }
     192             : 
     193          19 : int av_timecode_init_from_string(AVTimecode *tc, AVRational rate, const char *str, void *log_ctx)
     194             : {
     195             :     char c;
     196             :     int hh, mm, ss, ff, ret;
     197             : 
     198          19 :     if (sscanf(str, "%d:%d:%d%c%d", &hh, &mm, &ss, &c, &ff) != 5) {
     199           0 :         av_log(log_ctx, AV_LOG_ERROR, "Unable to parse timecode, "
     200             :                                       "syntax: hh:mm:ss[:;.]ff\n");
     201           0 :         return AVERROR_INVALIDDATA;
     202             :     }
     203             : 
     204          19 :     memset(tc, 0, sizeof(*tc));
     205          19 :     tc->flags = c != ':' ? AV_TIMECODE_FLAG_DROPFRAME : 0; // drop if ';', '.', ...
     206          19 :     tc->rate  = rate;
     207          19 :     tc->fps   = fps_from_frame_rate(rate);
     208             : 
     209          19 :     ret = check_timecode(log_ctx, tc);
     210          19 :     if (ret < 0)
     211           0 :         return ret;
     212             : 
     213          19 :     tc->start = (hh*3600 + mm*60 + ss) * tc->fps + ff;
     214          19 :     if (tc->flags & AV_TIMECODE_FLAG_DROPFRAME) { /* adjust frame number */
     215          10 :         int tmins = 60*hh + mm;
     216          10 :         tc->start -= 2 * (tmins - tmins/10);
     217             :     }
     218          19 :     return 0;
     219             : }

Generated by: LCOV version 1.12