Commit Graph

1113 Commits

Author SHA1 Message Date
Michael Niedermayer
e1089a581b 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>
(cherry picked from commit 4c1afa2925)
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2021-10-06 14:41:41 +02:00
Michael Niedermayer
3ac8105084 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>
(cherry picked from commit 5c2ff44f91)
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2021-10-06 14:41:41 +02:00
Andreas Rheinhardt
bcf3ac039b 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>
(cherry picked from commit e3b5316bed)
2021-02-27 07:20:58 +01:00
Andreas Rheinhardt
3fc8dc3589 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>
(cherry picked from commit 432f291dff)
2021-02-27 07:20:58 +01:00
Andreas Rheinhardt
dc4e3b9586 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>
(cherry picked from commit 22c3cd1760)
2021-02-27 07:20:58 +01:00
Andreas Rheinhardt
0608949fa2 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 3d894db700: 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>
(cherry picked from commit 22a2386a56)
2021-02-27 07:20:58 +01:00
Andreas Rheinhardt
89231df489 avformat/movenc: Fix segfault upon allocation error
Reviewed-by: Jan Ekström <jeebjp@gmail.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
(cherry picked from commit 4106013523)
2021-02-27 07:20:58 +01:00
Martin Storsjö
638b5d51cf movenc: Fix accidental leftover duplication from 1aec1fbcc5
Signed-off-by: Martin Storsjö <martin@martin.st>
2020-05-22 22:00:03 +03:00
Martin Storsjö
1aec1fbcc5 movenc: Fix conversion of the first frame for extradata-less H264/HEVC
Move the copying of the frame to vos_data further up in the function,
so that when writing the actual frame data for the first frame, it's
clear that the stream really is in annex b format, for the cases where
we create extradata from the first frame.

Alternatively - we could invert the checks for bitstream format. If
extradata is missing, we can't pretend that the bitstream is in
mp4 form, because we can't even know the NAL unit length prefix size
in that case.

Also avoid creating extradata for AVC intra. If the track tag is
an AVC intra tag, don't copy the frame into vos_data - this matches
other existing cases of how vos_data and TAG_IS_AVCI interact in
other places.

Signed-off-by: Martin Storsjö <martin@martin.st>
2020-05-22 21:01:44 +03:00
Martin Storsjö
9f891d0b4b movenc: Use first H264/HEVC frame as extradata, if it is missing
Sticking a full frame in the extradata works, as the code for writing
the avcC/hvcC extracts the relevant parameter set NAL units - provided
that they actually exist in the frame.

Some encoders don't provide split out extradata directly on init (or
at all). In particular, the MediaFoundation encoder wrapper doesn't
always (depending on the actual encoder device) - this is the case for
Qualcomm's HEVC encoder on SD835, and also on some QSV H264 encoders).

This only works for cases where the moov hasn't already been written
(e.g. when not writing fragmented mp4 with empty_moov, unless using
the delay_moov option).

Signed-off-by: Martin Storsjö <martin@martin.st>
2020-05-20 22:55:00 +03:00
Limin Wang
0b11ad5172 avformat/movenc: cosmetics
Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2020-04-26 14:34:03 +02:00
Jun Zhao
b8243b3edf lavf/movenc: enable dvcC/dvvC box support from DOVI sidedata
enable dvcC/dvvC box support from DOVI sidedata.

Signed-off-by: Jun Zhao <barryjzhao@tencent.com>
2020-04-23 08:06:19 +08:00
Limin Wang
a97281699b avformat/movenc: cosmetics
Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
Signed-off-by: Josh de Kock <josh@itanimul.li>
2020-04-20 15:59:32 +00:00
James Almer
e7eb379d98 avformat/movenc: remove call to av_copy_packet_side_data() when concatenating eac3 syncframes
This generates a potential memory leak, and mixes side data from the last
packet with other properties from the first.

Keep all the properties from the first packet only in the output packet
instead.

Signed-off-by: James Almer <jamrial@gmail.com>
2020-04-15 14:12:38 -03:00
Michael Bradshaw
4ae8d13e6d avformat/movenc: write the mdcv atom by default
The mdcv atom isn't in ISO/IEC 14496-12:2015 but it is expected to be
added soon. See:
http://ffmpeg.org/pipermail/ffmpeg-devel/2020-April/259529.html

The mdcv atom is already parsed in FFmpeg in mov.c.

Signed-off-by: Michael Bradshaw <mjbshaw@google.com>
2020-04-13 10:43:48 -06:00
Michael Bradshaw
3ebf449766 avformat/movenc: remove the write_clli mov flag
The clli atom is expected to be standardized soon. See
http://ffmpeg.org/pipermail/ffmpeg-devel/2020-April/259529.html

We now write the clli atom by default.

Signed-off-by: Michael Bradshaw <mjbshaw@google.com>
2020-04-13 10:37:45 -06:00
Michael Bradshaw
9842fd3aaf avformat/movenc: stop guessing colr atom values 2020-04-13 10:04:19 -06:00
Michael Bradshaw
b1699f4ac3 avformat/movenc: use enum values directly for colr atom
The switch cases were missing:

  - Primaries: bt470m, film, smpte428, and ebu3213.
  - TRCs: gamma22, gamma28, linear, log, log_sqrt, iec61966_2_4, bt1361,
    iec61966_2_1, bt2020_10bit, and bt2020_12bit.
  - Space: rgb, fcc, ycgco, bt2020_cl, smpte2085, chroma-derived-nc,
    chroma-derived-c, and ictcp.

They also annoyingly remapped the following (which are functionally
equivalent but can be treated differently by clients):

  - smpte240m primaries to smpte170m.
  - smpte170m TRC to bt709.
  - bt470bg color space to smpte170m.

The enum values in FFmpeg are the same values as ITU-T H.273 and
ISO/IEC 23001-8 so we can just use them directly, which is both simpler
and preserves the user intent.

Signed-off-by: Michael Bradshaw <mjbshaw@google.com>
2020-04-13 09:46:07 -06:00
Michael Bradshaw
5a0575e32c avformat/movenc: add write_clli flag to write clli atom
The clli atom isn't in ISO/IEC 14496-12:2015 so the flag is marked as
experimental and the clli atom is not written by default.

The clli atom is already parsed by FFmpeg in mov.c.

Signed-off-by: Michael Bradshaw <mjbshaw@google.com>
2020-04-01 09:44:18 -06:00
Lynne
ca7a192d10
movenc: mark Opus encapsulation as stable
The specifications are de-facto frozen now as they've already been used in
production for years, the author has indicated reluctance on IRC to change
it further, and the only potential changes would, from what I understand,
be forward-compatible.
2020-03-27 13:34:09 +00:00
James Almer
59c993e227 avformat/movenc: Reduce size of the allocated MOVIentry array
Increasing it by 2048 entries per realloc is exessive.
Reduces memory usage, especially on long, non fragmented output.

Reviewed-by: Anton Khirnov <anton@khirnov.net>
Signed-off-by: James Almer <jamrial@gmail.com>
2020-03-26 11:46:08 -03:00
vectronic
dc1c3c640d avformat/movenc: add ICC profile support to colr atom
If 'write_colr' movflag is set, then movflag 'prefer_icc' can
be used to first look for an AV_PKT_DATA_ICC_PROFILE entry to
encode.

If ICC profile doesn't exist, default behaviour enabled by
'write_colr' occurs.

Signed-off-by: vectronic <hello.vectronic@gmail.com>
2020-03-10 15:11:47 +00:00
Andreas Rheinhardt
a3e43e0cf3 avformat/av1: Avoid allocation + copying when filtering OBUs
Certain types of OBUs are stripped away before muxing into Matroska and
ISOBMFF; there are two functions to do this: One that outputs by
directly writing in an AVIOContext and one that returns a freshly
allocated buffer with the units not stripped away copied into it.

The latter option is bad for performance, especially when the input
does already not contain any of the units intended to be stripped away
(this covers typical remuxing scenarios). Therefore this commit changes
this by avoiding allocating and copying when possible; it is possible if
the OBUs to be retained are consecutively in the input buffer (without
an OBU to be discarded between them). In this case, the caller receives
the offset as well as the length of the part of the buffer that contains
the units to be kept. This also avoids copying when e.g. the only unit
to be discarded is a temporal delimiter at the front.

For a 22.7mb/s file with average framesize 113 kB this improved the time
for the calls to ff_av1_filter_obus_buf() when writing Matroska from
313319 decicycles to 2368 decicycles; for another file with 1.5mb/s
(average framesize 7.3 kB) it improved from 34539 decicycles to 1922
decicyles. For these files the only units that needed to be stripped
away were temporal unit delimiters at the front.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2020-01-26 12:41:32 -03:00
Andreas Rheinhardt
e8518c2580 avformat/movenc: Check for reformatting errors when writing hint tracks
If this is not done, the avio_write() calls will cause segfaults
immediately afterwards on error.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2020-01-26 12:41:31 -03:00
Gyan Doshi
75d1d9eb34 avformat/movenc: allow ISMV timescale to be user-set
As per the PIFF standard, the timescale of 10000000
is recommended but not mandatory, so don't override
the user-set value.

A warning is shown for non-recommended values.
2020-01-25 21:36:17 +05:30
Andreas Rheinhardt
61e0d71946 avformat/movenc, segafilmenc: Remove unnecessary avio_tell()
When the faststart option for the mov/mp4 muxer is used, the current
position (i.e. the size of the already written data pre-shifting) was
evaluated twice: First in an initialization and then again later,
overwriting the first value without having ever touched it. So remove
the initialization.

Also, the clone of this code in the Sega FILM muxer behaves the same and
has been treated the same.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2020-01-17 20:17:55 +01:00
James Almer
ab9627223e Revert "avformat/utils: make ff_ntp_time() accept a timestamp as input argument"
Signed-off-by: James Almer <jamrial@gmail.com>
2020-01-16 22:27:35 -03:00
James Almer
cc929cee76 avformat/movenc: add a flag to enable CMAF compatability
Sets some required constrains and reports compatability with the relevant
compatible brand.

Signed-off-by: James Almer <jamrial@gmail.com>
2020-01-15 23:34:21 -03:00
James Almer
7abc0979e6 avformat/movenc: use AVProducerReferenceTime side data to write the prft atom
If not available, set flags to 24 (bits 4 and 5), to signal the wallclock value
is read at the time of writing the atom.

Signed-off-by: James Almer <jamrial@gmail.com>
2020-01-15 23:34:20 -03:00
James Almer
4e4ac20340 avformat/utils: make ff_ntp_time() accept a timestamp as input argument
Will be needed by the next patch.

Signed-off-by: James Almer <jamrial@gmail.com>
2020-01-15 23:34:20 -03:00
Martin Storsjö
3d894db700 movenc: Write durations based on pts into mvhd/mdhd/tkhd/elst
Keep all the existing data fields as they are (there's lots and
lots of nontrivial calculation and heuristics based on them in
their current form), but derive the duration as the difference
between the pts of the first packet to the maximum pts+duration
(not necessarily the last packet); use this duration in any box
where the actual presentation duration is supposed to be.

Fixes: 8420

Signed-off-by: Martin Storsjö <martin@martin.st>
2020-01-10 09:14:00 +02:00
Marton Balint
3414115cd4 avformat: convert some avio_flush() calls to avio_write_marker(AVIO_DATA_MARKER_FLUSH_POINT)
Converting explicit avio_flush() calls helps us to buffer more data and avoid
flushing the IO context too often which causes reduced IO throughput for
non-streamed file output.

The user can control FLUSH_POINT flushing behaviour using the -flush_packets
option, the default typically means to flush unless a non-streamed file output
is used, so this change should have no adverse effect on streaming even if it
is assumed that after an avio_flush() the output buffer is clean so small
seekbacks within the output buffer will work even when the IO context is not
seekable.

Signed-off-by: Marton Balint <cus@passwd.hu>
2020-01-07 21:51:45 +01:00
Marton Balint
4bf90e095b avformat: remove avio_flush() calls from the end of write_header functions
To make it consistent with other muxers.

The user can still control the generic flushing behaviour after write_header
(same way as after packets) using the -flush_packets option, the default
typically means to flush unless a non-streamed file output is used.

Therefore this change should have no adverse effect on streaming, even if it is
assumed that the first packet has a clean buffer, so small seekbacks within the
output buffer work even when the IO context is not seekable.

Signed-off-by: Marton Balint <cus@passwd.hu>
2020-01-07 21:51:45 +01:00
James Almer
191df4f2d1 avformat/movenc: use iso6 major brand when signed CTS offsets are used in trun boxes
Signed-off-by: James Almer <jamrial@gmail.com>
2019-12-21 11:57:28 -03:00
James Almer
245ace4e2d avformat/movenc: ensure we don't write the major brand as a compatible brand more than once
Signed-off-by: James Almer <jamrial@gmail.com>
2019-12-21 11:57:28 -03:00
James Almer
58ac760816 avformat/movenc: write the major brand also as the first compatible brand
Signed-off-by: James Almer <jamrial@gmail.com>
2019-12-21 11:57:28 -03:00
Yuki Tsuchiya
0ceed513d5 lavf/movenc: cosmetics
Signed-off-by: Yuki Tsuchiya <Yuki.Tsuchiya@sony.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2019-12-12 12:36:13 -03:00
Yuki Tsuchiya
632b8298b7 lavf/isom: support for demuxing and remuxing of MPEG-H 3D Audio in MP4
Implemented according to the specification at https://www.iso.org/standard/69561.html
The 'mhm1' sample entry is registered with MP4RA, which is defined as MHAS encapsulated single stream MPEG-H 3D Audio.
'MHAS' stands for MPEG-H audio stream, which contains encoded audio data and corresponds metadata for decoding.
This patch enables extracting the MHAS bitstream from MP4 and remuxing into MP4.

Signed-off-by: James Almer <jamrial@gmail.com>
2019-12-12 11:44:57 -03:00
Andreas Rheinhardt
4d97b2ad2f avformat/movenc: Avoid allocation for small dynamic buffers
By using avio_get_dyn_buf() + ffio_free_dyn_buf() instead of
avio_close_dyn_buf() + av_free() one can avoid an allocation + copy for
small dynamic buffers.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2019-11-28 15:20:37 -03:00
James Almer
58aa0ed8f1 aformat/movenc: add missing padding to output track extradata
Fixes ticket #8183.

Tested-by: Thierry Foucu <tfoucu@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2019-09-26 15:19:00 -03:00
Andreas Rheinhardt
646799b42f avformat/movenc: Fix undefined shift
Fixes the movenc FATE-test.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2019-09-26 14:12:24 +02:00
James Almer
179e15dc97 avformat/movenc: don't hardcode the colr box size
Signed-off-by: James Almer <jamrial@gmail.com>
2019-09-19 21:32:50 -03:00
James Almer
808a6717e0 avformat/movenc: add muxing support for Dolby TrueHD streams
Signed-off-by: James Almer <jamrial@gmail.com>
2019-08-24 22:42:34 -03:00
Marton Balint
397abca001 avformat/movenc: use unspecified language by default
English was used before.

Signed-off-by: Marton Balint <cus@passwd.hu>
2019-07-12 20:26:38 +02:00
Carl Eugen Hoyos
d6a8392224 lavf/movenc: Pass correct pointer to av_log(). 2019-04-09 15:29:23 +02:00
Gyan Doshi
8161ac2902 lavf/movenc: fix tmcd writing for non-MP4/MOV modes
write_tmcd allows tmcd track to be created with any mode but in
mov_write_header, index for first tmcd track is only set for modes
MP4 or MOV, causing a crash if tmcd creation is attempted with other
modes.
2019-04-03 15:50:22 +05:30
Karthick J
3d0894990d avformat/movenc: Fix skip_trailer when global_sidx is enabled 2019-04-02 11:15:57 +05:30
James Almer
27c94c57dc avformat/movenc: free eac3 private data only when closing the stream
This makes sure the data is available when writing the moov atom during the
second pass triggered by the faststart movflag.

Fixes ticket #7780

Signed-off-by: James Almer <jamrial@gmail.com>
2019-03-31 20:08:28 -03:00
Paweł Wegner
4ed6a485d3 libavformat/movenc: mov: added subtitle codec tags to codec tag list
This fixes avformat_query_codec incorrectly returning 0 for
mov container and mov_text subtitles.

Signed-off-by: Paweł Wegner <pawel.wegner95@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2019-03-18 19:15:35 -03:00
Nikolas Bowe
2b8458fcc5 avformat/movenc: Add more error checking when writing sample entries.
Fixes a problem where a sample entry which cannot be written correctly appears to succeed, but produces an invalid file.
For example, this command:
ffmpeg -f lavfi -i sine=frequency=1000:duration=5 -codec:a ac3 -movflags +empty_moov -frag_duration 5000000 /tmp/foo.mp4
produced a file with the ac-3 sample entry, but no AC3SpecificBox (dac3) child, which is invalid according to ETSI TS 102 366.

Reviewed-by: Baptiste Coudurier <baptiste.coudurier@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2019-02-07 00:29:01 +01:00