* cleanup
[libav.git] / libavcodec / i386 / mpegvideo_mmx_template.c
CommitLineData
2f349de2
MN
1/*
2 Copyright (C) 2002 Michael Niedermayer <michaelni@gmx.at>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17*/
18
19#undef SPREADW
20#undef PMAXW
21#ifdef HAVE_MMX2
22#define SPREADW(a) "pshufw $0, " #a ", " #a " \n\t"
23#define PMAXW(a,b) "pmaxsw " #a ", " #b " \n\t"
24
25#else
26#define SPREADW(a) \
27 "punpcklwd " #a ", " #a " \n\t"\
28 "punpcklwd " #a ", " #a " \n\t"
29#define PMAXW(a,b) \
30 "psubusw " #a ", " #b " \n\t"\
31 "paddw " #a ", " #b " \n\t"
32#endif
33
34static int RENAME(dct_quantize)(MpegEncContext *s,
35 DCTELEM *block, int n,
36 int qscale)
37{
38 int i, level, last_non_zero_p1, q;
39 const UINT16 *qmat;
40 static __align8 INT16 temp_block[64];
41 int minLevel, maxLevel;
42
43 if(s->avctx!=NULL && s->avctx->codec->id==CODEC_ID_MPEG4){
44 /* mpeg4 */
45 minLevel= -2048;
46 maxLevel= 2047;
47 }else if(s->out_format==FMT_MPEG1){
48 /* mpeg1 */
49 minLevel= -255;
50 maxLevel= 255;
3cb32e3d
A
51 }else if(s->out_format==FMT_MJPEG){
52 /* (m)jpeg */
53 minLevel= -1023;
54 maxLevel= 1023;
2f349de2
MN
55 }else{
56 /* h263 / msmpeg4 */
57 minLevel= -128;
58 maxLevel= 127;
59 }
60
61 av_fdct (block);
62
63 if (s->mb_intra) {
64 int dummy;
65 if (n < 4)
66 q = s->y_dc_scale;
67 else
68 q = s->c_dc_scale;
69
70 /* note: block[0] is assumed to be positive */
71#if 1
72 asm volatile (
73 "xorl %%edx, %%edx \n\t"
5dfd24ac 74 "mul %%ecx \n\t"
2f349de2 75 : "=d" (temp_block[0]), "=a"(dummy)
5dfd24ac 76 : "a" (block[0] + (q >> 1)), "c" (inverse[q])
2f349de2
MN
77 );
78#else
79 asm volatile (
80 "xorl %%edx, %%edx \n\t"
5dfd24ac 81 "divw %%cx \n\t"
2f349de2
MN
82 "movzwl %%ax, %%eax \n\t"
83 : "=a" (temp_block[0])
5dfd24ac 84 : "a" (block[0] + (q >> 1)), "c" (q)
2f349de2
MN
85 : "%edx"
86 );
87#endif
88// temp_block[0] = (block[0] + (q >> 1)) / q;
89 i = 1;
90 last_non_zero_p1 = 1;
91 if (s->out_format == FMT_H263) {
92 qmat = s->q_non_intra_matrix16;
93 } else {
94 qmat = s->q_intra_matrix16;
95 }
96 for(i=1;i<4;i++) {
97 level = block[i] * qmat[i];
98 level = level / (1 << (QMAT_SHIFT_MMX - 3));
99 /* XXX: currently, this code is not optimal. the range should be:
100 mpeg1: -255..255
101 mpeg2: -2048..2047
102 h263: -128..127
103 mpeg4: -2048..2047
104 */
105 if (level > maxLevel)
106 level = maxLevel;
107 else if (level < minLevel)
108 level = minLevel;
109 temp_block[i] = level;
110
111 if(level)
112 if(last_non_zero_p1 < inv_zigzag_direct16[i]) last_non_zero_p1= inv_zigzag_direct16[i];
113 block[i]=0;
114 }
115 } else {
116 i = 0;
117 last_non_zero_p1 = 0;
118 qmat = s->q_non_intra_matrix16;
119 }
120
121 asm volatile( /* XXX: small rounding bug, but it shouldnt matter */
122 "movd %3, %%mm3 \n\t"
123 SPREADW(%%mm3)
124 "movd %4, %%mm4 \n\t"
125 SPREADW(%%mm4)
5dfd24ac 126#ifndef HAVE_MMX2
2f349de2
MN
127 "movd %5, %%mm5 \n\t"
128 SPREADW(%%mm5)
5dfd24ac 129#endif
2f349de2
MN
130 "pxor %%mm7, %%mm7 \n\t"
131 "movd %%eax, %%mm2 \n\t"
132 SPREADW(%%mm2)
133 "movl %6, %%eax \n\t"
134 ".balign 16 \n\t"
135 "1: \n\t"
136 "movq (%1, %%eax), %%mm0 \n\t"
137 "movq (%2, %%eax), %%mm1 \n\t"
138 "movq %%mm0, %%mm6 \n\t"
139 "psraw $15, %%mm6 \n\t"
140 "pmulhw %%mm0, %%mm1 \n\t"
141 "psubsw %%mm6, %%mm1 \n\t"
142#ifdef HAVE_MMX2
143 "pminsw %%mm3, %%mm1 \n\t"
144 "pmaxsw %%mm4, %%mm1 \n\t"
145#else
146 "paddsw %%mm3, %%mm1 \n\t"
147 "psubusw %%mm4, %%mm1 \n\t"
148 "paddsw %%mm5, %%mm1 \n\t"
149#endif
150 "movq %%mm1, (%8, %%eax) \n\t"
151 "pcmpeqw %%mm7, %%mm1 \n\t"
152 "movq (%7, %%eax), %%mm0 \n\t"
153 "movq %%mm7, (%1, %%eax) \n\t"
154 "pandn %%mm0, %%mm1 \n\t"
155 PMAXW(%%mm1, %%mm2)
156 "addl $8, %%eax \n\t"
157 " js 1b \n\t"
158 "movq %%mm2, %%mm0 \n\t"
159 "psrlq $32, %%mm2 \n\t"
160 PMAXW(%%mm0, %%mm2)
161 "movq %%mm2, %%mm0 \n\t"
162 "psrlq $16, %%mm2 \n\t"
163 PMAXW(%%mm0, %%mm2)
164 "movd %%mm2, %%eax \n\t"
165 "movzbl %%al, %%eax \n\t"
166 : "+a" (last_non_zero_p1)
167 : "r" (block+64), "r" (qmat+64),
168#ifdef HAVE_MMX2
5dfd24ac 169 "m" (maxLevel), "m" (minLevel), "m" (minLevel /* dummy */), "g" (2*i - 128),
2f349de2 170#else
5dfd24ac 171 "m" (0x7FFF - maxLevel), "m" (0x7FFF -maxLevel + minLevel), "m" (minLevel), "g" (2*i - 128),
2f349de2
MN
172#endif
173 "r" (inv_zigzag_direct16+64), "r" (temp_block+64)
174 );
175// last_non_zero_p1=64;
176 /* permute for IDCT */
177 asm volatile(
178 "movl %0, %%eax \n\t"
179 "pushl %%ebp \n\t"
180 "movl %%esp, " MANGLE(esp_temp) "\n\t"
181 "1: \n\t"
182 "movzbl (%1, %%eax), %%ebx \n\t"
183 "movzbl 1(%1, %%eax), %%ebp \n\t"
184 "movw (%2, %%ebx, 2), %%cx \n\t"
185 "movw (%2, %%ebp, 2), %%sp \n\t"
186 "movzbl " MANGLE(permutation) "(%%ebx), %%ebx\n\t"
187 "movzbl " MANGLE(permutation) "(%%ebp), %%ebp\n\t"
188 "movw %%cx, (%3, %%ebx, 2) \n\t"
189 "movw %%sp, (%3, %%ebp, 2) \n\t"
190 "addl $2, %%eax \n\t"
191 " js 1b \n\t"
192 "movl " MANGLE(esp_temp) ", %%esp\n\t"
193 "popl %%ebp \n\t"
194 :
195 : "g" (-last_non_zero_p1), "d" (zigzag_direct_noperm+last_non_zero_p1), "S" (temp_block), "D" (block)
196 : "%eax", "%ebx", "%ecx"
197 );
198/*
199 for(i=0; i<last_non_zero_p1; i++)
200 {
201 int j= zigzag_direct_noperm[i];
202 block[block_permute_op(j)]= temp_block[j];
203 }
204*/
205//block_permute(block);
206 return last_non_zero_p1 - 1;
207}