ZLMediaKit/src/Extension/H265.cpp

265 lines
8.5 KiB
C++
Raw Normal View History

2018-10-25 10:00:17 +08:00
/*
2023-12-09 16:23:51 +08:00
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
2018-10-25 10:00:17 +08:00
*
2023-12-09 16:23:51 +08:00
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
2018-10-25 10:00:17 +08:00
*
2023-12-09 16:23:51 +08:00
* Use of this source code is governed by MIT-like license that can be found in the
2020-04-04 20:30:09 +08:00
* 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.
2018-10-25 10:00:17 +08:00
*/
2018-10-18 23:48:00 +08:00
2018-10-30 14:59:42 +08:00
#include "H265.h"
2019-07-30 21:29:31 +08:00
#include "SPSParser.h"
#include "Util/base64.h"
2018-10-24 17:17:55 +08:00
2023-12-09 16:23:51 +08:00
#ifdef ENABLE_MP4
#include "mpeg4-hevc.h"
#endif
using namespace std;
using namespace toolkit;
namespace mediakit {
2018-10-24 17:17:55 +08:00
bool getHEVCInfo(const char * vps, size_t vps_len,const char * sps,size_t sps_len,int &iVideoWidth, int &iVideoHeight, float &iVideoFps){
2019-07-30 21:29:31 +08:00
T_GetBitContext tGetBitBuf;
T_HEVCSPS tH265SpsInfo;
T_HEVCVPS tH265VpsInfo;
2020-03-20 11:51:24 +08:00
if ( vps_len > 2 ){
memset(&tGetBitBuf,0,sizeof(tGetBitBuf));
memset(&tH265VpsInfo,0,sizeof(tH265VpsInfo));
tGetBitBuf.pu8Buf = (uint8_t*)vps+2;
tGetBitBuf.iBufSize = (int)(vps_len-2);
2020-03-20 11:51:24 +08:00
if(0 != h265DecVideoParameterSet((void *) &tGetBitBuf, &tH265VpsInfo)){
return false;
}
}
2020-03-20 11:51:24 +08:00
if ( sps_len > 2 ){
memset(&tGetBitBuf,0,sizeof(tGetBitBuf));
memset(&tH265SpsInfo,0,sizeof(tH265SpsInfo));
tGetBitBuf.pu8Buf = (uint8_t*)sps+2;
tGetBitBuf.iBufSize = (int)(sps_len-2);
2020-03-20 11:51:24 +08:00
if(0 != h265DecSeqParameterSet((void *) &tGetBitBuf, &tH265SpsInfo)){
return false;
}
}
else
return false;
2019-07-30 21:29:31 +08:00
h265GetWidthHeight(&tH265SpsInfo, &iVideoWidth, &iVideoHeight);
2020-03-20 11:51:24 +08:00
iVideoFps = 0;
h265GeFramerate(&tH265VpsInfo, &tH265SpsInfo, &iVideoFps);
2019-07-30 21:29:31 +08:00
return true;
}
2019-08-02 13:51:24 +08:00
bool getHEVCInfo(const string &strVps, const string &strSps, int &iVideoWidth, int &iVideoHeight, float &iVideoFps) {
return getHEVCInfo(strVps.data(), strVps.size(), strSps.data(), strSps.size(), iVideoWidth, iVideoHeight,iVideoFps);
2019-08-02 13:51:24 +08:00
}
2019-07-30 21:29:31 +08:00
2021-02-05 11:51:16 +08:00
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
H265Track::H265Track(const string &vps,const string &sps, const string &pps,int vps_prefix_len, int sps_prefix_len, int pps_prefix_len) {
_vps = vps.substr(vps_prefix_len);
_sps = sps.substr(sps_prefix_len);
_pps = pps.substr(pps_prefix_len);
2023-12-09 16:23:51 +08:00
update();
2021-02-05 11:51:16 +08:00
}
CodecId H265Track::getCodecId() const {
return CodecH265;
}
int H265Track::getVideoHeight() const {
return _height;
}
int H265Track::getVideoWidth() const {
return _width;
}
float H265Track::getVideoFps() const {
return _fps;
}
2023-12-09 16:23:51 +08:00
bool H265Track::ready() const {
2021-02-05 11:51:16 +08:00
return !_vps.empty() && !_sps.empty() && !_pps.empty();
}
bool H265Track::inputFrame(const Frame::Ptr &frame) {
int type = H265_TYPE(frame->data()[frame->prefixSize()]);
if (!frame->configFrame() && type != H265Frame::NAL_SEI_PREFIX && ready()) {
return inputFrame_l(frame);
2021-02-05 11:51:16 +08:00
}
bool ret = false;
splitH264(frame->data(), frame->size(), frame->prefixSize(), [&](const char *ptr, size_t len, size_t prefix) {
using H265FrameInternal = FrameInternal<H265FrameNoCacheAble>;
H265FrameInternal::Ptr sub_frame = std::make_shared<H265FrameInternal>(frame, (char *) ptr, len, prefix);
if (inputFrame_l(sub_frame)) {
ret = true;
}
});
return ret;
2021-02-05 11:51:16 +08:00
}
bool H265Track::inputFrame_l(const Frame::Ptr &frame) {
if (frame->keyFrame()) {
2021-02-05 11:51:16 +08:00
insertConfigFrame(frame);
_is_idr = true;
return VideoTrack::inputFrame(frame);
2021-02-05 11:51:16 +08:00
}
_is_idr = false;
bool ret = true;
2021-02-05 11:51:16 +08:00
//非idr帧
2021-07-09 14:04:34 +08:00
switch (H265_TYPE( frame->data()[frame->prefixSize()])) {
2021-02-05 11:51:16 +08:00
case H265Frame::NAL_VPS: {
_vps = string(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize());
break;
}
case H265Frame::NAL_SPS: {
_sps = string(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize());
break;
}
case H265Frame::NAL_PPS: {
_pps = string(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize());
break;
}
default: {
ret = VideoTrack::inputFrame(frame);
2021-02-05 11:51:16 +08:00
break;
}
}
if (_width == 0 && ready()) {
2023-12-09 16:23:51 +08:00
update();
2021-02-05 11:51:16 +08:00
}
return ret;
2021-02-05 11:51:16 +08:00
}
2023-12-09 16:23:51 +08:00
toolkit::Buffer::Ptr H265Track::getExtraData() const {
CHECK(ready());
#ifdef ENABLE_MP4
struct mpeg4_hevc_t hevc;
memset(&hevc, 0, sizeof(hevc));
string vps_sps_pps = string("\x00\x00\x00\x01", 4) + _vps + string("\x00\x00\x00\x01", 4) + _sps + string("\x00\x00\x00\x01", 4) + _pps;
h265_annexbtomp4(&hevc, vps_sps_pps.data(), (int) vps_sps_pps.size(), NULL, 0, NULL, NULL);
std::string extra_data;
extra_data.resize(1024);
auto extra_data_size = mpeg4_hevc_decoder_configuration_record_save(&hevc, (uint8_t *)extra_data.data(), extra_data.size());
if (extra_data_size == -1) {
WarnL << "生成H265 extra_data 失败";
return nullptr;
}
return std::make_shared<BufferString>(std::move(extra_data));
#else
WarnL << "请开启MP4相关功能并使能\"ENABLE_MP4\",否则对H265的支持不完善";
return nullptr;
#endif
}
2023-12-09 16:23:51 +08:00
void H265Track::setExtraData(const uint8_t *data, size_t bytes) {
#ifdef ENABLE_MP4
struct mpeg4_hevc_t hevc;
memset(&hevc, 0, sizeof(hevc));
if (mpeg4_hevc_decoder_configuration_record_load(data, bytes, &hevc) > 0) {
std::vector<uint8_t> config(bytes * 2);
int size = mpeg4_hevc_to_nalu(&hevc, config.data(), bytes * 2);
if (size > 4) {
splitH264((char *)config.data(), size, 4, [&](const char *ptr, size_t len, size_t prefix) {
inputFrame_l(std::make_shared<H265FrameNoCacheAble>((char *)ptr, len, 0, 0, prefix));
});
update();
}
2021-02-28 20:58:30 +08:00
}
2023-12-09 16:23:51 +08:00
#else
WarnL << "请开启MP4相关功能并使能\"ENABLE_MP4\",否则对H265的支持不完善";
#endif
2021-02-05 11:51:16 +08:00
}
2023-12-09 16:23:51 +08:00
bool H265Track::update() {
return getHEVCInfo(_vps, _sps, _width, _height, _fps);
}
Track::Ptr H265Track::clone() const {
return std::make_shared<H265Track>(*this);
2021-02-05 11:51:16 +08:00
}
void H265Track::insertConfigFrame(const Frame::Ptr &frame) {
if (_is_idr) {
return;
}
if (!_vps.empty()) {
auto vpsFrame = FrameImp::create<H265Frame>();
vpsFrame->_prefix_size = 4;
vpsFrame->_buffer.assign("\x00\x00\x00\x01", 4);
vpsFrame->_buffer.append(_vps);
vpsFrame->_dts = frame->dts();
VideoTrack::inputFrame(vpsFrame);
}
if (!_sps.empty()) {
auto spsFrame = FrameImp::create<H265Frame>();
spsFrame->_prefix_size = 4;
spsFrame->_buffer.assign("\x00\x00\x00\x01", 4);
spsFrame->_buffer.append(_sps);
spsFrame->_dts = frame->dts();
VideoTrack::inputFrame(spsFrame);
}
if (!_pps.empty()) {
auto ppsFrame = FrameImp::create<H265Frame>();
ppsFrame->_prefix_size = 4;
ppsFrame->_buffer.assign("\x00\x00\x00\x01", 4);
ppsFrame->_buffer.append(_pps);
ppsFrame->_dts = frame->dts();
VideoTrack::inputFrame(ppsFrame);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* h265类型sdp
*/
class H265Sdp : public Sdp {
public:
/**
*
* @param sps 265 sps,0x00000001
* @param pps 265 pps,0x00000001
* @param payload_type rtp payload type 96
* @param bitrate
*/
2023-12-09 16:23:51 +08:00
H265Sdp(const string &strVPS, const string &strSPS, const string &strPPS, int payload_type, int bitrate) : Sdp(90000, payload_type) {
2021-02-05 11:51:16 +08:00
//视频通道
_printer << "m=video 0 RTP/AVP " << payload_type << "\r\n";
if (bitrate) {
_printer << "b=AS:" << bitrate << "\r\n";
}
2023-12-09 16:23:51 +08:00
_printer << "a=rtpmap:" << payload_type << " " << getCodecName(CodecH265) << "/" << 90000 << "\r\n";
2021-02-05 11:51:16 +08:00
_printer << "a=fmtp:" << payload_type << " ";
_printer << "sprop-vps=";
_printer << encodeBase64(strVPS) << "; ";
_printer << "sprop-sps=";
_printer << encodeBase64(strSPS) << "; ";
_printer << "sprop-pps=";
_printer << encodeBase64(strPPS) << "\r\n";
}
2023-12-09 16:23:51 +08:00
string getSdp() const override { return _printer; }
2021-02-05 11:51:16 +08:00
private:
_StrPrinter _printer;
};
2023-12-09 16:23:51 +08:00
Sdp::Ptr H265Track::getSdp(uint8_t payload_type) const {
if (!ready()) {
2020-04-18 18:46:20 +08:00
WarnL << getCodecName() << " Track未准备好";
2019-06-28 16:12:39 +08:00
return nullptr;
}
2023-12-09 16:23:51 +08:00
return std::make_shared<H265Sdp>(_vps, _sps, _pps, payload_type, getBitRate() / 1024);
2019-06-28 16:12:39 +08:00
}
2021-02-05 11:51:16 +08:00
2018-10-30 14:59:42 +08:00
}//namespace mediakit