Merge svn changes up to r31050
[mplayer/glamo.git] / libmpcodecs / vf.c
blob0f2dcc75d2c1301123dee667e1b12480d5ac529e
1 /*
2 * This file is part of MPlayer.
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.
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.
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.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
23 #include "config.h"
24 #if HAVE_MALLOC_H
25 #include <malloc.h>
26 #endif
28 #ifdef MP_DEBUG
29 #include <assert.h>
30 #endif
32 #include "mp_msg.h"
33 #include "m_option.h"
34 #include "m_struct.h"
37 #include "img_format.h"
38 #include "mp_image.h"
39 #include "vf.h"
41 #include "libvo/fastmemcpy.h"
42 #include "libavutil/mem.h"
44 extern const vf_info_t vf_info_vo;
45 extern const vf_info_t vf_info_rectangle;
46 extern const vf_info_t vf_info_bmovl;
47 extern const vf_info_t vf_info_crop;
48 extern const vf_info_t vf_info_expand;
49 extern const vf_info_t vf_info_pp;
50 extern const vf_info_t vf_info_scale;
51 extern const vf_info_t vf_info_format;
52 extern const vf_info_t vf_info_noformat;
53 extern const vf_info_t vf_info_yuy2;
54 extern const vf_info_t vf_info_flip;
55 extern const vf_info_t vf_info_rgb2bgr;
56 extern const vf_info_t vf_info_rotate;
57 extern const vf_info_t vf_info_mirror;
58 extern const vf_info_t vf_info_palette;
59 extern const vf_info_t vf_info_lavc;
60 extern const vf_info_t vf_info_zrmjpeg;
61 extern const vf_info_t vf_info_dvbscale;
62 extern const vf_info_t vf_info_cropdetect;
63 extern const vf_info_t vf_info_test;
64 extern const vf_info_t vf_info_noise;
65 extern const vf_info_t vf_info_yvu9;
66 extern const vf_info_t vf_info_lavcdeint;
67 extern const vf_info_t vf_info_eq;
68 extern const vf_info_t vf_info_eq2;
69 extern const vf_info_t vf_info_gradfun;
70 extern const vf_info_t vf_info_halfpack;
71 extern const vf_info_t vf_info_dint;
72 extern const vf_info_t vf_info_1bpp;
73 extern const vf_info_t vf_info_2xsai;
74 extern const vf_info_t vf_info_unsharp;
75 extern const vf_info_t vf_info_swapuv;
76 extern const vf_info_t vf_info_il;
77 extern const vf_info_t vf_info_fil;
78 extern const vf_info_t vf_info_boxblur;
79 extern const vf_info_t vf_info_sab;
80 extern const vf_info_t vf_info_smartblur;
81 extern const vf_info_t vf_info_perspective;
82 extern const vf_info_t vf_info_down3dright;
83 extern const vf_info_t vf_info_field;
84 extern const vf_info_t vf_info_denoise3d;
85 extern const vf_info_t vf_info_hqdn3d;
86 extern const vf_info_t vf_info_detc;
87 extern const vf_info_t vf_info_telecine;
88 extern const vf_info_t vf_info_tinterlace;
89 extern const vf_info_t vf_info_tfields;
90 extern const vf_info_t vf_info_ivtc;
91 extern const vf_info_t vf_info_ilpack;
92 extern const vf_info_t vf_info_dsize;
93 extern const vf_info_t vf_info_decimate;
94 extern const vf_info_t vf_info_softpulldown;
95 extern const vf_info_t vf_info_pullup;
96 extern const vf_info_t vf_info_filmdint;
97 extern const vf_info_t vf_info_framestep;
98 extern const vf_info_t vf_info_tile;
99 extern const vf_info_t vf_info_delogo;
100 extern const vf_info_t vf_info_remove_logo;
101 extern const vf_info_t vf_info_hue;
102 extern const vf_info_t vf_info_spp;
103 extern const vf_info_t vf_info_uspp;
104 extern const vf_info_t vf_info_fspp;
105 extern const vf_info_t vf_info_pp7;
106 extern const vf_info_t vf_info_yuvcsp;
107 extern const vf_info_t vf_info_kerndeint;
108 extern const vf_info_t vf_info_rgbtest;
109 extern const vf_info_t vf_info_qp;
110 extern const vf_info_t vf_info_phase;
111 extern const vf_info_t vf_info_divtc;
112 extern const vf_info_t vf_info_harddup;
113 extern const vf_info_t vf_info_softskip;
114 extern const vf_info_t vf_info_screenshot;
115 extern const vf_info_t vf_info_ass;
116 extern const vf_info_t vf_info_mcdeint;
117 extern const vf_info_t vf_info_yadif;
118 extern const vf_info_t vf_info_blackframe;
119 extern const vf_info_t vf_info_geq;
120 extern const vf_info_t vf_info_ow;
121 extern const vf_info_t vf_info_fixpts;
123 // list of available filters:
124 static const vf_info_t* const filter_list[]={
125 &vf_info_rectangle,
126 #ifdef HAVE_POSIX_SELECT
127 &vf_info_bmovl,
128 #endif
129 &vf_info_crop,
130 &vf_info_expand,
131 #ifdef CONFIG_LIBPOSTPROC
132 &vf_info_pp,
133 #endif
134 &vf_info_scale,
135 // &vf_info_osd,
136 &vf_info_vo,
137 &vf_info_format,
138 &vf_info_noformat,
139 #ifdef CONFIG_LIBSWSCALE_INTERNALS
140 &vf_info_yuy2,
141 #endif
142 &vf_info_flip,
143 #ifdef CONFIG_LIBSWSCALE_INTERNALS
144 &vf_info_rgb2bgr,
145 #endif
146 &vf_info_rotate,
147 &vf_info_mirror,
148 #ifdef CONFIG_LIBSWSCALE_INTERNALS
149 &vf_info_palette,
150 #endif
151 &vf_info_pp7,
152 #ifdef CONFIG_LIBAVCODEC
153 &vf_info_lavc,
154 &vf_info_lavcdeint,
155 &vf_info_screenshot,
156 &vf_info_uspp,
157 #endif
158 #ifdef CONFIG_ZR
159 &vf_info_zrmjpeg,
160 #endif
161 &vf_info_dvbscale,
162 &vf_info_cropdetect,
163 &vf_info_test,
164 &vf_info_noise,
165 &vf_info_yvu9,
166 &vf_info_eq,
167 &vf_info_eq2,
168 &vf_info_gradfun,
169 #ifdef CONFIG_LIBSWSCALE_INTERNALS
170 &vf_info_halfpack,
171 #endif
172 &vf_info_dint,
173 &vf_info_1bpp,
174 &vf_info_2xsai,
175 &vf_info_unsharp,
176 &vf_info_swapuv,
177 &vf_info_il,
178 &vf_info_fil,
179 &vf_info_boxblur,
180 &vf_info_sab,
181 &vf_info_smartblur,
182 &vf_info_perspective,
183 &vf_info_down3dright,
184 &vf_info_field,
185 &vf_info_denoise3d,
186 &vf_info_hqdn3d,
187 &vf_info_detc,
188 &vf_info_telecine,
189 &vf_info_tinterlace,
190 &vf_info_tfields,
191 &vf_info_ivtc,
192 &vf_info_ilpack,
193 &vf_info_dsize,
194 &vf_info_decimate,
195 &vf_info_softpulldown,
196 &vf_info_pullup,
197 &vf_info_filmdint,
198 &vf_info_framestep,
199 &vf_info_tile,
200 &vf_info_delogo,
201 &vf_info_remove_logo,
202 &vf_info_hue,
203 #ifdef CONFIG_LIBAVCODEC_INTERNALS
204 &vf_info_spp,
205 &vf_info_fspp,
206 &vf_info_qp,
207 &vf_info_mcdeint,
208 &vf_info_geq,
209 #endif
210 &vf_info_yuvcsp,
211 &vf_info_kerndeint,
212 &vf_info_rgbtest,
213 &vf_info_phase,
214 &vf_info_divtc,
215 &vf_info_harddup,
216 &vf_info_softskip,
217 #ifdef CONFIG_ASS
218 &vf_info_ass,
219 #endif
220 &vf_info_yadif,
221 &vf_info_blackframe,
222 &vf_info_ow,
223 &vf_info_fixpts,
224 NULL
227 // For the vf option
228 const m_obj_list_t vf_obj_list = {
229 (void**)filter_list,
230 M_ST_OFF(vf_info_t,name),
231 M_ST_OFF(vf_info_t,info),
232 M_ST_OFF(vf_info_t,opts)
235 //============================================================================
236 // mpi stuff:
238 void vf_mpi_clear(mp_image_t* mpi,int x0,int y0,int w,int h){
239 int y;
240 if(mpi->flags&MP_IMGFLAG_PLANAR){
241 y0&=~1;h+=h&1;
242 if(x0==0 && w==mpi->width){
243 // full width clear:
244 memset(mpi->planes[0]+mpi->stride[0]*y0,0,mpi->stride[0]*h);
245 memset(mpi->planes[1]+mpi->stride[1]*(y0>>mpi->chroma_y_shift),128,mpi->stride[1]*(h>>mpi->chroma_y_shift));
246 memset(mpi->planes[2]+mpi->stride[2]*(y0>>mpi->chroma_y_shift),128,mpi->stride[2]*(h>>mpi->chroma_y_shift));
247 } else
248 for(y=y0;y<y0+h;y+=2){
249 memset(mpi->planes[0]+x0+mpi->stride[0]*y,0,w);
250 memset(mpi->planes[0]+x0+mpi->stride[0]*(y+1),0,w);
251 memset(mpi->planes[1]+(x0>>mpi->chroma_x_shift)+mpi->stride[1]*(y>>mpi->chroma_y_shift),128,(w>>mpi->chroma_x_shift));
252 memset(mpi->planes[2]+(x0>>mpi->chroma_x_shift)+mpi->stride[2]*(y>>mpi->chroma_y_shift),128,(w>>mpi->chroma_x_shift));
254 return;
256 // packed:
257 for(y=y0;y<y0+h;y++){
258 unsigned char* dst=mpi->planes[0]+mpi->stride[0]*y+(mpi->bpp>>3)*x0;
259 if(mpi->flags&MP_IMGFLAG_YUV){
260 unsigned int* p=(unsigned int*) dst;
261 int size=(mpi->bpp>>3)*w/4;
262 int i;
263 #if HAVE_BIGENDIAN
264 #define CLEAR_PACKEDYUV_PATTERN 0x00800080
265 #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x80008000
266 #else
267 #define CLEAR_PACKEDYUV_PATTERN 0x80008000
268 #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x00800080
269 #endif
270 if(mpi->flags&MP_IMGFLAG_SWAPPED){
271 for(i=0;i<size-3;i+=4) p[i]=p[i+1]=p[i+2]=p[i+3]=CLEAR_PACKEDYUV_PATTERN_SWAPPED;
272 for(;i<size;i++) p[i]=CLEAR_PACKEDYUV_PATTERN_SWAPPED;
273 } else {
274 for(i=0;i<size-3;i+=4) p[i]=p[i+1]=p[i+2]=p[i+3]=CLEAR_PACKEDYUV_PATTERN;
275 for(;i<size;i++) p[i]=CLEAR_PACKEDYUV_PATTERN;
277 } else
278 memset(dst,0,(mpi->bpp>>3)*w);
282 mp_image_t* vf_get_image(vf_instance_t* vf, unsigned int outfmt, int mp_imgtype, int mp_imgflag, int w, int h){
283 mp_image_t* mpi=NULL;
284 int w2;
285 int number = mp_imgtype >> 16;
287 #ifdef MP_DEBUG
288 assert(w == -1 || w >= vf->w);
289 assert(h == -1 || h >= vf->h);
290 assert(vf->w > 0);
291 assert(vf->h > 0);
292 #endif
294 // fprintf(stderr, "get_image: %d:%d, vf: %d:%d\n", w,h,vf->w,vf->h);
296 if (w == -1) w = vf->w;
297 if (h == -1) h = vf->h;
299 w2=(mp_imgflag&MP_IMGFLAG_ACCEPT_ALIGNED_STRIDE)?((w+15)&(~15)):w;
301 if(vf->put_image==vf_next_put_image){
302 // passthru mode, if the filter uses the fallback/default put_image() code
303 return vf_get_image(vf->next,outfmt,mp_imgtype,mp_imgflag,w,h);
306 // Note: we should call libvo first to check if it supports direct rendering
307 // and if not, then fallback to software buffers:
308 switch(mp_imgtype & 0xff){
309 case MP_IMGTYPE_EXPORT:
310 if(!vf->imgctx.export_images[0]) vf->imgctx.export_images[0]=new_mp_image(w2,h);
311 mpi=vf->imgctx.export_images[0];
312 break;
313 case MP_IMGTYPE_STATIC:
314 if(!vf->imgctx.static_images[0]) vf->imgctx.static_images[0]=new_mp_image(w2,h);
315 mpi=vf->imgctx.static_images[0];
316 break;
317 case MP_IMGTYPE_TEMP:
318 if(!vf->imgctx.temp_images[0]) vf->imgctx.temp_images[0]=new_mp_image(w2,h);
319 mpi=vf->imgctx.temp_images[0];
320 break;
321 case MP_IMGTYPE_IPB:
322 if(!(mp_imgflag&MP_IMGFLAG_READABLE)){ // B frame:
323 if(!vf->imgctx.temp_images[0]) vf->imgctx.temp_images[0]=new_mp_image(w2,h);
324 mpi=vf->imgctx.temp_images[0];
325 break;
327 case MP_IMGTYPE_IP:
328 if(!vf->imgctx.static_images[vf->imgctx.static_idx]) vf->imgctx.static_images[vf->imgctx.static_idx]=new_mp_image(w2,h);
329 mpi=vf->imgctx.static_images[vf->imgctx.static_idx];
330 vf->imgctx.static_idx^=1;
331 break;
332 case MP_IMGTYPE_NUMBERED:
333 if (number == -1) {
334 int i;
335 for (i = 0; i < NUM_NUMBERED_MPI; i++)
336 if (!vf->imgctx.numbered_images[i] || !vf->imgctx.numbered_images[i]->usage_count)
337 break;
338 number = i;
340 if (number < 0 || number >= NUM_NUMBERED_MPI) return NULL;
341 if (!vf->imgctx.numbered_images[number]) vf->imgctx.numbered_images[number] = new_mp_image(w2,h);
342 mpi = vf->imgctx.numbered_images[number];
343 mpi->number = number;
344 break;
346 if(mpi){
347 mpi->type=mp_imgtype;
348 mpi->w=vf->w; mpi->h=vf->h;
349 // keep buffer allocation status & color flags only:
350 // mpi->flags&=~(MP_IMGFLAG_PRESERVE|MP_IMGFLAG_READABLE|MP_IMGFLAG_DIRECT);
351 mpi->flags&=MP_IMGFLAG_ALLOCATED|MP_IMGFLAG_TYPE_DISPLAYED|MP_IMGFLAGMASK_COLORS;
352 // accept restrictions, draw_slice and palette flags only:
353 mpi->flags|=mp_imgflag&(MP_IMGFLAGMASK_RESTRICTIONS|MP_IMGFLAG_DRAW_CALLBACK|MP_IMGFLAG_RGB_PALETTE);
354 if(!vf->draw_slice) mpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
355 if(mpi->width!=w2 || mpi->height!=h){
356 // printf("vf.c: MPI parameters changed! %dx%d -> %dx%d \n", mpi->width,mpi->height,w2,h);
357 if(mpi->flags&MP_IMGFLAG_ALLOCATED){
358 if(mpi->width<w2 || mpi->height<h){
359 // need to re-allocate buffer memory:
360 av_free(mpi->planes[0]);
361 mpi->flags&=~MP_IMGFLAG_ALLOCATED;
362 mp_msg(MSGT_VFILTER,MSGL_V,"vf.c: have to REALLOCATE buffer memory :(\n");
364 // } else {
366 mpi->width=w2; mpi->chroma_width=(w2 + (1<<mpi->chroma_x_shift) - 1)>>mpi->chroma_x_shift;
367 mpi->height=h; mpi->chroma_height=(h + (1<<mpi->chroma_y_shift) - 1)>>mpi->chroma_y_shift;
370 if(!mpi->bpp) mp_image_setfmt(mpi,outfmt);
371 if(!(mpi->flags&MP_IMGFLAG_ALLOCATED) && mpi->type>MP_IMGTYPE_EXPORT){
373 // check libvo first!
374 if(vf->get_image) vf->get_image(vf,mpi);
376 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
377 // non-direct and not yet allocated image. allocate it!
378 if (!mpi->bpp) { // no way we can allocate this
379 mp_msg(MSGT_DECVIDEO, MSGL_FATAL,
380 "vf_get_image: Tried to allocate a format that can not be allocated!\n");
381 return NULL;
384 // check if codec prefer aligned stride:
385 if(mp_imgflag&MP_IMGFLAG_PREFER_ALIGNED_STRIDE){
386 int align=(mpi->flags&MP_IMGFLAG_PLANAR &&
387 mpi->flags&MP_IMGFLAG_YUV) ?
388 (8<<mpi->chroma_x_shift)-1 : 15; // -- maybe FIXME
389 w2=((w+align)&(~align));
390 if(mpi->width!=w2){
391 // we have to change width... check if we CAN co it:
392 int flags=vf->query_format(vf,outfmt); // should not fail
393 if(!(flags&3)) mp_msg(MSGT_DECVIDEO,MSGL_WARN,"??? vf_get_image{vf->query_format(outfmt)} failed!\n");
394 // printf("query -> 0x%X \n",flags);
395 if(flags&VFCAP_ACCEPT_STRIDE){
396 mpi->width=w2;
397 mpi->chroma_width=(w2 + (1<<mpi->chroma_x_shift) - 1)>>mpi->chroma_x_shift;
402 mp_image_alloc_planes(mpi);
403 // printf("clearing img!\n");
404 vf_mpi_clear(mpi,0,0,mpi->width,mpi->height);
407 if(mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)
408 if(vf->start_slice) vf->start_slice(vf,mpi);
409 if(!(mpi->flags&MP_IMGFLAG_TYPE_DISPLAYED)){
410 mp_msg(MSGT_DECVIDEO,MSGL_V,"*** [%s] %s%s mp_image_t, %dx%dx%dbpp %s %s, %d bytes\n",
411 vf->info->name,
412 (mpi->type==MP_IMGTYPE_EXPORT)?"Exporting":
413 ((mpi->flags&MP_IMGFLAG_DIRECT)?"Direct Rendering":"Allocating"),
414 (mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)?" (slices)":"",
415 mpi->width,mpi->height,mpi->bpp,
416 (mpi->flags&MP_IMGFLAG_YUV)?"YUV":((mpi->flags&MP_IMGFLAG_SWAPPED)?"BGR":"RGB"),
417 (mpi->flags&MP_IMGFLAG_PLANAR)?"planar":"packed",
418 mpi->bpp*mpi->width*mpi->height/8);
419 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",
420 mpi->imgfmt, mpi->planes[0], mpi->planes[1], mpi->planes[2],
421 mpi->stride[0], mpi->stride[1], mpi->stride[2],
422 mpi->chroma_width, mpi->chroma_height, mpi->chroma_x_shift, mpi->chroma_y_shift);
423 mpi->flags|=MP_IMGFLAG_TYPE_DISPLAYED;
426 mpi->qscale = NULL;
428 mpi->usage_count++;
429 // printf("\rVF_MPI: %p %p %p %d %d %d \n",
430 // mpi->planes[0],mpi->planes[1],mpi->planes[2],
431 // mpi->stride[0],mpi->stride[1],mpi->stride[2]);
432 return mpi;
435 //============================================================================
437 // By default vf doesn't accept MPEGPES
438 static int vf_default_query_format(struct vf_instance* vf, unsigned int fmt){
439 if(fmt == IMGFMT_MPEGPES) return 0;
440 return vf_next_query_format(vf,fmt);
443 struct vf_instance *vf_open_plugin_noerr(struct MPOpts *opts,
444 const vf_info_t * const *filter_list,
445 vf_instance_t *next, const char *name,
446 char **args, int *retcode)
448 vf_instance_t* vf;
449 int i;
450 for(i=0;;i++){
451 if(!filter_list[i]){
452 mp_tmsg(MSGT_VFILTER,MSGL_ERR,"Couldn't find video filter '%s'.\n",name);
453 return NULL; // no such filter!
455 if(!strcmp(filter_list[i]->name,name)) break;
457 vf = calloc(1, sizeof *vf);
458 vf->opts = opts;
459 vf->info=filter_list[i];
460 vf->next=next;
461 vf->config=vf_next_config;
462 vf->control=vf_next_control;
463 vf->query_format=vf_default_query_format;
464 vf->put_image=vf_next_put_image;
465 vf->default_caps=VFCAP_ACCEPT_STRIDE;
466 vf->default_reqs=0;
467 if(vf->info->opts) { // vf_vo get some special argument
468 const m_struct_t* st = vf->info->opts;
469 void* vf_priv = m_struct_alloc(st);
470 int n;
471 for(n = 0 ; args && args[2*n] ; n++)
472 m_struct_set(st,vf_priv,args[2*n],args[2*n+1]);
473 vf->priv = vf_priv;
474 args = NULL;
475 } else // Otherwise we should have the '_oldargs_'
476 if(args && !strcmp(args[0],"_oldargs_"))
477 args = (char**)args[1];
478 else
479 args = NULL;
480 *retcode = vf->info->vf_open(vf,(char*)args);
481 if (*retcode > 0)
482 return vf;
483 free(vf);
484 return NULL;
487 struct vf_instance *vf_open_plugin(struct MPOpts *opts,
488 const vf_info_t * const *filter_list,
489 vf_instance_t *next, const char *name,
490 char **args)
492 struct vf_instance *vf = vf_open_plugin_noerr(opts, filter_list, next,
493 name, args, &(int){0});
494 if (!vf)
495 mp_tmsg(MSGT_VFILTER, MSGL_ERR, "Couldn't open video filter '%s'.\n",
496 name);
497 return vf;
500 vf_instance_t* vf_open_filter(struct MPOpts *opts, vf_instance_t* next, const char *name, char **args){
501 if(args && strcmp(args[0],"_oldargs_")) {
502 int i,l = 0;
503 for(i = 0 ; args && args[2*i] ; i++)
504 l += 1 + strlen(args[2*i]) + 1 + strlen(args[2*i+1]);
505 l += strlen(name);
507 char str[l+1];
508 char* p = str;
509 p += sprintf(str,"%s",name);
510 for(i = 0 ; args && args[2*i] ; i++)
511 p += sprintf(p," %s=%s",args[2*i],args[2*i+1]);
512 mp_msg(MSGT_VFILTER, MSGL_INFO, "%s[%s]\n",
513 mp_gtext("Opening video filter: "), str);
515 } else if(strcmp(name,"vo")) {
516 if(args && strcmp(args[0],"_oldargs_") == 0)
517 mp_msg(MSGT_VFILTER, MSGL_INFO, "%s[%s=%s]\n",
518 mp_gtext("Opening video filter: "), name, args[1]);
519 else
520 mp_msg(MSGT_VFILTER, MSGL_INFO, "%s[%s]\n",
521 mp_gtext("Opening video filter: "), name);
523 return vf_open_plugin(opts, filter_list,next,name,args);
527 * \brief adds a filter before the last one (which should be the vo filter).
528 * \param vf start of the filter chain.
529 * \param name name of the filter to add.
530 * \param args argument list for the filter.
531 * \return pointer to the filter instance that was created.
533 vf_instance_t* vf_add_before_vo(vf_instance_t **vf, char *name, char **args) {
534 struct MPOpts *opts = (*vf)->opts;
535 vf_instance_t *vo, *prev = NULL, *new;
536 // Find the last filter (should be vf_vo)
537 for (vo = *vf; vo->next; vo = vo->next)
538 prev = vo;
539 new = vf_open_filter(opts, vo, name, args);
540 if (prev)
541 prev->next = new;
542 else
543 *vf = new;
544 return new;
547 //============================================================================
549 unsigned int vf_match_csp(vf_instance_t** vfp,const unsigned int* list,unsigned int preferred){
550 vf_instance_t* vf=*vfp;
551 struct MPOpts *opts = vf->opts;
552 const unsigned int* p;
553 unsigned int best=0;
554 int ret;
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) return best; // bingo, they have common csp!
563 // ok, then try with scale:
564 if(vf->info == &vf_info_scale) return 0; // avoid infinite recursion!
565 vf=vf_open_filter(opts, vf,"scale",NULL);
566 if(!vf) return 0; // failed to init "scale"
567 // try the preferred csp first:
568 if(preferred && vf->query_format(vf,preferred)) best=preferred; else
569 // try the list again, now with "scaler" :
570 if((p=list)) while(*p){
571 ret=vf->query_format(vf,*p);
572 mp_msg(MSGT_VFILTER,MSGL_V,"[%s] query(%s) -> %d\n",vf->info->name,vo_format_name(*p),ret&3);
573 if(ret&2){ best=*p; break;} // no conversion -> bingo!
574 if(ret&1 && !best) best=*p; // best with conversion
575 ++p;
577 if(best) *vfp=vf; // else uninit vf !FIXME!
578 return best;
581 void vf_clone_mpi_attributes(mp_image_t* dst, mp_image_t* src){
582 dst->pict_type= src->pict_type;
583 dst->fields = src->fields;
584 dst->qscale_type= src->qscale_type;
585 if(dst->width == src->width && dst->height == src->height){
586 dst->qstride= src->qstride;
587 dst->qscale= src->qscale;
591 void vf_queue_frame(vf_instance_t *vf, int (*func)(vf_instance_t *))
593 vf->continue_buffered_image = func;
596 // Output the next buffered image (if any) from the filter chain.
597 // The queue could be kept as a simple stack/list instead avoiding the
598 // looping here, but there's currently no good context variable where
599 // that could be stored so this was easier to implement.
601 int vf_output_queued_frame(vf_instance_t *vf)
603 while (1) {
604 int ret;
605 vf_instance_t *current;
606 vf_instance_t *last=NULL;
607 int (*tmp)(vf_instance_t *);
608 for (current = vf; current; current = current->next)
609 if (current->continue_buffered_image)
610 last = current;
611 if (!last)
612 return 0;
613 tmp = last->continue_buffered_image;
614 last->continue_buffered_image = NULL;
615 ret = tmp(last);
616 if (ret)
617 return ret;
623 * \brief Video config() function wrapper
625 * Blocks config() calls with different size or format for filters
626 * with VFCAP_CONSTANT
628 * First call is redirected to vf->config.
630 * In following calls, it verifies that the configuration parameters
631 * are unchanged, and returns either success or error.
634 int vf_config_wrapper(struct vf_instance* vf,
635 int width, int height, int d_width, int d_height,
636 unsigned int flags, unsigned int outfmt)
638 int r;
639 if ((vf->default_caps&VFCAP_CONSTANT) && vf->fmt.have_configured) {
640 if ((vf->fmt.orig_width != width)
641 || (vf->fmt.orig_height != height)
642 || (vf->fmt.orig_fmt != outfmt)) {
643 mp_tmsg(MSGT_VFILTER,MSGL_ERR,"\nNew video file has different resolution or colorspace than the previous one.\n");
644 return 0;
646 return 1;
648 vf->fmt.have_configured = 1;
649 vf->fmt.orig_height = height;
650 vf->fmt.orig_width = width;
651 vf->fmt.orig_fmt = outfmt;
652 r = vf->config(vf, width, height, d_width, d_height, flags, outfmt);
653 if (!r) vf->fmt.have_configured = 0;
654 return r;
657 int vf_next_config(struct vf_instance* vf,
658 int width, int height, int d_width, int d_height,
659 unsigned int voflags, unsigned int outfmt){
660 struct MPOpts *opts = vf->opts;
661 int miss;
662 int flags=vf->next->query_format(vf->next,outfmt);
663 if(!flags){
664 // hmm. colorspace mismatch!!!
665 // let's insert the 'scale' filter, it does the job for us:
666 vf_instance_t* vf2;
667 if(vf->next->info==&vf_info_scale) return 0; // scale->scale
668 vf2=vf_open_filter(opts, vf->next,"scale",NULL);
669 if(!vf2) return 0; // shouldn't happen!
670 vf->next=vf2;
671 flags=vf->next->query_format(vf->next,outfmt);
672 if(!flags){
673 mp_tmsg(MSGT_VFILTER,MSGL_ERR,"Cannot find matching colorspace, even by inserting 'scale' :(\n");
674 return 0; // FAIL
677 mp_msg(MSGT_VFILTER,MSGL_V,"REQ: flags=0x%X req=0x%X \n",flags,vf->default_reqs);
678 miss=vf->default_reqs - (flags&vf->default_reqs);
679 if(miss&VFCAP_ACCEPT_STRIDE){
680 // vf requires stride support but vf->next doesn't support it!
681 // let's insert the 'expand' filter, it does the job for us:
682 vf_instance_t* vf2=vf_open_filter(opts, vf->next,"expand",NULL);
683 if(!vf2) return 0; // shouldn't happen!
684 vf->next=vf2;
686 vf->next->w = width; vf->next->h = height;
687 return vf_config_wrapper(vf->next,width,height,d_width,d_height,voflags,outfmt);
690 int vf_next_control(struct vf_instance* vf, int request, void* data){
691 return vf->next->control(vf->next,request,data);
694 int vf_next_query_format(struct vf_instance* vf, unsigned int fmt){
695 int flags=vf->next->query_format(vf->next,fmt);
696 if(flags) flags|=vf->default_caps;
697 return flags;
700 int vf_next_put_image(struct vf_instance* vf,mp_image_t *mpi, double pts){
701 return vf->next->put_image(vf->next,mpi, pts);
704 void vf_next_draw_slice(struct vf_instance* vf,unsigned char** src, int * stride,int w, int h, int x, int y){
705 if (vf->next->draw_slice) {
706 vf->next->draw_slice(vf->next,src,stride,w,h,x,y);
707 return;
709 if (!vf->dmpi) {
710 mp_msg(MSGT_VFILTER,MSGL_ERR,"draw_slice: dmpi not stored by vf_%s\n", vf->info->name);
711 return;
713 if (!(vf->dmpi->flags & MP_IMGFLAG_PLANAR)) {
714 memcpy_pic(vf->dmpi->planes[0]+y*vf->dmpi->stride[0]+vf->dmpi->bpp/8*x,
715 src[0], vf->dmpi->bpp/8*w, h, vf->dmpi->stride[0], stride[0]);
716 return;
718 memcpy_pic(vf->dmpi->planes[0]+y*vf->dmpi->stride[0]+x, src[0],
719 w, h, vf->dmpi->stride[0], stride[0]);
720 memcpy_pic(vf->dmpi->planes[1]+(y>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[1]+(x>>vf->dmpi->chroma_x_shift),
721 src[1], w>>vf->dmpi->chroma_x_shift, h>>vf->dmpi->chroma_y_shift, vf->dmpi->stride[1], stride[1]);
722 memcpy_pic(vf->dmpi->planes[2]+(y>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[2]+(x>>vf->dmpi->chroma_x_shift),
723 src[2], w>>vf->dmpi->chroma_x_shift, h>>vf->dmpi->chroma_y_shift, vf->dmpi->stride[2], stride[2]);
726 //============================================================================
728 vf_instance_t *append_filters(vf_instance_t* last,
729 struct m_obj_settings *vf_settings)
731 struct MPOpts *opts = last->opts;
732 vf_instance_t* vf;
733 int i;
735 if(vf_settings) {
736 // We want to add them in the 'right order'
737 for(i = 0 ; vf_settings[i].name ; i++)
738 /* NOP */;
739 for(i-- ; i >= 0 ; i--) {
740 //printf("Open filter %s\n",vf_settings[i].name);
741 vf = vf_open_filter(opts, last,vf_settings[i].name,vf_settings[i].attribs);
742 if(vf) last=vf;
745 return last;
748 //============================================================================
750 void vf_uninit_filter(vf_instance_t* vf){
751 if(vf->uninit) vf->uninit(vf);
752 free_mp_image(vf->imgctx.static_images[0]);
753 free_mp_image(vf->imgctx.static_images[1]);
754 free_mp_image(vf->imgctx.temp_images[0]);
755 free_mp_image(vf->imgctx.export_images[0]);
756 for (int i = 0; i < NUM_NUMBERED_MPI; i++)
757 free_mp_image(vf->imgctx.numbered_images[i]);
758 free(vf);
761 void vf_uninit_filter_chain(vf_instance_t* vf){
762 while(vf){
763 vf_instance_t* next=vf->next;
764 vf_uninit_filter(vf);
765 vf=next;