1 |
;/************************************************************************** |
;/**************************************************************************** |
2 |
; * |
; * |
3 |
; * XVID MPEG-4 VIDEO CODEC |
; * XVID MPEG-4 VIDEO CODEC |
4 |
; * - mmx quantization/dequantization - |
; * - 3dne Quantization/Dequantization - |
5 |
; * |
; * |
6 |
; * Copyright(C) 2001-2003 XviD Team <xvid-devel@xvid.org> |
; * Copyright (C) 2002-2003 Peter Ross <pross@xvid.org> |
7 |
|
; * 2002 Jaan Kalda |
8 |
; * |
; * |
9 |
; * This program is free software ; you can redistribute it and/or modify |
; * This program is free software ; you can redistribute it and/or modify |
10 |
; * it under the terms of the GNU General Public License as published by |
; * it under the terms of the GNU General Public License as published by |
20 |
; * along with this program ; if not, write to the Free Software |
; * along with this program ; if not, write to the Free Software |
21 |
; * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
; * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
22 |
; * |
; * |
23 |
; * $Id: quantize_mpeg_xmm.asm,v 1.1.2.1 2003-10-07 13:02:35 edgomez Exp $ |
; * $Id: quantize_mpeg_xmm.asm,v 1.1.2.3 2003-10-28 22:23:03 edgomez Exp $ |
24 |
; * |
; * |
25 |
; *************************************************************************/ |
; ***************************************************************************/ |
26 |
;/************************************************************************** |
|
|
; * quant4 bugs have been fixed: (a) overflow bug for matrix elements |
|
|
; * equal to 1 or 2 is fixed by substituting pmulhw with pmulhuw (iSSE) |
|
|
; * and using multiplier 0ffffh instead of 10001h (for matrix element = 1; |
|
|
; * in that case, 1 is added before multiplying, that additional 1 comes |
|
|
; * from intra_matrix1; (b) rounding error for large coefficients and matrix |
|
|
; * elements is fixed by two-step approach: first approximation (rounded |
|
|
; * down) is found as usual; the result is multiplied by the matrix element |
|
|
; * and mismatch is used to calculate the correction. |
|
|
; *************************************************************************/ |
|
27 |
; _3dne functions are compatible with iSSE, but are optimized specifically |
; _3dne functions are compatible with iSSE, but are optimized specifically |
28 |
; for K7 pipelines |
; for K7 pipelines |
|
; |
|
|
;--------------------------------------------------------------------------- |
|
|
; 09.12.2002 Athlon optimizations contributed by Jaan Kalda |
|
|
;--------------------------------------------------------------------------- |
|
|
|
|
29 |
|
|
|
; data/text alignment |
|
|
%define ALIGN 8 |
|
30 |
%define SATURATE |
%define SATURATE |
31 |
|
|
32 |
bits 32 |
BITS 32 |
|
|
|
|
%ifdef FORMAT_COFF |
|
|
SECTION .data data |
|
|
%else |
|
|
SECTION .data data align=8 |
|
|
%endif |
|
33 |
|
|
34 |
%macro cglobal 1 |
%macro cglobal 1 |
35 |
%ifdef PREFIX |
%ifdef PREFIX |
48 |
extern %1 |
extern %1 |
49 |
%endif |
%endif |
50 |
%endmacro |
%endmacro |
|
align 8 |
|
|
mmzero dd 0,0 |
|
51 |
|
|
52 |
mmx_one times 4 dw 1 |
;============================================================================= |
53 |
|
; Local data |
54 |
|
;============================================================================= |
55 |
|
|
56 |
|
SECTION .rodata |
57 |
|
|
58 |
|
ALIGN 8 |
59 |
|
mmzero: |
60 |
|
dd 0,0 |
61 |
|
mmx_one: |
62 |
|
times 4 dw 1 |
63 |
|
|
64 |
;=========================================================================== |
;----------------------------------------------------------------------------- |
|
; |
|
65 |
; divide by 2Q table |
; divide by 2Q table |
66 |
; |
;----------------------------------------------------------------------------- |
|
;=========================================================================== |
|
67 |
|
|
68 |
align ALIGN |
ALIGN 16 |
69 |
mmx_divs ;i>2 |
mmx_divs: ;i>2 |
70 |
%assign i 1 |
%assign i 1 |
71 |
%rep 31 |
%rep 31 |
72 |
times 4 dw ((1 << 15) / i + 1) |
times 4 dw ((1 << 15) / i + 1) |
73 |
%assign i i+1 |
%assign i i+1 |
74 |
%endrep |
%endrep |
75 |
|
|
76 |
align ALIGN |
ALIGN 16 |
77 |
mmx_div ;i>2 |
mmx_div: ;quant>2 |
78 |
%assign i 1 |
times 4 dw 65535 ; the div by 2 formula will overflow for the case |
79 |
|
; quant=1 but we don't care much because quant=1 |
80 |
|
; is handled by a different piece of code that |
81 |
|
; doesn't use this table. |
82 |
|
%assign quant 2 |
83 |
%rep 31 |
%rep 31 |
84 |
times 4 dw ((1 << 16) / i + 1) |
times 4 dw ((1 << 16) / quant + 1) |
85 |
%assign i i+1 |
%assign quant quant+1 |
86 |
%endrep |
%endrep |
87 |
|
|
88 |
|
|
89 |
;=========================================================================== |
;----------------------------------------------------------------------------- |
|
; |
|
90 |
; intra matrix |
; intra matrix |
91 |
; |
; (TODO remove dependency on external tables for instance safiness) |
92 |
;=========================================================================== |
;----------------------------------------------------------------------------- |
93 |
|
|
94 |
%macro FIXX 1 |
%macro FIXX 1 |
95 |
dw (1 << 16) / (%1) + 1 |
dw (1 << 16) / (%1) + 1 |
100 |
cextern intra_matrix1 |
cextern intra_matrix1 |
101 |
cextern intra_matrix |
cextern intra_matrix |
102 |
|
|
103 |
;=========================================================================== |
;----------------------------------------------------------------------------- |
|
; |
|
104 |
; inter matrix |
; inter matrix |
105 |
; |
; (TODO remove dependency on external tables for instance safiness) |
106 |
;=========================================================================== |
;----------------------------------------------------------------------------- |
107 |
|
|
108 |
cextern inter_matrix1 |
cextern inter_matrix1 |
109 |
cextern inter_matrix |
cextern inter_matrix |
111 |
cextern inter_matrix_fixl |
cextern inter_matrix_fixl |
112 |
|
|
113 |
|
|
|
%define VM18P 3 |
|
|
%define VM18Q 4 |
|
114 |
%define nop4 db 08Dh,074h,026h,0 |
%define nop4 db 08Dh,074h,026h,0 |
115 |
%define nop3 add esp,byte 0 |
%define nop3 add esp,byte 0 |
116 |
%define nop2 mov esp,esp |
%define nop2 mov esp,esp |
117 |
%define nop7 db 08dh,02ch,02dh,0,0,0,0 |
%define nop7 db 08dh,02ch,02dh,0,0,0,0 |
118 |
%define nop6 add ebp,dword 0 |
%define nop6 add ebp,dword 0 |
119 |
|
|
120 |
;=========================================================================== |
;----------------------------------------------------------------------------- |
|
; |
|
121 |
; quantd table |
; quantd table |
122 |
; |
;----------------------------------------------------------------------------- |
|
;=========================================================================== |
|
123 |
|
|
124 |
|
%define VM18P 3 |
125 |
|
%define VM18Q 4 |
126 |
|
|
127 |
quantd |
ALIGN 16 |
128 |
|
quantd: |
129 |
%assign i 1 |
%assign i 1 |
130 |
%rep 31 |
%rep 31 |
131 |
times 4 dw (((VM18P*i) + (VM18Q/2)) / VM18Q) |
times 4 dw (((VM18P*i) + (VM18Q/2)) / VM18Q) |
132 |
%assign i i+1 |
%assign i i+1 |
133 |
%endrep |
%endrep |
134 |
|
|
135 |
;=========================================================================== |
;----------------------------------------------------------------------------- |
|
; |
|
136 |
; multiple by 2Q table |
; multiple by 2Q table |
137 |
; |
;----------------------------------------------------------------------------- |
|
;=========================================================================== |
|
|
|
|
138 |
|
|
139 |
mmx_mul_quant |
ALIGN 16 |
140 |
|
mmx_mul_quant: |
141 |
%assign i 1 |
%assign i 1 |
142 |
%rep 31 |
%rep 31 |
143 |
times 4 dw i |
times 4 dw i |
144 |
%assign i i+1 |
%assign i i+1 |
145 |
%endrep |
%endrep |
146 |
|
|
147 |
;=========================================================================== |
;----------------------------------------------------------------------------- |
|
; |
|
148 |
; saturation limits |
; saturation limits |
149 |
; |
;----------------------------------------------------------------------------- |
|
;=========================================================================== |
|
|
|
|
|
align 16 |
|
150 |
|
|
151 |
mmx_32767_minus_2047 times 4 dw (32767-2047) |
ALIGN 16 |
152 |
mmx_32768_minus_2048 times 4 dw (32768-2048) |
mmx_32767_minus_2047: |
153 |
mmx_2047 times 4 dw 2047 |
times 4 dw (32767-2047) |
154 |
mmx_minus_2048 times 4 dw (-2048) |
mmx_32768_minus_2048: |
155 |
zero times 4 dw 0 |
times 4 dw (32768-2048) |
156 |
|
mmx_2047: |
157 |
|
times 4 dw 2047 |
158 |
|
mmx_minus_2048: |
159 |
|
times 4 dw (-2048) |
160 |
|
zero: |
161 |
|
times 4 dw 0 |
162 |
|
|
163 |
int_div |
int_div: |
164 |
dd 0 |
dd 0 |
165 |
%assign i 1 |
%assign i 1 |
166 |
%rep 255 |
%rep 255 |
168 |
%assign i i+1 |
%assign i i+1 |
169 |
%endrep |
%endrep |
170 |
|
|
171 |
section .text |
;============================================================================= |
172 |
|
; Code |
173 |
|
;============================================================================= |
174 |
|
|
175 |
|
SECTION .text |
176 |
|
|
177 |
|
cglobal quant_mpeg_intra_xmm |
178 |
|
cglobal quant_mpeg_inter_xmm |
179 |
|
cglobal dequant_mpeg_intra_3dne |
180 |
|
cglobal dequant_mpeg_inter_3dne |
181 |
|
|
182 |
;=========================================================================== |
;----------------------------------------------------------------------------- |
183 |
; |
; |
184 |
; void quant4_intra_xmm(int16_t * coeff, |
; uint32_t quant_mpeg_intra_xmm(int16_t * coeff, |
185 |
; const int16_t const * data, |
; const int16_t const * data, |
186 |
; const uint32_t quant, |
; const uint32_t quant, |
187 |
; const uint32_t dcscalar); |
; const uint32_t dcscalar); |
188 |
; |
; |
189 |
;=========================================================================== |
;----------------------------------------------------------------------------- |
190 |
|
|
191 |
align ALIGN |
ALIGN 16 |
|
cglobal quant_mpeg_intra_xmm |
|
192 |
quant_mpeg_intra_xmm: |
quant_mpeg_intra_xmm: |
193 |
mov eax, [esp + 8] ; data |
mov eax, [esp + 8] ; data |
194 |
mov ecx, [esp + 12] ; quant |
mov ecx, [esp + 12] ; quant |
207 |
jg near .lloop |
jg near .lloop |
208 |
nop6 |
nop6 |
209 |
|
|
210 |
|
ALIGN 16 |
|
align ALIGN |
|
211 |
.loop |
.loop |
212 |
movq mm1, [eax + 8*esi+112] ; mm0 = [1st] |
movq mm1, [eax + 8*esi+112] ; mm0 = [1st] |
213 |
psubw mm0, mm1 ;-mm1 |
psubw mm0, mm1 ;-mm1 |
281 |
add esp, byte 12 |
add esp, byte 12 |
282 |
mov [edx], cx ; coeff[0] = ax |
mov [edx], cx ; coeff[0] = ax |
283 |
|
|
284 |
|
xor eax, eax |
285 |
ret |
ret |
286 |
|
|
287 |
align ALIGN |
ALIGN 16 |
288 |
.q1loop |
.q1loop |
289 |
movq mm1, [eax + 8*esi+112] ; mm0 = [1st] |
movq mm1, [eax + 8*esi+112] ; mm0 = [1st] |
290 |
psubw mm0, mm1 ;-mm1 |
psubw mm0, mm1 ;-mm1 |
334 |
jng near .q1loop |
jng near .q1loop |
335 |
jmp near .done |
jmp near .done |
336 |
|
|
337 |
align 8 |
ALIGN 8 |
338 |
.lloop |
.lloop |
339 |
movq mm1, [eax + 8*esi+112] ; mm0 = [1st] |
movq mm1, [eax + 8*esi+112] ; mm0 = [1st] |
340 |
psubw mm0, mm1 ;-mm1 |
psubw mm0, mm1 ;-mm1 |
341 |
movq mm4, [eax + 8*esi+120] ; |
movq mm4, [eax + 8*esi+120] |
342 |
psubw mm3, mm4 ;-mm4 |
psubw mm3, mm4 ;-mm4 |
343 |
pmaxsw mm0, mm1 ;|src| |
pmaxsw mm0, mm1 ;|src| |
344 |
pmaxsw mm3, mm4 |
pmaxsw mm3, mm4 |
387 |
jng near .lloop |
jng near .lloop |
388 |
jmp near .done |
jmp near .done |
389 |
|
|
390 |
;=========================================================================== |
;----------------------------------------------------------------------------- |
391 |
; |
; |
392 |
; uint32_t quant4_inter_xmm(int16_t * coeff, |
; uint32_t quant_mpeg_inter_xmm(int16_t * coeff, |
393 |
; const int16_t const * data, |
; const int16_t const * data, |
394 |
; const uint32_t quant); |
; const uint32_t quant); |
395 |
; |
; |
396 |
;=========================================================================== |
;----------------------------------------------------------------------------- |
397 |
|
|
398 |
align ALIGN |
ALIGN 16 |
|
cglobal quant_mpeg_inter_xmm |
|
399 |
quant_mpeg_inter_xmm: |
quant_mpeg_inter_xmm: |
400 |
mov eax, [esp + 8] ; data |
mov eax, [esp + 8] ; data |
401 |
mov ecx, [esp + 12] ; quant |
mov ecx, [esp + 12] ; quant |
409 |
mov ebx,esp |
mov ebx,esp |
410 |
sub esp,byte 24 |
sub esp,byte 24 |
411 |
lea ebx,[esp+8] |
lea ebx,[esp+8] |
412 |
and ebx,byte -8 ;align 8 |
and ebx, byte -8 ;ALIGN 8 |
413 |
pxor mm0,mm0 |
pxor mm0,mm0 |
414 |
pxor mm3,mm3 |
pxor mm3,mm3 |
415 |
movq [byte ebx],mm0 |
movq [byte ebx],mm0 |
420 |
jg near .lloop |
jg near .lloop |
421 |
nop |
nop |
422 |
|
|
423 |
align ALIGN |
ALIGN 16 |
424 |
.loop |
.loop |
425 |
movq mm1, [eax + 8*esi+112] ; mm0 = [1st] |
movq mm1, [eax + 8*esi+112] ; mm0 = [1st] |
426 |
psubw mm0,mm1 ;-mm1 |
psubw mm0,mm1 ;-mm1 |
484 |
|
|
485 |
ret |
ret |
486 |
|
|
487 |
align ALIGN |
ALIGN 16 |
488 |
.q1loop |
.q1loop |
489 |
movq mm1, [eax + 8*esi+112] ; mm0 = [1st] |
movq mm1, [eax + 8*esi+112] ; mm0 = [1st] |
490 |
psubw mm0,mm1 ;-mm1 |
psubw mm0,mm1 ;-mm1 |
491 |
movq mm4, [eax + 8*esi+120] ; |
movq mm4, [eax + 8*esi+120] |
492 |
psubw mm3,mm4 ;-mm4 |
psubw mm3,mm4 ;-mm4 |
493 |
pmaxsw mm0,mm1 ;|src| |
pmaxsw mm0,mm1 ;|src| |
494 |
pmaxsw mm3,mm4 |
pmaxsw mm3,mm4 |
533 |
jng near .q1loop |
jng near .q1loop |
534 |
jmp near .done |
jmp near .done |
535 |
|
|
536 |
align 8 |
ALIGN 8 |
537 |
.lloop |
.lloop |
538 |
movq mm1, [eax + 8*esi+112] ; mm0 = [1st] |
movq mm1, [eax + 8*esi+112] ; mm0 = [1st] |
539 |
psubw mm0,mm1 ;-mm1 |
psubw mm0,mm1 ;-mm1 |
540 |
movq mm4, [eax + 8*esi+120] ; |
movq mm4, [eax + 8*esi+120] |
541 |
psubw mm3,mm4 ;-mm4 |
psubw mm3,mm4 ;-mm4 |
542 |
pmaxsw mm0,mm1 ;|src| |
pmaxsw mm0,mm1 ;|src| |
543 |
pmaxsw mm3,mm4 |
pmaxsw mm3,mm4 |
586 |
jmp near .done |
jmp near .done |
587 |
|
|
588 |
|
|
589 |
;=========================================================================== |
;----------------------------------------------------------------------------- |
590 |
; |
; |
591 |
; void dequant4_intra_mmx(int16_t *data, |
; uint32_t dequant_mpeg_intra_3dne(int16_t *data, |
592 |
; const int16_t const *coeff, |
; const int16_t const *coeff, |
593 |
; const uint32_t quant, |
; const uint32_t quant, |
594 |
; const uint32_t dcscalar); |
; const uint32_t dcscalar); |
595 |
; |
; |
596 |
;=========================================================================== |
;----------------------------------------------------------------------------- |
597 |
|
|
598 |
; Note: in order to saturate 'easily', we pre-shift the quantifier |
; Note: in order to saturate 'easily', we pre-shift the quantifier |
599 |
; by 4. Then, the high-word of (coeff[]*matrix[i]*quant) are used to |
; by 4. Then, the high-word of (coeff[]*matrix[i]*quant) are used to |
604 |
; checked. Input ranges are: coeff in [-127,127], inter_matrix in [1..255],a |
; checked. Input ranges are: coeff in [-127,127], inter_matrix in [1..255],a |
605 |
; and quant in [1..31]. |
; and quant in [1..31]. |
606 |
; |
; |
607 |
;******************************************************************** |
|
608 |
%macro DEQUANT4INTRAMMX 1 |
%macro DEQUANT4INTRAMMX 1 |
609 |
movq mm1, [byte ecx+ 16 * %1] ; mm0 = c = coeff[i] |
movq mm1, [byte ecx+ 16 * %1] ; mm0 = c = coeff[i] |
610 |
movq mm4, [ecx+ 16 * %1 +8]; mm3 = c' = coeff[i+1] |
movq mm4, [ecx+ 16 * %1 +8]; mm3 = c' = coeff[i+1] |
644 |
movq [edx + 16 * %1 +8], mm7 ; data[i+1] |
movq [edx + 16 * %1 +8], mm7 ; data[i+1] |
645 |
%endmacro |
%endmacro |
646 |
|
|
647 |
align 16 |
ALIGN 16 |
|
cglobal dequant_mpeg_intra_3dne |
|
648 |
dequant_mpeg_intra_3dne: |
dequant_mpeg_intra_3dne: |
649 |
mov eax, [esp+12] ; quant |
mov eax, [esp+12] ; quant |
650 |
mov ecx, [esp+8] ; coeff |
mov ecx, [esp+8] ; coeff |
658 |
push esi |
push esi |
659 |
lea eax,[esp-28] |
lea eax,[esp-28] |
660 |
sub esp,byte 32 |
sub esp,byte 32 |
661 |
and eax,byte -8 ;points to qword aligned space on stack |
and eax, byte -8 ;points to qword ALIGNed space on stack |
662 |
movq [eax],mm0 |
movq [eax],mm0 |
663 |
movq [eax+8],mm7 |
movq [eax+8],mm7 |
664 |
imul ebx,[esp+16+8+32] ; dcscalar |
imul ebx,[esp+16+8+32] ; dcscalar |
665 |
movq mm2,mm7 |
movq mm2,mm7 |
666 |
|
|
667 |
|
ALIGN 4 |
|
align 4 |
|
668 |
|
|
669 |
DEQUANT4INTRAMMX 0 |
DEQUANT4INTRAMMX 0 |
670 |
|
|
697 |
|
|
698 |
add esp, byte 32+8 |
add esp, byte 32+8 |
699 |
|
|
700 |
|
xor eax, eax |
701 |
ret |
ret |
702 |
|
|
703 |
;=========================================================================== |
;----------------------------------------------------------------------------- |
704 |
; |
; |
705 |
; void dequant4_inter_3dne(int16_t * data, |
; uint32_t dequant_mpeg_inter_3dne(int16_t * data, |
706 |
; const int16_t * const coeff, |
; const int16_t * const coeff, |
707 |
; const uint32_t quant); |
; const uint32_t quant); |
708 |
; |
; |
709 |
;=========================================================================== |
;----------------------------------------------------------------------------- |
710 |
|
|
711 |
; Note: We use (2*c + sgn(c) - sgn(-c)) as multiplier |
; Note: We use (2*c + sgn(c) - sgn(-c)) as multiplier |
712 |
; so we handle the 3 cases: c<0, c==0, and c>0 in one shot. |
; so we handle the 3 cases: c<0, c==0, and c>0 in one shot. |
713 |
; sgn(x) is the result of 'pcmpgtw 0,x': 0 if x>=0, -1 if x<0. |
; sgn(x) is the result of 'pcmpgtw 0,x': 0 if x>=0, -1 if x<0. |
714 |
; It's mixed with the extraction of the absolute value. |
; It's mixed with the extraction of the absolute value. |
715 |
|
|
716 |
align 16 |
ALIGN 16 |
|
cglobal dequant_mpeg_inter_3dne |
|
717 |
dequant_mpeg_inter_3dne: |
dequant_mpeg_inter_3dne: |
718 |
mov edx, [esp+ 4] ; data |
mov edx, [esp+ 4] ; data |
719 |
mov ecx, [esp+ 8] ; coeff |
mov ecx, [esp+ 8] ; coeff |
729 |
nop |
nop |
730 |
nop4 |
nop4 |
731 |
|
|
732 |
align 16 |
ALIGN 16 |
733 |
.loop |
.loop |
734 |
movq mm0, [ecx+8*eax + 7*16 ] ; mm0 = coeff[i] |
movq mm0, [ecx+8*eax + 7*16 ] ; mm0 = coeff[i] |
735 |
pcmpgtw mm1, mm0 ; mm1 = sgn(c) (preserved) |
pcmpgtw mm1, mm0 ; mm1 = sgn(c) (preserved) |
807 |
add esp,byte 4 |
add esp,byte 4 |
808 |
xor word [edx + 2*63], ax |
xor word [edx + 2*63], ax |
809 |
|
|
810 |
|
xor eax, eax |
811 |
ret |
ret |