Update to MPlayer SVN rev 32628 and FFmpeg SVN rev 25754.
[vaapi:mplayer.git] / libmpcodecs / vf_pp.c
1 /*
2  * This file is part of MPlayer.
3  *
4  * MPlayer is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * MPlayer is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <inttypes.h>
23 #include <errno.h>
24
25 #include "config.h"
26 #include "mp_msg.h"
27 #include "cpudetect.h"
28
29 #if HAVE_MALLOC_H
30 #include <malloc.h>
31 #endif
32
33 #include "dec_video.h"
34 #include "img_format.h"
35 #include "mp_image.h"
36 #include "vf.h"
37 #include "libavutil/internal.h"
38 #include "libpostproc/postprocess.h"
39
40 #ifdef CONFIG_FFMPEG_A
41 #define EMU_OLD
42 #include "libpostproc/postprocess_internal.h"
43 #endif
44
45 #undef malloc
46
47 struct vf_priv_s {
48     int pp;
49     pp_mode_t *ppMode[PP_QUALITY_MAX+1];
50     void *context;
51     unsigned int outfmt;
52 };
53
54 //===========================================================================//
55
56 static int config(struct vf_instance *vf,
57         int width, int height, int d_width, int d_height,
58         unsigned int voflags, unsigned int outfmt){
59     int flags=
60           (gCpuCaps.hasMMX   ? PP_CPU_CAPS_MMX   : 0)
61         | (gCpuCaps.hasMMX2  ? PP_CPU_CAPS_MMX2  : 0)
62         | (gCpuCaps.has3DNow ? PP_CPU_CAPS_3DNOW : 0);
63
64     switch(outfmt){
65     case IMGFMT_444P: flags|= PP_FORMAT_444; break;
66     case IMGFMT_422P: flags|= PP_FORMAT_422; break;
67     case IMGFMT_411P: flags|= PP_FORMAT_411; break;
68     default:          flags|= PP_FORMAT_420; break;
69     }
70
71     if(vf->priv->context) pp_free_context(vf->priv->context);
72     vf->priv->context= pp_get_context(width, height, flags);
73
74     return vf_next_config(vf,width,height,d_width,d_height,voflags,outfmt);
75 }
76
77 static void uninit(struct vf_instance *vf){
78     int i;
79     for(i=0; i<=PP_QUALITY_MAX; i++){
80         if(vf->priv->ppMode[i])
81             pp_free_mode(vf->priv->ppMode[i]);
82     }
83     if(vf->priv->context) pp_free_context(vf->priv->context);
84 }
85
86 static int query_format(struct vf_instance *vf, unsigned int fmt){
87     switch(fmt){
88     case IMGFMT_YV12:
89     case IMGFMT_I420:
90     case IMGFMT_IYUV:
91     case IMGFMT_444P:
92     case IMGFMT_422P:
93     case IMGFMT_411P:
94         return vf_next_query_format(vf,fmt);
95     }
96     return 0;
97 }
98
99 static int control(struct vf_instance *vf, int request, void* data){
100     switch(request){
101     case VFCTRL_QUERY_MAX_PP_LEVEL:
102         return PP_QUALITY_MAX;
103     case VFCTRL_SET_PP_LEVEL:
104         vf->priv->pp= *((unsigned int*)data);
105         return CONTROL_TRUE;
106     }
107     return vf_next_control(vf,request,data);
108 }
109
110 static void get_image(struct vf_instance *vf, mp_image_t *mpi){
111     if(vf->priv->pp&0xFFFF) return; // non-local filters enabled
112     if((mpi->type==MP_IMGTYPE_IPB || vf->priv->pp) &&
113         mpi->flags&MP_IMGFLAG_PRESERVE) return; // don't change
114     if(!(mpi->flags&MP_IMGFLAG_ACCEPT_STRIDE) && mpi->imgfmt!=vf->priv->outfmt)
115         return; // colorspace differ
116     // ok, we can do pp in-place (or pp disabled):
117     vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
118         mpi->type, mpi->flags | MP_IMGFLAG_READABLE, mpi->width, mpi->height);
119     mpi->planes[0]=vf->dmpi->planes[0];
120     mpi->stride[0]=vf->dmpi->stride[0];
121     mpi->width=vf->dmpi->width;
122     if(mpi->flags&MP_IMGFLAG_PLANAR){
123         mpi->planes[1]=vf->dmpi->planes[1];
124         mpi->planes[2]=vf->dmpi->planes[2];
125         mpi->stride[1]=vf->dmpi->stride[1];
126         mpi->stride[2]=vf->dmpi->stride[2];
127     }
128     mpi->flags|=MP_IMGFLAG_DIRECT;
129 }
130
131 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
132     if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
133         // no DR, so get a new image! hope we'll get DR buffer:
134         vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
135             MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE |
136             MP_IMGFLAG_PREFER_ALIGNED_STRIDE | MP_IMGFLAG_READABLE,
137 //          MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
138 //          mpi->w,mpi->h);
139             (mpi->width+7)&(~7),(mpi->height+7)&(~7));
140         vf->dmpi->w=mpi->w; vf->dmpi->h=mpi->h; // display w;h
141     }
142
143     if(vf->priv->pp || !(mpi->flags&MP_IMGFLAG_DIRECT)){
144         // do the postprocessing! (or copy if no DR)
145         pp_postprocess(mpi->planes           ,mpi->stride,
146                     vf->dmpi->planes,vf->dmpi->stride,
147                     (mpi->w+7)&(~7),mpi->h,
148                     mpi->qscale, mpi->qstride,
149                     vf->priv->ppMode[ vf->priv->pp ], vf->priv->context,
150 #ifdef PP_PICT_TYPE_QP2
151                     mpi->pict_type | (mpi->qscale_type ? PP_PICT_TYPE_QP2 : 0));
152 #else
153                     mpi->pict_type);
154 #endif
155     }
156     return vf_next_put_image(vf,vf->dmpi, pts);
157 }
158
159 //===========================================================================//
160
161 static const unsigned int fmt_list[]={
162     IMGFMT_YV12,
163     IMGFMT_I420,
164     IMGFMT_IYUV,
165     IMGFMT_444P,
166     IMGFMT_422P,
167     IMGFMT_411P,
168     0
169 };
170
171 static int vf_open(vf_instance_t *vf, char *args){
172     char *endptr, *name;
173     int i;
174     int hex_mode=0;
175
176     vf->query_format=query_format;
177     vf->control=control;
178     vf->config=config;
179     vf->get_image=get_image;
180     vf->put_image=put_image;
181     vf->uninit=uninit;
182     vf->default_caps=VFCAP_ACCEPT_STRIDE|VFCAP_POSTPROC;
183     vf->priv=malloc(sizeof(struct vf_priv_s));
184     vf->priv->context=NULL;
185
186     // check csp:
187     vf->priv->outfmt=vf_match_csp(&vf->next,fmt_list,IMGFMT_YV12);
188     if(!vf->priv->outfmt) return 0; // no csp match :(
189
190     if(args){
191         hex_mode= strtol(args, &endptr, 0);
192         if(*endptr){
193             name= args;
194         }else
195             name= NULL;
196     }else{
197         name="de";
198     }
199
200 #ifdef EMU_OLD
201     if(name){
202 #endif
203         for(i=0; i<=PP_QUALITY_MAX; i++){
204             vf->priv->ppMode[i]= pp_get_mode_by_name_and_quality(name, i);
205             if(vf->priv->ppMode[i]==NULL) return -1;
206         }
207 #ifdef EMU_OLD
208     }else{
209         /* hex mode for compatibility */
210         for(i=0; i<=PP_QUALITY_MAX; i++){
211             PPMode *ppMode;
212
213             ppMode= (PPMode*)memalign(8, sizeof(PPMode));
214
215             ppMode->lumMode= hex_mode;
216             ppMode->chromMode= ((hex_mode&0xFF)>>4) | (hex_mode&0xFFFFFF00);
217             ppMode->maxTmpNoise[0]= 700;
218             ppMode->maxTmpNoise[1]= 1500;
219             ppMode->maxTmpNoise[2]= 3000;
220             ppMode->maxAllowedY= 234;
221             ppMode->minAllowedY= 16;
222             ppMode->baseDcDiff= 256/4;
223             ppMode->flatnessThreshold=40;
224
225             vf->priv->ppMode[i]= ppMode;
226         }
227     }
228 #endif
229
230     vf->priv->pp=PP_QUALITY_MAX;
231     return 1;
232 }
233
234 const vf_info_t vf_info_pp = {
235     "postprocessing",
236     "pp",
237     "A'rpi",
238     "",
239     vf_open,
240     NULL
241 };
242
243 //===========================================================================//