From 2c045d4c730335ab38d48b5d3af97629fed0189d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20R=C3=B6ger?= Date: Sun, 23 Nov 2025 21:04:53 +0100 Subject: [PATCH] feat(mm2): smear frames --- app/mosh-me-2.cpp | 81 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) diff --git a/app/mosh-me-2.cpp b/app/mosh-me-2.cpp index a3ae6aa..891c911 100644 --- a/app/mosh-me-2.cpp +++ b/app/mosh-me-2.cpp @@ -1,3 +1,82 @@ +#include +#include +#include +#include #include +#include -int main() { spdlog::info("MM2"); } +int main(int argc, char *argv[]) { + + if (argc < 4) { + spdlog::error("Usage: {} ", + argv[0]); + return 1; + } + + const char *input_file = argv[1]; + const char *output_file = argv[2]; + int smear_frames = atoi(argv[3]); + + av::init(); + + av::FormatContext ictx; + av::FormatContext octx; + ictx.openInput(input_file); + ictx.findStreamInfo(); + + std::vector stream_map(ictx.streamsCount()); + + off_t oix = 0; + for (size_t i = 0; i < stream_map.size(); i++) { + auto istream = ictx.stream(i); + + if (istream.codecParameters().codecType() != AVMEDIA_TYPE_AUDIO || + istream.codecParameters().codecType() != AVMEDIA_TYPE_VIDEO || + istream.codecParameters().codecType() != AVMEDIA_TYPE_SUBTITLE) { + stream_map[i] = -1; + } + + auto ostream = octx.addStream(); + ostream.setCodecParameters(istream.codecParameters()); + ostream.codecParameters().codecTag(0); + stream_map[i] = oix++; + } + + octx.openOutput(output_file); + octx.writeHeader(); + + av::Packet packet_buffer; + + for (;;) { + auto pkt = ictx.readPacket(); + + if (pkt.isNull()) { + break; + } + + auto istream = ictx.stream(pkt.streamIndex()); + + if (pkt.streamIndex() >= stream_map.size() || + stream_map[pkt.streamIndex()] < 0 || pkt.isKeyPacket()) { + continue; + } + + pkt.setStreamIndex(stream_map[pkt.streamIndex()]); + + if (auto ts = pkt.pts().timestamp(); ts % smear_frames == 0) { + packet_buffer = pkt; + } else { + auto dts = pkt.dts(); + auto pts = pkt.pts(); + auto dur = pkt.duration(); + pkt = packet_buffer; + pkt.setDts(dts); + pkt.setPts(pts); + pkt.setDuration(dur); + } + + octx.writePacket(pkt); + } + + octx.writeTrailer(); +}