Skip to content

Commit

Permalink
Fixed a few minor things related to audio support in `FFmpegFrameGrab…
Browse files Browse the repository at this point in the history
…ber`
  • Loading branch information
saudet committed Oct 8, 2012
1 parent 82bf5dd commit 5b41901
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<maven.build.timestamp.format>yyyyMMddhhmm</maven.build.timestamp.format>
<javacpp.skip>false</javacpp.skip> <!-- To skip native compilation phase: -Djavacpp.cpp=true -->
<platform.name>${os.name}-${os.arch}</platform.name> <!-- For Android: -Dplatform.name=android-arm -->
<platform.root></platform.root> <!-- For Android: -Dplatform.root=<path/to/android-ndk-r8> -->
<platform.root></platform.root> <!-- For Android: -Dplatform.root=<path/to/android-ndk-r8b> -->
<compiler.path></compiler.path> <!-- For Android: -Dcompiler.path=<path/to/arm-linux-androideabi-g++> -->
<javacpp.options></javacpp.options> <!-- To pass other options to JavaCPP -->
<!-- Reinclude the following with profiles, either all: -Pall
Expand Down
28 changes: 18 additions & 10 deletions src/main/java/com/googlecode/javacv/FFmpegFrameGrabber.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import com.googlecode.javacpp.Loader;
import com.googlecode.javacpp.PointerPointer;
import java.io.File;
import java.nio.Buffer;
import java.nio.ByteBuffer;

import static com.googlecode.javacv.cpp.avcodec.*;
Expand Down Expand Up @@ -171,6 +172,8 @@ public void release() throws Exception {
private AVFrame picture, picture_rgb;
private BytePointer buffer_rgb;
private AVFrame samples_frame;
private BytePointer samples_ptr;
private Buffer samples_buf;
private AVPacket pkt, pkt2;
private int sizeof_pkt;
private int[] got_frame;
Expand Down Expand Up @@ -322,7 +325,7 @@ public void start() throws Exception {
av_dict_set(options, "channels", "" + audioChannels, 0);
}
if ((ret = avformat_open_input(oc, filename, f, options)) < 0) {
throw new Exception("avformat_open_input() error " + ret + ": Could not open input \"" + filename + "\".");
throw new Exception("avformat_open_input() error " + ret + ": Could not open input \"" + filename + "\". (Has setFormat() been called?)");
}
av_dict_free(options);

Expand Down Expand Up @@ -555,18 +558,23 @@ private Frame grabFrame(boolean processImage, boolean doAudio) throws Exception
/* if a frame has been decoded, output it */
int data_size = av_samples_get_buffer_size(null, audio_c.channels(),
samples_frame.nb_samples(), audio_c.sample_fmt(), 1);
ByteBuffer samples = samples_frame.data(0).capacity(data_size).asBuffer();
done = true;
frame.image = null;
frame.samples = samples;
switch (samples_frame.format()) {
case AV_SAMPLE_FMT_U8: frame.samples = samples; break;
case AV_SAMPLE_FMT_S16: frame.samples = samples.asShortBuffer(); break;
case AV_SAMPLE_FMT_S32: frame.samples = samples.asIntBuffer(); break;
case AV_SAMPLE_FMT_FLT: frame.samples = samples.asFloatBuffer(); break;
case AV_SAMPLE_FMT_DBL: frame.samples = samples.asDoubleBuffer(); break;
default: assert false;
BytePointer p = samples_frame.data(0);
if (!p.equals(samples_ptr) || samples_ptr.capacity() < data_size) {
samples_ptr = p.capacity(data_size);
ByteBuffer b = p.asBuffer();
switch (samples_frame.format()) {
case AV_SAMPLE_FMT_U8: samples_buf = b; break;
case AV_SAMPLE_FMT_S16: samples_buf = b.asShortBuffer(); break;
case AV_SAMPLE_FMT_S32: samples_buf = b.asIntBuffer(); break;
case AV_SAMPLE_FMT_FLT: samples_buf = b.asFloatBuffer(); break;
case AV_SAMPLE_FMT_DBL: samples_buf = b.asDoubleBuffer(); break;
default: assert false;
}
}
frame.samples = samples_buf.position(0).
limit(data_size / av_get_bytes_per_sample(samples_frame.format()));
}
}
}
Expand Down

0 comments on commit 5b41901

Please sign in to comment.