• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavfilter/libmpcodecs/vf_pp7.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2005 Michael Niedermayer <michaelni@gmx.at>
00003  *
00004  * This file is part of MPlayer.
00005  *
00006  * MPlayer is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * MPlayer is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License along
00017  * with MPlayer; if not, write to the Free Software Foundation, Inc.,
00018  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00019  */
00020 
00021 
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025 #include <inttypes.h>
00026 #include <math.h>
00027 
00028 #include "config.h"
00029 
00030 #include "mp_msg.h"
00031 #include "cpudetect.h"
00032 
00033 #if HAVE_MALLOC_H
00034 #include <malloc.h>
00035 #endif
00036 
00037 #include "img_format.h"
00038 #include "mp_image.h"
00039 #include "vf.h"
00040 #include "libvo/fastmemcpy.h"
00041 
00042 #define XMIN(a,b) ((a) < (b) ? (a) : (b))
00043 #define XMAX(a,b) ((a) > (b) ? (a) : (b))
00044 
00045 typedef short DCTELEM;
00046 
00047 //===========================================================================//
00048 static const uint8_t  __attribute__((aligned(8))) dither[8][8]={
00049 {  0,  48,  12,  60,   3,  51,  15,  63, },
00050 { 32,  16,  44,  28,  35,  19,  47,  31, },
00051 {  8,  56,   4,  52,  11,  59,   7,  55, },
00052 { 40,  24,  36,  20,  43,  27,  39,  23, },
00053 {  2,  50,  14,  62,   1,  49,  13,  61, },
00054 { 34,  18,  46,  30,  33,  17,  45,  29, },
00055 { 10,  58,   6,  54,   9,  57,   5,  53, },
00056 { 42,  26,  38,  22,  41,  25,  37,  21, },
00057 };
00058 
00059 struct vf_priv_s {
00060     int qp;
00061     int mode;
00062     int mpeg2;
00063     int temp_stride;
00064     uint8_t *src;
00065 };
00066 #if 0
00067 static inline void dct7_c(DCTELEM *dst, int s0, int s1, int s2, int s3, int step){
00068     int s, d;
00069     int dst2[64];
00070 //#define S0 (1024/0.37796447300922719759)
00071 #define C0 ((int)(1024*0.37796447300922719759+0.5)) //sqrt(1/7)
00072 #define C1 ((int)(1024*0.53452248382484879308/6+0.5)) //sqrt(2/7)/6
00073 
00074 #define C2 ((int)(1024*0.45221175985034745004/2+0.5))
00075 #define C3 ((int)(1024*0.36264567479870879474/2+0.5))
00076 
00077 //0.1962505182412941918 0.0149276808419397944-0.2111781990832339584
00078 #define C4 ((int)(1024*0.1962505182412941918+0.5))
00079 #define C5 ((int)(1024*0.0149276808419397944+0.5))
00080 //#define C6 ((int)(1024*0.2111781990832339584+0.5))
00081 #if 0
00082     s= s0 + s1 + s2;
00083     dst[0*step] = ((s + s3)*C0 + 512) >> 10;
00084     s= (s - 6*s3)*C1 + 512;
00085     d= (s0-s2)*C4 + (s1-s2)*C5;
00086     dst[1*step] = (s + 2*d)>>10;
00087     s -= d;
00088     d= (s1-s0)*C2 + (s1-s2)*C3;
00089     dst[2*step] = (s + d)>>10;
00090     dst[3*step] = (s - d)>>10;
00091 #elif 1
00092     s = s3+s3;
00093     s3= s-s0;
00094     s0= s+s0;
00095     s = s2+s1;
00096     s2= s2-s1;
00097     dst[0*step]= s0 + s;
00098     dst[2*step]= s0 - s;
00099     dst[1*step]= 2*s3 +   s2;
00100     dst[3*step]=   s3 - 2*s2;
00101 #else
00102     int i,j,n=7;
00103     for(i=0; i<7; i+=2){
00104         dst2[i*step/2]= 0;
00105         for(j=0; j<4; j++)
00106             dst2[i*step/2] += src[j*step] * cos(i*M_PI/n*(j+0.5)) * sqrt((i?2.0:1.0)/n);
00107         if(fabs(dst2[i*step/2] - dst[i*step/2]) > 20)
00108             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]);
00109     }
00110 #endif
00111 }
00112 #endif
00113 
00114 static inline void dctA_c(DCTELEM *dst, uint8_t *src, int stride){
00115     int i;
00116 
00117     for(i=0; i<4; i++){
00118         int s0=  src[0*stride] + src[6*stride];
00119         int s1=  src[1*stride] + src[5*stride];
00120         int s2=  src[2*stride] + src[4*stride];
00121         int s3=  src[3*stride];
00122         int s= s3+s3;
00123         s3= s-s0;
00124         s0= s+s0;
00125         s = s2+s1;
00126         s2= s2-s1;
00127         dst[0]= s0 + s;
00128         dst[2]= s0 - s;
00129         dst[1]= 2*s3 +   s2;
00130         dst[3]=   s3 - 2*s2;
00131         src++;
00132         dst+=4;
00133     }
00134 }
00135 
00136 static void dctB_c(DCTELEM *dst, DCTELEM *src){
00137     int i;
00138 
00139     for(i=0; i<4; i++){
00140         int s0=  src[0*4] + src[6*4];
00141         int s1=  src[1*4] + src[5*4];
00142         int s2=  src[2*4] + src[4*4];
00143         int s3=  src[3*4];
00144         int s= s3+s3;
00145         s3= s-s0;
00146         s0= s+s0;
00147         s = s2+s1;
00148         s2= s2-s1;
00149         dst[0*4]= s0 + s;
00150         dst[2*4]= s0 - s;
00151         dst[1*4]= 2*s3 +   s2;
00152         dst[3*4]=   s3 - 2*s2;
00153         src++;
00154         dst++;
00155     }
00156 }
00157 
00158 #if HAVE_MMX
00159 static void dctB_mmx(DCTELEM *dst, DCTELEM *src){
00160     __asm__ volatile (
00161         "movq  (%0), %%mm0      \n\t"
00162         "movq  1*4*2(%0), %%mm1 \n\t"
00163         "paddw 6*4*2(%0), %%mm0 \n\t"
00164         "paddw 5*4*2(%0), %%mm1 \n\t"
00165         "movq  2*4*2(%0), %%mm2 \n\t"
00166         "movq  3*4*2(%0), %%mm3 \n\t"
00167         "paddw 4*4*2(%0), %%mm2 \n\t"
00168         "paddw %%mm3, %%mm3     \n\t" //s
00169         "movq %%mm3, %%mm4      \n\t" //s
00170         "psubw %%mm0, %%mm3     \n\t" //s-s0
00171         "paddw %%mm0, %%mm4     \n\t" //s+s0
00172         "movq %%mm2, %%mm0      \n\t" //s2
00173         "psubw %%mm1, %%mm2     \n\t" //s2-s1
00174         "paddw %%mm1, %%mm0     \n\t" //s2+s1
00175         "movq %%mm4, %%mm1      \n\t" //s0'
00176         "psubw %%mm0, %%mm4     \n\t" //s0'-s'
00177         "paddw %%mm0, %%mm1     \n\t" //s0'+s'
00178         "movq %%mm3, %%mm0      \n\t" //s3'
00179         "psubw %%mm2, %%mm3     \n\t"
00180         "psubw %%mm2, %%mm3     \n\t"
00181         "paddw %%mm0, %%mm2     \n\t"
00182         "paddw %%mm0, %%mm2     \n\t"
00183         "movq %%mm1, (%1)       \n\t"
00184         "movq %%mm4, 2*4*2(%1)  \n\t"
00185         "movq %%mm2, 1*4*2(%1)  \n\t"
00186         "movq %%mm3, 3*4*2(%1)  \n\t"
00187         :: "r" (src), "r"(dst)
00188     );
00189 }
00190 #endif
00191 
00192 static void (*dctB)(DCTELEM *dst, DCTELEM *src)= dctB_c;
00193 
00194 #define N0 4
00195 #define N1 5
00196 #define N2 10
00197 #define SN0 2
00198 #define SN1 2.2360679775
00199 #define SN2 3.16227766017
00200 #define N (1<<16)
00201 
00202 static const int factor[16]={
00203     N/(N0*N0), N/(N0*N1), N/(N0*N0),N/(N0*N2),
00204     N/(N1*N0), N/(N1*N1), N/(N1*N0),N/(N1*N2),
00205     N/(N0*N0), N/(N0*N1), N/(N0*N0),N/(N0*N2),
00206     N/(N2*N0), N/(N2*N1), N/(N2*N0),N/(N2*N2),
00207 };
00208 
00209 static const int thres[16]={
00210     N/(SN0*SN0), N/(SN0*SN2), N/(SN0*SN0),N/(SN0*SN2),
00211     N/(SN2*SN0), N/(SN2*SN2), N/(SN2*SN0),N/(SN2*SN2),
00212     N/(SN0*SN0), N/(SN0*SN2), N/(SN0*SN0),N/(SN0*SN2),
00213     N/(SN2*SN0), N/(SN2*SN2), N/(SN2*SN0),N/(SN2*SN2),
00214 };
00215 
00216 static int thres2[99][16];
00217 
00218 static void init_thres2(void){
00219     int qp, i;
00220     int bias= 0; //FIXME
00221 
00222     for(qp=0; qp<99; qp++){
00223         for(i=0; i<16; i++){
00224             thres2[qp][i]= ((i&1)?SN2:SN0) * ((i&4)?SN2:SN0) * XMAX(1,qp) * (1<<2) - 1 - bias;
00225         }
00226     }
00227 }
00228 
00229 static int hardthresh_c(DCTELEM *src, int qp){
00230     int i;
00231     int a;
00232 
00233     a= src[0] * factor[0];
00234     for(i=1; i<16; i++){
00235         unsigned int threshold1= thres2[qp][i];
00236         unsigned int threshold2= (threshold1<<1);
00237         int level= src[i];
00238         if(((unsigned)(level+threshold1))>threshold2){
00239             a += level * factor[i];
00240         }
00241     }
00242     return (a + (1<<11))>>12;
00243 }
00244 
00245 static int mediumthresh_c(DCTELEM *src, int qp){
00246     int i;
00247     int a;
00248 
00249     a= src[0] * factor[0];
00250     for(i=1; i<16; i++){
00251         unsigned int threshold1= thres2[qp][i];
00252         unsigned int threshold2= (threshold1<<1);
00253         int level= src[i];
00254         if(((unsigned)(level+threshold1))>threshold2){
00255             if(((unsigned)(level+2*threshold1))>2*threshold2){
00256                 a += level * factor[i];
00257             }else{
00258                 if(level>0) a+= 2*(level - (int)threshold1)*factor[i];
00259                 else        a+= 2*(level + (int)threshold1)*factor[i];
00260             }
00261         }
00262     }
00263     return (a + (1<<11))>>12;
00264 }
00265 
00266 static int softthresh_c(DCTELEM *src, int qp){
00267     int i;
00268     int a;
00269 
00270     a= src[0] * factor[0];
00271     for(i=1; i<16; i++){
00272         unsigned int threshold1= thres2[qp][i];
00273         unsigned int threshold2= (threshold1<<1);
00274         int level= src[i];
00275         if(((unsigned)(level+threshold1))>threshold2){
00276             if(level>0) a+= (level - (int)threshold1)*factor[i];
00277             else        a+= (level + (int)threshold1)*factor[i];
00278         }
00279     }
00280     return (a + (1<<11))>>12;
00281 }
00282 
00283 static int (*requantize)(DCTELEM *src, int qp)= hardthresh_c;
00284 
00285 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){
00286     int x, y;
00287     const int stride= is_luma ? p->temp_stride : ((width+16+15)&(~15));
00288     uint8_t  *p_src= p->src + 8*stride;
00289     DCTELEM *block= (DCTELEM *)p->src;
00290     DCTELEM *temp= (DCTELEM *)(p->src + 32);
00291 
00292     if (!src || !dst) return; // HACK avoid crash for Y8 colourspace
00293     for(y=0; y<height; y++){
00294         int index= 8 + 8*stride + y*stride;
00295         fast_memcpy(p_src + index, src + y*src_stride, width);
00296         for(x=0; x<8; x++){
00297             p_src[index         - x - 1]= p_src[index +         x    ];
00298             p_src[index + width + x    ]= p_src[index + width - x - 1];
00299         }
00300     }
00301     for(y=0; y<8; y++){
00302         fast_memcpy(p_src + (       7-y)*stride, p_src + (       y+8)*stride, stride);
00303         fast_memcpy(p_src + (height+8+y)*stride, p_src + (height-y+7)*stride, stride);
00304     }
00305     //FIXME (try edge emu)
00306 
00307     for(y=0; y<height; y++){
00308         for(x=-8; x<0; x+=4){
00309             const int index= x + y*stride + (8-3)*(1+stride) + 8; //FIXME silly offset
00310             uint8_t *src  = p_src + index;
00311             DCTELEM *tp= temp+4*x;
00312 
00313             dctA_c(tp+4*8, src, stride);
00314         }
00315         for(x=0; x<width; ){
00316             const int qps= 3 + is_luma;
00317             int qp;
00318             int end= XMIN(x+8, width);
00319 
00320             if(p->qp)
00321                 qp= p->qp;
00322             else{
00323                 qp= qp_store[ (XMIN(x, width-1)>>qps) + (XMIN(y, height-1)>>qps) * qp_stride];
00324                 qp=norm_qscale(qp, p->mpeg2);
00325             }
00326             for(; x<end; x++){
00327                 const int index= x + y*stride + (8-3)*(1+stride) + 8; //FIXME silly offset
00328                 uint8_t *src  = p_src + index;
00329                 DCTELEM *tp= temp+4*x;
00330                 int v;
00331 
00332                 if((x&3)==0)
00333                     dctA_c(tp+4*8, src, stride);
00334 
00335                 dctB(block, tp);
00336 
00337                 v= requantize(block, qp);
00338                 v= (v + dither[y&7][x&7])>>6;
00339                 if((unsigned)v > 255)
00340                     v= (-v)>>31;
00341                 dst[x + y*dst_stride]= v;
00342             }
00343         }
00344     }
00345 }
00346 
00347 static int config(struct vf_instance *vf,
00348     int width, int height, int d_width, int d_height,
00349     unsigned int flags, unsigned int outfmt){
00350     int h= (height+16+15)&(~15);
00351 
00352     vf->priv->temp_stride= (width+16+15)&(~15);
00353     vf->priv->src = av_malloc(vf->priv->temp_stride*(h+8)*sizeof(uint8_t));
00354 
00355     return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
00356 }
00357 
00358 static void get_image(struct vf_instance *vf, mp_image_t *mpi){
00359     if(mpi->flags&MP_IMGFLAG_PRESERVE) return; // don't change
00360     // ok, we can do pp in-place (or pp disabled):
00361     vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
00362         mpi->type, mpi->flags | MP_IMGFLAG_READABLE, mpi->width, mpi->height);
00363     mpi->planes[0]=vf->dmpi->planes[0];
00364     mpi->stride[0]=vf->dmpi->stride[0];
00365     mpi->width=vf->dmpi->width;
00366     if(mpi->flags&MP_IMGFLAG_PLANAR){
00367         mpi->planes[1]=vf->dmpi->planes[1];
00368         mpi->planes[2]=vf->dmpi->planes[2];
00369         mpi->stride[1]=vf->dmpi->stride[1];
00370         mpi->stride[2]=vf->dmpi->stride[2];
00371     }
00372     mpi->flags|=MP_IMGFLAG_DIRECT;
00373 }
00374 
00375 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
00376     mp_image_t *dmpi;
00377 
00378     if(mpi->flags&MP_IMGFLAG_DIRECT){
00379         dmpi=vf->dmpi;
00380     }else{
00381         // no DR, so get a new image! hope we'll get DR buffer:
00382         dmpi=vf_get_image(vf->next,mpi->imgfmt,
00383             MP_IMGTYPE_TEMP,
00384             MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
00385             mpi->width,mpi->height);
00386         vf_clone_mpi_attributes(dmpi, mpi);
00387     }
00388 
00389     vf->priv->mpeg2= mpi->qscale_type;
00390     if(mpi->qscale || vf->priv->qp){
00391         filter(vf->priv, dmpi->planes[0], mpi->planes[0], dmpi->stride[0], mpi->stride[0], mpi->w, mpi->h, mpi->qscale, mpi->qstride, 1);
00392         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);
00393         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);
00394     }else{
00395         memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h, dmpi->stride[0], mpi->stride[0]);
00396         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]);
00397         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]);
00398     }
00399 
00400 #if HAVE_MMX
00401     if(gCpuCaps.hasMMX) __asm__ volatile ("emms\n\t");
00402 #endif
00403 #if HAVE_MMX2
00404     if(gCpuCaps.hasMMX2) __asm__ volatile ("sfence\n\t");
00405 #endif
00406 
00407     return vf_next_put_image(vf,dmpi, pts);
00408 }
00409 
00410 static void uninit(struct vf_instance *vf){
00411     if(!vf->priv) return;
00412 
00413     av_free(vf->priv->src);
00414     vf->priv->src= NULL;
00415 
00416     free(vf->priv);
00417     vf->priv=NULL;
00418 }
00419 
00420 //===========================================================================//
00421 static int query_format(struct vf_instance *vf, unsigned int fmt){
00422     switch(fmt){
00423     case IMGFMT_YVU9:
00424     case IMGFMT_IF09:
00425     case IMGFMT_YV12:
00426     case IMGFMT_I420:
00427     case IMGFMT_IYUV:
00428     case IMGFMT_CLPL:
00429     case IMGFMT_Y800:
00430     case IMGFMT_Y8:
00431     case IMGFMT_444P:
00432     case IMGFMT_422P:
00433     case IMGFMT_411P:
00434         return vf_next_query_format(vf,fmt);
00435     }
00436     return 0;
00437 }
00438 
00439 static int control(struct vf_instance *vf, int request, void* data){
00440     return vf_next_control(vf,request,data);
00441 }
00442 
00443 static int vf_open(vf_instance_t *vf, char *args){
00444     vf->config=config;
00445     vf->put_image=put_image;
00446     vf->get_image=get_image;
00447     vf->query_format=query_format;
00448     vf->uninit=uninit;
00449     vf->control= control;
00450     vf->priv=malloc(sizeof(struct vf_priv_s));
00451     memset(vf->priv, 0, sizeof(struct vf_priv_s));
00452 
00453     if (args) sscanf(args, "%d:%d", &vf->priv->qp, &vf->priv->mode);
00454 
00455     if(vf->priv->qp < 0)
00456         vf->priv->qp = 0;
00457 
00458     init_thres2();
00459 
00460     switch(vf->priv->mode){
00461         case 0: requantize= hardthresh_c; break;
00462         case 1: requantize= softthresh_c; break;
00463         default:
00464         case 2: requantize= mediumthresh_c; break;
00465     }
00466 
00467 #if HAVE_MMX
00468     if(gCpuCaps.hasMMX){
00469         dctB= dctB_mmx;
00470     }
00471 #endif
00472 #if 0
00473     if(gCpuCaps.hasMMX){
00474         switch(vf->priv->mode){
00475             case 0: requantize= hardthresh_mmx; break;
00476             case 1: requantize= softthresh_mmx; break;
00477         }
00478     }
00479 #endif
00480 
00481     return 1;
00482 }
00483 
00484 const vf_info_t vf_info_pp7 = {
00485     "postprocess 7",
00486     "pp7",
00487     "Michael Niedermayer",
00488     "",
00489     vf_open,
00490     NULL
00491 };

Generated on Fri Feb 22 2013 07:24:30 for FFmpeg by  doxygen 1.7.1