mirror of
https://github.com/darksaid98/Flashback.git
synced 2026-12-01 22:16:01 +00:00
0.6.0
This commit is contained in:
1 parent
d589647c36
commit
7f1d199704
20 files changed
+461
-94
No files matched your search
+4
-13
@@ -3,25 +3,19 @@ package com.moulberry.flashback.exporting;
|
||||
import com.mojang.blaze3d.platform.NativeImage;
|
||||
import com.moulberry.flashback.Flashback;
|
||||
import com.moulberry.flashback.SneakyThrow;
|
||||
import com.moulberry.flashback.mixin.record.MixinFFmpegFrameRecorder;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import org.bytedeco.ffmpeg.avutil.AVComponentDescriptor;
|
||||
import org.bytedeco.ffmpeg.avutil.AVFrame;
|
||||
import org.bytedeco.ffmpeg.avutil.AVPixFmtDescriptor;
|
||||
import org.bytedeco.ffmpeg.global.avcodec;
|
||||
import org.bytedeco.ffmpeg.global.avutil;
|
||||
import org.bytedeco.ffmpeg.global.swscale;
|
||||
import org.bytedeco.ffmpeg.swscale.SwsContext;
|
||||
import org.bytedeco.javacpp.BytePointer;
|
||||
import org.bytedeco.javacpp.DoublePointer;
|
||||
import org.bytedeco.javacpp.Pointer;
|
||||
import org.bytedeco.javacpp.PointerPointer;
|
||||
import org.bytedeco.javacv.FFmpegFrameRecorder;
|
||||
import org.bytedeco.javacv.FFmpegLogCallback;
|
||||
import org.bytedeco.javacv.Frame;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.system.MemoryUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -34,10 +28,9 @@ import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.concurrent.locks.LockSupport;
|
||||
|
||||
import static org.bytedeco.ffmpeg.global.avutil.*;
|
||||
import static org.bytedeco.ffmpeg.global.swscale.SWS_BILINEAR;
|
||||
import static org.bytedeco.ffmpeg.global.swscale.sws_freeContext;
|
||||
|
||||
public class AsyncVideoEncoder implements AutoCloseable {
|
||||
public class AsyncFFmpegVideoWriter implements AutoCloseable, VideoWriter {
|
||||
|
||||
@Nullable
|
||||
private final ArrayBlockingQueue<ImageFrame> rescaleQueue;
|
||||
@@ -52,8 +45,6 @@ public class AsyncVideoEncoder implements AutoCloseable {
|
||||
|
||||
private final AtomicReference<Throwable> threadedError = new AtomicReference<>(null);
|
||||
|
||||
public static final int SRC_PIXEL_FORMAT = avutil.AV_PIX_FMT_RGBA;
|
||||
|
||||
private record ImageFrame(long pointer, int size, int width, int height, int channels, int imageDepth, int stride, int pixelFormat,
|
||||
@Nullable FloatBuffer audioBuffer) implements AutoCloseable {
|
||||
public void close() {
|
||||
@@ -61,7 +52,7 @@ public class AsyncVideoEncoder implements AutoCloseable {
|
||||
}
|
||||
}
|
||||
|
||||
public AsyncVideoEncoder(ExportSettings settings, String filename) {
|
||||
public AsyncFFmpegVideoWriter(ExportSettings settings, String filename) {
|
||||
int width = settings.resolutionX();
|
||||
int height = settings.resolutionY();
|
||||
|
||||
@@ -96,7 +87,7 @@ public class AsyncVideoEncoder implements AutoCloseable {
|
||||
|
||||
int dstPixelFormat = PixelFormatHelper.getBestPixelFormat(settings.encoder(), wantTransparency);
|
||||
Flashback.LOGGER.info("Encoding video with pixel format {}", PixelFormatHelper.pixelFormatToString(dstPixelFormat));
|
||||
boolean needsRescale = SRC_PIXEL_FORMAT != dstPixelFormat;
|
||||
boolean needsRescale = ExportJob.SRC_PIXEL_FORMAT != dstPixelFormat;
|
||||
|
||||
int audioChannels = 0;
|
||||
if (settings.recordAudio()) {
|
||||
@@ -338,7 +329,7 @@ public class AsyncVideoEncoder implements AutoCloseable {
|
||||
while (true) {
|
||||
try {
|
||||
ImageFrame imageFrame = new ImageFrame(src.pixels, (int) src.size, src.getWidth(), src.getHeight(),
|
||||
4, Frame.DEPTH_INT, src.getWidth(), SRC_PIXEL_FORMAT, audioBuffer);
|
||||
4, Frame.DEPTH_INT, src.getWidth(), ExportJob.SRC_PIXEL_FORMAT, audioBuffer);
|
||||
if (this.rescaleQueue != null) {
|
||||
this.rescaleQueue.put(imageFrame);
|
||||
} else {
|
||||
@@ -8,7 +8,9 @@ import com.mojang.blaze3d.systems.RenderSystem;
|
||||
import com.mojang.blaze3d.vertex.*;
|
||||
import com.moulberry.flashback.FixedDeltaTracker;
|
||||
import com.moulberry.flashback.Flashback;
|
||||
import com.moulberry.flashback.SneakyThrow;
|
||||
import com.moulberry.flashback.Utils;
|
||||
import com.moulberry.flashback.combo_options.VideoContainer;
|
||||
import com.moulberry.flashback.keyframe.KeyframeType;
|
||||
import com.moulberry.flashback.keyframe.handler.KeyframeHandler;
|
||||
import com.moulberry.flashback.keyframe.handler.MinecraftKeyframeHandler;
|
||||
@@ -28,6 +30,7 @@ import net.minecraft.client.renderer.FogRenderer;
|
||||
import net.minecraft.client.renderer.ShaderInstance;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.bytedeco.ffmpeg.global.avutil;
|
||||
import org.joml.Matrix4f;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
import org.lwjgl.openal.SOFTLoopback;
|
||||
@@ -74,6 +77,8 @@ public class ExportJob {
|
||||
|
||||
private final AtomicBoolean finishedServerTick = new AtomicBoolean(false);
|
||||
|
||||
public static final int SRC_PIXEL_FORMAT = avutil.AV_PIX_FMT_RGBA;
|
||||
|
||||
public ExportJob(ExportSettings settings) {
|
||||
this.settings = settings;
|
||||
this.particleRandom = this.settings.resetRng() ? new Random(2000) : null;
|
||||
@@ -135,12 +140,14 @@ public class ExportJob {
|
||||
RenderTarget mainTarget = Minecraft.getInstance().mainRenderTarget;
|
||||
infoRenderTarget = new TextureTarget(mainTarget.width, mainTarget.height, false, Minecraft.ON_OSX);
|
||||
|
||||
try (AsyncVideoEncoder encoder = new AsyncVideoEncoder(this.settings, tempFileName);
|
||||
SaveableFramebufferQueue downloader = new SaveableFramebufferQueue(this.settings.resolutionX(), this.settings.resolutionY())) {
|
||||
try (VideoWriter encoder = createVideoWriter(this.settings, tempFileName);
|
||||
SaveableFramebufferQueue downloader = new SaveableFramebufferQueue(this.settings.resolutionX(), this.settings.resolutionY())) {
|
||||
doExport(encoder, downloader, infoRenderTarget);
|
||||
}
|
||||
|
||||
Files.move(exportTempFile, this.settings.output(), StandardCopyOption.REPLACE_EXISTING);
|
||||
if (this.settings.container() != VideoContainer.PNG_SEQUENCE) {
|
||||
Files.move(exportTempFile, this.settings.output(), StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
@@ -170,7 +177,15 @@ public class ExportJob {
|
||||
}
|
||||
}
|
||||
|
||||
private void doExport(AsyncVideoEncoder encoder, SaveableFramebufferQueue downloader, TextureTarget infoRenderTarget) {
|
||||
private static VideoWriter createVideoWriter(ExportSettings settings, String tempFileName) {
|
||||
if (settings.container() == VideoContainer.PNG_SEQUENCE) {
|
||||
return new PNGSequenceVideoWriter(settings);
|
||||
} else {
|
||||
return new AsyncFFmpegVideoWriter(settings, tempFileName);
|
||||
}
|
||||
}
|
||||
|
||||
private void doExport(VideoWriter videoWriter, SaveableFramebufferQueue downloader, TextureTarget infoRenderTarget) {
|
||||
ReplayServer replayServer = Flashback.getReplayServer();
|
||||
if (replayServer == null) {
|
||||
return;
|
||||
@@ -301,7 +316,7 @@ public class ExportJob {
|
||||
cancel = finishFrame(renderTarget, infoRenderTarget, tickIndex, ticks.size());
|
||||
this.shouldChangeFramebufferSize = true;
|
||||
|
||||
submitDownloadedFrames(encoder, downloader, false);
|
||||
submitDownloadedFrames(videoWriter, downloader, false);
|
||||
|
||||
saveable.audioBuffer = audioBuffer;
|
||||
downloader.startDownload(renderTarget, saveable, this.settings.ssaa());
|
||||
@@ -312,8 +327,8 @@ public class ExportJob {
|
||||
}
|
||||
}
|
||||
|
||||
submitDownloadedFrames(encoder, downloader, true);
|
||||
encoder.finish();
|
||||
submitDownloadedFrames(videoWriter, downloader, true);
|
||||
videoWriter.finish();
|
||||
}
|
||||
|
||||
private void updateRandoms(Random random, Random mathRandom) {
|
||||
@@ -432,7 +447,7 @@ public class ExportJob {
|
||||
}
|
||||
}
|
||||
|
||||
private void submitDownloadedFrames(AsyncVideoEncoder encoder, SaveableFramebufferQueue downloader, boolean drain) {
|
||||
private void submitDownloadedFrames(VideoWriter videoWriter, SaveableFramebufferQueue downloader, boolean drain) {
|
||||
SaveableFramebufferQueue.DownloadedFrame frame;
|
||||
while (true) {
|
||||
long start = System.nanoTime();
|
||||
@@ -444,7 +459,7 @@ public class ExportJob {
|
||||
}
|
||||
|
||||
start = System.nanoTime();
|
||||
encoder.encode(frame.image(), frame.audioBuffer());
|
||||
videoWriter.encode(frame.image(), frame.audioBuffer());
|
||||
encodeTimeNanos += System.nanoTime() - start;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,158 @@
|
||||
package com.moulberry.flashback.exporting;
|
||||
|
||||
import com.mojang.blaze3d.platform.NativeImage;
|
||||
import com.moulberry.flashback.Flashback;
|
||||
import com.moulberry.flashback.SneakyThrow;
|
||||
import org.bytedeco.javacv.FFmpegFrameRecorder;
|
||||
import org.bytedeco.javacv.Frame;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.lwjgl.system.MemoryUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.FloatBuffer;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.concurrent.locks.LockSupport;
|
||||
|
||||
public class PNGSequenceVideoWriter implements VideoWriter {
|
||||
|
||||
private final ExportSettings settings;
|
||||
private int sequenceNumber = 0;
|
||||
|
||||
private final AtomicBoolean finishEncodeThread = new AtomicBoolean(false);
|
||||
private final AtomicBoolean finishedWriting = new AtomicBoolean(false);
|
||||
|
||||
private final AtomicReference<Throwable> threadedError = new AtomicReference<>(null);
|
||||
|
||||
private final ArrayBlockingQueue<NativeImage> encodeQueue;
|
||||
|
||||
public PNGSequenceVideoWriter(ExportSettings settings) {
|
||||
this.settings = settings;
|
||||
this.encodeQueue = new ArrayBlockingQueue<>(32);
|
||||
|
||||
createEncodeThread().start();
|
||||
}
|
||||
|
||||
private Thread createEncodeThread() {
|
||||
Thread encodeThread = new Thread(() -> {
|
||||
while (true) {
|
||||
NativeImage src;
|
||||
|
||||
try {
|
||||
src = this.encodeQueue.poll(10, TimeUnit.MILLISECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
throw SneakyThrow.sneakyThrow(e);
|
||||
}
|
||||
|
||||
try {
|
||||
if (src == null) {
|
||||
if (this.finishEncodeThread.get()) {
|
||||
this.finishedWriting.set(true);
|
||||
return;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure pixels are fully opaque if transparency is disabled
|
||||
if (!this.settings.transparent() && src.format().hasAlpha()) {
|
||||
int alpha = 255 << src.format().alphaOffset();
|
||||
for (int y = 0; y < src.getHeight(); y++) {
|
||||
for (int x = 0; x < src.getWidth(); x++) {
|
||||
src.setPixelRGBA(x, y, src.getPixelRGBA(x, y) | alpha);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.sequenceNumber += 1;
|
||||
String filename = String.format("%04d.png", this.sequenceNumber);
|
||||
src.writeToFile(this.settings.output().resolve(filename));
|
||||
} catch (Throwable t) {
|
||||
this.threadedError.set(t);
|
||||
this.finishEncodeThread.set(true);
|
||||
this.finishedWriting.set(true);
|
||||
return;
|
||||
} finally {
|
||||
if (src != null) {
|
||||
src.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
encodeThread.setName("Video Encode Thread");
|
||||
return encodeThread;
|
||||
}
|
||||
|
||||
private void checkEncodeError(@Nullable AutoCloseable closeable) {
|
||||
Throwable t = this.threadedError.get();
|
||||
if (t != null) {
|
||||
this.finishEncodeThread.set(true);
|
||||
this.finishedWriting.set(true);
|
||||
|
||||
if (closeable != null) {
|
||||
try {
|
||||
closeable.close();
|
||||
} catch (Exception e) {
|
||||
Flashback.LOGGER.error("Error while trying to close passed AutoClosable", e);
|
||||
}
|
||||
}
|
||||
SneakyThrow.sneakyThrow(t);
|
||||
}
|
||||
}
|
||||
|
||||
public void encode(NativeImage src, @Nullable FloatBuffer audioBuffer) {
|
||||
if (audioBuffer != null) {
|
||||
throw new RuntimeException("PNG Sequence does not support encoding audio");
|
||||
}
|
||||
|
||||
checkEncodeError(src);
|
||||
|
||||
if (this.finishEncodeThread.get() || this.finishedWriting.get()) {
|
||||
src.close();
|
||||
throw new IllegalStateException("Cannot encode after finish()");
|
||||
}
|
||||
|
||||
while (true) {
|
||||
try {
|
||||
this.encodeQueue.put(src);
|
||||
break;
|
||||
} catch (InterruptedException ignored) {}
|
||||
checkEncodeError(src);
|
||||
}
|
||||
}
|
||||
|
||||
public void finish() {
|
||||
checkEncodeError(null);
|
||||
|
||||
while (!this.encodeQueue.isEmpty()) {
|
||||
checkEncodeError(null);
|
||||
LockSupport.parkNanos("waiting for encode queue to empty", 100000L);
|
||||
}
|
||||
|
||||
this.finishEncodeThread.set(true);
|
||||
|
||||
while (!this.finishedWriting.get()) {
|
||||
LockSupport.parkNanos("waiting for encoder thread to finish", 100000L);
|
||||
}
|
||||
|
||||
checkEncodeError(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
for (NativeImage src : this.encodeQueue) {
|
||||
src.close();
|
||||
}
|
||||
|
||||
this.finishEncodeThread.set(true);
|
||||
|
||||
while (!this.finishedWriting.get()) {
|
||||
LockSupport.parkNanos("waiting for encoder thread to finish", 100000L);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -69,13 +69,13 @@ public class PixelFormatHelper {
|
||||
supportedFormats.add(avutil.AV_PIX_FMT_NONE);
|
||||
|
||||
if (transparent) {
|
||||
int format = avcodec.avcodec_find_best_pix_fmt_of_list(supportedFormats.toIntArray(), AsyncVideoEncoder.SRC_PIXEL_FORMAT, 1, new int[1]);
|
||||
int format = avcodec.avcodec_find_best_pix_fmt_of_list(supportedFormats.toIntArray(), ExportJob.SRC_PIXEL_FORMAT, 1, new int[1]);
|
||||
if (format != avutil.AV_PIX_FMT_NONE) {
|
||||
return format;
|
||||
}
|
||||
}
|
||||
|
||||
return avcodec.avcodec_find_best_pix_fmt_of_list(supportedFormats.toIntArray(), AsyncVideoEncoder.SRC_PIXEL_FORMAT, 0, new int[1]);
|
||||
return avcodec.avcodec_find_best_pix_fmt_of_list(supportedFormats.toIntArray(), ExportJob.SRC_PIXEL_FORMAT, 0, new int[1]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.moulberry.flashback.exporting;
|
||||
|
||||
import com.mojang.blaze3d.platform.NativeImage;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.nio.FloatBuffer;
|
||||
|
||||
public interface VideoWriter extends AutoCloseable {
|
||||
|
||||
void encode(NativeImage src, @Nullable FloatBuffer audioBuffer);
|
||||
void finish();
|
||||
|
||||
default void close() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user