aboutsummaryrefslogtreecommitdiffstats
path: root/libavcodec/i386/mpegvideo_mmx_template.c
blob: ffe5c848afab061f6228673c110bd6740322b129 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
    Copyright (C) 2002 Michael Niedermayer <michaelni@gmx.at>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#undef SPREADW
#undef PMAXW
#ifdef HAVE_MMX2
#define SPREADW(a) "pshufw $0, " #a ", " #a " \n\t"
#define PMAXW(a,b) "pmaxsw " #a ", " #b " \n\t"

#else
#define SPREADW(a) \
	"punpcklwd " #a ", " #a " \n\t"\
	"punpcklwd " #a ", " #a " \n\t"
#define PMAXW(a,b) \
	"psubusw " #a ", " #b " \n\t"\
	"paddw " #a ", " #b " \n\t"
#endif

static int RENAME(dct_quantize)(MpegEncContext *s,
                            DCTELEM *block, int n,
                            int qscale)
{
    int i, level, last_non_zero_p1, q;
    const UINT16 *qmat;
    static __align8 INT16 temp_block[64];
    int minLevel, maxLevel;
    
    if(s->avctx!=NULL && s->avctx->codec->id==CODEC_ID_MPEG4){
	/* mpeg4 */
        minLevel= -2048;
	maxLevel= 2047;
    }else if(s->out_format==FMT_MPEG1){
	/* mpeg1 */
        minLevel= -255;
	maxLevel= 255;
    }else{
	/* h263 / msmpeg4 */
        minLevel= -128;
	maxLevel= 127;
    }

    av_fdct (block);
    
    if (s->mb_intra) {
        int dummy;
        if (n < 4)
            q = s->y_dc_scale;
        else
            q = s->c_dc_scale;
        
        /* note: block[0] is assumed to be positive */
#if 1
	asm volatile (
		"xorl %%edx, %%edx	\n\t"
		"mul %%ecx		\n\t"
		: "=d" (temp_block[0]), "=a"(dummy)
		: "a" (block[0] + (q >> 1)), "c" (inverse[q])
	);
#else
	asm volatile (
		"xorl %%edx, %%edx	\n\t"
		"divw %%cx		\n\t"
		"movzwl %%ax, %%eax	\n\t"
		: "=a" (temp_block[0])
		: "a" (block[0] + (q >> 1)), "c" (q)
		: "%edx"
	);
#endif
//        temp_block[0] = (block[0] + (q >> 1)) / q;
        i = 1;
        last_non_zero_p1 = 1;
        if (s->out_format == FMT_H263) {
            qmat = s->q_non_intra_matrix16;
        } else {
            qmat = s->q_intra_matrix16;
        }
        for(i=1;i<4;i++) {
            level = block[i] * qmat[i];
            level = level / (1 << (QMAT_SHIFT_MMX - 3));
            /* XXX: currently, this code is not optimal. the range should be:
               mpeg1: -255..255
               mpeg2: -2048..2047
               h263:  -128..127
               mpeg4: -2048..2047
            */
            if (level > maxLevel)
                level = maxLevel;
            else if (level < minLevel)
                level = minLevel;
            temp_block[i] = level;

	    if(level) 
	        if(last_non_zero_p1 < inv_zigzag_direct16[i]) last_non_zero_p1= inv_zigzag_direct16[i];
	    block[i]=0;
        }
    } else {
        i = 0;
        last_non_zero_p1 = 0;
        qmat = s->q_non_intra_matrix16;
    }

    asm volatile( /* XXX: small rounding bug, but it shouldnt matter */
	"movd %3, %%mm3			\n\t"
	SPREADW(%%mm3)
	"movd %4, %%mm4			\n\t"
	SPREADW(%%mm4)
#ifndef HAVE_MMX2	
	"movd %5, %%mm5			\n\t"
	SPREADW(%%mm5)
#endif
	"pxor %%mm7, %%mm7		\n\t"
	"movd %%eax, %%mm2		\n\t"
	SPREADW(%%mm2)
	"movl %6, %%eax			\n\t"
	".balign 16			\n\t"
	"1:				\n\t"
	"movq (%1, %%eax), %%mm0	\n\t"
	"movq (%2, %%eax), %%mm1	\n\t"
	"movq %%mm0, %%mm6		\n\t"
	"psraw $15, %%mm6		\n\t"
	"pmulhw %%mm0, %%mm1		\n\t"
	"psubsw %%mm6, %%mm1		\n\t"
#ifdef HAVE_MMX2
	"pminsw %%mm3, %%mm1		\n\t"
	"pmaxsw %%mm4, %%mm1		\n\t"
#else
	"paddsw %%mm3, %%mm1		\n\t"
	"psubusw %%mm4, %%mm1		\n\t"
	"paddsw %%mm5, %%mm1		\n\t"
#endif
	"movq %%mm1, (%8, %%eax)	\n\t"
	"pcmpeqw %%mm7, %%mm1		\n\t"
	"movq (%7, %%eax), %%mm0	\n\t"
	"movq %%mm7, (%1, %%eax)	\n\t"
	"pandn %%mm0, %%mm1		\n\t"
	PMAXW(%%mm1, %%mm2)
	"addl $8, %%eax			\n\t"
	" js 1b				\n\t"
	"movq %%mm2, %%mm0		\n\t"
	"psrlq $32, %%mm2		\n\t"
	PMAXW(%%mm0, %%mm2)
	"movq %%mm2, %%mm0		\n\t"
	"psrlq $16, %%mm2		\n\t"
	PMAXW(%%mm0, %%mm2)
	"movd %%mm2, %%eax		\n\t"
	"movzbl %%al, %%eax		\n\t"
	: "+a" (last_non_zero_p1)
	: "r" (block+64), "r" (qmat+64), 
#ifdef HAVE_MMX2
	  "m" (maxLevel),          "m" (minLevel),                    "m" (minLevel /* dummy */), "g" (2*i - 128),
#else
	  "m" (0x7FFF - maxLevel), "m" (0x7FFF -maxLevel + minLevel), "m" (minLevel),             "g" (2*i - 128),
#endif
	  "r" (inv_zigzag_direct16+64), "r" (temp_block+64)
    );
// last_non_zero_p1=64;       
    /* permute for IDCT */
    asm volatile(
	"movl %0, %%eax			\n\t"
	"pushl %%ebp			\n\t"
	"movl %%esp, " MANGLE(esp_temp) "\n\t"
	"1:				\n\t"
	"movzbl (%1, %%eax), %%ebx	\n\t"
	"movzbl 1(%1, %%eax), %%ebp	\n\t"
	"movw (%2, %%ebx, 2), %%cx	\n\t"
	"movw (%2, %%ebp, 2), %%sp	\n\t"
	"movzbl " MANGLE(permutation) "(%%ebx), %%ebx\n\t"
	"movzbl " MANGLE(permutation) "(%%ebp), %%ebp\n\t"
	"movw %%cx, (%3, %%ebx, 2)	\n\t"
	"movw %%sp, (%3, %%ebp, 2)	\n\t"
	"addl $2, %%eax			\n\t"
	" js 1b				\n\t"
	"movl " MANGLE(esp_temp) ", %%esp\n\t"
	"popl %%ebp			\n\t"
	: 
	: "g" (-last_non_zero_p1), "d" (zigzag_direct_noperm+last_non_zero_p1), "S" (temp_block), "D" (block)
	: "%eax", "%ebx", "%ecx"
	);
/*
    for(i=0; i<last_non_zero_p1; i++)
    {
       int j= zigzag_direct_noperm[i];
       block[block_permute_op(j)]= temp_block[j];
    }
*/
//block_permute(block);
    return last_non_zero_p1 - 1;
}