Statistics
| Branch: | Revision:

ffmpeg / libavcodec / x86 / h264dsp_mmx.c @ 0cc8a5d0

History | View | Annotate | Download (16.9 KB)

1
/*
2
 * Copyright (c) 2004-2005 Michael Niedermayer, Loren Merritt
3
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20

    
21
#include "libavutil/cpu.h"
22
#include "libavutil/x86_cpu.h"
23
#include "libavcodec/h264dsp.h"
24
#include "dsputil_mmx.h"
25

    
26
DECLARE_ALIGNED(8, static const uint64_t, ff_pb_3_1  ) = 0x0103010301030103ULL;
27

    
28
/***********************************/
29
/* IDCT */
30

    
31
void ff_h264_idct_add_mmx     (uint8_t *dst, int16_t *block, int stride);
32
void ff_h264_idct8_add_mmx    (uint8_t *dst, int16_t *block, int stride);
33
void ff_h264_idct8_add_sse2   (uint8_t *dst, int16_t *block, int stride);
34
void ff_h264_idct_dc_add_mmx2 (uint8_t *dst, int16_t *block, int stride);
35
void ff_h264_idct8_dc_add_mmx2(uint8_t *dst, int16_t *block, int stride);
36

    
37
void ff_h264_idct_add16_mmx      (uint8_t *dst, const int *block_offset,
38
                                  DCTELEM *block, int stride, const uint8_t nnzc[6*8]);
39
void ff_h264_idct8_add4_mmx      (uint8_t *dst, const int *block_offset,
40
                                  DCTELEM *block, int stride, const uint8_t nnzc[6*8]);
41
void ff_h264_idct_add16_mmx2     (uint8_t *dst, const int *block_offset,
42
                                  DCTELEM *block, int stride, const uint8_t nnzc[6*8]);
43
void ff_h264_idct_add16intra_mmx (uint8_t *dst, const int *block_offset,
44
                                  DCTELEM *block, int stride, const uint8_t nnzc[6*8]);
45
void ff_h264_idct_add16intra_mmx2(uint8_t *dst, const int *block_offset,
46
                                  DCTELEM *block, int stride, const uint8_t nnzc[6*8]);
47
void ff_h264_idct8_add4_mmx2     (uint8_t *dst, const int *block_offset,
48
                                  DCTELEM *block, int stride, const uint8_t nnzc[6*8]);
49
void ff_h264_idct8_add4_sse2     (uint8_t *dst, const int *block_offset,
50
                                  DCTELEM *block, int stride, const uint8_t nnzc[6*8]);
51
void ff_h264_idct_add8_mmx       (uint8_t **dest, const int *block_offset,
52
                                  DCTELEM *block, int stride, const uint8_t nnzc[6*8]);
53
void ff_h264_idct_add8_mmx2      (uint8_t **dest, const int *block_offset,
54
                                  DCTELEM *block, int stride, const uint8_t nnzc[6*8]);
55

    
56
void ff_h264_idct_add16_sse2     (uint8_t *dst, const int *block_offset, DCTELEM *block,
57
                                  int stride, const uint8_t nnzc[6*8]);
58
void ff_h264_idct_add16intra_sse2(uint8_t *dst, const int *block_offset, DCTELEM *block,
59
                                  int stride, const uint8_t nnzc[6*8]);
60
void ff_h264_idct_add8_sse2      (uint8_t **dest, const int *block_offset, DCTELEM *block,
61
                                  int stride, const uint8_t nnzc[6*8]);
62

    
63
/***********************************/
64
/* deblocking */
65

    
66
static av_always_inline void h264_loop_filter_strength_iteration_mmx2(int16_t bS[2][4][4], uint8_t nnz[40],
67
                                                                      int8_t ref[2][40],   int16_t mv[2][40][2],
68
                                                                      int bidir,   int edges, int step,
69
                                                                      int mask_mv, int dir, const int d_idx,
70
                                                                      const uint64_t mask_dir)
71
{
72
        int b_idx, edge;
73
        for( b_idx=12, edge=0; edge<edges; edge+=step, b_idx+=8*step ) {
74
            if (!mask_dir)
75
            __asm__ volatile(
76
                    "pxor %%mm0, %%mm0 \n\t"
77
                    ::
78
            );
79
            if(!(mask_mv & edge)) {
80
                if(bidir) {
81
                    __asm__ volatile(
82
                        "movd         %a2(%0), %%mm2 \n"
83
                        "punpckldq    %a3(%0), %%mm2 \n" // { ref0[bn], ref1[bn] }
84
                        "pshufw $0x44,   (%0), %%mm0 \n" // { ref0[b], ref0[b] }
85
                        "pshufw $0x44, 40(%0), %%mm1 \n" // { ref1[b], ref1[b] }
86
                        "pshufw $0x4E, %%mm2, %%mm3 \n"
87
                        "psubb         %%mm2, %%mm0 \n" // { ref0[b]!=ref0[bn], ref0[b]!=ref1[bn] }
88
                        "psubb         %%mm3, %%mm1 \n" // { ref1[b]!=ref1[bn], ref1[b]!=ref0[bn] }
89

    
90
                        "por           %%mm1, %%mm0 \n"
91
                        "movq        %a4(%1), %%mm1 \n"
92
                        "movq        %a5(%1), %%mm2 \n"
93
                        "movq          %%mm1, %%mm3 \n"
94
                        "movq          %%mm2, %%mm4 \n"
95
                        "psubw          (%1), %%mm1 \n"
96
                        "psubw         8(%1), %%mm2 \n"
97
                        "psubw       160(%1), %%mm3 \n"
98
                        "psubw       168(%1), %%mm4 \n"
99
                        "packsswb      %%mm2, %%mm1 \n"
100
                        "packsswb      %%mm4, %%mm3 \n"
101
                        "paddb         %%mm6, %%mm1 \n"
102
                        "paddb         %%mm6, %%mm3 \n"
103
                        "psubusb       %%mm5, %%mm1 \n" // abs(mv[b] - mv[bn]) >= limit
104
                        "psubusb       %%mm5, %%mm3 \n"
105
                        "packsswb      %%mm3, %%mm1 \n"
106

    
107
                        "por           %%mm1, %%mm0 \n"
108
                        "movq        %a6(%1), %%mm1 \n"
109
                        "movq        %a7(%1), %%mm2 \n"
110
                        "movq          %%mm1, %%mm3 \n"
111
                        "movq          %%mm2, %%mm4 \n"
112
                        "psubw          (%1), %%mm1 \n"
113
                        "psubw         8(%1), %%mm2 \n"
114
                        "psubw       160(%1), %%mm3 \n"
115
                        "psubw       168(%1), %%mm4 \n"
116
                        "packsswb      %%mm2, %%mm1 \n"
117
                        "packsswb      %%mm4, %%mm3 \n"
118
                        "paddb         %%mm6, %%mm1 \n"
119
                        "paddb         %%mm6, %%mm3 \n"
120
                        "psubusb       %%mm5, %%mm1 \n" // abs(mv[b] - mv[bn]) >= limit
121
                        "psubusb       %%mm5, %%mm3 \n"
122
                        "packsswb      %%mm3, %%mm1 \n"
123

    
124
                        "pshufw $0x4E, %%mm1, %%mm1 \n"
125
                        "por           %%mm1, %%mm0 \n"
126
                        "pshufw $0x4E, %%mm0, %%mm1 \n"
127
                        "pminub        %%mm1, %%mm0 \n"
128
                        ::"r"(ref[0]+b_idx),
129
                          "r"(mv[0]+b_idx),
130
                          "i"(d_idx),
131
                          "i"(d_idx+40),
132
                          "i"(d_idx*4),
133
                          "i"(d_idx*4+8),
134
                          "i"(d_idx*4+160),
135
                          "i"(d_idx*4+168)
136
                    );
137
                } else {
138
                    __asm__ volatile(
139
                        "movd        (%1), %%mm0 \n"
140
                        "psubb    %a0(%1), %%mm0 \n" // ref[b] != ref[bn]
141
                        "movq        (%2), %%mm1 \n"
142
                        "movq       8(%2), %%mm2 \n"
143
                        "psubw    %a3(%2), %%mm1 \n"
144
                        "psubw    %a4(%2), %%mm2 \n"
145
                        "packsswb   %%mm2, %%mm1 \n"
146
                        "paddb      %%mm6, %%mm1 \n"
147
                        "psubusb    %%mm5, %%mm1 \n" // abs(mv[b] - mv[bn]) >= limit
148
                        "packsswb   %%mm1, %%mm1 \n"
149
                        "por        %%mm1, %%mm0 \n"
150
                        ::"i"(d_idx),
151
                          "r"(ref[0]+b_idx),
152
                          "r"(mv[0]+b_idx),
153
                          "i"(d_idx*4),
154
                          "i"(d_idx*4+8)
155
                    );
156
                }
157
            }
158
            __asm__ volatile(
159
                "movd   (%0), %%mm1 \n"
160
                "por %a1(%0), %%mm1 \n" // nnz[b] || nnz[bn]
161
                ::"r"(nnz+b_idx),
162
                  "i"(d_idx)
163
            );
164
            __asm__ volatile(
165
                "pminub    %%mm7, %%mm1 \n"
166
                "pminub    %%mm7, %%mm0 \n"
167
                "psllw        $1, %%mm1 \n"
168
                "pxor      %%mm2, %%mm2 \n"
169
                "pmaxub    %%mm0, %%mm1 \n"
170
                "punpcklbw %%mm2, %%mm1 \n"
171
                "movq      %%mm1, %0    \n"
172
                :"=m"(*bS[dir][edge])
173
                ::"memory"
174
            );
175
        }
176
}
177

    
178
static void h264_loop_filter_strength_mmx2( int16_t bS[2][4][4], uint8_t nnz[40], int8_t ref[2][40], int16_t mv[2][40][2],
179
                                            int bidir, int edges, int step, int mask_mv0, int mask_mv1, int field ) {
180
    __asm__ volatile(
181
        "movq %0, %%mm7 \n"
182
        "movq %1, %%mm6 \n"
183
        ::"m"(ff_pb_1), "m"(ff_pb_3)
184
    );
185
    if(field)
186
        __asm__ volatile(
187
            "movq %0, %%mm6 \n"
188
            ::"m"(ff_pb_3_1)
189
        );
190
    __asm__ volatile(
191
        "movq  %%mm6, %%mm5 \n"
192
        "paddb %%mm5, %%mm5 \n"
193
    :);
194

    
195
    // could do a special case for dir==0 && edges==1, but it only reduces the
196
    // average filter time by 1.2%
197
    h264_loop_filter_strength_iteration_mmx2(bS, nnz, ref, mv, bidir, edges, step, mask_mv1, 1, -8,  0);
198
    h264_loop_filter_strength_iteration_mmx2(bS, nnz, ref, mv, bidir,     4,    1, mask_mv0, 0, -1, -1);
199

    
200
    __asm__ volatile(
201
        "movq   (%0), %%mm0 \n\t"
202
        "movq  8(%0), %%mm1 \n\t"
203
        "movq 16(%0), %%mm2 \n\t"
204
        "movq 24(%0), %%mm3 \n\t"
205
        TRANSPOSE4(%%mm0, %%mm1, %%mm2, %%mm3, %%mm4)
206
        "movq %%mm0,   (%0) \n\t"
207
        "movq %%mm3,  8(%0) \n\t"
208
        "movq %%mm4, 16(%0) \n\t"
209
        "movq %%mm2, 24(%0) \n\t"
210
        ::"r"(bS[0])
211
        :"memory"
212
    );
213
}
214

    
215
#define LF_FUNC(DIR, TYPE, OPT) \
216
void ff_x264_deblock_ ## DIR ## _ ## TYPE ## _ ## OPT (uint8_t *pix, int stride, \
217
                                               int alpha, int beta, int8_t *tc0);
218
#define LF_IFUNC(DIR, TYPE, OPT) \
219
void ff_x264_deblock_ ## DIR ## _ ## TYPE ## _ ## OPT (uint8_t *pix, int stride, \
220
                                               int alpha, int beta);
221

    
222
LF_FUNC (h,  chroma,       mmxext)
223
LF_IFUNC(h,  chroma_intra, mmxext)
224
LF_FUNC (v,  chroma,       mmxext)
225
LF_IFUNC(v,  chroma_intra, mmxext)
226

    
227
LF_FUNC (h,  luma,         mmxext)
228
LF_IFUNC(h,  luma_intra,   mmxext)
229
#if HAVE_YASM && ARCH_X86_32
230
LF_FUNC (v8, luma,         mmxext)
231
static void ff_x264_deblock_v_luma_mmxext(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
232
{
233
    if((tc0[0] & tc0[1]) >= 0)
234
        ff_x264_deblock_v8_luma_mmxext(pix+0, stride, alpha, beta, tc0);
235
    if((tc0[2] & tc0[3]) >= 0)
236
        ff_x264_deblock_v8_luma_mmxext(pix+8, stride, alpha, beta, tc0+2);
237
}
238
LF_IFUNC(v8, luma_intra,   mmxext)
239
static void ff_x264_deblock_v_luma_intra_mmxext(uint8_t *pix, int stride, int alpha, int beta)
240
{
241
    ff_x264_deblock_v8_luma_intra_mmxext(pix+0, stride, alpha, beta);
242
    ff_x264_deblock_v8_luma_intra_mmxext(pix+8, stride, alpha, beta);
243
}
244
#endif
245

    
246
LF_FUNC (h,  luma,         sse2)
247
LF_IFUNC(h,  luma_intra,   sse2)
248
LF_FUNC (v,  luma,         sse2)
249
LF_IFUNC(v,  luma_intra,   sse2)
250

    
251
/***********************************/
252
/* weighted prediction */
253

    
254
#define H264_WEIGHT(W, H, OPT) \
255
void ff_h264_weight_ ## W ## x ## H ## _ ## OPT(uint8_t *dst, \
256
    int stride, int log2_denom, int weight, int offset);
257

    
258
#define H264_BIWEIGHT(W, H, OPT) \
259
void ff_h264_biweight_ ## W ## x ## H ## _ ## OPT(uint8_t *dst, \
260
    uint8_t *src, int stride, int log2_denom, int weightd, \
261
    int weights, int offset);
262

    
263
#define H264_BIWEIGHT_MMX(W,H) \
264
H264_WEIGHT  (W, H, mmx2) \
265
H264_BIWEIGHT(W, H, mmx2)
266

    
267
#define H264_BIWEIGHT_MMX_SSE(W,H) \
268
H264_BIWEIGHT_MMX(W, H) \
269
H264_WEIGHT      (W, H, sse2) \
270
H264_BIWEIGHT    (W, H, sse2) \
271
H264_BIWEIGHT    (W, H, ssse3)
272

    
273
H264_BIWEIGHT_MMX_SSE(16, 16)
274
H264_BIWEIGHT_MMX_SSE(16,  8)
275
H264_BIWEIGHT_MMX_SSE( 8, 16)
276
H264_BIWEIGHT_MMX_SSE( 8,  8)
277
H264_BIWEIGHT_MMX_SSE( 8,  4)
278
H264_BIWEIGHT_MMX    ( 4,  8)
279
H264_BIWEIGHT_MMX    ( 4,  4)
280
H264_BIWEIGHT_MMX    ( 4,  2)
281

    
282
void ff_h264dsp_init_x86(H264DSPContext *c)
283
{
284
    int mm_flags = av_get_cpu_flags();
285

    
286
    if (mm_flags & AV_CPU_FLAG_MMX2) {
287
        c->h264_loop_filter_strength= h264_loop_filter_strength_mmx2;
288
    }
289
#if HAVE_YASM
290
    if (mm_flags & AV_CPU_FLAG_MMX) {
291
        c->h264_idct_dc_add=
292
        c->h264_idct_add= ff_h264_idct_add_mmx;
293
        c->h264_idct8_dc_add=
294
        c->h264_idct8_add= ff_h264_idct8_add_mmx;
295

    
296
        c->h264_idct_add16     = ff_h264_idct_add16_mmx;
297
        c->h264_idct8_add4     = ff_h264_idct8_add4_mmx;
298
        c->h264_idct_add8      = ff_h264_idct_add8_mmx;
299
        c->h264_idct_add16intra= ff_h264_idct_add16intra_mmx;
300

    
301
        if (mm_flags & AV_CPU_FLAG_MMX2) {
302
            c->h264_idct_dc_add= ff_h264_idct_dc_add_mmx2;
303
            c->h264_idct8_dc_add= ff_h264_idct8_dc_add_mmx2;
304
            c->h264_idct_add16     = ff_h264_idct_add16_mmx2;
305
            c->h264_idct8_add4     = ff_h264_idct8_add4_mmx2;
306
            c->h264_idct_add8      = ff_h264_idct_add8_mmx2;
307
            c->h264_idct_add16intra= ff_h264_idct_add16intra_mmx2;
308

    
309
            c->h264_v_loop_filter_chroma= ff_x264_deblock_v_chroma_mmxext;
310
            c->h264_h_loop_filter_chroma= ff_x264_deblock_h_chroma_mmxext;
311
            c->h264_v_loop_filter_chroma_intra= ff_x264_deblock_v_chroma_intra_mmxext;
312
            c->h264_h_loop_filter_chroma_intra= ff_x264_deblock_h_chroma_intra_mmxext;
313
#if ARCH_X86_32
314
            c->h264_v_loop_filter_luma= ff_x264_deblock_v_luma_mmxext;
315
            c->h264_h_loop_filter_luma= ff_x264_deblock_h_luma_mmxext;
316
            c->h264_v_loop_filter_luma_intra = ff_x264_deblock_v_luma_intra_mmxext;
317
            c->h264_h_loop_filter_luma_intra = ff_x264_deblock_h_luma_intra_mmxext;
318
#endif
319
            c->weight_h264_pixels_tab[0]= ff_h264_weight_16x16_mmx2;
320
            c->weight_h264_pixels_tab[1]= ff_h264_weight_16x8_mmx2;
321
            c->weight_h264_pixels_tab[2]= ff_h264_weight_8x16_mmx2;
322
            c->weight_h264_pixels_tab[3]= ff_h264_weight_8x8_mmx2;
323
            c->weight_h264_pixels_tab[4]= ff_h264_weight_8x4_mmx2;
324
            c->weight_h264_pixels_tab[5]= ff_h264_weight_4x8_mmx2;
325
            c->weight_h264_pixels_tab[6]= ff_h264_weight_4x4_mmx2;
326
            c->weight_h264_pixels_tab[7]= ff_h264_weight_4x2_mmx2;
327

    
328
            c->biweight_h264_pixels_tab[0]= ff_h264_biweight_16x16_mmx2;
329
            c->biweight_h264_pixels_tab[1]= ff_h264_biweight_16x8_mmx2;
330
            c->biweight_h264_pixels_tab[2]= ff_h264_biweight_8x16_mmx2;
331
            c->biweight_h264_pixels_tab[3]= ff_h264_biweight_8x8_mmx2;
332
            c->biweight_h264_pixels_tab[4]= ff_h264_biweight_8x4_mmx2;
333
            c->biweight_h264_pixels_tab[5]= ff_h264_biweight_4x8_mmx2;
334
            c->biweight_h264_pixels_tab[6]= ff_h264_biweight_4x4_mmx2;
335
            c->biweight_h264_pixels_tab[7]= ff_h264_biweight_4x2_mmx2;
336

    
337
            if (mm_flags&AV_CPU_FLAG_SSE2) {
338
                c->h264_idct8_add = ff_h264_idct8_add_sse2;
339
                c->h264_idct8_add4= ff_h264_idct8_add4_sse2;
340

    
341
                c->weight_h264_pixels_tab[0]= ff_h264_weight_16x16_sse2;
342
                c->weight_h264_pixels_tab[1]= ff_h264_weight_16x8_sse2;
343
                c->weight_h264_pixels_tab[2]= ff_h264_weight_8x16_sse2;
344
                c->weight_h264_pixels_tab[3]= ff_h264_weight_8x8_sse2;
345
                c->weight_h264_pixels_tab[4]= ff_h264_weight_8x4_sse2;
346

    
347
                c->biweight_h264_pixels_tab[0]= ff_h264_biweight_16x16_sse2;
348
                c->biweight_h264_pixels_tab[1]= ff_h264_biweight_16x8_sse2;
349
                c->biweight_h264_pixels_tab[2]= ff_h264_biweight_8x16_sse2;
350
                c->biweight_h264_pixels_tab[3]= ff_h264_biweight_8x8_sse2;
351
                c->biweight_h264_pixels_tab[4]= ff_h264_biweight_8x4_sse2;
352

    
353
#if HAVE_ALIGNED_STACK
354
                c->h264_v_loop_filter_luma = ff_x264_deblock_v_luma_sse2;
355
                c->h264_h_loop_filter_luma = ff_x264_deblock_h_luma_sse2;
356
                c->h264_v_loop_filter_luma_intra = ff_x264_deblock_v_luma_intra_sse2;
357
                c->h264_h_loop_filter_luma_intra = ff_x264_deblock_h_luma_intra_sse2;
358
#endif
359

    
360
                c->h264_idct_add16 = ff_h264_idct_add16_sse2;
361
                c->h264_idct_add8  = ff_h264_idct_add8_sse2;
362
                c->h264_idct_add16intra = ff_h264_idct_add16intra_sse2;
363
            }
364
            if (mm_flags&AV_CPU_FLAG_SSSE3) {
365
                c->biweight_h264_pixels_tab[0]= ff_h264_biweight_16x16_ssse3;
366
                c->biweight_h264_pixels_tab[1]= ff_h264_biweight_16x8_ssse3;
367
                c->biweight_h264_pixels_tab[2]= ff_h264_biweight_8x16_ssse3;
368
                c->biweight_h264_pixels_tab[3]= ff_h264_biweight_8x8_ssse3;
369
                c->biweight_h264_pixels_tab[4]= ff_h264_biweight_8x4_ssse3;
370
            }
371
        }
372
    }
373
#endif
374
}