summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibVideo/VideoFrame.h
blob: 049d88a1218e0ba2c6ca9c7d3af45cd0779df81b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
 * Copyright (c) 2022, Gregory Bertilson <zaggy1024@gmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/ByteBuffer.h>
#include <AK/Concepts.h>
#include <AK/FixedArray.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/Size.h>
#include <LibVideo/Color/CodingIndependentCodePoints.h>
#include <LibVideo/DecoderError.h>

namespace Video {

class VideoFrame {

public:
    virtual ~VideoFrame() { }

    virtual DecoderErrorOr<void> output_to_bitmap(Gfx::Bitmap& bitmap) = 0;
    virtual DecoderErrorOr<NonnullRefPtr<Gfx::Bitmap>> to_bitmap()
    {
        auto bitmap = DECODER_TRY_ALLOC(Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, m_size));
        TRY(output_to_bitmap(bitmap));
        return bitmap;
    }

    inline Gfx::IntSize size() { return m_size; }
    inline size_t width() { return size().width(); }
    inline size_t height() { return size().height(); }

    inline u8 bit_depth() { return m_bit_depth; }
    inline CodingIndependentCodePoints& cicp() { return m_cicp; }

protected:
    VideoFrame(Gfx::IntSize size,
        u8 bit_depth, CodingIndependentCodePoints cicp)
        : m_size(size)
        , m_bit_depth(bit_depth)
        , m_cicp(cicp)
    {
    }

    Gfx::IntSize m_size;
    u8 m_bit_depth;
    CodingIndependentCodePoints m_cicp;
};

class SubsampledYUVFrame : public VideoFrame {

public:
    static ErrorOr<NonnullOwnPtr<SubsampledYUVFrame>> try_create(
        Gfx::IntSize size,
        u8 bit_depth, CodingIndependentCodePoints cicp,
        bool subsampling_horizontal, bool subsampling_vertical,
        Span<u16> plane_y, Span<u16> plane_u, Span<u16> plane_v);

    SubsampledYUVFrame(
        Gfx::IntSize size,
        u8 bit_depth, CodingIndependentCodePoints cicp,
        bool subsampling_horizontal, bool subsampling_vertical,
        FixedArray<u16>& plane_y, FixedArray<u16>& plane_u, FixedArray<u16>& plane_v)
        : VideoFrame(size, bit_depth, cicp)
        , m_subsampling_horizontal(subsampling_horizontal)
        , m_subsampling_vertical(subsampling_vertical)
        , m_plane_y(move(plane_y))
        , m_plane_u(move(plane_u))
        , m_plane_v(move(plane_v))
    {
    }

    DecoderErrorOr<void> output_to_bitmap(Gfx::Bitmap& bitmap) override;

protected:
    bool m_subsampling_horizontal;
    bool m_subsampling_vertical;
    FixedArray<u16> m_plane_y;
    FixedArray<u16> m_plane_u;
    FixedArray<u16> m_plane_v;
};

}