]> git.sesse.net Git - ffmpeg/blob - libavcodec/x86/h264dsp_mmx.c
Unloop the outer loop in h264_loop_filter_strength_mmx2(), which allows
[ffmpeg] / libavcodec / x86 / h264dsp_mmx.c
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)
70 {
71         const x86_reg d_idx = dir ? -8 : -1;
72         DECLARE_ALIGNED(8, const uint64_t, mask_dir) = dir ? 0 : 0xffffffffffffffffULL;
73         int b_idx, edge;
74         for( b_idx=12, edge=0; edge<edges; edge+=step, b_idx+=8*step ) {
75             __asm__ volatile(
76                 "pand %0, %%mm0 \n\t"
77                 ::"m"(mask_dir)
78             );
79             if(!(mask_mv & edge)) {
80                 if(bidir) {
81                     __asm__ volatile(
82                         "movd         (%1,%0), %%mm2 \n"
83                         "punpckldq  40(%1,%0), %%mm2 \n" // { ref0[bn], ref1[bn] }
84                         "pshufw $0x44,   (%1), %%mm0 \n" // { ref0[b], ref0[b] }
85                         "pshufw $0x44, 40(%1), %%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                         "1: \n"
90                         "por           %%mm1, %%mm0 \n"
91                         "movq      (%2,%0,4), %%mm1 \n"
92                         "movq     8(%2,%0,4), %%mm2 \n"
93                         "movq          %%mm1, %%mm3 \n"
94                         "movq          %%mm2, %%mm4 \n"
95                         "psubw          (%2), %%mm1 \n"
96                         "psubw         8(%2), %%mm2 \n"
97                         "psubw       160(%2), %%mm3 \n"
98                         "psubw       168(%2), %%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                         "add $40, %0 \n"
107                         "cmp $40, %0 \n"
108                         "jl 1b \n"
109                         "sub $80, %0 \n"
110                         "pshufw $0x4E, %%mm1, %%mm1 \n"
111                         "por           %%mm1, %%mm0 \n"
112                         "pshufw $0x4E, %%mm0, %%mm1 \n"
113                         "pminub        %%mm1, %%mm0 \n"
114                         ::"r"(d_idx),
115                           "r"(ref[0]+b_idx),
116                           "r"(mv[0]+b_idx)
117                     );
118                 } else {
119                     __asm__ volatile(
120                         "movd        (%1), %%mm0 \n"
121                         "psubb    (%1,%0), %%mm0 \n" // ref[b] != ref[bn]
122                         "movq        (%2), %%mm1 \n"
123                         "movq       8(%2), %%mm2 \n"
124                         "psubw  (%2,%0,4), %%mm1 \n"
125                         "psubw 8(%2,%0,4), %%mm2 \n"
126                         "packsswb   %%mm2, %%mm1 \n"
127                         "paddb      %%mm6, %%mm1 \n"
128                         "psubusb    %%mm5, %%mm1 \n" // abs(mv[b] - mv[bn]) >= limit
129                         "packsswb   %%mm1, %%mm1 \n"
130                         "por        %%mm1, %%mm0 \n"
131                         ::"r"(d_idx),
132                           "r"(ref[0]+b_idx),
133                           "r"(mv[0]+b_idx)
134                     );
135                 }
136             }
137             __asm__ volatile(
138                 "movd %0, %%mm1 \n"
139                 "por  %1, %%mm1 \n" // nnz[b] || nnz[bn]
140                 ::"m"(nnz[b_idx]),
141                   "m"(nnz[b_idx+d_idx])
142             );
143             __asm__ volatile(
144                 "pminub    %%mm7, %%mm1 \n"
145                 "pminub    %%mm7, %%mm0 \n"
146                 "psllw        $1, %%mm1 \n"
147                 "pxor      %%mm2, %%mm2 \n"
148                 "pmaxub    %%mm0, %%mm1 \n"
149                 "punpcklbw %%mm2, %%mm1 \n"
150                 "movq      %%mm1, %0    \n"
151                 :"=m"(*bS[dir][edge])
152                 ::"memory"
153             );
154         }
155 }
156
157 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],
158                                             int bidir, int edges, int step, int mask_mv0, int mask_mv1, int field ) {
159     __asm__ volatile(
160         "movq %0, %%mm7 \n"
161         "movq %1, %%mm6 \n"
162         ::"m"(ff_pb_1), "m"(ff_pb_3)
163     );
164     if(field)
165         __asm__ volatile(
166             "movq %0, %%mm6 \n"
167             ::"m"(ff_pb_3_1)
168         );
169     __asm__ volatile(
170         "movq  %%mm6, %%mm5 \n"
171         "paddb %%mm5, %%mm5 \n"
172     :);
173
174     // could do a special case for dir==0 && edges==1, but it only reduces the
175     // average filter time by 1.2%
176     h264_loop_filter_strength_iteration_mmx2(bS, nnz, ref, mv, bidir, edges, step, mask_mv1, 1);
177     h264_loop_filter_strength_iteration_mmx2(bS, nnz, ref, mv, bidir,     4,    1, mask_mv0, 0);
178
179     __asm__ volatile(
180         "movq   (%0), %%mm0 \n\t"
181         "movq  8(%0), %%mm1 \n\t"
182         "movq 16(%0), %%mm2 \n\t"
183         "movq 24(%0), %%mm3 \n\t"
184         TRANSPOSE4(%%mm0, %%mm1, %%mm2, %%mm3, %%mm4)
185         "movq %%mm0,   (%0) \n\t"
186         "movq %%mm3,  8(%0) \n\t"
187         "movq %%mm4, 16(%0) \n\t"
188         "movq %%mm2, 24(%0) \n\t"
189         ::"r"(bS[0])
190         :"memory"
191     );
192 }
193
194 #define LF_FUNC(DIR, TYPE, OPT) \
195 void ff_x264_deblock_ ## DIR ## _ ## TYPE ## _ ## OPT (uint8_t *pix, int stride, \
196                                                int alpha, int beta, int8_t *tc0);
197 #define LF_IFUNC(DIR, TYPE, OPT) \
198 void ff_x264_deblock_ ## DIR ## _ ## TYPE ## _ ## OPT (uint8_t *pix, int stride, \
199                                                int alpha, int beta);
200
201 LF_FUNC (h,  chroma,       mmxext)
202 LF_IFUNC(h,  chroma_intra, mmxext)
203 LF_FUNC (v,  chroma,       mmxext)
204 LF_IFUNC(v,  chroma_intra, mmxext)
205
206 LF_FUNC (h,  luma,         mmxext)
207 LF_IFUNC(h,  luma_intra,   mmxext)
208 #if HAVE_YASM && ARCH_X86_32
209 LF_FUNC (v8, luma,         mmxext)
210 static void ff_x264_deblock_v_luma_mmxext(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
211 {
212     if((tc0[0] & tc0[1]) >= 0)
213         ff_x264_deblock_v8_luma_mmxext(pix+0, stride, alpha, beta, tc0);
214     if((tc0[2] & tc0[3]) >= 0)
215         ff_x264_deblock_v8_luma_mmxext(pix+8, stride, alpha, beta, tc0+2);
216 }
217 LF_IFUNC(v8, luma_intra,   mmxext)
218 static void ff_x264_deblock_v_luma_intra_mmxext(uint8_t *pix, int stride, int alpha, int beta)
219 {
220     ff_x264_deblock_v8_luma_intra_mmxext(pix+0, stride, alpha, beta);
221     ff_x264_deblock_v8_luma_intra_mmxext(pix+8, stride, alpha, beta);
222 }
223 #endif
224
225 LF_FUNC (h,  luma,         sse2)
226 LF_IFUNC(h,  luma_intra,   sse2)
227 LF_FUNC (v,  luma,         sse2)
228 LF_IFUNC(v,  luma_intra,   sse2)
229
230 /***********************************/
231 /* weighted prediction */
232
233 #define H264_WEIGHT(W, H, OPT) \
234 void ff_h264_weight_ ## W ## x ## H ## _ ## OPT(uint8_t *dst, \
235     int stride, int log2_denom, int weight, int offset);
236
237 #define H264_BIWEIGHT(W, H, OPT) \
238 void ff_h264_biweight_ ## W ## x ## H ## _ ## OPT(uint8_t *dst, \
239     uint8_t *src, int stride, int log2_denom, int weightd, \
240     int weights, int offset);
241
242 #define H264_BIWEIGHT_MMX(W,H) \
243 H264_WEIGHT  (W, H, mmx2) \
244 H264_BIWEIGHT(W, H, mmx2)
245
246 #define H264_BIWEIGHT_MMX_SSE(W,H) \
247 H264_BIWEIGHT_MMX(W, H) \
248 H264_WEIGHT      (W, H, sse2) \
249 H264_BIWEIGHT    (W, H, sse2) \
250 H264_BIWEIGHT    (W, H, ssse3)
251
252 H264_BIWEIGHT_MMX_SSE(16, 16)
253 H264_BIWEIGHT_MMX_SSE(16,  8)
254 H264_BIWEIGHT_MMX_SSE( 8, 16)
255 H264_BIWEIGHT_MMX_SSE( 8,  8)
256 H264_BIWEIGHT_MMX_SSE( 8,  4)
257 H264_BIWEIGHT_MMX    ( 4,  8)
258 H264_BIWEIGHT_MMX    ( 4,  4)
259 H264_BIWEIGHT_MMX    ( 4,  2)
260
261 void ff_h264dsp_init_x86(H264DSPContext *c)
262 {
263     int mm_flags = av_get_cpu_flags();
264
265     if (mm_flags & AV_CPU_FLAG_MMX2) {
266         c->h264_loop_filter_strength= h264_loop_filter_strength_mmx2;
267     }
268 #if HAVE_YASM
269     if (mm_flags & AV_CPU_FLAG_MMX) {
270         c->h264_idct_dc_add=
271         c->h264_idct_add= ff_h264_idct_add_mmx;
272         c->h264_idct8_dc_add=
273         c->h264_idct8_add= ff_h264_idct8_add_mmx;
274
275         c->h264_idct_add16     = ff_h264_idct_add16_mmx;
276         c->h264_idct8_add4     = ff_h264_idct8_add4_mmx;
277         c->h264_idct_add8      = ff_h264_idct_add8_mmx;
278         c->h264_idct_add16intra= ff_h264_idct_add16intra_mmx;
279
280         if (mm_flags & AV_CPU_FLAG_MMX2) {
281             c->h264_idct_dc_add= ff_h264_idct_dc_add_mmx2;
282             c->h264_idct8_dc_add= ff_h264_idct8_dc_add_mmx2;
283             c->h264_idct_add16     = ff_h264_idct_add16_mmx2;
284             c->h264_idct8_add4     = ff_h264_idct8_add4_mmx2;
285             c->h264_idct_add8      = ff_h264_idct_add8_mmx2;
286             c->h264_idct_add16intra= ff_h264_idct_add16intra_mmx2;
287
288             c->h264_v_loop_filter_chroma= ff_x264_deblock_v_chroma_mmxext;
289             c->h264_h_loop_filter_chroma= ff_x264_deblock_h_chroma_mmxext;
290             c->h264_v_loop_filter_chroma_intra= ff_x264_deblock_v_chroma_intra_mmxext;
291             c->h264_h_loop_filter_chroma_intra= ff_x264_deblock_h_chroma_intra_mmxext;
292 #if ARCH_X86_32
293             c->h264_v_loop_filter_luma= ff_x264_deblock_v_luma_mmxext;
294             c->h264_h_loop_filter_luma= ff_x264_deblock_h_luma_mmxext;
295             c->h264_v_loop_filter_luma_intra = ff_x264_deblock_v_luma_intra_mmxext;
296             c->h264_h_loop_filter_luma_intra = ff_x264_deblock_h_luma_intra_mmxext;
297 #endif
298             c->weight_h264_pixels_tab[0]= ff_h264_weight_16x16_mmx2;
299             c->weight_h264_pixels_tab[1]= ff_h264_weight_16x8_mmx2;
300             c->weight_h264_pixels_tab[2]= ff_h264_weight_8x16_mmx2;
301             c->weight_h264_pixels_tab[3]= ff_h264_weight_8x8_mmx2;
302             c->weight_h264_pixels_tab[4]= ff_h264_weight_8x4_mmx2;
303             c->weight_h264_pixels_tab[5]= ff_h264_weight_4x8_mmx2;
304             c->weight_h264_pixels_tab[6]= ff_h264_weight_4x4_mmx2;
305             c->weight_h264_pixels_tab[7]= ff_h264_weight_4x2_mmx2;
306
307             c->biweight_h264_pixels_tab[0]= ff_h264_biweight_16x16_mmx2;
308             c->biweight_h264_pixels_tab[1]= ff_h264_biweight_16x8_mmx2;
309             c->biweight_h264_pixels_tab[2]= ff_h264_biweight_8x16_mmx2;
310             c->biweight_h264_pixels_tab[3]= ff_h264_biweight_8x8_mmx2;
311             c->biweight_h264_pixels_tab[4]= ff_h264_biweight_8x4_mmx2;
312             c->biweight_h264_pixels_tab[5]= ff_h264_biweight_4x8_mmx2;
313             c->biweight_h264_pixels_tab[6]= ff_h264_biweight_4x4_mmx2;
314             c->biweight_h264_pixels_tab[7]= ff_h264_biweight_4x2_mmx2;
315
316             if (mm_flags&AV_CPU_FLAG_SSE2) {
317                 c->h264_idct8_add = ff_h264_idct8_add_sse2;
318                 c->h264_idct8_add4= ff_h264_idct8_add4_sse2;
319
320                 c->weight_h264_pixels_tab[0]= ff_h264_weight_16x16_sse2;
321                 c->weight_h264_pixels_tab[1]= ff_h264_weight_16x8_sse2;
322                 c->weight_h264_pixels_tab[2]= ff_h264_weight_8x16_sse2;
323                 c->weight_h264_pixels_tab[3]= ff_h264_weight_8x8_sse2;
324                 c->weight_h264_pixels_tab[4]= ff_h264_weight_8x4_sse2;
325
326                 c->biweight_h264_pixels_tab[0]= ff_h264_biweight_16x16_sse2;
327                 c->biweight_h264_pixels_tab[1]= ff_h264_biweight_16x8_sse2;
328                 c->biweight_h264_pixels_tab[2]= ff_h264_biweight_8x16_sse2;
329                 c->biweight_h264_pixels_tab[3]= ff_h264_biweight_8x8_sse2;
330                 c->biweight_h264_pixels_tab[4]= ff_h264_biweight_8x4_sse2;
331
332 #if HAVE_ALIGNED_STACK
333                 c->h264_v_loop_filter_luma = ff_x264_deblock_v_luma_sse2;
334                 c->h264_h_loop_filter_luma = ff_x264_deblock_h_luma_sse2;
335                 c->h264_v_loop_filter_luma_intra = ff_x264_deblock_v_luma_intra_sse2;
336                 c->h264_h_loop_filter_luma_intra = ff_x264_deblock_h_luma_intra_sse2;
337 #endif
338
339                 c->h264_idct_add16 = ff_h264_idct_add16_sse2;
340                 c->h264_idct_add8  = ff_h264_idct_add8_sse2;
341                 c->h264_idct_add16intra = ff_h264_idct_add16intra_sse2;
342             }
343             if (mm_flags&AV_CPU_FLAG_SSSE3) {
344                 c->biweight_h264_pixels_tab[0]= ff_h264_biweight_16x16_ssse3;
345                 c->biweight_h264_pixels_tab[1]= ff_h264_biweight_16x8_ssse3;
346                 c->biweight_h264_pixels_tab[2]= ff_h264_biweight_8x16_ssse3;
347                 c->biweight_h264_pixels_tab[3]= ff_h264_biweight_8x8_ssse3;
348                 c->biweight_h264_pixels_tab[4]= ff_h264_biweight_8x4_ssse3;
349             }
350         }
351     }
352 #endif
353 }