blob: eaad72a030898f6c688c4b3d57d7d9a3a30970f3 [file] [log] [blame]
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001/*
2 * jdsample.c
3 *
noel@chromium.org3395bcc2014-04-14 06:56:00 +00004 * This file was part of the Independent JPEG Group's software:
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00005 * Copyright (C) 1991-1996, Thomas G. Lane.
noel@chromium.org3395bcc2014-04-14 06:56:00 +00006 * libjpeg-turbo Modifications:
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00007 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
Tom Hudson0d47d2d2016-05-04 13:22:56 -04008 * Copyright (C) 2010, 2015-2016, D. R. Commander.
Chris Blumecca8c4d2019-03-01 01:09:50 -08009 * Copyright (C) 2014, MIPS Technologies, Inc., California.
Tom Hudson0d47d2d2016-05-04 13:22:56 -040010 * Copyright (C) 2015, Google, Inc.
Jonathan Wrightbbb82822020-11-25 13:36:43 +000011 * Copyright (C) 2019-2020, Arm Limited.
Tom Hudson0d47d2d2016-05-04 13:22:56 -040012 * For conditions of distribution and use, see the accompanying README.ijg
13 * file.
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000014 *
15 * This file contains upsampling routines.
16 *
17 * Upsampling input data is counted in "row groups". A row group
18 * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
19 * sample rows of each component. Upsampling will normally produce
20 * max_v_samp_factor pixel rows from each row group (but this could vary
21 * if the upsampler is applying a scale factor of its own).
22 *
23 * An excellent reference for image resampling is
24 * Digital Image Warping, George Wolberg, 1990.
25 * Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
26 */
27
Tom Hudson0d47d2d2016-05-04 13:22:56 -040028#include "jinclude.h"
Aaron Gablec9c87552015-08-03 09:34:32 -070029#include "jdsample.h"
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000030#include "jsimd.h"
hbono@chromium.org98626972011-08-03 03:13:08 +000031#include "jpegcomp.h"
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000032
33
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000034
35/*
36 * Initialize for an upsampling pass.
37 */
38
39METHODDEF(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -080040start_pass_upsample(j_decompress_ptr cinfo)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000041{
Chris Blumecca8c4d2019-03-01 01:09:50 -080042 my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000043
44 /* Mark the conversion buffer empty */
45 upsample->next_row_out = cinfo->max_v_samp_factor;
46 /* Initialize total-height counter for detecting bottom of image */
47 upsample->rows_to_go = cinfo->output_height;
48}
49
50
51/*
52 * Control routine to do upsampling (and color conversion).
53 *
54 * In this version we upsample each component independently.
55 * We upsample one row group into the conversion buffer, then apply
56 * color conversion a row at a time.
57 */
58
59METHODDEF(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -080060sep_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
61 JDIMENSION *in_row_group_ctr, JDIMENSION in_row_groups_avail,
62 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
63 JDIMENSION out_rows_avail)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000064{
Chris Blumecca8c4d2019-03-01 01:09:50 -080065 my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000066 int ci;
Tom Hudson0d47d2d2016-05-04 13:22:56 -040067 jpeg_component_info *compptr;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000068 JDIMENSION num_rows;
69
70 /* Fill the conversion buffer, if it's empty */
71 if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
72 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
Tom Hudson0d47d2d2016-05-04 13:22:56 -040073 ci++, compptr++) {
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000074 /* Invoke per-component upsample method. Notice we pass a POINTER
75 * to color_buf[ci], so that fullsize_upsample can change it.
76 */
77 (*upsample->methods[ci]) (cinfo, compptr,
Tom Hudson0d47d2d2016-05-04 13:22:56 -040078 input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
79 upsample->color_buf + ci);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000080 }
81 upsample->next_row_out = 0;
82 }
83
84 /* Color-convert and emit rows */
85
86 /* How many we have in the buffer: */
Chris Blumecca8c4d2019-03-01 01:09:50 -080087 num_rows = (JDIMENSION)(cinfo->max_v_samp_factor - upsample->next_row_out);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000088 /* Not more than the distance to the end of the image. Need this test
89 * in case the image height is not a multiple of max_v_samp_factor:
90 */
Tom Hudson0d47d2d2016-05-04 13:22:56 -040091 if (num_rows > upsample->rows_to_go)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000092 num_rows = upsample->rows_to_go;
93 /* And not more than what the client can accept: */
94 out_rows_avail -= *out_row_ctr;
95 if (num_rows > out_rows_avail)
96 num_rows = out_rows_avail;
97
98 (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf,
Chris Blumecca8c4d2019-03-01 01:09:50 -080099 (JDIMENSION)upsample->next_row_out,
100 output_buf + *out_row_ctr, (int)num_rows);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000101
102 /* Adjust counts */
103 *out_row_ctr += num_rows;
104 upsample->rows_to_go -= num_rows;
105 upsample->next_row_out += num_rows;
106 /* When the buffer is emptied, declare this input row group consumed */
107 if (upsample->next_row_out >= cinfo->max_v_samp_factor)
108 (*in_row_group_ctr)++;
109}
110
111
112/*
113 * These are the routines invoked by sep_upsample to upsample pixel values
114 * of a single component. One row group is processed per call.
115 */
116
117
118/*
119 * For full-size components, we just make color_buf[ci] point at the
120 * input buffer, and thus avoid copying any data. Note that this is
121 * safe only because sep_upsample doesn't declare the input row group
122 * "consumed" until we are done color converting and emitting it.
123 */
124
125METHODDEF(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800126fullsize_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
127 JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000128{
129 *output_data_ptr = input_data;
130}
131
132
133/*
134 * This is a no-op version used for "uninteresting" components.
135 * These components will not be referenced by color conversion.
136 */
137
138METHODDEF(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800139noop_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
140 JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000141{
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400142 *output_data_ptr = NULL; /* safety check */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000143}
144
145
146/*
147 * This version handles any integral sampling ratios.
148 * This is not used for typical JPEG files, so it need not be fast.
149 * Nor, for that matter, is it particularly accurate: the algorithm is
150 * simple replication of the input pixel onto the corresponding output
151 * pixels. The hi-falutin sampling literature refers to this as a
152 * "box filter". A box filter tends to introduce visible artifacts,
153 * so if you are actually going to use 3:1 or 4:1 sampling ratios
154 * you would be well advised to improve this code.
155 */
156
157METHODDEF(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800158int_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
159 JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000160{
Chris Blumecca8c4d2019-03-01 01:09:50 -0800161 my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000162 JSAMPARRAY output_data = *output_data_ptr;
163 register JSAMPROW inptr, outptr;
164 register JSAMPLE invalue;
165 register int h;
166 JSAMPROW outend;
167 int h_expand, v_expand;
168 int inrow, outrow;
169
170 h_expand = upsample->h_expand[compptr->component_index];
171 v_expand = upsample->v_expand[compptr->component_index];
172
173 inrow = outrow = 0;
174 while (outrow < cinfo->max_v_samp_factor) {
175 /* Generate one output row with proper horizontal expansion */
176 inptr = input_data[inrow];
177 outptr = output_data[outrow];
178 outend = outptr + cinfo->output_width;
179 while (outptr < outend) {
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000180 invalue = *inptr++;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000181 for (h = h_expand; h > 0; h--) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400182 *outptr++ = invalue;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000183 }
184 }
185 /* Generate any additional output rows by duplicating the first one */
186 if (v_expand > 1) {
Chris Blumecca8c4d2019-03-01 01:09:50 -0800187 jcopy_sample_rows(output_data, outrow, output_data, outrow + 1,
188 v_expand - 1, cinfo->output_width);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000189 }
190 inrow++;
191 outrow += v_expand;
192 }
193}
194
195
196/*
197 * Fast processing for the common case of 2:1 horizontal and 1:1 vertical.
198 * It's still a box filter.
199 */
200
201METHODDEF(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800202h2v1_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
203 JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000204{
205 JSAMPARRAY output_data = *output_data_ptr;
206 register JSAMPROW inptr, outptr;
207 register JSAMPLE invalue;
208 JSAMPROW outend;
209 int inrow;
210
211 for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
212 inptr = input_data[inrow];
213 outptr = output_data[inrow];
214 outend = outptr + cinfo->output_width;
215 while (outptr < outend) {
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000216 invalue = *inptr++;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000217 *outptr++ = invalue;
218 *outptr++ = invalue;
219 }
220 }
221}
222
223
224/*
225 * Fast processing for the common case of 2:1 horizontal and 2:1 vertical.
226 * It's still a box filter.
227 */
228
229METHODDEF(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800230h2v2_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
231 JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000232{
233 JSAMPARRAY output_data = *output_data_ptr;
234 register JSAMPROW inptr, outptr;
235 register JSAMPLE invalue;
236 JSAMPROW outend;
237 int inrow, outrow;
238
239 inrow = outrow = 0;
240 while (outrow < cinfo->max_v_samp_factor) {
241 inptr = input_data[inrow];
242 outptr = output_data[outrow];
243 outend = outptr + cinfo->output_width;
244 while (outptr < outend) {
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000245 invalue = *inptr++;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000246 *outptr++ = invalue;
247 *outptr++ = invalue;
248 }
Chris Blumecca8c4d2019-03-01 01:09:50 -0800249 jcopy_sample_rows(output_data, outrow, output_data, outrow + 1, 1,
250 cinfo->output_width);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000251 inrow++;
252 outrow += 2;
253 }
254}
255
256
257/*
258 * Fancy processing for the common case of 2:1 horizontal and 1:1 vertical.
259 *
260 * The upsampling algorithm is linear interpolation between pixel centers,
261 * also known as a "triangle filter". This is a good compromise between
262 * speed and visual quality. The centers of the output pixels are 1/4 and 3/4
263 * of the way between input pixel centers.
264 *
265 * A note about the "bias" calculations: when rounding fractional values to
266 * integer, we do not want to always round 0.5 up to the next integer.
267 * If we did that, we'd introduce a noticeable bias towards larger values.
268 * Instead, this code is arranged so that 0.5 will be rounded up or down at
269 * alternate pixel locations (a simple ordered dither pattern).
270 */
271
272METHODDEF(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800273h2v1_fancy_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
274 JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000275{
276 JSAMPARRAY output_data = *output_data_ptr;
277 register JSAMPROW inptr, outptr;
278 register int invalue;
279 register JDIMENSION colctr;
280 int inrow;
281
282 for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
283 inptr = input_data[inrow];
284 outptr = output_data[inrow];
285 /* Special case for first column */
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000286 invalue = *inptr++;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800287 *outptr++ = (JSAMPLE)invalue;
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000288 *outptr++ = (JSAMPLE)((invalue * 3 + inptr[0] + 2) >> 2);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000289
290 for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
291 /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000292 invalue = (*inptr++) * 3;
293 *outptr++ = (JSAMPLE)((invalue + inptr[-2] + 1) >> 2);
294 *outptr++ = (JSAMPLE)((invalue + inptr[0] + 2) >> 2);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000295 }
296
297 /* Special case for last column */
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000298 invalue = *inptr;
299 *outptr++ = (JSAMPLE)((invalue * 3 + inptr[-1] + 1) >> 2);
Chris Blumecca8c4d2019-03-01 01:09:50 -0800300 *outptr++ = (JSAMPLE)invalue;
301 }
302}
303
304
305/*
306 * Fancy processing for 1:1 horizontal and 2:1 vertical (4:4:0 subsampling).
307 *
308 * This is a less common case, but it can be encountered when losslessly
309 * rotating/transposing a JPEG file that uses 4:2:2 chroma subsampling.
310 */
311
312METHODDEF(void)
313h1v2_fancy_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
314 JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
315{
316 JSAMPARRAY output_data = *output_data_ptr;
317 JSAMPROW inptr0, inptr1, outptr;
318#if BITS_IN_JSAMPLE == 8
Jonathan Wrightd78acdd2019-05-09 13:46:53 +0100319 int thiscolsum, bias;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800320#else
Jonathan Wrightd78acdd2019-05-09 13:46:53 +0100321 JLONG thiscolsum, bias;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800322#endif
323 JDIMENSION colctr;
324 int inrow, outrow, v;
325
326 inrow = outrow = 0;
327 while (outrow < cinfo->max_v_samp_factor) {
328 for (v = 0; v < 2; v++) {
329 /* inptr0 points to nearest input row, inptr1 points to next nearest */
330 inptr0 = input_data[inrow];
Jonathan Wrightd78acdd2019-05-09 13:46:53 +0100331 if (v == 0) { /* next nearest is row above */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800332 inptr1 = input_data[inrow - 1];
Jonathan Wrightd78acdd2019-05-09 13:46:53 +0100333 bias = 1;
334 } else { /* next nearest is row below */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800335 inptr1 = input_data[inrow + 1];
Jonathan Wrightd78acdd2019-05-09 13:46:53 +0100336 bias = 2;
337 }
Chris Blumecca8c4d2019-03-01 01:09:50 -0800338 outptr = output_data[outrow++];
339
340 for (colctr = 0; colctr < compptr->downsampled_width; colctr++) {
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000341 thiscolsum = (*inptr0++) * 3 + (*inptr1++);
Jonathan Wrightd78acdd2019-05-09 13:46:53 +0100342 *outptr++ = (JSAMPLE)((thiscolsum + bias) >> 2);
Chris Blumecca8c4d2019-03-01 01:09:50 -0800343 }
344 }
345 inrow++;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000346 }
347}
348
349
350/*
351 * Fancy processing for the common case of 2:1 horizontal and 2:1 vertical.
352 * Again a triangle filter; see comments for h2v1 case, above.
353 *
354 * It is OK for us to reference the adjacent input rows because we demanded
355 * context from the main buffer controller (see initialization code).
356 */
357
358METHODDEF(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800359h2v2_fancy_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
360 JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000361{
362 JSAMPARRAY output_data = *output_data_ptr;
363 register JSAMPROW inptr0, inptr1, outptr;
364#if BITS_IN_JSAMPLE == 8
365 register int thiscolsum, lastcolsum, nextcolsum;
366#else
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400367 register JLONG thiscolsum, lastcolsum, nextcolsum;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000368#endif
369 register JDIMENSION colctr;
370 int inrow, outrow, v;
371
372 inrow = outrow = 0;
373 while (outrow < cinfo->max_v_samp_factor) {
374 for (v = 0; v < 2; v++) {
375 /* inptr0 points to nearest input row, inptr1 points to next nearest */
376 inptr0 = input_data[inrow];
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400377 if (v == 0) /* next nearest is row above */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800378 inptr1 = input_data[inrow - 1];
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400379 else /* next nearest is row below */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800380 inptr1 = input_data[inrow + 1];
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000381 outptr = output_data[outrow++];
382
383 /* Special case for first column */
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000384 thiscolsum = (*inptr0++) * 3 + (*inptr1++);
385 nextcolsum = (*inptr0++) * 3 + (*inptr1++);
Chris Blumecca8c4d2019-03-01 01:09:50 -0800386 *outptr++ = (JSAMPLE)((thiscolsum * 4 + 8) >> 4);
387 *outptr++ = (JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
388 lastcolsum = thiscolsum; thiscolsum = nextcolsum;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000389
390 for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400391 /* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */
392 /* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000393 nextcolsum = (*inptr0++) * 3 + (*inptr1++);
Chris Blumecca8c4d2019-03-01 01:09:50 -0800394 *outptr++ = (JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
395 *outptr++ = (JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
396 lastcolsum = thiscolsum; thiscolsum = nextcolsum;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000397 }
398
399 /* Special case for last column */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800400 *outptr++ = (JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
401 *outptr++ = (JSAMPLE)((thiscolsum * 4 + 7) >> 4);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000402 }
403 inrow++;
404 }
405}
406
407
408/*
409 * Module initialization routine for upsampling.
410 */
411
412GLOBAL(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800413jinit_upsampler(j_decompress_ptr cinfo)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000414{
415 my_upsample_ptr upsample;
416 int ci;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400417 jpeg_component_info *compptr;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000418 boolean need_buffer, do_fancy;
419 int h_in_group, v_in_group, h_out_group, v_out_group;
420
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400421 if (!cinfo->master->jinit_upsampler_no_alloc) {
422 upsample = (my_upsample_ptr)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800423 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400424 sizeof(my_upsampler));
Chris Blumecca8c4d2019-03-01 01:09:50 -0800425 cinfo->upsample = (struct jpeg_upsampler *)upsample;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400426 upsample->pub.start_pass = start_pass_upsample;
427 upsample->pub.upsample = sep_upsample;
428 upsample->pub.need_context_rows = FALSE; /* until we find out differently */
429 } else
Chris Blumecca8c4d2019-03-01 01:09:50 -0800430 upsample = (my_upsample_ptr)cinfo->upsample;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000431
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400432 if (cinfo->CCIR601_sampling) /* this isn't supported */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000433 ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
434
435 /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
436 * so don't ask for it.
437 */
hbono@chromium.org98626972011-08-03 03:13:08 +0000438 do_fancy = cinfo->do_fancy_upsampling && cinfo->_min_DCT_scaled_size > 1;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000439
440 /* Verify we can handle the sampling factors, select per-component methods,
441 * and create storage as needed.
442 */
443 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
444 ci++, compptr++) {
445 /* Compute size of an "input group" after IDCT scaling. This many samples
446 * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
447 */
hbono@chromium.org98626972011-08-03 03:13:08 +0000448 h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400449 cinfo->_min_DCT_scaled_size;
hbono@chromium.org98626972011-08-03 03:13:08 +0000450 v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400451 cinfo->_min_DCT_scaled_size;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000452 h_out_group = cinfo->max_h_samp_factor;
453 v_out_group = cinfo->max_v_samp_factor;
454 upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
455 need_buffer = TRUE;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800456 if (!compptr->component_needed) {
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000457 /* Don't bother to upsample an uninteresting component. */
458 upsample->methods[ci] = noop_upsample;
459 need_buffer = FALSE;
460 } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
461 /* Fullsize components can be processed without any work. */
462 upsample->methods[ci] = fullsize_upsample;
463 need_buffer = FALSE;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800464 } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000465 /* Special cases for 2h1v upsampling */
466 if (do_fancy && compptr->downsampled_width > 2) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400467 if (jsimd_can_h2v1_fancy_upsample())
468 upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
469 else
470 upsample->methods[ci] = h2v1_fancy_upsample;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000471 } else {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400472 if (jsimd_can_h2v1_upsample())
473 upsample->methods[ci] = jsimd_h2v1_upsample;
474 else
475 upsample->methods[ci] = h2v1_upsample;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000476 }
Chris Blumecca8c4d2019-03-01 01:09:50 -0800477 } else if (h_in_group == h_out_group &&
478 v_in_group * 2 == v_out_group && do_fancy) {
479 /* Non-fancy upsampling is handled by the generic method */
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000480#if defined(__arm__) || defined(__aarch64__) || \
481 defined(_M_ARM) || defined(_M_ARM64)
Jonathan Wright0927aa32019-05-08 15:43:26 +0100482 if (jsimd_can_h1v2_fancy_upsample())
483 upsample->methods[ci] = jsimd_h1v2_fancy_upsample;
484 else
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000485#endif
Jonathan Wright0927aa32019-05-08 15:43:26 +0100486 upsample->methods[ci] = h1v2_fancy_upsample;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800487 upsample->pub.need_context_rows = TRUE;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000488 } else if (h_in_group * 2 == h_out_group &&
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400489 v_in_group * 2 == v_out_group) {
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000490 /* Special cases for 2h2v upsampling */
491 if (do_fancy && compptr->downsampled_width > 2) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400492 if (jsimd_can_h2v2_fancy_upsample())
493 upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
494 else
495 upsample->methods[ci] = h2v2_fancy_upsample;
496 upsample->pub.need_context_rows = TRUE;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000497 } else {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400498 if (jsimd_can_h2v2_upsample())
499 upsample->methods[ci] = jsimd_h2v2_upsample;
500 else
501 upsample->methods[ci] = h2v2_upsample;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000502 }
503 } else if ((h_out_group % h_in_group) == 0 &&
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400504 (v_out_group % v_in_group) == 0) {
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000505 /* Generic integral-factors upsampling method */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400506#if defined(__mips__)
507 if (jsimd_can_int_upsample())
508 upsample->methods[ci] = jsimd_int_upsample;
509 else
510#endif
511 upsample->methods[ci] = int_upsample;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800512 upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
513 upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000514 } else
515 ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400516 if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000517 upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800518 ((j_common_ptr)cinfo, JPOOL_IMAGE,
519 (JDIMENSION)jround_up((long)cinfo->output_width,
520 (long)cinfo->max_h_samp_factor),
521 (JDIMENSION)cinfo->max_v_samp_factor);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000522 }
523 }
524}