1 ;******************************************************************************
2 ;* MMX/SSE2-optimized functions for the VP3 decoder
3 ;* Copyright (c) 2007 Aurelien Jacobs <aurel@gnuage.org>
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
20 ;******************************************************************************
22 %include "libavutil/x86/x86inc.asm"
23 %include "libavutil/x86/x86util.asm"
25 ; MMX-optimized functions cribbed from the original VP3 source code.
29 vp3_idct_data: times 8 dw 64277
48 ; this is off by one or two for some cases when filter_limit is greater than 63
49 ; in: p0 in mm6, p1 in mm4, p2 in mm2, p3 in mm1
50 ; out: p1 in mm4, p2 in mm3
51 %macro VP3_LOOP_FILTER 0
53 pand m6, [pb_7] ; p0&7
55 pand m7, [pb_1F] ; p0>>3
58 pand m2, [pb_1] ; (p2^p1)&1
61 paddb m2, m5 ; 3*(p2^p1)&1
62 paddb m2, m6 ; extra bits lost in shifts
64 pxor m1, m0 ; 255 - p3
65 pavgb m1, m2 ; (256 - p3 + extrabits) >> 1
66 pxor m0, m4 ; 255 - p1
67 pavgb m0, m3 ; (256 + p2-p1) >> 1
69 pavgb m1, m0 ; 128+2+( p2-p1 - p3) >> 2
70 pavgb m1, m0 ; 128+1+(3*(p2-p1) - p3) >> 3
71 paddusb m7, m1 ; d+128+1
76 movq m5, [r2+516] ; flim
93 %macro STORE_4_WORDS 1
106 cglobal vp3_v_loop_filter, 3, 4
123 cglobal vp3_h_loop_filter, 3, 4
134 punpcklbw m6, [r0 -2]
135 punpcklbw m4, [r0+r1 -2]
136 punpcklbw m2, [r0+r1*2-2]
137 punpcklbw m1, [r0+r3 -2]
141 TRANSPOSE4x4B 6, 4, 2, 1, 0
143 SBUTTERFLY bw, 4, 3, 5
150 ; from original comments: The Macro does IDct on 4 1-D Dcts
156 pmulhw m4, m6 ; r4 = c3*i3 - i3
158 pmulhw m6, m7 ; r6 = c3*i5 - i5
160 pmulhw m1, m2 ; r1 = c5*i3 - i3
162 pmulhw m5, m7 ; r5 = c5*i5 - i5
164 paddw m4, m2 ; r4 = c3*i3
165 paddw m6, m7 ; r6 = c3*i5
166 paddw m2, m1 ; r2 = c5*i3
168 paddw m7, m5 ; r7 = c5*i5
169 movq m5, m0 ; r5 = c1
170 pmulhw m0, m3 ; r0 = c1*i1 - i1
171 paddsw m4, m7 ; r4 = C = c3*i3 + c5*i5
172 pmulhw m5, m1 ; r5 = c1*i7 - i7
174 psubsw m6, m2 ; r6 = D = c3*i5 - c5*i3
175 paddw m0, m3 ; r0 = c1*i1
176 pmulhw m3, m7 ; r3 = c7*i1
178 pmulhw m7, m1 ; r7 = c7*i7
179 paddw m5, m1 ; r5 = c1*i7
180 movq m1, m2 ; r1 = i2
181 pmulhw m2, C(2) ; r2 = c2*i2 - i2
182 psubsw m3, m5 ; r3 = B = c7*i1 - c1*i7
184 paddsw m0, m7 ; r0 = A = c1*i1 + c7*i7
185 movq m7, m5 ; r7 = i6
186 psubsw m0, m4 ; r0 = A - C
187 pmulhw m5, C(2) ; r5 = c2*i6 - i6
188 paddw m2, m1 ; r2 = c2*i2
189 pmulhw m1, C(6) ; r1 = c6*i2
190 paddsw m4, m4 ; r4 = C + C
191 paddsw m4, m0 ; r4 = C. = A + C
192 psubsw m3, m6 ; r3 = B - D
193 paddw m5, m7 ; r5 = c2*i6
194 paddsw m6, m6 ; r6 = D + D
195 pmulhw m7, C(6) ; r7 = c6*i6
196 paddsw m6, m3 ; r6 = D. = B + D
197 movq I(1), m4 ; save C. at I(1)
198 psubsw m1, m5 ; r1 = H = c6*i2 - c2*i6
200 movq m5, m3 ; r5 = B - D
201 pmulhw m3, m4 ; r3 = (c4 - 1) * (B - D)
202 paddsw m7, m2 ; r3 = (c4 - 1) * (B - D)
203 movq I(2), m6 ; save D. at I(2)
204 movq m2, m0 ; r2 = A - C
206 pmulhw m0, m4 ; r0 = (c4 - 1) * (A - C)
207 paddw m5, m3 ; r5 = B. = c4 * (B - D)
209 psubsw m5, m1 ; r5 = B.. = B. - H
210 paddw m2, m0 ; r0 = A. = c4 * (A - C)
211 psubsw m6, m3 ; r6 = i0 - i4
213 pmulhw m6, m4 ; r6 = (c4 - 1) * (i0 - i4)
214 paddsw m3, m3 ; r3 = i4 + i4
215 paddsw m1, m1 ; r1 = H + H
216 paddsw m3, m0 ; r3 = i0 + i4
217 paddsw m1, m5 ; r1 = H. = B + H
218 pmulhw m4, m3 ; r4 = (c4 - 1) * (i0 + i4)
219 paddsw m6, m0 ; r6 = F = c4 * (i0 - i4)
220 psubsw m6, m2 ; r6 = F. = F - A.
221 paddsw m2, m2 ; r2 = A. + A.
222 movq m0, I(1) ; r0 = C.
223 paddsw m2, m6 ; r2 = A.. = F + A.
224 paddw m4, m3 ; r4 = E = c4 * (i0 + i4)
225 psubsw m2, m1 ; r2 = R2 = A.. - H.
228 ; RowIDCT gets ready to transpose
231 movq m3, I(2) ; r3 = D.
232 psubsw m4, m7 ; r4 = E. = E - G
233 paddsw m1, m1 ; r1 = H. + H.
234 paddsw m7, m7 ; r7 = G + G
235 paddsw m1, m2 ; r1 = R1 = A.. + H.
236 paddsw m7, m4 ; r1 = R1 = A.. + H.
237 psubsw m4, m3 ; r4 = R4 = E. - D.
239 psubsw m6, m5 ; r6 = R6 = F. - B..
241 paddsw m3, m4 ; r3 = R3 = E. + D.
242 paddsw m5, m6 ; r5 = R5 = F. + B..
243 psubsw m7, m0 ; r7 = R7 = G. - C.
245 movq I(1), m1 ; save R1
246 paddsw m0, m7 ; r0 = R0 = G. + C.
249 ; Column IDCT normalizes and stores final results
252 paddsw m2, OC_8 ; adjust R2 (and R1) for shift
253 paddsw m1, m1 ; r1 = H. + H.
254 paddsw m1, m2 ; r1 = R1 = A.. + H.
255 psraw m2, 4 ; r2 = NR2
256 psubsw m4, m7 ; r4 = E. = E - G
257 psraw m1, 4 ; r1 = NR2
258 movq m3, I(2) ; r3 = D.
259 paddsw m7, m7 ; r7 = G + G
260 movq I(2), m2 ; store NR2 at I2
261 paddsw m7, m4 ; r7 = G. = E + G
262 movq I(1), m1 ; store NR1 at I1
263 psubsw m4, m3 ; r4 = R4 = E. - D.
264 paddsw m4, OC_8 ; adjust R4 (and R3) for shift
265 paddsw m3, m3 ; r3 = D. + D.
266 paddsw m3, m4 ; r3 = R3 = E. + D.
267 psraw m4, 4 ; r4 = NR4
268 psubsw m6, m5 ; r6 = R6 = F. - B..
269 psraw m3, 4 ; r3 = NR3
270 paddsw m6, OC_8 ; adjust R6 (and R5) for shift
271 paddsw m5, m5 ; r5 = B.. + B..
272 paddsw m5, m6 ; r5 = R5 = F. + B..
273 psraw m6, 4 ; r6 = NR6
274 movq J(4), m4 ; store NR4 at J4
275 psraw m5, 4 ; r5 = NR5
276 movq I(3), m3 ; store NR3 at I3
277 psubsw m7, m0 ; r7 = R7 = G. - C.
278 paddsw m7, OC_8 ; adjust R7 (and R0) for shift
279 paddsw m0, m0 ; r0 = C. + C.
280 paddsw m0, m7 ; r0 = R0 = G. + C.
281 psraw m7, 4 ; r7 = NR7
282 movq J(6), m6 ; store NR6 at J6
283 psraw m0, 4 ; r0 = NR0
284 movq J(5), m5 ; store NR5 at J5
285 movq J(7), m7 ; store NR7 at J7
286 movq I(0), m0 ; store NR0 at I0
289 ; Following macro does two 4x4 transposes in place.
291 ; At entry (we assume):
315 ; I(0) I(1) I(2) I(3) is the transpose of r0 I(1) r2 r3.
316 ; J(4) J(5) J(6) J(7) is the transpose of r4 r5 r6 r7.
318 ; Since r1 is free at entry, we calculate the Js first.
320 movq m1, m4 ; r1 = e3 e2 e1 e0
321 punpcklwd m4, m5 ; r4 = f1 e1 f0 e0
322 movq I(0), m0 ; save a3 a2 a1 a0
323 punpckhwd m1, m5 ; r1 = f3 e3 f2 e2
324 movq m0, m6 ; r0 = g3 g2 g1 g0
325 punpcklwd m6, m7 ; r6 = h1 g1 h0 g0
326 movq m5, m4 ; r5 = f1 e1 f0 e0
327 punpckldq m4, m6 ; r4 = h0 g0 f0 e0 = R4
328 punpckhdq m5, m6 ; r5 = h1 g1 f1 e1 = R5
329 movq m6, m1 ; r6 = f3 e3 f2 e2
331 punpckhwd m0, m7 ; r0 = h3 g3 h2 g2
333 punpckhdq m6, m0 ; r6 = h3 g3 f3 e3 = R7
334 movq m4, I(0) ; r4 = a3 a2 a1 a0
335 punpckldq m1, m0 ; r1 = h2 g2 f2 e2 = R6
336 movq m5, I(1) ; r5 = b3 b2 b1 b0
337 movq m0, m4 ; r0 = a3 a2 a1 a0
339 punpcklwd m0, m5 ; r0 = b1 a1 b0 a0
341 punpckhwd m4, m5 ; r4 = b3 a3 b2 a2
342 movq m5, m2 ; r5 = c3 c2 c1 c0
343 punpcklwd m2, m3 ; r2 = d1 c1 d0 c0
344 movq m1, m0 ; r1 = b1 a1 b0 a0
345 punpckldq m0, m2 ; r0 = d0 c0 b0 a0 = R0
346 punpckhdq m1, m2 ; r1 = d1 c1 b1 a1 = R1
347 movq m2, m4 ; r2 = b3 a3 b2 a2
349 punpckhwd m5, m3 ; r5 = d3 c3 d2 c2
351 punpckhdq m4, m5 ; r4 = d3 c3 b3 a3 = R3
352 punpckldq m2, m5 ; r2 = d2 c2 b2 a2 = R2
357 %macro VP3_1D_IDCT_SSE2 0
358 movdqa m2, I(3) ; xmm2 = i3
359 movdqa m6, C(3) ; xmm6 = c3
360 movdqa m4, m2 ; xmm4 = i3
361 movdqa m7, I(5) ; xmm7 = i5
362 pmulhw m4, m6 ; xmm4 = c3 * i3 - i3
363 movdqa m1, C(5) ; xmm1 = c5
364 pmulhw m6, m7 ; xmm6 = c3 * i5 - i5
365 movdqa m5, m1 ; xmm5 = c5
366 pmulhw m1, m2 ; xmm1 = c5 * i3 - i3
367 movdqa m3, I(1) ; xmm3 = i1
368 pmulhw m5, m7 ; xmm5 = c5 * i5 - i5
369 movdqa m0, C(1) ; xmm0 = c1
370 paddw m4, m2 ; xmm4 = c3 * i3
371 paddw m6, m7 ; xmm6 = c3 * i5
372 paddw m2, m1 ; xmm2 = c5 * i3
373 movdqa m1, I(7) ; xmm1 = i7
374 paddw m7, m5 ; xmm7 = c5 * i5
375 movdqa m5, m0 ; xmm5 = c1
376 pmulhw m0, m3 ; xmm0 = c1 * i1 - i1
377 paddsw m4, m7 ; xmm4 = c3 * i3 + c5 * i5 = C
378 pmulhw m5, m1 ; xmm5 = c1 * i7 - i7
379 movdqa m7, C(7) ; xmm7 = c7
380 psubsw m6, m2 ; xmm6 = c3 * i5 - c5 * i3 = D
381 paddw m0, m3 ; xmm0 = c1 * i1
382 pmulhw m3, m7 ; xmm3 = c7 * i1
383 movdqa m2, I(2) ; xmm2 = i2
384 pmulhw m7, m1 ; xmm7 = c7 * i7
385 paddw m5, m1 ; xmm5 = c1 * i7
386 movdqa m1, m2 ; xmm1 = i2
387 pmulhw m2, C(2) ; xmm2 = i2 * c2 -i2
388 psubsw m3, m5 ; xmm3 = c7 * i1 - c1 * i7 = B
389 movdqa m5, I(6) ; xmm5 = i6
390 paddsw m0, m7 ; xmm0 = c1 * i1 + c7 * i7 = A
391 movdqa m7, m5 ; xmm7 = i6
392 psubsw m0, m4 ; xmm0 = A - C
393 pmulhw m5, C(2) ; xmm5 = c2 * i6 - i6
394 paddw m2, m1 ; xmm2 = i2 * c2
395 pmulhw m1, C(6) ; xmm1 = c6 * i2
396 paddsw m4, m4 ; xmm4 = C + C
397 paddsw m4, m0 ; xmm4 = A + C = C.
398 psubsw m3, m6 ; xmm3 = B - D
399 paddw m5, m7 ; xmm5 = c2 * i6
400 paddsw m6, m6 ; xmm6 = D + D
401 pmulhw m7, C(6) ; xmm7 = c6 * i6
402 paddsw m6, m3 ; xmm6 = B + D = D.
403 movdqa I(1), m4 ; Save C. at I(1)
404 psubsw m1, m5 ; xmm1 = c6 * i2 - c2 * i6 = H
405 movdqa m4, C(4) ; xmm4 = C4
406 movdqa m5, m3 ; xmm5 = B - D
407 pmulhw m3, m4 ; xmm3 = ( c4 -1 ) * ( B - D )
408 paddsw m7, m2 ; xmm7 = c2 * i2 + c6 * i6 = G
409 movdqa I(2), m6 ; save D. at I(2)
410 movdqa m2, m0 ; xmm2 = A - C
411 movdqa m6, I(0) ; xmm6 = i0
412 pmulhw m0, m4 ; xmm0 = ( c4 - 1 ) * ( A - C ) = A.
413 paddw m5, m3 ; xmm5 = c4 * ( B - D ) = B.
414 movdqa m3, I(4) ; xmm3 = i4
415 psubsw m5, m1 ; xmm5 = B. - H = B..
416 paddw m2, m0 ; xmm2 = c4 * ( A - C) = A.
417 psubsw m6, m3 ; xmm6 = i0 - i4
418 movdqa m0, m6 ; xmm0 = i0 - i4
419 pmulhw m6, m4 ; xmm6 = (c4 - 1) * (i0 - i4) = F
420 paddsw m3, m3 ; xmm3 = i4 + i4
421 paddsw m1, m1 ; xmm1 = H + H
422 paddsw m3, m0 ; xmm3 = i0 + i4
423 paddsw m1, m5 ; xmm1 = B. + H = H.
424 pmulhw m4, m3 ; xmm4 = ( c4 - 1 ) * ( i0 + i4 )
425 paddw m6, m0 ; xmm6 = c4 * ( i0 - i4 )
426 psubsw m6, m2 ; xmm6 = F - A. = F.
427 paddsw m2, m2 ; xmm2 = A. + A.
428 movdqa m0, I(1) ; Load C. from I(1)
429 paddsw m2, m6 ; xmm2 = F + A. = A..
430 paddw m4, m3 ; xmm4 = c4 * ( i0 + i4 ) = 3
431 psubsw m2, m1 ; xmm2 = A.. - H. = R2
432 ADD(m2) ; Adjust R2 and R1 before shifting
433 paddsw m1, m1 ; xmm1 = H. + H.
434 paddsw m1, m2 ; xmm1 = A.. + H. = R1
435 SHIFT(m2) ; xmm2 = op2
436 psubsw m4, m7 ; xmm4 = E - G = E.
437 SHIFT(m1) ; xmm1 = op1
438 movdqa m3, I(2) ; Load D. from I(2)
439 paddsw m7, m7 ; xmm7 = G + G
440 paddsw m7, m4 ; xmm7 = E + G = G.
441 psubsw m4, m3 ; xmm4 = E. - D. = R4
442 ADD(m4) ; Adjust R4 and R3 before shifting
443 paddsw m3, m3 ; xmm3 = D. + D.
444 paddsw m3, m4 ; xmm3 = E. + D. = R3
445 SHIFT(m4) ; xmm4 = op4
446 psubsw m6, m5 ; xmm6 = F. - B..= R6
447 SHIFT(m3) ; xmm3 = op3
448 ADD(m6) ; Adjust R6 and R5 before shifting
449 paddsw m5, m5 ; xmm5 = B.. + B..
450 paddsw m5, m6 ; xmm5 = F. + B.. = R5
451 SHIFT(m6) ; xmm6 = op6
452 SHIFT(m5) ; xmm5 = op5
453 psubsw m7, m0 ; xmm7 = G. - C. = R7
454 ADD(m7) ; Adjust R7 and R0 before shifting
455 paddsw m0, m0 ; xmm0 = C. + C.
456 paddsw m0, m7 ; xmm0 = G. + C.
457 SHIFT(m7) ; xmm7 = op7
458 SHIFT(m0) ; xmm0 = op0
474 %define I(x) [%1+16*x]
475 %define O(x) [%1+16*x]
476 %define C(x) [vp3_idct_data+16*(x-1)]
481 TRANSPOSE8x8W 0, 1, 2, 3, 4, 5, 6, 7, 8
483 TRANSPOSE8x8W 0, 1, 2, 3, 4, 5, 6, 7, [%1], [%1+16]
485 PUT_BLOCK 0, 1, 2, 3, 4, 5, 6, 7
487 %define SHIFT(x) psraw x, 4
488 %define ADD(x) paddsw x, [pw_8]
490 PUT_BLOCK 0, 1, 2, 3, 4, 5, 6, 7
492 ; eax = quantized input
493 ; ebx = dequantizer matrix
494 ; ecx = IDCT constants
495 ; M(I) = ecx + MaskOffset(0) + I * 8
496 ; C(I) = ecx + CosineOffset(32) + (I-1) * 8
500 %define C(x) [vp3_idct_data+16*(x-1)]
502 ; at this point, function has completed dequantization + dezigzag +
503 ; partial transposition; now do the idct itself
504 %define I(x) [%1+16* x ]
505 %define J(x) [%1+16*(x-4)+8]
509 %define I(x) [%1+16* x +64]
510 %define J(x) [%1+16*(x-4)+72]
514 %define I(x) [%1+16*x]
515 %define J(x) [%1+16*x]
518 %define I(x) [%1+16*x+8]
519 %define J(x) [%1+16*x+8]
521 %endif ; mmsize == 16/8
524 %macro vp3_idct_funcs 0
525 cglobal vp3_idct_put, 3, 4, 9
533 mova m0, [r2+mmsize*0+%%i]
534 mova m1, [r2+mmsize*2+%%i]
535 mova m2, [r2+mmsize*4+%%i]
536 mova m3, [r2+mmsize*6+%%i]
537 packsswb m0, [r2+mmsize*1+%%i]
538 packsswb m1, [r2+mmsize*3+%%i]
539 packsswb m2, [r2+mmsize*5+%%i]
540 packsswb m3, [r2+mmsize*7+%%i]
568 cglobal vp3_idct_add, 3, 4, 9
638 cglobal vp3_idct_dc_add, 3, 4