Commit Graph

783 Commits

Author SHA1 Message Date
Stefano Sabatini
bba492dc9c lavf/matroskaenc: apply consistent style to options descriptions 2024-04-19 08:44:20 +02:00
Andreas Rheinhardt
790f793844 avutil/common: Don't auto-include mem.h
There are lots of files that don't need it: The number of object
files that actually need it went down from 2011 to 884 here.

Keep it for external users in order to not cause breakages.

Also improve the other headers a bit while just at it.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2024-03-31 00:08:43 +01:00
Andreas Rheinhardt
233e13f285 avformat/mux: Rename FF_FMT_ALLOW_FLUSH->FF_OFMT_FLAG_ALLOW_FLUSH
It better reflects that this is a muxer-only flag.
Also document the flag.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2024-03-22 23:57:19 +01:00
James Almer
53dd31497b avformat/matroska: use named constants for ITU-T T.35 metadata
Signed-off-by: James Almer <jamrial@gmail.com>
2024-03-19 10:49:20 -03:00
Anton Khirnov
1e7d2007c3 all: use designated initializers for AVOption.unit
Makes it robust against adding fields before it, which will be useful in
following commits.

Majority of the patch generated by the following Coccinelle script:

@@
typedef AVOption;
identifier arr_name;
initializer list il;
initializer list[8] il1;
expression tail;
@@
AVOption arr_name[] = { il, { il1,
- tail
+ .unit = tail
}, ...  };

with some manual changes, as the script:
* has trouble with options defined inside macros
* sometimes does not handle options under an #else branch
* sometimes swallows whitespace
2024-02-14 14:53:41 +01:00
asivery
7f4abe7c37 avformat/matroska: Add support for A_ATRAC/AT1
Signed-off-by: asivery <asivery@protonmail.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2024-02-13 15:27:29 +01:00
Anton Khirnov
08bebeb1be Revert "all: Don't set AVClass.item_name to its default value"
Some callers assume that item_name is always set, so this may be
considered an API break.

This reverts commit 0c6203c97a.
2024-01-20 10:34:48 +01:00
Andreas Rheinhardt
0c6203c97a all: Don't set AVClass.item_name to its default value
Unnecessary since acf63d5350adeae551d412db699f8ca03f7e76b9;
also avoids relocations.

Reviewed-by: Anton Khirnov <anton@khirnov.net>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2023-12-22 15:12:33 +01:00
James Almer
5432d2aaca avformat/avformat: use the side data from AVStream.codecpar
Deprecate AVStream.side_data and its helpers in favor of the AVStream's
codecpar.coded_side_data.

This will considerably simplify the propagation of global side data to decoders
and from encoders. Instead of having to do it inside packets, it will be
available during init().
Global and frame specific side data will therefore be distinct.

Signed-off-by: James Almer <jamrial@gmail.com>
2023-10-06 10:03:57 -03:00
Andreas Rheinhardt
dd48e49d54 avformat/avformat: Deprecate AVFMT_ALLOW_FLUSH
It is of no value to the user, because every muxer can always
be flushed with a NULL packet. As its documentation shows
("If not set, the muxer will not receive a NULL packet in
the write_packet function") it is actually an internal flag
that has been publically exposed because there was no internal
flags field for output formats for a long time. But now there is
and so use it by replacing the public flag with a private one.

Reviewed-by: James Almer <jamrial@gmail.com>
Reviewed-by: Anton Khirnov <anton@khirnov.net>
Reviewed-by: Martin Storsjö <martin@martin.st>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2023-10-03 01:45:46 +02:00
Andreas Rheinhardt
259e1d2bd7 avformat/matroskaenc: Write default duration for audio
This is easily possible for those codecs with a fixed frame-size
(in samples).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2023-10-02 12:22:20 +02:00
Andreas Rheinhardt
0c1103d4dc avformat/matroskaenc: Don't create wrong packet durations
We have to write an explicit BlockDuration element (and use
a BlockGroup instead of a SimpleBlock) in case the Track
has a DefaultDuration that is inconsistent with the duration
of the packet.

The matroska-h264-remux test uses a file with coded fields
where the duration of a Block is the duration of a field,
not of a frame, therefore this patch writes said BlockDuration
elements.

(When using a BlockGroup, one has to add ReferenceBlock elements
to distinguish keyframes from non-keyframes. Unfortunately,
the AV1 codec mapping [1] requires us to reference all references
and to really use the real references, which requires a lot of
effort for basically no gain. When BlockGroups are used with AV1,
the created files are most likely invalid, both before and after
this patch, but this patch makes this more likely to happen.)

[1]: https://github.com/ietf-wg-cellar/matroska-specification/blob/master/codec/av1.md

Reviewed-by: James Almer <jamrial@gmail.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2023-10-02 12:22:05 +02:00
Andreas Rheinhardt
8238bc0b5e avcodec/defs: Add AV_PROFILE_* defines, deprecate FF_PROFILE_* defines
These defines are also used in other contexts than just AVCodecContext
ones, e.g. in libavformat. Furthermore, given that these defines are
public, the AV-prefix is the right one, so deprecate (and not just move)
the FF-macros.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2023-09-07 00:39:02 +02:00
Andreas Rheinhardt
fc993e7a53 avformat/matroska(dec|enc): Rename macro to avoid clash with winbase.h
winbase.h defines IGNORE and is included via bzlib.h when compiling
for Windows. So rename this macro to NOTHING.
Also rename the muxer macro for consistency.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2023-09-02 10:36:20 +02:00
Andreas Rheinhardt
2d09488ac3 avformat/matroskaenc: Improve message for WebM-incompatible StereoModes
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2023-08-30 19:31:34 +02:00
Andreas Rheinhardt
884653ee5b avformat/matroskaenc: Don't add side-data to input stream
When muxing, the AVStreams' side-data is typically set
by the caller before avformat_write_header();
it is not documented to be else. Yet the Matroska muxer
added an AVStereo3D side data if certain metadata
was present:

Since commit 4d686fb721
(adding support for AVStereo3D stream side-data),
the Matroska muxer checked certain stream tags that
contain Matroska's StereoMode and (if they are present)
converted this value into an AVStereo3D struct that
gets attached to the AVStream (reusing a function from
the demuxer). Afterwards the AVStereo3D side data struct
(whether it has just been added by the muxer or not) gets
parsed and converted back into a Matroska StereoMode.

Besides being an API violation this change broke
StereoMode values without a corresponding AVStereo3D
(namely the anaglyph ones).

This commit fixes this: A StereoMode given via tags
is now used-as-is; if no such tag exists and an AVStereo3D
side data exists, it is converted into the corresponding
StereoMode (if possible). This approach also fixes
handling of the anaglyph ones; the changes to the
matroska-stereo_mode are due to this.

The new STEREOMODE_STEREO3D_MAPPING has been put to
good use for this.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2023-08-30 19:31:30 +02:00
Andreas Rheinhardt
088f08db71 avformat/matroskaenc: Avoid atoi()
It has undefined behaviour in case the value does not fit into an int.
Also stop allowing to override a stream level "alpha_mode" tag
by an AVFormatContext one and properly check that the stereo_mode
number given via a tag is actually in the range 0..14: Negative
values would have been treated as zero before this patch.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2023-08-30 19:15:48 +02:00
Steinar H. Gunderson
69c308a6d1 avformat/matroskaenc: Fix writing of markers
When the marker writing code was merged from libav to FFmpeg
in dc62016c, it failed to take into account that the meaning of
cluster_pos had changed in bda5b662; in particular, the special
value for “I'm not currently working on a cluster” had changed
from 0 to -1. This makes the avio_write_marker() call never
be called. Update the if statement to fix it.

Fixes: Ticket9843
Signed-off-by: Steinar H. Gunderson <steinar+ffmpeg@gunderson.no>
Signed-off-by: Martin Storsjö <martin@martin.st>
2023-08-29 23:28:44 +03:00
Andreas Rheinhardt
d53acf452f avformat/matroskaenc: Don't write \0 unnecessarily
Writing the duration SimpleTag is special: It's size is
reserved in advance via an EBML Void element (if seekable)
and this reserved space is overwritten when writing the trailer;
it does not use put_ebml_string().

The string to write is created via snprintf on a buffer
of size 20; this buffer is then written via put_ebml_binary()
with a size of 20.

EBML strings need not be zero-terminated; if not, they
are implicitly terminated by the element's length field.
snprintf() always zero-terminates the buffer, i.e.
the last byte can be discarded when using an EBML string.
This patch does this.

The FATE changes are as expected: One byte saved for every
track; the only exception is the matroska-qt-mode test:
An additional byte is saved because an additional byte
could be saved from the enclosing Tags length field.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2023-08-10 23:56:35 +02:00
Andreas Rheinhardt
59cb099478 avformat/matroskaenc: Reindent after the previous commit
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2023-08-10 23:56:35 +02:00
Andreas Rheinhardt
b5968df9f0 avformat/matroskaenc: Don't reserve space for HDR10+ when unnecessary
Do it only for video (the only thing for type for which HDR10+
makes sense).

This effectively reverts changes to several FATE ref-files
made in bda44f0f39.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2023-08-10 23:56:35 +02:00
Andreas Rheinhardt
a315474a02 avformat/matroskaenc: Add const where appropriate
Also move getting the DOVI side data immediately before its use.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2023-08-10 23:56:35 +02:00
Andreas Rheinhardt
3b75375c9c avformat/matroskaenc: Avoid allocations when writing Dynamic HDR10+
Possible since 61b27b15fc.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2023-08-10 23:56:35 +02:00
Andreas Rheinhardt
cf20f18c41 avformat/matroskaenc: Use dedicated pointer for accesses
Improves readability; also split overlong lines.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2023-08-10 23:56:35 +02:00
Andreas Rheinhardt
1ed03b5757 avformat/matroskaenc: Use proper AVIOContext
These two AVIOContexts currently coincide, but this is not
guaranteed to remain so (in fact, I have plans to write each
TrackEntry into its own AVIOContext).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2023-08-10 23:56:35 +02:00
Andreas Rheinhardt
f49515b082 avformat/matroskaenc: Remove unnecessary check
It is only WebVTT which is special in WebM; hypothetical future
subtitle codecs in WebM will presumably use the ordinary code.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2023-08-10 23:56:35 +02:00
Andreas Rheinhardt
43db0e5ecf avformat/matroskaenc: Hoist check out of loop
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2023-08-10 23:56:35 +02:00
Andreas Rheinhardt
1df13b3891 avformat/matroskaenc: Don't pretend to be able to mux RV30
The demuxer uses a extradata offset of 26, so we would need
to recreate the missing 26 bytes somehow in the muxer, but
we just don't. Remuxed files (like real/rv30.rm from the FATE-suite)
don't work due to missing extradata.

(The extradata offset also applies to RV40 and the extradata
is indeed lost upon remuxing, yet remuxing real/spygames-2MB.rmvb
works; our RV40 decoder does not use extradata at all.)

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2023-08-10 08:37:01 +02:00
Andreas Rheinhardt
4e43cdcd1a avformat/matroskaenc: Don't pretend to support unsupported codecs
RV10 and RV20 are unsupported because creating the correct CodecPrivate
is unsupported (the demuxer uses a codecpriv_offset of 26, so one
would need to recreate the missing 26 bytes); COOK and SIPR are
unsupported, because Matroska uses a packetization mode that is
different from what FFmpeg uses in its packets (see
matroska_parse_rm_audio() in the demuxer).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2023-08-10 08:37:01 +02:00
Andreas Rheinhardt
fd8a395cd7 avformat/matroskaenc: Don't reserve unnecessarily many EBML elements
bda44f0f39 added code that
potentially added another BlockMore master and BlockAdditional
data as well as BlockAddID number, yet it bumped the number
of EBML elements by four instead of only three.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2023-08-10 00:17:11 +02:00
Andreas Rheinhardt
c797b6400d avformat/matroskaenc: Support rotations
Matroska supports orthogonal transformations (both pure rotations
as well as reflections) via its 3D-projection elements, namely
ProjectionPoseYaw (for a horizontal reflection) as well as
ProjectionPoseRoll (for rotations). This commit adds support
for this.

Support for this in the demuxer has been added in
937bb6bbc1 and
the sample used in the matroska-dovi-write-config8 FATE-test
includes a displaymatrix indicating a rotation which is now
properly written and read, thereby providing coverage for
the relevant code in the muxer as well as the demuxer.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2023-08-10 00:17:11 +02:00
James Almer
efdc6e8200 avformat/matroskaenc: remove accidental variable shadowing
Should fix use of uninitialized value in a failure path.

Signed-off-by: James Almer <jamrial@gmail.com>
2023-05-20 09:28:46 -03:00
James Almer
1a7df525f6 avformat/matroskaenc: fix memory leak in fail codepath
Signed-off-by: James Almer <jamrial@gmail.com>
2023-04-10 22:12:13 -03:00
James Almer
bda44f0f39 avformat/matroskaenc: support writing Dynamic HDR10+ packet side data
Signed-off-by: James Almer <jamrial@gmail.com>
2023-04-08 10:28:41 -03:00
James Almer
1c2a1e0750 avformat/matroskaenc: write a MaxBlockAdditionID element
A non zero value is mandatory for Matroska if the track has blocks with BlockAdditions.

Signed-off-by: James Almer <jamrial@gmail.com>
2023-04-05 09:47:12 -03:00
James Almer
00fde8063a avformat/matroska: add a few more Block Addition ID Type enum values
Signed-off-by: James Almer <jamrial@gmail.com>
2023-04-05 09:47:12 -03:00
rcombs
f758d4fcb9 lavf/matroska: add support for ARIB captions 2023-03-08 14:57:54 -06:00
Andreas Rheinhardt
59c9dc82f4 avformat/avformat: Move AVOutputFormat internals out of public header
This commit does for AVOutputFormat what commit
20f9727018 did for AVCodec:
It adds a new type FFOutputFormat, moves all the internals
of AVOutputFormat to it and adds a now reduced AVOutputFormat
as first member.

This does not affect/improve extensibility of both public
or private fields for muxers (it is still a mess due to lavd).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Signed-off-by: Anton Khirnov <anton@khirnov.net>
2023-02-09 15:24:15 +01:00
Marvin Scholz
d75d8370bd avformat/matroskaenc: use av_dict_iterate
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-12-01 11:21:15 +01:00
Andreas Rheinhardt
2b41463b87 avformat/internal: Don't include avcodec.h
The general demuxing API uses parsers and decoders. Therefore
FFStream contains pointers to AVCodecContexts and
AVCodecParserContext and lavf/internal.h includes lavc/avcodec.h.

Yet actually only a few files files really use these; and it is best
when this number stays small. Therefore this commit uses opaque
structs in lavf/internal.h for these contexts and stops including
avcodec.h.
This also avoids including lavc/codec_desc.h implicitly. All other
headers are implicitly included as now (mostly through codec.h).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-09-26 03:02:50 +02:00
Andreas Rheinhardt
832e6563df avformat/matroska*: Use av_chroma_location_(pos_to_enum|enum_to_pos)
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-09-26 03:02:27 +02:00
Andreas Rheinhardt
8913539a5d avformat/matroskaenc: Write CodecDelay for codecs != Opus
The field is not specific to Opus.
The mp2fixed encoder signals initial_padding and is used
by both the matroska-encoding-delay test as well as
the lavf-mkv tests which necessitated several FATE ref changes.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-09-05 20:36:53 +02:00
Andreas Rheinhardt
bca4fef46a avformat/matroskaenc: Use custom min timestamp
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-09-05 20:36:22 +02:00
Andreas Rheinhardt
eb325324aa avformat/matroskaenc: Actually apply timestamp offset for Opus
Matroska generally requires timestamps to be nonnegative, but
there is an exception: Data that corresponds to encoder delay
and is not supposed to be output anyway can have a negative
timestamp. This is achieved by using the CodecDelay header
field: The demuxer has to subtract this value from the raw
(nonnegative) timestamps of the corresponding track.
Therefore the muxer has to add this value first to write
this raw timestamp.

Support for writing CodecDelay has been added in FFmpeg commit
d92b1b1bab and in Libav commit
a1aa37dd0b. The former simply
wrote the header field and did not apply any timestamp offsets,
leading to desynchronisation (if one uses multiple tracks).
The latter applied it at two places, but not at the one where
it actually matters, namely in mkv_write_block(), leading to
the same desynchronisation as with the former commit. It furthermore
used the wrong stream timebase to convert the delay to the
stream's timebase, as the conversion used the timebase from
before avpriv_set_pts_info().

When the latter was merged in 82e4f39883,
it was only done in a deactivated state that still did not
offset the timestamps when muxing due to "assertion failures
and av sync errors". a1aa37dd0b
made it definitely more likely to run into assertion failures
(namely if the relative block timestamp doesn't fit into an int16_t).

Yet all of the above issues have been fixed (in commits
962d631573,
5d3953a5dc and
4ebeab15b0. This commit therefore
enables applying CodecDelay, fixing ticket #7182.

There is just one slight regression from this: If one has input
with encoder delay where the first timestamp is negative, but
the pts of the part of the data that is actually intended to be
output is nonnegative, then the timestamps will currently by default
be shifted to make them nonnegative before they reach the muxer;
the muxer will then ensure that the shifted timestamps are retained.
Before this commit, the muxer did not ensure this; instead the
timestamps that the demuxer will output were shifted and
if the first timestamp of the actually intended output was zero
before shifting, then this unintentional shift just cancels
the shift performed before the packet reached the muxer.
(But notice that this only applies if all the tracks use the same
CodecDelay, or the relative sync between tracks will be impaired.)
This happens in the matroska-opus-remux and matroska-ogg-opus-remux
FATE tests. Future commits will forward the information that
the Matroska muxer has a limited capability to handle negative
timestamps so that the shifting in libavformat can take advantage
of it.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-09-05 20:35:20 +02:00
Andreas Rheinhardt
be0a2515ab avformat/matroskaenc: Don't override samplerate for CodecDelay
Opus can be decoded to multiple samplerates (namely 48kHz, 24KHz,
16Khz, 12 KHz and 8Khz); libopus as well as our encoder wrapper
support these sample rates. The OpusHead contains a field for
this original samplerate. Yet the pre-skip (and the granule-position
in the Ogg-Opus mapping in general) are always in the 48KHz clock,
irrespective of the original sample rate.

Before commit c3c22bee63, our libopus
encoder was buggy: It did not account for the fact that the pre-skip
field is always according to a 48kHz clock and wrote a too small
value in case one uses the encoder with a sample rate other than 48kHz;
this discrepancy between CodecDelay and OpusHead led to Firefox
rejecting such streams.

In order to account for that, said commit made the muxer always use
48kHz instead of the actual sample rate to convert the initial_padding
(in samples in the stream's sample rate) to ns. This meant that both
fields are now off by the same factor, so Firefox was happy.

Then commit f4bdeddc3c fixed the issue
in libopusenc; so the OpusHead is correct, but the CodecDelay is
still off*. This commit fixes this by effectively reverting
c3c22bee63.

*: Firefox seems to no longer abort when CodecDelay and OpusHead
are off.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-09-05 20:33:59 +02:00
Andreas Rheinhardt
7bacef580f avformat/matroskaenc: Only write DiscardPadding if nonzero
It is possible for the trailing padding to be zero, namely
e.g. if the AV_PKT_DATA_SKIP_SAMPLES side data is used
for leading padding. Matroska supports this (use a negative
DiscardPadding), but players do not; at least Firefox refuses
to play such a file. So for now only write DiscardPadding
if it is trailing padding and nonzero.
The fate-matroska-ogg-opus-remux was affected by this.

(I wish CodecDelay would not exist and DiscardPadding would
be used to instead trim the codec delay away (with the Block
timestamp corresponding to the time at which the actually
output audio is output).)

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-09-05 20:33:09 +02:00
Andreas Rheinhardt
2cce1d8528 avformat/matroskaenc: Fix use of uninitialized value
Regression since 67eea6cf02.
Affects only WebVTT when muxing WebM. (This is covered
by the webm-webvtt-remux FATE test which fails for several
FATE boxes on fate-ffmpeg.org.)

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-06-26 06:12:32 +02:00
Andreas Rheinhardt
cf30b538e5 avformat/matroskaenc: Reuse dynamic buffer
Avoids some allocations.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-06-24 18:33:18 +02:00
Andreas Rheinhardt
745966ab81 avformat/matroskaenc: Fix outdated comment
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-06-24 18:33:18 +02:00
Andreas Rheinhardt
e689a1171d avformat/matroskaenc: Improve handling of AV1 extradata
Up until now, only the first four bytes (the ones preceding
the OBU) were written because not enough space has been reserved
for the complete CodecPrivate. This commit changes this
by increasing the space reserved for the CodecPrivate (it is big
enough for every sane sequence header plus something extra);
the code falls back to writing four bytes in case the increased
space turns out to be insufficient.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-06-24 18:33:18 +02:00