Commit Graph

277 Commits

Author SHA1 Message Date
Nico Weber
1dfb065a9c LibGfx+LibVideo: Make BooleanDecoder usable for both VP8 and VP9
The marker bit is VP9-only, so move that into a new initialize_vp9()
function.

finish_decode() is VP9-only, so rename that to finish_decode_vp9().
2023-05-27 05:47:42 +02:00
Nico Weber
fbc53c1ec3 LibGfx+LibVideo: Move VP9/BooleanDecoder to LibGfx/ImageFormats
...and keep a forwarding header around in VP9, so we don't have to
update all references to the class there.

In time, we probably want to merge LibGfx/ImageDecoders and LibVideo
into LibMedia, but for now I need just this class for the lossy
webp decoder. So move just it over.

No behvior change.
2023-05-27 05:47:42 +02:00
kleines Filmröllchen
fc5cab5c21 Everywhere: Use MonotonicTime instead of Duration
This is easily identifiable by anyone who uses Duration::now_monotonic,
and any downstream users of that data.
2023-05-24 23:18:07 +02:00
kleines Filmröllchen
213025f210 AK: Rename Time to Duration
That's what this class really is; in fact that's what the first line of
the comment says it is.

This commit does not rename the main files, since those will contain
other time-related classes in a little bit.
2023-05-24 23:18:07 +02:00
Nico Weber
da00dadfe7 LibVideo: Rename local variable from "max_bits" to "bits_left"
That's how the member variable it gets copied into is called.

No behavior change.
2023-05-24 04:11:05 -07:00
Nico Weber
e43c21f4d7 LibVideo: Rename BooleanDecoder::initialize param
...from "bytes" to "size_in_bytes".

I thought at first that `bytes` meant the variable contained
bytes that the decoder would read from.

Also, this variable is called `sz` (for `size`) in the spec.

No behavior change.
2023-05-24 04:11:05 -07:00
Nico Weber
35883c337f LibVideo: Remove unused BooleanDecoder::bits_remaining() 2023-05-24 04:11:05 -07:00
Zaggy1024
e31f696ee6 LibVideo/PlaybackManager: Use a function to start the internal timer
In addition, this renames the internal timer to better suit its new
purpose since the playback state handlers were added. Not only is it
used to time frame presentations, but also to poll the queue when
seeking or buffering.
2023-05-23 05:17:48 -06:00
Zaggy1024
71d70df34f LibVideo/PlaybackManager: Decode frames off the main thread
Running decoding on the main thread can cause frames to be delayed due
to the presentation timer waiting for a decode to finish. If a frame
takes too long to decode, this delay can be quite visible. Therefore,
decoding has been moved to a separate thread, with frames sent to the
main thread through a thread-safe queue.

This results in frame times going from being late by up to 16ms to a
very consistent ~1-2ms.
2023-05-23 05:17:48 -06:00
Zaggy1024
eae7422ebc LibVideo: Fallibly construct playback manager fields 2023-05-23 05:17:48 -06:00
Caoimhe
465fa3460f LibVideo: Add PlaybackManager::from_mapped_file
This allows us to create a PlaybackManager from a file which has already
been mapped, instead of passing a file name.

This means that anyone who uses `PlaybackManager` can now use LibFSAC :)
2023-05-14 15:56:49 -06:00
Ben Wiederhake
ee47c0275e Everywhere: Run spellcheck on all documentation 2023-05-07 01:05:09 +02:00
Zaggy1024
1422f7f904 LibVideo/VP9: Revert framebuffer size reduction to allow OOB blocks
The framebuffer size was reduced in f2c0cee, but this caused some niche
block layouts to write outside of the frame.

This could be fixed by adding checks to see if a block being predicted/
reconstructed is within the frame, but the branches introduced by that
reduce performance slightly. Therefore, it's better to keep the
framebuffer sized according to the decoded frame size in 8x8 blocks so
that any block can be decoded without bounds checking.

A test was added to ensure that this continues to work.
2023-05-02 07:00:46 -04:00
Zaggy1024
2ec043c4db LibVideo/VP9: Make inter-prediction fast path accumulators 32-bit
Some occasional cases could cause the accumulator to overflow and have
an incorrect result. It would be nice to use a smaller accumulator, but
it seems not to be correct. :^(

We now cast to i16 to allow 128-bit vectorization to make use of one
whole register instead of having to split the loop into multiple.

This results in about a 5% reduction in performance in my testing.
2023-04-30 05:58:27 +02:00
Zaggy1024
b10da81c7c LibVideo: Fast-path converting colors by only matrix coefficients
We don't need to run through the whole floating-point color converter
for videos that use sRGB transfer characteristics and BT.709 color
primaries. This commit adds a new templated inlining function to
ColorConverter to do a very fast fixed-point YCbCr to RGB conversion.

With the fast path, frame conversion times go from ~7.8ms down to
~3.7ms. The fast path can benefit a lot more from extra SIMD vector
width, as well.
2023-04-25 17:44:36 -04:00
Zaggy1024
d6b867ba89 LibVideo/VP9: Force inlining of inverse_transform_2d() and the IDCT
Clang was reluctant to inline these for some reason. However, inlining
them seems to be quite beneficial, reducing decoding time in an intra-
heavy video by about 21% (~12.7s -> ~10.0s).
2023-04-25 17:44:36 -04:00
Zaggy1024
90c0e1ad8f LibVideo/VP9: Pre-calculate the quantizers at the start of each frame
Quantizers are a constant for the whole frame, except when segment
features override them, in which case they are a constant per segment
ID. We take advantage of this by pre-calculating those after reading
the quantization parameters and segmentation features for a frame.
This results in a small 1.5% improvement (~12.9s -> ~12.7s).
2023-04-25 17:44:36 -04:00
Zaggy1024
094b0d8a78 LibVideo/VP9: Use an enum to select segment features
This throws out some ugly `#define`s we had that were taking the role
of an enum anyway. We now have some nice getters in the contexts that
take the place of the combo of `seg_feature_active()` and then doing a
lookup in `FrameContext::m_segmentation_features` directly.
2023-04-25 17:44:36 -04:00
Zaggy1024
6e6cc1ddb2 LibVideo/VP9: Make a lookup table for bit reversals
Bit reversals are used very often in intra-predicted frames. Turning
these into a constexpr lookup table reduces the branching needed for
block transforms significantly. This reduces the times spent decoding
an intra-heavy 1080p video by about 9% (~14.3s -> ~12.9s).
2023-04-25 17:44:36 -04:00
Zaggy1024
f6764beead LibVideo/VP9: Specialize transforms on their block size
Previously, the block sizes would be checked at runtime to
determine the transform size to apply for residuals. Making the block
sizes into constant expressions allows all the loops to be unrolled
and reduces branching significantly.

This results in about a 26% improvement (~18s -> ~13.2s) in speed in an
intra-heavy test video.
2023-04-25 17:44:36 -04:00
Zaggy1024
5b4c1056f1 LibVideo/Color: Always inline convert_yuv_to_full_range_rgb()
Inlining the color conversion reduces time spent for frame conversions
in a 1080p video from ~12ms down to ~9ms.
2023-04-25 17:44:36 -04:00
Zaggy1024
8ad0dff5c2 LibVideo/VP9: Implement unscaled fast paths in inter prediction
Inter-prediction convolution filters are selected based on the
subpixel position determined for the motion vector relative to the
block being predicted. The subpixel position 0 only uses one single
sample in the center of the convolution, not averaging any other
samples. Let's call this a copy.

Reference frames can also be a different size relative to the frame
being predicted, but in almost every case, that scale will be 1:1
for every single frame in a video.

Taking into account these facts, we can create multiple fast paths for
inter prediction. These fast paths are only active when scaling is 1:1.

If we are doing a copy in both dimensions, then we can do a straight
memcpy from the reference frame to the output block buffer. In videos
where there is no motion, this is a dramatic speedup.

If we are doing a copy in one dimension, we can just do one convolution
and average directly into the output block buffer.

If we aren't doing a copy in either dimension, we can still cut out a
few operations from the convolution loops, since we only need to
advance our samples by whole pixels instead of subpixels.

These fast paths result in about a 34% improvement (~31.2s -> ~20.6s)
in a video which relies heavily on intra-predicted blocks due to high
motion. In videos with less motion, the improvement will be even
greater.

Also, note that the accumulators in these faster loops are only 16-bit.
High bit-depth videos will overflow those, so for now the fast path is
only used for 8-bit videos.
2023-04-25 17:44:36 -04:00
Zaggy1024
8cd72ad1ed LibVideo/VP9: Use the Y scale value in predict_inter_block()
A typo caused the Y scale value to never be used, so if a reference
frame's aspect ratio didn't match up with the current frame's, it would
decode incorrectly.

Some comments have been added to clarify the frame-constants used in
the function as well.
2023-04-25 17:44:36 -04:00
Zaggy1024
7a58577fee LibVideo: Convert subsampled frames in a vectorization-friendly way
This change reduces the time spent converting frames for display in
a 1080p video from ~19ms per frame to ~12ms per frame.
2023-04-25 17:44:36 -04:00
Zaggy1024
f2c0cee522 LibVideo/VP9: Consolidate frame size calculations
This moves all the frame size calculation to `FrameContext`, where the
subsampling is easily accessible to determine the size for each plane.
The internal framebuffer size has also been reduced to the exact frame
size that is output.
2023-04-25 17:44:36 -04:00
Zaggy1024
57c7389200 LibVideo/VP9: Fix rounding of components in the motion vector selection
The division in the `round_mv_...()` functions contained in the motion
vector selection process was done by bit shifting right. However, since
bit shifting negative values will truncate towards the negative end, it
was flooring instead of rounding.

This changes it to match the spec and rely on the compiler to simplify
down to a bit shift.
2023-04-25 17:44:36 -04:00
Timothy Flynn
374a24d84c LibVideo: Remove hook to override LibVideo's playback timers
This was added to allow Ladybird to override the timers with Qt timers.
2023-04-25 18:02:22 +02:00
Zaggy1024
eba72fa3a7 LibVideo/VP9: Wait for workers to finish when there are decoding errors
Previously, the `Parser::decode_tiles()` function wouldn't wait for the
tile-decoding workers to finish before exiting the function, which
could mean that the data the threads are working with could become
invalid if the decoder is deleted after an error is encountered.
2023-04-25 06:35:13 -04:00
Timothy Flynn
f9d8e42636 LibVideo: Allocate Vector2D underlying storage with new, not malloc
Using malloc does not invoke T's constructor, nor were were invoking T's
constructor ourselves. Accessing T without invoking its constructor is
undefined behavior.
2023-04-25 02:11:11 -06:00
Timothy Flynn
8b7c5db186 LibVideo: Do not invoke Vector2D's destructor in order to resize it
This leads to all kinds of undefined behavior, especially when callers
have a view into the Vector2D.
2023-04-24 18:21:01 +02:00
Zaggy1024
036eb82aca LibVideo/VP9: Implement threaded tile column decoding
This adds a new WorkerThread class to run one task asynchronously,
and allow waiting for that thread to finish its work.

TileContexts are placed into multiple tile column vectors with their
streams to read from pre-created. Once those are ready, the threads can
start their work on each vector separately. The main thread waits for
those tasks to finish, then sums up the syntax element counts for each
tile that was decoded.
2023-04-23 23:14:30 +02:00
Zaggy1024
1fcac52e77 LibVideo/VP9: Count syntax elements in TileContext, and sum at the end
Syntax element counters were previously accessed across tiles, which
would cause a race condition updating the counts in a tile-threaded
mode.
2023-04-23 23:14:30 +02:00
Zaggy1024
a8604d9356 LibVideo/VP9: Fallibly allocate the probability tables 2023-04-23 23:14:30 +02:00
Zaggy1024
8ce4245214 LibVideo/VP9: Return Corrupted error when tile range decoder init fails
Previously, we were incorrectly wrapping an error from `BooleanDecoder`
initialization in a `DecoderErrorCategory::Memory` error. This caused
an incorrect error message in VideoPlayer. Now it will instead return
`DecoderErrorCategory::Corrupted`.
2023-04-23 23:14:30 +02:00
Timothy Flynn
f8f35fdaad LibVideo: Remove hook to notify clients upon reaching end of the stream
This reverts commit 33047b38ec.

This use case is now satisfied with on_playback_state_change and getting
the playback state from the PlaybackManager.
2023-04-17 01:16:04 +02:00
Zaggy1024
5e3192c8d9 LibVideo/VP9: Extend the borders on reference frames to avoid branching
Extending the borders on reference frames so that motion vectors that
point outside the reference frame allows `predict_inter_block()` to
avoid some branches to clamp the sample coordinates in its loops.

This results in about a 25% improvement in decode time of a motion-
heavy YouTube video (~20.8s -> ~15.6s).
2023-04-14 07:11:45 -04:00
Zaggy1024
08b90bb2d0 LibVideo/VP9: Clamp reference frame prediction coords outside loops
Moving the clamping of the coordinates of the reference frame samples
as well as some bounds checks outside of the loop reduces the branches
needed in the `predict_inter_block()` significantly.

This results in a whopping ~41% improvement in decode performance
of an inter-prediction-heavy YouTube video (~35.4s -> ~20.8s).
2023-04-14 07:11:45 -04:00
Zaggy1024
bc49af08b4 LibVideo/VP9: Pre-calculate inter-frames' reference frame scale factors
Changing the calculation of reference frame scale factors to be done on
a per-frame basis reduces the amount of work done in
`predict_inter_block()`, which is a big hotspot in most videos.

This reduces decode times in a test video from YouTube by about 5%
(~37.2s -> ~35.4s).
2023-04-14 07:11:45 -04:00
Zaggy1024
5cd5edc3bd LibVideo/VP9: Copy data to reference frames row by row
This changes the order of the loop copying data to a reference frame
store so that it copies each row in a contiguous line rather than
copying a column at a time, which caused unnecessary branches.

This reduces the decode time on a fairly long 720p YouTube video by
about 14.5% (~43.5s to ~37.2s).
2023-04-14 07:11:45 -04:00
Zaggy1024
e1b3f9a26e LibVideo/Matroska: Remove assertion that cue seeks find earlier samples
Files can contain a first keyframe that is timestamped later than zero.
We don't want to crash in those cases, so don't assert that it can't
happen.
2023-04-14 12:05:52 +01:00
Zaggy1024
e391f18e9e LibVideo: Remove Starting playback state in favor of Seeking to zero 2023-04-14 12:05:52 +01:00
Zaggy1024
dc049e36cf LibVideo: Add a method to get the playback state from PlaybackManager 2023-04-14 12:05:52 +01:00
Zaggy1024
b081967762 LibVideo: Always present a frame when not fast-exiting seek
Previously, there was some leftover logic in `SeekingStateHandler`
which would avoid presenting a frame if it didn't find a frame both
before and after the target frame. However, this logic was unnecessary
as the `on_enter()` function would check if it needed to present a
frame and exit seeking if not.

This allows seeking to succeed if the Seeking handler cannot find a
frame before the one to be seeked to, which could occur if the first
frame of a video is not at timestamp 0.
2023-04-14 12:05:52 +01:00
Zaggy1024
989f965f54 LibVideo: Dispatch PlaybackManager state changes after on_enter()
Previously, the state change was dispatched before the new state that
was adopted had been entered, causing it to have invalid state.
2023-04-14 12:05:52 +01:00
Zaggy1024
41ed0cbbce LibVideo: Improve logging when PLAYBACK_MANAGER_DEBUG=on
This adds a timestamp to the debug output when presenting a frame, so
it can be clear the frame spacing between a presented frame and a state
change.

The seeking state will also now print when it early-exits if the seek
point is within the current sample's duration.
2023-04-14 12:05:52 +01:00
Zaggy1024
969c987787 LibVideo: Don't shadow m_playing in ResumingStateHandler classes
StartingStateHandler and SeekingStateHandler were declaring their own
`bool m_playing` fields (from previous code where there was no base
class).

In the case of SeekingStateHandler, this only made the logging wrong.

For StartingStateHandler, however, this meant that it was not using
the boolean passed as a parameter to the constructor to define the
state that would be transitioned to after the Starting state finished.
This meant that when the Stopping state replaced itself with the
Starting state, playback would not resume when Starting state exits.
2023-04-12 07:55:12 +02:00
Timothy Flynn
33047b38ec LibVideo: Add a hook to notify clients upon reaching end of the stream 2023-04-11 19:27:55 +02:00
Timothy Flynn
7132047c92 LibVideo: Add a forwarding header
And change some non-plain structs to classes for consistency.
2023-04-09 23:55:05 +02:00
Timothy Flynn
918ed5c920 LibVideo: Add a getter for the PlaybackManager's selected track 2023-04-09 23:55:05 +02:00
Timothy Flynn
519b79abde LibVideo: Add a factory to create a PlaybackManager from in-memory data
The demuxer already has such a factory, so this just exposes the same
factory in the PlaybackManager.
2023-04-09 23:55:05 +02:00