Configure needs AS to be set for the Makefiles.
[mplayer/glamo.git] / libmpcodecs / vf.c
blob46e41c37168bcb4941cd5ac710f5c76177ae1d3e
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
5 #include "config.h"
6 #if HAVE_MALLOC_H
7 #include <malloc.h>
8 #endif
10 #ifdef MP_DEBUG
11 #include <assert.h>
12 #endif
14 #include "mp_msg.h"
15 #include "help_mp.h"
16 #include "m_option.h"
17 #include "m_struct.h"
20 #include "img_format.h"
21 #include "mp_image.h"
22 #include "vf.h"
24 #include "libvo/fastmemcpy.h"
26 extern const vf_info_t vf_info_vo;
27 extern const vf_info_t vf_info_rectangle;
28 extern const vf_info_t vf_info_bmovl;
29 extern const vf_info_t vf_info_crop;
30 extern const vf_info_t vf_info_expand;
31 extern const vf_info_t vf_info_pp;
32 extern const vf_info_t vf_info_scale;
33 extern const vf_info_t vf_info_format;
34 extern const vf_info_t vf_info_noformat;
35 extern const vf_info_t vf_info_yuy2;
36 extern const vf_info_t vf_info_flip;
37 extern const vf_info_t vf_info_rgb2bgr;
38 extern const vf_info_t vf_info_rotate;
39 extern const vf_info_t vf_info_mirror;
40 extern const vf_info_t vf_info_palette;
41 extern const vf_info_t vf_info_lavc;
42 extern const vf_info_t vf_info_zrmjpeg;
43 extern const vf_info_t vf_info_dvbscale;
44 extern const vf_info_t vf_info_cropdetect;
45 extern const vf_info_t vf_info_test;
46 extern const vf_info_t vf_info_noise;
47 extern const vf_info_t vf_info_yvu9;
48 extern const vf_info_t vf_info_lavcdeint;
49 extern const vf_info_t vf_info_eq;
50 extern const vf_info_t vf_info_eq2;
51 extern const vf_info_t vf_info_halfpack;
52 extern const vf_info_t vf_info_dint;
53 extern const vf_info_t vf_info_1bpp;
54 extern const vf_info_t vf_info_2xsai;
55 extern const vf_info_t vf_info_unsharp;
56 extern const vf_info_t vf_info_swapuv;
57 extern const vf_info_t vf_info_il;
58 extern const vf_info_t vf_info_fil;
59 extern const vf_info_t vf_info_boxblur;
60 extern const vf_info_t vf_info_sab;
61 extern const vf_info_t vf_info_smartblur;
62 extern const vf_info_t vf_info_perspective;
63 extern const vf_info_t vf_info_down3dright;
64 extern const vf_info_t vf_info_field;
65 extern const vf_info_t vf_info_denoise3d;
66 extern const vf_info_t vf_info_hqdn3d;
67 extern const vf_info_t vf_info_detc;
68 extern const vf_info_t vf_info_telecine;
69 extern const vf_info_t vf_info_tinterlace;
70 extern const vf_info_t vf_info_tfields;
71 extern const vf_info_t vf_info_ivtc;
72 extern const vf_info_t vf_info_ilpack;
73 extern const vf_info_t vf_info_dsize;
74 extern const vf_info_t vf_info_decimate;
75 extern const vf_info_t vf_info_softpulldown;
76 extern const vf_info_t vf_info_pullup;
77 extern const vf_info_t vf_info_filmdint;
78 extern const vf_info_t vf_info_framestep;
79 extern const vf_info_t vf_info_tile;
80 extern const vf_info_t vf_info_delogo;
81 extern const vf_info_t vf_info_remove_logo;
82 extern const vf_info_t vf_info_hue;
83 extern const vf_info_t vf_info_spp;
84 extern const vf_info_t vf_info_uspp;
85 extern const vf_info_t vf_info_fspp;
86 extern const vf_info_t vf_info_pp7;
87 extern const vf_info_t vf_info_yuvcsp;
88 extern const vf_info_t vf_info_kerndeint;
89 extern const vf_info_t vf_info_rgbtest;
90 extern const vf_info_t vf_info_qp;
91 extern const vf_info_t vf_info_phase;
92 extern const vf_info_t vf_info_divtc;
93 extern const vf_info_t vf_info_harddup;
94 extern const vf_info_t vf_info_softskip;
95 extern const vf_info_t vf_info_screenshot;
96 extern const vf_info_t vf_info_ass;
97 extern const vf_info_t vf_info_mcdeint;
98 extern const vf_info_t vf_info_yadif;
99 extern const vf_info_t vf_info_blackframe;
100 extern const vf_info_t vf_info_geq;
101 extern const vf_info_t vf_info_ow;
103 // list of available filters:
104 static const vf_info_t* const filter_list[]={
105 &vf_info_rectangle,
106 #ifdef HAVE_POSIX_SELECT
107 &vf_info_bmovl,
108 #endif
109 &vf_info_crop,
110 &vf_info_expand,
111 #ifdef CONFIG_LIBPOSTPROC
112 &vf_info_pp,
113 #endif
114 &vf_info_scale,
115 // &vf_info_osd,
116 &vf_info_vo,
117 &vf_info_format,
118 &vf_info_noformat,
119 &vf_info_yuy2,
120 &vf_info_flip,
121 &vf_info_rgb2bgr,
122 &vf_info_rotate,
123 &vf_info_mirror,
124 &vf_info_palette,
125 &vf_info_pp7,
126 #ifdef CONFIG_LIBAVCODEC
127 &vf_info_lavc,
128 &vf_info_lavcdeint,
129 &vf_info_screenshot,
130 #endif
131 #ifdef CONFIG_ZR
132 &vf_info_zrmjpeg,
133 #endif
134 &vf_info_dvbscale,
135 &vf_info_cropdetect,
136 &vf_info_test,
137 &vf_info_noise,
138 &vf_info_yvu9,
139 &vf_info_eq,
140 &vf_info_eq2,
141 &vf_info_halfpack,
142 &vf_info_dint,
143 &vf_info_1bpp,
144 &vf_info_2xsai,
145 &vf_info_unsharp,
146 &vf_info_swapuv,
147 &vf_info_il,
148 &vf_info_fil,
149 &vf_info_boxblur,
150 &vf_info_sab,
151 &vf_info_smartblur,
152 &vf_info_perspective,
153 &vf_info_down3dright,
154 &vf_info_field,
155 &vf_info_denoise3d,
156 &vf_info_hqdn3d,
157 &vf_info_detc,
158 &vf_info_telecine,
159 &vf_info_tinterlace,
160 &vf_info_tfields,
161 &vf_info_ivtc,
162 &vf_info_ilpack,
163 &vf_info_dsize,
164 &vf_info_decimate,
165 &vf_info_softpulldown,
166 &vf_info_pullup,
167 &vf_info_filmdint,
168 &vf_info_framestep,
169 &vf_info_tile,
170 &vf_info_delogo,
171 &vf_info_remove_logo,
172 &vf_info_hue,
173 #ifdef CONFIG_LIBAVCODEC_A
174 &vf_info_spp,
175 &vf_info_uspp,
176 &vf_info_fspp,
177 &vf_info_qp,
178 &vf_info_mcdeint,
179 &vf_info_geq,
180 #endif
181 &vf_info_yuvcsp,
182 &vf_info_kerndeint,
183 &vf_info_rgbtest,
184 &vf_info_phase,
185 &vf_info_divtc,
186 &vf_info_harddup,
187 &vf_info_softskip,
188 #ifdef CONFIG_ASS
189 &vf_info_ass,
190 #endif
191 &vf_info_yadif,
192 &vf_info_blackframe,
193 &vf_info_ow,
194 NULL
197 // For the vf option
198 m_obj_settings_t* vf_settings = NULL;
199 const m_obj_list_t vf_obj_list = {
200 (void**)filter_list,
201 M_ST_OFF(vf_info_t,name),
202 M_ST_OFF(vf_info_t,info),
203 M_ST_OFF(vf_info_t,opts)
206 //============================================================================
207 // mpi stuff:
209 void vf_mpi_clear(mp_image_t* mpi,int x0,int y0,int w,int h){
210 int y;
211 if(mpi->flags&MP_IMGFLAG_PLANAR){
212 y0&=~1;h+=h&1;
213 if(x0==0 && w==mpi->width){
214 // full width clear:
215 memset(mpi->planes[0]+mpi->stride[0]*y0,0,mpi->stride[0]*h);
216 memset(mpi->planes[1]+mpi->stride[1]*(y0>>mpi->chroma_y_shift),128,mpi->stride[1]*(h>>mpi->chroma_y_shift));
217 memset(mpi->planes[2]+mpi->stride[2]*(y0>>mpi->chroma_y_shift),128,mpi->stride[2]*(h>>mpi->chroma_y_shift));
218 } else
219 for(y=y0;y<y0+h;y+=2){
220 memset(mpi->planes[0]+x0+mpi->stride[0]*y,0,w);
221 memset(mpi->planes[0]+x0+mpi->stride[0]*(y+1),0,w);
222 memset(mpi->planes[1]+(x0>>mpi->chroma_x_shift)+mpi->stride[1]*(y>>mpi->chroma_y_shift),128,(w>>mpi->chroma_x_shift));
223 memset(mpi->planes[2]+(x0>>mpi->chroma_x_shift)+mpi->stride[2]*(y>>mpi->chroma_y_shift),128,(w>>mpi->chroma_x_shift));
225 return;
227 // packed:
228 for(y=y0;y<y0+h;y++){
229 unsigned char* dst=mpi->planes[0]+mpi->stride[0]*y+(mpi->bpp>>3)*x0;
230 if(mpi->flags&MP_IMGFLAG_YUV){
231 unsigned int* p=(unsigned int*) dst;
232 int size=(mpi->bpp>>3)*w/4;
233 int i;
234 #ifdef WORDS_BIGENDIAN
235 #define CLEAR_PACKEDYUV_PATTERN 0x00800080
236 #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x80008000
237 #else
238 #define CLEAR_PACKEDYUV_PATTERN 0x80008000
239 #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x00800080
240 #endif
241 if(mpi->flags&MP_IMGFLAG_SWAPPED){
242 for(i=0;i<size-3;i+=4) p[i]=p[i+1]=p[i+2]=p[i+3]=CLEAR_PACKEDYUV_PATTERN_SWAPPED;
243 for(;i<size;i++) p[i]=CLEAR_PACKEDYUV_PATTERN_SWAPPED;
244 } else {
245 for(i=0;i<size-3;i+=4) p[i]=p[i+1]=p[i+2]=p[i+3]=CLEAR_PACKEDYUV_PATTERN;
246 for(;i<size;i++) p[i]=CLEAR_PACKEDYUV_PATTERN;
248 } else
249 memset(dst,0,(mpi->bpp>>3)*w);
253 mp_image_t* vf_get_image(vf_instance_t* vf, unsigned int outfmt, int mp_imgtype, int mp_imgflag, int w, int h){
254 mp_image_t* mpi=NULL;
255 int w2;
256 int number = mp_imgtype >> 16;
258 #ifdef MP_DEBUG
259 assert(w == -1 || w >= vf->w);
260 assert(h == -1 || h >= vf->h);
261 assert(vf->w > 0);
262 assert(vf->h > 0);
263 #endif
265 // fprintf(stderr, "get_image: %d:%d, vf: %d:%d\n", w,h,vf->w,vf->h);
267 if (w == -1) w = vf->w;
268 if (h == -1) h = vf->h;
270 w2=(mp_imgflag&MP_IMGFLAG_ACCEPT_ALIGNED_STRIDE)?((w+15)&(~15)):w;
272 if(vf->put_image==vf_next_put_image){
273 // passthru mode, if the filter uses the fallback/default put_image() code
274 return vf_get_image(vf->next,outfmt,mp_imgtype,mp_imgflag,w,h);
277 // Note: we should call libvo first to check if it supports direct rendering
278 // and if not, then fallback to software buffers:
279 switch(mp_imgtype & 0xff){
280 case MP_IMGTYPE_EXPORT:
281 if(!vf->imgctx.export_images[0]) vf->imgctx.export_images[0]=new_mp_image(w2,h);
282 mpi=vf->imgctx.export_images[0];
283 break;
284 case MP_IMGTYPE_STATIC:
285 if(!vf->imgctx.static_images[0]) vf->imgctx.static_images[0]=new_mp_image(w2,h);
286 mpi=vf->imgctx.static_images[0];
287 break;
288 case MP_IMGTYPE_TEMP:
289 if(!vf->imgctx.temp_images[0]) vf->imgctx.temp_images[0]=new_mp_image(w2,h);
290 mpi=vf->imgctx.temp_images[0];
291 break;
292 case MP_IMGTYPE_IPB:
293 if(!(mp_imgflag&MP_IMGFLAG_READABLE)){ // B frame:
294 if(!vf->imgctx.temp_images[0]) vf->imgctx.temp_images[0]=new_mp_image(w2,h);
295 mpi=vf->imgctx.temp_images[0];
296 break;
298 case MP_IMGTYPE_IP:
299 if(!vf->imgctx.static_images[vf->imgctx.static_idx]) vf->imgctx.static_images[vf->imgctx.static_idx]=new_mp_image(w2,h);
300 mpi=vf->imgctx.static_images[vf->imgctx.static_idx];
301 vf->imgctx.static_idx^=1;
302 break;
303 case MP_IMGTYPE_NUMBERED:
304 if (number == -1) {
305 int i;
306 for (i = 0; i < NUM_NUMBERED_MPI; i++)
307 if (!vf->imgctx.numbered_images[i] || !vf->imgctx.numbered_images[i]->usage_count)
308 break;
309 number = i;
311 if (number < 0 || number >= NUM_NUMBERED_MPI) return NULL;
312 if (!vf->imgctx.numbered_images[number]) vf->imgctx.numbered_images[number] = new_mp_image(w2,h);
313 mpi = vf->imgctx.numbered_images[number];
314 mpi->number = number;
315 break;
317 if(mpi){
318 mpi->type=mp_imgtype;
319 mpi->w=vf->w; mpi->h=vf->h;
320 // keep buffer allocation status & color flags only:
321 // mpi->flags&=~(MP_IMGFLAG_PRESERVE|MP_IMGFLAG_READABLE|MP_IMGFLAG_DIRECT);
322 mpi->flags&=MP_IMGFLAG_ALLOCATED|MP_IMGFLAG_TYPE_DISPLAYED|MP_IMGFLAGMASK_COLORS;
323 // accept restrictions & draw_slice flags only:
324 mpi->flags|=mp_imgflag&(MP_IMGFLAGMASK_RESTRICTIONS|MP_IMGFLAG_DRAW_CALLBACK);
325 if(!vf->draw_slice) mpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
326 if(mpi->width!=w2 || mpi->height!=h){
327 // printf("vf.c: MPI parameters changed! %dx%d -> %dx%d \n", mpi->width,mpi->height,w2,h);
328 if(mpi->flags&MP_IMGFLAG_ALLOCATED){
329 if(mpi->width<w2 || mpi->height<h){
330 // need to re-allocate buffer memory:
331 free(mpi->planes[0]);
332 mpi->flags&=~MP_IMGFLAG_ALLOCATED;
333 mp_msg(MSGT_VFILTER,MSGL_V,"vf.c: have to REALLOCATE buffer memory :(\n");
335 // } else {
337 mpi->width=w2; mpi->chroma_width=(w2 + (1<<mpi->chroma_x_shift) - 1)>>mpi->chroma_x_shift;
338 mpi->height=h; mpi->chroma_height=(h + (1<<mpi->chroma_y_shift) - 1)>>mpi->chroma_y_shift;
341 if(!mpi->bpp) mp_image_setfmt(mpi,outfmt);
342 if(!(mpi->flags&MP_IMGFLAG_ALLOCATED) && mpi->type>MP_IMGTYPE_EXPORT){
344 // check libvo first!
345 if(vf->get_image) vf->get_image(vf,mpi);
347 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
348 // non-direct and not yet allocated image. allocate it!
349 if (!mpi->bpp) { // no way we can allocate this
350 mp_msg(MSGT_DECVIDEO, MSGL_FATAL,
351 "vf_get_image: Tried to allocate a format that can not be allocated!\n");
352 return NULL;
355 // check if codec prefer aligned stride:
356 if(mp_imgflag&MP_IMGFLAG_PREFER_ALIGNED_STRIDE){
357 int align=(mpi->flags&MP_IMGFLAG_PLANAR &&
358 mpi->flags&MP_IMGFLAG_YUV) ?
359 (8<<mpi->chroma_x_shift)-1 : 15; // -- maybe FIXME
360 w2=((w+align)&(~align));
361 if(mpi->width!=w2){
362 // we have to change width... check if we CAN co it:
363 int flags=vf->query_format(vf,outfmt); // should not fail
364 if(!(flags&3)) mp_msg(MSGT_DECVIDEO,MSGL_WARN,"??? vf_get_image{vf->query_format(outfmt)} failed!\n");
365 // printf("query -> 0x%X \n",flags);
366 if(flags&VFCAP_ACCEPT_STRIDE){
367 mpi->width=w2;
368 mpi->chroma_width=(w2 + (1<<mpi->chroma_x_shift) - 1)>>mpi->chroma_x_shift;
373 // IF09 - allocate space for 4. plane delta info - unused
374 if (mpi->imgfmt == IMGFMT_IF09)
376 mpi->planes[0]=memalign(64, mpi->bpp*mpi->width*(mpi->height+2)/8+
377 mpi->chroma_width*mpi->chroma_height);
378 /* export delta table */
379 mpi->planes[3]=mpi->planes[0]+(mpi->width*mpi->height)+2*(mpi->chroma_width*mpi->chroma_height);
381 else
382 mpi->planes[0]=memalign(64, mpi->bpp*mpi->width*(mpi->height+2)/8);
383 if(mpi->flags&MP_IMGFLAG_PLANAR){
384 // YV12/I420/YVU9/IF09. feel free to add other planar formats here...
385 //if(!mpi->stride[0])
386 mpi->stride[0]=mpi->width;
387 //if(!mpi->stride[1])
388 if(mpi->num_planes > 2){
389 mpi->stride[1]=mpi->stride[2]=mpi->chroma_width;
390 if(mpi->flags&MP_IMGFLAG_SWAPPED){
391 // I420/IYUV (Y,U,V)
392 mpi->planes[1]=mpi->planes[0]+mpi->width*mpi->height;
393 mpi->planes[2]=mpi->planes[1]+mpi->chroma_width*mpi->chroma_height;
394 } else {
395 // YV12,YVU9,IF09 (Y,V,U)
396 mpi->planes[2]=mpi->planes[0]+mpi->width*mpi->height;
397 mpi->planes[1]=mpi->planes[2]+mpi->chroma_width*mpi->chroma_height;
399 } else {
400 // NV12/NV21
401 mpi->stride[1]=mpi->chroma_width;
402 mpi->planes[1]=mpi->planes[0]+mpi->width*mpi->height;
404 } else {
405 //if(!mpi->stride[0])
406 mpi->stride[0]=mpi->width*mpi->bpp/8;
408 // printf("clearing img!\n");
409 vf_mpi_clear(mpi,0,0,mpi->width,mpi->height);
410 mpi->flags|=MP_IMGFLAG_ALLOCATED;
413 if(mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)
414 if(vf->start_slice) vf->start_slice(vf,mpi);
415 if(!(mpi->flags&MP_IMGFLAG_TYPE_DISPLAYED)){
416 mp_msg(MSGT_DECVIDEO,MSGL_V,"*** [%s] %s%s mp_image_t, %dx%dx%dbpp %s %s, %d bytes\n",
417 vf->info->name,
418 (mpi->type==MP_IMGTYPE_EXPORT)?"Exporting":
419 ((mpi->flags&MP_IMGFLAG_DIRECT)?"Direct Rendering":"Allocating"),
420 (mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)?" (slices)":"",
421 mpi->width,mpi->height,mpi->bpp,
422 (mpi->flags&MP_IMGFLAG_YUV)?"YUV":((mpi->flags&MP_IMGFLAG_SWAPPED)?"BGR":"RGB"),
423 (mpi->flags&MP_IMGFLAG_PLANAR)?"planar":"packed",
424 mpi->bpp*mpi->width*mpi->height/8);
425 mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"(imgfmt: %x, planes: %p,%p,%p strides: %d,%d,%d, chroma: %dx%d, shift: h:%d,v:%d)\n",
426 mpi->imgfmt, mpi->planes[0], mpi->planes[1], mpi->planes[2],
427 mpi->stride[0], mpi->stride[1], mpi->stride[2],
428 mpi->chroma_width, mpi->chroma_height, mpi->chroma_x_shift, mpi->chroma_y_shift);
429 mpi->flags|=MP_IMGFLAG_TYPE_DISPLAYED;
432 mpi->qscale = NULL;
434 mpi->usage_count++;
435 // printf("\rVF_MPI: %p %p %p %d %d %d \n",
436 // mpi->planes[0],mpi->planes[1],mpi->planes[2],
437 // mpi->stride[0],mpi->stride[1],mpi->stride[2]);
438 return mpi;
441 //============================================================================
443 // By default vf doesn't accept MPEGPES
444 static int vf_default_query_format(struct vf_instance_s* vf, unsigned int fmt){
445 if(fmt == IMGFMT_MPEGPES) return 0;
446 return vf_next_query_format(vf,fmt);
449 vf_instance_t* vf_open_plugin(const vf_info_t* const* filter_list, vf_instance_t* next, const char *name, char **args){
450 vf_instance_t* vf;
451 int i;
452 for(i=0;;i++){
453 if(!filter_list[i]){
454 mp_msg(MSGT_VFILTER,MSGL_ERR,MSGTR_CouldNotFindVideoFilter,name);
455 return NULL; // no such filter!
457 if(!strcmp(filter_list[i]->name,name)) break;
459 vf=malloc(sizeof(vf_instance_t));
460 memset(vf,0,sizeof(vf_instance_t));
461 vf->info=filter_list[i];
462 vf->next=next;
463 vf->config=vf_next_config;
464 vf->control=vf_next_control;
465 vf->query_format=vf_default_query_format;
466 vf->put_image=vf_next_put_image;
467 vf->default_caps=VFCAP_ACCEPT_STRIDE;
468 vf->default_reqs=0;
469 if(vf->info->opts) { // vf_vo get some special argument
470 const m_struct_t* st = vf->info->opts;
471 void* vf_priv = m_struct_alloc(st);
472 int n;
473 for(n = 0 ; args && args[2*n] ; n++)
474 m_struct_set(st,vf_priv,args[2*n],args[2*n+1]);
475 vf->priv = vf_priv;
476 args = NULL;
477 } else // Otherwise we should have the '_oldargs_'
478 if(args && !strcmp(args[0],"_oldargs_"))
479 args = (char**)args[1];
480 else
481 args = NULL;
482 if(vf->info->open(vf,(char*)args)>0) return vf; // Success!
483 free(vf);
484 mp_msg(MSGT_VFILTER,MSGL_ERR,MSGTR_CouldNotOpenVideoFilter,name);
485 return NULL;
488 vf_instance_t* vf_open_filter(vf_instance_t* next, const char *name, char **args){
489 if(args && strcmp(args[0],"_oldargs_")) {
490 int i,l = 0;
491 for(i = 0 ; args && args[2*i] ; i++)
492 l += 1 + strlen(args[2*i]) + 1 + strlen(args[2*i+1]);
493 l += strlen(name);
495 char str[l+1];
496 char* p = str;
497 p += sprintf(str,"%s",name);
498 for(i = 0 ; args && args[2*i] ; i++)
499 p += sprintf(p," %s=%s",args[2*i],args[2*i+1]);
500 mp_msg(MSGT_VFILTER,MSGL_INFO,MSGTR_OpeningVideoFilter "[%s]\n",str);
502 } else if(strcmp(name,"vo")) {
503 if(args && strcmp(args[0],"_oldargs_") == 0)
504 mp_msg(MSGT_VFILTER,MSGL_INFO,MSGTR_OpeningVideoFilter
505 "[%s=%s]\n", name,args[1]);
506 else
507 mp_msg(MSGT_VFILTER,MSGL_INFO,MSGTR_OpeningVideoFilter
508 "[%s]\n", name);
510 return vf_open_plugin(filter_list,next,name,args);
514 * \brief adds a filter before the last one (which should be the vo filter).
515 * \param vf start of the filter chain.
516 * \param name name of the filter to add.
517 * \param args argument list for the filter.
518 * \return pointer to the filter instance that was created.
520 vf_instance_t* vf_add_before_vo(vf_instance_t **vf, char *name, char **args) {
521 vf_instance_t *vo, *prev = NULL, *new;
522 // Find the last filter (should be vf_vo)
523 for (vo = *vf; vo->next; vo = vo->next)
524 prev = vo;
525 new = vf_open_filter(vo, name, args);
526 if (prev)
527 prev->next = new;
528 else
529 *vf = new;
530 return new;
533 //============================================================================
535 unsigned int vf_match_csp(vf_instance_t** vfp,const unsigned int* list,unsigned int preferred){
536 vf_instance_t* vf=*vfp;
537 const unsigned int* p;
538 unsigned int best=0;
539 int ret;
540 if((p=list)) while(*p){
541 ret=vf->query_format(vf,*p);
542 mp_msg(MSGT_VFILTER,MSGL_V,"[%s] query(%s) -> %d\n",vf->info->name,vo_format_name(*p),ret&3);
543 if(ret&2){ best=*p; break;} // no conversion -> bingo!
544 if(ret&1 && !best) best=*p; // best with conversion
545 ++p;
547 if(best) return best; // bingo, they have common csp!
548 // ok, then try with scale:
549 if(vf->info == &vf_info_scale) return 0; // avoid infinite recursion!
550 vf=vf_open_filter(vf,"scale",NULL);
551 if(!vf) return 0; // failed to init "scale"
552 // try the preferred csp first:
553 if(preferred && vf->query_format(vf,preferred)) best=preferred; else
554 // try the list again, now with "scaler" :
555 if((p=list)) while(*p){
556 ret=vf->query_format(vf,*p);
557 mp_msg(MSGT_VFILTER,MSGL_V,"[%s] query(%s) -> %d\n",vf->info->name,vo_format_name(*p),ret&3);
558 if(ret&2){ best=*p; break;} // no conversion -> bingo!
559 if(ret&1 && !best) best=*p; // best with conversion
560 ++p;
562 if(best) *vfp=vf; // else uninit vf !FIXME!
563 return best;
566 void vf_clone_mpi_attributes(mp_image_t* dst, mp_image_t* src){
567 dst->pict_type= src->pict_type;
568 dst->fields = src->fields;
569 dst->qscale_type= src->qscale_type;
570 if(dst->width == src->width && dst->height == src->height){
571 dst->qstride= src->qstride;
572 dst->qscale= src->qscale;
576 void vf_queue_frame(vf_instance_t *vf, int (*func)(vf_instance_t *))
578 vf->continue_buffered_image = func;
581 // Output the next buffered image (if any) from the filter chain.
582 // The queue could be kept as a simple stack/list instead avoiding the
583 // looping here, but there's currently no good context variable where
584 // that could be stored so this was easier to implement.
586 int vf_output_queued_frame(vf_instance_t *vf)
588 while (1) {
589 int ret;
590 vf_instance_t *current;
591 vf_instance_t *last=NULL;
592 int (*tmp)(vf_instance_t *);
593 for (current = vf; current; current = current->next)
594 if (current->continue_buffered_image)
595 last = current;
596 if (!last)
597 return 0;
598 tmp = last->continue_buffered_image;
599 last->continue_buffered_image = NULL;
600 ret = tmp(last);
601 if (ret > 0) {
602 vf->control(vf, VFCTRL_DRAW_OSD, NULL);
603 #ifdef CONFIG_ASS
604 vf->control(vf, VFCTRL_DRAW_EOSD, NULL);
605 #endif
607 if (ret)
608 return ret;
614 * \brief Video config() function wrapper
616 * Blocks config() calls with different size or format for filters
617 * with VFCAP_CONSTANT
619 * First call is redirected to vf->config.
621 * In following calls, it verifies that the configuration parameters
622 * are unchanged, and returns either success or error.
625 int vf_config_wrapper(struct vf_instance_s* vf,
626 int width, int height, int d_width, int d_height,
627 unsigned int flags, unsigned int outfmt)
629 int r;
630 if ((vf->default_caps&VFCAP_CONSTANT) && vf->fmt.have_configured) {
631 if ((vf->fmt.orig_width != width)
632 || (vf->fmt.orig_height != height)
633 || (vf->fmt.orig_fmt != outfmt)) {
634 mp_msg(MSGT_VFILTER,MSGL_ERR,MSGTR_ResolutionDoesntMatch);
635 return 0;
637 return 1;
639 vf->fmt.have_configured = 1;
640 vf->fmt.orig_height = height;
641 vf->fmt.orig_width = width;
642 vf->fmt.orig_fmt = outfmt;
643 r = vf->config(vf, width, height, d_width, d_height, flags, outfmt);
644 if (!r) vf->fmt.have_configured = 0;
645 return r;
648 int vf_next_config(struct vf_instance_s* vf,
649 int width, int height, int d_width, int d_height,
650 unsigned int voflags, unsigned int outfmt){
651 int miss;
652 int flags=vf->next->query_format(vf->next,outfmt);
653 if(!flags){
654 // hmm. colorspace mismatch!!!
655 // let's insert the 'scale' filter, it does the job for us:
656 vf_instance_t* vf2;
657 if(vf->next->info==&vf_info_scale) return 0; // scale->scale
658 vf2=vf_open_filter(vf->next,"scale",NULL);
659 if(!vf2) return 0; // shouldn't happen!
660 vf->next=vf2;
661 flags=vf->next->query_format(vf->next,outfmt);
662 if(!flags){
663 mp_msg(MSGT_VFILTER,MSGL_ERR,MSGTR_CannotFindColorspace);
664 return 0; // FAIL
667 mp_msg(MSGT_VFILTER,MSGL_V,"REQ: flags=0x%X req=0x%X \n",flags,vf->default_reqs);
668 miss=vf->default_reqs - (flags&vf->default_reqs);
669 if(miss&VFCAP_ACCEPT_STRIDE){
670 // vf requires stride support but vf->next doesn't support it!
671 // let's insert the 'expand' filter, it does the job for us:
672 vf_instance_t* vf2=vf_open_filter(vf->next,"expand",NULL);
673 if(!vf2) return 0; // shouldn't happen!
674 vf->next=vf2;
676 vf->next->w = width; vf->next->h = height;
677 return vf_config_wrapper(vf->next,width,height,d_width,d_height,voflags,outfmt);
680 int vf_next_control(struct vf_instance_s* vf, int request, void* data){
681 return vf->next->control(vf->next,request,data);
684 void vf_extra_flip(struct vf_instance_s* vf) {
685 vf_next_control(vf, VFCTRL_DRAW_OSD, NULL);
686 #ifdef CONFIG_ASS
687 vf_next_control(vf, VFCTRL_DRAW_EOSD, NULL);
688 #endif
689 vf_next_control(vf, VFCTRL_FLIP_PAGE, NULL);
692 int vf_next_query_format(struct vf_instance_s* vf, unsigned int fmt){
693 int flags=vf->next->query_format(vf->next,fmt);
694 if(flags) flags|=vf->default_caps;
695 return flags;
698 int vf_next_put_image(struct vf_instance_s* vf,mp_image_t *mpi, double pts){
699 return vf->next->put_image(vf->next,mpi, pts);
702 void vf_next_draw_slice(struct vf_instance_s* vf,unsigned char** src, int * stride,int w, int h, int x, int y){
703 if (vf->next->draw_slice) {
704 vf->next->draw_slice(vf->next,src,stride,w,h,x,y);
705 return;
707 if (!vf->dmpi) {
708 mp_msg(MSGT_VFILTER,MSGL_ERR,"draw_slice: dmpi not stored by vf_%s\n", vf->info->name);
709 return;
711 if (!(vf->dmpi->flags & MP_IMGFLAG_PLANAR)) {
712 memcpy_pic(vf->dmpi->planes[0]+y*vf->dmpi->stride[0]+vf->dmpi->bpp/8*x,
713 src[0], vf->dmpi->bpp/8*w, h, vf->dmpi->stride[0], stride[0]);
714 return;
716 memcpy_pic(vf->dmpi->planes[0]+y*vf->dmpi->stride[0]+x, src[0],
717 w, h, vf->dmpi->stride[0], stride[0]);
718 memcpy_pic(vf->dmpi->planes[1]+(y>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[1]+(x>>vf->dmpi->chroma_x_shift),
719 src[1], w>>vf->dmpi->chroma_x_shift, h>>vf->dmpi->chroma_y_shift, vf->dmpi->stride[1], stride[1]);
720 memcpy_pic(vf->dmpi->planes[2]+(y>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[2]+(x>>vf->dmpi->chroma_x_shift),
721 src[2], w>>vf->dmpi->chroma_x_shift, h>>vf->dmpi->chroma_y_shift, vf->dmpi->stride[2], stride[2]);
724 //============================================================================
726 vf_instance_t* append_filters(vf_instance_t* last){
727 vf_instance_t* vf;
728 int i;
730 if(vf_settings) {
731 // We want to add them in the 'right order'
732 for(i = 0 ; vf_settings[i].name ; i++)
733 /* NOP */;
734 for(i-- ; i >= 0 ; i--) {
735 //printf("Open filter %s\n",vf_settings[i].name);
736 vf = vf_open_filter(last,vf_settings[i].name,vf_settings[i].attribs);
737 if(vf) last=vf;
740 return last;
743 //============================================================================
745 void vf_uninit_filter(vf_instance_t* vf){
746 if(vf->uninit) vf->uninit(vf);
747 free_mp_image(vf->imgctx.static_images[0]);
748 free_mp_image(vf->imgctx.static_images[1]);
749 free_mp_image(vf->imgctx.temp_images[0]);
750 free_mp_image(vf->imgctx.export_images[0]);
751 free(vf);
754 void vf_uninit_filter_chain(vf_instance_t* vf){
755 while(vf){
756 vf_instance_t* next=vf->next;
757 vf_uninit_filter(vf);
758 vf=next;