优化性能,修复mp4播放速度不对
Some checks are pending
CI / host-build (push) Waiting to run
CI / rk3588-cross-build (push) Waiting to run

This commit is contained in:
sladro 2026-01-06 16:42:13 +08:00
parent 763f1473bc
commit 2ad8a934e0

View File

@ -251,8 +251,10 @@ std::string ClipAction::ProcessClipFromPackets(const std::vector<std::shared_ptr
return "";
}
stream->time_base = AVRational{1, 1000};
stream->avg_frame_rate = AVRational{std::max(1, fps_), 1};
const int fps_eff = std::max(1, (first->codec->fps > 0 ? first->codec->fps : fps_));
// Use a stable timescale to avoid rounding drift and "fast playback" caused by bad source PTS.
stream->time_base = AVRational{1, 90000};
stream->avg_frame_rate = AVRational{fps_eff, 1};
stream->r_frame_rate = stream->avg_frame_rate;
stream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
stream->codecpar->codec_id = cid;
@ -312,10 +314,8 @@ std::string ClipAction::ProcessClipFromPackets(const std::vector<std::shared_ptr
}
}
int64_t first_pts = 0;
bool have_first = false;
int64_t last_pts = -1;
const int64_t frame_dur = (fps_ > 0) ? (1000LL / std::max(1, fps_)) : 40;
const int64_t frame_dur = av_rescale_q(1, AVRational{1, fps_eff}, stream->time_base);
int64_t frame_idx = 0;
for (size_t i = start_idx; i < metas.size(); ++i) {
const auto& m = metas[i];
@ -323,14 +323,7 @@ std::string ClipAction::ProcessClipFromPackets(const std::vector<std::shared_ptr
if (!m->codec || m->pkt.data.empty()) continue;
if (m->pkt.pts_ms <= 0) continue;
if (!have_first) {
first_pts = m->pkt.pts_ms;
have_first = true;
}
int64_t pts = m->pkt.pts_ms - first_pts;
if (pts < 0) pts = 0;
if (pts <= last_pts) pts = last_pts + 1;
last_pts = pts;
const int64_t pts = av_rescale_q(frame_idx, AVRational{1, fps_eff}, stream->time_base);
std::vector<uint8_t> sample = m->pkt.data;
if (cid == AV_CODEC_ID_H264 && HasAnnexBStartCode(sample.data(), sample.size())) {
@ -353,6 +346,8 @@ std::string ClipAction::ProcessClipFromPackets(const std::vector<std::shared_ptr
(void)av_interleaved_write_frame(fmt_ctx, pkt);
av_packet_free(&pkt);
++frame_idx;
}
av_write_trailer(fmt_ctx);