Skip to content

Effort free video editing!

Seek audio back by the decoder's pre-roll

WyattBlue3bc116882286
diff --git a/patches/ffmpeg.patch b/patches/ffmpeg.patch
index 081b6954..bec7a4df 100644
--- a/patches/ffmpeg.patch
+++ b/patches/ffmpeg.patch
@@ -20,6 +20,61 @@ index f5e1fbb..fb4ca4c 100755
gmp
libaribb24
libastcenc
+diff --git a/libavcodec/mpegaudiodec_template.c b/libavcodec/mpegaudiodec_template.c
+--- a/libavcodec/mpegaudiodec_template.c
++++ b/libavcodec/mpegaudiodec_template.c
+@@ -41,6 +41,7 @@
+ #include "decode.h"
+ #include "get_bits.h"
+ #include "mathops.h"
++#include "mpegaudiodata.h"
+ #include "mpegaudiodsp.h"
+
+ /*
+@@ -1558,6 +1559,35 @@
+ return nb_frames * 32 * sizeof(OUT_INT) * s->hdr.nb_channels;
+ }
+
++/**
++ * Samples a decoder started inside the stream outputs before its output is
++ * exact: the synthesis history, then for layer III the IMDCT overlap and
++ * the granules whose main data begins before the first frame decoded. The
++ * bit reservoir reach is bounded for the smallest frames this stream format
++ * allows, as an encoder may use all of it.
++ */
++static int seek_preroll(const MPADecodeHeader2 *hdr)
++{
++ const int synth_history = 512 - 32;
++ const int imdct_overlap = 576;
++
++ if (hdr->layer != 3)
++ return synth_history;
++
++ const int samples_per_frame = hdr->lsf ? 576 : 1152;
++ const int max_main_data_begin = hdr->lsf ? 255 : 511;
++ const int side_info_size = hdr->lsf ? (hdr->nb_channels == 1 ? 9 : 17)
++ : (hdr->nb_channels == 1 ? 17 : 32);
++ const int min_frame_size = ff_mpa_bitrate_tab[hdr->lsf][2][1] * 144000 /
++ (hdr->sample_rate << hdr->lsf);
++ /* header and CRC */
++ const int min_main_data = min_frame_size - 4 - 2 - side_info_size;
++
++ return synth_history + imdct_overlap +
++ (max_main_data_begin + min_main_data - 1) / min_main_data *
++ samples_per_frame;
++}
++
+ static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
+ int *got_frame_ptr, AVPacket *avpkt)
+ {
+@@ -1597,6 +1627,7 @@
+ (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
+ if (!avctx->bit_rate)
+ avctx->bit_rate = s->hdr.bit_rate;
++ avctx->seek_preroll = FFMAX(avctx->seek_preroll, seek_preroll(&s->hdr));
+
+ if (s->hdr.frame_size <= 0) {
+ av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
diff --git a/libavdevice/avfoundation.m b/libavdevice/avfoundation.m
index 8b7e728..c10ab93 100644
--- a/libavdevice/avfoundation.m
diff --git a/src/ffmpeg.nim b/src/ffmpeg.nim
index 52f5cd1a..476aaa74 100644
--- a/src/ffmpeg.nim
+++ b/src/ffmpeg.nim
@@ -239,6 +239,7 @@ type
video_delay*: cint
ch_layout*: AVChannelLayout
sample_rate*: cint
+ seek_preroll*: cint
AVCodecConfig* {.importc: "enum AVCodecConfig", header: "<libavcodec/codec.h>".} = enum
AV_CODEC_CONFIG_PIX_FORMAT,
@@ -307,6 +308,7 @@ type
subtitle_header_size*: cint
pkt_timebase*: AVRational
skip_frame*: AVDiscard
+ seek_preroll*: cint
# ... other fields omitted for brevity
proc `==`*(a: AVSampleFormat, b: cint): bool = cint(ord(a)) == b
diff --git a/src/render/audio.nim b/src/render/audio.nim
index a7e1b0fb..f0884558 100644
--- a/src/render/audio.nim
+++ b/src/render/audio.nim
@@ -275,8 +275,17 @@ proc get(getter: Getter, start, endSample: int, sequential = false): seq[int16]
totalSamples = held
else:
getter.carry.setLen(0)
- # Seek to the approximate position
- if av_seek_frame(container.formatContext, stream.index, startPts,
+ # A decoder started inside the stream needs its pre-roll before its
+ # output is exact (AAC's overlap, MP3's bit reservoir), so seek that much
+ # earlier; the frames before `start` are dropped below. The probe and the
+ # decoder each may declare it. MP3 declares a worst case reaching
+ # seconds at low MPEG-2 bitrates, more than real files use, so it's
+ # capped to keep many cuts cheap.
+ const maxPrerollSeconds = 1.0
+ let preroll = min(max(stream.codecpar.seek_preroll, decoderCtx.seek_preroll).float /
+ sampleRate.float, maxPrerollSeconds)
+ let seekPts = startPts - int64(preroll * timeBase.den.float / timeBase.num.float)
+ if av_seek_frame(container.formatContext, stream.index, seekPts,
AVSEEK_FLAG_BACKWARD) < 0:
# If seeking fails, fall back to reading from beginning
discard av_seek_frame(container.formatContext, stream.index, 0,