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