Add global hardware acceleration codec switch

This commit is contained in:
dijunkun
2023-09-21 10:42:16 +08:00
parent a794cd43b9
commit b4318cc8d6
17 changed files with 113 additions and 85 deletions

View File

@@ -2,7 +2,7 @@
#include "log.h"
#define SAVE_ENCODER_STREAM 1
#define SAVE_DECODER_STREAM 1
extern "C" {
#include <libavformat/avformat.h>
@@ -10,17 +10,10 @@ extern "C" {
#include <libswscale/swscale.h>
};
FfmpegVideoDecoder::FfmpegVideoDecoder() {
if (SAVE_ENCODER_STREAM) {
file_ = fopen("decode_stream.yuv", "w+b");
if (!file_) {
LOG_WARN("Fail to open stream.yuv");
}
}
}
FfmpegVideoDecoder::FfmpegVideoDecoder() {}
FfmpegVideoDecoder::~FfmpegVideoDecoder() {
if (SAVE_ENCODER_STREAM && file_) {
if (SAVE_DECODER_STREAM && file_) {
fflush(file_);
fclose(file_);
file_ = nullptr;
@@ -31,11 +24,26 @@ FfmpegVideoDecoder::~FfmpegVideoDecoder() {
decoded_frame_ = nullptr;
}
av_frame_free(&frame_);
av_frame_free(&frame_nv12_);
sws_freeContext(img_convert_ctx);
avcodec_close(codec_ctx_);
av_free(codec_ctx_);
if (packet_) {
av_packet_free(&packet_);
}
if (frame_) {
av_frame_free(&frame_);
}
if (frame_nv12_) {
av_frame_free(&frame_nv12_);
}
if (img_convert_ctx) {
sws_freeContext(img_convert_ctx);
}
if (codec_ctx_) {
avcodec_close(codec_ctx_);
}
if (codec_ctx_) {
av_free(codec_ctx_);
}
}
int FfmpegVideoDecoder::Init() {
@@ -71,12 +79,20 @@ int FfmpegVideoDecoder::Init() {
frame_ = av_frame_alloc();
frame_nv12_ = av_frame_alloc();
packet_ = av_packet_alloc();
img_convert_ctx =
sws_getContext(1280, 720, AV_PIX_FMT_YUV420P, 1280, 720, AV_PIX_FMT_NV12,
SWS_FAST_BILINEAR, nullptr, nullptr, nullptr);
decoded_frame_ = new VideoFrame(1280 * 720 * 3 / 2);
if (SAVE_DECODER_STREAM) {
file_ = fopen("decode_stream.yuv", "w+b");
if (!file_) {
LOG_WARN("Fail to open stream.yuv");
}
}
return 0;
}
@@ -93,10 +109,11 @@ int FfmpegVideoDecoder::Decode(
}
}
packet_.data = (uint8_t *)data;
packet_.size = size;
packet_->data = (uint8_t *)data;
packet_->size = size;
int ret = avcodec_send_packet(codec_ctx_, &packet_);
int ret = avcodec_send_packet(codec_ctx_, packet_);
av_packet_unref(packet_);
while (ret >= 0) {
ret = avcodec_receive_frame(codec_ctx_, frame_);
@@ -135,9 +152,9 @@ int FfmpegVideoDecoder::Decode(
LOG_ERROR("cost {}", now_ts - start_ts);
on_receive_decoded_frame(*decoded_frame_);
if (SAVE_ENCODER_STREAM) {
fwrite((unsigned char *)frame_->data, 1,
frame_->width * frame_->height * 3 / 2, file_);
if (SAVE_DECODER_STREAM) {
fwrite((unsigned char *)decoded_frame_->Buffer(), 1,
decoded_frame_->Size(), file_);
}
}
}

View File

@@ -22,7 +22,7 @@ extern "C" {
class FfmpegVideoDecoder : public VideoDecoder {
public:
FfmpegVideoDecoder();
~FfmpegVideoDecoder();
virtual ~FfmpegVideoDecoder();
public:
int Init();
@@ -33,7 +33,7 @@ class FfmpegVideoDecoder : public VideoDecoder {
AVCodecID codec_id_;
const AVCodec *codec_;
AVCodecContext *codec_ctx_ = nullptr;
AVPacket packet_;
AVPacket *packet_ = nullptr;
AVFrame *frame_ = nullptr;
AVFrame *frame_nv12_ = nullptr;
struct SwsContext *img_convert_ctx = nullptr;

View File

@@ -2,18 +2,11 @@
#include "log.h"
#define SAVE_ENCODER_STREAM 0
#define SAVE_DECODER_STREAM 1
NvidiaVideoDecoder::NvidiaVideoDecoder() {
if (SAVE_ENCODER_STREAM) {
file_ = fopen("decode_stream.h264", "w+b");
if (!file_) {
LOG_WARN("Fail to open stream.h264");
}
}
}
NvidiaVideoDecoder::NvidiaVideoDecoder() {}
NvidiaVideoDecoder::~NvidiaVideoDecoder() {
if (SAVE_ENCODER_STREAM && file_) {
if (SAVE_DECODER_STREAM && file_) {
fflush(file_);
fclose(file_);
file_ = nullptr;
@@ -40,6 +33,13 @@ int NvidiaVideoDecoder::Init() {
}
decoder = new NvDecoder(cuContext, false, cudaVideoCodec_H264, true);
if (SAVE_DECODER_STREAM) {
file_ = fopen("decode_stream.h264", "w+b");
if (!file_) {
LOG_WARN("Fail to open stream.h264");
}
}
return 0;
}
@@ -54,10 +54,6 @@ int NvidiaVideoDecoder::Decode(
// LOG_WARN("Receive key frame");
}
if (SAVE_ENCODER_STREAM) {
fwrite((unsigned char *)data, 1, size, file_);
}
int num_frame_returned = decoder->Decode(data, size);
for (size_t i = 0; i < num_frame_returned; ++i) {
@@ -71,6 +67,10 @@ int NvidiaVideoDecoder::Decode(
data, decoder->GetWidth() * decoder->GetHeight() * 3 / 2,
decoder->GetWidth(), decoder->GetHeight());
on_receive_decoded_frame(decoded_frame);
if (SAVE_DECODER_STREAM) {
fwrite((unsigned char *)decoded_frame.Buffer(), 1,
decoded_frame.Size(), file_);
}
}
}
}

View File

@@ -9,7 +9,7 @@
class NvidiaVideoDecoder : public VideoDecoder {
public:
NvidiaVideoDecoder();
~NvidiaVideoDecoder();
virtual ~NvidiaVideoDecoder();
public:
int Init();

View File

@@ -7,11 +7,11 @@ VideoDecoderFactory::VideoDecoderFactory() {}
VideoDecoderFactory::~VideoDecoderFactory() {}
VideoDecoder *VideoDecoderFactory::CreateVideoDecoder(
std::unique_ptr<VideoDecoder> VideoDecoderFactory::CreateVideoDecoder(
bool hardware_acceleration) {
if (hardware_acceleration) {
return new NvidiaVideoDecoder();
return std::make_unique<NvidiaVideoDecoder>(NvidiaVideoDecoder());
} else {
return new FfmpegVideoDecoder();
return std::make_unique<FfmpegVideoDecoder>(FfmpegVideoDecoder());
}
}

View File

@@ -7,10 +7,8 @@ class VideoDecoderFactory {
VideoDecoderFactory();
~VideoDecoderFactory();
static VideoDecoder *CreateVideoDecoder(bool hardware_acceleration);
private:
bool hardware_acceleration_ = false;
static std::unique_ptr<VideoDecoder> CreateVideoDecoder(
bool hardware_acceleration);
};
#endif

View File

@@ -4,16 +4,9 @@
#include "log.h"
#define SAVE_ENCODER_STREAM 0
#define SAVE_ENCODER_STREAM 1
FFmpegVideoEncoder::FFmpegVideoEncoder() {
if (SAVE_ENCODER_STREAM) {
file_ = fopen("encode_stream.h264", "w+b");
if (!file_) {
LOG_WARN("Fail to open stream.h264");
}
}
}
FFmpegVideoEncoder::FFmpegVideoEncoder() {}
FFmpegVideoEncoder::~FFmpegVideoEncoder() {
if (SAVE_ENCODER_STREAM && file_) {
fflush(file_);
@@ -21,12 +14,14 @@ FFmpegVideoEncoder::~FFmpegVideoEncoder() {
file_ = nullptr;
}
av_packet_free(&packet_);
if (nv12_data_) {
free(nv12_data_);
nv12_data_ = nullptr;
}
if (packet_) {
av_packet_free(&packet_);
}
}
int FFmpegVideoEncoder::Init() {
@@ -82,6 +77,13 @@ int FFmpegVideoEncoder::Init() {
packet_ = av_packet_alloc();
if (SAVE_ENCODER_STREAM) {
file_ = fopen("encode_stream.h264", "w+b");
if (!file_) {
LOG_WARN("Fail to open stream.h264");
}
}
return 0;
}

View File

@@ -29,7 +29,7 @@ extern "C" {
class FFmpegVideoEncoder : public VideoEncoder {
public:
FFmpegVideoEncoder();
~FFmpegVideoEncoder();
virtual ~FFmpegVideoEncoder();
int Init();
int Encode(
@@ -57,7 +57,7 @@ class FFmpegVideoEncoder : public VideoEncoder {
const AVCodec* codec_ = nullptr;
AVCodecContext* codec_ctx_ = nullptr;
AVFrame* frame_ = nullptr;
AVPacket* packet_;
AVPacket* packet_ = nullptr;
bool got_output_ = false;
uint32_t pts_ = 0;
};

View File

@@ -4,16 +4,9 @@
#include "log.h"
#define SAVE_ENCODER_STREAM 0
#define SAVE_ENCODER_STREAM 1
NvidiaVideoEncoder::NvidiaVideoEncoder() {
if (SAVE_ENCODER_STREAM) {
file_ = fopen("encode_stream.h264", "w+b");
if (!file_) {
LOG_WARN("Fail to open stream.h264");
}
}
}
NvidiaVideoEncoder::NvidiaVideoEncoder() {}
NvidiaVideoEncoder::~NvidiaVideoEncoder() {
if (SAVE_ENCODER_STREAM && file_) {
fflush(file_);
@@ -70,6 +63,13 @@ int NvidiaVideoEncoder::Init() {
max_payload_size_;
encoder_->CreateEncoder(&init_params);
if (SAVE_ENCODER_STREAM) {
file_ = fopen("encode_stream.h264", "w+b");
if (!file_) {
LOG_WARN("Fail to open stream.h264");
}
}
return 0;
}

View File

@@ -9,7 +9,7 @@
class NvidiaVideoEncoder : public VideoEncoder {
public:
NvidiaVideoEncoder();
~NvidiaVideoEncoder();
virtual ~NvidiaVideoEncoder();
int Init();
int Encode(

View File

@@ -11,6 +11,9 @@ class VideoEncoder {
on_encoded_image) = 0;
virtual int OnEncodedImage(char* encoded_packets, size_t size) = 0;
virtual void ForceIdr() = 0;
VideoEncoder() = default;
virtual ~VideoEncoder() {}
};
#endif

View File

@@ -7,11 +7,11 @@ VideoEncoderFactory::VideoEncoderFactory() {}
VideoEncoderFactory::~VideoEncoderFactory() {}
VideoEncoder *VideoEncoderFactory::CreateVideoEncoder(
std::unique_ptr<VideoEncoder> VideoEncoderFactory::CreateVideoEncoder(
bool hardware_acceleration) {
if (hardware_acceleration) {
return new NvidiaVideoEncoder();
return std::make_unique<NvidiaVideoEncoder>(NvidiaVideoEncoder());
} else {
return new FFmpegVideoEncoder();
return std::make_unique<FFmpegVideoEncoder>(FFmpegVideoEncoder());
}
}

View File

@@ -7,10 +7,8 @@ class VideoEncoderFactory {
VideoEncoderFactory();
~VideoEncoderFactory();
static VideoEncoder *CreateVideoEncoder(bool hardware_acceleration);
private:
bool hardware_acceleration_ = false;
static std::unique_ptr<VideoEncoder> CreateVideoEncoder(
bool hardware_acceleration);
};
#endif