FileInputStream istream;
AVDictionary options = new AVDictionary(null);
AVInputFormat inputFormat = null;
BytePointer buffer;
int BUFFER_SIZE=16384;
// allocate buffer
buffer = new BytePointer(av_malloc(BUFFER_SIZE));
// create format context
formatContext = avformat_alloc_context();
formatContext.pb(avio_alloc_context(buffer, BUFFER_SIZE, 0, null, new ReadInput(istream), null, null));
formatContext.pb().seekable(0);
// open input
if (avformat_open_input(formatContext, "", inputFormat, options) < 0) {
throw new JavaAVException("Could not open input: " + inputSource);
}
avformat_open_input only open 10 input with AVIOContext in the same time, and not enter the Read_packet_Pointer_BytePointer_int function callback. why?
It's the default maximum number of callbacks available. We can change that with the @Allocator(max=...) annotation value as added in commit https://github.com/bytedeco/javacpp/commit/8fa2688b33db1eb9fef91839947969a9687d08a4.
But, when I added @Allocator(max=...) to the Callback function, still only open 10 input in the same time in parallel, and if I add 'synchronized', it can output 'max' tasks, but the tasks were executed one by one, not in parallel , why?
synchronized (org.bytedeco.ffmpeg.global.avcodec.class) {
ffmpegProcessInput(...)
}
It's possible your callbacks are getting garbage collected. Keep references in fields to make sure that doesn't happen.
It's possible your callbacks are getting garbage collected. Keep references in fields to make sure that doesn't happen.
No, the callbacks are in each ffmpegProcessInput which is not dealloced until running exited.
@Allocator(max=1000)
static class AVIOInterruptCallackPointer extends AVIOInterruptCB.Callback_Pointer {
public AVIOInterruptCallackPointer() { }
public AVIOInterruptCallackPointer(Pointer p) { super(p); }
}
@Allocator(max=1000)
static class ReadPacketBytePointerInt extends Read_packet_Pointer_BytePointer_int {
public ReadPacketBytePointerInt() { }
public ReadPacketBytePointerInt(Pointer p) { super(p); }
}
@Allocator(max=1000)
static class AVIOClosePointer extends AVFormatContext.Io_close_AVFormatContext_AVIOContext {
public AVIOClosePointer() { }
public AVIOClosePointer(Pointer p) { super(p);
}
int ffmpegProcessInput() {
AVIOInterruptCallackPointer callbackPointer = null;
ReadPacketBytePointerInt readBufferCB = null;
AVIOClosePointer ofmtIoCloseCB = null;
avformat_open_input...;
...
while (av_read_frame()) {
...
}
dealloc....
}
The Allocator annotation is only effective on direct subclasses of
FunctionPointer.
So, have any solutions to solve the overhead of ten input in parallel ?
Overhead of what? What are you trying to do?
And javacpp-ffmpeg often crash while open muilti input in parallel without synchronized (org.bytedeco.ffmpeg.global.avcodec.class), in function av_read_frame or av_interleaved_write_frame or av_write_trailer
Overhead of what? What are you trying to do?
Sorry , opened more than ten inputs in parallel. if added synchronized, files output one by one, not in parallel.
We can open as many as we want, there's no limits. The only issue is that,
if you need to use multiple instances of the callback, you'll have to
increase the max value of its Allocator annotation, as explained above.
We can open as many as we want, there's no limits. The only issue is that, if you need to use multiple instances of the callback, you'll have to increase the max value of its Allocator annotation, as explained above.
The Allocator annotation is only effective on direct subclasses of FunctionPointer.
But I wanted to use ffmpeg subclasses callbacks(AVIOInterruptCallackPointer / ReadPacketBytePointerInt) for opening, how to use @Allocator(max...) in them?
We can't, but we can use only one instance, for example, as is done in
FFmpegFrameGrabber.
We can't, but we can use only one instance, for example, as is done in FFmpegFrameGrabber.
Ok, thanks.
BTW, why do you need to use multiple instances of those callbacks? FFmpeg was designed so that we don't need to do that.
BTW, why do you need to use multiple instances of those callbacks? FFmpeg was designed so that we don't need to do that.
I wanted to use the callback in each worker thread separately.
The same callback function can be called from multiple threads. There's no issue with that.
The same callback function can be called from multiple threads. There's no issue with that.
Yeah, got it. It has been tested ok.