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. Linux编程之文件锁

    1. 使用 fcntl() 给记录加锁 使用 fcntl() 能够在一个文件的任意部分上放置一把锁,这个文件部分既可以是一个字节,也可以是整个文件.这种形式的文件加锁通常被称为记录加锁,但这种称谓是不 ...

  2. synchronized三种使用方式,及锁的类型验证

    Synchronized常用三种使用方式 1.修饰普通方法:锁对象即为当前对象 2.修饰静态方法:锁对象为当前Class对象 3.修饰代码块:锁对象为synchronized紧接着的小括号内的对象 一 ...

  3. centos7编译安装Python 3.6.8 后用pip3出现SSL未配置问题(import ssl失败)解决方法

    下载源码编译安装openssl https://www.openssl.org/source/openssl-1.0.2j.tar.gz ./config --prefix=/usr/local/op ...

  4. python 面对对象 类(继承, 多态)

    继承,继承其它实例化样本的属性和方法,需要在声明里重新定义和使用 class School(object): def __init__(self, name, addr): self.name = n ...

  5. Vue-2:官方教程学习

    1,先把下面这些内容都按照官方教程敲一遍,打好基础,类似于“前戏”,其作用我想爸爸就不必多说了吧(づ。◕‿‿◕。)づ. https://cn.vuejs.org/v2/guide/ 同时可以配合配套视 ...

  6. GitHub:Facebook

    ylbtech-GitHub:Facebook 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部 1. https://github.com/facebook 2. ...

  7. vim 软件

    1. 安装vim     2. 使用vim        操作模式:         1. 一般模式 ,默认进入的一般模式,该模式不能编辑文档 ,只能查看            按 i(insert) ...

  8. .NetCore接入Log4Net

    首先接入NuGet包Log4Net 在项目中添加log4net.config文件 Log4Net的级别None>FATAL>ERROR>WARN>INFO>DEBUG&g ...

  9. 电脑上不了网,但是能登录QQ 问题解决方案

    电脑上不了网,但是能登录QQ 问题解决方案 问题现象 个人电脑连上Wifi,打不开百度等网页,但是能登录QQ. 解决方案 1.打开命令提示对话框(Win+R) 2.输入ipconfig/display ...

  10. 通过TCODE查找SPRO路径

    1.SE11:CUS_ACTOBJ,根据OBJECTNAME(对象名称),即视图名称,获取Customizing activity(ACT_ID) 2.根据ACT_ID在表CUS_IMGACT获取说明 ...