2 * Optimized for ia32 CPUs by Nick Kurshev <nickols_k@mail.ru>
3 * H.263, MPEG-1, MPEG-2 dequantizer & draw_edges by Michael Niedermayer <michaelni@gmx.at>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/attributes.h"
23 #include "libavutil/cpu.h"
24 #include "libavutil/x86/asm.h"
25 #include "libavutil/x86/cpu.h"
26 #include "libavcodec/avcodec.h"
27 #include "libavcodec/mpegvideo.h"
28 #include "libavcodec/mpegvideodata.h"
32 static void dct_unquantize_h263_intra_mmx(MpegEncContext *s,
33 int16_t *block, int n, int qscale)
35 x86_reg level, qmul, qadd, nCoeffs;
39 av_assert2(s->block_last_index[n]>=0 || s->h263_aic);
43 level = block[0] * s->y_dc_scale;
45 level = block[0] * s->c_dc_scale;
46 qadd = (qscale - 1) | 1;
54 nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ];
57 "movd %1, %%mm6 \n\t" //qmul
58 "packssdw %%mm6, %%mm6 \n\t"
59 "packssdw %%mm6, %%mm6 \n\t"
60 "movd %2, %%mm5 \n\t" //qadd
61 "pxor %%mm7, %%mm7 \n\t"
62 "packssdw %%mm5, %%mm5 \n\t"
63 "packssdw %%mm5, %%mm5 \n\t"
64 "psubw %%mm5, %%mm7 \n\t"
65 "pxor %%mm4, %%mm4 \n\t"
68 "movq (%0, %3), %%mm0 \n\t"
69 "movq 8(%0, %3), %%mm1 \n\t"
71 "pmullw %%mm6, %%mm0 \n\t"
72 "pmullw %%mm6, %%mm1 \n\t"
74 "movq (%0, %3), %%mm2 \n\t"
75 "movq 8(%0, %3), %%mm3 \n\t"
77 "pcmpgtw %%mm4, %%mm2 \n\t" // block[i] < 0 ? -1 : 0
78 "pcmpgtw %%mm4, %%mm3 \n\t" // block[i] < 0 ? -1 : 0
80 "pxor %%mm2, %%mm0 \n\t"
81 "pxor %%mm3, %%mm1 \n\t"
83 "paddw %%mm7, %%mm0 \n\t"
84 "paddw %%mm7, %%mm1 \n\t"
86 "pxor %%mm0, %%mm2 \n\t"
87 "pxor %%mm1, %%mm3 \n\t"
89 "pcmpeqw %%mm7, %%mm0 \n\t" // block[i] == 0 ? -1 : 0
90 "pcmpeqw %%mm7, %%mm1 \n\t" // block[i] == 0 ? -1 : 0
92 "pandn %%mm2, %%mm0 \n\t"
93 "pandn %%mm3, %%mm1 \n\t"
95 "movq %%mm0, (%0, %3) \n\t"
96 "movq %%mm1, 8(%0, %3) \n\t"
100 ::"r" (block+nCoeffs), "rm"(qmul), "rm" (qadd), "r" (2*(-nCoeffs))
107 static void dct_unquantize_h263_inter_mmx(MpegEncContext *s,
108 int16_t *block, int n, int qscale)
110 x86_reg qmul, qadd, nCoeffs;
113 qadd = (qscale - 1) | 1;
115 av_assert2(s->block_last_index[n]>=0 || s->h263_aic);
117 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
120 "movd %1, %%mm6 \n\t" //qmul
121 "packssdw %%mm6, %%mm6 \n\t"
122 "packssdw %%mm6, %%mm6 \n\t"
123 "movd %2, %%mm5 \n\t" //qadd
124 "pxor %%mm7, %%mm7 \n\t"
125 "packssdw %%mm5, %%mm5 \n\t"
126 "packssdw %%mm5, %%mm5 \n\t"
127 "psubw %%mm5, %%mm7 \n\t"
128 "pxor %%mm4, %%mm4 \n\t"
131 "movq (%0, %3), %%mm0 \n\t"
132 "movq 8(%0, %3), %%mm1 \n\t"
134 "pmullw %%mm6, %%mm0 \n\t"
135 "pmullw %%mm6, %%mm1 \n\t"
137 "movq (%0, %3), %%mm2 \n\t"
138 "movq 8(%0, %3), %%mm3 \n\t"
140 "pcmpgtw %%mm4, %%mm2 \n\t" // block[i] < 0 ? -1 : 0
141 "pcmpgtw %%mm4, %%mm3 \n\t" // block[i] < 0 ? -1 : 0
143 "pxor %%mm2, %%mm0 \n\t"
144 "pxor %%mm3, %%mm1 \n\t"
146 "paddw %%mm7, %%mm0 \n\t"
147 "paddw %%mm7, %%mm1 \n\t"
149 "pxor %%mm0, %%mm2 \n\t"
150 "pxor %%mm1, %%mm3 \n\t"
152 "pcmpeqw %%mm7, %%mm0 \n\t" // block[i] == 0 ? -1 : 0
153 "pcmpeqw %%mm7, %%mm1 \n\t" // block[i] == 0 ? -1 : 0
155 "pandn %%mm2, %%mm0 \n\t"
156 "pandn %%mm3, %%mm1 \n\t"
158 "movq %%mm0, (%0, %3) \n\t"
159 "movq %%mm1, 8(%0, %3) \n\t"
163 ::"r" (block+nCoeffs), "rm"(qmul), "rm" (qadd), "r" (2*(-nCoeffs))
168 static void dct_unquantize_mpeg1_intra_mmx(MpegEncContext *s,
169 int16_t *block, int n, int qscale)
172 const uint16_t *quant_matrix;
175 av_assert2(s->block_last_index[n]>=0);
177 nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ]+1;
180 block0 = block[0] * s->y_dc_scale;
182 block0 = block[0] * s->c_dc_scale;
183 /* XXX: only MPEG-1 */
184 quant_matrix = s->intra_matrix;
186 "pcmpeqw %%mm7, %%mm7 \n\t"
187 "psrlw $15, %%mm7 \n\t"
188 "movd %2, %%mm6 \n\t"
189 "packssdw %%mm6, %%mm6 \n\t"
190 "packssdw %%mm6, %%mm6 \n\t"
191 "mov %3, %%"FF_REG_a" \n\t"
194 "movq (%0, %%"FF_REG_a"), %%mm0 \n\t"
195 "movq 8(%0, %%"FF_REG_a"), %%mm1\n\t"
196 "movq (%1, %%"FF_REG_a"), %%mm4 \n\t"
197 "movq 8(%1, %%"FF_REG_a"), %%mm5\n\t"
198 "pmullw %%mm6, %%mm4 \n\t" // q=qscale*quant_matrix[i]
199 "pmullw %%mm6, %%mm5 \n\t" // q=qscale*quant_matrix[i]
200 "pxor %%mm2, %%mm2 \n\t"
201 "pxor %%mm3, %%mm3 \n\t"
202 "pcmpgtw %%mm0, %%mm2 \n\t" // block[i] < 0 ? -1 : 0
203 "pcmpgtw %%mm1, %%mm3 \n\t" // block[i] < 0 ? -1 : 0
204 "pxor %%mm2, %%mm0 \n\t"
205 "pxor %%mm3, %%mm1 \n\t"
206 "psubw %%mm2, %%mm0 \n\t" // abs(block[i])
207 "psubw %%mm3, %%mm1 \n\t" // abs(block[i])
208 "pmullw %%mm4, %%mm0 \n\t" // abs(block[i])*q
209 "pmullw %%mm5, %%mm1 \n\t" // abs(block[i])*q
210 "pxor %%mm4, %%mm4 \n\t"
211 "pxor %%mm5, %%mm5 \n\t" // FIXME slow
212 "pcmpeqw (%0, %%"FF_REG_a"), %%mm4 \n\t" // block[i] == 0 ? -1 : 0
213 "pcmpeqw 8(%0, %%"FF_REG_a"), %%mm5\n\t" // block[i] == 0 ? -1 : 0
214 "psraw $3, %%mm0 \n\t"
215 "psraw $3, %%mm1 \n\t"
216 "psubw %%mm7, %%mm0 \n\t"
217 "psubw %%mm7, %%mm1 \n\t"
218 "por %%mm7, %%mm0 \n\t"
219 "por %%mm7, %%mm1 \n\t"
220 "pxor %%mm2, %%mm0 \n\t"
221 "pxor %%mm3, %%mm1 \n\t"
222 "psubw %%mm2, %%mm0 \n\t"
223 "psubw %%mm3, %%mm1 \n\t"
224 "pandn %%mm0, %%mm4 \n\t"
225 "pandn %%mm1, %%mm5 \n\t"
226 "movq %%mm4, (%0, %%"FF_REG_a") \n\t"
227 "movq %%mm5, 8(%0, %%"FF_REG_a")\n\t"
229 "add $16, %%"FF_REG_a" \n\t"
231 ::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "rm" (qscale), "g" (-2*nCoeffs)
232 : "%"FF_REG_a, "memory"
237 static void dct_unquantize_mpeg1_inter_mmx(MpegEncContext *s,
238 int16_t *block, int n, int qscale)
241 const uint16_t *quant_matrix;
243 av_assert2(s->block_last_index[n]>=0);
245 nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ]+1;
247 quant_matrix = s->inter_matrix;
249 "pcmpeqw %%mm7, %%mm7 \n\t"
250 "psrlw $15, %%mm7 \n\t"
251 "movd %2, %%mm6 \n\t"
252 "packssdw %%mm6, %%mm6 \n\t"
253 "packssdw %%mm6, %%mm6 \n\t"
254 "mov %3, %%"FF_REG_a" \n\t"
257 "movq (%0, %%"FF_REG_a"), %%mm0 \n\t"
258 "movq 8(%0, %%"FF_REG_a"), %%mm1\n\t"
259 "movq (%1, %%"FF_REG_a"), %%mm4 \n\t"
260 "movq 8(%1, %%"FF_REG_a"), %%mm5\n\t"
261 "pmullw %%mm6, %%mm4 \n\t" // q=qscale*quant_matrix[i]
262 "pmullw %%mm6, %%mm5 \n\t" // q=qscale*quant_matrix[i]
263 "pxor %%mm2, %%mm2 \n\t"
264 "pxor %%mm3, %%mm3 \n\t"
265 "pcmpgtw %%mm0, %%mm2 \n\t" // block[i] < 0 ? -1 : 0
266 "pcmpgtw %%mm1, %%mm3 \n\t" // block[i] < 0 ? -1 : 0
267 "pxor %%mm2, %%mm0 \n\t"
268 "pxor %%mm3, %%mm1 \n\t"
269 "psubw %%mm2, %%mm0 \n\t" // abs(block[i])
270 "psubw %%mm3, %%mm1 \n\t" // abs(block[i])
271 "paddw %%mm0, %%mm0 \n\t" // abs(block[i])*2
272 "paddw %%mm1, %%mm1 \n\t" // abs(block[i])*2
273 "paddw %%mm7, %%mm0 \n\t" // abs(block[i])*2 + 1
274 "paddw %%mm7, %%mm1 \n\t" // abs(block[i])*2 + 1
275 "pmullw %%mm4, %%mm0 \n\t" // (abs(block[i])*2 + 1)*q
276 "pmullw %%mm5, %%mm1 \n\t" // (abs(block[i])*2 + 1)*q
277 "pxor %%mm4, %%mm4 \n\t"
278 "pxor %%mm5, %%mm5 \n\t" // FIXME slow
279 "pcmpeqw (%0, %%"FF_REG_a"), %%mm4 \n\t" // block[i] == 0 ? -1 : 0
280 "pcmpeqw 8(%0, %%"FF_REG_a"), %%mm5\n\t" // block[i] == 0 ? -1 : 0
281 "psraw $4, %%mm0 \n\t"
282 "psraw $4, %%mm1 \n\t"
283 "psubw %%mm7, %%mm0 \n\t"
284 "psubw %%mm7, %%mm1 \n\t"
285 "por %%mm7, %%mm0 \n\t"
286 "por %%mm7, %%mm1 \n\t"
287 "pxor %%mm2, %%mm0 \n\t"
288 "pxor %%mm3, %%mm1 \n\t"
289 "psubw %%mm2, %%mm0 \n\t"
290 "psubw %%mm3, %%mm1 \n\t"
291 "pandn %%mm0, %%mm4 \n\t"
292 "pandn %%mm1, %%mm5 \n\t"
293 "movq %%mm4, (%0, %%"FF_REG_a") \n\t"
294 "movq %%mm5, 8(%0, %%"FF_REG_a")\n\t"
296 "add $16, %%"FF_REG_a" \n\t"
298 ::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "rm" (qscale), "g" (-2*nCoeffs)
299 : "%"FF_REG_a, "memory"
303 static void dct_unquantize_mpeg2_intra_mmx(MpegEncContext *s,
304 int16_t *block, int n, int qscale)
307 const uint16_t *quant_matrix;
310 av_assert2(s->block_last_index[n]>=0);
312 if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
315 if(s->alternate_scan) nCoeffs= 63; //FIXME
316 else nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ];
319 block0 = block[0] * s->y_dc_scale;
321 block0 = block[0] * s->c_dc_scale;
322 quant_matrix = s->intra_matrix;
324 "pcmpeqw %%mm7, %%mm7 \n\t"
325 "psrlw $15, %%mm7 \n\t"
326 "movd %2, %%mm6 \n\t"
327 "packssdw %%mm6, %%mm6 \n\t"
328 "packssdw %%mm6, %%mm6 \n\t"
329 "mov %3, %%"FF_REG_a" \n\t"
332 "movq (%0, %%"FF_REG_a"), %%mm0 \n\t"
333 "movq 8(%0, %%"FF_REG_a"), %%mm1\n\t"
334 "movq (%1, %%"FF_REG_a"), %%mm4 \n\t"
335 "movq 8(%1, %%"FF_REG_a"), %%mm5\n\t"
336 "pmullw %%mm6, %%mm4 \n\t" // q=qscale*quant_matrix[i]
337 "pmullw %%mm6, %%mm5 \n\t" // q=qscale*quant_matrix[i]
338 "pxor %%mm2, %%mm2 \n\t"
339 "pxor %%mm3, %%mm3 \n\t"
340 "pcmpgtw %%mm0, %%mm2 \n\t" // block[i] < 0 ? -1 : 0
341 "pcmpgtw %%mm1, %%mm3 \n\t" // block[i] < 0 ? -1 : 0
342 "pxor %%mm2, %%mm0 \n\t"
343 "pxor %%mm3, %%mm1 \n\t"
344 "psubw %%mm2, %%mm0 \n\t" // abs(block[i])
345 "psubw %%mm3, %%mm1 \n\t" // abs(block[i])
346 "pmullw %%mm4, %%mm0 \n\t" // abs(block[i])*q
347 "pmullw %%mm5, %%mm1 \n\t" // abs(block[i])*q
348 "pxor %%mm4, %%mm4 \n\t"
349 "pxor %%mm5, %%mm5 \n\t" // FIXME slow
350 "pcmpeqw (%0, %%"FF_REG_a"), %%mm4 \n\t" // block[i] == 0 ? -1 : 0
351 "pcmpeqw 8(%0, %%"FF_REG_a"), %%mm5\n\t" // block[i] == 0 ? -1 : 0
352 "psraw $4, %%mm0 \n\t"
353 "psraw $4, %%mm1 \n\t"
354 "pxor %%mm2, %%mm0 \n\t"
355 "pxor %%mm3, %%mm1 \n\t"
356 "psubw %%mm2, %%mm0 \n\t"
357 "psubw %%mm3, %%mm1 \n\t"
358 "pandn %%mm0, %%mm4 \n\t"
359 "pandn %%mm1, %%mm5 \n\t"
360 "movq %%mm4, (%0, %%"FF_REG_a") \n\t"
361 "movq %%mm5, 8(%0, %%"FF_REG_a")\n\t"
363 "add $16, %%"FF_REG_a" \n\t"
365 ::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "rm" (qscale), "g" (-2*nCoeffs)
366 : "%"FF_REG_a, "memory"
369 //Note, we do not do mismatch control for intra as errors cannot accumulate
372 static void dct_unquantize_mpeg2_inter_mmx(MpegEncContext *s,
373 int16_t *block, int n, int qscale)
376 const uint16_t *quant_matrix;
378 av_assert2(s->block_last_index[n]>=0);
380 if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
383 if(s->alternate_scan) nCoeffs= 63; //FIXME
384 else nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ];
386 quant_matrix = s->inter_matrix;
388 "pcmpeqw %%mm7, %%mm7 \n\t"
389 "psrlq $48, %%mm7 \n\t"
390 "movd %2, %%mm6 \n\t"
391 "packssdw %%mm6, %%mm6 \n\t"
392 "packssdw %%mm6, %%mm6 \n\t"
393 "mov %3, %%"FF_REG_a" \n\t"
396 "movq (%0, %%"FF_REG_a"), %%mm0 \n\t"
397 "movq 8(%0, %%"FF_REG_a"), %%mm1\n\t"
398 "movq (%1, %%"FF_REG_a"), %%mm4 \n\t"
399 "movq 8(%1, %%"FF_REG_a"), %%mm5\n\t"
400 "pmullw %%mm6, %%mm4 \n\t" // q=qscale*quant_matrix[i]
401 "pmullw %%mm6, %%mm5 \n\t" // q=qscale*quant_matrix[i]
402 "pxor %%mm2, %%mm2 \n\t"
403 "pxor %%mm3, %%mm3 \n\t"
404 "pcmpgtw %%mm0, %%mm2 \n\t" // block[i] < 0 ? -1 : 0
405 "pcmpgtw %%mm1, %%mm3 \n\t" // block[i] < 0 ? -1 : 0
406 "pxor %%mm2, %%mm0 \n\t"
407 "pxor %%mm3, %%mm1 \n\t"
408 "psubw %%mm2, %%mm0 \n\t" // abs(block[i])
409 "psubw %%mm3, %%mm1 \n\t" // abs(block[i])
410 "paddw %%mm0, %%mm0 \n\t" // abs(block[i])*2
411 "paddw %%mm1, %%mm1 \n\t" // abs(block[i])*2
412 "pmullw %%mm4, %%mm0 \n\t" // abs(block[i])*2*q
413 "pmullw %%mm5, %%mm1 \n\t" // abs(block[i])*2*q
414 "paddw %%mm4, %%mm0 \n\t" // (abs(block[i])*2 + 1)*q
415 "paddw %%mm5, %%mm1 \n\t" // (abs(block[i])*2 + 1)*q
416 "pxor %%mm4, %%mm4 \n\t"
417 "pxor %%mm5, %%mm5 \n\t" // FIXME slow
418 "pcmpeqw (%0, %%"FF_REG_a"), %%mm4 \n\t" // block[i] == 0 ? -1 : 0
419 "pcmpeqw 8(%0, %%"FF_REG_a"), %%mm5\n\t" // block[i] == 0 ? -1 : 0
420 "psrlw $5, %%mm0 \n\t"
421 "psrlw $5, %%mm1 \n\t"
422 "pxor %%mm2, %%mm0 \n\t"
423 "pxor %%mm3, %%mm1 \n\t"
424 "psubw %%mm2, %%mm0 \n\t"
425 "psubw %%mm3, %%mm1 \n\t"
426 "pandn %%mm0, %%mm4 \n\t"
427 "pandn %%mm1, %%mm5 \n\t"
428 "pxor %%mm4, %%mm7 \n\t"
429 "pxor %%mm5, %%mm7 \n\t"
430 "movq %%mm4, (%0, %%"FF_REG_a") \n\t"
431 "movq %%mm5, 8(%0, %%"FF_REG_a")\n\t"
433 "add $16, %%"FF_REG_a" \n\t"
435 "movd 124(%0, %3), %%mm0 \n\t"
436 "movq %%mm7, %%mm6 \n\t"
437 "psrlq $32, %%mm7 \n\t"
438 "pxor %%mm6, %%mm7 \n\t"
439 "movq %%mm7, %%mm6 \n\t"
440 "psrlq $16, %%mm7 \n\t"
441 "pxor %%mm6, %%mm7 \n\t"
442 "pslld $31, %%mm7 \n\t"
443 "psrlq $15, %%mm7 \n\t"
444 "pxor %%mm7, %%mm0 \n\t"
445 "movd %%mm0, 124(%0, %3) \n\t"
447 ::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "rm" (qscale), "r" (-2*nCoeffs)
448 : "%"FF_REG_a, "memory"
452 #endif /* HAVE_MMX_INLINE */
454 av_cold void ff_mpv_common_init_x86(MpegEncContext *s)
457 int cpu_flags = av_get_cpu_flags();
459 if (INLINE_MMX(cpu_flags)) {
460 s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_mmx;
461 s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_mmx;
462 s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_mmx;
463 s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_mmx;
464 if (!(s->avctx->flags & AV_CODEC_FLAG_BITEXACT))
465 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_mmx;
466 s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_mmx;
468 #endif /* HAVE_MMX_INLINE */