]> git.sesse.net Git - nageru/commitdiff
Stop leaking VA-API buffers.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 21 Oct 2018 13:33:12 +0000 (15:33 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 21 Oct 2018 13:33:12 +0000 (15:33 +0200)
vaapi_jpeg_decoder.cpp

index ae1febc041d355cf53deba1b0f1bc601c83799c4..0682103126281fcfeeb70b45a11bb5efd766fb4d 100644 (file)
@@ -299,6 +299,20 @@ void init_jpeg_vaapi()
        vaapi_jpeg_decoding_usable = true;
 }
 
+class VABufferDestroyer {
+public:
+       VABufferDestroyer(VADisplay *dpy, VABufferID buf)
+               : dpy(dpy), buf(buf) {}
+
+       ~VABufferDestroyer() {
+               vaDestroyBuffer(dpy, buf);
+       }
+
+private:
+       VADisplay *dpy;
+       VABufferID buf;
+};
+
 shared_ptr<Frame> decode_jpeg_vaapi(const string &filename)
 {
        jpeg_decompress_struct dinfo;
@@ -364,6 +378,7 @@ shared_ptr<Frame> decode_jpeg_vaapi(const string &filename)
        VABufferID pic_param_buffer;
        VAStatus va_status = vaCreateBuffer(va_dpy->va_dpy, config_id, VAPictureParameterBufferType, sizeof(pic_param), 1, &pic_param, &pic_param_buffer);
        CHECK_VASTATUS_RET(va_status, "vaCreateBuffer");
+       VABufferDestroyer destroy_pic_param(&va_dpy->va_dpy, pic_param_buffer);
 
        // Quantization matrices.
        VAIQMatrixBufferJPEGBaseline iq;
@@ -388,6 +403,7 @@ shared_ptr<Frame> decode_jpeg_vaapi(const string &filename)
        VABufferID iq_buffer;
        va_status = vaCreateBuffer(va_dpy->va_dpy, config_id, VAIQMatrixBufferType, sizeof(iq), 1, &iq, &iq_buffer);
        CHECK_VASTATUS_RET(va_status, "vaCreateBuffer");
+       VABufferDestroyer destroy_iq(&va_dpy->va_dpy, iq_buffer);
 
        // Huffman tables (arithmetic is not supported).
        VAHuffmanTableBufferJPEGBaseline huff;
@@ -421,6 +437,7 @@ shared_ptr<Frame> decode_jpeg_vaapi(const string &filename)
        VABufferID huff_buffer;
        va_status = vaCreateBuffer(va_dpy->va_dpy, config_id, VAHuffmanTableBufferType, sizeof(huff), 1, &huff, &huff_buffer);
        CHECK_VASTATUS_RET(va_status, "vaCreateBuffer");
+       VABufferDestroyer destroy_huff(&va_dpy->va_dpy, huff_buffer);
 
        // Slice parameters (metadata about the slice).
        VASliceParameterBufferJPEGBaseline parms;
@@ -450,11 +467,13 @@ shared_ptr<Frame> decode_jpeg_vaapi(const string &filename)
        VABufferID slice_param_buffer;
        va_status = vaCreateBuffer(va_dpy->va_dpy, config_id, VASliceParameterBufferType, sizeof(parms), 1, &parms, &slice_param_buffer);
        CHECK_VASTATUS_RET(va_status, "vaCreateBuffer");
+       VABufferDestroyer destroy_slice_param(&va_dpy->va_dpy, slice_param_buffer);
 
        // The actual data.
        VABufferID data_buffer;
        va_status = vaCreateBuffer(va_dpy->va_dpy, config_id, VASliceDataBufferType, str.size(), 1, &str[0], &data_buffer);
        CHECK_VASTATUS_RET(va_status, "vaCreateBuffer");
+       VABufferDestroyer destroy_data(&va_dpy->va_dpy, data_buffer);
 
        VAResources resources = get_va_resources(dinfo.image_width, dinfo.image_height);
        ReleaseVAResources release(resources);