__int64 const1 = 0x59BA0000D24B59BA; // Cr_r Cr_b Cr_g Cr_r
__int64 const2 = 0x00007168E9FA0000; // Cb-r Cb_b Cb_g Cb_r
__int64 const5 = 0x0000D24B59BA0000; // Cr_b Cr_g Cr_r Cr_b
__int64 const6 = 0x7168E9FA00007168; // Cb_b Cb_g Cb_r Cb_b

// constants for factors (One_Half/fix(x)) << 2
__int64 const05 = 0x0001000000000001; // Cr_r Cr_b Cr_g Cr_r
__int64 const15 = 0x00000001FFFA0000; // Cb-r Cb_b Cb_g Cb_r
__int64 const45 = 0x0000000000010000; // Cr_b Cr_g Cr_r Cr_b
__int64 const55 = 0x0001FFFA00000001; // Cb_b Cb_g Cb_r Cb_b

/*
* Upsample and color convert for the case of 2:1 horizontal and 2:1
vertical.
*/

METHODDEF(void)
h2v2_merged_upsample (j_decompress_ptr cinfo,
JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
JSAMPARRAY output_buf)
{
// added for MMX

__int64 const128 = 0x0080008000800080;

__int64 empty = 0x0000000000000000;

__int64 davemask = 0x0000FFFFFFFF0000;

////////////////////////////////////

my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
register int y, cred, cgreen, cblue;
int cb, cr;
register JSAMPROW outptr0, outptr1;
JSAMPROW inptr00, inptr01, inptr1, inptr2;
JDIMENSION col;
/* copy these pointers into registers if possible */
register JSAMPLE * range_limit = cinfo->sample_range_limit;
int * Crrtab = upsample->Cr_r_tab;
int * Cbbtab = upsample->Cb_b_tab;
INT32 * Crgtab = upsample->Cr_g_tab;
INT32 * Cbgtab = upsample->Cb_g_tab;
SHIFT_TEMPS

// Added for MMX

register int width = cinfo->image_width;
int cols = cinfo->output_width;
int cols_asm = (cols >> 3);
int diff = cols - (cols_asm<<3);
int cols_asm_copy = cols_asm;

///////////////////////////////////////

inptr00 = input_buf[0][in_row_group_ctr*2];
inptr01 = input_buf[0][in_row_group_ctr*2 + 1];
inptr1 = input_buf[1][in_row_group_ctr];
inptr2 = input_buf[2][in_row_group_ctr];
outptr0 = output_buf[0];
outptr1 = output_buf[1];
/* Loop for each group of output pixels */

_asm
{
mov esi, inptr00
mov eax, inptr01
mov ebx, inptr2
mov ecx, inptr1
mov edi, outptr0
mov edx, outptr1
do_next16:

movd mm0, [ebx] ; 0 0 0 0 Cr3 Cr2 Cr1 Cr0
pxor mm6, mm6
punpcklbw mm0, mm0 ; Cr3 Cr3 Cr2 Cr2 Cr1 Cr1 Cr0 Cr0
movq mm7, const128
punpcklwd mm0, mm0 ; Cr1 Cr1 Cr1 Cr1 Cr0 Cr0 Cr0 Cr0
movq mm4, mm0
punpcklbw mm0, mm6 ; Cr0 Cr0 Cr0 Cr0
psubsw mm0, mm7 ; Cr0 - 128:Cr0-128:Cr0-128:Cr0 -128
movd mm1, [ecx] ; 0 0 0 0 Cb3 Cb2 Cb1 Cb0
psllw mm0, 2 ; left shift by 2 bits
punpcklbw mm1, mm1 ; Cb3 Cb3 Cb2 Cb2 Cb1 Cb1 Cb0 Cb0
paddsw mm0, const05 ; add (one_half/fix(x)) << 2
punpcklwd mm1, mm1 ; Cb1 Cb1 Cb1 Cb1 Cb0 Cb0 Cb0 Cb0
movq mm5, mm1
pmulhw mm0, const1 ; multiply by (fix(x) >> 1)
punpcklbw mm1, mm6 ; Cb0 Cb0 Cb0 Cb0
punpckhbw mm4, mm6 ; Cr1 Cr1 Cr1 Cr1
psubsw mm1, mm7 ; Cb0 - 128:Cb0-128:Cb0-128:Cb0 -128
punpckhbw mm5, mm6 ; Cb1 Cb1 Cb1 Cb1
psllw mm1, 2 ; left shift by 2 bits
paddsw mm1, const15 ; add (one_half/fix(x)) << 2
psubsw mm4, mm7 ; Cr1 - 128:Cr1-128:Cr1-128:Cr1 -128
psubsw mm5, mm7 ; Cb1 - 128:Cb1-128:Cb1-128:Cb1 -128
pmulhw mm1, const2 ; multiply by (fix(x) >> 1)
psllw mm4, 2 ; left shift by 2 bits
psllw mm5, 2 ; left shift by 2 bits
paddsw mm4, const45 ; add (one_half/fix(x)) << 2
movd mm7, [esi] ; Y13 Y12 Y9 Y8 Y5 Y4 Y1 Y0
pmulhw mm4, const5 ; multiply by (fix(x) >> 1)
movq mm6, mm7
punpcklbw mm7, mm7 ; Y5 Y5 Y4 Y4 Y1 Y1 Y0 Y0
paddsw mm5, const55 ; add (one_half/fix(x)) << 2
paddsw mm0, mm1 ; cred0 cbl0 cgr0 cred0
movq mm1, mm7
pmulhw mm5, const6 ; multiply by (fix(x) >> 1)
movq mm2, mm0 ; cred0 cbl0 cgr0 cred0
punpcklwd mm7, mm6 ; Y5 Y4 Y1 Y1 Y1 Y0 Y0 Y0
pand mm2, davemask ; 0 cbl0 cgr0 0
psrlq mm1, 16 ; 0 0 Y5 Y5 Y4 Y4 Y1 Y1
psrlq mm2, 16 ; 0 0 cbl0 cgr0
punpcklbw mm7, empty ; Y1 Y0 Y0 Y0
paddsw mm4, mm5 ; cbl1 cgr1 cred1 cbl1
movq mm3, mm4 ; cbl1 cgr1 cred1 cbl1
pand mm3, davemask ; 0 cgr1 cred1 0
paddsw mm7, mm0 ; r1 b0 g0 r0
psllq mm3, 16 ; cgr1 cred1 0 0
movq mm6, mm1 ; 0 0 Y5 Y5 Y4 Y4 Y1 Y1
por mm2, mm3 ; cgr1 cred1 cbl0 cgr0
punpcklbw mm6, empty ; Y4 Y4 Y1 Y1
movd mm3, [eax] ; Y15 Y14 Y11 Y10 Y7 Y6 Y3 Y2
paddsw mm6, mm2 ; g4 r4 b1 g1
packuswb mm7, mm6 ; g4 r4 b1 g1 r1 b0 g0 r0
movq mm6, mm3 ; Y15 Y14 Y11 Y10 Y7 Y6 Y3 Y2
punpcklbw mm3, mm3 ; Y7 Y7 Y6 Y6 Y3 Y3 Y2 Y2
movq [edi], mm7 ; move to memory g4 r4 b1 g1 r1 b0 g0 r0
movq mm5, mm3 ; Y7 Y7 Y6 Y6 Y3 Y3 Y2 Y2
punpcklwd mm3, mm6 ; X X X X Y3 Y2 Y2 Y2
punpcklbw mm3, empty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3, mm3 ; X X X X Cb3 Cb3 Cb2 Cb2
psllw mm0, 2 ; left shift by 2 bits
paddsw mm0, const05 ; add (one_half/fix(x)) << 2
punpcklwd mm3, mm3 ; Cb3 Cb3 Cb3 Cb3 Cb2 Cb2 Cb2 Cb2
movq mm7, mm3
pmulhw mm0, const1 ; multiply by (fix(x) >> 1)
punpcklbw mm3, mm6 ; Cb2 Cb2 Cb2 Cb2
psubsw mm3, const128 ; Cb0 - 128:Cb0-128:Cb0-128:Cb0 -128
punpckhbw mm4, mm6 ; Cr3 Cr3 Cr3 Cr3
psllw mm3, 2 ; left shift by 2 bits
paddsw mm3, const15 ; add (one_half/fix(x)) << 2
punpckhbw mm7, mm6 ; Cb3 Cb3 Cb3 Cb3
pmulhw mm3, const2 ; multiply by (fix(x) >> 1)
psubsw mm7, const128 ; Cb3 - 128:Cb3-128:Cb3-128:Cb3 -128
paddsw mm0, mm3 ; cred2 cbl2 cgr2 cred2
psllw mm7, 2 ; left shift by 2 bits
psubsw mm4, const128 ; Cr3 - 128:Cr3-128:Cr3-128:Cr3 -128
movd mm3, [esi+4] ; Y21 Y20 Y17 Y16 Y13 Y12 Y9 Y8
psllw mm4, 2 ; left shift by 2 bits
paddsw mm7, const55 ; add (one_half/fix(x)) << 2
movq mm6, mm3 ; Y21 Y20 Y17 Y16 Y13 Y12 Y9 Y8
movq mm2, mm0
pand mm2, davemask
punpcklbw mm3, mm3 ; Y13 Y13 Y12 Y12 Y9 Y9 Y8 Y8
psrlq mm2, 16
paddsw mm4, const45 ; add (one_half/fix(x)) << 2
punpcklwd mm3, mm6 ; X X X X Y9 Y8 Y8 Y8
pmulhw mm4, const5 ; multiply by (fix(x) >> 1)
pmulhw mm7, const6 ; multiply by (fix(x) >> 1)
punpcklbw mm3, empty ; Y9 Y8 Y8 Y8
paddsw mm4, mm7 ; cbl3 cgr3 cred3 cbl3
paddsw mm3, mm0 ; r9 b8 g8 r8
movq mm7, mm4
packuswb mm1, mm3 ; r9 b8 g8 r8 b5 g5 r5 b4
movd mm3, [eax+4] ; Y23 Y22 Y19 Y18 Y15 Y14 Y11 Y10
pand mm7, davemask
psrlq mm6, 8 ; 0 Y21 Y20 Y17 Y16 Y13 Y12 Y9
psllq mm7, 16
movq [edi+8], mm1 ; move to memory r9 b8 g8 r8 b5 g5 r5 b4
por mm2, mm7
movq mm7, mm3 ; Y23 Y22 Y19 Y18 Y15 Y14 Y11 Y10
punpcklbw mm3, mm3 ; X X X X Y11 Y11 Y10 Y10
pxor mm1, mm1
punpcklwd mm3, mm7 ; X X X X Y11 Y10 Y10 Y10
punpcklbw mm3, mm1 ; Y11 Y10 Y10 Y10
psrlq mm7, 8 ; 0 Y23 Y22 Y19 Y18 Y15 Y14 Y11
paddsw mm3, mm0 ; r11 b10 g10 r10
movq mm0, mm7 ; 0 Y23 Y22 Y19 Y18 Y15 Y14 Y11
packuswb mm5, mm3 ; r11 b10 g10 r10 b7 g7 r7 b6
punpcklbw mm7, mm7 ; X X X X Y14 Y14 Y11 Y11
movq [edx+8], mm5 ; move to memory r11 b10 g10 r10 b7 g7 r7 b6
movq mm3, mm6 ; 0 Y21 Y20 Y17 Y16 Y13 Y12 Y9
punpcklbw mm6, mm6 ; X X X X Y12 Y12 Y9 Y9
punpcklbw mm7, mm1 ; Y14 Y14 Y11 Y11
punpcklbw mm6, mm1 ; Y12 Y12 Y9 Y9
paddsw mm7, mm2 ; g14 r14 b11 g11
paddsw mm6, mm2 ; g12 r12 b9 g9
psrlq mm3, 8 ; 0 0 Y21 Y20 Y17 Y16 Y13 Y12
movq mm1, mm3 ; 0 0 Y21 Y20 Y17 Y16 Y13 Y12
punpcklbw mm3, mm3 ; X X X X Y13 Y13 Y12 Y12
add esi, 8
psrlq mm3, 16 ; X X X X X X Y13 Y13 modified on 09/24
punpcklwd mm1, mm3 ; X X X X Y13 Y13 Y13 Y12
add eax, 8
psrlq mm0, 8 ; 0 0 Y23 Y22 Y19 Y18 Y15 Y14
punpcklbw mm1, empty ; Y13 Y13 Y13 Y12
movq mm5, mm0 ; 0 0 Y23 Y22 Y19 Y18 Y15 Y14
punpcklbw mm0, mm0 ; X X X X Y15 Y15 Y14 Y14
paddsw mm1, mm4 ; b13 g13 r13 b12
psrlq mm0, 16 ; X X X X X X Y15 Y15
add edi, 24
punpcklwd mm5, mm0 ; X X X X Y15 Y15 Y15 Y14
packuswb mm6, mm1 ; b13 g13 r13 b12 g12 r12 b9 g9
add edx, 24
punpcklbw mm5, empty ; Y15 Y15 Y15 Y14
add ebx, 4
paddsw mm5, mm4 ; b15 g15 r15 b14
movq [edi-8], mm6 ; move to memory b13 g13 r13 b12 g12 r12 b9 g9
packuswb mm7, mm5 ; b15 g15 r15 b14 g14 r14 b11 g11
add ecx, 4
movq [edx-8], mm7 ; move to memory b15 g15 r15 b14 g14 r14 b11 g11
dec cols_asm
jnz do_next16
EMMS
}

inptr1 += (cols_asm_copy<<2);
inptr2 += (cols_asm_copy<<2);
inptr00 += (cols_asm_copy<<3);
inptr01 += (cols_asm_copy<<3);
outptr0 += cols_asm_copy*24;
outptr1 += cols_asm_copy*24;

// Process the stragglers in C
for (col = diff >> 1; col > 0; col--) {
/* Do the chroma part of the calculation */
cb = GETJSAMPLE(*inptr1++);
cr = GETJSAMPLE(*inptr2++);
cred = Crrtab[cr];
cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
cblue = Cbbtab[cb];
/* Fetch 4 Y values and emit 4 pixels */
y = GETJSAMPLE(*inptr00++);
outptr0[RGB_RED] = range_limit[y + cred];
outptr0[RGB_GREEN] = range_limit[y + cgreen];
outptr0[RGB_BLUE] = range_limit[y + cblue];
outptr0 += RGB_PIXELSIZE;
y = GETJSAMPLE(*inptr00++);
outptr0[RGB_RED] = range_limit[y + cred];
outptr0[RGB_GREEN] = range_limit[y + cgreen];
outptr0[RGB_BLUE] = range_limit[y + cblue];
outptr0 += RGB_PIXELSIZE;
y = GETJSAMPLE(*inptr01++);
outptr1[RGB_RED] = range_limit[y + cred];
outptr1[RGB_GREEN] = range_limit[y + cgreen];
outptr1[RGB_BLUE] = range_limit[y + cblue];
outptr1 += RGB_PIXELSIZE;
y = GETJSAMPLE(*inptr01++);
outptr1[RGB_RED] = range_limit[y + cred];
outptr1[RGB_GREEN] = range_limit[y + cgreen];
outptr1[RGB_BLUE] = range_limit[y + cblue];
outptr1 += RGB_PIXELSIZE;
}

/* If image width is odd, do the last output column separately */
//if (cinfo->output_width & 1) {
if (diff & 1) {
cb = GETJSAMPLE(*inptr1);
cr = GETJSAMPLE(*inptr2);
cred = Crrtab[cr];
cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
cblue = Cbbtab[cb];
y = GETJSAMPLE(*inptr00);
outptr0[RGB_RED] = range_limit[y + cred];
outptr0[RGB_GREEN] = range_limit[y + cgreen];
outptr0[RGB_BLUE] = range_limit[y + cblue];
y = GETJSAMPLE(*inptr01);
outptr1[RGB_RED] = range_limit[y + cred];
outptr1[RGB_GREEN] = range_limit[y + cgreen];
outptr1[RGB_BLUE] = range_limit[y + cblue];
}

}
