From 2658217810bb49250eb6c2b8dbc0f3458f6aa1c8 Mon Sep 17 00:00:00 2001 From: Michael Zucchi Date: Mon, 24 Jun 2019 14:06:27 +0930 Subject: [PATCH] Add explicit release for FrameReader/FrameWriter. Add AVFrame ref/unref operations. --- .../au/notzed/jjmpeg/awt/JavaPixelReader.java | 6 +++++ .../au/notzed/jjmpeg/fx/FXPixelReader.java | 5 ++++ .../classes/au/notzed/jjmpeg/AVFrame.java | 14 +++++++++++ .../au/notzed/jjmpeg/AVPixelReader.java | 1 + .../au/notzed/jjmpeg/io/JJMediaReader.java | 4 +++ src/notzed.jjmpeg/jni/jj-avframe.c | 25 ++++++++++++++++--- src/notzed.jjmpeg/jni/jj-avframe.def | 3 +++ 7 files changed, 54 insertions(+), 4 deletions(-) diff --git a/src/notzed.jjmpeg.awt/classes/au/notzed/jjmpeg/awt/JavaPixelReader.java b/src/notzed.jjmpeg.awt/classes/au/notzed/jjmpeg/awt/JavaPixelReader.java index 8c60c9f..0f7c470 100644 --- a/src/notzed.jjmpeg.awt/classes/au/notzed/jjmpeg/awt/JavaPixelReader.java +++ b/src/notzed.jjmpeg.awt/classes/au/notzed/jjmpeg/awt/JavaPixelReader.java @@ -105,4 +105,10 @@ public class JavaPixelReader implements AVPixelReader { public void getPixels(int y, int h, int fmt, int[] buffer, int offset, int scanlineStride) { reader.getPixels(y, h, fmt, buffer, offset, scanlineStride); } + + @Override + public void release() { + reader.release(); + } + } diff --git a/src/notzed.jjmpeg.fx/classes/au/notzed/jjmpeg/fx/FXPixelReader.java b/src/notzed.jjmpeg.fx/classes/au/notzed/jjmpeg/fx/FXPixelReader.java index 957c08a..dddf359 100644 --- a/src/notzed.jjmpeg.fx/classes/au/notzed/jjmpeg/fx/FXPixelReader.java +++ b/src/notzed.jjmpeg.fx/classes/au/notzed/jjmpeg/fx/FXPixelReader.java @@ -105,4 +105,9 @@ public class FXPixelReader implements PixelReader, AVPixelReader { public void getPixels(int y, int h, int fmt, int[] buffer, int offset, int scanlineStride) { reader.getPixels(y, h, fmt, buffer, offset, scanlineStride); } + + @Override + public void release() { + reader.release(); + } } diff --git a/src/notzed.jjmpeg/classes/au/notzed/jjmpeg/AVFrame.java b/src/notzed.jjmpeg/classes/au/notzed/jjmpeg/AVFrame.java index 05b9026..3a8ad2d 100644 --- a/src/notzed.jjmpeg/classes/au/notzed/jjmpeg/AVFrame.java +++ b/src/notzed.jjmpeg/classes/au/notzed/jjmpeg/AVFrame.java @@ -45,6 +45,20 @@ public class AVFrame extends AVObject { public native void copyProperties(AVFrame source); + /** + * Call av_frame_ref() to copy the contents. this must be newly + * allocated or unreffed. + * + * @param source + */ + public native void ref(AVFrame source); + + /** + * Call av_frame_unref() to clear allocated data. This does not free + * the AVFrame itself. + */ + public native void unref(); + /* * Data stuff. *

diff --git a/src/notzed.jjmpeg/classes/au/notzed/jjmpeg/AVPixelReader.java b/src/notzed.jjmpeg/classes/au/notzed/jjmpeg/AVPixelReader.java index f7578da..fcfa91a 100644 --- a/src/notzed.jjmpeg/classes/au/notzed/jjmpeg/AVPixelReader.java +++ b/src/notzed.jjmpeg/classes/au/notzed/jjmpeg/AVPixelReader.java @@ -47,4 +47,5 @@ public interface AVPixelReader { public void getPixels(int y, int h, int fmt, int[] buffer, int offset, int scanlineStride); + public void release(); } 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 b2678bc..489657a 100644 --- a/src/notzed.jjmpeg/classes/au/notzed/jjmpeg/io/JJMediaReader.java +++ b/src/notzed.jjmpeg/classes/au/notzed/jjmpeg/io/JJMediaReader.java @@ -425,6 +425,10 @@ public class JJMediaReader implements AutoCloseable { return codec; } + public AVCodecContext getContext() { + return c; + } + /** * Retrieve duration of sequence, in milliseconds. * diff --git a/src/notzed.jjmpeg/jni/jj-avframe.c b/src/notzed.jjmpeg/jni/jj-avframe.c index bf79809..d2eb767 100644 --- a/src/notzed.jjmpeg/jni/jj-avframe.c +++ b/src/notzed.jjmpeg/jni/jj-avframe.c @@ -206,6 +206,7 @@ JNIEXPORT jobject JNICALL Java_au_notzed_jjmpeg_AVFrame_alloc__IIJI } /* **************************************** */ +// ?useful? JNIEXPORT void JNICALL Java_au_notzed_jjmpeg_AVFrame_copy (JNIEnv *env, jobject jdst, jobject jsrc) { AVFrame *dst = NativeZ_getP(env, jdst); @@ -213,11 +214,11 @@ JNIEXPORT void JNICALL Java_au_notzed_jjmpeg_AVFrame_copy int res; res = DLCALL(av_frame_copy)(dst, src); - // FIXME: this is not an i/o error if (res < 0) - jjthrowAVIOException(env, res, "Frame copy failed"); + nativez_ThrowOutOfMemoryError(env, "Copy frame"); } +// ?useful? JNIEXPORT void JNICALL Java_au_notzed_jjmpeg_AVFrame_copyProperties (JNIEnv *env, jobject jdst, jobject jsrc) { AVFrame *dst = NativeZ_getP(env, jdst); @@ -225,11 +226,27 @@ JNIEXPORT void JNICALL Java_au_notzed_jjmpeg_AVFrame_copyProperties int res; res = DLCALL(av_frame_copy_props)(dst, src); - // FIXME: this is not an i/o error if (res < 0) - jjthrowAVIOException(env, res, "Frame properties copy failed"); + nativez_ThrowOutOfMemoryError(env, "Copy frame"); } +JNIEXPORT void JNICALL Java_au_notzed_jjmpeg_AVFrame_ref +(JNIEnv *env, jobject jdst, jobject jsrc) { + AVFrame *dst = NativeZ_getP(env, jdst); + AVFrame *src = NativeZ_getP(env, jsrc); + int res; + + res = DLCALL(av_frame_ref)(dst, src); + if (res < 0) + nativez_ThrowOutOfMemoryError(env, "Copy frame"); +} + +JNIEXPORT void JNICALL Java_au_notzed_jjmpeg_AVFrame_unref +(JNIEnv *env, jobject jo) { + AVFrame *frame = NativeZ_getP(env, jo); + + DLCALL(av_frame_unref)(frame); +} /* ********************************************************************** */ /* FrameReader */ diff --git a/src/notzed.jjmpeg/jni/jj-avframe.def b/src/notzed.jjmpeg/jni/jj-avframe.def index f7fc80b..a409b7e 100644 --- a/src/notzed.jjmpeg/jni/jj-avframe.def +++ b/src/notzed.jjmpeg/jni/jj-avframe.def @@ -4,6 +4,9 @@ header avutil libavutil/frame.h { av_frame_copy_props av_frame_copy + av_frame_ref + av_frame_unref + av_frame_get_buffer } -- 2.39.5