]> git.sesse.net Git - nageru/blob - futatabi/vaapi_jpeg_decoder.cpp
Move VAResourcePool into a shared class between MJPEGEncoder in Nageru and the VA...
[nageru] / futatabi / vaapi_jpeg_decoder.cpp
1 #include "vaapi_jpeg_decoder.h"
2
3 #include "jpeg_destroyer.h"
4 #include "jpeg_frame.h"
5 #include "jpeglib_error_wrapper.h"
6 #include "pbo_pool.h"
7 #include "shared/memcpy_interleaved.h"
8 #include "shared/va_display.h"
9 #include "shared/va_resource_pool.h"
10
11 #include <X11/Xlib.h>
12 #include <assert.h>
13 #include <errno.h>
14 #include <fcntl.h>
15 #include <glob.h>
16 #include <jpeglib.h>
17 #include <list>
18 #include <mutex>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <string>
23 #include <unistd.h>
24 #include <va/va.h>
25 #include <va/va_drm.h>
26 #include <va/va_x11.h>
27
28 #define BUFFER_OFFSET(i) ((char *)nullptr + (i))
29
30 using namespace std;
31
32 static unique_ptr<VADisplayWithCleanup> va_dpy;
33 static unique_ptr<VAResourcePool> va_pool;
34
35 bool vaapi_jpeg_decoding_usable = false;
36
37 // From libjpeg (although it's of course identical between implementations).
38 static const int jpeg_natural_order[DCTSIZE2] = {
39          0,  1,  8, 16,  9,  2,  3, 10,
40         17, 24, 32, 25, 18, 11,  4,  5,
41         12, 19, 26, 33, 40, 48, 41, 34,
42         27, 20, 13,  6,  7, 14, 21, 28,
43         35, 42, 49, 56, 57, 50, 43, 36,
44         29, 22, 15, 23, 30, 37, 44, 51,
45         58, 59, 52, 45, 38, 31, 39, 46,
46         53, 60, 61, 54, 47, 55, 62, 63,
47 };
48
49 static unique_ptr<VADisplayWithCleanup> try_open_va_mjpeg(const string &va_display)
50 {
51         VAConfigID config_id_422, config_id_420;
52         VAImageFormat uyvy_format, nv12_format;
53
54         // Seemingly VA_FOURCC_422H is no good for vaGetImage(). :-/
55         unique_ptr<VADisplayWithCleanup> va_dpy =
56                 try_open_va(va_display, { VAProfileJPEGBaseline }, VAEntrypointVLD,
57                         { { "4:2:2", VA_RT_FORMAT_YUV422, VA_FOURCC_UYVY, &config_id_422, &uyvy_format },
58                           { "4:2:0", VA_RT_FORMAT_YUV420, VA_FOURCC_NV12, &config_id_420, &nv12_format } },
59                         /*chosen_profile=*/nullptr, /*error=*/nullptr);
60         if (va_dpy == nullptr) {
61                 return va_dpy;
62         }
63
64         va_pool.reset(new VAResourcePool(va_dpy->va_dpy, uyvy_format, nv12_format, config_id_422, config_id_420, /*with_data_buffer=*/false));
65
66         return va_dpy;
67 }
68
69 string get_usable_va_display()
70 {
71         // Reduce the amount of chatter while probing,
72         // unless the user has specified otherwise.
73         bool need_env_reset = false;
74         if (getenv("LIBVA_MESSAGING_LEVEL") == nullptr) {
75                 setenv("LIBVA_MESSAGING_LEVEL", "0", true);
76                 need_env_reset = true;
77         }
78
79         // First try the default (ie., whatever $DISPLAY is set to).
80         unique_ptr<VADisplayWithCleanup> va_dpy = try_open_va_mjpeg("");
81         if (va_dpy != nullptr) {
82                 if (need_env_reset) {
83                         unsetenv("LIBVA_MESSAGING_LEVEL");
84                 }
85                 return "";
86         }
87
88         fprintf(stderr, "The X11 display did not expose a VA-API JPEG decoder.\n");
89
90         // Try all /dev/dri/render* in turn. TODO: Accept /dev/dri/card*, too?
91         glob_t g;
92         int err = glob("/dev/dri/renderD*", 0, nullptr, &g);
93         if (err != 0) {
94                 fprintf(stderr, "Couldn't list render nodes (%s) when trying to autodetect a replacement.\n", strerror(errno));
95         } else {
96                 for (size_t i = 0; i < g.gl_pathc; ++i) {
97                         string path = g.gl_pathv[i];
98                         va_dpy = try_open_va_mjpeg(path);
99                         if (va_dpy != nullptr) {
100                                 fprintf(stderr, "Autodetected %s as a suitable replacement; using it.\n",
101                                         path.c_str());
102                                 globfree(&g);
103                                 if (need_env_reset) {
104                                         unsetenv("LIBVA_MESSAGING_LEVEL");
105                                 }
106                                 return path;
107                         }
108                 }
109         }
110
111         fprintf(stderr, "No suitable VA-API JPEG decoders were found in /dev/dri; giving up.\n");
112         fprintf(stderr, "Note that if you are using an Intel CPU with an external GPU,\n");
113         fprintf(stderr, "you may need to enable the integrated Intel GPU in your BIOS\n");
114         fprintf(stderr, "to expose Quick Sync.\n");
115         return "none";
116 }
117
118 void init_jpeg_vaapi()
119 {
120         string dpy = get_usable_va_display();
121         if (dpy == "none") {
122                 return;
123         }
124
125         va_dpy = try_open_va_mjpeg(dpy);
126         if (va_dpy == nullptr) {
127                 return;
128         }
129
130         fprintf(stderr, "VA-API JPEG decoding initialized.\n");
131         vaapi_jpeg_decoding_usable = true;
132 }
133
134 class VABufferDestroyer {
135 public:
136         VABufferDestroyer(VADisplay dpy, VABufferID buf)
137                 : dpy(dpy), buf(buf) {}
138
139         ~VABufferDestroyer()
140         {
141                 VAStatus va_status = vaDestroyBuffer(dpy, buf);
142                 CHECK_VASTATUS(va_status, "vaDestroyBuffer");
143         }
144
145 private:
146         VADisplay dpy;
147         VABufferID buf;
148 };
149
150 shared_ptr<Frame> decode_jpeg_vaapi(const string &jpeg)
151 {
152         jpeg_decompress_struct dinfo;
153         JPEGWrapErrorManager error_mgr(&dinfo);
154         if (!error_mgr.run([&dinfo] { jpeg_create_decompress(&dinfo); })) {
155                 return nullptr;
156         }
157         JPEGDestroyer destroy_dinfo(&dinfo);
158
159         jpeg_save_markers(&dinfo, JPEG_APP0 + 1, 0xFFFF);
160
161         jpeg_mem_src(&dinfo, reinterpret_cast<const unsigned char *>(jpeg.data()), jpeg.size());
162         if (!error_mgr.run([&dinfo] { jpeg_read_header(&dinfo, true); })) {
163                 return nullptr;
164         }
165
166         if (dinfo.num_components != 3) {
167                 fprintf(stderr, "Not a color JPEG. (%d components, Y=%dx%d, Cb=%dx%d, Cr=%dx%d)\n",
168                         dinfo.num_components,
169                         dinfo.comp_info[0].h_samp_factor, dinfo.comp_info[0].v_samp_factor,
170                         dinfo.comp_info[1].h_samp_factor, dinfo.comp_info[1].v_samp_factor,
171                         dinfo.comp_info[2].h_samp_factor, dinfo.comp_info[2].v_samp_factor);
172                 return nullptr;
173         }
174         if (dinfo.comp_info[0].h_samp_factor != 2 ||
175             dinfo.comp_info[1].h_samp_factor != 1 ||
176             dinfo.comp_info[1].v_samp_factor != dinfo.comp_info[0].v_samp_factor ||
177             dinfo.comp_info[2].h_samp_factor != 1 ||
178             dinfo.comp_info[2].v_samp_factor != dinfo.comp_info[0].v_samp_factor) {
179                 fprintf(stderr, "Not 4:2:2. (Y=%dx%d, Cb=%dx%d, Cr=%dx%d)\n",
180                         dinfo.comp_info[0].h_samp_factor, dinfo.comp_info[0].v_samp_factor,
181                         dinfo.comp_info[1].h_samp_factor, dinfo.comp_info[1].v_samp_factor,
182                         dinfo.comp_info[2].h_samp_factor, dinfo.comp_info[2].v_samp_factor);
183                 return nullptr;
184         }
185
186         // Picture parameters.
187         VAPictureParameterBufferJPEGBaseline pic_param;
188         memset(&pic_param, 0, sizeof(pic_param));
189         pic_param.picture_width = dinfo.image_width;
190         pic_param.picture_height = dinfo.image_height;
191         for (int component_idx = 0; component_idx < dinfo.num_components; ++component_idx) {
192                 const jpeg_component_info *comp = &dinfo.comp_info[component_idx];
193                 pic_param.components[component_idx].component_id = comp->component_id;
194                 pic_param.components[component_idx].h_sampling_factor = comp->h_samp_factor;
195                 pic_param.components[component_idx].v_sampling_factor = comp->v_samp_factor;
196                 pic_param.components[component_idx].quantiser_table_selector = comp->quant_tbl_no;
197         }
198         pic_param.num_components = dinfo.num_components;
199         pic_param.color_space = 0;  // YUV.
200         pic_param.rotation = VA_ROTATION_NONE;
201
202         VAResourcePool::VAResources resources = va_pool->get_va_resources(dinfo.image_width, dinfo.image_height, VA_FOURCC_UYVY);
203         ReleaseVAResources release(va_pool.get(), resources);
204
205         VABufferID pic_param_buffer;
206         VAStatus va_status = vaCreateBuffer(va_dpy->va_dpy, resources.context, VAPictureParameterBufferType, sizeof(pic_param), 1, &pic_param, &pic_param_buffer);
207         CHECK_VASTATUS_RET(va_status, "vaCreateBuffer");
208         VABufferDestroyer destroy_pic_param(va_dpy->va_dpy, pic_param_buffer);
209
210         // Quantization matrices.
211         VAIQMatrixBufferJPEGBaseline iq;
212         memset(&iq, 0, sizeof(iq));
213
214         for (int quant_tbl_idx = 0; quant_tbl_idx < min(4, NUM_QUANT_TBLS); ++quant_tbl_idx) {
215                 const JQUANT_TBL *qtbl = dinfo.quant_tbl_ptrs[quant_tbl_idx];
216                 if (qtbl == nullptr) {
217                         iq.load_quantiser_table[quant_tbl_idx] = 0;
218                 } else {
219                         iq.load_quantiser_table[quant_tbl_idx] = 1;
220                         for (int i = 0; i < 64; ++i) {
221                                 if (qtbl->quantval[i] > 255) {
222                                         fprintf(stderr, "Baseline JPEG only!\n");
223                                         return nullptr;
224                                 }
225                                 iq.quantiser_table[quant_tbl_idx][i] = qtbl->quantval[jpeg_natural_order[i]];
226                         }
227                 }
228         }
229
230         VABufferID iq_buffer;
231         va_status = vaCreateBuffer(va_dpy->va_dpy, resources.context, VAIQMatrixBufferType, sizeof(iq), 1, &iq, &iq_buffer);
232         CHECK_VASTATUS_RET(va_status, "vaCreateBuffer");
233         VABufferDestroyer destroy_iq(va_dpy->va_dpy, iq_buffer);
234
235         // Huffman tables (arithmetic is not supported).
236         VAHuffmanTableBufferJPEGBaseline huff;
237         memset(&huff, 0, sizeof(huff));
238
239         for (int huff_tbl_idx = 0; huff_tbl_idx < min(2, NUM_HUFF_TBLS); ++huff_tbl_idx) {
240                 const JHUFF_TBL *ac_hufftbl = dinfo.ac_huff_tbl_ptrs[huff_tbl_idx];
241                 const JHUFF_TBL *dc_hufftbl = dinfo.dc_huff_tbl_ptrs[huff_tbl_idx];
242                 if (ac_hufftbl == nullptr) {
243                         assert(dc_hufftbl == nullptr);
244                         huff.load_huffman_table[huff_tbl_idx] = 0;
245                 } else {
246                         assert(dc_hufftbl != nullptr);
247                         huff.load_huffman_table[huff_tbl_idx] = 1;
248
249                         for (int i = 0; i < 16; ++i) {
250                                 huff.huffman_table[huff_tbl_idx].num_dc_codes[i] = dc_hufftbl->bits[i + 1];
251                         }
252                         for (int i = 0; i < 12; ++i) {
253                                 huff.huffman_table[huff_tbl_idx].dc_values[i] = dc_hufftbl->huffval[i];
254                         }
255                         for (int i = 0; i < 16; ++i) {
256                                 huff.huffman_table[huff_tbl_idx].num_ac_codes[i] = ac_hufftbl->bits[i + 1];
257                         }
258                         for (int i = 0; i < 162; ++i) {
259                                 huff.huffman_table[huff_tbl_idx].ac_values[i] = ac_hufftbl->huffval[i];
260                         }
261                 }
262         }
263
264         VABufferID huff_buffer;
265         va_status = vaCreateBuffer(va_dpy->va_dpy, resources.context, VAHuffmanTableBufferType, sizeof(huff), 1, &huff, &huff_buffer);
266         CHECK_VASTATUS_RET(va_status, "vaCreateBuffer");
267         VABufferDestroyer destroy_huff(va_dpy->va_dpy, huff_buffer);
268
269         // Slice parameters (metadata about the slice).
270         VASliceParameterBufferJPEGBaseline parms;
271         memset(&parms, 0, sizeof(parms));
272         parms.slice_data_size = dinfo.src->bytes_in_buffer;
273         parms.slice_data_offset = 0;
274         parms.slice_data_flag = VA_SLICE_DATA_FLAG_ALL;
275         parms.slice_horizontal_position = 0;
276         parms.slice_vertical_position = 0;
277         for (int component_idx = 0; component_idx < dinfo.num_components; ++component_idx) {
278                 const jpeg_component_info *comp = &dinfo.comp_info[component_idx];
279                 parms.components[component_idx].component_selector = comp->component_id;
280                 parms.components[component_idx].dc_table_selector = comp->dc_tbl_no;
281                 parms.components[component_idx].ac_table_selector = comp->ac_tbl_no;
282                 if (parms.components[component_idx].dc_table_selector > 1 ||
283                     parms.components[component_idx].ac_table_selector > 1) {
284                         fprintf(stderr, "Uses too many Huffman tables\n");
285                         return nullptr;
286                 }
287         }
288         parms.num_components = dinfo.num_components;
289         parms.restart_interval = dinfo.restart_interval;
290         int horiz_mcus = (dinfo.image_width + (DCTSIZE * 2) - 1) / (DCTSIZE * 2);
291         int vert_mcus = (dinfo.image_height + DCTSIZE - 1) / DCTSIZE;
292         parms.num_mcus = horiz_mcus * vert_mcus;
293
294         VABufferID slice_param_buffer;
295         va_status = vaCreateBuffer(va_dpy->va_dpy, resources.context, VASliceParameterBufferType, sizeof(parms), 1, &parms, &slice_param_buffer);
296         CHECK_VASTATUS_RET(va_status, "vaCreateBuffer");
297         VABufferDestroyer destroy_slice_param(va_dpy->va_dpy, slice_param_buffer);
298
299         // The actual data. VA-API will destuff and all for us.
300         VABufferID data_buffer;
301         va_status = vaCreateBuffer(va_dpy->va_dpy, resources.context, VASliceDataBufferType, dinfo.src->bytes_in_buffer, 1, const_cast<unsigned char *>(dinfo.src->next_input_byte), &data_buffer);
302         CHECK_VASTATUS_RET(va_status, "vaCreateBuffer");
303         VABufferDestroyer destroy_data(va_dpy->va_dpy, data_buffer);
304
305         va_status = vaBeginPicture(va_dpy->va_dpy, resources.context, resources.surface);
306         CHECK_VASTATUS_RET(va_status, "vaBeginPicture");
307         va_status = vaRenderPicture(va_dpy->va_dpy, resources.context, &pic_param_buffer, 1);
308         CHECK_VASTATUS_RET(va_status, "vaRenderPicture(pic_param)");
309         va_status = vaRenderPicture(va_dpy->va_dpy, resources.context, &iq_buffer, 1);
310         CHECK_VASTATUS_RET(va_status, "vaRenderPicture(iq)");
311         va_status = vaRenderPicture(va_dpy->va_dpy, resources.context, &huff_buffer, 1);
312         CHECK_VASTATUS_RET(va_status, "vaRenderPicture(huff)");
313         va_status = vaRenderPicture(va_dpy->va_dpy, resources.context, &slice_param_buffer, 1);
314         CHECK_VASTATUS_RET(va_status, "vaRenderPicture(slice_param)");
315         va_status = vaRenderPicture(va_dpy->va_dpy, resources.context, &data_buffer, 1);
316         CHECK_VASTATUS_RET(va_status, "vaRenderPicture(data)");
317         va_status = vaEndPicture(va_dpy->va_dpy, resources.context);
318         CHECK_VASTATUS_RET(va_status, "vaEndPicture");
319
320         // vaDeriveImage() works, but the resulting image seems to live in
321         // uncached memory, which makes copying data out from it very, very slow.
322         // Thanks to FFmpeg for the observation that you can vaGetImage() the
323         // surface onto your own image (although then, it can't be planar, which
324         // is unfortunate for us).
325 #if 0
326         VAImage image;
327         va_status = vaDeriveImage(va_dpy->va_dpy, surf, &image);
328         CHECK_VASTATUS_RET(va_status, "vaDeriveImage");
329 #else
330         va_status = vaSyncSurface(va_dpy->va_dpy, resources.surface);
331         CHECK_VASTATUS_RET(va_status, "vaSyncSurface");
332
333         va_status = vaGetImage(va_dpy->va_dpy, resources.surface, 0, 0, dinfo.image_width, dinfo.image_height, resources.image.image_id);
334         CHECK_VASTATUS_RET(va_status, "vaGetImage");
335 #endif
336
337         void *mapped;
338         va_status = vaMapBuffer(va_dpy->va_dpy, resources.image.buf, &mapped);
339         CHECK_VASTATUS_RET(va_status, "vaMapBuffer");
340
341         shared_ptr<Frame> frame(new Frame);
342 #if 0
343         // 4:2:2 planar (for vaDeriveImage).
344         frame->y.reset(new uint8_t[dinfo.image_width * dinfo.image_height]);
345         frame->cb.reset(new uint8_t[(dinfo.image_width / 2) * dinfo.image_height]);
346         frame->cr.reset(new uint8_t[(dinfo.image_width / 2) * dinfo.image_height]);
347         for (int component_idx = 0; component_idx < dinfo.num_components; ++component_idx) {
348                 uint8_t *dptr;
349                 size_t width;
350                 if (component_idx == 0) {
351                         dptr = frame->y.get();
352                         width = dinfo.image_width;
353                 } else if (component_idx == 1) {
354                         dptr = frame->cb.get();
355                         width = dinfo.image_width / 2;
356                 } else if (component_idx == 2) {
357                         dptr = frame->cr.get();
358                         width = dinfo.image_width / 2;
359                 } else {
360                         assert(false);
361                 }
362                 const uint8_t *sptr = (const uint8_t *)mapped + image.offsets[component_idx];
363                 size_t spitch = image.pitches[component_idx];
364                 for (size_t y = 0; y < dinfo.image_height; ++y) {
365                         memcpy(dptr + y * width, sptr + y * spitch, width);
366                 }
367         }
368 #else
369         // Convert Y'CbCr to separate Y' and CbCr.
370         frame->is_semiplanar = true;
371
372         PBO pbo = global_pbo_pool->alloc_pbo();
373         size_t cbcr_offset = dinfo.image_width * dinfo.image_height;
374         uint8_t *y_pix = pbo.ptr;
375         uint8_t *cbcr_pix = pbo.ptr + cbcr_offset;
376
377         const uint8_t *src = (const uint8_t *)mapped + resources.image.offsets[0];
378         if (resources.image.pitches[0] == dinfo.image_width * 2) {
379                 memcpy_interleaved(cbcr_pix, y_pix, src, dinfo.image_width * dinfo.image_height * 2);
380         } else {
381                 for (unsigned y = 0; y < dinfo.image_height; ++y) {
382                         memcpy_interleaved(cbcr_pix + y * dinfo.image_width, y_pix + y * dinfo.image_width,
383                                            src + y * resources.image.pitches[0], dinfo.image_width * 2);
384                 }
385         }
386
387         glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo.pbo);
388         frame->y = create_texture_2d(dinfo.image_width, dinfo.image_height, GL_R8, GL_RED, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
389         frame->cbcr = create_texture_2d(dinfo.image_width / 2, dinfo.image_height, GL_RG8, GL_RG, GL_UNSIGNED_BYTE, BUFFER_OFFSET(cbcr_offset));
390         glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
391
392         glFlushMappedNamedBufferRange(pbo.pbo, 0, dinfo.image_width * dinfo.image_height * 2);
393         glMemoryBarrier(GL_PIXEL_BUFFER_BARRIER_BIT);
394         pbo.upload_done = RefCountedGLsync(GL_SYNC_GPU_COMMANDS_COMPLETE, /*flags=*/0);
395         frame->uploaded_ui_thread = pbo.upload_done;
396         frame->uploaded_interpolation = pbo.upload_done;
397         global_pbo_pool->release_pbo(move(pbo));
398 #endif
399         frame->width = dinfo.image_width;
400         frame->height = dinfo.image_height;
401         frame->chroma_subsampling_x = 2;
402         frame->chroma_subsampling_y = 1;
403
404         if (dinfo.marker_list != nullptr &&
405             dinfo.marker_list->marker == JPEG_APP0 + 1 &&
406             dinfo.marker_list->data_length >= 4 &&
407             memcmp(dinfo.marker_list->data, "Exif", 4) == 0) {
408                 frame->exif_data.assign(reinterpret_cast<char *>(dinfo.marker_list->data),
409                         dinfo.marker_list->data_length);
410         }
411
412         va_status = vaUnmapBuffer(va_dpy->va_dpy, resources.image.buf);
413         CHECK_VASTATUS_RET(va_status, "vaUnmapBuffer");
414
415         return frame;
416 }