1 |
;/************************************************************************** |
;/**************************************************************************** |
2 |
; * |
; * |
3 |
; * XVID MPEG-4 VIDEO CODEC |
; * XVID MPEG-4 VIDEO CODEC |
4 |
; * - 3dne Quantization/Dequantization - |
; * - 3dne Quantization/Dequantization - |
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.2 2003-10-09 18:50:22 edgomez Exp $ |
; * $Id: quantize_mpeg_xmm.asm,v 1.1.2.3 2003-10-28 22:23:03 edgomez Exp $ |
24 |
; * |
; * |
25 |
; *************************************************************************/ |
; ***************************************************************************/ |
26 |
|
|
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 |
29 |
|
|
|
; data/text alignment |
|
|
%define ALIGN 8 |
|
30 |
%define SATURATE |
%define SATURATE |
31 |
|
|
32 |
bits 32 |
BITS 32 |
33 |
|
|
34 |
%macro cglobal 1 |
%macro cglobal 1 |
35 |
%ifdef PREFIX |
%ifdef PREFIX |
49 |
%endif |
%endif |
50 |
%endmacro |
%endmacro |
51 |
|
|
52 |
;*************************************************************************** |
;============================================================================= |
53 |
; Local data |
; Local data |
54 |
;*************************************************************************** |
;============================================================================= |
55 |
|
|
56 |
%ifdef FORMAT_COFF |
SECTION .rodata |
|
SECTION .data data |
|
|
%else |
|
|
SECTION .data data align=8 |
|
|
%endif |
|
57 |
|
|
58 |
align 8 |
ALIGN 8 |
59 |
mmzero: |
mmzero: |
60 |
dd 0,0 |
dd 0,0 |
61 |
mmx_one: |
mmx_one: |
62 |
times 4 dw 1 |
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 |
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 |
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 |
%define VM18P 3 |
125 |
%define VM18Q 4 |
%define VM18Q 4 |
126 |
|
|
127 |
align 16 |
ALIGN 16 |
128 |
quantd: |
quantd: |
129 |
%assign i 1 |
%assign i 1 |
130 |
%rep 31 |
%rep 31 |
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 |
align 16 |
ALIGN 16 |
140 |
mmx_mul_quant: |
mmx_mul_quant: |
141 |
%assign i 1 |
%assign i 1 |
142 |
%rep 31 |
%rep 31 |
144 |
%assign i i+1 |
%assign i i+1 |
145 |
%endrep |
%endrep |
146 |
|
|
147 |
;=========================================================================== |
;----------------------------------------------------------------------------- |
|
; |
|
148 |
; saturation limits |
; saturation limits |
149 |
; |
;----------------------------------------------------------------------------- |
|
;=========================================================================== |
|
150 |
|
|
151 |
align 16 |
ALIGN 16 |
152 |
mmx_32767_minus_2047: |
mmx_32767_minus_2047: |
153 |
times 4 dw (32767-2047) |
times 4 dw (32767-2047) |
154 |
mmx_32768_minus_2048: |
mmx_32768_minus_2048: |
168 |
%assign i i+1 |
%assign i i+1 |
169 |
%endrep |
%endrep |
170 |
|
|
171 |
;*************************************************************************** |
;============================================================================= |
172 |
; Code |
; Code |
173 |
;*************************************************************************** |
;============================================================================= |
174 |
|
|
175 |
section .text |
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 |
; uint32_t quant_mpeg_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 ALIGN |
ALIGN 16 |
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 |
284 |
xor eax, eax |
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 quant_mpeg_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 |
; uint32_t dequant_mpeg_intra_3dne(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] |
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 |
|
|
700 |
xor eax, eax |
xor eax, eax |
701 |
ret |
ret |
702 |
|
|
703 |
;=========================================================================== |
;----------------------------------------------------------------------------- |
704 |
; |
; |
705 |
; uint32_t dequant_mpeg_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) |