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