vf_qp.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004 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 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <math.h>
25 #include <inttypes.h>
26 
27 #include "mp_msg.h"
28 #include "cpudetect.h"
29 #include "img_format.h"
30 #include "mp_image.h"
31 #include "vf.h"
32 #include "libvo/fastmemcpy.h"
33 
34 #include "libavcodec/avcodec.h"
35 #include "libavutil/eval.h"
36 #include "libavutil/mem.h"
37 
38 
39 struct vf_priv_s {
40  char eq[200];
41  int8_t *qp;
42  int8_t lut[257];
43  int qp_stride;
44 };
45 
46 static int config(struct vf_instance *vf,
47  int width, int height, int d_width, int d_height,
48  unsigned int flags, unsigned int outfmt){
49  int h= (height+15)>>4;
50  int i;
51 
52  vf->priv->qp_stride= (width+15)>>4;
53  vf->priv->qp= av_malloc(vf->priv->qp_stride*h*sizeof(int8_t));
54 
55  for(i=-129; i<128; i++){
56  double const_values[]={
57  M_PI,
58  M_E,
59  i != -129,
60  i,
61  0
62  };
63  static const char *const_names[]={
64  "PI",
65  "E",
66  "known",
67  "qp",
68  NULL
69  };
70  double temp_val;
71  int res;
72 
73  res= av_expr_parse_and_eval(&temp_val, vf->priv->eq, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, NULL);
74 
75  if (res < 0){
76  ff_mp_msg(MSGT_VFILTER, MSGL_ERR, "qp: Error evaluating \"%s\" \n", vf->priv->eq);
77  return 0;
78  }
79  vf->priv->lut[i+129]= lrintf(temp_val);
80  }
81 
82  return ff_vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
83 }
84 
85 static void get_image(struct vf_instance *vf, mp_image_t *mpi){
86  if(mpi->flags&MP_IMGFLAG_PRESERVE) return; // don't change
87  // ok, we can do pp in-place (or pp disabled):
88  vf->dmpi=ff_vf_get_image(vf->next,mpi->imgfmt,
89  mpi->type, mpi->flags, mpi->w, mpi->h);
90  mpi->planes[0]=vf->dmpi->planes[0];
91  mpi->stride[0]=vf->dmpi->stride[0];
92  mpi->width=vf->dmpi->width;
93  if(mpi->flags&MP_IMGFLAG_PLANAR){
94  mpi->planes[1]=vf->dmpi->planes[1];
95  mpi->planes[2]=vf->dmpi->planes[2];
96  mpi->stride[1]=vf->dmpi->stride[1];
97  mpi->stride[2]=vf->dmpi->stride[2];
98  }
100 }
101 
102 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
103  mp_image_t *dmpi;
104  int x,y;
105 
106  if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
107  // no DR, so get a new image! hope we'll get DR buffer:
108  vf->dmpi=ff_vf_get_image(vf->next,mpi->imgfmt,
110  mpi->w,mpi->h);
111  }
112 
113  dmpi= vf->dmpi;
114 
115  if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
116  memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h, dmpi->stride[0], mpi->stride[0]);
117  if(mpi->flags&MP_IMGFLAG_PLANAR){
118  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]);
119  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  }
121  }
122  ff_vf_clone_mpi_attributes(dmpi, mpi);
123 
124  dmpi->qscale = vf->priv->qp;
125  dmpi->qstride= vf->priv->qp_stride;
126  if(mpi->qscale){
127  for(y=0; y<((dmpi->h+15)>>4); y++){
128  for(x=0; x<vf->priv->qp_stride; x++){
129  dmpi->qscale[x + dmpi->qstride*y]=
130  vf->priv->lut[ 129 + ((int8_t)mpi->qscale[x + mpi->qstride*y]) ];
131  }
132  }
133  }else{
134  int qp= vf->priv->lut[0];
135  for(y=0; y<((dmpi->h+15)>>4); y++){
136  for(x=0; x<vf->priv->qp_stride; x++){
137  dmpi->qscale[x + dmpi->qstride*y]= qp;
138  }
139  }
140  }
141 
142  return ff_vf_next_put_image(vf,dmpi, pts);
143 }
144 
145 static void uninit(struct vf_instance *vf){
146  if(!vf->priv) return;
147 
148  av_free(vf->priv->qp);
149  vf->priv->qp= NULL;
150 
151  av_free(vf->priv);
152  vf->priv=NULL;
153 }
154 
155 //===========================================================================//
156 static int vf_open(vf_instance_t *vf, char *args){
157  vf->config=config;
158  vf->put_image=put_image;
159  vf->get_image=get_image;
160  vf->uninit=uninit;
161  vf->priv=av_malloc(sizeof(struct vf_priv_s));
162  memset(vf->priv, 0, sizeof(struct vf_priv_s));
163 
164 // avcodec_init();
165 
166  if (args) strncpy(vf->priv->eq, args, 199);
167 
168  return 1;
169 }
170 
172  "QP changer",
173  "qp",
174  "Michael Niedermayer",
175  "",
176  vf_open,
177  NULL
178 };
const vf_info_t ff_vf_info_qp
Definition: vf_qp.c:171
unsigned int imgfmt
Definition: mp_image.h:130
void(* get_image)(struct vf_instance *vf, mp_image_t *mpi)
Definition: vf.h:66
#define MSGL_ERR
Definition: mp_msg.h:33
char * qscale
Definition: mp_image.h:135
memory handling functions
int8_t lut[257]
Definition: vf_qp.c:42
mp_image_t * ff_vf_get_image(vf_instance_t *vf, unsigned int outfmt, int mp_imgtype, int mp_imgflag, int w, int h)
Definition: vf_mp.c:380
#define MP_IMGFLAG_PRESERVE
Definition: mp_image.h:46
void(* uninit)(struct vf_instance *vf)
Definition: vf.h:74
int ff_vf_next_config(struct vf_instance *vf, int width, int height, int d_width, int d_height, unsigned int flags, unsigned int outfmt)
Definition: vf_mp.c:584
int width
Definition: mp_image.h:131
int(* put_image)(struct vf_instance *vf, mp_image_t *mpi, double pts)
Definition: vf.h:68
Definition: vf.h:31
unsigned char * planes[MP_MAX_PLANES]
Definition: mp_image.h:133
int8_t * qp
Definition: vf_qp.c:41
int stride[MP_MAX_PLANES]
Definition: mp_image.h:134
static const double const_values[]
Definition: opt.c:119
#define lrintf(x)
Definition: libm_mips.h:70
static int vf_open(vf_instance_t *vf, char *args)
Definition: vf_qp.c:156
int width
Definition: vf_fil.c:32
int qstride
Definition: mp_image.h:136
Discrete Time axis x
int av_expr_parse_and_eval(double *d, const char *s, const char *const *const_names, const double *const_values, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), void *opaque, int log_offset, void *log_ctx)
Parse and evaluate an expression.
Definition: eval.c:701
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
#define MP_IMGTYPE_TEMP
Definition: mp_image.h:104
int qp_stride
Definition: vf_qp.c:43
int qp
Definition: vf_fspp.c:95
#define MSGT_VFILTER
Definition: mp_msg.h:94
static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
Definition: vf_qp.c:102
external API header
static const char *const const_names[]
Definition: opt.c:126
#define MP_IMGFLAG_PREFER_ALIGNED_STRIDE
Definition: mp_image.h:61
#define M_E
Definition: ratecontrol.c:37
int chroma_y_shift
Definition: mp_image.h:145
void ff_vf_clone_mpi_attributes(mp_image_t *dst, mp_image_t *src)
Definition: vf_mp.c:293
struct vf_instance * next
Definition: vf.h:84
NULL
Definition: eval.c:55
#define memcpy_pic(d, s, b, h, ds, ss)
Definition: fastmemcpy.h:62
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
synthesis window for stochastic i
int w
Definition: mp_image.h:132
static void get_image(struct vf_instance *vf, mp_image_t *mpi)
Definition: vf_qp.c:85
int(* config)(struct vf_instance *vf, int width, int height, int d_width, int d_height, unsigned int flags, unsigned int outfmt)
Definition: vf.h:59
static int flags
Definition: cpu.c:23
static int config(struct vf_instance *vf, int width, int height, int d_width, int d_height, unsigned int flags, unsigned int outfmt)
Definition: vf_qp.c:46
unsigned char type
Definition: mp_image.h:127
void ff_mp_msg(int mod, int lev, const char *format,...)
Definition: vf_mp.c:275
#define MP_IMGFLAG_PLANAR
Definition: mp_image.h:76
function y
Definition: D.m:1
int ff_vf_next_put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
Definition: vf_mp.c:539
int chroma_x_shift
Definition: mp_image.h:144
char eq[200]
Definition: vf_qp.c:40
#define MP_IMGFLAG_ACCEPT_STRIDE
Definition: mp_image.h:63
struct vf_priv_s * priv
Definition: vf.h:86
#define M_PI
Definition: mathematics.h:46
#define MP_IMGFLAG_DIRECT
Definition: mp_image.h:91
int h
Definition: mp_image.h:132
static void uninit(struct vf_instance *vf)
Definition: vf_qp.c:145
Definition: vf.h:56
unsigned int flags
Definition: mp_image.h:126
int height
Definition: vf_fil.c:31
mp_image_t * dmpi
Definition: vf.h:85
simple arithmetic expression evaluator