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