Libav
mpegvideodsp.c
Go to the documentation of this file.
1 /*
2  * This file is part of Libav.
3  *
4  * Libav is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * Libav is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with Libav; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "config.h"
20 #include "libavutil/attributes.h"
21 #include "libavutil/cpu.h"
22 #include "libavutil/x86/asm.h"
23 #include "libavutil/x86/cpu.h"
25 
26 #if HAVE_INLINE_ASM
27 
28 static void gmc_mmx(uint8_t *dst, uint8_t *src,
29  int stride, int h, int ox, int oy,
30  int dxx, int dxy, int dyx, int dyy,
31  int shift, int r, int width, int height)
32 {
33  const int w = 8;
34  const int ix = ox >> (16 + shift);
35  const int iy = oy >> (16 + shift);
36  const int oxs = ox >> 4;
37  const int oys = oy >> 4;
38  const int dxxs = dxx >> 4;
39  const int dxys = dxy >> 4;
40  const int dyxs = dyx >> 4;
41  const int dyys = dyy >> 4;
42  const uint16_t r4[4] = { r, r, r, r };
43  const uint16_t dxy4[4] = { dxys, dxys, dxys, dxys };
44  const uint16_t dyy4[4] = { dyys, dyys, dyys, dyys };
45  const uint64_t shift2 = 2 * shift;
46  int x, y;
47 
48  const int dxw = (dxx - (1 << (16 + shift))) * (w - 1);
49  const int dyh = (dyy - (1 << (16 + shift))) * (h - 1);
50  const int dxh = dxy * (h - 1);
51  const int dyw = dyx * (w - 1);
52 
53  if ( // non-constant fullpel offset (3% of blocks)
54  ((ox ^ (ox + dxw)) | (ox ^ (ox + dxh)) | (ox ^ (ox + dxw + dxh)) |
55  (oy ^ (oy + dyw)) | (oy ^ (oy + dyh)) | (oy ^ (oy + dyw + dyh))) >> (16 + shift) ||
56  // uses more than 16 bits of subpel mv (only at huge resolution)
57  (dxx | dxy | dyx | dyy) & 15 ||
58  (unsigned) ix >= width - w ||
59  (unsigned) iy >= height - h) {
60  // FIXME could still use mmx for some of the rows
61  ff_gmc_c(dst, src, stride, h, ox, oy, dxx, dxy, dyx, dyy,
62  shift, r, width, height);
63  return;
64  }
65 
66  src += ix + iy * stride;
67 
68  __asm__ volatile (
69  "movd %0, %%mm6 \n\t"
70  "pxor %%mm7, %%mm7 \n\t"
71  "punpcklwd %%mm6, %%mm6 \n\t"
72  "punpcklwd %%mm6, %%mm6 \n\t"
73  :: "r" (1 << shift));
74 
75  for (x = 0; x < w; x += 4) {
76  uint16_t dx4[4] = { oxs - dxys + dxxs * (x + 0),
77  oxs - dxys + dxxs * (x + 1),
78  oxs - dxys + dxxs * (x + 2),
79  oxs - dxys + dxxs * (x + 3) };
80  uint16_t dy4[4] = { oys - dyys + dyxs * (x + 0),
81  oys - dyys + dyxs * (x + 1),
82  oys - dyys + dyxs * (x + 2),
83  oys - dyys + dyxs * (x + 3) };
84 
85  for (y = 0; y < h; y++) {
86  __asm__ volatile (
87  "movq %0, %%mm4 \n\t"
88  "movq %1, %%mm5 \n\t"
89  "paddw %2, %%mm4 \n\t"
90  "paddw %3, %%mm5 \n\t"
91  "movq %%mm4, %0 \n\t"
92  "movq %%mm5, %1 \n\t"
93  "psrlw $12, %%mm4 \n\t"
94  "psrlw $12, %%mm5 \n\t"
95  : "+m" (*dx4), "+m" (*dy4)
96  : "m" (*dxy4), "m" (*dyy4));
97 
98  __asm__ volatile (
99  "movq %%mm6, %%mm2 \n\t"
100  "movq %%mm6, %%mm1 \n\t"
101  "psubw %%mm4, %%mm2 \n\t"
102  "psubw %%mm5, %%mm1 \n\t"
103  "movq %%mm2, %%mm0 \n\t"
104  "movq %%mm4, %%mm3 \n\t"
105  "pmullw %%mm1, %%mm0 \n\t" // (s - dx) * (s - dy)
106  "pmullw %%mm5, %%mm3 \n\t" // dx * dy
107  "pmullw %%mm5, %%mm2 \n\t" // (s - dx) * dy
108  "pmullw %%mm4, %%mm1 \n\t" // dx * (s - dy)
109 
110  "movd %4, %%mm5 \n\t"
111  "movd %3, %%mm4 \n\t"
112  "punpcklbw %%mm7, %%mm5 \n\t"
113  "punpcklbw %%mm7, %%mm4 \n\t"
114  "pmullw %%mm5, %%mm3 \n\t" // src[1, 1] * dx * dy
115  "pmullw %%mm4, %%mm2 \n\t" // src[0, 1] * (s - dx) * dy
116 
117  "movd %2, %%mm5 \n\t"
118  "movd %1, %%mm4 \n\t"
119  "punpcklbw %%mm7, %%mm5 \n\t"
120  "punpcklbw %%mm7, %%mm4 \n\t"
121  "pmullw %%mm5, %%mm1 \n\t" // src[1, 0] * dx * (s - dy)
122  "pmullw %%mm4, %%mm0 \n\t" // src[0, 0] * (s - dx) * (s - dy)
123  "paddw %5, %%mm1 \n\t"
124  "paddw %%mm3, %%mm2 \n\t"
125  "paddw %%mm1, %%mm0 \n\t"
126  "paddw %%mm2, %%mm0 \n\t"
127 
128  "psrlw %6, %%mm0 \n\t"
129  "packuswb %%mm0, %%mm0 \n\t"
130  "movd %%mm0, %0 \n\t"
131 
132  : "=m" (dst[x + y * stride])
133  : "m" (src[0]), "m" (src[1]),
134  "m" (src[stride]), "m" (src[stride + 1]),
135  "m" (*r4), "m" (shift2));
136  src += stride;
137  }
138  src += 4 - h * stride;
139  }
140 }
141 
142 #endif /* HAVE_INLINE_ASM */
143 
145 {
146 #if HAVE_INLINE_ASM
147  int cpu_flags = av_get_cpu_flags();
148 
149  if (INLINE_MMX(cpu_flags))
150  c->gmc = gmc_mmx;
151 #endif /* HAVE_INLINE_ASM */
152 }
153 
int stride
Definition: mace.c:144
uint8_t
void(* gmc)(uint8_t *dst, uint8_t *src, int stride, int h, int ox, int oy, int dxx, int dxy, int dyx, int dyy, int shift, int r, int width, int height)
global motion compensation.
Definition: mpegvideodsp.h:37
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
Definition: cpu.c:47
#define r
Definition: input.c:51
av_cold void ff_mpegvideodsp_init_x86(MpegVideoDSPContext *c)
Definition: mpegvideodsp.c:144
void ff_gmc_c(uint8_t *dst, uint8_t *src, int stride, int h, int ox, int oy, int dxx, int dxy, int dyx, int dyy, int shift, int r, int width, int height)
Definition: mpegvideodsp.c:47
#define INLINE_MMX(flags)
Definition: cpu.h:63
static int width
Definition: utils.c:156
#define av_cold
Definition: attributes.h:66
int height
Definition: gxfenc.c:72
static const int shift2[6]
Definition: dxa.c:49