ZLMediaKit/src/Extension/H264.h

150 lines
4.2 KiB
C++
Raw Normal View History

2019-08-08 19:01:45 +08:00
/*
2020-04-04 20:30:09 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
2018-10-25 10:00:17 +08:00
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
2018-10-25 10:00:17 +08:00
*
2020-04-04 20:30:09 +08:00
* 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-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
#include "Frame.h"
2018-10-30 14:59:42 +08:00
#include "Track.h"
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
bool getAVCInfo(const std::string &strSps,int &iVideoWidth, int &iVideoHeight, float &iVideoFps);
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
template<typename Parent>
class H264FrameHelper : public Parent{
2018-10-21 21:21:14 +08:00
public:
friend class FrameImp;
friend class toolkit::ResourcePool_l<H264FrameHelper>;
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,
NAL_SEI = 6,
2020-09-12 13:45:16 +08:00
NAL_SPS = 7,
NAL_PPS = 8,
NAL_AUD = 9,
NAL_B_P = 1,
2021-02-05 11:51:16 +08:00
};
2018-10-30 15:56:00 +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
~H264FrameHelper() override = default;
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
};
/**
* 264
*/
using H264Frame = H264FrameHelper<FrameImp>;
/**
* H264类
* Frame类
*/
using H264FrameNoCacheAble = H264FrameHelper<FrameFromPtr>;
2018-10-21 21:21:14 +08:00
2018-10-27 22:54:16 +08:00
/**
* 264
*/
2018-10-23 11:47:27 +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 264340x00 00 00 01
* @param pps_prefix_len 264340x00 00 00 01
*/
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
/**
* h264类型的媒体
* @param sps sps帧
* @param pps pps帧
*/
2021-02-05 11:51:16 +08:00
H264Track(const Frame::Ptr &sps,const Frame::Ptr &pps);
2018-10-23 14:32:06 +08:00
/**
2021-02-05 11:51:16 +08:00
* 0x00 00 00 01sps/pps
2018-10-23 14:32:06 +08:00
*/
const std::string &getSps() const;
const std::string &getPps() const;
2018-10-23 14:32:06 +08:00
2021-02-05 11:51:16 +08:00
bool ready() override;
CodecId getCodecId() const override;
int getVideoHeight() const override;
int getVideoWidth() const override;
float getVideoFps() const override;
bool inputFrame(const Frame::Ptr &frame) override;
2018-10-23 16:41:25 +08:00
private:
2021-02-05 11:51:16 +08:00
void onReady();
Sdp::Ptr getSdp() override;
Track::Ptr clone() override;
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;
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