ZLMediaKit/src/Extension/H265.h

352 lines
9.5 KiB
C++
Raw Normal View History

2018-10-30 14:59:42 +08:00
/*
2020-04-04 20:30:09 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* 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-30 14:59:42 +08:00
#ifndef ZLMEDIAKIT_H265_H
#define ZLMEDIAKIT_H265_H
#include "Frame.h"
#include "Track.h"
2019-06-28 16:12:39 +08:00
#include "Util/base64.h"
#include "H264.h"
2019-06-28 16:12:39 +08:00
using namespace toolkit;
#define H265_TYPE(v) (((uint8_t)(v) >> 1) & 0x3f)
2018-10-30 14:59:42 +08:00
2018-10-30 15:56:00 +08:00
namespace mediakit {
2019-08-02 13:51:24 +08:00
bool getHEVCInfo(const string &strVps, const string &strSps, int &iVideoWidth, int &iVideoHeight, float &iVideoFps);
2019-07-30 21:29:31 +08:00
2018-10-30 14:59:42 +08:00
/**
2020-05-11 22:33:10 +08:00
* 265
*/
class H265Frame : public FrameImp {
2018-10-30 14:59:42 +08:00
public:
typedef std::shared_ptr<H265Frame> Ptr;
2018-10-30 15:56:00 +08:00
typedef enum {
NAL_TRAIL_N = 0,
NAL_TRAIL_R = 1,
NAL_TSA_N = 2,
NAL_TSA_R = 3,
NAL_STSA_N = 4,
NAL_STSA_R = 5,
NAL_RADL_N = 6,
NAL_RADL_R = 7,
NAL_RASL_N = 8,
NAL_RASL_R = 9,
NAL_BLA_W_LP = 16,
NAL_BLA_W_RADL = 17,
NAL_BLA_N_LP = 18,
NAL_IDR_W_RADL = 19,
NAL_IDR_N_LP = 20,
NAL_CRA_NUT = 21,
NAL_RSV_IRAP_VCL22 = 22,
NAL_RSV_IRAP_VCL23 = 23,
2018-10-30 15:56:00 +08:00
NAL_VPS = 32,
NAL_SPS = 33,
NAL_PPS = 34,
NAL_AUD = 35,
NAL_EOS_NUT = 36,
NAL_EOB_NUT = 37,
NAL_FD_NUT = 38,
NAL_SEI_PREFIX = 39,
NAL_SEI_SUFFIX = 40,
} NaleType;
2020-05-11 22:33:10 +08:00
H265Frame(){
_codecid = CodecH265;
2018-10-30 14:59:42 +08:00
}
bool keyFrame() const override {
return isKeyFrame(H265_TYPE(_buffer[_prefix_size]));
2019-08-01 18:49:04 +08:00
}
bool configFrame() const override{
switch(H265_TYPE(_buffer[_prefix_size])){
2019-08-01 18:49:04 +08:00
case H265Frame::NAL_VPS:
case H265Frame::NAL_SPS:
2020-05-11 22:33:10 +08:00
case H265Frame::NAL_PPS : return true;
default : return false;
2019-08-01 18:49:04 +08:00
}
2018-10-30 15:56:00 +08:00
}
static bool isKeyFrame(int type) {
return type >= NAL_BLA_W_LP && type <= NAL_RSV_IRAP_VCL23;
2018-10-30 14:59:42 +08:00
}
};
2020-05-11 22:33:10 +08:00
class H265FrameNoCacheAble : public FrameFromPtr {
2018-10-30 14:59:42 +08:00
public:
2019-07-24 18:02:55 +08:00
typedef std::shared_ptr<H265FrameNoCacheAble> Ptr;
2018-10-30 14:59:42 +08:00
2020-05-11 22:33:10 +08:00
H265FrameNoCacheAble(char *ptr, uint32_t size, uint32_t dts,uint32_t pts, int prefix_size = 4) {
2019-07-03 16:22:12 +08:00
_ptr = ptr;
_size = size;
_dts = dts;
_pts = pts;
2020-05-11 22:33:10 +08:00
_prefix_size = prefix_size;
2018-10-30 14:59:42 +08:00
}
2018-10-30 15:56:00 +08:00
CodecId getCodecId() const override {
2018-10-30 14:59:42 +08:00
return CodecH265;
}
bool keyFrame() const override {
2020-05-11 22:33:10 +08:00
return H265Frame::isKeyFrame(H265_TYPE(((uint8_t *) _ptr)[_prefix_size]));
2019-08-01 18:49:04 +08:00
}
bool configFrame() const override{
2020-05-11 22:33:10 +08:00
switch(H265_TYPE(((uint8_t *) _ptr)[_prefix_size])){
2019-08-01 18:49:04 +08:00
case H265Frame::NAL_VPS:
case H265Frame::NAL_SPS:
2020-05-11 22:33:10 +08:00
case H265Frame::NAL_PPS:return true;
default:return false;
2019-08-01 18:49:04 +08:00
}
2018-10-30 15:56:00 +08:00
}
};
typedef FrameInternal<H265FrameNoCacheAble> H265FrameInternal;
2018-10-30 15:56:00 +08:00
/**
* 265
*/
class H265Track : public VideoTrack {
public:
typedef std::shared_ptr<H265Track> Ptr;
/**
* sps pps构造h265类型的媒体
* inputFrame中获取sps pps
*/
H265Track() {}
/**
* h265类型的媒体
2018-10-30 16:12:32 +08:00
* @param vps vps帧数据
2018-10-30 15:56:00 +08:00
* @param sps sps帧数据
* @param pps pps帧数据
2018-10-30 16:12:32 +08:00
* @param vps_prefix_len 265340x00 00 00 01
2018-10-30 15:56:00 +08:00
* @param sps_prefix_len 265340x00 00 00 01
* @param pps_prefix_len 265340x00 00 00 01
*/
2018-10-30 16:12:32 +08:00
H265Track(const string &vps,const string &sps, const string &pps,int vps_prefix_len = 4, int sps_prefix_len = 4, int pps_prefix_len = 4) {
_vps = vps.substr(vps_prefix_len);
2018-10-30 15:56:00 +08:00
_sps = sps.substr(sps_prefix_len);
_pps = pps.substr(pps_prefix_len);
2020-03-20 11:51:24 +08:00
onReady();
2018-10-30 15:56:00 +08:00
}
/**
2018-10-30 16:12:32 +08:00
* 0x00 00 00 01vps
2018-10-30 15:56:00 +08:00
*/
2018-10-30 16:12:32 +08:00
const string &getVps() const {
return _vps;
2018-10-30 15:56:00 +08:00
}
/**
* 0x00 00 00 01sps
*/
const string &getSps() const {
return _sps;
}
/**
* 0x00 00 00 01pps
*/
const string &getPps() const {
return _pps;
}
CodecId getCodecId() const override {
return CodecH265;
}
2019-07-30 21:29:31 +08:00
/**
*
*/
int getVideoHeight() const override{
return _height ;
}
/**
*
*/
int getVideoWidth() const override{
return _width;
}
/**
* fps
*/
float getVideoFps() const override{
return _fps;
}
2018-10-30 15:56:00 +08:00
bool ready() override {
2018-10-31 09:07:01 +08:00
return !_vps.empty() && !_sps.empty() && !_pps.empty();
2018-10-30 14:59:42 +08:00
}
2018-10-30 15:56:00 +08:00
/**
2020-05-11 22:33:10 +08:00
* ,sps pps
* @param frame
*/
void inputFrame(const Frame::Ptr &frame) override{
2020-03-20 11:51:24 +08:00
int type = H265_TYPE(*((uint8_t *)frame->data() + frame->prefixSize()));
2020-05-21 14:10:55 +08:00
if(frame->configFrame() || type == H265Frame::NAL_SEI_PREFIX){
2020-04-30 10:00:55 +08:00
splitH264(frame->data(), frame->size(), frame->prefixSize(), [&](const char *ptr, int len, int prefix){
H265FrameInternal::Ptr sub_frame = std::make_shared<H265FrameInternal>(frame, (char*)ptr, len, prefix);
inputFrame_l(sub_frame);
});
} else {
inputFrame_l(frame);
}
}
private:
2018-10-30 15:56:00 +08:00
/**
* ,sps pps
* @param frame
*/
void inputFrame_l(const Frame::Ptr &frame) {
2018-10-30 15:56:00 +08:00
int type = H265_TYPE(((uint8_t *) frame->data() + frame->prefixSize())[0]);
if (H265Frame::isKeyFrame(type)) {
2019-04-09 12:39:38 +08:00
insertConfigFrame(frame);
2018-10-30 15:56:00 +08:00
VideoTrack::inputFrame(frame);
2019-04-09 12:39:38 +08:00
_last_frame_is_idr = true;
2018-10-30 15:56:00 +08:00
return;
}
2019-04-09 12:39:38 +08:00
_last_frame_is_idr = false;
2018-10-30 15:56:00 +08:00
//非idr帧
switch (type) {
case H265Frame::NAL_VPS: {
//vps
_vps = string(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize());
}
break;
case H265Frame::NAL_SPS: {
//sps
_sps = string(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize());
}
break;
case H265Frame::NAL_PPS: {
//pps
_pps = string(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize());
}
break;
default: {
//other frames
VideoTrack::inputFrame(frame);
}
break;
}
}
2020-03-20 11:51:24 +08:00
/**
2019-07-30 21:29:31 +08:00
* sps获取宽高fps
*/
void onReady(){
2019-08-02 13:51:24 +08:00
getHEVCInfo(_vps, _sps, _width, _height, _fps);
2019-07-30 21:29:31 +08:00
}
2018-10-30 15:56:00 +08:00
Track::Ptr clone() override {
return std::make_shared<std::remove_reference<decltype(*this)>::type>(*this);
}
2019-06-28 16:12:39 +08:00
//生成sdp
Sdp::Ptr getSdp() override ;
2019-04-09 12:39:38 +08:00
//在idr帧前插入vps sps pps帧
void insertConfigFrame(const Frame::Ptr &frame){
if(_last_frame_is_idr){
return;
}
if(!_vps.empty()){
2019-07-03 16:42:20 +08:00
auto vpsFrame = std::make_shared<H265Frame>();
vpsFrame->_prefix_size = 4;
vpsFrame->_buffer.assign("\x0\x0\x0\x1", 4);
vpsFrame->_buffer.append(_vps);
vpsFrame->_dts = frame->dts();
2019-07-03 16:42:20 +08:00
VideoTrack::inputFrame(vpsFrame);
2019-04-09 12:39:38 +08:00
}
if (!_sps.empty()) {
2019-07-03 16:42:20 +08:00
auto spsFrame = std::make_shared<H265Frame>();
spsFrame->_prefix_size = 4;
spsFrame->_buffer.assign("\x0\x0\x0\x1", 4);
spsFrame->_buffer.append(_sps);
spsFrame->_dts = frame->dts();
2019-07-03 16:42:20 +08:00
VideoTrack::inputFrame(spsFrame);
2019-04-09 12:39:38 +08:00
}
if (!_pps.empty()) {
2019-07-03 16:42:20 +08:00
auto ppsFrame = std::make_shared<H265Frame>();
ppsFrame->_prefix_size = 4;
ppsFrame->_buffer.assign("\x0\x0\x0\x1", 4);
ppsFrame->_buffer.append(_pps);
ppsFrame->_dts = frame->dts();
2019-07-03 16:42:20 +08:00
VideoTrack::inputFrame(ppsFrame);
2019-04-09 12:39:38 +08:00
}
}
2018-10-30 15:56:00 +08:00
private:
string _vps;
string _sps;
string _pps;
2019-07-30 21:29:31 +08:00
int _width = 0;
int _height = 0;
float _fps = 0;
2019-04-09 12:39:38 +08:00
bool _last_frame_is_idr = false;
2018-10-30 14:59:42 +08:00
};
2018-10-30 21:05:48 +08:00
/**
* h265类型sdp
*/
class H265Sdp : public Sdp {
public:
/**
2020-05-11 22:33:10 +08:00
*
2018-10-30 21:05:48 +08:00
* @param sps 265 sps,0x00000001
* @param pps 265 pps,0x00000001
2020-05-25 13:51:00 +08:00
* @param payload_type rtp payload type 96
2018-10-30 21:05:48 +08:00
* @param bitrate
*/
2018-11-02 15:06:27 +08:00
H265Sdp(const string &strVPS,
const string &strSPS,
2018-10-30 21:05:48 +08:00
const string &strPPS,
2020-05-25 13:51:00 +08:00
int payload_type = 96,
int bitrate = 4000) : Sdp(90000,payload_type) {
2018-10-30 21:05:48 +08:00
//视频通道
2020-05-25 13:51:00 +08:00
_printer << "m=video 0 RTP/AVP " << payload_type << "\r\n";
2018-10-30 21:05:48 +08:00
_printer << "b=AS:" << bitrate << "\r\n";
2020-05-25 13:51:00 +08:00
_printer << "a=rtpmap:" << payload_type << " H265/" << 90000 << "\r\n";
_printer << "a=fmtp:" << payload_type << " ";
2018-11-02 15:06:27 +08:00
_printer << "sprop-vps=";
_printer << encodeBase64(strVPS) << "; ";
_printer << "sprop-sps=";
2018-10-30 21:05:48 +08:00
_printer << encodeBase64(strSPS) << "; ";
_printer << "sprop-pps=";
_printer << encodeBase64(strPPS) << "\r\n";
2020-05-11 22:33:10 +08:00
_printer << "a=control:trackID=" << (int)TrackVideo << "\r\n";
2018-10-30 21:05:48 +08:00
}
string getSdp() const override {
return _printer;
}
CodecId getCodecId() const override {
return CodecH265;
}
private:
_StrPrinter _printer;
};
2018-10-30 14:59:42 +08:00
}//namespace mediakit
2020-05-11 22:33:10 +08:00
#endif //ZLMEDIAKIT_H265_H