ZLMediaKit/tests/FFMpegDecoder.h

97 lines
2.8 KiB
C++
Raw Normal View History

2017-10-09 22:11:01 +08:00
/*
2020-04-04 20:30:09 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
2017-09-27 16:20:30 +08:00
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
2017-09-27 16:20:30 +08:00
*
2020-04-04 20:30:09 +08:00
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
2017-05-08 18:03:43 +08:00
*/
#ifndef H264Decoder_H_
#define H264Decoder_H_
#include <string>
#include <memory>
#include <stdexcept>
2020-04-30 12:52:13 +08:00
#include "Extension/Frame.h"
2017-05-08 18:03:43 +08:00
#ifdef __cplusplus
extern "C" {
#endif
//#include "libavutil/mathematics.h"
#include "libavcodec/avcodec.h"
//#include "libswscale/swscale.h"
#ifdef __cplusplus
}
#endif
using namespace std;
2018-10-24 17:17:55 +08:00
namespace mediakit {
2017-05-08 18:03:43 +08:00
2020-04-30 12:52:13 +08:00
class FFMpegDecoder{
2017-05-08 18:03:43 +08:00
public:
2020-04-30 12:52:13 +08:00
FFMpegDecoder(int codec_id){
auto ff_codec_id = AV_CODEC_ID_H264;
switch (codec_id){
case CodecH264:
ff_codec_id = AV_CODEC_ID_H264;
break;
case CodecH265:
ff_codec_id = AV_CODEC_ID_H265;
break;
default:
throw std::invalid_argument("不支持该编码格式");
}
2020-03-20 11:51:24 +08:00
avcodec_register_all();
2020-04-30 12:52:13 +08:00
AVCodec *pCodec = avcodec_find_decoder(ff_codec_id);
2020-03-20 11:51:24 +08:00
if (!pCodec) {
2020-04-30 12:52:13 +08:00
throw std::runtime_error("未找到解码器");
2020-03-20 11:51:24 +08:00
}
m_pContext.reset(avcodec_alloc_context3(pCodec), [](AVCodecContext *pCtx) {
avcodec_close(pCtx);
avcodec_free_context(&pCtx);
});
if (!m_pContext) {
throw std::runtime_error("创建解码器失败");
}
if (pCodec->capabilities & AV_CODEC_CAP_TRUNCATED) {
/* we do not send complete frames */
m_pContext->flags |= AV_CODEC_FLAG_TRUNCATED;
}
if(avcodec_open2(m_pContext.get(), pCodec, NULL)< 0){
throw std::runtime_error("打开编码器失败");
}
m_pFrame.reset(av_frame_alloc(),[](AVFrame *pFrame){
av_frame_free(&pFrame);
});
if (!m_pFrame) {
throw std::runtime_error("创建帧缓存失败");
}
}
2020-04-30 12:52:13 +08:00
virtual ~FFMpegDecoder(void){}
2020-03-20 11:51:24 +08:00
bool inputVideo(unsigned char* data,unsigned int dataSize,uint32_t ui32Stamp,AVFrame **ppFrame){
AVPacket pkt;
av_init_packet(&pkt);
pkt.data = data;
pkt.size = dataSize;
pkt.dts = ui32Stamp;
int iGotPicture ;
auto iLen = avcodec_decode_video2(m_pContext.get(), m_pFrame.get(), &iGotPicture, &pkt);
if (!iGotPicture || iLen < 0) {
return false;
}
*ppFrame = m_pFrame.get();
return true;
}
2017-05-08 18:03:43 +08:00
private:
2020-03-20 11:51:24 +08:00
std::shared_ptr<AVCodecContext> m_pContext;
std::shared_ptr<AVFrame> m_pFrame;
2017-05-08 18:03:43 +08:00
};
2018-10-24 17:17:55 +08:00
} /* namespace mediakit */
2017-05-08 18:03:43 +08:00
#endif /* H264Decoder_H_ */