From b1638f7c07a0554e5e87770a0781e797da8150bb Mon Sep 17 00:00:00 2001 From: mjbudd77 Date: Fri, 30 Apr 2021 13:40:17 -0400 Subject: [PATCH] Successful test of x264 encoder in linux. --- src/drivers/Qt/AviRecord.cpp | 74 ++++++++++++++++++++++---------- src/drivers/Qt/AviRecord.h | 2 +- src/drivers/Qt/ConsoleWindow.cpp | 2 +- 3 files changed, 53 insertions(+), 25 deletions(-) diff --git a/src/drivers/Qt/AviRecord.cpp b/src/drivers/Qt/AviRecord.cpp index 040f0fdc..78034a0b 100644 --- a/src/drivers/Qt/AviRecord.cpp +++ b/src/drivers/Qt/AviRecord.cpp @@ -14,6 +14,7 @@ #include "Qt/avi/gwavi.h" #include "Qt/nes_shm.h" #include "Qt/ConsoleUtilities.h" +#include "Qt/fceuWrapper.h" static gwavi_t *gwavi = NULL; static bool recordEnable = false; @@ -26,6 +27,7 @@ static int abufSize = 0; static uint32_t *rawVideoBuf = NULL; static int16_t *rawAudioBuf = NULL; static int videoFormat = 2; +static int audioSampleRate = 48000; //************************************************************************************** static void convertRgb_32_to_24( const unsigned char *src, unsigned char *dest, int w, int h, int nPix ) @@ -258,7 +260,7 @@ static int close(void) }; // End X264 namespace #endif //************************************************************************************** -int aviRecordOpenFile( const char *filepath, int format, int width, int height ) +int aviRecordOpenFile( const char *filepath ) { char fourcc[8]; gwavi_audio_t audioConfig; @@ -270,12 +272,15 @@ int aviRecordOpenFile( const char *filepath, int format, int width, int height ) } fps = FCEUI_GetDesiredFPS() >> 24; + g_config->getOption("SDL.Sound.Rate", &audioSampleRate); + audioConfig.channels = 1; audioConfig.bits = 16; - audioConfig.samples_per_second = 48000; + audioConfig.samples_per_second = audioSampleRate; memset( fourcc, 0, sizeof(fourcc) ); + if ( videoFormat == 1 ) { strcpy( fourcc, "I420"); @@ -287,7 +292,7 @@ int aviRecordOpenFile( const char *filepath, int format, int width, int height ) gwavi = new gwavi_t(); - if ( gwavi->open( "/tmp/test.avi", nes_shm->video.ncol, nes_shm->video.nrow, fourcc, fps, &audioConfig ) ) + if ( gwavi->open( filepath, nes_shm->video.ncol, nes_shm->video.nrow, fourcc, fps, &audioConfig ) ) { printf("Error: Failed to open AVI file.\n"); recordEnable = false; @@ -300,6 +305,11 @@ int aviRecordOpenFile( const char *filepath, int format, int width, int height ) abufSize = 48000; rawAudioBuf = (int16_t*)malloc( abufSize * sizeof(uint16_t) ); + vbufHead = 0; + vbufTail = 0; + abufHead = 0; + abufTail = 0; + recordEnable = true; return 0; } @@ -315,6 +325,11 @@ int aviRecordAddFrame( void ) { return -1; } + if ( FCEUI_EmulationPaused() ) + { + return 0; + } + int i, head, numPixels, availSize; numPixels = nes_shm->video.ncol * nes_shm->video.nrow; @@ -418,16 +433,22 @@ AviRecordDiskThread_t::~AviRecordDiskThread_t(void) void AviRecordDiskThread_t::run(void) { int numPixels, width, height, numPixelsReady = 0; - int numSamples = 0; + int fps = 60, numSamples = 0; unsigned char *rgb24; int16_t audioOut[48000]; uint32_t videoOut[1048576]; + char writeAudio = 1; + int avgAudioPerFrame; printf("AVI Record Disk Start\n"); setPriority( QThread::HighestPriority ); - //avgAudioPerFrame = 48000 / 60; + fps = FCEUI_GetDesiredFPS() >> 24; + + avgAudioPerFrame = (audioSampleRate / fps) + 1; + + printf("Avg Audio Rate per Frame: %i \n", avgAudioPerFrame ); width = nes_shm->video.ncol; height = nes_shm->video.nrow; @@ -453,6 +474,10 @@ void AviRecordDiskThread_t::run(void) if ( numPixelsReady >= numPixels ) { + //printf("Adding Frame:%i\n", frameCount++); + + writeAudio = 1; + if ( videoFormat == 1) { Convert_4byte_To_I420Frame<4>(videoOut,rgb24,numPixels,width); @@ -474,26 +499,29 @@ void AviRecordDiskThread_t::run(void) numPixelsReady = 0; - numSamples = 0; - - while ( abufHead != abufTail ) + if ( writeAudio ) { - audioOut[ numSamples ] = rawAudioBuf[ abufTail ]; numSamples++; - - abufTail = (abufTail + 1) % abufSize; - - //if ( numSamples > avgAudioPerFrame ) - //{ - // break; - //} - } - - if ( numSamples > 0 ) - { - //printf("NUM Audio Samples: %i \n", numSamples ); - gwavi->add_audio( (unsigned char *)audioOut, numSamples*2); - numSamples = 0; + + while ( abufHead != abufTail ) + { + audioOut[ numSamples ] = rawAudioBuf[ abufTail ]; numSamples++; + + abufTail = (abufTail + 1) % abufSize; + + if ( numSamples > avgAudioPerFrame ) + { + break; + } + } + + if ( numSamples > 0 ) + { + //printf("NUM Audio Samples: %i \n", numSamples ); + gwavi->add_audio( (unsigned char *)audioOut, numSamples*2); + + numSamples = 0; + } } } else diff --git a/src/drivers/Qt/AviRecord.h b/src/drivers/Qt/AviRecord.h index 598c8213..485672df 100644 --- a/src/drivers/Qt/AviRecord.h +++ b/src/drivers/Qt/AviRecord.h @@ -7,7 +7,7 @@ #include #include -int aviRecordOpenFile( const char *filepath, int format, int width, int height ); +int aviRecordOpenFile( const char *filepath ); int aviRecordAddFrame( void ); diff --git a/src/drivers/Qt/ConsoleWindow.cpp b/src/drivers/Qt/ConsoleWindow.cpp index 41d4e642..b79fca8f 100644 --- a/src/drivers/Qt/ConsoleWindow.cpp +++ b/src/drivers/Qt/ConsoleWindow.cpp @@ -3116,7 +3116,7 @@ void consoleWin_t::aviOpen(void) else { fceuWrapperLock(); - aviRecordOpenFile( NULL, 0, 256, 240 ); + aviRecordOpenFile( "/tmp/test.avi" ); aviDiskThread->start(); fceuWrapperUnLock(); }