aboutsummaryrefslogtreecommitdiff
path: root/src/video_core/host1x/codecs/vp8.cpp
blob: 33d781816aadc6eecb0857e0995e5f98a039e8c7 (plain) (blame)
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
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later

// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <vector>

#include "video_core/host1x/codecs/vp8.h"
#include "video_core/host1x/host1x.h"
#include "video_core/memory_manager.h"

namespace Tegra::Decoders {
VP8::VP8(Host1x::Host1x& host1x_, const Host1x::NvdecCommon::NvdecRegisters& regs_, s32 id_)
    : Decoder{host1x_, id_, regs_}
{
    initialized = decode_api.Initialize(Host1x::NvdecCommon::VideoCodec::VP8);
}

VP8::~VP8() = default;

std::tuple<u64, u64> VP8::GetProgressiveOffsets() {
    auto luma{regs.surface_luma_offsets[static_cast<u32>(Vp8SurfaceIndex::Current)].Address()};
    auto chroma{regs.surface_chroma_offsets[static_cast<u32>(Vp8SurfaceIndex::Current)].Address()};
    return {luma, chroma};
}

std::tuple<u64, u64, u64, u64> VP8::GetInterlacedOffsets() {
    auto luma_top{regs.surface_luma_offsets[static_cast<u32>(Vp8SurfaceIndex::Current)].Address()};
    auto luma_bottom = regs.surface_luma_offsets[u32(Vp8SurfaceIndex::Current)].Address();
    auto chroma_top = regs.surface_chroma_offsets[u32(Vp8SurfaceIndex::Current)].Address();
    auto chroma_bottom = regs.surface_chroma_offsets[u32(Vp8SurfaceIndex::Current)].Address();
    return {luma_top, luma_bottom, chroma_top, chroma_bottom};
}

std::span<const u8> VP8::ComposeFrame() {
    host1x.gmmu_manager.ReadBlock(regs.picture_info_offset.Address(), &current_context, sizeof(VP8PictureInfo));
    SetFrameDimensions(current_context.frame_width, current_context.frame_height);

    const bool is_key_frame = current_context.key_frame == 1u;
    const auto bitstream_size = size_t(current_context.vld_buffer_size);
    const size_t header_size = is_key_frame ? 10u : 3u;
    frame_scratch.resize(header_size + bitstream_size);

    // Based on page 30 of the VP8 specification.
    // https://datatracker.ietf.org/doc/rfc6386/
    frame_scratch[0] = is_key_frame ? 0u : 1u; // 1-bit frame type (0: keyframe, 1: interframes).
    frame_scratch[0] |= u8((current_context.version & 7u) << 1u); // 3-bit version number
    frame_scratch[0] |= u8(1u << 4u);             // 1-bit show_frame flag

    // The next 19-bits are the first partition size
    frame_scratch[0] |= u8((current_context.first_part_size & 7u) << 5u);
    frame_scratch[1] = u8((current_context.first_part_size & 0x7f8u) >> 3u);
    frame_scratch[2] = u8((current_context.first_part_size & 0x7f800u) >> 11u);

    if (is_key_frame) {
        frame_scratch[3] = 0x9du;
        frame_scratch[4] = 0x01u;
        frame_scratch[5] = 0x2au;
        // TODO(ameerj): Horizontal/Vertical Scale
        // 16 bits: (2 bits Horizontal Scale << 14) | Width (14 bits)
        frame_scratch[6] = u8(current_context.frame_width & 0xff);
        frame_scratch[7] = u8(((current_context.frame_width >> 8) & 0x3f));
        // 16 bits:(2 bits Vertical Scale << 14) | Height (14 bits)
        frame_scratch[8] = u8(current_context.frame_height & 0xff);
        frame_scratch[9] = u8(((current_context.frame_height >> 8) & 0x3f));
    }

    const u64 bitstream_offset = regs.frame_bitstream_offset.Address();
    host1x.gmmu_manager.ReadBlock(bitstream_offset, frame_scratch.data() + header_size, bitstream_size);
    return frame_scratch;
}

} // namespace Tegra::Decoders