2 * The simplest mpeg encoder (well, it was the simplest!)
3 * Copyright (c) 2000,2001 Gerard Lantau.
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.
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.
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.
19 * Optimized for ia32 cpus by Nick Kurshev <nickols_k@mail.ru>
22 void MPV_frame_start(MpegEncContext *s)
24 if (s->pict_type == B_TYPE) {
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"
33 :"r"(s->current_picture), "r"(s->aux_picture)
34 :"eax","edx","ecx","memory");
36 /* swap next and last */
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"
51 :"r"(s->last_picture), "r"(s->next_picture), "r"(s->current_picture)
52 :"eax","edx","ecx","memory");
56 static void dct_unquantize(MpegEncContext *s, DCTELEM *block, int n, int qscale);
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;
64 Note: looking at PARANOID:
65 "enable all paranoid tests for rounding, overflows, etc..."
68 if (level < -2048 || level > 2047)
69 fprintf(stderr, "unquant error %d %d\n", i, level);
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
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 ---------------------------------
88 high3:low3 = low1*low2
91 #ifdef BIN_PORTABILITY
92 static void dct_unquantize_mmx
94 #define HAVE_DCT_UNQUANTIZE 1
95 static void dct_unquantize
97 (MpegEncContext *s,DCTELEM *block, int n, int qscale)
100 const UINT16 *quant_matrix;
103 block[0] = block[0] * s->y_dc_scale;
105 block[0] = block[0] * s->c_dc_scale;
106 if (s->out_format == FMT_H263) {
110 /* XXX: only mpeg1 */
111 quant_matrix = s->intra_matrix;
113 /* Align on 4 elements boundary */
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;
127 "movd %0, %%mm6\n\t" /* mm6 = qscale | 0 */
128 "punpckldq %%mm6, %%mm6\n\t" /* mm6 = qscale | qscale */
130 "movq %%mm6, %%mm7\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");
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]). */
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 */
155 "pand %%mm3, %%mm1\n\t" /* nullify if was zero */
158 :"m"(block[i]), "m"(quant_matrix[i])
164 quant_matrix = s->non_intra_matrix;
165 /* Align on 4 elements boundary */
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;
180 "movd %0, %%mm6\n\t" /* mm6 = qscale | 0 */
181 "punpckldq %%mm6, %%mm6\n\t" /* mm6 = qscale | qscale */
183 "movq %%mm6, %%mm7\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");
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] ++ */
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 */
210 "pand %%mm3, %%mm1\n\t" /* nullify if was zero */
213 :"m"(block[i]), "m"(quant_matrix[i])
219 #ifdef BIN_PORTABILITY
220 static void (*dct_unquantize_ptr)(MpegEncContext *s,
221 DCTELEM *block, int n, int qscale);
223 void MPV_common_init_mmx(void)
226 mm_flags = mm_support();
227 if (mm_flags & MM_MMX) {
228 dct_unquantize_ptr = dct_unquantize_mmx;
231 dct_unquantize_ptr = dct_unquantize;
235 #define DCT_UNQUANTIZE(a,b,c,d) (*dct_unquantize_ptr)(a,b,c,d)
237 #define DCT_UNQUANTIZE(a,b,c,d) dct_unquantize(a,b,c,d)
238 #endif /* BIN_PORTABILITY */
239 #endif /* HAVE_MMX */