vf_qp.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <math.h>
  24. #include <inttypes.h>
  25. #include "mp_msg.h"
  26. #include "cpudetect.h"
  27. #include "img_format.h"
  28. #include "mp_image.h"
  29. #include "vf.h"
  30. #include "libvo/fastmemcpy.h"
  31. #include "libavcodec/avcodec.h"
  32. #include "libavutil/eval.h"
  33. struct vf_priv_s {
  34. char eq[200];
  35. int8_t *qp;
  36. int8_t lut[257];
  37. int qp_stride;
  38. };
  39. static int config(struct vf_instance *vf,
  40. int width, int height, int d_width, int d_height,
  41. unsigned int flags, unsigned int outfmt){
  42. int h= (height+15)>>4;
  43. int i;
  44. vf->priv->qp_stride= (width+15)>>4;
  45. vf->priv->qp= av_malloc(vf->priv->qp_stride*h*sizeof(int8_t));
  46. for(i=-129; i<128; i++){
  47. double const_values[]={
  48. M_PI,
  49. M_E,
  50. i != -129,
  51. i,
  52. 0
  53. };
  54. static const char *const_names[]={
  55. "PI",
  56. "E",
  57. "known",
  58. "qp",
  59. NULL
  60. };
  61. double temp_val;
  62. int res;
  63. res= av_expr_parse_and_eval(&temp_val, vf->priv->eq, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, NULL);
  64. if (res < 0){
  65. mp_msg(MSGT_VFILTER, MSGL_ERR, "qp: Error evaluating \"%s\" \n", vf->priv->eq);
  66. return 0;
  67. }
  68. vf->priv->lut[i+129]= lrintf(temp_val);
  69. }
  70. return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
  71. }
  72. static void get_image(struct vf_instance *vf, mp_image_t *mpi){
  73. if(mpi->flags&MP_IMGFLAG_PRESERVE) return; // don't change
  74. // ok, we can do pp in-place (or pp disabled):
  75. vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
  76. mpi->type, mpi->flags, mpi->w, mpi->h);
  77. mpi->planes[0]=vf->dmpi->planes[0];
  78. mpi->stride[0]=vf->dmpi->stride[0];
  79. mpi->width=vf->dmpi->width;
  80. if(mpi->flags&MP_IMGFLAG_PLANAR){
  81. mpi->planes[1]=vf->dmpi->planes[1];
  82. mpi->planes[2]=vf->dmpi->planes[2];
  83. mpi->stride[1]=vf->dmpi->stride[1];
  84. mpi->stride[2]=vf->dmpi->stride[2];
  85. }
  86. mpi->flags|=MP_IMGFLAG_DIRECT;
  87. }
  88. static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
  89. mp_image_t *dmpi;
  90. int x,y;
  91. if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
  92. // no DR, so get a new image! hope we'll get DR buffer:
  93. vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
  94. MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
  95. mpi->w,mpi->h);
  96. }
  97. dmpi= vf->dmpi;
  98. if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
  99. memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h, dmpi->stride[0], mpi->stride[0]);
  100. if(mpi->flags&MP_IMGFLAG_PLANAR){
  101. 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]);
  102. 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]);
  103. }
  104. }
  105. vf_clone_mpi_attributes(dmpi, mpi);
  106. dmpi->qscale = vf->priv->qp;
  107. dmpi->qstride= vf->priv->qp_stride;
  108. if(mpi->qscale){
  109. for(y=0; y<((dmpi->h+15)>>4); y++){
  110. for(x=0; x<vf->priv->qp_stride; x++){
  111. dmpi->qscale[x + dmpi->qstride*y]=
  112. vf->priv->lut[ 129 + ((int8_t)mpi->qscale[x + mpi->qstride*y]) ];
  113. }
  114. }
  115. }else{
  116. int qp= vf->priv->lut[0];
  117. for(y=0; y<((dmpi->h+15)>>4); y++){
  118. for(x=0; x<vf->priv->qp_stride; x++){
  119. dmpi->qscale[x + dmpi->qstride*y]= qp;
  120. }
  121. }
  122. }
  123. return vf_next_put_image(vf,dmpi, pts);
  124. }
  125. static void uninit(struct vf_instance *vf){
  126. if(!vf->priv) return;
  127. av_free(vf->priv->qp);
  128. vf->priv->qp= NULL;
  129. av_free(vf->priv);
  130. vf->priv=NULL;
  131. }
  132. //===========================================================================//
  133. static int vf_open(vf_instance_t *vf, char *args){
  134. vf->config=config;
  135. vf->put_image=put_image;
  136. vf->get_image=get_image;
  137. vf->uninit=uninit;
  138. vf->priv=av_malloc(sizeof(struct vf_priv_s));
  139. memset(vf->priv, 0, sizeof(struct vf_priv_s));
  140. // avcodec_init();
  141. if (args) strncpy(vf->priv->eq, args, 199);
  142. return 1;
  143. }
  144. const vf_info_t vf_info_qp = {
  145. "QP changer",
  146. "qp",
  147. "Michael Niedermayer",
  148. "",
  149. vf_open,
  150. NULL
  151. };