comparison libmpcodecs/vf_pp7.c @ 15944:1a0c715343d2

pp7 filter (spp=6 filter with 7 point dct where only the center sample is used after idct) these differences from spp lead to a few nice symmetries which significantly reduce the computational cost almost not mmx optimized (iam lazy ...)
author michael
date Sat, 09 Jul 2005 07:49:51 +0000
parents
children 08338441314b
comparison
equal deleted inserted replaced
15943:e84185e72c8f 15944:1a0c715343d2
1 /*
2 Copyright (C) 2005 Michael Niedermayer <michaelni@gmx.at>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program 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
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <inttypes.h>
24 #include <math.h>
25
26 #include "../config.h"
27
28 #include "../mp_msg.h"
29 #include "../cpudetect.h"
30
31 #ifdef HAVE_MALLOC_H
32 #include <malloc.h>
33 #endif
34
35 #include "img_format.h"
36 #include "mp_image.h"
37 #include "vf.h"
38 #include "../libvo/fastmemcpy.h"
39
40 #define XMIN(a,b) ((a) < (b) ? (a) : (b))
41
42 typedef short DCTELEM;
43
44 //===========================================================================//
45 static const uint8_t __attribute__((aligned(8))) dither[8][8]={
46 { 0, 48, 12, 60, 3, 51, 15, 63, },
47 { 32, 16, 44, 28, 35, 19, 47, 31, },
48 { 8, 56, 4, 52, 11, 59, 7, 55, },
49 { 40, 24, 36, 20, 43, 27, 39, 23, },
50 { 2, 50, 14, 62, 1, 49, 13, 61, },
51 { 34, 18, 46, 30, 33, 17, 45, 29, },
52 { 10, 58, 6, 54, 9, 57, 5, 53, },
53 { 42, 26, 38, 22, 41, 25, 37, 21, },
54 };
55
56 struct vf_priv_s {
57 int qp;
58 int mode;
59 int mpeg2;
60 int temp_stride;
61 uint8_t *src;
62 };
63 #if 0
64 static inline void dct7_c(DCTELEM *dst, int s0, int s1, int s2, int s3, int step){
65 int s, d;
66 int dst2[64];
67 //#define S0 (1024/0.37796447300922719759)
68 #define C0 ((int)(1024*0.37796447300922719759+0.5)) //sqrt(1/7)
69 #define C1 ((int)(1024*0.53452248382484879308/6+0.5)) //sqrt(2/7)/6
70
71 #define C2 ((int)(1024*0.45221175985034745004/2+0.5))
72 #define C3 ((int)(1024*0.36264567479870879474/2+0.5))
73
74 //0.1962505182412941918 0.0149276808419397944-0.2111781990832339584
75 #define C4 ((int)(1024*0.1962505182412941918+0.5))
76 #define C5 ((int)(1024*0.0149276808419397944+0.5))
77 //#define C6 ((int)(1024*0.2111781990832339584+0.5))
78 #if 0
79 s= s0 + s1 + s2;
80 dst[0*step] = ((s + s3)*C0 + 512) >> 10;
81 s= (s - 6*s3)*C1 + 512;
82 d= (s0-s2)*C4 + (s1-s2)*C5;
83 dst[1*step] = (s + 2*d)>>10;
84 s -= d;
85 d= (s1-s0)*C2 + (s1-s2)*C3;
86 dst[2*step] = (s + d)>>10;
87 dst[3*step] = (s - d)>>10;
88 #elif 1
89 s = s3+s3;
90 s3= s-s0;
91 s0= s+s0;
92 s = s2+s1;
93 s2= s2-s1;
94 dst[0*step]= s0 + s;
95 dst[2*step]= s0 - s;
96 dst[1*step]= 2*s3 + s2;
97 dst[3*step]= s3 - 2*s2;
98 #else
99 int i,j,n=7;
100 for(i=0; i<7; i+=2){
101 dst2[i*step/2]= 0;
102 for(j=0; j<4; j++)
103 dst2[i*step/2] += src[j*step] * cos(i*M_PI/n*(j+0.5)) * sqrt((i?2.0:1.0)/n);
104 if(fabs(dst2[i*step/2] - dst[i*step/2]) > 20)
105 printf("%d %d %d (%d %d %d %d) -> (%d %d %d %d)\n", i,dst2[i*step/2], dst[i*step/2],src[0*step], src[1*step], src[2*step], src[3*step], dst[0*step], dst[1*step],dst[2*step],dst[3*step]);
106 }
107 #endif
108 }
109 #endif
110
111 static inline void dctA_c(DCTELEM *dst, uint8_t *src, int stride){
112 int i;
113
114 for(i=0; i<4; i++){
115 int s0= src[0*stride] + src[6*stride];
116 int s1= src[1*stride] + src[5*stride];
117 int s2= src[2*stride] + src[4*stride];
118 int s3= src[3*stride];
119 int s= s3+s3;
120 s3= s-s0;
121 s0= s+s0;
122 s = s2+s1;
123 s2= s2-s1;
124 dst[0]= s0 + s;
125 dst[2]= s0 - s;
126 dst[1]= 2*s3 + s2;
127 dst[3]= s3 - 2*s2;
128 src++;
129 dst+=4;
130 }
131 }
132
133 static void dctB_c(DCTELEM *dst, DCTELEM *src){
134 int i;
135
136 for(i=0; i<4; i++){
137 int s0= src[0*4] + src[6*4];
138 int s1= src[1*4] + src[5*4];
139 int s2= src[2*4] + src[4*4];
140 int s3= src[3*4];
141 int s= s3+s3;
142 s3= s-s0;
143 s0= s+s0;
144 s = s2+s1;
145 s2= s2-s1;
146 dst[0*4]= s0 + s;
147 dst[2*4]= s0 - s;
148 dst[1*4]= 2*s3 + s2;
149 dst[3*4]= s3 - 2*s2;
150 src++;
151 dst++;
152 }
153 }
154
155 static void dctB_mmx(DCTELEM *dst, DCTELEM *src){
156 asm volatile (
157 "movq (%0), %%mm0 \n\t"
158 "movq 1*4*2(%0), %%mm1 \n\t"
159 "paddw 6*4*2(%0), %%mm0 \n\t"
160 "paddw 5*4*2(%0), %%mm1 \n\t"
161 "movq 2*4*2(%0), %%mm2 \n\t"
162 "movq 3*4*2(%0), %%mm3 \n\t"
163 "paddw 4*4*2(%0), %%mm2 \n\t"
164 "paddw %%mm3, %%mm3 \n\t" //s
165 "movq %%mm3, %%mm4 \n\t" //s
166 "psubw %%mm0, %%mm3 \n\t" //s-s0
167 "paddw %%mm0, %%mm4 \n\t" //s+s0
168 "movq %%mm2, %%mm0 \n\t" //s2
169 "psubw %%mm1, %%mm2 \n\t" //s2-s1
170 "paddw %%mm1, %%mm0 \n\t" //s2+s1
171 "movq %%mm4, %%mm1 \n\t" //s0'
172 "psubw %%mm0, %%mm4 \n\t" //s0'-s'
173 "paddw %%mm0, %%mm1 \n\t" //s0'+s'
174 "movq %%mm3, %%mm0 \n\t" //s3'
175 "psubw %%mm2, %%mm3 \n\t"
176 "psubw %%mm2, %%mm3 \n\t"
177 "paddw %%mm0, %%mm2 \n\t"
178 "paddw %%mm0, %%mm2 \n\t"
179 "movq %%mm1, (%1) \n\t"
180 "movq %%mm4, 2*4*2(%1) \n\t"
181 "movq %%mm2, 1*4*2(%1) \n\t"
182 "movq %%mm3, 3*4*2(%1) \n\t"
183 :: "r" (src), "r"(dst)
184 );
185 }
186
187 static void (*dctB)(DCTELEM *dst, DCTELEM *src)= dctB_c;
188
189 #define N0 4
190 #define N1 5
191 #define N2 10
192 #define SN0 2
193 #define SN1 2.2360679775
194 #define SN2 3.16227766017
195 #define N (1<<16)
196
197 static const int factor[16]={
198 N/(N0*N0), N/(N0*N1), N/(N0*N0),N/(N0*N2),
199 N/(N1*N0), N/(N1*N1), N/(N1*N0),N/(N1*N2),
200 N/(N0*N0), N/(N0*N1), N/(N0*N0),N/(N0*N2),
201 N/(N2*N0), N/(N2*N1), N/(N2*N0),N/(N2*N2),
202 };
203
204 static const int thres[16]={
205 N/(SN0*SN0), N/(SN0*SN2), N/(SN0*SN0),N/(SN0*SN2),
206 N/(SN2*SN0), N/(SN2*SN2), N/(SN2*SN0),N/(SN2*SN2),
207 N/(SN0*SN0), N/(SN0*SN2), N/(SN0*SN0),N/(SN0*SN2),
208 N/(SN2*SN0), N/(SN2*SN2), N/(SN2*SN0),N/(SN2*SN2),
209 };
210
211 static int thres2[99][16];
212
213 static void init_thres2(){
214 int qp, i;
215 int bias= 0; //FIXME
216
217 for(qp=0; qp<99; qp++){
218 for(i=0; i<16; i++){
219 thres2[qp][i]= ((i&1)?SN2:SN0) * ((i&4)?SN2:SN0) * qp * (1<<2) - 1 - bias;
220 }
221 }
222 }
223
224 static int hardthresh_c(DCTELEM *src, int qp){
225 int i;
226 int a;
227
228 a= src[0] * factor[0];
229 for(i=1; i<16; i++){
230 unsigned int threshold1= thres2[qp][i];
231 unsigned int threshold2= (threshold1<<1);
232 int level= src[i];
233 if(((unsigned)(level+threshold1))>threshold2){
234 a += level * factor[i];
235 }
236 }
237 return (a + (1<<11))>>12;
238 }
239
240 static int mediumthresh_c(DCTELEM *src, int qp){
241 int i;
242 int a;
243
244 a= src[0] * factor[0];
245 for(i=1; i<16; i++){
246 unsigned int threshold1= thres2[qp][i];
247 unsigned int threshold2= (threshold1<<1);
248 int level= src[i];
249 if(((unsigned)(level+threshold1))>threshold2){
250 if(((unsigned)(level+2*threshold1))>2*threshold2){
251 a += level * factor[i];
252 }else{
253 if(level>0) a+= 2*(level - (int)threshold1)*factor[i];
254 else a+= 2*(level + (int)threshold1)*factor[i];
255 }
256 }
257 }
258 return (a + (1<<11))>>12;
259 }
260
261 static int softthresh_c(DCTELEM *src, int qp){
262 int i;
263 int a;
264
265 a= src[0] * factor[0];
266 for(i=1; i<16; i++){
267 unsigned int threshold1= thres2[qp][i];
268 unsigned int threshold2= (threshold1<<1);
269 int level= src[i];
270 if(((unsigned)(level+threshold1))>threshold2){
271 if(level>0) a+= (level - (int)threshold1)*factor[i];
272 else a+= (level + (int)threshold1)*factor[i];
273 }
274 }
275 return (a + (1<<11))>>12;
276 }
277
278 static int (*requantize)(DCTELEM *src, int qp)= hardthresh_c;
279
280 static void filter(struct vf_priv_s *p, uint8_t *dst, uint8_t *src, int dst_stride, int src_stride, int width, int height, uint8_t *qp_store, int qp_stride, int is_luma){
281 int x, y;
282 const int stride= is_luma ? p->temp_stride : ((width+16+15)&(~15));
283 uint8_t *p_src= p->src + 8*stride;
284 DCTELEM *block= p->src;
285 DCTELEM *temp= p->src + 32;
286
287 if (!src || !dst) return; // HACK avoid crash for Y8 colourspace
288 for(y=0; y<height; y++){
289 int index= 8 + 8*stride + y*stride;
290 memcpy(p_src + index, src + y*src_stride, width);
291 for(x=0; x<8; x++){
292 p_src[index - x - 1]= p_src[index + x ];
293 p_src[index + width + x ]= p_src[index + width - x - 1];
294 }
295 }
296 for(y=0; y<8; y++){
297 memcpy(p_src + ( 7-y)*stride, p_src + ( y+8)*stride, stride);
298 memcpy(p_src + (height+8+y)*stride, p_src + (height-y+7)*stride, stride);
299 }
300 //FIXME (try edge emu)
301
302 for(y=0; y<height; y++){
303 for(x=-8; x<0; x+=4){
304 const int index= x + y*stride + (8-3)*(1+stride) + 8; //FIXME silly offset
305 uint8_t *src = p_src + index;
306 DCTELEM *tp= temp+4*x;
307
308 dctA_c(tp+4*8, src, stride);
309 }
310 for(x=0; x<width; ){
311 const int qps= 3 + is_luma;
312 int qp;
313 int end= XMIN(x+8, width);
314
315 if(p->qp)
316 qp= p->qp;
317 else{
318 qp= qp_store[ (XMIN(x, width-1)>>qps) + (XMIN(y, height-1)>>qps) * qp_stride];
319 if(p->mpeg2) qp>>=1;
320 }
321 for(; x<end; x++){
322 const int index= x + y*stride + (8-3)*(1+stride) + 8; //FIXME silly offset
323 uint8_t *src = p_src + index;
324 DCTELEM *tp= temp+4*x;
325 int v;
326
327 if((x&3)==0)
328 dctA_c(tp+4*8, src, stride);
329
330 dctB(block, tp);
331
332 v= requantize(block, qp);
333 v= (v + dither[y&7][x&7])>>6;
334 if((unsigned)v > 255)
335 v= (-v)>>31;
336 dst[x + y*dst_stride]= v;
337 }
338 }
339 }
340 }
341
342 static int config(struct vf_instance_s* vf,
343 int width, int height, int d_width, int d_height,
344 unsigned int flags, unsigned int outfmt){
345 int h= (height+16+15)&(~15);
346
347 vf->priv->temp_stride= (width+16+15)&(~15);
348 vf->priv->src = memalign(8, vf->priv->temp_stride*(h+8)*sizeof(uint8_t));
349
350 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
351 }
352
353 static void get_image(struct vf_instance_s* vf, mp_image_t *mpi){
354 if(mpi->flags&MP_IMGFLAG_PRESERVE) return; // don't change
355 // ok, we can do pp in-place (or pp disabled):
356 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
357 mpi->type, mpi->flags, mpi->w, mpi->h);
358 mpi->planes[0]=vf->dmpi->planes[0];
359 mpi->stride[0]=vf->dmpi->stride[0];
360 mpi->width=vf->dmpi->width;
361 if(mpi->flags&MP_IMGFLAG_PLANAR){
362 mpi->planes[1]=vf->dmpi->planes[1];
363 mpi->planes[2]=vf->dmpi->planes[2];
364 mpi->stride[1]=vf->dmpi->stride[1];
365 mpi->stride[2]=vf->dmpi->stride[2];
366 }
367 mpi->flags|=MP_IMGFLAG_DIRECT;
368 }
369
370 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi){
371 mp_image_t *dmpi;
372
373 if(mpi->flags&MP_IMGFLAG_DIRECT){
374 dmpi=vf->dmpi;
375 }else{
376 // no DR, so get a new image! hope we'll get DR buffer:
377 dmpi=vf_get_image(vf->next,mpi->imgfmt,
378 MP_IMGTYPE_TEMP,
379 MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
380 mpi->w,mpi->h);
381 vf_clone_mpi_attributes(dmpi, mpi);
382 }
383
384 vf->priv->mpeg2= mpi->qscale_type;
385 if(mpi->qscale || vf->priv->qp){
386 filter(vf->priv, dmpi->planes[0], mpi->planes[0], dmpi->stride[0], mpi->stride[0], mpi->w, mpi->h, mpi->qscale, mpi->qstride, 1);
387 filter(vf->priv, dmpi->planes[1], mpi->planes[1], dmpi->stride[1], mpi->stride[1], mpi->w>>mpi->chroma_x_shift, mpi->h>>mpi->chroma_y_shift, mpi->qscale, mpi->qstride, 0);
388 filter(vf->priv, dmpi->planes[2], mpi->planes[2], dmpi->stride[2], mpi->stride[2], mpi->w>>mpi->chroma_x_shift, mpi->h>>mpi->chroma_y_shift, mpi->qscale, mpi->qstride, 0);
389 }else{
390 memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h, dmpi->stride[0], mpi->stride[0]);
391 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]);
392 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]);
393 }
394
395 #ifdef HAVE_MMX
396 if(gCpuCaps.hasMMX) asm volatile ("emms\n\t");
397 #endif
398 #ifdef HAVE_MMX2
399 if(gCpuCaps.hasMMX2) asm volatile ("sfence\n\t");
400 #endif
401
402 return vf_next_put_image(vf,dmpi);
403 }
404
405 static void uninit(struct vf_instance_s* vf){
406 if(!vf->priv) return;
407
408 if(vf->priv->src) free(vf->priv->src);
409 vf->priv->src= NULL;
410
411 free(vf->priv);
412 vf->priv=NULL;
413 }
414
415 //===========================================================================//
416 static int query_format(struct vf_instance_s* vf, unsigned int fmt){
417 switch(fmt){
418 case IMGFMT_YVU9:
419 case IMGFMT_IF09:
420 case IMGFMT_YV12:
421 case IMGFMT_I420:
422 case IMGFMT_IYUV:
423 case IMGFMT_CLPL:
424 case IMGFMT_Y800:
425 case IMGFMT_Y8:
426 case IMGFMT_444P:
427 case IMGFMT_422P:
428 case IMGFMT_411P:
429 return vf_next_query_format(vf,fmt);
430 }
431 return 0;
432 }
433
434 static int control(struct vf_instance_s* vf, int request, void* data){
435 return vf_next_control(vf,request,data);
436 }
437
438 static int open(vf_instance_t *vf, char* args){
439 vf->config=config;
440 vf->put_image=put_image;
441 vf->get_image=get_image;
442 vf->query_format=query_format;
443 vf->uninit=uninit;
444 vf->control= control;
445 vf->priv=malloc(sizeof(struct vf_priv_s));
446 memset(vf->priv, 0, sizeof(struct vf_priv_s));
447
448 if (args) sscanf(args, "%d:%d", &vf->priv->qp, &vf->priv->mode);
449
450 if(vf->priv->qp < 0)
451 vf->priv->qp = 0;
452
453 init_thres2();
454
455 switch(vf->priv->mode){
456 case 0: requantize= hardthresh_c; break;
457 case 1: requantize= softthresh_c; break;
458 default:
459 case 2: requantize= mediumthresh_c; break;
460 }
461
462 #ifdef HAVE_MMX
463 if(gCpuCaps.hasMMX){
464 dctB= dctB_mmx;
465 }
466 #endif
467 #if 0
468 if(gCpuCaps.hasMMX){
469 switch(vf->priv->mode){
470 case 0: requantize= hardthresh_mmx; break;
471 case 1: requantize= softthresh_mmx; break;
472 }
473 }
474 #endif
475
476 return 1;
477 }
478
479 vf_info_t vf_info_pp7 = {
480 "postprocess 7",
481 "pp7",
482 "Michael Niedermayer",
483 "",
484 open,
485 NULL
486 };