From 4ce1a25f090ccfa95542c026ce6d181feb9e3164 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Sun, 20 Sep 2020 19:45:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DMP4=E8=A7=A3=E5=A4=8D?= =?UTF-8?q?=E7=94=A8=E6=97=B6=E4=B8=8D=E5=86=99=E5=85=A5adts=E5=A4=B4?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Record/MP4Demuxer.cpp | 51 +++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/src/Record/MP4Demuxer.cpp b/src/Record/MP4Demuxer.cpp index 7854effd..65c4263d 100644 --- a/src/Record/MP4Demuxer.cpp +++ b/src/Record/MP4Demuxer.cpp @@ -160,6 +160,8 @@ struct Context{ BufferRaw::Ptr buffer; }; +#define DATA_OFFSET ADTS_HEADER_LEN + Frame::Ptr MP4Demuxer::readFrame(bool &keyFrame, bool &eof) { keyFrame = false; eof = false; @@ -174,9 +176,9 @@ Frame::Ptr MP4Demuxer::readFrame(bool &keyFrame, bool &eof) { static mov_onalloc mov_onalloc = [](void *param, int bytes) -> void * { Context *ctx = (Context *) param; ctx->buffer = ctx->thiz->_buffer_pool.obtain(); - ctx->buffer->setCapacity(bytes + 1); - ctx->buffer->setSize(bytes); - return ctx->buffer->data(); + ctx->buffer->setCapacity(bytes + DATA_OFFSET + 1); + ctx->buffer->setSize(bytes + DATA_OFFSET); + return ctx->buffer->data() + DATA_OFFSET; }; Context ctx = {this, 0}; @@ -204,11 +206,11 @@ template class FrameWrapper : public Parent{ public: ~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; } - 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; } @@ -224,37 +226,44 @@ Frame::Ptr MP4Demuxer::makeFrame(uint32_t track_id, const Buffer::Ptr &buf, int6 if (it == _track_to_codec.end()) { return nullptr; } - auto numBytes = buf->size(); - auto pBytes = buf->data(); + auto bytes = buf->size() - DATA_OFFSET; + auto data = buf->data() + DATA_OFFSET; auto codec = it->second->getCodecId(); switch (codec) { case CodecH264 : case CodecH265 : { - uint32_t iOffset = 0; - while (iOffset < numBytes) { - uint32_t iFrameLen; - memcpy(&iFrameLen, pBytes + iOffset, 4); - iFrameLen = ntohl(iFrameLen); - if (iFrameLen + iOffset + 4 > numBytes) { + uint32_t offset = 0; + while (offset < bytes) { + uint32_t frame_len; + memcpy(&frame_len, data + offset, 4); + frame_len = ntohl(frame_len); + if (frame_len + offset + 4 > bytes) { return nullptr; } - memcpy(pBytes + iOffset, "\x0\x0\x0\x1", 4); - iOffset += (iFrameLen + 4); + memcpy(data + offset, "\x0\x0\x0\x1", 4); + offset += (frame_len + 4); } if (codec == CodecH264) { - return std::make_shared >(buf, pts, dts, 4); + return std::make_shared >(buf, pts, dts, 4, DATA_OFFSET); } - return std::make_shared >(buf, pts, dts, 4); + return std::make_shared >(buf, pts, dts, 4, DATA_OFFSET); + } + + case CodecAAC: { + AACTrack::Ptr track = dynamic_pointer_cast(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 >(buf, pts, dts, ADTS_HEADER_LEN, DATA_OFFSET - ADTS_HEADER_LEN, codec); } case CodecOpus: - case CodecAAC: case CodecG711A: case CodecG711U: { - return std::make_shared >(codec, buf, pts, dts, 0); + return std::make_shared >(buf, pts, dts, 0, DATA_OFFSET, codec); } - default: - return nullptr; + + default: return nullptr; } }