Merge svn changes up to r30694
[mplayer/glamo.git] / libmpcodecs / vf.c
blobfa72ad25aa61f68d97624034b9c28c8755be454c
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 "help_mp.h"
34 #include "m_option.h"
35 #include "m_struct.h"
38 #include "img_format.h"
39 #include "mp_image.h"
40 #include "vf.h"
42 #include "libvo/fastmemcpy.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;
122 // list of available filters:
123 static const vf_info_t* const filter_list[]={
124 &vf_info_rectangle,
125 #ifdef HAVE_POSIX_SELECT
126 &vf_info_bmovl,
127 #endif
128 &vf_info_crop,
129 &vf_info_expand,
130 #ifdef CONFIG_LIBPOSTPROC
131 &vf_info_pp,
132 #endif
133 &vf_info_scale,
134 // &vf_info_osd,
135 &vf_info_vo,
136 &vf_info_format,
137 &vf_info_noformat,
138 #ifdef CONFIG_LIBSWSCALE_INTERNALS
139 &vf_info_yuy2,
140 #endif
141 &vf_info_flip,
142 #ifdef CONFIG_LIBSWSCALE_INTERNALS
143 &vf_info_rgb2bgr,
144 #endif
145 &vf_info_rotate,
146 &vf_info_mirror,
147 #ifdef CONFIG_LIBSWSCALE_INTERNALS
148 &vf_info_palette,
149 #endif
150 &vf_info_pp7,
151 #ifdef CONFIG_LIBAVCODEC
152 &vf_info_lavc,
153 &vf_info_lavcdeint,
154 &vf_info_screenshot,
155 &vf_info_uspp,
156 #endif
157 #ifdef CONFIG_ZR
158 &vf_info_zrmjpeg,
159 #endif
160 &vf_info_dvbscale,
161 &vf_info_cropdetect,
162 &vf_info_test,
163 &vf_info_noise,
164 &vf_info_yvu9,
165 &vf_info_eq,
166 &vf_info_eq2,
167 &vf_info_gradfun,
168 #ifdef CONFIG_LIBSWSCALE_INTERNALS
169 &vf_info_halfpack,
170 #endif
171 &vf_info_dint,
172 &vf_info_1bpp,
173 &vf_info_2xsai,
174 &vf_info_unsharp,
175 &vf_info_swapuv,
176 &vf_info_il,
177 &vf_info_fil,
178 &vf_info_boxblur,
179 &vf_info_sab,
180 &vf_info_smartblur,
181 &vf_info_perspective,
182 &vf_info_down3dright,
183 &vf_info_field,
184 &vf_info_denoise3d,
185 &vf_info_hqdn3d,
186 &vf_info_detc,
187 &vf_info_telecine,
188 &vf_info_tinterlace,
189 &vf_info_tfields,
190 &vf_info_ivtc,
191 &vf_info_ilpack,
192 &vf_info_dsize,
193 &vf_info_decimate,
194 &vf_info_softpulldown,
195 &vf_info_pullup,
196 &vf_info_filmdint,
197 &vf_info_framestep,
198 &vf_info_tile,
199 &vf_info_delogo,
200 &vf_info_remove_logo,
201 &vf_info_hue,
202 #ifdef CONFIG_LIBAVCODEC_INTERNALS
203 &vf_info_spp,
204 &vf_info_fspp,
205 &vf_info_qp,
206 &vf_info_mcdeint,
207 &vf_info_geq,
208 #endif
209 &vf_info_yuvcsp,
210 &vf_info_kerndeint,
211 &vf_info_rgbtest,
212 &vf_info_phase,
213 &vf_info_divtc,
214 &vf_info_harddup,
215 &vf_info_softskip,
216 #ifdef CONFIG_ASS
217 &vf_info_ass,
218 #endif
219 &vf_info_yadif,
220 &vf_info_blackframe,
221 &vf_info_ow,
222 NULL
225 // For the vf option
226 const m_obj_list_t vf_obj_list = {
227 (void**)filter_list,
228 M_ST_OFF(vf_info_t,name),
229 M_ST_OFF(vf_info_t,info),
230 M_ST_OFF(vf_info_t,opts)
233 //============================================================================
234 // mpi stuff:
236 void vf_mpi_clear(mp_image_t* mpi,int x0,int y0,int w,int h){
237 int y;
238 if(mpi->flags&MP_IMGFLAG_PLANAR){
239 y0&=~1;h+=h&1;
240 if(x0==0 && w==mpi->width){
241 // full width clear:
242 memset(mpi->planes[0]+mpi->stride[0]*y0,0,mpi->stride[0]*h);
243 memset(mpi->planes[1]+mpi->stride[1]*(y0>>mpi->chroma_y_shift),128,mpi->stride[1]*(h>>mpi->chroma_y_shift));
244 memset(mpi->planes[2]+mpi->stride[2]*(y0>>mpi->chroma_y_shift),128,mpi->stride[2]*(h>>mpi->chroma_y_shift));
245 } else
246 for(y=y0;y<y0+h;y+=2){
247 memset(mpi->planes[0]+x0+mpi->stride[0]*y,0,w);
248 memset(mpi->planes[0]+x0+mpi->stride[0]*(y+1),0,w);
249 memset(mpi->planes[1]+(x0>>mpi->chroma_x_shift)+mpi->stride[1]*(y>>mpi->chroma_y_shift),128,(w>>mpi->chroma_x_shift));
250 memset(mpi->planes[2]+(x0>>mpi->chroma_x_shift)+mpi->stride[2]*(y>>mpi->chroma_y_shift),128,(w>>mpi->chroma_x_shift));
252 return;
254 // packed:
255 for(y=y0;y<y0+h;y++){
256 unsigned char* dst=mpi->planes[0]+mpi->stride[0]*y+(mpi->bpp>>3)*x0;
257 if(mpi->flags&MP_IMGFLAG_YUV){
258 unsigned int* p=(unsigned int*) dst;
259 int size=(mpi->bpp>>3)*w/4;
260 int i;
261 #if HAVE_BIGENDIAN
262 #define CLEAR_PACKEDYUV_PATTERN 0x00800080
263 #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x80008000
264 #else
265 #define CLEAR_PACKEDYUV_PATTERN 0x80008000
266 #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x00800080
267 #endif
268 if(mpi->flags&MP_IMGFLAG_SWAPPED){
269 for(i=0;i<size-3;i+=4) p[i]=p[i+1]=p[i+2]=p[i+3]=CLEAR_PACKEDYUV_PATTERN_SWAPPED;
270 for(;i<size;i++) p[i]=CLEAR_PACKEDYUV_PATTERN_SWAPPED;
271 } else {
272 for(i=0;i<size-3;i+=4) p[i]=p[i+1]=p[i+2]=p[i+3]=CLEAR_PACKEDYUV_PATTERN;
273 for(;i<size;i++) p[i]=CLEAR_PACKEDYUV_PATTERN;
275 } else
276 memset(dst,0,(mpi->bpp>>3)*w);
280 mp_image_t* vf_get_image(vf_instance_t* vf, unsigned int outfmt, int mp_imgtype, int mp_imgflag, int w, int h){
281 mp_image_t* mpi=NULL;
282 int w2;
283 int number = mp_imgtype >> 16;
285 #ifdef MP_DEBUG
286 assert(w == -1 || w >= vf->w);
287 assert(h == -1 || h >= vf->h);
288 assert(vf->w > 0);
289 assert(vf->h > 0);
290 #endif
292 // fprintf(stderr, "get_image: %d:%d, vf: %d:%d\n", w,h,vf->w,vf->h);
294 if (w == -1) w = vf->w;
295 if (h == -1) h = vf->h;
297 w2=(mp_imgflag&MP_IMGFLAG_ACCEPT_ALIGNED_STRIDE)?((w+15)&(~15)):w;
299 if(vf->put_image==vf_next_put_image){
300 // passthru mode, if the filter uses the fallback/default put_image() code
301 return vf_get_image(vf->next,outfmt,mp_imgtype,mp_imgflag,w,h);
304 // Note: we should call libvo first to check if it supports direct rendering
305 // and if not, then fallback to software buffers:
306 switch(mp_imgtype & 0xff){
307 case MP_IMGTYPE_EXPORT:
308 if(!vf->imgctx.export_images[0]) vf->imgctx.export_images[0]=new_mp_image(w2,h);
309 mpi=vf->imgctx.export_images[0];
310 break;
311 case MP_IMGTYPE_STATIC:
312 if(!vf->imgctx.static_images[0]) vf->imgctx.static_images[0]=new_mp_image(w2,h);
313 mpi=vf->imgctx.static_images[0];
314 break;
315 case MP_IMGTYPE_TEMP:
316 if(!vf->imgctx.temp_images[0]) vf->imgctx.temp_images[0]=new_mp_image(w2,h);
317 mpi=vf->imgctx.temp_images[0];
318 break;
319 case MP_IMGTYPE_IPB:
320 if(!(mp_imgflag&MP_IMGFLAG_READABLE)){ // B frame:
321 if(!vf->imgctx.temp_images[0]) vf->imgctx.temp_images[0]=new_mp_image(w2,h);
322 mpi=vf->imgctx.temp_images[0];
323 break;
325 case MP_IMGTYPE_IP:
326 if(!vf->imgctx.static_images[vf->imgctx.static_idx]) vf->imgctx.static_images[vf->imgctx.static_idx]=new_mp_image(w2,h);
327 mpi=vf->imgctx.static_images[vf->imgctx.static_idx];
328 vf->imgctx.static_idx^=1;
329 break;
330 case MP_IMGTYPE_NUMBERED:
331 if (number == -1) {
332 int i;
333 for (i = 0; i < NUM_NUMBERED_MPI; i++)
334 if (!vf->imgctx.numbered_images[i] || !vf->imgctx.numbered_images[i]->usage_count)
335 break;
336 number = i;
338 if (number < 0 || number >= NUM_NUMBERED_MPI) return NULL;
339 if (!vf->imgctx.numbered_images[number]) vf->imgctx.numbered_images[number] = new_mp_image(w2,h);
340 mpi = vf->imgctx.numbered_images[number];
341 mpi->number = number;
342 break;
344 if(mpi){
345 mpi->type=mp_imgtype;
346 mpi->w=vf->w; mpi->h=vf->h;
347 // keep buffer allocation status & color flags only:
348 // mpi->flags&=~(MP_IMGFLAG_PRESERVE|MP_IMGFLAG_READABLE|MP_IMGFLAG_DIRECT);
349 mpi->flags&=MP_IMGFLAG_ALLOCATED|MP_IMGFLAG_TYPE_DISPLAYED|MP_IMGFLAGMASK_COLORS;
350 // accept restrictions, draw_slice and palette flags only:
351 mpi->flags|=mp_imgflag&(MP_IMGFLAGMASK_RESTRICTIONS|MP_IMGFLAG_DRAW_CALLBACK|MP_IMGFLAG_RGB_PALETTE);
352 if(!vf->draw_slice) mpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
353 if(mpi->width!=w2 || mpi->height!=h){
354 // printf("vf.c: MPI parameters changed! %dx%d -> %dx%d \n", mpi->width,mpi->height,w2,h);
355 if(mpi->flags&MP_IMGFLAG_ALLOCATED){
356 if(mpi->width<w2 || mpi->height<h){
357 // need to re-allocate buffer memory:
358 free(mpi->planes[0]);
359 mpi->flags&=~MP_IMGFLAG_ALLOCATED;
360 mp_msg(MSGT_VFILTER,MSGL_V,"vf.c: have to REALLOCATE buffer memory :(\n");
362 // } else {
364 mpi->width=w2; mpi->chroma_width=(w2 + (1<<mpi->chroma_x_shift) - 1)>>mpi->chroma_x_shift;
365 mpi->height=h; mpi->chroma_height=(h + (1<<mpi->chroma_y_shift) - 1)>>mpi->chroma_y_shift;
368 if(!mpi->bpp) mp_image_setfmt(mpi,outfmt);
369 if(!(mpi->flags&MP_IMGFLAG_ALLOCATED) && mpi->type>MP_IMGTYPE_EXPORT){
371 // check libvo first!
372 if(vf->get_image) vf->get_image(vf,mpi);
374 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
375 // non-direct and not yet allocated image. allocate it!
376 if (!mpi->bpp) { // no way we can allocate this
377 mp_msg(MSGT_DECVIDEO, MSGL_FATAL,
378 "vf_get_image: Tried to allocate a format that can not be allocated!\n");
379 return NULL;
382 // check if codec prefer aligned stride:
383 if(mp_imgflag&MP_IMGFLAG_PREFER_ALIGNED_STRIDE){
384 int align=(mpi->flags&MP_IMGFLAG_PLANAR &&
385 mpi->flags&MP_IMGFLAG_YUV) ?
386 (8<<mpi->chroma_x_shift)-1 : 15; // -- maybe FIXME
387 w2=((w+align)&(~align));
388 if(mpi->width!=w2){
389 // we have to change width... check if we CAN co it:
390 int flags=vf->query_format(vf,outfmt); // should not fail
391 if(!(flags&3)) mp_msg(MSGT_DECVIDEO,MSGL_WARN,"??? vf_get_image{vf->query_format(outfmt)} failed!\n");
392 // printf("query -> 0x%X \n",flags);
393 if(flags&VFCAP_ACCEPT_STRIDE){
394 mpi->width=w2;
395 mpi->chroma_width=(w2 + (1<<mpi->chroma_x_shift) - 1)>>mpi->chroma_x_shift;
400 mp_image_alloc_planes(mpi);
401 // printf("clearing img!\n");
402 vf_mpi_clear(mpi,0,0,mpi->width,mpi->height);
405 if(mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)
406 if(vf->start_slice) vf->start_slice(vf,mpi);
407 if(!(mpi->flags&MP_IMGFLAG_TYPE_DISPLAYED)){
408 mp_msg(MSGT_DECVIDEO,MSGL_V,"*** [%s] %s%s mp_image_t, %dx%dx%dbpp %s %s, %d bytes\n",
409 vf->info->name,
410 (mpi->type==MP_IMGTYPE_EXPORT)?"Exporting":
411 ((mpi->flags&MP_IMGFLAG_DIRECT)?"Direct Rendering":"Allocating"),
412 (mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)?" (slices)":"",
413 mpi->width,mpi->height,mpi->bpp,
414 (mpi->flags&MP_IMGFLAG_YUV)?"YUV":((mpi->flags&MP_IMGFLAG_SWAPPED)?"BGR":"RGB"),
415 (mpi->flags&MP_IMGFLAG_PLANAR)?"planar":"packed",
416 mpi->bpp*mpi->width*mpi->height/8);
417 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",
418 mpi->imgfmt, mpi->planes[0], mpi->planes[1], mpi->planes[2],
419 mpi->stride[0], mpi->stride[1], mpi->stride[2],
420 mpi->chroma_width, mpi->chroma_height, mpi->chroma_x_shift, mpi->chroma_y_shift);
421 mpi->flags|=MP_IMGFLAG_TYPE_DISPLAYED;
424 mpi->qscale = NULL;
426 mpi->usage_count++;
427 // printf("\rVF_MPI: %p %p %p %d %d %d \n",
428 // mpi->planes[0],mpi->planes[1],mpi->planes[2],
429 // mpi->stride[0],mpi->stride[1],mpi->stride[2]);
430 return mpi;
433 //============================================================================
435 // By default vf doesn't accept MPEGPES
436 static int vf_default_query_format(struct vf_instance* vf, unsigned int fmt){
437 if(fmt == IMGFMT_MPEGPES) return 0;
438 return vf_next_query_format(vf,fmt);
441 struct vf_instance *vf_open_plugin_noerr(struct MPOpts *opts,
442 const vf_info_t * const *filter_list,
443 vf_instance_t *next, const char *name,
444 char **args, int *retcode)
446 vf_instance_t* vf;
447 int i;
448 for(i=0;;i++){
449 if(!filter_list[i]){
450 mp_tmsg(MSGT_VFILTER,MSGL_ERR,"Couldn't find video filter '%s'.\n",name);
451 return NULL; // no such filter!
453 if(!strcmp(filter_list[i]->name,name)) break;
455 vf = calloc(1, sizeof *vf);
456 vf->opts = opts;
457 vf->info=filter_list[i];
458 vf->next=next;
459 vf->config=vf_next_config;
460 vf->control=vf_next_control;
461 vf->query_format=vf_default_query_format;
462 vf->put_image=vf_next_put_image;
463 vf->default_caps=VFCAP_ACCEPT_STRIDE;
464 vf->default_reqs=0;
465 if(vf->info->opts) { // vf_vo get some special argument
466 const m_struct_t* st = vf->info->opts;
467 void* vf_priv = m_struct_alloc(st);
468 int n;
469 for(n = 0 ; args && args[2*n] ; n++)
470 m_struct_set(st,vf_priv,args[2*n],args[2*n+1]);
471 vf->priv = vf_priv;
472 args = NULL;
473 } else // Otherwise we should have the '_oldargs_'
474 if(args && !strcmp(args[0],"_oldargs_"))
475 args = (char**)args[1];
476 else
477 args = NULL;
478 *retcode = vf->info->vf_open(vf,(char*)args);
479 if (*retcode > 0)
480 return vf;
481 free(vf);
482 return NULL;
485 struct vf_instance *vf_open_plugin(struct MPOpts *opts,
486 const vf_info_t * const *filter_list,
487 vf_instance_t *next, const char *name,
488 char **args)
490 struct vf_instance *vf = vf_open_plugin_noerr(opts, filter_list, next,
491 name, args, &(int){0});
492 if (!vf)
493 mp_tmsg(MSGT_VFILTER, MSGL_ERR, "Couldn't open video filter '%s'.\n",
494 name);
495 return vf;
498 vf_instance_t* vf_open_filter(struct MPOpts *opts, vf_instance_t* next, const char *name, char **args){
499 if(args && strcmp(args[0],"_oldargs_")) {
500 int i,l = 0;
501 for(i = 0 ; args && args[2*i] ; i++)
502 l += 1 + strlen(args[2*i]) + 1 + strlen(args[2*i+1]);
503 l += strlen(name);
505 char str[l+1];
506 char* p = str;
507 p += sprintf(str,"%s",name);
508 for(i = 0 ; args && args[2*i] ; i++)
509 p += sprintf(p," %s=%s",args[2*i],args[2*i+1]);
510 mp_msg(MSGT_VFILTER, MSGL_INFO, "%s[%s]\n",
511 mp_gtext("Opening video filter: "), str);
513 } else if(strcmp(name,"vo")) {
514 if(args && strcmp(args[0],"_oldargs_") == 0)
515 mp_msg(MSGT_VFILTER, MSGL_INFO, "%s[%s=%s]\n",
516 mp_gtext("Opening video filter: "), name, args[1]);
517 else
518 mp_msg(MSGT_VFILTER, MSGL_INFO, "%s[%s]\n",
519 mp_gtext("Opening video filter: "), name);
521 return vf_open_plugin(opts, filter_list,next,name,args);
525 * \brief adds a filter before the last one (which should be the vo filter).
526 * \param vf start of the filter chain.
527 * \param name name of the filter to add.
528 * \param args argument list for the filter.
529 * \return pointer to the filter instance that was created.
531 vf_instance_t* vf_add_before_vo(vf_instance_t **vf, char *name, char **args) {
532 struct MPOpts *opts = (*vf)->opts;
533 vf_instance_t *vo, *prev = NULL, *new;
534 // Find the last filter (should be vf_vo)
535 for (vo = *vf; vo->next; vo = vo->next)
536 prev = vo;
537 new = vf_open_filter(opts, vo, name, args);
538 if (prev)
539 prev->next = new;
540 else
541 *vf = new;
542 return new;
545 //============================================================================
547 unsigned int vf_match_csp(vf_instance_t** vfp,const unsigned int* list,unsigned int preferred){
548 vf_instance_t* vf=*vfp;
549 struct MPOpts *opts = vf->opts;
550 const unsigned int* p;
551 unsigned int best=0;
552 int ret;
553 if((p=list)) while(*p){
554 ret=vf->query_format(vf,*p);
555 mp_msg(MSGT_VFILTER,MSGL_V,"[%s] query(%s) -> %d\n",vf->info->name,vo_format_name(*p),ret&3);
556 if(ret&2){ best=*p; break;} // no conversion -> bingo!
557 if(ret&1 && !best) best=*p; // best with conversion
558 ++p;
560 if(best) return best; // bingo, they have common csp!
561 // ok, then try with scale:
562 if(vf->info == &vf_info_scale) return 0; // avoid infinite recursion!
563 vf=vf_open_filter(opts, vf,"scale",NULL);
564 if(!vf) return 0; // failed to init "scale"
565 // try the preferred csp first:
566 if(preferred && vf->query_format(vf,preferred)) best=preferred; else
567 // try the list again, now with "scaler" :
568 if((p=list)) while(*p){
569 ret=vf->query_format(vf,*p);
570 mp_msg(MSGT_VFILTER,MSGL_V,"[%s] query(%s) -> %d\n",vf->info->name,vo_format_name(*p),ret&3);
571 if(ret&2){ best=*p; break;} // no conversion -> bingo!
572 if(ret&1 && !best) best=*p; // best with conversion
573 ++p;
575 if(best) *vfp=vf; // else uninit vf !FIXME!
576 return best;
579 void vf_clone_mpi_attributes(mp_image_t* dst, mp_image_t* src){
580 dst->pict_type= src->pict_type;
581 dst->fields = src->fields;
582 dst->qscale_type= src->qscale_type;
583 if(dst->width == src->width && dst->height == src->height){
584 dst->qstride= src->qstride;
585 dst->qscale= src->qscale;
589 void vf_queue_frame(vf_instance_t *vf, int (*func)(vf_instance_t *))
591 vf->continue_buffered_image = func;
594 // Output the next buffered image (if any) from the filter chain.
595 // The queue could be kept as a simple stack/list instead avoiding the
596 // looping here, but there's currently no good context variable where
597 // that could be stored so this was easier to implement.
599 int vf_output_queued_frame(vf_instance_t *vf)
601 while (1) {
602 int ret;
603 vf_instance_t *current;
604 vf_instance_t *last=NULL;
605 int (*tmp)(vf_instance_t *);
606 for (current = vf; current; current = current->next)
607 if (current->continue_buffered_image)
608 last = current;
609 if (!last)
610 return 0;
611 tmp = last->continue_buffered_image;
612 last->continue_buffered_image = NULL;
613 ret = tmp(last);
614 if (ret)
615 return ret;
621 * \brief Video config() function wrapper
623 * Blocks config() calls with different size or format for filters
624 * with VFCAP_CONSTANT
626 * First call is redirected to vf->config.
628 * In following calls, it verifies that the configuration parameters
629 * are unchanged, and returns either success or error.
632 int vf_config_wrapper(struct vf_instance* vf,
633 int width, int height, int d_width, int d_height,
634 unsigned int flags, unsigned int outfmt)
636 int r;
637 if ((vf->default_caps&VFCAP_CONSTANT) && vf->fmt.have_configured) {
638 if ((vf->fmt.orig_width != width)
639 || (vf->fmt.orig_height != height)
640 || (vf->fmt.orig_fmt != outfmt)) {
641 mp_tmsg(MSGT_VFILTER,MSGL_ERR,"\nNew video file has different resolution or colorspace than the previous one.\n");
642 return 0;
644 return 1;
646 vf->fmt.have_configured = 1;
647 vf->fmt.orig_height = height;
648 vf->fmt.orig_width = width;
649 vf->fmt.orig_fmt = outfmt;
650 r = vf->config(vf, width, height, d_width, d_height, flags, outfmt);
651 if (!r) vf->fmt.have_configured = 0;
652 return r;
655 int vf_next_config(struct vf_instance* vf,
656 int width, int height, int d_width, int d_height,
657 unsigned int voflags, unsigned int outfmt){
658 struct MPOpts *opts = vf->opts;
659 int miss;
660 int flags=vf->next->query_format(vf->next,outfmt);
661 if(!flags){
662 // hmm. colorspace mismatch!!!
663 // let's insert the 'scale' filter, it does the job for us:
664 vf_instance_t* vf2;
665 if(vf->next->info==&vf_info_scale) return 0; // scale->scale
666 vf2=vf_open_filter(opts, vf->next,"scale",NULL);
667 if(!vf2) return 0; // shouldn't happen!
668 vf->next=vf2;
669 flags=vf->next->query_format(vf->next,outfmt);
670 if(!flags){
671 mp_tmsg(MSGT_VFILTER,MSGL_ERR,"Cannot find matching colorspace, even by inserting 'scale' :(\n");
672 return 0; // FAIL
675 mp_msg(MSGT_VFILTER,MSGL_V,"REQ: flags=0x%X req=0x%X \n",flags,vf->default_reqs);
676 miss=vf->default_reqs - (flags&vf->default_reqs);
677 if(miss&VFCAP_ACCEPT_STRIDE){
678 // vf requires stride support but vf->next doesn't support it!
679 // let's insert the 'expand' filter, it does the job for us:
680 vf_instance_t* vf2=vf_open_filter(opts, vf->next,"expand",NULL);
681 if(!vf2) return 0; // shouldn't happen!
682 vf->next=vf2;
684 vf->next->w = width; vf->next->h = height;
685 return vf_config_wrapper(vf->next,width,height,d_width,d_height,voflags,outfmt);
688 int vf_next_control(struct vf_instance* vf, int request, void* data){
689 return vf->next->control(vf->next,request,data);
692 int vf_next_query_format(struct vf_instance* 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* 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* 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 struct m_obj_settings *vf_settings)
729 struct MPOpts *opts = last->opts;
730 vf_instance_t* vf;
731 int i;
733 if(vf_settings) {
734 // We want to add them in the 'right order'
735 for(i = 0 ; vf_settings[i].name ; i++)
736 /* NOP */;
737 for(i-- ; i >= 0 ; i--) {
738 //printf("Open filter %s\n",vf_settings[i].name);
739 vf = vf_open_filter(opts, last,vf_settings[i].name,vf_settings[i].attribs);
740 if(vf) last=vf;
743 return last;
746 //============================================================================
748 void vf_uninit_filter(vf_instance_t* vf){
749 if(vf->uninit) vf->uninit(vf);
750 free_mp_image(vf->imgctx.static_images[0]);
751 free_mp_image(vf->imgctx.static_images[1]);
752 free_mp_image(vf->imgctx.temp_images[0]);
753 free_mp_image(vf->imgctx.export_images[0]);
754 for (int i = 0; i < NUM_NUMBERED_MPI; i++)
755 free_mp_image(vf->imgctx.numbered_images[i]);
756 free(vf);
759 void vf_uninit_filter_chain(vf_instance_t* vf){
760 while(vf){
761 vf_instance_t* next=vf->next;
762 vf_uninit_filter(vf);
763 vf=next;