summaryrefslogtreecommitdiff
path: root/libavformat/matroskadec.c
Commit message (Collapse)AuthorAge
* avformat: Constify all muxer/demuxersAndreas Rheinhardt2021-04-27
| | | | | | | This is possible now that the next-API is gone. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> Signed-off-by: James Almer <jamrial@gmail.com>
* avcodec, avformat: Remove AVPacket.convergence_durationAndreas Rheinhardt2021-04-27
| | | | | | | Deprecated in 948f3c19a8bd069768ca411212aaf8c1ed96b10d. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com> Signed-off-by: James Almer <jamrial@gmail.com>
* avformat: Add and use helper function to add attachment streamsAndreas Rheinhardt2021-04-01
| | | | | | | | | All instances of adding attached pictures to a stream or adding a stream and an attached packet to said stream have several things in common like setting the index and flags of the packet, setting the stream disposition etc. This commit therefore factors this out. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avformat/matroskadec: Reuse AVFormatInternal.parse_pktAndreas Rheinhardt2021-03-24
| | | | | | | | | | | | | | | | | Before 8d78e90a6ba96646f7f25aff6ca3e12e71cec164 the Matroska demuxer used stack packets to hold temporary packets; now it uses a temporary packet allocated by the Matroska demuxer. Yet because it used stack packets the code has always properly reset the packet on error, while on success these temporary packets were put into a packet list via avpriv_packet_list_put(), which already resets the source packet. This means that this code is compatible with just reusing AVFormatInternal.parse_pkt (which is unused while one is in the demuxer's read_packet() function). Compared to before 8d78e90a6 this no longer wastes one initialization per AVPacket read (the resetting of the stack packet performed by av_packet_move_ref() in avpriv_packet_list_put() was for naught). Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: use av_packet_alloc() to allocate packetsJames Almer2021-03-17
| | | | Signed-off-by: James Almer <jamrial@gmail.com>
* avcodec/packet_internal: make avpriv_packet_list_* functions use an internal ↵James Almer2021-03-17
| | | | | | | | | struct The next pointer is kept at the end for backwards compatability until the major bump, when it should ideally be moved at the front. Signed-off-by: James Almer <jamrial@gmail.com>
* avformat/matroskadec: Check for EOF in resync loopMichael Niedermayer2021-03-15
| | | | | | | | | Fixes: Timeout (too long -> instantly) Fixes: 29136/clusterfuzz-testcase-minimized-ffmpeg_dem_WEBM_DASH_MANIFEST_fuzzer-4586141227548672 Reviewed-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com> Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
* avformat/matroskadec: Add support for FlagOriginalAndreas Rheinhardt2021-03-02
| | | | | | | | | | | | | | | | Needs a CountedElement in order to distinguish the case of the element not being present and the element being present with a value of zero. (It has been argued by Ridley Combs that one should only ever use the AV_DISPOSITION_DUB field for audio tracks. Yet given that there is no definition for the disposition flags, one can also interpret it to mean that e.g. a subtitle track is meant to be used with the dubbed audio track or the original audio track. This commit interprets this flag in this sense, which also allows to maintain it on remuxing.) Reviewed-by: Anton Khirnov <anton@khirnov.net> Reviewed-by: Ridley Combs <rcombs@rcombs.me> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Use av_strstart instead of strncmpAndreas Rheinhardt2021-02-28
| | | | | | | It makes the intent clearer and avoids calculating the length separately. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Add webm file extensionAndreas Rheinhardt2021-02-27
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Add support for FlagTextDescriptionsAndreas Rheinhardt2021-02-22
| | | | | | | | This is the equivalent of the WebM "D_WEBVTT/DESCRIPTIONS" and is therefore only exported for subtitles. Reviewed-by: Ridley Combs <rcombs@rcombs.me> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Add support for FlagHearing/VisualImpairedAndreas Rheinhardt2021-02-22
| | | | | | | | | | Given that our disposition flags provide no way to distinguish the cases of "track is unsuitable for hearing impaired users" and "it is unknown whether the track is suitable for hearing impaired users" we do not need to use a CountedElement for these flags. Reviewed-by: Ridley Combs <rcombs@rcombs.me> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Add support for FlagCommentaryAndreas Rheinhardt2021-02-22
| | | | | | | | | | Hint: Matroska actually provides a way to distinguish the cases of "track is no commentary track" and "it is unknown whether the track is a commentary track", but our disposition flags do not. Therefore we need not use a CountedElement. Reviewed-by: Ridley Combs <rcombs@rcombs.me> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Beautify setting default valuesAndreas Rheinhardt2021-02-22
| | | | | Reviewed-by: Ridley Combs <rcombs@rcombs.me> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Reindent after the previous commitAndreas Rheinhardt2021-02-22
| | | | | Reviewed-by: Ridley Combs <rcombs@rcombs.me> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Make reading zero-length elements spec-compliantAndreas Rheinhardt2021-02-22
| | | | | | | | | | | | For a very long time, the payload of integer and float elements had to have a length > 0. Our parser treated such invalid elements as having a value zero. But now it has been defined what an EBML element with length zero means: It is a shorthand for the default value. This has also been defined for strings (both ASCII and UTF-8). This commit modifies our parser to support this. Reviewed-by: Ridley Combs <rcombs@rcombs.me> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Don't use fake default value for ReferenceBlockAndreas Rheinhardt2021-02-22
| | | | | | | | | This has been done in order to find out whether this element is present at all; but this can now be done in a cleaner way by using a CountedElement for it. Reviewed-by: Ridley Combs <rcombs@rcombs.me> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Check min_luminance more thoroughlyAndreas Rheinhardt2021-02-22
| | | | | | | | | | | | | | | | | | | | In the absence of an explicitly coded minimal luminance, the current code inferred it to be -1, an invalid value. Yet it did not check the value lateron at all, so that if a valid maximum luminance is encountered, but no minimal luminance, an invalid minimal luminance of -1 is exported. If an minimal luminance element with a negative value is present, it is exported, too. This can be simply fixed by adding a check for the value of the element. Yet given that a minimal luminance of zero Cd/m² is legal and can be coded with a length of zero, we must not use a fake default value to find out whether the element is present or not. Therefore this patch uses an explicit counter for it. While just at it, also check for max_luminance > min_luminance. Reviewed-by: Ridley Combs <rcombs@rcombs.me> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Allow to count the number of element occurencesAndreas Rheinhardt2021-02-22
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Up until now, the generic EBML reader used by the Matroska demuxer did not have the capability to record whether an element was actually present or not; instead, in cases where it mattered one typically added an invalid default value and checked whether the value is valid (in which case it is guaranteed to be present). This worked pretty well so far, yet the EBML specifications have evolved: It is now legal to use zero-length elements for floats, ints, uints and strings (both ASCII and UTF-8); the value of these elements is the default value of the element (if it has one) or zero for scalar types and an empty string for strings. Furthermore, having a default value does no longer imply that the element may be presumed to be present (with its default value) if it is absent; this is only true if the element is mandatory, too. These rules are designed to allow size savings as follows: Consider the newly added FlagOriginal: It being zero means the track is not in its original language, it being one means it is. For backward compatibility reasons, neither of the two values may be inferred automatically in the absence of the element. But one can still save a byte when one wants to write the element with a value of zero, as one can write the integer with a length of zero: 0x55AE 80 instead of 0x55AE 81 00. In the former case, a parser has to infer the value of the element to be zero (which is the element's default value). When encountering an element with length zero, our parser always infers a value of zero (or an empty string); this is wrong for values with a different default value. It needs to infer the default value (or zero in its absence) and this precludes using an invalid default value for elements like FlagOriginal. Ergo one needs to be able to record whether an element is present or not by other means. This patch allows to use a simple counter for this. While just at it, some invalid and unnecessary default values have been removed (mastering metadata elements used default values of -1.0, despite these elements only being used if they are > 0). Reviewed-by: Ridley Combs <rcombs@rcombs.me> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Sanity check codec_id/track typeMichael Niedermayer2020-12-09
| | | | | | | | Fixes: memleak Fixes: 27766/clusterfuzz-testcase-minimized-ffmpeg_dem_MATROSKA_fuzzer-5198300814508032 Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
* avformat/matroskadec: only use the track duration if it existsSteve Lhomme2020-11-20
| | | | | | No need to multiplying/dividing when we know it's zero. Signed-off-by: Anton Khirnov <anton@khirnov.net>
* avformat/matroskadec: adjust the cluster time to the track timebaseSteve Lhomme2020-11-20
| | | | | | | | | | | | The Block timestamp read in matroska_parse_block() is in track timebase and is passed on as such to the AVPacket which uses this timebase. In the normal case the Cluster and Track timebases are the same because the track->time_scale is 1.0. But when it is not the case, the values in Cluster timebase need to be transformed in Track timebase so they can be added together. Signed-off-by: Anton Khirnov <anton@khirnov.net>
* avformat/matroskadec: add a warning when the track TimestampScale won't be usedSteve Lhomme2020-11-20
| | | | Signed-off-by: Anton Khirnov <anton@khirnov.net>
* lavf: move AVStream.*index_entries* to AVStreamInternalAnton Khirnov2020-10-28
| | | | | | | Those are private fields, no reason to have them exposed in a public header. Since there are some (semi-)public fields located after these, even though this section is supposed to be private, keep some dummy padding there until the next major bump to preserve ABI compatibility.
* lavf: move AVStream.{request_probe,skip_to_keyframe} to AVStreamInternalAnton Khirnov2020-10-28
| | | | | Those are private fields, no reason to have them exposed in a public header.
* avcodec/packet: move AVPacketList definition and function helpers over from ↵James Almer2020-09-15
| | | | | | | | | libavformat And replace the flags parameter with a function callback that can be used to copy the contents of the packet (e.g, av_packet_ref and av_packet_copy_props). Signed-off-by: James Almer <jamrial@gmail.com>
* avformat/matroskadec: Slightly simplify version checkAndreas Rheinhardt2020-07-24
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Avoid undefined pointer arithmeticAndreas Rheinhardt2020-07-24
| | | | | | | | | | | | | | | | The Matroska demuxer currently always opens a GetByteContext to read the content of the projection's private data buffer; it does this even if there is no private data buffer in which case opening the GetByteContext will lead to a NULL + 0 which is undefined behaviour. Furthermore, in this case the code relied both on the implicit checks of the bytestream2 API as well as on the fact that it returns zero if there is not enough data available. Both of these issues have been addressed by not using the bytestream API any more; instead the data is simply read directly by using AV_RB. This is possible because the offsets are constants. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Fix memleaks in WebM DASH manifest demuxerAndreas Rheinhardt2020-06-15
| | | | | | | In certain error scenarios, the underlying Matroska demuxer was not properly closed, causing leaks. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Use right number of tracksAndreas Rheinhardt2020-06-15
| | | | | | | | | | When demuxing a Matroska/WebM file, streams are added for tracks and for attachments, so that the array containing the former can be NULL even when the corresponding AVFormatContext has streams. So check for there to be tracks in the MatroskaDemuxContext instead of just streams in the AVFormatContext before dereferencing the pointer to the tracks. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Fix handling gigantic durationsAndreas Rheinhardt2020-06-15
| | | | | | | | | | matroska_parse_block currently asserts that the duration is not equal to AV_NOPTS_VALUE, but there is nothing that actually guarantees this. It is easy to create (spec-compliant) files which run into this assert; so replace it and instead cap the duration to INT64_MAX, as the duration field of an AVPacket is an int64_t. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Move AVBufferRef instead of copying, fix memleakAndreas Rheinhardt2020-06-15
| | | | | | | | | | | | | EBML binary elements are already made reference-counted when read; so when populating the AVStream.attached_pic, one does not need to allocate a new buffer for the data; instead the current code just creates a new reference to the underlying AVBuffer. But this can be improved even further: Just move the already existing reference. This also fixes a memleak that happens upon error because matroska_read_close has not been called in this scenario. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Beautify matroska_parse_laces()Andreas Rheinhardt2020-05-26
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Use proper context for loggingAndreas Rheinhardt2020-05-23
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Export FileDescription as title tagAndreas Rheinhardt2020-05-19
| | | | | | | | | | Each AttachedFile in Matroska can have a FileDescription element that contains a human-friendly name for the attached file; yet this element has been ignored up until now. This commit changes this and exports it as title tag instead (the Matroska muxer mapped the title tag to the AttachedFile element since support for Attachments was added). Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Allow multiple Tags elementsAndreas Rheinhardt2020-05-08
| | | | | | | | | | | The Matroska specification allows multiple (level 1) Tags elements per file, yet our demuxer didn't: While it parsed any amount of Tags elements it found in front of the Clusters (albeit with warnings because of duplicate elements), it would treat any Tags element only referenced via a SeekHead entry as already parsed if any Tags element has already been parsed; therefore this Tags element would not be parsed at all. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Improve handling of circular SeekHeadsAndreas Rheinhardt2020-05-08
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | There can be more than one SeekHead in a Matroska file, but most of the other level 1 elements can only occur once.* Therefore the Matroska demuxer only allows one entry per ID in its internal list of level 1 elements known to it; the only exception to this are SeekHeads. The only exception to this are SeekHeads: When one is encountered (either directly or in the list of entries read from SeekHeads), a new entry in the list of known level-1 elements is always added, even when this entry is actually already known. This leads to lots of seeks in case of circular SeekHeads: Each time a SeekHead is parsed, a new entry for a SeekHead will be added to the list of entries read from SeekHeads. The exception for SeekHeads mentioned above now implies that this SeekHead will always appear new and unparsed and parsing will be attempted. This continued until the list of known level-1 elements is full. Fixing this is pretty simple: Don't add a new entry for a SeekHead if its position matches the position of an already known SeekHead. *: Actually, there can be multiple Tags and several other level 1 elements are "identically recurring" which means they may be resent multiple times, but each instance must be absolutely identical to the previous. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Sanitize SeekHead entriesAndreas Rheinhardt2020-05-08
| | | | | | | | | | | | | | | | | | | | A Seek element in a Matroska SeekHead should contain a SeekID and a SeekPosition element and upon reading, they should be sanitized: Given that IDs are restricted to 32 bit, longer SeekIDs should be treated as invalid. Instead currently the lower 32 bits have been used. For SeekPosition, no checks were performed for the element to be present and if present, whether it was excessively large (i.e. the absolute file position described by it exceeding INT64_MAX). The SeekPosition element had a default value of -1 which means that a check seems to have been intended; but it was not implemented. This commit adds a check for overflow to the calculation of the absolute file position of the referenced level 1 elements. Using -1 (i.e. UINT64_MAX) as default value for SeekPosition implies that a Seek element without SeekPosition will run afoul of this check. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Free right buffer on errorAndreas Rheinhardt2020-05-04
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Since commit 979b5b89594c7628bd846c63198cb64ef9d81d16, reverting the Matroska ContentCompression is no longer done inside matroska_parse_frame() (the function that creates AVPackets out of the parsed data (unless we are dealing with certain codecs that need special handling)), but instead in matroska_parse_block(). As a consequence, the data that matroska_parse_frame() receives is no longer always owned by an AVBuffer; it is owned by an AVBuffer iff no ContentCompression needed to be reversed; otherwise the data is independently allocated and needs to be freed on error. Whether the data is owned by an AVBuffer or not is indicated by a variable buf of type AVBufferRef *: If it is NULL, the data is independently allocated, if not it is owned by the underlying AVBuffer (and is used to avoid copying the data when creating the AVPackets). Because the allocation of the buffer holding the uncompressed data happens outside of matroska_parse_frame() (if a ContentCompression needs to be reversed), the data is passed as uint8_t ** in order to not leave any dangling pointers behind in matroska_parse_block() should the data need to be freed: In case of errors, said uint8_t ** would be av_freep()'ed in case buf indicated the data to be independently allocated. Yet there is a problem with this: Some codecs (namely WavPack and ProRes) need special handling: Their packets are only stored in Matroska in a stripped form to save space and the demuxer reconstructs full packets. This involved allocating a new, enlarged buffer. And if an error happens when trying to wrap this new buffer into an AVBuffer, this buffer needs to be freed; yet instead the given uint8_t ** (holding the uncompressed, yet still stripped form of the data) would be freed (av_freep()'ed) which certainly leads to a memleak of the new buffer; even worse, in case the track does not use ContentCompression the given uint8_t ** must not be freed as the actual data is owned by an AVBuffer and the data given to matroska_parse_frame() is not the start of the actual allocated buffer at all. Both of these issues are fixed by always freeing the current data in case it is independently allocated. Furthermore, while it would be possible to track whether the pointer from matroska_parse_block() needs to be reset or not, there is no gain in doing so, as the pointer is not used at all afterwards and the sematics are clear: If the data passed to matroska_parse_frame() is independently allocated, then ownership of the data passes to matroska_parse_frame(). So don't pass the data via uint8_t **. Fixes Coverity ID 1462661 (the issue as described by Coverity is btw a false positive: It thinks that this error can be triggered by ProRes with a size of zero after reconstructing the original packets, but the reconstructed packets can't have a size of zero). Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: CosmeticsAndreas Rheinhardt2020-05-01
| | | | | | | | Reindentation as well as marking several variables used for demuxing RealAudio as const to clearly see that they don't change during demuxing. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Support ContentCompression for all codecsAndreas Rheinhardt2020-05-01
| | | | | | | | | | | | The Matroska demuxer has three functions for creating packets out of the data read: One for certain RealAudio codecs (ATRAC3, cook, sipr, RealAudio 28.8), one for WebVTT (actually, the WebM flavour of it) and one for all the others. Only the last function supported Matroska's ContentCompression (e.g. it reversed zlib compression or added the removed headers to the packets). But in Matroska, all tracks are allowed to be compressed. This commit adds support for this. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Cache whether a track needs to be decodedAndreas Rheinhardt2020-05-01
| | | | | | There is no need to recheck this for every frame. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Improve forward compabilityAndreas Rheinhardt2020-05-01
| | | | | | | | | | | | | | | | | | | | | | | | | Matroska is built around the principle that a reader does not need to understand everything in a file in order to be able to make use of it; it just needs to ignore the data it doesn't know about. Our demuxer typically follows this principle, but there is one important instance where it does not: A Block belonging to a TrackEntry with no associated stream is treated as invalid data (i.e. the demuxer will try to resync to the next level 1 element because it takes this as a sign that it has lost sync). Given that we do not create streams if we don't know or don't support the type of the TrackEntry, this impairs this demuxer's forward compability. Furthermore, ignoring Blocks belonging to a TrackEntry without corresponding stream can (in future commits) also be used to ignore TrackEntries with obviously bogus entries without affecting the other TrackEntries (by not creating a stream for said TrackEntry). Finally, given that matroska_find_track_by_num() already emits its own error message in case there is no TrackEntry with a given TrackNumber, the error message (with level AV_LOG_INFO) for this can be removed. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Don't discard valid packetsAndreas Rheinhardt2020-05-01
| | | | | | | | | | | | | | | | | | | | | | A Block (meaning both a Block in a BlockGroup as well as a SimpleBlock) must have at least three bytes after the field containing the encoded TrackNumber. So if there are <= 3 bytes, the Matroska demuxer would skip this block, believing it to be an empty, but valid Block. This might discard valid nonempty Blocks, namely if the track uses header stripping. And certain definitely spec-incompliant Blocks don't raise errors: Those with two or less bytes left after the encoded TrackNumber and those with three bytes left, but with flags indicating that the Block uses lacing as then there has to be further data describing the lacing. Furthermore, zero-sized packets were still possible because only the size of the last entry of a lace was checked. This commit fixes this. All spec-compliant Blocks that contain data (even if side data only) are now returned to the caller; spec-compliant Blocks that don't contain anything are not returned. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Simplify checks for cook and ATRAC3Andreas Rheinhardt2020-05-01
| | | | | | | | Some conditions which don't change and which can therefore be checked in read_header() were instead rechecked upon parsing each block. This has been changed. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Don't output uninitialized data for RealAudio 28.8Andreas Rheinhardt2020-05-01
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The Matroska demuxer splits every sequence of h Matroska Blocks into h * w / cfs packets of size cfs; here h (sub_packet_h), w (frame_size) and cfs (coded_framesize) are parameters from the track's CodecPrivate. It does this by splitting the Block's data in h/2 pieces of size cfs each and putting them into a buffer at offset m * 2 * w + n * cfs where m (range 0..(h/2 - 1)) indicates the index of the current piece in the current Block and n (range 0..(h - 1)) is the index of the current Block in the current sequence of Blocks. The data in this buffer is then used for the output packets. The problem is that there is currently no check to actually guarantee that no uninitialized data will be output. One instance where this is trivially so is if h == 1; another is if cfs * h is so small that the input pieces do not cover everything that is output. In order to preclude this, rmdec.c checks for h * cfs == 2 * w and h >= 2. The former requirement certainly makes much sense, as it means that for every given m the input pieces (corresponding to the h different values of n) form a nonoverlapping partition of the two adjacent frames of size w corresponding to m. But precluding h == 1 is not enough, other odd values can cause problems, too. That is because the assumption behind the code is that h frames of size w contain data to be output, although the real number is h/2 * 2. E.g. for h = 3, cfs = 2 and w = 3 the current code would output four (== h * w / cfs) packets. although only data for three (== h/2 * h) packets has been read. (Notice that if h * cfs == 2 * w, h being even is equivalent to cfs dividing w; the latter condition also seems very reasonable: It means that the subframes are a partition of the frames.) Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Fix buffer overflow when demuxing RealAudio 28.8Andreas Rheinhardt2020-05-01
| | | | | | | | | | | | | | | | | | | | | | | | RealAudio 28.8 (like other RealAudio codecs) uses a special demuxing mode in which the data of the existing Matroska Blocks is not simply forwarded as-is. Instead data from several Blocks is recombined together to output several packets. The parameters governing this process are parsed from the CodecPrivate: Coded framesize (cfs), frame size (w) and sub_packet_h (h). During demuxing, h/2 pieces of data of size cfs each are read from every Matroska (Simple)Block and put at offset m * 2 * w + n * cfs of a buffer of size h * w, where m ranges from 0 to h/2 - 1 for each Block while n is initially zero and incremented after a Block has been parsed until it is h, at which poin the assembled packets are output and n reset. The highest offset is given by (h/2 - 1) * 2 * w + (h - 1) * cfs + cfs while the destination buffer's size is given by h * w. For even h, this leads to a buffer overflow (and potential segfault) if h * cfs > 2 * w; for odd h, the condition is h * cfs > 3 * w. This commit adds a check to rule this out. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Fix demuxing RealAudio 28.8Andreas Rheinhardt2020-05-01
| | | | | | | | | | | | | | | | | RealAudio 28.8 does not need or use sub_packet_size for its demuxing and this field is therefore commonly set to zero. But since 18ca491b the Real Audio specific demuxing is no longer applied if sub_packet_size is zero because the codepath for cook and ATRAC3 divide by it; this made these files undecodable. Furthermore, since 569d18aa (merged in 2c8d876d) sub_packet_size being zero is used as an indicator for invalid data, so that a file containing such a track was completely skipped. This commit fixes this by not checking sub_packet_size for RealAudio 28.8 at all. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Simplify check for RealAudioAndreas Rheinhardt2020-05-01
| | | | | | | | | They need a special parsing mode and in order to find out whether this mode is in use, several checks have to be performed. They can all be combined into one: If the buffer that is only used to assemble their packets has been allocated, use the RealAudio parsing mode. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/matroskadec: Reject sipr flavor > 3Andreas Rheinhardt2020-05-01
| | | | | | | | | | | | | | Only flavors 0..3 seem to exist. E.g. rmdec.c treats any flavor > 3 as invalid data. Furthermore, we do not know how big the packets to create ought to be given that for sipr these values are not read from the bitstream, but from a table. Furthermore, flavor is only used for sipr, so only check it for sipr; rmdec.c does the same. (The old check for flavor being < 0 was always wrong given that flavor is an int that is read via avio_rb16(), so it has been removed completely.) Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>