Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PR] avcodec/encode: add a helper to convert from a duration to samples (PR #21746)
@ 2026-02-13 17:02 James Almer via ffmpeg-devel
  0 siblings, 0 replies; only message in thread
From: James Almer via ffmpeg-devel @ 2026-02-13 17:02 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: James Almer

PR #21746 opened by James Almer (jamrial)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21746
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21746.patch


>From c42c5fe94c4332a6ef758dca617f5e8cb8aa894b Mon Sep 17 00:00:00 2001
From: James Almer <jamrial@gmail.com>
Date: Fri, 13 Feb 2026 13:00:10 -0300
Subject: [PATCH 1/6] avcodec/encode: add a helper to convert from a duration
 to samples

Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavcodec/encode.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/libavcodec/encode.h b/libavcodec/encode.h
index 82bd3c9431..b66b387030 100644
--- a/libavcodec/encode.h
+++ b/libavcodec/encode.h
@@ -99,6 +99,18 @@ static av_always_inline int64_t ff_samples_to_time_base(const AVCodecContext *av
                         avctx->time_base);
 }
 
+/**
+ * Rescale from time base to AVCodecContext.sample_rate.
+ */
+static av_always_inline int64_t ff_samples_from_time_base(const AVCodecContext *avctx,
+                                                          int64_t duration)
+{
+    if (!duration)
+        return duration;
+    return av_rescale_q(duration, avctx->time_base,
+                        (AVRational){ 1, avctx->sample_rate });
+}
+
 /**
  * Check if the elements of codec context matrices (intra_matrix, inter_matrix or
  * chroma_intra_matrix) are within the specified range.
-- 
2.52.0


>From 6ce1c604682b9a4a2429aaac2e0b8cd4d3854f2c Mon Sep 17 00:00:00 2001
From: James Almer <jamrial@gmail.com>
Date: Fri, 13 Feb 2026 13:01:28 -0300
Subject: [PATCH 2/6] avcodec/libfdk-aacenc: rescale packet duration when
 calculating discarded samples

Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavcodec/libfdk-aacenc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libavcodec/libfdk-aacenc.c b/libavcodec/libfdk-aacenc.c
index 1a3f127d37..6c28633fba 100644
--- a/libavcodec/libfdk-aacenc.c
+++ b/libavcodec/libfdk-aacenc.c
@@ -530,7 +530,7 @@ static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
     ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
                        &avpkt->duration);
 
-    discard_padding = avctx->frame_size - avpkt->duration;
+    discard_padding = avctx->frame_size - ff_samples_from_time_base(avctx, avpkt->duration);
     // Check if subtraction resulted in an overflow
     if ((discard_padding < avctx->frame_size) != (avpkt->duration > 0)) {
         av_log(avctx, AV_LOG_ERROR, "discard padding overflow\n");
-- 
2.52.0


>From 35bbcdf7efdacd9f92dfd8cac6b676d169d08669 Mon Sep 17 00:00:00 2001
From: James Almer <jamrial@gmail.com>
Date: Fri, 13 Feb 2026 13:01:54 -0300
Subject: [PATCH 3/6] avcodec/libmp3lame: rescale packet duration when
 calculating discarded samples

Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavcodec/libmp3lame.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libavcodec/libmp3lame.c b/libavcodec/libmp3lame.c
index 01b8985b8c..339505ccfe 100644
--- a/libavcodec/libmp3lame.c
+++ b/libavcodec/libmp3lame.c
@@ -285,7 +285,7 @@ static int mp3lame_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
         ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
                            &avpkt->duration);
 
-        discard_padding = avctx->frame_size - avpkt->duration;
+        discard_padding = avctx->frame_size - ff_samples_from_time_base(avctx, avpkt->duration);
         // Check if subtraction resulted in an overflow
         if ((discard_padding < avctx->frame_size) != (avpkt->duration > 0)) {
             av_log(avctx, AV_LOG_ERROR, "discard padding overflow\n");
-- 
2.52.0


>From 7f3199617cc6add47b7d74e4ea1c658b74baf9c3 Mon Sep 17 00:00:00 2001
From: James Almer <jamrial@gmail.com>
Date: Fri, 13 Feb 2026 13:02:07 -0300
Subject: [PATCH 4/6] avcodec/libopusenc: rescale packet duration when
 calculating discarded samples

Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavcodec/libopusenc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libavcodec/libopusenc.c b/libavcodec/libopusenc.c
index 131886c8ea..927776bbca 100644
--- a/libavcodec/libopusenc.c
+++ b/libavcodec/libopusenc.c
@@ -511,7 +511,7 @@ static int libopus_encode(AVCodecContext *avctx, AVPacket *avpkt,
     ff_af_queue_remove(&opus->afq, opus->opts.packet_size,
                        &avpkt->pts, &avpkt->duration);
 
-    discard_padding = opus->opts.packet_size - avpkt->duration;
+    discard_padding = opus->opts.packet_size - ff_samples_from_time_base(avctx, avpkt->duration);
     // Check if subtraction resulted in an overflow
     if ((discard_padding < opus->opts.packet_size) != (avpkt->duration > 0))
         return AVERROR(EINVAL);
-- 
2.52.0


>From ff2b1a636f5d33848da4f2e37d33fba6bb6cec6c Mon Sep 17 00:00:00 2001
From: James Almer <jamrial@gmail.com>
Date: Fri, 13 Feb 2026 13:02:20 -0300
Subject: [PATCH 5/6] avcodec/libvorbisenc: rescale packet duration when
 calculating discarded samples

Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavcodec/libvorbisenc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libavcodec/libvorbisenc.c b/libavcodec/libvorbisenc.c
index a3f02f3cb9..c54ebebae8 100644
--- a/libavcodec/libvorbisenc.c
+++ b/libavcodec/libvorbisenc.c
@@ -421,7 +421,7 @@ static int libvorbis_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
 
         ff_af_queue_remove(&s->afq, duration, &avpkt->pts, &avpkt->duration);
 
-        discard_padding = ff_samples_to_time_base(avctx, duration) - avpkt->duration;
+        discard_padding = duration - ff_samples_from_time_base(avctx, avpkt->duration);
         if (discard_padding > 0) {
             uint8_t *side_data = av_packet_new_side_data(avpkt,
                                                          AV_PKT_DATA_SKIP_SAMPLES,
-- 
2.52.0


>From 9241f44121715428a71bb42383c3661663e1cf93 Mon Sep 17 00:00:00 2001
From: James Almer <jamrial@gmail.com>
Date: Fri, 13 Feb 2026 13:03:26 -0300
Subject: [PATCH 6/6] avcodec/opus/enc: rescale packet duration when
 calculating discarded samples

Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavcodec/opus/enc.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/libavcodec/opus/enc.c b/libavcodec/opus/enc.c
index 65e6a09575..587e4ed69d 100644
--- a/libavcodec/opus/enc.c
+++ b/libavcodec/opus/enc.c
@@ -548,7 +548,7 @@ static int opus_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
                              const AVFrame *frame, int *got_packet_ptr)
 {
     OpusEncContext *s = avctx->priv_data;
-    int ret, frame_size, alloc_size = 0;
+    int ret, frame_size, discard_padding, alloc_size = 0;
 
     if (frame) { /* Add new frame to queue */
         if ((ret = ff_af_queue_add(&s->afq, frame)) < 0)
@@ -600,11 +600,13 @@ static int opus_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
 
     /* Remove samples from queue and skip if needed */
     ff_af_queue_remove(&s->afq, s->packet.frames*frame_size, &avpkt->pts, &avpkt->duration);
-    if (s->packet.frames*frame_size > avpkt->duration) {
+
+    discard_padding = s->packet.frames*frame_size - ff_samples_from_time_base(avctx, avpkt->duration);
+    if (discard_padding > 0) {
         uint8_t *side = av_packet_new_side_data(avpkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
         if (!side)
             return AVERROR(ENOMEM);
-        AV_WL32(&side[4], s->packet.frames*frame_size - avpkt->duration);
+        AV_WL32(&side[4], discard_padding);
     }
 
     *got_packet_ptr = 1;
-- 
2.52.0

_______________________________________________
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:[~2026-02-13 17:03 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-02-13 17:02 [FFmpeg-devel] [PR] avcodec/encode: add a helper to convert from a duration to samples (PR #21746) James Almer 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