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 "libyuv/convert_argb.h" michael@0: michael@0: #include "libyuv/cpu_id.h" michael@0: #include "libyuv/format_conversion.h" michael@0: #ifdef HAVE_JPEG michael@0: #include "libyuv/mjpeg_decoder.h" michael@0: #endif michael@0: #include "libyuv/rotate_argb.h" michael@0: #include "libyuv/row.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 ConvertToARGB(const uint8* sample, size_t sample_size, michael@0: uint8* crop_argb, int argb_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: 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 crop_argb is same as source sample, michael@0: // also enable temporary buffer. michael@0: LIBYUV_BOOL need_buf = (rotation && format != FOURCC_ARGB) || michael@0: crop_argb == sample; michael@0: uint8* tmp_argb = crop_argb; michael@0: int tmp_argb_stride = argb_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 (crop_argb == NULL || sample == NULL || 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: if (need_buf) { michael@0: int argb_size = crop_width * abs_crop_height * 4; michael@0: rotate_buffer = (uint8*)malloc(argb_size); michael@0: if (!rotate_buffer) { michael@0: return 1; // Out of memory runtime error. michael@0: } michael@0: crop_argb = rotate_buffer; michael@0: argb_stride = crop_width; 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 = YUY2ToARGB(src, aligned_src_width * 2, michael@0: crop_argb, argb_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 = UYVYToARGB(src, aligned_src_width * 2, michael@0: crop_argb, argb_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 = RGB24ToARGB(src, src_width * 3, michael@0: crop_argb, argb_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 = RAWToARGB(src, src_width * 3, michael@0: crop_argb, argb_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 = ARGBToARGB(src, src_width * 4, michael@0: crop_argb, argb_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 = BGRAToARGB(src, src_width * 4, michael@0: crop_argb, argb_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 = ABGRToARGB(src, src_width * 4, michael@0: crop_argb, argb_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 = RGBAToARGB(src, src_width * 4, michael@0: crop_argb, argb_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 = RGB565ToARGB(src, src_width * 2, michael@0: crop_argb, argb_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 = ARGB1555ToARGB(src, src_width * 2, michael@0: crop_argb, argb_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 = ARGB4444ToARGB(src, src_width * 2, michael@0: crop_argb, argb_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 = BayerBGGRToARGB(src, src_width, michael@0: crop_argb, argb_stride, michael@0: crop_width, inv_crop_height); michael@0: break; michael@0: michael@0: case FOURCC_GBRG: michael@0: src = sample + (src_width * crop_y + crop_x); michael@0: r = BayerGBRGToARGB(src, src_width, michael@0: crop_argb, argb_stride, michael@0: crop_width, inv_crop_height); michael@0: break; michael@0: michael@0: case FOURCC_GRBG: michael@0: src = sample + (src_width * crop_y + crop_x); michael@0: r = BayerGRBGToARGB(src, src_width, michael@0: crop_argb, argb_stride, michael@0: crop_width, inv_crop_height); michael@0: break; michael@0: michael@0: case FOURCC_RGGB: michael@0: src = sample + (src_width * crop_y + crop_x); michael@0: r = BayerRGGBToARGB(src, src_width, michael@0: crop_argb, argb_stride, michael@0: crop_width, inv_crop_height); michael@0: break; michael@0: michael@0: case FOURCC_I400: michael@0: src = sample + src_width * crop_y + crop_x; michael@0: r = I400ToARGB(src, src_width, michael@0: crop_argb, argb_stride, michael@0: crop_width, inv_crop_height); michael@0: break; michael@0: 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 = NV12ToARGB(src, src_width, michael@0: src_uv, aligned_src_width, michael@0: crop_argb, argb_stride, michael@0: crop_width, inv_crop_height); 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 = NV21ToARGB(src, src_width, michael@0: src_uv, aligned_src_width, michael@0: crop_argb, argb_stride, michael@0: crop_width, inv_crop_height); michael@0: break; michael@0: case FOURCC_M420: michael@0: src = sample + (src_width * crop_y) * 12 / 8 + crop_x; michael@0: r = M420ToARGB(src, src_width, michael@0: crop_argb, argb_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 = Q420ToARGB(src, src_width * 3, michael@0: // src_uv, src_width * 3, michael@0: // crop_argb, argb_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 = I420ToARGB(src_y, src_width, michael@0: src_u, halfwidth, michael@0: src_v, halfwidth, michael@0: crop_argb, argb_stride, michael@0: crop_width, inv_crop_height); 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 = I422ToARGB(src_y, src_width, michael@0: src_u, halfwidth, michael@0: src_v, halfwidth, michael@0: crop_argb, argb_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 = I444ToARGB(src_y, src_width, michael@0: src_u, src_width, michael@0: src_v, src_width, michael@0: crop_argb, argb_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 = I411ToARGB(src_y, src_width, michael@0: src_u, quarterwidth, michael@0: src_v, quarterwidth, michael@0: crop_argb, argb_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 = MJPGToARGB(sample, sample_size, michael@0: crop_argb, argb_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 = ARGBRotate(crop_argb, argb_stride, michael@0: tmp_argb, tmp_argb_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