PR #26378 opened by yuygfgg
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26378
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26378.patch
# Summary of changes
In VBR mode, `enable_pcr_generation_for_stream()` falls back to a hardcoded
512-sample frame size whenever `av_get_audio_frame_duration2(codecpar, 0)`
returns 0, even if `codecpar->frame_size` is already set.
For 48 kHz AAC (1024 samples/frame), the 512-sample fallback selects a 96 ms
PCR period. Because periodic PCRs are emitted at PES boundaries, a stream
with one frame per PES ends up spacing PCRs 5 frames apart (106.667 ms),
exceeding the 100 ms target.
Falling back to `codecpar->frame_size` before using the 512-sample default
fixes this and selects a 4-frame period (85.333 ms) instead.
## Verification
With `-muxdelay 0` (one AAC frame per PES) and `+omit_rai` (to isolate
periodic PCRs):
```sh
ffmpeg -v verbose -f lavfi -i anullsrc=r=48000:cl=stereo -t 2 \
-c:a aac -muxdelay 0 -mpegts_flags +omit_rai -f mpegts out.ts
```
- **Before:** logged `pcr_period=96ms`, max PCR interval in output: `106.667 ms`
- **After:** logged `pcr_period=85ms`, max PCR interval in output: `85.333 ms`
Output remains byte-identical when `frame_size` is unset, when `pcr_period`
is explicitly set (40 ms), or in CBR mode (768 kbit/s).
>From 0de00f89d2045831992f5dcf56a1c599754a396b Mon Sep 17 00:00:00 2001
From: yuygfgg <[email protected]>
Date: Fri, 27 Nov 2026 00:11:57 +0800
Subject: [PATCH] avformat/mpegtsenc: use the audio frame size for PCR
scheduling
Use codecpar->frame_size when the duration helper cannot determine the
frame size without a packet. At 48 kHz and 1024 samples per frame, the
512-sample fallback can leave 106.7 ms between PCRs. Using the known
frame size reduces the maximum interval to 85.3 ms in an AAC stream.
Signed-off-by: yuygfgg <[email protected]>
---
libavformat/mpegtsenc.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
index 582a9a3e605..ad03abb9d2e 100644
--- a/libavformat/mpegtsenc.c
+++ b/libavformat/mpegtsenc.c
@@ -1105,6 +1105,8 @@ static void
enable_pcr_generation_for_stream(AVFormatContext *s, AVStream *pcr_s
int64_t frame_period = 0;
if (pcr_st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
int frame_size = av_get_audio_frame_duration2(pcr_st->codecpar, 0);
+ if (!frame_size)
+ frame_size = pcr_st->codecpar->frame_size;
if (!frame_size) {
av_log(s, AV_LOG_WARNING, "frame size not set\n");
frame_size = 512;
--
2.54.1
_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]