Revision 35b2a786

View differences:

libavcodec/i386/mpegvideo.c
1
/*
2
 * The simplest mpeg encoder (well, it was the simplest!)
3
 * Copyright (c) 2000,2001 Gerard Lantau.
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
 *
19
 * Optimized for ia32 cpus by Nick Kurshev <nickols_k@mail.ru>
20
 */
21

  
22
void MPV_frame_start(MpegEncContext *s)
23
{
24
    if (s->pict_type == B_TYPE) {
25
	__asm __volatile(
26
	    "movl	(%1), %%eax\n\t"
27
	    "movl	4(%1), %%edx\n\t"
28
	    "movl	8(%1), %%ecx\n\t"
29
	    "movl	%%eax, (%0)\n\t"
30
	    "movl	%%edx, 4(%0)\n\t"
31
	    "movl	%%ecx, 8(%0)\n\t"
32
	    :
33
	    :"r"(s->current_picture), "r"(s->aux_picture)
34
	    :"eax","edx","ecx","memory");
35
    } else {
36
            /* swap next and last */
37
	__asm __volatile(
38
	    "movl	(%1), %%eax\n\t"
39
	    "movl	4(%1), %%edx\n\t"
40
	    "movl	8(%1), %%ecx\n\t"
41
	    "xchgl	(%0), %%eax\n\t"
42
	    "xchgl	4(%0), %%edx\n\t"
43
	    "xchgl	8(%0), %%ecx\n\t"
44
	    "movl	%%eax, (%1)\n\t"
45
	    "movl	%%edx, 4(%1)\n\t"
46
	    "movl	%%ecx, 8(%1)\n\t"
47
	    "movl	%%eax, (%2)\n\t"
48
	    "movl	%%edx, 4(%2)\n\t"
49
	    "movl	%%ecx, 8(%2)\n\t"
50
	    :
51
	    :"r"(s->last_picture), "r"(s->next_picture), "r"(s->current_picture)
52
	    :"eax","edx","ecx","memory");
53
    }
54
}
55

  
56
static void dct_unquantize(MpegEncContext *s, DCTELEM *block, int n, int qscale);
57

  
58
#ifdef HAVE_MMX 
59
static const unsigned long long int mm_wabs __attribute__ ((aligned(8))) = 0xffffffffffffffffULL;
60
static const unsigned long long int mm_wone __attribute__ ((aligned(8))) = 0x0001000100010001ULL;
61

  
62
/*
63
  NK:
64
  Note: looking at PARANOID:
65
  "enable all paranoid tests for rounding, overflows, etc..."
66

  
67
#ifdef PARANOID
68
                if (level < -2048 || level > 2047)
69
                    fprintf(stderr, "unquant error %d %d\n", i, level);
70
#endif
71
  We can suppose that result of two multiplications can't be greate of 0xFFFF
72
  i.e. is 16-bit, so we use here only PMULLW instruction and can avoid
73
  a complex multiplication.
74
=====================================================
75
 Full formula for multiplication of 2 integer numbers
76
 which are represent as high:low words:
77
 input: value1 = high1:low1
78
        value2 = high2:low2
79
 output: value3 = value1*value2
80
 value3=high3:low3 (on overflow: modulus 2^32 wrap-around)
81
 this mean that for 0x123456 * 0x123456 correct result is 0x766cb0ce4
82
 but this algorithm will compute only 0x66cb0ce4
83
 this limited by 16-bit size of operands
84
 ---------------------------------
85
 tlow1 = high1*low2
86
 tlow2 = high2*low1
87
 tlow1 = tlow1 + tlow2
88
 high3:low3 = low1*low2
89
 high3 += tlow1
90
*/
91
#ifdef BIN_PORTABILITY
92
static void dct_unquantize_mmx
93
#else
94
#define HAVE_DCT_UNQUANTIZE 1
95
static void dct_unquantize
96
#endif
97
(MpegEncContext *s,DCTELEM *block, int n, int qscale)
98
{
99
    int i, level;
100
    const UINT16 *quant_matrix;
101
    if (s->mb_intra) {
102
        if (n < 4) 
103
            block[0] = block[0] * s->y_dc_scale;
104
        else
105
            block[0] = block[0] * s->c_dc_scale;
106
        if (s->out_format == FMT_H263) {
107
            i = 1;
108
            goto unquant_even;
109
        }
110
        /* XXX: only mpeg1 */
111
        quant_matrix = s->intra_matrix;
112
	i=1;
113
	/* Align on 4 elements boundary */
114
	while(i&3)
115
	{
116
            level = block[i];
117
            if (level) {
118
                if (level < 0) level = -level;
119
                    level = (int)(level * qscale * quant_matrix[i]) >> 3;
120
                    level = (level - 1) | 1;
121
                if (block[i] < 0) level = -level;
122
                block[i] = level;
123
            }
124
	    i++;
125
	}
126
	__asm __volatile(
127
	"movd	%0, %%mm6\n\t"       /* mm6 = qscale | 0  */
128
	"punpckldq %%mm6, %%mm6\n\t" /* mm6 = qscale | qscale */
129
	"movq	%2, %%mm4\n\t"
130
	"movq	%%mm6, %%mm7\n\t"
131
	"movq	%1, %%mm5\n\t"
132
	"packssdw %%mm6, %%mm7\n\t" /* mm7 = qscale | qscale | qscale | qscale */
133
	"pxor	%%mm6, %%mm6\n\t"
134
	::"g"(qscale),"m"(mm_wone),"m"(mm_wabs):"memory");
135
        for(;i<64;i+=4) {
136
		__asm __volatile(
137
			"movq	%1, %%mm0\n\t"
138
			"movq	%%mm7, %%mm1\n\t"
139
			"movq	%%mm0, %%mm2\n\t"
140
			"movq	%%mm0, %%mm3\n\t"
141
			"pcmpgtw %%mm6, %%mm2\n\t"
142
			"pmullw	%2, %%mm1\n\t"
143
			"pandn	%%mm4, %%mm2\n\t"
144
			"por	%%mm5, %%mm2\n\t"  
145
			"pmullw	%%mm2, %%mm0\n\t" /* mm0 = abs(block[i]). */
146
			
147
			"pcmpeqw %%mm6, %%mm3\n\t"
148
			"pmullw	%%mm0, %%mm1\n\t"
149
			"psraw	$3, %%mm1\n\t"
150
			"psubw	%%mm5, %%mm1\n\t"   /* block[i] --; */
151
			"pandn	%%mm4, %%mm3\n\t"  /* fake of pcmpneqw : mm0 != 0 then mm1 = -1 */
152
			"por	%%mm5, %%mm1\n\t"   /* block[i] |= 1 */
153
			"pmullw %%mm2, %%mm1\n\t"   /* change signs again */
154

  
155
			"pand	%%mm3, %%mm1\n\t" /* nullify if was zero */
156
			"movq	%%mm1, %0"
157
			:"=m"(block[i])
158
			:"m"(block[i]), "m"(quant_matrix[i])
159
			:"memory");
160
        }
161
    } else {
162
        i = 0;
163
    unquant_even:
164
        quant_matrix = s->non_intra_matrix;
165
	/* Align on 4 elements boundary */
166
	while(i&3)
167
	{
168
	    level = block[i];
169
            if (level) {
170
                if (level < 0) level = -level;
171
                    level = (((level << 1) + 1) * qscale *
172
                             ((int) quant_matrix[i])) >> 4;
173
                    level = (level - 1) | 1;
174
                if(block[i] < 0) level = -level;
175
                block[i] = level;
176
	    }
177
	    i++;
178
	}
179
	__asm __volatile(
180
	"movd	%0, %%mm6\n\t"       /* mm6 = qscale | 0  */
181
	"punpckldq %%mm6, %%mm6\n\t" /* mm6 = qscale | qscale */
182
	"movq	%2, %%mm4\n\t"
183
	"movq	%%mm6, %%mm7\n\t"
184
	"movq	%1, %%mm5\n\t"
185
	"packssdw %%mm6, %%mm7\n\t" /* mm7 = qscale | qscale | qscale | qscale */
186
	"pxor	%%mm6, %%mm6\n\t"
187
	::"g"(qscale),"m"(mm_wone),"m"(mm_wabs):"memory");
188
        for(;i<64;i+=4) {
189
		__asm __volatile(
190
			"movq	%1, %%mm0\n\t"
191
			"movq	%%mm7, %%mm1\n\t"
192
			"movq	%%mm0, %%mm2\n\t"
193
			"movq	%%mm0, %%mm3\n\t"
194
			"pcmpgtw %%mm6, %%mm2\n\t"
195
			"pmullw	%2, %%mm1\n\t"
196
			"pandn	%%mm4, %%mm2\n\t"
197
			"por	%%mm5, %%mm2\n\t"  
198
			"pmullw	%%mm2, %%mm0\n\t" /* mm0 = abs(block[i]). */
199
			"psllw	$1, %%mm0\n\t" /* block[i] <<= 1 */
200
			"paddw	%%mm5, %%mm0\n\t" /* block[i] ++ */
201
			
202
			"pmullw	%%mm0, %%mm1\n\t"
203
			"psraw	$4, %%mm1\n\t"
204
			"pcmpeqw %%mm6, %%mm3\n\t"
205
			"psubw	%%mm5, %%mm1\n\t"   /* block[i] --; */
206
			"pandn	%%mm4, %%mm3\n\t"  /* fake of pcmpneqw : mm0 != 0 then mm1 = -1 */
207
			"por	%%mm5, %%mm1\n\t"   /* block[i] |= 1 */
208
			"pmullw %%mm2, %%mm1\n\t"   /* change signs again */
209

  
210
			"pand	%%mm3, %%mm1\n\t" /* nullify if was zero */
211
			"movq	%%mm1, %0"
212
			:"=m"(block[i])
213
			:"m"(block[i]), "m"(quant_matrix[i])
214
			:"memory");
215
        }
216
    }
217
}
218

  
219
#ifdef BIN_PORTABILITY
220
static void (*dct_unquantize_ptr)(MpegEncContext *s, 
221
                		    DCTELEM *block, int n, int qscale);
222

  
223
void MPV_common_init_mmx(void)
224
{
225
  int mm_flags;
226
  mm_flags = mm_support();
227
  if (mm_flags & MM_MMX) {
228
    dct_unquantize_ptr = dct_unquantize_mmx;
229
  }
230
  else {
231
    dct_unquantize_ptr = dct_unquantize;
232
  }
233
}
234

  
235
#define DCT_UNQUANTIZE(a,b,c,d) (*dct_unquantize_ptr)(a,b,c,d)
236
#else
237
#define DCT_UNQUANTIZE(a,b,c,d) dct_unquantize(a,b,c,d)
238
#endif /* BIN_PORTABILITY */
239
#endif /* HAVE_MMX */
libavcodec/i386/mpegvideo_mmx.c
1
/*
2
 * The simplest mpeg encoder (well, it was the simplest!)
3
 * Copyright (c) 2000,2001 Gerard Lantau.
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
 *
19
 * Optimized for ia32 cpus by Nick Kurshev <nickols_k@mail.ru>
20
 */
21

  
22
#include "../dsputil.h"
23
#include "../mpegvideo.h"
24

  
25
#if 0
26

  
27
/* XXX: GL: I don't understand why this function needs optimization
28
   (it is called only once per frame!), so I disabled it */
29

  
30
void MPV_frame_start(MpegEncContext *s)
31
{
32
    if (s->pict_type == B_TYPE) {
33
	__asm __volatile(
34
	    "movl	(%1), %%eax\n\t"
35
	    "movl	4(%1), %%edx\n\t"
36
	    "movl	8(%1), %%ecx\n\t"
37
	    "movl	%%eax, (%0)\n\t"
38
	    "movl	%%edx, 4(%0)\n\t"
39
	    "movl	%%ecx, 8(%0)\n\t"
40
	    :
41
	    :"r"(s->current_picture), "r"(s->aux_picture)
42
	    :"eax","edx","ecx","memory");
43
    } else {
44
            /* swap next and last */
45
	__asm __volatile(
46
	    "movl	(%1), %%eax\n\t"
47
	    "movl	4(%1), %%edx\n\t"
48
	    "movl	8(%1), %%ecx\n\t"
49
	    "xchgl	(%0), %%eax\n\t"
50
	    "xchgl	4(%0), %%edx\n\t"
51
	    "xchgl	8(%0), %%ecx\n\t"
52
	    "movl	%%eax, (%1)\n\t"
53
	    "movl	%%edx, 4(%1)\n\t"
54
	    "movl	%%ecx, 8(%1)\n\t"
55
	    "movl	%%eax, (%2)\n\t"
56
	    "movl	%%edx, 4(%2)\n\t"
57
	    "movl	%%ecx, 8(%2)\n\t"
58
	    :
59
	    :"r"(s->last_picture), "r"(s->next_picture), "r"(s->current_picture)
60
	    :"eax","edx","ecx","memory");
61
    }
62
}
63
#endif
64

  
65
static const unsigned long long int mm_wabs __attribute__ ((aligned(8))) = 0xffffffffffffffffULL;
66
static const unsigned long long int mm_wone __attribute__ ((aligned(8))) = 0x0001000100010001ULL;
67

  
68
/*
69
  NK:
70
  Note: looking at PARANOID:
71
  "enable all paranoid tests for rounding, overflows, etc..."
72

  
73
#ifdef PARANOID
74
                if (level < -2048 || level > 2047)
75
                    fprintf(stderr, "unquant error %d %d\n", i, level);
76
#endif
77
  We can suppose that result of two multiplications can't be greate of 0xFFFF
78
  i.e. is 16-bit, so we use here only PMULLW instruction and can avoid
79
  a complex multiplication.
80
=====================================================
81
 Full formula for multiplication of 2 integer numbers
82
 which are represent as high:low words:
83
 input: value1 = high1:low1
84
        value2 = high2:low2
85
 output: value3 = value1*value2
86
 value3=high3:low3 (on overflow: modulus 2^32 wrap-around)
87
 this mean that for 0x123456 * 0x123456 correct result is 0x766cb0ce4
88
 but this algorithm will compute only 0x66cb0ce4
89
 this limited by 16-bit size of operands
90
 ---------------------------------
91
 tlow1 = high1*low2
92
 tlow2 = high2*low1
93
 tlow1 = tlow1 + tlow2
94
 high3:low3 = low1*low2
95
 high3 += tlow1
96
*/
97
static void dct_unquantize_mpeg1_mmx(MpegEncContext *s,
98
                                     DCTELEM *block, int n, int qscale)
99
{
100
    int i, level;
101
    const UINT16 *quant_matrix;
102
    if (s->mb_intra) {
103
        if (n < 4) 
104
            block[0] = block[0] * s->y_dc_scale;
105
        else
106
            block[0] = block[0] * s->c_dc_scale;
107
        if (s->out_format == FMT_H263) {
108
            i = 1;
109
            goto unquant_even;
110
        }
111
        /* XXX: only mpeg1 */
112
        quant_matrix = s->intra_matrix;
113
	i=1;
114
	/* Align on 4 elements boundary */
115
	while(i&3)
116
	{
117
            level = block[i];
118
            if (level) {
119
                if (level < 0) level = -level;
120
                    level = (int)(level * qscale * quant_matrix[i]) >> 3;
121
                    level = (level - 1) | 1;
122
                if (block[i] < 0) level = -level;
123
                block[i] = level;
124
            }
125
	    i++;
126
	}
127
	__asm __volatile(
128
	"movd	%0, %%mm6\n\t"       /* mm6 = qscale | 0  */
129
	"punpckldq %%mm6, %%mm6\n\t" /* mm6 = qscale | qscale */
130
	"movq	%2, %%mm4\n\t"
131
	"movq	%%mm6, %%mm7\n\t"
132
	"movq	%1, %%mm5\n\t"
133
	"packssdw %%mm6, %%mm7\n\t" /* mm7 = qscale | qscale | qscale | qscale */
134
	"pxor	%%mm6, %%mm6\n\t"
135
	::"g"(qscale),"m"(mm_wone),"m"(mm_wabs):"memory");
136
        for(;i<64;i+=4) {
137
		__asm __volatile(
138
			"movq	%1, %%mm0\n\t"
139
			"movq	%%mm7, %%mm1\n\t"
140
			"movq	%%mm0, %%mm2\n\t"
141
			"movq	%%mm0, %%mm3\n\t"
142
			"pcmpgtw %%mm6, %%mm2\n\t"
143
			"pmullw	%2, %%mm1\n\t"
144
			"pandn	%%mm4, %%mm2\n\t"
145
			"por	%%mm5, %%mm2\n\t"  
146
			"pmullw	%%mm2, %%mm0\n\t" /* mm0 = abs(block[i]). */
147
			
148
			"pcmpeqw %%mm6, %%mm3\n\t"
149
			"pmullw	%%mm0, %%mm1\n\t"
150
			"psraw	$3, %%mm1\n\t"
151
			"psubw	%%mm5, %%mm1\n\t"   /* block[i] --; */
152
			"pandn	%%mm4, %%mm3\n\t"  /* fake of pcmpneqw : mm0 != 0 then mm1 = -1 */
153
			"por	%%mm5, %%mm1\n\t"   /* block[i] |= 1 */
154
			"pmullw %%mm2, %%mm1\n\t"   /* change signs again */
155

  
156
			"pand	%%mm3, %%mm1\n\t" /* nullify if was zero */
157
			"movq	%%mm1, %0"
158
			:"=m"(block[i])
159
			:"m"(block[i]), "m"(quant_matrix[i])
160
			:"memory");
161
        }
162
    } else {
163
        i = 0;
164
    unquant_even:
165
        quant_matrix = s->non_intra_matrix;
166
	/* Align on 4 elements boundary */
167
	while(i&3)
168
	{
169
	    level = block[i];
170
            if (level) {
171
                if (level < 0) level = -level;
172
                    level = (((level << 1) + 1) * qscale *
173
                             ((int) quant_matrix[i])) >> 4;
174
                    level = (level - 1) | 1;
175
                if(block[i] < 0) level = -level;
176
                block[i] = level;
177
	    }
178
	    i++;
179
	}
180
	__asm __volatile(
181
	"movd	%0, %%mm6\n\t"       /* mm6 = qscale | 0  */
182
	"punpckldq %%mm6, %%mm6\n\t" /* mm6 = qscale | qscale */
183
	"movq	%2, %%mm4\n\t"
184
	"movq	%%mm6, %%mm7\n\t"
185
	"movq	%1, %%mm5\n\t"
186
	"packssdw %%mm6, %%mm7\n\t" /* mm7 = qscale | qscale | qscale | qscale */
187
	"pxor	%%mm6, %%mm6\n\t"
188
	::"g"(qscale),"m"(mm_wone),"m"(mm_wabs):"memory");
189
        for(;i<64;i+=4) {
190
		__asm __volatile(
191
			"movq	%1, %%mm0\n\t"
192
			"movq	%%mm7, %%mm1\n\t"
193
			"movq	%%mm0, %%mm2\n\t"
194
			"movq	%%mm0, %%mm3\n\t"
195
			"pcmpgtw %%mm6, %%mm2\n\t"
196
			"pmullw	%2, %%mm1\n\t"
197
			"pandn	%%mm4, %%mm2\n\t"
198
			"por	%%mm5, %%mm2\n\t"  
199
			"pmullw	%%mm2, %%mm0\n\t" /* mm0 = abs(block[i]). */
200
			"psllw	$1, %%mm0\n\t" /* block[i] <<= 1 */
201
			"paddw	%%mm5, %%mm0\n\t" /* block[i] ++ */
202
			
203
			"pmullw	%%mm0, %%mm1\n\t"
204
			"psraw	$4, %%mm1\n\t"
205
			"pcmpeqw %%mm6, %%mm3\n\t"
206
			"psubw	%%mm5, %%mm1\n\t"   /* block[i] --; */
207
			"pandn	%%mm4, %%mm3\n\t"  /* fake of pcmpneqw : mm0 != 0 then mm1 = -1 */
208
			"por	%%mm5, %%mm1\n\t"   /* block[i] |= 1 */
209
			"pmullw %%mm2, %%mm1\n\t"   /* change signs again */
210

  
211
			"pand	%%mm3, %%mm1\n\t" /* nullify if was zero */
212
			"movq	%%mm1, %0"
213
			:"=m"(block[i])
214
			:"m"(block[i]), "m"(quant_matrix[i])
215
			:"memory");
216
        }
217
    }
218
}
219

  
220
void MPV_common_init_mmx(MpegEncContext *s)
221
{
222
    if (mm_flags & MM_MMX) {
223
        /* XXX: should include h263 optimization too. It would go even
224
           faster! */
225
        s->dct_unquantize = dct_unquantize_mpeg1_mmx;
226
    }
227
}

Also available in: Unified diff