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
29 #include "cpudetect.h"
35 #include "img_format.h"
38 #include "libvo/fastmemcpy.h"
40 #define XMIN(a,b) ((a) < (b) ? (a) : (b))
42 typedef short DCTELEM
;
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, },
64 static inline void dct7_c(DCTELEM
*dst
, int s0
, int s1
, int s2
, int s3
, int step
){
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
71 #define C2 ((int)(1024*0.45221175985034745004/2+0.5))
72 #define C3 ((int)(1024*0.36264567479870879474/2+0.5))
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))
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;
85 d
= (s1
-s0
)*C2
+ (s1
-s2
)*C3
;
86 dst
[2*step
] = (s
+ d
)>>10;
87 dst
[3*step
] = (s
- d
)>>10;
96 dst
[1*step
]= 2*s3
+ s2
;
97 dst
[3*step
]= s3
- 2*s2
;
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
]);
111 static inline void dctA_c(DCTELEM
*dst
, uint8_t *src
, int stride
){
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
];
133 static void dctB_c(DCTELEM
*dst
, DCTELEM
*src
){
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];
156 static void dctB_mmx(DCTELEM
*dst
, DCTELEM
*src
){
158 "movq (%0), %%mm0 \n\t"
159 "movq 1*4*2(%0), %%mm1 \n\t"
160 "paddw 6*4*2(%0), %%mm0 \n\t"
161 "paddw 5*4*2(%0), %%mm1 \n\t"
162 "movq 2*4*2(%0), %%mm2 \n\t"
163 "movq 3*4*2(%0), %%mm3 \n\t"
164 "paddw 4*4*2(%0), %%mm2 \n\t"
165 "paddw %%mm3, %%mm3 \n\t" //s
166 "movq %%mm3, %%mm4 \n\t" //s
167 "psubw %%mm0, %%mm3 \n\t" //s-s0
168 "paddw %%mm0, %%mm4 \n\t" //s+s0
169 "movq %%mm2, %%mm0 \n\t" //s2
170 "psubw %%mm1, %%mm2 \n\t" //s2-s1
171 "paddw %%mm1, %%mm0 \n\t" //s2+s1
172 "movq %%mm4, %%mm1 \n\t" //s0'
173 "psubw %%mm0, %%mm4 \n\t" //s0'-s'
174 "paddw %%mm0, %%mm1 \n\t" //s0'+s'
175 "movq %%mm3, %%mm0 \n\t" //s3'
176 "psubw %%mm2, %%mm3 \n\t"
177 "psubw %%mm2, %%mm3 \n\t"
178 "paddw %%mm0, %%mm2 \n\t"
179 "paddw %%mm0, %%mm2 \n\t"
180 "movq %%mm1, (%1) \n\t"
181 "movq %%mm4, 2*4*2(%1) \n\t"
182 "movq %%mm2, 1*4*2(%1) \n\t"
183 "movq %%mm3, 3*4*2(%1) \n\t"
184 :: "r" (src
), "r"(dst
)
189 static void (*dctB
)(DCTELEM
*dst
, DCTELEM
*src
)= dctB_c
;
195 #define SN1 2.2360679775
196 #define SN2 3.16227766017
199 static const int factor
[16]={
200 N
/(N0
*N0
), N
/(N0
*N1
), N
/(N0
*N0
),N
/(N0
*N2
),
201 N
/(N1
*N0
), N
/(N1
*N1
), N
/(N1
*N0
),N
/(N1
*N2
),
202 N
/(N0
*N0
), N
/(N0
*N1
), N
/(N0
*N0
),N
/(N0
*N2
),
203 N
/(N2
*N0
), N
/(N2
*N1
), N
/(N2
*N0
),N
/(N2
*N2
),
206 static const int thres
[16]={
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 N
/(SN0
*SN0
), N
/(SN0
*SN2
), N
/(SN0
*SN0
),N
/(SN0
*SN2
),
210 N
/(SN2
*SN0
), N
/(SN2
*SN2
), N
/(SN2
*SN0
),N
/(SN2
*SN2
),
213 static int thres2
[99][16];
215 static void init_thres2(void){
219 for(qp
=0; qp
<99; qp
++){
221 thres2
[qp
][i
]= ((i
&1)?SN2
:SN0
) * ((i
&4)?SN2
:SN0
) * qp
* (1<<2) - 1 - bias
;
226 static int hardthresh_c(DCTELEM
*src
, int qp
){
230 a
= src
[0] * factor
[0];
232 unsigned int threshold1
= thres2
[qp
][i
];
233 unsigned int threshold2
= (threshold1
<<1);
235 if(((unsigned)(level
+threshold1
))>threshold2
){
236 a
+= level
* factor
[i
];
239 return (a
+ (1<<11))>>12;
242 static int mediumthresh_c(DCTELEM
*src
, int qp
){
246 a
= src
[0] * factor
[0];
248 unsigned int threshold1
= thres2
[qp
][i
];
249 unsigned int threshold2
= (threshold1
<<1);
251 if(((unsigned)(level
+threshold1
))>threshold2
){
252 if(((unsigned)(level
+2*threshold1
))>2*threshold2
){
253 a
+= level
* factor
[i
];
255 if(level
>0) a
+= 2*(level
- (int)threshold1
)*factor
[i
];
256 else a
+= 2*(level
+ (int)threshold1
)*factor
[i
];
260 return (a
+ (1<<11))>>12;
263 static int softthresh_c(DCTELEM
*src
, int qp
){
267 a
= src
[0] * factor
[0];
269 unsigned int threshold1
= thres2
[qp
][i
];
270 unsigned int threshold2
= (threshold1
<<1);
272 if(((unsigned)(level
+threshold1
))>threshold2
){
273 if(level
>0) a
+= (level
- (int)threshold1
)*factor
[i
];
274 else a
+= (level
+ (int)threshold1
)*factor
[i
];
277 return (a
+ (1<<11))>>12;
280 static int (*requantize
)(DCTELEM
*src
, int qp
)= hardthresh_c
;
282 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 const int stride
= is_luma
? p
->temp_stride
: ((width
+16+15)&(~15));
285 uint8_t *p_src
= p
->src
+ 8*stride
;
286 DCTELEM
*block
= p
->src
;
287 DCTELEM
*temp
= p
->src
+ 32;
289 if (!src
|| !dst
) return; // HACK avoid crash for Y8 colourspace
290 for(y
=0; y
<height
; y
++){
291 int index
= 8 + 8*stride
+ y
*stride
;
292 memcpy(p_src
+ index
, src
+ y
*src_stride
, width
);
294 p_src
[index
- x
- 1]= p_src
[index
+ x
];
295 p_src
[index
+ width
+ x
]= p_src
[index
+ width
- x
- 1];
299 memcpy(p_src
+ ( 7-y
)*stride
, p_src
+ ( y
+8)*stride
, stride
);
300 memcpy(p_src
+ (height
+8+y
)*stride
, p_src
+ (height
-y
+7)*stride
, stride
);
302 //FIXME (try edge emu)
304 for(y
=0; y
<height
; y
++){
305 for(x
=-8; x
<0; x
+=4){
306 const int index
= x
+ y
*stride
+ (8-3)*(1+stride
) + 8; //FIXME silly offset
307 uint8_t *src
= p_src
+ index
;
308 DCTELEM
*tp
= temp
+4*x
;
310 dctA_c(tp
+4*8, src
, stride
);
313 const int qps
= 3 + is_luma
;
315 int end
= XMIN(x
+8, width
);
320 qp
= qp_store
[ (XMIN(x
, width
-1)>>qps
) + (XMIN(y
, height
-1)>>qps
) * qp_stride
];
324 const int index
= x
+ y
*stride
+ (8-3)*(1+stride
) + 8; //FIXME silly offset
325 uint8_t *src
= p_src
+ index
;
326 DCTELEM
*tp
= temp
+4*x
;
330 dctA_c(tp
+4*8, src
, stride
);
334 v
= requantize(block
, qp
);
335 v
= (v
+ dither
[y
&7][x
&7])>>6;
336 if((unsigned)v
> 255)
338 dst
[x
+ y
*dst_stride
]= v
;
344 static int config(struct vf_instance_s
* vf
,
345 int width
, int height
, int d_width
, int d_height
,
346 unsigned int flags
, unsigned int outfmt
){
347 int h
= (height
+16+15)&(~15);
349 vf
->priv
->temp_stride
= (width
+16+15)&(~15);
350 vf
->priv
->src
= memalign(8, vf
->priv
->temp_stride
*(h
+8)*sizeof(uint8_t));
352 return vf_next_config(vf
,width
,height
,d_width
,d_height
,flags
,outfmt
);
355 static void get_image(struct vf_instance_s
* vf
, mp_image_t
*mpi
){
356 if(mpi
->flags
&MP_IMGFLAG_PRESERVE
) return; // don't change
357 // ok, we can do pp in-place (or pp disabled):
358 vf
->dmpi
=vf_get_image(vf
->next
,mpi
->imgfmt
,
359 mpi
->type
, mpi
->flags
| MP_IMGFLAG_READABLE
, mpi
->width
, mpi
->height
);
360 mpi
->planes
[0]=vf
->dmpi
->planes
[0];
361 mpi
->stride
[0]=vf
->dmpi
->stride
[0];
362 mpi
->width
=vf
->dmpi
->width
;
363 if(mpi
->flags
&MP_IMGFLAG_PLANAR
){
364 mpi
->planes
[1]=vf
->dmpi
->planes
[1];
365 mpi
->planes
[2]=vf
->dmpi
->planes
[2];
366 mpi
->stride
[1]=vf
->dmpi
->stride
[1];
367 mpi
->stride
[2]=vf
->dmpi
->stride
[2];
369 mpi
->flags
|=MP_IMGFLAG_DIRECT
;
372 static int put_image(struct vf_instance_s
* vf
, mp_image_t
*mpi
, double pts
){
375 if(mpi
->flags
&MP_IMGFLAG_DIRECT
){
378 // no DR, so get a new image! hope we'll get DR buffer:
379 dmpi
=vf_get_image(vf
->next
,mpi
->imgfmt
,
381 MP_IMGFLAG_ACCEPT_STRIDE
|MP_IMGFLAG_PREFER_ALIGNED_STRIDE
,
382 mpi
->width
,mpi
->height
);
383 vf_clone_mpi_attributes(dmpi
, mpi
);
386 vf
->priv
->mpeg2
= mpi
->qscale_type
;
387 if(mpi
->qscale
|| vf
->priv
->qp
){
388 filter(vf
->priv
, dmpi
->planes
[0], mpi
->planes
[0], dmpi
->stride
[0], mpi
->stride
[0], mpi
->w
, mpi
->h
, mpi
->qscale
, mpi
->qstride
, 1);
389 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);
390 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 memcpy_pic(dmpi
->planes
[0], mpi
->planes
[0], mpi
->w
, mpi
->h
, dmpi
->stride
[0], mpi
->stride
[0]);
393 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]);
394 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 if(gCpuCaps
.hasMMX
) asm volatile ("emms\n\t");
401 if(gCpuCaps
.hasMMX2
) asm volatile ("sfence\n\t");
404 return vf_next_put_image(vf
,dmpi
, pts
);
407 static void uninit(struct vf_instance_s
* vf
){
408 if(!vf
->priv
) return;
410 if(vf
->priv
->src
) free(vf
->priv
->src
);
417 //===========================================================================//
418 static int query_format(struct vf_instance_s
* vf
, unsigned int fmt
){
431 return vf_next_query_format(vf
,fmt
);
436 static int control(struct vf_instance_s
* vf
, int request
, void* data
){
437 return vf_next_control(vf
,request
,data
);
440 static int open(vf_instance_t
*vf
, char* args
){
442 vf
->put_image
=put_image
;
443 vf
->get_image
=get_image
;
444 vf
->query_format
=query_format
;
446 vf
->control
= control
;
447 vf
->priv
=malloc(sizeof(struct vf_priv_s
));
448 memset(vf
->priv
, 0, sizeof(struct vf_priv_s
));
450 if (args
) sscanf(args
, "%d:%d", &vf
->priv
->qp
, &vf
->priv
->mode
);
457 switch(vf
->priv
->mode
){
458 case 0: requantize
= hardthresh_c
; break;
459 case 1: requantize
= softthresh_c
; break;
461 case 2: requantize
= mediumthresh_c
; break;
471 switch(vf
->priv
->mode
){
472 case 0: requantize
= hardthresh_mmx
; break;
473 case 1: requantize
= softthresh_mmx
; break;
481 vf_info_t vf_info_pp7
= {
484 "Michael Niedermayer",