]> git.sesse.net Git - nageru/blob - futatabi/jpeg_frame_view.cpp
Add metrics for how many frames we are decoding, but did not have the time to display.
[nageru] / futatabi / jpeg_frame_view.cpp
1 #include "jpeg_frame_view.h"
2
3 #include "defs.h"
4 #include "flags.h"
5 #include "jpeg_destroyer.h"
6 #include "jpeglib_error_wrapper.h"
7 #include "shared/metrics.h"
8 #include "shared/post_to_main_thread.h"
9 #include "video_stream.h"
10 #include "ycbcr_converter.h"
11
12 #include <QMouseEvent>
13 #include <QScreen>
14 #include <atomic>
15 #include <chrono>
16 #include <condition_variable>
17 #include <deque>
18 #include <jpeglib.h>
19 #include <movit/init.h>
20 #include <movit/resource_pool.h>
21 #include <movit/util.h>
22 #include <mutex>
23 #include <stdint.h>
24 #include <thread>
25 #include <unistd.h>
26 #include <utility>
27
28 // Must come after the Qt stuff.
29 #include "vaapi_jpeg_decoder.h"
30
31 using namespace movit;
32 using namespace std;
33 using namespace std::chrono;
34
35 namespace {
36
37 // Just an arbitrary order for std::map.
38 struct FrameOnDiskLexicalOrder {
39         bool operator()(const FrameOnDisk &a, const FrameOnDisk &b) const
40         {
41                 if (a.pts != b.pts)
42                         return a.pts < b.pts;
43                 if (a.offset != b.offset)
44                         return a.offset < b.offset;
45                 if (a.filename_idx != b.filename_idx)
46                         return a.filename_idx < b.filename_idx;
47                 assert(a.size == b.size);
48                 return false;
49         }
50 };
51
52 inline size_t frame_size(const Frame &frame)
53 {
54         size_t y_size = frame.width * frame.height;
55         size_t cbcr_size = y_size / frame.chroma_subsampling_x / frame.chroma_subsampling_y;
56         return y_size + cbcr_size * 2;
57 }
58
59 struct LRUFrame {
60         shared_ptr<Frame> frame;
61         size_t last_used;
62 };
63
64 // There can be multiple JPEGFrameView instances, so make all the metrics static.
65 once_flag jpeg_metrics_inited;
66 atomic<int64_t> metric_jpeg_cache_used_bytes{ 0 };  // Same value as cache_bytes_used.
67 atomic<int64_t> metric_jpeg_cache_limit_bytes{ size_t(CACHE_SIZE_MB) * 1024 * 1024 };
68 atomic<int64_t> metric_jpeg_cache_given_up_frames{ 0 };
69 atomic<int64_t> metric_jpeg_cache_hit_frames{ 0 };
70 atomic<int64_t> metric_jpeg_cache_miss_frames{ 0 };
71 atomic<int64_t> metric_jpeg_software_decode_frames{ 0 };
72 atomic<int64_t> metric_jpeg_software_fail_frames{ 0 };
73 atomic<int64_t> metric_jpeg_vaapi_decode_frames{ 0 };
74 atomic<int64_t> metric_jpeg_vaapi_fail_frames{ 0 };
75 atomic<int64_t> metric_jpeg_prepared_frames{ 0 };
76 atomic<int64_t> metric_jpeg_displayed_frames{ 0 };
77 Summary metric_jpeg_decode_time_seconds;
78
79 }  // namespace
80
81 mutex cache_mu;
82 map<FrameOnDisk, LRUFrame, FrameOnDiskLexicalOrder> cache;  // Under cache_mu.
83 size_t cache_bytes_used = 0;  // Under cache_mu.
84 atomic<size_t> event_counter{ 0 };
85 extern QGLWidget *global_share_widget;
86 extern atomic<bool> should_quit;
87
88 shared_ptr<Frame> decode_jpeg(const string &jpeg)
89 {
90         steady_clock::time_point start = steady_clock::now();
91         shared_ptr<Frame> frame;
92         if (vaapi_jpeg_decoding_usable) {
93                 frame = decode_jpeg_vaapi(jpeg);
94                 if (frame != nullptr) {
95                         ++metric_jpeg_vaapi_decode_frames;
96                         steady_clock::time_point stop = steady_clock::now();
97                         metric_jpeg_decode_time_seconds.count_event(duration<double>(stop - start).count());
98                         return frame;
99                 }
100                 fprintf(stderr, "VA-API hardware decoding failed; falling back to software.\n");
101                 ++metric_jpeg_vaapi_fail_frames;
102         }
103
104         frame.reset(new Frame);
105
106         jpeg_decompress_struct dinfo;
107         JPEGWrapErrorManager error_mgr(&dinfo);
108         if (!error_mgr.run([&dinfo] { jpeg_create_decompress(&dinfo); })) {
109                 return get_black_frame();
110         }
111         JPEGDestroyer destroy_dinfo(&dinfo);
112
113         if (!error_mgr.run([&dinfo, &jpeg] {
114                     jpeg_mem_src(&dinfo, reinterpret_cast<const unsigned char *>(jpeg.data()), jpeg.size());
115                     jpeg_read_header(&dinfo, true);
116             })) {
117                 return get_black_frame();
118         }
119
120         jpeg_save_markers(&dinfo, JPEG_APP0 + 1, 0xFFFF);
121
122         if (dinfo.num_components != 3) {
123                 fprintf(stderr, "Not a color JPEG. (%d components, Y=%dx%d, Cb=%dx%d, Cr=%dx%d)\n",
124                         dinfo.num_components,
125                         dinfo.comp_info[0].h_samp_factor, dinfo.comp_info[0].v_samp_factor,
126                         dinfo.comp_info[1].h_samp_factor, dinfo.comp_info[1].v_samp_factor,
127                         dinfo.comp_info[2].h_samp_factor, dinfo.comp_info[2].v_samp_factor);
128                 return get_black_frame();
129         }
130         if (dinfo.comp_info[0].h_samp_factor != dinfo.max_h_samp_factor ||
131             dinfo.comp_info[0].v_samp_factor != dinfo.max_v_samp_factor ||  // Y' must not be subsampled.
132             dinfo.comp_info[1].h_samp_factor != dinfo.comp_info[2].h_samp_factor ||
133             dinfo.comp_info[1].v_samp_factor != dinfo.comp_info[2].v_samp_factor ||  // Cb and Cr must be identically subsampled.
134             (dinfo.max_h_samp_factor % dinfo.comp_info[1].h_samp_factor) != 0 ||
135             (dinfo.max_v_samp_factor % dinfo.comp_info[1].v_samp_factor) != 0) {  // No 2:3 subsampling or other weirdness.
136                 fprintf(stderr, "Unsupported subsampling scheme. (Y=%dx%d, Cb=%dx%d, Cr=%dx%d)\n",
137                         dinfo.comp_info[0].h_samp_factor, dinfo.comp_info[0].v_samp_factor,
138                         dinfo.comp_info[1].h_samp_factor, dinfo.comp_info[1].v_samp_factor,
139                         dinfo.comp_info[2].h_samp_factor, dinfo.comp_info[2].v_samp_factor);
140                 abort();
141         }
142         dinfo.raw_data_out = true;
143
144         if (!error_mgr.run([&dinfo] {
145                     jpeg_start_decompress(&dinfo);
146             })) {
147                 return get_black_frame();
148         }
149
150         frame->width = dinfo.output_width;
151         frame->height = dinfo.output_height;
152         frame->chroma_subsampling_x = dinfo.max_h_samp_factor / dinfo.comp_info[1].h_samp_factor;
153         frame->chroma_subsampling_y = dinfo.max_v_samp_factor / dinfo.comp_info[1].v_samp_factor;
154
155         unsigned h_mcu_size = DCTSIZE * dinfo.max_h_samp_factor;
156         unsigned v_mcu_size = DCTSIZE * dinfo.max_v_samp_factor;
157         unsigned mcu_width_blocks = (dinfo.output_width + h_mcu_size - 1) / h_mcu_size;
158         unsigned mcu_height_blocks = (dinfo.output_height + v_mcu_size - 1) / v_mcu_size;
159
160         unsigned luma_width_blocks = mcu_width_blocks * dinfo.comp_info[0].h_samp_factor;
161         unsigned chroma_width_blocks = mcu_width_blocks * dinfo.comp_info[1].h_samp_factor;
162         unsigned luma_height_blocks = mcu_height_blocks * dinfo.comp_info[0].v_samp_factor;
163         unsigned chroma_height_blocks = mcu_height_blocks * dinfo.comp_info[1].v_samp_factor;
164
165         // TODO: Decode into a PBO.
166         frame->y.reset(new uint8_t[luma_width_blocks * luma_height_blocks * DCTSIZE2]);
167         frame->cb.reset(new uint8_t[chroma_width_blocks * chroma_height_blocks * DCTSIZE2]);
168         frame->cr.reset(new uint8_t[chroma_width_blocks * chroma_height_blocks * DCTSIZE2]);
169         frame->pitch_y = luma_width_blocks * DCTSIZE;
170         frame->pitch_chroma = chroma_width_blocks * DCTSIZE;
171
172         if (dinfo.marker_list != nullptr &&
173             dinfo.marker_list->marker == JPEG_APP0 + 1 &&
174             dinfo.marker_list->data_length >= 4 &&
175             memcmp(dinfo.marker_list->data, "Exif", 4) == 0) {
176                 frame->exif_data.assign(reinterpret_cast<char *>(dinfo.marker_list->data),
177                         dinfo.marker_list->data_length);
178         }
179
180         if (!error_mgr.run([&dinfo, &frame, v_mcu_size, mcu_height_blocks] {
181                     JSAMPROW yptr[v_mcu_size], cbptr[v_mcu_size], crptr[v_mcu_size];
182                     JSAMPARRAY data[3] = { yptr, cbptr, crptr };
183                     for (unsigned y = 0; y < mcu_height_blocks; ++y) {
184                             // NOTE: The last elements of cbptr/crptr will be unused for vertically subsampled chroma.
185                             for (unsigned yy = 0; yy < v_mcu_size; ++yy) {
186                                     yptr[yy] = frame->y.get() + (y * DCTSIZE * dinfo.max_v_samp_factor + yy) * frame->pitch_y;
187                                     cbptr[yy] = frame->cb.get() + (y * DCTSIZE * dinfo.comp_info[1].v_samp_factor + yy) * frame->pitch_chroma;
188                                     crptr[yy] = frame->cr.get() + (y * DCTSIZE * dinfo.comp_info[1].v_samp_factor + yy) * frame->pitch_chroma;
189                             }
190
191                             jpeg_read_raw_data(&dinfo, data, v_mcu_size);
192                     }
193
194                     (void)jpeg_finish_decompress(&dinfo);
195             })) {
196                 return get_black_frame();
197         }
198
199         ++metric_jpeg_software_decode_frames;
200         steady_clock::time_point stop = steady_clock::now();
201         metric_jpeg_decode_time_seconds.count_event(duration<double>(stop - start).count());
202         return frame;
203 }
204
205 void prune_cache()
206 {
207         // Assumes cache_mu is held.
208         int64_t bytes_still_to_remove = cache_bytes_used - (size_t(CACHE_SIZE_MB) * 1024 * 1024) * 9 / 10;
209         if (bytes_still_to_remove <= 0)
210                 return;
211
212         vector<pair<size_t, size_t>> lru_timestamps_and_size;
213         for (const auto &key_and_value : cache) {
214                 lru_timestamps_and_size.emplace_back(
215                         key_and_value.second.last_used,
216                         frame_size(*key_and_value.second.frame));
217         }
218         sort(lru_timestamps_and_size.begin(), lru_timestamps_and_size.end());
219
220         // Remove the oldest ones until we are below 90% of the cache used.
221         size_t lru_cutoff_point = 0;
222         for (const pair<size_t, size_t> &it : lru_timestamps_and_size) {
223                 lru_cutoff_point = it.first;
224                 bytes_still_to_remove -= it.second;
225                 if (bytes_still_to_remove <= 0)
226                         break;
227         }
228
229         for (auto it = cache.begin(); it != cache.end();) {
230                 if (it->second.last_used <= lru_cutoff_point) {
231                         cache_bytes_used -= frame_size(*it->second.frame);
232                         metric_jpeg_cache_used_bytes = cache_bytes_used;
233                         it = cache.erase(it);
234                 } else {
235                         ++it;
236                 }
237         }
238 }
239
240 shared_ptr<Frame> decode_jpeg_with_cache(FrameOnDisk frame_spec, CacheMissBehavior cache_miss_behavior, FrameReader *frame_reader, bool *did_decode)
241 {
242         *did_decode = false;
243         {
244                 lock_guard<mutex> lock(cache_mu);
245                 auto it = cache.find(frame_spec);
246                 if (it != cache.end()) {
247                         ++metric_jpeg_cache_hit_frames;
248                         it->second.last_used = event_counter++;
249                         return it->second.frame;
250                 }
251         }
252
253         if (cache_miss_behavior == RETURN_NULLPTR_IF_NOT_IN_CACHE) {
254                 ++metric_jpeg_cache_given_up_frames;
255                 return nullptr;
256         }
257
258         ++metric_jpeg_cache_miss_frames;
259
260         *did_decode = true;
261         shared_ptr<Frame> frame = decode_jpeg(frame_reader->read_frame(frame_spec, /*read_video=*/true, /*read_audio=*/false).video);
262
263         lock_guard<mutex> lock(cache_mu);
264         cache_bytes_used += frame_size(*frame);
265         metric_jpeg_cache_used_bytes = cache_bytes_used;
266         cache[frame_spec] = LRUFrame{ frame, event_counter++ };
267
268         if (cache_bytes_used > size_t(CACHE_SIZE_MB) * 1024 * 1024) {
269                 prune_cache();
270         }
271         return frame;
272 }
273
274 void JPEGFrameView::jpeg_decoder_thread_func()
275 {
276         size_t num_decoded = 0, num_dropped = 0;
277
278         pthread_setname_np(pthread_self(), "JPEGDecoder");
279         while (!should_quit.load()) {
280                 PendingDecode decode;
281                 CacheMissBehavior cache_miss_behavior = DECODE_IF_NOT_IN_CACHE;
282                 {
283                         unique_lock<mutex> lock(cache_mu);  // TODO: Perhaps under another lock?
284                         any_pending_decodes.wait(lock, [this] {
285                                 return !pending_decodes.empty() || should_quit.load();
286                         });
287                         if (should_quit.load())
288                                 break;
289                         decode = pending_decodes.front();
290                         pending_decodes.pop_front();
291
292                         if (pending_decodes.size() > 3) {
293                                 cache_miss_behavior = RETURN_NULLPTR_IF_NOT_IN_CACHE;
294                         }
295                 }
296
297                 if (decode.frame != nullptr) {
298                         // Already decoded, so just show it.
299                         setDecodedFrame(decode.frame, nullptr, 1.0f);
300                         continue;
301                 }
302
303                 shared_ptr<Frame> primary_frame, secondary_frame;
304                 bool drop = false;
305                 for (int subframe_idx = 0; subframe_idx < 2; ++subframe_idx) {
306                         const FrameOnDisk &frame_spec = (subframe_idx == 0 ? decode.primary : decode.secondary);
307                         if (frame_spec.pts == -1) {
308                                 // No secondary frame.
309                                 continue;
310                         }
311
312                         bool found_in_cache;
313                         shared_ptr<Frame> frame = decode_jpeg_with_cache(frame_spec, cache_miss_behavior, &frame_reader, &found_in_cache);
314
315                         if (frame == nullptr) {
316                                 assert(cache_miss_behavior == RETURN_NULLPTR_IF_NOT_IN_CACHE);
317                                 drop = true;
318                                 break;
319                         }
320
321                         if (!found_in_cache) {
322                                 ++num_decoded;
323                                 if (num_decoded % 1000 == 0) {
324                                         fprintf(stderr, "Decoded %zu images, dropped %zu (%.2f%% dropped)\n",
325                                                 num_decoded, num_dropped, (100.0 * num_dropped) / (num_decoded + num_dropped));
326                                 }
327                         }
328                         if (subframe_idx == 0) {
329                                 primary_frame = std::move(frame);
330                         } else {
331                                 secondary_frame = std::move(frame);
332                         }
333                 }
334                 if (drop) {
335                         ++num_dropped;
336                         continue;
337                 }
338
339                 // TODO: Could we get jitter between non-interpolated and interpolated frames here?
340                 setDecodedFrame(primary_frame, secondary_frame, decode.fade_alpha);
341         }
342 }
343
344 JPEGFrameView::~JPEGFrameView()
345 {
346         any_pending_decodes.notify_all();
347         jpeg_decoder_thread.join();
348 }
349
350 JPEGFrameView::JPEGFrameView(QWidget *parent)
351         : QGLWidget(parent, global_share_widget)
352 {
353         call_once(jpeg_metrics_inited, [] {
354                 global_metrics.add("jpeg_cache_used_bytes", &metric_jpeg_cache_used_bytes, Metrics::TYPE_GAUGE);
355                 global_metrics.add("jpeg_cache_limit_bytes", &metric_jpeg_cache_limit_bytes, Metrics::TYPE_GAUGE);
356                 global_metrics.add("jpeg_cache_frames", { { "action", "given_up" } }, &metric_jpeg_cache_given_up_frames);
357                 global_metrics.add("jpeg_cache_frames", { { "action", "hit" } }, &metric_jpeg_cache_hit_frames);
358                 global_metrics.add("jpeg_cache_frames", { { "action", "miss" } }, &metric_jpeg_cache_miss_frames);
359                 global_metrics.add("jpeg_decode_frames", { { "decoder", "software" }, { "result", "decode" } }, &metric_jpeg_software_decode_frames);
360                 global_metrics.add("jpeg_decode_frames", { { "decoder", "software" }, { "result", "fail" } }, &metric_jpeg_software_fail_frames);
361                 global_metrics.add("jpeg_decode_frames", { { "decoder", "vaapi" }, { "result", "decode" } }, &metric_jpeg_vaapi_decode_frames);
362                 global_metrics.add("jpeg_decode_frames", { { "decoder", "vaapi" }, { "result", "fail" } }, &metric_jpeg_vaapi_fail_frames);
363                 global_metrics.add("jpeg_frames", { { "action", "prepared" } }, &metric_jpeg_prepared_frames);
364                 global_metrics.add("jpeg_frames", { { "action", "displayed" } }, &metric_jpeg_displayed_frames);
365                 vector<double> quantiles{ 0.01, 0.1, 0.25, 0.5, 0.75, 0.9, 0.99 };
366                 metric_jpeg_decode_time_seconds.init(quantiles, 60.0);
367                 global_metrics.add("jpeg_decode_time_seconds", &metric_jpeg_decode_time_seconds);
368         });
369 }
370
371 void JPEGFrameView::setFrame(unsigned stream_idx, FrameOnDisk frame, FrameOnDisk secondary_frame, float fade_alpha)
372 {
373         current_stream_idx = stream_idx;  // TODO: Does this interact with fades?
374
375         lock_guard<mutex> lock(cache_mu);
376         PendingDecode decode;
377         decode.primary = frame;
378         decode.secondary = secondary_frame;
379         decode.fade_alpha = fade_alpha;
380         pending_decodes.push_back(decode);
381         any_pending_decodes.notify_all();
382 }
383
384 void JPEGFrameView::setFrame(shared_ptr<Frame> frame)
385 {
386         lock_guard<mutex> lock(cache_mu);
387         PendingDecode decode;
388         decode.frame = std::move(frame);
389         pending_decodes.push_back(decode);
390         any_pending_decodes.notify_all();
391 }
392
393 void JPEGFrameView::initializeGL()
394 {
395         glDisable(GL_BLEND);
396         glDisable(GL_DEPTH_TEST);
397         check_error();
398
399         resource_pool = new ResourcePool;
400         jpeg_decoder_thread = std::thread(&JPEGFrameView::jpeg_decoder_thread_func, this);
401
402         ycbcr_converter.reset(new YCbCrConverter(YCbCrConverter::OUTPUT_TO_RGBA, resource_pool));
403
404         ImageFormat inout_format;
405         inout_format.color_space = COLORSPACE_sRGB;
406         inout_format.gamma_curve = GAMMA_sRGB;
407
408         overlay_chain.reset(new EffectChain(overlay_base_width, overlay_base_height, resource_pool));
409         overlay_input = (movit::FlatInput *)overlay_chain->add_input(new FlatInput(inout_format, FORMAT_GRAYSCALE, GL_UNSIGNED_BYTE, overlay_base_width, overlay_base_height));
410
411         overlay_chain->add_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED);
412         overlay_chain->finalize();
413 }
414
415 void JPEGFrameView::resizeGL(int width, int height)
416 {
417         check_error();
418         glViewport(0, 0, width, height);
419         check_error();
420
421         // Save these, as width() and height() will lie with DPI scaling.
422         gl_width = width;
423         gl_height = height;
424 }
425
426 void JPEGFrameView::paintGL()
427 {
428         glViewport(0, 0, gl_width, gl_height);
429         if (current_frame == nullptr) {
430                 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
431                 glClear(GL_COLOR_BUFFER_BIT);
432                 return;
433         }
434
435         if (!displayed_this_frame) {
436                 ++metric_jpeg_displayed_frames;
437                 displayed_this_frame = true;
438         }
439
440         check_error();
441         current_chain->render_to_screen();
442
443         if (overlay_image != nullptr) {
444                 if (overlay_input_needs_refresh) {
445                         overlay_input->set_width(overlay_width);
446                         overlay_input->set_height(overlay_height);
447                         overlay_input->set_pixel_data(overlay_image->bits());
448                         overlay_input_needs_refresh = false;
449                 }
450                 glViewport(gl_width - overlay_width, 0, overlay_width, overlay_height);
451                 overlay_chain->render_to_screen();
452         }
453 }
454
455 namespace {
456
457 }  // namespace
458
459 void JPEGFrameView::setDecodedFrame(shared_ptr<Frame> frame, shared_ptr<Frame> secondary_frame, float fade_alpha)
460 {
461         post_to_main_thread([this, frame, secondary_frame, fade_alpha] {
462                 current_frame = frame;
463                 current_secondary_frame = secondary_frame;
464
465                 if (secondary_frame != nullptr) {
466                         current_chain = ycbcr_converter->prepare_chain_for_fade(frame, secondary_frame, fade_alpha);
467                 } else {
468                         current_chain = ycbcr_converter->prepare_chain_for_conversion(frame);
469                 }
470                 ++metric_jpeg_prepared_frames;
471                 displayed_this_frame = false;
472                 update();
473         });
474 }
475
476 void JPEGFrameView::mousePressEvent(QMouseEvent *event)
477 {
478         if (event->type() == QEvent::MouseButtonPress && event->button() == Qt::LeftButton) {
479                 emit clicked();
480         }
481 }
482
483 void JPEGFrameView::set_overlay(const string &text)
484 {
485         if (text.empty()) {
486                 overlay_image.reset();
487                 return;
488         }
489
490         // Figure out how large the texture needs to be.
491         {
492                 QImage img(overlay_width, overlay_height, QImage::Format_Grayscale8);
493                 QPainter painter(&img);
494                 QFont font = painter.font();
495                 font.setPointSize(12);
496                 QFontMetrics metrics(font);
497                 overlay_base_width = lrint(metrics.boundingRect(QString::fromStdString(text)).width() + 8.0);
498                 overlay_base_height = lrint(metrics.height());
499         }
500
501         float dpr = QGuiApplication::primaryScreen()->devicePixelRatio();
502         overlay_width = lrint(overlay_base_width * dpr);
503         overlay_height = lrint(overlay_base_height * dpr);
504
505         // Work around OpenGL alignment issues.
506         while (overlay_width % 4 != 0) ++overlay_width;
507
508         // Now do the actual drawing.
509         overlay_image.reset(new QImage(overlay_width, overlay_height, QImage::Format_Grayscale8));
510         overlay_image->setDevicePixelRatio(dpr);
511         overlay_image->fill(0);
512         QPainter painter(overlay_image.get());
513
514         painter.setPen(Qt::white);
515         QFont font = painter.font();
516         font.setPointSize(12);
517         painter.setFont(font);
518
519         painter.drawText(QRectF(0, 0, overlay_base_width, overlay_base_height), Qt::AlignCenter, QString::fromStdString(text));
520
521         // Don't refresh immediately; we might not have an OpenGL context here.
522         overlay_input_needs_refresh = true;
523 }
524
525 shared_ptr<Frame> get_black_frame()
526 {
527         static shared_ptr<Frame> black_frame;
528         static once_flag flag;
529         call_once(flag, [] {
530                 black_frame.reset(new Frame);
531                 black_frame->y.reset(new uint8_t[global_flags.width * global_flags.height]);
532                 black_frame->cb.reset(new uint8_t[(global_flags.width / 2) * (global_flags.height / 2)]);
533                 black_frame->cr.reset(new uint8_t[(global_flags.width / 2) * (global_flags.height / 2)]);
534                 black_frame->width = global_flags.width;
535                 black_frame->height = global_flags.height;
536                 black_frame->chroma_subsampling_x = 2;
537                 black_frame->chroma_subsampling_y = 2;
538                 black_frame->pitch_y = global_flags.width;
539                 black_frame->pitch_chroma = global_flags.width / 2;
540         });
541         ++metric_jpeg_software_fail_frames;
542         return black_frame;
543 }