Commit 4ce1a25f by xiongziliang

修复MP4解复用时不写入adts头的问题

parent 071d0a9f
...@@ -160,6 +160,8 @@ struct Context{ ...@@ -160,6 +160,8 @@ struct Context{
BufferRaw::Ptr buffer; BufferRaw::Ptr buffer;
}; };
#define DATA_OFFSET ADTS_HEADER_LEN
Frame::Ptr MP4Demuxer::readFrame(bool &keyFrame, bool &eof) { Frame::Ptr MP4Demuxer::readFrame(bool &keyFrame, bool &eof) {
keyFrame = false; keyFrame = false;
eof = false; eof = false;
...@@ -174,9 +176,9 @@ Frame::Ptr MP4Demuxer::readFrame(bool &keyFrame, bool &eof) { ...@@ -174,9 +176,9 @@ Frame::Ptr MP4Demuxer::readFrame(bool &keyFrame, bool &eof) {
static mov_onalloc mov_onalloc = [](void *param, int bytes) -> void * { static mov_onalloc mov_onalloc = [](void *param, int bytes) -> void * {
Context *ctx = (Context *) param; Context *ctx = (Context *) param;
ctx->buffer = ctx->thiz->_buffer_pool.obtain(); ctx->buffer = ctx->thiz->_buffer_pool.obtain();
ctx->buffer->setCapacity(bytes + 1); ctx->buffer->setCapacity(bytes + DATA_OFFSET + 1);
ctx->buffer->setSize(bytes); ctx->buffer->setSize(bytes + DATA_OFFSET);
return ctx->buffer->data(); return ctx->buffer->data() + DATA_OFFSET;
}; };
Context ctx = {this, 0}; Context ctx = {this, 0};
...@@ -204,11 +206,11 @@ template <typename Parent> ...@@ -204,11 +206,11 @@ template <typename Parent>
class FrameWrapper : public Parent{ class FrameWrapper : public Parent{
public: public:
~FrameWrapper() = default; ~FrameWrapper() = default;
FrameWrapper(const Buffer::Ptr &buf, int64_t pts, int64_t dts, int prefix) : Parent(buf->data(), buf->size(), dts, pts, prefix){ 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; _buf = buf;
} }
FrameWrapper(CodecId codec,const Buffer::Ptr &buf, int64_t pts, int64_t dts, int prefix) : Parent(codec, buf->data(), buf->size(), dts, pts, prefix){ 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; _buf = buf;
} }
...@@ -224,37 +226,44 @@ Frame::Ptr MP4Demuxer::makeFrame(uint32_t track_id, const Buffer::Ptr &buf, int6 ...@@ -224,37 +226,44 @@ Frame::Ptr MP4Demuxer::makeFrame(uint32_t track_id, const Buffer::Ptr &buf, int6
if (it == _track_to_codec.end()) { if (it == _track_to_codec.end()) {
return nullptr; return nullptr;
} }
auto numBytes = buf->size(); auto bytes = buf->size() - DATA_OFFSET;
auto pBytes = buf->data(); auto data = buf->data() + DATA_OFFSET;
auto codec = it->second->getCodecId(); auto codec = it->second->getCodecId();
switch (codec) { switch (codec) {
case CodecH264 : case CodecH264 :
case CodecH265 : { case CodecH265 : {
uint32_t iOffset = 0; uint32_t offset = 0;
while (iOffset < numBytes) { while (offset < bytes) {
uint32_t iFrameLen; uint32_t frame_len;
memcpy(&iFrameLen, pBytes + iOffset, 4); memcpy(&frame_len, data + offset, 4);
iFrameLen = ntohl(iFrameLen); frame_len = ntohl(frame_len);
if (iFrameLen + iOffset + 4 > numBytes) { if (frame_len + offset + 4 > bytes) {
return nullptr; return nullptr;
} }
memcpy(pBytes + iOffset, "\x0\x0\x0\x1", 4); memcpy(data + offset, "\x0\x0\x0\x1", 4);
iOffset += (iFrameLen + 4); offset += (frame_len + 4);
} }
if (codec == CodecH264) { if (codec == CodecH264) {
return std::make_shared<FrameWrapper<H264FrameNoCacheAble> >(buf, pts, dts, 4); return std::make_shared<FrameWrapper<H264FrameNoCacheAble> >(buf, pts, dts, 4, DATA_OFFSET);
} }
return std::make_shared<FrameWrapper<H265FrameNoCacheAble> >(buf, pts, dts, 4); return std::make_shared<FrameWrapper<H265FrameNoCacheAble> >(buf, pts, dts, 4, DATA_OFFSET);
}
case CodecAAC: {
AACTrack::Ptr track = dynamic_pointer_cast<AACTrack>(it->second);
assert(track);
//加上adts头
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);
} }
case CodecOpus: case CodecOpus:
case CodecAAC:
case CodecG711A: case CodecG711A:
case CodecG711U: { case CodecG711U: {
return std::make_shared<FrameWrapper<FrameFromPtr> >(codec, buf, pts, dts, 0); return std::make_shared<FrameWrapper<FrameFromPtr> >(buf, pts, dts, 0, DATA_OFFSET, codec);
} }
default:
return nullptr; default: return nullptr;
} }
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论