1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libjpeg/jdmerge.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,464 @@ 1.4 +/* 1.5 + * jdmerge.c 1.6 + * 1.7 + * This file was part of the Independent JPEG Group's software: 1.8 + * Copyright (C) 1994-1996, Thomas G. Lane. 1.9 + * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB 1.10 + * libjpeg-turbo Modifications: 1.11 + * Copyright (C) 2009, 2011, D. R. Commander. 1.12 + * For conditions of distribution and use, see the accompanying README file. 1.13 + * 1.14 + * This file contains code for merged upsampling/color conversion. 1.15 + * 1.16 + * This file combines functions from jdsample.c and jdcolor.c; 1.17 + * read those files first to understand what's going on. 1.18 + * 1.19 + * When the chroma components are to be upsampled by simple replication 1.20 + * (ie, box filtering), we can save some work in color conversion by 1.21 + * calculating all the output pixels corresponding to a pair of chroma 1.22 + * samples at one time. In the conversion equations 1.23 + * R = Y + K1 * Cr 1.24 + * G = Y + K2 * Cb + K3 * Cr 1.25 + * B = Y + K4 * Cb 1.26 + * only the Y term varies among the group of pixels corresponding to a pair 1.27 + * of chroma samples, so the rest of the terms can be calculated just once. 1.28 + * At typical sampling ratios, this eliminates half or three-quarters of the 1.29 + * multiplications needed for color conversion. 1.30 + * 1.31 + * This file currently provides implementations for the following cases: 1.32 + * YCbCr => RGB color conversion only. 1.33 + * Sampling ratios of 2h1v or 2h2v. 1.34 + * No scaling needed at upsample time. 1.35 + * Corner-aligned (non-CCIR601) sampling alignment. 1.36 + * Other special cases could be added, but in most applications these are 1.37 + * the only common cases. (For uncommon cases we fall back on the more 1.38 + * general code in jdsample.c and jdcolor.c.) 1.39 + */ 1.40 + 1.41 +#define JPEG_INTERNALS 1.42 +#include "jinclude.h" 1.43 +#include "jpeglib.h" 1.44 +#include "jsimd.h" 1.45 +#include "config.h" 1.46 + 1.47 +#ifdef UPSAMPLE_MERGING_SUPPORTED 1.48 + 1.49 + 1.50 +/* Private subobject */ 1.51 + 1.52 +typedef struct { 1.53 + struct jpeg_upsampler pub; /* public fields */ 1.54 + 1.55 + /* Pointer to routine to do actual upsampling/conversion of one row group */ 1.56 + JMETHOD(void, upmethod, (j_decompress_ptr cinfo, 1.57 + JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, 1.58 + JSAMPARRAY output_buf)); 1.59 + 1.60 + /* Private state for YCC->RGB conversion */ 1.61 + int * Cr_r_tab; /* => table for Cr to R conversion */ 1.62 + int * Cb_b_tab; /* => table for Cb to B conversion */ 1.63 + INT32 * Cr_g_tab; /* => table for Cr to G conversion */ 1.64 + INT32 * Cb_g_tab; /* => table for Cb to G conversion */ 1.65 + 1.66 + /* For 2:1 vertical sampling, we produce two output rows at a time. 1.67 + * We need a "spare" row buffer to hold the second output row if the 1.68 + * application provides just a one-row buffer; we also use the spare 1.69 + * to discard the dummy last row if the image height is odd. 1.70 + */ 1.71 + JSAMPROW spare_row; 1.72 + boolean spare_full; /* T if spare buffer is occupied */ 1.73 + 1.74 + JDIMENSION out_row_width; /* samples per output row */ 1.75 + JDIMENSION rows_to_go; /* counts rows remaining in image */ 1.76 +} my_upsampler; 1.77 + 1.78 +typedef my_upsampler * my_upsample_ptr; 1.79 + 1.80 +#define SCALEBITS 16 /* speediest right-shift on some machines */ 1.81 +#define ONE_HALF ((INT32) 1 << (SCALEBITS-1)) 1.82 +#define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5)) 1.83 + 1.84 + 1.85 +/* Include inline routines for colorspace extensions */ 1.86 + 1.87 +#include "jdmrgext.c" 1.88 +#undef RGB_RED 1.89 +#undef RGB_GREEN 1.90 +#undef RGB_BLUE 1.91 +#undef RGB_PIXELSIZE 1.92 + 1.93 +#define RGB_RED EXT_RGB_RED 1.94 +#define RGB_GREEN EXT_RGB_GREEN 1.95 +#define RGB_BLUE EXT_RGB_BLUE 1.96 +#define RGB_PIXELSIZE EXT_RGB_PIXELSIZE 1.97 +#define h2v1_merged_upsample_internal extrgb_h2v1_merged_upsample_internal 1.98 +#define h2v2_merged_upsample_internal extrgb_h2v2_merged_upsample_internal 1.99 +#include "jdmrgext.c" 1.100 +#undef RGB_RED 1.101 +#undef RGB_GREEN 1.102 +#undef RGB_BLUE 1.103 +#undef RGB_PIXELSIZE 1.104 +#undef h2v1_merged_upsample_internal 1.105 +#undef h2v2_merged_upsample_internal 1.106 + 1.107 +#define RGB_RED EXT_RGBX_RED 1.108 +#define RGB_GREEN EXT_RGBX_GREEN 1.109 +#define RGB_BLUE EXT_RGBX_BLUE 1.110 +#define RGB_ALPHA 3 1.111 +#define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE 1.112 +#define h2v1_merged_upsample_internal extrgbx_h2v1_merged_upsample_internal 1.113 +#define h2v2_merged_upsample_internal extrgbx_h2v2_merged_upsample_internal 1.114 +#include "jdmrgext.c" 1.115 +#undef RGB_RED 1.116 +#undef RGB_GREEN 1.117 +#undef RGB_BLUE 1.118 +#undef RGB_ALPHA 1.119 +#undef RGB_PIXELSIZE 1.120 +#undef h2v1_merged_upsample_internal 1.121 +#undef h2v2_merged_upsample_internal 1.122 + 1.123 +#define RGB_RED EXT_BGR_RED 1.124 +#define RGB_GREEN EXT_BGR_GREEN 1.125 +#define RGB_BLUE EXT_BGR_BLUE 1.126 +#define RGB_PIXELSIZE EXT_BGR_PIXELSIZE 1.127 +#define h2v1_merged_upsample_internal extbgr_h2v1_merged_upsample_internal 1.128 +#define h2v2_merged_upsample_internal extbgr_h2v2_merged_upsample_internal 1.129 +#include "jdmrgext.c" 1.130 +#undef RGB_RED 1.131 +#undef RGB_GREEN 1.132 +#undef RGB_BLUE 1.133 +#undef RGB_PIXELSIZE 1.134 +#undef h2v1_merged_upsample_internal 1.135 +#undef h2v2_merged_upsample_internal 1.136 + 1.137 +#define RGB_RED EXT_BGRX_RED 1.138 +#define RGB_GREEN EXT_BGRX_GREEN 1.139 +#define RGB_BLUE EXT_BGRX_BLUE 1.140 +#define RGB_ALPHA 3 1.141 +#define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE 1.142 +#define h2v1_merged_upsample_internal extbgrx_h2v1_merged_upsample_internal 1.143 +#define h2v2_merged_upsample_internal extbgrx_h2v2_merged_upsample_internal 1.144 +#include "jdmrgext.c" 1.145 +#undef RGB_RED 1.146 +#undef RGB_GREEN 1.147 +#undef RGB_BLUE 1.148 +#undef RGB_ALPHA 1.149 +#undef RGB_PIXELSIZE 1.150 +#undef h2v1_merged_upsample_internal 1.151 +#undef h2v2_merged_upsample_internal 1.152 + 1.153 +#define RGB_RED EXT_XBGR_RED 1.154 +#define RGB_GREEN EXT_XBGR_GREEN 1.155 +#define RGB_BLUE EXT_XBGR_BLUE 1.156 +#define RGB_ALPHA 0 1.157 +#define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE 1.158 +#define h2v1_merged_upsample_internal extxbgr_h2v1_merged_upsample_internal 1.159 +#define h2v2_merged_upsample_internal extxbgr_h2v2_merged_upsample_internal 1.160 +#include "jdmrgext.c" 1.161 +#undef RGB_RED 1.162 +#undef RGB_GREEN 1.163 +#undef RGB_BLUE 1.164 +#undef RGB_ALPHA 1.165 +#undef RGB_PIXELSIZE 1.166 +#undef h2v1_merged_upsample_internal 1.167 +#undef h2v2_merged_upsample_internal 1.168 + 1.169 +#define RGB_RED EXT_XRGB_RED 1.170 +#define RGB_GREEN EXT_XRGB_GREEN 1.171 +#define RGB_BLUE EXT_XRGB_BLUE 1.172 +#define RGB_ALPHA 0 1.173 +#define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE 1.174 +#define h2v1_merged_upsample_internal extxrgb_h2v1_merged_upsample_internal 1.175 +#define h2v2_merged_upsample_internal extxrgb_h2v2_merged_upsample_internal 1.176 +#include "jdmrgext.c" 1.177 +#undef RGB_RED 1.178 +#undef RGB_GREEN 1.179 +#undef RGB_BLUE 1.180 +#undef RGB_ALPHA 1.181 +#undef RGB_PIXELSIZE 1.182 +#undef h2v1_merged_upsample_internal 1.183 +#undef h2v2_merged_upsample_internal 1.184 + 1.185 + 1.186 +/* 1.187 + * Initialize tables for YCC->RGB colorspace conversion. 1.188 + * This is taken directly from jdcolor.c; see that file for more info. 1.189 + */ 1.190 + 1.191 +LOCAL(void) 1.192 +build_ycc_rgb_table (j_decompress_ptr cinfo) 1.193 +{ 1.194 + my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; 1.195 + int i; 1.196 + INT32 x; 1.197 + SHIFT_TEMPS 1.198 + 1.199 + upsample->Cr_r_tab = (int *) 1.200 + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.201 + (MAXJSAMPLE+1) * SIZEOF(int)); 1.202 + upsample->Cb_b_tab = (int *) 1.203 + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.204 + (MAXJSAMPLE+1) * SIZEOF(int)); 1.205 + upsample->Cr_g_tab = (INT32 *) 1.206 + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.207 + (MAXJSAMPLE+1) * SIZEOF(INT32)); 1.208 + upsample->Cb_g_tab = (INT32 *) 1.209 + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.210 + (MAXJSAMPLE+1) * SIZEOF(INT32)); 1.211 + 1.212 + for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) { 1.213 + /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */ 1.214 + /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */ 1.215 + /* Cr=>R value is nearest int to 1.40200 * x */ 1.216 + upsample->Cr_r_tab[i] = (int) 1.217 + RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS); 1.218 + /* Cb=>B value is nearest int to 1.77200 * x */ 1.219 + upsample->Cb_b_tab[i] = (int) 1.220 + RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS); 1.221 + /* Cr=>G value is scaled-up -0.71414 * x */ 1.222 + upsample->Cr_g_tab[i] = (- FIX(0.71414)) * x; 1.223 + /* Cb=>G value is scaled-up -0.34414 * x */ 1.224 + /* We also add in ONE_HALF so that need not do it in inner loop */ 1.225 + upsample->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF; 1.226 + } 1.227 +} 1.228 + 1.229 + 1.230 +/* 1.231 + * Initialize for an upsampling pass. 1.232 + */ 1.233 + 1.234 +METHODDEF(void) 1.235 +start_pass_merged_upsample (j_decompress_ptr cinfo) 1.236 +{ 1.237 + my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; 1.238 + 1.239 + /* Mark the spare buffer empty */ 1.240 + upsample->spare_full = FALSE; 1.241 + /* Initialize total-height counter for detecting bottom of image */ 1.242 + upsample->rows_to_go = cinfo->output_height; 1.243 +} 1.244 + 1.245 + 1.246 +/* 1.247 + * Control routine to do upsampling (and color conversion). 1.248 + * 1.249 + * The control routine just handles the row buffering considerations. 1.250 + */ 1.251 + 1.252 +METHODDEF(void) 1.253 +merged_2v_upsample (j_decompress_ptr cinfo, 1.254 + JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, 1.255 + JDIMENSION in_row_groups_avail, 1.256 + JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, 1.257 + JDIMENSION out_rows_avail) 1.258 +/* 2:1 vertical sampling case: may need a spare row. */ 1.259 +{ 1.260 + my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; 1.261 + JSAMPROW work_ptrs[2]; 1.262 + JDIMENSION num_rows; /* number of rows returned to caller */ 1.263 + 1.264 + if (upsample->spare_full) { 1.265 + /* If we have a spare row saved from a previous cycle, just return it. */ 1.266 + jcopy_sample_rows(& upsample->spare_row, 0, output_buf + *out_row_ctr, 0, 1.267 + 1, upsample->out_row_width); 1.268 + num_rows = 1; 1.269 + upsample->spare_full = FALSE; 1.270 + } else { 1.271 + /* Figure number of rows to return to caller. */ 1.272 + num_rows = 2; 1.273 + /* Not more than the distance to the end of the image. */ 1.274 + if (num_rows > upsample->rows_to_go) 1.275 + num_rows = upsample->rows_to_go; 1.276 + /* And not more than what the client can accept: */ 1.277 + out_rows_avail -= *out_row_ctr; 1.278 + if (num_rows > out_rows_avail) 1.279 + num_rows = out_rows_avail; 1.280 + /* Create output pointer array for upsampler. */ 1.281 + work_ptrs[0] = output_buf[*out_row_ctr]; 1.282 + if (num_rows > 1) { 1.283 + work_ptrs[1] = output_buf[*out_row_ctr + 1]; 1.284 + } else { 1.285 + work_ptrs[1] = upsample->spare_row; 1.286 + upsample->spare_full = TRUE; 1.287 + } 1.288 + /* Now do the upsampling. */ 1.289 + (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs); 1.290 + } 1.291 + 1.292 + /* Adjust counts */ 1.293 + *out_row_ctr += num_rows; 1.294 + upsample->rows_to_go -= num_rows; 1.295 + /* When the buffer is emptied, declare this input row group consumed */ 1.296 + if (! upsample->spare_full) 1.297 + (*in_row_group_ctr)++; 1.298 +} 1.299 + 1.300 + 1.301 +METHODDEF(void) 1.302 +merged_1v_upsample (j_decompress_ptr cinfo, 1.303 + JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, 1.304 + JDIMENSION in_row_groups_avail, 1.305 + JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, 1.306 + JDIMENSION out_rows_avail) 1.307 +/* 1:1 vertical sampling case: much easier, never need a spare row. */ 1.308 +{ 1.309 + my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; 1.310 + 1.311 + /* Just do the upsampling. */ 1.312 + (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, 1.313 + output_buf + *out_row_ctr); 1.314 + /* Adjust counts */ 1.315 + (*out_row_ctr)++; 1.316 + (*in_row_group_ctr)++; 1.317 +} 1.318 + 1.319 + 1.320 +/* 1.321 + * These are the routines invoked by the control routines to do 1.322 + * the actual upsampling/conversion. One row group is processed per call. 1.323 + * 1.324 + * Note: since we may be writing directly into application-supplied buffers, 1.325 + * we have to be honest about the output width; we can't assume the buffer 1.326 + * has been rounded up to an even width. 1.327 + */ 1.328 + 1.329 + 1.330 +/* 1.331 + * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical. 1.332 + */ 1.333 + 1.334 +METHODDEF(void) 1.335 +h2v1_merged_upsample (j_decompress_ptr cinfo, 1.336 + JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, 1.337 + JSAMPARRAY output_buf) 1.338 +{ 1.339 + switch (cinfo->out_color_space) { 1.340 + case JCS_EXT_RGB: 1.341 + extrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, 1.342 + output_buf); 1.343 + break; 1.344 + case JCS_EXT_RGBX: 1.345 + case JCS_EXT_RGBA: 1.346 + extrgbx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, 1.347 + output_buf); 1.348 + break; 1.349 + case JCS_EXT_BGR: 1.350 + extbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, 1.351 + output_buf); 1.352 + break; 1.353 + case JCS_EXT_BGRX: 1.354 + case JCS_EXT_BGRA: 1.355 + extbgrx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, 1.356 + output_buf); 1.357 + break; 1.358 + case JCS_EXT_XBGR: 1.359 + case JCS_EXT_ABGR: 1.360 + extxbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, 1.361 + output_buf); 1.362 + break; 1.363 + case JCS_EXT_XRGB: 1.364 + case JCS_EXT_ARGB: 1.365 + extxrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, 1.366 + output_buf); 1.367 + break; 1.368 + default: 1.369 + h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, 1.370 + output_buf); 1.371 + break; 1.372 + } 1.373 +} 1.374 + 1.375 + 1.376 +/* 1.377 + * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical. 1.378 + */ 1.379 + 1.380 +METHODDEF(void) 1.381 +h2v2_merged_upsample (j_decompress_ptr cinfo, 1.382 + JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, 1.383 + JSAMPARRAY output_buf) 1.384 +{ 1.385 + switch (cinfo->out_color_space) { 1.386 + case JCS_EXT_RGB: 1.387 + extrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, 1.388 + output_buf); 1.389 + break; 1.390 + case JCS_EXT_RGBX: 1.391 + case JCS_EXT_RGBA: 1.392 + extrgbx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, 1.393 + output_buf); 1.394 + break; 1.395 + case JCS_EXT_BGR: 1.396 + extbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, 1.397 + output_buf); 1.398 + break; 1.399 + case JCS_EXT_BGRX: 1.400 + case JCS_EXT_BGRA: 1.401 + extbgrx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, 1.402 + output_buf); 1.403 + break; 1.404 + case JCS_EXT_XBGR: 1.405 + case JCS_EXT_ABGR: 1.406 + extxbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, 1.407 + output_buf); 1.408 + break; 1.409 + case JCS_EXT_XRGB: 1.410 + case JCS_EXT_ARGB: 1.411 + extxrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, 1.412 + output_buf); 1.413 + break; 1.414 + default: 1.415 + h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, 1.416 + output_buf); 1.417 + break; 1.418 + } 1.419 +} 1.420 + 1.421 + 1.422 +/* 1.423 + * Module initialization routine for merged upsampling/color conversion. 1.424 + * 1.425 + * NB: this is called under the conditions determined by use_merged_upsample() 1.426 + * in jdmaster.c. That routine MUST correspond to the actual capabilities 1.427 + * of this module; no safety checks are made here. 1.428 + */ 1.429 + 1.430 +GLOBAL(void) 1.431 +jinit_merged_upsampler (j_decompress_ptr cinfo) 1.432 +{ 1.433 + my_upsample_ptr upsample; 1.434 + 1.435 + upsample = (my_upsample_ptr) 1.436 + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.437 + SIZEOF(my_upsampler)); 1.438 + cinfo->upsample = (struct jpeg_upsampler *) upsample; 1.439 + upsample->pub.start_pass = start_pass_merged_upsample; 1.440 + upsample->pub.need_context_rows = FALSE; 1.441 + 1.442 + upsample->out_row_width = cinfo->output_width * cinfo->out_color_components; 1.443 + 1.444 + if (cinfo->max_v_samp_factor == 2) { 1.445 + upsample->pub.upsample = merged_2v_upsample; 1.446 + if (jsimd_can_h2v2_merged_upsample()) 1.447 + upsample->upmethod = jsimd_h2v2_merged_upsample; 1.448 + else 1.449 + upsample->upmethod = h2v2_merged_upsample; 1.450 + /* Allocate a spare row buffer */ 1.451 + upsample->spare_row = (JSAMPROW) 1.452 + (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.453 + (size_t) (upsample->out_row_width * SIZEOF(JSAMPLE))); 1.454 + } else { 1.455 + upsample->pub.upsample = merged_1v_upsample; 1.456 + if (jsimd_can_h2v1_merged_upsample()) 1.457 + upsample->upmethod = jsimd_h2v1_merged_upsample; 1.458 + else 1.459 + upsample->upmethod = h2v1_merged_upsample; 1.460 + /* No spare row needed */ 1.461 + upsample->spare_row = NULL; 1.462 + } 1.463 + 1.464 + build_ycc_rgb_table(cinfo); 1.465 +} 1.466 + 1.467 +#endif /* UPSAMPLE_MERGING_SUPPORTED */