sync with en/mplayer.1 rev. 30611
[mplayer/glamo.git] / libmpcodecs / vf.c
blobe6611efeb8042d1f6102312795992c4a21fa9b19
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 &vf_info_yuy2,
139 &vf_info_flip,
140 &vf_info_rgb2bgr,
141 &vf_info_rotate,
142 &vf_info_mirror,
143 &vf_info_palette,
144 &vf_info_pp7,
145 #ifdef CONFIG_LIBAVCODEC
146 &vf_info_lavc,
147 &vf_info_lavcdeint,
148 &vf_info_screenshot,
149 #endif
150 #ifdef CONFIG_ZR
151 &vf_info_zrmjpeg,
152 #endif
153 &vf_info_dvbscale,
154 &vf_info_cropdetect,
155 &vf_info_test,
156 &vf_info_noise,
157 &vf_info_yvu9,
158 &vf_info_eq,
159 &vf_info_eq2,
160 &vf_info_gradfun,
161 &vf_info_halfpack,
162 &vf_info_dint,
163 &vf_info_1bpp,
164 &vf_info_2xsai,
165 &vf_info_unsharp,
166 &vf_info_swapuv,
167 &vf_info_il,
168 &vf_info_fil,
169 &vf_info_boxblur,
170 &vf_info_sab,
171 &vf_info_smartblur,
172 &vf_info_perspective,
173 &vf_info_down3dright,
174 &vf_info_field,
175 &vf_info_denoise3d,
176 &vf_info_hqdn3d,
177 &vf_info_detc,
178 &vf_info_telecine,
179 &vf_info_tinterlace,
180 &vf_info_tfields,
181 &vf_info_ivtc,
182 &vf_info_ilpack,
183 &vf_info_dsize,
184 &vf_info_decimate,
185 &vf_info_softpulldown,
186 &vf_info_pullup,
187 &vf_info_filmdint,
188 &vf_info_framestep,
189 &vf_info_tile,
190 &vf_info_delogo,
191 &vf_info_remove_logo,
192 &vf_info_hue,
193 #ifdef CONFIG_LIBAVCODEC_A
194 &vf_info_spp,
195 &vf_info_uspp,
196 &vf_info_fspp,
197 &vf_info_qp,
198 &vf_info_mcdeint,
199 &vf_info_geq,
200 #endif
201 &vf_info_yuvcsp,
202 &vf_info_kerndeint,
203 &vf_info_rgbtest,
204 &vf_info_phase,
205 &vf_info_divtc,
206 &vf_info_harddup,
207 &vf_info_softskip,
208 #ifdef CONFIG_ASS
209 &vf_info_ass,
210 #endif
211 &vf_info_yadif,
212 &vf_info_blackframe,
213 &vf_info_ow,
214 NULL
217 // For the vf option
218 m_obj_settings_t* vf_settings = NULL;
219 const m_obj_list_t vf_obj_list = {
220 (void**)filter_list,
221 M_ST_OFF(vf_info_t,name),
222 M_ST_OFF(vf_info_t,info),
223 M_ST_OFF(vf_info_t,opts)
226 //============================================================================
227 // mpi stuff:
229 void vf_mpi_clear(mp_image_t* mpi,int x0,int y0,int w,int h){
230 int y;
231 if(mpi->flags&MP_IMGFLAG_PLANAR){
232 y0&=~1;h+=h&1;
233 if(x0==0 && w==mpi->width){
234 // full width clear:
235 memset(mpi->planes[0]+mpi->stride[0]*y0,0,mpi->stride[0]*h);
236 memset(mpi->planes[1]+mpi->stride[1]*(y0>>mpi->chroma_y_shift),128,mpi->stride[1]*(h>>mpi->chroma_y_shift));
237 memset(mpi->planes[2]+mpi->stride[2]*(y0>>mpi->chroma_y_shift),128,mpi->stride[2]*(h>>mpi->chroma_y_shift));
238 } else
239 for(y=y0;y<y0+h;y+=2){
240 memset(mpi->planes[0]+x0+mpi->stride[0]*y,0,w);
241 memset(mpi->planes[0]+x0+mpi->stride[0]*(y+1),0,w);
242 memset(mpi->planes[1]+(x0>>mpi->chroma_x_shift)+mpi->stride[1]*(y>>mpi->chroma_y_shift),128,(w>>mpi->chroma_x_shift));
243 memset(mpi->planes[2]+(x0>>mpi->chroma_x_shift)+mpi->stride[2]*(y>>mpi->chroma_y_shift),128,(w>>mpi->chroma_x_shift));
245 return;
247 // packed:
248 for(y=y0;y<y0+h;y++){
249 unsigned char* dst=mpi->planes[0]+mpi->stride[0]*y+(mpi->bpp>>3)*x0;
250 if(mpi->flags&MP_IMGFLAG_YUV){
251 unsigned int* p=(unsigned int*) dst;
252 int size=(mpi->bpp>>3)*w/4;
253 int i;
254 #if HAVE_BIGENDIAN
255 #define CLEAR_PACKEDYUV_PATTERN 0x00800080
256 #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x80008000
257 #else
258 #define CLEAR_PACKEDYUV_PATTERN 0x80008000
259 #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x00800080
260 #endif
261 if(mpi->flags&MP_IMGFLAG_SWAPPED){
262 for(i=0;i<size-3;i+=4) p[i]=p[i+1]=p[i+2]=p[i+3]=CLEAR_PACKEDYUV_PATTERN_SWAPPED;
263 for(;i<size;i++) p[i]=CLEAR_PACKEDYUV_PATTERN_SWAPPED;
264 } else {
265 for(i=0;i<size-3;i+=4) p[i]=p[i+1]=p[i+2]=p[i+3]=CLEAR_PACKEDYUV_PATTERN;
266 for(;i<size;i++) p[i]=CLEAR_PACKEDYUV_PATTERN;
268 } else
269 memset(dst,0,(mpi->bpp>>3)*w);
273 mp_image_t* vf_get_image(vf_instance_t* vf, unsigned int outfmt, int mp_imgtype, int mp_imgflag, int w, int h){
274 mp_image_t* mpi=NULL;
275 int w2;
276 int number = mp_imgtype >> 16;
278 #ifdef MP_DEBUG
279 assert(w == -1 || w >= vf->w);
280 assert(h == -1 || h >= vf->h);
281 assert(vf->w > 0);
282 assert(vf->h > 0);
283 #endif
285 // fprintf(stderr, "get_image: %d:%d, vf: %d:%d\n", w,h,vf->w,vf->h);
287 if (w == -1) w = vf->w;
288 if (h == -1) h = vf->h;
290 w2=(mp_imgflag&MP_IMGFLAG_ACCEPT_ALIGNED_STRIDE)?((w+15)&(~15)):w;
292 if(vf->put_image==vf_next_put_image){
293 // passthru mode, if the filter uses the fallback/default put_image() code
294 return vf_get_image(vf->next,outfmt,mp_imgtype,mp_imgflag,w,h);
297 // Note: we should call libvo first to check if it supports direct rendering
298 // and if not, then fallback to software buffers:
299 switch(mp_imgtype & 0xff){
300 case MP_IMGTYPE_EXPORT:
301 if(!vf->imgctx.export_images[0]) vf->imgctx.export_images[0]=new_mp_image(w2,h);
302 mpi=vf->imgctx.export_images[0];
303 break;
304 case MP_IMGTYPE_STATIC:
305 if(!vf->imgctx.static_images[0]) vf->imgctx.static_images[0]=new_mp_image(w2,h);
306 mpi=vf->imgctx.static_images[0];
307 break;
308 case MP_IMGTYPE_TEMP:
309 if(!vf->imgctx.temp_images[0]) vf->imgctx.temp_images[0]=new_mp_image(w2,h);
310 mpi=vf->imgctx.temp_images[0];
311 break;
312 case MP_IMGTYPE_IPB:
313 if(!(mp_imgflag&MP_IMGFLAG_READABLE)){ // B frame:
314 if(!vf->imgctx.temp_images[0]) vf->imgctx.temp_images[0]=new_mp_image(w2,h);
315 mpi=vf->imgctx.temp_images[0];
316 break;
318 case MP_IMGTYPE_IP:
319 if(!vf->imgctx.static_images[vf->imgctx.static_idx]) vf->imgctx.static_images[vf->imgctx.static_idx]=new_mp_image(w2,h);
320 mpi=vf->imgctx.static_images[vf->imgctx.static_idx];
321 vf->imgctx.static_idx^=1;
322 break;
323 case MP_IMGTYPE_NUMBERED:
324 if (number == -1) {
325 int i;
326 for (i = 0; i < NUM_NUMBERED_MPI; i++)
327 if (!vf->imgctx.numbered_images[i] || !vf->imgctx.numbered_images[i]->usage_count)
328 break;
329 number = i;
331 if (number < 0 || number >= NUM_NUMBERED_MPI) return NULL;
332 if (!vf->imgctx.numbered_images[number]) vf->imgctx.numbered_images[number] = new_mp_image(w2,h);
333 mpi = vf->imgctx.numbered_images[number];
334 mpi->number = number;
335 break;
337 if(mpi){
338 mpi->type=mp_imgtype;
339 mpi->w=vf->w; mpi->h=vf->h;
340 // keep buffer allocation status & color flags only:
341 // mpi->flags&=~(MP_IMGFLAG_PRESERVE|MP_IMGFLAG_READABLE|MP_IMGFLAG_DIRECT);
342 mpi->flags&=MP_IMGFLAG_ALLOCATED|MP_IMGFLAG_TYPE_DISPLAYED|MP_IMGFLAGMASK_COLORS;
343 // accept restrictions, draw_slice and palette flags only:
344 mpi->flags|=mp_imgflag&(MP_IMGFLAGMASK_RESTRICTIONS|MP_IMGFLAG_DRAW_CALLBACK|MP_IMGFLAG_RGB_PALETTE);
345 if(!vf->draw_slice) mpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
346 if(mpi->width!=w2 || mpi->height!=h){
347 // printf("vf.c: MPI parameters changed! %dx%d -> %dx%d \n", mpi->width,mpi->height,w2,h);
348 if(mpi->flags&MP_IMGFLAG_ALLOCATED){
349 if(mpi->width<w2 || mpi->height<h){
350 // need to re-allocate buffer memory:
351 free(mpi->planes[0]);
352 mpi->flags&=~MP_IMGFLAG_ALLOCATED;
353 mp_msg(MSGT_VFILTER,MSGL_V,"vf.c: have to REALLOCATE buffer memory :(\n");
355 // } else {
357 mpi->width=w2; mpi->chroma_width=(w2 + (1<<mpi->chroma_x_shift) - 1)>>mpi->chroma_x_shift;
358 mpi->height=h; mpi->chroma_height=(h + (1<<mpi->chroma_y_shift) - 1)>>mpi->chroma_y_shift;
361 if(!mpi->bpp) mp_image_setfmt(mpi,outfmt);
362 if(!(mpi->flags&MP_IMGFLAG_ALLOCATED) && mpi->type>MP_IMGTYPE_EXPORT){
364 // check libvo first!
365 if(vf->get_image) vf->get_image(vf,mpi);
367 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
368 // non-direct and not yet allocated image. allocate it!
369 if (!mpi->bpp) { // no way we can allocate this
370 mp_msg(MSGT_DECVIDEO, MSGL_FATAL,
371 "vf_get_image: Tried to allocate a format that can not be allocated!\n");
372 return NULL;
375 // check if codec prefer aligned stride:
376 if(mp_imgflag&MP_IMGFLAG_PREFER_ALIGNED_STRIDE){
377 int align=(mpi->flags&MP_IMGFLAG_PLANAR &&
378 mpi->flags&MP_IMGFLAG_YUV) ?
379 (8<<mpi->chroma_x_shift)-1 : 15; // -- maybe FIXME
380 w2=((w+align)&(~align));
381 if(mpi->width!=w2){
382 // we have to change width... check if we CAN co it:
383 int flags=vf->query_format(vf,outfmt); // should not fail
384 if(!(flags&3)) mp_msg(MSGT_DECVIDEO,MSGL_WARN,"??? vf_get_image{vf->query_format(outfmt)} failed!\n");
385 // printf("query -> 0x%X \n",flags);
386 if(flags&VFCAP_ACCEPT_STRIDE){
387 mpi->width=w2;
388 mpi->chroma_width=(w2 + (1<<mpi->chroma_x_shift) - 1)>>mpi->chroma_x_shift;
393 mp_image_alloc_planes(mpi);
394 // printf("clearing img!\n");
395 vf_mpi_clear(mpi,0,0,mpi->width,mpi->height);
398 if(mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)
399 if(vf->start_slice) vf->start_slice(vf,mpi);
400 if(!(mpi->flags&MP_IMGFLAG_TYPE_DISPLAYED)){
401 mp_msg(MSGT_DECVIDEO,MSGL_V,"*** [%s] %s%s mp_image_t, %dx%dx%dbpp %s %s, %d bytes\n",
402 vf->info->name,
403 (mpi->type==MP_IMGTYPE_EXPORT)?"Exporting":
404 ((mpi->flags&MP_IMGFLAG_DIRECT)?"Direct Rendering":"Allocating"),
405 (mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)?" (slices)":"",
406 mpi->width,mpi->height,mpi->bpp,
407 (mpi->flags&MP_IMGFLAG_YUV)?"YUV":((mpi->flags&MP_IMGFLAG_SWAPPED)?"BGR":"RGB"),
408 (mpi->flags&MP_IMGFLAG_PLANAR)?"planar":"packed",
409 mpi->bpp*mpi->width*mpi->height/8);
410 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",
411 mpi->imgfmt, mpi->planes[0], mpi->planes[1], mpi->planes[2],
412 mpi->stride[0], mpi->stride[1], mpi->stride[2],
413 mpi->chroma_width, mpi->chroma_height, mpi->chroma_x_shift, mpi->chroma_y_shift);
414 mpi->flags|=MP_IMGFLAG_TYPE_DISPLAYED;
417 mpi->qscale = NULL;
419 mpi->usage_count++;
420 // printf("\rVF_MPI: %p %p %p %d %d %d \n",
421 // mpi->planes[0],mpi->planes[1],mpi->planes[2],
422 // mpi->stride[0],mpi->stride[1],mpi->stride[2]);
423 return mpi;
426 //============================================================================
428 // By default vf doesn't accept MPEGPES
429 static int vf_default_query_format(struct vf_instance_s* vf, unsigned int fmt){
430 if(fmt == IMGFMT_MPEGPES) return 0;
431 return vf_next_query_format(vf,fmt);
434 vf_instance_t* vf_open_plugin(const vf_info_t* const* filter_list, vf_instance_t* next, const char *name, char **args){
435 vf_instance_t* vf;
436 int i;
437 for(i=0;;i++){
438 if(!filter_list[i]){
439 mp_msg(MSGT_VFILTER,MSGL_ERR,MSGTR_CouldNotFindVideoFilter,name);
440 return NULL; // no such filter!
442 if(!strcmp(filter_list[i]->name,name)) break;
444 vf=malloc(sizeof(vf_instance_t));
445 memset(vf,0,sizeof(vf_instance_t));
446 vf->info=filter_list[i];
447 vf->next=next;
448 vf->config=vf_next_config;
449 vf->control=vf_next_control;
450 vf->query_format=vf_default_query_format;
451 vf->put_image=vf_next_put_image;
452 vf->default_caps=VFCAP_ACCEPT_STRIDE;
453 vf->default_reqs=0;
454 if(vf->info->opts) { // vf_vo get some special argument
455 const m_struct_t* st = vf->info->opts;
456 void* vf_priv = m_struct_alloc(st);
457 int n;
458 for(n = 0 ; args && args[2*n] ; n++)
459 m_struct_set(st,vf_priv,args[2*n],args[2*n+1]);
460 vf->priv = vf_priv;
461 args = NULL;
462 } else // Otherwise we should have the '_oldargs_'
463 if(args && !strcmp(args[0],"_oldargs_"))
464 args = (char**)args[1];
465 else
466 args = NULL;
467 if(vf->info->open(vf,(char*)args)>0) return vf; // Success!
468 free(vf);
469 mp_msg(MSGT_VFILTER,MSGL_ERR,MSGTR_CouldNotOpenVideoFilter,name);
470 return NULL;
473 vf_instance_t* vf_open_filter(vf_instance_t* next, const char *name, char **args){
474 if(args && strcmp(args[0],"_oldargs_")) {
475 int i,l = 0;
476 for(i = 0 ; args && args[2*i] ; i++)
477 l += 1 + strlen(args[2*i]) + 1 + strlen(args[2*i+1]);
478 l += strlen(name);
480 char str[l+1];
481 char* p = str;
482 p += sprintf(str,"%s",name);
483 for(i = 0 ; args && args[2*i] ; i++)
484 p += sprintf(p," %s=%s",args[2*i],args[2*i+1]);
485 mp_msg(MSGT_VFILTER,MSGL_INFO,MSGTR_OpeningVideoFilter "[%s]\n",str);
487 } else if(strcmp(name,"vo")) {
488 if(args && strcmp(args[0],"_oldargs_") == 0)
489 mp_msg(MSGT_VFILTER,MSGL_INFO,MSGTR_OpeningVideoFilter
490 "[%s=%s]\n", name,args[1]);
491 else
492 mp_msg(MSGT_VFILTER,MSGL_INFO,MSGTR_OpeningVideoFilter
493 "[%s]\n", name);
495 return vf_open_plugin(filter_list,next,name,args);
499 * \brief adds a filter before the last one (which should be the vo filter).
500 * \param vf start of the filter chain.
501 * \param name name of the filter to add.
502 * \param args argument list for the filter.
503 * \return pointer to the filter instance that was created.
505 vf_instance_t* vf_add_before_vo(vf_instance_t **vf, char *name, char **args) {
506 vf_instance_t *vo, *prev = NULL, *new;
507 // Find the last filter (should be vf_vo)
508 for (vo = *vf; vo->next; vo = vo->next)
509 prev = vo;
510 new = vf_open_filter(vo, name, args);
511 if (prev)
512 prev->next = new;
513 else
514 *vf = new;
515 return new;
518 //============================================================================
520 unsigned int vf_match_csp(vf_instance_t** vfp,const unsigned int* list,unsigned int preferred){
521 vf_instance_t* vf=*vfp;
522 const unsigned int* p;
523 unsigned int best=0;
524 int ret;
525 if((p=list)) while(*p){
526 ret=vf->query_format(vf,*p);
527 mp_msg(MSGT_VFILTER,MSGL_V,"[%s] query(%s) -> %d\n",vf->info->name,vo_format_name(*p),ret&3);
528 if(ret&2){ best=*p; break;} // no conversion -> bingo!
529 if(ret&1 && !best) best=*p; // best with conversion
530 ++p;
532 if(best) return best; // bingo, they have common csp!
533 // ok, then try with scale:
534 if(vf->info == &vf_info_scale) return 0; // avoid infinite recursion!
535 vf=vf_open_filter(vf,"scale",NULL);
536 if(!vf) return 0; // failed to init "scale"
537 // try the preferred csp first:
538 if(preferred && vf->query_format(vf,preferred)) best=preferred; else
539 // try the list again, now with "scaler" :
540 if((p=list)) while(*p){
541 ret=vf->query_format(vf,*p);
542 mp_msg(MSGT_VFILTER,MSGL_V,"[%s] query(%s) -> %d\n",vf->info->name,vo_format_name(*p),ret&3);
543 if(ret&2){ best=*p; break;} // no conversion -> bingo!
544 if(ret&1 && !best) best=*p; // best with conversion
545 ++p;
547 if(best) *vfp=vf; // else uninit vf !FIXME!
548 return best;
551 void vf_clone_mpi_attributes(mp_image_t* dst, mp_image_t* src){
552 dst->pict_type= src->pict_type;
553 dst->fields = src->fields;
554 dst->qscale_type= src->qscale_type;
555 if(dst->width == src->width && dst->height == src->height){
556 dst->qstride= src->qstride;
557 dst->qscale= src->qscale;
561 void vf_queue_frame(vf_instance_t *vf, int (*func)(vf_instance_t *))
563 vf->continue_buffered_image = func;
566 // Output the next buffered image (if any) from the filter chain.
567 // The queue could be kept as a simple stack/list instead avoiding the
568 // looping here, but there's currently no good context variable where
569 // that could be stored so this was easier to implement.
571 int vf_output_queued_frame(vf_instance_t *vf)
573 while (1) {
574 int ret;
575 vf_instance_t *current;
576 vf_instance_t *last=NULL;
577 int (*tmp)(vf_instance_t *);
578 for (current = vf; current; current = current->next)
579 if (current->continue_buffered_image)
580 last = current;
581 if (!last)
582 return 0;
583 tmp = last->continue_buffered_image;
584 last->continue_buffered_image = NULL;
585 ret = tmp(last);
586 if (ret > 0) {
587 vf->control(vf, VFCTRL_DRAW_OSD, NULL);
588 #ifdef CONFIG_ASS
589 vf->control(vf, VFCTRL_DRAW_EOSD, NULL);
590 #endif
592 if (ret)
593 return ret;
599 * \brief Video config() function wrapper
601 * Blocks config() calls with different size or format for filters
602 * with VFCAP_CONSTANT
604 * First call is redirected to vf->config.
606 * In following calls, it verifies that the configuration parameters
607 * are unchanged, and returns either success or error.
610 int vf_config_wrapper(struct vf_instance_s* vf,
611 int width, int height, int d_width, int d_height,
612 unsigned int flags, unsigned int outfmt)
614 int r;
615 if ((vf->default_caps&VFCAP_CONSTANT) && vf->fmt.have_configured) {
616 if ((vf->fmt.orig_width != width)
617 || (vf->fmt.orig_height != height)
618 || (vf->fmt.orig_fmt != outfmt)) {
619 mp_msg(MSGT_VFILTER,MSGL_ERR,MSGTR_ResolutionDoesntMatch);
620 return 0;
622 return 1;
624 vf->fmt.have_configured = 1;
625 vf->fmt.orig_height = height;
626 vf->fmt.orig_width = width;
627 vf->fmt.orig_fmt = outfmt;
628 r = vf->config(vf, width, height, d_width, d_height, flags, outfmt);
629 if (!r) vf->fmt.have_configured = 0;
630 return r;
633 int vf_next_config(struct vf_instance_s* vf,
634 int width, int height, int d_width, int d_height,
635 unsigned int voflags, unsigned int outfmt){
636 int miss;
637 int flags=vf->next->query_format(vf->next,outfmt);
638 if(!flags){
639 // hmm. colorspace mismatch!!!
640 // let's insert the 'scale' filter, it does the job for us:
641 vf_instance_t* vf2;
642 if(vf->next->info==&vf_info_scale) return 0; // scale->scale
643 vf2=vf_open_filter(vf->next,"scale",NULL);
644 if(!vf2) return 0; // shouldn't happen!
645 vf->next=vf2;
646 flags=vf->next->query_format(vf->next,outfmt);
647 if(!flags){
648 mp_msg(MSGT_VFILTER,MSGL_ERR,MSGTR_CannotFindColorspace);
649 return 0; // FAIL
652 mp_msg(MSGT_VFILTER,MSGL_V,"REQ: flags=0x%X req=0x%X \n",flags,vf->default_reqs);
653 miss=vf->default_reqs - (flags&vf->default_reqs);
654 if(miss&VFCAP_ACCEPT_STRIDE){
655 // vf requires stride support but vf->next doesn't support it!
656 // let's insert the 'expand' filter, it does the job for us:
657 vf_instance_t* vf2=vf_open_filter(vf->next,"expand",NULL);
658 if(!vf2) return 0; // shouldn't happen!
659 vf->next=vf2;
661 vf->next->w = width; vf->next->h = height;
662 return vf_config_wrapper(vf->next,width,height,d_width,d_height,voflags,outfmt);
665 int vf_next_control(struct vf_instance_s* vf, int request, void* data){
666 return vf->next->control(vf->next,request,data);
669 void vf_extra_flip(struct vf_instance_s* vf) {
670 vf_next_control(vf, VFCTRL_DRAW_OSD, NULL);
671 #ifdef CONFIG_ASS
672 vf_next_control(vf, VFCTRL_DRAW_EOSD, NULL);
673 #endif
674 vf_next_control(vf, VFCTRL_FLIP_PAGE, NULL);
677 int vf_next_query_format(struct vf_instance_s* vf, unsigned int fmt){
678 int flags=vf->next->query_format(vf->next,fmt);
679 if(flags) flags|=vf->default_caps;
680 return flags;
683 int vf_next_put_image(struct vf_instance_s* vf,mp_image_t *mpi, double pts){
684 return vf->next->put_image(vf->next,mpi, pts);
687 void vf_next_draw_slice(struct vf_instance_s* vf,unsigned char** src, int * stride,int w, int h, int x, int y){
688 if (vf->next->draw_slice) {
689 vf->next->draw_slice(vf->next,src,stride,w,h,x,y);
690 return;
692 if (!vf->dmpi) {
693 mp_msg(MSGT_VFILTER,MSGL_ERR,"draw_slice: dmpi not stored by vf_%s\n", vf->info->name);
694 return;
696 if (!(vf->dmpi->flags & MP_IMGFLAG_PLANAR)) {
697 memcpy_pic(vf->dmpi->planes[0]+y*vf->dmpi->stride[0]+vf->dmpi->bpp/8*x,
698 src[0], vf->dmpi->bpp/8*w, h, vf->dmpi->stride[0], stride[0]);
699 return;
701 memcpy_pic(vf->dmpi->planes[0]+y*vf->dmpi->stride[0]+x, src[0],
702 w, h, vf->dmpi->stride[0], stride[0]);
703 memcpy_pic(vf->dmpi->planes[1]+(y>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[1]+(x>>vf->dmpi->chroma_x_shift),
704 src[1], w>>vf->dmpi->chroma_x_shift, h>>vf->dmpi->chroma_y_shift, vf->dmpi->stride[1], stride[1]);
705 memcpy_pic(vf->dmpi->planes[2]+(y>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[2]+(x>>vf->dmpi->chroma_x_shift),
706 src[2], w>>vf->dmpi->chroma_x_shift, h>>vf->dmpi->chroma_y_shift, vf->dmpi->stride[2], stride[2]);
709 //============================================================================
711 vf_instance_t* append_filters(vf_instance_t* last){
712 vf_instance_t* vf;
713 int i;
715 if(vf_settings) {
716 // We want to add them in the 'right order'
717 for(i = 0 ; vf_settings[i].name ; i++)
718 /* NOP */;
719 for(i-- ; i >= 0 ; i--) {
720 //printf("Open filter %s\n",vf_settings[i].name);
721 vf = vf_open_filter(last,vf_settings[i].name,vf_settings[i].attribs);
722 if(vf) last=vf;
725 return last;
728 //============================================================================
730 void vf_uninit_filter(vf_instance_t* vf){
731 if(vf->uninit) vf->uninit(vf);
732 free_mp_image(vf->imgctx.static_images[0]);
733 free_mp_image(vf->imgctx.static_images[1]);
734 free_mp_image(vf->imgctx.temp_images[0]);
735 free_mp_image(vf->imgctx.export_images[0]);
736 free(vf);
739 void vf_uninit_filter_chain(vf_instance_t* vf){
740 while(vf){
741 vf_instance_t* next=vf->next;
742 vf_uninit_filter(vf);
743 vf=next;