lotheren / javacv

Automatically exported from code.google.com/p/javacv
0 stars 0 forks source link

FFmpegFrameGrabber multi threading issue #377

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Use multiple threads to grab frames for particular time interval.
2. Use 3 or more thread, each thread grabs frame for particular time interval.
3. All threads are working on the same video file to capture frame.

What is the expected output? What do you see instead?
Each thread should grab the frames those are assign to that tread separately to 
process the large video. If i use single thread, it take very long to process.

What version of the product are you using? On what operating system?
- Android 4.0 and later.

Please provide any additional information below.
I want to grab the frames from the local SDCard video file. If i use single 
thread, everything is working fine. But it takes lots of time to process the 
single file. To overcome this is that i am using multiple threads, each tread 
capture frames from different intervals on the same video file. 

Here, i am attaching my main source code. Please help me on this. Any help is 
appreciate. 

Original issue reported on code.google.com by java.web...@gmail.com on 24 Oct 2013 at 11:54

Attachments:

GoogleCodeExporter commented 8 years ago
That should work fine. What is the issue?

Original comment by samuel.a...@gmail.com on 24 Oct 2013 at 12:01

GoogleCodeExporter commented 8 years ago
Hi there,
Thanks for your response.

=> GrabberThread is a thread class as show in above attached file 
"MainActivity.java" that is responsible to grab frame that is assigned to it.
e.g.
new GrabberThread(1, 5).start(); //Grab frames of 1st,2nd,3rd,4th and 5th 
second.
new GrabberThread(6, 10).start(); //Grab Frames of 6th to 10th frame as above.
new GrabberThread(11, 15).start(); //Grab Frames of 11th to 15th frame as above.

=> The issue is that if i use three separate thread as shown above, it does 
nothing. Very strange thing is that it does not show any error message on 
console. It stucks at line 
m_ffmpegFrameGrabber = new FFmpegFrameGrabber(m_file);
in above "MainActivity.java" file.

=> If i use single thread everything is working fine like :
new GrabberThread(1, 5).start(); //Grab frames of 1st,2nd,3rd,4th and 5th 
second.

Original comment by java.web...@gmail.com on 25 Oct 2013 at 5:16

GoogleCodeExporter commented 8 years ago
Sounds like the initialization functions are not thread safe:
        avcodec_register_all();
        avdevice_register_all();
        av_register_all();
        avformat_network_init();

Could you try to move those out of the constructor and into a static { } block 
and see if that fixes the problem? thanks!

Original comment by samuel.a...@gmail.com on 25 Oct 2013 at 10:22

GoogleCodeExporter commented 8 years ago
Thanks for your help.
I move all those methods in static block as you suggest. Now i got the error 
message like this :

com.googlecode.javacv.FrameGrabber$Exception: avcodec_open2() error -22: Could 
not open video codec.
at 
com.example.javacvframe.FFmpegFrameGrabberNew.start(FFmpegFrameGrabberNew.java:4
99)
at com.example.javacvframe.MainActivity$GrabberThread.run(MainActivity.java:96)

Please help.

Original comment by java.web...@gmail.com on 25 Oct 2013 at 10:44

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
Looks like avcodec_open2() isn't thread safe either... We could safeguard the 
whole start() method, just to be sure, in the same way as I'm doing for 
Pointer.DeallocatorReference.add()/remove() here:
https://code.google.com/p/javacpp/source/browse/src/main/java/com/googlecode/jav
acpp/Pointer.java#230
i.e.:

synchronized(FFmpegFrameGrabber.class) {
...
}

Does that work?

Original comment by samuel.a...@gmail.com on 25 Oct 2013 at 11:02

GoogleCodeExporter commented 8 years ago
Hi,

Thank you so much. You are genius. It is working perfectly as i want.

Thanks again.

I have one question here.

When i user m_ffmpegFrameGrabber.setTimestamp(MICROSECOND * m_startSecond);
It is directly seek to particular time stamp or it is traversing one by one 
bytes/seconds etc?

Original comment by java.web...@gmail.com on 25 Oct 2013 at 12:21

GoogleCodeExporter commented 8 years ago
It should seek yes, but that depends on the container of the media file. For 
some files, especially corrupted one, FFmpeg needs to traverse all the file.

Original comment by samuel.a...@gmail.com on 26 Oct 2013 at 7:53

GoogleCodeExporter commented 8 years ago
I've made the changes in this revision:
http://code.google.com/p/javacv/source/detail?r=31e4015a2349905bbe877fb7c57e68a9
a5c7b61d

Let me know that it works well, and thanks for reporting!

Original comment by samuel.a...@gmail.com on 3 Nov 2013 at 3:31

GoogleCodeExporter commented 8 years ago
Thanks for updating on the issue. Everything works well but now face another 
issue related to my previous question whether it is really seeking or 
traversing.

Let me explain in detail :

1) Video length is 60 Seconds.
2) Frame rate is 24.
3) I am capturing the frame at 55th second. I am getting right frame, but the 
problem is that the data usage is more than actual size of the frame. For 
Example, The data usage is 320KB while actual size of the frame/image is 110KB 
that is stored on the device's sdcard. This causes very serioud issue if i 
capture 8-10 frames. I uses near about 6-7 MB. That is major issue.

I am not sure why this is happening and what is the issue behind this, but it 
may be due to "It reads audio packet also that is not require in this case".

Please help me on this. Your help is really appreciated.

Thanks.

Original comment by java.web...@gmail.com on 12 Nov 2013 at 7:34

GoogleCodeExporter commented 8 years ago
You could load compressed data in memory, and decompress on demand, but it 
would require a bit of work to get FFmpeg to decompress data from memory. If 
you're interested in that, flag issue #363 to get potential future updates 
about that.

Original comment by samuel.a...@gmail.com on 17 Nov 2013 at 8:26

GoogleCodeExporter commented 8 years ago
In any case, the fix for multithreading is included in JavaCV 0.7!

Original comment by samuel.a...@gmail.com on 7 Jan 2014 at 1:02

GoogleCodeExporter commented 8 years ago
Can any one help me how to use opencv for multi processing of images for ocr 
using java

Original comment by sweetyte...@gmail.com on 12 Feb 2015 at 1:42