设为首页 加入收藏

TOP

FFmpeg4.0笔记:封装ffmpeg的视频帧转换功能类CSws(一)
2019-06-14 12:07:56 】 浏览:100
Tags:FFmpeg4.0 笔记 封装 ffmpeg 视频 转换 功能 CSws

Github

https://github.com/gongluck/FFmpeg4.0-study/tree/master/Cff

CSws.h

#ifndef __CSWS_H__
#define __CSWS_H__

#ifdef __cplusplus
extern "C"
{
#endif

#include <libswscale/swscale.h>
#include <libavutil/imgutils.h> // av_image_alloc


#ifdef __cplusplus
}
#endif

#include <string>
#include <mutex>

class CSws
{
public:
    ~CSws();

    // 状态
    enum STATUS { STOP, LOCKED };
    // 设置源参数
    bool set_src_opt(AVPixelFormat pixfmt, int w, int h, std::string& err);
    // 设置目标参数
    bool set_dst_opt(AVPixelFormat pixfmt, int w, int h, std::string& err);
    // 锁定设置
    bool lock_opt(std::string& err);
    // 解除锁定
    bool unlock_opt(std::string& err);
    // 转换
    bool scale(const uint8_t* const srcSlice[], const int srcStride[], int srcSliceY, int srcSliceH, uint8_t* const dst[], const int dstStride[], std::string& err);

private:
    STATUS status_ = STOP;
    std::recursive_mutex mutex_;

    SwsContext* swsctx_ = nullptr;

    AVPixelFormat src_pix_fmt_ = AV_PIX_FMT_NONE;
    AVPixelFormat dst_pix_fmt_ = AV_PIX_FMT_NONE;
    int src_w_ = 0;
    int src_h_ = 0;
    int dst_w_ = 0;
    int dst_h_ = 0;
};

#endif//__CSWS_H__

CSws.cpp

#include "common.h"
#include "CSws.h"

CSws::~CSws()
{
    std::string err;
    unlock_opt(err);
}

bool CSws::set_src_opt(AVPixelFormat pixfmt, int w, int h, std::string& err)
{
    LOCK();
    CHECKSTOP(err);
    src_pix_fmt_ = pixfmt;
    src_w_ = w;
    src_h_ = h;
    return true;
}

bool CSws::set_dst_opt(AVPixelFormat pixfmt, int w, int h, std::string& err)
{
    LOCK();
    CHECKSTOP(err);
    dst_pix_fmt_ = pixfmt;
    dst_w_ = w;
    dst_h_ = h;
    return true;
}

bool CSws::lock_opt(std::string& err)
{
    LOCK();
    CHECKSTOP(err);
    swsctx_ = sws_getContext(src_w_, src_h_, src_pix_fmt_, dst_w_, dst_h_, dst_pix_fmt_, SWS_FAST_BILINEAR, nullptr, nullptr, nullptr);
    if (swsctx_ == nullptr)
    {
        err = "sws_getContext(src_w_, src_h_, src_pix_fmt_, dst_w_, dst_h_, dst_pix_fmt_, SWS_FAST_BILINEAR, nullptr, nullptr, nullptr) return nullptr.";
        return false;
    }
    status_ = LOCKED;
    return true;
}

bool CSws::unlock_opt(std::string& err)
{
    LOCK();
    sws_freeContext(swsctx_);
    swsctx_ = nullptr;
    status_ = STOP;
    src_w_ = 0;
    src_h_ = 0;
    dst_w_ = 0;
    dst_h_ = 0;
    return true;
}

bool CSws::scale(const uint8_t* const srcSlice[], const int srcStride[], int srcSliceY, int srcSliceH, uint8_t* const dst[], const int dstStride[], std::string& err)
{
    LOCK();
    CHECKNOTSTOP(err);
    int ret = sws_scale(swsctx_, srcSlice, srcStride, srcSliceY, srcSliceH, dst, dstStride);
    CHECKFFRET(ret);
    return true;
}

测试

#include "CDecode.h"
#include "CSws.h"
#include <iostream>
#include <fstream>

CSws g_sws;
uint8_t* g_pointers[4] = { 0 };
int g_linesizes[4] = { 0 };

void DecStatusCB(CDecode::STATUS status, std::string err, void* param)
{
    std::cout << std::this_thread::get_id() << " got a status " << status << std::endl;
}

void DecFrameCB(const AVFrame* frame, CDecode::FRAMETYPE frametype, void* param)
{
    //std::cout << std::this_thread::get_id() << " got a frame." << frametype << std::endl;
    if (frametype == CDecode::FRAMETYPE::VIDEO)
    {
        if (frame->format == AV_PIX_FMT_YUV420P)
        {
            static std::ofstream video("out.rgb", std::ios::binary | std::ios::
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇递推(一):递推法的基本思想 下一篇[WC2013] 糖果公园

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目