rockcarry wrote:Anes wrote:Is it worth to try to encode in a different thread?
I made the following: i created a thread like this:
so at the end of the frame i set "video_record" to "1".
The emu run fast, but when i play the video runs too fast, i don't know why.
I suggest you first write some test code for ffencoder, for example, generate random data in audio & video buffer, or some special data such as color bar, sine wave, etc. And then try to use ffencode to encode audio & video into mp4 file. After this, you will exactly know how to use ffencode.
Then you can try to use ffencoder in you emulator, but I suggest you try it in single thread first. In my ffnes, I am using single thread, so I am not sure it can work when using in a different thread. In theory it can work in multi-thread case, because ffmpeg support multi-thread.
And for the process of encoding thread, if you using multi-thread, I sugguest you using event intead of video_record flag and Sleep().
I have a primary emulation thread which isn't connected to the GUI thread at all. All effects/graphics conversion/upscaling is done using a sleeping-dispatch thread. Technically if my emulator finished emulating one frame it dispatches the PPU frame to another thread/threads which takes care of upscaling (2xsai,xbrz, ntsc, 2x, 3x, tv modes, 1:1 -> 8:7 resizing, etc). when it finished the frame it will pause itself and send the data to the GUI thread..
The GUI thread draws the frame in the window and also encodes it to a videofile (using VfW). probably will add your ffencoder.dll to as it also compresses audio!. The reason why I have done this is as follows:
first:
The emulation thread doesn't need to be paused/halted, when the rendering/upscaling/conversion thread is compositing the image. (unless the 2nd frame is ready before the 1st frame has been processed then the emulation thread needs to be paused). After compositing the image the GUI thread takes over and draws it to the screen and if you are recording.. encodes it to a video stream.
And then. while we are encoding to a videostream.. the emulation thread fires another composition request.. meaning we have 3 threads taking care of:
* emulation
* display composition
* and encoding to a video stream.
at any given moment nothing is idle. because when the video encode is complete, the next frame is ready or almost ready from the compositor and the emulation thread is already computing the next frame. Ofcourse using 4xBRZ (1024x672) will bog down the emulator eventually on a old core2quad cpu. At that moment we are just asking too much.