libavcodec/timecode.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006 Smartjog S.A.S, Baptiste Coudurier <baptiste.coudurier@gmail.com>
3  * Copyright (C) 2011 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  * This *private* API is deprecated, please use the one available in libavutil instead.
26  */
27 
28 #include "version.h"
29 
30 #if FF_API_OLD_TIMECODE
31 
32 #include <stdio.h>
33 #include "timecode.h"
34 #include "libavutil/log.h"
35 
36 int avpriv_framenum_to_drop_timecode(int frame_num)
37 {
38  /* only works for NTSC 29.97 */
39  int d = frame_num / 17982;
40  int m = frame_num % 17982;
41  //if (m < 2) m += 2; /* not needed since -2,-1 / 1798 in C returns 0 */
42  return frame_num + 18 * d + 2 * ((m - 2) / 1798);
43 }
44 
45 uint32_t avpriv_framenum_to_smpte_timecode(unsigned frame, int fps, int drop)
46 {
47  return (0 << 31) | // color frame flag
48  (drop << 30) | // drop frame flag
49  ( ((frame % fps) / 10) << 28) | // tens of frames
50  ( ((frame % fps) % 10) << 24) | // units of frames
51  (0 << 23) | // field phase (NTSC), b0 (PAL)
52  ((((frame / fps) % 60) / 10) << 20) | // tens of seconds
53  ((((frame / fps) % 60) % 10) << 16) | // units of seconds
54  (0 << 15) | // b0 (NTSC), b2 (PAL)
55  ((((frame / (fps * 60)) % 60) / 10) << 12) | // tens of minutes
56  ((((frame / (fps * 60)) % 60) % 10) << 8) | // units of minutes
57  (0 << 7) | // b1
58  (0 << 6) | // b2 (NTSC), field phase (PAL)
59  ((((frame / (fps * 3600) % 24)) / 10) << 4) | // tens of hours
60  ( (frame / (fps * 3600) % 24)) % 10; // units of hours
61 }
62 
63 int avpriv_check_timecode_rate(void *avcl, AVRational rate, int drop)
64 {
65  int fps;
66 
67  if (!rate.num || !rate.den) {
68  av_log(avcl, AV_LOG_ERROR, "Timecode frame rate must be specified\n");
69  return -1;
70  }
71  fps = (rate.num + rate.den/2) / rate.den;
72  if (drop && fps != 30) {
73  av_log(avcl, AV_LOG_ERROR, "Drop frame is only allowed with 30000/1001 FPS\n");
74  return -2;
75  }
76  switch (fps) {
77  case 24:
78  case 25:
79  case 30: return 0;
80 
81  default:
82  av_log(avcl, AV_LOG_ERROR, "Timecode frame rate not supported\n");
83  return -3;
84  }
85 }
86 
87 char *avpriv_timecode_to_string(char *buf, const struct ff_timecode *tc, unsigned frame)
88 {
89  int frame_num = tc->start + frame;
90  int fps = (tc->rate.num + tc->rate.den/2) / tc->rate.den;
91  int hh, mm, ss, ff, neg = 0;
92 
93  if (tc->drop)
94  frame_num = avpriv_framenum_to_drop_timecode(frame_num);
95  if (frame_num < 0) {
96  frame_num = -frame_num;
97  neg = 1;
98  }
99  ff = frame_num % fps;
100  ss = frame_num / fps % 60;
101  mm = frame_num / (fps*60) % 60;
102  hh = frame_num / (fps*3600);
103  snprintf(buf, 16, "%s%02d:%02d:%02d%c%02d",
104  neg ? "-" : "",
105  hh, mm, ss, tc->drop ? ';' : ':', ff);
106  return buf;
107 }
108 
109 int avpriv_init_smpte_timecode(void *avcl, struct ff_timecode *tc)
110 {
111  int hh, mm, ss, ff, fps, ret;
112  char c;
113 
114  if (sscanf(tc->str, "%d:%d:%d%c%d", &hh, &mm, &ss, &c, &ff) != 5) {
115  av_log(avcl, AV_LOG_ERROR, "unable to parse timecode, "
116  "syntax: hh:mm:ss[:;.]ff\n");
117  return -1;
118  }
119 
120  tc->drop = c != ':'; // drop if ';', '.', ...
121 
122  ret = avpriv_check_timecode_rate(avcl, tc->rate, tc->drop);
123  if (ret < 0)
124  return ret;
125 
126  fps = (tc->rate.num + tc->rate.den/2) / tc->rate.den;
127  tc->start = (hh*3600 + mm*60 + ss) * fps + ff;
128 
129  if (tc->drop) { /* adjust frame number */
130  int tmins = 60*hh + mm;
131  tc->start -= 2 * (tmins - tmins/10);
132  }
133  return 0;
134 }
135 
136 int ff_framenum_to_drop_timecode(int frame_num)
137 {
138  return avpriv_framenum_to_drop_timecode(frame_num);
139 }
140 
141 uint32_t ff_framenum_to_smtpe_timecode(unsigned frame, int fps, int drop)
142 {
143  return avpriv_framenum_to_smpte_timecode(frame, fps, drop);
144 }
145 
146 int ff_init_smtpe_timecode(void *avcl, struct ff_timecode *tc)
147 {
148  return avpriv_init_smpte_timecode(avcl, tc);
149 }
150 #endif
int num
numerator
Definition: rational.h:44
#define tc
Definition: regdef.h:69
set threshold d
initialize output if(nPeaks >3)%at least 3 peaks in spectrum for trying to find f0 nf0peaks
window constants for m
frame
Definition: stft.m:14
Libavcodec version macros.
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
ret
Definition: avfilter.c:821
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
void * buf
Definition: avisynth_c.h:594
rational number numerator/denominator
Definition: rational.h:43
#define snprintf
Definition: snprintf.h:34
static double c[64]
int den
denominator
Definition: rational.h:45