Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH] lavc/mpeg12enc: Allow timecode to be overwritten (PR #20839)
@ 2025-11-05 12:03 jnightingale-hei via ffmpeg-devel
  0 siblings, 0 replies; only message in thread
From: jnightingale-hei via ffmpeg-devel @ 2025-11-05 12:03 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: jnightingale-hei

PR #20839 opened by jnightingale-hei
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20839
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20839.patch

If a custom timecode is specified in the frame side data using `AV_FRAME_DATA_GOP_TIMECODE` this timecode is then written into the encoded frame data.


>From fd61bde14156b43b88d3daf6577e4a40f48f8a04 Mon Sep 17 00:00:00 2001
From: Jonny Nightingale <jonny.nightingale@hawkeyeinnovations.com>
Date: Thu, 31 Jul 2025 12:16:26 +0100
Subject: [PATCH] lavc/mpeg12enc: Allow timecode to be overwritten

If a custom timecode is specified in the frame side data using
AV_FRAME_DATA_GOP_TIMECODE this timecode is then written into the
encoded frame data.

Signed-off-by: Jonny Nightingale <jonny.nightingale@hawkeyeinnovations.com>
---
 libavcodec/mpeg12enc.c | 36 ++++++++++++++++++++++++++++++------
 1 file changed, 30 insertions(+), 6 deletions(-)

diff --git a/libavcodec/mpeg12enc.c b/libavcodec/mpeg12enc.c
index fb480d0eec..c391e726fa 100644
--- a/libavcodec/mpeg12enc.c
+++ b/libavcodec/mpeg12enc.c
@@ -144,6 +144,15 @@ static void put_header(MPVEncContext *const s, uint32_t header)
     put_bits32(&s->pb, header);
 }
 
+static unsigned bcd2uint(uint8_t bcd)
+{
+    unsigned low  = bcd & 0xf;
+    unsigned high = bcd >> 4;
+    if (low > 9 || high > 9)
+        return 0;
+    return low + 10*high;
+}
+
 /* put sequence header if needed */
 static void mpeg1_encode_sequence_header(MPEG12EncContext *mpeg12)
 {
@@ -155,6 +164,7 @@ static void mpeg1_encode_sequence_header(MPEG12EncContext *mpeg12)
     int64_t best_aspect_error = INT64_MAX;
     AVRational aspect_ratio = s->c.avctx->sample_aspect_ratio;
     int aspect_ratio_info;
+    const AVFrameSideData *timecode_side_data;
 
     put_bits_assume_flushed(&s->pb);
 
@@ -283,12 +293,26 @@ static void mpeg1_encode_sequence_header(MPEG12EncContext *mpeg12)
     }
 
     put_header(s, GOP_START_CODE);
-    put_bits(&s->pb, 1, mpeg12->drop_frame_timecode);    // drop frame flag
-    /* time code: we must convert from the real frame rate to a
-     * fake MPEG frame rate in case of low frame rate */
-    fps       = (framerate.num + framerate.den / 2) / framerate.den;
-    time_code = s->c.cur_pic.ptr->coded_picture_number +
-                mpeg12->timecode_frame_start;
+
+    timecode_side_data = av_frame_get_side_data(s->c.cur_pic.ptr->f, AV_FRAME_DATA_GOP_TIMECODE);
+    if (timecode_side_data && timecode_side_data->data && timecode_side_data->size >= 8) {
+        int64_t tc25bit = *(int64_t *)timecode_side_data->data;
+        unsigned hh = tc25bit>>19 & 0x1f; // 5-bit hours
+        unsigned mm = tc25bit>>13 & 0x3f; // 6-bit minutes
+        unsigned ss = tc25bit>>6  & 0x3f; // 6-bit seconds
+        unsigned ff = tc25bit     & 0x3f; // 6-bit frames
+        mpeg12->drop_frame_timecode = !!(tc25bit & 1<<24);  // 1-bit drop
+        fps = (framerate.num + framerate.den / 2) / framerate.den;
+        time_code = (hh*3600 + mm*60 + ss) * fps + ff;
+        put_bits(&s->pb, 1, mpeg12->drop_frame_timecode);    // drop frame flag
+    } else {
+        put_bits(&s->pb, 1, mpeg12->drop_frame_timecode);    // drop frame flag
+        /* time code: we must convert from the real frame rate to a
+         * fake MPEG frame rate in case of low frame rate */
+        fps       = (framerate.num + framerate.den / 2) / framerate.den;
+        time_code = s->c.cur_pic.ptr->coded_picture_number +
+                    mpeg12->timecode_frame_start;
+    }
 
     mpeg12->gop_picture_number = s->c.cur_pic.ptr->coded_picture_number;
 
-- 
2.49.1

_______________________________________________
ffmpeg-devel mailing list -- ffmpeg-devel@ffmpeg.org
To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2025-11-05 12:03 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-11-05 12:03 [FFmpeg-devel] [PATCH] lavc/mpeg12enc: Allow timecode to be overwritten (PR #20839) jnightingale-hei via ffmpeg-devel

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git