[feat] use VideoFrameWrapper to store frame info

This commit is contained in:
dijunkun
2025-02-21 16:13:31 +08:00
parent f797cc3f91
commit a39d0f6652
15 changed files with 150 additions and 55 deletions

View File

@@ -266,10 +266,10 @@ int AomAv1Encoder::Init() {
return 0;
}
int AomAv1Encoder::Encode(const XVideoFrame *video_frame,
std::function<int(char *encoded_packets, size_t size,
VideoFrameType frame_type)>
on_encoded_image) {
int AomAv1Encoder::Encode(
const XVideoFrame *video_frame,
std::function<int(std::shared_ptr<VideoFrameWrapper> encoded_frame)>
on_encoded_image) {
#ifdef SAVE_RECEIVED_NV12_STREAM
fwrite(video_frame->data, 1, video_frame->size, file_nv12_);
#endif
@@ -342,8 +342,15 @@ int AomAv1Encoder::Encode(const XVideoFrame *video_frame,
// LOG_INFO("Encoded frame qp = {}", qp);
if (on_encoded_image) {
on_encoded_image((char *)encoded_frame_, encoded_frame_size_,
frame_type);
std::shared_ptr<VideoFrameWrapper> encoded_frame =
std::make_shared<VideoFrameWrapper>(
encoded_frame_, encoded_frame_size_, video_frame->width,
video_frame->height);
encoded_frame->SetFrameType(frame_type);
encoded_frame->SetCaptureTimestamp(video_frame->timestamp);
encoded_frame->SetEncodedWidth(video_frame->width);
encoded_frame->SetEncodedHeight(video_frame->height);
on_encoded_image(encoded_frame);
#ifdef SAVE_ENCODED_AV1_STREAM
fwrite(encoded_frame_, 1, encoded_frame_size_, file_av1_);
#endif

View File

@@ -37,10 +37,10 @@ class AomAv1Encoder : public VideoEncoder {
public:
int Init();
int Encode(const XVideoFrame* video_frame,
std::function<int(char* encoded_packets, size_t size,
VideoFrameType frame_type)>
on_encoded_image);
int Encode(
const XVideoFrame* video_frame,
std::function<int(std::shared_ptr<VideoFrameWrapper> encoded_frame)>
on_encoded_image);
int ForceIdr();

View File

@@ -130,8 +130,7 @@ int NvidiaVideoEncoder::Init() {
int NvidiaVideoEncoder::Encode(
const XVideoFrame *video_frame,
std::function<int(char *encoded_packets, size_t size,
VideoFrameType frame_type)>
std::function<int(std::shared_ptr<VideoFrameWrapper> encoded_frame)>
on_encoded_image) {
if (!encoder_) {
LOG_ERROR("Invalid encoder");
@@ -182,7 +181,15 @@ int NvidiaVideoEncoder::Encode(
for (const auto &packet : encoded_packets_) {
if (on_encoded_image) {
on_encoded_image((char *)packet.data(), packet.size(), frame_type);
std::shared_ptr<VideoFrameWrapper> encoded_frame =
std::make_shared<VideoFrameWrapper>(packet.data(), packet.size(),
encoder_->GetEncodeWidth(),
encoder_->GetEncodeHeight());
encoded_frame->SetFrameType(frame_type);
encoded_frame->SetCaptureTimestamp(video_frame->timestamp);
encoded_frame->SetEncodedWidth(encoder_->GetEncodeWidth());
encoded_frame->SetEncodedHeight(encoder_->GetEncodeHeight());
on_encoded_image(encoded_frame);
#ifdef SAVE_ENCODED_H264_STREAM
fwrite((unsigned char *)packet.data(), 1, packet.size(), file_h264_);
#endif

View File

@@ -13,10 +13,10 @@ class NvidiaVideoEncoder : public VideoEncoder {
int Init();
int Encode(const XVideoFrame* video_frame,
std::function<int(char* encoded_packets, size_t size,
VideoFrameType frame_type)>
on_encoded_image);
int Encode(
const XVideoFrame* video_frame,
std::function<int(std::shared_ptr<VideoFrameWrapper> encoded_frame)>
on_encoded_image);
int ForceIdr();

View File

@@ -181,8 +181,7 @@ int OpenH264Encoder::Init() {
int OpenH264Encoder::Encode(
const XVideoFrame *video_frame,
std::function<int(char *encoded_packets, size_t size,
VideoFrameType frame_type)>
std::function<int(std::shared_ptr<VideoFrameWrapper> encoded_frame)>
on_encoded_image) {
if (!openh264_encoder_) {
LOG_ERROR("Invalid openh264 encoder");
@@ -282,7 +281,15 @@ int OpenH264Encoder::Encode(
encoded_frame_size_ = encoded_frame_size;
if (on_encoded_image) {
on_encoded_image((char *)encoded_frame_, encoded_frame_size_, frame_type);
std::shared_ptr<VideoFrameWrapper> encoded_frame =
std::make_shared<VideoFrameWrapper>(encoded_frame_, encoded_frame_size_,
raw_frame_.iPicWidth,
raw_frame_.iPicHeight);
encoded_frame->SetFrameType(frame_type);
encoded_frame->SetCaptureTimestamp(video_frame->timestamp);
encoded_frame->SetEncodedWidth(raw_frame_.iPicWidth);
encoded_frame->SetEncodedHeight(raw_frame_.iPicHeight);
on_encoded_image(encoded_frame);
#ifdef SAVE_ENCODED_H264_STREAM
fwrite(encoded_frame_, 1, encoded_frame_size_, file_h264_);
#endif

View File

@@ -24,10 +24,10 @@ class OpenH264Encoder : public VideoEncoder {
int Init();
int Encode(const XVideoFrame* video_frame,
std::function<int(char* encoded_packets, size_t size,
VideoFrameType frame_type)>
on_encoded_image);
int Encode(
const XVideoFrame* video_frame,
std::function<int(std::shared_ptr<VideoFrameWrapper> encoded_frame)>
on_encoded_image);
int ForceIdr();

View File

@@ -5,25 +5,20 @@
#include <cstdint>
#include <cstdio>
#include <functional>
#include <memory>
#include <string>
#include "video_frame_wrapper.h"
#include "x.h"
class VideoEncoder {
public:
enum VideoFrameType {
kEmptyFrame = 0,
kVideoFrameKey = 3,
kVideoFrameDelta = 4,
};
public:
virtual int Init() = 0;
virtual int Encode(const XVideoFrame* video_frame,
std::function<int(char* encoded_packets, size_t size,
VideoFrameType frame_type)>
on_encoded_image) = 0;
virtual int Encode(
const XVideoFrame* video_frame,
std::function<int(std::shared_ptr<VideoFrameWrapper> encoded_frame)>
on_encoded_image) = 0;
virtual int ForceIdr() = 0;