]> git.sesse.net Git - nageru/blob - nageru/pbo_frame_allocator.cpp
When uploading MJPEG data to VA-API, do it directly into the buffer.
[nageru] / nageru / pbo_frame_allocator.cpp
1 #include "pbo_frame_allocator.h"
2
3 #include <bmusb/bmusb.h>
4 #include <movit/util.h>
5 #include <stdbool.h>
6 #include <stdint.h>
7 #include <stdio.h>
8 #include <cstddef>
9
10 #include "flags.h"
11 #include "mjpeg_encoder.h"
12 #include "v210_converter.h"
13 #include "va_display_with_cleanup.h"
14
15 using namespace std;
16
17 namespace {
18
19 void set_clamp_to_edge()
20 {
21         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
22         check_error();
23         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
24         check_error();
25         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
26         check_error();
27 }
28
29 }  // namespace
30
31 PBOFrameAllocator::PBOFrameAllocator(bmusb::PixelFormat pixel_format, size_t frame_size, GLuint width, GLuint height, unsigned card_index, MJPEGEncoder *mjpeg_encoder, size_t num_queued_frames, GLenum buffer, GLenum permissions, GLenum map_bits)
32         : card_index(card_index), mjpeg_encoder(mjpeg_encoder), pixel_format(pixel_format), buffer(buffer)
33 {
34         userdata.reset(new Userdata[num_queued_frames]);
35         for (size_t i = 0; i < num_queued_frames; ++i) {
36                 init_frame(i, frame_size, width, height, permissions, map_bits);
37         }
38         glBindBuffer(buffer, 0);
39         check_error();
40         glBindTexture(GL_TEXTURE_2D, 0);
41         check_error();
42 }
43
44 void PBOFrameAllocator::init_frame(size_t frame_idx, size_t frame_size, GLuint width, GLuint height, GLenum permissions, GLenum map_bits)
45 {
46         GLuint pbo;
47         glGenBuffers(1, &pbo);
48         check_error();
49         glBindBuffer(buffer, pbo);
50         check_error();
51         glBufferStorage(buffer, frame_size, nullptr, permissions | GL_MAP_PERSISTENT_BIT);
52         check_error();
53
54         Frame frame;
55         frame.data = (uint8_t *)glMapBufferRange(buffer, 0, frame_size, permissions | map_bits | GL_MAP_PERSISTENT_BIT);
56         frame.data2 = frame.data + frame_size / 2;
57         check_error();
58         frame.size = frame_size;
59         Userdata *ud = &userdata[frame_idx];
60         frame.userdata = ud;
61         ud->pbo = pbo;
62         ud->pixel_format = pixel_format;
63         ud->data_copy_malloc = new uint8_t[frame_size];
64         frame.owner = this;
65
66         // For 8-bit non-planar Y'CbCr, we ask the driver to split Y' and Cb/Cr
67         // into separate textures. For 10-bit, the input format (v210)
68         // is complicated enough that we need to interpolate up to 4:4:4,
69         // which we do in a compute shader ourselves. For BGRA, the data
70         // is already 4:4:4:4.
71         frame.interleaved = (pixel_format == bmusb::PixelFormat_8BitYCbCr);
72
73         // Create textures. We don't allocate any data for the second field at this point
74         // (just create the texture state with the samplers), since our default assumed
75         // resolution is progressive.
76         switch (pixel_format) {
77         case bmusb::PixelFormat_8BitYCbCr:
78                 glGenTextures(2, ud->tex_y);
79                 check_error();
80                 glGenTextures(2, ud->tex_cbcr);
81                 check_error();
82                 break;
83         case bmusb::PixelFormat_10BitYCbCr:
84                 glGenTextures(2, ud->tex_v210);
85                 check_error();
86                 glGenTextures(2, ud->tex_444);
87                 check_error();
88                 break;
89         case bmusb::PixelFormat_8BitBGRA:
90                 glGenTextures(2, ud->tex_rgba);
91                 check_error();
92                 break;
93         case bmusb::PixelFormat_8BitYCbCrPlanar:
94                 glGenTextures(2, ud->tex_y);
95                 check_error();
96                 glGenTextures(2, ud->tex_cb);
97                 check_error();
98                 glGenTextures(2, ud->tex_cr);
99                 check_error();
100                 break;
101         default:
102                 assert(false);
103         }
104
105         ud->last_width[0] = width;
106         ud->last_height[0] = height;
107         ud->last_cbcr_width[0] = width / 2;
108         ud->last_cbcr_height[0] = height;
109         ud->last_v210_width[0] = 0;
110
111         ud->last_width[1] = 0;
112         ud->last_height[1] = 0;
113         ud->last_cbcr_width[1] = 0;
114         ud->last_cbcr_height[1] = 0;
115         ud->last_v210_width[1] = 0;
116
117         ud->last_interlaced = false;
118         ud->last_has_signal = false;
119         ud->last_is_connected = false;
120         for (unsigned field = 0; field < 2; ++field) {
121                 switch (pixel_format) {
122                 case bmusb::PixelFormat_10BitYCbCr: {
123                         const size_t v210_width = v210Converter::get_minimum_v210_texture_width(width);
124
125                         // Seemingly we need to set the minification filter even though
126                         // shader image loads don't use them, or NVIDIA will just give us
127                         // zero back.
128                         glBindTexture(GL_TEXTURE_2D, ud->tex_v210[field]);
129                         check_error();
130                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
131                         check_error();
132                         if (field == 0) {
133                                 ud->last_v210_width[0] = v210_width;
134                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, v210_width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, nullptr);
135                                 check_error();
136                         }
137
138                         glBindTexture(GL_TEXTURE_2D, ud->tex_444[field]);
139                         check_error();
140                         set_clamp_to_edge();
141                         if (field == 0) {
142                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, nullptr);
143                                 check_error();
144                         }
145                         break;
146                 }
147                 case bmusb::PixelFormat_8BitYCbCr:
148                         glBindTexture(GL_TEXTURE_2D, ud->tex_y[field]);
149                         check_error();
150                         set_clamp_to_edge();
151                         if (field == 0) {
152                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
153                                 check_error();
154                         }
155
156                         glBindTexture(GL_TEXTURE_2D, ud->tex_cbcr[field]);
157                         check_error();
158                         set_clamp_to_edge();
159                         if (field == 0) {
160                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, width / 2, height, 0, GL_RG, GL_UNSIGNED_BYTE, nullptr);
161                                 check_error();
162                         }
163                         break;
164                 case bmusb::PixelFormat_8BitBGRA:
165                         glBindTexture(GL_TEXTURE_2D, ud->tex_rgba[field]);
166                         check_error();
167                         set_clamp_to_edge();
168                         if (field == 0) {
169                                 if (global_flags.can_disable_srgb_decoder) {  // See the comments in tweaked_inputs.h.
170                                         glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8_ALPHA8, width, height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, nullptr);
171                                 } else {
172                                         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, nullptr);
173                                 }
174                                 check_error();
175                         }
176                         break;
177                 case bmusb::PixelFormat_8BitYCbCrPlanar:
178                         glBindTexture(GL_TEXTURE_2D, ud->tex_y[field]);
179                         check_error();
180                         set_clamp_to_edge();
181                         if (field == 0) {
182                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
183                                 check_error();
184                         }
185
186                         glBindTexture(GL_TEXTURE_2D, ud->tex_cb[field]);
187                         check_error();
188                         set_clamp_to_edge();
189                         if (field == 0) {
190                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width / 2, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
191                                 check_error();
192                         }
193
194                         glBindTexture(GL_TEXTURE_2D, ud->tex_cr[field]);
195                         check_error();
196                         set_clamp_to_edge();
197                         if (field == 0) {
198                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width / 2, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
199                                 check_error();
200                         }
201                         break;
202                 default:
203                         assert(false);
204                 }
205         }
206
207         freelist.push(frame);
208 }
209
210 PBOFrameAllocator::~PBOFrameAllocator()
211 {
212         while (!freelist.empty()) {
213                 Frame frame = freelist.front();
214                 freelist.pop();
215                 destroy_frame(&frame);
216         }
217 }
218
219 void PBOFrameAllocator::destroy_frame(Frame *frame)
220 {
221         Userdata *ud = (Userdata *)frame->userdata;
222         delete[] ud->data_copy_malloc;
223
224         GLuint pbo = ud->pbo;
225         glBindBuffer(buffer, pbo);
226         check_error();
227         glUnmapBuffer(buffer);
228         check_error();
229         glBindBuffer(buffer, 0);
230         check_error();
231         glDeleteBuffers(1, &pbo);
232         check_error();
233         switch (pixel_format) {
234         case bmusb::PixelFormat_10BitYCbCr:
235                 glDeleteTextures(2, ud->tex_v210);
236                 check_error();
237                 glDeleteTextures(2, ud->tex_444);
238                 check_error();
239                 break;
240         case bmusb::PixelFormat_8BitYCbCr:
241                 glDeleteTextures(2, ud->tex_y);
242                 check_error();
243                 glDeleteTextures(2, ud->tex_cbcr);
244                 check_error();
245                 break;
246         case bmusb::PixelFormat_8BitBGRA:
247                 glDeleteTextures(2, ud->tex_rgba);
248                 check_error();
249                 break;
250         case bmusb::PixelFormat_8BitYCbCrPlanar:
251                 glDeleteTextures(2, ud->tex_y);
252                 check_error();
253                 glDeleteTextures(2, ud->tex_cb);
254                 check_error();
255                 glDeleteTextures(2, ud->tex_cr);
256                 check_error();
257                 break;
258         default:
259                 assert(false);
260         }
261 }
262 //static int sumsum = 0;
263
264 bmusb::FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
265 {
266         Frame vf;
267
268         lock_guard<mutex> lock(freelist_mutex);  // Meh.
269         if (freelist.empty()) {
270                 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
271         } else {
272                 //fprintf(stderr, "freelist has %d allocated\n", ++sumsum);
273                 vf = freelist.front();
274                 freelist.pop();  // Meh.
275         }
276         vf.len = 0;
277         vf.overflow = 0;
278
279         if (mjpeg_encoder != nullptr && mjpeg_encoder->using_vaapi() &&
280             mjpeg_encoder->get_mjpeg_stream_for_card(card_index) != -1) {
281                 Userdata *ud = (Userdata *)vf.userdata;
282                 vf.data_copy = ud->data_copy_malloc;
283                 ud->data_copy_current_src = Userdata::FROM_MALLOC;
284         } else {
285                 vf.data_copy = nullptr;
286         }
287
288         return vf;
289 }
290
291 bmusb::FrameAllocator::Frame PBOFrameAllocator::create_frame(size_t width, size_t height, size_t stride)
292 {
293         Frame vf;
294
295         {
296                 lock_guard<mutex> lock(freelist_mutex);
297                 if (freelist.empty()) {
298                         printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
299                         vf.len = 0;
300                         vf.overflow = 0;
301                         return vf;
302                 } else {
303                         vf = freelist.front();
304                         freelist.pop();
305                 }
306         }
307         vf.len = 0;
308         vf.overflow = 0;
309
310         Userdata *userdata = (Userdata *)vf.userdata;
311
312         if (mjpeg_encoder != nullptr && mjpeg_encoder->using_vaapi() &&
313             mjpeg_encoder->get_mjpeg_stream_for_card(card_index) != -1) {
314                 VADisplay va_dpy = mjpeg_encoder->va_dpy->va_dpy;
315                 MJPEGEncoder::VAResources resources = mjpeg_encoder->get_va_resources(width, height);
316                 MJPEGEncoder::ReleaseVAResources release(mjpeg_encoder, resources);
317
318                 VAImage image;
319                 VAStatus va_status = vaDeriveImage(va_dpy, resources.surface, &image);
320                 CHECK_VASTATUS(va_status, "vaDeriveImage");
321
322                 if (image.pitches[0] == stride) {
323                         userdata->va_resources = move(resources);
324                         userdata->va_resources_release = move(release);
325                         userdata->va_image = move(image);
326
327                         va_status = vaMapBuffer(va_dpy, image.buf, (void **)&vf.data_copy);
328                         CHECK_VASTATUS(va_status, "vaMapBuffer");
329                         vf.data_copy += image.offsets[0];
330                         userdata->data_copy_current_src = Userdata::FROM_VA_API;
331                 } else {
332                         printf("WARNING: Could not copy directly into VA-API MJPEG buffer for %zu x %zu, since producer and consumer disagreed on stride (%zu != %d).\n", width, height, stride, image.pitches[0]);
333                         vf.data_copy = userdata->data_copy_malloc;
334                         userdata->data_copy_current_src = Userdata::FROM_MALLOC;
335
336                         va_status = vaDestroyImage(va_dpy, image.image_id);
337                         CHECK_VASTATUS(va_status, "vaDestroyImage");
338                 }
339         } else {
340                 vf.data_copy = nullptr;
341         }
342
343         return vf;
344 }
345
346 void PBOFrameAllocator::release_frame(Frame frame)
347 {
348         if (frame.overflow > 0) {
349                 printf("%d bytes overflow after last (PBO) frame\n", int(frame.overflow));
350         }
351
352 #if 0
353         // Poison the page. (Note that this might be bogus if you don't have an OpenGL context.)
354         memset(frame.data, 0, frame.size);
355         Userdata *userdata = (Userdata *)frame.userdata;
356         for (unsigned field = 0; field < 2; ++field) {
357                 glBindTexture(GL_TEXTURE_2D, userdata->tex_y[field]);
358                 check_error();
359                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
360                 check_error();
361                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
362                 check_error();
363                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
364                 check_error();
365                 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, userdata->last_width[field], userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
366                 check_error();
367
368                 glBindTexture(GL_TEXTURE_2D, userdata->tex_cbcr[field]);
369                 check_error();
370                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
371                 check_error();
372                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
373                 check_error();
374                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
375                 check_error();
376                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, userdata->last_width[field] / 2, userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
377                 check_error();
378         }
379 #endif
380
381         lock_guard<mutex> lock(freelist_mutex);
382         freelist.push(frame);
383         //--sumsum;
384 }