Use factory to create encoder/decoder

This commit is contained in:
dijunkun
2023-09-20 17:44:29 +08:00
parent 3d4e1effe9
commit a794cd43b9
17 changed files with 173 additions and 57 deletions

View File

@@ -1,4 +1,4 @@
#include "ffmpeg_decoder.h"
#include "ffmpeg_video_decoder.h"
#include "log.h"
@@ -10,7 +10,7 @@ extern "C" {
#include <libswscale/swscale.h>
};
VideoDecoder::VideoDecoder() {
FfmpegVideoDecoder::FfmpegVideoDecoder() {
if (SAVE_ENCODER_STREAM) {
file_ = fopen("decode_stream.yuv", "w+b");
if (!file_) {
@@ -19,7 +19,7 @@ VideoDecoder::VideoDecoder() {
}
}
VideoDecoder::~VideoDecoder() {
FfmpegVideoDecoder::~FfmpegVideoDecoder() {
if (SAVE_ENCODER_STREAM && file_) {
fflush(file_);
fclose(file_);
@@ -38,7 +38,7 @@ VideoDecoder::~VideoDecoder() {
av_free(codec_ctx_);
}
int VideoDecoder::Init() {
int FfmpegVideoDecoder::Init() {
// av_log_set_level(AV_LOG_DEBUG);
codec_id_ = AV_CODEC_ID_H264;
@@ -80,7 +80,7 @@ int VideoDecoder::Init() {
return 0;
}
int VideoDecoder::Decode(
int FfmpegVideoDecoder::Decode(
const uint8_t *data, int size,
std::function<void(VideoFrame)> on_receive_decoded_frame) {
if (!first_) {

View File

@@ -1,5 +1,5 @@
#ifndef _FFMPEG_DECODER_H_
#define _FFMPEG_DECODER_H_
#ifndef _FFMPEG_VIDEO_DECODER_H_
#define _FFMPEG_VIDEO_DECODER_H_
#ifdef _WIN32
extern "C" {
@@ -17,12 +17,12 @@ extern "C" {
#include <functional>
#include "frame.h"
#include "video_decoder.h"
class VideoDecoder {
class FfmpegVideoDecoder : public VideoDecoder {
public:
VideoDecoder();
~VideoDecoder();
FfmpegVideoDecoder();
~FfmpegVideoDecoder();
public:
int Init();

View File

@@ -1,10 +1,10 @@
#include "nv_decoder.h"
#include "nvidia_video_decoder.h"
#include "log.h"
#define SAVE_ENCODER_STREAM 0
VideoDecoder::VideoDecoder() {
NvidiaVideoDecoder::NvidiaVideoDecoder() {
if (SAVE_ENCODER_STREAM) {
file_ = fopen("decode_stream.h264", "w+b");
if (!file_) {
@@ -12,7 +12,7 @@ VideoDecoder::VideoDecoder() {
}
}
}
VideoDecoder::~VideoDecoder() {
NvidiaVideoDecoder::~NvidiaVideoDecoder() {
if (SAVE_ENCODER_STREAM && file_) {
fflush(file_);
fclose(file_);
@@ -20,7 +20,7 @@ VideoDecoder::~VideoDecoder() {
}
}
int VideoDecoder::Init() {
int NvidiaVideoDecoder::Init() {
ck(cuInit(0));
int nGpu = 0;
int iGpu = 0;
@@ -43,7 +43,7 @@ int VideoDecoder::Init() {
return 0;
}
int VideoDecoder::Decode(
int NvidiaVideoDecoder::Decode(
const uint8_t *data, int size,
std::function<void(VideoFrame)> on_receive_decoded_frame) {
if (!decoder) {

View File

@@ -1,15 +1,15 @@
#ifndef _NV_DECODER_H_
#define _NV_DECODER_H_
#ifndef _NVIDIA_VIDEO_DECODER_H_
#define _NVIDIA_VIDEO_DECODER_H_
#include <functional>
#include "NvDecoder.h"
#include "frame.h"
#include "video_decoder.h"
class VideoDecoder {
class NvidiaVideoDecoder : public VideoDecoder {
public:
VideoDecoder();
~VideoDecoder();
NvidiaVideoDecoder();
~NvidiaVideoDecoder();
public:
int Init();

View File

@@ -0,0 +1,16 @@
#ifndef _VIDEO_DECODER_H_
#define _VIDEO_DECODER_H_
#include <functional>
#include "frame.h"
class VideoDecoder {
public:
virtual int Init() = 0;
virtual int Decode(
const uint8_t *data, int size,
std::function<void(VideoFrame)> on_receive_decoded_frame) = 0;
};
#endif

View File

@@ -0,0 +1,17 @@
#include "video_decoder_factory.h"
#include "ffmpeg/ffmpeg_video_decoder.h"
#include "nvcodec/nvidia_video_decoder.h"
VideoDecoderFactory::VideoDecoderFactory() {}
VideoDecoderFactory::~VideoDecoderFactory() {}
VideoDecoder *VideoDecoderFactory::CreateVideoDecoder(
bool hardware_acceleration) {
if (hardware_acceleration) {
return new NvidiaVideoDecoder();
} else {
return new FfmpegVideoDecoder();
}
}

View File

@@ -0,0 +1,16 @@
#ifndef _VIDEO_DECODER_FACTORY_H_
#define _VIDEO_DECODER_FACTORY_H_
#include "video_decoder.h"
class VideoDecoderFactory {
public:
VideoDecoderFactory();
~VideoDecoderFactory();
static VideoDecoder *CreateVideoDecoder(bool hardware_acceleration);
private:
bool hardware_acceleration_ = false;
};
#endif