2019-08-08 19:01:45 +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-21 21:21:14 +08:00
|
|
|
|
|
2018-10-30 14:59:42 +08:00
|
|
|
|
#ifndef ZLMEDIAKIT_H264_H
|
|
|
|
|
#define ZLMEDIAKIT_H264_H
|
2018-10-21 21:21:14 +08:00
|
|
|
|
|
2023-12-10 10:21:40 +08:00
|
|
|
|
#include "Extension/Frame.h"
|
|
|
|
|
#include "Extension/Track.h"
|
2022-02-02 20:34:50 +08:00
|
|
|
|
|
2018-10-30 15:56:00 +08:00
|
|
|
|
#define H264_TYPE(v) ((uint8_t)(v) & 0x1F)
|
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
namespace mediakit{
|
2018-10-21 21:21:14 +08:00
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
void splitH264(const char *ptr, size_t len, size_t prefix, const std::function<void(const char *, size_t, size_t)> &cb);
|
|
|
|
|
size_t prefixSize(const char *ptr, size_t len);
|
2021-02-05 11:51:16 +08:00
|
|
|
|
|
2021-07-09 13:38:20 +08:00
|
|
|
|
template<typename Parent>
|
|
|
|
|
class H264FrameHelper : public Parent{
|
2018-10-21 21:21:14 +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<H264FrameHelper>;
|
2021-07-09 13:38:20 +08:00
|
|
|
|
using Ptr = std::shared_ptr<H264FrameHelper>;
|
2018-10-23 18:39:17 +08:00
|
|
|
|
|
2021-02-05 11:51:16 +08:00
|
|
|
|
enum {
|
2018-10-30 15:56:00 +08:00
|
|
|
|
NAL_IDR = 5,
|
2020-01-13 11:51:29 +08:00
|
|
|
|
NAL_SEI = 6,
|
2020-09-12 13:45:16 +08:00
|
|
|
|
NAL_SPS = 7,
|
|
|
|
|
NAL_PPS = 8,
|
|
|
|
|
NAL_AUD = 9,
|
2020-09-12 19:06:26 +08:00
|
|
|
|
NAL_B_P = 1,
|
2021-02-05 11:51:16 +08:00
|
|
|
|
};
|
2018-10-30 15:56:00 +08:00
|
|
|
|
|
2021-07-09 13:38:20 +08:00
|
|
|
|
template<typename ...ARGS>
|
|
|
|
|
H264FrameHelper(ARGS &&...args): Parent(std::forward<ARGS>(args)...) {
|
|
|
|
|
this->_codec_id = CodecH264;
|
|
|
|
|
}
|
2018-10-23 14:32:06 +08:00
|
|
|
|
|
2021-07-09 13:38:20 +08:00
|
|
|
|
bool keyFrame() const override {
|
|
|
|
|
auto nal_ptr = (uint8_t *) this->data() + this->prefixSize();
|
|
|
|
|
return H264_TYPE(*nal_ptr) == NAL_IDR && decodeAble();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool configFrame() const override {
|
|
|
|
|
auto nal_ptr = (uint8_t *) this->data() + this->prefixSize();
|
|
|
|
|
switch (H264_TYPE(*nal_ptr)) {
|
|
|
|
|
case NAL_SPS:
|
|
|
|
|
case NAL_PPS: return true;
|
|
|
|
|
default: return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool dropAble() const override {
|
|
|
|
|
auto nal_ptr = (uint8_t *) this->data() + this->prefixSize();
|
|
|
|
|
switch (H264_TYPE(*nal_ptr)) {
|
|
|
|
|
case NAL_SEI:
|
|
|
|
|
case NAL_AUD: return true;
|
|
|
|
|
default: return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool decodeAble() const override {
|
|
|
|
|
auto nal_ptr = (uint8_t *) this->data() + this->prefixSize();
|
|
|
|
|
auto type = H264_TYPE(*nal_ptr);
|
|
|
|
|
//多slice情况下, first_mb_in_slice 表示其为一帧的开始
|
|
|
|
|
return type >= NAL_B_P && type <= NAL_IDR && (nal_ptr[1] & 0x80);
|
|
|
|
|
}
|
2018-10-21 21:21:14 +08:00
|
|
|
|
};
|
|
|
|
|
|
2021-07-09 13:38:20 +08:00
|
|
|
|
/**
|
|
|
|
|
* 264帧类
|
|
|
|
|
*/
|
|
|
|
|
using H264Frame = H264FrameHelper<FrameImp>;
|
|
|
|
|
|
2019-07-25 12:09:36 +08:00
|
|
|
|
/**
|
|
|
|
|
* 防止内存拷贝的H264类
|
|
|
|
|
* 用户可以通过该类型快速把一个指针无拷贝的包装成Frame类
|
|
|
|
|
*/
|
2021-07-09 13:38:20 +08:00
|
|
|
|
using H264FrameNoCacheAble = H264FrameHelper<FrameFromPtr>;
|
2018-10-21 21:21:14 +08:00
|
|
|
|
|
2018-10-27 22:54:16 +08:00
|
|
|
|
/**
|
|
|
|
|
* 264视频通道
|
|
|
|
|
*/
|
2023-02-05 22:00:36 +08:00
|
|
|
|
class H264Track : public VideoTrack {
|
2018-10-21 21:21:14 +08:00
|
|
|
|
public:
|
2021-02-05 11:51:16 +08:00
|
|
|
|
using Ptr = std::shared_ptr<H264Track>;
|
2018-10-23 14:32:06 +08:00
|
|
|
|
|
2018-10-23 16:41:25 +08:00
|
|
|
|
/**
|
|
|
|
|
* 不指定sps pps构造h264类型的媒体
|
|
|
|
|
* 在随后的inputFrame中获取sps pps
|
|
|
|
|
*/
|
2021-02-05 11:51:16 +08:00
|
|
|
|
H264Track() = default;
|
|
|
|
|
|
2018-10-23 14:32:06 +08:00
|
|
|
|
/**
|
|
|
|
|
* 构造h264类型的媒体
|
|
|
|
|
* @param sps sps帧数据
|
|
|
|
|
* @param pps pps帧数据
|
|
|
|
|
* @param sps_prefix_len 264头长度,可以为3个或4个字节,一般为0x00 00 00 01
|
|
|
|
|
* @param pps_prefix_len 264头长度,可以为3个或4个字节,一般为0x00 00 00 01
|
|
|
|
|
*/
|
2023-12-09 16:23:51 +08:00
|
|
|
|
H264Track(const std::string &sps, const std::string &pps, int sps_prefix_len = 4, int pps_prefix_len = 4);
|
2018-10-23 14:32:06 +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 getVideoHeight() const override;
|
|
|
|
|
int getVideoWidth() 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;
|
2018-10-23 16:41:25 +08:00
|
|
|
|
|
2019-01-30 11:44:41 +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);
|
2018-10-23 16:41:25 +08:00
|
|
|
|
|
2019-04-09 12:39:38 +08:00
|
|
|
|
private:
|
2022-04-10 23:38:42 +08:00
|
|
|
|
bool _latest_is_config_frame = false;
|
2018-10-23 14:32:06 +08:00
|
|
|
|
int _width = 0;
|
|
|
|
|
int _height = 0;
|
|
|
|
|
float _fps = 0;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::string _sps;
|
|
|
|
|
std::string _pps;
|
2018-10-30 17:58:10 +08:00
|
|
|
|
};
|
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
}//namespace mediakit
|
2022-04-10 23:38:42 +08:00
|
|
|
|
#endif //ZLMEDIAKIT_H264_H
|