2018-10-25 10:00:17 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2018-10-25 10:00:17 +08:00
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2018-10-25 10:00:17 +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.
|
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"
|
2022-11-29 11:07:13 +08:00
|
|
|
|
#include "Util/base64.h"
|
2018-10-24 17:17:55 +08:00
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace toolkit;
|
|
|
|
|
|
|
|
|
|
namespace mediakit {
|
2018-10-24 17:17:55 +08:00
|
|
|
|
|
2021-01-17 18:31:50 +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;
|
2019-08-02 10:46:16 +08:00
|
|
|
|
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;
|
2021-01-17 18:31:50 +08:00
|
|
|
|
tGetBitBuf.iBufSize = (int)(vps_len-2);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
if(0 != h265DecVideoParameterSet((void *) &tGetBitBuf, &tH265VpsInfo)){
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-02 10:46:16 +08:00
|
|
|
|
|
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;
|
2021-01-17 18:31:50 +08:00
|
|
|
|
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;
|
2019-08-02 10:46:16 +08:00
|
|
|
|
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) {
|
2021-01-17 18:31:50 +08:00
|
|
|
|
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);
|
|
|
|
|
onReady();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const string &H265Track::getVps() const {
|
|
|
|
|
return _vps;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const string &H265Track::getSps() const {
|
|
|
|
|
return _sps;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const string &H265Track::getPps() const {
|
|
|
|
|
return _pps;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CodecId H265Track::getCodecId() const {
|
|
|
|
|
return CodecH265;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int H265Track::getVideoHeight() const {
|
|
|
|
|
return _height;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int H265Track::getVideoWidth() const {
|
|
|
|
|
return _width;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float H265Track::getVideoFps() const {
|
|
|
|
|
return _fps;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool H265Track::ready() {
|
|
|
|
|
return !_vps.empty() && !_sps.empty() && !_pps.empty();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-27 13:12:53 +08:00
|
|
|
|
bool H265Track::inputFrame(const Frame::Ptr &frame) {
|
|
|
|
|
int type = H265_TYPE(frame->data()[frame->prefixSize()]);
|
2022-02-17 14:32:49 +08:00
|
|
|
|
if (!frame->configFrame() && type != H265Frame::NAL_SEI_PREFIX && ready()) {
|
2021-09-27 13:12:53 +08:00
|
|
|
|
return inputFrame_l(frame);
|
2021-02-05 11:51:16 +08:00
|
|
|
|
}
|
2021-09-27 13:12:53 +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
|
|
|
|
}
|
|
|
|
|
|
2021-09-27 13:12:53 +08:00
|
|
|
|
bool H265Track::inputFrame_l(const Frame::Ptr &frame) {
|
2021-07-09 13:38:20 +08:00
|
|
|
|
if (frame->keyFrame()) {
|
2021-02-05 11:51:16 +08:00
|
|
|
|
insertConfigFrame(frame);
|
|
|
|
|
_is_idr = true;
|
2021-09-27 13:12:53 +08:00
|
|
|
|
return VideoTrack::inputFrame(frame);
|
2021-02-05 11:51:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-09 13:38:20 +08:00
|
|
|
|
_is_idr = false;
|
2021-09-27 13:12:53 +08:00
|
|
|
|
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: {
|
2021-09-27 13:12:53 +08:00
|
|
|
|
ret = VideoTrack::inputFrame(frame);
|
2021-02-05 11:51:16 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (_width == 0 && ready()) {
|
|
|
|
|
onReady();
|
|
|
|
|
}
|
2021-09-27 13:12:53 +08:00
|
|
|
|
return ret;
|
2021-02-05 11:51:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-07 23:36:41 +08:00
|
|
|
|
bool H265Track::update() {
|
|
|
|
|
return getHEVCInfo(_vps, _sps, _width, _height, _fps);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-05 11:51:16 +08:00
|
|
|
|
void H265Track::onReady() {
|
2021-02-28 20:58:30 +08:00
|
|
|
|
if (!getHEVCInfo(_vps, _sps, _width, _height, _fps)) {
|
|
|
|
|
_vps.clear();
|
|
|
|
|
_sps.clear();
|
|
|
|
|
_pps.clear();
|
|
|
|
|
}
|
2021-02-05 11:51:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Track::Ptr H265Track::clone() {
|
|
|
|
|
return std::make_shared<std::remove_reference<decltype(*this)>::type>(*this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 比特率
|
|
|
|
|
*/
|
|
|
|
|
H265Sdp(const string &strVPS,
|
|
|
|
|
const string &strSPS,
|
|
|
|
|
const string &strPPS,
|
|
|
|
|
int bitrate = 4000,
|
|
|
|
|
int payload_type = 96) : Sdp(90000,payload_type) {
|
|
|
|
|
//视频通道
|
|
|
|
|
_printer << "m=video 0 RTP/AVP " << payload_type << "\r\n";
|
|
|
|
|
if (bitrate) {
|
|
|
|
|
_printer << "b=AS:" << bitrate << "\r\n";
|
|
|
|
|
}
|
2021-03-30 10:59:15 +08:00
|
|
|
|
_printer << "a=rtpmap:" << payload_type << " " << getCodecName() << "/" << 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";
|
|
|
|
|
_printer << "a=control:trackID=" << (int)TrackVideo << "\r\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string getSdp() const override {
|
|
|
|
|
return _printer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CodecId getCodecId() const override {
|
|
|
|
|
return CodecH265;
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
_StrPrinter _printer;
|
|
|
|
|
};
|
|
|
|
|
|
2019-06-28 16:12:39 +08:00
|
|
|
|
Sdp::Ptr H265Track::getSdp() {
|
|
|
|
|
if(!ready()){
|
2020-04-18 18:46:20 +08:00
|
|
|
|
WarnL << getCodecName() << " Track未准备好";
|
2019-06-28 16:12:39 +08:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2020-12-05 12:22:17 +08:00
|
|
|
|
return std::make_shared<H265Sdp>(getVps(), getSps(), getPps(), 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
|
|
|
|
|
|