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