兼容把SPS PPS IDR打包在一起的帧

This commit is contained in:
xiongziliang 2019-01-30 11:44:41 +08:00
parent 7ba81499ec
commit cc7556b5a8
3 changed files with 86 additions and 12 deletions

@ -1 +1 @@
Subproject commit 5573789f4ec0546f8788bc7be3d564de6e1365d6 Subproject commit 8c1a0f88a0b8e332c3eaf04dbb9a8f2402b267ba

View File

@ -51,6 +51,32 @@ bool getAVCInfo(const char * sps,int sps_len,int &iVideoWidth, int &iVideoHeight
return true; return true;
} }
const char *memfind(const char *buf, int len, const char *subbuf, int sublen) {
for (auto i = 0; i < len - sublen; ++i) {
if (memcmp(buf + i, subbuf, sublen) == 0) {
return buf + i;
}
}
return NULL;
}
void splitH264(const char *ptr, int len, const std::function<void(const char *, int)> &cb) {
auto nal = ptr;
auto end = ptr + len;
while(true) {
auto next_nal = memfind(nal + 3,end - nal - 3,"\x0\x0\x1",3);
if(next_nal){
cb(nal,next_nal - nal);
nal = next_nal;
continue;
}
cb(nal,end - nal);
break;
}
}
}//namespace mediakit }//namespace mediakit

View File

@ -38,6 +38,7 @@ namespace mediakit{
bool getAVCInfo(const string &strSps,int &iVideoWidth, int &iVideoHeight, float &iVideoFps); bool getAVCInfo(const string &strSps,int &iVideoWidth, int &iVideoHeight, float &iVideoFps);
bool getAVCInfo(const char * sps,int sps_len,int &iVideoWidth, int &iVideoHeight, float &iVideoFps); bool getAVCInfo(const char * sps,int sps_len,int &iVideoWidth, int &iVideoHeight, float &iVideoFps);
void splitH264(const char *ptr, int len, const std::function<void(const char *, int)> &cb);
/** /**
* 264 * 264
@ -116,6 +117,20 @@ public:
} }
}; };
class H264FrameSubFrame : public H264FrameNoCopyAble{
public:
typedef std::shared_ptr<H264FrameSubFrame> Ptr;
H264FrameSubFrame(const Frame::Ptr &strongRef,
char *ptr,
uint32_t size,
uint32_t stamp,
int prefixeSize) : H264FrameNoCopyAble(ptr,size,stamp,prefixeSize){
_strongRef = strongRef;
}
private:
Frame::Ptr _strongRef;
};
/** /**
* 264 * 264
@ -204,12 +219,55 @@ public:
return !_sps.empty() && !_pps.empty(); return !_sps.empty() && !_pps.empty();
} }
/** /**
* ,sps pps * ,sps pps
* @param frame * @param frame
*/ */
void inputFrame(const Frame::Ptr &frame) override{ void inputFrame(const Frame::Ptr &frame) override{
int type = H264_TYPE(*((uint8_t *)frame->data() + frame->prefixSize()));
if(type == H264Frame::NAL_SPS){
//有些设备会把SPS PPS IDR帧当做一个帧打包所以我们要split一下
bool first_frame = true;
splitH264(frame->data() + frame->prefixSize(),
frame->size() - frame->prefixSize(),
[&](const char *ptr, int len){
if(first_frame){
H264FrameSubFrame::Ptr sub_frame = std::make_shared<H264FrameSubFrame>(frame,
frame->data(),
len + frame->prefixSize(),
frame->stamp(),
frame->prefixSize());
inputFrame_l(sub_frame);
first_frame = false;
}else{
H264FrameSubFrame::Ptr sub_frame = std::make_shared<H264FrameSubFrame>(frame,
(char *)ptr,
len ,
frame->stamp(),
3);
inputFrame_l(sub_frame);
}
});
} else{
inputFrame_l(frame);
}
}
private:
/**
* sps获取宽高fps
*/
void onReady(){
getAVCInfo(_sps,_width,_height,_fps);
}
Track::Ptr clone() override {
return std::make_shared<std::remove_reference<decltype(*this)>::type >(*this);
}
/**
* ,sps pps
* @param frame
*/
void inputFrame_l(const Frame::Ptr &frame){
int type = H264_TYPE(*((uint8_t *)frame->data() + frame->prefixSize())); int type = H264_TYPE(*((uint8_t *)frame->data() + frame->prefixSize()));
switch (type){ switch (type){
case H264Frame::NAL_SPS:{ case H264Frame::NAL_SPS:{
@ -271,16 +329,6 @@ public:
onReady(); onReady();
} }
} }
private:
/**
* sps获取宽高fps
*/
void onReady(){
getAVCInfo(_sps,_width,_height,_fps);
}
Track::Ptr clone() override {
return std::make_shared<std::remove_reference<decltype(*this)>::type >(*this);
}
private: private:
string _sps; string _sps;
string _pps; string _pps;