Github

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

CDecode.h

/*******************************************************************
* Copyright(c) 2019
* All rights reserved.
*
* 文件名称: CDecode.h
* 简要描述: 解码
*
* 作者: gongluck
* 说明:
*
*******************************************************************/ #ifndef __CDECODE_H__
#define __CDECODE_H__ #ifdef __cplusplus
extern "C"
{
#endif #include <libavcodec/avcodec.h> #ifdef __cplusplus
}
#endif #include <string>
#include <mutex> class CDecode
{
public:
virtual ~CDecode();
// 解码帧回调声明
typedef void (*DecFrameCallback)(const AVFrame* frame, void* param); // 设置解码帧回调
bool set_dec_callback(DecFrameCallback cb, void* param, std::string& err); // 设置硬解
bool set_hwdec_type(AVHWDeviceType hwtype, bool trans, std::string& err); // 设置解码器
bool set_codeid(AVCodecID id, std::string& err);
bool copy_param(const AVCodecParameters* par, std::string& err); // 打开解码器
bool codec_open(std::string& err); // 解码
bool decode(const AVPacket* packet, std::string& err);
bool decode(const void* data, uint32_t size, std::string& err); // 清理资源
bool clean_opt(std::string& err); private:
std::recursive_mutex mutex_; DecFrameCallback decframecb_ = nullptr;
void* decframecbparam_ = nullptr; //ffmpeg
AVCodecContext* codectx_ = nullptr;
AVCodec* codec_ = nullptr;
AVCodecParserContext* par_ = nullptr;
AVHWDeviceType hwtype_ = AV_HWDEVICE_TYPE_NONE;
AVPixelFormat hwfmt_ = AV_PIX_FMT_NONE;
AVPacket pkt_;
bool trans_ = false;
}; #endif//__CDECODE_H__

CDecode.cpp

/*******************************************************************
* Copyright(c) 2019
* All rights reserved.
*
* 文件名称: CDecode.cpp
* 简要描述: 解码
*
* 作者: gongluck
* 说明:
*
*******************************************************************/ #include "common.h"
#include "CDecode.h" CDecode::~CDecode()
{
std::string err;
clean_opt(err);
} bool CDecode::set_dec_callback(DecFrameCallback cb, void* param, std::string& err)
{
LOCK();
err = "opt succeed."; decframecb_ = cb;
decframecbparam_ = param; return true;
} bool CDecode::set_hwdec_type(AVHWDeviceType hwtype, bool trans, std::string& err)
{
LOCK();
err = "opt succeed."; hwtype_ = hwtype;
trans_ = trans; return true;
} bool CDecode::set_codeid(AVCodecID id, std::string& err)
{
LOCK();
err = "opt succeed.";
int ret; if (!clean_opt(err))
{
return false;
} do
{
codec_ = avcodec_find_decoder(id);
if (codec_ == nullptr)
{
err = "avcodec_find_decoder return nullptr";
break;
}
codectx_ = avcodec_alloc_context3(codec_);
if (codectx_ == nullptr)
{
err = "avcodec_alloc_context3 return nullptr";
break;
}
par_ = av_parser_init(codec_->id);
if (par_ == nullptr)
{
err = "av_parser_init return nullptr";
//break;
} if (hwtype_ != AV_HWDEVICE_TYPE_NONE)
{
// 查询硬解码支持
for (int i = 0;; i++)
{
const AVCodecHWConfig* config = avcodec_get_hw_config(codec_, i);
if (config == nullptr)
{
err = codec_->name + std::string(" not support ") + av_hwdevice_get_type_name(hwtype_);
break;
}
if (config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
config->device_type == hwtype_)
{
// 硬解上下文
AVBufferRef* hwbufref = nullptr;
ret = av_hwdevice_ctx_create(&hwbufref, hwtype_, nullptr, nullptr, 0);
if (ret < 0)
{
err = av_err2str(ret);
break;
}
else
{
codectx_->hw_device_ctx = av_buffer_ref(hwbufref);
if (codectx_->hw_device_ctx == nullptr)
{
err = "av_buffer_ref(hwbufref) return nullptr.";
break;
}
av_buffer_unref(&hwbufref);
hwfmt_ = config->pix_fmt;
return true;
}
}
}
}
return true;
} while (true); std::string e;
clean_opt(e);
return false;
} bool CDecode::copy_param(const AVCodecParameters* par, std::string& err)
{
LOCK();
err = "opt succeed.";
int ret = 0; if (par == nullptr)
{
err = "par is nullptr";
return false;
}
if (!set_codeid(par->codec_id, err))
{
return false;
} ret = avcodec_parameters_to_context(codectx_, par);
if (ret < 0)
{
clean_opt(err);
err = av_err2str(ret);
return false;
} return true;
} bool CDecode::codec_open(std::string& err)
{
LOCK();
err = "opt succeed.";
int ret = 0; if (codectx_ == nullptr || codec_ == nullptr)
{
err = "codectx_ is nullptr or codec_ is nullptr";
return false;
} ret = avcodec_open2(codectx_, codec_, nullptr);
if (ret < 0)
{
err = av_err2str(ret);
return false;
} return true;
} bool CDecode::decode(const AVPacket* packet, std::string& err)
{
LOCK();
err = "opt succeed.";
int ret = 0; if (packet == nullptr)
{
err == "packet is nullptr.";
return false;
}
else if (codectx_ == nullptr)
{
err = "codectx_ is nullptr.";
return false;
} // 发送将要解码的数据
ret = avcodec_send_packet(codectx_, packet);
CHECKFFRET(ret); AVFrame* frame = av_frame_alloc();
AVFrame* traframe = av_frame_alloc();
if (frame == nullptr || traframe == nullptr)
{
err = "av_frame_alloc() return nullptr.";
av_frame_free(&frame);
av_frame_free(&traframe);
return false;
} while (ret >= 0)
{
// 接收解码数据
ret = avcodec_receive_frame(codectx_, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
{
// 不完整或者EOF
err = av_err2str(ret);
break;
}
else if (ret < 0)
{
// 其他错误
err = av_err2str(ret);
break;
}
else
{
// 得到解码数据
if (decframecb_ != nullptr)
{
if (hwtype_ != AV_HWDEVICE_TYPE_NONE // 使用硬解
&& frame->format == hwfmt_ // 硬解格式
&& trans_ // 显卡->内存转换
)
{
ret = av_hwframe_transfer_data(traframe, frame, 0);
if (ret < 0)
{
err = av_err2str(ret);
break;
}
else
{
traframe->pts = frame->pts;
traframe->pkt_dts = frame->pkt_dts;
traframe->pkt_duration = frame->pkt_duration;
decframecb_(traframe, decframecbparam_);
}
}
else
{
decframecb_(frame, decframecbparam_);
}
}
// 这里没有直接break,是因为存在再次调用avcodec_receive_frame能拿到新数据的可能
}
} av_frame_free(&frame);
av_frame_free(&traframe);
return true;
} bool CDecode::decode(const void* data, uint32_t size, std::string& err)
{
LOCK();
err = "opt succeed.";
int ret = 0; int pos = 0;
while (size > 0)
{
ret = av_parser_parse2(par_, codectx_, &pkt_.data, &pkt_.size, static_cast<const uint8_t*>(data)+pos, size, AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
CHECKFFRET(ret);
pos += ret;
size -= ret; if (pkt_.size > 0)
{
ret = decode(&pkt_, err);
CHECKFFRET(ret);
}
} return true;
} bool CDecode::clean_opt(std::string& err)
{
LOCK();
err = "opt succeed."; codec_ = nullptr;
av_parser_close(par_);
avcodec_free_context(&codectx_); return true;
}

测试

// 解码h264
void test_decode_h264()
{
bool ret = false;
std::string err;
std::ifstream h264("in.h264", std::ios::binary);
char buf[1024] = { 0 };
CDecode decode; ret = decode.set_dec_callback(DecVideoFrameCB, &decode, err);
TESTCHECKRET(ret);
//ret = decode.set_hwdec_type(AV_HWDEVICE_TYPE_DXVA2, true, err);
//TESTCHECKRET(ret);
ret = decode.set_codeid(AV_CODEC_ID_H264, err);
TESTCHECKRET(ret);
ret = decode.codec_open(err);
TESTCHECKRET(ret); while (!h264.eof())
{
h264.read(buf, sizeof(buf));
ret = decode.decode(buf, sizeof(buf), err);
TESTCHECKRET(ret);
}
} // 解码aac
void test_decode_aac()
{
bool ret = false;
std::string err;
std::ifstream aac("in.aac", std::ios::binary);
char buf[1024] = { 0 };
CDecode decode; ret = decode.set_dec_callback(DecAudioFrameCB, &decode, err);
TESTCHECKRET(ret);
ret = decode.set_codeid(AV_CODEC_ID_AAC, err);
TESTCHECKRET(ret);
ret = decode.codec_open(err);
TESTCHECKRET(ret); while (!aac.eof())
{
aac.read(buf, sizeof(buf));
ret = decode.decode(buf, sizeof(buf), err);
TESTCHECKRET(ret);
}
} // 解码mp3
void test_decode_mp3()
{
bool ret = false;
std::string err;
std::ifstream mp3("in.mp3", std::ios::binary);
char buf[1024] = { 0 };
CDecode decode; ret = decode.set_dec_callback(DecAudioFrameCB, &decode, err);
TESTCHECKRET(ret);
ret = decode.set_codeid(AV_CODEC_ID_MP3, err);
TESTCHECKRET(ret);
ret = decode.codec_open(err);
TESTCHECKRET(ret); while (!mp3.eof())
{
mp3.read(buf, sizeof(buf));
ret = decode.decode(buf, sizeof(buf), err);
TESTCHECKRET(ret);
}
}

FFmpeg4.0笔记:封装ffmpeg的解码功能类CDecode的更多相关文章

  1. FFmpeg4.0笔记:封装ffmpeg的解封装功能类CDemux

    Github https://github.com/gongluck/FFmpeg4.0-study/tree/master/Cff CDemux.h /*********************** ...

  2. FFmpeg4.0笔记:封装ffmpeg的视频帧转换功能类CSws

    Github https://github.com/gongluck/FFmpeg4.0-study/tree/master/Cff CSws.h /************************* ...

  3. FFmpeg4.0笔记:封装ffmpeg的音频重采样功能类CSwr

    Github https://github.com/gongluck/FFmpeg4.0-study/tree/master/Cff CSwr.h /************************* ...

  4. FFmpeg4.0笔记:本地媒体文件解码、帧格式转换、重采样、编码、封装、转封装、avio、硬解码等例子

    Github https://github.com/gongluck/FFmpeg4.0-study/blob/master/official%20example/my_example.cpp #in ...

  5. FFmpeg4.0笔记:rtsp2rtmp

    Github https://github.com/gongluck/FFmpeg4.0-study.git #include <iostream> using namespace std ...

  6. FFmpeg4.0笔记:file2rtmp

    Github: https://github.com/gongluck/FFmpeg4.0-study.git #include <iostream> using namespace st ...

  7. UWP笔记-使用FFmpeg编解码

    在开发UWP媒体应用的时候,使用的MediaElement可以支持主流的格式,不过还是有些格式本地编解码器是不支持的,如.flv..rmvb等,这里讲到的是第三方开源库FFmpeg,可以直接播放更多的 ...

  8. FFmpeg4.0笔记:VS2019编译FFmpeg4.0源码

    0.下载TDM.msys和yasm 1.安装TDM-GCC-64 2.安装msys到TDM-GCC的安装目录中 3.将call "C:\Program Files (x86)\Microso ...

  9. FFmpeg4.0笔记:采集系统声音

    Github https://github.com/gongluck/FFmpeg4.0-study/tree/master/Cff // 采集系统声音 void test_systemsound() ...

随机推荐

  1. Codeforces 437D The Child and Zoo(并查集)

    Codeforces 437D The Child and Zoo 题目大意: 有一张连通图,每个点有对应的值.定义从p点走向q点的其中一条路径的花费为途径点的最小值.定义f(p,q)为从点p走向点q ...

  2. MySQL的安装教程

    一.MYSQL的安装 首先登入官网下载mysql的安装包,官网地址:https://dev.mysql.com/downloads/mysql/ 一般下载这个就好,现在的最新版本是5.8,但是据说已经 ...

  3. 关于一次同余方程的一类解法(exgcd,CRT,exCRT)

    1.解同余方程: 同余方程可以转化为不定方程,其实就是,这样的问题一般用拓展欧几里德算法求解. LL exgcd(LL a,LL b,LL &x,LL &y){ if(!b){ x=; ...

  4. Redis内存满了的几种解决方法(内存淘汰策略与Redis集群)

    1,增加内存: 2,使用内存淘汰策略. 3,Redis集群. 重点介绍下23: 第2点: 我们知道,redis设置配置文件的maxmemory参数,可以控制其最大可用内存大小(字节). 那么当所需内存 ...

  5. react-redux 的总结

    第一步,我们将我们要使用的插件来先一步进行安装 create-react-app app  // 在这里我们使用了 react 的脚手架来搭建的项目 cd app // 进入我们的项目 npm i - ...

  6. android 知识体系

  7. WebStrom编程小技巧--HTML快速创建指定id或者类名的div

    打印div标签快速方法:“先打出#yz,然后Tab键补全即可获得<div id="yz"></div>同理:我们也可以先打出“.tz"然后Tab键 ...

  8. SLC-Microsoft:Microsoft Lifecycle Policy

    ylbtech-SLC-Microsoft:Microsoft Lifecycle Policy Microsoft Lifecycle Policy The Microsoft Lifecycle ...

  9. ControlTemplate in WPF —— Slider

    <!--Slider 样式--> <Style x:Key="StyleForRepeatButton" TargetType="{x:Type Rep ...

  10. idea中git远程版本回退

    idea中git远程版本回退 2017年10月15日 15:25:36 gomeplus 阅读数:19313 工作中遇到git远程仓库需要回退到历史版本的问题,根据网上的搜索结果结合自己的实践,整理了 ...