2023-02-28 01:15:26 +00:00
|
|
|
// AUTOGENERATED COPYRIGHT HEADER START
|
|
|
|
// Copyright (C) 2020-2023 Michael Fabian 'Xaymar' Dirks <info@xaymar.com>
|
|
|
|
// Copyright (C) 2022 lainon <GermanAizek@yandex.ru>
|
|
|
|
// AUTOGENERATED COPYRIGHT HEADER END
|
2020-01-13 22:40:08 +00:00
|
|
|
|
|
|
|
#pragma once
|
2020-04-02 15:02:01 +00:00
|
|
|
#include "common.hpp"
|
2022-08-29 10:29:44 +00:00
|
|
|
#include "ffmpeg/avframe-queue.hpp"
|
|
|
|
#include "ffmpeg/hwapi/base.hpp"
|
|
|
|
#include "ffmpeg/swscale.hpp"
|
|
|
|
#include "handlers/handler.hpp"
|
|
|
|
#include "obs/obs-encoder-factory.hpp"
|
|
|
|
|
|
|
|
#include "warning-disable.hpp"
|
2020-01-13 22:40:08 +00:00
|
|
|
#include <condition_variable>
|
|
|
|
#include <map>
|
|
|
|
#include <mutex>
|
|
|
|
#include <queue>
|
|
|
|
#include <stack>
|
|
|
|
#include <thread>
|
|
|
|
#include <vector>
|
2022-08-29 10:29:44 +00:00
|
|
|
#include "warning-enable.hpp"
|
2020-01-13 22:40:08 +00:00
|
|
|
|
|
|
|
extern "C" {
|
2022-08-29 10:29:44 +00:00
|
|
|
#include "warning-disable.hpp"
|
2020-01-13 22:40:08 +00:00
|
|
|
#include <libavcodec/avcodec.h>
|
|
|
|
#include <libavutil/frame.h>
|
2022-08-29 10:29:44 +00:00
|
|
|
#include "warning-enable.hpp"
|
2020-01-13 22:40:08 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 16:52:06 +00:00
|
|
|
namespace streamfx::encoder::ffmpeg {
|
2020-01-13 22:40:08 +00:00
|
|
|
class ffmpeg_factory;
|
|
|
|
|
2020-06-14 20:20:21 +00:00
|
|
|
class ffmpeg_instance : public obs::encoder_instance {
|
2020-01-13 22:40:08 +00:00
|
|
|
ffmpeg_factory* _factory;
|
|
|
|
const AVCodec* _codec;
|
|
|
|
AVCodecContext* _context;
|
|
|
|
|
|
|
|
std::shared_ptr<handler::handler> _handler;
|
|
|
|
|
2021-06-08 02:12:55 +00:00
|
|
|
::streamfx::ffmpeg::swscale _scaler;
|
2022-08-27 11:15:30 +00:00
|
|
|
std::shared_ptr<AVPacket> _packet;
|
2020-06-14 20:20:21 +00:00
|
|
|
|
2021-06-08 02:12:55 +00:00
|
|
|
std::shared_ptr<::streamfx::ffmpeg::hwapi::base> _hwapi;
|
|
|
|
std::shared_ptr<::streamfx::ffmpeg::hwapi::instance> _hwinst;
|
2020-01-13 22:40:08 +00:00
|
|
|
|
2020-04-08 21:38:42 +00:00
|
|
|
std::size_t _lag_in_frames;
|
2020-06-14 20:20:21 +00:00
|
|
|
std::size_t _sent_frames;
|
2022-09-10 12:45:20 +00:00
|
|
|
std::size_t _framerate_divisor;
|
2020-01-13 22:40:08 +00:00
|
|
|
|
|
|
|
// Extra Data
|
2020-08-10 01:29:05 +00:00
|
|
|
bool _have_first_frame;
|
|
|
|
std::vector<uint8_t> _extra_data;
|
|
|
|
std::vector<uint8_t> _sei_data;
|
2020-01-13 22:40:08 +00:00
|
|
|
|
|
|
|
// Frame Stack and Queue
|
|
|
|
std::stack<std::shared_ptr<AVFrame>> _free_frames;
|
|
|
|
std::queue<std::shared_ptr<AVFrame>> _used_frames;
|
|
|
|
std::chrono::high_resolution_clock::time_point _free_frames_last_used;
|
|
|
|
|
|
|
|
public:
|
2020-07-27 19:07:32 +00:00
|
|
|
ffmpeg_instance(obs_data_t* settings, obs_encoder_t* self, bool is_hw);
|
2020-01-13 22:40:08 +00:00
|
|
|
virtual ~ffmpeg_instance();
|
|
|
|
|
2020-06-14 20:20:21 +00:00
|
|
|
public:
|
|
|
|
void get_properties(obs_properties_t* props);
|
|
|
|
|
2020-08-10 01:29:05 +00:00
|
|
|
void migrate(obs_data_t* settings, uint64_t version) override;
|
2020-01-13 22:40:08 +00:00
|
|
|
|
2020-06-14 20:20:21 +00:00
|
|
|
bool update(obs_data_t* settings) override;
|
2020-01-13 22:40:08 +00:00
|
|
|
|
2020-06-14 20:20:21 +00:00
|
|
|
bool encode_audio(struct encoder_frame* frame, struct encoder_packet* packet, bool* received_packet) override;
|
2020-01-13 22:40:08 +00:00
|
|
|
|
2020-06-14 20:20:21 +00:00
|
|
|
bool encode_video(struct encoder_frame* frame, struct encoder_packet* packet, bool* received_packet) override;
|
2020-01-13 22:40:08 +00:00
|
|
|
|
2023-05-13 12:35:46 +00:00
|
|
|
bool encode_video(uint32_t handle, int64_t pts, uint64_t lock_key, uint64_t* next_key, struct encoder_packet* packet, bool* received_packet) override;
|
2020-01-13 22:40:08 +00:00
|
|
|
|
2020-06-14 20:20:21 +00:00
|
|
|
bool get_extra_data(uint8_t** extra_data, size_t* size) override;
|
2020-01-13 22:40:08 +00:00
|
|
|
|
2020-06-14 20:20:21 +00:00
|
|
|
bool get_sei_data(uint8_t** sei_data, size_t* size) override;
|
2020-01-13 22:40:08 +00:00
|
|
|
|
2020-06-14 20:20:21 +00:00
|
|
|
void get_video_info(struct video_scale_info* info) override;
|
2020-01-13 22:40:08 +00:00
|
|
|
|
2020-06-14 20:20:21 +00:00
|
|
|
public:
|
|
|
|
void initialize_sw(obs_data_t* settings);
|
|
|
|
void initialize_hw(obs_data_t* settings);
|
2020-01-13 22:40:08 +00:00
|
|
|
|
2020-06-14 20:20:21 +00:00
|
|
|
void push_free_frame(std::shared_ptr<AVFrame> frame);
|
|
|
|
std::shared_ptr<AVFrame> pop_free_frame();
|
|
|
|
|
|
|
|
void push_used_frame(std::shared_ptr<AVFrame> frame);
|
|
|
|
std::shared_ptr<AVFrame> pop_used_frame();
|
2020-01-13 22:40:08 +00:00
|
|
|
|
|
|
|
int receive_packet(bool* received_packet, struct encoder_packet* packet);
|
|
|
|
|
|
|
|
int send_frame(std::shared_ptr<AVFrame> frame);
|
|
|
|
|
|
|
|
bool encode_avframe(std::shared_ptr<AVFrame> frame, struct encoder_packet* packet, bool* received_packet);
|
|
|
|
|
|
|
|
public: // Handler API
|
|
|
|
bool is_hardware_encode();
|
|
|
|
|
|
|
|
const AVCodec* get_avcodec();
|
|
|
|
|
|
|
|
const AVCodecContext* get_avcodeccontext();
|
|
|
|
|
2022-07-21 11:09:10 +00:00
|
|
|
void parse_ffmpeg_commandline(std::string_view text);
|
2020-01-13 22:40:08 +00:00
|
|
|
};
|
2020-04-05 16:52:06 +00:00
|
|
|
|
2020-06-14 20:20:21 +00:00
|
|
|
class ffmpeg_factory : public obs::encoder_factory<ffmpeg_factory, ffmpeg_instance> {
|
|
|
|
std::string _id;
|
|
|
|
std::string _codec;
|
|
|
|
std::string _name;
|
|
|
|
|
|
|
|
const AVCodec* _avcodec;
|
|
|
|
|
|
|
|
std::shared_ptr<handler::handler> _handler;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ffmpeg_factory(const AVCodec* codec);
|
|
|
|
virtual ~ffmpeg_factory();
|
|
|
|
|
|
|
|
const char* get_name() override;
|
|
|
|
|
|
|
|
void get_defaults2(obs_data_t* data) override;
|
|
|
|
|
2021-12-02 16:05:05 +00:00
|
|
|
void migrate(obs_data_t* data, uint64_t version) override;
|
|
|
|
|
2020-06-14 20:20:21 +00:00
|
|
|
obs_properties_t* get_properties2(instance_t* data) override;
|
|
|
|
|
2021-04-16 23:43:30 +00:00
|
|
|
#ifdef ENABLE_FRONTEND
|
|
|
|
static bool on_manual_open(obs_properties_t* props, obs_property_t* property, void* data);
|
|
|
|
#endif
|
|
|
|
|
2020-06-14 20:20:21 +00:00
|
|
|
public:
|
|
|
|
const AVCodec* get_avcodec();
|
2020-08-10 00:37:51 +00:00
|
|
|
|
|
|
|
obs_encoder_info* get_info();
|
2020-06-14 20:20:21 +00:00
|
|
|
};
|
|
|
|
|
2020-04-05 16:52:06 +00:00
|
|
|
class ffmpeg_manager {
|
|
|
|
std::map<const AVCodec*, std::shared_ptr<ffmpeg_factory>> _factories;
|
|
|
|
std::map<std::string, std::shared_ptr<handler::handler>> _handlers;
|
|
|
|
std::shared_ptr<handler::handler> _debug_handler;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ffmpeg_manager();
|
|
|
|
~ffmpeg_manager();
|
|
|
|
|
2020-06-14 20:20:21 +00:00
|
|
|
void register_encoders();
|
|
|
|
|
2020-04-05 16:52:06 +00:00
|
|
|
void register_handler(std::string codec, std::shared_ptr<handler::handler> handler);
|
|
|
|
|
|
|
|
std::shared_ptr<handler::handler> get_handler(std::string codec);
|
|
|
|
|
2022-07-21 11:09:10 +00:00
|
|
|
bool has_handler(std::string_view codec);
|
2020-04-05 16:52:06 +00:00
|
|
|
|
|
|
|
public: // Singleton
|
|
|
|
static void initialize();
|
|
|
|
|
|
|
|
static void finalize();
|
|
|
|
|
2023-05-14 04:50:27 +00:00
|
|
|
static std::shared_ptr<ffmpeg_manager> instance();
|
2020-04-05 16:52:06 +00:00
|
|
|
};
|
|
|
|
} // namespace streamfx::encoder::ffmpeg
|