Change license headers to say 'FFmpeg' instead of 'this program/this library'
[ffmpeg.git] / libavcodec / i386 / mpegvideo_mmx_template.c
1 /*
2  * MPEG video MMX templates
3  *
4  * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 #undef SPREADW
23 #undef PMAXW
24 #undef PMAX
25 #ifdef HAVE_MMX2
26 #define SPREADW(a) "pshufw $0, " #a ", " #a " \n\t"
27 #define PMAXW(a,b) "pmaxsw " #a ", " #b "     \n\t"
28 #define PMAX(a,b) \
29             "pshufw $0x0E," #a ", " #b "        \n\t"\
30             PMAXW(b, a)\
31             "pshufw $0x01," #a ", " #b "        \n\t"\
32             PMAXW(b, a)
33 #else
34 #define SPREADW(a) \
35         "punpcklwd " #a ", " #a " \n\t"\
36         "punpcklwd " #a ", " #a " \n\t"
37 #define PMAXW(a,b) \
38         "psubusw " #a ", " #b " \n\t"\
39         "paddw " #a ", " #b "   \n\t"
40 #define PMAX(a,b)  \
41             "movq " #a ", " #b "                \n\t"\
42             "psrlq $32, " #a "                  \n\t"\
43             PMAXW(b, a)\
44             "movq " #a ", " #b "                \n\t"\
45             "psrlq $16, " #a "                  \n\t"\
46             PMAXW(b, a)
47
48 #endif
49
50 static int RENAME(dct_quantize)(MpegEncContext *s,
51                             DCTELEM *block, int n,
52                             int qscale, int *overflow)
53 {
54     long last_non_zero_p1;
55     int level=0, q; //=0 is cuz gcc says uninitalized ...
56     const uint16_t *qmat, *bias;
57     DECLARE_ALIGNED_8(int16_t, temp_block[64]);
58
59     assert((7&(int)(&temp_block[0])) == 0); //did gcc align it correctly?
60
61     //s->fdct (block);
62     RENAMEl(ff_fdct) (block); //cant be anything else ...
63
64     if(s->dct_error_sum)
65         s->denoise_dct(s, block);
66
67     if (s->mb_intra) {
68         int dummy;
69         if (n < 4)
70             q = s->y_dc_scale;
71         else
72             q = s->c_dc_scale;
73         /* note: block[0] is assumed to be positive */
74         if (!s->h263_aic) {
75 #if 1
76         asm volatile (
77                 "mul %%ecx                \n\t"
78                 : "=d" (level), "=a"(dummy)
79                 : "a" ((block[0]>>2) + q), "c" (inverse[q<<1])
80         );
81 #else
82         asm volatile (
83                 "xorl %%edx, %%edx        \n\t"
84                 "divw %%cx                \n\t"
85                 "movzwl %%ax, %%eax       \n\t"
86                 : "=a" (level)
87                 : "a" ((block[0]>>2) + q), "c" (q<<1)
88                 : "%edx"
89         );
90 #endif
91         } else
92             /* For AIC we skip quant/dequant of INTRADC */
93             level = (block[0] + 4)>>3;
94
95         block[0]=0; //avoid fake overflow
96 //        temp_block[0] = (block[0] + (q >> 1)) / q;
97         last_non_zero_p1 = 1;
98         bias = s->q_intra_matrix16[qscale][1];
99         qmat = s->q_intra_matrix16[qscale][0];
100     } else {
101         last_non_zero_p1 = 0;
102         bias = s->q_inter_matrix16[qscale][1];
103         qmat = s->q_inter_matrix16[qscale][0];
104     }
105
106     if((s->out_format == FMT_H263 || s->out_format == FMT_H261) && s->mpeg_quant==0){
107
108         asm volatile(
109             "movd %%"REG_a", %%mm3              \n\t" // last_non_zero_p1
110             SPREADW(%%mm3)
111             "pxor %%mm7, %%mm7                  \n\t" // 0
112             "pxor %%mm4, %%mm4                  \n\t" // 0
113             "movq (%2), %%mm5                   \n\t" // qmat[0]
114             "pxor %%mm6, %%mm6                  \n\t"
115             "psubw (%3), %%mm6                  \n\t" // -bias[0]
116             "mov $-128, %%"REG_a"               \n\t"
117             ASMALIGN(4)
118             "1:                                 \n\t"
119             "pxor %%mm1, %%mm1                  \n\t" // 0
120             "movq (%1, %%"REG_a"), %%mm0        \n\t" // block[i]
121             "pcmpgtw %%mm0, %%mm1               \n\t" // block[i] <= 0 ? 0xFF : 0x00
122             "pxor %%mm1, %%mm0                  \n\t"
123             "psubw %%mm1, %%mm0                 \n\t" // ABS(block[i])
124             "psubusw %%mm6, %%mm0               \n\t" // ABS(block[i]) + bias[0]
125             "pmulhw %%mm5, %%mm0                \n\t" // (ABS(block[i])*qmat[0] - bias[0]*qmat[0])>>16
126             "por %%mm0, %%mm4                   \n\t"
127             "pxor %%mm1, %%mm0                  \n\t"
128             "psubw %%mm1, %%mm0                 \n\t" // out=((ABS(block[i])*qmat[0] - bias[0]*qmat[0])>>16)*sign(block[i])
129             "movq %%mm0, (%5, %%"REG_a")        \n\t"
130             "pcmpeqw %%mm7, %%mm0               \n\t" // out==0 ? 0xFF : 0x00
131             "movq (%4, %%"REG_a"), %%mm1        \n\t"
132             "movq %%mm7, (%1, %%"REG_a")        \n\t" // 0
133             "pandn %%mm1, %%mm0                 \n\t"
134             PMAXW(%%mm0, %%mm3)
135             "add $8, %%"REG_a"                  \n\t"
136             " js 1b                             \n\t"
137             PMAX(%%mm3, %%mm0)
138             "movd %%mm3, %%"REG_a"              \n\t"
139             "movzb %%al, %%"REG_a"              \n\t" // last_non_zero_p1
140             : "+a" (last_non_zero_p1)
141             : "r" (block+64), "r" (qmat), "r" (bias),
142               "r" (inv_zigzag_direct16+64), "r" (temp_block+64)
143         );
144         // note the asm is split cuz gcc doesnt like that many operands ...
145         asm volatile(
146             "movd %1, %%mm1                     \n\t" // max_qcoeff
147             SPREADW(%%mm1)
148             "psubusw %%mm1, %%mm4               \n\t"
149             "packuswb %%mm4, %%mm4              \n\t"
150             "movd %%mm4, %0                     \n\t" // *overflow
151         : "=g" (*overflow)
152         : "g" (s->max_qcoeff)
153         );
154     }else{ // FMT_H263
155         asm volatile(
156             "movd %%"REG_a", %%mm3              \n\t" // last_non_zero_p1
157             SPREADW(%%mm3)
158             "pxor %%mm7, %%mm7                  \n\t" // 0
159             "pxor %%mm4, %%mm4                  \n\t" // 0
160             "mov $-128, %%"REG_a"               \n\t"
161             ASMALIGN(4)
162             "1:                                 \n\t"
163             "pxor %%mm1, %%mm1                  \n\t" // 0
164             "movq (%1, %%"REG_a"), %%mm0        \n\t" // block[i]
165             "pcmpgtw %%mm0, %%mm1               \n\t" // block[i] <= 0 ? 0xFF : 0x00
166             "pxor %%mm1, %%mm0                  \n\t"
167             "psubw %%mm1, %%mm0                 \n\t" // ABS(block[i])
168             "movq (%3, %%"REG_a"), %%mm6        \n\t" // bias[0]
169             "paddusw %%mm6, %%mm0               \n\t" // ABS(block[i]) + bias[0]
170             "movq (%2, %%"REG_a"), %%mm5        \n\t" // qmat[i]
171             "pmulhw %%mm5, %%mm0                \n\t" // (ABS(block[i])*qmat[0] + bias[0]*qmat[0])>>16
172             "por %%mm0, %%mm4                   \n\t"
173             "pxor %%mm1, %%mm0                  \n\t"
174             "psubw %%mm1, %%mm0                 \n\t" // out=((ABS(block[i])*qmat[0] - bias[0]*qmat[0])>>16)*sign(block[i])
175             "movq %%mm0, (%5, %%"REG_a")        \n\t"
176             "pcmpeqw %%mm7, %%mm0               \n\t" // out==0 ? 0xFF : 0x00
177             "movq (%4, %%"REG_a"), %%mm1        \n\t"
178             "movq %%mm7, (%1, %%"REG_a")        \n\t" // 0
179             "pandn %%mm1, %%mm0                 \n\t"
180             PMAXW(%%mm0, %%mm3)
181             "add $8, %%"REG_a"                  \n\t"
182             " js 1b                             \n\t"
183             PMAX(%%mm3, %%mm0)
184             "movd %%mm3, %%"REG_a"              \n\t"
185             "movzb %%al, %%"REG_a"              \n\t" // last_non_zero_p1
186             : "+a" (last_non_zero_p1)
187             : "r" (block+64), "r" (qmat+64), "r" (bias+64),
188               "r" (inv_zigzag_direct16+64), "r" (temp_block+64)
189         );
190         // note the asm is split cuz gcc doesnt like that many operands ...
191         asm volatile(
192             "movd %1, %%mm1                     \n\t" // max_qcoeff
193             SPREADW(%%mm1)
194             "psubusw %%mm1, %%mm4               \n\t"
195             "packuswb %%mm4, %%mm4              \n\t"
196             "movd %%mm4, %0                     \n\t" // *overflow
197         : "=g" (*overflow)
198         : "g" (s->max_qcoeff)
199         );
200     }
201
202     if(s->mb_intra) block[0]= level;
203     else            block[0]= temp_block[0];
204
205     if(s->dsp.idct_permutation_type == FF_SIMPLE_IDCT_PERM){
206         if(last_non_zero_p1 <= 1) goto end;
207         block[0x08] = temp_block[0x01]; block[0x10] = temp_block[0x08];
208         block[0x20] = temp_block[0x10];
209         if(last_non_zero_p1 <= 4) goto end;
210         block[0x18] = temp_block[0x09]; block[0x04] = temp_block[0x02];
211         block[0x09] = temp_block[0x03];
212         if(last_non_zero_p1 <= 7) goto end;
213         block[0x14] = temp_block[0x0A]; block[0x28] = temp_block[0x11];
214         block[0x12] = temp_block[0x18]; block[0x02] = temp_block[0x20];
215         if(last_non_zero_p1 <= 11) goto end;
216         block[0x1A] = temp_block[0x19]; block[0x24] = temp_block[0x12];
217         block[0x19] = temp_block[0x0B]; block[0x01] = temp_block[0x04];
218         block[0x0C] = temp_block[0x05];
219         if(last_non_zero_p1 <= 16) goto end;
220         block[0x11] = temp_block[0x0C]; block[0x29] = temp_block[0x13];
221         block[0x16] = temp_block[0x1A]; block[0x0A] = temp_block[0x21];
222         block[0x30] = temp_block[0x28]; block[0x22] = temp_block[0x30];
223         block[0x38] = temp_block[0x29]; block[0x06] = temp_block[0x22];
224         if(last_non_zero_p1 <= 24) goto end;
225         block[0x1B] = temp_block[0x1B]; block[0x21] = temp_block[0x14];
226         block[0x1C] = temp_block[0x0D]; block[0x05] = temp_block[0x06];
227         block[0x0D] = temp_block[0x07]; block[0x15] = temp_block[0x0E];
228         block[0x2C] = temp_block[0x15]; block[0x13] = temp_block[0x1C];
229         if(last_non_zero_p1 <= 32) goto end;
230         block[0x0B] = temp_block[0x23]; block[0x34] = temp_block[0x2A];
231         block[0x2A] = temp_block[0x31]; block[0x32] = temp_block[0x38];
232         block[0x3A] = temp_block[0x39]; block[0x26] = temp_block[0x32];
233         block[0x39] = temp_block[0x2B]; block[0x03] = temp_block[0x24];
234         if(last_non_zero_p1 <= 40) goto end;
235         block[0x1E] = temp_block[0x1D]; block[0x25] = temp_block[0x16];
236         block[0x1D] = temp_block[0x0F]; block[0x2D] = temp_block[0x17];
237         block[0x17] = temp_block[0x1E]; block[0x0E] = temp_block[0x25];
238         block[0x31] = temp_block[0x2C]; block[0x2B] = temp_block[0x33];
239         if(last_non_zero_p1 <= 48) goto end;
240         block[0x36] = temp_block[0x3A]; block[0x3B] = temp_block[0x3B];
241         block[0x23] = temp_block[0x34]; block[0x3C] = temp_block[0x2D];
242         block[0x07] = temp_block[0x26]; block[0x1F] = temp_block[0x1F];
243         block[0x0F] = temp_block[0x27]; block[0x35] = temp_block[0x2E];
244         if(last_non_zero_p1 <= 56) goto end;
245         block[0x2E] = temp_block[0x35]; block[0x33] = temp_block[0x3C];
246         block[0x3E] = temp_block[0x3D]; block[0x27] = temp_block[0x36];
247         block[0x3D] = temp_block[0x2F]; block[0x2F] = temp_block[0x37];
248         block[0x37] = temp_block[0x3E]; block[0x3F] = temp_block[0x3F];
249     }else if(s->dsp.idct_permutation_type == FF_LIBMPEG2_IDCT_PERM){
250         if(last_non_zero_p1 <= 1) goto end;
251         block[0x04] = temp_block[0x01];
252         block[0x08] = temp_block[0x08]; block[0x10] = temp_block[0x10];
253         if(last_non_zero_p1 <= 4) goto end;
254         block[0x0C] = temp_block[0x09]; block[0x01] = temp_block[0x02];
255         block[0x05] = temp_block[0x03];
256         if(last_non_zero_p1 <= 7) goto end;
257         block[0x09] = temp_block[0x0A]; block[0x14] = temp_block[0x11];
258         block[0x18] = temp_block[0x18]; block[0x20] = temp_block[0x20];
259         if(last_non_zero_p1 <= 11) goto end;
260         block[0x1C] = temp_block[0x19];
261         block[0x11] = temp_block[0x12]; block[0x0D] = temp_block[0x0B];
262         block[0x02] = temp_block[0x04]; block[0x06] = temp_block[0x05];
263         if(last_non_zero_p1 <= 16) goto end;
264         block[0x0A] = temp_block[0x0C]; block[0x15] = temp_block[0x13];
265         block[0x19] = temp_block[0x1A]; block[0x24] = temp_block[0x21];
266         block[0x28] = temp_block[0x28]; block[0x30] = temp_block[0x30];
267         block[0x2C] = temp_block[0x29]; block[0x21] = temp_block[0x22];
268         if(last_non_zero_p1 <= 24) goto end;
269         block[0x1D] = temp_block[0x1B]; block[0x12] = temp_block[0x14];
270         block[0x0E] = temp_block[0x0D]; block[0x03] = temp_block[0x06];
271         block[0x07] = temp_block[0x07]; block[0x0B] = temp_block[0x0E];
272         block[0x16] = temp_block[0x15]; block[0x1A] = temp_block[0x1C];
273         if(last_non_zero_p1 <= 32) goto end;
274         block[0x25] = temp_block[0x23]; block[0x29] = temp_block[0x2A];
275         block[0x34] = temp_block[0x31]; block[0x38] = temp_block[0x38];
276         block[0x3C] = temp_block[0x39]; block[0x31] = temp_block[0x32];
277         block[0x2D] = temp_block[0x2B]; block[0x22] = temp_block[0x24];
278         if(last_non_zero_p1 <= 40) goto end;
279         block[0x1E] = temp_block[0x1D]; block[0x13] = temp_block[0x16];
280         block[0x0F] = temp_block[0x0F]; block[0x17] = temp_block[0x17];
281         block[0x1B] = temp_block[0x1E]; block[0x26] = temp_block[0x25];
282         block[0x2A] = temp_block[0x2C]; block[0x35] = temp_block[0x33];
283         if(last_non_zero_p1 <= 48) goto end;
284         block[0x39] = temp_block[0x3A]; block[0x3D] = temp_block[0x3B];
285         block[0x32] = temp_block[0x34]; block[0x2E] = temp_block[0x2D];
286             block[0x23] = temp_block[0x26]; block[0x1F] = temp_block[0x1F];
287         block[0x27] = temp_block[0x27]; block[0x2B] = temp_block[0x2E];
288         if(last_non_zero_p1 <= 56) goto end;
289         block[0x36] = temp_block[0x35]; block[0x3A] = temp_block[0x3C];
290         block[0x3E] = temp_block[0x3D]; block[0x33] = temp_block[0x36];
291         block[0x2F] = temp_block[0x2F]; block[0x37] = temp_block[0x37];
292         block[0x3B] = temp_block[0x3E]; block[0x3F] = temp_block[0x3F];
293     }else{
294         if(last_non_zero_p1 <= 1) goto end;
295         block[0x01] = temp_block[0x01];
296         block[0x08] = temp_block[0x08]; block[0x10] = temp_block[0x10];
297         if(last_non_zero_p1 <= 4) goto end;
298         block[0x09] = temp_block[0x09]; block[0x02] = temp_block[0x02];
299         block[0x03] = temp_block[0x03];
300         if(last_non_zero_p1 <= 7) goto end;
301         block[0x0A] = temp_block[0x0A]; block[0x11] = temp_block[0x11];
302         block[0x18] = temp_block[0x18]; block[0x20] = temp_block[0x20];
303         if(last_non_zero_p1 <= 11) goto end;
304         block[0x19] = temp_block[0x19];
305         block[0x12] = temp_block[0x12]; block[0x0B] = temp_block[0x0B];
306         block[0x04] = temp_block[0x04]; block[0x05] = temp_block[0x05];
307         if(last_non_zero_p1 <= 16) goto end;
308         block[0x0C] = temp_block[0x0C]; block[0x13] = temp_block[0x13];
309         block[0x1A] = temp_block[0x1A]; block[0x21] = temp_block[0x21];
310         block[0x28] = temp_block[0x28]; block[0x30] = temp_block[0x30];
311         block[0x29] = temp_block[0x29]; block[0x22] = temp_block[0x22];
312         if(last_non_zero_p1 <= 24) goto end;
313         block[0x1B] = temp_block[0x1B]; block[0x14] = temp_block[0x14];
314         block[0x0D] = temp_block[0x0D]; block[0x06] = temp_block[0x06];
315         block[0x07] = temp_block[0x07]; block[0x0E] = temp_block[0x0E];
316         block[0x15] = temp_block[0x15]; block[0x1C] = temp_block[0x1C];
317         if(last_non_zero_p1 <= 32) goto end;
318         block[0x23] = temp_block[0x23]; block[0x2A] = temp_block[0x2A];
319         block[0x31] = temp_block[0x31]; block[0x38] = temp_block[0x38];
320         block[0x39] = temp_block[0x39]; block[0x32] = temp_block[0x32];
321         block[0x2B] = temp_block[0x2B]; block[0x24] = temp_block[0x24];
322         if(last_non_zero_p1 <= 40) goto end;
323         block[0x1D] = temp_block[0x1D]; block[0x16] = temp_block[0x16];
324         block[0x0F] = temp_block[0x0F]; block[0x17] = temp_block[0x17];
325         block[0x1E] = temp_block[0x1E]; block[0x25] = temp_block[0x25];
326         block[0x2C] = temp_block[0x2C]; block[0x33] = temp_block[0x33];
327         if(last_non_zero_p1 <= 48) goto end;
328         block[0x3A] = temp_block[0x3A]; block[0x3B] = temp_block[0x3B];
329         block[0x34] = temp_block[0x34]; block[0x2D] = temp_block[0x2D];
330         block[0x26] = temp_block[0x26]; block[0x1F] = temp_block[0x1F];
331         block[0x27] = temp_block[0x27]; block[0x2E] = temp_block[0x2E];
332         if(last_non_zero_p1 <= 56) goto end;
333         block[0x35] = temp_block[0x35]; block[0x3C] = temp_block[0x3C];
334         block[0x3D] = temp_block[0x3D]; block[0x36] = temp_block[0x36];
335         block[0x2F] = temp_block[0x2F]; block[0x37] = temp_block[0x37];
336         block[0x3E] = temp_block[0x3E]; block[0x3F] = temp_block[0x3F];
337     }
338     end:
339 /*
340     for(i=0; i<last_non_zero_p1; i++)
341     {
342        int j= zigzag_direct_noperm[i];
343        block[block_permute_op(j)]= temp_block[j];
344     }
345 */
346
347     return last_non_zero_p1 - 1;
348 }