FFmpeg
mpeg4videodsp.c
Go to the documentation of this file.
1 /*
2  * GMC (Global Motion Compensation), AltiVec-enabled
3  *
4  * Copyright (c) 2003 Romain Dolbeau <romain@dolbeau.org>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include "libavutil/attributes.h"
24 #include "libavutil/cpu.h"
25 #include "libavutil/mem_internal.h"
26 #include "libavutil/ppc/cpu.h"
28 
30 
31 #if HAVE_ALTIVEC
32 /* AltiVec-enhanced gmc1. ATM this code assumes stride is a multiple of 8
33  * to preserve proper dst alignment. */
34 static void gmc1_altivec(uint8_t *dst /* align 8 */, const uint8_t *src /* align1 */,
35  int stride, int h, int x16, int y16, int rounder)
36 {
37  int i;
38  const DECLARE_ALIGNED(16, unsigned short, rounder_a) = rounder;
39  const DECLARE_ALIGNED(16, unsigned short, ABCD)[8] = {
40  (16 - x16) * (16 - y16), /* A */
41  (x16) * (16 - y16), /* B */
42  (16 - x16) * (y16), /* C */
43  (x16) * (y16), /* D */
44  0, 0, 0, 0 /* padding */
45  };
46  register const vector unsigned char vczero =
47  (const vector unsigned char) vec_splat_u8(0);
48  register const vector unsigned short vcsr8 =
49  (const vector unsigned short) vec_splat_u16(8);
50  register vector unsigned char dstv, dstv2, srcvB, srcvC, srcvD;
51  register vector unsigned short tempB, tempC, tempD;
52  unsigned long dst_odd = (unsigned long) dst & 0x0000000F;
53  unsigned long src_really_odd = (unsigned long) src & 0x0000000F;
54  register vector unsigned short tempA =
55  vec_ld(0, (const unsigned short *) ABCD);
56  register vector unsigned short Av = vec_splat(tempA, 0);
57  register vector unsigned short Bv = vec_splat(tempA, 1);
58  register vector unsigned short Cv = vec_splat(tempA, 2);
59  register vector unsigned short Dv = vec_splat(tempA, 3);
60  register vector unsigned short rounderV =
61  vec_splat((vec_u16) vec_lde(0, &rounder_a), 0);
62 
63  /* we'll be able to pick-up our 9 char elements at src from those
64  * 32 bytes we load the first batch here, as inside the loop we can
65  * reuse 'src + stride' from one iteration as the 'src' of the next. */
66  register vector unsigned char src_0 = vec_ld(0, src);
67  register vector unsigned char src_1 = vec_ld(16, src);
68  register vector unsigned char srcvA = vec_perm(src_0, src_1,
69  vec_lvsl(0, src));
70 
71  if (src_really_odd != 0x0000000F)
72  /* If (src & 0xF) == 0xF, then (src + 1) is properly aligned
73  * on the second vector. */
74  srcvB = vec_perm(src_0, src_1, vec_lvsl(1, src));
75  else
76  srcvB = src_1;
77  srcvA = vec_mergeh(vczero, srcvA);
78  srcvB = vec_mergeh(vczero, srcvB);
79 
80  for (i = 0; i < h; i++) {
81  dst_odd = (unsigned long) dst & 0x0000000F;
82  src_really_odd = (((unsigned long) src) + stride) & 0x0000000F;
83 
84  dstv = vec_ld(0, dst);
85 
86  /* We'll be able to pick-up our 9 char elements at src + stride from
87  * those 32 bytes then reuse the resulting 2 vectors srvcC and srcvD
88  * as the next srcvA and srcvB. */
89  src_0 = vec_ld(stride + 0, src);
90  src_1 = vec_ld(stride + 16, src);
91  srcvC = vec_perm(src_0, src_1, vec_lvsl(stride + 0, src));
92 
93  if (src_really_odd != 0x0000000F)
94  /* If (src & 0xF) == 0xF, then (src + 1) is properly aligned
95  * on the second vector. */
96  srcvD = vec_perm(src_0, src_1, vec_lvsl(stride + 1, src));
97  else
98  srcvD = src_1;
99 
100  srcvC = vec_mergeh(vczero, srcvC);
101  srcvD = vec_mergeh(vczero, srcvD);
102 
103  /* OK, now we (finally) do the math :-)
104  * Those four instructions replace 32 int muls & 32 int adds.
105  * Isn't AltiVec nice? */
106  tempA = vec_mladd((vector unsigned short) srcvA, Av, rounderV);
107  tempB = vec_mladd((vector unsigned short) srcvB, Bv, tempA);
108  tempC = vec_mladd((vector unsigned short) srcvC, Cv, tempB);
109  tempD = vec_mladd((vector unsigned short) srcvD, Dv, tempC);
110 
111  srcvA = srcvC;
112  srcvB = srcvD;
113 
114  tempD = vec_sr(tempD, vcsr8);
115 
116  dstv2 = vec_pack(tempD, (vector unsigned short) vczero);
117 
118  if (dst_odd)
119  dstv2 = vec_perm(dstv, dstv2, vcprm(0, 1, s0, s1));
120  else
121  dstv2 = vec_perm(dstv, dstv2, vcprm(s0, s1, 2, 3));
122 
123  vec_st(dstv2, 0, dst);
124 
125  dst += stride;
126  src += stride;
127  }
128 }
129 #endif /* HAVE_ALTIVEC */
130 
132 {
133 #if HAVE_ALTIVEC
135  return;
136 
137  c->gmc1 = gmc1_altivec;
138 #endif /* HAVE_ALTIVEC */
139 }
mem_internal.h
Mpeg4VideoDSPContext
Definition: mpeg4videodsp.h:28
av_get_cpu_flags
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
Definition: cpu.c:103
av_cold
#define av_cold
Definition: attributes.h:90
s1
#define s1
Definition: regdef.h:38
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
PPC_ALTIVEC
#define PPC_ALTIVEC(flags)
Definition: cpu.h:25
DECLARE_ALIGNED
#define DECLARE_ALIGNED(n, t, v)
Definition: mem_internal.h:87
cpu.h
attributes.h
ff_mpeg4videodsp_init_ppc
av_cold void ff_mpeg4videodsp_init_ppc(Mpeg4VideoDSPContext *c)
Definition: mpeg4videodsp.c:131
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
stride
#define stride
Definition: h264pred_template.c:537
mpeg4videodsp.h
s0
#define s0
Definition: regdef.h:37
util_altivec.h
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
cpu.h
h
h
Definition: vp9dsp_template.c:2038
short
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option keep it simple and lowercase description are short
Definition: writing_filters.txt:89
vec_u16
#define vec_u16
Definition: util_altivec.h:36