PR #26287 opened by Baptiste Coudurier (bcoudurier)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26287
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26287.patch

Based on https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26052 (first commit); 
fixes the two pre-existing E-AC-3 issues noted there.

- movenc: automatic fragment cuts (frag_duration etc.) no longer split an 
aggregated E-AC-3 sample (durations were 2560/512/2048/1024, now 4x1536).
- hlsenc: the final partial E-AC-3 sample goes into the last fMP4 segment 
instead of leaving an empty byte range.

Tests: fate-mov-fragment-eac3-boundary, fate-hls-fmp4-eac3-tail.

🤖 Generated with [Claude Code](https://claude.com/claude-code)



From efec4a55d966ef0afc14fdc86915b22421a6e8ba Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Fri, 13 Nov 2026 21:25:15 -0800
Subject: [PATCH 1/3] avformat/movenc: preserve the final audio packet duration
 in fragments

Keep it when a fragmented edit cannot trim padding, clip sidx at the edit 
start, and let the next boundary DTS finalize the previous sample.
In single-audio fMP4 HLS, take segment durations from the stored samples.

Signed-off-by: Baptiste Coudurier <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 libavformat/hlsenc.c                     |  31 +++++++
 libavformat/movenc.c                     |  55 ++++++++---
 libavformat/movenc.h                     |   4 +
 tests/api/Makefile                       |   1 +
 tests/api/api-hlsenc-test.c              | 111 +++++++++++++++++++++++
 tests/api/api-movenc-test.c              |  74 +++++++++++++++
 tests/fate/api.mak                       |   5 +
 tests/fate/hlsenc.mak                    |  50 +++++++++-
 tests/fate/mov.mak                       |  21 +++++
 tests/ref/fate/api-hlsenc                |   2 +
 tests/ref/fate/api-movenc-global-sidx    |   4 +
 tests/ref/fate/hls-fmp4-aac-discont      |   2 +
 tests/ref/fate/hls-fmp4-aac-tail         |   2 +
 tests/ref/fate/hls-fmp4-eac3-boundary    |   4 +
 tests/ref/fate/hls-fmp4-eac3-cut         |   8 ++
 tests/ref/fate/mov-fragment-aac-tail     |   2 +
 tests/ref/fate/mov-hls-alac-boundary     |   2 +
 tests/ref/fate/mov-hls-alac-boundary-max |   2 +
 18 files changed, 366 insertions(+), 14 deletions(-)
 create mode 100644 tests/api/api-hlsenc-test.c
 create mode 100644 tests/ref/fate/api-hlsenc
 create mode 100644 tests/ref/fate/hls-fmp4-aac-discont
 create mode 100644 tests/ref/fate/hls-fmp4-aac-tail
 create mode 100644 tests/ref/fate/hls-fmp4-eac3-boundary
 create mode 100644 tests/ref/fate/hls-fmp4-eac3-cut
 create mode 100644 tests/ref/fate/mov-fragment-aac-tail
 create mode 100644 tests/ref/fate/mov-hls-alac-boundary
 create mode 100644 tests/ref/fate/mov-hls-alac-boundary-max

diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index ab529d54311..8e273e62265 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -139,6 +139,7 @@ typedef struct VariantStream {
     int new_start;
     int start_pts_from_audio;
     double dpp;           // duration per packet
+    int64_t fmp4_start_duration;
     int64_t start_pts;
     int64_t end_pts;
     int64_t video_lastpos;
@@ -2476,6 +2477,31 @@ static int64_t append_single_file(AVFormatContext *s, 
VariantStream *vs)
 
     return ret;
 }
+
+/* For a single audio stream in fMP4, the duration MOV stored since the last 
cut. */
+static int64_t fmp4_audio_stored(HLSContext *hls, VariantStream *vs)
+{
+#if CONFIG_MP4_MUXER
+    AVFormatContext *oc = vs->avf;
+
+    if (hls->segment_type == SEGMENT_TYPE_FMP4 && oc->nb_streams == 1 &&
+        oc->streams[0]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
+        return ff_mov_get_audio_duration(oc, 0) - vs->fmp4_start_duration;
+#endif
+    return -1;
+}
+
+static double fmp4_audio_duration(HLSContext *hls, VariantStream *vs, double 
duration)
+{
+    int64_t stored = fmp4_audio_stored(hls, vs);
+
+    if (stored > 0) {
+        duration = stored * av_q2d(vs->avf->streams[0]->time_base);
+        vs->fmp4_start_duration += stored;
+    }
+    return duration;
+}
+
 static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
 {
     HLSContext *hls = s->priv_data;
@@ -2572,6 +2598,8 @@ static int hls_write_packet(AVFormatContext *s, AVPacket 
*pkt)
     }
 
     can_split = can_split && (pkt->pts - vs->end_pts > 0);
+    /* MOV may still buffer the whole audio sample: cut once it is stored. */
+    can_split = can_split && fmp4_audio_stored(hls, vs) != 0;
     if (vs->packets_written && can_split && (av_rescale_q(pkt->pts, 
st->time_base, AV_TIME_BASE_Q) - vs->start_pts
                                                           >= end_pts)) {
         int64_t new_start_pos;
@@ -2684,6 +2712,7 @@ static int hls_write_packet(AVFormatContext *s, AVPacket 
*pkt)
         }
 
         cur_duration = (double)(pkt->pts - vs->end_pts) * st->time_base.num / 
st->time_base.den;
+        cur_duration = fmp4_audio_duration(hls, vs, cur_duration);
         ret = hls_append_segment(s, hls, vs, cur_duration, vs->start_pos, 
vs->size);
         vs->end_pts = pkt->pts;
         vs->duration = 0;
@@ -2855,6 +2884,8 @@ static int hls_write_trailer(struct AVFormatContext *s)
         if (ret < 0)
             goto failed;
 
+        vs->duration = fmp4_audio_duration(hls, vs, vs->duration + vs->dpp);
+        vs->dpp = 0;
         vs->size = range_length;
         ret = hlsenc_io_close(s, &vs->out, filename);
         if (ret < 0) {
diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index e0057dba3bf..6452bee8d9d 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -6097,7 +6097,7 @@ static int mov_add_tfra_entries(AVIOContext *pb, 
MOVMuxContext *mov, int tracks,
         // away parts of the media track using an edit list,
         // and the corresponding start presentation time is zero.
         if (info->time < 0) {
-            info->duration += info->time;
+            info->duration = FFMAX(track->end_pts, 0);
             info->time = 0;
         }
         info->tfrf_offset = 0;
@@ -6216,7 +6216,7 @@ static int mov_write_sidx_tag(AVIOContext *pb,
 
         // pts<0 should be cut away using edts
         if (presentation_time < 0) {
-            duration += presentation_time;
+            duration = FFMAX(track->end_pts, 0);
             presentation_time = 0;
         }
     } else {
@@ -6949,6 +6949,11 @@ static int mov_finish_fragment(MOVMuxContext *mov, 
MOVTrack *track,
                track->cluster, track->entry * sizeof(*track->cluster));
         track->entry_written += track->entry;
     }
+    if (track->par->codec_type == AVMEDIA_TYPE_AUDIO) {
+        int64_t first_dts = track->cluster[0].dts - track->start_dts;
+        track->written_audio_duration = 
av_sat_add64(track->written_audio_duration,
+                                                     track->track_duration - 
first_dts);
+    }
     track->entry = 0;
     track->entries_flushed = 0;
     track->end_reliable = 0;
@@ -7220,12 +7225,23 @@ static int check_pkt(AVFormatContext *s, MOVTrack *trk, 
AVPacket *pkt,
     return 0;
 }
 
+int64_t ff_mov_get_audio_duration(AVFormatContext *s, int stream_index)
+{
+    const MOVTrack *track = s->streams[stream_index]->priv_data;
+    int64_t pending = 0;
+
+    if (track->entry)
+        pending = track->track_duration - (track->cluster[0].dts - 
track->start_dts);
+    return av_sat_add64(track->written_audio_duration, pending);
+}
+
 int ff_mov_set_fragment_end_hint(AVFormatContext *s, int stream_index,
                                  const AVPacket *pkt, AVRational src_time_base)
 {
     MOVMuxContext *mov = s->priv_data;
     AVStream *st;
     MOVTrack *track;
+    const struct eac3_info *info;
     int64_t offset, dts, pts, candidate_duration;
 
     if (!(mov->flags & FF_MOV_FLAG_FRAGMENT) ||
@@ -7235,22 +7251,31 @@ int ff_mov_set_fragment_end_hint(AVFormatContext *s, 
int stream_index,
 
     st = s->streams[stream_index];
     track = st->priv_data;
+    info = track->eac3_priv;
     if (!track->entry || track->start_dts == AV_NOPTS_VALUE)
         return 0;
 
-    if (ff_get_muxer_ts_offset(s, stream_index, &offset) < 0)
-        return 0;
+    if (info && info->pkt && info->pkt->size) {
+        /* A buffered E-AC-3 syncframe already starts the next MP4 sample. */
+        pkt = info->pkt;
+        dts = pkt->dts;
+        pts = pkt->pts;
+    } else {
+        if (ff_get_muxer_ts_offset(s, stream_index, &offset) < 0)
+            return 0;
 
-    dts = av_rescale_q(pkt->dts, src_time_base, st->time_base) + offset;
-    pts = pkt->pts == AV_NOPTS_VALUE
-        ? AV_NOPTS_VALUE
-        : av_rescale_q(pkt->pts, src_time_base, st->time_base) + offset;
-    if (track->dts_shift != AV_NOPTS_VALUE)
-        dts += track->dts_shift;
+        dts = av_rescale_q(pkt->dts, src_time_base, st->time_base) + offset;
+        pts = pkt->pts == AV_NOPTS_VALUE
+            ? AV_NOPTS_VALUE
+            : av_rescale_q(pkt->pts, src_time_base, st->time_base) + offset;
+        if (track->dts_shift != AV_NOPTS_VALUE)
+            dts += track->dts_shift;
+    }
 
     candidate_duration = dts - track->start_dts;
     if (dts <= track->cluster[track->entry - 1].dts ||
-        candidate_duration < 0 || candidate_duration >= track->track_duration)
+        dts - (uint64_t)track->cluster[track->entry - 1].dts > INT_MAX ||
+        candidate_duration < 0 || candidate_duration == track->track_duration)
         return 0;
 
     track->track_duration = candidate_duration;
@@ -7689,16 +7714,22 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket 
*pkt)
     sd = av_packet_side_data_get(pkt->side_data, pkt->side_data_elems, 
AV_PKT_DATA_SKIP_SAMPLES);
     if (sd && sd->size >= 10 && trk->par->frame_size) {
         int coded_samples = av_get_audio_frame_duration2(trk->par, pkt->size);
+        int open_ended_edit = mov->use_editlist && (mov->flags & 
FF_MOV_FLAG_FRAGMENT) &&
+                             !(mov->flags & FF_MOV_FLAG_HYBRID_FRAGMENTED);
+
         if (coded_samples <= 0)
             coded_samples = trk->par->frame_size;
         duration = FFMAX(av_rescale_q(coded_samples, (AVRational){ 1, 
trk->par->sample_rate },
                                       trk->st->time_base), pkt->duration);
-        if (mov->use_editlist)
+        /* Only a finite edit can trim trailing padding. */
+        if (mov->use_editlist && !open_ended_edit)
             pkt->duration = duration;
         duration -= av_rescale_q(AV_RL32(sd->data + 4), (AVRational){ 1, 
trk->par->sample_rate },
                                  trk->st->time_base);
         if (duration < 0)
             return AVERROR_INVALIDDATA;
+        if (open_ended_edit && pkt->duration > 0)
+            duration = FFMIN(duration, pkt->duration);
     } else
         duration = pkt->duration;
 
diff --git a/libavformat/movenc.h b/libavformat/movenc.h
index 576c98320b2..97396dd439e 100644
--- a/libavformat/movenc.h
+++ b/libavformat/movenc.h
@@ -98,6 +98,7 @@ typedef struct MOVTrack {
     unsigned    timescale;
     uint64_t    time;
     int64_t     track_duration;
+    int64_t     written_audio_duration;
     int         last_sample_is_subtitle_end;
 
     /* multiple stsd */
@@ -306,6 +307,9 @@ typedef struct MOVMuxContext {
 
 int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt);
 
+/* Return the sum of the audio sample durations, written or pending. */
+int64_t ff_mov_get_audio_duration(AVFormatContext *s, int stream_index);
+
 int ff_mov_set_fragment_end_hint(AVFormatContext *s, int stream_index,
                                  const AVPacket *pkt, AVRational 
src_time_base);
 
diff --git a/tests/api/Makefile b/tests/api/Makefile
index c411e82e5f1..f7d78815c0e 100644
--- a/tests/api/Makefile
+++ b/tests/api/Makefile
@@ -3,6 +3,7 @@ APITESTPROGS-$(call ALLYES, LIBASTCENC_ENCODER 
LIBASTCENC_DECODER) += api-astc-p
 APITESTPROGS-$(call ENCDEC, FLAC, FLAC) += api-flac
 APITESTPROGS-$(call DEMDEC, H264, H264) += api-h264
 APITESTPROGS-$(call DEMDEC, H264, H264) += api-h264-slice
+APITESTPROGS-$(CONFIG_HLS_MUXER)        += api-hlsenc
 APITESTPROGS-$(CONFIG_MOV_MUXER)        += api-movenc
 APITESTPROGS-$(CONFIG_MOVTEXT_ENCODER)  += api-movtext
 APITESTPROGS-$(call ALLYES, HEVC_MP4TOANNEXB_BSF HEVC_DECODER) += 
api-hevc-mp4toannexb
diff --git a/tests/api/api-hlsenc-test.c b/tests/api/api-hlsenc-test.c
new file mode 100644
index 00000000000..7741689698d
--- /dev/null
+++ b/tests/api/api-hlsenc-test.c
@@ -0,0 +1,111 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * Mux AAC packets into fMP4 HLS, the packet starting the final segment
+ * having an unknown duration, and print the playlist segment durations.
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include "libavformat/avformat.h"
+#include "libavutil/channel_layout.h"
+#include "libavutil/dict.h"
+#include "libavutil/mem.h"
+
+int main(int argc, char **argv)
+{
+    static const int64_t durations[] = { 1024, 1024, 0, 1024 };
+    static const uint8_t asc[] = { 0x12, 0x10 };
+    AVFormatContext *oc = NULL;
+    AVDictionary *opts = NULL;
+    AVPacket *pkt = NULL;
+    AVStream *st;
+    char segments[1024], line[256];
+    FILE *f;
+    int ret;
+
+    if (argc < 2) {
+        fprintf(stderr, "usage: %s <playlist.m3u8>\n", argv[0]);
+        return 1;
+    }
+
+    ret = avformat_alloc_output_context2(&oc, NULL, "hls", argv[1]);
+    if (ret < 0)
+        return 1;
+    oc->flags |= AVFMT_FLAG_BITEXACT;
+
+    st = avformat_new_stream(oc, NULL);
+    pkt = av_packet_alloc();
+    if (!st || !pkt)
+        goto fail;
+    st->time_base                = (AVRational){ 1, 44100 };
+    st->codecpar->codec_type     = AVMEDIA_TYPE_AUDIO;
+    st->codecpar->codec_id       = AV_CODEC_ID_AAC;
+    st->codecpar->sample_rate    = 44100;
+    av_channel_layout_default(&st->codecpar->ch_layout, 2);
+    st->codecpar->extradata      = av_mallocz(sizeof(asc) + 
AV_INPUT_BUFFER_PADDING_SIZE);
+    if (!st->codecpar->extradata)
+        goto fail;
+    memcpy(st->codecpar->extradata, asc, sizeof(asc));
+    st->codecpar->extradata_size = sizeof(asc);
+
+    snprintf(segments, sizeof(segments), "%s_%%d.m4s", argv[1]);
+    av_dict_set(&opts, "hls_segment_type", "fmp4", 0);
+    av_dict_set(&opts, "hls_time", "0.04", 0);
+    av_dict_set(&opts, "hls_list_size", "0", 0);
+    av_dict_set(&opts, "hls_segment_filename", segments, 0);
+    av_dict_set(&opts, "hls_fmp4_init_filename", "api-hlsenc-init.mp4", 0);
+    ret = avformat_write_header(oc, &opts);
+    av_dict_free(&opts);
+    if (ret < 0)
+        goto fail;
+
+    for (int i = 0; i < FF_ARRAY_ELEMS(durations); i++) {
+        ret = av_new_packet(pkt, 16);
+        if (ret < 0)
+            goto fail;
+        memset(pkt->data, 0, pkt->size);
+        pkt->pts = pkt->dts = i * 1024;
+        pkt->duration = durations[i];
+        pkt->flags = AV_PKT_FLAG_KEY;
+        ret = av_write_frame(oc, pkt);
+        av_packet_unref(pkt);
+        if (ret < 0)
+            goto fail;
+    }
+    if (av_write_trailer(oc) < 0)
+        goto fail;
+
+    f = fopen(argv[1], "r");
+    if (!f)
+        goto fail;
+    while (fgets(line, sizeof(line), f))
+        if (!strncmp(line, "#EXTINF:", 8))
+            fputs(line, stdout);
+    fclose(f);
+
+    av_packet_free(&pkt);
+    avformat_free_context(oc);
+    return 0;
+fail:
+    av_packet_free(&pkt);
+    avformat_free_context(oc);
+    return 1;
+}
diff --git a/tests/api/api-movenc-test.c b/tests/api/api-movenc-test.c
index f4a78313f5b..fde350deb98 100644
--- a/tests/api/api-movenc-test.c
+++ b/tests/api/api-movenc-test.c
@@ -409,6 +409,76 @@ static void test_global_sidx(const char *movflags)
     avformat_close_input(&ic);
 }
 
+// One AAC packet starting 128 samples before a leading skip of 128 samples:
+// sidx must only index the media after the edit start.
+static void test_skip_sidx(const char *movflags, int pkt_duration)
+{
+    AVFormatContext *oc = NULL;
+    AVStream *st;
+    uint8_t pktdata[8] = { 0 }, buf[4096], *side;
+    size_t size;
+    FILE *f;
+
+    if (avformat_alloc_output_context2(&oc, NULL, "mp4", out_path) < 0 ||
+        avio_open(&oc->pb, out_path, AVIO_FLAG_WRITE) < 0)
+        exit(1);
+    oc->flags |= AVFMT_FLAG_BITEXACT;
+    st = avformat_new_stream(oc, NULL);
+    if (!st)
+        exit(1);
+    st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
+    st->codecpar->codec_id = AV_CODEC_ID_AAC;
+    st->codecpar->sample_rate = 44100;
+    st->codecpar->frame_size = 1024;
+    st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
+    st->time_base = (AVRational){ 1, 44100 };
+    st->codecpar->extradata = av_mallocz(sizeof(aac_extradata) + 
AV_INPUT_BUFFER_PADDING_SIZE);
+    if (!st->codecpar->extradata)
+        exit(1);
+    memcpy(st->codecpar->extradata, aac_extradata, sizeof(aac_extradata));
+    st->codecpar->extradata_size = sizeof(aac_extradata);
+    av_dict_set(&opts, "movflags", movflags, 0);
+    if (avformat_write_header(oc, &opts) < 0)
+        exit(1);
+    av_dict_free(&opts);
+
+    av_packet_unref(pkt);
+    pkt->data = pktdata;
+    pkt->size = sizeof(pktdata);
+    pkt->pts = pkt->dts = -128;
+    pkt->duration = pkt_duration;
+    pkt->flags = AV_PKT_FLAG_KEY;
+    side = av_packet_new_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
+    if (!side)
+        exit(1);
+    AV_WL32(side, 128);
+    check(av_write_frame(oc, pkt) >= 0, "Failed to write packet");
+    av_packet_unref(pkt);
+    check(av_write_trailer(oc) >= 0, "Failed to write trailer");
+    avio_closep(&oc->pb);
+    avformat_free_context(oc);
+
+    f = fopen(out_path, "rb");
+    if (!f)
+        exit(1);
+    size = fread(buf, 1, sizeof(buf), f);
+    fclose(f);
+    printf("%s, duration %d: sidx", movflags, pkt_duration);
+    for (size_t i = 0; i + 8 <= size; i++) {
+        size_t p;
+        int refs;
+        if (memcmp(buf + i + 4, "sidx", 4))
+            continue;
+        p = i + 20 + (buf[i + 8] ? 16 : 8) + 2;
+        if (p + 2 > size)
+            break;
+        refs = AV_RB16(buf + p);
+        for (p += 2; refs-- > 0 && p + 12 <= size; p += 12)
+            printf(" %u", AV_RB32(buf + p + 4));
+    }
+    printf("\n");
+}
+
 static void help(void)
 {
     printf("movenc-test [-w] [-s file]\n"
@@ -454,6 +524,10 @@ int main(int argc, char **argv)
     if (out_path) {
         test_global_sidx("frag_keyframe+empty_moov+global_sidx");
         test_global_sidx("frag_keyframe+delay_moov+global_sidx");
+        test_skip_sidx("dash+delay_moov", 54);
+        test_skip_sidx("dash+delay_moov", 182);
+        test_skip_sidx("frag_keyframe+delay_moov+global_sidx", 54);
+        test_skip_sidx("frag_keyframe+delay_moov+global_sidx", 182);
         goto end;
     }
 
diff --git a/tests/fate/api.mak b/tests/fate/api.mak
index 1f3c1b43c76..1e3c581ad64 100644
--- a/tests/fate/api.mak
+++ b/tests/fate/api.mak
@@ -50,6 +50,11 @@ fate-lavf-flv: CMP =
 fate-api-seek: CMD = run $(APITESTSDIR)/api-seek-test$(EXESUF) 
$(TARGET_PATH)/tests/data/lavf/lavf.flv 0 720
 fate-api-seek: CMP = null
 
+FATE_API_LIBAVFORMAT-$(call ALLYES, HLS_MUXER MP4_MUXER FILE_PROTOCOL) += 
fate-api-hlsenc
+fate-api-hlsenc: $(APITESTSDIR)/api-hlsenc-test$(EXESUF)
+fate-api-hlsenc: CLEANFILES = tests/data/fate/api-hlsenc.m3u8 
tests/data/fate/api-hlsenc.m3u8_*.m4s tests/data/fate/api-hlsenc-init.mp4
+fate-api-hlsenc: CMD = run $(APITESTSDIR)/api-hlsenc-test$(EXESUF) 
$(TARGET_PATH)/tests/data/fate/api-hlsenc.m3u8
+
 FATE_API_LIBAVFORMAT-$(call ALLYES, MP4_MUXER ISMV_MUXER) += fate-api-movenc
 fate-api-movenc: $(APITESTSDIR)/api-movenc-test$(EXESUF)
 fate-api-movenc: CMD = run $(APITESTSDIR)/api-movenc-test$(EXESUF)
diff --git a/tests/fate/hlsenc.mak b/tests/fate/hlsenc.mak
index f57275d79ed..b42b46704ea 100644
--- a/tests/fate/hlsenc.mak
+++ b/tests/fate/hlsenc.mak
@@ -337,9 +337,55 @@ fate-hls-fmp4-frag-keyframe-short: CLEANFILES = 
tests/data/hls_fmp4_frag_keyfram
 fate-hls-fmp4-frag-keyframe-short: CMD = sed -n -e /^\#EXT-X-MAP:/p -e 
/^\#EXT-X-BYTERANGE:/p 
$(TARGET_PATH)/tests/data/hls_fmp4_frag_keyframe_short.m3u8
 fate-hls-fmp4-frag-keyframe-short: CMP = diff
 
+# A short AAC packet opens the last segment.
+tests/data/hls_fmp4_aac_discont.m3u8: AAC_TIMING_OPTS = -output_ts_offset 5 
-hls_segment_options movflags=+frag_discont
+tests/data/hls_fmp4_aac_tail.m3u8 tests/data/hls_fmp4_aac_discont.m3u8: 
ffmpeg$(PROGSSUF)$(EXESUF) | tests/data
+       $(M)$(TARGET_EXEC) $(TARGET_PATH)/$< -nostdin -v error -y \
+           -filter_complex sine=d=2.3 -c:a aac -bitexact -hls_time 2.31 \
+           -hls_segment_type fmp4 -hls_flags single_file $(AAC_TIMING_OPTS) \
+           -hls_segment_filename $(TARGET_PATH)/$(@:.m3u8=.mp4) 
$(TARGET_PATH)/$@
+
+FATE_HLSENC_FFMPEG-$(call ALLYES, SINE_FILTER ARESAMPLE_FILTER AAC_ENCODER 
HLS_MUXER MP4_MUXER FILE_PROTOCOL) += fate-hls-fmp4-aac-tail 
fate-hls-fmp4-aac-discont
+fate-hls-fmp4-aac-tail: tests/data/hls_fmp4_aac_tail.m3u8
+fate-hls-fmp4-aac-discont: tests/data/hls_fmp4_aac_discont.m3u8
+fate-hls-fmp4-aac-discont: AAC_TIMING_CASE = discont
+fate-hls-fmp4-aac-tail: AAC_TIMING_CASE = tail
+fate-hls-fmp4-aac-tail fate-hls-fmp4-aac-discont: CLEANFILES = 
tests/data/hls_fmp4_aac_$(AAC_TIMING_CASE).m3u8 
tests/data/hls_fmp4_aac_$(AAC_TIMING_CASE).mp4
+fate-hls-fmp4-aac-tail fate-hls-fmp4-aac-discont: CMD = sed -n -e 
/^\#EXTINF:/p tests/data/hls_fmp4_aac_$(AAC_TIMING_CASE).m3u8
+
+# HLS can request a cut while MOV is buffering the next E-AC-3 sample.
+tests/data/hls_fmp4_eac3_boundary.m3u8: ffmpeg$(PROGSSUF)$(EXESUF) | tests/data
+       $(M)$(TARGET_EXEC) $(TARGET_PATH)/$< -nostdin -v error -y \
+           -i $(TARGET_SAMPLES)/eac3/serenity_english_5.1_1536_small.eac3 
-frames:a 24 -c copy \
+           -hls_time 0.05 -hls_segment_type fmp4 -hls_flags single_file \
+           -hls_segment_filename 
$(TARGET_PATH)/tests/data/hls_fmp4_eac3_boundary.mp4 \
+           $(TARGET_PATH)/$@
+
+FATE_HLSENC_FFMPEG_FFPROBE_SAMPLES-$(call ALLYES, EAC3_DEMUXER AC3_PARSER 
HLS_MUXER MP4_MUXER MOV_DEMUXER FILE_PROTOCOL) += fate-hls-fmp4-eac3-boundary
+fate-hls-fmp4-eac3-boundary: tests/data/hls_fmp4_eac3_boundary.m3u8
+fate-hls-fmp4-eac3-boundary: CLEANFILES = 
tests/data/hls_fmp4_eac3_boundary.m3u8 tests/data/hls_fmp4_eac3_boundary.mp4
+fate-hls-fmp4-eac3-boundary: CMD = run ffprobe$(PROGSSUF)$(EXESUF) -v error 
-fflags +noparse+nofillin \
+    -show_entries packet=dts,duration -of compact 
$(TARGET_PATH)/tests/data/hls_fmp4_eac3_boundary.mp4
+
+# A cut while MOV buffers the whole E-AC-3 sample waits for the next packet.
+tests/data/hls_fmp4_eac3_cut.m3u8: ffmpeg$(PROGSSUF)$(EXESUF) | tests/data
+       $(M)$(TARGET_EXEC) $(TARGET_PATH)/$< -nostdin -v error -y \
+           -i $(TARGET_SAMPLES)/eac3/serenity_english_5.1_1536_small.eac3 
-frames:a 24 -c copy \
+           -hls_time 0.03 -hls_segment_type fmp4 -hls_flags single_file \
+           -hls_segment_filename 
$(TARGET_PATH)/tests/data/hls_fmp4_eac3_cut.mp4 \
+           $(TARGET_PATH)/$@
+
+FATE_HLSENC_FFMPEG_SAMPLES-$(call ALLYES, EAC3_DEMUXER AC3_PARSER HLS_MUXER 
MP4_MUXER FILE_PROTOCOL) += fate-hls-fmp4-eac3-cut
+fate-hls-fmp4-eac3-cut: tests/data/hls_fmp4_eac3_cut.m3u8
+fate-hls-fmp4-eac3-cut: CLEANFILES = tests/data/hls_fmp4_eac3_cut.m3u8 
tests/data/hls_fmp4_eac3_cut.mp4
+fate-hls-fmp4-eac3-cut: CMD = sed -n -e /^\#EXTINF:/p -e 
/^\#EXT-X-BYTERANGE:/p tests/data/hls_fmp4_eac3_cut.m3u8
+fate-hls-fmp4-eac3-cut: CMP = diff
+
 FATE_HLSENC_LAVFI-yes := $(if $(call FRAMECRC), $(FATE_HLSENC_LAVFI-yes))
 
 FATE_FFMPEG_FFPROBE += $(FATE_HLSENC_FFMPEG_FFPROBE-yes)
+FATE_SAMPLES_FFMPEG_FFPROBE += $(FATE_HLSENC_FFMPEG_FFPROBE_SAMPLES-yes)
+FATE_SAMPLES_FFMPEG += $(FATE_HLSENC_FFMPEG_SAMPLES-yes)
 
-FATE_FFMPEG += $(FATE_HLSENC_LAVFI-yes)
-fate-hlsenc: $(FATE_HLSENC-yes) $(FATE_HLSENC_PROBE-yes) 
$(FATE_HLSENC_LAVFI-yes) $(FATE_HLSENC_FFMPEG_FFPROBE-yes)
+FATE_FFMPEG += $(FATE_HLSENC_LAVFI-yes) $(FATE_HLSENC_FFMPEG-yes)
+fate-hlsenc: $(FATE_HLSENC-yes) $(FATE_HLSENC_FFMPEG-yes) 
$(FATE_HLSENC_PROBE-yes) $(FATE_HLSENC_LAVFI-yes) 
$(FATE_HLSENC_FFMPEG_FFPROBE-yes) $(FATE_HLSENC_FFMPEG_FFPROBE_SAMPLES-yes) 
$(FATE_HLSENC_FFMPEG_SAMPLES-yes)
diff --git a/tests/fate/mov.mak b/tests/fate/mov.mak
index 931a1115436..110d795225f 100644
--- a/tests/fate/mov.mak
+++ b/tests/fate/mov.mak
@@ -756,6 +756,27 @@ fate-mov-chpl-utf8: tests/data/chpl-utf8.ffmeta
 FATE_MOV_FFMPEG_FFPROBE-$(call ALLYES, COLOR_FILTER MPEG4_ENCODER 
FFMETADATA_DEMUXER TGP_MUXER MOV_DEMUXER FILE_PROTOCOL) += fate-mov-chpl-utf8
 fate-mov-chpl-utf8: CMD = run_with_temp "$(FFMPEG) -nostdin -v error 
-filter_complex color=c=black:s=2x2:r=1 -f ffmetadata -i 
$(TARGET_PATH)/tests/data/chpl-utf8.ffmeta -map_chapters 0 -frames:v 1 -c:v 
mpeg4 -threads 1 -flags +bitexact -fflags +bitexact -f 3gp -y" 
"ffprobe$(PROGSSUF)$(EXESUF) -loglevel error -bitexact -show_chapters" 3gp
 
+# An open-ended fragment edit cannot trim the short final AAC packet.
+FATE_MOV_FFMPEG_FFPROBE-$(call ALLYES, SINE_FILTER ARESAMPLE_FILTER 
AAC_ENCODER MP4_MUXER MOV_DEMUXER FILE_PROTOCOL) += fate-mov-fragment-aac-tail
+fate-mov-fragment-aac-tail: CMD = run_with_temp "$(FFMPEG) -nostdin -v error 
-filter_complex sine=d=2.3 -c:a aac -bitexact -use_editlist 1 -movflags 
+delay_moov -frag_duration 2310000 -y" "ffprobe$(PROGSSUF)$(EXESUF) -v error 
-bitexact -read_intervals 2.3% -show_entries packet=pts,duration -of compact" 
mp4
+
+# The next packet DTS must extend the previous ALAC sample before HLS flushes.
+tests/data/mov_hls_alac_boundary.m3u8: HINT_STEP = 2048
+tests/data/mov_hls_alac_boundary_max.m3u8: HINT_STEP = 2147483647
+tests/data/mov_hls_alac_boundary.m3u8 
tests/data/mov_hls_alac_boundary_max.m3u8: ffmpeg$(PROGSSUF)$(EXESUF) | 
tests/data
+       $(M)$(TARGET_EXEC) $(TARGET_PATH)/$< -nostdin -v error -y \
+           -filter_complex sine=r=48000:d=0.17 -c:a alac -bitexact \
+           -bsf:a "setts=pts=N*$(HINT_STEP):dts=N*$(HINT_STEP):duration=1024" \
+           -f hls -hls_time 0.02 -hls_segment_type fmp4 -hls_flags single_file 
\
+           -hls_segment_filename $(TARGET_PATH)/$(@:.m3u8=.mp4) 
$(TARGET_PATH)/$@
+
+FATE_MOV_FFMPEG_FFPROBE-$(call ALLYES, SINE_FILTER ARESAMPLE_FILTER 
ALAC_ENCODER SETTS_BSF HLS_MUXER MP4_MUXER MOV_DEMUXER FILE_PROTOCOL) += 
fate-mov-hls-alac-boundary fate-mov-hls-alac-boundary-max
+fate-mov-hls-alac-boundary: tests/data/mov_hls_alac_boundary.m3u8
+fate-mov-hls-alac-boundary-max: tests/data/mov_hls_alac_boundary_max.m3u8
+fate-mov-hls-alac-boundary-max: BOUNDARY_SUFFIX = _max
+fate-mov-hls-alac-boundary fate-mov-hls-alac-boundary-max: CLEANFILES = 
tests/data/mov_hls_alac_boundary$(BOUNDARY_SUFFIX).m3u8 
tests/data/mov_hls_alac_boundary$(BOUNDARY_SUFFIX).mp4
+fate-mov-hls-alac-boundary fate-mov-hls-alac-boundary-max: CMD = run 
ffprobe$(PROGSSUF)$(EXESUF) -v error -fflags +noparse+nofillin -show_entries 
packet=dts,duration -of compact 
$(TARGET_PATH)/tests/data/mov_hls_alac_boundary$(BOUNDARY_SUFFIX).mp4
+
 FATE_FFMPEG += $(FATE_MOV_FFMPEG-yes)
 FATE_FFMPEG_FFPROBE += $(FATE_MOV_FFMPEG_FFPROBE-yes)
 
diff --git a/tests/ref/fate/api-hlsenc b/tests/ref/fate/api-hlsenc
new file mode 100644
index 00000000000..c17ecdc1743
--- /dev/null
+++ b/tests/ref/fate/api-hlsenc
@@ -0,0 +1,2 @@
+#EXTINF:0.046440,
+#EXTINF:0.046440,
diff --git a/tests/ref/fate/api-movenc-global-sidx 
b/tests/ref/fate/api-movenc-global-sidx
index 5de4663a1b0..cdd12b86c33 100644
--- a/tests/ref/fate/api-movenc-global-sidx
+++ b/tests/ref/fate/api-movenc-global-sidx
@@ -1,2 +1,6 @@
 frag_keyframe+empty_moov+global_sidx: 147 packets, 0 wrong, eof=1
 frag_keyframe+delay_moov+global_sidx: 147 packets, 0 wrong, eof=1
+dash+delay_moov, duration 54: sidx 0
+dash+delay_moov, duration 182: sidx 54
+frag_keyframe+delay_moov+global_sidx, duration 54: sidx 0
+frag_keyframe+delay_moov+global_sidx, duration 182: sidx 54
diff --git a/tests/ref/fate/hls-fmp4-aac-discont 
b/tests/ref/fate/hls-fmp4-aac-discont
new file mode 100644
index 00000000000..68c2e356238
--- /dev/null
+++ b/tests/ref/fate/hls-fmp4-aac-discont
@@ -0,0 +1,2 @@
+#EXTINF:2.321995,
+#EXTINF:0.001224,
diff --git a/tests/ref/fate/hls-fmp4-aac-tail b/tests/ref/fate/hls-fmp4-aac-tail
new file mode 100644
index 00000000000..68c2e356238
--- /dev/null
+++ b/tests/ref/fate/hls-fmp4-aac-tail
@@ -0,0 +1,2 @@
+#EXTINF:2.321995,
+#EXTINF:0.001224,
diff --git a/tests/ref/fate/hls-fmp4-eac3-boundary 
b/tests/ref/fate/hls-fmp4-eac3-boundary
new file mode 100644
index 00000000000..2b11421f767
--- /dev/null
+++ b/tests/ref/fate/hls-fmp4-eac3-boundary
@@ -0,0 +1,4 @@
+packet|dts=0|duration=1536
+packet|dts=1536|duration=1536
+packet|dts=3072|duration=1536
+packet|dts=4608|duration=1536
diff --git a/tests/ref/fate/hls-fmp4-eac3-cut b/tests/ref/fate/hls-fmp4-eac3-cut
new file mode 100644
index 00000000000..0f2c8139110
--- /dev/null
+++ b/tests/ref/fate/hls-fmp4-eac3-cut
@@ -0,0 +1,8 @@
+#EXTINF:0.032000,
+#EXT-X-BYTERANGE:6304@728
+#EXTINF:0.032000,
+#EXT-X-BYTERANGE:6304@7032
+#EXTINF:0.032000,
+#EXT-X-BYTERANGE:6304@13336
+#EXTINF:0.032000,
+#EXT-X-BYTERANGE:6304@19640
diff --git a/tests/ref/fate/mov-fragment-aac-tail 
b/tests/ref/fate/mov-fragment-aac-tail
new file mode 100644
index 00000000000..62fed99b40b
--- /dev/null
+++ b/tests/ref/fate/mov-fragment-aac-tail
@@ -0,0 +1,2 @@
+packet|pts=100352|duration=1024
+packet|pts=101376|duration=54
diff --git a/tests/ref/fate/mov-hls-alac-boundary 
b/tests/ref/fate/mov-hls-alac-boundary
new file mode 100644
index 00000000000..294f3e1ba0f
--- /dev/null
+++ b/tests/ref/fate/mov-hls-alac-boundary
@@ -0,0 +1,2 @@
+packet|dts=0|duration=2048
+packet|dts=2048|duration=1024
diff --git a/tests/ref/fate/mov-hls-alac-boundary-max 
b/tests/ref/fate/mov-hls-alac-boundary-max
new file mode 100644
index 00000000000..3b1c7073071
--- /dev/null
+++ b/tests/ref/fate/mov-hls-alac-boundary-max
@@ -0,0 +1,2 @@
+packet|dts=0|duration=2147483647
+packet|dts=2147483647|duration=1024
-- 
2.54.1


From 3f9e7e6c60abca46b68ce8180a90cf83952ecd9d Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Sun, 22 Nov 2026 22:35:57 -0800
Subject: [PATCH 2/3] avformat/movenc: do not cut fragments inside an E-AC-3
 sample

Wait for the next sample to start before an automatic fragment cut.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavformat/movenc.c                      | 7 +++++--
 tests/fate/mov.mak                        | 4 ++++
 tests/ref/fate/mov-fragment-eac3-boundary | 4 ++++
 3 files changed, 13 insertions(+), 2 deletions(-)
 create mode 100644 tests/ref/fate/mov-fragment-eac3-boundary

diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index 6452bee8d9d..3b6965c83bc 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -7860,6 +7860,7 @@ static int mov_write_single_packet(AVFormatContext *s, 
AVPacket *pkt)
     MOVMuxContext *mov = s->priv_data;
     MOVTrack *trk = s->streams[pkt->stream_index]->priv_data;
     AVCodecParameters *par = trk->par;
+    const struct eac3_info *eac3_info = trk->eac3_priv;
     int64_t frag_duration = 0;
     int size = pkt->size;
 
@@ -7930,13 +7931,15 @@ static int mov_write_single_packet(AVFormatContext *s, 
AVPacket *pkt)
         frag_duration = av_rescale_q(pkt->dts - trk->cluster[0].dts,
                 s->streams[pkt->stream_index]->time_base,
                 AV_TIME_BASE_Q);
-    if ((mov->max_fragment_duration &&
+    /* Do not cut inside an E-AC-3 sample: this syncframe continues it. */
+    if (!(eac3_info && eac3_info->pkt && eac3_info->pkt->size) &&
+        ((mov->max_fragment_duration &&
                 frag_duration >= mov->max_fragment_duration) ||
             (mov->max_fragment_size && mov->mdat_size + size >= 
mov->max_fragment_size) ||
             (mov->flags & FF_MOV_FLAG_FRAG_KEYFRAME &&
              par->codec_type == AVMEDIA_TYPE_VIDEO &&
              trk->entry && pkt->flags & AV_PKT_FLAG_KEY) ||
-            (mov->flags & FF_MOV_FLAG_FRAG_EVERY_FRAME)) {
+            (mov->flags & FF_MOV_FLAG_FRAG_EVERY_FRAME))) {
         if (frag_duration >= mov->min_fragment_duration) {
             if (trk->entry) {
                 // Set the duration of this track to line up with the next
diff --git a/tests/fate/mov.mak b/tests/fate/mov.mak
index 110d795225f..f8d1167086e 100644
--- a/tests/fate/mov.mak
+++ b/tests/fate/mov.mak
@@ -760,6 +760,10 @@ fate-mov-chpl-utf8: CMD = run_with_temp "$(FFMPEG) 
-nostdin -v error -filter_com
 FATE_MOV_FFMPEG_FFPROBE-$(call ALLYES, SINE_FILTER ARESAMPLE_FILTER 
AAC_ENCODER MP4_MUXER MOV_DEMUXER FILE_PROTOCOL) += fate-mov-fragment-aac-tail
 fate-mov-fragment-aac-tail: CMD = run_with_temp "$(FFMPEG) -nostdin -v error 
-filter_complex sine=d=2.3 -c:a aac -bitexact -use_editlist 1 -movflags 
+delay_moov -frag_duration 2310000 -y" "ffprobe$(PROGSSUF)$(EXESUF) -v error 
-bitexact -read_intervals 2.3% -show_entries packet=pts,duration -of compact" 
mp4
 
+# An automatic fragment cut must not split an E-AC-3 sample.
+FATE_MOV_FFMPEG_FFPROBE_SAMPLES-$(call ALLYES, EAC3_DEMUXER AC3_PARSER 
MP4_MUXER MOV_DEMUXER FILE_PROTOCOL) += fate-mov-fragment-eac3-boundary
+fate-mov-fragment-eac3-boundary: CMD = run_with_temp "$(FFMPEG) -nostdin -v 
error -i $(TARGET_SAMPLES)/eac3/serenity_english_5.1_1536_small.eac3 -frames:a 
24 -c copy -movflags +delay_moov -frag_duration 50000 -y" 
"ffprobe$(PROGSSUF)$(EXESUF) -v error -fflags +noparse+nofillin -show_entries 
packet=dts,duration -of compact" mp4
+
 # The next packet DTS must extend the previous ALAC sample before HLS flushes.
 tests/data/mov_hls_alac_boundary.m3u8: HINT_STEP = 2048
 tests/data/mov_hls_alac_boundary_max.m3u8: HINT_STEP = 2147483647
diff --git a/tests/ref/fate/mov-fragment-eac3-boundary 
b/tests/ref/fate/mov-fragment-eac3-boundary
new file mode 100644
index 00000000000..2b11421f767
--- /dev/null
+++ b/tests/ref/fate/mov-fragment-eac3-boundary
@@ -0,0 +1,4 @@
+packet|dts=0|duration=1536
+packet|dts=1536|duration=1536
+packet|dts=3072|duration=1536
+packet|dts=4608|duration=1536
-- 
2.54.1


From d6b15d41e410926bddd970204840af7017d13565 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Sun, 22 Nov 2026 22:35:57 -0800
Subject: [PATCH 3/3] avformat/hlsenc: write the final partial E-AC-3 sample
 into the last segment

MOV only wrote it in its trailer, after HLS had copied the segment.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavformat/hlsenc.c              |  8 ++++++
 libavformat/movenc.c              | 42 ++++++++++++++++++-------------
 libavformat/movenc.h              |  3 +++
 tests/fate/hlsenc.mak             | 14 +++++++++++
 tests/ref/fate/hls-fmp4-eac3-tail |  6 +++++
 5 files changed, 56 insertions(+), 17 deletions(-)
 create mode 100644 tests/ref/fate/hls-fmp4-eac3-tail

diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index 8e273e62265..e7cc1ce8dd2 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -2858,6 +2858,14 @@ static int hls_write_trailer(struct AVFormatContext *s)
             return AVERROR(ENOMEM);
         }
 
+#if CONFIG_MP4_MUXER
+        /* The MOV trailer would write a final partial E-AC-3 sample
+         * after this segment is copied. */
+        if (hls->segment_type == SEGMENT_TYPE_FMP4 &&
+            (ret = ff_mov_write_buffered_packets(oc)) < 0)
+            goto failed;
+#endif
+
         if (hls->segment_type == SEGMENT_TYPE_FMP4 && !vs->init_range_length) {
             av_write_frame(oc, NULL); /* Flush any buffered data */
 
diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index 3b6965c83bc..694b6f7d395 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -9534,6 +9534,29 @@ static void mov_write_mdat_size(AVFormatContext *s)
     }
 }
 
+int ff_mov_write_buffered_packets(AVFormatContext *s)
+{
+    MOVMuxContext *mov = s->priv_data;
+
+    for (int i = 0; i < mov->nb_tracks; i++) {
+        MOVTrack *trk = &mov->tracks[i];
+        struct eac3_info *info = trk->eac3_priv;
+        int ret;
+
+        if (trk->par->codec_id != AV_CODEC_ID_EAC3 ||
+            !info || !info->pkt || !info->pkt->size)
+            continue;
+
+        av_packet_move_ref(mov->pkt, info->pkt);
+        info->eof = 1;
+        ret = mov_write_single_packet(s, mov->pkt);
+        av_packet_unref(mov->pkt);
+        if (ret < 0)
+            return ret;
+    }
+    return 0;
+}
+
 static int mov_write_trailer(AVFormatContext *s)
 {
     MOVMuxContext *mov = s->priv_data;
@@ -9555,23 +9578,8 @@ static int mov_write_trailer(AVFormatContext *s)
         }
     }
 
-    //Also check for a buffered EAC3 packet
-    for (i = 0; i < mov->nb_tracks; i++) {
-        MOVTrack *trk = &mov->tracks[i];
-        if (trk->par->codec_id != AV_CODEC_ID_EAC3)
-            continue;
-        struct eac3_info *info = trk->eac3_priv;
-        if (!info || !info->pkt || !info->pkt->size)
-            continue;
-
-        av_packet_move_ref(mov->pkt, info->pkt);
-        info->eof = 1;
-        res = mov_write_single_packet(s, mov->pkt);
-        if (res < 0)
-            return res;
-
-        av_packet_unref(mov->pkt);
-    }
+    if ((res = ff_mov_write_buffered_packets(s)) < 0)
+        return res;
 
     // Check if we have any tracks that require squashing.
     // In that case, we'll have to write the packet here.
diff --git a/libavformat/movenc.h b/libavformat/movenc.h
index 97396dd439e..f8c67e5ac72 100644
--- a/libavformat/movenc.h
+++ b/libavformat/movenc.h
@@ -310,6 +310,9 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt);
 /* Return the sum of the audio sample durations, written or pending. */
 int64_t ff_mov_get_audio_duration(AVFormatContext *s, int stream_index);
 
+/* Write the E-AC-3 syncframes still buffered for a partial sample. */
+int ff_mov_write_buffered_packets(AVFormatContext *s);
+
 int ff_mov_set_fragment_end_hint(AVFormatContext *s, int stream_index,
                                  const AVPacket *pkt, AVRational 
src_time_base);
 
diff --git a/tests/fate/hlsenc.mak b/tests/fate/hlsenc.mak
index b42b46704ea..96338c51b3a 100644
--- a/tests/fate/hlsenc.mak
+++ b/tests/fate/hlsenc.mak
@@ -381,6 +381,20 @@ fate-hls-fmp4-eac3-cut: CLEANFILES = 
tests/data/hls_fmp4_eac3_cut.m3u8 tests/dat
 fate-hls-fmp4-eac3-cut: CMD = sed -n -e /^\#EXTINF:/p -e 
/^\#EXT-X-BYTERANGE:/p tests/data/hls_fmp4_eac3_cut.m3u8
 fate-hls-fmp4-eac3-cut: CMP = diff
 
+# The final partial E-AC-3 sample belongs to the last segment.
+tests/data/hls_fmp4_eac3_tail.m3u8: ffmpeg$(PROGSSUF)$(EXESUF) | tests/data
+       $(M)$(TARGET_EXEC) $(TARGET_PATH)/$< -nostdin -v error -y \
+           -i $(TARGET_SAMPLES)/eac3/serenity_english_5.1_1536_small.eac3 
-frames:a 23 -c copy \
+           -hls_time 0.05 -hls_segment_type fmp4 -hls_flags single_file \
+           -hls_segment_filename 
$(TARGET_PATH)/tests/data/hls_fmp4_eac3_tail.mp4 \
+           $(TARGET_PATH)/$@
+
+FATE_HLSENC_FFMPEG_SAMPLES-$(call ALLYES, EAC3_DEMUXER AC3_PARSER HLS_MUXER 
MP4_MUXER FILE_PROTOCOL) += fate-hls-fmp4-eac3-tail
+fate-hls-fmp4-eac3-tail: tests/data/hls_fmp4_eac3_tail.m3u8
+fate-hls-fmp4-eac3-tail: CLEANFILES = tests/data/hls_fmp4_eac3_tail.m3u8 
tests/data/hls_fmp4_eac3_tail.mp4
+fate-hls-fmp4-eac3-tail: CMD = sed -n -e /^\#EXTINF:/p -e 
/^\#EXT-X-BYTERANGE:/p tests/data/hls_fmp4_eac3_tail.m3u8
+fate-hls-fmp4-eac3-tail: CMP = diff
+
 FATE_HLSENC_LAVFI-yes := $(if $(call FRAMECRC), $(FATE_HLSENC_LAVFI-yes))
 
 FATE_FFMPEG_FFPROBE += $(FATE_HLSENC_FFMPEG_FFPROBE-yes)
diff --git a/tests/ref/fate/hls-fmp4-eac3-tail 
b/tests/ref/fate/hls-fmp4-eac3-tail
new file mode 100644
index 00000000000..27ff6b95018
--- /dev/null
+++ b/tests/ref/fate/hls-fmp4-eac3-tail
@@ -0,0 +1,6 @@
+#EXTINF:0.032000,
+#EXT-X-BYTERANGE:6304@728
+#EXTINF:0.064000,
+#EXT-X-BYTERANGE:12448@7032
+#EXTINF:0.026667,
+#EXT-X-BYTERANGE:5280@19480
-- 
2.54.1

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to