blob: edf061a737b13cade3a7e29425bbece69eab1232 [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001/*
2 * jdmerge.c
3 *
Thomas G. Lane489583f1996-02-07 00:00:00 +00004 * Copyright (C) 1994-1996, Thomas G. Lane.
Pierre Ossman59a39382009-03-09 13:15:56 +00005 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
DRC8ece7fe2009-08-06 08:32:00 +00006 * Copyright (C) 2009, D. R. Commander.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00007 * This file is part of the Independent JPEG Group's software.
8 * For conditions of distribution and use, see the accompanying README file.
9 *
10 * This file contains code for merged upsampling/color conversion.
11 *
12 * This file combines functions from jdsample.c and jdcolor.c;
13 * read those files first to understand what's going on.
14 *
15 * When the chroma components are to be upsampled by simple replication
16 * (ie, box filtering), we can save some work in color conversion by
17 * calculating all the output pixels corresponding to a pair of chroma
18 * samples at one time. In the conversion equations
19 * R = Y + K1 * Cr
20 * G = Y + K2 * Cb + K3 * Cr
21 * B = Y + K4 * Cb
22 * only the Y term varies among the group of pixels corresponding to a pair
23 * of chroma samples, so the rest of the terms can be calculated just once.
24 * At typical sampling ratios, this eliminates half or three-quarters of the
25 * multiplications needed for color conversion.
26 *
27 * This file currently provides implementations for the following cases:
28 * YCbCr => RGB color conversion only.
29 * Sampling ratios of 2h1v or 2h2v.
30 * No scaling needed at upsample time.
31 * Corner-aligned (non-CCIR601) sampling alignment.
32 * Other special cases could be added, but in most applications these are
33 * the only common cases. (For uncommon cases we fall back on the more
34 * general code in jdsample.c and jdcolor.c.)
35 */
36
37#define JPEG_INTERNALS
38#include "jinclude.h"
39#include "jpeglib.h"
Pierre Ossman59a39382009-03-09 13:15:56 +000040#include "jsimd.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000041
42#ifdef UPSAMPLE_MERGING_SUPPORTED
43
44
45/* Private subobject */
46
47typedef struct {
48 struct jpeg_upsampler pub; /* public fields */
49
50 /* Pointer to routine to do actual upsampling/conversion of one row group */
51 JMETHOD(void, upmethod, (j_decompress_ptr cinfo,
52 JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
53 JSAMPARRAY output_buf));
54
55 /* Private state for YCC->RGB conversion */
56 int * Cr_r_tab; /* => table for Cr to R conversion */
57 int * Cb_b_tab; /* => table for Cb to B conversion */
58 INT32 * Cr_g_tab; /* => table for Cr to G conversion */
59 INT32 * Cb_g_tab; /* => table for Cb to G conversion */
60
61 /* For 2:1 vertical sampling, we produce two output rows at a time.
62 * We need a "spare" row buffer to hold the second output row if the
63 * application provides just a one-row buffer; we also use the spare
64 * to discard the dummy last row if the image height is odd.
65 */
66 JSAMPROW spare_row;
67 boolean spare_full; /* T if spare buffer is occupied */
68
69 JDIMENSION out_row_width; /* samples per output row */
70 JDIMENSION rows_to_go; /* counts rows remaining in image */
71} my_upsampler;
72
73typedef my_upsampler * my_upsample_ptr;
74
75#define SCALEBITS 16 /* speediest right-shift on some machines */
76#define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
77#define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
78
79
80/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +000081 * Initialize tables for YCC->RGB colorspace conversion.
82 * This is taken directly from jdcolor.c; see that file for more info.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000083 */
84
Thomas G. Lane489583f1996-02-07 00:00:00 +000085LOCAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000086build_ycc_rgb_table (j_decompress_ptr cinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000087{
88 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
Thomas G. Lanebc79e061995-08-02 00:00:00 +000089 int i;
90 INT32 x;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000091 SHIFT_TEMPS
92
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000093 upsample->Cr_r_tab = (int *)
94 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
95 (MAXJSAMPLE+1) * SIZEOF(int));
96 upsample->Cb_b_tab = (int *)
97 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
98 (MAXJSAMPLE+1) * SIZEOF(int));
99 upsample->Cr_g_tab = (INT32 *)
100 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
101 (MAXJSAMPLE+1) * SIZEOF(INT32));
102 upsample->Cb_g_tab = (INT32 *)
103 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
104 (MAXJSAMPLE+1) * SIZEOF(INT32));
105
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000106 for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000107 /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000108 /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000109 /* Cr=>R value is nearest int to 1.40200 * x */
110 upsample->Cr_r_tab[i] = (int)
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000111 RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000112 /* Cb=>B value is nearest int to 1.77200 * x */
113 upsample->Cb_b_tab[i] = (int)
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000114 RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000115 /* Cr=>G value is scaled-up -0.71414 * x */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000116 upsample->Cr_g_tab[i] = (- FIX(0.71414)) * x;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000117 /* Cb=>G value is scaled-up -0.34414 * x */
118 /* We also add in ONE_HALF so that need not do it in inner loop */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000119 upsample->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000120 }
121}
122
123
124/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000125 * Initialize for an upsampling pass.
126 */
127
Thomas G. Lane489583f1996-02-07 00:00:00 +0000128METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000129start_pass_merged_upsample (j_decompress_ptr cinfo)
130{
131 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
132
133 /* Mark the spare buffer empty */
134 upsample->spare_full = FALSE;
135 /* Initialize total-height counter for detecting bottom of image */
136 upsample->rows_to_go = cinfo->output_height;
137}
138
139
140/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000141 * Control routine to do upsampling (and color conversion).
142 *
143 * The control routine just handles the row buffering considerations.
144 */
145
Thomas G. Lane489583f1996-02-07 00:00:00 +0000146METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000147merged_2v_upsample (j_decompress_ptr cinfo,
148 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
149 JDIMENSION in_row_groups_avail,
150 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
151 JDIMENSION out_rows_avail)
152/* 2:1 vertical sampling case: may need a spare row. */
153{
154 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
155 JSAMPROW work_ptrs[2];
156 JDIMENSION num_rows; /* number of rows returned to caller */
157
158 if (upsample->spare_full) {
159 /* If we have a spare row saved from a previous cycle, just return it. */
160 jcopy_sample_rows(& upsample->spare_row, 0, output_buf + *out_row_ctr, 0,
161 1, upsample->out_row_width);
162 num_rows = 1;
163 upsample->spare_full = FALSE;
164 } else {
165 /* Figure number of rows to return to caller. */
166 num_rows = 2;
167 /* Not more than the distance to the end of the image. */
168 if (num_rows > upsample->rows_to_go)
169 num_rows = upsample->rows_to_go;
170 /* And not more than what the client can accept: */
171 out_rows_avail -= *out_row_ctr;
172 if (num_rows > out_rows_avail)
173 num_rows = out_rows_avail;
174 /* Create output pointer array for upsampler. */
175 work_ptrs[0] = output_buf[*out_row_ctr];
176 if (num_rows > 1) {
177 work_ptrs[1] = output_buf[*out_row_ctr + 1];
178 } else {
179 work_ptrs[1] = upsample->spare_row;
180 upsample->spare_full = TRUE;
181 }
182 /* Now do the upsampling. */
183 (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);
184 }
185
186 /* Adjust counts */
187 *out_row_ctr += num_rows;
188 upsample->rows_to_go -= num_rows;
189 /* When the buffer is emptied, declare this input row group consumed */
190 if (! upsample->spare_full)
191 (*in_row_group_ctr)++;
192}
193
194
Thomas G. Lane489583f1996-02-07 00:00:00 +0000195METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000196merged_1v_upsample (j_decompress_ptr cinfo,
197 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
198 JDIMENSION in_row_groups_avail,
199 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
200 JDIMENSION out_rows_avail)
201/* 1:1 vertical sampling case: much easier, never need a spare row. */
202{
203 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
204
205 /* Just do the upsampling. */
206 (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr,
207 output_buf + *out_row_ctr);
208 /* Adjust counts */
209 (*out_row_ctr)++;
210 (*in_row_group_ctr)++;
211}
212
213
214/*
215 * These are the routines invoked by the control routines to do
216 * the actual upsampling/conversion. One row group is processed per call.
217 *
218 * Note: since we may be writing directly into application-supplied buffers,
219 * we have to be honest about the output width; we can't assume the buffer
220 * has been rounded up to an even width.
221 */
222
223
224/*
225 * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
226 */
227
Thomas G. Lane489583f1996-02-07 00:00:00 +0000228METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000229h2v1_merged_upsample (j_decompress_ptr cinfo,
230 JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
231 JSAMPARRAY output_buf)
232{
233 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
234 register int y, cred, cgreen, cblue;
235 int cb, cr;
236 register JSAMPROW outptr;
237 JSAMPROW inptr0, inptr1, inptr2;
238 JDIMENSION col;
239 /* copy these pointers into registers if possible */
240 register JSAMPLE * range_limit = cinfo->sample_range_limit;
241 int * Crrtab = upsample->Cr_r_tab;
242 int * Cbbtab = upsample->Cb_b_tab;
243 INT32 * Crgtab = upsample->Cr_g_tab;
244 INT32 * Cbgtab = upsample->Cb_g_tab;
245 SHIFT_TEMPS
246
247 inptr0 = input_buf[0][in_row_group_ctr];
248 inptr1 = input_buf[1][in_row_group_ctr];
249 inptr2 = input_buf[2][in_row_group_ctr];
250 outptr = output_buf[0];
251 /* Loop for each pair of output pixels */
252 for (col = cinfo->output_width >> 1; col > 0; col--) {
253 /* Do the chroma part of the calculation */
254 cb = GETJSAMPLE(*inptr1++);
255 cr = GETJSAMPLE(*inptr2++);
256 cred = Crrtab[cr];
257 cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
258 cblue = Cbbtab[cb];
259 /* Fetch 2 Y values and emit 2 pixels */
260 y = GETJSAMPLE(*inptr0++);
DRCf25c0712009-04-03 12:00:51 +0000261 outptr[rgb_red[cinfo->out_color_space]] = range_limit[y + cred];
262 outptr[rgb_green[cinfo->out_color_space]] = range_limit[y + cgreen];
263 outptr[rgb_blue[cinfo->out_color_space]] = range_limit[y + cblue];
264 outptr += rgb_pixelsize[cinfo->out_color_space];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000265 y = GETJSAMPLE(*inptr0++);
DRCf25c0712009-04-03 12:00:51 +0000266 outptr[rgb_red[cinfo->out_color_space]] = range_limit[y + cred];
267 outptr[rgb_green[cinfo->out_color_space]] = range_limit[y + cgreen];
268 outptr[rgb_blue[cinfo->out_color_space]] = range_limit[y + cblue];
269 outptr += rgb_pixelsize[cinfo->out_color_space];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000270 }
271 /* If image width is odd, do the last output column separately */
272 if (cinfo->output_width & 1) {
273 cb = GETJSAMPLE(*inptr1);
274 cr = GETJSAMPLE(*inptr2);
275 cred = Crrtab[cr];
276 cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
277 cblue = Cbbtab[cb];
278 y = GETJSAMPLE(*inptr0);
DRCf25c0712009-04-03 12:00:51 +0000279 outptr[rgb_red[cinfo->out_color_space]] = range_limit[y + cred];
280 outptr[rgb_green[cinfo->out_color_space]] = range_limit[y + cgreen];
281 outptr[rgb_blue[cinfo->out_color_space]] = range_limit[y + cblue];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000282 }
283}
284
285
286/*
287 * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
288 */
289
Thomas G. Lane489583f1996-02-07 00:00:00 +0000290METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000291h2v2_merged_upsample (j_decompress_ptr cinfo,
292 JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
293 JSAMPARRAY output_buf)
294{
295 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
296 register int y, cred, cgreen, cblue;
297 int cb, cr;
298 register JSAMPROW outptr0, outptr1;
299 JSAMPROW inptr00, inptr01, inptr1, inptr2;
300 JDIMENSION col;
301 /* copy these pointers into registers if possible */
302 register JSAMPLE * range_limit = cinfo->sample_range_limit;
303 int * Crrtab = upsample->Cr_r_tab;
304 int * Cbbtab = upsample->Cb_b_tab;
305 INT32 * Crgtab = upsample->Cr_g_tab;
306 INT32 * Cbgtab = upsample->Cb_g_tab;
307 SHIFT_TEMPS
308
309 inptr00 = input_buf[0][in_row_group_ctr*2];
310 inptr01 = input_buf[0][in_row_group_ctr*2 + 1];
311 inptr1 = input_buf[1][in_row_group_ctr];
312 inptr2 = input_buf[2][in_row_group_ctr];
313 outptr0 = output_buf[0];
314 outptr1 = output_buf[1];
315 /* Loop for each group of output pixels */
316 for (col = cinfo->output_width >> 1; col > 0; col--) {
317 /* Do the chroma part of the calculation */
318 cb = GETJSAMPLE(*inptr1++);
319 cr = GETJSAMPLE(*inptr2++);
320 cred = Crrtab[cr];
321 cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
322 cblue = Cbbtab[cb];
323 /* Fetch 4 Y values and emit 4 pixels */
324 y = GETJSAMPLE(*inptr00++);
DRCf25c0712009-04-03 12:00:51 +0000325 outptr0[rgb_red[cinfo->out_color_space]] = range_limit[y + cred];
326 outptr0[rgb_green[cinfo->out_color_space]] = range_limit[y + cgreen];
327 outptr0[rgb_blue[cinfo->out_color_space]] = range_limit[y + cblue];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000328 outptr0 += RGB_PIXELSIZE;
329 y = GETJSAMPLE(*inptr00++);
DRCf25c0712009-04-03 12:00:51 +0000330 outptr0[rgb_red[cinfo->out_color_space]] = range_limit[y + cred];
331 outptr0[rgb_green[cinfo->out_color_space]] = range_limit[y + cgreen];
332 outptr0[rgb_blue[cinfo->out_color_space]] = range_limit[y + cblue];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000333 outptr0 += RGB_PIXELSIZE;
334 y = GETJSAMPLE(*inptr01++);
DRCf25c0712009-04-03 12:00:51 +0000335 outptr1[rgb_red[cinfo->out_color_space]] = range_limit[y + cred];
336 outptr1[rgb_green[cinfo->out_color_space]] = range_limit[y + cgreen];
337 outptr1[rgb_blue[cinfo->out_color_space]] = range_limit[y + cblue];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000338 outptr1 += RGB_PIXELSIZE;
339 y = GETJSAMPLE(*inptr01++);
DRCf25c0712009-04-03 12:00:51 +0000340 outptr1[rgb_red[cinfo->out_color_space]] = range_limit[y + cred];
341 outptr1[rgb_green[cinfo->out_color_space]] = range_limit[y + cgreen];
342 outptr1[rgb_blue[cinfo->out_color_space]] = range_limit[y + cblue];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000343 outptr1 += RGB_PIXELSIZE;
344 }
345 /* If image width is odd, do the last output column separately */
346 if (cinfo->output_width & 1) {
347 cb = GETJSAMPLE(*inptr1);
348 cr = GETJSAMPLE(*inptr2);
349 cred = Crrtab[cr];
350 cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
351 cblue = Cbbtab[cb];
352 y = GETJSAMPLE(*inptr00);
DRCf25c0712009-04-03 12:00:51 +0000353 outptr0[rgb_red[cinfo->out_color_space]] = range_limit[y + cred];
354 outptr0[rgb_green[cinfo->out_color_space]] = range_limit[y + cgreen];
355 outptr0[rgb_blue[cinfo->out_color_space]] = range_limit[y + cblue];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000356 y = GETJSAMPLE(*inptr01);
DRCf25c0712009-04-03 12:00:51 +0000357 outptr1[rgb_red[cinfo->out_color_space]] = range_limit[y + cred];
358 outptr1[rgb_green[cinfo->out_color_space]] = range_limit[y + cgreen];
359 outptr1[rgb_blue[cinfo->out_color_space]] = range_limit[y + cblue];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000360 }
361}
362
363
364/*
365 * Module initialization routine for merged upsampling/color conversion.
366 *
367 * NB: this is called under the conditions determined by use_merged_upsample()
368 * in jdmaster.c. That routine MUST correspond to the actual capabilities
369 * of this module; no safety checks are made here.
370 */
371
Thomas G. Lane489583f1996-02-07 00:00:00 +0000372GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000373jinit_merged_upsampler (j_decompress_ptr cinfo)
374{
375 my_upsample_ptr upsample;
376
377 upsample = (my_upsample_ptr)
378 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
379 SIZEOF(my_upsampler));
380 cinfo->upsample = (struct jpeg_upsampler *) upsample;
381 upsample->pub.start_pass = start_pass_merged_upsample;
382 upsample->pub.need_context_rows = FALSE;
383
384 upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;
385
386 if (cinfo->max_v_samp_factor == 2) {
387 upsample->pub.upsample = merged_2v_upsample;
Pierre Ossman59a39382009-03-09 13:15:56 +0000388 if (jsimd_can_h2v2_merged_upsample())
389 upsample->upmethod = jsimd_h2v2_merged_upsample;
390 else
391 upsample->upmethod = h2v2_merged_upsample;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000392 /* Allocate a spare row buffer */
393 upsample->spare_row = (JSAMPROW)
394 (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
395 (size_t) (upsample->out_row_width * SIZEOF(JSAMPLE)));
396 } else {
397 upsample->pub.upsample = merged_1v_upsample;
Pierre Ossman59a39382009-03-09 13:15:56 +0000398 if (jsimd_can_h2v1_merged_upsample())
399 upsample->upmethod = jsimd_h2v1_merged_upsample;
400 else
401 upsample->upmethod = h2v1_merged_upsample;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000402 /* No spare row needed */
403 upsample->spare_row = NULL;
404 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000405
406 build_ycc_rgb_table(cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000407}
408
409#endif /* UPSAMPLE_MERGING_SUPPORTED */