FormatConversions.cpp 12.0 KB
Newer Older
Z
ZhaoPengyuan 已提交
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
/*
 * Copyright (C) 2016 Google, Inc.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include "FormatConversions.h"

#include <cutils/log.h>
#include <string.h>

#define DEBUG 0

#if DEBUG
#define DD(...) ALOGD(...)
#else
#define DD(...)
#endif

void get_yv12_offsets(int width, int height, uint32_t *yStride_out, uint32_t *cStride_out, uint32_t *totalSz_out) {
    uint32_t align = 16;
    uint32_t yStride = (width + (align - 1)) & ~(align - 1);
    uint32_t uvStride = (yStride / 2 + (align - 1)) & ~(align - 1);
    uint32_t uvHeight = height / 2;
    uint32_t sz = yStride * height + 2 * (uvHeight * uvStride);

    if (yStride_out) {
        *yStride_out = yStride;
    }
    if (cStride_out) {
        *cStride_out = uvStride;
    }
    if (totalSz_out) {
        *totalSz_out = sz;
    }
}

void get_yuv420p_offsets(int width, int height, uint32_t *yStride_out, uint32_t *cStride_out, uint32_t *totalSz_out) {
    uint32_t align = 1;
    uint32_t yStride = (width + (align - 1)) & ~(align - 1);
    uint32_t uvStride = (yStride / 2 + (align - 1)) & ~(align - 1);
    uint32_t uvHeight = height / 2;
    uint32_t sz = yStride * height + 2 * (uvHeight * uvStride);

    if (yStride_out) { *yStride_out = yStride; }
    if (cStride_out) { *cStride_out = uvStride; }
    if (totalSz_out) { *totalSz_out = sz; }
}

signed clamp_rgb(signed value) {
    if (value > 255) {
        value = 255;
    } else if (value < 0) {
        value = 0;
    }
    return value;
}

void rgb565_to_yv12(char *dest, char *src, int width, int height, int left, int top, int right, int bottom) {
    int align = 16;
    int yStride = (width + (align - 1)) & ~(align - 1);
    int cStride = (yStride / 2 + (align - 1)) & ~(align - 1);
    int yOffset = 0;
    int cSize = cStride * height / 2;

    uint16_t *rgb_ptr0 = (uint16_t *) src;
    uint8_t *yv12_y0 = (uint8_t *) dest;
    uint8_t *yv12_v0 = yv12_y0 + yStride * height;
    uint8_t *yv12_u0 = yv12_v0 + cSize;

    for (int j = top; j <= bottom; ++j) {
        uint8_t *yv12_y = yv12_y0 + j * yStride;
        uint8_t *yv12_v = yv12_v0 + (j / 2) * cStride;
        uint8_t *yv12_u = yv12_v + cSize;
        uint16_t *rgb_ptr = rgb_ptr0 + j * width;
        bool jeven = (j & 1) == 0;
        for (int i = left; i <= right; ++i) {
            uint8_t r = ((rgb_ptr[i]) >> 11) & 0x01f;
            uint8_t g = ((rgb_ptr[i]) >> 5) & 0x03f;
            uint8_t b = (rgb_ptr[i]) & 0x01f;
            // convert to 8bits
            // http://stackoverflow.com/questions/2442576/how-does-one-convert-16-bit-rgb565-to-24-bit-rgb888
            uint8_t R = (r * 527 + 23) >> 6;
            uint8_t G = (g * 259 + 33) >> 6;
            uint8_t B = (b * 527 + 23) >> 6;
            // convert to YV12
            // frameworks/base/core/jni/android_hardware_camera2_legacy_LegacyCameraDevice.cpp
            yv12_y[i] = clamp_rgb((77 * R + 150 * G + 29 * B) >> 8);
            bool ieven = (i & 1) == 0;
            if (jeven && ieven) {
                yv12_u[i] = clamp_rgb(((-43 * R - 85 * G + 128 * B) >> 8) + 128);
                yv12_v[i] = clamp_rgb(((128 * R - 107 * G - 21 * B) >> 8) + 128);
            }
        }
    }
}

void rgb888_to_yv12(char *dest, char *src, int width, int height, int left, int top, int right, int bottom) {
    DD("%s convert %d by %d", __func__, width, height);
    int align = 16;
    int yStride = (width + (align - 1)) & ~(align - 1);
    int cStride = (yStride / 2 + (align - 1)) & ~(align - 1);
    int yOffset = 0;
    int cSize = cStride * height / 2;
    int rgb_stride = 3;

    uint8_t *rgb_ptr0 = (uint8_t *) src;
    uint8_t *yv12_y0 = (uint8_t *) dest;
    uint8_t *yv12_v0 = yv12_y0 + yStride * height;
    uint8_t *yv12_u0 = yv12_v0 + cSize;

    for (int j = top; j <= bottom; ++j) {
        uint8_t *yv12_y = yv12_y0 + j * yStride;
        uint8_t *yv12_v = yv12_v0 + (j / 2) * cStride;
        uint8_t *yv12_u = yv12_v + cSize;
        uint8_t *rgb_ptr = rgb_ptr0 + j * width * rgb_stride;
        bool jeven = (j & 1) == 0;
        for (int i = left; i <= right; ++i) {
            uint8_t R = rgb_ptr[i * rgb_stride];
            uint8_t G = rgb_ptr[i * rgb_stride + 1];
            uint8_t B = rgb_ptr[i * rgb_stride + 2];
            // convert to YV12
            // frameworks/base/core/jni/android_hardware_camera2_legacy_LegacyCameraDevice.cpp
            yv12_y[i] = clamp_rgb((77 * R + 150 * G + 29 * B) >> 8);
            bool ieven = (i & 1) == 0;
            if (jeven && ieven) {
                yv12_u[i] = clamp_rgb(((-43 * R - 85 * G + 128 * B) >> 8) + 128);
                yv12_v[i] = clamp_rgb(((128 * R - 107 * G - 21 * B) >> 8) + 128);
            }
        }
    }
}

void rgb888_to_yuv420p(char *dest, char *src, int width, int height, int left, int top, int right, int bottom) {
    DD("%s convert %d by %d", __func__, width, height);
    int yStride = width;
    int cStride = yStride / 2;
    int yOffset = 0;
    int cSize = cStride * height / 2;
    int rgb_stride = 3;

    uint8_t *rgb_ptr0 = (uint8_t *) src;
    uint8_t *yv12_y0 = (uint8_t *) dest;
    uint8_t *yv12_u0 = yv12_y0 + yStride * height;
    uint8_t *yv12_v0 = yv12_u0 + cSize;

    for (int j = top; j <= bottom; ++j) {
        uint8_t *yv12_y = yv12_y0 + j * yStride;
        uint8_t *yv12_u = yv12_u0 + (j / 2) * cStride;
        uint8_t *yv12_v = yv12_u + cStride;
        uint8_t *rgb_ptr = rgb_ptr0 + j * width * rgb_stride;
        bool jeven = (j & 1) == 0;
        for (int i = left; i <= right; ++i) {
            uint8_t R = rgb_ptr[i * rgb_stride];
            uint8_t G = rgb_ptr[i * rgb_stride + 1];
            uint8_t B = rgb_ptr[i * rgb_stride + 2];
            // convert to YV12
            // frameworks/base/core/jni/android_hardware_camera2_legacy_LegacyCameraDevice.cpp
            yv12_y[i] = clamp_rgb((77 * R + 150 * G + 29 * B) >> 8);
            bool ieven = (i & 1) == 0;
            if (jeven && ieven) {
                yv12_u[i] = clamp_rgb(((-43 * R - 85 * G + 128 * B) >> 8) + 128);
                yv12_v[i] = clamp_rgb(((128 * R - 107 * G - 21 * B) >> 8) + 128);
            }
        }
    }
}

// YV12 is aka YUV420Planar, or YUV420p; the only difference is that YV12 has
// certain stride requirements for Y and UV respectively.
void yv12_to_rgb565(char *dest, char *src, int width, int height, int left, int top, int right, int bottom) {
    DD("%s convert %d by %d", __func__, width, height);
    int align = 16;
    int yStride = (width + (align - 1)) & ~(align - 1);
    int cStride = (yStride / 2 + (align - 1)) & ~(align - 1);
    int yOffset = 0;
    int cSize = cStride * height / 2;

    uint16_t *rgb_ptr0 = (uint16_t *) dest;
    uint8_t *yv12_y0 = (uint8_t *) src;
    uint8_t *yv12_v0 = yv12_y0 + yStride * height;
    uint8_t *yv12_u0 = yv12_v0 + cSize;

    for (int j = top; j <= bottom; ++j) {
        uint8_t *yv12_y = yv12_y0 + j * yStride;
        uint8_t *yv12_v = yv12_v0 + (j / 2) * cStride;
        uint8_t *yv12_u = yv12_v + cSize;
        uint16_t *rgb_ptr = rgb_ptr0 + (j - top) * (right - left + 1);
        for (int i = left; i <= right; ++i) {
            // convert to rgb
            // frameworks/av/media/libstagefright/colorconversion/ColorConverter.cpp
            signed y1 = (signed) yv12_y[i] - 16;
            signed u = (signed) yv12_u[i / 2] - 128;
            signed v = (signed) yv12_v[i / 2] - 128;

            signed u_b = u * 517;
            signed u_g = -u * 100;
            signed v_g = -v * 208;
            signed v_r = v * 409;

            signed tmp1 = y1 * 298;
            signed b1 = clamp_rgb((tmp1 + u_b) / 256);
            signed g1 = clamp_rgb((tmp1 + v_g + u_g) / 256);
            signed r1 = clamp_rgb((tmp1 + v_r) / 256);

            uint16_t rgb1 = ((r1 >> 3) << 11) | ((g1 >> 2) << 5) | (b1 >> 3);

            rgb_ptr[i - left] = rgb1;
        }
    }
}

// YV12 is aka YUV420Planar, or YUV420p; the only difference is that YV12 has
// certain stride requirements for Y and UV respectively.
void yv12_to_rgb888(char *dest, char *src, int width, int height, int left, int top, int right, int bottom) {
    DD("%s convert %d by %d", __func__, width, height);
    int align = 16;
    int yStride = (width + (align - 1)) & ~(align - 1);
    int cStride = (yStride / 2 + (align - 1)) & ~(align - 1);
    int yOffset = 0;
    int cSize = cStride * height / 2;
    int rgb_stride = 3;

    uint8_t *rgb_ptr0 = (uint8_t *) dest;
    uint8_t *yv12_y0 = (uint8_t *) src;
    uint8_t *yv12_v0 = yv12_y0 + yStride * height;
    uint8_t *yv12_u0 = yv12_v0 + cSize;

    for (int j = top; j <= bottom; ++j) {
        uint8_t *yv12_y = yv12_y0 + j * yStride;
        uint8_t *yv12_v = yv12_v0 + (j / 2) * cStride;
        uint8_t *yv12_u = yv12_v + cSize;
        uint8_t *rgb_ptr = rgb_ptr0 + (j - top) * (right - left + 1) * rgb_stride;
        for (int i = left; i <= right; ++i) {
            // convert to rgb
            // frameworks/av/media/libstagefright/colorconversion/ColorConverter.cpp
            signed y1 = (signed) yv12_y[i] - 16;
            signed u = (signed) yv12_u[i / 2] - 128;
            signed v = (signed) yv12_v[i / 2] - 128;

            signed u_b = u * 517;
            signed u_g = -u * 100;
            signed v_g = -v * 208;
            signed v_r = v * 409;

            signed tmp1 = y1 * 298;
            signed b1 = clamp_rgb((tmp1 + u_b) / 256);
            signed g1 = clamp_rgb((tmp1 + v_g + u_g) / 256);
            signed r1 = clamp_rgb((tmp1 + v_r) / 256);

            rgb_ptr[(i - left) * rgb_stride] = r1;
            rgb_ptr[(i - left) * rgb_stride + 1] = g1;
            rgb_ptr[(i - left) * rgb_stride + 2] = b1;
        }
    }
}

// YV12 is aka YUV420Planar, or YUV420p; the only difference is that YV12 has
// certain stride requirements for Y and UV respectively.
void yuv420p_to_rgb888(char *dest, char *src, int width, int height, int left, int top, int right, int bottom) {
    DD("%s convert %d by %d", __func__, width, height);
    int yStride = width;
    int cStride = yStride / 2;
    int yOffset = 0;
    int cSize = cStride * height / 2;
    int rgb_stride = 3;

    uint8_t *rgb_ptr0 = (uint8_t *) dest;
    uint8_t *yv12_y0 = (uint8_t *) src;
    uint8_t *yv12_u0 = yv12_y0 + yStride * height;
    uint8_t *yv12_v0 = yv12_u0 + cSize;

    for (int j = top; j <= bottom; ++j) {
        uint8_t *yv12_y = yv12_y0 + j * yStride;
        uint8_t *yv12_u = yv12_u0 + (j / 2) * cStride;
        uint8_t *yv12_v = yv12_u + cSize;
        uint8_t *rgb_ptr = rgb_ptr0 + (j - top) * (right - left + 1) * rgb_stride;
        for (int i = left; i <= right; ++i) {
            // convert to rgb
            // frameworks/av/media/libstagefright/colorconversion/ColorConverter.cpp
            signed y1 = (signed) yv12_y[i] - 16;
            signed u = (signed) yv12_u[i / 2] - 128;
            signed v = (signed) yv12_v[i / 2] - 128;

            signed u_b = u * 517;
            signed u_g = -u * 100;
            signed v_g = -v * 208;
            signed v_r = v * 409;

            signed tmp1 = y1 * 298;
            signed b1 = clamp_rgb((tmp1 + u_b) / 256);
            signed g1 = clamp_rgb((tmp1 + v_g + u_g) / 256);
            signed r1 = clamp_rgb((tmp1 + v_r) / 256);

            rgb_ptr[(i - left) * rgb_stride] = r1;
            rgb_ptr[(i - left) * rgb_stride + 1] = g1;
            rgb_ptr[(i - left) * rgb_stride + 2] = b1;
        }
    }
}

void
copy_rgb_buffer_from_unlocked(char *_dst, char *raw_data, int unlockedWidth, int width, int height, int top, int left,
                              int bpp) {
    char *dst = _dst;
    int dst_line_len = width * bpp;
    int src_line_len = unlockedWidth * bpp;
    char *src = (char *) raw_data + top * src_line_len + left * bpp;
    for (int y = 0; y < height; y++) {
        memcpy(dst, src, dst_line_len);
        src += src_line_len;
        dst += dst_line_len;
    }
}