2020-05-18 15:31:49 +08:00
|
|
|
|
/*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
|
2020-04-03 20:46:55 +08:00
|
|
|
|
*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
|
2020-04-03 20:46:55 +08:00
|
|
|
|
*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Use of this source code is governed by MIT-like license that can be found in the
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* 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.
|
2020-04-03 20:46:55 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifdef ENABLE_MP4
|
|
|
|
|
#include "MP4Demuxer.h"
|
|
|
|
|
#include "Util/logger.h"
|
2023-12-09 16:23:51 +08:00
|
|
|
|
#include "Extension/Factory.h"
|
2023-10-12 17:26:53 +08:00
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using namespace std;
|
2023-10-12 17:26:53 +08:00
|
|
|
|
using namespace toolkit;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
|
2020-04-03 20:46:55 +08:00
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
|
|
|
|
MP4Demuxer::~MP4Demuxer() {
|
2021-08-03 21:48:19 +08:00
|
|
|
|
closeMP4();
|
2020-04-03 20:46:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-03 21:48:19 +08:00
|
|
|
|
void MP4Demuxer::openMP4(const string &file) {
|
|
|
|
|
closeMP4();
|
|
|
|
|
|
|
|
|
|
_mp4_file = std::make_shared<MP4FileDisk>();
|
|
|
|
|
_mp4_file->openFile(file.data(), "rb+");
|
|
|
|
|
_mov_reader = _mp4_file->createReader();
|
2020-09-20 19:45:04 +08:00
|
|
|
|
getAllTracks();
|
|
|
|
|
_duration_ms = mov_reader_getduration(_mov_reader.get());
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-03 21:48:19 +08:00
|
|
|
|
void MP4Demuxer::closeMP4() {
|
|
|
|
|
_mov_reader.reset();
|
|
|
|
|
_mp4_file.reset();
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-03 20:46:55 +08:00
|
|
|
|
int MP4Demuxer::getAllTracks() {
|
|
|
|
|
static mov_reader_trackinfo_t s_on_track = {
|
|
|
|
|
[](void *param, uint32_t track, uint8_t object, int width, int height, const void *extra, size_t bytes) {
|
|
|
|
|
//onvideo
|
|
|
|
|
MP4Demuxer *thiz = (MP4Demuxer *)param;
|
|
|
|
|
thiz->onVideoTrack(track,object,width,height,extra,bytes);
|
|
|
|
|
},
|
|
|
|
|
[](void *param, uint32_t track, uint8_t object, int channel_count, int bit_per_sample, int sample_rate, const void *extra, size_t bytes) {
|
|
|
|
|
//onaudio
|
|
|
|
|
MP4Demuxer *thiz = (MP4Demuxer *)param;
|
|
|
|
|
thiz->onAudioTrack(track,object,channel_count,bit_per_sample,sample_rate,extra,bytes);
|
|
|
|
|
},
|
|
|
|
|
[](void *param, uint32_t track, uint8_t object, const void *extra, size_t bytes) {
|
|
|
|
|
//onsubtitle, do nothing
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
return mov_reader_getinfo(_mov_reader.get(),&s_on_track,this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MP4Demuxer::onVideoTrack(uint32_t track, uint8_t object, int width, int height, const void *extra, size_t bytes) {
|
2023-12-09 16:23:51 +08:00
|
|
|
|
auto video = Factory::getTrackByCodecId(getCodecByMovId(object));
|
|
|
|
|
if (!video) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-12-09 22:34:22 +08:00
|
|
|
|
video->setIndex(track);
|
|
|
|
|
_tracks.emplace(track, video);
|
2023-12-09 16:23:51 +08:00
|
|
|
|
if (extra && bytes) {
|
|
|
|
|
video->setExtraData((uint8_t *)extra, bytes);
|
2020-04-03 20:46:55 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-09 16:23:51 +08:00
|
|
|
|
void MP4Demuxer::onAudioTrack(uint32_t track, uint8_t object, int channel_count, int bit_per_sample, int sample_rate, const void *extra, size_t bytes) {
|
|
|
|
|
auto audio = Factory::getTrackByCodecId(getCodecByMovId(object), sample_rate, channel_count, bit_per_sample / channel_count);
|
|
|
|
|
if (!audio) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-12-09 22:34:22 +08:00
|
|
|
|
audio->setIndex(track);
|
|
|
|
|
_tracks.emplace(track, audio);
|
2023-12-09 16:23:51 +08:00
|
|
|
|
if (extra && bytes) {
|
|
|
|
|
audio->setExtraData((uint8_t *)extra, bytes);
|
2020-04-03 20:46:55 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int64_t MP4Demuxer::seekTo(int64_t stamp_ms) {
|
|
|
|
|
if(0 != mov_reader_seek(_mov_reader.get(),&stamp_ms)){
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return stamp_ms;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-08 17:36:07 +08:00
|
|
|
|
struct Context {
|
|
|
|
|
Context(MP4Demuxer *ptr) : thiz(ptr) {}
|
2020-04-03 20:46:55 +08:00
|
|
|
|
MP4Demuxer *thiz;
|
2022-08-08 17:36:07 +08:00
|
|
|
|
int flags = 0;
|
|
|
|
|
int64_t pts = 0;
|
|
|
|
|
int64_t dts = 0;
|
|
|
|
|
uint32_t track_id = 0;
|
2020-04-03 20:46:55 +08:00
|
|
|
|
BufferRaw::Ptr buffer;
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-13 09:44:05 +08:00
|
|
|
|
Frame::Ptr MP4Demuxer::readFrame(bool &keyFrame, bool &eof) {
|
|
|
|
|
keyFrame = false;
|
|
|
|
|
eof = false;
|
2021-10-22 14:16:39 +08:00
|
|
|
|
|
|
|
|
|
static mov_reader_onread2 mov_onalloc = [](void *param, uint32_t track_id, size_t bytes, int64_t pts, int64_t dts, int flags) -> void * {
|
2020-04-03 20:46:55 +08:00
|
|
|
|
Context *ctx = (Context *) param;
|
|
|
|
|
ctx->pts = pts;
|
|
|
|
|
ctx->dts = dts;
|
|
|
|
|
ctx->flags = flags;
|
|
|
|
|
ctx->track_id = track_id;
|
|
|
|
|
|
2022-01-06 14:30:44 +08:00
|
|
|
|
ctx->buffer = ctx->thiz->_buffer_pool.obtain2();
|
2023-12-09 16:23:51 +08:00
|
|
|
|
ctx->buffer->setCapacity(bytes + 1);
|
|
|
|
|
ctx->buffer->setSize(bytes);
|
|
|
|
|
return ctx->buffer->data();
|
2020-04-03 20:46:55 +08:00
|
|
|
|
};
|
|
|
|
|
|
2022-08-08 17:36:07 +08:00
|
|
|
|
Context ctx(this);
|
2021-10-22 14:16:39 +08:00
|
|
|
|
auto ret = mov_reader_read2(_mov_reader.get(), mov_onalloc, &ctx);
|
2020-04-03 20:46:55 +08:00
|
|
|
|
switch (ret) {
|
2020-04-13 09:44:05 +08:00
|
|
|
|
case 0 : {
|
|
|
|
|
eof = true;
|
2020-04-03 22:04:13 +08:00
|
|
|
|
return nullptr;
|
2020-04-13 09:44:05 +08:00
|
|
|
|
}
|
2020-04-03 20:46:55 +08:00
|
|
|
|
|
|
|
|
|
case 1 : {
|
2020-04-03 21:39:44 +08:00
|
|
|
|
keyFrame = ctx.flags & MOV_AV_FLAG_KEYFREAME;
|
2020-04-03 22:04:13 +08:00
|
|
|
|
return makeFrame(ctx.track_id, ctx.buffer, ctx.pts, ctx.dts);
|
2020-04-03 20:46:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-13 09:44:05 +08:00
|
|
|
|
default : {
|
|
|
|
|
eof = true;
|
2020-04-03 20:46:55 +08:00
|
|
|
|
WarnL << "读取mp4文件数据失败:" << ret;
|
2020-04-03 22:04:13 +08:00
|
|
|
|
return nullptr;
|
2020-04-13 09:44:05 +08:00
|
|
|
|
}
|
2020-04-03 20:46:55 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Frame::Ptr MP4Demuxer::makeFrame(uint32_t track_id, const Buffer::Ptr &buf, int64_t pts, int64_t dts) {
|
2023-12-09 22:34:22 +08:00
|
|
|
|
auto it = _tracks.find(track_id);
|
|
|
|
|
if (it == _tracks.end()) {
|
2020-04-03 20:46:55 +08:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2021-12-15 20:51:26 +08:00
|
|
|
|
Frame::Ptr ret;
|
2023-12-09 16:23:51 +08:00
|
|
|
|
auto codec = it->second->getCodecId();
|
2020-04-03 20:46:55 +08:00
|
|
|
|
switch (codec) {
|
2023-12-09 16:23:51 +08:00
|
|
|
|
case CodecH264:
|
|
|
|
|
case CodecH265: {
|
|
|
|
|
auto bytes = buf->size();
|
|
|
|
|
auto data = buf->data();
|
|
|
|
|
auto offset = 0u;
|
2020-09-20 19:45:21 +08:00
|
|
|
|
while (offset < bytes) {
|
|
|
|
|
uint32_t frame_len;
|
|
|
|
|
memcpy(&frame_len, data + offset, 4);
|
|
|
|
|
frame_len = ntohl(frame_len);
|
|
|
|
|
if (frame_len + offset + 4 > bytes) {
|
2020-04-03 20:46:55 +08:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2021-02-05 11:51:16 +08:00
|
|
|
|
memcpy(data + offset, "\x00\x00\x00\x01", 4);
|
2020-09-20 19:45:21 +08:00
|
|
|
|
offset += (frame_len + 4);
|
2020-04-03 20:46:55 +08:00
|
|
|
|
}
|
2023-12-09 16:23:51 +08:00
|
|
|
|
ret = Factory::getFrameFromBuffer(codec, std::move(buf), dts, pts);
|
2023-10-12 17:26:53 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-09 16:23:51 +08:00
|
|
|
|
default: {
|
|
|
|
|
ret = Factory::getFrameFromBuffer(codec, std::move(buf), dts, pts);
|
2021-12-15 20:51:26 +08:00
|
|
|
|
break;
|
2020-04-03 20:46:55 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-15 20:51:26 +08:00
|
|
|
|
if (ret) {
|
2023-12-09 22:34:22 +08:00
|
|
|
|
ret->setIndex(track_id);
|
2021-12-15 20:51:26 +08:00
|
|
|
|
it->second->inputFrame(ret);
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
2020-04-03 20:46:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-09 22:34:22 +08:00
|
|
|
|
vector<Track::Ptr> MP4Demuxer::getTracks(bool ready) const {
|
2020-04-03 20:46:55 +08:00
|
|
|
|
vector<Track::Ptr> ret;
|
2023-12-09 22:34:22 +08:00
|
|
|
|
for (auto &pr : _tracks) {
|
|
|
|
|
if (ready && !pr.second->ready()) {
|
2020-04-03 20:46:55 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
ret.push_back(pr.second);
|
|
|
|
|
}
|
2020-09-21 14:32:56 +08:00
|
|
|
|
return ret;
|
2020-04-03 20:46:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint64_t MP4Demuxer::getDurationMS() const {
|
|
|
|
|
return _duration_ms;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}//namespace mediakit
|
2023-10-12 17:26:53 +08:00
|
|
|
|
#endif// ENABLE_MP4
|