Use proper length specifiers in mp_msg calls, fixes the warnings:
[mplayer/greg.git] / libmpcodecs / vf_pp7.c
blobbd104a41bdbfe0991fac842a5261acb61f2f3f37
1 /*
2 Copyright (C) 2005 Michael Niedermayer <michaelni@gmx.at>
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <inttypes.h>
24 #include <math.h>
26 #include "config.h"
28 #include "mp_msg.h"
29 #include "cpudetect.h"
31 #ifdef HAVE_MALLOC_H
32 #include <malloc.h>
33 #endif
35 #include "img_format.h"
36 #include "mp_image.h"
37 #include "vf.h"
38 #include "libvo/fastmemcpy.h"
40 #define XMIN(a,b) ((a) < (b) ? (a) : (b))
41 #define XMAX(a,b) ((a) > (b) ? (a) : (b))
43 typedef short DCTELEM;
45 //===========================================================================//
46 static const uint8_t __attribute__((aligned(8))) dither[8][8]={
47 { 0, 48, 12, 60, 3, 51, 15, 63, },
48 { 32, 16, 44, 28, 35, 19, 47, 31, },
49 { 8, 56, 4, 52, 11, 59, 7, 55, },
50 { 40, 24, 36, 20, 43, 27, 39, 23, },
51 { 2, 50, 14, 62, 1, 49, 13, 61, },
52 { 34, 18, 46, 30, 33, 17, 45, 29, },
53 { 10, 58, 6, 54, 9, 57, 5, 53, },
54 { 42, 26, 38, 22, 41, 25, 37, 21, },
57 struct vf_priv_s {
58 int qp;
59 int mode;
60 int mpeg2;
61 int temp_stride;
62 uint8_t *src;
64 #if 0
65 static inline void dct7_c(DCTELEM *dst, int s0, int s1, int s2, int s3, int step){
66 int s, d;
67 int dst2[64];
68 //#define S0 (1024/0.37796447300922719759)
69 #define C0 ((int)(1024*0.37796447300922719759+0.5)) //sqrt(1/7)
70 #define C1 ((int)(1024*0.53452248382484879308/6+0.5)) //sqrt(2/7)/6
72 #define C2 ((int)(1024*0.45221175985034745004/2+0.5))
73 #define C3 ((int)(1024*0.36264567479870879474/2+0.5))
75 //0.1962505182412941918 0.0149276808419397944-0.2111781990832339584
76 #define C4 ((int)(1024*0.1962505182412941918+0.5))
77 #define C5 ((int)(1024*0.0149276808419397944+0.5))
78 //#define C6 ((int)(1024*0.2111781990832339584+0.5))
79 #if 0
80 s= s0 + s1 + s2;
81 dst[0*step] = ((s + s3)*C0 + 512) >> 10;
82 s= (s - 6*s3)*C1 + 512;
83 d= (s0-s2)*C4 + (s1-s2)*C5;
84 dst[1*step] = (s + 2*d)>>10;
85 s -= d;
86 d= (s1-s0)*C2 + (s1-s2)*C3;
87 dst[2*step] = (s + d)>>10;
88 dst[3*step] = (s - d)>>10;
89 #elif 1
90 s = s3+s3;
91 s3= s-s0;
92 s0= s+s0;
93 s = s2+s1;
94 s2= s2-s1;
95 dst[0*step]= s0 + s;
96 dst[2*step]= s0 - s;
97 dst[1*step]= 2*s3 + s2;
98 dst[3*step]= s3 - 2*s2;
99 #else
100 int i,j,n=7;
101 for(i=0; i<7; i+=2){
102 dst2[i*step/2]= 0;
103 for(j=0; j<4; j++)
104 dst2[i*step/2] += src[j*step] * cos(i*M_PI/n*(j+0.5)) * sqrt((i?2.0:1.0)/n);
105 if(fabs(dst2[i*step/2] - dst[i*step/2]) > 20)
106 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]);
108 #endif
110 #endif
112 static inline void dctA_c(DCTELEM *dst, uint8_t *src, int stride){
113 int i;
115 for(i=0; i<4; i++){
116 int s0= src[0*stride] + src[6*stride];
117 int s1= src[1*stride] + src[5*stride];
118 int s2= src[2*stride] + src[4*stride];
119 int s3= src[3*stride];
120 int s= s3+s3;
121 s3= s-s0;
122 s0= s+s0;
123 s = s2+s1;
124 s2= s2-s1;
125 dst[0]= s0 + s;
126 dst[2]= s0 - s;
127 dst[1]= 2*s3 + s2;
128 dst[3]= s3 - 2*s2;
129 src++;
130 dst+=4;
134 static void dctB_c(DCTELEM *dst, DCTELEM *src){
135 int i;
137 for(i=0; i<4; i++){
138 int s0= src[0*4] + src[6*4];
139 int s1= src[1*4] + src[5*4];
140 int s2= src[2*4] + src[4*4];
141 int s3= src[3*4];
142 int s= s3+s3;
143 s3= s-s0;
144 s0= s+s0;
145 s = s2+s1;
146 s2= s2-s1;
147 dst[0*4]= s0 + s;
148 dst[2*4]= s0 - s;
149 dst[1*4]= 2*s3 + s2;
150 dst[3*4]= s3 - 2*s2;
151 src++;
152 dst++;
156 #ifdef HAVE_MMX
157 static void dctB_mmx(DCTELEM *dst, DCTELEM *src){
158 asm volatile (
159 "movq (%0), %%mm0 \n\t"
160 "movq 1*4*2(%0), %%mm1 \n\t"
161 "paddw 6*4*2(%0), %%mm0 \n\t"
162 "paddw 5*4*2(%0), %%mm1 \n\t"
163 "movq 2*4*2(%0), %%mm2 \n\t"
164 "movq 3*4*2(%0), %%mm3 \n\t"
165 "paddw 4*4*2(%0), %%mm2 \n\t"
166 "paddw %%mm3, %%mm3 \n\t" //s
167 "movq %%mm3, %%mm4 \n\t" //s
168 "psubw %%mm0, %%mm3 \n\t" //s-s0
169 "paddw %%mm0, %%mm4 \n\t" //s+s0
170 "movq %%mm2, %%mm0 \n\t" //s2
171 "psubw %%mm1, %%mm2 \n\t" //s2-s1
172 "paddw %%mm1, %%mm0 \n\t" //s2+s1
173 "movq %%mm4, %%mm1 \n\t" //s0'
174 "psubw %%mm0, %%mm4 \n\t" //s0'-s'
175 "paddw %%mm0, %%mm1 \n\t" //s0'+s'
176 "movq %%mm3, %%mm0 \n\t" //s3'
177 "psubw %%mm2, %%mm3 \n\t"
178 "psubw %%mm2, %%mm3 \n\t"
179 "paddw %%mm0, %%mm2 \n\t"
180 "paddw %%mm0, %%mm2 \n\t"
181 "movq %%mm1, (%1) \n\t"
182 "movq %%mm4, 2*4*2(%1) \n\t"
183 "movq %%mm2, 1*4*2(%1) \n\t"
184 "movq %%mm3, 3*4*2(%1) \n\t"
185 :: "r" (src), "r"(dst)
188 #endif
190 static void (*dctB)(DCTELEM *dst, DCTELEM *src)= dctB_c;
192 #define N0 4
193 #define N1 5
194 #define N2 10
195 #define SN0 2
196 #define SN1 2.2360679775
197 #define SN2 3.16227766017
198 #define N (1<<16)
200 static const int factor[16]={
201 N/(N0*N0), N/(N0*N1), N/(N0*N0),N/(N0*N2),
202 N/(N1*N0), N/(N1*N1), N/(N1*N0),N/(N1*N2),
203 N/(N0*N0), N/(N0*N1), N/(N0*N0),N/(N0*N2),
204 N/(N2*N0), N/(N2*N1), N/(N2*N0),N/(N2*N2),
207 static const int thres[16]={
208 N/(SN0*SN0), N/(SN0*SN2), N/(SN0*SN0),N/(SN0*SN2),
209 N/(SN2*SN0), N/(SN2*SN2), N/(SN2*SN0),N/(SN2*SN2),
210 N/(SN0*SN0), N/(SN0*SN2), N/(SN0*SN0),N/(SN0*SN2),
211 N/(SN2*SN0), N/(SN2*SN2), N/(SN2*SN0),N/(SN2*SN2),
214 static int thres2[99][16];
216 static void init_thres2(void){
217 int qp, i;
218 int bias= 0; //FIXME
220 for(qp=0; qp<99; qp++){
221 for(i=0; i<16; i++){
222 thres2[qp][i]= ((i&1)?SN2:SN0) * ((i&4)?SN2:SN0) * XMAX(1,qp) * (1<<2) - 1 - bias;
227 static int hardthresh_c(DCTELEM *src, int qp){
228 int i;
229 int a;
231 a= src[0] * factor[0];
232 for(i=1; i<16; i++){
233 unsigned int threshold1= thres2[qp][i];
234 unsigned int threshold2= (threshold1<<1);
235 int level= src[i];
236 if(((unsigned)(level+threshold1))>threshold2){
237 a += level * factor[i];
240 return (a + (1<<11))>>12;
243 static int mediumthresh_c(DCTELEM *src, int qp){
244 int i;
245 int a;
247 a= src[0] * factor[0];
248 for(i=1; i<16; i++){
249 unsigned int threshold1= thres2[qp][i];
250 unsigned int threshold2= (threshold1<<1);
251 int level= src[i];
252 if(((unsigned)(level+threshold1))>threshold2){
253 if(((unsigned)(level+2*threshold1))>2*threshold2){
254 a += level * factor[i];
255 }else{
256 if(level>0) a+= 2*(level - (int)threshold1)*factor[i];
257 else a+= 2*(level + (int)threshold1)*factor[i];
261 return (a + (1<<11))>>12;
264 static int softthresh_c(DCTELEM *src, int qp){
265 int i;
266 int a;
268 a= src[0] * factor[0];
269 for(i=1; i<16; i++){
270 unsigned int threshold1= thres2[qp][i];
271 unsigned int threshold2= (threshold1<<1);
272 int level= src[i];
273 if(((unsigned)(level+threshold1))>threshold2){
274 if(level>0) a+= (level - (int)threshold1)*factor[i];
275 else a+= (level + (int)threshold1)*factor[i];
278 return (a + (1<<11))>>12;
281 static int (*requantize)(DCTELEM *src, int qp)= hardthresh_c;
283 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){
284 int x, y;
285 const int stride= is_luma ? p->temp_stride : ((width+16+15)&(~15));
286 uint8_t *p_src= p->src + 8*stride;
287 DCTELEM *block= p->src;
288 DCTELEM *temp= p->src + 32;
290 if (!src || !dst) return; // HACK avoid crash for Y8 colourspace
291 for(y=0; y<height; y++){
292 int index= 8 + 8*stride + y*stride;
293 fast_memcpy(p_src + index, src + y*src_stride, width);
294 for(x=0; x<8; x++){
295 p_src[index - x - 1]= p_src[index + x ];
296 p_src[index + width + x ]= p_src[index + width - x - 1];
299 for(y=0; y<8; y++){
300 fast_memcpy(p_src + ( 7-y)*stride, p_src + ( y+8)*stride, stride);
301 fast_memcpy(p_src + (height+8+y)*stride, p_src + (height-y+7)*stride, stride);
303 //FIXME (try edge emu)
305 for(y=0; y<height; y++){
306 for(x=-8; x<0; x+=4){
307 const int index= x + y*stride + (8-3)*(1+stride) + 8; //FIXME silly offset
308 uint8_t *src = p_src + index;
309 DCTELEM *tp= temp+4*x;
311 dctA_c(tp+4*8, src, stride);
313 for(x=0; x<width; ){
314 const int qps= 3 + is_luma;
315 int qp;
316 int end= XMIN(x+8, width);
318 if(p->qp)
319 qp= p->qp;
320 else{
321 qp= qp_store[ (XMIN(x, width-1)>>qps) + (XMIN(y, height-1)>>qps) * qp_stride];
322 if(p->mpeg2) qp>>=1;
324 for(; x<end; x++){
325 const int index= x + y*stride + (8-3)*(1+stride) + 8; //FIXME silly offset
326 uint8_t *src = p_src + index;
327 DCTELEM *tp= temp+4*x;
328 int v;
330 if((x&3)==0)
331 dctA_c(tp+4*8, src, stride);
333 dctB(block, tp);
335 v= requantize(block, qp);
336 v= (v + dither[y&7][x&7])>>6;
337 if((unsigned)v > 255)
338 v= (-v)>>31;
339 dst[x + y*dst_stride]= v;
345 static int config(struct vf_instance_s* vf,
346 int width, int height, int d_width, int d_height,
347 unsigned int flags, unsigned int outfmt){
348 int h= (height+16+15)&(~15);
350 vf->priv->temp_stride= (width+16+15)&(~15);
351 vf->priv->src = memalign(8, vf->priv->temp_stride*(h+8)*sizeof(uint8_t));
353 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
356 static void get_image(struct vf_instance_s* vf, mp_image_t *mpi){
357 if(mpi->flags&MP_IMGFLAG_PRESERVE) return; // don't change
358 // ok, we can do pp in-place (or pp disabled):
359 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
360 mpi->type, mpi->flags | MP_IMGFLAG_READABLE, mpi->width, mpi->height);
361 mpi->planes[0]=vf->dmpi->planes[0];
362 mpi->stride[0]=vf->dmpi->stride[0];
363 mpi->width=vf->dmpi->width;
364 if(mpi->flags&MP_IMGFLAG_PLANAR){
365 mpi->planes[1]=vf->dmpi->planes[1];
366 mpi->planes[2]=vf->dmpi->planes[2];
367 mpi->stride[1]=vf->dmpi->stride[1];
368 mpi->stride[2]=vf->dmpi->stride[2];
370 mpi->flags|=MP_IMGFLAG_DIRECT;
373 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts){
374 mp_image_t *dmpi;
376 if(mpi->flags&MP_IMGFLAG_DIRECT){
377 dmpi=vf->dmpi;
378 }else{
379 // no DR, so get a new image! hope we'll get DR buffer:
380 dmpi=vf_get_image(vf->next,mpi->imgfmt,
381 MP_IMGTYPE_TEMP,
382 MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
383 mpi->width,mpi->height);
384 vf_clone_mpi_attributes(dmpi, mpi);
387 vf->priv->mpeg2= mpi->qscale_type;
388 if(mpi->qscale || vf->priv->qp){
389 filter(vf->priv, dmpi->planes[0], mpi->planes[0], dmpi->stride[0], mpi->stride[0], mpi->w, mpi->h, mpi->qscale, mpi->qstride, 1);
390 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);
391 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);
392 }else{
393 memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h, dmpi->stride[0], mpi->stride[0]);
394 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]);
395 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]);
398 #ifdef HAVE_MMX
399 if(gCpuCaps.hasMMX) asm volatile ("emms\n\t");
400 #endif
401 #ifdef HAVE_MMX2
402 if(gCpuCaps.hasMMX2) asm volatile ("sfence\n\t");
403 #endif
405 return vf_next_put_image(vf,dmpi, pts);
408 static void uninit(struct vf_instance_s* vf){
409 if(!vf->priv) return;
411 if(vf->priv->src) free(vf->priv->src);
412 vf->priv->src= NULL;
414 free(vf->priv);
415 vf->priv=NULL;
418 //===========================================================================//
419 static int query_format(struct vf_instance_s* vf, unsigned int fmt){
420 switch(fmt){
421 case IMGFMT_YVU9:
422 case IMGFMT_IF09:
423 case IMGFMT_YV12:
424 case IMGFMT_I420:
425 case IMGFMT_IYUV:
426 case IMGFMT_CLPL:
427 case IMGFMT_Y800:
428 case IMGFMT_Y8:
429 case IMGFMT_444P:
430 case IMGFMT_422P:
431 case IMGFMT_411P:
432 return vf_next_query_format(vf,fmt);
434 return 0;
437 static int control(struct vf_instance_s* vf, int request, void* data){
438 return vf_next_control(vf,request,data);
441 static int open(vf_instance_t *vf, char* args){
442 vf->config=config;
443 vf->put_image=put_image;
444 vf->get_image=get_image;
445 vf->query_format=query_format;
446 vf->uninit=uninit;
447 vf->control= control;
448 vf->priv=malloc(sizeof(struct vf_priv_s));
449 memset(vf->priv, 0, sizeof(struct vf_priv_s));
451 if (args) sscanf(args, "%d:%d", &vf->priv->qp, &vf->priv->mode);
453 if(vf->priv->qp < 0)
454 vf->priv->qp = 0;
456 init_thres2();
458 switch(vf->priv->mode){
459 case 0: requantize= hardthresh_c; break;
460 case 1: requantize= softthresh_c; break;
461 default:
462 case 2: requantize= mediumthresh_c; break;
465 #ifdef HAVE_MMX
466 if(gCpuCaps.hasMMX){
467 dctB= dctB_mmx;
469 #endif
470 #if 0
471 if(gCpuCaps.hasMMX){
472 switch(vf->priv->mode){
473 case 0: requantize= hardthresh_mmx; break;
474 case 1: requantize= softthresh_mmx; break;
477 #endif
479 return 1;
482 const vf_info_t vf_info_pp7 = {
483 "postprocess 7",
484 "pp7",
485 "Michael Niedermayer",
487 open,
488 NULL