Skip to content

File InputSource.h

File List > cpp > input > InputSource.h

Go to the documentation of this file

/*
 * SPDX-FileCopyrightText: 2026 Stagelab Coop SCCL
 * SPDX-License-Identifier: GPL-3.0-or-later
 * SPDX-FileContributor: Ion Reguera <ion@stagelab.coop>
 *
 * This file is part of cuems-videocomposer.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef VIDEOCOMPOSER_INPUTSOURCE_H
#define VIDEOCOMPOSER_INPUTSOURCE_H

#include "../video/FrameBuffer.h"
#include "../video/FrameFormat.h"
#include "../video/GPUTextureFrameBuffer.h"
#include <string>
#include <cstdint>

namespace videocomposer {

class InputSource {
public:
    virtual ~InputSource() = default;

    virtual bool open(const std::string& source) = 0;

    virtual void close() = 0;

    virtual bool isReady() const = 0;

    virtual bool readFrame(int64_t frameNumber, FrameBuffer& buffer) = 0;

    virtual bool seek(int64_t frameNumber) = 0;

    virtual void resetSeekState() {}

    virtual FrameInfo getFrameInfo() const = 0;

    virtual int64_t getCurrentFrame() const = 0;

    enum class CodecType {
        HAP,           // HAP codec (standard)
        HAP_Q,         // HAP Q variant (higher quality)
        HAP_ALPHA,     // HAP Alpha variant (with alpha channel)
        H264,          // H.264/AVC
        HEVC,          // H.265/HEVC
        AV1,           // AV1
        SOFTWARE       // Software codec (fallback)
    };

    enum class DecodeBackend {
        HAP_DIRECT,    // HAP direct GPU texture (zero-copy)
        GPU_HARDWARE,  // GPU hardware decoder (NVDEC, VAAPI, etc.)
        CPU_SOFTWARE   // CPU software decoder
    };

    virtual CodecType detectCodec() const = 0;

    virtual bool supportsDirectGPUTexture() const = 0;

    virtual DecodeBackend getOptimalBackend() const = 0;

    virtual bool isLiveStream() const { return false; }  // Default: not live

    virtual bool readLatestFrame(FrameBuffer& buffer) {
        return readFrame(0, buffer);
    }

    // --- Shared decoder cache ---
    // Used by driver layers to store decoded frames for secondary layers to read.
    // Cache stores frames by value inside InputSource (not raw pointers to external buffers).
    // Implemented in the base class — all subclasses inherit cache support automatically.

    bool hasCachedFrame() const { return hasCachedFrame_; }
    const GPUTextureFrameBuffer* getCachedGPUTexture() const { return cachedOnGPU_ ? &cachedGPU_ : nullptr; }
    const FrameBuffer* getCachedCPUFrame() const { return !cachedOnGPU_ ? &cachedCPU_ : nullptr; }
    int64_t getCachedFrameNumber() const { return cachedFrameNumber_; }
    bool isCachedOnGPU() const { return cachedOnGPU_; }

    void setCachedFrame(int64_t frameNumber, const GPUTextureFrameBuffer& gpu) {
        cachedGPU_ = gpu;               // non-owning copy (ownsTexture_=false via copy ctor)
        cachedFrameNumber_ = frameNumber;
        cachedOnGPU_ = true;
        hasCachedFrame_ = true;
    }

    void setCachedFrame(int64_t frameNumber, const FrameBuffer& cpu) {
        cachedCPU_.copyFrom(cpu);        // non-owning shallow copy (pointer + metadata, NOT pixels)
        cachedFrameNumber_ = frameNumber;
        cachedOnGPU_ = false;
        hasCachedFrame_ = true;
    }

    void invalidateCache() {
        hasCachedFrame_ = false;
        cachedFrameNumber_ = -1;
    }

private:
    // Shared decoder cache storage (only populated when this InputSource is used as a shared driver)
    bool hasCachedFrame_ = false;
    int64_t cachedFrameNumber_ = -1;
    GPUTextureFrameBuffer cachedGPU_;
    FrameBuffer cachedCPU_;
    bool cachedOnGPU_ = false;
};

} // namespace videocomposer

#endif // VIDEOCOMPOSER_INPUTSOURCE_H