]> git.sesse.net Git - nageru/blob - futatabi/vaapi_jpeg_decoder.cpp
452e61c3d13647b3c8f3204f75d9d882aa715bdd
[nageru] / futatabi / vaapi_jpeg_decoder.cpp
1 #include "vaapi_jpeg_decoder.h"
2
3 #include "jpeg_destroyer.h"
4 #include "jpeg_frame.h"
5 #include "jpeglib_error_wrapper.h"
6 #include "pbo_pool.h"
7 #include "shared/memcpy_interleaved.h"
8
9 #include <X11/Xlib.h>
10 #include <assert.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <glob.h>
14 #include <jpeglib.h>
15 #include <list>
16 #include <mutex>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <string>
21 #include <unistd.h>
22 #include <va/va.h>
23 #include <va/va_drm.h>
24 #include <va/va_x11.h>
25
26 #define BUFFER_OFFSET(i) ((char *)nullptr + (i))
27
28 using namespace std;
29
30 static unique_ptr<VADisplayWithCleanup> va_dpy;
31 static VAConfigID config_id;
32 static VAImageFormat uyvy_format;
33 bool vaapi_jpeg_decoding_usable = false;
34
35 struct VAResources {
36         unsigned width, height;
37         VASurfaceID surface;
38         VAContextID context;
39         VAImage image;
40 };
41 static list<VAResources> va_resources_freelist;
42 static mutex va_resources_mutex;
43
44 #define CHECK_VASTATUS(va_status, func) \
45         if (va_status != VA_STATUS_SUCCESS) { \
46                 fprintf(stderr, "%s:%d (%s) failed with %d\n", __func__, __LINE__, func, va_status); \
47                 abort(); \
48         }
49
50 #define CHECK_VASTATUS_RET(va_status, func) \
51         if (va_status != VA_STATUS_SUCCESS) { \
52                 fprintf(stderr, "%s:%d (%s) failed with %d\n", __func__, __LINE__, func, va_status); \
53                 return nullptr; \
54         }
55
56 // From libjpeg (although it's of course identical between implementations).
57 static const int jpeg_natural_order[DCTSIZE2] = {
58          0,  1,  8, 16,  9,  2,  3, 10,
59         17, 24, 32, 25, 18, 11,  4,  5,
60         12, 19, 26, 33, 40, 48, 41, 34,
61         27, 20, 13,  6,  7, 14, 21, 28,
62         35, 42, 49, 56, 57, 50, 43, 36,
63         29, 22, 15, 23, 30, 37, 44, 51,
64         58, 59, 52, 45, 38, 31, 39, 46,
65         53, 60, 61, 54, 47, 55, 62, 63,
66 };
67
68 VAResources get_va_resources(unsigned width, unsigned height)
69 {
70         {
71                 lock_guard<mutex> lock(va_resources_mutex);
72                 for (auto it = va_resources_freelist.begin(); it != va_resources_freelist.end(); ++it) {
73                         if (it->width == width && it->height == height) {
74                                 VAResources ret = *it;
75                                 va_resources_freelist.erase(it);
76                                 return ret;
77                         }
78                 }
79         }
80
81         VAResources ret;
82
83         ret.width = width;
84         ret.height = height;
85
86         VAStatus va_status = vaCreateSurfaces(va_dpy->va_dpy, VA_RT_FORMAT_YUV422,
87                                               width, height,
88                                               &ret.surface, 1, nullptr, 0);
89         CHECK_VASTATUS(va_status, "vaCreateSurfaces");
90
91         va_status = vaCreateContext(va_dpy->va_dpy, config_id, width, height, 0, &ret.surface, 1, &ret.context);
92         CHECK_VASTATUS(va_status, "vaCreateContext");
93
94         va_status = vaCreateImage(va_dpy->va_dpy, &uyvy_format, width, height, &ret.image);
95         CHECK_VASTATUS(va_status, "vaCreateImage");
96
97         return ret;
98 }
99
100 void release_va_resources(VAResources resources)
101 {
102         lock_guard<mutex> lock(va_resources_mutex);
103         if (va_resources_freelist.size() > 10) {
104                 auto it = va_resources_freelist.end();
105                 --it;
106
107                 VAStatus va_status = vaDestroyImage(va_dpy->va_dpy, it->image.image_id);
108                 CHECK_VASTATUS(va_status, "vaDestroyImage");
109
110                 va_status = vaDestroyContext(va_dpy->va_dpy, it->context);
111                 CHECK_VASTATUS(va_status, "vaDestroyContext");
112
113                 va_status = vaDestroySurfaces(va_dpy->va_dpy, &it->surface, 1);
114                 CHECK_VASTATUS(va_status, "vaDestroySurfaces");
115
116                 va_resources_freelist.erase(it);
117         }
118
119         va_resources_freelist.push_front(resources);
120 }
121
122 // RAII wrapper to release VAResources on return (even on error).
123 class ReleaseVAResources {
124 public:
125         ReleaseVAResources(const VAResources &resources)
126                 : resources(resources) {}
127         ~ReleaseVAResources()
128         {
129                 if (!committed) {
130                         release_va_resources(resources);
131                 }
132         }
133
134         void commit() { committed = true; }
135
136 private:
137         const VAResources &resources;
138         bool committed = false;
139 };
140
141 unique_ptr<VADisplayWithCleanup> va_open_display(const string &va_display)
142 {
143         if (va_display.empty() || va_display[0] != '/') {  // An X display.
144                 Display *x11_display = XOpenDisplay(va_display.empty() ? nullptr : va_display.c_str());
145                 if (x11_display == nullptr) {
146                         fprintf(stderr, "error: can't connect to X server!\n");
147                         return nullptr;
148                 }
149
150                 unique_ptr<VADisplayWithCleanup> ret(new VADisplayWithCleanup);
151                 ret->x11_display = x11_display;
152                 ret->va_dpy = vaGetDisplay(x11_display);
153                 if (ret->va_dpy == nullptr) {
154                         return nullptr;
155                 }
156                 return ret;
157         } else {  // A DRM node on the filesystem (e.g. /dev/dri/renderD128).
158                 int drm_fd = open(va_display.c_str(), O_RDWR);
159                 if (drm_fd == -1) {
160                         perror(va_display.c_str());
161                         return nullptr;
162                 }
163                 unique_ptr<VADisplayWithCleanup> ret(new VADisplayWithCleanup);
164                 ret->drm_fd = drm_fd;
165                 ret->va_dpy = vaGetDisplayDRM(drm_fd);
166                 if (ret->va_dpy == nullptr) {
167                         return nullptr;
168                 }
169                 return ret;
170         }
171 }
172
173 unique_ptr<VADisplayWithCleanup> try_open_va(const string &va_display, string *error)
174 {
175         unique_ptr<VADisplayWithCleanup> va_dpy = va_open_display(va_display);
176         if (va_dpy == nullptr) {
177                 if (error)
178                         *error = "Opening VA display failed";
179                 return nullptr;
180         }
181         int major_ver, minor_ver;
182         VAStatus va_status = vaInitialize(va_dpy->va_dpy, &major_ver, &minor_ver);
183         if (va_status != VA_STATUS_SUCCESS) {
184                 char buf[256];
185                 snprintf(buf, sizeof(buf), "vaInitialize() failed with status %d\n", va_status);
186                 if (error != nullptr)
187                         *error = buf;
188                 return nullptr;
189         }
190
191         int num_entrypoints = vaMaxNumEntrypoints(va_dpy->va_dpy);
192         unique_ptr<VAEntrypoint[]> entrypoints(new VAEntrypoint[num_entrypoints]);
193         if (entrypoints == nullptr) {
194                 if (error != nullptr)
195                         *error = "Failed to allocate memory for VA entry points";
196                 return nullptr;
197         }
198
199         vaQueryConfigEntrypoints(va_dpy->va_dpy, VAProfileJPEGBaseline, entrypoints.get(), &num_entrypoints);
200         for (int slice_entrypoint = 0; slice_entrypoint < num_entrypoints; slice_entrypoint++) {
201                 if (entrypoints[slice_entrypoint] != VAEntrypointVLD) {
202                         continue;
203                 }
204
205                 // We found a usable decode, so return it.
206                 return va_dpy;
207         }
208
209         if (error != nullptr)
210                 *error = "Can't find VAEntrypointVLD for the JPEG profile";
211         return nullptr;
212 }
213
214 string get_usable_va_display()
215 {
216         // Reduce the amount of chatter while probing,
217         // unless the user has specified otherwise.
218         bool need_env_reset = false;
219         if (getenv("LIBVA_MESSAGING_LEVEL") == nullptr) {
220                 setenv("LIBVA_MESSAGING_LEVEL", "0", true);
221                 need_env_reset = true;
222         }
223
224         // First try the default (ie., whatever $DISPLAY is set to).
225         unique_ptr<VADisplayWithCleanup> va_dpy = try_open_va("", nullptr);
226         if (va_dpy != nullptr) {
227                 if (need_env_reset) {
228                         unsetenv("LIBVA_MESSAGING_LEVEL");
229                 }
230                 return "";
231         }
232
233         fprintf(stderr, "The X11 display did not expose a VA-API JPEG decoder.\n");
234
235         // Try all /dev/dri/render* in turn. TODO: Accept /dev/dri/card*, too?
236         glob_t g;
237         int err = glob("/dev/dri/renderD*", 0, nullptr, &g);
238         if (err != 0) {
239                 fprintf(stderr, "Couldn't list render nodes (%s) when trying to autodetect a replacement.\n", strerror(errno));
240         } else {
241                 for (size_t i = 0; i < g.gl_pathc; ++i) {
242                         string path = g.gl_pathv[i];
243                         va_dpy = try_open_va(path, nullptr);
244                         if (va_dpy != nullptr) {
245                                 fprintf(stderr, "Autodetected %s as a suitable replacement; using it.\n",
246                                         path.c_str());
247                                 globfree(&g);
248                                 if (need_env_reset) {
249                                         unsetenv("LIBVA_MESSAGING_LEVEL");
250                                 }
251                                 return path;
252                         }
253                 }
254         }
255
256         fprintf(stderr, "No suitable VA-API JPEG decoders were found in /dev/dri; giving up.\n");
257         fprintf(stderr, "Note that if you are using an Intel CPU with an external GPU,\n");
258         fprintf(stderr, "you may need to enable the integrated Intel GPU in your BIOS\n");
259         fprintf(stderr, "to expose Quick Sync.\n");
260         return "none";
261 }
262
263 void init_jpeg_vaapi()
264 {
265         string dpy = get_usable_va_display();
266         if (dpy == "none") {
267                 return;
268         }
269
270         va_dpy = try_open_va(dpy, nullptr);
271         if (va_dpy == nullptr) {
272                 return;
273         }
274
275         VAConfigAttrib attr = { VAConfigAttribRTFormat, VA_RT_FORMAT_YUV422 };
276
277         VAStatus va_status = vaCreateConfig(va_dpy->va_dpy, VAProfileJPEGBaseline, VAEntrypointVLD,
278                                             &attr, 1, &config_id);
279         CHECK_VASTATUS(va_status, "vaCreateConfig");
280
281         int num_formats = vaMaxNumImageFormats(va_dpy->va_dpy);
282         assert(num_formats > 0);
283
284         unique_ptr<VAImageFormat[]> formats(new VAImageFormat[num_formats]);
285         va_status = vaQueryImageFormats(va_dpy->va_dpy, formats.get(), &num_formats);
286         CHECK_VASTATUS(va_status, "vaQueryImageFormats");
287
288         bool found = false;
289         for (int i = 0; i < num_formats; ++i) {
290                 // Seemingly VA_FOURCC_422H is no good for vaGetImage(). :-/
291                 if (formats[i].fourcc == VA_FOURCC_UYVY) {
292                         memcpy(&uyvy_format, &formats[i], sizeof(VAImageFormat));
293                         found = true;
294                         break;
295                 }
296         }
297         if (!found) {
298                 return;
299         }
300
301         fprintf(stderr, "VA-API JPEG decoding initialized.\n");
302         vaapi_jpeg_decoding_usable = true;
303 }
304
305 class VABufferDestroyer {
306 public:
307         VABufferDestroyer(VADisplay dpy, VABufferID buf)
308                 : dpy(dpy), buf(buf) {}
309
310         ~VABufferDestroyer()
311         {
312                 VAStatus va_status = vaDestroyBuffer(dpy, buf);
313                 CHECK_VASTATUS(va_status, "vaDestroyBuffer");
314         }
315
316 private:
317         VADisplay dpy;
318         VABufferID buf;
319 };
320
321 shared_ptr<Frame> decode_jpeg_vaapi(const string &jpeg)
322 {
323         jpeg_decompress_struct dinfo;
324         JPEGWrapErrorManager error_mgr(&dinfo);
325         if (!error_mgr.run([&dinfo] { jpeg_create_decompress(&dinfo); })) {
326                 return nullptr;
327         }
328         JPEGDestroyer destroy_dinfo(&dinfo);
329
330         jpeg_save_markers(&dinfo, JPEG_APP0 + 1, 0xFFFF);
331
332         jpeg_mem_src(&dinfo, reinterpret_cast<const unsigned char *>(jpeg.data()), jpeg.size());
333         if (!error_mgr.run([&dinfo] { jpeg_read_header(&dinfo, true); })) {
334                 return nullptr;
335         }
336
337         if (dinfo.num_components != 3) {
338                 fprintf(stderr, "Not a color JPEG. (%d components, Y=%dx%d, Cb=%dx%d, Cr=%dx%d)\n",
339                         dinfo.num_components,
340                         dinfo.comp_info[0].h_samp_factor, dinfo.comp_info[0].v_samp_factor,
341                         dinfo.comp_info[1].h_samp_factor, dinfo.comp_info[1].v_samp_factor,
342                         dinfo.comp_info[2].h_samp_factor, dinfo.comp_info[2].v_samp_factor);
343                 return nullptr;
344         }
345         if (dinfo.comp_info[0].h_samp_factor != 2 ||
346             dinfo.comp_info[1].h_samp_factor != 1 ||
347             dinfo.comp_info[1].v_samp_factor != dinfo.comp_info[0].v_samp_factor ||
348             dinfo.comp_info[2].h_samp_factor != 1 ||
349             dinfo.comp_info[2].v_samp_factor != dinfo.comp_info[0].v_samp_factor) {
350                 fprintf(stderr, "Not 4:2:2. (Y=%dx%d, Cb=%dx%d, Cr=%dx%d)\n",
351                         dinfo.comp_info[0].h_samp_factor, dinfo.comp_info[0].v_samp_factor,
352                         dinfo.comp_info[1].h_samp_factor, dinfo.comp_info[1].v_samp_factor,
353                         dinfo.comp_info[2].h_samp_factor, dinfo.comp_info[2].v_samp_factor);
354                 return nullptr;
355         }
356
357         // Picture parameters.
358         VAPictureParameterBufferJPEGBaseline pic_param;
359         memset(&pic_param, 0, sizeof(pic_param));
360         pic_param.picture_width = dinfo.image_width;
361         pic_param.picture_height = dinfo.image_height;
362         for (int component_idx = 0; component_idx < dinfo.num_components; ++component_idx) {
363                 const jpeg_component_info *comp = &dinfo.comp_info[component_idx];
364                 pic_param.components[component_idx].component_id = comp->component_id;
365                 pic_param.components[component_idx].h_sampling_factor = comp->h_samp_factor;
366                 pic_param.components[component_idx].v_sampling_factor = comp->v_samp_factor;
367                 pic_param.components[component_idx].quantiser_table_selector = comp->quant_tbl_no;
368         }
369         pic_param.num_components = dinfo.num_components;
370         pic_param.color_space = 0;  // YUV.
371         pic_param.rotation = VA_ROTATION_NONE;
372
373         VAResources resources = get_va_resources(dinfo.image_width, dinfo.image_height);
374         ReleaseVAResources release(resources);
375
376         VABufferID pic_param_buffer;
377         VAStatus va_status = vaCreateBuffer(va_dpy->va_dpy, resources.context, VAPictureParameterBufferType, sizeof(pic_param), 1, &pic_param, &pic_param_buffer);
378         CHECK_VASTATUS_RET(va_status, "vaCreateBuffer");
379         VABufferDestroyer destroy_pic_param(va_dpy->va_dpy, pic_param_buffer);
380
381         // Quantization matrices.
382         VAIQMatrixBufferJPEGBaseline iq;
383         memset(&iq, 0, sizeof(iq));
384
385         for (int quant_tbl_idx = 0; quant_tbl_idx < min(4, NUM_QUANT_TBLS); ++quant_tbl_idx) {
386                 const JQUANT_TBL *qtbl = dinfo.quant_tbl_ptrs[quant_tbl_idx];
387                 if (qtbl == nullptr) {
388                         iq.load_quantiser_table[quant_tbl_idx] = 0;
389                 } else {
390                         iq.load_quantiser_table[quant_tbl_idx] = 1;
391                         for (int i = 0; i < 64; ++i) {
392                                 if (qtbl->quantval[i] > 255) {
393                                         fprintf(stderr, "Baseline JPEG only!\n");
394                                         return nullptr;
395                                 }
396                                 iq.quantiser_table[quant_tbl_idx][i] = qtbl->quantval[jpeg_natural_order[i]];
397                         }
398                 }
399         }
400
401         VABufferID iq_buffer;
402         va_status = vaCreateBuffer(va_dpy->va_dpy, resources.context, VAIQMatrixBufferType, sizeof(iq), 1, &iq, &iq_buffer);
403         CHECK_VASTATUS_RET(va_status, "vaCreateBuffer");
404         VABufferDestroyer destroy_iq(va_dpy->va_dpy, iq_buffer);
405
406         // Huffman tables (arithmetic is not supported).
407         VAHuffmanTableBufferJPEGBaseline huff;
408         memset(&huff, 0, sizeof(huff));
409
410         for (int huff_tbl_idx = 0; huff_tbl_idx < min(2, NUM_HUFF_TBLS); ++huff_tbl_idx) {
411                 const JHUFF_TBL *ac_hufftbl = dinfo.ac_huff_tbl_ptrs[huff_tbl_idx];
412                 const JHUFF_TBL *dc_hufftbl = dinfo.dc_huff_tbl_ptrs[huff_tbl_idx];
413                 if (ac_hufftbl == nullptr) {
414                         assert(dc_hufftbl == nullptr);
415                         huff.load_huffman_table[huff_tbl_idx] = 0;
416                 } else {
417                         assert(dc_hufftbl != nullptr);
418                         huff.load_huffman_table[huff_tbl_idx] = 1;
419
420                         for (int i = 0; i < 16; ++i) {
421                                 huff.huffman_table[huff_tbl_idx].num_dc_codes[i] = dc_hufftbl->bits[i + 1];
422                         }
423                         for (int i = 0; i < 12; ++i) {
424                                 huff.huffman_table[huff_tbl_idx].dc_values[i] = dc_hufftbl->huffval[i];
425                         }
426                         for (int i = 0; i < 16; ++i) {
427                                 huff.huffman_table[huff_tbl_idx].num_ac_codes[i] = ac_hufftbl->bits[i + 1];
428                         }
429                         for (int i = 0; i < 162; ++i) {
430                                 huff.huffman_table[huff_tbl_idx].ac_values[i] = ac_hufftbl->huffval[i];
431                         }
432                 }
433         }
434
435         VABufferID huff_buffer;
436         va_status = vaCreateBuffer(va_dpy->va_dpy, resources.context, VAHuffmanTableBufferType, sizeof(huff), 1, &huff, &huff_buffer);
437         CHECK_VASTATUS_RET(va_status, "vaCreateBuffer");
438         VABufferDestroyer destroy_huff(va_dpy->va_dpy, huff_buffer);
439
440         // Slice parameters (metadata about the slice).
441         VASliceParameterBufferJPEGBaseline parms;
442         memset(&parms, 0, sizeof(parms));
443         parms.slice_data_size = dinfo.src->bytes_in_buffer;
444         parms.slice_data_offset = 0;
445         parms.slice_data_flag = VA_SLICE_DATA_FLAG_ALL;
446         parms.slice_horizontal_position = 0;
447         parms.slice_vertical_position = 0;
448         for (int component_idx = 0; component_idx < dinfo.num_components; ++component_idx) {
449                 const jpeg_component_info *comp = &dinfo.comp_info[component_idx];
450                 parms.components[component_idx].component_selector = comp->component_id;
451                 parms.components[component_idx].dc_table_selector = comp->dc_tbl_no;
452                 parms.components[component_idx].ac_table_selector = comp->ac_tbl_no;
453                 if (parms.components[component_idx].dc_table_selector > 1 ||
454                     parms.components[component_idx].ac_table_selector > 1) {
455                         fprintf(stderr, "Uses too many Huffman tables\n");
456                         return nullptr;
457                 }
458         }
459         parms.num_components = dinfo.num_components;
460         parms.restart_interval = dinfo.restart_interval;
461         int horiz_mcus = (dinfo.image_width + (DCTSIZE * 2) - 1) / (DCTSIZE * 2);
462         int vert_mcus = (dinfo.image_height + DCTSIZE - 1) / DCTSIZE;
463         parms.num_mcus = horiz_mcus * vert_mcus;
464
465         VABufferID slice_param_buffer;
466         va_status = vaCreateBuffer(va_dpy->va_dpy, resources.context, VASliceParameterBufferType, sizeof(parms), 1, &parms, &slice_param_buffer);
467         CHECK_VASTATUS_RET(va_status, "vaCreateBuffer");
468         VABufferDestroyer destroy_slice_param(va_dpy->va_dpy, slice_param_buffer);
469
470         // The actual data. VA-API will destuff and all for us.
471         VABufferID data_buffer;
472         va_status = vaCreateBuffer(va_dpy->va_dpy, resources.context, VASliceDataBufferType, dinfo.src->bytes_in_buffer, 1, const_cast<unsigned char *>(dinfo.src->next_input_byte), &data_buffer);
473         CHECK_VASTATUS_RET(va_status, "vaCreateBuffer");
474         VABufferDestroyer destroy_data(va_dpy->va_dpy, data_buffer);
475
476         va_status = vaBeginPicture(va_dpy->va_dpy, resources.context, resources.surface);
477         CHECK_VASTATUS_RET(va_status, "vaBeginPicture");
478         va_status = vaRenderPicture(va_dpy->va_dpy, resources.context, &pic_param_buffer, 1);
479         CHECK_VASTATUS_RET(va_status, "vaRenderPicture(pic_param)");
480         va_status = vaRenderPicture(va_dpy->va_dpy, resources.context, &iq_buffer, 1);
481         CHECK_VASTATUS_RET(va_status, "vaRenderPicture(iq)");
482         va_status = vaRenderPicture(va_dpy->va_dpy, resources.context, &huff_buffer, 1);
483         CHECK_VASTATUS_RET(va_status, "vaRenderPicture(huff)");
484         va_status = vaRenderPicture(va_dpy->va_dpy, resources.context, &slice_param_buffer, 1);
485         CHECK_VASTATUS_RET(va_status, "vaRenderPicture(slice_param)");
486         va_status = vaRenderPicture(va_dpy->va_dpy, resources.context, &data_buffer, 1);
487         CHECK_VASTATUS_RET(va_status, "vaRenderPicture(data)");
488         va_status = vaEndPicture(va_dpy->va_dpy, resources.context);
489         CHECK_VASTATUS_RET(va_status, "vaEndPicture");
490
491         // vaDeriveImage() works, but the resulting image seems to live in
492         // uncached memory, which makes copying data out from it very, very slow.
493         // Thanks to FFmpeg for the observation that you can vaGetImage() the
494         // surface onto your own image (although then, it can't be planar, which
495         // is unfortunate for us).
496 #if 0
497         VAImage image;
498         va_status = vaDeriveImage(va_dpy->va_dpy, surf, &image);
499         CHECK_VASTATUS_RET(va_status, "vaDeriveImage");
500 #else
501         va_status = vaSyncSurface(va_dpy->va_dpy, resources.surface);
502         CHECK_VASTATUS_RET(va_status, "vaSyncSurface");
503
504         va_status = vaGetImage(va_dpy->va_dpy, resources.surface, 0, 0, dinfo.image_width, dinfo.image_height, resources.image.image_id);
505         CHECK_VASTATUS_RET(va_status, "vaGetImage");
506 #endif
507
508         void *mapped;
509         va_status = vaMapBuffer(va_dpy->va_dpy, resources.image.buf, &mapped);
510         CHECK_VASTATUS_RET(va_status, "vaMapBuffer");
511
512         shared_ptr<Frame> frame(new Frame);
513 #if 0
514         // 4:2:2 planar (for vaDeriveImage).
515         frame->y.reset(new uint8_t[dinfo.image_width * dinfo.image_height]);
516         frame->cb.reset(new uint8_t[(dinfo.image_width / 2) * dinfo.image_height]);
517         frame->cr.reset(new uint8_t[(dinfo.image_width / 2) * dinfo.image_height]);
518         for (int component_idx = 0; component_idx < dinfo.num_components; ++component_idx) {
519                 uint8_t *dptr;
520                 size_t width;
521                 if (component_idx == 0) {
522                         dptr = frame->y.get();
523                         width = dinfo.image_width;
524                 } else if (component_idx == 1) {
525                         dptr = frame->cb.get();
526                         width = dinfo.image_width / 2;
527                 } else if (component_idx == 2) {
528                         dptr = frame->cr.get();
529                         width = dinfo.image_width / 2;
530                 } else {
531                         assert(false);
532                 }
533                 const uint8_t *sptr = (const uint8_t *)mapped + image.offsets[component_idx];
534                 size_t spitch = image.pitches[component_idx];
535                 for (size_t y = 0; y < dinfo.image_height; ++y) {
536                         memcpy(dptr + y * width, sptr + y * spitch, width);
537                 }
538         }
539 #else
540         // Convert Y'CbCr to separate Y' and CbCr.
541         frame->is_semiplanar = true;
542
543         PBO pbo = global_pbo_pool->alloc_pbo();
544         size_t cbcr_offset = dinfo.image_width * dinfo.image_height;
545         uint8_t *y_pix = pbo.ptr;
546         uint8_t *cbcr_pix = pbo.ptr + cbcr_offset;
547
548         const uint8_t *src = (const uint8_t *)mapped + resources.image.offsets[0];
549         if (resources.image.pitches[0] == dinfo.image_width * 2) {
550                 memcpy_interleaved(cbcr_pix, y_pix, src, dinfo.image_width * dinfo.image_height * 2);
551         } else {
552                 for (unsigned y = 0; y < dinfo.image_height; ++y) {
553                         memcpy_interleaved(cbcr_pix + y * dinfo.image_width, y_pix + y * dinfo.image_width,
554                                            src + y * resources.image.pitches[0], dinfo.image_width * 2);
555                 }
556         }
557
558         glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo.pbo);
559         frame->y = create_texture_2d(dinfo.image_width, dinfo.image_height, GL_R8, GL_RED, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
560         frame->cbcr = create_texture_2d(dinfo.image_width / 2, dinfo.image_height, GL_RG8, GL_RG, GL_UNSIGNED_BYTE, BUFFER_OFFSET(cbcr_offset));
561         glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
562
563         glFlushMappedNamedBufferRange(pbo.pbo, 0, dinfo.image_width * dinfo.image_height * 2);
564         glMemoryBarrier(GL_PIXEL_BUFFER_BARRIER_BIT);
565         pbo.upload_done = RefCountedGLsync(GL_SYNC_GPU_COMMANDS_COMPLETE, /*flags=*/0);
566         frame->uploaded_ui_thread = pbo.upload_done;
567         frame->uploaded_interpolation = pbo.upload_done;
568         global_pbo_pool->release_pbo(move(pbo));
569 #endif
570         frame->width = dinfo.image_width;
571         frame->height = dinfo.image_height;
572         frame->chroma_subsampling_x = 2;
573         frame->chroma_subsampling_y = 1;
574
575         if (dinfo.marker_list != nullptr &&
576             dinfo.marker_list->marker == JPEG_APP0 + 1 &&
577             dinfo.marker_list->data_length >= 4 &&
578             memcmp(dinfo.marker_list->data, "Exif", 4) == 0) {
579                 frame->exif_data.assign(reinterpret_cast<char *>(dinfo.marker_list->data),
580                         dinfo.marker_list->data_length);
581         }
582
583         va_status = vaUnmapBuffer(va_dpy->va_dpy, resources.image.buf);
584         CHECK_VASTATUS_RET(va_status, "vaUnmapBuffer");
585
586         return frame;
587 }