Make AVI demuxer more resilient against broken or incomplete files.
[mplayer/glamo.git] / libmpcodecs / vf_pp.c
blob2b4b6931b09ae70a01628d7eafd490a790049202
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <inttypes.h>
5 #include <errno.h>
7 #include "config.h"
8 #include "mp_msg.h"
9 #include "cpudetect.h"
11 #ifdef HAVE_MALLOC_H
12 #include <malloc.h>
13 #endif
15 #include "img_format.h"
16 #include "mp_image.h"
17 #include "vf.h"
18 #include "libpostproc/postprocess.h"
20 #ifdef CONFIG_LIBPOSTPROC_A
21 #define EMU_OLD
22 #include "libpostproc/postprocess_internal.h"
23 #endif
25 struct vf_priv_s {
26 int pp;
27 pp_mode_t *ppMode[PP_QUALITY_MAX+1];
28 void *context;
29 unsigned int outfmt;
32 //===========================================================================//
34 static int config(struct vf_instance_s* vf,
35 int width, int height, int d_width, int d_height,
36 unsigned int voflags, unsigned int outfmt){
37 int flags=
38 (gCpuCaps.hasMMX ? PP_CPU_CAPS_MMX : 0)
39 | (gCpuCaps.hasMMX2 ? PP_CPU_CAPS_MMX2 : 0)
40 | (gCpuCaps.has3DNow ? PP_CPU_CAPS_3DNOW : 0);
42 switch(outfmt){
43 case IMGFMT_444P: flags|= PP_FORMAT_444; break;
44 case IMGFMT_422P: flags|= PP_FORMAT_422; break;
45 case IMGFMT_411P: flags|= PP_FORMAT_411; break;
46 default: flags|= PP_FORMAT_420; break;
49 if(vf->priv->context) pp_free_context(vf->priv->context);
50 vf->priv->context= pp_get_context(width, height, flags);
52 return vf_next_config(vf,width,height,d_width,d_height,voflags,outfmt);
55 static void uninit(struct vf_instance_s* vf){
56 int i;
57 for(i=0; i<=PP_QUALITY_MAX; i++){
58 if(vf->priv->ppMode[i])
59 pp_free_mode(vf->priv->ppMode[i]);
61 if(vf->priv->context) pp_free_context(vf->priv->context);
64 static int query_format(struct vf_instance_s* vf, unsigned int fmt){
65 switch(fmt){
66 case IMGFMT_YV12:
67 case IMGFMT_I420:
68 case IMGFMT_IYUV:
69 case IMGFMT_444P:
70 case IMGFMT_422P:
71 case IMGFMT_411P:
72 return vf_next_query_format(vf,fmt);
74 return 0;
77 static int control(struct vf_instance_s* vf, int request, void* data){
78 switch(request){
79 case VFCTRL_QUERY_MAX_PP_LEVEL:
80 return PP_QUALITY_MAX;
81 case VFCTRL_SET_PP_LEVEL:
82 vf->priv->pp= *((unsigned int*)data);
83 return CONTROL_TRUE;
85 return vf_next_control(vf,request,data);
88 static void get_image(struct vf_instance_s* vf, mp_image_t *mpi){
89 if(vf->priv->pp&0xFFFF) return; // non-local filters enabled
90 if((mpi->type==MP_IMGTYPE_IPB || vf->priv->pp) &&
91 mpi->flags&MP_IMGFLAG_PRESERVE) return; // don't change
92 if(!(mpi->flags&MP_IMGFLAG_ACCEPT_STRIDE) && mpi->imgfmt!=vf->priv->outfmt)
93 return; // colorspace differ
94 // ok, we can do pp in-place (or pp disabled):
95 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
96 mpi->type, mpi->flags | MP_IMGFLAG_READABLE, mpi->width, mpi->height);
97 mpi->planes[0]=vf->dmpi->planes[0];
98 mpi->stride[0]=vf->dmpi->stride[0];
99 mpi->width=vf->dmpi->width;
100 if(mpi->flags&MP_IMGFLAG_PLANAR){
101 mpi->planes[1]=vf->dmpi->planes[1];
102 mpi->planes[2]=vf->dmpi->planes[2];
103 mpi->stride[1]=vf->dmpi->stride[1];
104 mpi->stride[2]=vf->dmpi->stride[2];
106 mpi->flags|=MP_IMGFLAG_DIRECT;
109 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts){
110 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
111 // no DR, so get a new image! hope we'll get DR buffer:
112 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
113 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE |
114 MP_IMGFLAG_PREFER_ALIGNED_STRIDE | MP_IMGFLAG_READABLE,
115 // MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
116 // mpi->w,mpi->h);
117 (mpi->width+7)&(~7),(mpi->height+7)&(~7));
118 vf->dmpi->w=mpi->w; vf->dmpi->h=mpi->h; // display w;h
121 if(vf->priv->pp || !(mpi->flags&MP_IMGFLAG_DIRECT)){
122 // do the postprocessing! (or copy if no DR)
123 pp_postprocess(mpi->planes ,mpi->stride,
124 vf->dmpi->planes,vf->dmpi->stride,
125 (mpi->w+7)&(~7),mpi->h,
126 mpi->qscale, mpi->qstride,
127 vf->priv->ppMode[ vf->priv->pp ], vf->priv->context,
128 #ifdef PP_PICT_TYPE_QP2
129 mpi->pict_type | (mpi->qscale_type ? PP_PICT_TYPE_QP2 : 0));
130 #else
131 mpi->pict_type);
132 #endif
134 return vf_next_put_image(vf,vf->dmpi, pts);
137 //===========================================================================//
139 extern int divx_quality;
141 static unsigned int fmt_list[]={
142 IMGFMT_YV12,
143 IMGFMT_I420,
144 IMGFMT_IYUV,
145 IMGFMT_444P,
146 IMGFMT_422P,
147 IMGFMT_411P,
151 static int open(vf_instance_t *vf, char* args){
152 char *endptr, *name;
153 int i;
154 int hex_mode=0;
156 vf->query_format=query_format;
157 vf->control=control;
158 vf->config=config;
159 vf->get_image=get_image;
160 vf->put_image=put_image;
161 vf->uninit=uninit;
162 vf->default_caps=VFCAP_ACCEPT_STRIDE|VFCAP_POSTPROC;
163 vf->priv=malloc(sizeof(struct vf_priv_s));
164 vf->priv->context=NULL;
166 // check csp:
167 vf->priv->outfmt=vf_match_csp(&vf->next,fmt_list,IMGFMT_YV12);
168 if(!vf->priv->outfmt) return 0; // no csp match :(
170 if(args){
171 hex_mode= strtol(args, &endptr, 0);
172 if(*endptr){
173 name= args;
174 }else
175 name= NULL;
176 }else{
177 name="de";
180 #ifdef EMU_OLD
181 if(name){
182 #endif
183 for(i=0; i<=PP_QUALITY_MAX; i++){
184 vf->priv->ppMode[i]= pp_get_mode_by_name_and_quality(name, i);
185 if(vf->priv->ppMode[i]==NULL) return -1;
187 #ifdef EMU_OLD
188 }else{
189 /* hex mode for compatibility */
190 for(i=0; i<=PP_QUALITY_MAX; i++){
191 PPMode *ppMode;
193 ppMode= (PPMode*)memalign(8, sizeof(PPMode));
195 ppMode->lumMode= hex_mode;
196 ppMode->chromMode= ((hex_mode&0xFF)>>4) | (hex_mode&0xFFFFFF00);
197 ppMode->maxTmpNoise[0]= 700;
198 ppMode->maxTmpNoise[1]= 1500;
199 ppMode->maxTmpNoise[2]= 3000;
200 ppMode->maxAllowedY= 234;
201 ppMode->minAllowedY= 16;
202 ppMode->baseDcDiff= 256/4;
203 ppMode->flatnessThreshold=40;
205 vf->priv->ppMode[i]= ppMode;
208 #endif
210 vf->priv->pp=PP_QUALITY_MAX;
211 return 1;
214 const vf_info_t vf_info_pp = {
215 "postprocessing",
216 "pp",
217 "A'rpi",
219 open,
220 NULL
223 //===========================================================================//