From 9838e64de8d4cf12301e4b355709ba83748d5fca Mon Sep 17 00:00:00 2001 From: Samuel Audet Date: Thu, 7 May 2015 21:25:57 +0900 Subject: [PATCH] * Add `FFmpegFrameGrabber.grabImage()` method to restore the functionality previously provided by `IplImage grab()` (issue #116) Also add calls to `av_dict_free()` to release memory allocated for options and metadata --- CHANGELOG.md | 1 + .../org/bytedeco/javacv/FFmpegFrameGrabber.java | 5 +++++ .../org/bytedeco/javacv/FFmpegFrameRecorder.java | 15 +++++++++++++++ 3 files changed, 21 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d28c4f24..5a52b78b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ + * Add `FFmpegFrameGrabber.grabImage()` method to restore the functionality previously provided by `IplImage grab()` ([issue #116](https://github.com/bytedeco/javacv/issues/116)) * Give users of `FFmpegFrameGrabber` and `FFmpegFrameRecorder` access to more options and metadata ([issue #132](https://github.com/bytedeco/javacv/issues/132)) * Add the ability to specify from which video and audio streams `FFmpegFrameGrabber` should grab from ([issue #135](https://github.com/bytedeco/javacv/issues/135)) * Fix `Java2DFrameConverter` when used with `BufferedImage.TYPE_INT_RGB` or other types based on `int` ([issue #140](https://github.com/bytedeco/javacv/issues/140)) diff --git a/src/main/java/org/bytedeco/javacv/FFmpegFrameGrabber.java b/src/main/java/org/bytedeco/javacv/FFmpegFrameGrabber.java index 2b9f243d..b9377037 100644 --- a/src/main/java/org/bytedeco/javacv/FFmpegFrameGrabber.java +++ b/src/main/java/org/bytedeco/javacv/FFmpegFrameGrabber.java @@ -472,6 +472,7 @@ public void startUnsafe() throws Exception { if ((ret = avcodec_open2(video_c, codec, options)) < 0) { throw new Exception("avcodec_open2() error " + ret + ": Could not open video codec."); } + av_dict_free(options); // Hack to correct wrong frame rates that seem to be generated by some codecs if (video_c.time_base().num() > 1000 && video_c.time_base().den() == 1) { @@ -532,6 +533,7 @@ public void startUnsafe() throws Exception { if ((ret = avcodec_open2(audio_c, codec, options)) < 0) { throw new Exception("avcodec_open2() error " + ret + ": Could not open audio codec."); } + av_dict_free(options); // Allocate audio samples frame if ((samples_frame = av_frame_alloc()) == null) { @@ -609,6 +611,9 @@ private void processImage() throws Exception { public Frame grab() throws Exception { return grabFrame(true, true, false); } + public Frame grabImage() throws Exception { + return grabFrame(true, false, false); + } public Frame grabFrame(boolean processImage) throws Exception { return grabFrame(processImage, true, false); } diff --git a/src/main/java/org/bytedeco/javacv/FFmpegFrameRecorder.java b/src/main/java/org/bytedeco/javacv/FFmpegFrameRecorder.java index 2ec137f0..639fdbe8 100644 --- a/src/main/java/org/bytedeco/javacv/FFmpegFrameRecorder.java +++ b/src/main/java/org/bytedeco/javacv/FFmpegFrameRecorder.java @@ -192,6 +192,14 @@ public void releaseUnsafe() throws Exception { av_free(audio_outbuf); audio_outbuf = null; } + if (video_st.metadata() != null) { + av_dict_free(video_st.metadata()); + video_st.metadata(null); + } + if (audio_st.metadata() != null) { + av_dict_free(audio_st.metadata()); + audio_st.metadata(null); + } video_st = null; audio_st = null; @@ -208,6 +216,12 @@ public void releaseUnsafe() throws Exception { av_free(oc.streams(i)); } + /* free metadata */ + if (oc.metadata() != null) { + av_dict_free(oc.metadata()); + oc.metadata(null); + } + /* free the stream */ av_free(oc); oc = null; @@ -633,6 +647,7 @@ public void startUnsafe() throws Exception { } /* write the stream header, if any */ avformat_write_header(oc.metadata(metadata), options); + av_dict_free(options); } public void stop() throws Exception {