typo fixes
[mplayer/greg.git] / libmpcodecs / vf.c
bloba9bff4b7717d1190f726f60da9d08901fafb974e
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
5 #include "config.h"
6 #ifdef 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 vf_info_t vf_info_vo;
27 extern vf_info_t vf_info_rectangle;
28 #ifndef HAVE_NO_POSIX_SELECT
29 extern vf_info_t vf_info_bmovl;
30 #endif
31 extern vf_info_t vf_info_crop;
32 extern vf_info_t vf_info_expand;
33 #if defined(USE_LIBPOSTPROC) || defined(USE_LIBPOSTPROC_SO)
34 extern vf_info_t vf_info_pp;
35 #endif
36 extern vf_info_t vf_info_scale;
37 #ifdef USE_LIBFAME
38 extern vf_info_t vf_info_fame;
39 #endif
40 extern vf_info_t vf_info_format;
41 extern vf_info_t vf_info_noformat;
42 extern vf_info_t vf_info_yuy2;
43 extern vf_info_t vf_info_flip;
44 extern vf_info_t vf_info_rgb2bgr;
45 extern vf_info_t vf_info_rotate;
46 extern vf_info_t vf_info_mirror;
47 extern vf_info_t vf_info_palette;
48 extern vf_info_t vf_info_lavc;
49 extern vf_info_t vf_info_zrmjpeg;
50 extern vf_info_t vf_info_dvbscale;
51 extern vf_info_t vf_info_cropdetect;
52 extern vf_info_t vf_info_test;
53 extern vf_info_t vf_info_noise;
54 extern vf_info_t vf_info_yvu9;
55 extern vf_info_t vf_info_lavcdeint;
56 extern vf_info_t vf_info_eq;
57 extern vf_info_t vf_info_eq2;
58 extern vf_info_t vf_info_halfpack;
59 extern vf_info_t vf_info_dint;
60 extern vf_info_t vf_info_1bpp;
61 extern vf_info_t vf_info_2xsai;
62 extern vf_info_t vf_info_unsharp;
63 extern vf_info_t vf_info_swapuv;
64 extern vf_info_t vf_info_il;
65 extern vf_info_t vf_info_fil;
66 extern vf_info_t vf_info_boxblur;
67 extern vf_info_t vf_info_sab;
68 extern vf_info_t vf_info_smartblur;
69 extern vf_info_t vf_info_perspective;
70 extern vf_info_t vf_info_down3dright;
71 extern vf_info_t vf_info_field;
72 extern vf_info_t vf_info_denoise3d;
73 extern vf_info_t vf_info_hqdn3d;
74 extern vf_info_t vf_info_detc;
75 extern vf_info_t vf_info_telecine;
76 extern vf_info_t vf_info_tinterlace;
77 extern vf_info_t vf_info_tfields;
78 extern vf_info_t vf_info_ivtc;
79 extern vf_info_t vf_info_ilpack;
80 extern vf_info_t vf_info_dsize;
81 extern vf_info_t vf_info_decimate;
82 extern vf_info_t vf_info_softpulldown;
83 extern vf_info_t vf_info_pullup;
84 extern vf_info_t vf_info_filmdint;
85 extern vf_info_t vf_info_framestep;
86 extern vf_info_t vf_info_tile;
87 extern vf_info_t vf_info_delogo;
88 extern vf_info_t vf_info_remove_logo;
89 extern vf_info_t vf_info_hue;
90 extern vf_info_t vf_info_spp;
91 extern vf_info_t vf_info_uspp;
92 extern vf_info_t vf_info_fspp;
93 extern vf_info_t vf_info_pp7;
94 extern vf_info_t vf_info_yuvcsp;
95 extern vf_info_t vf_info_kerndeint;
96 extern vf_info_t vf_info_rgbtest;
97 extern vf_info_t vf_info_qp;
98 extern vf_info_t vf_info_phase;
99 extern vf_info_t vf_info_divtc;
100 extern vf_info_t vf_info_harddup;
101 extern vf_info_t vf_info_softskip;
102 extern vf_info_t vf_info_screenshot;
104 // list of available filters:
105 static vf_info_t* filter_list[]={
106 &vf_info_rectangle,
107 #ifndef HAVE_NO_POSIX_SELECT
108 &vf_info_bmovl,
109 #endif
110 &vf_info_crop,
111 &vf_info_expand,
112 #if defined(USE_LIBPOSTPROC) || defined(USE_LIBPOSTPROC_SO)
113 &vf_info_pp,
114 #endif
115 &vf_info_scale,
116 // &vf_info_osd,
117 &vf_info_vo,
118 #ifdef USE_LIBFAME
119 &vf_info_fame,
120 #endif
121 &vf_info_format,
122 &vf_info_noformat,
123 &vf_info_yuy2,
124 &vf_info_flip,
125 &vf_info_rgb2bgr,
126 &vf_info_rotate,
127 &vf_info_mirror,
128 &vf_info_palette,
129 #ifdef USE_LIBAVCODEC
130 &vf_info_lavc,
131 &vf_info_lavcdeint,
132 #endif
133 #ifdef HAVE_ZR
134 &vf_info_zrmjpeg,
135 #endif
136 &vf_info_dvbscale,
137 &vf_info_cropdetect,
138 &vf_info_test,
139 &vf_info_noise,
140 &vf_info_yvu9,
141 &vf_info_eq,
142 &vf_info_eq2,
143 &vf_info_halfpack,
144 &vf_info_dint,
145 &vf_info_1bpp,
146 &vf_info_2xsai,
147 &vf_info_unsharp,
148 &vf_info_swapuv,
149 &vf_info_il,
150 &vf_info_fil,
151 &vf_info_boxblur,
152 &vf_info_sab,
153 &vf_info_smartblur,
154 &vf_info_perspective,
155 &vf_info_down3dright,
156 &vf_info_field,
157 &vf_info_denoise3d,
158 &vf_info_hqdn3d,
159 &vf_info_detc,
160 &vf_info_telecine,
161 &vf_info_tinterlace,
162 &vf_info_tfields,
163 &vf_info_ivtc,
164 &vf_info_ilpack,
165 &vf_info_dsize,
166 &vf_info_decimate,
167 &vf_info_softpulldown,
168 &vf_info_pullup,
169 &vf_info_filmdint,
170 &vf_info_framestep,
171 &vf_info_tile,
172 &vf_info_delogo,
173 &vf_info_remove_logo,
174 &vf_info_hue,
175 #ifdef USE_LIBAVCODEC_DSPUTIL
176 &vf_info_spp,
177 &vf_info_uspp,
178 &vf_info_fspp,
179 &vf_info_qp,
180 #endif
181 &vf_info_yuvcsp,
182 &vf_info_kerndeint,
183 &vf_info_rgbtest,
184 #ifdef USE_LIBAVCODEC
185 &vf_info_pp7,
186 #endif
187 &vf_info_phase,
188 &vf_info_divtc,
189 &vf_info_harddup,
190 &vf_info_softskip,
191 #ifdef HAVE_PNG
192 &vf_info_screenshot,
193 #endif
194 NULL
197 // For the vf option
198 m_obj_settings_t* vf_settings = NULL;
199 // For the vop option
200 m_obj_settings_t* vo_plugin_args = NULL;
201 m_obj_list_t vf_obj_list = {
202 (void**)filter_list,
203 M_ST_OFF(vf_info_t,name),
204 M_ST_OFF(vf_info_t,info),
205 M_ST_OFF(vf_info_t,opts)
208 //============================================================================
209 // mpi stuff:
211 void vf_mpi_clear(mp_image_t* mpi,int x0,int y0,int w,int h){
212 int y;
213 if(mpi->flags&MP_IMGFLAG_PLANAR){
214 y0&=~1;h+=h&1;
215 if(x0==0 && w==mpi->width){
216 // full width clear:
217 memset(mpi->planes[0]+mpi->stride[0]*y0,0,mpi->stride[0]*h);
218 memset(mpi->planes[1]+mpi->stride[1]*(y0>>mpi->chroma_y_shift),128,mpi->stride[1]*(h>>mpi->chroma_y_shift));
219 memset(mpi->planes[2]+mpi->stride[2]*(y0>>mpi->chroma_y_shift),128,mpi->stride[2]*(h>>mpi->chroma_y_shift));
220 } else
221 for(y=y0;y<y0+h;y+=2){
222 memset(mpi->planes[0]+x0+mpi->stride[0]*y,0,w);
223 memset(mpi->planes[0]+x0+mpi->stride[0]*(y+1),0,w);
224 memset(mpi->planes[1]+(x0>>mpi->chroma_x_shift)+mpi->stride[1]*(y>>mpi->chroma_y_shift),128,(w>>mpi->chroma_x_shift));
225 memset(mpi->planes[2]+(x0>>mpi->chroma_x_shift)+mpi->stride[2]*(y>>mpi->chroma_y_shift),128,(w>>mpi->chroma_x_shift));
227 return;
229 // packed:
230 for(y=y0;y<y0+h;y++){
231 unsigned char* dst=mpi->planes[0]+mpi->stride[0]*y+(mpi->bpp>>3)*x0;
232 if(mpi->flags&MP_IMGFLAG_YUV){
233 unsigned int* p=(unsigned int*) dst;
234 int size=(mpi->bpp>>3)*w/4;
235 int i;
236 #ifdef WORDS_BIGENDIAN
237 #define CLEAR_PACKEDYUV_PATTERN 0x00800080
238 #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x80008000
239 #else
240 #define CLEAR_PACKEDYUV_PATTERN 0x80008000
241 #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x00800080
242 #endif
243 if(mpi->flags&MP_IMGFLAG_SWAPPED){
244 for(i=0;i<size-3;i+=4) p[i]=p[i+1]=p[i+2]=p[i+3]=CLEAR_PACKEDYUV_PATTERN_SWAPPED;
245 for(;i<size;i++) p[i]=CLEAR_PACKEDYUV_PATTERN_SWAPPED;
246 } else {
247 for(i=0;i<size-3;i+=4) p[i]=p[i+1]=p[i+2]=p[i+3]=CLEAR_PACKEDYUV_PATTERN;
248 for(;i<size;i++) p[i]=CLEAR_PACKEDYUV_PATTERN;
250 } else
251 memset(dst,0,(mpi->bpp>>3)*w);
255 mp_image_t* vf_get_image(vf_instance_t* vf, unsigned int outfmt, int mp_imgtype, int mp_imgflag, int w, int h){
256 mp_image_t* mpi=NULL;
257 int w2;
259 #ifdef MP_DEBUG
260 assert(w == -1 || w >= vf->w);
261 assert(h == -1 || h >= vf->h);
262 assert(vf->w > 0);
263 assert(vf->h > 0);
264 #endif
266 // fprintf(stderr, "get_image: %d:%d, vf: %d:%d\n", w,h,vf->w,vf->h);
268 if (w == -1) w = vf->w;
269 if (h == -1) h = vf->h;
271 w2=(mp_imgflag&MP_IMGFLAG_ACCEPT_ALIGNED_STRIDE)?((w+15)&(~15)):w;
273 if(vf->put_image==vf_next_put_image){
274 // passthru mode, if the plugin uses the fallback/default put_image() code
275 return vf_get_image(vf->next,outfmt,mp_imgtype,mp_imgflag,w,h);
278 // Note: we should call libvo first to check if it supports direct rendering
279 // and if not, then fallback to software buffers:
280 switch(mp_imgtype){
281 case MP_IMGTYPE_EXPORT:
282 if(!vf->imgctx.export_images[0]) vf->imgctx.export_images[0]=new_mp_image(w2,h);
283 mpi=vf->imgctx.export_images[0];
284 break;
285 case MP_IMGTYPE_STATIC:
286 if(!vf->imgctx.static_images[0]) vf->imgctx.static_images[0]=new_mp_image(w2,h);
287 mpi=vf->imgctx.static_images[0];
288 break;
289 case MP_IMGTYPE_TEMP:
290 if(!vf->imgctx.temp_images[0]) vf->imgctx.temp_images[0]=new_mp_image(w2,h);
291 mpi=vf->imgctx.temp_images[0];
292 break;
293 case MP_IMGTYPE_IPB:
294 if(!(mp_imgflag&MP_IMGFLAG_READABLE)){ // B frame:
295 if(!vf->imgctx.temp_images[0]) vf->imgctx.temp_images[0]=new_mp_image(w2,h);
296 mpi=vf->imgctx.temp_images[0];
297 break;
299 case MP_IMGTYPE_IP:
300 if(!vf->imgctx.static_images[vf->imgctx.static_idx]) vf->imgctx.static_images[vf->imgctx.static_idx]=new_mp_image(w2,h);
301 mpi=vf->imgctx.static_images[vf->imgctx.static_idx];
302 vf->imgctx.static_idx^=1;
303 break;
305 if(mpi){
306 mpi->type=mp_imgtype;
307 mpi->w=vf->w; mpi->h=vf->h;
308 // keep buffer allocation status & color flags only:
309 // mpi->flags&=~(MP_IMGFLAG_PRESERVE|MP_IMGFLAG_READABLE|MP_IMGFLAG_DIRECT);
310 mpi->flags&=MP_IMGFLAG_ALLOCATED|MP_IMGFLAG_TYPE_DISPLAYED|MP_IMGFLAGMASK_COLORS;
311 // accept restrictions & draw_slice flags only:
312 mpi->flags|=mp_imgflag&(MP_IMGFLAGMASK_RESTRICTIONS|MP_IMGFLAG_DRAW_CALLBACK);
313 if(!vf->draw_slice) mpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
314 if(mpi->width!=w2 || mpi->height!=h){
315 // printf("vf.c: MPI parameters changed! %dx%d -> %dx%d \n", mpi->width,mpi->height,w2,h);
316 if(mpi->flags&MP_IMGFLAG_ALLOCATED){
317 if(mpi->width<w2 || mpi->height<h){
318 // need to re-allocate buffer memory:
319 free(mpi->planes[0]);
320 mpi->flags&=~MP_IMGFLAG_ALLOCATED;
321 mp_msg(MSGT_VFILTER,MSGL_V,"vf.c: have to REALLOCATE buffer memory :(\n");
323 // } else {
325 mpi->width=w2; mpi->chroma_width=(w2 + (1<<mpi->chroma_x_shift) - 1)>>mpi->chroma_x_shift;
326 mpi->height=h; mpi->chroma_height=(h + (1<<mpi->chroma_y_shift) - 1)>>mpi->chroma_y_shift;
329 if(!mpi->bpp) mp_image_setfmt(mpi,outfmt);
330 if(!(mpi->flags&MP_IMGFLAG_ALLOCATED) && mpi->type>MP_IMGTYPE_EXPORT){
332 // check libvo first!
333 if(vf->get_image) vf->get_image(vf,mpi);
335 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
336 // non-direct and not yet allocated image. allocate it!
338 // check if codec prefer aligned stride:
339 if(mp_imgflag&MP_IMGFLAG_PREFER_ALIGNED_STRIDE){
340 int align=(mpi->flags&MP_IMGFLAG_PLANAR &&
341 mpi->flags&MP_IMGFLAG_YUV) ?
342 (8<<mpi->chroma_x_shift)-1 : 15; // -- maybe FIXME
343 w2=((w+align)&(~align));
344 if(mpi->width!=w2){
345 // we have to change width... check if we CAN co it:
346 int flags=vf->query_format(vf,outfmt); // should not fail
347 if(!(flags&3)) mp_msg(MSGT_DECVIDEO,MSGL_WARN,"??? vf_get_image{vf->query_format(outfmt)} failed!\n");
348 // printf("query -> 0x%X \n",flags);
349 if(flags&VFCAP_ACCEPT_STRIDE){
350 mpi->width=w2;
351 mpi->chroma_width=(w2 + (1<<mpi->chroma_x_shift) - 1)>>mpi->chroma_x_shift;
356 // IF09 - allocate space for 4. plane delta info - unused
357 if (mpi->imgfmt == IMGFMT_IF09)
359 mpi->planes[0]=memalign(64, mpi->bpp*mpi->width*(mpi->height+2)/8+
360 mpi->chroma_width*mpi->chroma_height);
361 /* export delta table */
362 mpi->planes[3]=mpi->planes[0]+(mpi->width*mpi->height)+2*(mpi->chroma_width*mpi->chroma_height);
364 else
365 mpi->planes[0]=memalign(64, mpi->bpp*mpi->width*(mpi->height+2)/8);
366 if(mpi->flags&MP_IMGFLAG_PLANAR){
367 // YV12/I420/YVU9/IF09. feel free to add other planar formats here...
368 //if(!mpi->stride[0])
369 mpi->stride[0]=mpi->width;
370 //if(!mpi->stride[1])
371 if(mpi->num_planes > 2){
372 mpi->stride[1]=mpi->stride[2]=mpi->chroma_width;
373 if(mpi->flags&MP_IMGFLAG_SWAPPED){
374 // I420/IYUV (Y,U,V)
375 mpi->planes[1]=mpi->planes[0]+mpi->width*mpi->height;
376 mpi->planes[2]=mpi->planes[1]+mpi->chroma_width*mpi->chroma_height;
377 } else {
378 // YV12,YVU9,IF09 (Y,V,U)
379 mpi->planes[2]=mpi->planes[0]+mpi->width*mpi->height;
380 mpi->planes[1]=mpi->planes[2]+mpi->chroma_width*mpi->chroma_height;
382 } else {
383 // NV12/NV21
384 mpi->stride[1]=mpi->chroma_width;
385 mpi->planes[1]=mpi->planes[0]+mpi->width*mpi->height;
387 } else {
388 //if(!mpi->stride[0])
389 mpi->stride[0]=mpi->width*mpi->bpp/8;
391 // printf("clearing img!\n");
392 vf_mpi_clear(mpi,0,0,mpi->width,mpi->height);
393 mpi->flags|=MP_IMGFLAG_ALLOCATED;
396 if(mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)
397 if(vf->start_slice) vf->start_slice(vf,mpi);
398 if(!(mpi->flags&MP_IMGFLAG_TYPE_DISPLAYED)){
399 mp_msg(MSGT_DECVIDEO,MSGL_V,"*** [%s] %s%s mp_image_t, %dx%dx%dbpp %s %s, %d bytes\n",
400 vf->info->name,
401 (mpi->type==MP_IMGTYPE_EXPORT)?"Exporting":
402 ((mpi->flags&MP_IMGFLAG_DIRECT)?"Direct Rendering":"Allocating"),
403 (mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)?" (slices)":"",
404 mpi->width,mpi->height,mpi->bpp,
405 (mpi->flags&MP_IMGFLAG_YUV)?"YUV":((mpi->flags&MP_IMGFLAG_SWAPPED)?"BGR":"RGB"),
406 (mpi->flags&MP_IMGFLAG_PLANAR)?"planar":"packed",
407 mpi->bpp*mpi->width*mpi->height/8);
408 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",
409 mpi->imgfmt, mpi->planes[0], mpi->planes[1], mpi->planes[2],
410 mpi->stride[0], mpi->stride[1], mpi->stride[2],
411 mpi->chroma_width, mpi->chroma_height, mpi->chroma_x_shift, mpi->chroma_y_shift);
412 mpi->flags|=MP_IMGFLAG_TYPE_DISPLAYED;
415 mpi->qscale = NULL;
417 // printf("\rVF_MPI: %p %p %p %d %d %d \n",
418 // mpi->planes[0],mpi->planes[1],mpi->planes[2],
419 // mpi->stride[0],mpi->stride[1],mpi->stride[2]);
420 return mpi;
423 //============================================================================
425 // By default vf doesn't accept MPEGPES
426 static int vf_default_query_format(struct vf_instance_s* vf, unsigned int fmt){
427 if(fmt == IMGFMT_MPEGPES) return 0;
428 return vf_next_query_format(vf,fmt);
431 vf_instance_t* vf_open_plugin(vf_info_t** filter_list, vf_instance_t* next, char *name, char **args){
432 vf_instance_t* vf;
433 int i;
434 for(i=0;;i++){
435 if(!filter_list[i]){
436 mp_msg(MSGT_VFILTER,MSGL_ERR,MSGTR_CouldNotFindVideoFilter,name);
437 return NULL; // no such filter!
439 if(!strcmp(filter_list[i]->name,name)) break;
441 vf=malloc(sizeof(vf_instance_t));
442 memset(vf,0,sizeof(vf_instance_t));
443 vf->info=filter_list[i];
444 vf->next=next;
445 vf->config=vf_next_config;
446 vf->control=vf_next_control;
447 vf->query_format=vf_default_query_format;
448 vf->put_image=vf_next_put_image;
449 vf->default_caps=VFCAP_ACCEPT_STRIDE;
450 vf->default_reqs=0;
451 if(vf->info->opts) { // vf_vo get some special argument
452 m_struct_t* st = vf->info->opts;
453 void* vf_priv = m_struct_alloc(st);
454 int n;
455 for(n = 0 ; args && args[2*n] ; n++)
456 m_struct_set(st,vf_priv,args[2*n],args[2*n+1]);
457 vf->priv = vf_priv;
458 args = NULL;
459 } else // Otherwise we should have the '_oldargs_'
460 if(args && !strcmp(args[0],"_oldargs_"))
461 args = (char**)args[1];
462 else
463 args = NULL;
464 if(vf->info->open(vf,(char*)args)>0) return vf; // Success!
465 free(vf);
466 mp_msg(MSGT_VFILTER,MSGL_ERR,MSGTR_CouldNotOpenVideoFilter,name);
467 return NULL;
470 vf_instance_t* vf_open_filter(vf_instance_t* next, char *name, char **args){
471 if(args && strcmp(args[0],"_oldargs_")) {
472 int i,l = 0;
473 for(i = 0 ; args && args[2*i] ; i++)
474 l += 1 + strlen(args[2*i]) + 1 + strlen(args[2*i+1]);
475 l += strlen(name);
477 char str[l+1];
478 char* p = str;
479 p += sprintf(str,"%s",name);
480 for(i = 0 ; args && args[2*i] ; i++)
481 p += sprintf(p," %s=%s",args[2*i],args[2*i+1]);
482 mp_msg(MSGT_VFILTER,MSGL_INFO,MSGTR_OpeningVideoFilter "[%s]\n",str);
484 } else if(strcmp(name,"vo")) {
485 if(args && strcmp(args[0],"_oldargs_") == 0)
486 mp_msg(MSGT_VFILTER,MSGL_INFO,MSGTR_OpeningVideoFilter
487 "[%s=%s]\n", name,args[1]);
488 else
489 mp_msg(MSGT_VFILTER,MSGL_INFO,MSGTR_OpeningVideoFilter
490 "[%s]\n", name);
492 return vf_open_plugin(filter_list,next,name,args);
496 * \brief adds a filter before the last one (which should be the vo filter).
497 * \param vf start of the filter chain.
498 * \param name name of the filter to add.
499 * \param args argument list for the filter.
500 * \return pointer to the filter instance that was created.
502 vf_instance_t* vf_add_before_vo(vf_instance_t **vf, char *name, char **args) {
503 vf_instance_t *vo, *prev = NULL, *new;
504 // Find the last filter (should be vf_vo)
505 for (vo = *vf; vo->next; vo = vo->next)
506 prev = vo;
507 new = vf_open_filter(vo, name, args);
508 if (prev)
509 prev->next = new;
510 else
511 *vf = new;
512 return new;
515 //============================================================================
517 unsigned int vf_match_csp(vf_instance_t** vfp,unsigned int* list,unsigned int preferred){
518 vf_instance_t* vf=*vfp;
519 unsigned int* p;
520 unsigned int best=0;
521 int ret;
522 if((p=list)) while(*p){
523 ret=vf->query_format(vf,*p);
524 mp_msg(MSGT_VFILTER,MSGL_V,"[%s] query(%s) -> %d\n",vf->info->name,vo_format_name(*p),ret&3);
525 if(ret&2){ best=*p; break;} // no conversion -> bingo!
526 if(ret&1 && !best) best=*p; // best with conversion
527 ++p;
529 if(best) return best; // bingo, they have common csp!
530 // ok, then try with scale:
531 if(vf->info == &vf_info_scale) return 0; // avoid infinite recursion!
532 vf=vf_open_filter(vf,"scale",NULL);
533 if(!vf) return 0; // failed to init "scale"
534 // try the preferred csp first:
535 if(preferred && vf->query_format(vf,preferred)) best=preferred; else
536 // try the list again, now with "scaler" :
537 if((p=list)) while(*p){
538 ret=vf->query_format(vf,*p);
539 mp_msg(MSGT_VFILTER,MSGL_V,"[%s] query(%s) -> %d\n",vf->info->name,vo_format_name(*p),ret&3);
540 if(ret&2){ best=*p; break;} // no conversion -> bingo!
541 if(ret&1 && !best) best=*p; // best with conversion
542 ++p;
544 if(best) *vfp=vf; // else uninit vf !FIXME!
545 return best;
548 void vf_clone_mpi_attributes(mp_image_t* dst, mp_image_t* src){
549 dst->pict_type= src->pict_type;
550 dst->fields = src->fields;
551 dst->qscale_type= src->qscale_type;
552 if(dst->width == src->width && dst->height == src->height){
553 dst->qstride= src->qstride;
554 dst->qscale= src->qscale;
558 * \brief Video config() function wrapper
560 * Blocks config() calls with different size or format for filters
561 * with VFCAP_CONSTANT
563 * First call is redirected to vf->config.
565 * In following calls, it verifies that the configuration parameters
566 * are unchanged, and returns either success or error.
569 int vf_config_wrapper(struct vf_instance_s* vf,
570 int width, int height, int d_width, int d_height,
571 unsigned int flags, unsigned int outfmt)
573 int r;
574 if ((vf->default_caps&VFCAP_CONSTANT) && vf->fmt.have_configured) {
575 if ((vf->fmt.orig_width != width)
576 || (vf->fmt.orig_height != height)
577 || (vf->fmt.orig_fmt != outfmt)) {
578 mp_msg(MSGT_VFILTER,MSGL_ERR,MSGTR_ResolutionDoesntMatch);
579 return 0;
581 return 1;
583 vf->fmt.have_configured = 1;
584 vf->fmt.orig_height = height;
585 vf->fmt.orig_width = width;
586 vf->fmt.orig_fmt = outfmt;
587 r = vf->config(vf, width, height, d_width, d_height, flags, outfmt);
588 if (!r) vf->fmt.have_configured = 0;
589 return r;
592 int vf_next_config(struct vf_instance_s* vf,
593 int width, int height, int d_width, int d_height,
594 unsigned int voflags, unsigned int outfmt){
595 int miss;
596 int flags=vf->next->query_format(vf->next,outfmt);
597 if(!flags){
598 // hmm. colorspace mismatch!!!
599 // let's insert the 'scale' filter, it does the job for us:
600 vf_instance_t* vf2;
601 if(vf->next->info==&vf_info_scale) return 0; // scale->scale
602 vf2=vf_open_filter(vf->next,"scale",NULL);
603 if(!vf2) return 0; // shouldn't happen!
604 vf->next=vf2;
605 flags=vf->next->query_format(vf->next,outfmt);
606 if(!flags){
607 mp_msg(MSGT_VFILTER,MSGL_ERR,MSGTR_CannotFindColorspace);
608 return 0; // FAIL
611 mp_msg(MSGT_VFILTER,MSGL_V,"REQ: flags=0x%X req=0x%X \n",flags,vf->default_reqs);
612 miss=vf->default_reqs - (flags&vf->default_reqs);
613 if(miss&VFCAP_ACCEPT_STRIDE){
614 // vf requires stride support but vf->next doesn't support it!
615 // let's insert the 'expand' filter, it does the job for us:
616 vf_instance_t* vf2=vf_open_filter(vf->next,"expand",NULL);
617 if(!vf2) return 0; // shouldn't happen!
618 vf->next=vf2;
620 vf->next->w = width; vf->next->h = height;
621 return vf_config_wrapper(vf->next,width,height,d_width,d_height,voflags,outfmt);
624 int vf_next_control(struct vf_instance_s* vf, int request, void* data){
625 return vf->next->control(vf->next,request,data);
628 int vf_next_query_format(struct vf_instance_s* vf, unsigned int fmt){
629 int flags=vf->next->query_format(vf->next,fmt);
630 if(flags) flags|=vf->default_caps;
631 return flags;
634 int vf_next_put_image(struct vf_instance_s* vf,mp_image_t *mpi, double pts){
635 return vf->next->put_image(vf->next,mpi, pts);
638 void vf_next_draw_slice(struct vf_instance_s* vf,unsigned char** src, int * stride,int w, int h, int x, int y){
639 if (vf->next->draw_slice) {
640 vf->next->draw_slice(vf->next,src,stride,w,h,x,y);
641 return;
643 if (!vf->dmpi) {
644 mp_msg(MSGT_VFILTER,MSGL_ERR,"draw_slice: dmpi not stored by vf_%s\n", vf->info->name);
645 return;
647 if (!(vf->dmpi->flags & MP_IMGFLAG_PLANAR)) {
648 memcpy_pic(vf->dmpi->planes[0]+y*vf->dmpi->stride[0]+vf->dmpi->bpp/8*x,
649 src[0], vf->dmpi->bpp/8*w, h, vf->dmpi->stride[0], stride[0]);
650 return;
652 memcpy_pic(vf->dmpi->planes[0]+y*vf->dmpi->stride[0]+x, src[0],
653 w, h, vf->dmpi->stride[0], stride[0]);
654 memcpy_pic(vf->dmpi->planes[1]+(y>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[1]+(x>>vf->dmpi->chroma_x_shift),
655 src[1], w>>vf->dmpi->chroma_x_shift, h>>vf->dmpi->chroma_y_shift, vf->dmpi->stride[1], stride[1]);
656 memcpy_pic(vf->dmpi->planes[2]+(y>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[2]+(x>>vf->dmpi->chroma_x_shift),
657 src[2], w>>vf->dmpi->chroma_x_shift, h>>vf->dmpi->chroma_y_shift, vf->dmpi->stride[2], stride[2]);
660 //============================================================================
662 vf_instance_t* append_filters(vf_instance_t* last){
663 vf_instance_t* vf;
664 int i;
666 // -vf take precedence over -vop
667 if(vf_settings) {
668 // We want to add them in the 'right order'
669 for(i = 0 ; vf_settings[i].name ; i++)
670 /* NOP */;
671 for(i-- ; i >= 0 ; i--) {
672 //printf("Open filter %s\n",vf_settings[i].name);
673 vf = vf_open_filter(last,vf_settings[i].name,vf_settings[i].attribs);
674 if(vf) last=vf;
676 } else if(vo_plugin_args) {
677 for(i = 0 ; vo_plugin_args[i].name ; i++) {
678 vf = vf_open_filter(last,vo_plugin_args[i].name,
679 vo_plugin_args[i].attribs);
680 if(vf) last=vf;
683 return last;
686 //============================================================================
688 void vf_uninit_filter(vf_instance_t* vf){
689 if(vf->uninit) vf->uninit(vf);
690 free_mp_image(vf->imgctx.static_images[0]);
691 free_mp_image(vf->imgctx.static_images[1]);
692 free_mp_image(vf->imgctx.temp_images[0]);
693 free_mp_image(vf->imgctx.export_images[0]);
694 free(vf);
697 void vf_uninit_filter_chain(vf_instance_t* vf){
698 while(vf){
699 vf_instance_t* next=vf->next;
700 vf_uninit_filter(vf);
701 vf=next;
705 void vf_list_plugins(void){
706 int i=0;
707 while(filter_list[i]){
708 mp_msg(MSGT_VFILTER,MSGL_INFO,"\t%-10s: %s\n",filter_list[i]->name,filter_list[i]->info);
709 i++;