2018-10-30 14:59:42 +08:00
|
|
|
|
/*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
|
2020-04-04 20:30:09 +08:00
|
|
|
|
*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
|
2020-04-04 20:30:09 +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-30 14:59:42 +08:00
|
|
|
|
|
|
|
|
|
#ifndef ZLMEDIAKIT_H265_H
|
|
|
|
|
#define ZLMEDIAKIT_H265_H
|
|
|
|
|
|
2019-07-30 13:54:19 +08:00
|
|
|
|
#include "H264.h"
|
2023-12-10 10:21:40 +08:00
|
|
|
|
#include "Extension/Track.h"
|
|
|
|
|
#include "Extension/Frame.h"
|
2022-02-02 20:34:50 +08:00
|
|
|
|
|
2019-06-28 16:12:39 +08:00
|
|
|
|
#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 {
|
|
|
|
|
|
2021-07-09 13:38:20 +08:00
|
|
|
|
template<typename Parent>
|
|
|
|
|
class H265FrameHelper : public Parent{
|
2018-10-30 14:59:42 +08:00
|
|
|
|
public:
|
2021-07-09 13:38:20 +08:00
|
|
|
|
friend class FrameImp;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
friend class toolkit::ResourcePool_l<H265FrameHelper>;
|
2021-07-09 13:38:20 +08:00
|
|
|
|
using Ptr = std::shared_ptr<H265FrameHelper>;
|
|
|
|
|
|
2021-02-05 11:51:16 +08:00
|
|
|
|
enum {
|
2018-10-30 15:56:00 +08:00
|
|
|
|
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,
|
2020-04-29 10:01:56 +08:00
|
|
|
|
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,
|
2021-02-05 11:51:16 +08:00
|
|
|
|
};
|
2018-10-30 14:59:42 +08:00
|
|
|
|
|
2021-07-09 13:38:20 +08:00
|
|
|
|
template<typename ...ARGS>
|
|
|
|
|
H265FrameHelper(ARGS &&...args): Parent(std::forward<ARGS>(args)...) {
|
|
|
|
|
this->_codec_id = CodecH265;
|
|
|
|
|
}
|
2019-08-01 18:49:04 +08:00
|
|
|
|
|
2021-07-09 13:38:20 +08:00
|
|
|
|
bool keyFrame() const override {
|
|
|
|
|
auto nal_ptr = (uint8_t *) this->data() + this->prefixSize();
|
|
|
|
|
auto type = H265_TYPE(*nal_ptr);
|
2021-08-18 22:26:24 +08:00
|
|
|
|
// 参考自FFmpeg: IRAP VCL NAL unit types span the range
|
|
|
|
|
// [BLA_W_LP (16), RSV_IRAP_VCL23 (23)].
|
|
|
|
|
return (type >= NAL_BLA_W_LP && type <= NAL_RSV_IRAP_VCL23) && decodeAble() ;
|
2021-07-09 13:38:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool configFrame() const override {
|
|
|
|
|
auto nal_ptr = (uint8_t *) this->data() + this->prefixSize();
|
|
|
|
|
switch (H265_TYPE(*nal_ptr)) {
|
|
|
|
|
case NAL_VPS:
|
|
|
|
|
case NAL_SPS:
|
|
|
|
|
case NAL_PPS : return true;
|
|
|
|
|
default : return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-01 18:49:04 +08:00
|
|
|
|
|
2021-07-09 13:38:20 +08:00
|
|
|
|
bool dropAble() const override {
|
|
|
|
|
auto nal_ptr = (uint8_t *) this->data() + this->prefixSize();
|
2021-07-09 14:02:25 +08:00
|
|
|
|
switch (H265_TYPE(*nal_ptr)) {
|
2021-07-09 13:38:20 +08:00
|
|
|
|
case NAL_AUD:
|
|
|
|
|
case NAL_SEI_SUFFIX:
|
|
|
|
|
case NAL_SEI_PREFIX: return true;
|
|
|
|
|
default: return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool decodeAble() const override {
|
|
|
|
|
auto nal_ptr = (uint8_t *) this->data() + this->prefixSize();
|
|
|
|
|
auto type = H265_TYPE(*nal_ptr);
|
|
|
|
|
//多slice情况下, first_slice_segment_in_pic_flag 表示其为一帧的开始
|
2021-07-15 17:36:44 +08:00
|
|
|
|
return type >= NAL_TRAIL_N && type <= NAL_RSV_IRAP_VCL23 && (nal_ptr[2] & 0x80);
|
2021-07-09 13:38:20 +08:00
|
|
|
|
}
|
2018-10-30 15:56:00 +08:00
|
|
|
|
};
|
|
|
|
|
|
2021-07-09 13:38:20 +08:00
|
|
|
|
/**
|
|
|
|
|
* 265帧类
|
|
|
|
|
*/
|
|
|
|
|
using H265Frame = H265FrameHelper<FrameImp>;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 防止内存拷贝的H265类
|
|
|
|
|
* 用户可以通过该类型快速把一个指针无拷贝的包装成Frame类
|
|
|
|
|
*/
|
|
|
|
|
using H265FrameNoCacheAble = H265FrameHelper<FrameFromPtr>;
|
|
|
|
|
|
2018-10-30 15:56:00 +08:00
|
|
|
|
/**
|
|
|
|
|
* 265视频通道
|
|
|
|
|
*/
|
|
|
|
|
class H265Track : public VideoTrack {
|
|
|
|
|
public:
|
2021-02-05 11:51:16 +08:00
|
|
|
|
using Ptr = std::shared_ptr<H265Track>;
|
2018-10-30 15:56:00 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 不指定sps pps构造h265类型的媒体
|
|
|
|
|
* 在随后的inputFrame中获取sps pps
|
|
|
|
|
*/
|
2021-02-05 11:51:16 +08:00
|
|
|
|
H265Track() = default;
|
2018-10-30 15:56:00 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造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 265头长度,可以为3个或4个字节,一般为0x00 00 00 01
|
2018-10-30 15:56:00 +08:00
|
|
|
|
* @param sps_prefix_len 265头长度,可以为3个或4个字节,一般为0x00 00 00 01
|
|
|
|
|
* @param pps_prefix_len 265头长度,可以为3个或4个字节,一般为0x00 00 00 01
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
H265Track(const std::string &vps,const std::string &sps, const std::string &pps,int vps_prefix_len = 4, int sps_prefix_len = 4, int pps_prefix_len = 4);
|
2018-10-30 15:56:00 +08:00
|
|
|
|
|
2023-12-09 16:23:51 +08:00
|
|
|
|
bool ready() const override;
|
2021-02-05 11:51:16 +08:00
|
|
|
|
CodecId getCodecId() const override;
|
|
|
|
|
int getVideoWidth() const override;
|
|
|
|
|
int getVideoHeight() const override;
|
|
|
|
|
float getVideoFps() const override;
|
2021-09-27 13:12:53 +08:00
|
|
|
|
bool inputFrame(const Frame::Ptr &frame) override;
|
2023-12-09 16:23:51 +08:00
|
|
|
|
toolkit::Buffer::Ptr getExtraData() const override;
|
|
|
|
|
void setExtraData(const uint8_t *data, size_t size) override;
|
2023-11-07 23:36:41 +08:00
|
|
|
|
bool update() override;
|
2024-06-21 20:46:17 +08:00
|
|
|
|
std::vector<Frame::Ptr> getConfigFrames() const override;
|
2019-07-30 13:54:19 +08:00
|
|
|
|
|
|
|
|
|
private:
|
2023-12-09 16:23:51 +08:00
|
|
|
|
Sdp::Ptr getSdp(uint8_t payload_type) const override;
|
|
|
|
|
Track::Ptr clone() const override;
|
2021-09-27 13:12:53 +08:00
|
|
|
|
bool inputFrame_l(const Frame::Ptr &frame);
|
2021-02-05 11:51:16 +08:00
|
|
|
|
void insertConfigFrame(const Frame::Ptr &frame);
|
2019-07-30 13:54:19 +08:00
|
|
|
|
|
2018-10-30 15:56:00 +08:00
|
|
|
|
private:
|
2024-09-17 09:43:37 +08:00
|
|
|
|
bool _latest_is_config_frame = false;
|
2021-02-05 11:51:16 +08:00
|
|
|
|
int _width = 0;
|
|
|
|
|
int _height = 0;
|
|
|
|
|
float _fps = 0;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::string _vps;
|
|
|
|
|
std::string _sps;
|
|
|
|
|
std::string _pps;
|
2018-10-30 21:05:48 +08:00
|
|
|
|
};
|
|
|
|
|
|
2018-10-30 14:59:42 +08:00
|
|
|
|
}//namespace mediakit
|
2020-05-11 22:33:10 +08:00
|
|
|
|
#endif //ZLMEDIAKIT_H265_H
|