aboutsummaryrefslogtreecommitdiff
path: root/modules/ocl/cl_utils.cpp
blob: 87d440258769b74212c7b1954d7b47152c438852 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
 * cl_utils.cpp - CL Utilities
 *
 *  Copyright (c) 2016 Intel Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author: Wind Yuan <feng.yuan@intel.com>
 */

#include "cl_utils.h"
#include "image_file_handle.h"

namespace XCam {

struct NV12Pixel {
    float x_pos;
    float y_pos;

    float y;
    float u;
    float v;

    NV12Pixel ()
        : x_pos (0.0f), y_pos (0.0f)
        , y (0.0f), u (0.0f), v (0.0f)
    {}
};

static inline void
clamp (float &value, float min, float max)
{
    value = (value < min) ? min : ((value > max) ? max : value);
}

bool
dump_image (SmartPtr<CLImage> image, const char *file_name)
{
    XCAM_ASSERT (file_name);

    const CLImageDesc &desc = image->get_image_desc ();
    void *ptr = NULL;
    size_t origin[3] = {0, 0, 0};
    size_t region[3] = {desc.width, desc.height, 1};
    size_t row_pitch;
    size_t slice_pitch;

    XCamReturn ret = image->enqueue_map (ptr, origin, region, &row_pitch, &slice_pitch, CL_MAP_READ);
    XCAM_ASSERT (ret == XCAM_RETURN_NO_ERROR);
    XCAM_ASSERT (ptr);
    XCAM_ASSERT (row_pitch == desc.row_pitch);
    uint8_t *buf_start = (uint8_t *)ptr;
    uint32_t width = image->get_pixel_bytes () * desc.width;

    FILE *fp = fopen (file_name, "wb");
    XCAM_FAIL_RETURN (ERROR, fp, false, "open file(%s) failed", file_name);

    for (uint32_t i = 0; i < desc.height; ++i) {
        uint8_t *buf_line = buf_start + row_pitch * i;
        fwrite (buf_line, width, 1, fp);
    }
    image->enqueue_unmap (ptr);
    fclose (fp);
    XCAM_LOG_INFO ("write image:%s\n", file_name);
    return true;
}

SmartPtr<CLBuffer>
convert_to_clbuffer (
    const SmartPtr<CLContext> &context,
    const SmartPtr<VideoBuffer> &buf)
{
    SmartPtr<CLBuffer> cl_buf;

    SmartPtr<CLVideoBuffer> cl_video_buf = buf.dynamic_cast_ptr<CLVideoBuffer> ();
    if (cl_video_buf.ptr ()) {
        cl_buf = cl_video_buf;
    }
#if HAVE_LIBDRM
    else {
        SmartPtr<DrmBoBuffer> bo_buf = buf.dynamic_cast_ptr<DrmBoBuffer> ();
        cl_buf = new CLVaBuffer (context, bo_buf);
    }
#else
    XCAM_UNUSED (context);
#endif

    XCAM_FAIL_RETURN (WARNING, cl_buf.ptr (), NULL, "convert to clbuffer failed");
    return cl_buf;
}

SmartPtr<CLImage>
convert_to_climage (
    const SmartPtr<CLContext> &context,
    SmartPtr<VideoBuffer> &buf,
    const CLImageDesc &desc,
    uint32_t offset,
    cl_mem_flags flags)
{
    SmartPtr<CLImage> cl_image;

    SmartPtr<CLVideoBuffer> cl_video_buf = buf.dynamic_cast_ptr<CLVideoBuffer> ();
    if (cl_video_buf.ptr ()) {
        SmartPtr<CLBuffer> cl_buf;

        if (offset == 0) {
            cl_buf = cl_video_buf;
        } else {
            uint32_t row_pitch = CLImage::calculate_pixel_bytes (desc.format) *
                                 XCAM_ALIGN_UP (desc.width, XCAM_CL_IMAGE_ALIGNMENT_X);
            uint32_t size = row_pitch * desc.height;

            cl_buf = new CLSubBuffer (context, cl_video_buf, flags, offset, size);
        }

        cl_image = new CLImage2D (context, desc, flags, cl_buf);
    }
#if HAVE_LIBDRM
    else {
        SmartPtr<DrmBoBuffer> bo_buf = buf.dynamic_cast_ptr<DrmBoBuffer> ();
        cl_image = new CLVaImage (context, bo_buf, desc, offset);
    }
#endif

    XCAM_FAIL_RETURN (WARNING, cl_image.ptr (), NULL, "convert to climage failed");
    return cl_image;
}

XCamReturn
convert_nv12_mem_to_video_buffer (
    void *nv12_mem, uint32_t width, uint32_t height, uint32_t row_pitch, uint32_t offset_uv,
    SmartPtr<VideoBuffer> &buf)
{
    XCAM_ASSERT (nv12_mem);
    XCAM_ASSERT (row_pitch >= width);

    VideoBufferPlanarInfo planar;
    const VideoBufferInfo info = buf->get_video_info ();
    XCAM_ASSERT ((width == info.width) && (height == info.height));

    uint8_t *out_mem = buf->map ();
    XCAM_FAIL_RETURN (ERROR, out_mem, XCAM_RETURN_ERROR_MEM, "map buffer failed");

    uint8_t *src = (uint8_t *)nv12_mem;
    uint8_t *dest = NULL;
    for (uint32_t index = 0; index < info.components; index++) {
        info.get_planar_info (planar, index);

        dest = out_mem + info.offsets[index];
        for (uint32_t i = 0; i < planar.height; i++) {
            memcpy (dest, src, width);
            src += row_pitch;
            dest += info.strides[index];
        }

        src = (uint8_t *)nv12_mem + offset_uv;
    }

    buf->unmap ();
    return XCAM_RETURN_NO_ERROR;
}

XCamReturn
interpolate_pixel_value (
    uint8_t* stitch_mem,
    float image_coord_x, float image_coord_y,
    float &y, float &u, float &v,
    const VideoBufferInfo& stitch_info)
{
    XCAM_ASSERT (image_coord_y < stitch_info.height && image_coord_x < stitch_info.width);

    uint8_t y00, y01, y10, y11;
    uint8_t u00, u01, u10, u11;
    uint8_t v00, v01, v10, v11;

    uint32_t x0 = (uint32_t) image_coord_x;
    uint32_t x1 = (x0 < stitch_info.width - 1) ? (x0 + 1) : x0;
    uint32_t y0 = (uint32_t) image_coord_y;
    uint32_t y1 = (y0 < stitch_info.height - 1) ? (y0 + 1) : y0;

    float rate00 = (x0 + 1 - image_coord_x) * (y0 + 1 - image_coord_y);
    float rate01 = (x0 + 1 - image_coord_x) * (image_coord_y - y0);
    float rate10 = (image_coord_x - x0) * (y0 + 1 - image_coord_y);
    float rate11 = (image_coord_x - x0) * (image_coord_y - y0);

    y00 = stitch_mem[y0 * stitch_info.strides[0] + x0];
    y01 = stitch_mem[y1 * stitch_info.strides[0] + x0];
    y10 = stitch_mem[y0 * stitch_info.strides[0] + x1];
    y11 = stitch_mem[y1 * stitch_info.strides[0] + x1];

    u00 = stitch_mem[stitch_info.offsets[1] + y0 / 2 * stitch_info.strides[1] + XCAM_ALIGN_DOWN (x0, 2)];
    u01 = stitch_mem[stitch_info.offsets[1] + y1 / 2 * stitch_info.strides[1] + XCAM_ALIGN_DOWN (x0, 2)];
    u10 = stitch_mem[stitch_info.offsets[1] + y0 / 2 * stitch_info.strides[1] + XCAM_ALIGN_DOWN (x1, 2)];
    u11 = stitch_mem[stitch_info.offsets[1] + y1 / 2 * stitch_info.strides[1] + XCAM_ALIGN_DOWN (x1, 2)];

    v00 = stitch_mem[stitch_info.offsets[1] + y0 / 2 * stitch_info.strides[1] + XCAM_ALIGN_DOWN (x0, 2) + 1];
    v01 = stitch_mem[stitch_info.offsets[1] + y1 / 2 * stitch_info.strides[1] + XCAM_ALIGN_DOWN (x0, 2) + 1];
    v10 = stitch_mem[stitch_info.offsets[1] + y0 / 2 * stitch_info.strides[1] + XCAM_ALIGN_DOWN (x1, 2) + 1];
    v11 = stitch_mem[stitch_info.offsets[1] + y1 / 2 * stitch_info.strides[1] + XCAM_ALIGN_DOWN (x1, 2) + 1];

    y = y00 * rate00 + y01 * rate01 + y10 * rate10 + y11 * rate11;
    u = u00 * rate00 + u01 * rate01 + u10 * rate10 + u11 * rate11;
    v = v00 * rate00 + v01 * rate01 + v10 * rate10 + v11 * rate11;

    return XCAM_RETURN_NO_ERROR;
}

XCamReturn
map_to_specific_view (
    uint8_t *specific_view_mem, uint8_t* stitch_mem,
    uint32_t row, uint32_t col,
    float image_coord_x, float image_coord_y,
    const VideoBufferInfo& specific_view_info, const VideoBufferInfo& stitch_info)
{
    XCAM_ASSERT (row < specific_view_info.height && col < specific_view_info.width);

    float y, u, v;

    interpolate_pixel_value (stitch_mem, image_coord_x, image_coord_y, y, u, v, stitch_info);

    uint32_t y_index = row * specific_view_info.strides[0] + col;
    uint32_t u_index = specific_view_info.offsets[1] + row / 2 * specific_view_info.strides[1] + XCAM_ALIGN_DOWN (col, 2);

    specific_view_mem[y_index] = (uint8_t)y;
    specific_view_mem[u_index] = (uint8_t)u;
    specific_view_mem[u_index + 1] = (uint8_t)v;

    return XCAM_RETURN_NO_ERROR;
}

XCamReturn
generate_topview_map_table (
    const VideoBufferInfo &stitch_info,
    const BowlDataConfig &config,
    std::vector<PointFloat2> &map_table,
    int width, int height)
{
    int center_x = width / 2;
    int center_y = height / 2;

    float show_width_mm = 5000.0f;
    float length_per_pixel = show_width_mm / height;

    map_table.resize (height * width);

    for(int row = 0; row < height; row++) {
        for(int col = 0; col < width; col++) {
            PointFloat3 world;
            world.x = (col - center_x) * length_per_pixel;
            world.y = (center_y - row) * length_per_pixel;
            world.z = 0.0f;

            PointFloat2 image_pos =
                bowl_view_coords_to_image (config, world, stitch_info.width, stitch_info.height);

            map_table[row * width + col] = image_pos;
        }
    }

    return XCAM_RETURN_NO_ERROR;
}

XCamReturn
generate_rectifiedview_map_table (
    const VideoBufferInfo &stitch_info,
    const BowlDataConfig &config,
    std::vector<PointFloat2> &map_table,
    float angle_start, float angle_end,
    int width, int height)
{
    float center_x = width / 2;

    float focal_plane_dist = 6000.0f;

    float angle_center = (angle_start + angle_end) / 2.0f;
    float theta = degree2radian((angle_end - angle_start)) / 2.0f;
    float length_per_pixel_x = 2 * focal_plane_dist * tan (theta) / width;

    float fov_up = degree2radian (20.0f);
    float fov_down = degree2radian (35.0f);

    float length_per_pixel_y = (focal_plane_dist * tan (fov_up) + focal_plane_dist * tan (fov_down)) / height;

    float center_y = tan (fov_up) / (tan (fov_up) + tan (fov_down)) * height;

    PointFloat3 world_pos;
    float plane_center_coords[3];

    plane_center_coords[0] = focal_plane_dist * cos (degree2radian (angle_center));
    plane_center_coords[1] = -focal_plane_dist * sin (degree2radian (angle_center));
    plane_center_coords[2] = 0.0f;

    map_table.resize (width * height);

    for (int row = 0; row < height; row++) {
        for (int col = 0; col < width; col++) {
            float plane_point_coords[3];
            plane_point_coords[0] = (center_x - col) * length_per_pixel_x * cos (PI / 2 - degree2radian (angle_center)) + plane_center_coords[0];
            plane_point_coords[1] = (center_x - col) * length_per_pixel_x * sin (PI / 2 - degree2radian (angle_center)) + plane_center_coords[1];
            plane_point_coords[2] = (center_y - row) * length_per_pixel_y + plane_center_coords[2];

            float rate_xz, rate_yz;
            if (XCAM_DOUBLE_EQUAL_AROUND (plane_point_coords[2], 0.0f) && XCAM_DOUBLE_EQUAL_AROUND (plane_point_coords[1], 0.0f)) {
                world_pos.x = config.a;
                world_pos.y = 0;
                world_pos.z = 0;
            } else if (XCAM_DOUBLE_EQUAL_AROUND (plane_point_coords[2], 0.0f)) {
                world_pos.z = 0.0f;

                float rate_xy = plane_point_coords[0] / plane_point_coords[1];
                float square_y = 1 / (rate_xy * rate_xy / (config.a * config.a) + 1 / (config.b * config.b));
                world_pos.y = (plane_point_coords[1] > 0) ? sqrt (square_y) : -sqrt (square_y);
                world_pos.x = rate_xy * world_pos.y;
            } else {
                rate_xz = plane_point_coords[0] / plane_point_coords[2];
                rate_yz = plane_point_coords[1] / plane_point_coords[2];

                float square_z = 1 / (rate_xz * rate_xz / (config.a * config.a) + rate_yz * rate_yz / (config.b * config.b) + 1 / (config.c * config.c));
                world_pos.z = (plane_point_coords[2] > 0) ? sqrt (square_z) : -sqrt (square_z);
                world_pos.z = (world_pos.z <= -config.center_z) ? -config.center_z : world_pos.z;
                world_pos.x = rate_xz * world_pos.z;
                world_pos.y = rate_yz * world_pos.z;
            }

            world_pos.z += config.center_z;

            PointFloat2 image_coord =
                bowl_view_coords_to_image (config, world_pos, stitch_info.width, stitch_info.height);

            map_table[row * width + col] = image_coord;
        }
    }

    return XCAM_RETURN_NO_ERROR;
}

XCamReturn
sample_generate_top_view (
    SmartPtr<VideoBuffer> &stitch_buf,
    SmartPtr<VideoBuffer> top_view_buf,
    const BowlDataConfig &config,
    std::vector<PointFloat2> &map_table)
{
    const VideoBufferInfo top_view_info = top_view_buf->get_video_info ();
    const VideoBufferInfo stitch_info = stitch_buf->get_video_info ();

    int top_view_resolution_w = top_view_buf->get_video_info ().width;
    int top_view_resolution_h = top_view_buf->get_video_info ().height;

    if((int)map_table.size () != top_view_resolution_w * top_view_resolution_h) {
        map_table.clear ();
        generate_topview_map_table (stitch_info, config, map_table, top_view_resolution_w, top_view_resolution_h);
    }

    uint8_t *top_view_mem = NULL;
    uint8_t *stitch_mem = NULL;
    top_view_mem = top_view_buf->map ();
    stitch_mem = stitch_buf->map ();

    for(int row = 0; row < top_view_resolution_h; row++) {
        for(int col = 0; col < top_view_resolution_w; col++) {
            PointFloat2 image_coord = map_table[row * top_view_resolution_w + col];

            map_to_specific_view (top_view_mem, stitch_mem, row, col, image_coord.x, image_coord.y, top_view_info, stitch_info);
        }
    }

    top_view_buf->unmap();
    stitch_buf->unmap();

    return XCAM_RETURN_NO_ERROR;
}

XCamReturn
sample_generate_rectified_view (
    SmartPtr<VideoBuffer> &stitch_buf,
    SmartPtr<VideoBuffer> rectified_view_buf,
    const BowlDataConfig &config,
    float angle_start, float angle_end,
    std::vector<PointFloat2> &map_table)
{
    const VideoBufferInfo rectified_view_info = rectified_view_buf->get_video_info ();
    const VideoBufferInfo stitch_info = stitch_buf->get_video_info ();

    int rectified_view_resolution_w = rectified_view_buf->get_video_info ().width;
    int rectified_view_resolution_h = rectified_view_buf->get_video_info ().height;

    if((int)map_table.size () != rectified_view_resolution_w * rectified_view_resolution_h) {
        map_table.clear ();
        generate_rectifiedview_map_table (stitch_info, config, map_table, angle_start, angle_end, rectified_view_resolution_w, rectified_view_resolution_h);
    }

    uint8_t *rectified_view_mem = NULL;
    uint8_t *stitch_mem = NULL;
    rectified_view_mem = rectified_view_buf->map ();
    stitch_mem = stitch_buf->map ();

    for(int row = 0; row < rectified_view_resolution_h; row++) {
        for(int col = 0; col < rectified_view_resolution_w; col++) {
            PointFloat2 image_coord = map_table[row * rectified_view_resolution_w + col];

            map_to_specific_view (rectified_view_mem, stitch_mem, row, col, image_coord.x, image_coord.y, rectified_view_info, stitch_info);
        }
    }

    rectified_view_buf->unmap();
    stitch_buf->unmap();

    return XCAM_RETURN_NO_ERROR;
}

}