8
|
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 */
|