Merge svn changes up to r30165
[mplayer/glamo.git] / libmpcodecs / vf.c
blobd753d3d68a918676bbfc90e3652271a89ee01947
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_gradfun;
52 extern const vf_info_t vf_info_halfpack;
53 extern const vf_info_t vf_info_dint;
54 extern const vf_info_t vf_info_1bpp;
55 extern const vf_info_t vf_info_2xsai;
56 extern const vf_info_t vf_info_unsharp;
57 extern const vf_info_t vf_info_swapuv;
58 extern const vf_info_t vf_info_il;
59 extern const vf_info_t vf_info_fil;
60 extern const vf_info_t vf_info_boxblur;
61 extern const vf_info_t vf_info_sab;
62 extern const vf_info_t vf_info_smartblur;
63 extern const vf_info_t vf_info_perspective;
64 extern const vf_info_t vf_info_down3dright;
65 extern const vf_info_t vf_info_field;
66 extern const vf_info_t vf_info_denoise3d;
67 extern const vf_info_t vf_info_hqdn3d;
68 extern const vf_info_t vf_info_detc;
69 extern const vf_info_t vf_info_telecine;
70 extern const vf_info_t vf_info_tinterlace;
71 extern const vf_info_t vf_info_tfields;
72 extern const vf_info_t vf_info_ivtc;
73 extern const vf_info_t vf_info_ilpack;
74 extern const vf_info_t vf_info_dsize;
75 extern const vf_info_t vf_info_decimate;
76 extern const vf_info_t vf_info_softpulldown;
77 extern const vf_info_t vf_info_pullup;
78 extern const vf_info_t vf_info_filmdint;
79 extern const vf_info_t vf_info_framestep;
80 extern const vf_info_t vf_info_tile;
81 extern const vf_info_t vf_info_delogo;
82 extern const vf_info_t vf_info_remove_logo;
83 extern const vf_info_t vf_info_hue;
84 extern const vf_info_t vf_info_spp;
85 extern const vf_info_t vf_info_uspp;
86 extern const vf_info_t vf_info_fspp;
87 extern const vf_info_t vf_info_pp7;
88 extern const vf_info_t vf_info_yuvcsp;
89 extern const vf_info_t vf_info_kerndeint;
90 extern const vf_info_t vf_info_rgbtest;
91 extern const vf_info_t vf_info_qp;
92 extern const vf_info_t vf_info_phase;
93 extern const vf_info_t vf_info_divtc;
94 extern const vf_info_t vf_info_harddup;
95 extern const vf_info_t vf_info_softskip;
96 extern const vf_info_t vf_info_screenshot;
97 extern const vf_info_t vf_info_ass;
98 extern const vf_info_t vf_info_mcdeint;
99 extern const vf_info_t vf_info_yadif;
100 extern const vf_info_t vf_info_blackframe;
101 extern const vf_info_t vf_info_geq;
102 extern const vf_info_t vf_info_ow;
104 // list of available filters:
105 static const vf_info_t* const filter_list[]={
106 &vf_info_rectangle,
107 #ifdef HAVE_POSIX_SELECT
108 &vf_info_bmovl,
109 #endif
110 &vf_info_crop,
111 &vf_info_expand,
112 #ifdef CONFIG_LIBPOSTPROC
113 &vf_info_pp,
114 #endif
115 &vf_info_scale,
116 // &vf_info_osd,
117 &vf_info_vo,
118 &vf_info_format,
119 &vf_info_noformat,
120 #ifdef CONFIG_LIBSWSCALE_INTERNALS
121 &vf_info_yuy2,
122 #endif
123 &vf_info_flip,
124 #ifdef CONFIG_LIBSWSCALE_INTERNALS
125 &vf_info_rgb2bgr,
126 #endif
127 &vf_info_rotate,
128 &vf_info_mirror,
129 #ifdef CONFIG_LIBSWSCALE_INTERNALS
130 &vf_info_palette,
131 #endif
132 &vf_info_pp7,
133 #ifdef CONFIG_LIBAVCODEC
134 &vf_info_lavc,
135 &vf_info_lavcdeint,
136 &vf_info_screenshot,
137 &vf_info_uspp,
138 #endif
139 #ifdef CONFIG_ZR
140 &vf_info_zrmjpeg,
141 #endif
142 &vf_info_dvbscale,
143 &vf_info_cropdetect,
144 &vf_info_test,
145 &vf_info_noise,
146 &vf_info_yvu9,
147 &vf_info_eq,
148 &vf_info_eq2,
149 &vf_info_gradfun,
150 #ifdef CONFIG_LIBSWSCALE_INTERNALS
151 &vf_info_halfpack,
152 #endif
153 &vf_info_dint,
154 &vf_info_1bpp,
155 &vf_info_2xsai,
156 &vf_info_unsharp,
157 &vf_info_swapuv,
158 &vf_info_il,
159 &vf_info_fil,
160 &vf_info_boxblur,
161 &vf_info_sab,
162 &vf_info_smartblur,
163 &vf_info_perspective,
164 &vf_info_down3dright,
165 &vf_info_field,
166 &vf_info_denoise3d,
167 &vf_info_hqdn3d,
168 &vf_info_detc,
169 &vf_info_telecine,
170 &vf_info_tinterlace,
171 &vf_info_tfields,
172 &vf_info_ivtc,
173 &vf_info_ilpack,
174 &vf_info_dsize,
175 &vf_info_decimate,
176 &vf_info_softpulldown,
177 &vf_info_pullup,
178 &vf_info_filmdint,
179 &vf_info_framestep,
180 &vf_info_tile,
181 &vf_info_delogo,
182 &vf_info_remove_logo,
183 &vf_info_hue,
184 #ifdef CONFIG_LIBAVCODEC_INTERNALS
185 &vf_info_spp,
186 &vf_info_fspp,
187 &vf_info_qp,
188 &vf_info_mcdeint,
189 &vf_info_geq,
190 #endif
191 &vf_info_yuvcsp,
192 &vf_info_kerndeint,
193 &vf_info_rgbtest,
194 &vf_info_phase,
195 &vf_info_divtc,
196 &vf_info_harddup,
197 &vf_info_softskip,
198 #ifdef CONFIG_ASS
199 &vf_info_ass,
200 #endif
201 &vf_info_yadif,
202 &vf_info_blackframe,
203 &vf_info_ow,
204 NULL
207 // For the vf option
208 const m_obj_list_t vf_obj_list = {
209 (void**)filter_list,
210 M_ST_OFF(vf_info_t,name),
211 M_ST_OFF(vf_info_t,info),
212 M_ST_OFF(vf_info_t,opts)
215 //============================================================================
216 // mpi stuff:
218 void vf_mpi_clear(mp_image_t* mpi,int x0,int y0,int w,int h){
219 int y;
220 if(mpi->flags&MP_IMGFLAG_PLANAR){
221 y0&=~1;h+=h&1;
222 if(x0==0 && w==mpi->width){
223 // full width clear:
224 memset(mpi->planes[0]+mpi->stride[0]*y0,0,mpi->stride[0]*h);
225 memset(mpi->planes[1]+mpi->stride[1]*(y0>>mpi->chroma_y_shift),128,mpi->stride[1]*(h>>mpi->chroma_y_shift));
226 memset(mpi->planes[2]+mpi->stride[2]*(y0>>mpi->chroma_y_shift),128,mpi->stride[2]*(h>>mpi->chroma_y_shift));
227 } else
228 for(y=y0;y<y0+h;y+=2){
229 memset(mpi->planes[0]+x0+mpi->stride[0]*y,0,w);
230 memset(mpi->planes[0]+x0+mpi->stride[0]*(y+1),0,w);
231 memset(mpi->planes[1]+(x0>>mpi->chroma_x_shift)+mpi->stride[1]*(y>>mpi->chroma_y_shift),128,(w>>mpi->chroma_x_shift));
232 memset(mpi->planes[2]+(x0>>mpi->chroma_x_shift)+mpi->stride[2]*(y>>mpi->chroma_y_shift),128,(w>>mpi->chroma_x_shift));
234 return;
236 // packed:
237 for(y=y0;y<y0+h;y++){
238 unsigned char* dst=mpi->planes[0]+mpi->stride[0]*y+(mpi->bpp>>3)*x0;
239 if(mpi->flags&MP_IMGFLAG_YUV){
240 unsigned int* p=(unsigned int*) dst;
241 int size=(mpi->bpp>>3)*w/4;
242 int i;
243 #if HAVE_BIGENDIAN
244 #define CLEAR_PACKEDYUV_PATTERN 0x00800080
245 #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x80008000
246 #else
247 #define CLEAR_PACKEDYUV_PATTERN 0x80008000
248 #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x00800080
249 #endif
250 if(mpi->flags&MP_IMGFLAG_SWAPPED){
251 for(i=0;i<size-3;i+=4) p[i]=p[i+1]=p[i+2]=p[i+3]=CLEAR_PACKEDYUV_PATTERN_SWAPPED;
252 for(;i<size;i++) p[i]=CLEAR_PACKEDYUV_PATTERN_SWAPPED;
253 } else {
254 for(i=0;i<size-3;i+=4) p[i]=p[i+1]=p[i+2]=p[i+3]=CLEAR_PACKEDYUV_PATTERN;
255 for(;i<size;i++) p[i]=CLEAR_PACKEDYUV_PATTERN;
257 } else
258 memset(dst,0,(mpi->bpp>>3)*w);
262 mp_image_t* vf_get_image(vf_instance_t* vf, unsigned int outfmt, int mp_imgtype, int mp_imgflag, int w, int h){
263 mp_image_t* mpi=NULL;
264 int w2;
265 int number = mp_imgtype >> 16;
267 #ifdef MP_DEBUG
268 assert(w == -1 || w >= vf->w);
269 assert(h == -1 || h >= vf->h);
270 assert(vf->w > 0);
271 assert(vf->h > 0);
272 #endif
274 // fprintf(stderr, "get_image: %d:%d, vf: %d:%d\n", w,h,vf->w,vf->h);
276 if (w == -1) w = vf->w;
277 if (h == -1) h = vf->h;
279 w2=(mp_imgflag&MP_IMGFLAG_ACCEPT_ALIGNED_STRIDE)?((w+15)&(~15)):w;
281 if(vf->put_image==vf_next_put_image){
282 // passthru mode, if the filter uses the fallback/default put_image() code
283 return vf_get_image(vf->next,outfmt,mp_imgtype,mp_imgflag,w,h);
286 // Note: we should call libvo first to check if it supports direct rendering
287 // and if not, then fallback to software buffers:
288 switch(mp_imgtype & 0xff){
289 case MP_IMGTYPE_EXPORT:
290 if(!vf->imgctx.export_images[0]) vf->imgctx.export_images[0]=new_mp_image(w2,h);
291 mpi=vf->imgctx.export_images[0];
292 break;
293 case MP_IMGTYPE_STATIC:
294 if(!vf->imgctx.static_images[0]) vf->imgctx.static_images[0]=new_mp_image(w2,h);
295 mpi=vf->imgctx.static_images[0];
296 break;
297 case MP_IMGTYPE_TEMP:
298 if(!vf->imgctx.temp_images[0]) vf->imgctx.temp_images[0]=new_mp_image(w2,h);
299 mpi=vf->imgctx.temp_images[0];
300 break;
301 case MP_IMGTYPE_IPB:
302 if(!(mp_imgflag&MP_IMGFLAG_READABLE)){ // B frame:
303 if(!vf->imgctx.temp_images[0]) vf->imgctx.temp_images[0]=new_mp_image(w2,h);
304 mpi=vf->imgctx.temp_images[0];
305 break;
307 case MP_IMGTYPE_IP:
308 if(!vf->imgctx.static_images[vf->imgctx.static_idx]) vf->imgctx.static_images[vf->imgctx.static_idx]=new_mp_image(w2,h);
309 mpi=vf->imgctx.static_images[vf->imgctx.static_idx];
310 vf->imgctx.static_idx^=1;
311 break;
312 case MP_IMGTYPE_NUMBERED:
313 if (number == -1) {
314 int i;
315 for (i = 0; i < NUM_NUMBERED_MPI; i++)
316 if (!vf->imgctx.numbered_images[i] || !vf->imgctx.numbered_images[i]->usage_count)
317 break;
318 number = i;
320 if (number < 0 || number >= NUM_NUMBERED_MPI) return NULL;
321 if (!vf->imgctx.numbered_images[number]) vf->imgctx.numbered_images[number] = new_mp_image(w2,h);
322 mpi = vf->imgctx.numbered_images[number];
323 mpi->number = number;
324 break;
326 if(mpi){
327 mpi->type=mp_imgtype;
328 mpi->w=vf->w; mpi->h=vf->h;
329 // keep buffer allocation status & color flags only:
330 // mpi->flags&=~(MP_IMGFLAG_PRESERVE|MP_IMGFLAG_READABLE|MP_IMGFLAG_DIRECT);
331 mpi->flags&=MP_IMGFLAG_ALLOCATED|MP_IMGFLAG_TYPE_DISPLAYED|MP_IMGFLAGMASK_COLORS;
332 // accept restrictions, draw_slice and palette flags only:
333 mpi->flags|=mp_imgflag&(MP_IMGFLAGMASK_RESTRICTIONS|MP_IMGFLAG_DRAW_CALLBACK|MP_IMGFLAG_RGB_PALETTE);
334 if(!vf->draw_slice) mpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
335 if(mpi->width!=w2 || mpi->height!=h){
336 // printf("vf.c: MPI parameters changed! %dx%d -> %dx%d \n", mpi->width,mpi->height,w2,h);
337 if(mpi->flags&MP_IMGFLAG_ALLOCATED){
338 if(mpi->width<w2 || mpi->height<h){
339 // need to re-allocate buffer memory:
340 free(mpi->planes[0]);
341 mpi->flags&=~MP_IMGFLAG_ALLOCATED;
342 mp_msg(MSGT_VFILTER,MSGL_V,"vf.c: have to REALLOCATE buffer memory :(\n");
344 // } else {
346 mpi->width=w2; mpi->chroma_width=(w2 + (1<<mpi->chroma_x_shift) - 1)>>mpi->chroma_x_shift;
347 mpi->height=h; mpi->chroma_height=(h + (1<<mpi->chroma_y_shift) - 1)>>mpi->chroma_y_shift;
350 if(!mpi->bpp) mp_image_setfmt(mpi,outfmt);
351 if(!(mpi->flags&MP_IMGFLAG_ALLOCATED) && mpi->type>MP_IMGTYPE_EXPORT){
353 // check libvo first!
354 if(vf->get_image) vf->get_image(vf,mpi);
356 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
357 // non-direct and not yet allocated image. allocate it!
358 if (!mpi->bpp) { // no way we can allocate this
359 mp_msg(MSGT_DECVIDEO, MSGL_FATAL,
360 "vf_get_image: Tried to allocate a format that can not be allocated!\n");
361 return NULL;
364 // check if codec prefer aligned stride:
365 if(mp_imgflag&MP_IMGFLAG_PREFER_ALIGNED_STRIDE){
366 int align=(mpi->flags&MP_IMGFLAG_PLANAR &&
367 mpi->flags&MP_IMGFLAG_YUV) ?
368 (8<<mpi->chroma_x_shift)-1 : 15; // -- maybe FIXME
369 w2=((w+align)&(~align));
370 if(mpi->width!=w2){
371 // we have to change width... check if we CAN co it:
372 int flags=vf->query_format(vf,outfmt); // should not fail
373 if(!(flags&3)) mp_msg(MSGT_DECVIDEO,MSGL_WARN,"??? vf_get_image{vf->query_format(outfmt)} failed!\n");
374 // printf("query -> 0x%X \n",flags);
375 if(flags&VFCAP_ACCEPT_STRIDE){
376 mpi->width=w2;
377 mpi->chroma_width=(w2 + (1<<mpi->chroma_x_shift) - 1)>>mpi->chroma_x_shift;
382 mp_image_alloc_planes(mpi);
383 // printf("clearing img!\n");
384 vf_mpi_clear(mpi,0,0,mpi->width,mpi->height);
387 if(mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)
388 if(vf->start_slice) vf->start_slice(vf,mpi);
389 if(!(mpi->flags&MP_IMGFLAG_TYPE_DISPLAYED)){
390 mp_msg(MSGT_DECVIDEO,MSGL_V,"*** [%s] %s%s mp_image_t, %dx%dx%dbpp %s %s, %d bytes\n",
391 vf->info->name,
392 (mpi->type==MP_IMGTYPE_EXPORT)?"Exporting":
393 ((mpi->flags&MP_IMGFLAG_DIRECT)?"Direct Rendering":"Allocating"),
394 (mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)?" (slices)":"",
395 mpi->width,mpi->height,mpi->bpp,
396 (mpi->flags&MP_IMGFLAG_YUV)?"YUV":((mpi->flags&MP_IMGFLAG_SWAPPED)?"BGR":"RGB"),
397 (mpi->flags&MP_IMGFLAG_PLANAR)?"planar":"packed",
398 mpi->bpp*mpi->width*mpi->height/8);
399 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",
400 mpi->imgfmt, mpi->planes[0], mpi->planes[1], mpi->planes[2],
401 mpi->stride[0], mpi->stride[1], mpi->stride[2],
402 mpi->chroma_width, mpi->chroma_height, mpi->chroma_x_shift, mpi->chroma_y_shift);
403 mpi->flags|=MP_IMGFLAG_TYPE_DISPLAYED;
406 mpi->qscale = NULL;
408 mpi->usage_count++;
409 // printf("\rVF_MPI: %p %p %p %d %d %d \n",
410 // mpi->planes[0],mpi->planes[1],mpi->planes[2],
411 // mpi->stride[0],mpi->stride[1],mpi->stride[2]);
412 return mpi;
415 //============================================================================
417 // By default vf doesn't accept MPEGPES
418 static int vf_default_query_format(struct vf_instance* vf, unsigned int fmt){
419 if(fmt == IMGFMT_MPEGPES) return 0;
420 return vf_next_query_format(vf,fmt);
423 vf_instance_t* vf_open_plugin(struct MPOpts *opts, const vf_info_t* const* filter_list, vf_instance_t* next, const char *name, char **args){
424 vf_instance_t* vf;
425 int i;
426 for(i=0;;i++){
427 if(!filter_list[i]){
428 mp_tmsg(MSGT_VFILTER,MSGL_ERR,"Couldn't find video filter '%s'.\n",name);
429 return NULL; // no such filter!
431 if(!strcmp(filter_list[i]->name,name)) break;
433 vf = calloc(1, sizeof *vf);
434 vf->opts = opts;
435 vf->info=filter_list[i];
436 vf->next=next;
437 vf->config=vf_next_config;
438 vf->control=vf_next_control;
439 vf->query_format=vf_default_query_format;
440 vf->put_image=vf_next_put_image;
441 vf->default_caps=VFCAP_ACCEPT_STRIDE;
442 vf->default_reqs=0;
443 if(vf->info->opts) { // vf_vo get some special argument
444 const m_struct_t* st = vf->info->opts;
445 void* vf_priv = m_struct_alloc(st);
446 int n;
447 for(n = 0 ; args && args[2*n] ; n++)
448 m_struct_set(st,vf_priv,args[2*n],args[2*n+1]);
449 vf->priv = vf_priv;
450 args = NULL;
451 } else // Otherwise we should have the '_oldargs_'
452 if(args && !strcmp(args[0],"_oldargs_"))
453 args = (char**)args[1];
454 else
455 args = NULL;
456 if(vf->info->open(vf,(char*)args)>0) return vf; // Success!
457 free(vf);
458 mp_tmsg(MSGT_VFILTER,MSGL_ERR,"Couldn't open video filter '%s'.\n",name);
459 return NULL;
462 vf_instance_t* vf_open_filter(struct MPOpts *opts, vf_instance_t* next, const char *name, char **args){
463 if(args && strcmp(args[0],"_oldargs_")) {
464 int i,l = 0;
465 for(i = 0 ; args && args[2*i] ; i++)
466 l += 1 + strlen(args[2*i]) + 1 + strlen(args[2*i+1]);
467 l += strlen(name);
469 char str[l+1];
470 char* p = str;
471 p += sprintf(str,"%s",name);
472 for(i = 0 ; args && args[2*i] ; i++)
473 p += sprintf(p," %s=%s",args[2*i],args[2*i+1]);
474 mp_tmsg(MSGT_VFILTER,MSGL_INFO,"Opening video filter: " "[%s]\n",str);
476 } else if(strcmp(name,"vo")) {
477 if(args && strcmp(args[0],"_oldargs_") == 0)
478 mp_tmsg(MSGT_VFILTER,MSGL_INFO,"Opening video filter: "
479 "[%s=%s]\n", name,args[1]);
480 else
481 mp_tmsg(MSGT_VFILTER,MSGL_INFO,"Opening video filter: "
482 "[%s]\n", name);
484 return vf_open_plugin(opts, filter_list,next,name,args);
488 * \brief adds a filter before the last one (which should be the vo filter).
489 * \param vf start of the filter chain.
490 * \param name name of the filter to add.
491 * \param args argument list for the filter.
492 * \return pointer to the filter instance that was created.
494 vf_instance_t* vf_add_before_vo(vf_instance_t **vf, char *name, char **args) {
495 struct MPOpts *opts = (*vf)->opts;
496 vf_instance_t *vo, *prev = NULL, *new;
497 // Find the last filter (should be vf_vo)
498 for (vo = *vf; vo->next; vo = vo->next)
499 prev = vo;
500 new = vf_open_filter(opts, vo, name, args);
501 if (prev)
502 prev->next = new;
503 else
504 *vf = new;
505 return new;
508 //============================================================================
510 unsigned int vf_match_csp(vf_instance_t** vfp,const unsigned int* list,unsigned int preferred){
511 vf_instance_t* vf=*vfp;
512 struct MPOpts *opts = vf->opts;
513 const unsigned int* p;
514 unsigned int best=0;
515 int ret;
516 if((p=list)) while(*p){
517 ret=vf->query_format(vf,*p);
518 mp_msg(MSGT_VFILTER,MSGL_V,"[%s] query(%s) -> %d\n",vf->info->name,vo_format_name(*p),ret&3);
519 if(ret&2){ best=*p; break;} // no conversion -> bingo!
520 if(ret&1 && !best) best=*p; // best with conversion
521 ++p;
523 if(best) return best; // bingo, they have common csp!
524 // ok, then try with scale:
525 if(vf->info == &vf_info_scale) return 0; // avoid infinite recursion!
526 vf=vf_open_filter(opts, vf,"scale",NULL);
527 if(!vf) return 0; // failed to init "scale"
528 // try the preferred csp first:
529 if(preferred && vf->query_format(vf,preferred)) best=preferred; else
530 // try the list again, now with "scaler" :
531 if((p=list)) while(*p){
532 ret=vf->query_format(vf,*p);
533 mp_msg(MSGT_VFILTER,MSGL_V,"[%s] query(%s) -> %d\n",vf->info->name,vo_format_name(*p),ret&3);
534 if(ret&2){ best=*p; break;} // no conversion -> bingo!
535 if(ret&1 && !best) best=*p; // best with conversion
536 ++p;
538 if(best) *vfp=vf; // else uninit vf !FIXME!
539 return best;
542 void vf_clone_mpi_attributes(mp_image_t* dst, mp_image_t* src){
543 dst->pict_type= src->pict_type;
544 dst->fields = src->fields;
545 dst->qscale_type= src->qscale_type;
546 if(dst->width == src->width && dst->height == src->height){
547 dst->qstride= src->qstride;
548 dst->qscale= src->qscale;
552 void vf_queue_frame(vf_instance_t *vf, int (*func)(vf_instance_t *))
554 vf->continue_buffered_image = func;
557 // Output the next buffered image (if any) from the filter chain.
558 // The queue could be kept as a simple stack/list instead avoiding the
559 // looping here, but there's currently no good context variable where
560 // that could be stored so this was easier to implement.
562 int vf_output_queued_frame(vf_instance_t *vf)
564 while (1) {
565 int ret;
566 vf_instance_t *current;
567 vf_instance_t *last=NULL;
568 int (*tmp)(vf_instance_t *);
569 for (current = vf; current; current = current->next)
570 if (current->continue_buffered_image)
571 last = current;
572 if (!last)
573 return 0;
574 tmp = last->continue_buffered_image;
575 last->continue_buffered_image = NULL;
576 ret = tmp(last);
577 if (ret)
578 return ret;
584 * \brief Video config() function wrapper
586 * Blocks config() calls with different size or format for filters
587 * with VFCAP_CONSTANT
589 * First call is redirected to vf->config.
591 * In following calls, it verifies that the configuration parameters
592 * are unchanged, and returns either success or error.
595 int vf_config_wrapper(struct vf_instance* vf,
596 int width, int height, int d_width, int d_height,
597 unsigned int flags, unsigned int outfmt)
599 int r;
600 if ((vf->default_caps&VFCAP_CONSTANT) && vf->fmt.have_configured) {
601 if ((vf->fmt.orig_width != width)
602 || (vf->fmt.orig_height != height)
603 || (vf->fmt.orig_fmt != outfmt)) {
604 mp_tmsg(MSGT_VFILTER,MSGL_ERR,"\nNew video file has different resolution or colorspace than the previous one.\n");
605 return 0;
607 return 1;
609 vf->fmt.have_configured = 1;
610 vf->fmt.orig_height = height;
611 vf->fmt.orig_width = width;
612 vf->fmt.orig_fmt = outfmt;
613 r = vf->config(vf, width, height, d_width, d_height, flags, outfmt);
614 if (!r) vf->fmt.have_configured = 0;
615 return r;
618 int vf_next_config(struct vf_instance* vf,
619 int width, int height, int d_width, int d_height,
620 unsigned int voflags, unsigned int outfmt){
621 struct MPOpts *opts = vf->opts;
622 int miss;
623 int flags=vf->next->query_format(vf->next,outfmt);
624 if(!flags){
625 // hmm. colorspace mismatch!!!
626 // let's insert the 'scale' filter, it does the job for us:
627 vf_instance_t* vf2;
628 if(vf->next->info==&vf_info_scale) return 0; // scale->scale
629 vf2=vf_open_filter(opts, vf->next,"scale",NULL);
630 if(!vf2) return 0; // shouldn't happen!
631 vf->next=vf2;
632 flags=vf->next->query_format(vf->next,outfmt);
633 if(!flags){
634 mp_tmsg(MSGT_VFILTER,MSGL_ERR,"Cannot find matching colorspace, even by inserting 'scale' :(\n");
635 return 0; // FAIL
638 mp_msg(MSGT_VFILTER,MSGL_V,"REQ: flags=0x%X req=0x%X \n",flags,vf->default_reqs);
639 miss=vf->default_reqs - (flags&vf->default_reqs);
640 if(miss&VFCAP_ACCEPT_STRIDE){
641 // vf requires stride support but vf->next doesn't support it!
642 // let's insert the 'expand' filter, it does the job for us:
643 vf_instance_t* vf2=vf_open_filter(opts, vf->next,"expand",NULL);
644 if(!vf2) return 0; // shouldn't happen!
645 vf->next=vf2;
647 vf->next->w = width; vf->next->h = height;
648 return vf_config_wrapper(vf->next,width,height,d_width,d_height,voflags,outfmt);
651 int vf_next_control(struct vf_instance* vf, int request, void* data){
652 return vf->next->control(vf->next,request,data);
655 int vf_next_query_format(struct vf_instance* vf, unsigned int fmt){
656 int flags=vf->next->query_format(vf->next,fmt);
657 if(flags) flags|=vf->default_caps;
658 return flags;
661 int vf_next_put_image(struct vf_instance* vf,mp_image_t *mpi, double pts){
662 return vf->next->put_image(vf->next,mpi, pts);
665 void vf_next_draw_slice(struct vf_instance* vf,unsigned char** src, int * stride,int w, int h, int x, int y){
666 if (vf->next->draw_slice) {
667 vf->next->draw_slice(vf->next,src,stride,w,h,x,y);
668 return;
670 if (!vf->dmpi) {
671 mp_msg(MSGT_VFILTER,MSGL_ERR,"draw_slice: dmpi not stored by vf_%s\n", vf->info->name);
672 return;
674 if (!(vf->dmpi->flags & MP_IMGFLAG_PLANAR)) {
675 memcpy_pic(vf->dmpi->planes[0]+y*vf->dmpi->stride[0]+vf->dmpi->bpp/8*x,
676 src[0], vf->dmpi->bpp/8*w, h, vf->dmpi->stride[0], stride[0]);
677 return;
679 memcpy_pic(vf->dmpi->planes[0]+y*vf->dmpi->stride[0]+x, src[0],
680 w, h, vf->dmpi->stride[0], stride[0]);
681 memcpy_pic(vf->dmpi->planes[1]+(y>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[1]+(x>>vf->dmpi->chroma_x_shift),
682 src[1], w>>vf->dmpi->chroma_x_shift, h>>vf->dmpi->chroma_y_shift, vf->dmpi->stride[1], stride[1]);
683 memcpy_pic(vf->dmpi->planes[2]+(y>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[2]+(x>>vf->dmpi->chroma_x_shift),
684 src[2], w>>vf->dmpi->chroma_x_shift, h>>vf->dmpi->chroma_y_shift, vf->dmpi->stride[2], stride[2]);
687 //============================================================================
689 vf_instance_t *append_filters(vf_instance_t* last,
690 struct m_obj_settings *vf_settings)
692 struct MPOpts *opts = last->opts;
693 vf_instance_t* vf;
694 int i;
696 if(vf_settings) {
697 // We want to add them in the 'right order'
698 for(i = 0 ; vf_settings[i].name ; i++)
699 /* NOP */;
700 for(i-- ; i >= 0 ; i--) {
701 //printf("Open filter %s\n",vf_settings[i].name);
702 vf = vf_open_filter(opts, last,vf_settings[i].name,vf_settings[i].attribs);
703 if(vf) last=vf;
706 return last;
709 //============================================================================
711 void vf_uninit_filter(vf_instance_t* vf){
712 if(vf->uninit) vf->uninit(vf);
713 free_mp_image(vf->imgctx.static_images[0]);
714 free_mp_image(vf->imgctx.static_images[1]);
715 free_mp_image(vf->imgctx.temp_images[0]);
716 free_mp_image(vf->imgctx.export_images[0]);
717 for (int i = 0; i < NUM_NUMBERED_MPI; i++)
718 free_mp_image(vf->imgctx.numbered_images[i]);
719 free(vf);
722 void vf_uninit_filter_chain(vf_instance_t* vf){
723 while(vf){
724 vf_instance_t* next=vf->next;
725 vf_uninit_filter(vf);
726 vf=next;