]> git.sesse.net Git - nageru/blobdiff - futatabi/jpeg_frame_view.cpp
Stop refreshing the Futatabi channel overlay every frame.
[nageru] / futatabi / jpeg_frame_view.cpp
index 059a3410586285ff230d6ebcc469846f428a4dc3..adc376455ec4eb06c7f8e5550b38037fa1aebeb8 100644 (file)
@@ -4,6 +4,7 @@
 #include "flags.h"
 #include "jpeg_destroyer.h"
 #include "jpeglib_error_wrapper.h"
+#include "shared/metrics.h"
 #include "shared/post_to_main_thread.h"
 #include "video_stream.h"
 #include "ycbcr_converter.h"
@@ -32,9 +33,8 @@ using namespace std;
 namespace {
 
 // Just an arbitrary order for std::map.
-struct FrameOnDiskLexicalOrder
-{
-       bool operator() (const FrameOnDisk &a, const FrameOnDisk &b) const
+struct FrameOnDiskLexicalOrder {
+       bool operator()(const FrameOnDisk &a, const FrameOnDisk &b) const
        {
                if (a.pts != b.pts)
                        return a.pts < b.pts;
@@ -59,28 +59,24 @@ struct LRUFrame {
        size_t last_used;
 };
 
-struct PendingDecode {
-       JPEGFrameView *destination;
-
-       // For actual decodes (only if frame below is nullptr).
-       FrameOnDisk primary, secondary;
-       float fade_alpha;  // Irrelevant if secondary.stream_idx == -1.
-
-       // Already-decoded frames are also sent through PendingDecode,
-       // so that they get drawn in the right order. If frame is nullptr,
-       // it's a real decode.
-       shared_ptr<Frame> frame;
-};
+// There can be multiple JPEGFrameView instances, so make all the metrics static.
+once_flag jpeg_metrics_inited;
+atomic<int64_t> metric_jpeg_cache_used_bytes{ 0 };  // Same value as cache_bytes_used.
+atomic<int64_t> metric_jpeg_cache_limit_bytes{ size_t(CACHE_SIZE_MB) * 1024 * 1024 };
+atomic<int64_t> metric_jpeg_cache_given_up_frames{ 0 };
+atomic<int64_t> metric_jpeg_cache_hit_frames{ 0 };
+atomic<int64_t> metric_jpeg_cache_miss_frames{ 0 };
+atomic<int64_t> metric_jpeg_software_decode_frames{ 0 };
+atomic<int64_t> metric_jpeg_software_fail_frames{ 0 };
+atomic<int64_t> metric_jpeg_vaapi_decode_frames{ 0 };
+atomic<int64_t> metric_jpeg_vaapi_fail_frames{ 0 };
 
 }  // namespace
 
-thread JPEGFrameView::jpeg_decoder_thread;
 mutex cache_mu;
 map<FrameOnDisk, LRUFrame, FrameOnDiskLexicalOrder> cache;  // Under cache_mu.
 size_t cache_bytes_used = 0;  // Under cache_mu.
-condition_variable any_pending_decodes;
-deque<PendingDecode> pending_decodes;  // Under cache_mu.
-atomic<size_t> event_counter{0};
+atomic<size_t> event_counter{ 0 };
 extern QGLWidget *global_share_widget;
 extern atomic<bool> should_quit;
 
@@ -90,33 +86,37 @@ shared_ptr<Frame> decode_jpeg(const string &jpeg)
        if (vaapi_jpeg_decoding_usable) {
                frame = decode_jpeg_vaapi(jpeg);
                if (frame != nullptr) {
+                       ++metric_jpeg_vaapi_decode_frames;
                        return frame;
                }
                fprintf(stderr, "VA-API hardware decoding failed; falling back to software.\n");
+               ++metric_jpeg_vaapi_fail_frames;
        }
 
        frame.reset(new Frame);
 
        jpeg_decompress_struct dinfo;
        JPEGWrapErrorManager error_mgr(&dinfo);
-       if (!error_mgr.run([&dinfo]{ jpeg_create_decompress(&dinfo); })) {
+       if (!error_mgr.run([&dinfo] { jpeg_create_decompress(&dinfo); })) {
                return get_black_frame();
        }
        JPEGDestroyer destroy_dinfo(&dinfo);
 
-       if (!error_mgr.run([&dinfo, &jpeg]{
-               jpeg_mem_src(&dinfo, reinterpret_cast<const unsigned char *>(jpeg.data()), jpeg.size());
-               jpeg_read_header(&dinfo, true);
-       })) {
+       if (!error_mgr.run([&dinfo, &jpeg] {
+                   jpeg_mem_src(&dinfo, reinterpret_cast<const unsigned char *>(jpeg.data()), jpeg.size());
+                   jpeg_read_header(&dinfo, true);
+           })) {
                return get_black_frame();
        }
 
+       jpeg_save_markers(&dinfo, JPEG_APP0 + 1, 0xFFFF);
+
        if (dinfo.num_components != 3) {
                fprintf(stderr, "Not a color JPEG. (%d components, Y=%dx%d, Cb=%dx%d, Cr=%dx%d)\n",
-                       dinfo.num_components,
-                       dinfo.comp_info[0].h_samp_factor, dinfo.comp_info[0].v_samp_factor,
-                       dinfo.comp_info[1].h_samp_factor, dinfo.comp_info[1].v_samp_factor,
-                       dinfo.comp_info[2].h_samp_factor, dinfo.comp_info[2].v_samp_factor);
+                       dinfo.num_components,
+                       dinfo.comp_info[0].h_samp_factor, dinfo.comp_info[0].v_samp_factor,
+                       dinfo.comp_info[1].h_samp_factor, dinfo.comp_info[1].v_samp_factor,
+                       dinfo.comp_info[2].h_samp_factor, dinfo.comp_info[2].v_samp_factor);
                return get_black_frame();
        }
        if (dinfo.comp_info[0].h_samp_factor != dinfo.max_h_samp_factor ||
@@ -126,16 +126,16 @@ shared_ptr<Frame> decode_jpeg(const string &jpeg)
            (dinfo.max_h_samp_factor % dinfo.comp_info[1].h_samp_factor) != 0 ||
            (dinfo.max_v_samp_factor % dinfo.comp_info[1].v_samp_factor) != 0) {  // No 2:3 subsampling or other weirdness.
                fprintf(stderr, "Unsupported subsampling scheme. (Y=%dx%d, Cb=%dx%d, Cr=%dx%d)\n",
-                       dinfo.comp_info[0].h_samp_factor, dinfo.comp_info[0].v_samp_factor,
-                       dinfo.comp_info[1].h_samp_factor, dinfo.comp_info[1].v_samp_factor,
-                       dinfo.comp_info[2].h_samp_factor, dinfo.comp_info[2].v_samp_factor);
-               exit(1);
+                       dinfo.comp_info[0].h_samp_factor, dinfo.comp_info[0].v_samp_factor,
+                       dinfo.comp_info[1].h_samp_factor, dinfo.comp_info[1].v_samp_factor,
+                       dinfo.comp_info[2].h_samp_factor, dinfo.comp_info[2].v_samp_factor);
+               abort();
        }
        dinfo.raw_data_out = true;
 
-       if (!error_mgr.run([&dinfo]{
-               jpeg_start_decompress(&dinfo);
-       })) {
+       if (!error_mgr.run([&dinfo] {
+                   jpeg_start_decompress(&dinfo);
+           })) {
                return get_black_frame();
        }
 
@@ -161,25 +161,34 @@ shared_ptr<Frame> decode_jpeg(const string &jpeg)
        frame->pitch_y = luma_width_blocks * DCTSIZE;
        frame->pitch_chroma = chroma_width_blocks * DCTSIZE;
 
-       if (!error_mgr.run([&dinfo, &frame, v_mcu_size, mcu_height_blocks] {
-               JSAMPROW yptr[v_mcu_size], cbptr[v_mcu_size], crptr[v_mcu_size];
-               JSAMPARRAY data[3] = { yptr, cbptr, crptr };
-               for (unsigned y = 0; y < mcu_height_blocks; ++y) {
-                       // NOTE: The last elements of cbptr/crptr will be unused for vertically subsampled chroma.
-                       for (unsigned yy = 0; yy < v_mcu_size; ++yy) {
-                               yptr[yy] = frame->y.get() + (y * DCTSIZE * dinfo.max_v_samp_factor + yy) * frame->pitch_y;
-                               cbptr[yy] = frame->cb.get() + (y * DCTSIZE * dinfo.comp_info[1].v_samp_factor + yy) * frame->pitch_chroma;
-                               crptr[yy] = frame->cr.get() + (y * DCTSIZE * dinfo.comp_info[1].v_samp_factor + yy) * frame->pitch_chroma;
-                       }
-
-                       jpeg_read_raw_data(&dinfo, data, v_mcu_size);
-               }
+       if (dinfo.marker_list != nullptr &&
+           dinfo.marker_list->marker == JPEG_APP0 + 1 &&
+           dinfo.marker_list->data_length >= 4 &&
+           memcmp(dinfo.marker_list->data, "Exif", 4) == 0) {
+               frame->exif_data.assign(reinterpret_cast<char *>(dinfo.marker_list->data),
+                       dinfo.marker_list->data_length);
+       }
 
-               (void)jpeg_finish_decompress(&dinfo);
-       })) {
+       if (!error_mgr.run([&dinfo, &frame, v_mcu_size, mcu_height_blocks] {
+                   JSAMPROW yptr[v_mcu_size], cbptr[v_mcu_size], crptr[v_mcu_size];
+                   JSAMPARRAY data[3] = { yptr, cbptr, crptr };
+                   for (unsigned y = 0; y < mcu_height_blocks; ++y) {
+                           // NOTE: The last elements of cbptr/crptr will be unused for vertically subsampled chroma.
+                           for (unsigned yy = 0; yy < v_mcu_size; ++yy) {
+                                   yptr[yy] = frame->y.get() + (y * DCTSIZE * dinfo.max_v_samp_factor + yy) * frame->pitch_y;
+                                   cbptr[yy] = frame->cb.get() + (y * DCTSIZE * dinfo.comp_info[1].v_samp_factor + yy) * frame->pitch_chroma;
+                                   crptr[yy] = frame->cr.get() + (y * DCTSIZE * dinfo.comp_info[1].v_samp_factor + yy) * frame->pitch_chroma;
+                           }
+
+                           jpeg_read_raw_data(&dinfo, data, v_mcu_size);
+                   }
+
+                   (void)jpeg_finish_decompress(&dinfo);
+           })) {
                return get_black_frame();
        }
 
+       ++metric_jpeg_software_decode_frames;
        return frame;
 }
 
@@ -187,7 +196,8 @@ void prune_cache()
 {
        // Assumes cache_mu is held.
        int64_t bytes_still_to_remove = cache_bytes_used - (size_t(CACHE_SIZE_MB) * 1024 * 1024) * 9 / 10;
-       if (bytes_still_to_remove <= 0) return;
+       if (bytes_still_to_remove <= 0)
+               return;
 
        vector<pair<size_t, size_t>> lru_timestamps_and_size;
        for (const auto &key_and_value : cache) {
@@ -202,12 +212,14 @@ void prune_cache()
        for (const pair<size_t, size_t> &it : lru_timestamps_and_size) {
                lru_cutoff_point = it.first;
                bytes_still_to_remove -= it.second;
-               if (bytes_still_to_remove <= 0) break;
+               if (bytes_still_to_remove <= 0)
+                       break;
        }
 
-       for (auto it = cache.begin(); it != cache.end(); ) {
+       for (auto it = cache.begin(); it != cache.end();) {
                if (it->second.last_used <= lru_cutoff_point) {
                        cache_bytes_used -= frame_size(*it->second.frame);
+                       metric_jpeg_cache_used_bytes = cache_bytes_used;
                        it = cache.erase(it);
                } else {
                        ++it;
@@ -219,23 +231,28 @@ shared_ptr<Frame> decode_jpeg_with_cache(FrameOnDisk frame_spec, CacheMissBehavi
 {
        *did_decode = false;
        {
-               unique_lock<mutex> lock(cache_mu);
+               lock_guard<mutex> lock(cache_mu);
                auto it = cache.find(frame_spec);
                if (it != cache.end()) {
+                       ++metric_jpeg_cache_hit_frames;
                        it->second.last_used = event_counter++;
                        return it->second.frame;
                }
        }
 
        if (cache_miss_behavior == RETURN_NULLPTR_IF_NOT_IN_CACHE) {
+               ++metric_jpeg_cache_given_up_frames;
                return nullptr;
        }
 
+       ++metric_jpeg_cache_miss_frames;
+
        *did_decode = true;
-       shared_ptr<Frame> frame = decode_jpeg(frame_reader->read_frame(frame_spec));
+       shared_ptr<Frame> frame = decode_jpeg(frame_reader->read_frame(frame_spec, /*read_video=*/true, /*read_audio=*/false).video);
 
-       unique_lock<mutex> lock(cache_mu);
+       lock_guard<mutex> lock(cache_mu);
        cache_bytes_used += frame_size(*frame);
+       metric_jpeg_cache_used_bytes = cache_bytes_used;
        cache[frame_spec] = LRUFrame{ frame, event_counter++ };
 
        if (cache_bytes_used > size_t(CACHE_SIZE_MB) * 1024 * 1024) {
@@ -254,7 +271,7 @@ void JPEGFrameView::jpeg_decoder_thread_func()
                CacheMissBehavior cache_miss_behavior = DECODE_IF_NOT_IN_CACHE;
                {
                        unique_lock<mutex> lock(cache_mu);  // TODO: Perhaps under another lock?
-                       any_pending_decodes.wait(lock, [] {
+                       any_pending_decodes.wait(lock, [this] {
                                return !pending_decodes.empty() || should_quit.load();
                        });
                        if (should_quit.load())
@@ -262,20 +279,14 @@ void JPEGFrameView::jpeg_decoder_thread_func()
                        decode = pending_decodes.front();
                        pending_decodes.pop_front();
 
-                       size_t num_pending = 0;
-                       for (const PendingDecode &other_decode : pending_decodes) {
-                               if (other_decode.destination == decode.destination) {
-                                       ++num_pending;
-                               }
-                       }
-                       if (num_pending > 3) {
+                       if (pending_decodes.size() > 3) {
                                cache_miss_behavior = RETURN_NULLPTR_IF_NOT_IN_CACHE;
                        }
                }
 
                if (decode.frame != nullptr) {
                        // Already decoded, so just show it.
-                       decode.destination->setDecodedFrame(decode.frame, nullptr, 1.0f);
+                       setDecodedFrame(decode.frame, nullptr, 1.0f);
                        continue;
                }
 
@@ -289,7 +300,7 @@ void JPEGFrameView::jpeg_decoder_thread_func()
                        }
 
                        bool found_in_cache;
-                       shared_ptr<Frame> frame = decode_jpeg_with_cache(frame_spec, cache_miss_behavior, &decode.destination->frame_reader, &found_in_cache);
+                       shared_ptr<Frame> frame = decode_jpeg_with_cache(frame_spec, cache_miss_behavior, &frame_reader, &found_in_cache);
 
                        if (frame == nullptr) {
                                assert(cache_miss_behavior == RETURN_NULLPTR_IF_NOT_IN_CACHE);
@@ -301,7 +312,7 @@ void JPEGFrameView::jpeg_decoder_thread_func()
                                ++num_decoded;
                                if (num_decoded % 1000 == 0) {
                                        fprintf(stderr, "Decoded %zu images, dropped %zu (%.2f%% dropped)\n",
-                                               num_decoded, num_dropped, (100.0 * num_dropped) / (num_decoded + num_dropped));
+                                               num_decoded, num_dropped, (100.0 * num_dropped) / (num_decoded + num_dropped));
                                }
                        }
                        if (subframe_idx == 0) {
@@ -316,11 +327,11 @@ void JPEGFrameView::jpeg_decoder_thread_func()
                }
 
                // TODO: Could we get jitter between non-interpolated and interpolated frames here?
-               decode.destination->setDecodedFrame(primary_frame, secondary_frame, decode.fade_alpha);
+               setDecodedFrame(primary_frame, secondary_frame, decode.fade_alpha);
        }
 }
 
-void JPEGFrameView::shutdown()
+JPEGFrameView::~JPEGFrameView()
 {
        any_pending_decodes.notify_all();
        jpeg_decoder_thread.join();
@@ -329,45 +340,49 @@ void JPEGFrameView::shutdown()
 JPEGFrameView::JPEGFrameView(QWidget *parent)
        : QGLWidget(parent, global_share_widget)
 {
+       call_once(jpeg_metrics_inited, [] {
+               global_metrics.add("jpeg_cache_used_bytes", &metric_jpeg_cache_used_bytes, Metrics::TYPE_GAUGE);
+               global_metrics.add("jpeg_cache_limit_bytes", &metric_jpeg_cache_limit_bytes, Metrics::TYPE_GAUGE);
+               global_metrics.add("jpeg_cache_frames", { { "action", "given_up" } }, &metric_jpeg_cache_given_up_frames);
+               global_metrics.add("jpeg_cache_frames", { { "action", "hit" } }, &metric_jpeg_cache_hit_frames);
+               global_metrics.add("jpeg_cache_frames", { { "action", "miss" } }, &metric_jpeg_cache_miss_frames);
+               global_metrics.add("jpeg_decode_frames", { { "decoder", "software" }, { "result", "decode" } }, &metric_jpeg_software_decode_frames);
+               global_metrics.add("jpeg_decode_frames", { { "decoder", "software" }, { "result", "fail" } }, &metric_jpeg_software_fail_frames);
+               global_metrics.add("jpeg_decode_frames", { { "decoder", "vaapi" }, { "result", "decode" } }, &metric_jpeg_vaapi_decode_frames);
+               global_metrics.add("jpeg_decode_frames", { { "decoder", "vaapi" }, { "result", "fail" } }, &metric_jpeg_vaapi_fail_frames);
+       });
 }
 
 void JPEGFrameView::setFrame(unsigned stream_idx, FrameOnDisk frame, FrameOnDisk secondary_frame, float fade_alpha)
 {
        current_stream_idx = stream_idx;  // TODO: Does this interact with fades?
 
-       unique_lock<mutex> lock(cache_mu);
+       lock_guard<mutex> lock(cache_mu);
        PendingDecode decode;
        decode.primary = frame;
        decode.secondary = secondary_frame;
        decode.fade_alpha = fade_alpha;
-       decode.destination = this;
        pending_decodes.push_back(decode);
        any_pending_decodes.notify_all();
 }
 
 void JPEGFrameView::setFrame(shared_ptr<Frame> frame)
 {
-       unique_lock<mutex> lock(cache_mu);
+       lock_guard<mutex> lock(cache_mu);
        PendingDecode decode;
        decode.frame = std::move(frame);
-       decode.destination = this;
        pending_decodes.push_back(decode);
        any_pending_decodes.notify_all();
 }
 
-ResourcePool *resource_pool = nullptr;
-
 void JPEGFrameView::initializeGL()
 {
        glDisable(GL_BLEND);
        glDisable(GL_DEPTH_TEST);
        check_error();
 
-       static once_flag once;
-       call_once(once, [] {
-               resource_pool = new ResourcePool;
-               jpeg_decoder_thread = std::thread(jpeg_decoder_thread_func);
-       });
+       resource_pool = new ResourcePool;
+       jpeg_decoder_thread = std::thread(&JPEGFrameView::jpeg_decoder_thread_func, this);
 
        ycbcr_converter.reset(new YCbCrConverter(YCbCrConverter::OUTPUT_TO_RGBA, resource_pool));
 
@@ -410,6 +425,7 @@ void JPEGFrameView::paintGL()
                        overlay_input->set_width(overlay_width);
                        overlay_input->set_height(overlay_height);
                        overlay_input->set_pixel_data(overlay_image->bits());
+                       overlay_input_needs_refresh = false;
                }
                glViewport(gl_width - overlay_width, 0, overlay_width, overlay_height);
                overlay_chain->render_to_screen();
@@ -485,5 +501,6 @@ shared_ptr<Frame> get_black_frame()
                black_frame->pitch_y = global_flags.width;
                black_frame->pitch_chroma = global_flags.width / 2;
        });
+       ++metric_jpeg_software_fail_frames;
        return black_frame;
 }