20% faster hqdn3d on x86_64
[mplayer/glamo.git] / libmpcodecs / vf.c
blob08d0b130c1512b9108ebd14b70e498d5792ff262
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 &vf_info_yuy2,
121 &vf_info_flip,
122 &vf_info_rgb2bgr,
123 &vf_info_rotate,
124 &vf_info_mirror,
125 &vf_info_palette,
126 &vf_info_pp7,
127 #ifdef CONFIG_LIBAVCODEC
128 &vf_info_lavc,
129 &vf_info_lavcdeint,
130 &vf_info_screenshot,
131 #endif
132 #ifdef CONFIG_ZR
133 &vf_info_zrmjpeg,
134 #endif
135 &vf_info_dvbscale,
136 &vf_info_cropdetect,
137 &vf_info_test,
138 &vf_info_noise,
139 &vf_info_yvu9,
140 &vf_info_eq,
141 &vf_info_eq2,
142 &vf_info_gradfun,
143 &vf_info_halfpack,
144 &vf_info_dint,
145 &vf_info_1bpp,
146 &vf_info_2xsai,
147 &vf_info_unsharp,
148 &vf_info_swapuv,
149 &vf_info_il,
150 &vf_info_fil,
151 &vf_info_boxblur,
152 &vf_info_sab,
153 &vf_info_smartblur,
154 &vf_info_perspective,
155 &vf_info_down3dright,
156 &vf_info_field,
157 &vf_info_denoise3d,
158 &vf_info_hqdn3d,
159 &vf_info_detc,
160 &vf_info_telecine,
161 &vf_info_tinterlace,
162 &vf_info_tfields,
163 &vf_info_ivtc,
164 &vf_info_ilpack,
165 &vf_info_dsize,
166 &vf_info_decimate,
167 &vf_info_softpulldown,
168 &vf_info_pullup,
169 &vf_info_filmdint,
170 &vf_info_framestep,
171 &vf_info_tile,
172 &vf_info_delogo,
173 &vf_info_remove_logo,
174 &vf_info_hue,
175 #ifdef CONFIG_LIBAVCODEC_A
176 &vf_info_spp,
177 &vf_info_uspp,
178 &vf_info_fspp,
179 &vf_info_qp,
180 &vf_info_mcdeint,
181 &vf_info_geq,
182 #endif
183 &vf_info_yuvcsp,
184 &vf_info_kerndeint,
185 &vf_info_rgbtest,
186 &vf_info_phase,
187 &vf_info_divtc,
188 &vf_info_harddup,
189 &vf_info_softskip,
190 #ifdef CONFIG_ASS
191 &vf_info_ass,
192 #endif
193 &vf_info_yadif,
194 &vf_info_blackframe,
195 &vf_info_ow,
196 NULL
199 // For the vf option
200 m_obj_settings_t* vf_settings = NULL;
201 const m_obj_list_t vf_obj_list = {
202 (void**)filter_list,
203 M_ST_OFF(vf_info_t,name),
204 M_ST_OFF(vf_info_t,info),
205 M_ST_OFF(vf_info_t,opts)
208 //============================================================================
209 // mpi stuff:
211 void vf_mpi_clear(mp_image_t* mpi,int x0,int y0,int w,int h){
212 int y;
213 if(mpi->flags&MP_IMGFLAG_PLANAR){
214 y0&=~1;h+=h&1;
215 if(x0==0 && w==mpi->width){
216 // full width clear:
217 memset(mpi->planes[0]+mpi->stride[0]*y0,0,mpi->stride[0]*h);
218 memset(mpi->planes[1]+mpi->stride[1]*(y0>>mpi->chroma_y_shift),128,mpi->stride[1]*(h>>mpi->chroma_y_shift));
219 memset(mpi->planes[2]+mpi->stride[2]*(y0>>mpi->chroma_y_shift),128,mpi->stride[2]*(h>>mpi->chroma_y_shift));
220 } else
221 for(y=y0;y<y0+h;y+=2){
222 memset(mpi->planes[0]+x0+mpi->stride[0]*y,0,w);
223 memset(mpi->planes[0]+x0+mpi->stride[0]*(y+1),0,w);
224 memset(mpi->planes[1]+(x0>>mpi->chroma_x_shift)+mpi->stride[1]*(y>>mpi->chroma_y_shift),128,(w>>mpi->chroma_x_shift));
225 memset(mpi->planes[2]+(x0>>mpi->chroma_x_shift)+mpi->stride[2]*(y>>mpi->chroma_y_shift),128,(w>>mpi->chroma_x_shift));
227 return;
229 // packed:
230 for(y=y0;y<y0+h;y++){
231 unsigned char* dst=mpi->planes[0]+mpi->stride[0]*y+(mpi->bpp>>3)*x0;
232 if(mpi->flags&MP_IMGFLAG_YUV){
233 unsigned int* p=(unsigned int*) dst;
234 int size=(mpi->bpp>>3)*w/4;
235 int i;
236 #ifdef WORDS_BIGENDIAN
237 #define CLEAR_PACKEDYUV_PATTERN 0x00800080
238 #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x80008000
239 #else
240 #define CLEAR_PACKEDYUV_PATTERN 0x80008000
241 #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x00800080
242 #endif
243 if(mpi->flags&MP_IMGFLAG_SWAPPED){
244 for(i=0;i<size-3;i+=4) p[i]=p[i+1]=p[i+2]=p[i+3]=CLEAR_PACKEDYUV_PATTERN_SWAPPED;
245 for(;i<size;i++) p[i]=CLEAR_PACKEDYUV_PATTERN_SWAPPED;
246 } else {
247 for(i=0;i<size-3;i+=4) p[i]=p[i+1]=p[i+2]=p[i+3]=CLEAR_PACKEDYUV_PATTERN;
248 for(;i<size;i++) p[i]=CLEAR_PACKEDYUV_PATTERN;
250 } else
251 memset(dst,0,(mpi->bpp>>3)*w);
255 mp_image_t* vf_get_image(vf_instance_t* vf, unsigned int outfmt, int mp_imgtype, int mp_imgflag, int w, int h){
256 mp_image_t* mpi=NULL;
257 int w2;
258 int number = mp_imgtype >> 16;
260 #ifdef MP_DEBUG
261 assert(w == -1 || w >= vf->w);
262 assert(h == -1 || h >= vf->h);
263 assert(vf->w > 0);
264 assert(vf->h > 0);
265 #endif
267 // fprintf(stderr, "get_image: %d:%d, vf: %d:%d\n", w,h,vf->w,vf->h);
269 if (w == -1) w = vf->w;
270 if (h == -1) h = vf->h;
272 w2=(mp_imgflag&MP_IMGFLAG_ACCEPT_ALIGNED_STRIDE)?((w+15)&(~15)):w;
274 if(vf->put_image==vf_next_put_image){
275 // passthru mode, if the filter uses the fallback/default put_image() code
276 return vf_get_image(vf->next,outfmt,mp_imgtype,mp_imgflag,w,h);
279 // Note: we should call libvo first to check if it supports direct rendering
280 // and if not, then fallback to software buffers:
281 switch(mp_imgtype & 0xff){
282 case MP_IMGTYPE_EXPORT:
283 if(!vf->imgctx.export_images[0]) vf->imgctx.export_images[0]=new_mp_image(w2,h);
284 mpi=vf->imgctx.export_images[0];
285 break;
286 case MP_IMGTYPE_STATIC:
287 if(!vf->imgctx.static_images[0]) vf->imgctx.static_images[0]=new_mp_image(w2,h);
288 mpi=vf->imgctx.static_images[0];
289 break;
290 case MP_IMGTYPE_TEMP:
291 if(!vf->imgctx.temp_images[0]) vf->imgctx.temp_images[0]=new_mp_image(w2,h);
292 mpi=vf->imgctx.temp_images[0];
293 break;
294 case MP_IMGTYPE_IPB:
295 if(!(mp_imgflag&MP_IMGFLAG_READABLE)){ // B frame:
296 if(!vf->imgctx.temp_images[0]) vf->imgctx.temp_images[0]=new_mp_image(w2,h);
297 mpi=vf->imgctx.temp_images[0];
298 break;
300 case MP_IMGTYPE_IP:
301 if(!vf->imgctx.static_images[vf->imgctx.static_idx]) vf->imgctx.static_images[vf->imgctx.static_idx]=new_mp_image(w2,h);
302 mpi=vf->imgctx.static_images[vf->imgctx.static_idx];
303 vf->imgctx.static_idx^=1;
304 break;
305 case MP_IMGTYPE_NUMBERED:
306 if (number == -1) {
307 int i;
308 for (i = 0; i < NUM_NUMBERED_MPI; i++)
309 if (!vf->imgctx.numbered_images[i] || !vf->imgctx.numbered_images[i]->usage_count)
310 break;
311 number = i;
313 if (number < 0 || number >= NUM_NUMBERED_MPI) return NULL;
314 if (!vf->imgctx.numbered_images[number]) vf->imgctx.numbered_images[number] = new_mp_image(w2,h);
315 mpi = vf->imgctx.numbered_images[number];
316 mpi->number = number;
317 break;
319 if(mpi){
320 mpi->type=mp_imgtype;
321 mpi->w=vf->w; mpi->h=vf->h;
322 // keep buffer allocation status & color flags only:
323 // mpi->flags&=~(MP_IMGFLAG_PRESERVE|MP_IMGFLAG_READABLE|MP_IMGFLAG_DIRECT);
324 mpi->flags&=MP_IMGFLAG_ALLOCATED|MP_IMGFLAG_TYPE_DISPLAYED|MP_IMGFLAGMASK_COLORS;
325 // accept restrictions & draw_slice flags only:
326 mpi->flags|=mp_imgflag&(MP_IMGFLAGMASK_RESTRICTIONS|MP_IMGFLAG_DRAW_CALLBACK);
327 if(!vf->draw_slice) mpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
328 if(mpi->width!=w2 || mpi->height!=h){
329 // printf("vf.c: MPI parameters changed! %dx%d -> %dx%d \n", mpi->width,mpi->height,w2,h);
330 if(mpi->flags&MP_IMGFLAG_ALLOCATED){
331 if(mpi->width<w2 || mpi->height<h){
332 // need to re-allocate buffer memory:
333 free(mpi->planes[0]);
334 mpi->flags&=~MP_IMGFLAG_ALLOCATED;
335 mp_msg(MSGT_VFILTER,MSGL_V,"vf.c: have to REALLOCATE buffer memory :(\n");
337 // } else {
339 mpi->width=w2; mpi->chroma_width=(w2 + (1<<mpi->chroma_x_shift) - 1)>>mpi->chroma_x_shift;
340 mpi->height=h; mpi->chroma_height=(h + (1<<mpi->chroma_y_shift) - 1)>>mpi->chroma_y_shift;
343 if(!mpi->bpp) mp_image_setfmt(mpi,outfmt);
344 if(!(mpi->flags&MP_IMGFLAG_ALLOCATED) && mpi->type>MP_IMGTYPE_EXPORT){
346 // check libvo first!
347 if(vf->get_image) vf->get_image(vf,mpi);
349 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
350 // non-direct and not yet allocated image. allocate it!
351 if (!mpi->bpp) { // no way we can allocate this
352 mp_msg(MSGT_DECVIDEO, MSGL_FATAL,
353 "vf_get_image: Tried to allocate a format that can not be allocated!\n");
354 return NULL;
357 // check if codec prefer aligned stride:
358 if(mp_imgflag&MP_IMGFLAG_PREFER_ALIGNED_STRIDE){
359 int align=(mpi->flags&MP_IMGFLAG_PLANAR &&
360 mpi->flags&MP_IMGFLAG_YUV) ?
361 (8<<mpi->chroma_x_shift)-1 : 15; // -- maybe FIXME
362 w2=((w+align)&(~align));
363 if(mpi->width!=w2){
364 // we have to change width... check if we CAN co it:
365 int flags=vf->query_format(vf,outfmt); // should not fail
366 if(!(flags&3)) mp_msg(MSGT_DECVIDEO,MSGL_WARN,"??? vf_get_image{vf->query_format(outfmt)} failed!\n");
367 // printf("query -> 0x%X \n",flags);
368 if(flags&VFCAP_ACCEPT_STRIDE){
369 mpi->width=w2;
370 mpi->chroma_width=(w2 + (1<<mpi->chroma_x_shift) - 1)>>mpi->chroma_x_shift;
375 // IF09 - allocate space for 4. plane delta info - unused
376 if (mpi->imgfmt == IMGFMT_IF09)
378 mpi->planes[0]=memalign(64, mpi->bpp*mpi->width*(mpi->height+2)/8+
379 mpi->chroma_width*mpi->chroma_height);
380 /* export delta table */
381 mpi->planes[3]=mpi->planes[0]+(mpi->width*mpi->height)+2*(mpi->chroma_width*mpi->chroma_height);
383 else
384 mpi->planes[0]=memalign(64, mpi->bpp*mpi->width*(mpi->height+2)/8);
385 if(mpi->flags&MP_IMGFLAG_PLANAR){
386 // YV12/I420/YVU9/IF09. feel free to add other planar formats here...
387 //if(!mpi->stride[0])
388 mpi->stride[0]=mpi->width;
389 //if(!mpi->stride[1])
390 if(mpi->num_planes > 2){
391 mpi->stride[1]=mpi->stride[2]=mpi->chroma_width;
392 if(mpi->flags&MP_IMGFLAG_SWAPPED){
393 // I420/IYUV (Y,U,V)
394 mpi->planes[1]=mpi->planes[0]+mpi->width*mpi->height;
395 mpi->planes[2]=mpi->planes[1]+mpi->chroma_width*mpi->chroma_height;
396 } else {
397 // YV12,YVU9,IF09 (Y,V,U)
398 mpi->planes[2]=mpi->planes[0]+mpi->width*mpi->height;
399 mpi->planes[1]=mpi->planes[2]+mpi->chroma_width*mpi->chroma_height;
401 } else {
402 // NV12/NV21
403 mpi->stride[1]=mpi->chroma_width;
404 mpi->planes[1]=mpi->planes[0]+mpi->width*mpi->height;
406 } else {
407 //if(!mpi->stride[0])
408 mpi->stride[0]=mpi->width*mpi->bpp/8;
410 // printf("clearing img!\n");
411 vf_mpi_clear(mpi,0,0,mpi->width,mpi->height);
412 mpi->flags|=MP_IMGFLAG_ALLOCATED;
415 if(mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)
416 if(vf->start_slice) vf->start_slice(vf,mpi);
417 if(!(mpi->flags&MP_IMGFLAG_TYPE_DISPLAYED)){
418 mp_msg(MSGT_DECVIDEO,MSGL_V,"*** [%s] %s%s mp_image_t, %dx%dx%dbpp %s %s, %d bytes\n",
419 vf->info->name,
420 (mpi->type==MP_IMGTYPE_EXPORT)?"Exporting":
421 ((mpi->flags&MP_IMGFLAG_DIRECT)?"Direct Rendering":"Allocating"),
422 (mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)?" (slices)":"",
423 mpi->width,mpi->height,mpi->bpp,
424 (mpi->flags&MP_IMGFLAG_YUV)?"YUV":((mpi->flags&MP_IMGFLAG_SWAPPED)?"BGR":"RGB"),
425 (mpi->flags&MP_IMGFLAG_PLANAR)?"planar":"packed",
426 mpi->bpp*mpi->width*mpi->height/8);
427 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",
428 mpi->imgfmt, mpi->planes[0], mpi->planes[1], mpi->planes[2],
429 mpi->stride[0], mpi->stride[1], mpi->stride[2],
430 mpi->chroma_width, mpi->chroma_height, mpi->chroma_x_shift, mpi->chroma_y_shift);
431 mpi->flags|=MP_IMGFLAG_TYPE_DISPLAYED;
434 mpi->qscale = NULL;
436 mpi->usage_count++;
437 // printf("\rVF_MPI: %p %p %p %d %d %d \n",
438 // mpi->planes[0],mpi->planes[1],mpi->planes[2],
439 // mpi->stride[0],mpi->stride[1],mpi->stride[2]);
440 return mpi;
443 //============================================================================
445 // By default vf doesn't accept MPEGPES
446 static int vf_default_query_format(struct vf_instance_s* vf, unsigned int fmt){
447 if(fmt == IMGFMT_MPEGPES) return 0;
448 return vf_next_query_format(vf,fmt);
451 vf_instance_t* vf_open_plugin(const vf_info_t* const* filter_list, vf_instance_t* next, const char *name, char **args){
452 vf_instance_t* vf;
453 int i;
454 for(i=0;;i++){
455 if(!filter_list[i]){
456 mp_msg(MSGT_VFILTER,MSGL_ERR,MSGTR_CouldNotFindVideoFilter,name);
457 return NULL; // no such filter!
459 if(!strcmp(filter_list[i]->name,name)) break;
461 vf=malloc(sizeof(vf_instance_t));
462 memset(vf,0,sizeof(vf_instance_t));
463 vf->info=filter_list[i];
464 vf->next=next;
465 vf->config=vf_next_config;
466 vf->control=vf_next_control;
467 vf->query_format=vf_default_query_format;
468 vf->put_image=vf_next_put_image;
469 vf->default_caps=VFCAP_ACCEPT_STRIDE;
470 vf->default_reqs=0;
471 if(vf->info->opts) { // vf_vo get some special argument
472 const m_struct_t* st = vf->info->opts;
473 void* vf_priv = m_struct_alloc(st);
474 int n;
475 for(n = 0 ; args && args[2*n] ; n++)
476 m_struct_set(st,vf_priv,args[2*n],args[2*n+1]);
477 vf->priv = vf_priv;
478 args = NULL;
479 } else // Otherwise we should have the '_oldargs_'
480 if(args && !strcmp(args[0],"_oldargs_"))
481 args = (char**)args[1];
482 else
483 args = NULL;
484 if(vf->info->open(vf,(char*)args)>0) return vf; // Success!
485 free(vf);
486 mp_msg(MSGT_VFILTER,MSGL_ERR,MSGTR_CouldNotOpenVideoFilter,name);
487 return NULL;
490 vf_instance_t* vf_open_filter(vf_instance_t* next, const char *name, char **args){
491 if(args && strcmp(args[0],"_oldargs_")) {
492 int i,l = 0;
493 for(i = 0 ; args && args[2*i] ; i++)
494 l += 1 + strlen(args[2*i]) + 1 + strlen(args[2*i+1]);
495 l += strlen(name);
497 char str[l+1];
498 char* p = str;
499 p += sprintf(str,"%s",name);
500 for(i = 0 ; args && args[2*i] ; i++)
501 p += sprintf(p," %s=%s",args[2*i],args[2*i+1]);
502 mp_msg(MSGT_VFILTER,MSGL_INFO,MSGTR_OpeningVideoFilter "[%s]\n",str);
504 } else if(strcmp(name,"vo")) {
505 if(args && strcmp(args[0],"_oldargs_") == 0)
506 mp_msg(MSGT_VFILTER,MSGL_INFO,MSGTR_OpeningVideoFilter
507 "[%s=%s]\n", name,args[1]);
508 else
509 mp_msg(MSGT_VFILTER,MSGL_INFO,MSGTR_OpeningVideoFilter
510 "[%s]\n", name);
512 return vf_open_plugin(filter_list,next,name,args);
516 * \brief adds a filter before the last one (which should be the vo filter).
517 * \param vf start of the filter chain.
518 * \param name name of the filter to add.
519 * \param args argument list for the filter.
520 * \return pointer to the filter instance that was created.
522 vf_instance_t* vf_add_before_vo(vf_instance_t **vf, char *name, char **args) {
523 vf_instance_t *vo, *prev = NULL, *new;
524 // Find the last filter (should be vf_vo)
525 for (vo = *vf; vo->next; vo = vo->next)
526 prev = vo;
527 new = vf_open_filter(vo, name, args);
528 if (prev)
529 prev->next = new;
530 else
531 *vf = new;
532 return new;
535 //============================================================================
537 unsigned int vf_match_csp(vf_instance_t** vfp,const unsigned int* list,unsigned int preferred){
538 vf_instance_t* vf=*vfp;
539 const unsigned int* p;
540 unsigned int best=0;
541 int ret;
542 if((p=list)) while(*p){
543 ret=vf->query_format(vf,*p);
544 mp_msg(MSGT_VFILTER,MSGL_V,"[%s] query(%s) -> %d\n",vf->info->name,vo_format_name(*p),ret&3);
545 if(ret&2){ best=*p; break;} // no conversion -> bingo!
546 if(ret&1 && !best) best=*p; // best with conversion
547 ++p;
549 if(best) return best; // bingo, they have common csp!
550 // ok, then try with scale:
551 if(vf->info == &vf_info_scale) return 0; // avoid infinite recursion!
552 vf=vf_open_filter(vf,"scale",NULL);
553 if(!vf) return 0; // failed to init "scale"
554 // try the preferred csp first:
555 if(preferred && vf->query_format(vf,preferred)) best=preferred; else
556 // try the list again, now with "scaler" :
557 if((p=list)) while(*p){
558 ret=vf->query_format(vf,*p);
559 mp_msg(MSGT_VFILTER,MSGL_V,"[%s] query(%s) -> %d\n",vf->info->name,vo_format_name(*p),ret&3);
560 if(ret&2){ best=*p; break;} // no conversion -> bingo!
561 if(ret&1 && !best) best=*p; // best with conversion
562 ++p;
564 if(best) *vfp=vf; // else uninit vf !FIXME!
565 return best;
568 void vf_clone_mpi_attributes(mp_image_t* dst, mp_image_t* src){
569 dst->pict_type= src->pict_type;
570 dst->fields = src->fields;
571 dst->qscale_type= src->qscale_type;
572 if(dst->width == src->width && dst->height == src->height){
573 dst->qstride= src->qstride;
574 dst->qscale= src->qscale;
578 void vf_queue_frame(vf_instance_t *vf, int (*func)(vf_instance_t *))
580 vf->continue_buffered_image = func;
583 // Output the next buffered image (if any) from the filter chain.
584 // The queue could be kept as a simple stack/list instead avoiding the
585 // looping here, but there's currently no good context variable where
586 // that could be stored so this was easier to implement.
588 int vf_output_queued_frame(vf_instance_t *vf)
590 while (1) {
591 int ret;
592 vf_instance_t *current;
593 vf_instance_t *last=NULL;
594 int (*tmp)(vf_instance_t *);
595 for (current = vf; current; current = current->next)
596 if (current->continue_buffered_image)
597 last = current;
598 if (!last)
599 return 0;
600 tmp = last->continue_buffered_image;
601 last->continue_buffered_image = NULL;
602 ret = tmp(last);
603 if (ret > 0) {
604 vf->control(vf, VFCTRL_DRAW_OSD, NULL);
605 #ifdef CONFIG_ASS
606 vf->control(vf, VFCTRL_DRAW_EOSD, NULL);
607 #endif
609 if (ret)
610 return ret;
616 * \brief Video config() function wrapper
618 * Blocks config() calls with different size or format for filters
619 * with VFCAP_CONSTANT
621 * First call is redirected to vf->config.
623 * In following calls, it verifies that the configuration parameters
624 * are unchanged, and returns either success or error.
627 int vf_config_wrapper(struct vf_instance_s* vf,
628 int width, int height, int d_width, int d_height,
629 unsigned int flags, unsigned int outfmt)
631 int r;
632 if ((vf->default_caps&VFCAP_CONSTANT) && vf->fmt.have_configured) {
633 if ((vf->fmt.orig_width != width)
634 || (vf->fmt.orig_height != height)
635 || (vf->fmt.orig_fmt != outfmt)) {
636 mp_msg(MSGT_VFILTER,MSGL_ERR,MSGTR_ResolutionDoesntMatch);
637 return 0;
639 return 1;
641 vf->fmt.have_configured = 1;
642 vf->fmt.orig_height = height;
643 vf->fmt.orig_width = width;
644 vf->fmt.orig_fmt = outfmt;
645 r = vf->config(vf, width, height, d_width, d_height, flags, outfmt);
646 if (!r) vf->fmt.have_configured = 0;
647 return r;
650 int vf_next_config(struct vf_instance_s* vf,
651 int width, int height, int d_width, int d_height,
652 unsigned int voflags, unsigned int outfmt){
653 int miss;
654 int flags=vf->next->query_format(vf->next,outfmt);
655 if(!flags){
656 // hmm. colorspace mismatch!!!
657 // let's insert the 'scale' filter, it does the job for us:
658 vf_instance_t* vf2;
659 if(vf->next->info==&vf_info_scale) return 0; // scale->scale
660 vf2=vf_open_filter(vf->next,"scale",NULL);
661 if(!vf2) return 0; // shouldn't happen!
662 vf->next=vf2;
663 flags=vf->next->query_format(vf->next,outfmt);
664 if(!flags){
665 mp_msg(MSGT_VFILTER,MSGL_ERR,MSGTR_CannotFindColorspace);
666 return 0; // FAIL
669 mp_msg(MSGT_VFILTER,MSGL_V,"REQ: flags=0x%X req=0x%X \n",flags,vf->default_reqs);
670 miss=vf->default_reqs - (flags&vf->default_reqs);
671 if(miss&VFCAP_ACCEPT_STRIDE){
672 // vf requires stride support but vf->next doesn't support it!
673 // let's insert the 'expand' filter, it does the job for us:
674 vf_instance_t* vf2=vf_open_filter(vf->next,"expand",NULL);
675 if(!vf2) return 0; // shouldn't happen!
676 vf->next=vf2;
678 vf->next->w = width; vf->next->h = height;
679 return vf_config_wrapper(vf->next,width,height,d_width,d_height,voflags,outfmt);
682 int vf_next_control(struct vf_instance_s* vf, int request, void* data){
683 return vf->next->control(vf->next,request,data);
686 void vf_extra_flip(struct vf_instance_s* vf) {
687 vf_next_control(vf, VFCTRL_DRAW_OSD, NULL);
688 #ifdef CONFIG_ASS
689 vf_next_control(vf, VFCTRL_DRAW_EOSD, NULL);
690 #endif
691 vf_next_control(vf, VFCTRL_FLIP_PAGE, NULL);
694 int vf_next_query_format(struct vf_instance_s* 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_s* 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_s* 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 vf_instance_t* vf;
730 int i;
732 if(vf_settings) {
733 // We want to add them in the 'right order'
734 for(i = 0 ; vf_settings[i].name ; i++)
735 /* NOP */;
736 for(i-- ; i >= 0 ; i--) {
737 //printf("Open filter %s\n",vf_settings[i].name);
738 vf = vf_open_filter(last,vf_settings[i].name,vf_settings[i].attribs);
739 if(vf) last=vf;
742 return last;
745 //============================================================================
747 void vf_uninit_filter(vf_instance_t* vf){
748 if(vf->uninit) vf->uninit(vf);
749 free_mp_image(vf->imgctx.static_images[0]);
750 free_mp_image(vf->imgctx.static_images[1]);
751 free_mp_image(vf->imgctx.temp_images[0]);
752 free_mp_image(vf->imgctx.export_images[0]);
753 free(vf);
756 void vf_uninit_filter_chain(vf_instance_t* vf){
757 while(vf){
758 vf_instance_t* next=vf->next;
759 vf_uninit_filter(vf);
760 vf=next;