summaryrefslogtreecommitdiffstats
path: root/libvpx/vp9/common/x86/vp9_postproc_mmx.asm
blob: c2118dbb74e7d0e0011b042e613eea8a322f1d66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
;
;  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
;
;  Use of this source code is governed by a BSD-style license
;  that can be found in the LICENSE file in the root of the source
;  tree. An additional intellectual property rights grant can be found
;  in the file PATENTS.  All contributing project authors may
;  be found in the AUTHORS file in the root of the source tree.
;


%include "vpx_ports/x86_abi_support.asm"

%define VP9_FILTER_WEIGHT 128
%define VP9_FILTER_SHIFT  7

;void vp9_post_proc_down_and_across_mmx
;(
;    unsigned char *src_ptr,
;    unsigned char *dst_ptr,
;    int src_pixels_per_line,
;    int dst_pixels_per_line,
;    int rows,
;    int cols,
;    int flimit
;)
global sym(vp9_post_proc_down_and_across_mmx) PRIVATE
sym(vp9_post_proc_down_and_across_mmx):
    push        rbp
    mov         rbp, rsp
    SHADOW_ARGS_TO_STACK 7
    GET_GOT     rbx
    push        rsi
    push        rdi
    ; end prolog

%if ABI_IS_32BIT=1 && CONFIG_PIC=1
    ; move the global rd onto the stack, since we don't have enough registers
    ; to do PIC addressing
    movq        mm0, [GLOBAL(rd)]
    sub         rsp, 8
    movq        [rsp], mm0
%define RD [rsp]
%else
%define RD [GLOBAL(rd)]
%endif

        push        rbx
        lea         rbx, [GLOBAL(Blur)]
        movd        mm2, dword ptr arg(6) ;flimit
        punpcklwd   mm2, mm2
        punpckldq   mm2, mm2

        mov         rsi,        arg(0) ;src_ptr
        mov         rdi,        arg(1) ;dst_ptr

        movsxd      rcx, DWORD PTR arg(4) ;rows
        movsxd      rax, DWORD PTR arg(2) ;src_pixels_per_line ; destination pitch?
        pxor        mm0, mm0              ; mm0 = 00000000

.nextrow:

        xor         rdx,        rdx       ; clear out rdx for use as loop counter
.nextcol:

        pxor        mm7, mm7              ; mm7 = 00000000
        movq        mm6, [rbx + 32 ]      ; mm6 = kernel 2 taps
        movq        mm3, [rsi]            ; mm4 = r0 p0..p7
        punpcklbw   mm3, mm0              ; mm3 = p0..p3
        movq        mm1, mm3              ; mm1 = p0..p3
        pmullw      mm3, mm6              ; mm3 *= kernel 2 modifiers

        movq        mm6, [rbx + 48]       ; mm6 = kernel 3 taps
        movq        mm5, [rsi + rax]      ; mm4 = r1 p0..p7
        punpcklbw   mm5, mm0              ; mm5 = r1 p0..p3
        pmullw      mm6, mm5              ; mm6 *= p0..p3 * kernel 3 modifiers
        paddusw     mm3, mm6              ; mm3 += mm6

        ; thresholding
        movq        mm7, mm1              ; mm7 = r0 p0..p3
        psubusw     mm7, mm5              ; mm7 = r0 p0..p3 - r1 p0..p3
        psubusw     mm5, mm1              ; mm5 = r1 p0..p3 - r0 p0..p3
        paddusw     mm7, mm5              ; mm7 = abs(r0 p0..p3 - r1 p0..p3)
        pcmpgtw     mm7, mm2

        movq        mm6, [rbx + 64 ]      ; mm6 = kernel 4 modifiers
        movq        mm5, [rsi + 2*rax]    ; mm4 = r2 p0..p7
        punpcklbw   mm5, mm0              ; mm5 = r2 p0..p3
        pmullw      mm6, mm5              ; mm5 *= kernel 4 modifiers
        paddusw     mm3, mm6              ; mm3 += mm5

        ; thresholding
        movq        mm6, mm1              ; mm6 = r0 p0..p3
        psubusw     mm6, mm5              ; mm6 = r0 p0..p3 - r2 p0..p3
        psubusw     mm5, mm1              ; mm5 = r2 p0..p3 - r2 p0..p3
        paddusw     mm6, mm5              ; mm6 = abs(r0 p0..p3 - r2 p0..p3)
        pcmpgtw     mm6, mm2
        por         mm7, mm6              ; accumulate thresholds


        neg         rax
        movq        mm6, [rbx ]           ; kernel 0 taps
        movq        mm5, [rsi+2*rax]      ; mm4 = r-2 p0..p7
        punpcklbw   mm5, mm0              ; mm5 = r-2 p0..p3
        pmullw      mm6, mm5              ; mm5 *= kernel 0 modifiers
        paddusw     mm3, mm6              ; mm3 += mm5

        ; thresholding
        movq        mm6, mm1              ; mm6 = r0 p0..p3
        psubusw     mm6, mm5              ; mm6 = p0..p3 - r-2 p0..p3
        psubusw     mm5, mm1              ; mm5 = r-2 p0..p3 - p0..p3
        paddusw     mm6, mm5              ; mm6 = abs(r0 p0..p3 - r-2 p0..p3)
        pcmpgtw     mm6, mm2
        por         mm7, mm6              ; accumulate thresholds

        movq        mm6, [rbx + 16]       ; kernel 1 taps
        movq        mm4, [rsi+rax]        ; mm4 = r-1 p0..p7
        punpcklbw   mm4, mm0              ; mm4 = r-1 p0..p3
        pmullw      mm6, mm4              ; mm4 *= kernel 1 modifiers.
        paddusw     mm3, mm6              ; mm3 += mm5

        ; thresholding
        movq        mm6, mm1              ; mm6 = r0 p0..p3
        psubusw     mm6, mm4              ; mm6 = p0..p3 - r-2 p0..p3
        psubusw     mm4, mm1              ; mm5 = r-1 p0..p3 - p0..p3
        paddusw     mm6, mm4              ; mm6 = abs(r0 p0..p3 - r-1 p0..p3)
        pcmpgtw     mm6, mm2
        por         mm7, mm6              ; accumulate thresholds


        paddusw     mm3, RD               ; mm3 += round value
        psraw       mm3, VP9_FILTER_SHIFT     ; mm3 /= 128

        pand        mm1, mm7              ; mm1 select vals > thresh from source
        pandn       mm7, mm3              ; mm7 select vals < thresh from blurred result
        paddusw     mm1, mm7              ; combination

        packuswb    mm1, mm0              ; pack to bytes

        movd        [rdi], mm1            ;
        neg         rax                   ; pitch is positive


        add         rsi, 4
        add         rdi, 4
        add         rdx, 4

        cmp         edx, dword ptr arg(5) ;cols
        jl          .nextcol
        ; done with the all cols, start the across filtering in place
        sub         rsi, rdx
        sub         rdi, rdx


        push        rax
        xor         rdx,    rdx
        mov         rax,    [rdi-4];

.acrossnextcol:
        pxor        mm7, mm7              ; mm7 = 00000000
        movq        mm6, [rbx + 32 ]      ;
        movq        mm4, [rdi+rdx]        ; mm4 = p0..p7
        movq        mm3, mm4              ; mm3 = p0..p7
        punpcklbw   mm3, mm0              ; mm3 = p0..p3
        movq        mm1, mm3              ; mm1 = p0..p3
        pmullw      mm3, mm6              ; mm3 *= kernel 2 modifiers

        movq        mm6, [rbx + 48]
        psrlq       mm4, 8                ; mm4 = p1..p7
        movq        mm5, mm4              ; mm5 = p1..p7
        punpcklbw   mm5, mm0              ; mm5 = p1..p4
        pmullw      mm6, mm5              ; mm6 *= p1..p4 * kernel 3 modifiers
        paddusw     mm3, mm6              ; mm3 += mm6

        ; thresholding
        movq        mm7, mm1              ; mm7 = p0..p3
        psubusw     mm7, mm5              ; mm7 = p0..p3 - p1..p4
        psubusw     mm5, mm1              ; mm5 = p1..p4 - p0..p3
        paddusw     mm7, mm5              ; mm7 = abs(p0..p3 - p1..p4)
        pcmpgtw     mm7, mm2

        movq        mm6, [rbx + 64 ]
        psrlq       mm4, 8                ; mm4 = p2..p7
        movq        mm5, mm4              ; mm5 = p2..p7
        punpcklbw   mm5, mm0              ; mm5 = p2..p5
        pmullw      mm6, mm5              ; mm5 *= kernel 4 modifiers
        paddusw     mm3, mm6              ; mm3 += mm5

        ; thresholding
        movq        mm6, mm1              ; mm6 = p0..p3
        psubusw     mm6, mm5              ; mm6 = p0..p3 - p1..p4
        psubusw     mm5, mm1              ; mm5 = p1..p4 - p0..p3
        paddusw     mm6, mm5              ; mm6 = abs(p0..p3 - p1..p4)
        pcmpgtw     mm6, mm2
        por         mm7, mm6              ; accumulate thresholds


        movq        mm6, [rbx ]
        movq        mm4, [rdi+rdx-2]      ; mm4 = p-2..p5
        movq        mm5, mm4              ; mm5 = p-2..p5
        punpcklbw   mm5, mm0              ; mm5 = p-2..p1
        pmullw      mm6, mm5              ; mm5 *= kernel 0 modifiers
        paddusw     mm3, mm6              ; mm3 += mm5

        ; thresholding
        movq        mm6, mm1              ; mm6 = p0..p3
        psubusw     mm6, mm5              ; mm6 = p0..p3 - p1..p4
        psubusw     mm5, mm1              ; mm5 = p1..p4 - p0..p3
        paddusw     mm6, mm5              ; mm6 = abs(p0..p3 - p1..p4)
        pcmpgtw     mm6, mm2
        por         mm7, mm6              ; accumulate thresholds

        movq        mm6, [rbx + 16]
        psrlq       mm4, 8                ; mm4 = p-1..p5
        punpcklbw   mm4, mm0              ; mm4 = p-1..p2
        pmullw      mm6, mm4              ; mm4 *= kernel 1 modifiers.
        paddusw     mm3, mm6              ; mm3 += mm5

        ; thresholding
        movq        mm6, mm1              ; mm6 = p0..p3
        psubusw     mm6, mm4              ; mm6 = p0..p3 - p1..p4
        psubusw     mm4, mm1              ; mm5 = p1..p4 - p0..p3
        paddusw     mm6, mm4              ; mm6 = abs(p0..p3 - p1..p4)
        pcmpgtw     mm6, mm2
        por         mm7, mm6              ; accumulate thresholds

        paddusw     mm3, RD               ; mm3 += round value
        psraw       mm3, VP9_FILTER_SHIFT     ; mm3 /= 128

        pand        mm1, mm7              ; mm1 select vals > thresh from source
        pandn       mm7, mm3              ; mm7 select vals < thresh from blurred result
        paddusw     mm1, mm7              ; combination

        packuswb    mm1, mm0              ; pack to bytes
        mov         DWORD PTR [rdi+rdx-4],  eax   ; store previous four bytes
        movd        eax,    mm1

        add         rdx, 4
        cmp         edx, dword ptr arg(5) ;cols
        jl          .acrossnextcol;

        mov         DWORD PTR [rdi+rdx-4],  eax
        pop         rax

        ; done with this rwo
        add         rsi,rax               ; next line
        movsxd      rax, dword ptr arg(3) ;dst_pixels_per_line ; destination pitch?
        add         rdi,rax               ; next destination
        movsxd      rax, dword ptr arg(2) ;src_pixels_per_line ; destination pitch?

        dec         rcx                   ; decrement count
        jnz         .nextrow               ; next row
        pop         rbx

    ; begin epilog
    pop rdi
    pop rsi
    RESTORE_GOT
    UNSHADOW_ARGS
    pop         rbp
    ret
%undef RD


;void vp9_mbpost_proc_down_mmx(unsigned char *dst,
;                             int pitch, int rows, int cols,int flimit)
extern sym(vp9_rv)
global sym(vp9_mbpost_proc_down_mmx) PRIVATE
sym(vp9_mbpost_proc_down_mmx):
    push        rbp
    mov         rbp, rsp
    SHADOW_ARGS_TO_STACK 5
    GET_GOT     rbx
    push        rsi
    push        rdi
    ; end prolog

    ALIGN_STACK 16, rax
    sub         rsp, 136

    ; unsigned char d[16][8] at [rsp]
    ; create flimit2 at [rsp+128]
    mov         eax, dword ptr arg(4) ;flimit
    mov         [rsp+128], eax
    mov         [rsp+128+4], eax
%define flimit2 [rsp+128]

%if ABI_IS_32BIT=0
    lea         r8,       [GLOBAL(sym(vp9_rv))]
%endif

    ;rows +=8;
    add         dword ptr arg(2), 8

    ;for(c=0; c<cols; c+=4)
.loop_col:
            mov         rsi,        arg(0)  ;s
            pxor        mm0,        mm0     ;

            movsxd      rax,        dword ptr arg(1) ;pitch       ;
            neg         rax                                     ; rax = -pitch

            lea         rsi,        [rsi + rax*8];              ; rdi = s[-pitch*8]
            neg         rax


            pxor        mm5,        mm5
            pxor        mm6,        mm6     ;

            pxor        mm7,        mm7     ;
            mov         rdi,        rsi

            mov         rcx,        15          ;

.loop_initvar:
            movd        mm1,        DWORD PTR [rdi];
            punpcklbw   mm1,        mm0     ;

            paddw       mm5,        mm1     ;
            pmullw      mm1,        mm1     ;

            movq        mm2,        mm1     ;
            punpcklwd   mm1,        mm0     ;

            punpckhwd   mm2,        mm0     ;
            paddd       mm6,        mm1     ;

            paddd       mm7,        mm2     ;
            lea         rdi,        [rdi+rax]   ;

            dec         rcx
            jne         .loop_initvar
            ;save the var and sum
            xor         rdx,        rdx
.loop_row:
            movd        mm1,        DWORD PTR [rsi]     ; [s-pitch*8]
            movd        mm2,        DWORD PTR [rdi]     ; [s+pitch*7]

            punpcklbw   mm1,        mm0
            punpcklbw   mm2,        mm0

            paddw       mm5,        mm2
            psubw       mm5,        mm1

            pmullw      mm2,        mm2
            movq        mm4,        mm2

            punpcklwd   mm2,        mm0
            punpckhwd   mm4,        mm0

            paddd       mm6,        mm2
            paddd       mm7,        mm4

            pmullw      mm1,        mm1
            movq        mm2,        mm1

            punpcklwd   mm1,        mm0
            psubd       mm6,        mm1

            punpckhwd   mm2,        mm0
            psubd       mm7,        mm2


            movq        mm3,        mm6
            pslld       mm3,        4

            psubd       mm3,        mm6
            movq        mm1,        mm5

            movq        mm4,        mm5
            pmullw      mm1,        mm1

            pmulhw      mm4,        mm4
            movq        mm2,        mm1

            punpcklwd   mm1,        mm4
            punpckhwd   mm2,        mm4

            movq        mm4,        mm7
            pslld       mm4,        4

            psubd       mm4,        mm7

            psubd       mm3,        mm1
            psubd       mm4,        mm2

            psubd       mm3,        flimit2
            psubd       mm4,        flimit2

            psrad       mm3,        31
            psrad       mm4,        31

            packssdw    mm3,        mm4
            packsswb    mm3,        mm0

            movd        mm1,        DWORD PTR [rsi+rax*8]

            movq        mm2,        mm1
            punpcklbw   mm1,        mm0

            paddw       mm1,        mm5
            mov         rcx,        rdx

            and         rcx,        127
%if ABI_IS_32BIT=1 && CONFIG_PIC=1
            push        rax
            lea         rax,        [GLOBAL(sym(vp9_rv))]
            movq        mm4,        [rax + rcx*2] ;vp9_rv[rcx*2]
            pop         rax
%elif ABI_IS_32BIT=0
            movq        mm4,        [r8 + rcx*2] ;vp9_rv[rcx*2]
%else
            movq        mm4,        [sym(vp9_rv) + rcx*2]
%endif
            paddw       mm1,        mm4
            ;paddw     xmm1,       eight8s
            psraw       mm1,        4

            packuswb    mm1,        mm0
            pand        mm1,        mm3

            pandn       mm3,        mm2
            por         mm1,        mm3

            and         rcx,        15
            movd        DWORD PTR   [rsp+rcx*4], mm1 ;d[rcx*4]

            mov         rcx,        rdx
            sub         rcx,        8

            and         rcx,        15
            movd        mm1,        DWORD PTR [rsp+rcx*4] ;d[rcx*4]

            movd        [rsi],      mm1
            lea         rsi,        [rsi+rax]

            lea         rdi,        [rdi+rax]
            add         rdx,        1

            cmp         edx,        dword arg(2) ;rows
            jl          .loop_row


        add         dword arg(0), 4 ; s += 4
        sub         dword arg(3), 4 ; cols -= 4
        cmp         dword arg(3), 0
        jg          .loop_col

    add         rsp, 136
    pop         rsp

    ; begin epilog
    pop rdi
    pop rsi
    RESTORE_GOT
    UNSHADOW_ARGS
    pop         rbp
    ret
%undef flimit2


;void vp9_plane_add_noise_mmx (unsigned char *start, unsigned char *noise,
;                            unsigned char blackclamp[16],
;                            unsigned char whiteclamp[16],
;                            unsigned char bothclamp[16],
;                            unsigned int width, unsigned int height, int pitch)
extern sym(rand)
global sym(vp9_plane_add_noise_mmx) PRIVATE
sym(vp9_plane_add_noise_mmx):
    push        rbp
    mov         rbp, rsp
    SHADOW_ARGS_TO_STACK 8
    GET_GOT     rbx
    push        rsi
    push        rdi
    ; end prolog

.addnoise_loop:
    call sym(rand) WRT_PLT
    mov     rcx, arg(1) ;noise
    and     rax, 0xff
    add     rcx, rax

    ; we rely on the fact that the clamping vectors are stored contiguously
    ; in black/white/both order. Note that we have to reload this here because
    ; rdx could be trashed by rand()
    mov     rdx, arg(2) ; blackclamp


            mov     rdi, rcx
            movsxd  rcx, dword arg(5) ;[Width]
            mov     rsi, arg(0) ;Pos
            xor         rax,rax

.addnoise_nextset:
            movq        mm1,[rsi+rax]         ; get the source

            psubusb     mm1, [rdx]    ;blackclamp        ; clamp both sides so we don't outrange adding noise
            paddusb     mm1, [rdx+32] ;bothclamp
            psubusb     mm1, [rdx+16] ;whiteclamp

            movq        mm2,[rdi+rax]         ; get the noise for this line
            paddb       mm1,mm2              ; add it in
            movq        [rsi+rax],mm1         ; store the result

            add         rax,8                 ; move to the next line

            cmp         rax, rcx
            jl          .addnoise_nextset

    movsxd  rax, dword arg(7) ; Pitch
    add     arg(0), rax ; Start += Pitch
    sub     dword arg(6), 1   ; Height -= 1
    jg      .addnoise_loop

    ; begin epilog
    pop rdi
    pop rsi
    RESTORE_GOT
    UNSHADOW_ARGS
    pop         rbp
    ret


SECTION_RODATA
align 16
Blur:
    times 16 dw 16
    times  8 dw 64
    times 16 dw 16
    times  8 dw  0

rd:
    times 4 dw 0x40