avcodec/mlpdsp: Fix runtime error: signed integer overflow: -24419392 * 128 cannot...
[ffmpeg.git] / cmdutils_opencl.c
index 2a04db9..906aef4 100644 (file)
@@ -22,6 +22,7 @@
 #include "libavutil/time.h"
 #include "libavutil/log.h"
 #include "libavutil/opencl.h"
+#include "libavutil/avstring.h"
 #include "cmdutils.h"
 
 typedef struct {
@@ -128,11 +129,11 @@ static int64_t run_opencl_bench(AVOpenCLExternalEnv *ext_opencl_env)
     cl_int status;
     size_t kernel_len;
     char *inbuf;
-    int *mask;
+    int *mask = NULL;
     int buf_size = width * height * sizeof(char);
     int mask_size = sizeof(uint32_t) * 128;
 
-    cl_mem cl_mask, cl_inbuf, cl_outbuf;
+    cl_mem cl_mask = NULL, cl_inbuf = NULL, cl_outbuf = NULL;
     cl_kernel kernel = NULL;
     cl_program program = NULL;
     size_t local_work_size_2d[2] = {16, 16};
@@ -181,12 +182,12 @@ static int64_t run_opencl_bench(AVOpenCLExternalEnv *ext_opencl_env)
     OCLCHECK(clSetKernelArg, kernel, arg++, sizeof(cl_int), &width);
     OCLCHECK(clSetKernelArg, kernel, arg++, sizeof(cl_int), &height);
 
-    start = av_gettime();
+    start = av_gettime_relative();
     for (i = 0; i < OPENCL_NB_ITER; i++)
         OCLCHECK(clEnqueueNDRangeKernel, ext_opencl_env->command_queue, kernel, 2, NULL,
                  global_work_size_2d, local_work_size_2d, 0, NULL, NULL);
     clFinish(ext_opencl_env->command_queue);
-    ret = (av_gettime() - start)/OPENCL_NB_ITER;
+    ret = (av_gettime_relative() - start)/OPENCL_NB_ITER;
 end:
     if (kernel)
         clReleaseKernel(kernel);
@@ -205,27 +206,34 @@ end:
 
 static int compare_ocl_device_desc(const void *a, const void *b)
 {
-    return ((OpenCLDeviceBenchmark*)a)->runtime - ((OpenCLDeviceBenchmark*)b)->runtime;
+    const OpenCLDeviceBenchmark* va = (const OpenCLDeviceBenchmark*)a;
+    const OpenCLDeviceBenchmark* vb = (const OpenCLDeviceBenchmark*)b;
+    return FFDIFFSIGN(va->runtime , vb->runtime);
 }
 
 int opt_opencl_bench(void *optctx, const char *opt, const char *arg)
 {
-    int i, j, nb_devices = 0, count = 0;
+    int i, j, nb_devices = 0, count = 0, ret = 0;
     int64_t score = 0;
     AVOpenCLDeviceList *device_list;
     AVOpenCLDeviceNode *device_node = NULL;
     OpenCLDeviceBenchmark *devices = NULL;
     cl_platform_id platform;
 
-    av_opencl_get_device_list(&device_list);
+    ret = av_opencl_get_device_list(&device_list);
+    if (ret < 0) {
+        return ret;
+    }
     for (i = 0; i < device_list->platform_num; i++)
         nb_devices += device_list->platform_node[i]->device_num;
     if (!nb_devices) {
         av_log(NULL, AV_LOG_ERROR, "No OpenCL device detected!\n");
+        av_opencl_free_device_list(&device_list);
         return AVERROR(EINVAL);
     }
-    if (!(devices = av_malloc(sizeof(OpenCLDeviceBenchmark) * nb_devices))) {
+    if (!(devices = av_malloc_array(nb_devices, sizeof(OpenCLDeviceBenchmark)))) {
         av_log(NULL, AV_LOG_ERROR, "Could not allocate buffer\n");
+        av_opencl_free_device_list(&device_list);
         return AVERROR(ENOMEM);
     }
 
@@ -238,7 +246,8 @@ int opt_opencl_bench(void *optctx, const char *opt, const char *arg)
                 devices[count].platform_idx = i;
                 devices[count].device_idx = j;
                 devices[count].runtime = score;
-                strcpy(devices[count].device_name, device_node->device_name);
+                av_strlcpy(devices[count].device_name, device_node->device_name,
+                           sizeof(devices[count].device_name));
                 count++;
             }
         }