From 402bd2030c08f9eaf28943ed91b4d93711f92d82 Mon Sep 17 00:00:00 2001 From: EdenGottlieb Date: Thu, 16 Dec 2021 12:52:54 +0200 Subject: [PATCH 01/12] Fix framerate for 30fps --- sherlock265/VideoDecoder.cc | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/sherlock265/VideoDecoder.cc b/sherlock265/VideoDecoder.cc index 1109f7a1a..5df9001f8 100644 --- a/sherlock265/VideoDecoder.cc +++ b/sherlock265/VideoDecoder.cc @@ -37,6 +37,12 @@ using namespace videogfx; //#include "decctx.h" #include "visualize.h" +#include +#include +#include +#include + +using namespace std; VideoDecoder::VideoDecoder() : mFH(NULL), @@ -127,6 +133,7 @@ void VideoDecoder::decoder_loop() img = de265_peek_next_picture(ctx); while (img==NULL) { + auto start = std::chrono::system_clock::now(); mutex.unlock(); int more=1; de265_error err = de265_decode(ctx, &more); @@ -135,6 +142,10 @@ void VideoDecoder::decoder_loop() if (more && err == DE265_OK) { // try again to get picture + std::chrono::duration decode_time = std::chrono::system_clock::now() - start; + std::chrono::duration sleep_for_time = std::chrono::milliseconds(33) - decode_time; + std::this_thread::sleep_for(sleep_for_time); + img = de265_peek_next_picture(ctx); } else if (more && err == DE265_ERROR_WAITING_FOR_INPUT_DATA) { From 9e7e6670df823801352b5d5ca4b86e30a7d06021 Mon Sep 17 00:00:00 2001 From: EdenGottlieb Date: Thu, 16 Dec 2021 12:57:28 +0200 Subject: [PATCH 02/12] use namespace --- sherlock265/VideoDecoder.cc | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/sherlock265/VideoDecoder.cc b/sherlock265/VideoDecoder.cc index 5df9001f8..84a70919d 100644 --- a/sherlock265/VideoDecoder.cc +++ b/sherlock265/VideoDecoder.cc @@ -43,6 +43,7 @@ using namespace videogfx; #include using namespace std; +using namespace std::chrono; VideoDecoder::VideoDecoder() : mFH(NULL), @@ -133,7 +134,7 @@ void VideoDecoder::decoder_loop() img = de265_peek_next_picture(ctx); while (img==NULL) { - auto start = std::chrono::system_clock::now(); + auto start = system_clock::now(); mutex.unlock(); int more=1; de265_error err = de265_decode(ctx, &more); @@ -142,8 +143,9 @@ void VideoDecoder::decoder_loop() if (more && err == DE265_OK) { // try again to get picture - std::chrono::duration decode_time = std::chrono::system_clock::now() - start; - std::chrono::duration sleep_for_time = std::chrono::milliseconds(33) - decode_time; + duration decode_time = system_clock::now() - start; + duration sleep_for_time = milliseconds(33) - decode_time; + std::this_thread::sleep_for(sleep_for_time); img = de265_peek_next_picture(ctx); From f882c2a14e1616f5ed2dd75b716ebd33f744bd3a Mon Sep 17 00:00:00 2001 From: EdenGottlieb Date: Thu, 16 Dec 2021 12:58:08 +0200 Subject: [PATCH 03/12] rename variable --- sherlock265/VideoDecoder.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sherlock265/VideoDecoder.cc b/sherlock265/VideoDecoder.cc index 84a70919d..076230fc2 100644 --- a/sherlock265/VideoDecoder.cc +++ b/sherlock265/VideoDecoder.cc @@ -134,7 +134,8 @@ void VideoDecoder::decoder_loop() img = de265_peek_next_picture(ctx); while (img==NULL) { - auto start = system_clock::now(); + auto start_time = system_clock::now(); + mutex.unlock(); int more=1; de265_error err = de265_decode(ctx, &more); @@ -143,7 +144,7 @@ void VideoDecoder::decoder_loop() if (more && err == DE265_OK) { // try again to get picture - duration decode_time = system_clock::now() - start; + duration decode_time = system_clock::now() - start_time; duration sleep_for_time = milliseconds(33) - decode_time; std::this_thread::sleep_for(sleep_for_time); From 8839867852cae28ee994f52c5914ab44a4895764 Mon Sep 17 00:00:00 2001 From: EdenGottlieb Date: Tue, 21 Dec 2021 13:50:09 +0200 Subject: [PATCH 04/12] Add framerate slider --- sherlock265/VideoDecoder.cc | 7 ++++++- sherlock265/VideoDecoder.hh | 2 ++ sherlock265/VideoPlayer.cc | 7 +++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/sherlock265/VideoDecoder.cc b/sherlock265/VideoDecoder.cc index 076230fc2..15d706324 100644 --- a/sherlock265/VideoDecoder.cc +++ b/sherlock265/VideoDecoder.cc @@ -51,6 +51,7 @@ VideoDecoder::VideoDecoder() img(NULL), mNextBuffer(0), mFrameCount(0), + mFramerate(30), mPlayingVideo(false), mVideoEnded(false), mSingleStep(false), @@ -145,7 +146,7 @@ void VideoDecoder::decoder_loop() // try again to get picture duration decode_time = system_clock::now() - start_time; - duration sleep_for_time = milliseconds(33) - decode_time; + duration sleep_for_time = milliseconds(1000 / mFramerate) - decode_time; std::this_thread::sleep_for(sleep_for_time); @@ -350,6 +351,10 @@ void VideoDecoder::show_frame(const de265_image* img) mFrameCount++; } +void VideoDecoder::setFramerate(int framerate) +{ + mFramerate = framerate; +} void VideoDecoder::showCBPartitioning(bool flag) { diff --git a/sherlock265/VideoDecoder.hh b/sherlock265/VideoDecoder.hh index 97b6c2079..f128f8835 100644 --- a/sherlock265/VideoDecoder.hh +++ b/sherlock265/VideoDecoder.hh @@ -64,6 +64,7 @@ public slots: void stopDecoder(); void singleStepDecoder(); + void setFramerate(int framerate); void showCBPartitioning(bool flag); void showTBPartitioning(bool flag); void showPBPartitioning(bool flag); @@ -92,6 +93,7 @@ private: QImage mImgBuffers[2]; int mNextBuffer; int mFrameCount; + int mFramerate; bool mPlayingVideo; bool mVideoEnded; diff --git a/sherlock265/VideoPlayer.cc b/sherlock265/VideoPlayer.cc index 73e92276b..f731a5746 100644 --- a/sherlock265/VideoPlayer.cc +++ b/sherlock265/VideoPlayer.cc @@ -48,7 +48,13 @@ VideoPlayer::VideoPlayer(const char* filename) QObject::connect(mDecoder, SIGNAL(displayImage(QImage*)), videoWidget, SLOT(setImage(QImage*)), Qt::QueuedConnection); + QSpinBox *framerateSpinbox = new QSpinBox(); + framerateSpinbox->setMinimum(1); + framerateSpinbox->setMaximum(60); + framerateSpinbox->setValue(30); + framerateSpinbox->setSuffix(" FPS"); + QObject::connect(framerateSpinbox, QOverload::of(&QSpinBox::valueChanged), mDecoder, &VideoDecoder::setFramerate); QPushButton* showCBPartitioningButton = new QPushButton("CB-tree"); showCBPartitioningButton->setCheckable(true); @@ -116,6 +122,7 @@ VideoPlayer::VideoPlayer(const char* filename) layout->addWidget(showPBPredModeButton, 2,4,1,1); layout->addWidget(showQuantPYButton, 2,5,1,1); layout->addWidget(showMotionVecButton, 2,6,1,1); + layout->addWidget(framerateSpinbox, 1, 3, 1, 1); setLayout(layout); From 2e44845e5a975927e489448e65b5904ad0899fd9 Mon Sep 17 00:00:00 2001 From: EdenGottlieb Date: Tue, 21 Dec 2021 13:59:05 +0200 Subject: [PATCH 05/12] Use sleep that doesn't block the UI thread --- sherlock265/VideoDecoder.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sherlock265/VideoDecoder.cc b/sherlock265/VideoDecoder.cc index 15d706324..c2792922d 100644 --- a/sherlock265/VideoDecoder.cc +++ b/sherlock265/VideoDecoder.cc @@ -148,7 +148,7 @@ void VideoDecoder::decoder_loop() duration decode_time = system_clock::now() - start_time; duration sleep_for_time = milliseconds(1000 / mFramerate) - decode_time; - std::this_thread::sleep_for(sleep_for_time); + QThread::msleep(sleep_for_time.count() * 1000); img = de265_peek_next_picture(ctx); } From 4371256a4a23e72ca6fa448f5bdda6284891e8c5 Mon Sep 17 00:00:00 2001 From: EdenGottlieb Date: Tue, 21 Dec 2021 14:19:11 +0200 Subject: [PATCH 06/12] Fix bug where decode_time > frame time This caused the thread to sleep for negative ms, effectively hanging it --- sherlock265/VideoDecoder.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sherlock265/VideoDecoder.cc b/sherlock265/VideoDecoder.cc index c2792922d..8fb16c1f3 100644 --- a/sherlock265/VideoDecoder.cc +++ b/sherlock265/VideoDecoder.cc @@ -148,7 +148,7 @@ void VideoDecoder::decoder_loop() duration decode_time = system_clock::now() - start_time; duration sleep_for_time = milliseconds(1000 / mFramerate) - decode_time; - QThread::msleep(sleep_for_time.count() * 1000); + QThread::msleep(std::max(sleep_for_time.count() * 1000, (double)0)); img = de265_peek_next_picture(ctx); } @@ -353,6 +353,7 @@ void VideoDecoder::show_frame(const de265_image* img) void VideoDecoder::setFramerate(int framerate) { + std::cout << "settings framerate " << framerate << std::endl; mFramerate = framerate; } From a3f84a8226119bdad6703d06466c170277be990d Mon Sep 17 00:00:00 2001 From: EdenGottlieb Date: Tue, 21 Dec 2021 14:25:00 +0200 Subject: [PATCH 07/12] Remove unused imports & log --- sherlock265/VideoDecoder.cc | 4 ---- 1 file changed, 4 deletions(-) diff --git a/sherlock265/VideoDecoder.cc b/sherlock265/VideoDecoder.cc index 8fb16c1f3..9ced5a057 100644 --- a/sherlock265/VideoDecoder.cc +++ b/sherlock265/VideoDecoder.cc @@ -37,10 +37,7 @@ using namespace videogfx; //#include "decctx.h" #include "visualize.h" -#include -#include #include -#include using namespace std; using namespace std::chrono; @@ -353,7 +350,6 @@ void VideoDecoder::show_frame(const de265_image* img) void VideoDecoder::setFramerate(int framerate) { - std::cout << "settings framerate " << framerate << std::endl; mFramerate = framerate; } From 9d1cc22d66b1f8e2fe526a35370dff5ecbc7ec9d Mon Sep 17 00:00:00 2001 From: EdenGottlieb Date: Tue, 21 Dec 2021 14:29:01 +0200 Subject: [PATCH 08/12] Increase maximum framerate to 300 This is capped because even on very fast machines, it wouldn't be able to decode faster than 300 fps --- sherlock265/VideoPlayer.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sherlock265/VideoPlayer.cc b/sherlock265/VideoPlayer.cc index f731a5746..a7f458a59 100644 --- a/sherlock265/VideoPlayer.cc +++ b/sherlock265/VideoPlayer.cc @@ -50,7 +50,7 @@ VideoPlayer::VideoPlayer(const char* filename) QSpinBox *framerateSpinbox = new QSpinBox(); framerateSpinbox->setMinimum(1); - framerateSpinbox->setMaximum(60); + framerateSpinbox->setMaximum(300); framerateSpinbox->setValue(30); framerateSpinbox->setSuffix(" FPS"); From 300d7377d1fd59501121e4afe72ff182452c7f76 Mon Sep 17 00:00:00 2001 From: EdenGottlieb Date: Tue, 21 Dec 2021 14:30:19 +0200 Subject: [PATCH 09/12] Don't sleep if not required This is a performance enhancment, guessing that sleeping for 0 ms might have some performance penantly --- sherlock265/VideoDecoder.cc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sherlock265/VideoDecoder.cc b/sherlock265/VideoDecoder.cc index 9ced5a057..3da991e4e 100644 --- a/sherlock265/VideoDecoder.cc +++ b/sherlock265/VideoDecoder.cc @@ -145,7 +145,10 @@ void VideoDecoder::decoder_loop() duration decode_time = system_clock::now() - start_time; duration sleep_for_time = milliseconds(1000 / mFramerate) - decode_time; - QThread::msleep(std::max(sleep_for_time.count() * 1000, (double)0)); + if (sleep_for_time.count() > 0) + { + QThread::msleep(sleep_for_time.count() * 1000); + } img = de265_peek_next_picture(ctx); } From 8f007b3285f31407e34c04b13bda9cda54d1d2b8 Mon Sep 17 00:00:00 2001 From: EdenGottlieb Date: Tue, 21 Dec 2021 14:31:37 +0200 Subject: [PATCH 10/12] code structure --- sherlock265/VideoPlayer.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sherlock265/VideoPlayer.cc b/sherlock265/VideoPlayer.cc index a7f458a59..84d461617 100644 --- a/sherlock265/VideoPlayer.cc +++ b/sherlock265/VideoPlayer.cc @@ -122,7 +122,7 @@ VideoPlayer::VideoPlayer(const char* filename) layout->addWidget(showPBPredModeButton, 2,4,1,1); layout->addWidget(showQuantPYButton, 2,5,1,1); layout->addWidget(showMotionVecButton, 2,6,1,1); - layout->addWidget(framerateSpinbox, 1, 3, 1, 1); + layout->addWidget(framerateSpinbox, 1,3,1,1); setLayout(layout); From df223156ff693e8355f9bccca44da7e372ebf183 Mon Sep 17 00:00:00 2001 From: EdenGottlieb Date: Tue, 21 Dec 2021 14:35:59 +0200 Subject: [PATCH 11/12] Use microseconds for sleep This improves decoding precision --- sherlock265/VideoDecoder.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sherlock265/VideoDecoder.cc b/sherlock265/VideoDecoder.cc index 3da991e4e..fb15cac4e 100644 --- a/sherlock265/VideoDecoder.cc +++ b/sherlock265/VideoDecoder.cc @@ -147,7 +147,7 @@ void VideoDecoder::decoder_loop() if (sleep_for_time.count() > 0) { - QThread::msleep(sleep_for_time.count() * 1000); + QThread::usleep(sleep_for_time.count() * 1000000); } img = de265_peek_next_picture(ctx); From c12604025febc95768d93381348f6ea65be67d9f Mon Sep 17 00:00:00 2001 From: EdenGottlieb Date: Tue, 21 Dec 2021 14:37:08 +0200 Subject: [PATCH 12/12] variable rename --- sherlock265/VideoDecoder.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sherlock265/VideoDecoder.cc b/sherlock265/VideoDecoder.cc index fb15cac4e..54e5f055f 100644 --- a/sherlock265/VideoDecoder.cc +++ b/sherlock265/VideoDecoder.cc @@ -132,7 +132,7 @@ void VideoDecoder::decoder_loop() img = de265_peek_next_picture(ctx); while (img==NULL) { - auto start_time = system_clock::now(); + auto decode_start_time = system_clock::now(); mutex.unlock(); int more=1; @@ -142,7 +142,7 @@ void VideoDecoder::decoder_loop() if (more && err == DE265_OK) { // try again to get picture - duration decode_time = system_clock::now() - start_time; + duration decode_time = system_clock::now() - decode_start_time; duration sleep_for_time = milliseconds(1000 / mFramerate) - decode_time; if (sleep_for_time.count() > 0)