2 * Copyright (C) 2003 Michael Niedermayer <michaelni@gmx.at>
3 * Copyright (C) 2005 Nikolaj Poroshin <porosh3@psu.ru>
5 * This file is part of MPlayer.
7 * MPlayer is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * MPlayer is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * This implementation is based on an algorithm described in
24 * "Aria Nosratinia Embedded Post-Processing for
25 * Enhancement of Compressed Images (1999)"
26 * (http://citeseer.nj.nec.com/nosratinia99embedded.html)
27 * Further, with splitting (i)dct into hor/ver passes, one of them can be
28 * performed once per block, not pixel. This allows for much better speed.
32 Heavily optimized version of SPP filter by Nikolaj
44 #include "cpudetect.h"
45 #include "img_format.h"
48 #include "av_helpers.h"
49 #include "libvo/fastmemcpy.h"
51 #include "libavutil/internal.h"
52 #include "libavutil/intreadwrite.h"
53 #include "libavutil/mem.h"
54 #include "libavutil/x86/asm.h"
55 #include "libavcodec/avcodec.h"
56 #include "libavcodec/dsputil.h"
61 //===========================================================================//
64 static const short custom_threshold[64]=
65 // values (296) can't be too high
66 // -it causes too big quant dependence
67 // or maybe overflow(check), which results in some flashing
68 { 71, 296, 295, 237, 71, 40, 38, 19,
69 245, 193, 185, 121, 102, 73, 53, 27,
70 158, 129, 141, 107, 97, 73, 50, 26,
71 102, 116, 109, 98, 82, 66, 45, 23,
72 71, 94, 95, 81, 70, 56, 38, 20,
73 56, 77, 74, 66, 56, 44, 30, 15,
74 38, 53, 50, 45, 38, 30, 21, 11,
75 20, 27, 26, 23, 20, 15, 11, 5
78 DECLARE_ALIGNED(32, static const uint8_t, dither)[8][8] = {
79 { 0, 48, 12, 60, 3, 51, 15, 63, },
80 { 32, 16, 44, 28, 35, 19, 47, 31, },
81 { 8, 56, 4, 52, 11, 59, 7, 55, },
82 { 40, 24, 36, 20, 43, 27, 39, 23, },
83 { 2, 50, 14, 62, 1, 49, 13, 61, },
84 { 34, 18, 46, 30, 33, 17, 45, 29, },
85 { 10, 58, 6, 54, 9, 57, 5, 53, },
86 { 42, 26, 38, 22, 41, 25, 37, 21, },
89 struct vf_priv_s { //align 16 !
90 uint64_t threshold_mtx_noq[8*2];
91 uint64_t threshold_mtx[8*2];//used in both C & MMX (& later SSE2) versions
107 //This func reads from 1 slice, 1 and clears 0 & 1
108 static void store_slice_c(uint8_t *dst, int16_t *src, int dst_stride, int src_stride, int width, int height, int log2_scale)
111 temp= (src[x + pos] + (d[pos]>>log2_scale))>>(6-log2_scale); \
112 src[x + pos]=src[x + pos - 8*src_stride]=0; \
113 if(temp & 0x100) temp= ~(temp>>31); \
116 for(y=0; y<height; y++){
117 const uint8_t *d= dither[y];
118 for(x=0; x<width; x+=8){
134 //This func reads from 2 slices, 0 & 2 and clears 2-nd
135 static void store_slice2_c(uint8_t *dst, int16_t *src, int dst_stride, int src_stride, int width, int height, int log2_scale)
137 #define STORE2(pos) \
138 temp= (src[x + pos] + src[x + pos + 16*src_stride] + (d[pos]>>log2_scale))>>(6-log2_scale); \
139 src[x + pos + 16*src_stride]=0; \
140 if(temp & 0x100) temp= ~(temp>>31); \
143 for(y=0; y<height; y++){
144 const uint8_t *d= dither[y];
145 for(x=0; x<width; x+=8){
161 static void mul_thrmat_c(struct vf_priv_s *p,int q)
165 ((short*)p->threshold_mtx)[a]=q * ((short*)p->threshold_mtx_noq)[a];//ints faster in C
168 static void column_fidct_c(int16_t* thr_adr, int16_t *data, int16_t *output, int cnt);
169 static void row_idct_c(int16_t* workspace,
170 int16_t* output_adr, int output_stride, int cnt);
171 static void row_fdct_c(int16_t *data, const uint8_t *pixels, int line_size, int cnt);
173 //this is rather ugly, but there is no need for function pointers
174 #define store_slice_s store_slice_c
175 #define store_slice2_s store_slice2_c
176 #define mul_thrmat_s mul_thrmat_c
177 #define column_fidct_s column_fidct_c
178 #define row_idct_s row_idct_c
179 #define row_fdct_s row_fdct_c
183 //This func reads from 1 slice, 1 and clears 0 & 1
184 static void store_slice_mmx(uint8_t *dst, int16_t *src, long dst_stride, long src_stride, long width, long height, long log2_scale)
186 const uint8_t *od=&dither[0][0];
187 const uint8_t *end=&dither[height][0];
188 width = (width+7)&~7;
190 //src_stride=(src_stride-width)*2;
192 "mov %5, %%"REG_d" \n\t"
193 "mov %6, %%"REG_S" \n\t"
194 "mov %7, %%"REG_D" \n\t"
195 "mov %1, %%"REG_a" \n\t"
196 "movd %%"REG_d", %%mm5 \n\t"
197 "xor $-1, %%"REG_d" \n\t"
198 "mov %%"REG_a", %%"REG_c" \n\t"
199 "add $7, %%"REG_d" \n\t"
201 "sub %0, %%"REG_c" \n\t"
202 "add %%"REG_c", %%"REG_c" \n\t"
203 "movd %%"REG_d", %%mm2 \n\t"
204 "mov %%"REG_c", %1 \n\t"
205 "mov %2, %%"REG_d" \n\t"
206 "shl $4, %%"REG_a" \n\t"
209 "movq (%%"REG_d"), %%mm3 \n\t"
210 "movq %%mm3, %%mm4 \n\t"
211 "pxor %%mm7, %%mm7 \n\t"
212 "punpcklbw %%mm7, %%mm3 \n\t"
213 "punpckhbw %%mm7, %%mm4 \n\t"
214 "mov %0, %%"REG_c" \n\t"
215 "psraw %%mm5, %%mm3 \n\t"
216 "psraw %%mm5, %%mm4 \n\t"
218 "movq %%mm7, (%%"REG_S",%%"REG_a") \n\t"
219 "movq (%%"REG_S"), %%mm0 \n\t"
220 "movq 8(%%"REG_S"), %%mm1 \n\t"
222 "movq %%mm7, 8(%%"REG_S",%%"REG_a") \n\t"
223 "paddw %%mm3, %%mm0 \n\t"
224 "paddw %%mm4, %%mm1 \n\t"
226 "movq %%mm7, (%%"REG_S") \n\t"
227 "psraw %%mm2, %%mm0 \n\t"
228 "psraw %%mm2, %%mm1 \n\t"
230 "movq %%mm7, 8(%%"REG_S") \n\t"
231 "packuswb %%mm1, %%mm0 \n\t"
232 "add $16, %%"REG_S" \n\t"
234 "movq %%mm0, (%%"REG_D") \n\t"
235 "add $8, %%"REG_D" \n\t"
236 "sub $8, %%"REG_c" \n\t"
238 "add %1, %%"REG_S" \n\t"
239 "add $8, %%"REG_d" \n\t"
240 "add %3, %%"REG_D" \n\t"
241 "cmp %4, %%"REG_d" \n\t"
245 : "m" (width), "m" (src_stride), "erm" (od), "m" (dst_stride), "erm" (end),
246 "m" (log2_scale), "m" (src), "m" (dst) //input
247 : "%"REG_a, "%"REG_c, "%"REG_d, "%"REG_S, "%"REG_D
251 //This func reads from 2 slices, 0 & 2 and clears 2-nd
252 static void store_slice2_mmx(uint8_t *dst, int16_t *src, long dst_stride, long src_stride, long width, long height, long log2_scale)
254 const uint8_t *od=&dither[0][0];
255 const uint8_t *end=&dither[height][0];
256 width = (width+7)&~7;
258 //src_stride=(src_stride-width)*2;
260 "mov %5, %%"REG_d" \n\t"
261 "mov %6, %%"REG_S" \n\t"
262 "mov %7, %%"REG_D" \n\t"
263 "mov %1, %%"REG_a" \n\t"
264 "movd %%"REG_d", %%mm5 \n\t"
265 "xor $-1, %%"REG_d" \n\t"
266 "mov %%"REG_a", %%"REG_c" \n\t"
267 "add $7, %%"REG_d" \n\t"
268 "sub %0, %%"REG_c" \n\t"
269 "add %%"REG_c", %%"REG_c" \n\t"
270 "movd %%"REG_d", %%mm2 \n\t"
271 "mov %%"REG_c", %1 \n\t"
272 "mov %2, %%"REG_d" \n\t"
273 "shl $5, %%"REG_a" \n\t"
276 "movq (%%"REG_d"), %%mm3 \n\t"
277 "movq %%mm3, %%mm4 \n\t"
278 "pxor %%mm7, %%mm7 \n\t"
279 "punpcklbw %%mm7, %%mm3 \n\t"
280 "punpckhbw %%mm7, %%mm4 \n\t"
281 "mov %0, %%"REG_c" \n\t"
282 "psraw %%mm5, %%mm3 \n\t"
283 "psraw %%mm5, %%mm4 \n\t"
285 "movq (%%"REG_S"), %%mm0 \n\t"
286 "movq 8(%%"REG_S"), %%mm1 \n\t"
287 "paddw %%mm3, %%mm0 \n\t"
289 "paddw (%%"REG_S",%%"REG_a"), %%mm0 \n\t"
290 "paddw %%mm4, %%mm1 \n\t"
291 "movq 8(%%"REG_S",%%"REG_a"), %%mm6 \n\t"
293 "movq %%mm7, (%%"REG_S",%%"REG_a") \n\t"
294 "psraw %%mm2, %%mm0 \n\t"
295 "paddw %%mm6, %%mm1 \n\t"
297 "movq %%mm7, 8(%%"REG_S",%%"REG_a") \n\t"
298 "psraw %%mm2, %%mm1 \n\t"
299 "packuswb %%mm1, %%mm0 \n\t"
301 "movq %%mm0, (%%"REG_D") \n\t"
302 "add $16, %%"REG_S" \n\t"
303 "add $8, %%"REG_D" \n\t"
304 "sub $8, %%"REG_c" \n\t"
306 "add %1, %%"REG_S" \n\t"
307 "add $8, %%"REG_d" \n\t"
308 "add %3, %%"REG_D" \n\t"
309 "cmp %4, %%"REG_d" \n\t"
313 : "m" (width), "m" (src_stride), "erm" (od), "m" (dst_stride), "erm" (end),
314 "m" (log2_scale), "m" (src), "m" (dst) //input
315 : "%"REG_a, "%"REG_c, "%"REG_d, "%"REG_D, "%"REG_S
319 static void mul_thrmat_mmx(struct vf_priv_s *p, int q)
321 uint64_t *adr=&p->threshold_mtx_noq[0];
323 "movd %0, %%mm7 \n\t"
324 "add $8*8*2, %%"REG_D" \n\t"
325 "movq 0*8(%%"REG_S"), %%mm0 \n\t"
326 "punpcklwd %%mm7, %%mm7 \n\t"
327 "movq 1*8(%%"REG_S"), %%mm1 \n\t"
328 "punpckldq %%mm7, %%mm7 \n\t"
329 "pmullw %%mm7, %%mm0 \n\t"
331 "movq 2*8(%%"REG_S"), %%mm2 \n\t"
332 "pmullw %%mm7, %%mm1 \n\t"
334 "movq 3*8(%%"REG_S"), %%mm3 \n\t"
335 "pmullw %%mm7, %%mm2 \n\t"
337 "movq %%mm0, 0*8(%%"REG_D") \n\t"
338 "movq 4*8(%%"REG_S"), %%mm4 \n\t"
339 "pmullw %%mm7, %%mm3 \n\t"
341 "movq %%mm1, 1*8(%%"REG_D") \n\t"
342 "movq 5*8(%%"REG_S"), %%mm5 \n\t"
343 "pmullw %%mm7, %%mm4 \n\t"
345 "movq %%mm2, 2*8(%%"REG_D") \n\t"
346 "movq 6*8(%%"REG_S"), %%mm6 \n\t"
347 "pmullw %%mm7, %%mm5 \n\t"
349 "movq %%mm3, 3*8(%%"REG_D") \n\t"
350 "movq 7*8+0*8(%%"REG_S"), %%mm0 \n\t"
351 "pmullw %%mm7, %%mm6 \n\t"
353 "movq %%mm4, 4*8(%%"REG_D") \n\t"
354 "movq 7*8+1*8(%%"REG_S"), %%mm1 \n\t"
355 "pmullw %%mm7, %%mm0 \n\t"
357 "movq %%mm5, 5*8(%%"REG_D") \n\t"
358 "movq 7*8+2*8(%%"REG_S"), %%mm2 \n\t"
359 "pmullw %%mm7, %%mm1 \n\t"
361 "movq %%mm6, 6*8(%%"REG_D") \n\t"
362 "movq 7*8+3*8(%%"REG_S"), %%mm3 \n\t"
363 "pmullw %%mm7, %%mm2 \n\t"
365 "movq %%mm0, 7*8+0*8(%%"REG_D") \n\t"
366 "movq 7*8+4*8(%%"REG_S"), %%mm4 \n\t"
367 "pmullw %%mm7, %%mm3 \n\t"
369 "movq %%mm1, 7*8+1*8(%%"REG_D") \n\t"
370 "movq 7*8+5*8(%%"REG_S"), %%mm5 \n\t"
371 "pmullw %%mm7, %%mm4 \n\t"
373 "movq %%mm2, 7*8+2*8(%%"REG_D") \n\t"
374 "movq 7*8+6*8(%%"REG_S"), %%mm6 \n\t"
375 "pmullw %%mm7, %%mm5 \n\t"
377 "movq %%mm3, 7*8+3*8(%%"REG_D") \n\t"
378 "movq 14*8+0*8(%%"REG_S"), %%mm0 \n\t"
379 "pmullw %%mm7, %%mm6 \n\t"
381 "movq %%mm4, 7*8+4*8(%%"REG_D") \n\t"
382 "movq 14*8+1*8(%%"REG_S"), %%mm1 \n\t"
383 "pmullw %%mm7, %%mm0 \n\t"
385 "movq %%mm5, 7*8+5*8(%%"REG_D") \n\t"
386 "pmullw %%mm7, %%mm1 \n\t"
388 "movq %%mm6, 7*8+6*8(%%"REG_D") \n\t"
389 "movq %%mm0, 14*8+0*8(%%"REG_D") \n\t"
390 "movq %%mm1, 14*8+1*8(%%"REG_D") \n\t"
392 : "+g" (q), "+S" (adr), "+D" (adr)
397 static void column_fidct_mmx(int16_t* thr_adr, int16_t *data, int16_t *output, int cnt);
398 static void row_idct_mmx(int16_t* workspace,
399 int16_t* output_adr, int output_stride, int cnt);
400 static void row_fdct_mmx(int16_t *data, const uint8_t *pixels, int line_size, int cnt);
402 #define store_slice_s store_slice_mmx
403 #define store_slice2_s store_slice2_mmx
404 #define mul_thrmat_s mul_thrmat_mmx
405 #define column_fidct_s column_fidct_mmx
406 #define row_idct_s row_idct_mmx
407 #define row_fdct_s row_fdct_mmx
410 static void filter(struct vf_priv_s *p, uint8_t *dst, uint8_t *src,
411 int dst_stride, int src_stride,
412 int width, int height,
413 uint8_t *qp_store, int qp_stride, int is_luma)
415 int x, x0, y, es, qy, t;
416 const int stride= is_luma ? p->temp_stride : (width+16);//((width+16+15)&(~15))
417 const int step=6-p->log2_count;
418 const int qps= 3 + is_luma;
419 DECLARE_ALIGNED(32, int32_t, block_align)[4*8*BLOCKSZ+ 4*8*BLOCKSZ];
420 int16_t *block= (int16_t *)block_align;
421 int16_t *block3=(int16_t *)(block_align+4*8*BLOCKSZ);
423 memset(block3, 0, 4*8*BLOCKSZ);
425 //p->src=src-src_stride*8-8;//!
426 if (!src || !dst) return; // HACK avoid crash for Y8 colourspace
427 for(y=0; y<height; y++){
428 int index= 8 + 8*stride + y*stride;
429 fast_memcpy(p->src + index, src + y*src_stride, width);//this line can be avoided by using DR & user fr.buffers
431 p->src[index - x - 1]= p->src[index + x ];
432 p->src[index + width + x ]= p->src[index + width - x - 1];
436 fast_memcpy(p->src + ( 7-y)*stride, p->src + ( y+8)*stride, stride);
437 fast_memcpy(p->src + (height+8+y)*stride, p->src + (height-y+7)*stride, stride);
439 //FIXME (try edge emu)
442 memset(p->temp+ 8 +y*stride, 0,width*sizeof(int16_t));
444 for(y=step; y<height+8; y+=step){ //step= 1,2
446 if (qy>height-1) qy=height-1;
448 qy=(qy>>qps)*qp_stride;
449 row_fdct_s(block, p->src + y*stride +2-(y&1), stride, 2);
450 for(x0=0; x0<width+8-8*(BLOCKSZ-1); x0+=8*(BLOCKSZ-1)){
451 row_fdct_s(block+8*8, p->src + y*stride+8+x0 +2-(y&1), stride, 2*(BLOCKSZ-1));
453 column_fidct_s((int16_t*)(&p->threshold_mtx[0]), block+0*8, block3+0*8, 8*(BLOCKSZ-1)); //yes, this is a HOTSPOT
455 for (x=0; x<8*(BLOCKSZ-1); x+=8) {
456 t=x+x0-2; //correct t=x+x0-2-(y&1), but its the same
457 if (t<0) t=0;//t always < width-2
458 t=qp_store[qy+(t>>qps)];
459 t=norm_qscale(t, p->mpeg2);
460 if (t!=p->prev_q) p->prev_q=t, mul_thrmat_s(p, t);
461 column_fidct_s((int16_t*)(&p->threshold_mtx[0]), block+x*8, block3+x*8, 8); //yes, this is a HOTSPOT
463 row_idct_s(block3+0*8, p->temp + (y&15)*stride+x0+2-(y&1), stride, 2*(BLOCKSZ-1));
464 memmove(block, block+(BLOCKSZ-1)*64, 8*8*sizeof(int16_t)); //cycling
465 memmove(block3, block3+(BLOCKSZ-1)*64, 6*8*sizeof(int16_t));
468 es=width+8-x0; // 8, ...
470 row_fdct_s(block+8*8, p->src + y*stride+8+x0 +2-(y&1), stride, (es-4)>>2);
471 column_fidct_s((int16_t*)(&p->threshold_mtx[0]), block, block3, es&(~1));
472 row_idct_s(block3+0*8, p->temp + (y&15)*stride+x0+2-(y&1), stride, es>>2);
473 {const int y1=y-8+step;//l5-7 l4-6
475 if (y1&8) store_slice_s(dst + (y1-8)*dst_stride, p->temp+ 8 +8*stride,
476 dst_stride, stride, width, 8, 5-p->log2_count);
477 else store_slice2_s(dst + (y1-8)*dst_stride, p->temp+ 8 +0*stride,
478 dst_stride, stride, width, 8, 5-p->log2_count);
482 if (y&7) { // == height & 7
483 if (y&8) store_slice_s(dst + ((y-8)&~7)*dst_stride, p->temp+ 8 +8*stride,
484 dst_stride, stride, width, y&7, 5-p->log2_count);
485 else store_slice2_s(dst + ((y-8)&~7)*dst_stride, p->temp+ 8 +0*stride,
486 dst_stride, stride, width, y&7, 5-p->log2_count);
490 static int config(struct vf_instance *vf,
491 int width, int height, int d_width, int d_height,
492 unsigned int flags, unsigned int outfmt)
494 int h= (height+16+15)&(~15);
496 vf->priv->temp_stride= (width+16+15)&(~15);
497 vf->priv->temp= (int16_t*)av_mallocz(vf->priv->temp_stride*3*8*sizeof(int16_t));
498 //this can also be avoided, see above
499 vf->priv->src = (uint8_t*)av_malloc(vf->priv->temp_stride*h*sizeof(uint8_t));
501 return ff_vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
504 static void get_image(struct vf_instance *vf, mp_image_t *mpi)
506 if(mpi->flags&MP_IMGFLAG_PRESERVE) return; // don't change
507 // ok, we can do pp in-place (or pp disabled):
508 vf->dmpi=ff_vf_get_image(vf->next,mpi->imgfmt,
509 mpi->type, mpi->flags, mpi->width, mpi->height);
510 mpi->planes[0]=vf->dmpi->planes[0];
511 mpi->stride[0]=vf->dmpi->stride[0];
512 mpi->width=vf->dmpi->width;
513 if(mpi->flags&MP_IMGFLAG_PLANAR){
514 mpi->planes[1]=vf->dmpi->planes[1];
515 mpi->planes[2]=vf->dmpi->planes[2];
516 mpi->stride[1]=vf->dmpi->stride[1];
517 mpi->stride[2]=vf->dmpi->stride[2];
519 mpi->flags|=MP_IMGFLAG_DIRECT;
522 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
525 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
526 // no DR, so get a new image! hope we'll get DR buffer:
527 dmpi=ff_vf_get_image(vf->next,mpi->imgfmt,
529 MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
530 mpi->width,mpi->height);
531 ff_vf_clone_mpi_attributes(dmpi, mpi);
536 vf->priv->mpeg2= mpi->qscale_type;
537 if(mpi->pict_type != 3 && mpi->qscale && !vf->priv->qp){
538 int w = mpi->qstride;
539 int h = (mpi->h + 15) >> 4;
541 w = (mpi->w + 15) >> 4;
544 if(!vf->priv->non_b_qp)
545 vf->priv->non_b_qp= malloc(w*h);
546 fast_memcpy(vf->priv->non_b_qp, mpi->qscale, w*h);
548 if(vf->priv->log2_count || !(mpi->flags&MP_IMGFLAG_DIRECT)){
549 char *qp_tab= vf->priv->non_b_qp;
550 if(vf->priv->bframes || !qp_tab)
553 if(qp_tab || vf->priv->qp){
554 filter(vf->priv, dmpi->planes[0], mpi->planes[0], dmpi->stride[0], mpi->stride[0],
555 mpi->w, mpi->h, qp_tab, mpi->qstride, 1);
556 filter(vf->priv, dmpi->planes[1], mpi->planes[1], dmpi->stride[1], mpi->stride[1],
557 mpi->w>>mpi->chroma_x_shift, mpi->h>>mpi->chroma_y_shift, qp_tab, mpi->qstride, 0);
558 filter(vf->priv, dmpi->planes[2], mpi->planes[2], dmpi->stride[2], mpi->stride[2],
559 mpi->w>>mpi->chroma_x_shift, mpi->h>>mpi->chroma_y_shift, qp_tab, mpi->qstride, 0);
561 memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h, dmpi->stride[0], mpi->stride[0]);
562 memcpy_pic(dmpi->planes[1], mpi->planes[1], mpi->w>>mpi->chroma_x_shift, mpi->h>>mpi->chroma_y_shift, dmpi->stride[1], mpi->stride[1]);
563 memcpy_pic(dmpi->planes[2], mpi->planes[2], mpi->w>>mpi->chroma_x_shift, mpi->h>>mpi->chroma_y_shift, dmpi->stride[2], mpi->stride[2]);
568 if(ff_gCpuCaps.hasMMX) __asm__ volatile ("emms\n\t");
571 if(ff_gCpuCaps.hasMMX2) __asm__ volatile ("sfence\n\t");
573 return ff_vf_next_put_image(vf,dmpi, pts);
576 static void uninit(struct vf_instance *vf)
578 if(!vf->priv) return;
580 av_free(vf->priv->temp);
581 vf->priv->temp= NULL;
582 av_free(vf->priv->src);
584 //free(vf->priv->avctx);
585 //vf->priv->avctx= NULL;
586 free(vf->priv->non_b_qp);
587 vf->priv->non_b_qp= NULL;
593 //===========================================================================//
595 static int query_format(struct vf_instance *vf, unsigned int fmt)
609 return ff_vf_next_query_format(vf,fmt);
614 static int control(struct vf_instance *vf, int request, void* data)
617 case VFCTRL_QUERY_MAX_PP_LEVEL:
619 case VFCTRL_SET_PP_LEVEL:
620 vf->priv->log2_count= *((unsigned int*)data);
621 if (vf->priv->log2_count < 4) vf->priv->log2_count=4;
624 return ff_vf_next_control(vf,request,data);
627 static int vf_open(vf_instance_t *vf, char *args)
630 int custom_threshold_m[64];
634 vf->put_image=put_image;
635 vf->get_image=get_image;
636 vf->query_format=query_format;
638 vf->control= control;
639 vf->priv=av_mallocz(sizeof(struct vf_priv_s));//assumes align 16 !
643 //vf->priv->avctx= avcodec_alloc_context();
644 //dsputil_init(&vf->priv->dsp, vf->priv->avctx);
646 vf->priv->log2_count= 4;
647 vf->priv->bframes = 0;
649 if (args) sscanf(args, "%d:%d:%d:%d", &log2c, &vf->priv->qp, &i, &vf->priv->bframes);
651 if( log2c >=4 && log2c <=5 )
652 vf->priv->log2_count = log2c;
653 else if( log2c >= 6 )
654 vf->priv->log2_count = 5;
659 if (i < -15) i = -15;
662 bias= (1<<4)+i; //regulable
665 for(i=0;i<64;i++) //FIXME: tune custom_threshold[] and remove this !
666 custom_threshold_m[i]=(int)(custom_threshold[i]*(bias/71.)+ 0.5);
668 vf->priv->threshold_mtx_noq[2*i]=(uint64_t)custom_threshold_m[i*8+2]
669 |(((uint64_t)custom_threshold_m[i*8+6])<<16)
670 |(((uint64_t)custom_threshold_m[i*8+0])<<32)
671 |(((uint64_t)custom_threshold_m[i*8+4])<<48);
672 vf->priv->threshold_mtx_noq[2*i+1]=(uint64_t)custom_threshold_m[i*8+5]
673 |(((uint64_t)custom_threshold_m[i*8+3])<<16)
674 |(((uint64_t)custom_threshold_m[i*8+1])<<32)
675 |(((uint64_t)custom_threshold_m[i*8+7])<<48);
678 if (vf->priv->qp) vf->priv->prev_q=vf->priv->qp, mul_thrmat_s(vf->priv, vf->priv->qp);
683 const vf_info_t ff_vf_info_fspp = {
684 "fast simple postprocess",
686 "Michael Niedermayer, Nikolaj Poroshin",
692 //====================================================================
693 //Specific spp's dct, idct and threshold functions
694 //I'd prefer to have them in the separate file.
696 //#define MANGLE(a) #a
698 //typedef int16_t int16_t; //! only int16_t
701 #define DCTSIZE_S "8"
703 #define FIX(x,s) ((int) ((x) * (1<<s) + 0.5)&0xffff)
704 #define C64(x) ((uint64_t)((x)|(x)<<16))<<32 | (uint64_t)(x) | (uint64_t)(x)<<16
705 #define FIX64(x,s) C64(FIX(x,s))
707 #define MULTIPLY16H(x,k) (((x)*(k))>>16)
708 #define THRESHOLD(r,x,t) if(((unsigned)((x)+t))>t*2) r=(x);else r=0;
709 #define DESCALE(x,n) (((x) + (1 << ((n)-1))) >> n)
713 DECLARE_ASM_CONST(8, uint64_t, MM_FIX_0_382683433)=FIX64(0.382683433, 14);
714 DECLARE_ALIGNED(8, uint64_t, ff_MM_FIX_0_541196100)=FIX64(0.541196100, 14);
715 DECLARE_ALIGNED(8, uint64_t, ff_MM_FIX_0_707106781)=FIX64(0.707106781, 14);
716 DECLARE_ASM_CONST(8, uint64_t, MM_FIX_1_306562965)=FIX64(1.306562965, 14);
718 DECLARE_ASM_CONST(8, uint64_t, MM_FIX_1_414213562_A)=FIX64(1.414213562, 14);
720 DECLARE_ASM_CONST(8, uint64_t, MM_FIX_1_847759065)=FIX64(1.847759065, 13);
721 DECLARE_ASM_CONST(8, uint64_t, MM_FIX_2_613125930)=FIX64(-2.613125930, 13); //-
722 DECLARE_ASM_CONST(8, uint64_t, MM_FIX_1_414213562)=FIX64(1.414213562, 13);
723 DECLARE_ASM_CONST(8, uint64_t, MM_FIX_1_082392200)=FIX64(1.082392200, 13);
724 //for t3,t5,t7 == 0 shortcut
725 DECLARE_ASM_CONST(8, uint64_t, MM_FIX_0_847759065)=FIX64(0.847759065, 14);
726 DECLARE_ASM_CONST(8, uint64_t, MM_FIX_0_566454497)=FIX64(0.566454497, 14);
727 DECLARE_ASM_CONST(8, uint64_t, MM_FIX_0_198912367)=FIX64(0.198912367, 14);
729 DECLARE_ASM_CONST(8, uint64_t, MM_DESCALE_RND)=C64(4);
730 DECLARE_ASM_CONST(8, uint64_t, MM_2)=C64(2);
732 #else /* !HAVE_MMX */
734 typedef int32_t int_simd16_t;
735 static const int16_t FIX_0_382683433=FIX(0.382683433, 14);
736 static const int16_t FIX_0_541196100=FIX(0.541196100, 14);
737 static const int16_t FIX_0_707106781=FIX(0.707106781, 14);
738 static const int16_t FIX_1_306562965=FIX(1.306562965, 14);
739 static const int16_t FIX_1_414213562_A=FIX(1.414213562, 14);
740 static const int16_t FIX_1_847759065=FIX(1.847759065, 13);
741 static const int16_t FIX_2_613125930=FIX(-2.613125930, 13); //-
742 static const int16_t FIX_1_414213562=FIX(1.414213562, 13);
743 static const int16_t FIX_1_082392200=FIX(1.082392200, 13);
749 static void column_fidct_c(int16_t* thr_adr, int16_t *data, int16_t *output, int cnt)
751 int_simd16_t tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
752 int_simd16_t tmp10, tmp11, tmp12, tmp13;
753 int_simd16_t z1,z2,z3,z4,z5, z10, z11, z12, z13;
754 int_simd16_t d0, d1, d2, d3, d4, d5, d6, d7;
764 for (; cnt > 0; cnt-=2) { //start positions
765 threshold=(int16_t*)thr_adr;//threshold_mtx
766 for (ctr = DCTSIZE; ctr > 0; ctr--) {
767 // Process columns from input, add to output.
768 tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7];
769 tmp7 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7];
771 tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6];
772 tmp6 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6];
774 tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5];
775 tmp5 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5];
777 tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4];
778 tmp4 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4];
790 z1 = MULTIPLY16H((tmp12 + tmp13) <<2, FIX_0_707106781);
796 THRESHOLD(tmp0, d0, threshold[0*8]);
797 THRESHOLD(tmp1, d2, threshold[2*8]);
798 THRESHOLD(tmp2, d4, threshold[4*8]);
799 THRESHOLD(tmp3, d6, threshold[6*8]);
801 tmp10 = (tmp0 + tmp2)>>2;
802 tmp11 = (tmp0 - tmp2)>>2;
804 tmp13 = (tmp1 + tmp3)>>2; //+2 ! (psnr decides)
805 tmp12 = MULTIPLY16H((tmp1 - tmp3), FIX_1_414213562_A) - tmp13; //<<2
807 tmp0 = tmp10 + tmp13; //->temps
808 tmp3 = tmp10 - tmp13; //->temps
809 tmp1 = tmp11 + tmp12; //->temps
810 tmp2 = tmp11 - tmp12; //->temps
818 z5 = MULTIPLY16H((tmp10 - tmp12)<<2, FIX_0_382683433);
819 z2 = MULTIPLY16H(tmp10 <<2, FIX_0_541196100) + z5;
820 z4 = MULTIPLY16H(tmp12 <<2, FIX_1_306562965) + z5;
821 z3 = MULTIPLY16H(tmp11 <<2, FIX_0_707106781);
833 THRESHOLD(tmp4, d1, threshold[1*8]);
834 THRESHOLD(tmp5, d3, threshold[3*8]);
835 THRESHOLD(tmp6, d5, threshold[5*8]);
836 THRESHOLD(tmp7, d7, threshold[7*8]);
838 //Simd version uses here a shortcut for the tmp5,tmp6,tmp7 == 0
840 z10 = (tmp6 - tmp5)<<1;
842 z12 = (tmp4 - tmp7)<<1;
844 tmp7 = (z11 + z13)>>2; //+2 !
845 tmp11 = MULTIPLY16H((z11 - z13)<<1, FIX_1_414213562);
846 z5 = MULTIPLY16H(z10 + z12, FIX_1_847759065);
847 tmp10 = MULTIPLY16H(z12, FIX_1_082392200) - z5;
848 tmp12 = MULTIPLY16H(z10, FIX_2_613125930) + z5; // - !!
854 wsptr[DCTSIZE*0]+= (tmp0 + tmp7);
855 wsptr[DCTSIZE*1]+= (tmp1 + tmp6);
856 wsptr[DCTSIZE*2]+= (tmp2 + tmp5);
857 wsptr[DCTSIZE*3]+= (tmp3 - tmp4);
858 wsptr[DCTSIZE*4]+= (tmp3 + tmp4);
859 wsptr[DCTSIZE*5]+= (tmp2 - tmp5);
860 wsptr[DCTSIZE*6]= (tmp1 - tmp6);
861 wsptr[DCTSIZE*7]= (tmp0 - tmp7);
863 dataptr++; //next column
867 dataptr+=8; //skip each second start pos
874 static void column_fidct_mmx(int16_t* thr_adr, int16_t *data, int16_t *output, int cnt)
876 DECLARE_ALIGNED(8, uint64_t, temps)[4];
880 "movq "DCTSIZE_S"*0*2(%%"REG_S"), %%mm1 \n\t"
882 "movq "DCTSIZE_S"*3*2(%%"REG_S"), %%mm7 \n\t"
883 "movq %%mm1, %%mm0 \n\t"
885 "paddw "DCTSIZE_S"*7*2(%%"REG_S"), %%mm1 \n\t" //t0
886 "movq %%mm7, %%mm3 \n\t"
888 "paddw "DCTSIZE_S"*4*2(%%"REG_S"), %%mm7 \n\t" //t3
889 "movq %%mm1, %%mm5 \n\t"
891 "movq "DCTSIZE_S"*1*2(%%"REG_S"), %%mm6 \n\t"
892 "psubw %%mm7, %%mm1 \n\t" //t13
894 "movq "DCTSIZE_S"*2*2(%%"REG_S"), %%mm2 \n\t"
895 "movq %%mm6, %%mm4 \n\t"
897 "paddw "DCTSIZE_S"*6*2(%%"REG_S"), %%mm6 \n\t" //t1
898 "paddw %%mm7, %%mm5 \n\t" //t10
900 "paddw "DCTSIZE_S"*5*2(%%"REG_S"), %%mm2 \n\t" //t2
901 "movq %%mm6, %%mm7 \n\t"
903 "paddw %%mm2, %%mm6 \n\t" //t11
904 "psubw %%mm2, %%mm7 \n\t" //t12
906 "movq %%mm5, %%mm2 \n\t"
907 "paddw %%mm6, %%mm5 \n\t" //d0
908 // i0 t13 t12 i3 i1 d0 - d4
909 "psubw %%mm6, %%mm2 \n\t" //d4
910 "paddw %%mm1, %%mm7 \n\t"
912 "movq 4*16(%%"REG_d"), %%mm6 \n\t"
913 "psllw $2, %%mm7 \n\t"
915 "psubw 0*16(%%"REG_d"), %%mm5 \n\t"
916 "psubw %%mm6, %%mm2 \n\t"
918 "paddusw 0*16(%%"REG_d"), %%mm5 \n\t"
919 "paddusw %%mm6, %%mm2 \n\t"
921 "pmulhw "MANGLE(ff_MM_FIX_0_707106781)", %%mm7 \n\t"
923 "paddw 0*16(%%"REG_d"), %%mm5 \n\t"
924 "paddw %%mm6, %%mm2 \n\t"
926 "psubusw 0*16(%%"REG_d"), %%mm5 \n\t"
927 "psubusw %%mm6, %%mm2 \n\t"
929 //This func is totally compute-bound, operates at huge speed. So, DC shortcut
930 // at this place isn't worthwhile due to BTB miss penalty (checked on Pent. 3).
931 //However, typical numbers: nondc - 29%%, dc - 46%%, zero - 25%%. All <> 0 case is very rare.
932 "paddw "MANGLE(MM_2)", %%mm5 \n\t"
933 "movq %%mm2, %%mm6 \n\t"
935 "paddw %%mm5, %%mm2 \n\t"
936 "psubw %%mm6, %%mm5 \n\t"
938 "movq %%mm1, %%mm6 \n\t"
939 "paddw %%mm7, %%mm1 \n\t" //d2
941 "psubw 2*16(%%"REG_d"), %%mm1 \n\t"
942 "psubw %%mm7, %%mm6 \n\t" //d6
944 "movq 6*16(%%"REG_d"), %%mm7 \n\t"
945 "psraw $2, %%mm5 \n\t"
947 "paddusw 2*16(%%"REG_d"), %%mm1 \n\t"
948 "psubw %%mm7, %%mm6 \n\t"
949 // t7 d2 /t11 t4 t6 - d6 /t10
951 "paddw 2*16(%%"REG_d"), %%mm1 \n\t"
952 "paddusw %%mm7, %%mm6 \n\t"
954 "psubusw 2*16(%%"REG_d"), %%mm1 \n\t"
955 "paddw %%mm7, %%mm6 \n\t"
957 "psubw "DCTSIZE_S"*4*2(%%"REG_S"), %%mm3 \n\t"
958 "psubusw %%mm7, %%mm6 \n\t"
960 //movq [edi+"DCTSIZE_S"*2*2], mm1
961 //movq [edi+"DCTSIZE_S"*6*2], mm6
962 "movq %%mm1, %%mm7 \n\t"
963 "psraw $2, %%mm2 \n\t"
965 "psubw "DCTSIZE_S"*6*2(%%"REG_S"), %%mm4 \n\t"
966 "psubw %%mm6, %%mm1 \n\t"
968 "psubw "DCTSIZE_S"*7*2(%%"REG_S"), %%mm0 \n\t"
969 "paddw %%mm7, %%mm6 \n\t" //'t13
971 "psraw $2, %%mm6 \n\t" //paddw mm6, MM_2 !! ---
972 "movq %%mm2, %%mm7 \n\t"
974 "pmulhw "MANGLE(MM_FIX_1_414213562_A)", %%mm1 \n\t"
975 "paddw %%mm6, %%mm2 \n\t" //'t0
977 "movq %%mm2, 0*8+%3 \n\t" //!
978 "psubw %%mm6, %%mm7 \n\t" //'t3
980 "movq "DCTSIZE_S"*2*2(%%"REG_S"), %%mm2 \n\t"
981 "psubw %%mm6, %%mm1 \n\t" //'t12
983 "psubw "DCTSIZE_S"*5*2(%%"REG_S"), %%mm2 \n\t" //t5
984 "movq %%mm5, %%mm6 \n\t"
986 "movq %%mm7, 3*8+%3 \n\t"
987 "paddw %%mm2, %%mm3 \n\t" //t10
989 "paddw %%mm4, %%mm2 \n\t" //t11
990 "paddw %%mm0, %%mm4 \n\t" //t12
992 "movq %%mm3, %%mm7 \n\t"
993 "psubw %%mm4, %%mm3 \n\t"
995 "psllw $2, %%mm3 \n\t"
996 "psllw $2, %%mm7 \n\t" //opt for P6
998 "pmulhw "MANGLE(MM_FIX_0_382683433)", %%mm3 \n\t"
999 "psllw $2, %%mm4 \n\t"
1001 "pmulhw "MANGLE(ff_MM_FIX_0_541196100)", %%mm7 \n\t"
1002 "psllw $2, %%mm2 \n\t"
1004 "pmulhw "MANGLE(MM_FIX_1_306562965)", %%mm4 \n\t"
1005 "paddw %%mm1, %%mm5 \n\t" //'t1
1007 "pmulhw "MANGLE(ff_MM_FIX_0_707106781)", %%mm2 \n\t"
1008 "psubw %%mm1, %%mm6 \n\t" //'t2
1009 // t7 't12 't11 t4 t6 - 't13 't10 ---
1011 "paddw %%mm3, %%mm7 \n\t" //z2
1013 "movq %%mm5, 1*8+%3 \n\t"
1014 "paddw %%mm3, %%mm4 \n\t" //z4
1016 "movq 3*16(%%"REG_d"), %%mm3 \n\t"
1017 "movq %%mm0, %%mm1 \n\t"
1019 "movq %%mm6, 2*8+%3 \n\t"
1020 "psubw %%mm2, %%mm1 \n\t" //z13
1023 "paddw %%mm2, %%mm0 \n\t" //z11
1024 "movq %%mm1, %%mm5 \n\t"
1026 "movq 5*16(%%"REG_d"), %%mm2 \n\t"
1027 "psubw %%mm7, %%mm1 \n\t" //d3
1029 "paddw %%mm7, %%mm5 \n\t" //d5
1030 "psubw %%mm3, %%mm1 \n\t"
1032 "movq 1*16(%%"REG_d"), %%mm7 \n\t"
1033 "psubw %%mm2, %%mm5 \n\t"
1035 "movq %%mm0, %%mm6 \n\t"
1036 "paddw %%mm4, %%mm0 \n\t" //d1
1038 "paddusw %%mm3, %%mm1 \n\t"
1039 "psubw %%mm4, %%mm6 \n\t" //d7
1041 // d1 d3 - - - d5 d7 -
1042 "movq 7*16(%%"REG_d"), %%mm4 \n\t"
1043 "psubw %%mm7, %%mm0 \n\t"
1045 "psubw %%mm4, %%mm6 \n\t"
1046 "paddusw %%mm2, %%mm5 \n\t"
1048 "paddusw %%mm4, %%mm6 \n\t"
1049 "paddw %%mm3, %%mm1 \n\t"
1051 "paddw %%mm2, %%mm5 \n\t"
1052 "paddw %%mm4, %%mm6 \n\t"
1054 "psubusw %%mm3, %%mm1 \n\t"
1055 "psubusw %%mm2, %%mm5 \n\t"
1057 "psubusw %%mm4, %%mm6 \n\t"
1058 "movq %%mm1, %%mm4 \n\t"
1060 "por %%mm5, %%mm4 \n\t"
1061 "paddusw %%mm7, %%mm0 \n\t"
1063 "por %%mm6, %%mm4 \n\t"
1064 "paddw %%mm7, %%mm0 \n\t"
1066 "packssdw %%mm4, %%mm4 \n\t"
1067 "psubusw %%mm7, %%mm0 \n\t"
1069 "movd %%mm4, %%"REG_a" \n\t"
1070 "or %%"REG_a", %%"REG_a" \n\t"
1072 //movq [edi+"DCTSIZE_S"*3*2], mm1
1073 //movq [edi+"DCTSIZE_S"*5*2], mm5
1074 //movq [edi+"DCTSIZE_S"*1*2], mm0
1075 //movq [edi+"DCTSIZE_S"*7*2], mm6
1076 // t4 t5 - - - t6 t7 -
1077 //--- t4 (mm0) may be <>0; mm1, mm5, mm6 == 0
1078 //Typical numbers: nondc - 19%%, dc - 26%%, zero - 55%%. zero case alone isn't worthwhile
1079 "movq 0*8+%3, %%mm4 \n\t"
1080 "movq %%mm0, %%mm1 \n\t"
1082 "pmulhw "MANGLE(MM_FIX_0_847759065)", %%mm0 \n\t" //tmp6
1083 "movq %%mm1, %%mm2 \n\t"
1085 "movq "DCTSIZE_S"*0*2(%%"REG_D"), %%mm5 \n\t"
1086 "movq %%mm2, %%mm3 \n\t"
1088 "pmulhw "MANGLE(MM_FIX_0_566454497)", %%mm1 \n\t" //tmp5
1089 "paddw %%mm4, %%mm5 \n\t"
1091 "movq 1*8+%3, %%mm6 \n\t"
1093 "psraw $2, %%mm3 \n\t" //tmp7
1095 "pmulhw "MANGLE(MM_FIX_0_198912367)", %%mm2 \n\t" //-tmp4
1096 "psubw %%mm3, %%mm4 \n\t"
1098 "movq "DCTSIZE_S"*1*2(%%"REG_D"), %%mm7 \n\t"
1099 "paddw %%mm3, %%mm5 \n\t"
1101 "movq %%mm4, "DCTSIZE_S"*7*2(%%"REG_D") \n\t"
1102 "paddw %%mm6, %%mm7 \n\t"
1104 "movq 2*8+%3, %%mm3 \n\t"
1105 "psubw %%mm0, %%mm6 \n\t"
1107 "movq "DCTSIZE_S"*2*2(%%"REG_D"), %%mm4 \n\t"
1108 "paddw %%mm0, %%mm7 \n\t"
1110 "movq %%mm5, "DCTSIZE_S"*0*2(%%"REG_D") \n\t"
1111 "paddw %%mm3, %%mm4 \n\t"
1113 "movq %%mm6, "DCTSIZE_S"*6*2(%%"REG_D") \n\t"
1114 "psubw %%mm1, %%mm3 \n\t"
1116 "movq "DCTSIZE_S"*5*2(%%"REG_D"), %%mm5 \n\t"
1117 "paddw %%mm1, %%mm4 \n\t"
1119 "movq "DCTSIZE_S"*3*2(%%"REG_D"), %%mm6 \n\t"
1120 "paddw %%mm3, %%mm5 \n\t"
1122 "movq 3*8+%3, %%mm0 \n\t"
1123 "add $8, %%"REG_S" \n\t"
1125 "movq %%mm7, "DCTSIZE_S"*1*2(%%"REG_D") \n\t"
1126 "paddw %%mm0, %%mm6 \n\t"
1128 "movq %%mm4, "DCTSIZE_S"*2*2(%%"REG_D") \n\t"
1129 "psubw %%mm2, %%mm0 \n\t"
1131 "movq "DCTSIZE_S"*4*2(%%"REG_D"), %%mm7 \n\t"
1132 "paddw %%mm2, %%mm6 \n\t"
1134 "movq %%mm5, "DCTSIZE_S"*5*2(%%"REG_D") \n\t"
1135 "paddw %%mm0, %%mm7 \n\t"
1137 "movq %%mm6, "DCTSIZE_S"*3*2(%%"REG_D") \n\t"
1139 "movq %%mm7, "DCTSIZE_S"*4*2(%%"REG_D") \n\t"
1140 "add $8, %%"REG_D" \n\t"
1145 //psraw mm1, 2 w/o it -> offset. thr1, thr1, thr1 (actually thr1, thr1, thr1-1)
1149 "movq %%mm5, %%mm3 \n\t"
1150 "psubw %%mm1, %%mm5 \n\t"
1152 "psllw $1, %%mm5 \n\t" //'z10
1153 "paddw %%mm1, %%mm3 \n\t" //'z13
1155 "movq %%mm0, %%mm2 \n\t"
1156 "psubw %%mm6, %%mm0 \n\t"
1158 "movq %%mm5, %%mm1 \n\t"
1159 "psllw $1, %%mm0 \n\t" //'z12
1161 "pmulhw "MANGLE(MM_FIX_2_613125930)", %%mm1 \n\t" //-
1162 "paddw %%mm0, %%mm5 \n\t"
1164 "pmulhw "MANGLE(MM_FIX_1_847759065)", %%mm5 \n\t" //'z5
1165 "paddw %%mm6, %%mm2 \n\t" //'z11
1167 "pmulhw "MANGLE(MM_FIX_1_082392200)", %%mm0 \n\t"
1168 "movq %%mm2, %%mm7 \n\t"
1171 "movq 0*8+%3, %%mm4 \n\t"
1172 "psubw %%mm3, %%mm2 \n\t"
1174 "psllw $1, %%mm2 \n\t"
1175 "paddw %%mm3, %%mm7 \n\t" //'t7
1177 "pmulhw "MANGLE(MM_FIX_1_414213562)", %%mm2 \n\t" //'t11
1178 "movq %%mm4, %%mm6 \n\t"
1180 "psraw $2, %%mm7 \n\t"
1182 "paddw "DCTSIZE_S"*0*2(%%"REG_D"), %%mm4 \n\t"
1183 "psubw %%mm7, %%mm6 \n\t"
1185 "movq 1*8+%3, %%mm3 \n\t"
1186 "paddw %%mm7, %%mm4 \n\t"
1188 "movq %%mm6, "DCTSIZE_S"*7*2(%%"REG_D") \n\t"
1189 "paddw %%mm5, %%mm1 \n\t" //'t12
1191 "movq %%mm4, "DCTSIZE_S"*0*2(%%"REG_D") \n\t"
1192 "psubw %%mm7, %%mm1 \n\t" //'t6
1194 "movq 2*8+%3, %%mm7 \n\t"
1195 "psubw %%mm5, %%mm0 \n\t" //'t10
1197 "movq 3*8+%3, %%mm6 \n\t"
1198 "movq %%mm3, %%mm5 \n\t"
1200 "paddw "DCTSIZE_S"*1*2(%%"REG_D"), %%mm3 \n\t"
1201 "psubw %%mm1, %%mm5 \n\t"
1203 "psubw %%mm1, %%mm2 \n\t" //'t5
1204 "paddw %%mm1, %%mm3 \n\t"
1206 "movq %%mm5, "DCTSIZE_S"*6*2(%%"REG_D") \n\t"
1207 "movq %%mm7, %%mm4 \n\t"
1209 "paddw "DCTSIZE_S"*2*2(%%"REG_D"), %%mm7 \n\t"
1210 "psubw %%mm2, %%mm4 \n\t"
1212 "paddw "DCTSIZE_S"*5*2(%%"REG_D"), %%mm4 \n\t"
1213 "paddw %%mm2, %%mm7 \n\t"
1215 "movq %%mm3, "DCTSIZE_S"*1*2(%%"REG_D") \n\t"
1216 "paddw %%mm2, %%mm0 \n\t" //'t4
1218 // 't4 't6 't5 - - - - 't7
1219 "movq %%mm7, "DCTSIZE_S"*2*2(%%"REG_D") \n\t"
1220 "movq %%mm6, %%mm1 \n\t"
1222 "paddw "DCTSIZE_S"*4*2(%%"REG_D"), %%mm6 \n\t"
1223 "psubw %%mm0, %%mm1 \n\t"
1225 "paddw "DCTSIZE_S"*3*2(%%"REG_D"), %%mm1 \n\t"
1226 "paddw %%mm0, %%mm6 \n\t"
1228 "movq %%mm4, "DCTSIZE_S"*5*2(%%"REG_D") \n\t"
1229 "add $8, %%"REG_S" \n\t"
1231 "movq %%mm6, "DCTSIZE_S"*4*2(%%"REG_D") \n\t"
1233 "movq %%mm1, "DCTSIZE_S"*3*2(%%"REG_D") \n\t"
1234 "add $8, %%"REG_D" \n\t"
1237 //=part 2 (the same)===========================================================
1238 "movq "DCTSIZE_S"*0*2(%%"REG_S"), %%mm1 \n\t"
1240 "movq "DCTSIZE_S"*3*2(%%"REG_S"), %%mm7 \n\t"
1241 "movq %%mm1, %%mm0 \n\t"
1243 "paddw "DCTSIZE_S"*7*2(%%"REG_S"), %%mm1 \n\t" //t0
1244 "movq %%mm7, %%mm3 \n\t"
1246 "paddw "DCTSIZE_S"*4*2(%%"REG_S"), %%mm7 \n\t" //t3
1247 "movq %%mm1, %%mm5 \n\t"
1249 "movq "DCTSIZE_S"*1*2(%%"REG_S"), %%mm6 \n\t"
1250 "psubw %%mm7, %%mm1 \n\t" //t13
1252 "movq "DCTSIZE_S"*2*2(%%"REG_S"), %%mm2 \n\t"
1253 "movq %%mm6, %%mm4 \n\t"
1255 "paddw "DCTSIZE_S"*6*2(%%"REG_S"), %%mm6 \n\t" //t1
1256 "paddw %%mm7, %%mm5 \n\t" //t10
1258 "paddw "DCTSIZE_S"*5*2(%%"REG_S"), %%mm2 \n\t" //t2
1259 "movq %%mm6, %%mm7 \n\t"
1261 "paddw %%mm2, %%mm6 \n\t" //t11
1262 "psubw %%mm2, %%mm7 \n\t" //t12
1264 "movq %%mm5, %%mm2 \n\t"
1265 "paddw %%mm6, %%mm5 \n\t" //d0
1266 // i0 t13 t12 i3 i1 d0 - d4
1267 "psubw %%mm6, %%mm2 \n\t" //d4
1268 "paddw %%mm1, %%mm7 \n\t"
1270 "movq 1*8+4*16(%%"REG_d"), %%mm6 \n\t"
1271 "psllw $2, %%mm7 \n\t"
1273 "psubw 1*8+0*16(%%"REG_d"), %%mm5 \n\t"
1274 "psubw %%mm6, %%mm2 \n\t"
1276 "paddusw 1*8+0*16(%%"REG_d"), %%mm5 \n\t"
1277 "paddusw %%mm6, %%mm2 \n\t"
1279 "pmulhw "MANGLE(ff_MM_FIX_0_707106781)", %%mm7 \n\t"
1281 "paddw 1*8+0*16(%%"REG_d"), %%mm5 \n\t"
1282 "paddw %%mm6, %%mm2 \n\t"
1284 "psubusw 1*8+0*16(%%"REG_d"), %%mm5 \n\t"
1285 "psubusw %%mm6, %%mm2 \n\t"
1287 //This func is totally compute-bound, operates at huge speed. So, DC shortcut
1288 // at this place isn't worthwhile due to BTB miss penalty (checked on Pent. 3).
1289 //However, typical numbers: nondc - 29%%, dc - 46%%, zero - 25%%. All <> 0 case is very rare.
1290 "paddw "MANGLE(MM_2)", %%mm5 \n\t"
1291 "movq %%mm2, %%mm6 \n\t"
1293 "paddw %%mm5, %%mm2 \n\t"
1294 "psubw %%mm6, %%mm5 \n\t"
1296 "movq %%mm1, %%mm6 \n\t"
1297 "paddw %%mm7, %%mm1 \n\t" //d2
1299 "psubw 1*8+2*16(%%"REG_d"), %%mm1 \n\t"
1300 "psubw %%mm7, %%mm6 \n\t" //d6
1302 "movq 1*8+6*16(%%"REG_d"), %%mm7 \n\t"
1303 "psraw $2, %%mm5 \n\t"
1305 "paddusw 1*8+2*16(%%"REG_d"), %%mm1 \n\t"
1306 "psubw %%mm7, %%mm6 \n\t"
1307 // t7 d2 /t11 t4 t6 - d6 /t10
1309 "paddw 1*8+2*16(%%"REG_d"), %%mm1 \n\t"
1310 "paddusw %%mm7, %%mm6 \n\t"
1312 "psubusw 1*8+2*16(%%"REG_d"), %%mm1 \n\t"
1313 "paddw %%mm7, %%mm6 \n\t"
1315 "psubw "DCTSIZE_S"*4*2(%%"REG_S"), %%mm3 \n\t"
1316 "psubusw %%mm7, %%mm6 \n\t"
1318 //movq [edi+"DCTSIZE_S"*2*2], mm1
1319 //movq [edi+"DCTSIZE_S"*6*2], mm6
1320 "movq %%mm1, %%mm7 \n\t"
1321 "psraw $2, %%mm2 \n\t"
1323 "psubw "DCTSIZE_S"*6*2(%%"REG_S"), %%mm4 \n\t"
1324 "psubw %%mm6, %%mm1 \n\t"
1326 "psubw "DCTSIZE_S"*7*2(%%"REG_S"), %%mm0 \n\t"
1327 "paddw %%mm7, %%mm6 \n\t" //'t13
1329 "psraw $2, %%mm6 \n\t" //paddw mm6, MM_2 !! ---
1330 "movq %%mm2, %%mm7 \n\t"
1332 "pmulhw "MANGLE(MM_FIX_1_414213562_A)", %%mm1 \n\t"
1333 "paddw %%mm6, %%mm2 \n\t" //'t0
1335 "movq %%mm2, 0*8+%3 \n\t" //!
1336 "psubw %%mm6, %%mm7 \n\t" //'t3
1338 "movq "DCTSIZE_S"*2*2(%%"REG_S"), %%mm2 \n\t"
1339 "psubw %%mm6, %%mm1 \n\t" //'t12
1341 "psubw "DCTSIZE_S"*5*2(%%"REG_S"), %%mm2 \n\t" //t5
1342 "movq %%mm5, %%mm6 \n\t"
1344 "movq %%mm7, 3*8+%3 \n\t"
1345 "paddw %%mm2, %%mm3 \n\t" //t10
1347 "paddw %%mm4, %%mm2 \n\t" //t11
1348 "paddw %%mm0, %%mm4 \n\t" //t12
1350 "movq %%mm3, %%mm7 \n\t"
1351 "psubw %%mm4, %%mm3 \n\t"
1353 "psllw $2, %%mm3 \n\t"
1354 "psllw $2, %%mm7 \n\t" //opt for P6
1356 "pmulhw "MANGLE(MM_FIX_0_382683433)", %%mm3 \n\t"
1357 "psllw $2, %%mm4 \n\t"
1359 "pmulhw "MANGLE(ff_MM_FIX_0_541196100)", %%mm7 \n\t"
1360 "psllw $2, %%mm2 \n\t"
1362 "pmulhw "MANGLE(MM_FIX_1_306562965)", %%mm4 \n\t"
1363 "paddw %%mm1, %%mm5 \n\t" //'t1
1365 "pmulhw "MANGLE(ff_MM_FIX_0_707106781)", %%mm2 \n\t"
1366 "psubw %%mm1, %%mm6 \n\t" //'t2
1367 // t7 't12 't11 t4 t6 - 't13 't10 ---
1369 "paddw %%mm3, %%mm7 \n\t" //z2
1371 "movq %%mm5, 1*8+%3 \n\t"
1372 "paddw %%mm3, %%mm4 \n\t" //z4
1374 "movq 1*8+3*16(%%"REG_d"), %%mm3 \n\t"
1375 "movq %%mm0, %%mm1 \n\t"
1377 "movq %%mm6, 2*8+%3 \n\t"
1378 "psubw %%mm2, %%mm1 \n\t" //z13
1381 "paddw %%mm2, %%mm0 \n\t" //z11
1382 "movq %%mm1, %%mm5 \n\t"
1384 "movq 1*8+5*16(%%"REG_d"), %%mm2 \n\t"
1385 "psubw %%mm7, %%mm1 \n\t" //d3
1387 "paddw %%mm7, %%mm5 \n\t" //d5
1388 "psubw %%mm3, %%mm1 \n\t"
1390 "movq 1*8+1*16(%%"REG_d"), %%mm7 \n\t"
1391 "psubw %%mm2, %%mm5 \n\t"
1393 "movq %%mm0, %%mm6 \n\t"
1394 "paddw %%mm4, %%mm0 \n\t" //d1
1396 "paddusw %%mm3, %%mm1 \n\t"
1397 "psubw %%mm4, %%mm6 \n\t" //d7
1399 // d1 d3 - - - d5 d7 -
1400 "movq 1*8+7*16(%%"REG_d"), %%mm4 \n\t"
1401 "psubw %%mm7, %%mm0 \n\t"
1403 "psubw %%mm4, %%mm6 \n\t"
1404 "paddusw %%mm2, %%mm5 \n\t"
1406 "paddusw %%mm4, %%mm6 \n\t"
1407 "paddw %%mm3, %%mm1 \n\t"
1409 "paddw %%mm2, %%mm5 \n\t"
1410 "paddw %%mm4, %%mm6 \n\t"
1412 "psubusw %%mm3, %%mm1 \n\t"
1413 "psubusw %%mm2, %%mm5 \n\t"
1415 "psubusw %%mm4, %%mm6 \n\t"
1416 "movq %%mm1, %%mm4 \n\t"
1418 "por %%mm5, %%mm4 \n\t"
1419 "paddusw %%mm7, %%mm0 \n\t"
1421 "por %%mm6, %%mm4 \n\t"
1422 "paddw %%mm7, %%mm0 \n\t"
1424 "packssdw %%mm4, %%mm4 \n\t"
1425 "psubusw %%mm7, %%mm0 \n\t"
1427 "movd %%mm4, %%"REG_a" \n\t"
1428 "or %%"REG_a", %%"REG_a" \n\t"
1430 //movq [edi+"DCTSIZE_S"*3*2], mm1
1431 //movq [edi+"DCTSIZE_S"*5*2], mm5
1432 //movq [edi+"DCTSIZE_S"*1*2], mm0
1433 //movq [edi+"DCTSIZE_S"*7*2], mm6
1434 // t4 t5 - - - t6 t7 -
1435 //--- t4 (mm0) may be <>0; mm1, mm5, mm6 == 0
1436 //Typical numbers: nondc - 19%%, dc - 26%%, zero - 55%%. zero case alone isn't worthwhile
1437 "movq 0*8+%3, %%mm4 \n\t"
1438 "movq %%mm0, %%mm1 \n\t"
1440 "pmulhw "MANGLE(MM_FIX_0_847759065)", %%mm0 \n\t" //tmp6
1441 "movq %%mm1, %%mm2 \n\t"
1443 "movq "DCTSIZE_S"*0*2(%%"REG_D"), %%mm5 \n\t"
1444 "movq %%mm2, %%mm3 \n\t"
1446 "pmulhw "MANGLE(MM_FIX_0_566454497)", %%mm1 \n\t" //tmp5
1447 "paddw %%mm4, %%mm5 \n\t"
1449 "movq 1*8+%3, %%mm6 \n\t"
1451 "psraw $2, %%mm3 \n\t" //tmp7
1453 "pmulhw "MANGLE(MM_FIX_0_198912367)", %%mm2 \n\t" //-tmp4
1454 "psubw %%mm3, %%mm4 \n\t"
1456 "movq "DCTSIZE_S"*1*2(%%"REG_D"), %%mm7 \n\t"
1457 "paddw %%mm3, %%mm5 \n\t"
1459 "movq %%mm4, "DCTSIZE_S"*7*2(%%"REG_D") \n\t"
1460 "paddw %%mm6, %%mm7 \n\t"
1462 "movq 2*8+%3, %%mm3 \n\t"
1463 "psubw %%mm0, %%mm6 \n\t"
1465 "movq "DCTSIZE_S"*2*2(%%"REG_D"), %%mm4 \n\t"
1466 "paddw %%mm0, %%mm7 \n\t"
1468 "movq %%mm5, "DCTSIZE_S"*0*2(%%"REG_D") \n\t"
1469 "paddw %%mm3, %%mm4 \n\t"
1471 "movq %%mm6, "DCTSIZE_S"*6*2(%%"REG_D") \n\t"
1472 "psubw %%mm1, %%mm3 \n\t"
1474 "movq "DCTSIZE_S"*5*2(%%"REG_D"), %%mm5 \n\t"
1475 "paddw %%mm1, %%mm4 \n\t"
1477 "movq "DCTSIZE_S"*3*2(%%"REG_D"), %%mm6 \n\t"
1478 "paddw %%mm3, %%mm5 \n\t"
1480 "movq 3*8+%3, %%mm0 \n\t"
1481 "add $24, %%"REG_S" \n\t"
1483 "movq %%mm7, "DCTSIZE_S"*1*2(%%"REG_D") \n\t"
1484 "paddw %%mm0, %%mm6 \n\t"
1486 "movq %%mm4, "DCTSIZE_S"*2*2(%%"REG_D") \n\t"
1487 "psubw %%mm2, %%mm0 \n\t"
1489 "movq "DCTSIZE_S"*4*2(%%"REG_D"), %%mm7 \n\t"
1490 "paddw %%mm2, %%mm6 \n\t"
1492 "movq %%mm5, "DCTSIZE_S"*5*2(%%"REG_D") \n\t"
1493 "paddw %%mm0, %%mm7 \n\t"
1495 "movq %%mm6, "DCTSIZE_S"*3*2(%%"REG_D") \n\t"
1497 "movq %%mm7, "DCTSIZE_S"*4*2(%%"REG_D") \n\t"
1498 "add $24, %%"REG_D" \n\t"
1499 "sub $2, %%"REG_c" \n\t"
1505 //psraw mm1, 2 w/o it -> offset. thr1, thr1, thr1 (actually thr1, thr1, thr1-1)
1509 "movq %%mm5, %%mm3 \n\t"
1510 "psubw %%mm1, %%mm5 \n\t"
1512 "psllw $1, %%mm5 \n\t" //'z10
1513 "paddw %%mm1, %%mm3 \n\t" //'z13
1515 "movq %%mm0, %%mm2 \n\t"
1516 "psubw %%mm6, %%mm0 \n\t"
1518 "movq %%mm5, %%mm1 \n\t"
1519 "psllw $1, %%mm0 \n\t" //'z12
1521 "pmulhw "MANGLE(MM_FIX_2_613125930)", %%mm1 \n\t" //-
1522 "paddw %%mm0, %%mm5 \n\t"
1524 "pmulhw "MANGLE(MM_FIX_1_847759065)", %%mm5 \n\t" //'z5
1525 "paddw %%mm6, %%mm2 \n\t" //'z11
1527 "pmulhw "MANGLE(MM_FIX_1_082392200)", %%mm0 \n\t"
1528 "movq %%mm2, %%mm7 \n\t"
1531 "movq 0*8+%3, %%mm4 \n\t"
1532 "psubw %%mm3, %%mm2 \n\t"
1534 "psllw $1, %%mm2 \n\t"
1535 "paddw %%mm3, %%mm7 \n\t" //'t7
1537 "pmulhw "MANGLE(MM_FIX_1_414213562)", %%mm2 \n\t" //'t11
1538 "movq %%mm4, %%mm6 \n\t"
1540 "psraw $2, %%mm7 \n\t"
1542 "paddw "DCTSIZE_S"*0*2(%%"REG_D"), %%mm4 \n\t"
1543 "psubw %%mm7, %%mm6 \n\t"
1545 "movq 1*8+%3, %%mm3 \n\t"
1546 "paddw %%mm7, %%mm4 \n\t"
1548 "movq %%mm6, "DCTSIZE_S"*7*2(%%"REG_D") \n\t"
1549 "paddw %%mm5, %%mm1 \n\t" //'t12
1551 "movq %%mm4, "DCTSIZE_S"*0*2(%%"REG_D") \n\t"
1552 "psubw %%mm7, %%mm1 \n\t" //'t6
1554 "movq 2*8+%3, %%mm7 \n\t"
1555 "psubw %%mm5, %%mm0 \n\t" //'t10
1557 "movq 3*8+%3, %%mm6 \n\t"
1558 "movq %%mm3, %%mm5 \n\t"
1560 "paddw "DCTSIZE_S"*1*2(%%"REG_D"), %%mm3 \n\t"
1561 "psubw %%mm1, %%mm5 \n\t"
1563 "psubw %%mm1, %%mm2 \n\t" //'t5
1564 "paddw %%mm1, %%mm3 \n\t"
1566 "movq %%mm5, "DCTSIZE_S"*6*2(%%"REG_D") \n\t"
1567 "movq %%mm7, %%mm4 \n\t"
1569 "paddw "DCTSIZE_S"*2*2(%%"REG_D"), %%mm7 \n\t"
1570 "psubw %%mm2, %%mm4 \n\t"
1572 "paddw "DCTSIZE_S"*5*2(%%"REG_D"), %%mm4 \n\t"
1573 "paddw %%mm2, %%mm7 \n\t"
1575 "movq %%mm3, "DCTSIZE_S"*1*2(%%"REG_D") \n\t"
1576 "paddw %%mm2, %%mm0 \n\t" //'t4
1578 // 't4 't6 't5 - - - - 't7
1579 "movq %%mm7, "DCTSIZE_S"*2*2(%%"REG_D") \n\t"
1580 "movq %%mm6, %%mm1 \n\t"
1582 "paddw "DCTSIZE_S"*4*2(%%"REG_D"), %%mm6 \n\t"
1583 "psubw %%mm0, %%mm1 \n\t"
1585 "paddw "DCTSIZE_S"*3*2(%%"REG_D"), %%mm1 \n\t"
1586 "paddw %%mm0, %%mm6 \n\t"
1588 "movq %%mm4, "DCTSIZE_S"*5*2(%%"REG_D") \n\t"
1589 "add $24, %%"REG_S" \n\t"
1591 "movq %%mm6, "DCTSIZE_S"*4*2(%%"REG_D") \n\t"
1593 "movq %%mm1, "DCTSIZE_S"*3*2(%%"REG_D") \n\t"
1594 "add $24, %%"REG_D" \n\t"
1595 "sub $2, %%"REG_c" \n\t"
1599 : "+S"(data), "+D"(output), "+c"(cnt), "=o"(temps)
1601 NAMED_CONSTRAINTS_ADD(ff_MM_FIX_0_707106781,MM_2,MM_FIX_1_414213562_A,MM_FIX_1_414213562,MM_FIX_0_382683433,
1602 ff_MM_FIX_0_541196100,MM_FIX_1_306562965,MM_FIX_0_847759065)
1603 NAMED_CONSTRAINTS_ADD(MM_FIX_0_566454497,MM_FIX_0_198912367,MM_FIX_2_613125930,MM_FIX_1_847759065,
1613 static void row_idct_c(int16_t* workspace,
1614 int16_t* output_adr, int output_stride, int cnt)
1616 int_simd16_t tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
1617 int_simd16_t tmp10, tmp11, tmp12, tmp13;
1618 int_simd16_t z5, z10, z11, z12, z13;
1624 outptr = output_adr;
1625 for (; cnt > 0; cnt--) {
1627 //Simd version reads 4x4 block and transposes it
1628 tmp10 = ( wsptr[2] + wsptr[3]);
1629 tmp11 = ( wsptr[2] - wsptr[3]);
1631 tmp13 = ( wsptr[0] + wsptr[1]);
1632 tmp12 = (MULTIPLY16H( wsptr[0] - wsptr[1], FIX_1_414213562_A)<<2) - tmp13;//this shift order to avoid overflow
1634 tmp0 = tmp10 + tmp13; //->temps
1635 tmp3 = tmp10 - tmp13; //->temps
1636 tmp1 = tmp11 + tmp12;
1637 tmp2 = tmp11 - tmp12;
1640 //Also transpose, with previous:
1642 // ---- ---- idct ||||
1643 // ---- ---- ---> ||||
1645 z13 = wsptr[4] + wsptr[5];
1646 z10 = wsptr[4] - wsptr[5];
1647 z11 = wsptr[6] + wsptr[7];
1648 z12 = wsptr[6] - wsptr[7];
1651 tmp11 = MULTIPLY16H(z11 - z13, FIX_1_414213562);
1653 z5 = MULTIPLY16H(z10 + z12, FIX_1_847759065);
1654 tmp10 = MULTIPLY16H(z12, FIX_1_082392200) - z5;
1655 tmp12 = MULTIPLY16H(z10, FIX_2_613125930) + z5; // - FIX_
1657 tmp6 = (tmp12<<3) - tmp7;
1658 tmp5 = (tmp11<<3) - tmp6;
1659 tmp4 = (tmp10<<3) + tmp5;
1661 // Final output stage: descale and write column
1662 outptr[0*output_stride]+= DESCALE(tmp0 + tmp7, 3);
1663 outptr[1*output_stride]+= DESCALE(tmp1 + tmp6, 3);
1664 outptr[2*output_stride]+= DESCALE(tmp2 + tmp5, 3);
1665 outptr[3*output_stride]+= DESCALE(tmp3 - tmp4, 3);
1666 outptr[4*output_stride]+= DESCALE(tmp3 + tmp4, 3);
1667 outptr[5*output_stride]+= DESCALE(tmp2 - tmp5, 3);
1668 outptr[6*output_stride]+= DESCALE(tmp1 - tmp6, 3); //no += ?
1669 outptr[7*output_stride]+= DESCALE(tmp0 - tmp7, 3); //no += ?
1672 wsptr += DCTSIZE; // advance pointer to next row
1676 #else /* HAVE_MMX */
1678 static void row_idct_mmx (int16_t* workspace,
1679 int16_t* output_adr, int output_stride, int cnt)
1681 DECLARE_ALIGNED(8, uint64_t, temps)[4];
1683 "lea (%%"REG_a",%%"REG_a",2), %%"REG_d" \n\t"
1685 "movq "DCTSIZE_S"*0*2(%%"REG_S"), %%mm0 \n\t"
1688 "movq "DCTSIZE_S"*1*2(%%"REG_S"), %%mm1 \n\t"
1689 "movq %%mm0, %%mm4 \n\t"
1691 "movq "DCTSIZE_S"*2*2(%%"REG_S"), %%mm2 \n\t"
1692 "punpcklwd %%mm1, %%mm0 \n\t"
1694 "movq "DCTSIZE_S"*3*2(%%"REG_S"), %%mm3 \n\t"
1695 "punpckhwd %%mm1, %%mm4 \n\t"
1698 "movq %%mm2, %%mm7 \n\t"
1699 "punpcklwd %%mm3, %%mm2 \n\t"
1701 "movq %%mm0, %%mm6 \n\t"
1702 "punpckldq %%mm2, %%mm0 \n\t" //0
1704 "punpckhdq %%mm2, %%mm6 \n\t" //1
1705 "movq %%mm0, %%mm5 \n\t"
1707 "punpckhwd %%mm3, %%mm7 \n\t"
1708 "psubw %%mm6, %%mm0 \n\t"
1710 "pmulhw "MANGLE(MM_FIX_1_414213562_A)", %%mm0 \n\t"
1711 "movq %%mm4, %%mm2 \n\t"
1713 "punpckldq %%mm7, %%mm4 \n\t" //2
1714 "paddw %%mm6, %%mm5 \n\t"
1716 "punpckhdq %%mm7, %%mm2 \n\t" //3
1717 "movq %%mm4, %%mm1 \n\t"
1719 "psllw $2, %%mm0 \n\t"
1720 "paddw %%mm2, %%mm4 \n\t" //t10
1722 "movq "DCTSIZE_S"*0*2+"DCTSIZE_S"(%%"REG_S"), %%mm3 \n\t"
1723 "psubw %%mm2, %%mm1 \n\t" //t11
1725 "movq "DCTSIZE_S"*1*2+"DCTSIZE_S"(%%"REG_S"), %%mm2 \n\t"
1726 "psubw %%mm5, %%mm0 \n\t"
1728 "movq %%mm4, %%mm6 \n\t"
1729 "paddw %%mm5, %%mm4 \n\t" //t0
1731 "psubw %%mm5, %%mm6 \n\t" //t3
1732 "movq %%mm1, %%mm7 \n\t"
1734 "movq "DCTSIZE_S"*2*2+"DCTSIZE_S"(%%"REG_S"), %%mm5 \n\t"
1735 "paddw %%mm0, %%mm1 \n\t" //t1
1737 "movq %%mm4, 0*8+%3 \n\t" //t0
1738 "movq %%mm3, %%mm4 \n\t"
1740 "movq %%mm6, 1*8+%3 \n\t" //t3
1741 "punpcklwd %%mm2, %%mm3 \n\t"
1744 "movq "DCTSIZE_S"*3*2+"DCTSIZE_S"(%%"REG_S"), %%mm6 \n\t"
1745 "punpckhwd %%mm2, %%mm4 \n\t"
1747 "movq %%mm5, %%mm2 \n\t"
1748 "punpcklwd %%mm6, %%mm5 \n\t"
1750 "psubw %%mm0, %%mm7 \n\t" //t2
1751 "punpckhwd %%mm6, %%mm2 \n\t"
1753 "movq %%mm3, %%mm0 \n\t"
1754 "punpckldq %%mm5, %%mm3 \n\t" //4
1756 "punpckhdq %%mm5, %%mm0 \n\t" //5
1757 "movq %%mm4, %%mm5 \n\t"
1760 "movq %%mm3, %%mm6 \n\t"
1761 "punpckldq %%mm2, %%mm4 \n\t" //6
1763 "psubw %%mm0, %%mm3 \n\t" //z10
1764 "punpckhdq %%mm2, %%mm5 \n\t" //7
1766 "paddw %%mm0, %%mm6 \n\t" //z13
1767 "movq %%mm4, %%mm2 \n\t"
1769 "movq %%mm3, %%mm0 \n\t"
1770 "psubw %%mm5, %%mm4 \n\t" //z12
1772 "pmulhw "MANGLE(MM_FIX_2_613125930)", %%mm0 \n\t" //-
1773 "paddw %%mm4, %%mm3 \n\t"
1775 "pmulhw "MANGLE(MM_FIX_1_847759065)", %%mm3 \n\t" //z5
1776 "paddw %%mm5, %%mm2 \n\t" //z11 >
1778 "pmulhw "MANGLE(MM_FIX_1_082392200)", %%mm4 \n\t"
1779 "movq %%mm2, %%mm5 \n\t"
1781 "psubw %%mm6, %%mm2 \n\t"
1782 "paddw %%mm6, %%mm5 \n\t" //t7
1784 "pmulhw "MANGLE(MM_FIX_1_414213562)", %%mm2 \n\t" //t11
1785 "paddw %%mm3, %%mm0 \n\t" //t12
1787 "psllw $3, %%mm0 \n\t"
1788 "psubw %%mm3, %%mm4 \n\t" //t10
1790 "movq 0*8+%3, %%mm6 \n\t"
1791 "movq %%mm1, %%mm3 \n\t"
1793 "psllw $3, %%mm4 \n\t"
1794 "psubw %%mm5, %%mm0 \n\t" //t6
1796 "psllw $3, %%mm2 \n\t"
1797 "paddw %%mm0, %%mm1 \n\t" //d1
1799 "psubw %%mm0, %%mm2 \n\t" //t5
1800 "psubw %%mm0, %%mm3 \n\t" //d6
1802 "paddw %%mm2, %%mm4 \n\t" //t4
1803 "movq %%mm7, %%mm0 \n\t"
1805 "paddw %%mm2, %%mm7 \n\t" //d2
1806 "psubw %%mm2, %%mm0 \n\t" //d5
1808 "movq "MANGLE(MM_DESCALE_RND)", %%mm2 \n\t" //4
1809 "psubw %%mm5, %%mm6 \n\t" //d7
1811 "paddw 0*8+%3, %%mm5 \n\t" //d0
1812 "paddw %%mm2, %%mm1 \n\t"
1814 "paddw %%mm2, %%mm5 \n\t"
1815 "psraw $3, %%mm1 \n\t"
1817 "paddw %%mm2, %%mm7 \n\t"
1818 "psraw $3, %%mm5 \n\t"
1820 "paddw (%%"REG_D"), %%mm5 \n\t"
1821 "psraw $3, %%mm7 \n\t"
1823 "paddw (%%"REG_D",%%"REG_a"), %%mm1 \n\t"
1824 "paddw %%mm2, %%mm0 \n\t"
1826 "paddw (%%"REG_D",%%"REG_a",2), %%mm7 \n\t"
1827 "paddw %%mm2, %%mm3 \n\t"
1829 "movq %%mm5, (%%"REG_D") \n\t"
1830 "paddw %%mm2, %%mm6 \n\t"
1832 "movq %%mm1, (%%"REG_D",%%"REG_a") \n\t"
1833 "psraw $3, %%mm0 \n\t"
1835 "movq %%mm7, (%%"REG_D",%%"REG_a",2) \n\t"
1836 "add %%"REG_d", %%"REG_D" \n\t" //3*ls
1838 "movq 1*8+%3, %%mm5 \n\t" //t3
1839 "psraw $3, %%mm3 \n\t"
1841 "paddw (%%"REG_D",%%"REG_a",2), %%mm0 \n\t"
1842 "psubw %%mm4, %%mm5 \n\t" //d3
1844 "paddw (%%"REG_D",%%"REG_d"), %%mm3 \n\t"
1845 "psraw $3, %%mm6 \n\t"
1847 "paddw 1*8+%3, %%mm4 \n\t" //d4
1848 "paddw %%mm2, %%mm5 \n\t"
1850 "paddw (%%"REG_D",%%"REG_a",4), %%mm6 \n\t"
1851 "paddw %%mm2, %%mm4 \n\t"
1853 "movq %%mm0, (%%"REG_D",%%"REG_a",2) \n\t"
1854 "psraw $3, %%mm5 \n\t"
1856 "paddw (%%"REG_D"), %%mm5 \n\t"
1857 "psraw $3, %%mm4 \n\t"
1859 "paddw (%%"REG_D",%%"REG_a"), %%mm4 \n\t"
1860 "add $"DCTSIZE_S"*2*4, %%"REG_S" \n\t" //4 rows
1862 "movq %%mm3, (%%"REG_D",%%"REG_d") \n\t"
1863 "movq %%mm6, (%%"REG_D",%%"REG_a",4) \n\t"
1864 "movq %%mm5, (%%"REG_D") \n\t"
1865 "movq %%mm4, (%%"REG_D",%%"REG_a") \n\t"
1867 "sub %%"REG_d", %%"REG_D" \n\t"
1868 "add $8, %%"REG_D" \n\t"
1869 "dec %%"REG_c" \n\t"
1872 : "+S"(workspace), "+D"(output_adr), "+c"(cnt), "=o"(temps)
1873 : "a"(output_stride*sizeof(short))
1874 NAMED_CONSTRAINTS_ADD(MM_FIX_1_414213562_A,MM_FIX_2_613125930,MM_FIX_1_847759065,MM_FIX_1_082392200,
1875 MM_FIX_1_414213562,MM_DESCALE_RND)
1884 static void row_fdct_c(int16_t *data, const uint8_t *pixels, int line_size, int cnt)
1886 int_simd16_t tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
1887 int_simd16_t tmp10, tmp11, tmp12, tmp13;
1888 int_simd16_t z1, z2, z3, z4, z5, z11, z13;
1892 // Pass 1: process rows.
1895 for (; cnt > 0; cnt--) {
1896 tmp0 = pixels[line_size*0] + pixels[line_size*7];
1897 tmp7 = pixels[line_size*0] - pixels[line_size*7];
1898 tmp1 = pixels[line_size*1] + pixels[line_size*6];
1899 tmp6 = pixels[line_size*1] - pixels[line_size*6];
1900 tmp2 = pixels[line_size*2] + pixels[line_size*5];
1901 tmp5 = pixels[line_size*2] - pixels[line_size*5];
1902 tmp3 = pixels[line_size*3] + pixels[line_size*4];
1903 tmp4 = pixels[line_size*3] - pixels[line_size*4];
1907 tmp10 = tmp0 + tmp3;
1908 tmp13 = tmp0 - tmp3;
1909 tmp11 = tmp1 + tmp2;
1910 tmp12 = tmp1 - tmp2;
1911 //Even columns are written first, this leads to different order of columns
1912 //in column_fidct(), but they are processed independently, so all ok.
1913 //Later in the row_idct() columns readed at the same order.
1914 dataptr[2] = tmp10 + tmp11;
1915 dataptr[3] = tmp10 - tmp11;
1917 z1 = MULTIPLY16H((tmp12 + tmp13)<<2, FIX_0_707106781);
1918 dataptr[0] = tmp13 + z1;
1919 dataptr[1] = tmp13 - z1;
1923 tmp10 = (tmp4 + tmp5) <<2;
1924 tmp11 = (tmp5 + tmp6) <<2;
1925 tmp12 = (tmp6 + tmp7) <<2;
1927 z5 = MULTIPLY16H(tmp10 - tmp12, FIX_0_382683433);
1928 z2 = MULTIPLY16H(tmp10, FIX_0_541196100) + z5;
1929 z4 = MULTIPLY16H(tmp12, FIX_1_306562965) + z5;
1930 z3 = MULTIPLY16H(tmp11, FIX_0_707106781);
1935 dataptr[4] = z13 + z2;
1936 dataptr[5] = z13 - z2;
1937 dataptr[6] = z11 + z4;
1938 dataptr[7] = z11 - z4;
1940 pixels++; // advance pointer to next column
1945 #else /* HAVE_MMX */
1947 static void row_fdct_mmx(int16_t *data, const uint8_t *pixels, int line_size, int cnt)
1949 DECLARE_ALIGNED(8, uint64_t, temps)[4];
1951 "lea (%%"REG_a",%%"REG_a",2), %%"REG_d" \n\t"
1953 "movd (%%"REG_S"), %%mm0 \n\t"
1954 "pxor %%mm7, %%mm7 \n\t"
1956 "movd (%%"REG_S",%%"REG_a"), %%mm1 \n\t"
1957 "punpcklbw %%mm7, %%mm0 \n\t"
1959 "movd (%%"REG_S",%%"REG_a",2), %%mm2 \n\t"
1960 "punpcklbw %%mm7, %%mm1 \n\t"
1962 "punpcklbw %%mm7, %%mm2 \n\t"
1963 "add %%"REG_d", %%"REG_S" \n\t"
1965 "movq %%mm0, %%mm5 \n\t"
1968 "movd (%%"REG_S",%%"REG_a",4), %%mm3 \n\t" //7 ;prefetch!
1969 "movq %%mm1, %%mm6 \n\t"
1971 "movd (%%"REG_S",%%"REG_d"), %%mm4 \n\t" //6
1972 "punpcklbw %%mm7, %%mm3 \n\t"
1974 "psubw %%mm3, %%mm5 \n\t"
1975 "punpcklbw %%mm7, %%mm4 \n\t"
1977 "paddw %%mm3, %%mm0 \n\t"
1978 "psubw %%mm4, %%mm6 \n\t"
1980 "movd (%%"REG_S",%%"REG_a",2), %%mm3 \n\t" //5
1981 "paddw %%mm4, %%mm1 \n\t"
1983 "movq %%mm5, %3 \n\t" //t7
1984 "punpcklbw %%mm7, %%mm3 \n\t"
1986 "movq %%mm6, %4 \n\t" //t6
1987 "movq %%mm2, %%mm4 \n\t"
1989 "movd (%%"REG_S"), %%mm5 \n\t" //3
1990 "paddw %%mm3, %%mm2 \n\t"
1992 "movd (%%"REG_S",%%"REG_a"), %%mm6 \n\t" //4
1993 "punpcklbw %%mm7, %%mm5 \n\t"
1995 "psubw %%mm3, %%mm4 \n\t"
1996 "punpcklbw %%mm7, %%mm6 \n\t"
1998 "movq %%mm5, %%mm3 \n\t"
1999 "paddw %%mm6, %%mm5 \n\t" //t3
2001 "psubw %%mm6, %%mm3 \n\t" //t4 ; t0 t1 t2 t4 t5 t3 - -
2002 "movq %%mm0, %%mm6 \n\t"
2004 "movq %%mm1, %%mm7 \n\t"
2005 "psubw %%mm5, %%mm0 \n\t" //t13
2007 "psubw %%mm2, %%mm1 \n\t"
2008 "paddw %%mm2, %%mm7 \n\t" //t11
2010 "paddw %%mm0, %%mm1 \n\t"
2011 "movq %%mm7, %%mm2 \n\t"
2013 "psllw $2, %%mm1 \n\t"
2014 "paddw %%mm5, %%mm6 \n\t" //t10
2016 "pmulhw "MANGLE(ff_MM_FIX_0_707106781)", %%mm1 \n\t"
2017 "paddw %%mm6, %%mm7 \n\t" //d2
2019 "psubw %%mm2, %%mm6 \n\t" //d3
2020 "movq %%mm0, %%mm5 \n\t"
2023 "movq %%mm7, %%mm2 \n\t"
2024 "punpcklwd %%mm6, %%mm7 \n\t"
2026 "paddw %%mm1, %%mm0 \n\t" //d0
2027 "punpckhwd %%mm6, %%mm2 \n\t"
2029 "psubw %%mm1, %%mm5 \n\t" //d1
2030 "movq %%mm0, %%mm6 \n\t"
2032 "movq %4, %%mm1 \n\t"
2033 "punpcklwd %%mm5, %%mm0 \n\t"
2035 "punpckhwd %%mm5, %%mm6 \n\t"
2036 "movq %%mm0, %%mm5 \n\t"
2038 "punpckldq %%mm7, %%mm0 \n\t" //0
2039 "paddw %%mm4, %%mm3 \n\t"
2041 "punpckhdq %%mm7, %%mm5 \n\t" //1
2042 "movq %%mm6, %%mm7 \n\t"
2044 "movq %%mm0, "DCTSIZE_S"*0*2(%%"REG_D") \n\t"
2045 "punpckldq %%mm2, %%mm6 \n\t" //2
2047 "movq %%mm5, "DCTSIZE_S"*1*2(%%"REG_D") \n\t"
2048 "punpckhdq %%mm2, %%mm7 \n\t" //3
2050 "movq %%mm6, "DCTSIZE_S"*2*2(%%"REG_D") \n\t"
2051 "paddw %%mm1, %%mm4 \n\t"
2053 "movq %%mm7, "DCTSIZE_S"*3*2(%%"REG_D") \n\t"
2054 "psllw $2, %%mm3 \n\t" //t10
2056 "movq %3, %%mm2 \n\t"
2057 "psllw $2, %%mm4 \n\t" //t11
2059 "pmulhw "MANGLE(ff_MM_FIX_0_707106781)", %%mm4 \n\t" //z3
2060 "paddw %%mm2, %%mm1 \n\t"
2062 "psllw $2, %%mm1 \n\t" //t12
2063 "movq %%mm3, %%mm0 \n\t"
2065 "pmulhw "MANGLE(ff_MM_FIX_0_541196100)", %%mm0 \n\t"
2066 "psubw %%mm1, %%mm3 \n\t"
2068 "pmulhw "MANGLE(MM_FIX_0_382683433)", %%mm3 \n\t" //z5
2069 "movq %%mm2, %%mm5 \n\t"
2071 "pmulhw "MANGLE(MM_FIX_1_306562965)", %%mm1 \n\t"
2072 "psubw %%mm4, %%mm2 \n\t" //z13
2074 "paddw %%mm4, %%mm5 \n\t" //z11
2075 "movq %%mm2, %%mm6 \n\t"
2077 "paddw %%mm3, %%mm0 \n\t" //z2
2078 "movq %%mm5, %%mm7 \n\t"
2080 "paddw %%mm0, %%mm2 \n\t" //d4
2081 "psubw %%mm0, %%mm6 \n\t" //d5
2083 "movq %%mm2, %%mm4 \n\t"
2084 "paddw %%mm3, %%mm1 \n\t" //z4
2087 "punpcklwd %%mm6, %%mm2 \n\t"
2088 "paddw %%mm1, %%mm5 \n\t" //d6
2090 "punpckhwd %%mm6, %%mm4 \n\t"
2091 "psubw %%mm1, %%mm7 \n\t" //d7
2093 "movq %%mm5, %%mm6 \n\t"
2094 "punpcklwd %%mm7, %%mm5 \n\t"
2096 "punpckhwd %%mm7, %%mm6 \n\t"
2097 "movq %%mm2, %%mm7 \n\t"
2099 "punpckldq %%mm5, %%mm2 \n\t" //4
2100 "sub %%"REG_d", %%"REG_S" \n\t"
2102 "punpckhdq %%mm5, %%mm7 \n\t" //5
2103 "movq %%mm4, %%mm5 \n\t"
2105 "movq %%mm2, "DCTSIZE_S"*0*2+"DCTSIZE_S"(%%"REG_D") \n\t"
2106 "punpckldq %%mm6, %%mm4 \n\t" //6
2108 "movq %%mm7, "DCTSIZE_S"*1*2+"DCTSIZE_S"(%%"REG_D") \n\t"
2109 "punpckhdq %%mm6, %%mm5 \n\t" //7
2111 "movq %%mm4, "DCTSIZE_S"*2*2+"DCTSIZE_S"(%%"REG_D") \n\t"
2112 "add $4, %%"REG_S" \n\t"
2114 "movq %%mm5, "DCTSIZE_S"*3*2+"DCTSIZE_S"(%%"REG_D") \n\t"
2115 "add $"DCTSIZE_S"*2*4, %%"REG_D" \n\t" //4 rows
2116 "dec %%"REG_c" \n\t"
2119 : "+S"(pixels), "+D"(data), "+c"(cnt), "=o"(temps), "=o"(temps[1])
2121 NAMED_CONSTRAINTS_ADD(ff_MM_FIX_0_707106781,ff_MM_FIX_0_541196100,MM_FIX_0_382683433,MM_FIX_1_306562965)