| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- #ifndef AV_BASE_FRAME_WRAPPER_H
- #define AV_BASE_FRAME_WRAPPER_H
- #include "types.h"
- #include <memory>
- #include <functional>
- extern "C" {
- #include <libavutil/frame.h>
- #include <libavutil/avutil.h>
- }
- namespace av {
- /**
- * 安全的AVFrame包装器类
- * 提供比AVFramePtr更安全和易用的接口
- * 解决了智能指针在某些场景下的复杂性问题
- */
- class FrameWrapper {
- public:
- // 默认构造函数
- FrameWrapper() = default;
-
- // 从原生指针构造(接管所有权)
- explicit FrameWrapper(AVFrame* frame) : frame_(frame) {}
-
- // 从智能指针构造
- explicit FrameWrapper(AVFramePtr frame) : frame_(frame.release()) {}
-
- // 移动构造函数
- FrameWrapper(FrameWrapper&& other) noexcept
- : frame_(other.frame_) {
- other.frame_ = nullptr;
- }
-
- // 移动赋值操作符
- FrameWrapper& operator=(FrameWrapper&& other) noexcept {
- if (this != &other) {
- reset();
- frame_ = other.frame_;
- other.frame_ = nullptr;
- }
- return *this;
- }
-
- // 禁用拷贝构造和拷贝赋值
- FrameWrapper(const FrameWrapper&) = delete;
- FrameWrapper& operator=(const FrameWrapper&) = delete;
-
- // 析构函数
- ~FrameWrapper() {
- reset();
- }
-
- // 创建新帧
- static FrameWrapper create() {
- return FrameWrapper(av_frame_alloc());
- }
-
- // 创建视频帧
- static FrameWrapper createVideo(int width, int height, AVPixelFormat format) {
- AVFrame* frame = av_frame_alloc();
- if (!frame) return FrameWrapper();
-
- frame->width = width;
- frame->height = height;
- frame->format = format;
-
- if (av_frame_get_buffer(frame, 32) < 0) {
- av_frame_free(&frame);
- return FrameWrapper();
- }
-
- return FrameWrapper(frame);
- }
-
- // 创建音频帧
- static FrameWrapper createAudio(int sampleRate, int channels,
- AVSampleFormat format, int nbSamples) {
- AVFrame* frame = av_frame_alloc();
- if (!frame) return FrameWrapper();
-
- frame->sample_rate = sampleRate;
- frame->ch_layout.nb_channels = channels;
- frame->format = format;
- frame->nb_samples = nbSamples;
-
- if (av_frame_get_buffer(frame, 0) < 0) {
- av_frame_free(&frame);
- return FrameWrapper();
- }
-
- return FrameWrapper(frame);
- }
-
- // 获取原生指针(不转移所有权)
- AVFrame* get() const { return frame_; }
-
- // 获取原生指针并转移所有权
- AVFrame* release() {
- AVFrame* temp = frame_;
- frame_ = nullptr;
- return temp;
- }
-
- // 重置,释放当前帧
- void reset(AVFrame* newFrame = nullptr) {
- if (frame_) {
- av_frame_free(&frame_);
- }
- frame_ = newFrame;
- }
-
- // 检查是否有效
- bool isValid() const { return frame_ != nullptr; }
-
- // 操作符重载
- operator bool() const { return isValid(); }
- AVFrame* operator->() const { return frame_; }
- AVFrame& operator*() const { return *frame_; }
-
- // 克隆帧(深拷贝)
- FrameWrapper clone() const {
- if (!frame_) return FrameWrapper();
-
- AVFrame* newFrame = av_frame_alloc();
- if (!newFrame) return FrameWrapper();
-
- if (av_frame_ref(newFrame, frame_) < 0) {
- av_frame_free(&newFrame);
- return FrameWrapper();
- }
-
- return FrameWrapper(newFrame);
- }
-
- // 创建引用(浅拷贝)
- FrameWrapper ref() const {
- if (!frame_) return FrameWrapper();
-
- AVFrame* newFrame = av_frame_alloc();
- if (!newFrame) return FrameWrapper();
-
- if (av_frame_ref(newFrame, frame_) < 0) {
- av_frame_free(&newFrame);
- return FrameWrapper();
- }
-
- return FrameWrapper(newFrame);
- }
-
- // 便捷的属性访问
- int64_t pts() const { return frame_ ? frame_->pts : AV_NOPTS_VALUE; }
- void setPts(int64_t pts) { if (frame_) frame_->pts = pts; }
-
- int width() const { return frame_ ? frame_->width : 0; }
- int height() const { return frame_ ? frame_->height : 0; }
- int sampleRate() const { return frame_ ? frame_->sample_rate : 0; }
- int channels() const { return frame_ ? frame_->ch_layout.nb_channels : 0; }
- int nbSamples() const { return frame_ ? frame_->nb_samples : 0; }
-
- AVPixelFormat pixelFormat() const {
- return frame_ ? static_cast<AVPixelFormat>(frame_->format) : AV_PIX_FMT_NONE;
- }
-
- AVSampleFormat sampleFormat() const {
- return frame_ ? static_cast<AVSampleFormat>(frame_->format) : AV_SAMPLE_FMT_NONE;
- }
-
- bool isKeyFrame() const { return frame_ && frame_->key_frame; }
-
- // 转换为智能指针(转移所有权)
- AVFramePtr toSmartPtr() {
- return AVFramePtr(release());
- }
- private:
- AVFrame* frame_ = nullptr;
- };
- // 便捷的类型定义
- using Frame = FrameWrapper;
- // 回调函数类型定义(使用 FrameWrapper)
- using FrameWrapperCallback = std::function<void(const FrameWrapper&)>;
- } // namespace av
- #endif // AV_BASE_FRAME_WRAPPER_H
|