From: Not Zed Date: Tue, 9 May 2023 03:00:23 +0000 (+0930) Subject: Update to JDK 20 foreign-abi preview API. X-Git-Url: https://code.zedzone.au/cvs?a=commitdiff_plain;h=ce19ecc3fb23a024f84de506505355d8ffc4a343;p=jjmpeg Update to JDK 20 foreign-abi preview API. --- diff --git a/Makefile b/Makefile index 562a130..cbaada0 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ dist_EXTRA=README TODO \ include config.make -java_MODULES=notzed.jjmpeg #notzed.jjmpeg.fx notzed.jjmpeg.awt notzed.jjmpeg.demo +java_MODULES=notzed.jjmpeg notzed.jjmpeg.fx notzed.jjmpeg.awt notzed.jjmpeg.demo notzed.jjmpeg.awt_JDEPMOD=notzed.jjmpeg notzed.jjmpeg.fx_JDEPMOD=notzed.jjmpeg diff --git a/src/notzed.jjmpeg.demo/classes/au/notzed/jjmpeg/demo/audio/AudioPlay.java b/src/notzed.jjmpeg.demo/classes/au/notzed/jjmpeg/demo/audio/AudioPlay.java index 7b4daed..7f42390 100644 --- a/src/notzed.jjmpeg.demo/classes/au/notzed/jjmpeg/demo/audio/AudioPlay.java +++ b/src/notzed.jjmpeg.demo/classes/au/notzed/jjmpeg/demo/audio/AudioPlay.java @@ -19,11 +19,12 @@ package au.notzed.jjmpeg.demo.audio; import au.notzed.jjmpeg.AVChannelLayout; +import au.notzed.jjmpeg.AVObject; import au.notzed.jjmpeg.AVSampleFormat; import au.notzed.jjmpeg.AVSampleReader; import au.notzed.jjmpeg.io.JJMediaReader; import java.io.IOException; -import java.lang.foreign.MemorySession; +import java.lang.foreign.Arena; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; @@ -61,6 +62,8 @@ public class AudioPlay { int outputBits = 16; long outputLayout = 0; + AVObject.init(); + for (int i = 0; i < args.length; i++) { switch (args[i]) { case "--rate": @@ -103,8 +106,8 @@ public class AudioPlay { return; } - try (MemorySession scope = MemorySession.openConfined(); - JJMediaReader mr = new JJMediaReader(path, scope)) { + try (Arena arena = Arena.openConfined(); + JJMediaReader mr = new JJMediaReader(path, arena.scope())) { JJMediaReader.JJReaderAudio as = mr.openDefaultStream(JJMediaReader.TYPE_AUDIO); if (as == null) @@ -127,7 +130,7 @@ public class AudioPlay { byte[] buffer = new byte[8192]; System.err.printf("Audio: %s\n", path); - System.err.printf(" source format: %s %.1f Hz %s\n", AVSampleFormat.toString(fmt), (double) rate, AVChannelLayout.toString(layout)); + System.err.printf(" source format: %s %.1f Hz %s\n", AVSampleFormat.toString(fmt), (double)rate, AVChannelLayout.toString(layout)); System.err.printf(" output format: %s\n", format); SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss.SSS"); diff --git a/src/notzed.jjmpeg.demo/classes/au/notzed/jjmpeg/demo/video/VideoExport.java b/src/notzed.jjmpeg.demo/classes/au/notzed/jjmpeg/demo/video/VideoExport.java index 2ebf0b1..a096769 100644 --- a/src/notzed.jjmpeg.demo/classes/au/notzed/jjmpeg/demo/video/VideoExport.java +++ b/src/notzed.jjmpeg.demo/classes/au/notzed/jjmpeg/demo/video/VideoExport.java @@ -24,7 +24,8 @@ import au.notzed.jjmpeg.io.JJMediaReader; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; -import java.lang.foreign.MemorySession; +import java.lang.foreign.Arena; +import java.lang.foreign.SegmentScope; import java.util.IllegalFormatException; import javax.imageio.ImageIO; @@ -75,8 +76,8 @@ public class VideoExport { System.err.println("Unknown extension"); type = format.substring(dot + 1).toLowerCase(); - try (MemorySession scope = MemorySession.openConfined(); - JJMediaReader mr = new JJMediaReader(path, scope)) { + try (Arena arena = Arena.openConfined(); + JJMediaReader mr = new JJMediaReader(path, arena.scope())) { int index = 0; JJMediaReader.JJReaderVideo vs = mr.openDefaultStream(JJMediaReader.TYPE_VIDEO); diff --git a/src/notzed.jjmpeg.demo/classes/au/notzed/jjmpeg/demo/video/VideoPlay.java b/src/notzed.jjmpeg.demo/classes/au/notzed/jjmpeg/demo/video/VideoPlay.java index ec4a6fe..90f8607 100644 --- a/src/notzed.jjmpeg.demo/classes/au/notzed/jjmpeg/demo/video/VideoPlay.java +++ b/src/notzed.jjmpeg.demo/classes/au/notzed/jjmpeg/demo/video/VideoPlay.java @@ -26,7 +26,7 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import static java.lang.Double.min; -import java.lang.foreign.MemorySession; +import java.lang.foreign.Arena; import java.time.Instant; import java.time.LocalTime; import java.time.ZoneId; @@ -252,8 +252,8 @@ public class VideoPlay extends Application { * @param path */ void reader(String path) { - try (MemorySession scope = MemorySession.openConfined(); - JJMediaReader mr = new JJMediaReader(path, scope)) { + try (Arena arena = Arena.openConfined(); + JJMediaReader mr = new JJMediaReader(path, arena.scope())) { JJMediaReader.JJReaderVideo vs = mr.openDefaultStream(JJMediaReader.TYPE_VIDEO); if (vs != null) { diff --git a/src/notzed.jjmpeg.demo/classes/au/notzed/jjmpeg/demo/video/VideoScan.java b/src/notzed.jjmpeg.demo/classes/au/notzed/jjmpeg/demo/video/VideoScan.java index 77186e7..479b78f 100644 --- a/src/notzed.jjmpeg.demo/classes/au/notzed/jjmpeg/demo/video/VideoScan.java +++ b/src/notzed.jjmpeg.demo/classes/au/notzed/jjmpeg/demo/video/VideoScan.java @@ -21,7 +21,8 @@ package au.notzed.jjmpeg.demo.video; import au.notzed.jjmpeg.AVPixelFormat; import au.notzed.jjmpeg.io.JJMediaReader; import java.io.IOException; -import java.lang.foreign.MemorySession; +import java.lang.foreign.Arena; +import java.lang.foreign.SegmentScope; /** * A simple video scanner. @@ -39,8 +40,8 @@ public class VideoScan { String path = args[0]; - try (MemorySession scope = MemorySession.openConfined(); - JJMediaReader mr = new JJMediaReader(path, scope)) { + try (Arena arena = Arena.openConfined(); + JJMediaReader mr = new JJMediaReader(path, arena.scope())) { int count = 0; JJMediaReader.JJReaderVideo vs = mr.openDefaultStream(JJMediaReader.TYPE_VIDEO); diff --git a/src/notzed.jjmpeg/classes/au/notzed/jjmpeg/AVObject.java b/src/notzed.jjmpeg/classes/au/notzed/jjmpeg/AVObject.java index 44219b3..3ba93e4 100644 --- a/src/notzed.jjmpeg/classes/au/notzed/jjmpeg/AVObject.java +++ b/src/notzed.jjmpeg/classes/au/notzed/jjmpeg/AVObject.java @@ -19,7 +19,7 @@ package au.notzed.jjmpeg; import au.notzed.nativez.*; -import java.lang.foreign.MemorySession; +import java.lang.foreign.SegmentScope; import java.util.logging.Logger; /** @@ -40,7 +40,7 @@ public abstract class AVObject extends Native { System.loadLibrary("avutil"); AVUtil.setLogger(AVUtil.createAVLogger(Logger.getLogger("notzed.jjmpeg")), - MemorySession.global()); + SegmentScope.global()); } /** diff --git a/src/notzed.jjmpeg/classes/au/notzed/jjmpeg/FramePixelReader.java b/src/notzed.jjmpeg/classes/au/notzed/jjmpeg/FramePixelReader.java index da920ab..4ac7b66 100644 --- a/src/notzed.jjmpeg/classes/au/notzed/jjmpeg/FramePixelReader.java +++ b/src/notzed.jjmpeg/classes/au/notzed/jjmpeg/FramePixelReader.java @@ -21,8 +21,8 @@ package au.notzed.jjmpeg; import java.nio.Buffer; import java.nio.BufferOverflowException; import au.notzed.nativez.*; +import java.lang.foreign.Arena; import java.lang.foreign.MemorySegment; -import java.lang.foreign.MemorySession; public class FramePixelReader implements AVPixelReader { @@ -31,7 +31,7 @@ public class FramePixelReader implements AVPixelReader { final int dwidth, dheight, flags; int dfmt = -1; - MemorySession scope = MemorySession.openImplicit(); + Arena arena = Arena.openShared(); MemorySegment seg; public FramePixelReader(AVFrame frame, int dwidth, int dheight, int flags) { @@ -60,7 +60,7 @@ public class FramePixelReader implements AVPixelReader { //Memory.free(seg.baseAddress()); } //seg = Memory.ofNative(Memory.malloc(size), size); - seg = MemorySegment.allocateNative(size, 16, scope); + seg = arena.allocate(size, 16); } return seg; } @@ -69,9 +69,9 @@ public class FramePixelReader implements AVPixelReader { SwsContext ctx = getContext(fmt); // FIXME: check sizes - try ( Frame a = Frame.frame()) { - IntArray stride = IntArray.createArray(4, a); - PointerArray data = PointerArray.createArray(4, a); + try (Frame a = Frame.frame()) { + IntArray stride = IntArray.create(4, a); + PointerArray data = PointerArray.create(4, a); int res; res = AVUtil.av_image_fill_linesizes(stride, fmt, dwidth); @@ -96,9 +96,9 @@ public class FramePixelReader implements AVPixelReader { } public long bufferSize(int h, int fmt) { - try ( Frame a = Frame.frame()) { - IntArray stride = IntArray.createArray(4, a); - PointerArray data = PointerArray.createArray(4, a); + try (Frame a = Frame.frame()) { + IntArray stride = IntArray.create(4, a); + PointerArray data = PointerArray.create(4, a); int res; res = AVUtil.av_image_fill_linesizes(stride, fmt, dwidth); @@ -128,9 +128,9 @@ public class FramePixelReader implements AVPixelReader { } public void release() { - if (scope != null) { - scope.close(); - scope = null; + if (arena != null) { + arena.close(); + arena = null; } } } diff --git a/src/notzed.jjmpeg/classes/au/notzed/jjmpeg/io/JJMediaReader.java b/src/notzed.jjmpeg/classes/au/notzed/jjmpeg/io/JJMediaReader.java index 09b5c4f..006d4f3 100644 --- a/src/notzed.jjmpeg/classes/au/notzed/jjmpeg/io/JJMediaReader.java +++ b/src/notzed.jjmpeg/classes/au/notzed/jjmpeg/io/JJMediaReader.java @@ -37,7 +37,7 @@ import au.notzed.jjmpeg.AVStream; import au.notzed.nativez.Frame; import au.notzed.nativez.HandleArray; import java.io.FileNotFoundException; -import java.lang.foreign.MemorySession; +import java.lang.foreign.SegmentScope; import java.util.AbstractList; import java.util.List; import java.util.logging.Logger; @@ -110,11 +110,11 @@ public class JJMediaReader implements AutoCloseable { * @see #getStreams * @see #readFrame */ - public JJMediaReader(String name, MemorySession scope) throws AVIOException, FileNotFoundException { + public JJMediaReader(String name, SegmentScope scope) throws AVIOException, FileNotFoundException { this(name, null, null, scope); } - public JJMediaReader(String name, AVInputFormat fmt, HandleArray options, MemorySession scope) throws AVIOException, FileNotFoundException { + public JJMediaReader(String name, AVInputFormat fmt, HandleArray options, SegmentScope scope) throws AVIOException, FileNotFoundException { this(AVFormatContext.openInput(name, fmt, options, scope)); } @@ -431,7 +431,7 @@ public class JJMediaReader implements AutoCloseable { duration = stream.getDuration(); durationms = tb.rescale(duration, 1000); - try ( Frame f = Frame.frame()) { + try (Frame f = Frame.frame()) { // conservatively assume keyframe every 32 frames seekOffsetMS = stream.getAverageFrameRate().inv(f).rescale(1000, 32); diff --git a/src/notzed.jjmpeg/classes/au/notzed/jjmpeg/io/JJMediaWriter.java b/src/notzed.jjmpeg/classes/au/notzed/jjmpeg/io/JJMediaWriter.java index be16c63..f84b67f 100644 --- a/src/notzed.jjmpeg/classes/au/notzed/jjmpeg/io/JJMediaWriter.java +++ b/src/notzed.jjmpeg/classes/au/notzed/jjmpeg/io/JJMediaWriter.java @@ -44,7 +44,7 @@ import au.notzed.jjmpeg.AVIOException; import au.notzed.jjmpeg.AVMediaType; import au.notzed.jjmpeg.AVSampleFormat; import au.notzed.nativez.Frame; -import java.lang.foreign.MemorySession; +import java.lang.foreign.SegmentScope; import java.util.ArrayList; import java.util.List; import java.util.logging.Logger; @@ -72,7 +72,7 @@ public class JJMediaWriter implements AutoCloseable { * * @param filename */ - public JJMediaWriter(String filename, MemorySession scope) throws AVIOException { + public JJMediaWriter(String filename, SegmentScope scope) throws AVIOException { this.filename = filename; oc = AVFormatContext.allocOutputContext(null, null, filename, scope); diff --git a/src/notzed.jjmpeg/gen/ffmpeg.api b/src/notzed.jjmpeg/gen/ffmpeg.api index e66f72d..fcf49ee 100644 --- a/src/notzed.jjmpeg/gen/ffmpeg.api +++ b/src/notzed.jjmpeg/gen/ffmpeg.api @@ -41,8 +41,8 @@ struct AVFormatContext default=none func:rename=s/^(avformat_|av_)//,camel-case code: {{ // not relly ideal but it'll do - public void free() { free(segment.address()); } - public void close() { close(segment.address()); } + public void free() { free(segment); } + public void close() { close(segment); } }} } @@ -147,8 +147,8 @@ struct AVCodecContext default=none func:rename=s/^avcodec_//,camel-case field:re } public void free() { try (Frame frame = Frame.frame()) { - PointerArray holder = PointerArray.createArray(1, frame); - holder.set(0, segment.address()); + PointerArray holder = PointerArray.create(1, frame); + holder.set(0, segment); freeContext(holder); } } @@ -173,8 +173,8 @@ struct AVFrame default=all func:rename=s/^av_frame_//,camel-case field:rename=st } public void free() { try (Frame frame = Frame.frame()) { - PointerArray holder = PointerArray.createArray(1, frame); - holder.set(0, segment.address()); + PointerArray holder = PointerArray.create(1, frame); + holder.set(0, segment); freeFrame(holder); } } @@ -262,8 +262,8 @@ struct AVPacket default=all func:rename=s/^av_packet_//,camel-case field:rename= code: {{ public void free() { try (Frame frame = Frame.frame()) { - PointerArray holder = PointerArray.createArray(1, frame); - holder.set(0, segment.address()); + PointerArray holder = PointerArray.create(1, frame); + holder.set(0, segment); freePacket(holder); } } @@ -367,10 +367,10 @@ library AVUtil }; } - public static void setLogger(AVLogger log, MemorySession scope) { + public static void setLogger(AVLogger log, SegmentScope scope) { AVUtil.av_log_set_callback(AVLogFunc.upcall((avcl, level, fmt, valist) -> { try (Frame frame = Frame.frame()) { - ByteArray line = ByteArray.createArray(1024, frame); + ByteArray line = ByteArray.create(1024, frame); IntArray print_prefix = IntArray.create(frame, 1); AVUtil.av_log_format_line2(avcl, level, fmt, valist, line, print_prefix); log.log(level, line.toUtf8String());