Commit Graph

696 Commits

Author SHA1 Message Date
Andreas Rheinhardt
98a6c6ec82 avformat/matroskaenc: Simplify writing Void elements
Reserving space in Matroska works by writing a Void element. And until
now this worked as follows: The current position was recorded and the
EBML ID as well as the length field written; then the new position was
recorded to know how much more to write. Afterwards the actual writing
has been performed via ffio_fill().

But it is unnecessary to explicitly use the positions (obtained via
avio_tell()) to find out how much still needs to be written, because the
length of the ID and the length field are known. So rewrite the function
to no longer use them.

Also, given that ffio_fill() uses an int parameter and given that no
current caller (and no sane future caller) will want to reserve several
GB of space, make the size parameter of put_ebml_void() itself an int.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-04-02 20:12:00 +02:00
Andreas Rheinhardt
8e4c871196 avformat/matroskaenc: Avoid seek when writing Cues at the front
When the Cues are written in front of the Cluster, the muxer would seek
to the beginning (to where the Cues ought to be written) and write the
Cues; afterwards it would seek back to the end of the file only to seek
to the beginning once again to update several elements there. This
commit removes the seek to the end.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-04-02 20:00:34 +02:00
Andreas Rheinhardt
b788343446 avformat/matroskaenc: Fix edge case of writing Cues at the beginning
The Matroska muxer has the ability to write the Cues (the index) at the
beginning of the file (in front of the Cluster): The user inputs the
amount of space that should be reserved at the beginning of the file and
if this is sufficient, the Cues will be written there and the part of the
reserved space not used up by the Cues will be filled with a "Void"
element.

There is just one problem with this: One can not fill a single byte this
way, because said Void element is minimally two bytes long (one byte ID,
one byte length field). Up until now, if one reserved one byte more than
needed, one would run into an assert when writing the Void element.

There are two solutions for this: Error out if it happens. Or adjust the
length field of the Cues in order to ensure that the above situation
can't happen (i.e. write the length on one byte more than necessary).
The first solution is very unsatisfactory, as enough space has been
reserved. Therefore this commit implements the second solution.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-04-02 19:50:05 +02:00
Andreas Rheinhardt
06f108907d avformat/matroskaenc: Don't fail if reserved Cues space doesn't suffice
When the user opted to write the Cues at the beginning, the Cues were
simply written without checking in advance whether enough space has been
reserved for them. If it wasn't enough, the data following the space
reserved for the Cues was simply overwritten, corrupting the file.

This commit changes this by checking whether enough space has been
reserved for the Cues before outputting anything. If it isn't enough,
no Cues will be output at all and the file will be finalized normally,
yet writing the trailer will nevertheless return an error to notify
the user that his wish of having Cues at the front of the file hasn't
been fulfilled.

This change opens new usecases for this option: It is now safe to use
this option to e.g. record live streams or to use it when muxing the
output of an expensive encoding, because when the reserved space turns
out to be insufficient, one ends up with a file that just lacks Cues
but is otherwise fine.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-04-02 19:22:51 +02:00
Andreas Rheinhardt
7039045c56 avformat/matroskaenc: Update the default version of WavPack
The Matroska muxer currently assumed WavPack version 4.03 in case it was
not explicitly signalled via extradata; but following a recommendation
from David Bryant, the WavPack creator, this is changed to 4.10.

Reviewed-by: David Bryant <david@wavpack.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-04-02 07:22:45 +02:00
Andreas Rheinhardt
5d24b6843c avformat/matroskaenc: Check that Cluster has been opened
before setting the field indicating that a Cluster has been opened.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-03-30 06:40:50 +02:00
Andreas Rheinhardt
907b7f88ca avformat/matroskaenc: Remove unused function parameter
end_ebml_master_crc32_preliminary() has a MatroskaMuxContext as
parameter that isn't used at all. So remove it.
Furthermore it doesn't close its dynamic buffer; it just uses the
underlying buffer and therefore it only needs a pointer to the
dynamic buffer, not a pointer to a pointer.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-03-30 06:33:45 +02:00
Andreas Rheinhardt
d9c21ec639 avformat/matroskaenc: Write level 1 elements in one go
Up until now, writing level 1 elements proceeded as follows: First, the
element id was written to the ordinary output AVIOContext and a dynamic
buffer was opened for the content of the level 1 element in
start_ebml_master_crc32(). Then this buffer was actually used and after it
was closed (in end_ebml_master_crc32()), the size field corresponding to
the buffer's size was written, after which the actual data was written.

This commit changes this: Nothing is written to the main AVIOContext any
more in start_ebml_master_crc32(). end_ebml_master_crc32() now writes
both the id, the length field as well as the data. This implies that
one can start a level 1 element in memory without outputting anything.
This is done to enable to test whether enough space has been reserved
for the Cues (if space has been reserved for them) before writing them.
A large duration between outputting the header and outputting the rest
could also break certain streaming usecases like the one from #8578
(which this commit fixes).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-03-30 06:26:34 +02:00
Andreas Rheinhardt
5e3b7bd56d avformat/matroskaenc: Simplify writing Cues
When the Matroska muxer writes the Cues (the index), it groups index
entries with the same timestamp into the same CuePoint to save space.
But given Matroska's variable-length length fields, it either needs
to have an upper bound of the final size of the CuePoint before writing it
or the CuePoint has to be assembled in a different buffer, so that after
having assembled the CuePoint (when the real size is known), the CuePoint's
header can be written and its data copied after it.

The first of these approaches is the currently used one. This entails
finding out the number of entries in a CuePoint before starting the
CuePoint and therefore means that the list is read at least twice.
Furthermore, a worst-case upper-bound for the length of a single entry
was used, so that sometimes bytes are wasted on length fields.

This commit switches to the second approach. This is no longer more
expensive than the current approach if one only resets the dynamic
buffer used to write the CuePoint's content instead of opening a new
buffer for every CuePoint: Writing the trailer of a file with 540.000
CuePoints improved actually from 219054414 decicycles to 2164379394
decicycles (based upon 50 iterations).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-03-30 06:18:31 +02:00
Andreas Rheinhardt
5cdd2ebb55 avformat/matroskaenc: Avoid allocation for Cues
Up until now, the Matroska muxer would allocate a structure containing
three members: The segment offset, a pointer to an array containing Cue
(index) entries and a counter for said array. It is unnecessary to
allocate it separately and it is unnecessary to contain the segment
offset in said structure, as it duplicates another field contained in
the MatroskaMuxContext. This commit implements the corresponding
changes.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-03-30 05:43:44 +02:00
Andreas Rheinhardt
6cf69f0e72 avformat/matroskaenc: Cosmetics
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-03-30 05:36:21 +02:00
Andreas Rheinhardt
b1c3d711df avformat/matroskaenc: Avoid unnecessary seek
When writing the SeekHead (a form of index) at the end of the muxing
process, mkv_write_seekhead() would first seek to the position where the
SeekHead ought to be written, then write it there and seek back to the
original position afterwards. Which means: To the end of the file.
Afterwards, a seek to the beginning of the file is performed to update
further values. This of course means that the second seek in
mkv_write_seekhead() was unnecessary.

This has been changed: A new parameter was added to mkv_write_seekhead()
containing the destination for the second seek, effectively eliminating
the seek to the end of the file after writing the SeekHead.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-03-30 05:35:34 +02:00
Andreas Rheinhardt
8c89fc18e6 avformat/matroskaenc: Check for failure when writing SeekHead
mkv_write_seekhead() would up until now try to seek to the position where
the SeekHead ought to be written, write the SeekHead and seek back. The
first of these seeks was checked as was writing, yet the seek back was
unchecked. Moreover the return value of mkv_write_seekhead() was unchecked
(the ordinary return value was the position where the SeekHead was written).

This commit changes this: Everything is checked. In the unseekable case
(where the first seek may nevertheless work when it happens in the buffer)
a failure at the first seek is not considered an error. In any case,
failure to seek back is an error.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-03-30 05:25:11 +02:00
Andreas Rheinhardt
e6ea75a38d avformat/matroskaenc: Improve calculating EBML ID size
When the Matroska muxer writes an EBML ID, it calculates the length of
said ID before; and it does this as if this were a number that needs to
be encoded as EBML number: The formula used is (av_log2(id + 1) - 1) / 7
+ 1. But the constants used already contain the VINT_MARKER (the leading
bit indicating the length of the EBML number) and therefore the algorithm
used makes no sense. Instead the position of the most significant byte
set gives the desired length.

The algorithm used until now worked because EBML numbers are subject to
restrictions: If the EBML number takes up k bytes, then the bit 1 << (7
* k) is set and av_log2(id) is 7 * k. So the current algorithm produces
the correct result unless the EBML ID is of the form 7 * k - 1 because
of the "id + 1". But contrary to encoding lengths as EBML number (where
the + 1 exists to avoid the encodings reserved for unknown length),
such EBML numbers are simply forbidden as EBML IDs and as such none of
them were ever written.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-03-30 04:23:06 +02:00
Andreas Rheinhardt
dc2f6b54ac avformat/matroskaenc: Avoid allocations for SeekHead
Up until e7ddafd5, the Matroska muxer wrote two SeekHeads: One at the
beginning referencing the main level 1 elements (i.e. not the Clusters)
and one at the end, referencing the Clusters. This second SeekHead was
useless and has therefore been removed. Yet the SeekHead-related
functions and structures are still geared towards this usecase: They
are built around an allocated array of variable size that gets
reallocated every time an element is added to it although the maximum
number of Seek entries is a small compile-time constant, so that one should
rather include the array in the SeekHead structure itself; and said
structure should be contained in the MatroskaMuxContext instead of being
allocated separately.

The earlier code reserved space for a SeekHead with 10 entries, although
we currently write at most 6. Reducing said number implied that every
Matroska/Webm file will be 84 bytes smaller and required to adapt
several FATE tests; furthermore, the reserved amount overestimated the
amount needed for for the SeekHead's length field and how many bytes
need to be reserved to write a EBML Void element, bringing the total
reduction to 89 bytes.

This also fixes a potential segfault: If !mkv->is_live and if the
AVIOContext is initially unseekable when writing the header, the
SeekHead is already written when writing the header and this used to
free the SeekHead-related structures that have been allocated. But if
the AVIOContext happens to be seekable when writing the trailer, it will
be attempted to write the SeekHead again which will lead to segfaults
because the corresponding structures have already been freed.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2020-03-26 03:19:56 +01:00
Andreas Rheinhardt
6e9cc96429 avformat/matroskaenc: Check BlockAdditional size before use
Don't read a 64bit number before having checked that the data is at
least 8 bytes long.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2020-03-14 22:07:27 +01:00
Andreas Rheinhardt
b4f300f8ea avformat/matroskaenc: Check functions that can fail
Sometimes it has not been checked whether opening the dynamic buffer for
writing Tags fails; this might have led to segfaults.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2020-02-05 17:41:59 +01:00
Andreas Rheinhardt
44be057380 avformat/matroskaenc: Remove useless AVIOContext
Write a few numbers directly via AV_WB32 instead of using an AVIOContext
(that is initialized only for this very purpose) to write these numbers
at known offsets into a fixed buffer.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2020-01-27 16:38:06 -03:00
Andreas Rheinhardt
9c8aa86883 avformat/matroskaenc: Improve writing Projection
The Matroska Projection master element has such a small maximum length
that it can always be written with a length field of length one.
So it is unnecessary to first write the element into a dynamic buffer to
get the accurate length in order not to waste bytes on the length field.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2020-01-27 16:37:59 -03: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
e4749a4497 avformat/matroskaenc: Cosmetics
Add {, } in situations like
if ()
    ...
else if ()
    /* Comment */
    ...
else ...

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
Andreas Rheinhardt
58428bef4b avformat/matroskaenc: Check for reformatting errors
This is needed especially for AV1: If a reformatting error happens (e.g.
if the length field of an OBU contained in the current packet indicates
that said OBU extends beyond the current packet), the data pointer is
still NULL, yet the size is unchanged, so that writing the data leads
to a segmentation fault.

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
Andreas Rheinhardt
6a1bf8634a avformat/matroskaenc: Fix ReferenceBlock timestamp
In order to indicate that the frames in a BlockGroup are not keyframes,
one has to add a ReferenceBlock element containing the timestamp of a
referenced Block that has already been written. The timestamp ought to be
relative to the timestamp of the Block it is attached to. Yet the
Matroska muxer used the relative timestamp of the preceding Block of the
track, i.e. the timestamp of the preceding block relative to the
timestamp of the Cluster containing said block (that need not be the
Cluster containing the current Block). This has been fixed.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Reviewed-by: Paul B Mahol <onemda@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2020-01-11 11:18:58 -03: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
Andreas Rheinhardt
34e3810b04 avformat/matroskaenc: Check return value of ff_isom_write_hvcc()
The Matroska muxer currently does not check the return value of
ff_isom_write_hvcc(), the function used to write mp4-style
HEVC-extradata as Matroska also uses it. This was intentionally done in
7a5356c72 to allow remuxing from mpeg-ts.

But if ff_isom_write_hvcc() fails, it has not output anything and the
file ends up without CodecPrivate and, if the input was Annex B, with
Annex B data, which is against the spec. So check the return value
again.

The underlying issue of not having extradata seems to have been fixed by
the introduction of the extract_extradata bitstream filter.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Reviewed-by: "mypopy@gmail.com" <mypopy@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2020-01-01 16:38:28 +01:00
Andreas Rheinhardt
2968da7d9e avformat/matroskaenc: Adapt documentation of put_ebml_num
to its actual behaviour: That it uses the least amount of bytes unless
overridden.

The current documentation leaves it undefined how many bytes will be used
when no number to use has been given explicitly. But several estimates
(used to write EBML Master elements with a small length field) require
this number to be the least amount of bytes to work. Therefore change
the documentation; and remove a comment about writing length fields
indicating "unkown length". It has been outdated since 0580a122.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2020-01-01 16:38:28 +01:00
Andreas Rheinhardt
82d61a9ce3 avformat: Don't free old extradata before ff_alloc/get_extradata
These functions already free it themselves before they allocate the new
extradata.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2019-12-12 19:25:33 +01:00
Andreas Rheinhardt
3a58ec7c77 avformat/matroskaenc: 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 (i.e. small master elements).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2019-11-28 15:20:37 -03:00
Andreas Rheinhardt
6eb88daed1 avformat/matroskaenc: Remove outdated comment
This comment does not account for the fact that the limits on cluster
size and duration are configurable by the user since 98308bd4.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2019-11-24 16:01:56 +01:00
Andreas Rheinhardt
4470ab1e0e avformat/matroskaenc: Fix potential leak of cached packet
If mkv_write_trailer() is not called, the cached audio packet might
leak; so unref it in mkv_deinit().

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2019-11-24 16:01:56 +01:00
Andreas Rheinhardt
74cafb0e6f avformat/matroskaenc: Cosmetics
Contains renaming of variables (e.g. mkv_write_cues() contained
variables called tracknum that actually contain the index of a track in
s->streams and not the track number (which can differ in case an
explicit dash track number is set)).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2019-10-21 21:05:14 -03:00
Andreas Rheinhardt
c486fe206e avformat/matroskaenc: Use more appropriate function name
mkv_start_new_cluster() actually didn't start a new cluster, but ended
the old one instead and emitted a debug message that it had started a
new cluster. This has been changed: The debug message has been moved to
the place that really starts a new cluster and the function has been
renamed to mkv_end_cluster().

Furthermore, without this debug message the function can be used for
flushing.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2019-10-21 21:05:14 -03:00
Andreas Rheinhardt
b4e36df74f avformat/matroskaenc: Remove redundant assert
The Matroska muxer groups index entries with the same pts together in
order to save a few bytes. Because of Matroska's variable-length length
fields, mkv_write_cues() does this by first finding out how many index
entries will be grouped together before actually writing them.
Currently, it is asserted at both of these stages that the stream index
of the list of designated index entries is valid. But the second assert
is redundant, because the very same index entries have already been
checked.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2019-10-21 21:05:14 -03:00
Andreas Rheinhardt
65ef74f749 avformat/matroskaenc: Move track-related fields to mkv_track
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2019-10-21 21:05:14 -03:00
Andreas Rheinhardt
7797fdfe6f avformat/matroskaenc: Use ffio_free_dyn_buf
instead of replicating its behaviour.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2019-10-21 20:35:54 -03:00
Andreas Rheinhardt
982a98a0e3 avformat/matroskaenc: Fix memleak upon failure
The Matroska muxer up until now leaked memory in two scenarios:

1. If an error happened during writing the trailer, as
mkv_write_trailer() returned early without cleaning up.
2. If mkv_write_header() indicated success despite an error in the
underlying AVIOContext. In this case avformat_write_header() returned
the IO error and according to the API the caller is not allowed to call
av_write_trailer(), so that no cleanup happened for the allocations made
in mkv_write_header().

This has been fixed by using a dedicated deinit function.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2019-10-21 20:35:54 -03:00
James Almer
5f758c31f9 avformat/matroskaenc: use r_frame_rate as fallback to set a track's DefaultDuration
Signed-off-by: James Almer <jamrial@gmail.com>
2019-10-06 13:44:52 -03:00
James Almer
35bbaa6652 avformat: use avpriv_mpeg4audio_get_config2()
Signed-off-by: James Almer <jamrial@gmail.com>
2019-09-27 10:37:11 -03:00
Andreas Rheinhardt
48539b62fc avformat/matroskaenc: Reduce usage of ebml_master
After the last few commits, the functions for writing master elements
with CRC-32 elements didn't really make use of the ebml_master
structure any more, so remove these parameters from the functions.

The only things that still need to be kept are the positions of the
level 1 elements that are written preliminarily and updated later.
These positions are stored in the MatroskaMuxContext and
replace the corresponding ebml_master structures.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2019-05-08 13:02:30 -03:00
Andreas Rheinhardt
9b932a855e avformat/matroskaenc: Improve log messages for blocks
Up until now, a block's relative offset has been reported as the offset
in the log messages output when writing blocks; given that it is
impossible to know the real offset from the beginning of the file at
this point due to the fact that it is not yet known how many bytes will
be used for the containing cluster's length field both the relative
offset in the cluster as well as the offset of the containing cluster
will be reported from now on.

Furthermore, the TrackNumber of the written block has been added to the
log output.

Also, the log message for writing vtt blocks has been brought in line
with the message for normal blocks.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2019-05-08 13:02:30 -03:00
Andreas Rheinhardt
0b61ddb576 avformat/matroskaenc: Don't waste bytes writing level 1 elements
Up until now, the length field of most level 1 elements has been written
using eight bytes, although it is known in advance how much space the
content of said elements will take up so that it would be possible to
determine the minimal amount of bytes for the length field. This
commit changes this.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2019-05-08 13:02:30 -03:00
Andreas Rheinhardt
08f5d97299 avformat/matroskaenc: Cosmetics and typo
Fixes intendation, whitespace, a typo and renames a variable
(dyn_bc->cluster_bc) to make its meaning clearer and to bring
it more in line with the naming of similar variables.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2019-05-08 13:02:30 -03:00
Andreas Rheinhardt
add68dcca9 avformat/matroskaenc: Write CRC-32 in non-seekable mode
Given that in both the seekable as well as the non-seekable mode dynamic
buffers are used to write level 1 elements and that now no seeks are
used in the seekable case any more, the two modes can be combined; as a
consequence, the non-seekable mode automatically inherits the ability to
write CRC-32 elements.

There are no differences in case the output is seekable; when it is not
and writing CRC-32 elements is disabled, there can still be minor
differences because before this commit, the EBML ID and length field
were counted towards the cluster size limit; now they no longer are.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2019-05-08 13:02:30 -03:00
Andreas Rheinhardt
e04a24e701 avformat/matroskaenc: Avoid seeking when writing level 1 elements
Up until now, the writing process for level 1 elements (those elements
for which CRC-32 elements are written by default) was this in case the
output was seekable: Write the EBML ID, write an "unkown length" EBML
number of the desired length, then write the element into a dynamic
buffer, then write the dynamic buffer (after possible calculation and
writing of the CRC-element), then seek back to the size element and
overwrite the unknown-size element with the real size. The seeking and
overwriting part has been eliminated by not writing the size initially.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2019-05-08 13:02:30 -03:00
Andreas Rheinhardt
8f53fd2dfd avformat/matroskaenc: Change variable types
A Matroska EBML ID can only be maximally four bytes long, so make the
variables denoting EBML IDs uint32_t instead of unsigned int to
better reflect this.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2019-05-08 13:02:30 -03:00
Andreas Rheinhardt
4b2c24da46 avformat/matroskaenc: Remove redundant check
All places where end_ebml_master_crc32_preliminary are used already
check for whether the output is seekable, so the check in the function
is redundant.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2019-05-08 13:02:29 -03:00
Andreas Rheinhardt
924424d867 avformat/matroskaenc: Improve log message
Since 4e3bdf729a there is no reason any
more to treat the seekable and non-seekable cases separate with regards
to the log message for a new cluster. This effectively reverts
d41aeea8a6.

Also improved the log message: "pts 80dts 0" -> "pts 80, dts 0".

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2019-05-08 13:02:29 -03:00
Andreas Rheinhardt
30d07c74db avformat/matroskaenc: Simplify check for writing CRCs
Up until now, the check for whether to write CRC32 elements was always
mkv->write_crc && mkv->mode != MODE_WEBM. This is equivalent to simply
set write_crc to zero in WebM-mode. And this is what this commit does.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2019-05-08 13:02:29 -03:00
Andreas Rheinhardt
01506e6b66 avformat/matroskaenc: Remove traces of secondary seek head
Up until e7ddafd515 the Matroska muxer
wrote a secondary seek head referencing all the clusters. When this
was changed, a (now completely wrong) comment remained and the unique
remaining seek head was still called main_seekhead. This has been
changed.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2019-05-08 13:02:29 -03:00
Andreas Rheinhardt
4e6df068b5 avformat/matroskaenc: Don't waste bytes in EBML Header
Up until now the EBML Header length field has been written with eight
bytes, although the EBML Header is always so small that only one byte
is needed for it. This patch saves seven bytes for every Matroska/Webm
file.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2019-05-08 13:02:29 -03:00
Andreas Rheinhardt
5d7d0fcd70 avformat/matroskaenc: Slightly improve size bounds for cues
The upper bounds currently used for determining the size of a CuePoint's
length field can be improved somewhat; as a result, a CuePoint
containing three CueTrackPositions will now only need a size field
with one byte length.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2019-05-08 13:02:28 -03:00
Andreas Rheinhardt
8b7964f8f8 avformat/matroskaenc: Fix BlockGroup size calculation
The earlier code included the size of the BlockGroup's length field and
the EBML ID in the calculation of the size for the payload and ignored
the size of the  duration's length field. This meant that Blockgroups
corresponding to packets with size 2^(7n) - 17 - n - i, i = 0,..., n - 1,
n = 1,..., 8 (i.e. 110, 16364, 16365, 2097130..2097132, ...) were written
with length fields that are unnecessarily long.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2019-05-08 13:02:28 -03:00
Andreas Rheinhardt
4ebeab15b0 avformat/matroskaenc: Fix relative timestamp check
At this point, ts already includes the ts_offset so that the relative
time written with the cluster is already given by ts - mkv->cluster_pts.
It is this number that needs to fit into an int16_t.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2019-05-08 13:02:28 -03:00
Tristan Matthews
1ec777dcdd avformat/matroskaenc: fix leak on error
Signed-off-by: James Almer <jamrial@gmail.com>
2019-04-09 10:49:05 -03:00
Jun Zhao
0a347ff422 lavf/matroskaenc: Fix memory leak after write trailer
Fix memory leak after write trailer for #7827, only store a audio
packet whose buffer has size greater than zero in cur_audio_pkt.

Audio packets with size zero, but with side-data currently lead to
memleaks, in the Matroska muxer, because they are not properly freed:

They are currently put into an AVPacket in the MatroskaMuxContext to
ensure that the necessary audio is always available for a new cluster,
but are only written and freed when their size is > 0.

As the only use we have for such packets consists in updating the
CodecPrivate it makes no sense to store these packets at all and this
is how this commit solves the memleak.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@googlemail.com>
Signed-off-by: Jun Zhao <barryjzhao@tencent.com>
2019-04-09 09:16:07 +08:00
Carl Eugen Hoyos
715186456c lavf/matroskaenc: Allow dvdsub remuxing from some containers.
Fixes ticket #7783.
2019-03-10 20:45:45 +01:00
Paul B Mahol
c0fb6f963f avformat/vorbiscomment: add support for writing chapters
Fixes #7532.
2018-12-18 19:45:59 +01:00
Sigga Regina
dcbd89e000 avformat/matroskaenc: reserve free space for metadata on request
Signed-off-by: James Almer <jamrial@gmail.com>
2018-10-01 14:52:35 -03:00
James Almer
819e4e7979 avformat/matroskaenc: reindent after the previous commit
Signed-off-by: James Almer <jamrial@gmail.com>
2018-09-26 15:17:14 -03:00
James Almer
8439656503 avformat/matroskaenc: refactor checks for allowed codecs in WebM
Use the new ff_webm_codec_tags[] array for this purpose.

Signed-off-by: James Almer <jamrial@gmail.com>
2018-09-26 15:17:14 -03:00
James Almer
794079e815 avformat/matroskaenc: implement AVOutputFormat.query_codec for webm
Signed-off-by: James Almer <jamrial@gmail.com>
2018-09-26 15:17:13 -03:00
James Almer
14ac62f9af avformat/matroskaenc: remove unnecessary additional codec tags
They are listed in riff.c already.

Signed-off-by: James Almer <jamrial@gmail.com>
2018-09-26 15:17:13 -03:00
James Almer
a068594248 Revert "avformat/matroskaenc: write CodecPrivate in WebM"
This reverts commit 4755b6e6d1.

This change was not necessary. CodecPrivate was being written to WebM
for codecs like AV1 already.
2018-09-21 18:22:37 -03:00
James Almer
4755b6e6d1 avformat/matroskaenc: write CodecPrivate in WebM
This is now needed for AV1.

Signed-off-by: James Almer <jamrial@gmail.com>
2018-09-21 16:57:32 -03:00
Kagami Hiiragi
cbe5c7ef38 lavf/matroska: Allow AV1 in WebM
Nothing prevents it to work except this check. AV1 is already supported
by Matroska muxer and aomenc produces WebM/AV1 files as well.

Signed-off-by: Kagami Hiiragi <kagami@genshiken.org>
Signed-off-by: James Almer <jamrial@gmail.com>
2018-09-21 12:20:48 -03:00
James Almer
de1b44c206 avformat/matroskaenc: handle AV1 extradata in packet side data
This is a temporary workaround for transcoding scenarious using libaom-av1
encoder, which currently can't propagate extradata during initialization.

Signed-off-by: James Almer <jamrial@gmail.com>
2018-08-18 23:32:44 -03:00
James Almer
2de5209d91 avformat/matroskaenc: filter and reorder AV1 OBUs
Make sure to not write forbidden OBUs to CodecPrivate, and do the same with
unnecessary OBUs for packets.

Signed-off-by: James Almer <jamrial@gmail.com>
2018-08-02 12:41:55 -03:00
wm4
022d4a2114 avformat/matroskaenc: do not write timebase as framerate
If the API user doesn't set avg_frame_rate, matroskaenc will write the
current timebase as "default duration" for the video track. This makes
no sense, because the "default duration" implies the framerate of the
video. Since the timebase is forced to 1/1000, this will make the
resulting file claim 1000fps.

Drop it and don't write the element. It's optional, so it's better not
to write it if the framerate is unknown.

Strangely does not require FATE changes.
2018-05-04 14:16:38 +02:00
Carl Eugen Hoyos
d401ba6b3d lavf/matroskaenc: Force the minimum value for -reserve_index_space to 2.
Fixes an assertion failure:
Assertion size >= 2 failed at libavformat/matroskaenc.c:298
2018-02-12 22:50:52 +01:00
James Almer
5f67073b4c avformat/matroskaenc: add missing allocation failure checks for stream durations
Signed-off-by: James Almer <jamrial@gmail.com>
2017-11-28 01:23:23 -03:00
James Almer
9924f1bc34 avformat/matroskaenc: move some initialization checks to mkv_init
It's the correct place for them.

Reviewed-by: Carl Eugen Hoyos <ceffmpeg@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2017-11-27 23:24:34 -03:00
James Almer
9d464dc3fc avformat/matroskaenc: actually enforce the stream limit
Prevents out of array accesses. Adressess ticket #6873

Reviewed-by: Michael Niedermayer <michael@niedermayer.cc>
Signed-off-by: James Almer <jamrial@gmail.com>
2017-11-27 13:39:55 -03:00
James Almer
fd59207c1c Merge commit '827a05eaa9482e9ac2a17f7f2e42ead07c1d7574'
* commit '827a05eaa9482e9ac2a17f7f2e42ead07c1d7574':
  matroskaenc: add support for Spherical Video elements

See 58eb0f57f6. Merged for cosmetics
purposes.
Also includes changes from d32d59bc97

Merged-by: James Almer <jamrial@gmail.com>
2017-10-21 13:19:36 -03:00
James Almer
45121cbdda Merge commit '5d3953a5dcfd5f71391b7f34908517eb6f7e5146'
* commit '5d3953a5dcfd5f71391b7f34908517eb6f7e5146':
  matroskaenc: factor ts_offset into block timecode computation

Merged-by: James Almer <jamrial@gmail.com>
2017-10-02 15:26:56 -03:00
James Almer
27b7800ba9 avformat/matroskaenc: also write tags when output is WebM
WebM supports a subset of elements from the Tags master.
See https://www.webmproject.org/docs/container/#tagging

Reviewed-by: Ivan Janatra <janatra@google.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2017-09-08 20:02:43 -03:00
Aleksandr Slobodeniuk
0aa8fa963f avformat/riff.h : remove unused function parameter "const AVCodecTag *tags" of "void ff_put_bmp_header()"
Reviewed-by: Derek Buitenhuis <derek.buitenhuis@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2017-08-02 15:26:25 +02:00
James Almer
2ba896fef7 avformat/matroskaenc: also write chapters when output is WebM
WebM supports a subset of elements from the Chapters master.
See https://www.webmproject.org/docs/container/#chapters

Addresses ticket #6425

Reviewed-by: James Zern <jzern@google.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2017-06-02 20:44:53 -03:00
James Almer
7631f14bb3 avformat/matroskaenc: check packet side data for AAC extradata updates
This adapts and merges commit f4bf236338
from libav, originally skipped in 13a211e632
as it was not necessary back then.

Is's applied now in preparation for the following patches, where the
aac_adtstoasc bitstream filter will start to correctly propagate the new
extradata through packet side data.

Signed-off-by: James Almer <jamrial@gmail.com>
2017-05-24 20:27:59 -03:00
James Almer
37cc1c1e91 avformat/matroskaenc: add support for writing Content Light Level elements
Signed-off-by: James Almer <jamrial@gmail.com>
2017-04-30 20:04:57 -03:00
James Almer
a8b5f37501 avformat/matroskaenc: don't try to update flac extradata if live streaming 2017-04-08 15:44:53 -03:00
James Almer
4de591e6fb Merge commit '83548fe894cdb455cc127f754d09905b6d23c173'
* commit '83548fe894cdb455cc127f754d09905b6d23c173':
  lavf: fix usage of AVIOContext.seekable

Merged-by: James Almer <jamrial@gmail.com>
2017-03-21 17:02:30 -03:00
James Almer
827a05eaa9 matroskaenc: add support for Spherical Video elements
Signed-off-by: James Almer <jamrial@gmail.com>
Minor cosmetic changes by committer.
Signed-off-by: Vittorio Giovara <vittorio.giovara@gmail.com>
2017-03-15 17:52:12 -04:00
James Almer
58eb0f57f6 avformat/matroskaenc: add support for Spherical Video elements
Reviewed-by: Vittorio Giovara <vittorio.giovara@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2017-03-09 13:57:03 -03:00
Carl Eugen Hoyos
1638d956a3 lavf/matroska: Support QDMC. 2017-03-05 14:41:01 +01:00
Carl Eugen Hoyos
9ae762da7e lavf/matroska: Support codec ID V_FFV1 for demuxing.
Fixes ticket #6206.
2017-03-03 02:04:34 +01:00
James Almer
1ad60e4e70 avformat/matroskaenc: don't write DisplayUnit with value Unknown on WebM files
Value 4 (Unknown) is for the time being part of the Matroska spec but not
supported by WebM

Addresses ticket #6176
2017-02-21 21:15:51 -03:00
John Stebbins
5d3953a5dc matroskaenc: factor ts_offset into block timecode computation
ts_offset was added to cluster timecode, but then effectively subtracted
back off the block timecode

When setting initial_padding for an audio stream, the timestamps are
written incorrectly to the mkv file.  cluster timecode gets written
as pts0 + ts_offset which is correct, but then block timecode gets
written as pts - cluster timecode which expanded is
pts - (pts0 + ts_offset).  Adding cluster and block tc back together:
cluster + block = (pts0 + ts_offset) + (pts - (pts0 + ts_offset)) = pts
But the result should be pts + ts_offset since demux will subtract the
CodecDelay element from pts and set initial_padding to CodecDelay.
This patch gives the correct result.
2017-02-21 14:20:31 -07:00
James Almer
dce863421b avformat/matroskaenc: don't reserve more bytes than needed for the Colour master size
Found-by: Aaron Colwell <acolwell@google.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2017-01-28 13:46:26 -03:00
Sasi Inguva
e4a1d87ef8 lavf/matroskaenc.c: Free dyn bufs in mkv_free. Fixes memory leaks when muxing fails.
Signed-off-by: Sasi Inguva <isasi@google.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2017-01-27 22:16:37 +01:00
Rostislav Pehlivanov
e7dec52d4d matroskaenc: remove unofficial compliance on color information
When support for this was added the details weren't yet finalized.
This is no longer the case.
Fixes writing of mkv/webm files with HDR.

Reported-by: Kagami Hiiragi <kagami@genshiken.org>
Signed-off-by: Rostislav Pehlivanov <atomnuker@gmail.com>
Reviewed-by: James Almer <jamrial@gmail.com>
2017-01-15 17:49:21 +00:00
Carl Eugen Hoyos
935404923d Cosmetics: Reindent after last commit. 2017-01-14 06:07:06 +01:00
Carl Eugen Hoyos
c723108e25 lavf/matroskaenc: Do not write two CodecID elements for rawvideo.
Fixes ticket #6068.
2017-01-14 06:06:05 +01:00
softworkz
20e8be0c20 avformat/matroskaenc: Regression fix for invalid MKV headers
The following three commits created a regression by writing initially
invalid mkv headers:

650e17d88b avformat/matroskaenc: write a
CRC32 element on Tags
3bcadf8227 avformat/matroskaenc: write a
CRC32 element on Info
ee888cfbe7 avformat/matroskaenc: postpone
writing the Tracks master

Symptoms:

- You can no longer playback a file that is still processed by ffmpeg,
e.g. VLC fails playback
- You can no longer stream a file to a client while if is still being
processed
- Various diagnosing tools show header errors or incomplete headers
(e.g. ffprobe, mediainfo, mkvalidator)

Note: The symptoms do not apply to completed files or ffmpeg runs that
were interrupted with 'q'

Cause:

The mentioned commits made changes in a way that some header elements
are only partially written in
mkv_write_header, leaving the header in an invalid state. Only in
mkv_write_trailer, these elements
are finished correctly, but that does only occur at the end of the
process.

Regression:

Before these commits were applied, mkv headers have always been valid,
even before completion of ffmpeg.
This has worked reliably over many versions of ffmpeg, to it was an
obvious regression.

Bugtracker:

This issue has been recorded as #5977 which is resolved by this patch

Patch:

The patch adds a new function 'end_ebml_master_crc32_preliminary' that
preliminarily finishes the ebml
element without destroying the buffer. The buffer can be used to update
the ebml element later during
mkv_write_trailer. But most important: mkv_write_header finishes with a
valid mkv header again.

Signed-off-by: James Almer <jamrial@gmail.com>
2017-01-07 11:20:36 -03:00
James Almer
8c1342e631 avformat/matroskaenc: write updated STREAMINFO metadata for FLAC streams if available
FLAC streams originating from the FLAC encoder send updated and more
complete STREAMINFO metadata as part of the last packet, so write that
to CodecPrivate instead of the incomplete one available in extradata
during init.

Signed-off-by: James Almer <jamrial@gmail.com>
2016-11-20 19:31:32 -03:00
James Almer
ee888cfbe7 avformat/matroskaenc: postpone writing the Tracks master
This will allow us to write updated stream information not available
during write_header().

Signed-off-by: James Almer <jamrial@gmail.com>
2016-11-20 19:31:32 -03:00
James Almer
98cae966c7 matroskaenc: write updated STREAMINFO metadata for FLAC streams if available
FLAC streams originating from the FLAC encoder send updated and more
complete STREAMINFO metadata as part of the last packet, so write that
to CodecPrivate instead of the incomplete one available in extradata
during init.

Signed-off-by: James Almer <jamrial@gmail.com>
Signed-off-by: Anton Khirnov <anton@khirnov.net>
2016-11-10 09:15:24 +01:00
James Almer
f4bf236338 matroskaenc: fix muxing AAC streams when using aac_adtstoasc bsf
aac_adtstoasc makes the aac extradata available only after the first packet
is filtered, and as packet side data.

Assume extradata will be available as part of the first packet if
avpriv_mpeg4audio_get_config() fails the first time due to missing extradata
and reserve space for the OutputSampleRate element in the Tracks master.

Signed-off-by: James Almer <jamrial@gmail.com>
Signed-off-by: Anton Khirnov <anton@khirnov.net>
2016-11-10 09:01:18 +01:00
James Almer
5cb57131d3 avformat/matroskaenc: use display aspect ratio for DisplayWidth and DisplayHeight when possible
This avoids potential rounding errors and guarantees the source aspect
ratio is preserved.
Keep writing pixel values when Stereo 3D Mode is enabled and for WebM,
as the format doesn't support anything else.

This fixes ticket #5743, implementing the suggestion from ticket #5903.

Signed-off-by: James Almer <jamrial@gmail.com>
2016-11-02 17:08:38 -03:00
James Almer
bab6b60675 avformat/matroskaenc: support writing Chroma Location elements
Signed-off-by: James Almer <jamrial@gmail.com>
2016-11-02 17:07:04 -03:00
Rodger Combs
1a958f4eb0
lavf/matroskaenc: don't try to modify the header when live-streaming 2016-11-02 01:16:26 -05:00
Rodger Combs
be28ce210d
lavf/matroskaenc: fix uninitialized read 2016-11-02 01:16:26 -05:00
Adriano Pallavicino
6089c44a2a Fix build warnings due to misleading indentation
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2016-11-01 17:44:10 +01:00
James Almer
eabbc64728 avformat/matroskaenc: fix cue relative position values when CRC32 is enabled
The dynamic buffer does not contain the CRC32 element so calls to avio_tell()
don't take it into account. This resulted in CueRelativePosition values being
six bytes short.
This is a regression since 6724525a15

Instead of adding yet another custom check for CRC32 to fix a size or an offset,
remove the existing ones and reserve the six bytes in the dynamic buffer.

Signed-off-by: James Almer <jamrial@gmail.com>
2016-10-26 16:28:59 -03:00
James Almer
cc71fa319f avformat/matroskaenc: write DisplayWidth and DisplayHeight elements only if they differ from PixelWidth and PixelHeight
Signed-off-by: James Almer <jamrial@gmail.com>
2016-10-22 15:00:15 -03:00
James Almer
90826031a2 avformat/matroskaenc: write a DisplayUnit element when aspect ratio is unknown
We don't currently support values 1 (centimeters), 2 (inches) or 3 (DAR),
only the default value 0 (pixels) which doesn't need to be written.

The fate refs are updated as unknown SAR is now signaled in the output
files with the addition of the new element.

Reviewed-by: Carl Eugen Hoyos <ceffmpeg@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2016-10-15 20:48:03 -03:00
James Almer
8063978bfc avformat/matroskaenc: don't write a FlagInterlaced element if it would write the default value
The spec says:
"Mandatory elements with a default value may be left out of the file. In the absence
of a mandatory element, the element's default value is used."

Reviewed-by: Hendrik Leppkes <h.leppkes@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2016-10-11 19:49:19 -03:00
Carl Eugen Hoyos
5781256c01 lavf/matroskaenc: Always write V_QUICKTIME extradata.
Fixes ticket #5872.
2016-10-11 10:07:18 +02:00
James Almer
c44eae157f avformat/matroskaenc: fix targets for attachment tags
Attachment tags were being written targeting non-existent streams in the
output file.
Also filter filename and mimetype entries, as they are standard elements
in the Attachment master.

Signed-off-by: James Almer <jamrial@gmail.com>
2016-10-10 23:18:36 -03:00
James Almer
c45ba265fc avformat/matroskaenc: fix Tags master on seekable output if there are tags after the last stream duration
The dynamic AVIOContext would get closed pointing to the wrong position
in the buffer.
This is a regression since 650e17d88b.

Reviewed-by: Dave Rice <dave@dericed.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2016-10-07 16:46:45 -03:00
Michael Niedermayer
572f16e100 avformat/matroskaenc: Fix () error
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2016-10-07 14:56:53 +02:00
James Almer
711bfb33df avformat/matroskaenc: add an option to disable writting CRC32 elements
Also add missing Changelog entry.

Signed-off-by: James Almer <jamrial@gmail.com>
2016-10-06 18:42:10 -03:00
James Almer
3bcadf8227 avformat/matroskaenc: write a CRC32 element on Info
Finishes implementing ticket #4347

Tested-by: Dave Rice <dave@dericed.com>
Tested-by: Jerome Martinez <jerome@mediaarea.net>
Reviewed-by: Michael Niedermayer <michael@niedermayer.cc>
Signed-off-by: James Almer <jamrial@gmail.com>
2016-10-06 17:16:12 -03:00
James Almer
650e17d88b avformat/matroskaenc: write a CRC32 element on Tags
Implements part of ticket #4347

Tested-by: Dave Rice <dave@dericed.com>
Tested-by: Jerome Martinez <jerome@mediaarea.net>
Reviewed-by: Michael Niedermayer <michael@niedermayer.cc>
Signed-off-by: James Almer <jamrial@gmail.com>
2016-10-06 17:13:59 -03:00
James Almer
4687240d52 avformat/matroskaenc: write a CRC32 element on Attachments
Implements part of ticket #4347

Tested-by: Dave Rice <dave@dericed.com>
Tested-by: Jerome Martinez <jerome@mediaarea.net>
Reviewed-by: Michael Niedermayer <michael@niedermayer.cc>
Signed-off-by: James Almer <jamrial@gmail.com>
2016-10-06 17:10:51 -03:00
James Almer
eccefece61 avformat/matroskaenc: write a CRC32 element on Chapters
Implements part of ticket #4347

Tested-by: Dave Rice <dave@dericed.com>
Tested-by: Jerome Martinez <jerome@mediaarea.net>
Reviewed-by: Michael Niedermayer <michael@niedermayer.cc>
Signed-off-by: James Almer <jamrial@gmail.com>
2016-10-06 17:08:14 -03:00
James Almer
87ce2595de avformat/matroskaenc: write a CRC32 element on Tracks
Implements part of ticket #4347

Tested-by: Dave Rice <dave@dericed.com>
Tested-by: Jerome Martinez <jerome@mediaarea.net>
Reviewed-by: Michael Niedermayer <michael@niedermayer.cc>
Signed-off-by: James Almer <jamrial@gmail.com>
2016-10-06 17:05:03 -03:00
James Almer
79248795d4 avformat/matroskaenc: write a CRC32 element on Cues
Implements part of ticket #4347

Tested-by: Dave Rice <dave@dericed.com>
Tested-by: Jerome Martinez <jerome@mediaarea.net>
Reviewed-by: Michael Niedermayer <michael@niedermayer.cc>
Signed-off-by: James Almer <jamrial@gmail.com>
2016-10-06 17:01:20 -03:00
James Almer
3b189fae73 avformat/matroskaenc: write a CRC32 element on SeekHead
Implements part of ticket #4347

Tested-by: Dave Rice <dave@dericed.com>
Tested-by: Jerome Martinez <jerome@mediaarea.net>
Reviewed-by: Michael Niedermayer <michael@niedermayer.cc>
Signed-off-by: James Almer <jamrial@gmail.com>
2016-10-06 16:59:09 -03:00
James Almer
6724525a15 avformat/matroskaenc: write a CRC32 element on each Cluster
Implements part of ticket #4347

Tested-by: Dave Rice <dave@dericed.com>
Tested-by: Jerome Martinez <jerome@mediaarea.net>
Reviewed-by: Michael Niedermayer <michael@niedermayer.cc>
Signed-off-by: James Almer <jamrial@gmail.com>
2016-10-06 16:54:07 -03:00
James Almer
4e3bdf729a avformat/matroskaenc: always use a dynamic buffer when writting clusters
Tested-by: Dave Rice <dave@dericed.com>
Tested-by: Jerome Martinez <jerome@mediaarea.net>
Reviewed-by: Michael Niedermayer <michael@niedermayer.cc>
Signed-off-by: James Almer <jamrial@gmail.com>
2016-10-06 16:30:56 -03:00
James Almer
d41aeea8a6 avformat/matroskaenc: print debug message with cluster offsets only if the output is seekable
Printing the dynamic buffer offset is useless.

Reviewed-by: Michael Niedermayer <michael@niedermayer.cc>
Signed-off-by: James Almer <jamrial@gmail.com>
2016-10-06 16:30:56 -03:00
James Almer
9b8ac526f6 avformat/matroskaenc: don't write an empty Colour master element
Signed-off-by: James Almer <jamrial@gmail.com>
2016-10-06 13:49:10 -03:00
James Almer
b33369b612 avformat/matroskaenc: don't reserve space for stream duration tags if the output is not seekable
The durations are never written in that situation.

Reviewed-by: Michael Niedermayer <michael@niedermayer.cc>
Signed-off-by: James Almer <jamrial@gmail.com>
2016-10-04 21:19:58 -03:00
James Almer
3cc9d6d382 avformat/matroska: write FlagInterlaced element in WebM
It's listed as supported in both https://www.webmproject.org/docs/container/
and https://matroska.org/technical/specs/index.html

Reviewed-by: Dave Rice <dave@dericed.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2016-10-04 19:42:55 -03:00
Anton Khirnov
83548fe894 lavf: fix usage of AVIOContext.seekable
It is supposed to be a flag. The only currently defined value is
AVIO_SEEKABLE_NORMAL, but other ones may be added in the future.
However all the current lavf code treats this field as a bool (mainly
for historical reasons).
Change all those cases to properly check for AVIO_SEEKABLE_NORMAL.
2016-09-30 16:54:33 +02:00
Rodger Combs
843e72ea55
lavf/matroskaenc: use mkv_check_tag_name consistently
Previously, we used a different list of checks when deciding whether to
write a set of tags at all than we did when deciding whether to write an
individual tag in the set. This resulted in sometimes writing an empty
tag master and seekhead. Now we use mkv_check_tag_name everywhere, so
if a dictionary is entirely composed of tags we skip, we don't write a
tag master at all.

This affected the test file, since "language" was on one list but not
the other, so we were writing an empty tag master there. The test hash
is updated to reflect that change.
2016-09-06 17:25:37 -05:00
Rodger Combs
3829a02738
lavf/matroskaenc: skip writing "duration" tags 2016-09-06 17:25:36 -05:00
Rodger Combs
6ede4e93ca
lavf/matroskaenc: move skipped metadata keys to separate function 2016-09-06 17:25:36 -05:00
Michael Bradshaw
c9ae8be5a8 avformat/matroskaenc: fix Voids with size < 10
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2016-07-31 16:59:49 +02:00
softworkz
70c1647a35 avformat/matroskaenc: Write duration early during mkv_write_header (Rev #3)
Rev #2: Fixes doubled header writing, checked FATE running without errors
Rev #3: Fixed coding style

This commit addresses the following scenario:

we are using ffmpeg to transcode or remux mkv (or something else) to mkv. The result is being streamed on-the-fly to an HTML5 client (streaming starts while ffmpeg is still running). The problem here is that the client is unable to detect the duration because the duration is only written to the mkv at the end of the transcoding/remoxing process. In matroskaenc.c, the duration is only written during mkv_write_trailer but not during mkv_write_header.

The approach:

FFMPEG is currently putting quite some effort to estimate the durations of source streams, but in many cases the source stream durations are still left at 0 and these durations are nowhere mapped to or used for output streams. As much as I would have liked to deduct or estimate output durations based on input stream durations - I realized that this is a hard task (as Nicolas already mentioned in a previous conversation). It would involve changes to the duration calculation/estimation/deduction for input streams and propagating these durations to output streams or the output context in a correct way.
So I looked for a simple and small solution with better chances to get accepted. In webmdashenc.c I found that a duration is written during write_header and this duration is taken from the streams' metadata, so I decided for a similar approach.

And here's what it does:

At first it is checking the duration of the AVFormatContext. In typical cases this value is not set, but: It is set in cases where the user has specified a recording_time or an end_time via the -t or -to parameters.
Then it is looking for a DURATION metadata field in the metadata of the output context (AVFormatContext::metadata). This would only exist in case the user has explicitly specified a metadata DURATION value from the command line.
Then it is iterating all streams looking for a "DURATION" metadata (this works unless the option "-map_metadata -1" has been specified) and determines the maximum value.
The precendence is as follows: 1. Use duration of AVFormatContext - 2. Use explicitly specified metadata duration value - 3. Use maximum (mapped) metadata duration over all streams.

To test this:

1. With explicit recording time:
ffmpeg -i file:"src.mkv" -loglevel debug -t 01:38:36.000 -y "dest.mkv"

2. Take duration from metadata specified via command line parameters:
ffmpeg -i file:"src.mkv" -loglevel debug -map_metadata -1 -metadata Duration="01:14:33.00" -y "dest.mkv"

3. Take duration from mapped input metadata:
ffmpeg -i file:"src.mkv" -loglevel debug -y "dest.mkv"

Regression risk:

Very low IMO because it only affects the header while ffmpeg is still running. When ffmpeg completes the process, the duration is rewritten to the header with the usual value (same like without this commit).

Signed-off-by: SoftWorkz <softworkz@hotmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2016-07-28 02:06:55 +02:00
James Almer
be04c4aa00 avformt/matroskaenc: undo an accidental revert by commit 5d48e4ea
Commit 5d48e4eafa accidentally reverted changes
made to matroskaenc by commit 989a614b70.

Signed-off-by: James Almer <jamrial@gmail.com>
2016-07-26 15:22:32 -03:00
James Almer
1582e306a4 avformat/avlanguage: make av_convert_lang_to() internal
The header was never installed and the function is only used in libavformat

Reviewed-by: Paul B Mahol <onemda@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2016-07-19 21:43:14 -03:00
Matthieu Bouron
dc62016c4b Merge commit '71852a1ba89abc8749e309d9d662c49d47e19531'
* commit '71852a1ba89abc8749e309d9d662c49d47e19531':
  matroskaenc: Provide output bytestream markers

Merged-by: Matthieu Bouron <matthieu.bouron@stupeflix.com>
2016-06-23 17:57:34 +02:00
Clément Bœsch
5d48e4eafa Merge commit 'a6a750c7ef240b72ce01e9653343a0ddf247d196'
* commit 'a6a750c7ef240b72ce01e9653343a0ddf247d196':
  tests: Move all test programs to a subdirectory

Merged-by: Clément Bœsch <clement@stupeflix.com>
2016-06-22 13:44:34 +02:00
Michael Bradshaw
989a614b70 libavformat/matroskaenc: omit segment UID for webm
SegmentUID is not a supported element in WebM. See:
http://www.webmproject.org/docs/container/#SegmentUID

Reviewed-by: Dave Rice <dave@dericed.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2016-06-22 12:12:46 +02:00
Michael Niedermayer
566be4f9e8 avformat/matroskaenc: reindent after last commit
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2016-05-22 03:22:16 +02:00
Michael Niedermayer
2062d51275 avformat/matroskaenc: wrap par->format use as sample format under codec_type == AVMEDIA_TYPE_AUDI
Fixes CID1361946

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2016-05-22 03:22:09 +02:00
Martin Storsjö
71852a1ba8 matroskaenc: Provide output bytestream markers
Signed-off-by: Martin Storsjö <martin@martin.st>
2016-05-18 10:36:58 +03:00
Derek Buitenhuis
ee865e9780 Merge commit 'e3453fd44480d903338c663238bf280215dd9a07'
* commit 'e3453fd44480d903338c663238bf280215dd9a07':
  matroska: Write the field order information

Merged-by: Derek Buitenhuis <derek.buitenhuis@gmail.com>
2016-05-08 23:25:58 +01:00
Michael Niedermayer
c4fad02779 avformat/matroskaenc: Undo bits_per_coded_sample change as bits_per_raw_sample is available again
Reminded-by: James Almer
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2016-04-16 02:01:10 +02:00
Derek Buitenhuis
6f69f7a8bf Merge commit '9200514ad8717c63f82101dc394f4378854325bf'
* commit '9200514ad8717c63f82101dc394f4378854325bf':
  lavf: replace AVStream.codec with AVStream.codecpar

This has been a HUGE effort from:
    - Derek Buitenhuis <derek.buitenhuis@gmail.com>
    - Hendrik Leppkes <h.leppkes@gmail.com>
    - wm4 <nfxjfg@googlemail.com>
    - Clément Bœsch <clement@stupeflix.com>
    - James Almer <jamrial@gmail.com>
    - Michael Niedermayer <michael@niedermayer.cc>
    - Rostislav Pehlivanov <atomnuker@gmail.com>

Merged-by: Derek Buitenhuis <derek.buitenhuis@gmail.com>
2016-04-10 20:59:55 +01:00
Luca Barbato
e3453fd444 matroska: Write the field order information
And bump the document version to 4.
2016-04-03 19:36:57 +02:00
Neil Birkbeck
e7e5c5e6c4 lavf/matroskaenc.c: add early support for colour elements
Adding early support for a subset of the proposed colour elements
according to the latest version of spec:
https://mailarchive.ietf.org/arch/search/?email_list=cellar&gbt=1&index=hIKLhMdgTMTEwUTeA4ct38h0tmE

Like matroskadec, I've left out elements for pix_fmt related things
 as there still seems to be some discussion around these.

The new elements are exposed under strict experimental mode.

Signed-off-by: Neil Birkbeck <neil.birkbeck@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2016-03-21 00:10:18 +01:00
Hendrik Leppkes
c43d485811 matroskaenc: set the actual PCM bitdepth in the header
The actual bitdepth can be different to the storage format (ie. sample format).
Fixes the stored bitdepth for 24-bit formats like FLAC.
2016-03-16 12:52:35 +01:00
Ronald S. Bultje
2e6636aa87 vp9: add superframe merging bitstream filter.
Fixes ticket 4313.
2016-03-11 11:19:14 -05:00
Anton Khirnov
9200514ad8 lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.

In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.

There are multiple important problems with this approach:
    - the fields in AVCodecContext are in general one of
        * stream parameters
        * codec options
        * codec state
      However, it's not clear which ones are which. It is consequently
      unclear which fields are a demuxer allowed to set or a muxer allowed to
      read. This leads to erratic behaviour depending on whether decoding or
      encoding is being performed or not (and whether it uses the AVStream
      embedded codec context).
    - various synchronization issues arising from the fact that the same
      context is used by several different APIs (muxers/demuxers,
      parsers, bitstream filters and encoders/decoders) simultaneously, with
      there being no clear rules for who can modify what and the different
      processes being typically delayed with respect to each other.
    - avformat_find_stream_info() making it necessary to support opening
      and closing a single codec context multiple times, thus
      complicating the semantics of freeing various allocated objects in the
      codec context.

Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2016-02-23 17:01:58 +01:00
Marton Balint
66e85a180a avformat/matroskaenc: use ff_parse_creation_time_metadata
Also increase precision to microsecs, and avoid writing creation_time as a
simple tag metadata item.

Signed-off-by: Marton Balint <cus@passwd.hu>
2016-02-14 01:51:14 +01:00
Carl Eugen Hoyos
c3c22bee63 lavf/matroskaenc: Assume 48kHz sample rate for Opus initial padding.
Analyzed by Timothy B. Terriberry in Mozilla bug 1227153.
Fixes ticket #5121.
2016-01-05 21:34:35 +01:00
Rodger Combs
b287d7ea17 lavf/matroskaenc: add automatic bitstream filtering 2015-12-28 08:40:37 -06:00