michael@0: /* michael@0: * Copyright (c) 2010 The WebM 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: michael@0: /**************************************************************************** michael@0: * michael@0: * Module Title : scale.c michael@0: * michael@0: * Description : Image scaling functions. michael@0: * michael@0: ***************************************************************************/ michael@0: michael@0: /**************************************************************************** michael@0: * Header Files michael@0: ****************************************************************************/ michael@0: #include "./vpx_scale_rtcd.h" michael@0: #include "vpx_mem/vpx_mem.h" michael@0: #include "vpx_scale/yv12config.h" michael@0: michael@0: typedef struct { michael@0: int expanded_frame_width; michael@0: int expanded_frame_height; michael@0: michael@0: int HScale; michael@0: int HRatio; michael@0: int VScale; michael@0: int VRatio; michael@0: michael@0: YV12_BUFFER_CONFIG *src_yuv_config; michael@0: YV12_BUFFER_CONFIG *dst_yuv_config; michael@0: michael@0: } SCALE_VARS; michael@0: michael@0: /**************************************************************************** michael@0: * michael@0: * ROUTINE : scale1d_2t1_i michael@0: * michael@0: * INPUTS : const unsigned char *source : Pointer to data to be scaled. michael@0: * int source_step : Number of pixels to step on in source. michael@0: * unsigned int source_scale : Scale for source (UNUSED). michael@0: * unsigned int source_length : Length of source (UNUSED). michael@0: * unsigned char *dest : Pointer to output data array. michael@0: * int dest_step : Number of pixels to step on in destination. michael@0: * unsigned int dest_scale : Scale for destination (UNUSED). michael@0: * unsigned int dest_length : Length of destination. michael@0: * michael@0: * OUTPUTS : None. michael@0: * michael@0: * RETURNS : void michael@0: * michael@0: * FUNCTION : Performs 2-to-1 interpolated scaling. michael@0: * michael@0: * SPECIAL NOTES : None. michael@0: * michael@0: ****************************************************************************/ michael@0: static michael@0: void scale1d_2t1_i michael@0: ( michael@0: const unsigned char *source, michael@0: int source_step, michael@0: unsigned int source_scale, michael@0: unsigned int source_length, michael@0: unsigned char *dest, michael@0: int dest_step, michael@0: unsigned int dest_scale, michael@0: unsigned int dest_length michael@0: ) { michael@0: unsigned int i, j; michael@0: unsigned int temp; michael@0: int source_pitch = source_step; michael@0: (void) source_length; michael@0: (void) source_scale; michael@0: (void) dest_scale; michael@0: michael@0: source_step *= 2; michael@0: dest[0] = source[0]; michael@0: michael@0: for (i = dest_step, j = source_step; i < dest_length * dest_step; i += dest_step, j += source_step) { michael@0: temp = 8; michael@0: temp += 3 * source[j - source_pitch]; michael@0: temp += 10 * source[j]; michael@0: temp += 3 * source[j + source_pitch]; michael@0: temp >>= 4; michael@0: dest[i] = (char)(temp); michael@0: } michael@0: } michael@0: michael@0: /**************************************************************************** michael@0: * michael@0: * ROUTINE : scale1d_2t1_ps michael@0: * michael@0: * INPUTS : const unsigned char *source : Pointer to data to be scaled. michael@0: * int source_step : Number of pixels to step on in source. michael@0: * unsigned int source_scale : Scale for source (UNUSED). michael@0: * unsigned int source_length : Length of source (UNUSED). michael@0: * unsigned char *dest : Pointer to output data array. michael@0: * int dest_step : Number of pixels to step on in destination. michael@0: * unsigned int dest_scale : Scale for destination (UNUSED). michael@0: * unsigned int dest_length : Length of destination. michael@0: * michael@0: * OUTPUTS : None. michael@0: * michael@0: * RETURNS : void michael@0: * michael@0: * FUNCTION : Performs 2-to-1 point subsampled scaling. michael@0: * michael@0: * SPECIAL NOTES : None. michael@0: * michael@0: ****************************************************************************/ michael@0: static michael@0: void scale1d_2t1_ps michael@0: ( michael@0: const unsigned char *source, michael@0: int source_step, michael@0: unsigned int source_scale, michael@0: unsigned int source_length, michael@0: unsigned char *dest, michael@0: int dest_step, michael@0: unsigned int dest_scale, michael@0: unsigned int dest_length michael@0: ) { michael@0: unsigned int i, j; michael@0: michael@0: (void) source_length; michael@0: (void) source_scale; michael@0: (void) dest_scale; michael@0: michael@0: source_step *= 2; michael@0: j = 0; michael@0: michael@0: for (i = 0; i < dest_length * dest_step; i += dest_step, j += source_step) michael@0: dest[i] = source[j]; michael@0: } michael@0: /**************************************************************************** michael@0: * michael@0: * ROUTINE : scale1d_c michael@0: * michael@0: * INPUTS : const unsigned char *source : Pointer to data to be scaled. michael@0: * int source_step : Number of pixels to step on in source. michael@0: * unsigned int source_scale : Scale for source. michael@0: * unsigned int source_length : Length of source (UNUSED). michael@0: * unsigned char *dest : Pointer to output data array. michael@0: * int dest_step : Number of pixels to step on in destination. michael@0: * unsigned int dest_scale : Scale for destination. michael@0: * unsigned int dest_length : Length of destination. michael@0: * michael@0: * OUTPUTS : None. michael@0: * michael@0: * RETURNS : void michael@0: * michael@0: * FUNCTION : Performs linear interpolation in one dimension. michael@0: * michael@0: * SPECIAL NOTES : None. michael@0: * michael@0: ****************************************************************************/ michael@0: static michael@0: void scale1d_c michael@0: ( michael@0: const unsigned char *source, michael@0: int source_step, michael@0: unsigned int source_scale, michael@0: unsigned int source_length, michael@0: unsigned char *dest, michael@0: int dest_step, michael@0: unsigned int dest_scale, michael@0: unsigned int dest_length michael@0: ) { michael@0: unsigned int i; michael@0: unsigned int round_value = dest_scale / 2; michael@0: unsigned int left_modifier = dest_scale; michael@0: unsigned int right_modifier = 0; michael@0: unsigned char left_pixel = *source; michael@0: unsigned char right_pixel = *(source + source_step); michael@0: michael@0: (void) source_length; michael@0: michael@0: /* These asserts are needed if there are boundary issues... */ michael@0: /*assert ( dest_scale > source_scale );*/ michael@0: /*assert ( (source_length-1) * dest_scale >= (dest_length-1) * source_scale );*/ michael@0: michael@0: for (i = 0; i < dest_length * dest_step; i += dest_step) { michael@0: dest[i] = (char)((left_modifier * left_pixel + right_modifier * right_pixel + round_value) / dest_scale); michael@0: michael@0: right_modifier += source_scale; michael@0: michael@0: while (right_modifier > dest_scale) { michael@0: right_modifier -= dest_scale; michael@0: source += source_step; michael@0: left_pixel = *source; michael@0: right_pixel = *(source + source_step); michael@0: } michael@0: michael@0: left_modifier = dest_scale - right_modifier; michael@0: } michael@0: } michael@0: michael@0: /**************************************************************************** michael@0: * michael@0: * ROUTINE : Scale2D michael@0: * michael@0: * INPUTS : const unsigned char *source : Pointer to data to be scaled. michael@0: * int source_pitch : Stride of source image. michael@0: * unsigned int source_width : Width of input image. michael@0: * unsigned int source_height : Height of input image. michael@0: * unsigned char *dest : Pointer to output data array. michael@0: * int dest_pitch : Stride of destination image. michael@0: * unsigned int dest_width : Width of destination image. michael@0: * unsigned int dest_height : Height of destination image. michael@0: * unsigned char *temp_area : Pointer to temp work area. michael@0: * unsigned char temp_area_height : Height of temp work area. michael@0: * unsigned int hscale : Horizontal scale factor numerator. michael@0: * unsigned int hratio : Horizontal scale factor denominator. michael@0: * unsigned int vscale : Vertical scale factor numerator. michael@0: * unsigned int vratio : Vertical scale factor denominator. michael@0: * unsigned int interlaced : Interlace flag. michael@0: * michael@0: * OUTPUTS : None. michael@0: * michael@0: * RETURNS : void michael@0: * michael@0: * FUNCTION : Performs 2-tap linear interpolation in two dimensions. michael@0: * michael@0: * SPECIAL NOTES : Expansion is performed one band at a time to help with michael@0: * caching. michael@0: * michael@0: ****************************************************************************/ michael@0: static michael@0: void Scale2D michael@0: ( michael@0: /*const*/ michael@0: unsigned char *source, michael@0: int source_pitch, michael@0: unsigned int source_width, michael@0: unsigned int source_height, michael@0: unsigned char *dest, michael@0: int dest_pitch, michael@0: unsigned int dest_width, michael@0: unsigned int dest_height, michael@0: unsigned char *temp_area, michael@0: unsigned char temp_area_height, michael@0: unsigned int hscale, michael@0: unsigned int hratio, michael@0: unsigned int vscale, michael@0: unsigned int vratio, michael@0: unsigned int interlaced michael@0: ) { michael@0: /*unsigned*/ michael@0: int i, j, k; michael@0: int bands; michael@0: int dest_band_height; michael@0: int source_band_height; michael@0: michael@0: typedef void (*Scale1D)(const unsigned char * source, int source_step, unsigned int source_scale, unsigned int source_length, michael@0: unsigned char * dest, int dest_step, unsigned int dest_scale, unsigned int dest_length); michael@0: michael@0: Scale1D Scale1Dv = scale1d_c; michael@0: Scale1D Scale1Dh = scale1d_c; michael@0: michael@0: void (*horiz_line_scale)(const unsigned char *, unsigned int, unsigned char *, unsigned int) = NULL; michael@0: void (*vert_band_scale)(unsigned char *, unsigned int, unsigned char *, unsigned int, unsigned int) = NULL; michael@0: michael@0: int ratio_scalable = 1; michael@0: int interpolation = 0; michael@0: michael@0: unsigned char *source_base; /* = (unsigned char *) ((source_pitch >= 0) ? source : (source + ((source_height-1) * source_pitch))); */ michael@0: unsigned char *line_src; michael@0: michael@0: michael@0: source_base = (unsigned char *)source; michael@0: michael@0: if (source_pitch < 0) { michael@0: int offset; michael@0: michael@0: offset = (source_height - 1); michael@0: offset *= source_pitch; michael@0: michael@0: source_base += offset; michael@0: } michael@0: michael@0: /* find out the ratio for each direction */ michael@0: switch (hratio * 10 / hscale) { michael@0: case 8: michael@0: /* 4-5 Scale in Width direction */ michael@0: horiz_line_scale = vp8_horizontal_line_5_4_scale; michael@0: break; michael@0: case 6: michael@0: /* 3-5 Scale in Width direction */ michael@0: horiz_line_scale = vp8_horizontal_line_5_3_scale; michael@0: break; michael@0: case 5: michael@0: /* 1-2 Scale in Width direction */ michael@0: horiz_line_scale = vp8_horizontal_line_2_1_scale; michael@0: break; michael@0: default: michael@0: /* The ratio is not acceptable now */ michael@0: /* throw("The ratio is not acceptable for now!"); */ michael@0: ratio_scalable = 0; michael@0: break; michael@0: } michael@0: michael@0: switch (vratio * 10 / vscale) { michael@0: case 8: michael@0: /* 4-5 Scale in vertical direction */ michael@0: vert_band_scale = vp8_vertical_band_5_4_scale; michael@0: source_band_height = 5; michael@0: dest_band_height = 4; michael@0: break; michael@0: case 6: michael@0: /* 3-5 Scale in vertical direction */ michael@0: vert_band_scale = vp8_vertical_band_5_3_scale; michael@0: source_band_height = 5; michael@0: dest_band_height = 3; michael@0: break; michael@0: case 5: michael@0: /* 1-2 Scale in vertical direction */ michael@0: michael@0: if (interlaced) { michael@0: /* if the content is interlaced, point sampling is used */ michael@0: vert_band_scale = vp8_vertical_band_2_1_scale; michael@0: } else { michael@0: michael@0: interpolation = 1; michael@0: /* if the content is progressive, interplo */ michael@0: vert_band_scale = vp8_vertical_band_2_1_scale_i; michael@0: michael@0: } michael@0: michael@0: source_band_height = 2; michael@0: dest_band_height = 1; michael@0: break; michael@0: default: michael@0: /* The ratio is not acceptable now */ michael@0: /* throw("The ratio is not acceptable for now!"); */ michael@0: ratio_scalable = 0; michael@0: break; michael@0: } michael@0: michael@0: if (ratio_scalable) { michael@0: if (source_height == dest_height) { michael@0: /* for each band of the image */ michael@0: for (k = 0; k < (int)dest_height; k++) { michael@0: horiz_line_scale(source, source_width, dest, dest_width); michael@0: source += source_pitch; michael@0: dest += dest_pitch; michael@0: } michael@0: michael@0: return; michael@0: } michael@0: michael@0: if (interpolation) { michael@0: if (source < source_base) michael@0: source = source_base; michael@0: michael@0: horiz_line_scale(source, source_width, temp_area, dest_width); michael@0: } michael@0: michael@0: for (k = 0; k < (int)(dest_height + dest_band_height - 1) / dest_band_height; k++) { michael@0: /* scale one band horizontally */ michael@0: for (i = 0; i < source_band_height; i++) { michael@0: /* Trap case where we could read off the base of the source buffer */ michael@0: michael@0: line_src = (unsigned char *)source + i * source_pitch; michael@0: michael@0: if (line_src < source_base) michael@0: line_src = source_base; michael@0: michael@0: horiz_line_scale(line_src, source_width, michael@0: temp_area + (i + 1)*dest_pitch, dest_width); michael@0: } michael@0: michael@0: /* Vertical scaling is in place */ michael@0: vert_band_scale(temp_area + dest_pitch, dest_pitch, dest, dest_pitch, dest_width); michael@0: michael@0: if (interpolation) michael@0: vpx_memcpy(temp_area, temp_area + source_band_height * dest_pitch, dest_width); michael@0: michael@0: /* Next band... */ michael@0: source += (unsigned long) source_band_height * source_pitch; michael@0: dest += (unsigned long) dest_band_height * dest_pitch; michael@0: } michael@0: michael@0: return; michael@0: } michael@0: michael@0: if (hscale == 2 && hratio == 1) michael@0: Scale1Dh = scale1d_2t1_ps; michael@0: michael@0: if (vscale == 2 && vratio == 1) { michael@0: if (interlaced) michael@0: Scale1Dv = scale1d_2t1_ps; michael@0: else michael@0: Scale1Dv = scale1d_2t1_i; michael@0: } michael@0: michael@0: if (source_height == dest_height) { michael@0: /* for each band of the image */ michael@0: for (k = 0; k < (int)dest_height; k++) { michael@0: Scale1Dh(source, 1, hscale, source_width + 1, dest, 1, hratio, dest_width); michael@0: source += source_pitch; michael@0: dest += dest_pitch; michael@0: } michael@0: michael@0: return; michael@0: } michael@0: michael@0: if (dest_height > source_height) { michael@0: dest_band_height = temp_area_height - 1; michael@0: source_band_height = dest_band_height * source_height / dest_height; michael@0: } else { michael@0: source_band_height = temp_area_height - 1; michael@0: dest_band_height = source_band_height * vratio / vscale; michael@0: } michael@0: michael@0: /* first row needs to be done so that we can stay one row ahead for vertical zoom */ michael@0: Scale1Dh(source, 1, hscale, source_width + 1, temp_area, 1, hratio, dest_width); michael@0: michael@0: /* for each band of the image */ michael@0: bands = (dest_height + dest_band_height - 1) / dest_band_height; michael@0: michael@0: for (k = 0; k < bands; k++) { michael@0: /* scale one band horizontally */ michael@0: for (i = 1; i < source_band_height + 1; i++) { michael@0: if (k * source_band_height + i < (int) source_height) { michael@0: Scale1Dh(source + i * source_pitch, 1, hscale, source_width + 1, michael@0: temp_area + i * dest_pitch, 1, hratio, dest_width); michael@0: } else { /* Duplicate the last row */ michael@0: /* copy temp_area row 0 over from last row in the past */ michael@0: vpx_memcpy(temp_area + i * dest_pitch, temp_area + (i - 1)*dest_pitch, dest_pitch); michael@0: } michael@0: } michael@0: michael@0: /* scale one band vertically */ michael@0: for (j = 0; j < (int)dest_width; j++) { michael@0: Scale1Dv(&temp_area[j], dest_pitch, vscale, source_band_height + 1, michael@0: &dest[j], dest_pitch, vratio, dest_band_height); michael@0: } michael@0: michael@0: /* copy temp_area row 0 over from last row in the past */ michael@0: vpx_memcpy(temp_area, temp_area + source_band_height * dest_pitch, dest_pitch); michael@0: michael@0: /* move to the next band */ michael@0: source += source_band_height * source_pitch; michael@0: dest += dest_band_height * dest_pitch; michael@0: } michael@0: } michael@0: michael@0: /**************************************************************************** michael@0: * michael@0: * ROUTINE : vpx_scale_frame michael@0: * michael@0: * INPUTS : YV12_BUFFER_CONFIG *src : Pointer to frame to be scaled. michael@0: * YV12_BUFFER_CONFIG *dst : Pointer to buffer to hold scaled frame. michael@0: * unsigned char *temp_area : Pointer to temp work area. michael@0: * unsigned char temp_area_height : Height of temp work area. michael@0: * unsigned int hscale : Horizontal scale factor numerator. michael@0: * unsigned int hratio : Horizontal scale factor denominator. michael@0: * unsigned int vscale : Vertical scale factor numerator. michael@0: * unsigned int vratio : Vertical scale factor denominator. michael@0: * unsigned int interlaced : Interlace flag. michael@0: * michael@0: * OUTPUTS : None. michael@0: * michael@0: * RETURNS : void michael@0: * michael@0: * FUNCTION : Performs 2-tap linear interpolation in two dimensions. michael@0: * michael@0: * SPECIAL NOTES : Expansion is performed one band at a time to help with michael@0: * caching. michael@0: * michael@0: ****************************************************************************/ michael@0: void vpx_scale_frame michael@0: ( michael@0: YV12_BUFFER_CONFIG *src, michael@0: YV12_BUFFER_CONFIG *dst, michael@0: unsigned char *temp_area, michael@0: unsigned char temp_height, michael@0: unsigned int hscale, michael@0: unsigned int hratio, michael@0: unsigned int vscale, michael@0: unsigned int vratio, michael@0: unsigned int interlaced michael@0: ) { michael@0: int i; michael@0: int dw = (hscale - 1 + src->y_width * hratio) / hscale; michael@0: int dh = (vscale - 1 + src->y_height * vratio) / vscale; michael@0: michael@0: /* call our internal scaling routines!! */ michael@0: Scale2D((unsigned char *) src->y_buffer, src->y_stride, src->y_width, src->y_height, michael@0: (unsigned char *) dst->y_buffer, dst->y_stride, dw, dh, michael@0: temp_area, temp_height, hscale, hratio, vscale, vratio, interlaced); michael@0: michael@0: if (dw < (int)dst->y_width) michael@0: for (i = 0; i < dh; i++) michael@0: vpx_memset(dst->y_buffer + i * dst->y_stride + dw - 1, dst->y_buffer[i * dst->y_stride + dw - 2], dst->y_width - dw + 1); michael@0: michael@0: if (dh < (int)dst->y_height) michael@0: for (i = dh - 1; i < (int)dst->y_height; i++) michael@0: vpx_memcpy(dst->y_buffer + i * dst->y_stride, dst->y_buffer + (dh - 2) * dst->y_stride, dst->y_width + 1); michael@0: michael@0: Scale2D((unsigned char *) src->u_buffer, src->uv_stride, src->uv_width, src->uv_height, michael@0: (unsigned char *) dst->u_buffer, dst->uv_stride, dw / 2, dh / 2, michael@0: temp_area, temp_height, hscale, hratio, vscale, vratio, interlaced); michael@0: michael@0: if (dw / 2 < (int)dst->uv_width) michael@0: for (i = 0; i < dst->uv_height; i++) michael@0: vpx_memset(dst->u_buffer + i * dst->uv_stride + dw / 2 - 1, dst->u_buffer[i * dst->uv_stride + dw / 2 - 2], dst->uv_width - dw / 2 + 1); michael@0: michael@0: if (dh / 2 < (int)dst->uv_height) michael@0: for (i = dh / 2 - 1; i < (int)dst->y_height / 2; i++) michael@0: vpx_memcpy(dst->u_buffer + i * dst->uv_stride, dst->u_buffer + (dh / 2 - 2)*dst->uv_stride, dst->uv_width); michael@0: michael@0: Scale2D((unsigned char *) src->v_buffer, src->uv_stride, src->uv_width, src->uv_height, michael@0: (unsigned char *) dst->v_buffer, dst->uv_stride, dw / 2, dh / 2, michael@0: temp_area, temp_height, hscale, hratio, vscale, vratio, interlaced); michael@0: michael@0: if (dw / 2 < (int)dst->uv_width) michael@0: for (i = 0; i < dst->uv_height; i++) michael@0: vpx_memset(dst->v_buffer + i * dst->uv_stride + dw / 2 - 1, dst->v_buffer[i * dst->uv_stride + dw / 2 - 2], dst->uv_width - dw / 2 + 1); michael@0: michael@0: if (dh / 2 < (int) dst->uv_height) michael@0: for (i = dh / 2 - 1; i < (int)dst->y_height / 2; i++) michael@0: vpx_memcpy(dst->v_buffer + i * dst->uv_stride, dst->v_buffer + (dh / 2 - 2)*dst->uv_stride, dst->uv_width); michael@0: }