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"
|
2023-12-10 10:21:40 +08:00
|
|
|
|
#include "H265Rtp.h"
|
|
|
|
|
#include "H265Rtmp.h"
|
2019-07-30 21:29:31 +08:00
|
|
|
|
#include "SPSParser.h"
|
2022-11-29 11:07:13 +08:00
|
|
|
|
#include "Util/base64.h"
|
2023-12-10 10:21:40 +08:00
|
|
|
|
#include "Common/Parser.h"
|
|
|
|
|
#include "Extension/Factory.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
|
|
|
|
|
|
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);
|
2024-06-16 09:37:33 +08:00
|
|
|
|
H265Track::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();
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
2024-09-17 09:43:37 +08:00
|
|
|
|
int type = H265_TYPE(frame->data()[frame->prefixSize()]);
|
2021-09-27 13:12:53 +08:00
|
|
|
|
bool ret = true;
|
2024-09-17 09:43:37 +08:00
|
|
|
|
switch (type) {
|
2021-02-05 11:51:16 +08:00
|
|
|
|
case H265Frame::NAL_VPS: {
|
|
|
|
|
_vps = string(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize());
|
2024-09-17 09:43:37 +08:00
|
|
|
|
_latest_is_config_frame = true;
|
|
|
|
|
ret = VideoTrack::inputFrame(frame);
|
2021-02-05 11:51:16 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case H265Frame::NAL_SPS: {
|
|
|
|
|
_sps = string(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize());
|
2024-09-17 09:43:37 +08:00
|
|
|
|
_latest_is_config_frame = true;
|
|
|
|
|
ret = VideoTrack::inputFrame(frame);
|
2021-02-05 11:51:16 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case H265Frame::NAL_PPS: {
|
|
|
|
|
_pps = string(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize());
|
2024-09-17 09:43:37 +08:00
|
|
|
|
_latest_is_config_frame = true;
|
|
|
|
|
ret = VideoTrack::inputFrame(frame);
|
2021-02-05 11:51:16 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default: {
|
2024-09-17 09:43:37 +08:00
|
|
|
|
// 判断是否是I帧, 并且如果是,那判断前面是否插入过config帧, 如果插入过就不插入了
|
|
|
|
|
if (frame->keyFrame() && !_latest_is_config_frame) {
|
|
|
|
|
insertConfigFrame(frame);
|
|
|
|
|
}
|
|
|
|
|
if (!frame->dropAble()) {
|
|
|
|
|
_latest_is_config_frame = false;
|
|
|
|
|
}
|
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()) {
|
2023-12-09 16:23:51 +08:00
|
|
|
|
update();
|
2021-02-05 11:51:16 +08:00
|
|
|
|
}
|
2021-09-27 13:12:53 +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-11-07 23:36:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-21 20:46:17 +08:00
|
|
|
|
std::vector<Frame::Ptr> H265Track::getConfigFrames() const {
|
|
|
|
|
if (!ready()) {
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
return { createConfigFrame<H265Frame>(_vps, 0, getIndex()),
|
|
|
|
|
createConfigFrame<H265Frame>(_sps, 0, getIndex()),
|
|
|
|
|
createConfigFrame<H265Frame>(_pps, 0, getIndex()) };
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-09 16:23:51 +08:00
|
|
|
|
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 (!_vps.empty()) {
|
2024-06-19 14:05:23 +08:00
|
|
|
|
VideoTrack::inputFrame(createConfigFrame<H265Frame>(_vps, frame->dts(), frame->getIndex()));
|
2021-02-05 11:51:16 +08:00
|
|
|
|
}
|
|
|
|
|
if (!_sps.empty()) {
|
2024-06-19 14:05:23 +08:00
|
|
|
|
VideoTrack::inputFrame(createConfigFrame<H265Frame>(_sps, frame->dts(), frame->getIndex()));
|
2021-02-05 11:51:16 +08:00
|
|
|
|
}
|
|
|
|
|
if (!_pps.empty()) {
|
2024-06-19 14:05:23 +08:00
|
|
|
|
VideoTrack::inputFrame(createConfigFrame<H265Frame>(_pps, frame->dts(), frame->getIndex()));
|
2021-02-05 11:51:16 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* h265类型sdp
|
2024-09-19 14:53:50 +08:00
|
|
|
|
* h265 type sdp
|
|
|
|
|
|
|
|
|
|
* [AUTO-TRANSLATED:4418a7df]
|
2021-02-05 11:51:16 +08:00
|
|
|
|
*/
|
|
|
|
|
class H265Sdp : public Sdp {
|
|
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* 构造函数
|
|
|
|
|
* @param sps 265 sps,不带0x00000001头
|
|
|
|
|
* @param pps 265 pps,不带0x00000001头
|
|
|
|
|
* @param payload_type rtp payload type 默认96
|
|
|
|
|
* @param bitrate 比特率
|
2024-09-19 14:53:50 +08:00
|
|
|
|
* Constructor
|
|
|
|
|
* @param sps 265 sps, without 0x00000001 header
|
|
|
|
|
* @param pps 265 pps, without 0x00000001 header
|
|
|
|
|
* @param payload_type rtp payload type, default 96
|
|
|
|
|
* @param bitrate Bitrate
|
|
|
|
|
|
|
|
|
|
* [AUTO-TRANSLATED:93f4ec48]
|
2021-02-05 11:51:16 +08:00
|
|
|
|
*/
|
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) {
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 视频通道 [AUTO-TRANSLATED:642ca881]
|
|
|
|
|
// Video channel
|
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
|
|
|
|
|
2023-12-10 10:21:40 +08:00
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
CodecId getCodec() {
|
|
|
|
|
return CodecH265;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Track::Ptr getTrackByCodecId(int sample_rate, int channels, int sample_bit) {
|
|
|
|
|
return std::make_shared<H265Track>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Track::Ptr getTrackBySdp(const SdpTrack::Ptr &track) {
|
|
|
|
|
// a=fmtp:96 sprop-sps=QgEBAWAAAAMAsAAAAwAAAwBdoAKAgC0WNrkky/AIAAADAAgAAAMBlQg=; sprop-pps=RAHA8vA8kAA=
|
|
|
|
|
auto map = Parser::parseArgs(track->_fmtp, ";", "=");
|
|
|
|
|
auto vps = decodeBase64(map["sprop-vps"]);
|
|
|
|
|
auto sps = decodeBase64(map["sprop-sps"]);
|
|
|
|
|
auto pps = decodeBase64(map["sprop-pps"]);
|
|
|
|
|
if (sps.empty() || pps.empty()) {
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 如果sdp里面没有sps/pps,那么可能在后续的rtp里面恢复出sps/pps [AUTO-TRANSLATED:9300510b]
|
|
|
|
|
// If there is no sps/pps in the sdp, then it may be possible to recover sps/pps from the subsequent rtp
|
2023-12-10 10:21:40 +08:00
|
|
|
|
return std::make_shared<H265Track>();
|
|
|
|
|
}
|
|
|
|
|
return std::make_shared<H265Track>(vps, sps, pps, 0, 0, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RtpCodec::Ptr getRtpEncoderByCodecId(uint8_t pt) {
|
|
|
|
|
return std::make_shared<H265RtpEncoder>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RtpCodec::Ptr getRtpDecoderByCodecId() {
|
|
|
|
|
return std::make_shared<H265RtpDecoder>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RtmpCodec::Ptr getRtmpEncoderByTrack(const Track::Ptr &track) {
|
|
|
|
|
return std::make_shared<H265RtmpEncoder>(track);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RtmpCodec::Ptr getRtmpDecoderByTrack(const Track::Ptr &track) {
|
|
|
|
|
return std::make_shared<H265RtmpDecoder>(track);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Frame::Ptr getFrameFromPtr(const char *data, size_t bytes, uint64_t dts, uint64_t pts) {
|
|
|
|
|
return std::make_shared<H265FrameNoCacheAble>((char *)data, bytes, dts, pts, prefixSize(data, bytes));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
CodecPlugin h265_plugin = { getCodec,
|
|
|
|
|
getTrackByCodecId,
|
|
|
|
|
getTrackBySdp,
|
|
|
|
|
getRtpEncoderByCodecId,
|
|
|
|
|
getRtpDecoderByCodecId,
|
|
|
|
|
getRtmpEncoderByTrack,
|
|
|
|
|
getRtmpDecoderByTrack,
|
|
|
|
|
getFrameFromPtr };
|
|
|
|
|
|
2018-10-30 14:59:42 +08:00
|
|
|
|
}//namespace mediakit
|
|
|
|
|
|