using namespace movit;
using namespace std;
-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);
}
}
}
+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;
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();
});
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) {
}
}
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",
#include <memory>
+struct JPEGID {
+ unsigned stream_idx;
+ int64_t pts;
+};
struct Frame {
std::unique_ptr<uint8_t[]> y, cb, cr;
unsigned width, height;
unsigned chroma_subsampling_x, chroma_subsampling_y;
unsigned pitch_y, pitch_chroma;
};
+enum CacheMissBehavior {
+ DECODE_IF_NOT_IN_CACHE,
+ RETURN_NULLPTR_IF_NOT_IN_CACHE
+};
std::string filename_for_frame(unsigned stream_idx, int64_t pts);
std::shared_ptr<Frame> decode_jpeg(const std::string &filename);
+std::shared_ptr<Frame> decode_jpeg_with_cache(JPEGID id, CacheMissBehavior cache_miss_behavior, bool *did_decode);
class JPEGFrameView : public QGLWidget {
Q_OBJECT
// Convert frame0 and frame1 to OpenGL textures.
// TODO: Deduplicate against JPEGFrameView::setDecodedFrame?
for (size_t frame_no = 0; frame_no < 2; ++frame_no) {
- shared_ptr<Frame> frame = decode_jpeg(filename_for_frame(stream_idx, frame_no == 1 ? input_second_pts : input_first_pts));
+ JPEGID jpeg_id;
+ jpeg_id.stream_idx = stream_idx;
+ jpeg_id.pts = frame_no == 1 ? input_second_pts : input_first_pts;
+ bool did_decode;
+ shared_ptr<Frame> frame = decode_jpeg_with_cache(jpeg_id, DECODE_IF_NOT_IN_CACHE, &did_decode);
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);