Commit Graph

1158 Commits

Author SHA1 Message Date
Andreas Rheinhardt 2c47a94911 avformat/movenc: Simplify reserving space for tfrf tags
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2021-09-27 07:08:59 +02:00
Andreas Rheinhardt 1cf3c59b58 avformat/movenc: Limit ism_lookahead to a sane value
There can only be a maximum of 255 entries in a tfrf tag, so using
more makes no sense; moreover, several size computations can overflow
in this case. Fix this by limiting it to 255.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2021-09-27 07:08:59 +02:00
Andreas Rheinhardt 1ea3650823 Replace all occurences of av_mallocz_array() by av_calloc()
They do the same.

Reviewed-by: Paul B Mahol <onemda@gmail.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2021-09-20 01:03:52 +02:00
Andreas Rheinhardt 831718bbab avformat/movenc: Avoid calling strlen multiple times
Reviewed-by: Paul B Mahol <onemda@gmail.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2021-08-27 10:47:35 +02:00
Jan Ekström e41bd075dd avformat/movenc: add support for TTML muxing
Includes basic support for both the ISMV ('dfxp') and MP4 ('stpp')
methods. This initial version also foregoes fragmentation support
in case the built-in sample squashing is to be utilized, as this
eases the initial review.

Additionally, add basic tests for both muxing modes in MP4.

Signed-off-by: Jan Ekström <jan.ekstrom@24i.com>
2021-08-25 09:26:46 +03:00
Hu Weiwen 4cdab8d022 movenc: Get rid of frag_start
"frag_start" is redundant, and every occurance can be replaced with cluster[0].dts - start_dts

The proof of no behaviour changes: (All line number below is based on commit bff7d662d7)

"frag_start" is read at 4 place (with all possible call stacks):

mov_write_packet
  ...
  mov_flush_fragment
    mov_write_moof_tag
      mov_write_moof_tag_internal
        mov_write_traf_tag
          mov_write_tfxd_tag (#1)
          mov_write_tfdt_tag (#2)
      mov_add_tfra_entries (#3)
      mov_write_sidx_tags
        mov_write_sidx_tag (#4)

mov_write_trailer
  mov_auto_flush_fragment
    mov_flush_fragment
      ... (#1 #2 #3 #4)
  mov_write_sidx_tags
    mov_write_sidx_tag (#4)
  shift_data
    compute_sidx_size
      get_sidx_size
        mov_write_sidx_tags
          mov_write_sidx_tag (#4)

All read happens in "mov_write_trailer" and "mov_write_moof_tag". So we need to prove no behaviour change in these two
functions.

Condition 1: for every track that have "trk->entry == 0", trk->frag_start == trk->track_duration.

Condition 2: for every track that have "trk->entry > 0", trk->frag_start == trk->cluster[0].dts - trk->start_dts.

Definition 1: "Before flush" means just before the invocation of "mov_flush_fragment", except for the auto-flush case in
"mov_write_single_packet", which means before L5934.

Lemma 1: If Condition 1 & 2 is true before flush, Condition 1 & 2 is still true after "mov_flush_fragment" returns.

    Proof:
    No update to the tracks that have "trk->entry == 0" before flushing, so we only consider tracks that have "trk->entry > 0":

    Case 1: !moov_written and moov will be written in this iteration
        trk->entry = 0                                                                    L5366
        trk->frag_start == trk->cluster[0].dts - trk->start_dts                           Lemma condition
        trk->frag_start += trk->start_dts + trk->track_duration - trk->cluster[0].dts;    L5363
        So trk->entry == 0 && trk->frag_start == trk->track_duration
    Case 2: !moov_written and moov will NOT be written in this iteration
        nothing changed
    Case 3: moov_written
        trk->entry = 0                                                                    L5445
        trk->frag_start == trk->cluster[0].dts - trk->start_dts                           Lemma condition
        trk->frag_start += trk->start_dts + trk->track_duration - trk->cluster[0].dts;    L5444
        So trk->entry == 0 && trk->frag_start == trk->track_duration

    Note that trk->track_duration may be updated for the tracks that have "trk->entry > 0" (mov_write_moov_tag will
    update track_duration of "tmcd" track, but it must have 1 entry). But in all case, trk->frag_start is also updated
    to consider the new value.

Lemma 2: If Condition 1 & 2 is true before "ff_mov_write_packet" invocation, Condition 1 & 2 is still true after it returns.

    Proof:
    Only the track corresponding to the pkt is updated, and no update to relevant variables if trk->entry > 0 before invocation.
    So we only need to prove "trk->frag_start == trk->cluster[0].dts - trk->start_dts" after trk->entry increase from 0 to 1.

    Case 1: trk->start_dts == AV_NOPTS_VALUE
        Case 1.1: trk->frag_discont && use_editlist
            trk->cluster[0].dts = pkt->dts        at L5741
            trk->frag_start = pkt->pts            at L5785
            trk->start_dts = pkt->dts - pkt->pts  at L5786
            So trk->frag_start == trk->cluster[0].dts - trk->start_dts
        Case 1.2: trk->frag_discont && !use_editlist
            trk->cluster[0].dts = pkt->dts        at L5741
            trk->frag_start = pkt->dts            at L5790
            trk->start_dts = 0                    at L5791
            So trk->frag_start == trk->cluster[0].dts - trk->start_dts
        Case 1.3: !trk->frag_discont
            trk->cluster[0].dts = pkt->dts        at L5741
            trk->frag_start = 0                   init
            trk->start_dts = pkt->dts             at L5779
            So trk->frag_start == trk->cluster[0].dts - trk->start_dts
    Case 2: trk->start_dts != AV_NOPTS_VALUE
        Case 2.1: trk->frag_discont
            trk->cluster[0].dts = pkt->dts                at L5741
            trk->frag_start = pkt->dts - trk->start_dts   at L5763
            So trk->frag_start == trk->cluster[0].dts - trk->start_dts
        Case 2.2: !trk->frag_discont
            trk->cluster[0].dts = trk->start_dts + trk->track_duration  at L5749
            trk->track_duration == trk->frag_start                      Lemma condition
            So trk->frag_start == trk->cluster[0].dts - trk->start_dts

Lemma 3: Condition 1 & 2 is true in all case before and after "ff_mov_write_packet" invocation, before flush and after
         "mov_flush_fragment" returns.

    Proof: All updates to relevant variable happen either in "ff_mov_write_packet", or during flush. And Condition 1 & 2
           is true initially. So with lemma 1 & 2, we can prove this use induction.

Noticed that all read of "frag_start" only happen in "trk->entry > 0" branch. Now we need to prove Condition 2 is true
before each read.

Because no update to variables relevant to Condition 2 between "before flush" and "mov_write_moof_tag" invocation, we
can conclude Condition 2 is true before every invocation of "mov_write_moof_tag". No behaviour change in
"mov_write_moof_tag" is proved.

In "mov_write_trailer", No update to relevant variables after the last flush and before the invocation of
"mov_write_sidx_tag". So no behaviour change to "mov_write_trailer" is proved.

Q.E.D.

Signed-off-by: Hu Weiwen <sehuww@mail.scut.edu.cn>
Signed-off-by: Martin Storsjö <martin@martin.st>
2021-08-18 13:07:46 +03:00
Hu Weiwen 2b77a2f723 movenc: Ensure no separate moof written for empty track
track->mdat_buf can be not NULL while the track is still empty if the
last packet write failed.

Signed-off-by: Hu Weiwen <sehuww@mail.scut.edu.cn>
Signed-off-by: Martin Storsjö <martin@martin.st>
2021-08-18 13:07:46 +03:00
Martin Storsjö bff7d662d7 movenc: Don't try to fix the fragment end duration if none will be written
If autoflushing on a new packet (e.g. due to the frag_every_frame
flag being set), there's no samples to be written in the new fragment,
so we can't overwrite the track duration in order to make it line
up with the next packet to be written.

Signed-off-by: Martin Storsjö <martin@martin.st>
2021-08-10 10:24:47 +03:00
Andreas Rheinhardt 1be3d8a0cb avcodec/avcodec: Stop including channel_layout.h in avcodec.h
Also include channel_layout.h directly wherever used.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2021-07-22 11:14:31 +02:00
Andreas Rheinhardt 637e2c0a3e avformat/movenc: Deduplicate AVClasses
The child_class_next API relied on different (de)muxers to use
different AVClasses; yet this API has been replaced by
child_class_iterate.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2021-07-08 16:03:17 +02:00
Vadym Bezdushnyi b74beba9a9 avformat/movenc: Support encryption of H.265 stream in AnnexB format
Steps to test:

1. Create h265 test files - mp4 and h265 AnnexB streams:
ffmpeg -f lavfi -i testsrc=duration=10:size=640x480:rate=30 -c:v hevc input_h265.mp4
ffmpeg -f lavfi -i testsrc=duration=10:size=640x480:rate=30 -c:v hevc -bsf:v hevc_mp4toannexb input_h265.h265

2. Encrypt and decrypt files. Put appropriate input file name here: input_h265.mp4 or input_h265.h265
ffmpeg -i input_h265.h265 -vcodec copy -acodec copy -encryption_scheme cenc-aes-ctr \
 -encryption_key 00000000000000000000000000000000 -encryption_kid 00000000000000000000000000000000 \
 encrypted_h265.mp4
ffplay -i encrypted_h265.mp4  -decryption_key 00000000000000000000000000000000

Signed-off-by: Vadym Bezdushnyi <vadim.bezdush@gmail.com>
2021-06-29 18:20:17 +05:30
Joel Linn 127c40c9ca avcodec/movenc: fix spelling in warning message
hldr -> hdlr
2021-06-01 23:49:35 +05:30
Michael Niedermayer 4c1afa2925 avformat/movenc: Check pal_size before use
Fixes: assertion failure
Fixes: out of array read
Fixes: Ticket8190
Fixes: CVE-2020-22015

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2021-05-29 20:14:24 +02:00
Justin Ruggles c75dfa0435 movenc: add movie_timescale option instead of hardcoding 1000
There are cases where using 1000 as the MP4 timescale is not
accurate enough, for example when one needs sample-accurate audio
handling.

This adds a new AVOption to the MOV/MP4 muxer to override the
movie timescale, but it still defaults to 1000 to maintain current
default behavior.

Signed-off-by: Vittorio Giovara <vittorio.giovara@gmail.com>
2021-05-04 15:32:26 +02:00
Andreas Rheinhardt bc70684e74 avformat: Constify all muxer/demuxers
This is possible now that the next-API is gone.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2021-04-27 11:48:06 -03:00
James Almer 3749eede66 avformat: remove deprecated AVStream.codec
Signed-off-by: James Almer <jamrial@gmail.com>
2021-04-27 11:47:24 -03:00
Andreas Rheinhardt ef6a9e5e31 avutil/buffer: Switch AVBuffer API to size_t
Announced in 14040a1d91.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2021-04-27 10:43:13 -03:00
Andreas Rheinhardt 7b6012efaa avformat/mov, movenc: Stop exporting rotation via metadata
Deprecated in ddef3d902f.

(The reference file of the mov-zombie test needed to be updated, because
a rotate metadata tag is no longer exported; the side-data is of course
still present.)

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2021-04-27 10:43:09 -03:00
James Almer bc01061231 avformat/movenc: fix writing dOps atoms
Don't blindly copy all bytes in extradata past ChannelMappingFamily. Instead
check if ChannelMappingFamily is not 0 and then only write the correct amount
of bytes from ChannelMappingTable, as defined in the spec[1].

Fixes part of ticket #9190.

[1] https://opus-codec.org/docs/opus_in_isobmff.html#4.3.2

Signed-off-by: James Almer <jamrial@gmail.com>
2021-04-16 11:08:40 -03:00
Anton Khirnov 7ec8229982 lavf/movenc: use framerate correctly in mov_write_tmcd_tag
Current code uses its inverse.
2021-04-08 11:03:15 +02:00
Andreas Rheinhardt c81b8e04aa Avoid intermediate bitcount for number of bytes in PutBitContext
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2021-03-30 12:36:32 +02:00
Michael Niedermayer 5c2ff44f91 avformat/movenc: Avoid loosing cluster array on failure
Fixes: crash
Fixes: check_pkt.mp4

Found-by: Rafael Dutra <rafael.dutra@cispa.de>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2021-03-26 16:00:14 +01:00
James Almer ecdad29b67 avformat/movenc: use av_packet_alloc() to allocate packets
Signed-off-by: James Almer <jamrial@gmail.com>
2021-03-17 15:06:49 -03:00
James Almer e07126f54a avformat: use the buffer_size_t typedef where required
Signed-off-by: James Almer <jamrial@gmail.com>
2021-03-10 20:26:36 -03:00
Andreas Rheinhardt 0e645b98c6 Remove double ';'
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2021-03-01 06:10:44 +01:00
Andreas Rheinhardt 90723d1721 avformat/movenc: Only check for timecodes if they are used
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2021-02-28 17:14:21 +01:00
Andreas Rheinhardt 13101f916f avformat/movenc: Don't check for disabled muxers
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2021-02-28 17:14:21 +01:00
Andreas Rheinhardt f87b33b5bf avformat/movenc: Remove always true check
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2021-02-28 17:14:21 +01:00
Andreas Rheinhardt a442670a32 avformat/movenc: Deduplicate codec tags lists
Also saves relocations.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2021-02-23 11:30:57 +01:00
James Almer d5d6751a55 avformat/mux: return a pointer to the packet in ff_interleaved_peek()
And make it const, so the caller doesn't attempt to change it.
ff_get_muxer_ts_offset() should be used to get the muxer timestamp offset.

Signed-off-by: James Almer <jamrial@gmail.com>
2021-02-13 13:05:26 -03:00
Jan Ekström 752f14ff94 avformat/movenc: remove unnecessary HDR metadata log messages
These made sense before 3ebf449766
when the creation of these boxes was specifically requested by the
user, but now they have become unnecessary as they are just like
many other boxes: If the input has the information required, they
are written, otherwise they are not.

They were moved to verbose verbosity level (which happens to be
the last level still relatively usable), and now appear either once
(normal MP4 muxing), or thrice (with the faststart flag set) in
any normal MP4 usage, without giving much useful information. Thus,
remove them in their current form.
2021-02-10 21:35:21 +02:00
Martin Storsjö c2424b1f35 movenc: Present durations in mvhd/tkhd/mdhd as they are after edits
If the edit lists remove parts of the output timeline, or add a
delay to it, this should be included in the mvhd/tkhd/mdhd durations,
which should correspond to the edit lists.

For tracks starting with pts < 0, the edit list trims out the segment
before pts=0. For tracks starting with pts > 0, a delay element is
added in the edit list, delaying the start of the track data.

In both cases, the practical effect is that the post-edit output
is as if the track had started with pts = 0. Thus calculate the range
from pts=0 to end_pts, for the purposes of mvhd/tkhd/mdhd, unless
edit lists explicitly are disabled.

mov_write_edts_tag needs to operate on the actual pts duration of
the track samples, not the duration that already takes the edit
list effect into account.

Signed-off-by: Martin Storsjö <martin@martin.st>
2021-01-15 15:01:03 +02:00
Leo Izen f5dcaf2daa avformat/movenc: allow Apple Lossless inside mp4
The 'alac' identifier has been registered to ISO and thus towards
ISOBMFF at the MP4 registration authority. The existing non-MOV
mux mode matches the official ALAC-in-MP4 specification.
2020-11-22 14:28:31 +02:00
Andreas Rheinhardt 290de64759 avformat/movenc: Avoid allocation for timecode track
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-10-05 00:50:07 +02:00
Andreas Rheinhardt e3b5316bed avformat/movenc: Don't forget to free fragment buffers
The buffers used when fragmented output is enabled have up until now not
been freed in the deinit function; they leak e.g. if one errors out of
mov_write_trailer() before one reaches the point where they are normally
written out and freed. This can e.g. happen if allocating new vos_data
fails at the beginning of mov_write_trailer().

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-10-05 00:49:31 +02:00
Andreas Rheinhardt 432f291dff avformat/movenc: Free old vos_data before overwriting it
Otherwise the old data leaks whenever extradata needs to be rewritten
(e.g. when encoding FLAC with our encoder that sends an updated
extradata packet at the end).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-10-05 00:49:11 +02:00
Andreas Rheinhardt 0336bbf68a avformat/movenc: Don't free AVCodecParameters manually
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-10-05 00:48:55 +02:00
Andreas Rheinhardt e6cdd23bc7 avformat/movenc: Make some AVCodecTag tables static
They are not used anywhere else.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-10-04 23:41:26 +02:00
Andreas Rheinhardt 22c3cd1760 avformat/movenc: Fix segfault when remuxing rtp hint stream
When remuxing an rtp hint stream (or any stream with the tag "rtp "),
the mov muxer treats this as one of the rtp hint tracks it creates
internally when ordered to do so; yet this track lacks the
AVFormatContext for the hinting rtp muxer, leading to segfaults in
mov_write_udta_sdp() if a "trak" atom is written for this stream; if not,
the stream's codecpar is freed by mov_free() as if the mov muxer owned
it (it does for the internally created "rtp " tracks), but without
resetting st->codecpar, leading to double-frees lateron. This commit
therefore ignores said tag which makes rtp hint streams unremuxable.

This fixes tickets #8181 and #8186.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-10-04 23:38:43 +02:00
Jan Ekström d9e812797c avformat/movenc: handle tracks w/o AVStreams in calculate_mpeg4_bit_rates
The generated text streams for chapters lack an AVStream since they
are but an internal concept within movenc.

Fixes #8910
2020-10-02 16:49:44 +03:00
Andreas Rheinhardt 22a2386a56 avformat/movenc: Fix stack overflow when remuxing timecode tracks
There are two possible kinds of timecode tracks (with tag "tmcd") in the
mov muxer: Tracks created internally by the muxer and timecode tracks
sent by the user. If any of the latter exists, the former are
deactivated. The former all belong to another track, the source
track; the latter don't have a source track set, but the index of the
source track is initially zeroed by av_mallocz_array(). This is a
problem since 3d894db700cc1e360a7a75ab9ac8bf67ac6670a3: Said commit added
a function that calculates the duration of tracks and the duration of
timecode tracks is calculated by rescaling the duration (calculated by
the very same function) of the source track. This gives an infinite
recursion if the first track (the one that will be treated as source
track for all timecode tracks) is a timecode track itself, leading to a
stack overflow.

This commit fixes this by not using the nonexistent source track
when calculating the duration of timecode tracks not created internally
by the mov muxer.

Reviewed-by: Martin Storsjö <martin@martin.st>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-10-01 14:30:34 +02:00
Andreas Rheinhardt 4106013523 avformat/movenc: Fix segfault upon allocation error
Reviewed-by: Jan Ekström <jeebjp@gmail.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-09-28 20:41:20 +02:00
Jan Ekström 519bd469d0 avformat/movenc: simplify ISML manifest bit rate logic
The newly calculated average bit rate value is pretty much what is
being done here.
2020-09-22 18:25:44 +03:00
Jan Ekström 308882d9f2 avformat/movenc: use more fall-back values for average bit rate fields
If the average bit rate cannot be calculated, such as in the case
of streamed fragmented mp4, utilize various available parameters
in priority order.

Tests are updated where the esds or btrt or ISML manifest boxes'
output changes.
2020-09-22 18:25:44 +03:00
Jan Ekström 3838e8fc21 avformat/movenc: implement writing of the btrt box
This is utilized by various media ingests to figure out the bit
rate of the content you are pushing towards it, so write it for
video, audio and subtitle tracks in case at least one nonzero value
is available. It is only mentioned for timed metadata sample
descriptions in QTFF, so limit it only to ISOBMFF (MODE_MP4) mode.

Updates the FATE tests which have their results changed due to the
20 extra bytes being written per track.
2020-09-22 18:21:31 +03:00
Jan Ekström 6475dc18cc avformat/movenc: utilize bit rate helper function in ISML writing
This way we have a single location in movenc which utilizes the
CPB properties.
2020-09-21 19:19:52 +03:00
Jan Ekström 667d5045d3 avformat/movenc: split MPEG-4 bit rate value calculation
This can now be re-utilized in other places.
2020-09-21 17:31:07 +03:00
Michael Bradshaw c5b20cfe19 avformat/movenc: write the colr atom by default
The write_colr flag has been marked as experimental for over 5 years.
It should be safe to enable its behavior by default as follows:

  - Write the colr atom by default for mp4/mov if any of the following:
     - The primaries/trc/matrix are all specified, OR
     - There is an ICC profile, OR
     - The user specified +write_colr
  - Keep the write_colr flag for situations where the user wants to
    write the colr atom even if the color info is unspecified (e.g.,
    http://ffmpeg.org/pipermail/ffmpeg-devel/2020-March/259334.html)

This fixes https://trac.ffmpeg.org/ticket/7961

Signed-off-by: Michael Bradshaw <mjbshaw@google.com>
2020-08-21 10:01:58 -07:00
Derek Buitenhuis d5247fb1da lavf/movenc: Use a dynamic buffer when writing the mfra box
When doing streamed output, with e.g. +dash, if the mfra box ended
up being larger than the AVIOContext write buffer, the (unchecked)
seeking back to update the box size would silently fail and produce
an invalid mfra box.

This is similar to how other boxes are written in fragmented mode.

Signed-off-by: Derek Buitenhuis <derek.buitenhuis@gmail.com>
2020-06-28 20:16:29 +01:00
Andreas Rheinhardt c3cd6b765b avcodec, avformat: Remove unnecessary initializations of side data size
Reviewed-by: Anton Khirnov <anton@khirnov.net>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-06-22 12:20:37 +02:00