]> git.sesse.net Git - nageru/blobdiff - jpeg_frame_view.cpp
Small refactoring in JPEGFrameView.
[nageru] / jpeg_frame_view.cpp
index 95f7453b7c6512caa85969e37bf41841eb8aabd7..88a88dc5edacd555b6cbc87a3b173eaf99b7b837 100644 (file)
 #include <thread>
 #include <utility>
 
+#include <QMouseEvent>
+
 #include <movit/resource_pool.h>
 #include <movit/init.h>
 #include <movit/util.h>
 
 #include "defs.h"
 #include "post_to_main_thread.h"
+#include "video_stream.h"
 
 using namespace movit;
 using namespace std;
 
-string filename_for_frame(unsigned stream_idx, int64_t pts);
-
-struct JPEGID {
-       unsigned stream_idx;
-       int64_t pts;
-};
 bool operator< (const JPEGID &a, const JPEGID &b) {
        return make_pair(a.stream_idx, a.pts) < make_pair(b.stream_idx, b.pts);
 }
@@ -147,6 +144,34 @@ void prune_cache()
        }
 }
 
+shared_ptr<Frame> decode_jpeg_with_cache(JPEGID id, CacheMissBehavior cache_miss_behavior, bool *did_decode)
+{
+       *did_decode = false;
+       {
+               unique_lock<mutex> lock(cache_mu);
+               auto it = cache.find(id);
+               if (it != cache.end()) {
+                       it->second.last_used = event_counter++;
+                       return it->second.frame;
+               }
+       }
+
+       if (cache_miss_behavior == RETURN_NULLPTR_IF_NOT_IN_CACHE) {
+               return nullptr;
+       }
+
+       *did_decode = true;
+       shared_ptr<Frame> frame = decode_jpeg(filename_for_frame(id.stream_idx, id.pts));
+
+       unique_lock<mutex> lock(cache_mu);
+       cache[id] = LRUFrame{ frame, event_counter++ };
+
+       if (cache.size() > CACHE_SIZE) {
+               prune_cache();
+       }
+       return frame;
+}
+
 void jpeg_decoder_thread()
 {
        size_t num_decoded = 0, num_dropped = 0;
@@ -155,9 +180,9 @@ void jpeg_decoder_thread()
        for ( ;; ) {
                JPEGID id;
                JPEGFrameView *dest;
-               shared_ptr<Frame> frame;
+               CacheMissBehavior cache_miss_behavior = DECODE_IF_NOT_IN_CACHE;
                {
-                       unique_lock<mutex> lock(cache_mu);
+                       unique_lock<mutex> lock(cache_mu);  // TODO: Perhaps under another lock?
                        any_pending_decodes.wait(lock, [] {
                                return !pending_decodes.empty();
                        });
@@ -165,17 +190,6 @@ void jpeg_decoder_thread()
                        dest = pending_decodes.front().second;
                        pending_decodes.pop_front();
 
-                       auto it = cache.find(id);
-                       if (it != cache.end()) {
-                               frame = it->second.frame;
-                               it->second.last_used = event_counter++;
-                       }
-               }
-
-               if (frame == nullptr) {
-                       // Not found in the cache, so we need to do a decode or drop the request.
-                       // Prune the queue if there are too many pending for this destination.
-                       // TODO: Could we get starvation here?
                        size_t num_pending = 0;
                        for (const pair<JPEGID, JPEGFrameView *> &decode : pending_decodes) {
                                if (decode.second == dest) {
@@ -183,18 +197,20 @@ void jpeg_decoder_thread()
                                }
                        }
                        if (num_pending > 3) {
-                               ++num_dropped;
-                               continue;
+                               cache_miss_behavior = RETURN_NULLPTR_IF_NOT_IN_CACHE;
                        }
+               }
 
-                       frame = decode_jpeg(filename_for_frame(id.stream_idx, id.pts));
+               bool found_in_cache;
+               shared_ptr<Frame> frame = decode_jpeg_with_cache(id, cache_miss_behavior, &found_in_cache);
 
-                       unique_lock<mutex> lock(cache_mu);
-                       cache[id] = LRUFrame{ frame, event_counter++ };
+               if (frame == nullptr) {
+                       assert(cache_miss_behavior == RETURN_NULLPTR_IF_NOT_IN_CACHE);
+                       ++num_dropped;
+                       continue;
+               }
 
-                       if (cache.size() > CACHE_SIZE) {
-                               prune_cache();
-                       }
+               if (!found_in_cache) {
                        ++num_decoded;
                        if (num_decoded % 1000 == 0) {
                                fprintf(stderr, "Decoded %zu images, dropped %zu (%.2f%% dropped)\n",
@@ -210,8 +226,10 @@ JPEGFrameView::JPEGFrameView(QWidget *parent)
        : QGLWidget(parent, global_share_widget) {
 }
 
-void JPEGFrameView::update_frame()
+void JPEGFrameView::setFrame(unsigned stream_idx, int64_t pts)
 {
+       current_stream_idx = stream_idx;
+
        unique_lock<mutex> lock(cache_mu);
        pending_decodes.emplace_back(JPEGID{ stream_idx, pts }, this);
        any_pending_decodes.notify_all();
@@ -223,14 +241,11 @@ void JPEGFrameView::initializeGL()
 {
        glDisable(GL_BLEND);
        glDisable(GL_DEPTH_TEST);
-       glDepthMask(GL_FALSE);
        check_error();
 
        static once_flag once;
        call_once(once, [] {
-               CHECK(init_movit(MOVIT_SHADER_DIR, MOVIT_DEBUG_OFF));
                resource_pool = new ResourcePool;
-
                std::thread(&jpeg_decoder_thread).detach();
        });
 
@@ -271,8 +286,11 @@ void JPEGFrameView::resizeGL(int width, int height)
 
 void JPEGFrameView::paintGL()
 {
-       //glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
-       //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+       if (current_frame == nullptr) {
+               glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
+               glClear(GL_COLOR_BUFFER_BIT);
+               return;
+       }
 
        check_error();
        chain->render_to_screen();
@@ -282,6 +300,9 @@ void JPEGFrameView::setDecodedFrame(std::shared_ptr<Frame> frame)
 {
        post_to_main_thread([this, frame] {
                current_frame = frame;
+               ycbcr_format.chroma_subsampling_x = frame->chroma_subsampling_x;
+               ycbcr_format.chroma_subsampling_y = frame->chroma_subsampling_y;
+               ycbcr_input->change_ycbcr_format(ycbcr_format);
                ycbcr_input->set_width(frame->width);
                ycbcr_input->set_height(frame->height);
                ycbcr_input->set_pixel_data(0, frame->y.get());
@@ -290,9 +311,13 @@ void JPEGFrameView::setDecodedFrame(std::shared_ptr<Frame> frame)
                ycbcr_input->set_pitch(0, frame->pitch_y);
                ycbcr_input->set_pitch(1, frame->pitch_chroma);
                ycbcr_input->set_pitch(2, frame->pitch_chroma);
-               ycbcr_format.chroma_subsampling_x = frame->chroma_subsampling_x;
-               ycbcr_format.chroma_subsampling_y = frame->chroma_subsampling_y;
-               ycbcr_input->change_ycbcr_format(ycbcr_format);
                update();
        });
 }
+
+void JPEGFrameView::mousePressEvent(QMouseEvent *event)
+{
+       if (event->type() == QEvent::MouseButtonPress && event->button() == Qt::LeftButton) {
+               emit clicked();
+       }
+}