Skip to content

Truly independent web browser

2.4 KB
Raw Blame
/*
 * Copyright (c) 2026, Gregory Bertilson <[email protected]>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Error.h>
#include <AK/NonnullRefPtr.h>
#include <AK/Span.h>
#include <LibGfx/Forward.h>
#include <LibGfx/Size.h>
#include <LibMediaCodecs/Color/CodingIndependentCodePoints.h>
#include <LibMediaCodecs/Subsampling.h>

namespace Gfx {

// A non-owning view of planar YUV data with the metadata needed for conversion to RGB.
// The plane memory's lifetime is guaranteed by the object handing out the view
// (e.g. Media::VideoFrame).
class YUVData final {
public:
    struct PlaneSizes {
        size_t y;
        size_t u;
        size_t v;
        size_t total;
    };

    static ErrorOr<PlaneSizes> plane_sizes(IntSize size, u8 bit_depth, Media::Subsampling);
    static ErrorOr<YUVData> create(IntSize size, u8 bit_depth, Media::Subsampling, Media::CodingIndependentCodePoints, Bytes y_data, Bytes u_data, Bytes v_data);

    IntSize size() const { return m_size; }
    u8 bit_depth() const { return m_bit_depth; }
    Media::Subsampling subsampling() const { return m_subsampling; }
    Media::CodingIndependentCodePoints const& cicp() const { return m_cicp; }

    // Writable access for decoder to fill the planes after creation
    Bytes y_data() { return m_y_data; }
    Bytes u_data() { return m_u_data; }
    Bytes v_data() { return m_v_data; }
    ReadonlyBytes y_data() const { return m_y_data; }
    ReadonlyBytes u_data() const { return m_u_data; }
    ReadonlyBytes v_data() const { return m_v_data; }

    ErrorOr<NonnullRefPtr<Bitmap>> to_bitmap() const;

private:
    YUVData(IntSize size, u8 bit_depth, Media::Subsampling, Media::CodingIndependentCodePoints, Bytes y_data, Bytes u_data, Bytes v_data);

    IntSize m_size;
    u8 m_bit_depth { 0 };
    Media::Subsampling m_subsampling;
    Media::CodingIndependentCodePoints m_cicp;

    Bytes m_y_data;
    Bytes m_u_data;
    Bytes m_v_data;
};

struct BiplanarYUVPlane {
    ReadonlyBytes data;
    size_t stride;
};

// Converts the bi-planar layouts hardware decoders hand back, such as NV12, or P010 for bit depths above 8. Samples deeper
// than 8 bits sit in the high bits of each 16-bit component. Every row of each plane, the last included, must be
// padded up to its stride.
ErrorOr<NonnullRefPtr<Bitmap>> biplanar_yuv_to_bitmap(IntSize, u8 bit_depth, Media::Subsampling, Media::CodingIndependentCodePoints, BiplanarYUVPlane luma, BiplanarYUVPlane chroma);

}