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