1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2025-11-23 21:54:53 +02:00
Commit Graph

1302 Commits

Author SHA1 Message Date
Martin Storsjö
8313dc1120 movenc: Make hybrid_fragmented retain the fragmented form headers
This makes the final file truly hybrid: Externally the file
is a regular, non-fragmented file, but internally, the fragmented
form also exists un-overwritten.

To make any use of that, first, the fragments need to be muxed in
a position independent form, i.e. with empty_moov+default_base_moof
(or the dash or cmaf meta-flags).

Making use of the fragmented form when the file is finalized is
not entirely obvious though. One can dump the contents of the
single mdat box, and get the fragmented form. (This is a neat
trick, but not something that anybody really is expected to
want to do.)

The main expected use case is accessing fragments in the form of
byte range segments, for e.g. HLS.

Previously, the start of the file would look like this:

- ftyp
- free
- moov
 - (moov contents)

After finalizing the file, it would look like this:

- ftyp
- free
- mdat (previously moov)
 - (moov contents)

In this form, the size and type of the original moov box were
overwritten, and the original moov contents is just leftover
as unused data in the mdat box.

To avoid this issue, the start of the file now looks like this:

- ftyp
- free
- free
 - ftyp
- moov
 - (moov contents)

The second, hidden ftyp box inside mdat, would normally never be
seen.

After finalizing, the difference is that the mdat box now is
extended to cover the ftyp and the whole moov including its header
(and all the following fragments).

I.e., the start of the file looks like this:

- ftyp
- free
- mdat
 - ftyp
 - moov
  - (moov contents)

This allows accessing the "ftyp+moov" pair sequentially as such,
with a byte range - this range is untouched when finalizing,
producing the same ftyp+moov pair both while writing, when the
file is fragmented, and after finalizing, when the file is
transformed to non-fragmented externally.

Note; the sequential two "free+free" boxes may look slightly
silly; it could be tempting to make the second one an mdat
from the get-go. However, some players of fragmented mp4 (in
particular, Apple's HLS player) bail out if the initialization
segment contains an mdat box - therefore, use a free box.

It could also be possible to use just one single free box with
8 bytes of padding at the start - but that would require more
changes to the finalization logic.

For a segmenting user of the muxer, the only unclarity is how
to determine the right byte range for the internal ftyp+moov
pair. Currently, this requires parsing the muxer output and skip
past anything up to the start of the non-empty free box.
2025-11-04 10:23:31 +00:00
Anthony Bajoua
68152978b5 libavformat/movenc: Output ftyp atom with hybrid_fragmented+delay_moov 2025-10-27 09:06:06 +00:00
Kacper Michajłow
a2b47ccfbf avcodec/{png,mov}enc: use EOTF gamma approximation for gAMA chunk
This is how images encoded with specific transfer function should be
viewed. Image viewers that doesn't support named trc metadata, will
fallback to simple gAMA value and both of those cases should produce the
same image apperance for the viewer.

Fixes: https://github.com/mpv-player/mpv/issues/13438

Signed-off-by: Kacper Michajłow <kasper93@gmail.com>
2025-10-24 22:24:54 +00:00
Baptiste Coudurier
0b0cb7cd6c lavf/movenc: improve AVdh atom generation for DNxHD/DNxHR 2025-10-24 00:16:21 +00:00
Martin Storsjö
e442128944 movenc: Make sure to flush the delayed moov atom for hybrid fragmented
If using the delay_moov flag in combination with hybrid_fragment
(which is a potentially problematic combination otherwise - the
ftyp box does end up hidden in the end), then we need to flush
twice to get both the moov box and the first fragment, if the
file is finished before the first fragment is completed.
2025-10-21 08:38:32 +00:00
Martin Storsjö
27f5561885 movenc: Fix sample clustering for hybrid_fragmented+delay_moov
If samples were available when the moov was written, chunking
for those samples has been done already, which has to be reset
here.

This is the case when not using empty_moov, when the moov box
describes the first fragment - this case was accounted for already.
But if using the delay_moov flag, then those samples also were
available when writing the moov, so chunking for them has already
been done in this case as well.

Therefore, always reset chunking here (it should be harmless to
always do it), and update the comment to clarify the cases
involved here.
2025-10-21 08:38:32 +00:00
Martin Storsjö
36896af64a movenc: Make the hybrid_fragmented mode more robust
Write the moov tag at the end first, before overwriting the mdat size
at the start of the file.

In case writing the final moov box fails (e.g. due to being out
of disk), we haven't broken the initial moov box yet.

Thus if writing stops between these steps, we could end up with
a file with two moov boxes - which arguably is more feasible to
recover from, than from a file with no moov boxes at all.
2025-10-16 18:58:54 +00:00
Zhao Zhili
cd4b01707d Revert "avformat/movenc: sidx earliest_presentation_time is applied after editlist"
This reverts commit 301141b576.

cluster[0].dts, pts and frag_info[0].time are already in presentation
timeline, so they shouldn't be shift by start_pts.

Signed-off-by: Zhao Zhili <zhilizhao@tencent.com>
2025-10-16 11:22:37 +08:00
James Almer
6d8732f397 avformat/movenc: clear subsample information on fragment flush
Don't keep around information from a previous traf atom.

Fixes issue #20492.

Signed-off-by: James Almer <jamrial@gmail.com>
2025-09-25 23:18:08 -03:00
Jan Ekström
af35a13f6d avformat/movenc: add support for fragmented TTML muxing
Attempts to base the fragmentation timing on other streams
as most receivers expect media fragments to be more or less
aligned.

Currently does not support fragmentation on subtitle track
only, as the subtitle packet queue timings would have to be
checked in addition to the current fragmentation timing logic.

Signed-off-by: Jan Ekström <jan.ekstrom@24i.com>
2025-09-16 16:29:48 +03:00
Zhao Zhili
9a34ddc345 avformat/movenc: remove mvex from final mp4 in hybrid_fragmented mode
In hybrid_fragmented mode, the first moov at start should contain
mvex tag, since it's fmp4. When writing the moov at the second time,
it's not fmp4 any more, so mvex should be skipped.
2025-09-01 12:41:38 +00:00
Damiano Galassi
b2f5bc7fd3 movenc: ensure chapters track extradata is not null and populated
fix a regression introduced in 4eca8df,
writing chapters failed if chapters where
not available before mov_write_header().
2025-08-24 13:31:53 +00:00
Timo Rothenpieler
262d41c804 all: fix typos found by codespell 2025-08-03 13:48:47 +02:00
James Almer
9aed7383bd avformat/movenc: look for the Intra profile flag before assuming a stream is AVC-Intra
Fixes issue #20018.

Note that Codec Descriptors are not written for all these ai** codec tags, and no
considerations were ever made to ensure parameter sets are present in muxed packets.
Their usage may result in unplayable files if parameter sets are only available
in extradata (Default behavior for x264 encoding when combined with this muxer).

Signed-off-by: James Almer <jamrial@gmail.com>
2025-08-01 14:53:17 +00:00
James Almer
4eca8df8b8 avformat/movenc: support writing more than one STSD entry per track
If a packet contains new extradata within a side data entry, save for
specific cases it means the coded stream changed. If ignored, upon
demuxing every packet from then onwards may be undecodable.

Signed-off-by: James Almer <jamrial@gmail.com>
2025-07-30 16:48:14 -03:00
James Almer
306448756b avformat/movenc: allow storing more than one extradata buffer per track
This is in preparation for a following change

Signed-off-by: James Almer <jamrial@gmail.com>
2025-07-30 16:48:14 -03:00
David McElroy
4b6f6af384 avformat/movenc: fix HEVC fmp4 HLS init segment for Apple playback
HEVC fmp4 HLS video produced by ffmpeg is currently unplayable on Apple
software (Safari, QuickTime, AVFoundation).

This is caused by an empty sdtp atom being erroneously written to the
fmp4 init segment. The `has_disposable` flag can be set for a track
with B-frames, but the init segment contains no actual frames
(track->entry == 0). Writing an sdtp atom in this case is incorrect
and causes Apple's parsers to reject the file.

This patch fixes the issue by ensuring the sdtp atom is only written
if track->entry is non-zero.

A similar patch was proposed in November 2023 by Jay Zhang,
but it was never merged.

Link: https://lists.ffmpeg.org/pipermail/ffmpeg-devel/2023-November/317173.html
Co-authored-by: Jay Zhang <wangyoucao577@gmail.com>
Signed-off-by: David McElroy <david@mcelroy.online>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2025-07-28 17:27:51 +02:00
Dawid Kozinski
8baa691e5f avformat/mov_muxer: Extended MOV muxer to handle APV video content
- Changes in mov_write_video_tag function to handle APV elementary stream
- Provided structure APVDecoderConfigurationRecord that specifies the decoder configuration information for APV video content

Co-Authored-by: James Almer <jamrial@gmail.com>
Signed-off-by: Dawid Kozinski <d.kozinski@samsung.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2025-07-18 14:54:36 -03:00
Zhao Zhili
920071355d avformat/movenc: Fix editlist with hybrid_fragmented
The segment_duration must not be set to zero when writing the moov
atom for the second time. This is related to edit lists in standard
MP4 files.

Reviewed-by: Martin Storsjö <martin@martin.st>
Signed-off-by: Zhao Zhili <zhilizhao@tencent.com>
2025-06-16 10:55:04 +08:00
nyanmisaka
ebcf2dcb2c avformat/movenc: handle EAC-3 extension bits for Atmos
Based on a patch by nyanmisaka.
Fixes commit #9996.

Reviewed-by: Baptiste Coudurier <baptiste.coudurier@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2025-06-08 18:39:35 -03:00
James Almer
117343c0ba avcodec/ac3_parser: handle more header bits in ff_ac3_parse_header()
Based on a patch by nyanmisaka.

Signed-off-by: James Almer <jamrial@gmail.com>
2025-06-08 18:39:17 -03:00
James Almer
17729aa80c avformat/movenc: fix writing reserved bits in EC3SpecificBox
As described in section F.6.1 from ETSI TS 102 366.

Found-by: nyanmisaka
Reviewed-by: Baptiste Coudurier <baptiste.coudurier@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2025-06-05 21:49:11 -03:00
Zhao Zhili
8ea2b993fd avformat/movenc: Reduce loop iterations in mov_flush_fragment
Signed-off-by: Zhao Zhili <zhilizhao@tencent.com>
2025-06-01 16:37:00 +08:00
Zhao Zhili
56cf1c084d avformat/movenc: Fix flush fragment
The follow cmd output corrupted file before the patch:

ffmpeg -f lavfi -i color=blue,trim=duration=0.04 \
	-f lavfi -i anullsrc,atrim=duration=2 \
	-movflags +empty_moov+hybrid_fragmented \
	-frag_duration 1000000 \
	-frag_interleave 1 \
	output.mp4

1. first_track is the first track with track->entry != 0. As in the
command above, video track (track index 0) has a single frame. When
flush the second fragment, first_track is 1, the audio track.

2. write_moof = i == first_track, so write_moof is false for i = 0.

3. When mov->frag_interleave != 0, mov->mdat_buf != NULL, because
it contains audio data. So avio_write is called before write_moof,
that is, the data write before moof, and mov_finish_fragment
executed with wrong mdat_start.

4. With normal fmp4 output, the error isn't obvious. With
hybrid_fragmented, ffplay output.mp4 shows a lot of error messages.

Signed-off-by: Zhao Zhili <zhilizhao@tencent.com>
2025-06-01 16:36:54 +08:00
Gabriel Hege
6c291232cf avformat/movenc: fix VVC encoding with leading pictures
The default behavior for VVenC (since v1.10.0) is to create an IDR with
leading pictures for the first picture in decoding order (POC 32). This
leads to FFmpeg generating an edit list with an empty entry, skipping
the leading pictures.

This patch fixes the calculation for the start_pts, while the DTS is
negative (as produced by vvenc).

Signed-off-by: Gabriel Hege <g+ffmpeg@hege.cc>
2025-05-31 09:47:50 +08:00
Gyan Doshi
c79bce92b8 avformat/movenc: disallow buggy flags combination
Use of hybrid_fragmented and faststart together can result in files with
loss of sync and bitstream parsing errors upon playback.
2025-05-26 22:44:17 +05:30
Felicia Lim
2229b74127 avformat/movenc: fix setting elst/stss for IAMF with Opus 2025-04-14 17:25:39 -03:00
James Almer
c153238275 avformat: remove deprecated FF_API_ALLOW_FLUSH
Deprecated since 2023-10-02.

Signed-off-by: James Almer <jamrial@gmail.com>
2025-03-28 14:33:07 -03:00
James Almer
08e334e462 avformat/movenccenc: add support for CENC AV1 encryption
Signed-off-by: James Almer <jamrial@gmail.com>
2025-03-25 20:59:18 -03:00
Zhao Zhili
a053516e64 avformat/movenc: Add AVS3 support
'avs3' is registered at mp4ra.org. The Avs3ConfigurationBox 'av3c'
inside 'avs3' hasn't been registered yet, but is specified by the
AVS3 spec.

Signed-off-by: Zhao Zhili <zhilizhao@tencent.com>
2025-03-04 17:01:58 +08:00
James Almer
68ee3faf48 avformat/hevc: add a log context to ff_isom_write_{hvcc,lhvc}
Signed-off-by: James Almer <jamrial@gmail.com>
2024-12-08 19:12:19 -03:00
Gyan Doshi
914f89dbc6 avformat/movenc: log stream index for invalid pkt duration 2024-12-07 10:07:35 +05:30
Malek Assaad
6a108d4759 avformat/mov: add support for pssh box
* Enable PSSH box writing to mov & moof.
* Enable senc, saio & saiz writing to moof when writing fragments.

Signed-off-by: James Almer <jamrial@gmail.com>
2024-12-05 14:22:16 -03:00
James Almer
23a1c021f2 avformat/movenc: write stss boxes for xHE-AAC
This is in preparation for a following change.

Signed-off-by: James Almer <jamrial@gmail.com>
2024-11-24 20:14:11 -03:00
James Almer
ecc7d5db9c avformat/movenc: don't disable edit lists when writing CMAF output
They are needed for audio tracks with priming samples, where negative CTS
offsets can't be used.

Fixes ticket #11031.

Signed-off-by: James Almer <jamrial@gmail.com>
2024-11-22 20:50:05 -03:00
James Almer
b328bf8f7e avformat/mov: rename MOVCtts.duration to offset
The value is a timestamp offset, not a duration.

Signed-off-by: James Almer <jamrial@gmail.com>
2024-11-22 20:50:05 -03:00
James Almer
944212acad avformat/movenc: don't write a calculated avgBitrate when the provided one is unset
avgBitrate == 0 is used to signal a VBR track, so if that value is propagated by an
encoder, don't overwrite it with a calculated value based on track size.

Part of a fix for ticket #11303.

Signed-off-by: James Almer <jamrial@gmail.com>
2024-11-22 10:57:03 -03:00
James Almer
f0051604c7 avformat/movenc: don't write PS NALUs in hvcC for codec tag hvc1
array_completeness shall be 1 for those tracks.

Signed-off-by: James Almer <jamrial@gmail.com>
2024-11-22 10:49:32 -03:00
Michael Niedermayer
0b8c9cf5b4 avformat/movenc: Fix ffv1 support
Fixes: Ticket9975

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2024-11-16 13:24:16 +01:00
James Almer
1edc748eff avformat/movenc: explicitly support V30XLE and not the host native endian version
Should fix fate-vsynth*-v410 on BE targets.

Signed-off-by: James Almer <jamrial@gmail.com>
2024-10-17 21:20:27 -03:00
James Almer
02306cbfee avcodec: deprecate v410 de/encoder
The V30X pixel format was recently added, so this lavc workaround is no longer
needed.

Signed-off-by: James Almer <jamrial@gmail.com>
2024-10-12 11:21:14 -03:00
James Almer
3c64d614c7 avcodec: deprecate v308 de/encoder
The vyu444 pixel format was recently added, so this lavc workaround is no longer
needed.

Signed-off-by: James Almer <jamrial@gmail.com>
2024-10-12 11:21:14 -03:00
James Almer
e2427a4223 avcodec: deprecate v408 de/encoder
The uyva pixel format was recently added, so this lavc workaround is no longer
needed.

Signed-off-by: James Almer <jamrial@gmail.com>
2024-10-12 11:21:14 -03:00
James Almer
c5cf98d77e avformat/movenc: add support for V30X pixel format
Signed-off-by: James Almer <jamrial@gmail.com>
2024-10-12 11:21:14 -03:00
James Almer
12fc358249 avformat/movenc: add support for VYU444 pixel format
Signed-off-by: James Almer <jamrial@gmail.com>
2024-10-12 11:21:14 -03:00
James Almer
d6b08e0c0f avformat/movenc: add support for UYVA pixel format
Signed-off-by: James Almer <jamrial@gmail.com>
2024-10-12 11:21:14 -03:00
James Almer
472fbee812 avformat/movenc: don't look at previous track_id as it may not be set
Signed-off-by: James Almer <jamrial@gmail.com>
2024-08-12 11:41:38 -03:00
James Almer
49a6e448d7 avformat/movenc: use stream indexes when generating track ids
In some scenarios nb_tracks isn't the same as nb_streams, so a given id may end
up being used for two separate streams.

e.g. when muxing an IAMF track followed by a video track, if the IAMF track
consists of several streams, the video track would end up having an id of 2,
which may also be used by one of the IAMF streams.

Signed-off-by: James Almer <jamrial@gmail.com>
2024-08-07 00:16:21 -03:00
James Almer
5b87869c09 avformat/mov: fix track handling when mixing IAMF and video tracks
Fixes crashes when muxing the two together.

Signed-off-by: James Almer <jamrial@gmail.com>
2024-08-04 12:09:40 -03:00
Felicia Lim
180c869faf avformat/movenc: fix channel count and samplerate fields for IAMF tracks
Clause 6.2.3 of IAMF[1] states both of these shall be set to 0.

[1]https://aomediacodec.github.io/iamf/v1.0.0-errata.html#iasampleentry-section

Signed-off-by: James Almer <jamrial@gmail.com>
2024-07-18 23:27:20 -03:00