1 #include "jpeg_frame_view.h"
7 #include <condition_variable>
13 #include <movit/resource_pool.h>
14 #include <movit/init.h>
15 #include <movit/util.h>
18 #include "post_to_main_thread.h"
20 using namespace movit;
23 string filename_for_frame(unsigned stream_idx, int64_t pts);
29 bool operator< (const JPEGID &a, const JPEGID &b) {
30 return make_pair(a.stream_idx, a.pts) < make_pair(b.stream_idx, b.pts);
34 shared_ptr<Frame> frame;
39 map<JPEGID, LRUFrame> cache; // Under cache_mu.
40 condition_variable any_pending_decodes;
41 deque<pair<JPEGID, JPEGFrameView *>> pending_decodes; // Under cache_mu.
42 atomic<size_t> event_counter{0};
43 extern QGLWidget *global_share_widget;
45 // TODO: Decode using VA-API if available.
46 shared_ptr<Frame> decode_jpeg(const string &filename)
48 shared_ptr<Frame> frame(new Frame);
50 jpeg_decompress_struct dinfo;
52 dinfo.err = jpeg_std_error(&jerr);
53 jpeg_create_decompress(&dinfo);
55 FILE *fp = fopen(filename.c_str(), "rb");
57 perror(filename.c_str());
60 jpeg_stdio_src(&dinfo, fp);
62 jpeg_read_header(&dinfo, true);
64 if (dinfo.num_components != 3) {
65 fprintf(stderr, "Not a color JPEG. (%d components, Y=%dx%d, Cb=%dx%d, Cr=%dx%d)\n",
67 dinfo.comp_info[0].h_samp_factor, dinfo.comp_info[0].v_samp_factor,
68 dinfo.comp_info[1].h_samp_factor, dinfo.comp_info[1].v_samp_factor,
69 dinfo.comp_info[2].h_samp_factor, dinfo.comp_info[2].v_samp_factor);
72 if (dinfo.comp_info[0].h_samp_factor != dinfo.max_h_samp_factor ||
73 dinfo.comp_info[0].v_samp_factor != dinfo.max_v_samp_factor || // Y' must not be subsampled.
74 dinfo.comp_info[1].h_samp_factor != dinfo.comp_info[2].h_samp_factor ||
75 dinfo.comp_info[1].v_samp_factor != dinfo.comp_info[2].v_samp_factor || // Cb and Cr must be identically subsampled.
76 (dinfo.max_h_samp_factor % dinfo.comp_info[1].h_samp_factor) != 0 ||
77 (dinfo.max_v_samp_factor % dinfo.comp_info[1].v_samp_factor) != 0) { // No 2:3 subsampling or other weirdness.
78 fprintf(stderr, "Unsupported subsampling scheme. (Y=%dx%d, Cb=%dx%d, Cr=%dx%d)\n",
79 dinfo.comp_info[0].h_samp_factor, dinfo.comp_info[0].v_samp_factor,
80 dinfo.comp_info[1].h_samp_factor, dinfo.comp_info[1].v_samp_factor,
81 dinfo.comp_info[2].h_samp_factor, dinfo.comp_info[2].v_samp_factor);
84 dinfo.raw_data_out = true;
86 jpeg_start_decompress(&dinfo);
88 frame->width = dinfo.output_width;
89 frame->height = dinfo.output_height;
90 frame->chroma_subsampling_x = dinfo.max_h_samp_factor / dinfo.comp_info[1].h_samp_factor;
91 frame->chroma_subsampling_y = dinfo.max_v_samp_factor / dinfo.comp_info[1].v_samp_factor;
93 unsigned h_mcu_size = DCTSIZE * dinfo.max_h_samp_factor;
94 unsigned v_mcu_size = DCTSIZE * dinfo.max_v_samp_factor;
95 unsigned chroma_width_blocks = (dinfo.output_width + h_mcu_size - 1) / h_mcu_size;
96 unsigned width_blocks = chroma_width_blocks * dinfo.max_h_samp_factor;
97 unsigned chroma_height_blocks = (dinfo.output_height + v_mcu_size - 1) / v_mcu_size;
98 unsigned height_blocks = chroma_height_blocks * dinfo.max_v_samp_factor;
100 // TODO: Decode into a PBO.
101 frame->y.reset(new uint8_t[width_blocks * height_blocks * DCTSIZE2]);
102 frame->cb.reset(new uint8_t[chroma_width_blocks * chroma_height_blocks * DCTSIZE2]);
103 frame->cr.reset(new uint8_t[chroma_width_blocks * chroma_height_blocks * DCTSIZE2]);
105 JSAMPROW yptr[v_mcu_size], cbptr[v_mcu_size], crptr[v_mcu_size];
106 JSAMPARRAY data[3] = { yptr, cbptr, crptr };
107 for (unsigned y = 0; y < chroma_height_blocks; ++y) {
108 // NOTE: The last elements of cbptr/crptr will be unused for vertically subsampled chroma.
109 for (unsigned yy = 0; yy < v_mcu_size; ++yy) {
110 yptr[yy] = frame->y.get() + (y * DCTSIZE * dinfo.max_v_samp_factor + yy) * width_blocks * DCTSIZE;
111 cbptr[yy] = frame->cb.get() + (y * DCTSIZE * dinfo.comp_info[1].v_samp_factor + yy) * chroma_width_blocks * DCTSIZE;
112 crptr[yy] = frame->cr.get() + (y * DCTSIZE * dinfo.comp_info[1].v_samp_factor + yy) * chroma_width_blocks * DCTSIZE;
115 jpeg_read_raw_data(&dinfo, data, v_mcu_size);
118 (void) jpeg_finish_decompress(&dinfo);
119 jpeg_destroy_decompress(&dinfo);
127 // Assumes cache_mu is held.
128 vector<size_t> lru_timestamps;
129 for (const auto &key_and_value : cache) {
130 lru_timestamps.push_back(key_and_value.second.last_used);
133 size_t cutoff_point = CACHE_SIZE / 10; // Prune away the 10% oldest ones.
134 nth_element(lru_timestamps.begin(), lru_timestamps.begin() + cutoff_point, lru_timestamps.end());
135 size_t must_be_used_after = lru_timestamps[cutoff_point];
136 for (auto it = cache.begin(); it != cache.end(); ) {
137 if (it->second.last_used < must_be_used_after) {
138 it = cache.erase(it);
145 void jpeg_decoder_thread()
147 size_t num_decoded = 0, num_dropped = 0;
149 pthread_setname_np(pthread_self(), "JPEGDecoder");
153 shared_ptr<Frame> frame;
155 unique_lock<mutex> lock(cache_mu);
156 any_pending_decodes.wait(lock, [] {
157 return !pending_decodes.empty();
159 id = pending_decodes.front().first;
160 dest = pending_decodes.front().second;
161 pending_decodes.pop_front();
163 auto it = cache.find(id);
164 if (it != cache.end()) {
165 frame = it->second.frame;
166 it->second.last_used = event_counter++;
170 if (frame == nullptr) {
171 // Not found in the cache, so we need to do a decode or drop the request.
172 // Prune the queue if there are too many pending for this destination.
173 // TODO: Could we get starvation here?
174 size_t num_pending = 0;
175 for (const pair<JPEGID, JPEGFrameView *> &decode : pending_decodes) {
176 if (decode.second == dest) {
180 if (num_pending > 3) {
185 frame = decode_jpeg(filename_for_frame(id.stream_idx, id.pts));
187 unique_lock<mutex> lock(cache_mu);
188 cache[id] = LRUFrame{ frame, event_counter++ };
190 if (cache.size() > CACHE_SIZE) {
194 if (num_decoded % 1000 == 0) {
195 fprintf(stderr, "Decoded %zu images, dropped %zu (%.2f%% dropped)\n",
196 num_decoded, num_dropped, (100.0 * num_dropped) / (num_decoded + num_dropped));
200 dest->setDecodedFrame(frame);
204 JPEGFrameView::JPEGFrameView(QWidget *parent)
205 : QGLWidget(parent, global_share_widget) {
208 void JPEGFrameView::update_frame()
210 unique_lock<mutex> lock(cache_mu);
211 pending_decodes.emplace_back(JPEGID{ stream_idx, pts }, this);
212 any_pending_decodes.notify_all();
215 ResourcePool *resource_pool = nullptr;
217 void JPEGFrameView::initializeGL()
220 glDisable(GL_DEPTH_TEST);
221 glDepthMask(GL_FALSE);
224 static once_flag once;
226 CHECK(init_movit(MOVIT_SHADER_DIR, MOVIT_DEBUG_OFF));
227 resource_pool = new ResourcePool;
229 std::thread(&jpeg_decoder_thread).detach();
232 chain.reset(new EffectChain(1280, 720, resource_pool));
233 ImageFormat image_format;
234 image_format.color_space = COLORSPACE_sRGB;
235 image_format.gamma_curve = GAMMA_sRGB;
236 ycbcr_format.luma_coefficients = YCBCR_REC_709;
237 ycbcr_format.full_range = false;
238 ycbcr_format.num_levels = 256;
239 ycbcr_format.chroma_subsampling_x = 2;
240 ycbcr_format.chroma_subsampling_y = 1;
241 ycbcr_format.cb_x_position = 0.0f; // H.264 -- _not_ JPEG, even though our input is MJPEG-encoded
242 ycbcr_format.cb_y_position = 0.5f; // Irrelevant.
243 ycbcr_format.cr_x_position = 0.0f;
244 ycbcr_format.cr_y_position = 0.5f;
245 ycbcr_input = (movit::YCbCrInput *)chain->add_input(new YCbCrInput(image_format, ycbcr_format, 1280, 720));
247 ImageFormat inout_format;
248 inout_format.color_space = COLORSPACE_sRGB;
249 inout_format.gamma_curve = GAMMA_sRGB;
252 chain->add_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED);
254 chain->set_dither_bits(8);
260 void JPEGFrameView::resizeGL(int width, int height)
263 glViewport(0, 0, width, height);
267 void JPEGFrameView::paintGL()
269 //glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
270 //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
273 chain->render_to_screen();
276 void JPEGFrameView::setDecodedFrame(std::shared_ptr<Frame> frame)
278 post_to_main_thread([this, frame] {
279 current_frame = frame;
280 int width_blocks = (frame->width + 15) / 16;
281 ycbcr_input->set_width(frame->width);
282 ycbcr_input->set_height(frame->height);
283 ycbcr_input->set_pixel_data(0, frame->y.get());
284 ycbcr_input->set_pixel_data(1, frame->cb.get());
285 ycbcr_input->set_pixel_data(2, frame->cr.get());
286 ycbcr_input->set_pitch(0, width_blocks * 16);
287 ycbcr_input->set_pitch(1, width_blocks * 8);
288 ycbcr_input->set_pitch(2, width_blocks * 8);
289 ycbcr_format.chroma_subsampling_x = frame->chroma_subsampling_x;
290 ycbcr_format.chroma_subsampling_y = frame->chroma_subsampling_y;
291 ycbcr_input->change_ycbcr_format(ycbcr_format);