H265.cpp 7.08 KB
Newer Older
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
3
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
5
 *
xiongziliang committed
6 7 8
 * 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.
9
 */
10

11
#include "H265.h"
zqsong committed
12
#include "SPSParser.h"
xiongziliang committed
13 14 15

namespace mediakit{

16
bool getHEVCInfo(const char * vps, size_t vps_len,const char * sps,size_t sps_len,int &iVideoWidth, int &iVideoHeight, float  &iVideoFps){
zqsong committed
17
    T_GetBitContext tGetBitBuf;
18 19
    T_HEVCSPS tH265SpsInfo;	
    T_HEVCVPS tH265VpsInfo;
20 21 22 23
    if ( vps_len > 2 ){
        memset(&tGetBitBuf,0,sizeof(tGetBitBuf));	
        memset(&tH265VpsInfo,0,sizeof(tH265VpsInfo));
        tGetBitBuf.pu8Buf = (uint8_t*)vps+2;
24
        tGetBitBuf.iBufSize = (int)(vps_len-2);
25 26 27 28
        if(0 != h265DecVideoParameterSet((void *) &tGetBitBuf, &tH265VpsInfo)){
            return false;
        }
    }
29

30 31 32 33
    if ( sps_len > 2 ){
        memset(&tGetBitBuf,0,sizeof(tGetBitBuf));
        memset(&tH265SpsInfo,0,sizeof(tH265SpsInfo));
        tGetBitBuf.pu8Buf = (uint8_t*)sps+2;
34
        tGetBitBuf.iBufSize = (int)(sps_len-2);
35 36 37 38 39 40
        if(0 != h265DecSeqParameterSet((void *) &tGetBitBuf, &tH265SpsInfo)){
            return false;
        }
    }
    else 
        return false;
zqsong committed
41
    h265GetWidthHeight(&tH265SpsInfo, &iVideoWidth, &iVideoHeight);
42
    iVideoFps = 0;
43
    h265GeFramerate(&tH265VpsInfo, &tH265SpsInfo, &iVideoFps);
zqsong committed
44 45 46
    return true;
}

xiongziliang committed
47
bool getHEVCInfo(const string &strVps, const string &strSps, int &iVideoWidth, int &iVideoHeight, float &iVideoFps) {
48
    return getHEVCInfo(strVps.data(), strVps.size(), strSps.data(), strSps.size(), iVideoWidth, iVideoHeight,iVideoFps);
xiongziliang committed
49
}
zqsong committed
50

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

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();
}

void H265Track::inputFrame(const Frame::Ptr &frame) {
    using H265FrameInternal = FrameInternal<H265FrameNoCacheAble>;
ziyue committed
94
    int type = H265_TYPE( frame->data()[frame->prefixSize()]);
95 96 97 98 99 100 101 102 103 104 105
    if (frame->configFrame() || type == H265Frame::NAL_SEI_PREFIX) {
        splitH264(frame->data(), frame->size(), frame->prefixSize(), [&](const char *ptr, size_t len, size_t prefix) {
            H265FrameInternal::Ptr sub_frame = std::make_shared<H265FrameInternal>(frame, (char *) ptr, len, prefix);
            inputFrame_l(sub_frame);
        });
    } else {
        inputFrame_l(frame);
    }
}

void H265Track::inputFrame_l(const Frame::Ptr &frame) {
106
    if (frame->keyFrame()) {
107 108 109 110 111 112
        insertConfigFrame(frame);
        VideoTrack::inputFrame(frame);
        _is_idr = true;
        return;
    }

113
    _is_idr = false;
114
    //非idr帧
ziyue committed
115
    switch (H265_TYPE( frame->data()[frame->prefixSize()])) {
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
        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: {
            VideoTrack::inputFrame(frame);
            break;
        }
    }
    if (_width == 0 && ready()) {
        onReady();
    }
}

void H265Track::onReady() {
139 140 141 142 143
    if (!getHEVCInfo(_vps, _sps, _width, _height, _fps)) {
        _vps.clear();
        _sps.clear();
        _pps.clear();
    }
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
}

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";
        }
205
        _printer << "a=rtpmap:" << payload_type << " " << getCodecName() << "/" << 90000 << "\r\n";
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
        _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;
};

xiongziliang committed
227 228
Sdp::Ptr H265Track::getSdp() {
    if(!ready()){
xiongziliang committed
229
        WarnL << getCodecName() << " Track未准备好";
xiongziliang committed
230 231
        return nullptr;
    }
xiongziliang committed
232
    return std::make_shared<H265Sdp>(getVps(), getSps(), getPps(), getBitRate() / 1024);
xiongziliang committed
233
}
234

235 236
}//namespace mediakit