Commit graph

295 commits

Author SHA1 Message Date
Zaggy1024
f4e835635f LibVideo/VP9: Move quantizer indices into FrameContext
This also renames (most?) of the related quantizer functions and
variables to make more sense. I haven't determined what AC/DC stands
for here, but it may be just an arbitrary naming scheme for the first
and subsequent coefficients used to quantize the residuals for a block.
2022-11-30 08:28:30 +01:00
Zaggy1024
0df5c1f32f LibVideo/VP9: Move loop filter parameters to FrameContext 2022-11-30 08:28:30 +01:00
Zaggy1024
90f16c78fa LibVideo/VP9: Move fields set in uncompressed_header() to FrameContext 2022-11-30 08:28:30 +01:00
Zaggy1024
40bc987fe3 LibVideo/VP9: Store color config in the frame context
The color config is reused for most inter predicted frames, so we use a
struct ColorConfig to store the config from intra frames, and put it in
a field in Parser to copy from when an inter frame without color config
is encountered.
2022-11-30 08:28:30 +01:00
Zaggy1024
9f573264ea LibVideo/VP9: Add a FIXME to keep render_and_frame_size_different
The flag should be used to determine whether the pixel aspect ratio
should be updated when frame/render sizes change in the bitstream.
2022-11-30 08:28:30 +01:00
Zaggy1024
3259c99cab LibVideo/VP9: Choose whether/how to show new frames using an enum
There are three mutually exclusive frame-showing states:
- Show no new frame, only store the frame as a reference.
- Show a newly decoded frame.
- Show frame from the reference frame store.
Since they are mutually exclusive, using an enum rather than two bools
makes more sense.
2022-11-30 08:28:30 +01:00
Zaggy1024
befcd479ae LibVideo/VP9: Add Frame, Tile and Block context structs
These are used to pass context needed for decoding, with mutability
scoped only to the sections that the function receiving the contexts
needs to modify. This allows lifetimes of data to be more explicit
rather than being stored in fields, as well as preventing tile threads
from modifying outside their allowed bounds.
2022-11-30 08:28:30 +01:00
Zaggy1024
448a8b8efb LibVideo/VP9: Create Vector2DView to limit writable ranges of contexts 2022-11-30 08:28:30 +01:00
Zaggy1024
9da432f4d6 LibVideo/VP9: Remove m_eob_total field from parser
The field was only used once to track whether residual tokens were
present in the block. Parser::tokens() now returns a bool indicating
whether they were present.
2022-11-30 08:28:30 +01:00
Zaggy1024
10d207959d LibVideo/VP9: Remove m_mi_row and col fields from the parser
These are now passed as parameters to each function that uses them.
These will later be moved to a struct to further reduce the amount of
parameters that get passed around.

Above and left per-frame block contexts are now also parameters passed
to the functions that use them instead of being retrieved when needed
from a field. This will allow them to be more easily moved to a tile-
specific context later.
2022-11-30 08:28:30 +01:00
Zaggy1024
4a4aa697d9 LibVideo/VP9: Use a struct for block context to keep between frames
There are three fields that we need to store from FrameBlockContext to
keep between frames, which are used to parse for those same fields for
the next frame.
2022-11-30 08:28:30 +01:00
Zaggy1024
5275a1101e LibVideo/VP9: Remove dump_frame_info() function from Decoder
The function serves no purpose now, any debug information we want to
pull from the decoder should be instead accessed by some other yet to
be created interface.
2022-11-30 08:28:30 +01:00
Zaggy1024
0638c5d2b8 LibVideo/VP9: Use a class to store 2D context information 2022-11-30 08:28:30 +01:00
Zaggy1024
44413c31a9 LibVideo/VP9: Store data used between decode_block calls in a struct
All state that needed to persist between calls to decode_block was
previously stored in plain Vector fields. This moves them into a struct
which sets a more explicit lifetime on that data. It may be possible to
store this data on the stack of a function with the appropriate
lifetime now that it is split into its own struct.
2022-11-30 08:28:30 +01:00
Zaggy1024
9b6ab1d4e5 LibVideo/VP9: Change fields within decode_partition() to variables 2022-11-30 08:28:30 +01:00
Zaggy1024
e379223633 LibVideo/VP9: Don't store the default_intra_mode in a field
The default intra prediction mode was only used to set the sub-block
modes and the y prediction mode. Instead of storing it in a field, with
the sub modes are stored in an Array, we can just pull the last element
to set the y mode.
2022-11-30 08:28:30 +01:00
Zaggy1024
713b48cfe2 LibVideo/VP9: Remove unused parser field m_is_compound 2022-11-30 08:28:30 +01:00
Zaggy1024
eafc048101 LibVideo/VP9: Remove a FIXME that is impossible to fix
We can't memset an array with 32-bit integers to non-zero values,
silly past me :^)
2022-11-30 08:28:30 +01:00
Zaggy1024
5f7099cff6 LibVideo/VP9: Apply higher optimization levels to Decoder and Parser
With this change, decode times on GCC as measured by TestVP9Decode are
reduced by about 15%. Not a bad improvement for a few added lines :^)
2022-11-30 07:55:29 +01:00
Zaggy1024
3c68a6684e LibVideo: Don't crash when a decoder error is encountered while seeking
When errors are encountered by PlaybackManager, it attempts to switch
states to either Stopped or Corrupted. However, that causes it to set
the last presentation media time to the current playback time while the
last presentation time is unexpectedly negative because the seek never
ended.

Ending the seek before the state changes to Stopped or Corrupted
prevents this situation from happening.
2022-11-27 10:45:52 +01:00
Zaggy1024
f6830eaf73 LibVideo: Implement Matroska Cues for faster keyframe lookup
This implements the fastest seeking mode available for tracks with cues
using an array of cue points for each track. It approximates the index
based on the seeking timestamp and then finds the earliest cue point
before the timestamp. The approximation assumes that cues will be on
a regular interval, which I don't believe is always the case, but it
should at least be faster than iterating the whole set of cue points
each time.

Cues are stored per track, but most videos will only have cue points
for the video track(s) that are present. For now, this assumes that it
should only seek based on the cue points for the selected track. To
seek audio in a video file, we should copy the seeked iterator over to
the audio track's iterator after seeking is complete. The iterator will
then skip to the next audio block.
2022-11-25 23:28:39 +01:00
Zaggy1024
2a9fb8b439 LibVideo: Give Matroska duration an absolute value getter
Previously, the duration had to be multiplied by timestamp_scale and
converted to a Time object, now SegmentInformation::duration() does it
itself.
2022-11-25 23:28:39 +01:00
Zaggy1024
a58bf7c3d1 LibVideo: Calculate Block timestamps for Matroska according to spec
Tracks have a timestamp scale value that should be present which scales
each block's timestamp offset to allow video to be synced with audio.
They should also contain a CodecDelay element and may also contain a
TrackOffset that offsets the block timestamps.
2022-11-25 23:28:39 +01:00
Zaggy1024
fd3ffd88ce LibVideo: Add a fast seeking mode to seek only to keyframes
Now that we're able to find the nearest keyframe, we can have a fast
seeking mode that only seeks to keyframes, so that it doesn't have to
also decode inter frames until it reaches the timestamp.

The default is still accurate seeking, so that the entire seeking
implementation can be tested.
2022-11-25 23:28:39 +01:00
Zaggy1024
eef8867d9e LibVideo: Implement Matroska keyframe search for when there are no Cues
This just searches sequentially through each block in a SampleIterator
until it finds a block after the specified seek timestamp. Once it
finds one, it will try to set the input/output iterator to the most
recent keyframe. If the iterator's original position is closer to the
target, however, it leaves it at that original position, allowing
callers to continue decoding from that position until they reach the
target timestamp.
2022-11-25 23:28:39 +01:00
Zaggy1024
9040194d54 LibVideo: Make Matroska Block and Cluster timestamps absolute 2022-11-25 23:28:39 +01:00
Zaggy1024
5c2cede2c9 LibVideo: Implement accurate seeking to inter frames in PlaybackManager
This implements the PlaybackManager portion of seeking, so that it can
seek to any frame in the video by dropping all preceding frames until
it reaches the seek point.

MatroskaDemuxer currently will only seek to the start of the video.
That means that every seek has to drop all the frames until it comes
across the target timestamp.
2022-11-25 23:28:39 +01:00
Zaggy1024
9a9fe08449 LibVideo: Rewrite the video frame present function to be more readable
The PlaybackManager::update_presented_frame function was getting out of
hand and adding seeking was making it illegible. This rewrites it to be
(hopefully) quite a bit more readable, and adds a few comments to help
future readers of the code.

In addition, some helpful debugging prints were added that should help
debug any future issues with the player.
2022-11-25 23:28:39 +01:00
Zaggy1024
f31621b3f2 VideoPlayer/LibVideo: Implement the UI functionality for seeking
With these changes, the seek bar can be used, but only to seek to the
start of the file. Seeking to anywhere else in the file will cause an
error in the demuxer.

The timestamp label that was previously invisible now has its text set
according to either the playback or seek slider's position.
2022-11-25 23:28:39 +01:00
Zaggy1024
e216d1a65f LibVideo: Only print non-fatal playback errors when debug mode is on 2022-11-25 23:28:39 +01:00
Zaggy1024
393cfdd5c5 LibVideo: Read Matroska lazily so that large files can start quickly
The Demuxer class was changed to return errors for more functions so
that all of the underlying reading can be done lazily. Other than that,
the demuxer interface is unchanged, and only the underlying reader was
modified.

The MatroskaDocument class is no more, and MatroskaReader's getter
functions replace it. Every MatroskaReader getter beyond the Segment
element's position is parsed lazily from the file as needed. This means
that all getter functions can return DecoderErrors which must be
handled by callers.
2022-11-25 23:28:39 +01:00
Zaggy1024
f4c476b26f LibVideo: Make Matroska element parsing functions static
Making these functions static makes it easier to implement lazy-loading
since the parsing functions can now be called at any time.

The functions were reorganized because they were not defined in the
order they are called. However, instead of moving every function to
that order, I've declared some but defined them further into the file,
which allows the next commit's diff to be more readable.
2022-11-25 23:28:39 +01:00
Zaggy1024
be9de58932 LibVideo: Parse Matroska from ReadonlyBytes and keep the start position
Keeping the entire Matroska stream available is a prerequisite to being
able to stream from it and seek to cue points.
2022-11-25 23:28:39 +01:00
Zaggy1024
2dfd236dcd LibVideo: Propagate decoder errors in the Matroska Reader
Matroska::Reader functions now return DecoderErrorOr instead of values
being declared Optional. Useful errors can be handled by the users of
the parser, similarly to the VP9 decoder. A lot of the error checking
in the reader is a lot cleaner thanks to this change, since all reads
can be range checked in Streamer::read_octet() now.

Most functions for the Streamer class are now also out-of-line in
Reader.cpp now instead of residing in the header.
2022-11-25 23:28:39 +01:00
Zaggy1024
9cf7e8c5aa LibVideo: Reorganize demuxer file hierarchy and rename Matroska files
As new demuxers are added, this will get quite full of files, so it'll
be good to have a separate folder for these.

To avoid too many chained namespaces, the Containers subdirectory is
not also a namespace, but the Matroska folder is for the sake of
separating the multiple classes for parsed information entering the
Video namespace.
2022-11-25 23:28:39 +01:00
Zaggy1024
edec6bdc32 LibVideo: Make all PlaybackManager Timer::start calls set an interval
Timers keep their previously set interval even for single-shot mode.
We want all timers to fire immediately if they don't have a delay set
in the start() call.
2022-11-25 23:28:39 +01:00
Zaggy1024
7514e49c17 LibVideo: Make all VP9 block intermediates stack-allocated arrays
This has two benefits:
- I observed a ~34% decrease in decoding time running TestVP9Decode.
- Removing all of these silly Vector fields helps simplify the code
  relationships between all the functions in Decoder.cpp. It'll also be
  much easier to make these static with template specializations, if
  that turns out to be worthy performance improvement.
2022-11-25 02:44:18 +03:30
Zaggy1024
87aed17a46 VideoPlayer: Make PlaybackManager use OwnPtr
VideoPlayerWidget was keeping a reference to PlaybackManager when
changing files, so the old and new managers would both send frames to
be presented at the same time, causing it to flicker back and forth
between the two videos. However, PlaybackManager no longer relies on
event bubbling to pass events to its parent. By changing it to send
events directly to an Object, it can avoid being ref counted, so that
it will get destroyed with its containing object and stop sending
events.
2022-11-14 10:05:56 +00:00
Zaggy1024
9dc622475e LibVideo: Rename parse_tree_new to parse_tree in VP9/TreeParser.cpp
It is now the only function used to parse the binary trees in the VP9
decoder.
2022-11-12 10:17:27 -07:00
Zaggy1024
907816e629 LibVideo: Create TokensContext struct for token parsing parameters
With the addition of this struct, both the bool to determine if coefs
should be parsed and the token parse itself can take specific
parameters.

This is the last step in parameterizing all the tree parsing, so the
old functions in TreeParser are now unused. This patch is very
satisfying :^)

There's still more work to be done to clean up how the parameters are
passed from Parser, but that's work for another day.
2022-11-12 10:17:27 -07:00
Zaggy1024
e906bcc696 LibVideo: Parameterize all tree parsing for motion vectors in VP9 2022-11-12 10:17:27 -07:00
Zaggy1024
84f1aed40a LibVideo: Parameterize parsing single reference frame selection in VP9 2022-11-12 10:17:27 -07:00
Zaggy1024
37fab851f1 LibVideo: Parameterize parsing compound references in the VP9 decoder 2022-11-12 10:17:27 -07:00
Zaggy1024
dd18c42643 LibVideo: Parameterize compound mode parsing in the VP9 decoder 2022-11-12 10:17:27 -07:00
Zaggy1024
372a4ea8c1 LibVideo: Parameterize parsing if a block is inter predicted in VP9 2022-11-12 10:17:27 -07:00
Zaggy1024
93caa1e19d LibVideo: Parameterize TXSize parsing for the VP9 decoder 2022-11-12 10:17:27 -07:00
Zaggy1024
cc735a7001 LibVideo: Parameterize coefficient skip parsing for VP9 2022-11-12 10:17:27 -07:00
Zaggy1024
10ba956066 LibVideo: Parameterize segment ID parsing for the VP9 decoder 2022-11-12 10:17:27 -07:00
Zaggy1024
568e2fc1f2 LibVideo: Parameterize interpolation filter parsing for the VP9 decoder 2022-11-12 10:17:27 -07:00
Zaggy1024
cbb8a3f0f4 LibVideo: Parameterize inter mode parsing in the VP9 decoder 2022-11-12 10:17:27 -07:00
Zaggy1024
540ef22b95 LibVideo: Parameterize UV mode parsing in the VP9 decoder 2022-11-12 10:17:27 -07:00
Zaggy1024
858915ab3a LibVideo: Parameterize intra prediction mode parsing for VP9 2022-11-12 10:17:27 -07:00
Zaggy1024
1b66aa3cad LibVideo: Parameterize parsing the default UV prediction mode for VP9 2022-11-12 10:17:27 -07:00
Zaggy1024
c6ecad63d0 LibVideo: Parameterize parsing of the default VP9 intra mode tree 2022-11-12 10:17:27 -07:00
Zaggy1024
fce7639c90 LibVideo: Put motion vector or reference frame pairs in a struct
Since these two types are often passed around as a pair, it's easier to
handle them with a simple pair struct, at least for now. Once things
are fully being passed around as parameters wherever possible, it may
be good to change this type for something more generalized.
2022-11-12 10:17:27 -07:00
Zaggy1024
6192a33e79 LibVideo: Move parsing of Partition to its own TreeParser function
This adds a tree-parsing function that can be called statically from
specific trees' implementations in TreeParser, of which Partition is
the first. This way, all calls to tree parses will take the context
they need to be able to select a tree and probabilities, which will
allow removal of the state dependence in TreeParser on fields from
itself and Parser.
2022-11-12 10:17:27 -07:00
Zaggy1024
69e6feaf32 LibVideo: Add const getters to VP9/ProbabilityTables.h 2022-11-12 10:17:27 -07:00
Zaggy1024
17e1b205a4 LibVideo: Use the BlockSubsize enum where appropriate in the VP9 parser 2022-11-12 10:17:27 -07:00
Zaggy1024
981997c039 LibVideo: Combine VP9's Intra- and InterMode enums into PredictionMode
The two different mode sets are stored in single fields, and the
underlying values didn't overlap, so there was no reason to keep them
separate.

The enum is now an enum class as well, to enforce that almost all uses
of the enum are named. The only case where underlying values are used
is in lookup tables, but it may be worth abstracting that as well to
make array bounds more clear.
2022-11-12 10:17:27 -07:00
Zaggy1024
1c6d0a9777 LibVideo: Use Gfx::Size for VP9 frame sizes
Frame sizes will now be represented by Gfx::Size instead of storing
width and height separately.
2022-11-12 10:17:27 -07:00
Zaggy1024
40b0bb0914 LibVideo: Change all Span<u8 const> to ReadonlyBytes 2022-11-12 10:17:27 -07:00
Zaggy1024
72ed286e16 LibVideo: Allow the VP9 decoder to queue multiple frames
Frames will now be queued for retrieval by the user of the decoder.
When the end of the current queue is reached, a DecoderError of
category NeedsMoreInput will be emitted, allowing the caller to react
by displaying what was previously retrieved for sending more samples.
2022-11-12 10:17:27 -07:00
Zaggy1024
993385f18d LibVideo: Rename VP9's ReferenceFrame enum to ReferenceFrameType 2022-11-12 10:17:27 -07:00
Zaggy1024
6b392cef9c LibVideo: Treat BT.601/709/2020 input transfer characteristics as sRGB
I've realized that it probably makes more sense to change the input
transfer characteristics to treat these as sRGB since color conversion
in linear converted from BT.709 doesn't really make sense. If content
creation applications expect media players to display BT.709 without
conversions, this means they expect applications to treat it as sRGB,
since that's what most displays use. That most likely also means they
process it as sRGB internally, meaning we should do the same for our
color primaries conversion.
2022-11-11 11:34:03 +01:00
Zaggy1024
6f28c8deb0 LibVideo: Remove control codes from DecoderError location information 2022-11-10 12:32:55 +03:30
Zaggy1024
18a6a1dd10 LibVideo: Handle corrupted video errors without spamming dialogs
No longer will the video player explode with error dialogs that then
lock the user out of closing them.

To avoid issues where the playback state becomes invalid when an error
occurs, I've made all decoder errors pass through the frame queue.
This way, when a video is corrupted, there should be no chance that the
playback state becomes invalid due to setting the state to Corrupted
in the event handler while a presentation event is still pending.
Or at least I think that was what caused some issues I was seeing :^)

This system should be a lot more robust if any future errors need to be
handled.
2022-11-10 12:32:55 +03:30
Nico Weber
6911c5545c Everywhere: Fix a few comment typos 2022-11-09 16:00:32 +00:00
Tim Schumacher
ce2f1b845f Everywhere: Mark dependencies of most targets as PRIVATE
Otherwise, we end up propagating those dependencies into targets that
link against that library, which creates unnecessary link-time
dependencies.

Also included are changes to readd now missing dependencies to tools
that actually need them.
2022-11-01 14:49:09 +00:00
Zaggy1024
353e1c2b4d LibVideo: Add PlaybackManager to load and decode videos
This file will be the basis for abstracting away the out-of-thread or
later out-of-process decoding from applications displaying videos. For
now, the demuxer is hardcoded to be MatroskaParser, since that is all
we support so far. The demuxer should later be selected based on the
file header.

The playback and decoding are currently all done on one thread using
timers. The design of the code is such that adding threading should
be trivial, at least based on an earlier version of the code. For now,
though, it's better that this runs in one thread, as the multithreaded
approach causes the Video Player to lock up permanently after a few
frames are decoded.
2022-10-31 14:47:13 +01:00
Zaggy1024
0a4def1208 LibVideo: Abstract media container format demuxing
This creates an abstract Demuxer class to allow multiple container
container formats to be easily used by video playback systems.
2022-10-31 14:47:13 +01:00
Zaggy1024
3a2f6c700d LibVideo: Parse the duration of Matroska files 2022-10-31 14:47:13 +01:00
Zaggy1024
2b4b6c5613 LibVideo: Make VP9::Decoder a subclass of a new abstract VideoDecoder
This will allow other decoders to be used in place of VP9::Decoder when
new video decoders are implemented, such as AV1.
2022-10-31 14:47:13 +01:00
Zaggy1024
3720f66bb1 LibVideo: Set CodingIndependentCodePoints in its member functions
This moves the setting of code points in CICP structs to member
functions completely so that the code having to set these code points
can be much cleaner.
2022-10-31 14:47:13 +01:00
Zaggy1024
074f771b59 LibVideo: Add VideoFrame class for decoded video frames
The class is virtual and has one subclass, SubsampledYUVFrame, which
is used by the VP9 decoder to return a single frame. The
output_to_bitmap(Bitmap&) function can be used to set pixels on an
existing bitmap of the correct size to the RGB values that
should be displayed. The to_bitmap() function will allocate a new bitmap
and fill it using output_to_bitmap.

This new class also implements bilinear scaling of the subsampled U and
V planes so that subsampled videos' colors will appear smoother.
2022-10-31 14:47:13 +01:00
Zaggy1024
91fbfe1773 LibVideo: Make DecoderError getters const 2022-10-31 00:02:52 +01:00
Zaggy1024
b87398341b LibVideo: Add CICP parsing to MatroskaReader
This will allow correct independent code points to be selected from VP9
in WebM, since the VP9 bitstream does not specify them independently.
2022-10-25 11:06:11 +02:00
Zaggy1024
cd127b65c3 LibVideo: Implement CICP color space conversion
This adds a struct called CodingIndependentCodePoints and related enums
that are used by video codecs to define its color space that frames
must be converted from when displaying a video.

Pre-multiplied matrices and lookup tables are stored to avoid most of
the floating point division and exponentiation in the conversion.
2022-10-25 11:06:11 +02:00
Andrew Kaster
290d3449e0 LibVideo: Hide debug message behind MATROSKA_DEBUG
This clutters fuzzer output if enabled.
2022-10-13 11:25:03 +02:00
Andrew Kaster
bf014c4d20 LibVideo: Check parsed superframe sizes when decoding VP9 frames
Make sure that the next parsed superframe size will not overflow the
chunk data before splitting it out to decode a frame.
2022-10-13 11:25:03 +02:00
Andrew Kaster
9d3074f72f LibVideo: Always check byte length before reading first byte in Streamer
The check was missing at the front of
MatroskaReader::Streamer::read_variable_size_integer, causing assertions
on malformed input streams.
2022-10-13 11:25:03 +02:00
Zaggy1024
41cb705b47 LibVideo: Allow the VP9 decoder to decode ultra high resolution video
Previously, some integer overflows and truncations were causing parsing
errors for 4K videos, with those fixed it can fully decode 8K video.

This adds a test to ensure that 4K video will continue to be decoded.

Note: There seems to be unexpectedly high memory usage while decoding
them, causing 8K video to require more than a gigabyte of RAM. (!!!)
2022-10-12 00:54:31 -06:00
Zaggy1024
0c07bed89c LibVideo: Initialize VP9 BitStream's reservoir field
Leaving it uninitialized could lead to undefined behavior.
2022-10-10 11:04:39 +01:00
Zaggy1024
63ba01cad2 LibVideo: Remove unnecessary dbgln calls
Debug prints are expensive, so doing them every frame seems excessive
now that the decoder is completely functional on some test videos.
2022-10-09 20:32:40 -06:00
Zaggy1024
7dcd5ed206 LibVideo: Make probability tables save to the specified index
Previously, saved probability tables were being inserted, causing the
Vector to increase in size when it should say fixed at a size of 4. This
changes the Vector to an Array<T, 4> which will default-initalize and
allow assigning to any index without previously setting size.
2022-10-09 20:32:40 -06:00
Zaggy1024
7d27273dc7 LibVideo: Ensure that syntax element counts don't overflow
Integer overflow could sometimes occur due to counts going above 255,
where the values should instead be clamped at their maximum to avoid
wrapping to 0.
2022-10-09 20:32:40 -06:00
Zaggy1024
7c87a8e302 LibVideo: Prevent decode_block from saving motion vectors out of bounds
This fixes an issue causing frame 3 of the test video to fail to parse
because a reference vector was incorrectly within the range for a high
precision delta vector read.
2022-10-09 20:32:40 -06:00
Zaggy1024
be0760871e LibVideo: Add support for VP9 superframes
This allows the second shown frame of the VP9 test video to be decoded,
as the second chunk uses a superframe to encode a reference frame and
a second to inter predict between the keyframe and the reference frame.
2022-10-09 20:32:40 -06:00
Zaggy1024
b0187dfc27 LibVideo: Implement inter prediction
This enables the second frame of the test video to be decoded.

It appears that the test video uses a superframe (group of multiple
frames) for the first chunk of the file, but we haven't implemented
superframe parsing.

We also ignore the show_frame flag, so for now, this
means that the second frame read out is shown when it should not be. To
fix this, another error type needs to be implemented that is "thrown" to
decoder's client so they know to send another sample buffer.
2022-10-09 20:32:40 -06:00
Zaggy1024
50d4217dbc LibVideo: Look up interpolation filter probability correctly
The above interpolation filter mode was being taken from the left side
instead, causing some parsing errors.

This also changes the magic number 3 to SWITCHABLE_FILTERS.
Unfortunately, the spec uses the magic number, so this value was taken
instead from the reference codec, libvpx.
2022-10-09 20:32:40 -06:00
Zaggy1024
17107303f0 LibVideo: Fix incorrect VP9 InterMode enum values
These values were referencing the wrong column of a table in the spec,
the values should start from 10.
2022-10-09 20:32:40 -06:00
Zaggy1024
03738aa006 LibVideo: Implement block parsing for inter frames
This gets the decoder closer to fully parsing the second frame without
any errors. It will still be unable to output an inter-predicted frame.
The lack of output causes VideoPlayer to crash if it attempts to read
the buffers for frame 1, so it is still limited to the first frame.
2022-10-09 20:32:40 -06:00
Zaggy1024
6c648329c4 LibVideo: Add MotionVector lookup tables as constant expressions
This changes MotionVector by removing the cpp file and moving all
functions to the header, where they are now declared as constexpr
so that they can be compile-time evaluated in LookupTables.h.
2022-10-09 20:32:40 -06:00
Zaggy1024
1dc4652683 LibVideo: Rename MV to MotionVector for clarity 2022-10-09 20:32:40 -06:00
Zaggy1024
85fd56cf48 VideoPlayer: Display frames from the VP9 decoder
For testing purposes, the output buffer is taken directly from the
decoder and displayed in an image widget.

The first keyframe can be displayed, but the second will not decode
so VideoPlayer will stop at frame 0 for now.

This implements a BT.709 YCbCr to RGB conversion in VideoPlayer, but
that should be moved to a library for handling color space conversion.
2022-10-09 20:32:40 -06:00
Zaggy1024
1514004cd5 LibVideo: Implement VP9 intra-predicted frame decoding
The first keyframe of the test video can be decoded with these changes.

Raw memory allocations in the Parser have been replaced with Vector or
Array to avoid memory leaks and OOBs.
2022-10-09 20:32:40 -06:00
Zaggy1024
da9ff31166 LibVideo: Make new DecoderError class to report useful errors
This allows runtime strings, so we can format the errors to make them
more helpful. Errors in the VP9 decoder will now print out a function,
filename and line number for where a read or bitstream requirement
has failed.

The DecoderErrorCategory enum will classify the errors so library users
can show general user-friendly error messages, while providing the
debug information separately.

Any non-DecoderErrorOr<> results can be wrapped by DECODER_TRY to
return from decoder functions. This will also add the extra information
mentioned above to the error message.
2022-10-09 20:32:40 -06:00
Zaggy1024
72efd9a5ff LibVideo: Change decode_term_subexp read to the correct number of bits
This allows parsing of the implemented functions from the VP9 spec in
the test video included in /home/anon/Videos.
2022-10-09 20:32:40 -06:00
Zaggy1024
647472b716 LibVideo: Read multiple raw bits at once to refill the range decoder
This will allow BitStream::read_bool() to read more than one bit from
the range-coded bitstream at a time if needed.
2022-10-09 20:32:40 -06:00
Zaggy1024
13ccde8637 LibVideo: Improve error reporting for VP9 range decoder
init_bool will now check whether there is enough data in the bitstream
for the range coding size to be fully read.

exit_bool will now read the entire padding element regardless of size,
which the spec does not specify a limit on.
2022-10-09 20:32:40 -06:00
Zaggy1024
7f46033c01 LibVideo: Cache 64 bits at a time for reading in BitStream
Reads will now be done in larger chunks at a time.

The public read_byte() function was removed in favor of a private
fill_reservoir() function which will be used to fill the 64-bit
reservoir field which will then be bit-shifted and masked as necessary
for subsequent arbitrary bit-sized reads.

read_f(n) was renamed to read_bits to be clearer about its use.
2022-10-09 20:32:40 -06:00
Zaggy1024
b37ea6b414 LibVideo: Allow bit stream reads to throw errors
Errors are propagated to the user of the decoder so that they can be
aware of specific places where a read failed.
2022-10-09 20:32:40 -06:00
Zaggy1024
af0584ea53 LibVideo: Remove MV class's copy assignment overload
This was unnecessary, as the implicit one works correctly.
2022-10-09 20:32:40 -06:00
Zaggy1024
3ffbe20067 LibVideo: Remove printing of the interpolation filter in VP9 dump_info
The interpolation filter value is not set when reading an intra-only
frame, so printing this for the first keyframe of the file was printing
"220", which is invalid.
2022-10-09 20:32:40 -06:00
Zaggy1024
caee37ef9c LibVideo: Remove headers from CMakeLists.txt 2022-10-09 20:32:40 -06:00
sin-ack
3f3f45580a Everywhere: Add sv suffix to strings relying on StringView(char const*)
Each of these strings would previously rely on StringView's char const*
constructor overload, which would call __builtin_strlen on the string.
Since we now have operator ""sv, we can replace these with much simpler
versions. This opens the door to being able to remove
StringView(char const*).

No functional changes.
2022-07-12 23:11:35 +02:00
Idan Horowitz
086969277e Everywhere: Run clang-format 2022-04-01 21:24:45 +01:00
Hendiadyoin1
fbb798f98c AK: Move integral log2 and exp to IntegerMath.h 2022-02-06 17:52:33 +00:00
Sam Atkins
45cf40653a Everywhere: Convert ByteBuffer factory methods from Optional -> ErrorOr
Apologies for the enormous commit, but I don't see a way to split this
up nicely. In the vast majority of cases it's a simple change. A few
extra places can use TRY instead of manual error checking though. :^)
2022-01-24 22:36:09 +01:00
Michel Hermier
0f729cebf4 LibVideo/VP9: Do not null guard calls to free 2022-01-01 17:30:25 +00:00
Andreas Kling
58fb3ebf66 LibCore+AK: Move MappedFile from AK to LibCore
MappedFile is strictly a userspace thing, so it doesn't belong in AK
(which is supposed to be user/kernel agnostic.)
2021-11-23 11:33:36 +01:00
Andreas Kling
8b1108e485 Everywhere: Pass AK::StringView by value 2021-11-11 01:27:46 +01:00
Ali Mohammad Pur
97e97bccab Everywhere: Make ByteBuffer::{create_*,copy}() OOM-safe 2021-09-06 01:53:26 +02:00
Andreas Kling
84656788bf Userland: Use kmalloc_array() where appropriate 2021-08-08 00:03:45 +02:00
Hendiadyoin1
ed46d52252 Everywhere: Use AK/Math.h if applicable
AK's version should see better inlining behaviors, than the LibM one.
We avoid mixed usage for now though.

Also clean up some stale math includes and improper floatingpoint usage.
2021-07-19 16:34:21 +04:30
FalseHonesty
d60bd42972 LibVideo/VP9: Implement MV reading & rectify MV storage issues
With this patch we are finally done with section 6.4.X of the spec :^)
The only parsing left to be done is 6.5.X, motion vector prediction.

Additionally, this patch fixes how MVs were being stored in the parser.
Originally, due to the spec naming two very different values very
similarly, these properties had totally wrong data types, but this has
now been rectified.
2021-07-10 21:28:56 +02:00
FalseHonesty
27fdf8361c LibVideo/VP9: Finish implementing block decoding (6.4.4)
Though technically block decoding calls into some other incomplete
methods, so it isn't functionally complete yet. However, we are
very close to being done with the 6.4.X sections :)
2021-07-10 21:28:56 +02:00
FalseHonesty
074fbd1b06 LibVideo/VP9: Implement parsing Token and MoreCoefs trees
These elements were being used in the new tokens implementation, so
support for them in the TreeParser has been added.

Additionally, this uncovered a bug where the nonzero contexts were
being cleared with the wrong size.
2021-07-10 21:28:56 +02:00
FalseHonesty
aa27ca1b16 LibVideo/VP9: Implement token parsing (6.4.24-6.4.26)
Note that this now requires a couple new syntax types to be parsed
in the TreeParser, so a follow-up commit will implement that behavior.
2021-07-10 21:28:56 +02:00
FalseHonesty
d79c9c262f LibVideo/VP9: Implement sections 6.1.2 and 8.4.1-8.4.4
These section implement the behavior to refresh the probability
tables after parsing a frame.
2021-07-10 21:28:56 +02:00
FalseHonesty
cf6b3d0ce9 LibVideo/VP9: Begin reference frame update process (8.10)
This was required for correctly parsing more than one frame's
height/width data properly. Additionally, start handling failure
a little more gracefully. Since we don't fully parse a tile before
starting to parse the next tile, we will now no longer make it past
the first tile mark, meaning we should not handle that scenario well.
2021-07-10 21:28:56 +02:00
FalseHonesty
514559f074 LibVideo/VP9: Rename Decoder -> Parser & create an actual Decoder class
The class that was previously named Decoder handled section 6.X.X of
the spec, which actually deals with parsing out the syntax of the data,
not the actual decoding logic which is specified in section 8.X.X.
The new Decoder class will be in charge of owning and running the
Parser, as well as implementing all of the decoding behavior.
2021-07-10 21:28:56 +02:00
FalseHonesty
66628053d4 LibVideo/VP9: Start parsing residuals (6.4.21-6.4.23)
Additionally, this uncovered a couple bugs with existing code,
so those have been fixed. Currently, parsing a whole video does
fail because we are now using a new calculation for frame width,
but it hasn't been fully implemented yet.
2021-07-10 21:28:56 +02:00
FalseHonesty
cbff7c386a LibVideo/VP9: Refactor how above & left contexts are stored & cleared
These make more sense as Vectors, and it makes it much easier to manage
their sizing.
2021-07-10 21:28:56 +02:00
FalseHonesty
91572a49c4 LibVideo/VP9: Specify which spec section defines certain behaviors 2021-07-10 21:28:56 +02:00
FalseHonesty
ce524214c9 LibVideo/VP9: Clean up formatting & use range-based for loops 2021-07-10 21:28:56 +02:00
FalseHonesty
559f5a087d LibVideo/VP9: Implement simple FIXMEs that use now supported data 2021-07-10 21:28:56 +02:00
FalseHonesty
e4f015ce3d LibVideo/VP9: Implement more TreeParser probability calculations
Now TreeParser has mostly complete probability calculation
implementations for all currently used syntax elements. Some of these
calculation methods aren't actually finished because they use data
we have yet to parse in the Decoder, but they're close to finished.
2021-07-10 21:28:56 +02:00
FalseHonesty
f85f557a1f LibVideo/VP9: Implement syntax element counting for supported elements
With the progress made in the Decoder thus far, we have the ability
to support most of the syntax element counters in the tree parser.

Additionally, it will now crash when trying to count unsupported
elements.
2021-07-10 21:28:56 +02:00
FalseHonesty
988e17ed05 LibVideo: Migrate to east-const style & apply other minor fixes
This patch brings all of LibVideo up to the east-const style in the
project. Additionally, it applies a few fixes from the reviews in #8170
that referred to older LibVideo code.
2021-06-30 11:03:51 +02:00
FalseHonesty
7d4053dde1 LibVideo/VP9: Implement most of block_mode_info methods (6.4.15-6.4.18) 2021-06-30 11:03:51 +02:00
FalseHonesty
42fdaa7f60 LibVideo/VP9: Implement most of inter_frame_mode_info (6.4.11-6.4.14) 2021-06-30 11:03:51 +02:00
FalseHonesty
e687f05b42 LibVideo/VP9: Implement intra_frame_mode_info procedure (6.4.6) 2021-06-30 11:03:51 +02:00
FalseHonesty
cc1a9e3d1c LibVideo/VP9: Add SAFE_CALL macro to help propagate failure state 2021-06-30 11:03:51 +02:00
FalseHonesty
741677b992 LibVideo/VP9: Refactor how TreeParser accesses decoder data
The TreeParser requires information about a lot of the decoder's
current state in order to parse syntax tree elements correctly, so
there has to be some communication between the Decoder and the
TreeParser. Previously, the Decoder would copy its state to the
TreeParser when it changed, however, this was a poor choice. Now,
the TreeParser simply has a reference to its owning Decoder, and
accesses its state directly.
2021-06-30 11:03:51 +02:00
FalseHonesty
375dbad144 LibVideo/VP9: Begin decoding VP9 blocks 2021-06-30 11:03:51 +02:00
FalseHonesty
2ce4155b42 LibVideo/VP9: Successfully parse partition syntax element 2021-06-30 11:03:51 +02:00
FalseHonesty
7ff6315246 LibVideo/VP9: Begin creating a tree parser to parse syntax elements 2021-06-30 11:03:51 +02:00
FalseHonesty
cfd65eafa9 LibVideo/VP9: Begin decoding tiles 2021-06-30 11:03:51 +02:00
FalseHonesty
f9899fc17f LibVideo/VP9: Parse compressed header data
This patch adds compressed header parsing to the VP9 decoder (section
6.4 of the spec). This is the final decoder step before we can start to
decode tiles.
2021-06-30 11:03:51 +02:00
FalseHonesty
baff94e44d LibVideo/VP9: Add Decoder and begin parsing uncompressed header data
This patch brings all of the previous work together and starts to
actually parse and decode frame information. Currently it only parses
the uncompressed header data (section 6.2 of the spec).
2021-06-12 22:48:28 +04:30
FalseHonesty
2e31b9cf7c LibVideo/VP9: Implement a bit stream to decode VP9 data
The VP9 specification requires a special decoding process to parse a
lot of the data read from a frame, so this BitStream wrapper implements
that behavior. These processes are defined in section 9 of the
VP9 spec.
2021-06-12 22:48:28 +04:30
FalseHonesty
18759ff56d LibVideo/VP9: Implement syntax element counter 2021-06-12 22:48:28 +04:30
FalseHonesty
8dd9d1bbdb LibVideo/VP9: Add probability table constants
This patch adds VP9's probability tables and the functions needed
to load/clear them.
2021-06-12 22:48:28 +04:30
FalseHonesty
ff3a2a703f LibVideo/VP9: Define all VP9 symbols and enum constants 2021-06-12 22:48:28 +04:30
FalseHonesty
403bb07443 LibVideo: Scaffold LibVideo and implement simplistic Matroska parser
This commit initializes the LibVideo library and implements parsing
basic Matroska container files. Currently, it will only parse audio
and video tracks.
2021-06-06 17:47:00 +02:00