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