diff mbox series

[FFmpeg-devel,v4,2/7] avutil/timecode: add function av_timecode_get_smpte()

Message ID 1593006200-23911-2-git-send-email-lance.lmwang@gmail.com
State Accepted
Commit 79723c2a878b13110ce3e86db68c33f4f71f6da7
Headers show
Series [FFmpeg-devel,v4,1/7] FATE: add h264 timecode side data test | expand

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Lance Wang June 24, 2020, 1:43 p.m. UTC
From: Limin Wang <lance.lmwang@gmail.com>

Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
---
 libavutil/timecode.c | 32 ++++++++++++++++++++++++++++++++
 libavutil/timecode.h | 13 +++++++++++++
 2 files changed, 45 insertions(+)
diff mbox series

Patch

diff --git a/libavutil/timecode.c b/libavutil/timecode.c
index 60077ba..cca53d7 100644
--- a/libavutil/timecode.c
+++ b/libavutil/timecode.c
@@ -81,6 +81,38 @@  uint32_t av_timecode_get_smpte_from_framenum(const AVTimecode *tc, int framenum)
            (hh % 10);        // units of hours
 }
 
+uint32_t av_timecode_get_smpte(AVRational rate, int drop, int hh, int mm, int ss, int ff)
+{
+    uint32_t tc = 0;
+    uint32_t frames;
+
+    /* For SMPTE 12-M timecodes, frame count is a special case if > 30 FPS.
+       See SMPTE ST 12-1:2014 Sec 12.1 for more info. */
+    if (av_cmp_q(rate, (AVRational) {30, 1}) == 1) {
+        frames = ff / 2;
+        if (ff % 2 == 1) {
+            if (av_cmp_q(rate, (AVRational) {50, 1}) == 0)
+                tc |= (1 << 7);
+            else
+                tc |= (1 << 23);
+        }
+    } else {
+        frames = ff;
+    }
+
+    tc |= drop << 30;
+    tc |= (frames / 10) << 28;
+    tc |= (frames % 10) << 24;
+    tc |= (ss / 10) << 20;
+    tc |= (ss % 10) << 16;
+    tc |= (mm / 10) << 12;
+    tc |= (mm % 10) << 8;
+    tc |= (hh / 10) << 4;
+    tc |= (hh  % 10);
+
+    return tc;
+}
+
 char *av_timecode_make_string(const AVTimecode *tc, char *buf, int framenum)
 {
     int fps = tc->fps;
diff --git a/libavutil/timecode.h b/libavutil/timecode.h
index 37c1361..ab38e66 100644
--- a/libavutil/timecode.h
+++ b/libavutil/timecode.h
@@ -71,6 +71,19 @@  int av_timecode_adjust_ntsc_framenum2(int framenum, int fps);
 uint32_t av_timecode_get_smpte_from_framenum(const AVTimecode *tc, int framenum);
 
 /**
+ * Convert sei info to SMPTE 12M binary representation.
+ *
+ * @param rate     frame rate in rational form
+ * @param drop     drop flag
+ * @param hh       hour
+ * @param mm       minute
+ * @param ss       second
+ * @param ff       frame number
+ * @return         the SMPTE binary representation
+ */
+uint32_t av_timecode_get_smpte(AVRational rate, int drop, int hh, int mm, int ss, int ff);
+
+/**
  * Load timecode string in buf.
  *
  * @param buf      destination buffer, must be at least AV_TIMECODE_STR_SIZE long