00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <math.h>
00025 #include <inttypes.h>
00026
00027 #include "mp_msg.h"
00028 #include "cpudetect.h"
00029 #include "img_format.h"
00030 #include "mp_image.h"
00031 #include "vf.h"
00032 #include "libvo/fastmemcpy.h"
00033
00034 #include "libavcodec/avcodec.h"
00035 #include "libavutil/eval.h"
00036
00037
00038 struct vf_priv_s {
00039 char eq[200];
00040 int8_t *qp;
00041 int8_t lut[257];
00042 int qp_stride;
00043 };
00044
00045 static int config(struct vf_instance *vf,
00046 int width, int height, int d_width, int d_height,
00047 unsigned int flags, unsigned int outfmt){
00048 int h= (height+15)>>4;
00049 int i;
00050
00051 vf->priv->qp_stride= (width+15)>>4;
00052 vf->priv->qp= av_malloc(vf->priv->qp_stride*h*sizeof(int8_t));
00053
00054 for(i=-129; i<128; i++){
00055 double const_values[]={
00056 M_PI,
00057 M_E,
00058 i != -129,
00059 i,
00060 0
00061 };
00062 static const char *const_names[]={
00063 "PI",
00064 "E",
00065 "known",
00066 "qp",
00067 NULL
00068 };
00069 double temp_val;
00070 int res;
00071
00072 res= av_expr_parse_and_eval(&temp_val, vf->priv->eq, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, NULL);
00073
00074 if (res < 0){
00075 mp_msg(MSGT_VFILTER, MSGL_ERR, "qp: Error evaluating \"%s\" \n", vf->priv->eq);
00076 return 0;
00077 }
00078 vf->priv->lut[i+129]= lrintf(temp_val);
00079 }
00080
00081 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
00082 }
00083
00084 static void get_image(struct vf_instance *vf, mp_image_t *mpi){
00085 if(mpi->flags&MP_IMGFLAG_PRESERVE) return;
00086
00087 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
00088 mpi->type, mpi->flags, mpi->w, mpi->h);
00089 mpi->planes[0]=vf->dmpi->planes[0];
00090 mpi->stride[0]=vf->dmpi->stride[0];
00091 mpi->width=vf->dmpi->width;
00092 if(mpi->flags&MP_IMGFLAG_PLANAR){
00093 mpi->planes[1]=vf->dmpi->planes[1];
00094 mpi->planes[2]=vf->dmpi->planes[2];
00095 mpi->stride[1]=vf->dmpi->stride[1];
00096 mpi->stride[2]=vf->dmpi->stride[2];
00097 }
00098 mpi->flags|=MP_IMGFLAG_DIRECT;
00099 }
00100
00101 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
00102 mp_image_t *dmpi;
00103 int x,y;
00104
00105 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
00106
00107 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
00108 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
00109 mpi->w,mpi->h);
00110 }
00111
00112 dmpi= vf->dmpi;
00113
00114 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
00115 memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h, dmpi->stride[0], mpi->stride[0]);
00116 if(mpi->flags&MP_IMGFLAG_PLANAR){
00117 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]);
00118 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]);
00119 }
00120 }
00121 vf_clone_mpi_attributes(dmpi, mpi);
00122
00123 dmpi->qscale = vf->priv->qp;
00124 dmpi->qstride= vf->priv->qp_stride;
00125 if(mpi->qscale){
00126 for(y=0; y<((dmpi->h+15)>>4); y++){
00127 for(x=0; x<vf->priv->qp_stride; x++){
00128 dmpi->qscale[x + dmpi->qstride*y]=
00129 vf->priv->lut[ 129 + ((int8_t)mpi->qscale[x + mpi->qstride*y]) ];
00130 }
00131 }
00132 }else{
00133 int qp= vf->priv->lut[0];
00134 for(y=0; y<((dmpi->h+15)>>4); y++){
00135 for(x=0; x<vf->priv->qp_stride; x++){
00136 dmpi->qscale[x + dmpi->qstride*y]= qp;
00137 }
00138 }
00139 }
00140
00141 return vf_next_put_image(vf,dmpi, pts);
00142 }
00143
00144 static void uninit(struct vf_instance *vf){
00145 if(!vf->priv) return;
00146
00147 av_free(vf->priv->qp);
00148 vf->priv->qp= NULL;
00149
00150 av_free(vf->priv);
00151 vf->priv=NULL;
00152 }
00153
00154
00155 static int vf_open(vf_instance_t *vf, char *args){
00156 vf->config=config;
00157 vf->put_image=put_image;
00158 vf->get_image=get_image;
00159 vf->uninit=uninit;
00160 vf->priv=av_malloc(sizeof(struct vf_priv_s));
00161 memset(vf->priv, 0, sizeof(struct vf_priv_s));
00162
00163
00164
00165 if (args) strncpy(vf->priv->eq, args, 199);
00166
00167 return 1;
00168 }
00169
00170 const vf_info_t vf_info_qp = {
00171 "QP changer",
00172 "qp",
00173 "Michael Niedermayer",
00174 "",
00175 vf_open,
00176 NULL
00177 };