]> git.sesse.net Git - nageru/blobdiff - vaapi_jpeg_decoder.cpp
Allow symlinked frame files. Useful for testing.
[nageru] / vaapi_jpeg_decoder.cpp
index b6f9c50f487565137c102400723b1ab02e7a3e8e..12db78b194a52075ca8fc578d5af6589698e2df7 100644 (file)
@@ -1,33 +1,33 @@
 #include "vaapi_jpeg_decoder.h"
 
+#include "jpeg_destroyer.h"
+#include "jpeg_frame.h"
+#include "memcpy_interleaved.h"
+
 #include <X11/Xlib.h>
 #include <assert.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <glob.h>
 #include <jpeglib.h>
+#include <list>
+#include <mutex>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <string>
 #include <unistd.h>
 #include <va/va.h>
 #include <va/va_drm.h>
 #include <va/va_x11.h>
 
-#include <list>
-#include <mutex>
-#include <string>
-
-#include "jpeg_frame.h"
-#include "memcpy_interleaved.h"
-
 using namespace std;
 
 static unique_ptr<VADisplayWithCleanup> va_dpy;
 static VAConfigID config_id;
 static VAImageFormat uyvy_format;
 bool vaapi_jpeg_decoding_usable = false;
-       
+
 struct VAResources {
        unsigned width, height;
        VASurfaceID surface;
@@ -49,6 +49,18 @@ static mutex va_resources_mutex;
         return nullptr;                                                 \
     }
 
+// From libjpeg (although it's of course identical between implementations).
+static const int jpeg_natural_order[DCTSIZE2] = {
+        0,  1,  8, 16,  9,  2,  3, 10,
+       17, 24, 32, 25, 18, 11,  4,  5,
+       12, 19, 26, 33, 40, 48, 41, 34,
+       27, 20, 13,  6,  7, 14, 21, 28,
+       35, 42, 49, 56, 57, 50, 43, 36,
+       29, 22, 15, 23, 30, 37, 44, 51,
+       58, 59, 52, 45, 38, 31, 39, 46,
+       53, 60, 61, 54, 47, 55, 62, 63,
+};
+
 VAResources get_va_resources(unsigned width, unsigned height)
 {
        {
@@ -108,7 +120,8 @@ class ReleaseVAResources {
 public:
        ReleaseVAResources(const VAResources &resources)
                : resources(resources) {}
-       ~ReleaseVAResources() {
+       ~ReleaseVAResources()
+       {
                if (!committed) {
                        release_va_resources(resources);
                }
@@ -170,7 +183,8 @@ unique_ptr<VADisplayWithCleanup> try_open_va(const string &va_display, string *e
 {
        unique_ptr<VADisplayWithCleanup> va_dpy = va_open_display(va_display);
        if (va_dpy == nullptr) {
-               if (error) *error = "Opening VA display failed";
+               if (error)
+                       *error = "Opening VA display failed";
                return nullptr;
        }
        int major_ver, minor_ver;
@@ -178,14 +192,16 @@ unique_ptr<VADisplayWithCleanup> try_open_va(const string &va_display, string *e
        if (va_status != VA_STATUS_SUCCESS) {
                char buf[256];
                snprintf(buf, sizeof(buf), "vaInitialize() failed with status %d\n", va_status);
-               if (error != nullptr) *error = buf;
+               if (error != nullptr)
+                       *error = buf;
                return nullptr;
        }
 
        int num_entrypoints = vaMaxNumEntrypoints(va_dpy->va_dpy);
        unique_ptr<VAEntrypoint[]> entrypoints(new VAEntrypoint[num_entrypoints]);
        if (entrypoints == nullptr) {
-               if (error != nullptr) *error = "Failed to allocate memory for VA entry points";
+               if (error != nullptr)
+                       *error = "Failed to allocate memory for VA entry points";
                return nullptr;
        }
 
@@ -199,7 +215,8 @@ unique_ptr<VADisplayWithCleanup> try_open_va(const string &va_display, string *e
                return va_dpy;
        }
 
-       if (error != nullptr) *error = "Can't find VAEntrypointVLD for the JPEG profile";
+       if (error != nullptr)
+               *error = "Can't find VAEntrypointVLD for the JPEG profile";
        return nullptr;
 }
 
@@ -294,31 +311,32 @@ void init_jpeg_vaapi()
        vaapi_jpeg_decoding_usable = true;
 }
 
-shared_ptr<Frame> decode_jpeg_vaapi(const string &filename)
+class VABufferDestroyer {
+public:
+       VABufferDestroyer(VADisplay dpy, VABufferID buf)
+               : dpy(dpy), buf(buf) {}
+
+       ~VABufferDestroyer() {
+               VAStatus va_status = vaDestroyBuffer(dpy, buf);
+               CHECK_VASTATUS(va_status, "vaDestroyBuffer");
+       }
+
+private:
+       VADisplay dpy;
+       VABufferID buf;
+};
+
+shared_ptr<Frame> decode_jpeg_vaapi(const string &jpeg)
 {
        jpeg_decompress_struct dinfo;
        jpeg_error_mgr jerr;
        dinfo.err = jpeg_std_error(&jerr);
        jpeg_create_decompress(&dinfo);
+       JPEGDestroyer destroy_dinfo(&dinfo);
 
-       FILE *fp = fopen(filename.c_str(), "rb");
-       if (fp == nullptr) {
-               perror(filename.c_str());
-               exit(1);
-       }
-       jpeg_stdio_src(&dinfo, fp);
-
+       jpeg_mem_src(&dinfo, reinterpret_cast<const unsigned char *>(jpeg.data()), jpeg.size());
        jpeg_read_header(&dinfo, true);
 
-       // Read the data that comes after the header. VA-API will destuff and all for us.
-       std::string str((const char *)dinfo.src->next_input_byte, dinfo.src->bytes_in_buffer);
-       while (!feof(fp)) {
-               char buf[4096];
-               size_t ret = fread(buf, 1, sizeof(buf), fp);
-               str.append(buf, ret);
-       }
-       fclose(fp);
-
        if (dinfo.num_components != 3) {
                fprintf(stderr, "Not a color JPEG. (%d components, Y=%dx%d, Cb=%dx%d, Cr=%dx%d)\n",
                        dinfo.num_components,
@@ -328,11 +346,10 @@ shared_ptr<Frame> decode_jpeg_vaapi(const string &filename)
                return nullptr;
        }
        if (dinfo.comp_info[0].h_samp_factor != 2 ||
-           dinfo.comp_info[0].v_samp_factor != 2 ||
            dinfo.comp_info[1].h_samp_factor != 1 ||
-           dinfo.comp_info[1].v_samp_factor != 2 ||
+           dinfo.comp_info[1].v_samp_factor != dinfo.comp_info[0].v_samp_factor ||
            dinfo.comp_info[2].h_samp_factor != 1 ||
-           dinfo.comp_info[2].v_samp_factor != 2) {
+           dinfo.comp_info[2].v_samp_factor != dinfo.comp_info[0].v_samp_factor) {
                fprintf(stderr, "Not 4:2:2. (Y=%dx%d, Cb=%dx%d, Cr=%dx%d)\n",
                        dinfo.comp_info[0].h_samp_factor, dinfo.comp_info[0].v_samp_factor,
                        dinfo.comp_info[1].h_samp_factor, dinfo.comp_info[1].v_samp_factor,
@@ -359,6 +376,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;
@@ -375,7 +393,7 @@ shared_ptr<Frame> decode_jpeg_vaapi(const string &filename)
                                        fprintf(stderr, "Baseline JPEG only!\n");
                                        return nullptr;
                                }
-                               iq.quantiser_table[quant_tbl_idx][i] = qtbl->quantval[i];
+                               iq.quantiser_table[quant_tbl_idx][i] = qtbl->quantval[jpeg_natural_order[i]];
                        }
                }
        }
@@ -383,6 +401,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;
@@ -416,13 +435,14 @@ 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;
        memset(&parms, 0, sizeof(parms));
-       parms.slice_data_size = str.size();
+       parms.slice_data_size = dinfo.src->bytes_in_buffer;
        parms.slice_data_offset = 0;
-       parms.slice_data_flag = VA_SLICE_DATA_FLAG_ALL; 
+       parms.slice_data_flag = VA_SLICE_DATA_FLAG_ALL;
        parms.slice_horizontal_position = 0;
        parms.slice_vertical_position = 0;
        for (int component_idx = 0; component_idx < dinfo.num_components; ++component_idx) {
@@ -431,7 +451,7 @@ shared_ptr<Frame> decode_jpeg_vaapi(const string &filename)
                parms.components[component_idx].dc_table_selector = comp->dc_tbl_no;
                parms.components[component_idx].ac_table_selector = comp->ac_tbl_no;
                if (parms.components[component_idx].dc_table_selector > 1 ||
-                   parms.components[component_idx].ac_table_selector > 1) {
+                   parms.components[component_idx].ac_table_selector > 1) {
                        fprintf(stderr, "Uses too many Huffman tables\n");
                        return nullptr;
                }
@@ -445,11 +465,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.
+       // The actual data. VA-API will destuff and all for us.
        VABufferID data_buffer;
-       va_status = vaCreateBuffer(va_dpy->va_dpy, config_id, VASliceDataBufferType, str.size(), 1, &str[0], &data_buffer);
+       va_status = vaCreateBuffer(va_dpy->va_dpy, config_id, VASliceDataBufferType, dinfo.src->bytes_in_buffer, 1, const_cast<unsigned char *>(dinfo.src->next_input_byte), &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);