michael@0: /* michael@0: * Copyright 2011 The LibYuv Project Authors. All rights reserved. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license michael@0: * that can be found in the LICENSE file in the root of the source michael@0: * tree. An additional intellectual property rights grant can be found michael@0: * in the file PATENTS. All contributing project authors may michael@0: * be found in the AUTHORS file in the root of the source tree. michael@0: */ michael@0: michael@0: #include michael@0: michael@0: #include "libyuv/convert.h" michael@0: michael@0: #include "libyuv/format_conversion.h" michael@0: #include "libyuv/video_common.h" michael@0: michael@0: #ifdef __cplusplus michael@0: namespace libyuv { michael@0: extern "C" { michael@0: #endif michael@0: michael@0: // Convert camera sample to I420 with cropping, rotation and vertical flip. michael@0: // src_width is used for source stride computation michael@0: // src_height is used to compute location of planes, and indicate inversion michael@0: // sample_size is measured in bytes and is the size of the frame. michael@0: // With MJPEG it is the compressed size of the frame. michael@0: LIBYUV_API michael@0: int ConvertToI420(const uint8* sample, michael@0: size_t sample_size, michael@0: uint8* y, int y_stride, michael@0: uint8* u, int u_stride, michael@0: uint8* v, int v_stride, michael@0: int crop_x, int crop_y, michael@0: int src_width, int src_height, michael@0: int crop_width, int crop_height, michael@0: enum RotationMode rotation, michael@0: uint32 fourcc) { michael@0: uint32 format = CanonicalFourCC(fourcc); michael@0: int aligned_src_width = (src_width + 1) & ~1; michael@0: const uint8* src; michael@0: const uint8* src_uv; michael@0: int abs_src_height = (src_height < 0) ? -src_height : src_height; michael@0: int inv_crop_height = (crop_height < 0) ? -crop_height : crop_height; michael@0: int r = 0; michael@0: LIBYUV_BOOL need_buf = (rotation && format != FOURCC_I420 && michael@0: format != FOURCC_NV12 && format != FOURCC_NV21 && michael@0: format != FOURCC_YU12 && format != FOURCC_YV12) || y == sample; michael@0: uint8* tmp_y = y; michael@0: uint8* tmp_u = u; michael@0: uint8* tmp_v = v; michael@0: int tmp_y_stride = y_stride; michael@0: int tmp_u_stride = u_stride; michael@0: int tmp_v_stride = v_stride; michael@0: uint8* rotate_buffer = NULL; michael@0: int abs_crop_height = (crop_height < 0) ? -crop_height : crop_height; michael@0: michael@0: if (!y || !u || !v || !sample || michael@0: src_width <= 0 || crop_width <= 0 || michael@0: src_height == 0 || crop_height == 0) { michael@0: return -1; michael@0: } michael@0: if (src_height < 0) { michael@0: inv_crop_height = -inv_crop_height; michael@0: } michael@0: michael@0: // One pass rotation is available for some formats. For the rest, convert michael@0: // to I420 (with optional vertical flipping) into a temporary I420 buffer, michael@0: // and then rotate the I420 to the final destination buffer. michael@0: // For in-place conversion, if destination y is same as source sample, michael@0: // also enable temporary buffer. michael@0: if (need_buf) { michael@0: int y_size = crop_width * abs_crop_height; michael@0: int uv_size = ((crop_width + 1) / 2) * ((abs_crop_height + 1) / 2); michael@0: rotate_buffer = (uint8*)malloc(y_size + uv_size * 2); michael@0: if (!rotate_buffer) { michael@0: return 1; // Out of memory runtime error. michael@0: } michael@0: y = rotate_buffer; michael@0: u = y + y_size; michael@0: v = u + uv_size; michael@0: y_stride = crop_width; michael@0: u_stride = v_stride = ((crop_width + 1) / 2); michael@0: } michael@0: michael@0: switch (format) { michael@0: // Single plane formats michael@0: case FOURCC_YUY2: michael@0: src = sample + (aligned_src_width * crop_y + crop_x) * 2; michael@0: r = YUY2ToI420(src, aligned_src_width * 2, michael@0: y, y_stride, michael@0: u, u_stride, michael@0: v, v_stride, michael@0: crop_width, inv_crop_height); michael@0: break; michael@0: case FOURCC_UYVY: michael@0: src = sample + (aligned_src_width * crop_y + crop_x) * 2; michael@0: r = UYVYToI420(src, aligned_src_width * 2, michael@0: y, y_stride, michael@0: u, u_stride, michael@0: v, v_stride, michael@0: crop_width, inv_crop_height); michael@0: break; michael@0: case FOURCC_RGBP: michael@0: src = sample + (src_width * crop_y + crop_x) * 2; michael@0: r = RGB565ToI420(src, src_width * 2, michael@0: y, y_stride, michael@0: u, u_stride, michael@0: v, v_stride, michael@0: crop_width, inv_crop_height); michael@0: break; michael@0: case FOURCC_RGBO: michael@0: src = sample + (src_width * crop_y + crop_x) * 2; michael@0: r = ARGB1555ToI420(src, src_width * 2, michael@0: y, y_stride, michael@0: u, u_stride, michael@0: v, v_stride, michael@0: crop_width, inv_crop_height); michael@0: break; michael@0: case FOURCC_R444: michael@0: src = sample + (src_width * crop_y + crop_x) * 2; michael@0: r = ARGB4444ToI420(src, src_width * 2, michael@0: y, y_stride, michael@0: u, u_stride, michael@0: v, v_stride, michael@0: crop_width, inv_crop_height); michael@0: break; michael@0: case FOURCC_24BG: michael@0: src = sample + (src_width * crop_y + crop_x) * 3; michael@0: r = RGB24ToI420(src, src_width * 3, michael@0: y, y_stride, michael@0: u, u_stride, michael@0: v, v_stride, michael@0: crop_width, inv_crop_height); michael@0: break; michael@0: case FOURCC_RAW: michael@0: src = sample + (src_width * crop_y + crop_x) * 3; michael@0: r = RAWToI420(src, src_width * 3, michael@0: y, y_stride, michael@0: u, u_stride, michael@0: v, v_stride, michael@0: crop_width, inv_crop_height); michael@0: break; michael@0: case FOURCC_ARGB: michael@0: src = sample + (src_width * crop_y + crop_x) * 4; michael@0: r = ARGBToI420(src, src_width * 4, michael@0: y, y_stride, michael@0: u, u_stride, michael@0: v, v_stride, michael@0: crop_width, inv_crop_height); michael@0: break; michael@0: case FOURCC_BGRA: michael@0: src = sample + (src_width * crop_y + crop_x) * 4; michael@0: r = BGRAToI420(src, src_width * 4, michael@0: y, y_stride, michael@0: u, u_stride, michael@0: v, v_stride, michael@0: crop_width, inv_crop_height); michael@0: break; michael@0: case FOURCC_ABGR: michael@0: src = sample + (src_width * crop_y + crop_x) * 4; michael@0: r = ABGRToI420(src, src_width * 4, michael@0: y, y_stride, michael@0: u, u_stride, michael@0: v, v_stride, michael@0: crop_width, inv_crop_height); michael@0: break; michael@0: case FOURCC_RGBA: michael@0: src = sample + (src_width * crop_y + crop_x) * 4; michael@0: r = RGBAToI420(src, src_width * 4, michael@0: y, y_stride, michael@0: u, u_stride, michael@0: v, v_stride, michael@0: crop_width, inv_crop_height); michael@0: break; michael@0: // TODO(fbarchard): Support cropping Bayer by odd numbers michael@0: // by adjusting fourcc. michael@0: case FOURCC_BGGR: michael@0: src = sample + (src_width * crop_y + crop_x); michael@0: r = BayerBGGRToI420(src, src_width, michael@0: y, y_stride, michael@0: u, u_stride, michael@0: v, v_stride, michael@0: crop_width, inv_crop_height); michael@0: break; michael@0: case FOURCC_GBRG: michael@0: src = sample + (src_width * crop_y + crop_x); michael@0: r = BayerGBRGToI420(src, src_width, michael@0: y, y_stride, michael@0: u, u_stride, michael@0: v, v_stride, michael@0: crop_width, inv_crop_height); michael@0: break; michael@0: case FOURCC_GRBG: michael@0: src = sample + (src_width * crop_y + crop_x); michael@0: r = BayerGRBGToI420(src, src_width, michael@0: y, y_stride, michael@0: u, u_stride, michael@0: v, v_stride, michael@0: crop_width, inv_crop_height); michael@0: break; michael@0: case FOURCC_RGGB: michael@0: src = sample + (src_width * crop_y + crop_x); michael@0: r = BayerRGGBToI420(src, src_width, michael@0: y, y_stride, michael@0: u, u_stride, michael@0: v, v_stride, michael@0: crop_width, inv_crop_height); michael@0: break; michael@0: case FOURCC_I400: michael@0: src = sample + src_width * crop_y + crop_x; michael@0: r = I400ToI420(src, src_width, michael@0: y, y_stride, michael@0: u, u_stride, michael@0: v, v_stride, michael@0: crop_width, inv_crop_height); michael@0: break; michael@0: // Biplanar formats michael@0: case FOURCC_NV12: michael@0: src = sample + (src_width * crop_y + crop_x); michael@0: src_uv = sample + aligned_src_width * (src_height + crop_y / 2) + crop_x; michael@0: r = NV12ToI420Rotate(src, src_width, michael@0: src_uv, aligned_src_width, michael@0: y, y_stride, michael@0: u, u_stride, michael@0: v, v_stride, michael@0: crop_width, inv_crop_height, rotation); michael@0: break; michael@0: case FOURCC_NV21: michael@0: src = sample + (src_width * crop_y + crop_x); michael@0: src_uv = sample + aligned_src_width * (src_height + crop_y / 2) + crop_x; michael@0: // Call NV12 but with u and v parameters swapped. michael@0: r = NV12ToI420Rotate(src, src_width, michael@0: src_uv, aligned_src_width, michael@0: y, y_stride, michael@0: v, v_stride, michael@0: u, u_stride, michael@0: crop_width, inv_crop_height, rotation); michael@0: break; michael@0: case FOURCC_M420: michael@0: src = sample + (src_width * crop_y) * 12 / 8 + crop_x; michael@0: r = M420ToI420(src, src_width, michael@0: y, y_stride, michael@0: u, u_stride, michael@0: v, v_stride, michael@0: crop_width, inv_crop_height); michael@0: break; michael@0: case FOURCC_Q420: michael@0: src = sample + (src_width + aligned_src_width * 2) * crop_y + crop_x; michael@0: src_uv = sample + (src_width + aligned_src_width * 2) * crop_y + michael@0: src_width + crop_x * 2; michael@0: r = Q420ToI420(src, src_width * 3, michael@0: src_uv, src_width * 3, michael@0: y, y_stride, michael@0: u, u_stride, michael@0: v, v_stride, michael@0: crop_width, inv_crop_height); michael@0: break; michael@0: // Triplanar formats michael@0: case FOURCC_I420: michael@0: case FOURCC_YU12: michael@0: case FOURCC_YV12: { michael@0: const uint8* src_y = sample + (src_width * crop_y + crop_x); michael@0: const uint8* src_u; michael@0: const uint8* src_v; michael@0: int halfwidth = (src_width + 1) / 2; michael@0: int halfheight = (abs_src_height + 1) / 2; michael@0: if (format == FOURCC_YV12) { michael@0: src_v = sample + src_width * abs_src_height + michael@0: (halfwidth * crop_y + crop_x) / 2; michael@0: src_u = sample + src_width * abs_src_height + michael@0: halfwidth * (halfheight + crop_y / 2) + crop_x / 2; michael@0: } else { michael@0: src_u = sample + src_width * abs_src_height + michael@0: (halfwidth * crop_y + crop_x) / 2; michael@0: src_v = sample + src_width * abs_src_height + michael@0: halfwidth * (halfheight + crop_y / 2) + crop_x / 2; michael@0: } michael@0: r = I420Rotate(src_y, src_width, michael@0: src_u, halfwidth, michael@0: src_v, halfwidth, michael@0: y, y_stride, michael@0: u, u_stride, michael@0: v, v_stride, michael@0: crop_width, inv_crop_height, rotation); michael@0: break; michael@0: } michael@0: case FOURCC_I422: michael@0: case FOURCC_YV16: { michael@0: const uint8* src_y = sample + src_width * crop_y + crop_x; michael@0: const uint8* src_u; michael@0: const uint8* src_v; michael@0: int halfwidth = (src_width + 1) / 2; michael@0: if (format == FOURCC_YV16) { michael@0: src_v = sample + src_width * abs_src_height + michael@0: halfwidth * crop_y + crop_x / 2; michael@0: src_u = sample + src_width * abs_src_height + michael@0: halfwidth * (abs_src_height + crop_y) + crop_x / 2; michael@0: } else { michael@0: src_u = sample + src_width * abs_src_height + michael@0: halfwidth * crop_y + crop_x / 2; michael@0: src_v = sample + src_width * abs_src_height + michael@0: halfwidth * (abs_src_height + crop_y) + crop_x / 2; michael@0: } michael@0: r = I422ToI420(src_y, src_width, michael@0: src_u, halfwidth, michael@0: src_v, halfwidth, michael@0: y, y_stride, michael@0: u, u_stride, michael@0: v, v_stride, michael@0: crop_width, inv_crop_height); michael@0: break; michael@0: } michael@0: case FOURCC_I444: michael@0: case FOURCC_YV24: { michael@0: const uint8* src_y = sample + src_width * crop_y + crop_x; michael@0: const uint8* src_u; michael@0: const uint8* src_v; michael@0: if (format == FOURCC_YV24) { michael@0: src_v = sample + src_width * (abs_src_height + crop_y) + crop_x; michael@0: src_u = sample + src_width * (abs_src_height * 2 + crop_y) + crop_x; michael@0: } else { michael@0: src_u = sample + src_width * (abs_src_height + crop_y) + crop_x; michael@0: src_v = sample + src_width * (abs_src_height * 2 + crop_y) + crop_x; michael@0: } michael@0: r = I444ToI420(src_y, src_width, michael@0: src_u, src_width, michael@0: src_v, src_width, michael@0: y, y_stride, michael@0: u, u_stride, michael@0: v, v_stride, michael@0: crop_width, inv_crop_height); michael@0: break; michael@0: } michael@0: case FOURCC_I411: { michael@0: int quarterwidth = (src_width + 3) / 4; michael@0: const uint8* src_y = sample + src_width * crop_y + crop_x; michael@0: const uint8* src_u = sample + src_width * abs_src_height + michael@0: quarterwidth * crop_y + crop_x / 4; michael@0: const uint8* src_v = sample + src_width * abs_src_height + michael@0: quarterwidth * (abs_src_height + crop_y) + crop_x / 4; michael@0: r = I411ToI420(src_y, src_width, michael@0: src_u, quarterwidth, michael@0: src_v, quarterwidth, michael@0: y, y_stride, michael@0: u, u_stride, michael@0: v, v_stride, michael@0: crop_width, inv_crop_height); michael@0: break; michael@0: } michael@0: #ifdef HAVE_JPEG michael@0: case FOURCC_MJPG: michael@0: r = MJPGToI420(sample, sample_size, michael@0: y, y_stride, michael@0: u, u_stride, michael@0: v, v_stride, michael@0: src_width, abs_src_height, crop_width, inv_crop_height); michael@0: break; michael@0: #endif michael@0: default: michael@0: r = -1; // unknown fourcc - return failure code. michael@0: } michael@0: michael@0: if (need_buf) { michael@0: if (!r) { michael@0: r = I420Rotate(y, y_stride, michael@0: u, u_stride, michael@0: v, v_stride, michael@0: tmp_y, tmp_y_stride, michael@0: tmp_u, tmp_u_stride, michael@0: tmp_v, tmp_v_stride, michael@0: crop_width, abs_crop_height, rotation); michael@0: } michael@0: free(rotate_buffer); michael@0: } michael@0: michael@0: return r; michael@0: } michael@0: michael@0: #ifdef __cplusplus michael@0: } // extern "C" michael@0: } // namespace libyuv michael@0: #endif