2021-06-05 16:42:25 -04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Hunter Salyer <thefalsehonesty@gmail.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2022-10-29 17:39:45 -05:00
|
|
|
#include "LibVideo/Color/CodingIndependentCodePoints.h"
|
2022-10-29 17:02:43 -05:00
|
|
|
#include "LibVideo/MatroskaDemuxer.h"
|
2022-09-25 17:23:16 -05:00
|
|
|
#include <LibCore/ArgsParser.h>
|
|
|
|
#include <LibCore/ElapsedTimer.h>
|
2021-06-05 16:42:25 -04:00
|
|
|
#include <LibGUI/Application.h>
|
|
|
|
#include <LibGUI/BoxLayout.h>
|
|
|
|
#include <LibGUI/ImageWidget.h>
|
|
|
|
#include <LibGUI/Window.h>
|
|
|
|
#include <LibGfx/Bitmap.h>
|
2021-12-31 22:29:07 +00:00
|
|
|
#include <LibMain/Main.h>
|
2022-10-10 05:03:57 -05:00
|
|
|
#include <LibVideo/Color/ColorConverter.h>
|
2021-06-05 16:42:25 -04:00
|
|
|
#include <LibVideo/MatroskaReader.h>
|
|
|
|
#include <LibVideo/VP9/Decoder.h>
|
|
|
|
|
2021-12-31 22:29:07 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2021-06-05 16:42:25 -04:00
|
|
|
{
|
2022-09-25 17:23:16 -05:00
|
|
|
bool benchmark = false;
|
|
|
|
StringView filename = "/home/anon/Videos/test-webm.webm"sv;
|
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.add_option(benchmark, "Benchmark the video decoder.", "benchmark", 'b');
|
|
|
|
args_parser.add_positional_argument(filename, "The video file to display.", "filename", Core::ArgsParser::Required::No);
|
|
|
|
args_parser.parse(arguments);
|
|
|
|
|
2021-12-31 22:29:07 +00:00
|
|
|
auto app = TRY(GUI::Application::try_create(arguments));
|
|
|
|
auto window = TRY(GUI::Window::try_create());
|
2021-06-05 16:42:25 -04:00
|
|
|
|
2022-10-29 17:02:43 -05:00
|
|
|
auto demuxer_result = Video::MatroskaDemuxer::from_file(filename);
|
|
|
|
if (demuxer_result.is_error()) {
|
|
|
|
outln("Error parsing Matroska: {}", demuxer_result.release_error().string_literal());
|
2022-09-25 17:23:16 -05:00
|
|
|
return 1;
|
|
|
|
}
|
2022-10-29 17:02:43 -05:00
|
|
|
auto demuxer = demuxer_result.release_value();
|
|
|
|
auto tracks = demuxer->get_tracks_for_type(Video::TrackType::Video);
|
|
|
|
if (tracks.is_empty()) {
|
|
|
|
outln("No video tracks present.");
|
2021-06-05 16:42:25 -04:00
|
|
|
return 1;
|
2022-10-29 17:02:43 -05:00
|
|
|
}
|
|
|
|
auto track = tracks[0];
|
2022-09-16 04:07:52 -05:00
|
|
|
|
2022-01-07 15:01:38 +01:00
|
|
|
auto main_widget = TRY(window->try_set_main_widget<GUI::Widget>());
|
|
|
|
main_widget->set_fill_with_background_color(true);
|
|
|
|
main_widget->set_layout<GUI::VerticalBoxLayout>();
|
2022-09-16 04:07:52 -05:00
|
|
|
auto image_widget = TRY(main_widget->try_add<GUI::ImageWidget>());
|
2021-06-05 16:42:25 -04:00
|
|
|
|
2022-10-29 19:53:24 -05:00
|
|
|
OwnPtr<Video::VideoDecoder> decoder = make<Video::VP9::Decoder>();
|
2022-09-16 04:07:52 -05:00
|
|
|
auto frame_number = 0u;
|
|
|
|
|
|
|
|
auto display_next_frame = [&]() {
|
2022-10-29 17:02:43 -05:00
|
|
|
auto sample_result = demuxer->get_next_video_sample_for_track(track);
|
2022-09-16 04:07:52 -05:00
|
|
|
|
2022-10-29 17:02:43 -05:00
|
|
|
if (sample_result.is_error()) {
|
|
|
|
outln("Error demuxing next sample {}: {}", frame_number, sample_result.release_error().string_literal());
|
2022-09-16 04:07:52 -05:00
|
|
|
return;
|
2022-10-29 17:02:43 -05:00
|
|
|
}
|
2022-09-16 04:07:52 -05:00
|
|
|
|
2022-10-29 17:02:43 -05:00
|
|
|
auto sample = sample_result.release_value();
|
|
|
|
auto result = decoder->receive_sample(sample->data());
|
2022-09-16 04:07:52 -05:00
|
|
|
|
|
|
|
if (result.is_error()) {
|
|
|
|
outln("Error decoding frame {}: {}", frame_number, result.error().string_literal());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-29 19:53:24 -05:00
|
|
|
auto frame_result = decoder->get_decoded_frame();
|
2022-10-08 18:36:57 -05:00
|
|
|
if (frame_result.is_error()) {
|
|
|
|
outln("Error retrieving frame {}: {}", frame_number, frame_result.error().string_literal());
|
2022-10-10 05:03:57 -05:00
|
|
|
return;
|
|
|
|
}
|
2022-10-08 18:36:57 -05:00
|
|
|
auto frame = frame_result.release_value();
|
2022-09-16 04:07:52 -05:00
|
|
|
|
2022-10-08 18:36:57 -05:00
|
|
|
auto& cicp = frame->cicp();
|
2022-10-29 17:02:43 -05:00
|
|
|
cicp.adopt_specified_values(sample->container_cicp());
|
2022-10-29 17:39:45 -05:00
|
|
|
cicp.default_code_points_if_unspecified({ Video::ColorPrimaries::BT709, Video::TransferCharacteristics::BT709, Video::MatrixCoefficients::BT709, Video::ColorRange::Studio });
|
2022-10-10 05:03:57 -05:00
|
|
|
|
2022-10-29 17:02:43 -05:00
|
|
|
auto convert_result = frame->to_bitmap();
|
2022-10-08 18:36:57 -05:00
|
|
|
if (convert_result.is_error()) {
|
|
|
|
outln("Error creating bitmap for frame {}: {}", frame_number, convert_result.error().string_literal());
|
|
|
|
return;
|
2022-09-16 04:07:52 -05:00
|
|
|
}
|
|
|
|
|
2022-10-29 17:02:43 -05:00
|
|
|
image_widget->set_bitmap(convert_result.release_value());
|
|
|
|
image_widget->set_fixed_size(frame->size());
|
2022-09-16 04:07:52 -05:00
|
|
|
image_widget->update();
|
|
|
|
|
|
|
|
frame_number++;
|
|
|
|
};
|
|
|
|
|
2022-09-25 17:23:16 -05:00
|
|
|
image_widget->on_click = [&]() { display_next_frame(); };
|
|
|
|
|
|
|
|
if (benchmark) {
|
|
|
|
auto timer = Core::ElapsedTimer::start_new();
|
|
|
|
for (auto i = 0; i < 100; i++)
|
|
|
|
display_next_frame();
|
|
|
|
auto elapsed_time = timer.elapsed_time();
|
|
|
|
outln("Decoding 100 frames took {} ms", elapsed_time.to_milliseconds());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-09-16 04:07:52 -05:00
|
|
|
display_next_frame();
|
2021-06-05 16:42:25 -04:00
|
|
|
|
|
|
|
window->show();
|
|
|
|
return app->exec();
|
|
|
|
}
|