ao_pulse: work around PulseAudio timing bugs
[mplayer.git] / libmpcodecs / vf_qp.c
blob5e418d2814f34d2860ee1979a0310748232e7615
1 /*
2 * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of MPlayer.
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.
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.
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.
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <math.h>
25 #include <inttypes.h>
27 #include <libavutil/eval.h>
29 #include "mp_msg.h"
30 #include "cpudetect.h"
31 #include "img_format.h"
32 #include "mp_image.h"
33 #include "vf.h"
34 #include "libvo/fastmemcpy.h"
37 struct vf_priv_s {
38 char eq[200];
39 int8_t *qp;
40 int8_t lut[257];
41 int qp_stride;
44 static int config(struct vf_instance *vf,
45 int width, int height, int d_width, int d_height,
46 unsigned int flags, unsigned int outfmt){
47 int h= (height+15)>>4;
48 int i;
50 vf->priv->qp_stride= (width+15)>>4;
51 vf->priv->qp= av_malloc(vf->priv->qp_stride*h*sizeof(int8_t));
53 for(i=-129; i<128; i++){
54 double const_values[]={
55 M_PI,
56 M_E,
57 i != -129,
61 const char * const const_names[]={
62 "PI",
63 "E",
64 "known",
65 "qp",
66 NULL
68 double temp_val;
69 int res;
71 res= av_expr_parse_and_eval(&temp_val, vf->priv->eq, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, NULL);
73 if (res < 0){
74 mp_msg(MSGT_VFILTER, MSGL_ERR, "qp: Error evaluating \"%s\" \n", vf->priv->eq);
75 return 0;
77 vf->priv->lut[i+129]= lrintf(temp_val);
80 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
83 static void get_image(struct vf_instance *vf, mp_image_t *mpi){
84 if(mpi->flags&MP_IMGFLAG_PRESERVE) return; // don't change
85 // ok, we can do pp in-place (or pp disabled):
86 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
87 mpi->type, mpi->flags, mpi->w, mpi->h);
88 mpi->planes[0]=vf->dmpi->planes[0];
89 mpi->stride[0]=vf->dmpi->stride[0];
90 mpi->width=vf->dmpi->width;
91 if(mpi->flags&MP_IMGFLAG_PLANAR){
92 mpi->planes[1]=vf->dmpi->planes[1];
93 mpi->planes[2]=vf->dmpi->planes[2];
94 mpi->stride[1]=vf->dmpi->stride[1];
95 mpi->stride[2]=vf->dmpi->stride[2];
97 mpi->flags|=MP_IMGFLAG_DIRECT;
100 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
101 mp_image_t *dmpi;
102 int x,y;
104 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
105 // no DR, so get a new image! hope we'll get DR buffer:
106 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
107 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
108 mpi->w,mpi->h);
111 dmpi= vf->dmpi;
113 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
114 memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h, dmpi->stride[0], mpi->stride[0]);
115 if(mpi->flags&MP_IMGFLAG_PLANAR){
116 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]);
117 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]);
120 vf_clone_mpi_attributes(dmpi, mpi);
122 dmpi->qscale = vf->priv->qp;
123 dmpi->qstride= vf->priv->qp_stride;
124 if(mpi->qscale){
125 for(y=0; y<((dmpi->h+15)>>4); y++){
126 for(x=0; x<vf->priv->qp_stride; x++){
127 dmpi->qscale[x + dmpi->qstride*y]=
128 vf->priv->lut[ 129 + ((int8_t)mpi->qscale[x + mpi->qstride*y]) ];
131 }else{
132 int qp= vf->priv->lut[0];
133 for(y=0; y<((dmpi->h+15)>>4); y++){
134 for(x=0; x<vf->priv->qp_stride; x++){
135 dmpi->qscale[x + dmpi->qstride*y]= qp;
140 return vf_next_put_image(vf,dmpi, pts);
143 static void uninit(struct vf_instance *vf){
144 if(!vf->priv) return;
146 av_free(vf->priv->qp);
147 vf->priv->qp= NULL;
149 av_free(vf->priv);
150 vf->priv=NULL;
153 //===========================================================================//
154 static int vf_open(vf_instance_t *vf, char *args){
155 vf->config=config;
156 vf->put_image=put_image;
157 vf->get_image=get_image;
158 vf->uninit=uninit;
159 vf->priv=av_malloc(sizeof(struct vf_priv_s));
160 memset(vf->priv, 0, sizeof(struct vf_priv_s));
162 // avcodec_init();
164 if (args) strncpy(vf->priv->eq, args, 199);
166 return 1;
169 const vf_info_t vf_info_qp = {
170 "QP changer",
171 "qp",
172 "Michael Niedermayer",
174 vf_open,
175 NULL