summaryrefslogtreecommitdiffstats
path: root/demux/demux_mkv.c
Commit message (Collapse)AuthorAgeFilesLines
* demux_mkv: defer reading of seek index until first seekwm42013-05-231-3/+42
| | | | | | | | | | | | | | | | | | | | | | Playing Youtube videos often requires an additional seek to the end of the file. This flushes the stream cache. The reason for the seek is reading the cues (seek index). This poses the question why Google is muxing its files in such a way, since nothing in Matroska mandates that cues are located at the end of the file, but we want to handle this situation better anyway. The seek index is not needed for normal playback, only for seeking. This commit changes header parsing such that the index is not read on initialization in order to avoid the additional stream-level seek. Instead, read the index on the first demuxer-level seek, when the seek index is actually needed. If the cues are at the beginning of the file, they are read immediately as part of the normal header reading process. This commit changes behavior only if cues are outside of the header (i.e. not in the area between EBML header and clusters), and linked by a SeekHead. Other level 1 elements linked by the SeekHead might still cause seeks to the end of the file, although that seems to be rare.
* demux_mkv: use a single flag to indicate whether cues have been readwm42013-05-231-1/+3
| | | | | | | | | | | | Before this commit, the demuxer would in theory accept multiple cues elements (and append its contents to the index in the order as encountered during reading). According to the Matroska specification, there can be only one cues element in the segment, so this seems like an overcomplication. Change it so that redundant elements are ignored, like with all other unique header elements. This makes implementing deferred reading of the cues element easier.
* demux_mkv: support dirac in mkvwm42013-05-211-0/+1
| | | | | | | | | | | | | | Nobody uses this, and this is an absolute waste of time. Even the user who reported this turned out to have produced a sample manually. Sample produced with: wget http://diracvideo.org/download/test-streams/raw/vts/vts.LD-8Mb.drc mkvmerge -o dirac.mkv vts.LD-8Mb.drc mkvmerge writes a sort of broken aspect ratio. libavformat interprets it as 1:1 PAR, while demux_mkv thinks this is a 1:1 DAR. Maybe libavformat is more correct here.
* Silence some compiler warningswm42013-05-211-1/+1
| | | | None of these were actual issues.
* demux_mkv: export Matroska title element as metadatawm42013-05-151-0/+3
|
* Merge branch 'audio_changes'wm42013-05-121-3/+3
|\ | | | | | | | | Conflicts: audio/out/ao_lavc.c
| * core: use channel map on demuxer level toowm42013-05-121-3/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This helps passing the channel layout correctly from decoder to audio filter chain. (Because that part "reuses" the demuxer level codec parameters, which is very disgusting.) Note that ffmpeg stuff already passed the channel layout via mp_copy_lav_codec_headers(). So other than easier dealing with the demuxer/decoder parameters mess, there's no real advantage to doing this. Make the --channels option accept a channel map. Since simple numbers map to standard layouts with the given number of channels, this is downwards compatible. Likewise for demux_rawaudio.
* | Fix some cppcheck / scan-build warningswm42013-05-061-0/+2
| | | | | | | | | | | | | | | | These were found by the cppcheck and scan-build static analyzers. Most of these aren't interesting (the 2 previous commits fix some interesting cases found by these analyzers), and they don't nearly fix all warnings. (Most of the unfixed warnings are spam, things MPlayer never cared about, or false positives.)
* | demux_mkv: fix segfault issue playing back VC1 in a mkvBin Jin2013-04-271-1/+1
| | | | | | | | This bug was introduced in commit 06eee1b.
* | demux_mkv: cosmeticswm42013-04-241-4/+3
| |
* | demux_mkv: fix out of range comparisonwm42013-04-241-1/+1
| | | | | | | | | | | | | | | | | | | | | | This check was always false: if (num == EBML_UINT_INVALID) Fix it by using the proper type for the num variable. This case actually doesn't really matter, and this is just for hiding the warning and for being 100% correct.
* | demux_mkv: always set track->codec_id to a stringwm42013-04-201-1/+3
| | | | | | | | | | Otherwise audio/video/sub track handling code would dereference the NULL pointer.
* | demux_mkv: always add subtitle trackswm42013-04-201-26/+26
| | | | | | | | Even if the codec is unknown.
* | sub, demux: identify subtitle types with the codec namewm42013-04-201-16/+21
| | | | | | | | | | | | | | | | | | Get rid of the 1-char subtitle type field. Use sh_stream->codec instead just like audio and video do. Use codec names as defined by libavcodec for simplicity, even if they're somewhat verbose and annoying. Note that ffmpeg might switch to "ass" as codec name for ASS, so we don't bother with the current silly "ssa" name.
* | demux_mkv: introduce new_demux_packet_from() and use itwm42013-04-201-9/+6
| |
* | demux_mkv: simplify use of demuxer APIwm42013-04-201-106/+62
| | | | | | | | | | | | | | mkv_track_t now references sh_stream directly, instead of using an ID. Also remove all accesses to demux_stream (demuxer->video etc.). Remove some slave-mode things on the way, like "ID_SID_..." messages.
* | demux_mkv: code cleanupeng2013-04-201-20/+23
| | | | | | | | | | | | Cleanup based on results from cppcheck-1.59 Reduce the scope of several variables Replace 2 calloc/realloc calls with a single malloc
* | demux_mkv: use new way of track switchingwm42013-04-201-45/+0
| | | | | | | | | | | | Since demux_mkv queries the demuxer state when reading packets, track switching is completely passive. Cycling etc. is done by the frontend. As result, all track switching code can be removed.
* | demux_mkv: remove pointless video track selectionwm42013-04-201-37/+0
| | | | | | | | Possibly once needed, now it's just redundant code.
* | demux_mkv: support vp9wm42013-04-201-0/+1
| | | | | | | | Note that ffmpeg doesn't provide a decoder by default yet.
* | core: matroska: support concatenated segmentswm42013-04-201-10/+51
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Matroska files can contain multiple segments, which are literally further Matroska files appended to the main file. They can be referenced by segment linking. While this is an extraordinarily useless and dumb feature, we support it for the hell of it. This is implemented by adding a further demuxer parameter for skipping segments. When scanning for linked segments, each file is opened multiple times, until there are no further segments found. Each segment will have a separate demuxer instance (with a separate file handle etc.). It appears the Matroska spec. has an even worse feature for segments: live streaming can completely reconfigure the stream by starting a new segment. We won't add support for it, because there are 0 people on this earth who think Matroska life streaming is a good idea. (As opposed to serving Matroska/WebM files via HTTP.)
* | demux_mkv: don't terminate if there are no clusterswm42013-04-201-2/+3
| | | | | | | | | | | | Matroska segment linking allows abusing Matroska files as playlists without any actual video/audio/sub data, making files without any clusters still useful for the frontend.
* | demux_mkv: simplify handle_block() logic a bitwm42013-04-201-11/+8
| |
* | demux_mkv: verify laces separately, and in all caseswm42013-04-201-4/+10
| |
* | demux_mkv: get rid of the duplicated lace case labelswm42013-04-201-9/+4
| | | | | | | | | | Also change the extracting of the lace type bitfield from flags to make it more apparent that the value range is 0-3.
* | demux_mkv: there can be 256 laceswm42013-04-201-6/+5
| | | | | | | | | | The lace number is stored with an offset of 1, so the maximum number of laces is 255+1=256.
* | demux_mkv: check block malloc() resultwm42013-04-201-0/+2
| |
* | demux_mkv: use a bounded buffer for block datawm42013-04-201-108/+105
| | | | | | | | Should help avoiding out-of-bounds reads.
* | demux_mkv: static allocation for lace sizes bufferwm42013-04-201-12/+8
| | | | | | | | | | | | | | | | Avoid messy memory management and error handling. remove tmp_lace_buffer non-sense Not sure how my mind got 8k, or how this made sense at all.
* | demux_mkv: remove redundant checkwm42013-04-201-39/+37
| |
* | demux_mkv: fix seeking with index generationwm42013-04-201-45/+68
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Relative seeks backwards didn't work too well with incomplete files, or other files that are missing the seek index. The problem was that the on-the-fly seek index generation simply added cluster positions as seek entries. While this is perfectly fine, the seek code had no information about the location of video key frames. For example, a 5 second long cluster can have only 1 video key frame, which is located 4 seconds into the cluster. Seeking backwards by one second while still located in the same cluster would select this cluster as seek target again. Decoding would resume with the key frame, giving the impression that seeking is "stuck" at this frame. Make the generated index aware of key frame and track information, so that video can always be seeked in an idea way. This also uses the normal block parsing code for indexing the clusters, instead of the suspicious looking special code. (This code didn't parse the Matroska elements correctly, but was fine for files with normal structure. Files with corrupted clusters or clusters formatted for streaming were not handled properly.) Skipping is now quite a bit slower (takes about twice as long as before), but it removes the special cased skipping code, and it's still much faster (at least twice as fast) than libavformat. It needs to do more I/O (no more skipping entire clusters, all data is read), and has more CPU usage (more data needs to be parsed).
* | demux_mkv: move Block header parsing codewm42013-04-201-34/+43
| | | | | | | | | | | | Move parts of the Block element parsing to read_block(). This way read_block() can return block time and track information in struct block_info.
* | demux_mkv: split reading blocks and reading packetswm42013-04-201-37/+47
| | | | | | | | | | | | Move most code from demux_mkv_fill_buffer() to read_next_block(). The former is supposed to read raw blocks, while ..fill_buffer() reads blocks and turns them into packets.
* | demux_mkv: move BlockGroup reading code to a separate functionwm42013-04-201-49/+61
| | | | | | | | | | | | | | | | | | | | Somehow this was setup such that a BlockGroup can be incrementally read (at least in theory). This makes no sense, as BlockGroup can contain only one Block (despite its name). There's no need to read this incrementally, and makes the code confusing for no gain. Read all the BlockGroup sub-elements with a single function call, without keeping global state for BlockGroup parsing.
* | demux_mkv: factor block readingwm42013-04-201-55/+61
| | | | | | | | | | | | | | | | | | | | The code for reading block data was duplicated. Move it into a function. Instead of returning on error (possibly due to corrupt data) and signalling EOF, continue by trying to find the next block. This makes error handling slightly simpler too, because you don't have to care about freeing the current block. We could still signal EOF in this case, but trying to resync sounds better for dealing with corrupted files.
* | demux_mkv: fix streaming clusterswm42013-04-201-1/+9
| | | | | | | | | | | | Matroska files prepared for streaming have clusters with unknown size. These files are pretty rare, see e.g. test4.mkv from the official Matroska test file collection.
* | demux_mkv: simplify cluster reading codewm42013-04-201-32/+24
| | | | | | | | | | | | | | | | | | | | | | | | | | The end positions of the current cluster and block were managed by tracking their size and how much of them were read, instead of just using the absolute end positions. I'm not sure about the reasons why this code was originally written this way. One obvious concern is reading from pipes and such, but the stream layers hides this. stream_tell(s) works even when reading from pipes. It's also a fast call, and doesn't involve the stream implementation or syscalls. Keeping track of the cluster/block end is simpler and there's no reason why this wouldn't work.
* | demux_mkv: use normal index data structure even for incomplete fileswm42013-04-201-84/+63
|/ | | | | | | | | | | | Incomplete files don't have a valid index, because the index is usually located near the end of a file. In this case, an index is created on the fly during demuxing, or when seeks are done. This used a completely different code path, which leads to unnecessary complications and code duplication. Use the normal index data structure instead. The seeking code at the end of seek_creating_index() (in this commit renamed to create_index_until()) is removed. The normal seek code does the same thing instead.
* demux_mkv: move preroll subtitle check to the right placewm42013-04-041-2/+2
| | | | | | | | No subtitle selected was supposed to disable the preroll logic completely. However, the packet skipping logic was not properly enabled, so the demuxer would still return subtitle packets from before the seek target timecode. This shouldn't matter at all in practice, but fixing this makes the code clearer.
* demux_mkv: try to show current subtitle when seekingwm42013-04-041-9/+31
| | | | | | | | | | | | | | | | | Makes sure that seeking to a given time position shows the subtitle at that position. This can fail if the subtitle packet is not close enough to the seek target. Always enabled for hr-seeks, and can be manually enabled for normal seeks with --mkv-subtitle-preroll. This helps displaying subtitles correctly with ordered chapters. When switching ordered chapter segments, a seek is performed. If the subtitle is timed slightly before the start of the segment, it normally won't be demuxed. This is a problem with all seeks, but in this case normal playback is affected. Since switching segments always uses hr-seeks, the code added by this commit is always active in this situation. If no subtitles are selected or the subtitles come from an external file, the demuxer should behave exactly as before this commit.
* demux_mkv: fix handling of 0 DisplayWidth/Heightwm42013-04-041-2/+5
| | | | | | | | | Commit 546ae23 fixed aspect ratio if the DisplayWidth or DisplayHeight elements were missing. However, some bogus files [1] can have these elements present in the file, but set to 0. Use 1:1 pixel aspect for such files. [1] https://ffmpeg.org/trac/ffmpeg/ticket/2424
* demux_mkv: don't print non-sense warning on normal EOFwm42013-03-301-1/+1
| | | | | | | | | | | | | | | Commit ac1c5e6 (demux_mkv: improve robustness against broken files) added code to skip to the next cluster on error conditions. However, reaching normal EOF triggers this code as well, so explicitly check for EOF before this happens. Note that the EOF flag is only set _after_ reading the last byte, so EOF needs to be checked after the fact. (Or in other words, we must check for EOF after the ebml_read_id() call.) (To answer the question why reading packets actually reaches EOF, even if there's the seek index between the last packet and the end of the file: the cluster reading code skips the seeking related EBML elements as normal part of operation, so it hits EOF gracefully when trying to find the next cluster.)
* demux_mkv: improve robustness against broken fileswm42013-03-281-7/+16
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Fixes test7.mkv from the Matroska test file collection, as well as some real broken files I've found in the wild. (Unfortunately, true recovery requires resetting the decoders and playback state with a manual seek, but it's still better than just exiting.) If there are broken EBML elements, try harder to skip them correctly. Do this by searching for the next cluster element. The cluster element intentionally has a long ID, so it's a suitable element for resynchronizing (mkvmerge does something similar). We know that data is corrupt if the ID or length fields of an element are malformed. Additionally, if skipping an unknown element goes past the end of the file, we assume it's corrupt and undo the seek. Do this because it often happens that corrupt data is interpreted as correct EBML elements. Since these elements will have a ridiculous values in their length fields due to the large value range that is possible (0-2^56-2), they will go past the end of the file. So instead of skipping them (which would result in playback termination), try to find the next cluster instead. (We still skip unknown elements that are within the file, as this is needed for correct operation. Also, we first execute the seek, because we don't really know where the file ends. Doing it this way is better for unseekable streams too, because it will still work in the non-error case.) This is done as special case in the packet reading function only. On the other hand, that's the only part of the file that's read after initialization is done.
* demux_mkv: fix skipping broken header elementswm42013-03-281-1/+1
| | | | | | | | | | | | | | | | | | | | | | Fixes test4.mkv from the Matroska test file collection. demux_mkv_open() contains a loop that reads header elements. It starts by reading the EBML element ID with ebml_read_id(). If there is broken data in the header, ebml_read_id() might return EBML_ID_INVALID. However, that is not handled specially, and the code for handling unknown tags is invoked. This reads the EBML element length in order to skip data, which, if the EBML ID is broken, is entirely random. This caused a seek beyond the end of the file, making the demuxer fail. So don't skip any data if the EBML ID was invalid, and simply try to read the next element. ebml_read_id() reads at least one byte, so the parsing loop won't get stuck. All in all this is rather questionable, but since this affects error situations only, makes behavior a bit more robust (no random seeks), and actually fixes at least one sample, it's ok. libavformat's demuxer handled this.
* demux_mkv: set correct aspect ratio even if DisplayHeight is unsetwm42013-03-281-2/+4
| | | | | | | Fixes the file test2.mkv from the official Matroska test file collection. libavformat does the same thing.
* demux_mkv: support ALACwm42013-03-151-0/+11
| | | | | Test sample was produced with ffmpeg. Extradata handling closely follows libavformat/matroskadec.c.
* demux_mkv: Support playing Opus streams in MatroskaStephen Hutchinson2013-03-141-0/+5
| | | | | | | | | | | | FFmpeg recently changed how it writes Opus-in-Matroska to match the A_OPUS/EXPERIMENTAL name that mkvmerge uses, with the caveat that things will change and compatibility with old files can get worked out when the spec is finalized. This adds both A_OPUS and A_OPUS/EXPERIMENTAL so that *hopefully* it can play both the newer files that use A_OPUS/EXPERIMENTAL, and older ones muxed by FFmpeg that were simply A_OPUS, since this is also what FFmpeg seems to be doing to handle the situation.
* core: use playback time to determine playback percent positionwm42013-02-261-8/+0
| | | | | | | | | | | | | The percent position is used for the OSD, the status line, and for the OSD bar (shown on seeks). By default, the PTS of the last demuxed packet was used to calculate it. This led to a "jumpy" display when the percentage value (casted to int) was changing. The reasons for this were the presence of video frame reordering (packet PTS is not monotonic), or getting PTS values from different streams (like audio/subs). Since these rely on PTS values and correct file durations anyway, simplify it by calculating it with the current playback position in mplayer.c instead.