Statistics
| Branch: | Revision:

ffmpeg / libavcodec / i386 / mpegvideo_mmx.c @ 35b2a786

History | View | Annotate | Download (7.04 KB)

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
}