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
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user