支持265解码

This commit is contained in:
xiongziliang 2020-04-30 12:52:13 +08:00
parent 787081eb0c
commit 3ad1fe4924
2 changed files with 25 additions and 18 deletions

View File

@ -13,6 +13,7 @@
#include <string> #include <string>
#include <memory> #include <memory>
#include <stdexcept> #include <stdexcept>
#include "Extension/Frame.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -27,14 +28,24 @@ using namespace std;
namespace mediakit { namespace mediakit {
class H264Decoder class FFMpegDecoder{
{
public: public:
H264Decoder(void){ 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("不支持该编码格式");
}
avcodec_register_all(); avcodec_register_all();
AVCodec *pCodec = avcodec_find_decoder(AV_CODEC_ID_H264); AVCodec *pCodec = avcodec_find_decoder(ff_codec_id);
if (!pCodec) { if (!pCodec) {
throw std::runtime_error("未找到H264解码器"); throw std::runtime_error("未找到解码器");
} }
m_pContext.reset(avcodec_alloc_context3(pCodec), [](AVCodecContext *pCtx) { m_pContext.reset(avcodec_alloc_context3(pCodec), [](AVCodecContext *pCtx) {
avcodec_close(pCtx); avcodec_close(pCtx);
@ -57,7 +68,7 @@ public:
throw std::runtime_error("创建帧缓存失败"); throw std::runtime_error("创建帧缓存失败");
} }
} }
virtual ~H264Decoder(void){} virtual ~FFMpegDecoder(void){}
bool inputVideo(unsigned char* data,unsigned int dataSize,uint32_t ui32Stamp,AVFrame **ppFrame){ bool inputVideo(unsigned char* data,unsigned int dataSize,uint32_t ui32Stamp,AVFrame **ppFrame){
AVPacket pkt; AVPacket pkt;
av_init_packet(&pkt); av_init_packet(&pkt);

View File

@ -1,4 +1,4 @@
/* /*
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved. * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
* *
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit). * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
@ -12,13 +12,12 @@
#include "Util/util.h" #include "Util/util.h"
#include "Util/logger.h" #include "Util/logger.h"
#include <iostream> #include <iostream>
#include "Poller/EventPoller.h"
#include "Rtsp/UDPServer.h" #include "Rtsp/UDPServer.h"
#include "Player/MediaPlayer.h" #include "Player/MediaPlayer.h"
#include "Util/onceToken.h" #include "Util/onceToken.h"
#include "H264Decoder.h" #include "FFMpegDecoder.h"
#include "YuvDisplayer.h" #include "YuvDisplayer.h"
#include "Network/sockutil.h" #include "Extension/H265.h"
using namespace std; using namespace std;
using namespace toolkit; using namespace toolkit;
@ -111,8 +110,8 @@ int main(int argc, char *argv[]) {
} }
auto viedoTrack = strongPlayer->getTrack(TrackVideo); auto viedoTrack = strongPlayer->getTrack(TrackVideo);
if (!viedoTrack || viedoTrack->getCodecId() != CodecH264) { if (!viedoTrack) {
WarnL << "没有视频或者视频不是264编码!"; WarnL << "没有视频!";
return; return;
} }
@ -123,24 +122,21 @@ int main(int argc, char *argv[]) {
auto &displayer = (*storage)["displayer"]; auto &displayer = (*storage)["displayer"];
auto &merger = (*storage)["merger"]; auto &merger = (*storage)["merger"];
if(!decoder){ if(!decoder){
decoder.set<H264Decoder>(); decoder.set<FFMpegDecoder>(frame->getCodecId());
} }
if(!displayer){ if(!displayer){
displayer.set<YuvDisplayer>(nullptr,url); displayer.set<YuvDisplayer>(nullptr,url);
} }
if(!merger){ if(!merger){
merger.set<FrameMerger>(); merger.set<FrameMerger>();
}; }
merger.get<FrameMerger>().inputFrame(frame,[&](uint32_t dts,uint32_t pts,const Buffer::Ptr &buffer){ merger.get<FrameMerger>().inputFrame(frame,[&](uint32_t dts,uint32_t pts,const Buffer::Ptr &buffer){
AVFrame *pFrame = nullptr; AVFrame *pFrame = nullptr;
bool flag = decoder.get<H264Decoder>().inputVideo((unsigned char *) buffer->data(), buffer->size(), dts, &pFrame); bool flag = decoder.get<FFMpegDecoder>().inputVideo((unsigned char *) buffer->data(), buffer->size(), dts, &pFrame);
if (flag) { if (flag) {
displayer.get<YuvDisplayer>().displayYUV(pFrame); displayer.get<YuvDisplayer>().displayYUV(pFrame);
} }
}); });
return true; return true;
}); });
})); }));