2025-12-18 23:07:14 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <d3d11.h>
|
|
|
|
|
#include <wrl/client.h>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
2025-12-22 13:48:06 +08:00
|
|
|
#ifndef NO_FFMPEG
|
2025-12-18 23:07:14 +08:00
|
|
|
extern "C" {
|
|
|
|
|
#include <libavcodec/avcodec.h>
|
|
|
|
|
#include <libavutil/imgutils.h>
|
|
|
|
|
#include <libavutil/opt.h>
|
|
|
|
|
#include <libswscale/swscale.h>
|
|
|
|
|
}
|
2025-12-22 13:48:06 +08:00
|
|
|
#endif
|
2025-12-18 23:07:14 +08:00
|
|
|
|
|
|
|
|
using Microsoft::WRL::ComPtr;
|
|
|
|
|
|
|
|
|
|
class VideoEncoder {
|
|
|
|
|
public:
|
|
|
|
|
VideoEncoder();
|
|
|
|
|
~VideoEncoder();
|
|
|
|
|
|
|
|
|
|
bool Initialize(ID3D11Device* device, int width, int height, int fps, int bitrate);
|
2025-12-22 13:48:06 +08:00
|
|
|
bool Reinitialize(int width, int height, int fps, int bitrate);
|
2025-12-18 23:07:14 +08:00
|
|
|
bool EncodeFrame(ID3D11Texture2D* texture, std::vector<uint8_t>& outputData, bool& isKeyFrame);
|
2025-12-22 13:48:06 +08:00
|
|
|
void Release();
|
2025-12-18 23:07:14 +08:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
ID3D11Device* device_ = nullptr;
|
|
|
|
|
ID3D11DeviceContext* context_ = nullptr;
|
|
|
|
|
ComPtr<ID3D11Texture2D> stagingTexture_;
|
|
|
|
|
|
2025-12-22 13:48:06 +08:00
|
|
|
#ifndef NO_FFMPEG
|
2025-12-18 23:07:14 +08:00
|
|
|
AVCodecContext* codecContext_ = nullptr;
|
|
|
|
|
AVFrame* frame_ = nullptr;
|
|
|
|
|
AVPacket* packet_ = nullptr;
|
|
|
|
|
SwsContext* swsContext_ = nullptr;
|
2025-12-22 13:48:06 +08:00
|
|
|
#else
|
|
|
|
|
void* codecContext_ = nullptr;
|
|
|
|
|
void* frame_ = nullptr;
|
|
|
|
|
void* packet_ = nullptr;
|
|
|
|
|
void* swsContext_ = nullptr;
|
|
|
|
|
#endif
|
2025-12-18 23:07:14 +08:00
|
|
|
|
|
|
|
|
int width_ = 0;
|
|
|
|
|
int height_ = 0;
|
|
|
|
|
int pts_ = 0;
|
|
|
|
|
};
|