Serenity Operating System
1/*
2 * Copyright (c) 2022, Gregory Bertilson <zaggy1024@gmail.com>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#pragma once
8
9#include <AK/ByteBuffer.h>
10#include <AK/FixedArray.h>
11#include <LibGfx/Bitmap.h>
12#include <LibGfx/Size.h>
13#include <LibVideo/Color/CodingIndependentCodePoints.h>
14#include <LibVideo/DecoderError.h>
15
16namespace Video {
17
18class VideoFrame {
19
20public:
21 virtual ~VideoFrame() { }
22
23 virtual DecoderErrorOr<void> output_to_bitmap(Gfx::Bitmap& bitmap) = 0;
24 virtual DecoderErrorOr<NonnullRefPtr<Gfx::Bitmap>> to_bitmap()
25 {
26 auto bitmap = DECODER_TRY_ALLOC(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, m_size));
27 TRY(output_to_bitmap(bitmap));
28 return bitmap;
29 }
30
31 inline Gfx::IntSize size() { return m_size; }
32 inline size_t width() { return size().width(); }
33 inline size_t height() { return size().height(); }
34
35 inline u8 bit_depth() { return m_bit_depth; }
36 inline CodingIndependentCodePoints& cicp() { return m_cicp; }
37
38protected:
39 VideoFrame(Gfx::IntSize size,
40 u8 bit_depth, CodingIndependentCodePoints cicp)
41 : m_size(size)
42 , m_bit_depth(bit_depth)
43 , m_cicp(cicp)
44 {
45 }
46
47 Gfx::IntSize m_size;
48 u8 m_bit_depth;
49 CodingIndependentCodePoints m_cicp;
50};
51
52class SubsampledYUVFrame : public VideoFrame {
53
54public:
55 static ErrorOr<NonnullOwnPtr<SubsampledYUVFrame>> try_create(
56 Gfx::IntSize size,
57 u8 bit_depth, CodingIndependentCodePoints cicp,
58 bool subsampling_horizontal, bool subsampling_vertical,
59 Span<u16> plane_y, Span<u16> plane_u, Span<u16> plane_v);
60
61 SubsampledYUVFrame(
62 Gfx::IntSize size,
63 u8 bit_depth, CodingIndependentCodePoints cicp,
64 bool subsampling_horizontal, bool subsampling_vertical,
65 FixedArray<u16>& plane_y, FixedArray<u16>& plane_u, FixedArray<u16>& plane_v)
66 : VideoFrame(size, bit_depth, cicp)
67 , m_subsampling_horizontal(subsampling_horizontal)
68 , m_subsampling_vertical(subsampling_vertical)
69 , m_plane_y(move(plane_y))
70 , m_plane_u(move(plane_u))
71 , m_plane_v(move(plane_v))
72 {
73 }
74
75 DecoderErrorOr<void> output_to_bitmap(Gfx::Bitmap& bitmap) override;
76
77protected:
78 bool m_subsampling_horizontal;
79 bool m_subsampling_vertical;
80 FixedArray<u16> m_plane_y;
81 FixedArray<u16> m_plane_u;
82 FixedArray<u16> m_plane_v;
83};
84
85}