H265.h 9.45 KB
Newer Older
1
/*
xiongziliang committed
2 3 4 5 6 7 8 9
 * 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.
 */
10 11 12 13 14 15

#ifndef ZLMEDIAKIT_H265_H
#define ZLMEDIAKIT_H265_H

#include "Frame.h"
#include "Track.h"
xiongziliang committed
16
#include "Util/base64.h"
17
#include "H264.h"
xiongziliang committed
18 19
using namespace toolkit;
#define H265_TYPE(v) (((uint8_t)(v) >> 1) & 0x3f)
20

xiongziliang committed
21 22
namespace mediakit {

xiongziliang committed
23
bool getHEVCInfo(const string &strVps, const string &strSps, int &iVideoWidth, int &iVideoHeight, float &iVideoFps);
zqsong committed
24

25
/**
xiongziliang committed
26 27 28
 * 265帧类
 */
class H265Frame : public FrameImp {
29 30 31
public:
    typedef std::shared_ptr<H265Frame> Ptr;

xiongziliang committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    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,
49 50 51
        NAL_RSV_IRAP_VCL22 = 22,
        NAL_RSV_IRAP_VCL23 = 23,

xiongziliang committed
52 53 54 55 56 57 58 59 60 61 62
        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;

xiongziliang committed
63 64
    H265Frame(){
        _codecid = CodecH265;
65 66 67
    }

    bool keyFrame() const override {
68
        return isKeyFrame(H265_TYPE(_buffer[_prefix_size]));
xiongziliang committed
69 70 71
    }

    bool configFrame() const override{
72
        switch(H265_TYPE(_buffer[_prefix_size])){
xiongziliang committed
73 74
            case H265Frame::NAL_VPS:
            case H265Frame::NAL_SPS:
xiongziliang committed
75 76
            case H265Frame::NAL_PPS : return true;
            default : return false;
xiongziliang committed
77
        }
xiongziliang committed
78 79 80
    }

    static bool isKeyFrame(int type) {
81
        return type >= NAL_BLA_W_LP && type <= NAL_RSV_IRAP_VCL23;
82 83 84
    }
};

xiongziliang committed
85
class H265FrameNoCacheAble : public FrameFromPtr {
86
public:
87
    typedef std::shared_ptr<H265FrameNoCacheAble> Ptr;
88

xiongziliang committed
89
    H265FrameNoCacheAble(char *ptr, uint32_t size, uint32_t dts,uint32_t pts, int prefix_size = 4) {
xiongziliang committed
90 91 92 93
        _ptr = ptr;
        _size = size;
        _dts = dts;
        _pts = pts;
xiongziliang committed
94
        _prefix_size = prefix_size;
95 96
    }

xiongziliang committed
97
    CodecId getCodecId() const override {
98 99 100 101
        return CodecH265;
    }

    bool keyFrame() const override {
xiongziliang committed
102
        return H265Frame::isKeyFrame(H265_TYPE(((uint8_t *) _ptr)[_prefix_size]));
xiongziliang committed
103 104 105
    }

    bool configFrame() const override{
xiongziliang committed
106
        switch(H265_TYPE(((uint8_t *) _ptr)[_prefix_size])){
xiongziliang committed
107 108
            case H265Frame::NAL_VPS:
            case H265Frame::NAL_SPS:
xiongziliang committed
109 110
            case H265Frame::NAL_PPS:return true;
            default:return false;
xiongziliang committed
111
        }
xiongziliang committed
112 113 114
    }
};

115
typedef FrameInternal<H265FrameNoCacheAble> H265FrameInternal;
xiongziliang committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

/**
* 265视频通道
*/
class H265Track : public VideoTrack {
public:
    typedef std::shared_ptr<H265Track> Ptr;

    /**
     * 不指定sps pps构造h265类型的媒体
     * 在随后的inputFrame中获取sps pps
     */
    H265Track() {}

    /**
     * 构造h265类型的媒体
xiongziliang committed
132
     * @param vps vps帧数据
xiongziliang committed
133 134
     * @param sps sps帧数据
     * @param pps pps帧数据
xiongziliang committed
135
     * @param vps_prefix_len 265头长度,可以为3个或4个字节,一般为0x00 00 00 01
xiongziliang committed
136 137 138
     * @param sps_prefix_len 265头长度,可以为3个或4个字节,一般为0x00 00 00 01
     * @param pps_prefix_len 265头长度,可以为3个或4个字节,一般为0x00 00 00 01
     */
xiongziliang committed
139 140
    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);
xiongziliang committed
141 142
        _sps = sps.substr(sps_prefix_len);
        _pps = pps.substr(pps_prefix_len);
143
        onReady();
xiongziliang committed
144 145 146
    }

    /**
xiongziliang committed
147
     * 返回不带0x00 00 00 01头的vps
xiongziliang committed
148
     */
xiongziliang committed
149 150
    const string &getVps() const {
        return _vps;
xiongziliang committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    }

    /**
     * 返回不带0x00 00 00 01头的sps
     */
    const string &getSps() const {
        return _sps;
    }

    /**
     * 返回不带0x00 00 00 01头的pps
     */
    const string &getPps() const {
        return _pps;
    }

    CodecId getCodecId() const override {
        return CodecH265;
    }

zqsong committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
    /**
     * 返回视频高度
     */
    int getVideoHeight() const override{
        return _height ;
    }

    /**
     * 返回视频宽度
     */
    int getVideoWidth() const override{
        return _width;
    }

    /**
     * 返回视频fps
     */
    float getVideoFps() const override{
        return _fps;
    }

xiongziliang committed
192
    bool ready() override {
xiongziliang committed
193
        return !_vps.empty() && !_sps.empty() && !_pps.empty();
194
    }
xiongziliang committed
195 196

    /**
xiongziliang committed
197 198 199
     * 输入数据帧,并获取sps pps
     * @param frame 数据帧
     */
200
    void inputFrame(const Frame::Ptr &frame) override{
201
        int type = H265_TYPE(*((uint8_t *)frame->data() + frame->prefixSize()));
xiongziliang committed
202
        if(frame->configFrame()){
203 204 205 206 207 208 209
            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);
        }
210 211 212 213
    }

private:
    /**
xiongziliang committed
214 215 216
     * 输入数据帧,并获取sps pps
     * @param frame 数据帧
     */
217
    void inputFrame_l(const Frame::Ptr &frame) {
xiongziliang committed
218 219
        int type = H265_TYPE(((uint8_t *) frame->data() + frame->prefixSize())[0]);
        if (H265Frame::isKeyFrame(type)) {
xiongziliang committed
220
            insertConfigFrame(frame);
xiongziliang committed
221
            VideoTrack::inputFrame(frame);
xiongziliang committed
222
            _last_frame_is_idr = true;
xiongziliang committed
223 224 225
            return;
        }

xiongziliang committed
226 227
        _last_frame_is_idr = false;

xiongziliang committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
        //非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;
        }
    }
254

255
    /**
zqsong committed
256 257 258
     * 解析sps获取宽高fps
     */
    void onReady(){
xiongziliang committed
259
        getHEVCInfo(_vps, _sps, _width, _height, _fps);
zqsong committed
260
    }
xiongziliang committed
261 262 263 264
    Track::Ptr clone() override {
        return std::make_shared<std::remove_reference<decltype(*this)>::type>(*this);
    }

xiongziliang committed
265 266 267
    //生成sdp
    Sdp::Ptr getSdp() override ;

xiongziliang committed
268 269 270 271 272 273
    //在idr帧前插入vps sps pps帧
    void insertConfigFrame(const Frame::Ptr &frame){
        if(_last_frame_is_idr){
            return;
        }
        if(!_vps.empty()){
xiongziliang committed
274
            auto vpsFrame = std::make_shared<H265Frame>();
275 276 277 278
            vpsFrame->_prefix_size = 4;
            vpsFrame->_buffer.assign("\x0\x0\x0\x1", 4);
            vpsFrame->_buffer.append(_vps);
            vpsFrame->_dts = frame->dts();
xiongziliang committed
279
            VideoTrack::inputFrame(vpsFrame);
xiongziliang committed
280 281
        }
        if (!_sps.empty()) {
xiongziliang committed
282
            auto spsFrame = std::make_shared<H265Frame>();
283 284 285 286
            spsFrame->_prefix_size = 4;
            spsFrame->_buffer.assign("\x0\x0\x0\x1", 4);
            spsFrame->_buffer.append(_sps);
            spsFrame->_dts = frame->dts();
xiongziliang committed
287
            VideoTrack::inputFrame(spsFrame);
xiongziliang committed
288 289 290
        }

        if (!_pps.empty()) {
xiongziliang committed
291
            auto ppsFrame = std::make_shared<H265Frame>();
292 293 294 295
            ppsFrame->_prefix_size = 4;
            ppsFrame->_buffer.assign("\x0\x0\x0\x1", 4);
            ppsFrame->_buffer.append(_pps);
            ppsFrame->_dts = frame->dts();
xiongziliang committed
296
            VideoTrack::inputFrame(ppsFrame);
xiongziliang committed
297 298
        }
    }
xiongziliang committed
299 300 301 302
private:
    string _vps;
    string _sps;
    string _pps;
zqsong committed
303 304 305
    int _width = 0;
    int _height = 0;	
    float _fps = 0;
xiongziliang committed
306
    bool _last_frame_is_idr = false;
307 308
};

309 310 311 312 313 314
/**
* h265类型sdp
*/
class H265Sdp : public Sdp {
public:
    /**
xiongziliang committed
315
     * 构造函数
316 317 318 319 320
     * @param sps 265 sps,不带0x00000001头
     * @param pps 265 pps,不带0x00000001头
     * @param playload_type  rtp playload type 默认96
     * @param bitrate 比特率
     */
xiongziliang committed
321 322
    H265Sdp(const string &strVPS,
            const string &strSPS,
323 324 325 326 327 328 329
            const string &strPPS,
            int playload_type = 96,
            int bitrate = 4000) : Sdp(90000,playload_type) {
        //视频通道
        _printer << "m=video 0 RTP/AVP " << playload_type << "\r\n";
        _printer << "b=AS:" << bitrate << "\r\n";
        _printer << "a=rtpmap:" << playload_type << " H265/" << 90000 << "\r\n";
xiongziliang committed
330 331 332 333
        _printer << "a=fmtp:" << playload_type << " ";
        _printer << "sprop-vps=";
        _printer << encodeBase64(strVPS) << "; ";
        _printer << "sprop-sps=";
334 335 336
        _printer << encodeBase64(strSPS) << "; ";
        _printer << "sprop-pps=";
        _printer << encodeBase64(strPPS) << "\r\n";
xiongziliang committed
337
        _printer << "a=control:trackID=" << (int)TrackVideo << "\r\n";
338 339 340 341 342 343 344 345 346 347 348 349 350
    }

    string getSdp() const override {
        return _printer;
    }

    CodecId getCodecId() const override {
        return CodecH265;
    }
private:
    _StrPrinter _printer;
};

351
}//namespace mediakit
xiongziliang committed
352
#endif //ZLMEDIAKIT_H265_H