]> git.sesse.net Git - ffmpeg/blob - libavfilter/libmpcodecs/vf_pp.c
2624d67b0cd7338ba1dbc1ab5d560a2335b5247d
[ffmpeg] / libavfilter / 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 "img_format.h"
34 #include "mp_image.h"
35 #include "vf.h"
36 #include "libpostproc/postprocess.h"
37
38 #ifdef CONFIG_FFMPEG_A
39 #define EMU_OLD
40 #include "libpostproc/postprocess_internal.h"
41 #endif
42
43 #undef malloc
44
45 struct vf_priv_s {
46     int pp;
47     pp_mode *ppMode[PP_QUALITY_MAX+1];
48     void *context;
49     unsigned int outfmt;
50 };
51
52 //===========================================================================//
53
54 static int config(struct vf_instance *vf,
55         int width, int height, int d_width, int d_height,
56         unsigned int voflags, unsigned int outfmt){
57     int flags= PP_CPU_CAPS_AUTO;
58
59     switch(outfmt){
60     case IMGFMT_444P: flags|= PP_FORMAT_444; break;
61     case IMGFMT_422P: flags|= PP_FORMAT_422; break;
62     case IMGFMT_411P: flags|= PP_FORMAT_411; break;
63     default:          flags|= PP_FORMAT_420; break;
64     }
65
66     if(vf->priv->context) pp_free_context(vf->priv->context);
67     vf->priv->context= pp_get_context(width, height, flags);
68
69     return ff_vf_next_config(vf,width,height,d_width,d_height,voflags,outfmt);
70 }
71
72 static void uninit(struct vf_instance *vf){
73     int i;
74     for(i=0; i<=PP_QUALITY_MAX; i++){
75         if(vf->priv->ppMode[i])
76             pp_free_mode(vf->priv->ppMode[i]);
77     }
78     if(vf->priv->context) pp_free_context(vf->priv->context);
79     free(vf->priv);
80 }
81
82 static int query_format(struct vf_instance *vf, unsigned int fmt){
83     switch(fmt){
84     case IMGFMT_YV12:
85     case IMGFMT_I420:
86     case IMGFMT_IYUV:
87     case IMGFMT_444P:
88     case IMGFMT_422P:
89     case IMGFMT_411P:
90         return ff_vf_next_query_format(vf,fmt);
91     }
92     return 0;
93 }
94
95 static int control(struct vf_instance *vf, int request, void* data){
96     switch(request){
97     case VFCTRL_QUERY_MAX_PP_LEVEL:
98         return PP_QUALITY_MAX;
99     case VFCTRL_SET_PP_LEVEL:
100         vf->priv->pp= *((unsigned int*)data);
101         return CONTROL_TRUE;
102     }
103     return ff_vf_next_control(vf,request,data);
104 }
105
106 static void get_image(struct vf_instance *vf, mp_image_t *mpi){
107     if(vf->priv->pp&0xFFFF) return; // non-local filters enabled
108     if((mpi->type==MP_IMGTYPE_IPB || vf->priv->pp) &&
109         mpi->flags&MP_IMGFLAG_PRESERVE) return; // don't change
110     if(!(mpi->flags&MP_IMGFLAG_ACCEPT_STRIDE) && mpi->imgfmt!=vf->priv->outfmt)
111         return; // colorspace differ
112     // ok, we can do pp in-place (or pp disabled):
113     vf->dmpi=ff_vf_get_image(vf->next,mpi->imgfmt,
114         mpi->type, mpi->flags | MP_IMGFLAG_READABLE, mpi->width, mpi->height);
115     mpi->planes[0]=vf->dmpi->planes[0];
116     mpi->stride[0]=vf->dmpi->stride[0];
117     mpi->width=vf->dmpi->width;
118     if(mpi->flags&MP_IMGFLAG_PLANAR){
119         mpi->planes[1]=vf->dmpi->planes[1];
120         mpi->planes[2]=vf->dmpi->planes[2];
121         mpi->stride[1]=vf->dmpi->stride[1];
122         mpi->stride[2]=vf->dmpi->stride[2];
123     }
124     mpi->flags|=MP_IMGFLAG_DIRECT;
125 }
126
127 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
128     if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
129         // no DR, so get a new image! hope we'll get DR buffer:
130         vf->dmpi=ff_vf_get_image(vf->next,mpi->imgfmt,
131             MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE |
132             MP_IMGFLAG_PREFER_ALIGNED_STRIDE | MP_IMGFLAG_READABLE,
133 //            MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
134 //            mpi->w,mpi->h);
135             (mpi->width+7)&(~7),(mpi->height+7)&(~7));
136         vf->dmpi->w=mpi->w; vf->dmpi->h=mpi->h; // display w;h
137     }
138
139     if(vf->priv->pp || !(mpi->flags&MP_IMGFLAG_DIRECT)){
140         // do the postprocessing! (or copy if no DR)
141         pp_postprocess(mpi->planes           ,mpi->stride,
142                     vf->dmpi->planes,vf->dmpi->stride,
143                     (mpi->w+7)&(~7),mpi->h,
144                     mpi->qscale, mpi->qstride,
145                     vf->priv->ppMode[ vf->priv->pp ], vf->priv->context,
146 #ifdef PP_PICT_TYPE_QP2
147                     mpi->pict_type | (mpi->qscale_type ? PP_PICT_TYPE_QP2 : 0));
148 #else
149                     mpi->pict_type);
150 #endif
151     }
152     return ff_vf_next_put_image(vf,vf->dmpi, pts);
153 }
154
155 //===========================================================================//
156
157 static const unsigned int fmt_list[]={
158     IMGFMT_YV12,
159     IMGFMT_I420,
160     IMGFMT_IYUV,
161     IMGFMT_444P,
162     IMGFMT_422P,
163     IMGFMT_411P,
164     0
165 };
166
167 static int vf_open(vf_instance_t *vf, char *args){
168     char *endptr, *name;
169     int i;
170     int hex_mode=0;
171
172     vf->query_format=query_format;
173     vf->control=control;
174     vf->config=config;
175     vf->get_image=get_image;
176     vf->put_image=put_image;
177     vf->uninit=uninit;
178     vf->default_caps=VFCAP_ACCEPT_STRIDE|VFCAP_POSTPROC;
179     vf->priv=malloc(sizeof(struct vf_priv_s));
180     vf->priv->context=NULL;
181
182     // check csp:
183     vf->priv->outfmt=ff_vf_match_csp(&vf->next,fmt_list,IMGFMT_YV12);
184     if(!vf->priv->outfmt) return 0; // no csp match :(
185
186     if(args && *args){
187         hex_mode= strtol(args, &endptr, 0);
188         if(*endptr){
189             name= args;
190         }else
191             name= NULL;
192     }else{
193         name="de";
194     }
195
196 #ifdef EMU_OLD
197     if(name){
198 #endif
199         for(i=0; i<=PP_QUALITY_MAX; i++){
200             vf->priv->ppMode[i]= pp_get_mode_by_name_and_quality(name, i);
201             if(vf->priv->ppMode[i]==NULL) return -1;
202         }
203 #ifdef EMU_OLD
204     }else{
205         /* hex mode for compatibility */
206         for(i=0; i<=PP_QUALITY_MAX; i++){
207             PPMode *ppMode;
208
209             ppMode = av_malloc(sizeof(PPMode));
210
211             ppMode->lumMode= hex_mode;
212             ppMode->chromMode= ((hex_mode&0xFF)>>4) | (hex_mode&0xFFFFFF00);
213             ppMode->maxTmpNoise[0]= 700;
214             ppMode->maxTmpNoise[1]= 1500;
215             ppMode->maxTmpNoise[2]= 3000;
216             ppMode->maxAllowedY= 234;
217             ppMode->minAllowedY= 16;
218             ppMode->baseDcDiff= 256/4;
219             ppMode->flatnessThreshold=40;
220
221             vf->priv->ppMode[i]= ppMode;
222         }
223     }
224 #endif
225
226     vf->priv->pp=PP_QUALITY_MAX;
227     return 1;
228 }
229
230 const vf_info_t ff_vf_info_pp = {
231     "postprocessing",
232     "pp",
233     "A'rpi",
234     "",
235     vf_open,
236     NULL
237 };
238
239 //===========================================================================//