Skip to content

Commit

Permalink
[feat] filter: add input args property
Browse files Browse the repository at this point in the history
  • Loading branch information
fastZhe committed Nov 26, 2024
1 parent fd9ee20 commit a591258
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 34 deletions.
46 changes: 12 additions & 34 deletions src/main/java/org/bytedeco/javacv/FFmpegFrameFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import java.util.Arrays;
import java.util.Locale;

import org.bytedeco.javacpp.BytePointer;
import org.bytedeco.javacpp.DoublePointer;
import org.bytedeco.javacpp.FloatPointer;
Expand Down Expand Up @@ -92,13 +90,6 @@ public static class Exception extends FrameFilter.Exception {
public Exception(String message, Throwable cause) { super(message, cause); }
}

public static interface InputArgsGenerate {
public String generateArgs(int i,Object[] argCopy,String template);
}

private InputArgsGenerate videoInputArgsGenerate;
private InputArgsGenerate audioInputArgsGenerate;

private static Exception loadingException = null;
public static void tryLoad() throws Exception {
if (loadingException != null) {
Expand Down Expand Up @@ -317,6 +308,12 @@ public synchronized void startUnsafe() throws Exception {
frame = new Frame();
default_layout = new AVChannelLayout().retainReference();

if (videoFilterArgs!=null && videoInputs!=videoFilterArgs.length){
throw new Exception("The length of videoFilterArgs is different from videoInputs");
}
if (audioFilterArgs!=null && audioInputs!=audioFilterArgs.length){
throw new Exception("The length of audioFilterArgs is different from audioInputs");
}
if (image_frame == null || samples_frame == null || filt_frame == null) {
throw new Exception("Could not allocate frames");
}
Expand Down Expand Up @@ -351,22 +348,17 @@ private void startVideoUnsafe() throws Exception {

/* buffer video source: the decoded frames from the decoder will be inserted here. */
AVRational r = av_d2q(aspectRatio > 0 ? aspectRatio : 1, 255);
Object[] argList=new Object[]{imageWidth,imageHeight,pixelFormat,time_base.num(),time_base.den(),r.num(), r.den(), frame_rate.num(), frame_rate.den()};
String argsTemplate="video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d:frame_rate=%d/%d";
String args = String.format(Locale.ROOT, argsTemplate, argList);

String argsBase = String.format(Locale.ROOT, "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d:frame_rate=%d/%d",
imageWidth, imageHeight, pixelFormat, time_base.num(), time_base.den(), r.num(), r.den(), frame_rate.num(), frame_rate.den());
buffersrc_ctx = new AVFilterContext[videoInputs];
setpts_ctx = new AVFilterContext[videoInputs];
for (int i = 0; i < videoInputs; i++) {
String name = videoInputs > 1 ? i + ":v" : "in";
outputs[i] = avfilter_inout_alloc();
String realArgs=args;
if (this.videoInputArgsGenerate!=null){
realArgs=this.videoInputArgsGenerate.generateArgs(i, Arrays.copyOf(argList,argList.length),argsTemplate);
}

String args=videoFilterArgs!=null && videoFilterArgs[i]!=null ? videoFilterArgs[i]:argsBase;
ret = avfilter_graph_create_filter(buffersrc_ctx[i] = new AVFilterContext().retainReference(), buffersrc, name,
realArgs, null, filter_graph);
args, null, filter_graph);
if (ret < 0) {
throw new Exception("avfilter_graph_create_filter() error " + ret + ": Cannot create video buffer source.");
}
Expand Down Expand Up @@ -456,20 +448,14 @@ private void startAudioUnsafe() throws Exception {

abuffersrc_ctx = new AVFilterContext[audioInputs];
asetpts_ctx = new AVFilterContext[audioInputs];
String argsTemplate="channels=%d:sample_fmt=%d:sample_rate=%d:channel_layout=%d";
for (int i = 0; i < audioInputs; i++) {
String name = audioInputs > 1 ? i + ":a" : "in";
aoutputs[i] = avfilter_inout_alloc();

/* buffer audio source: the decoded frames from the decoder will be inserted here. */
av_channel_layout_default(default_layout, audioChannels);
Object[] argList=new Object[]{audioChannels, sampleFormat, sampleRate, default_layout.u_mask()};
String aargs = String.format(Locale.ROOT,argsTemplate,argList);

if (this.audioInputArgsGenerate!=null){
aargs=this.audioInputArgsGenerate.generateArgs(i,Arrays.copyOf(argList,argList.length),argsTemplate);
}

String aargs = audioFilterArgs!=null && audioFilterArgs[i]!=null ?audioFilterArgs[i]:String.format(Locale.ROOT, "channels=%d:sample_fmt=%d:sample_rate=%d:channel_layout=%d",
audioChannels, sampleFormat, sampleRate, default_layout.u_mask());
ret = avfilter_graph_create_filter(abuffersrc_ctx[i] = new AVFilterContext().retainReference(), abuffersrc, name,
aargs, null, afilter_graph);
if (ret < 0) {
Expand Down Expand Up @@ -836,12 +822,4 @@ public synchronized Frame pullSamples() throws Exception {

}
}

public void setVideoInputArgsGenerate(InputArgsGenerate videoInputArgsGenerate) {
this.videoInputArgsGenerate = videoInputArgsGenerate;
}

public void setAudioInputArgsGenerate(InputArgsGenerate audioInputArgsGenerate) {
this.audioInputArgsGenerate = audioInputArgsGenerate;
}
}
18 changes: 18 additions & 0 deletions src/main/java/org/bytedeco/javacv/FrameFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public static FrameFilter createDefault(String filtersDescr, int imageWidth, int
protected int sampleFormat;
protected int sampleRate;
protected int audioInputs;
protected String[] videoFilterArgs ;
protected String[] audioFilterArgs ;

public String getFilters() {
return filters;
Expand Down Expand Up @@ -127,6 +129,22 @@ public void setAudioInputs(int audioInputs) {
this.audioInputs = audioInputs;
}

public String[] getVideoFilterArgs() {
return videoFilterArgs;
}

public void setVideoFilterArgs(String[] videoFilterArgs) {
this.videoFilterArgs = videoFilterArgs;
}

public String[] getAudioFilterArgs() {
return audioFilterArgs;
}

public void setAudioFilterArgs(String[] audioFilterArgs) {
this.audioFilterArgs = audioFilterArgs;
}

public static class Exception extends IOException {
public Exception(String message) { super(message); }
public Exception(String message, Throwable cause) { super(message, cause); }
Expand Down

0 comments on commit a591258

Please sign in to comment.