+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);
+}
+
+struct LRUFrame {
+ shared_ptr<Frame> frame;
+ size_t last_used;
+};
+
+mutex cache_mu;
+map<JPEGID, LRUFrame> cache; // Under cache_mu.
+condition_variable any_pending_decodes;
+deque<pair<JPEGID, JPEGFrameView *>> pending_decodes; // Under cache_mu.
+atomic<size_t> event_counter{0};
+extern QGLWidget *global_share_widget;
+
+// TODO: Decode using VA-API if available.
+shared_ptr<Frame> decode_jpeg(const string &filename)
+{
+ shared_ptr<Frame> frame(new Frame);
+
+ jpeg_decompress_struct dinfo;
+ jpeg_error_mgr jerr;
+ dinfo.err = jpeg_std_error(&jerr);
+ jpeg_create_decompress(&dinfo);
+
+ FILE *fp = fopen(filename.c_str(), "rb");
+ if (fp == nullptr) {
+ perror(filename.c_str());
+ exit(1);
+ }
+ jpeg_stdio_src(&dinfo, fp);
+
+ jpeg_read_header(&dinfo, true);
+
+ 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);
+ exit(1);
+ }
+ if (dinfo.comp_info[0].h_samp_factor != dinfo.max_h_samp_factor ||
+ dinfo.comp_info[0].v_samp_factor != dinfo.max_v_samp_factor || // Y' must not be subsampled.
+ dinfo.comp_info[1].h_samp_factor != dinfo.comp_info[2].h_samp_factor ||
+ dinfo.comp_info[1].v_samp_factor != dinfo.comp_info[2].v_samp_factor || // Cb and Cr must be identically subsampled.
+ (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.raw_data_out = true;
+
+ jpeg_start_decompress(&dinfo);
+
+ frame->width = dinfo.output_width;
+ frame->height = dinfo.output_height;
+ frame->chroma_subsampling_x = dinfo.max_h_samp_factor / dinfo.comp_info[1].h_samp_factor;
+ frame->chroma_subsampling_y = dinfo.max_v_samp_factor / dinfo.comp_info[1].v_samp_factor;
+
+ unsigned h_mcu_size = DCTSIZE * dinfo.max_h_samp_factor;
+ unsigned v_mcu_size = DCTSIZE * dinfo.max_v_samp_factor;
+ unsigned mcu_width_blocks = (dinfo.output_width + h_mcu_size - 1) / h_mcu_size;
+ unsigned mcu_height_blocks = (dinfo.output_height + v_mcu_size - 1) / v_mcu_size;
+
+ unsigned luma_width_blocks = mcu_width_blocks * dinfo.comp_info[0].h_samp_factor;
+ unsigned chroma_width_blocks = mcu_width_blocks * dinfo.comp_info[1].h_samp_factor;
+ unsigned luma_height_blocks = mcu_height_blocks * dinfo.comp_info[0].v_samp_factor;
+ unsigned chroma_height_blocks = mcu_height_blocks * dinfo.comp_info[1].v_samp_factor;
+
+ // TODO: Decode into a PBO.
+ frame->y.reset(new uint8_t[luma_width_blocks * luma_height_blocks * DCTSIZE2]);
+ frame->cb.reset(new uint8_t[chroma_width_blocks * chroma_height_blocks * DCTSIZE2]);
+ frame->cr.reset(new uint8_t[chroma_width_blocks * chroma_height_blocks * DCTSIZE2]);
+ frame->pitch_y = luma_width_blocks * DCTSIZE;
+ frame->pitch_chroma = chroma_width_blocks * DCTSIZE;
+
+ 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);
+ jpeg_destroy_decompress(&dinfo);
+ fclose(fp);
+
+ return frame;
+}
+
+void prune_cache()
+{
+ // Assumes cache_mu is held.
+ vector<size_t> lru_timestamps;
+ for (const auto &key_and_value : cache) {
+ lru_timestamps.push_back(key_and_value.second.last_used);
+ }
+
+ size_t cutoff_point = CACHE_SIZE / 10; // Prune away the 10% oldest ones.
+ nth_element(lru_timestamps.begin(), lru_timestamps.begin() + cutoff_point, lru_timestamps.end());
+ size_t must_be_used_after = lru_timestamps[cutoff_point];
+ for (auto it = cache.begin(); it != cache.end(); ) {
+ if (it->second.last_used < must_be_used_after) {
+ it = cache.erase(it);
+ } else {
+ ++it;
+ }
+ }
+}
+
+void jpeg_decoder_thread()
+{
+ size_t num_decoded = 0, num_dropped = 0;
+
+ pthread_setname_np(pthread_self(), "JPEGDecoder");
+ for ( ;; ) {
+ JPEGID id;
+ JPEGFrameView *dest;
+ shared_ptr<Frame> frame;
+ {
+ unique_lock<mutex> lock(cache_mu);
+ any_pending_decodes.wait(lock, [] {
+ return !pending_decodes.empty();
+ });
+ id = pending_decodes.front().first;
+ 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) {
+ ++num_pending;
+ }
+ }
+ if (num_pending > 3) {
+ ++num_dropped;
+ continue;
+ }
+
+ 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();
+ }
+ ++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));
+ }
+ }
+
+ dest->setDecodedFrame(frame);
+ }
+}