减少内存拷贝,提升性能

This commit is contained in:
xiongziliang 2020-09-21 14:56:58 +08:00
parent 416d21df36
commit efdc105b40
3 changed files with 50 additions and 25 deletions

View File

@ -383,5 +383,49 @@ protected:
CodecId _codec_id = CodecInvalid; CodecId _codec_id = CodecInvalid;
}; };
/**
* Buffer对象转换成可缓存的Frame对象
*/
template <typename Parent>
class FrameWrapper : public Parent{
public:
~FrameWrapper() = default;
/**
* frame
* @param buf
* @param dts
* @param pts
* @param prefix
* @param offset buffer有效数据偏移量
*/
FrameWrapper(const Buffer::Ptr &buf, int64_t dts, int64_t pts, int prefix, int offset) : Parent(buf->data() + offset, buf->size() - offset, dts, pts, prefix){
_buf = buf;
}
/**
* frame
* @param buf
* @param dts
* @param pts
* @param prefix
* @param offset buffer有效数据偏移量
* @param codec
*/
FrameWrapper(const Buffer::Ptr &buf, int64_t dts, int64_t pts, int prefix, int offset, CodecId codec) : Parent(codec, buf->data() + offset, buf->size() - offset, dts, pts, prefix){
_buf = buf;
}
/**
*
*/
bool cacheAble() const override {
return true;
}
private:
Buffer::Ptr _buf;
};
}//namespace mediakit }//namespace mediakit
#endif //ZLMEDIAKIT_FRAME_H #endif //ZLMEDIAKIT_FRAME_H

View File

@ -202,25 +202,6 @@ Frame::Ptr MP4Demuxer::readFrame(bool &keyFrame, bool &eof) {
} }
} }
template <typename Parent>
class FrameWrapper : public Parent{
public:
~FrameWrapper() = default;
FrameWrapper(const Buffer::Ptr &buf, int64_t pts, int64_t dts, int prefix, int offset) : Parent(buf->data() + offset, buf->size() - offset, dts, pts, prefix){
_buf = buf;
}
FrameWrapper(const Buffer::Ptr &buf, int64_t pts, int64_t dts, int prefix, int offset, CodecId codec) : Parent(codec, buf->data() + offset, buf->size() - offset, dts, pts, prefix){
_buf = buf;
}
bool cacheAble() const override {
return true;
}
private:
Buffer::Ptr _buf;
};
Frame::Ptr MP4Demuxer::makeFrame(uint32_t track_id, const Buffer::Ptr &buf, int64_t pts, int64_t dts) { Frame::Ptr MP4Demuxer::makeFrame(uint32_t track_id, const Buffer::Ptr &buf, int64_t pts, int64_t dts) {
auto it = _track_to_codec.find(track_id); auto it = _track_to_codec.find(track_id);
if (it == _track_to_codec.end()) { if (it == _track_to_codec.end()) {
@ -244,9 +225,9 @@ Frame::Ptr MP4Demuxer::makeFrame(uint32_t track_id, const Buffer::Ptr &buf, int6
offset += (frame_len + 4); offset += (frame_len + 4);
} }
if (codec == CodecH264) { if (codec == CodecH264) {
return std::make_shared<FrameWrapper<H264FrameNoCacheAble> >(buf, pts, dts, 4, DATA_OFFSET); return std::make_shared<FrameWrapper<H264FrameNoCacheAble> >(buf, dts, pts, 4, DATA_OFFSET);
} }
return std::make_shared<FrameWrapper<H265FrameNoCacheAble> >(buf, pts, dts, 4, DATA_OFFSET); return std::make_shared<FrameWrapper<H265FrameNoCacheAble> >(buf, dts, pts, 4, DATA_OFFSET);
} }
case CodecAAC: { case CodecAAC: {
@ -254,13 +235,13 @@ Frame::Ptr MP4Demuxer::makeFrame(uint32_t track_id, const Buffer::Ptr &buf, int6
assert(track); assert(track);
//加上adts头 //加上adts头
dumpAacConfig(track->getAacCfg(), buf->size() - DATA_OFFSET, (uint8_t *) buf->data() + (DATA_OFFSET - ADTS_HEADER_LEN), ADTS_HEADER_LEN); dumpAacConfig(track->getAacCfg(), buf->size() - DATA_OFFSET, (uint8_t *) buf->data() + (DATA_OFFSET - ADTS_HEADER_LEN), ADTS_HEADER_LEN);
return std::make_shared<FrameWrapper<FrameFromPtr> >(buf, pts, dts, ADTS_HEADER_LEN, DATA_OFFSET - ADTS_HEADER_LEN, codec); return std::make_shared<FrameWrapper<FrameFromPtr> >(buf, dts, pts, ADTS_HEADER_LEN, DATA_OFFSET - ADTS_HEADER_LEN, codec);
} }
case CodecOpus: case CodecOpus:
case CodecG711A: case CodecG711A:
case CodecG711U: { case CodecG711U: {
return std::make_shared<FrameWrapper<FrameFromPtr> >(buf, pts, dts, 0, DATA_OFFSET, codec); return std::make_shared<FrameWrapper<FrameFromPtr> >(buf, dts, pts, 0, DATA_OFFSET, codec);
} }
default: return nullptr; default: return nullptr;

View File

@ -135,7 +135,7 @@ void DecoderImp::onDecode(int stream,int codecid,int flags,int64_t pts,int64_t d
auto frame = std::make_shared<H264FrameNoCacheAble>((char *) data, bytes, dts, pts,0); auto frame = std::make_shared<H264FrameNoCacheAble>((char *) data, bytes, dts, pts,0);
_merger.inputFrame(frame,[this](uint32_t dts, uint32_t pts, const Buffer::Ptr &buffer) { _merger.inputFrame(frame,[this](uint32_t dts, uint32_t pts, const Buffer::Ptr &buffer) {
onFrame(std::make_shared<H264FrameNoCacheAble>(buffer->data(), buffer->size(), dts, pts, prefixSize(buffer->data(), buffer->size()))); onFrame(std::make_shared<FrameWrapper<H264FrameNoCacheAble> >(buffer, dts, pts, prefixSize(buffer->data(), buffer->size()), 0));
}); });
break; break;
} }
@ -154,7 +154,7 @@ void DecoderImp::onDecode(int stream,int codecid,int flags,int64_t pts,int64_t d
} }
auto frame = std::make_shared<H265FrameNoCacheAble>((char *) data, bytes, dts, pts, 0); auto frame = std::make_shared<H265FrameNoCacheAble>((char *) data, bytes, dts, pts, 0);
_merger.inputFrame(frame,[this](uint32_t dts, uint32_t pts, const Buffer::Ptr &buffer) { _merger.inputFrame(frame,[this](uint32_t dts, uint32_t pts, const Buffer::Ptr &buffer) {
onFrame(std::make_shared<H265FrameNoCacheAble>(buffer->data(), buffer->size(), dts, pts, prefixSize(buffer->data(), buffer->size()))); onFrame(std::make_shared<FrameWrapper<H265FrameNoCacheAble> >(buffer, dts, pts, prefixSize(buffer->data(), buffer->size()), 0));
}); });
break; break;
} }