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. 记一次sparkOnyarn错误:java.lang.UnsatisfiedLinkError

    错误大概这样: Caused by: java.util.concurrent.ExecutionException: Boxed Error Caused by: java.lang.Unsatis ...

  2. Zookeeper 安装及命令行操作

    [参考文章]:[分布式]Zookeeper使用--命令行 [参考文章]:zookeeper的数据模型 [参考文章]:zookeeper ACL使用 1. 安装包下载 官方下载地址 选择一个具体的版本进 ...

  3. 邻居子系统 之 邻居表的初始化neigh_table_init

    概述 邻居子系统支持多种实现,例如ARP,ND等,这些实现需要在其初始化的时候,调用neigh_table_init将邻居表项添加到全局邻居子系统数组中,并对实例中的字段(如hash,定时器等)进行相 ...

  4. Redis内存碎片率

    一. 内存碎片率mem_fragmentation_ratio = used_memory_rss / used_memoryused_memory :Redis使用其分配器分配的内存大小used_m ...

  5. Linux安全工具之fail2ban防爆力破解

    一:简单介绍 fail2ban是一款实用软件,可以监视你的系统日志,然后匹配日志的错误信息(正则式匹配)执行相应的屏蔽动作 在企业中,有些很多人会开放root登录,这样就有机会给黑客造成暴力破解的机会 ...

  6. vmalloc详解

    vmalloc是一个接口函数, 内核代码使用它来分配在虚拟内存中连续但在物理内存中不一定连续的内存. 只需要一个参数,以字节为单位. 使用vmalloc的最著名的实例是内核对模块的实现. 因为模块可能 ...

  7. LC 562. Longest Line of Consecutive One in Matrix

    Given a 01 matrix M, find the longest line of consecutive one in the matrix. The line could be horiz ...

  8. java web 开发三剑客 -------电子书

    Internet,人们通常称为因特网,是当今世界上覆盖面最大和应用最广泛的网络.根据英语构词法,Internet是Inter + net,Inter-作为前缀在英语中表示“在一起,交互”,由此可知In ...

  9. CSS 浮动 float 属性

    浮动的框可以向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止. 由于浮动框不在文档的普通流中,所以文档的普通流中的块框表现得就像浮动框不存在一样. 请看下图,当把框 1 向右浮动时,它 ...

  10. php中应用redis

    下载软件包wget https://codeload.github.com/phpredis/phpredis/zip/develop mv develop phpredis.zip 解压unzip ...