Skip to content

Commit

Permalink
* Add FFmpegFrameGrabber.grabImage() method to restore the functio…
Browse files Browse the repository at this point in the history
…nality previously provided by `IplImage grab()` (issue #116)

Also add calls to `av_dict_free()` to release memory allocated for options and metadata
  • Loading branch information
saudet committed May 7, 2015
1 parent 1bd9de6 commit 9838e64
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/bytedeco/javacv/FFmpegFrameGrabber.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/bytedeco/javacv/FFmpegFrameRecorder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 9838e64

Please sign in to comment.