sh4: hpeldsp: Move half-pel assembly from dsputil to hpeldsp
[ffmpeg.git] / libavcodec / sh4 / hpeldsp.c
1 /*
2  * aligned/packed access motion
3  *
4  * Copyright (c) 2001-2003 BERO <bero@geocities.co.jp>
5  *
6  * This file is part of Libav.
7  *
8  * Libav 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  * Libav 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 Libav; 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 "libavcodec/avcodec.h"
25 #include "libavcodec/dsputil.h"
26 #include "libavcodec/hpeldsp.h"
27 #include "libavcodec/rnd_avg.h"
28 #include "dsputil_sh4.h"
29
30
31 #define         LP(p)           *(uint32_t*)(p)
32 #define         LPC(p)          *(const uint32_t*)(p)
33
34
35 #define         UNPACK(ph,pl,tt0,tt1) do { \
36         uint32_t t0,t1; t0=tt0;t1=tt1; \
37         ph = ( (t0 & ~BYTE_VEC32(0x03))>>2) + ( (t1 & ~BYTE_VEC32(0x03))>>2); \
38         pl = (t0 & BYTE_VEC32(0x03)) + (t1 & BYTE_VEC32(0x03)); } while(0)
39
40 #define         rnd_PACK(ph,pl,nph,npl) ph + nph + (((pl + npl + BYTE_VEC32(0x02))>>2) & BYTE_VEC32(0x03))
41 #define         no_rnd_PACK(ph,pl,nph,npl)      ph + nph + (((pl + npl + BYTE_VEC32(0x01))>>2) & BYTE_VEC32(0x03))
42
43 /* little-endian */
44 #define         MERGE1(a,b,ofs) (ofs==0)?a:( ((a)>>(8*ofs))|((b)<<(32-8*ofs)) )
45 #define         MERGE2(a,b,ofs) (ofs==3)?b:( ((a)>>(8*(ofs+1)))|((b)<<(32-8*(ofs+1))) )
46 /* big
47 #define         MERGE1(a,b,ofs) (ofs==0)?a:( ((a)<<(8*ofs))|((b)>>(32-8*ofs)) )
48 #define         MERGE2(a,b,ofs) (ofs==3)?b:( ((a)<<(8+8*ofs))|((b)>>(32-8-8*ofs)) )
49 */
50
51
52 #define         put(d,s)        d = s
53 #define         avg(d,s)        d = rnd_avg32(s,d)
54
55 #define         OP_C4(ofs) \
56         ref-=ofs; \
57         do { \
58                 OP(LP(dest),MERGE1(LPC(ref),LPC(ref+4),ofs)); \
59                 ref+=stride; \
60                 dest+=stride; \
61         } while(--height)
62
63 #define        OP_C40() \
64         do { \
65                 OP(LP(dest),LPC(ref)); \
66                 ref+=stride; \
67                 dest+=stride; \
68         } while(--height)
69
70
71 #define         OP      put
72
73 static void put_pixels4_c(uint8_t *dest, const uint8_t *ref,
74                           const int stride, int height)
75 {
76         switch((int)ref&3){
77         case 0: OP_C40(); return;
78         case 1: OP_C4(1); return;
79         case 2: OP_C4(2); return;
80         case 3: OP_C4(3); return;
81         }
82 }
83
84 #undef          OP
85 #define         OP      avg
86
87 static void avg_pixels4_c(uint8_t *dest, const uint8_t *ref,
88                           const int stride, int height)
89 {
90         switch((int)ref&3){
91         case 0: OP_C40(); return;
92         case 1: OP_C4(1); return;
93         case 2: OP_C4(2); return;
94         case 3: OP_C4(3); return;
95         }
96 }
97
98 #undef          OP
99
100 #define         OP_C(ofs,sz,avg2) \
101 { \
102         ref-=ofs; \
103         do { \
104                 uint32_t        t0,t1; \
105                 t0 = LPC(ref+0); \
106                 t1 = LPC(ref+4); \
107                 OP(LP(dest+0), MERGE1(t0,t1,ofs)); \
108                 t0 = LPC(ref+8); \
109                 OP(LP(dest+4), MERGE1(t1,t0,ofs)); \
110 if (sz==16) { \
111                 t1 = LPC(ref+12); \
112                 OP(LP(dest+8), MERGE1(t0,t1,ofs)); \
113                 t0 = LPC(ref+16); \
114                 OP(LP(dest+12), MERGE1(t1,t0,ofs)); \
115 } \
116                 ref+=stride; \
117                 dest+= stride; \
118         } while(--height); \
119 }
120
121 /* aligned */
122 #define         OP_C0(sz,avg2) \
123 { \
124         do { \
125                 OP(LP(dest+0), LPC(ref+0)); \
126                 OP(LP(dest+4), LPC(ref+4)); \
127 if (sz==16) { \
128                 OP(LP(dest+8), LPC(ref+8)); \
129                 OP(LP(dest+12), LPC(ref+12)); \
130 } \
131                 ref+=stride; \
132                 dest+= stride; \
133         } while(--height); \
134 }
135
136 #define         OP_X(ofs,sz,avg2) \
137 { \
138         ref-=ofs; \
139         do { \
140                 uint32_t        t0,t1; \
141                 t0 = LPC(ref+0); \
142                 t1 = LPC(ref+4); \
143                 OP(LP(dest+0), avg2(MERGE1(t0,t1,ofs),MERGE2(t0,t1,ofs))); \
144                 t0 = LPC(ref+8); \
145                 OP(LP(dest+4), avg2(MERGE1(t1,t0,ofs),MERGE2(t1,t0,ofs))); \
146 if (sz==16) { \
147                 t1 = LPC(ref+12); \
148                 OP(LP(dest+8), avg2(MERGE1(t0,t1,ofs),MERGE2(t0,t1,ofs))); \
149                 t0 = LPC(ref+16); \
150                 OP(LP(dest+12), avg2(MERGE1(t1,t0,ofs),MERGE2(t1,t0,ofs))); \
151 } \
152                 ref+=stride; \
153                 dest+= stride; \
154         } while(--height); \
155 }
156
157 /* aligned */
158 #define         OP_Y0(sz,avg2) \
159 { \
160         uint32_t t0,t1,t2,t3,t; \
161 \
162         t0 = LPC(ref+0); \
163         t1 = LPC(ref+4); \
164 if (sz==16) { \
165         t2 = LPC(ref+8); \
166         t3 = LPC(ref+12); \
167 } \
168         do { \
169                 ref += stride; \
170 \
171                 t = LPC(ref+0); \
172                 OP(LP(dest+0), avg2(t0,t)); t0 = t; \
173                 t = LPC(ref+4); \
174                 OP(LP(dest+4), avg2(t1,t)); t1 = t; \
175 if (sz==16) { \
176                 t = LPC(ref+8); \
177                 OP(LP(dest+8), avg2(t2,t)); t2 = t; \
178                 t = LPC(ref+12); \
179                 OP(LP(dest+12), avg2(t3,t)); t3 = t; \
180 } \
181                 dest+= stride; \
182         } while(--height); \
183 }
184
185 #define         OP_Y(ofs,sz,avg2) \
186 { \
187         uint32_t t0,t1,t2,t3,t,w0,w1; \
188 \
189         ref-=ofs; \
190         w0 = LPC(ref+0); \
191         w1 = LPC(ref+4); \
192         t0 = MERGE1(w0,w1,ofs); \
193         w0 = LPC(ref+8); \
194         t1 = MERGE1(w1,w0,ofs); \
195 if (sz==16) { \
196         w1 = LPC(ref+12); \
197         t2 = MERGE1(w0,w1,ofs); \
198         w0 = LPC(ref+16); \
199         t3 = MERGE1(w1,w0,ofs); \
200 } \
201         do { \
202                 ref += stride; \
203 \
204                 w0 = LPC(ref+0); \
205                 w1 = LPC(ref+4); \
206                 t = MERGE1(w0,w1,ofs); \
207                 OP(LP(dest+0), avg2(t0,t)); t0 = t; \
208                 w0 = LPC(ref+8); \
209                 t = MERGE1(w1,w0,ofs); \
210                 OP(LP(dest+4), avg2(t1,t)); t1 = t; \
211 if (sz==16) { \
212                 w1 = LPC(ref+12); \
213                 t = MERGE1(w0,w1,ofs); \
214                 OP(LP(dest+8), avg2(t2,t)); t2 = t; \
215                 w0 = LPC(ref+16); \
216                 t = MERGE1(w1,w0,ofs); \
217                 OP(LP(dest+12), avg2(t3,t)); t3 = t; \
218 } \
219                 dest+=stride; \
220         } while(--height); \
221 }
222
223 #define OP_X0(sz,avg2) OP_X(0,sz,avg2)
224 #define OP_XY0(sz,PACK) OP_XY(0,sz,PACK)
225 #define         OP_XY(ofs,sz,PACK) \
226 { \
227         uint32_t        t2,t3,w0,w1; \
228         uint32_t        a0,a1,a2,a3,a4,a5,a6,a7; \
229 \
230         ref -= ofs; \
231         w0 = LPC(ref+0); \
232         w1 = LPC(ref+4); \
233         UNPACK(a0,a1,MERGE1(w0,w1,ofs),MERGE2(w0,w1,ofs)); \
234         w0 = LPC(ref+8); \
235         UNPACK(a2,a3,MERGE1(w1,w0,ofs),MERGE2(w1,w0,ofs)); \
236 if (sz==16) { \
237         w1 = LPC(ref+12); \
238         UNPACK(a4,a5,MERGE1(w0,w1,ofs),MERGE2(w0,w1,ofs)); \
239         w0 = LPC(ref+16); \
240         UNPACK(a6,a7,MERGE1(w1,w0,ofs),MERGE2(w1,w0,ofs)); \
241 } \
242         do { \
243                 ref+=stride; \
244                 w0 = LPC(ref+0); \
245                 w1 = LPC(ref+4); \
246                 UNPACK(t2,t3,MERGE1(w0,w1,ofs),MERGE2(w0,w1,ofs)); \
247                 OP(LP(dest+0),PACK(a0,a1,t2,t3)); \
248                 a0 = t2; a1 = t3; \
249                 w0 = LPC(ref+8); \
250                 UNPACK(t2,t3,MERGE1(w1,w0,ofs),MERGE2(w1,w0,ofs)); \
251                 OP(LP(dest+4),PACK(a2,a3,t2,t3)); \
252                 a2 = t2; a3 = t3; \
253 if (sz==16) { \
254                 w1 = LPC(ref+12); \
255                 UNPACK(t2,t3,MERGE1(w0,w1,ofs),MERGE2(w0,w1,ofs)); \
256                 OP(LP(dest+8),PACK(a4,a5,t2,t3)); \
257                 a4 = t2; a5 = t3; \
258                 w0 = LPC(ref+16); \
259                 UNPACK(t2,t3,MERGE1(w1,w0,ofs),MERGE2(w1,w0,ofs)); \
260                 OP(LP(dest+12),PACK(a6,a7,t2,t3)); \
261                 a6 = t2; a7 = t3; \
262 } \
263                 dest+=stride; \
264         } while(--height); \
265 }
266
267 #define         DEFFUNC(prefix, op, rnd, xy, sz, OP_N, avgfunc) \
268 prefix void op##_##rnd##_pixels##sz##_##xy(uint8_t *dest, const uint8_t *ref, \
269                                            const ptrdiff_t stride, int height) \
270 { \
271         switch((int)ref&3) { \
272         case 0:OP_N##0(sz,rnd##_##avgfunc); return; \
273         case 1:OP_N(1,sz,rnd##_##avgfunc); return; \
274         case 2:OP_N(2,sz,rnd##_##avgfunc); return; \
275         case 3:OP_N(3,sz,rnd##_##avgfunc); return; \
276         } \
277 }
278
279 #define OP put
280
281 DEFFUNC(      ,ff_put,rnd,o,8,OP_C,avg32)
282 DEFFUNC(static,put,   rnd,x,8,OP_X,avg32)
283 DEFFUNC(static,put,no_rnd,x,8,OP_X,avg32)
284 DEFFUNC(static,put,   rnd,y,8,OP_Y,avg32)
285 DEFFUNC(static,put,no_rnd,y,8,OP_Y,avg32)
286 DEFFUNC(static,put,   rnd,xy,8,OP_XY,PACK)
287 DEFFUNC(static,put,no_rnd,xy,8,OP_XY,PACK)
288 DEFFUNC(      ,ff_put,rnd,o,16,OP_C,avg32)
289 DEFFUNC(static,put,   rnd,x,16,OP_X,avg32)
290 DEFFUNC(static,put,no_rnd,x,16,OP_X,avg32)
291 DEFFUNC(static,put,   rnd,y,16,OP_Y,avg32)
292 DEFFUNC(static,put,no_rnd,y,16,OP_Y,avg32)
293 DEFFUNC(static,put,   rnd,xy,16,OP_XY,PACK)
294 DEFFUNC(static,put,no_rnd,xy,16,OP_XY,PACK)
295
296 #undef OP
297 #define OP avg
298
299 DEFFUNC(      ,ff_avg,rnd,o,8,OP_C,avg32)
300 DEFFUNC(static,avg,   rnd,x,8,OP_X,avg32)
301 DEFFUNC(static,avg,   rnd,y,8,OP_Y,avg32)
302 DEFFUNC(static,avg,   rnd,xy,8,OP_XY,PACK)
303 DEFFUNC(      ,ff_avg,rnd,o,16,OP_C,avg32)
304 DEFFUNC(static,avg,   rnd,x,16,OP_X,avg32)
305 DEFFUNC(static,avg,no_rnd,x,16,OP_X,avg32)
306 DEFFUNC(static,avg,   rnd,y,16,OP_Y,avg32)
307 DEFFUNC(static,avg,no_rnd,y,16,OP_Y,avg32)
308 DEFFUNC(static,avg,   rnd,xy,16,OP_XY,PACK)
309 DEFFUNC(static,avg,no_rnd,xy,16,OP_XY,PACK)
310
311 #undef OP
312
313 #define         ff_put_no_rnd_pixels8_o     ff_put_rnd_pixels8_o
314 #define         ff_put_no_rnd_pixels16_o    ff_put_rnd_pixels16_o
315 #define         ff_avg_no_rnd_pixels16_o    ff_avg_rnd_pixels16_o
316
317 av_cold void ff_hpeldsp_init_sh4(HpelDSPContext *c, int flags)
318 {
319     c->put_pixels_tab[0][0] = ff_put_rnd_pixels16_o;
320     c->put_pixels_tab[0][1] = put_rnd_pixels16_x;
321     c->put_pixels_tab[0][2] = put_rnd_pixels16_y;
322     c->put_pixels_tab[0][3] = put_rnd_pixels16_xy;
323     c->put_pixels_tab[1][0] = ff_put_rnd_pixels8_o;
324     c->put_pixels_tab[1][1] = put_rnd_pixels8_x;
325     c->put_pixels_tab[1][2] = put_rnd_pixels8_y;
326     c->put_pixels_tab[1][3] = put_rnd_pixels8_xy;
327
328     c->put_no_rnd_pixels_tab[0][0] = ff_put_no_rnd_pixels16_o;
329     c->put_no_rnd_pixels_tab[0][1] = put_no_rnd_pixels16_x;
330     c->put_no_rnd_pixels_tab[0][2] = put_no_rnd_pixels16_y;
331     c->put_no_rnd_pixels_tab[0][3] = put_no_rnd_pixels16_xy;
332     c->put_no_rnd_pixels_tab[1][0] = ff_put_no_rnd_pixels8_o;
333     c->put_no_rnd_pixels_tab[1][1] = put_no_rnd_pixels8_x;
334     c->put_no_rnd_pixels_tab[1][2] = put_no_rnd_pixels8_y;
335     c->put_no_rnd_pixels_tab[1][3] = put_no_rnd_pixels8_xy;
336
337     c->avg_pixels_tab[0][0] = ff_avg_rnd_pixels16_o;
338     c->avg_pixels_tab[0][1] = avg_rnd_pixels16_x;
339     c->avg_pixels_tab[0][2] = avg_rnd_pixels16_y;
340     c->avg_pixels_tab[0][3] = avg_rnd_pixels16_xy;
341     c->avg_pixels_tab[1][0] = ff_avg_rnd_pixels8_o;
342     c->avg_pixels_tab[1][1] = avg_rnd_pixels8_x;
343     c->avg_pixels_tab[1][2] = avg_rnd_pixels8_y;
344     c->avg_pixels_tab[1][3] = avg_rnd_pixels8_xy;
345
346     c->avg_no_rnd_pixels_tab[0] = ff_avg_no_rnd_pixels16_o;
347     c->avg_no_rnd_pixels_tab[1] = avg_no_rnd_pixels16_x;
348     c->avg_no_rnd_pixels_tab[2] = avg_no_rnd_pixels16_y;
349     c->avg_no_rnd_pixels_tab[3] = avg_no_rnd_pixels16_xy;
350 }