Skip to content

Commit

Permalink
* Add FrameFilter.videoFilterArgs/audioFilterArgs properties to su…
Browse files Browse the repository at this point in the history
…pport multiple different inputs (pull #2304)
  • Loading branch information
fastZhe authored Nov 27, 2024
1 parent 295e89f commit 2b64c33
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

* Add `FrameFilter.videoFilterArgs/audioFilterArgs` properties to support multiple different inputs ([pull #2304](https://github.com/bytedeco/javacv/pull/2304))
* Ensure `FFmpegFrameGrabber.start()` skips over streams with no codecs ([issue #2299](https://github.com/bytedeco/javacv/issues/2299))
* Add `FFmpegLogCallback.logRejectedOptions()` for debugging purposes ([pull #2301](https://github.com/bytedeco/javacv/pull/2301))

Expand Down
16 changes: 12 additions & 4 deletions src/main/java/org/bytedeco/javacv/FFmpegFrameFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -308,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 @@ -342,14 +348,15 @@ 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);
String args = 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 args = videoFilterArgs != null && videoFilterArgs[i] != null ? videoFilterArgs[i]
: 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());
ret = avfilter_graph_create_filter(buffersrc_ctx[i] = new AVFilterContext().retainReference(), buffersrc, name,
args, null, filter_graph);
if (ret < 0) {
Expand Down Expand Up @@ -447,8 +454,9 @@ private void startAudioUnsafe() throws Exception {

/* buffer audio source: the decoded frames from the decoder will be inserted here. */
av_channel_layout_default(default_layout, audioChannels);
String aargs = String.format(Locale.ROOT, "channels=%d:sample_fmt=%d:sample_rate=%d:channel_layout=%d",
audioChannels, sampleFormat, sampleRate, default_layout.u_mask());
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
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 @@ -43,12 +43,14 @@ public static FrameFilter createDefault(String filtersDescr, int imageWidth, int
protected double frameRate;
protected double aspectRatio;
protected int videoInputs;
protected String[] videoFilterArgs;

protected String afilters;
protected int audioChannels;
protected int sampleFormat;
protected int sampleRate;
protected int audioInputs;
protected String[] audioFilterArgs;

public String getFilters() {
return filters;
Expand Down Expand Up @@ -99,6 +101,14 @@ public void setVideoInputs(int videoInputs) {
this.videoInputs = videoInputs;
}

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

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

public int getAudioChannels() {
return audioChannels;
}
Expand Down Expand Up @@ -127,6 +137,14 @@ public void setAudioInputs(int audioInputs) {
this.audioInputs = audioInputs;
}

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 2b64c33

Please sign in to comment.