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