Make dvdnav cache on Local AppData
[mplayer/kovensky.git] / libmpcodecs / vf.c
blob878bbb1edd6f0a754136deaa0e5b8aa0c2c7ace0
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 const m_obj_list_t vf_obj_list = {
201 (void**)filter_list,
202 M_ST_OFF(vf_info_t,name),
203 M_ST_OFF(vf_info_t,info),
204 M_ST_OFF(vf_info_t,opts)
207 //============================================================================
208 // mpi stuff:
210 void vf_mpi_clear(mp_image_t* mpi,int x0,int y0,int w,int h){
211 int y;
212 if(mpi->flags&MP_IMGFLAG_PLANAR){
213 y0&=~1;h+=h&1;
214 if(x0==0 && w==mpi->width){
215 // full width clear:
216 memset(mpi->planes[0]+mpi->stride[0]*y0,0,mpi->stride[0]*h);
217 memset(mpi->planes[1]+mpi->stride[1]*(y0>>mpi->chroma_y_shift),128,mpi->stride[1]*(h>>mpi->chroma_y_shift));
218 memset(mpi->planes[2]+mpi->stride[2]*(y0>>mpi->chroma_y_shift),128,mpi->stride[2]*(h>>mpi->chroma_y_shift));
219 } else
220 for(y=y0;y<y0+h;y+=2){
221 memset(mpi->planes[0]+x0+mpi->stride[0]*y,0,w);
222 memset(mpi->planes[0]+x0+mpi->stride[0]*(y+1),0,w);
223 memset(mpi->planes[1]+(x0>>mpi->chroma_x_shift)+mpi->stride[1]*(y>>mpi->chroma_y_shift),128,(w>>mpi->chroma_x_shift));
224 memset(mpi->planes[2]+(x0>>mpi->chroma_x_shift)+mpi->stride[2]*(y>>mpi->chroma_y_shift),128,(w>>mpi->chroma_x_shift));
226 return;
228 // packed:
229 for(y=y0;y<y0+h;y++){
230 unsigned char* dst=mpi->planes[0]+mpi->stride[0]*y+(mpi->bpp>>3)*x0;
231 if(mpi->flags&MP_IMGFLAG_YUV){
232 unsigned int* p=(unsigned int*) dst;
233 int size=(mpi->bpp>>3)*w/4;
234 int i;
235 #ifdef WORDS_BIGENDIAN
236 #define CLEAR_PACKEDYUV_PATTERN 0x00800080
237 #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x80008000
238 #else
239 #define CLEAR_PACKEDYUV_PATTERN 0x80008000
240 #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x00800080
241 #endif
242 if(mpi->flags&MP_IMGFLAG_SWAPPED){
243 for(i=0;i<size-3;i+=4) p[i]=p[i+1]=p[i+2]=p[i+3]=CLEAR_PACKEDYUV_PATTERN_SWAPPED;
244 for(;i<size;i++) p[i]=CLEAR_PACKEDYUV_PATTERN_SWAPPED;
245 } else {
246 for(i=0;i<size-3;i+=4) p[i]=p[i+1]=p[i+2]=p[i+3]=CLEAR_PACKEDYUV_PATTERN;
247 for(;i<size;i++) p[i]=CLEAR_PACKEDYUV_PATTERN;
249 } else
250 memset(dst,0,(mpi->bpp>>3)*w);
254 mp_image_t* vf_get_image(vf_instance_t* vf, unsigned int outfmt, int mp_imgtype, int mp_imgflag, int w, int h){
255 mp_image_t* mpi=NULL;
256 int w2;
257 int number = mp_imgtype >> 16;
259 #ifdef MP_DEBUG
260 assert(w == -1 || w >= vf->w);
261 assert(h == -1 || h >= vf->h);
262 assert(vf->w > 0);
263 assert(vf->h > 0);
264 #endif
266 // fprintf(stderr, "get_image: %d:%d, vf: %d:%d\n", w,h,vf->w,vf->h);
268 if (w == -1) w = vf->w;
269 if (h == -1) h = vf->h;
271 w2=(mp_imgflag&MP_IMGFLAG_ACCEPT_ALIGNED_STRIDE)?((w+15)&(~15)):w;
273 if(vf->put_image==vf_next_put_image){
274 // passthru mode, if the filter uses the fallback/default put_image() code
275 return vf_get_image(vf->next,outfmt,mp_imgtype,mp_imgflag,w,h);
278 // Note: we should call libvo first to check if it supports direct rendering
279 // and if not, then fallback to software buffers:
280 switch(mp_imgtype & 0xff){
281 case MP_IMGTYPE_EXPORT:
282 if(!vf->imgctx.export_images[0]) vf->imgctx.export_images[0]=new_mp_image(w2,h);
283 mpi=vf->imgctx.export_images[0];
284 break;
285 case MP_IMGTYPE_STATIC:
286 if(!vf->imgctx.static_images[0]) vf->imgctx.static_images[0]=new_mp_image(w2,h);
287 mpi=vf->imgctx.static_images[0];
288 break;
289 case MP_IMGTYPE_TEMP:
290 if(!vf->imgctx.temp_images[0]) vf->imgctx.temp_images[0]=new_mp_image(w2,h);
291 mpi=vf->imgctx.temp_images[0];
292 break;
293 case MP_IMGTYPE_IPB:
294 if(!(mp_imgflag&MP_IMGFLAG_READABLE)){ // B frame:
295 if(!vf->imgctx.temp_images[0]) vf->imgctx.temp_images[0]=new_mp_image(w2,h);
296 mpi=vf->imgctx.temp_images[0];
297 break;
299 case MP_IMGTYPE_IP:
300 if(!vf->imgctx.static_images[vf->imgctx.static_idx]) vf->imgctx.static_images[vf->imgctx.static_idx]=new_mp_image(w2,h);
301 mpi=vf->imgctx.static_images[vf->imgctx.static_idx];
302 vf->imgctx.static_idx^=1;
303 break;
304 case MP_IMGTYPE_NUMBERED:
305 if (number == -1) {
306 int i;
307 for (i = 0; i < NUM_NUMBERED_MPI; i++)
308 if (!vf->imgctx.numbered_images[i] || !vf->imgctx.numbered_images[i]->usage_count)
309 break;
310 number = i;
312 if (number < 0 || number >= NUM_NUMBERED_MPI) return NULL;
313 if (!vf->imgctx.numbered_images[number]) vf->imgctx.numbered_images[number] = new_mp_image(w2,h);
314 mpi = vf->imgctx.numbered_images[number];
315 mpi->number = number;
316 break;
318 if(mpi){
319 mpi->type=mp_imgtype;
320 mpi->w=vf->w; mpi->h=vf->h;
321 // keep buffer allocation status & color flags only:
322 // mpi->flags&=~(MP_IMGFLAG_PRESERVE|MP_IMGFLAG_READABLE|MP_IMGFLAG_DIRECT);
323 mpi->flags&=MP_IMGFLAG_ALLOCATED|MP_IMGFLAG_TYPE_DISPLAYED|MP_IMGFLAGMASK_COLORS;
324 // accept restrictions & draw_slice flags only:
325 mpi->flags|=mp_imgflag&(MP_IMGFLAGMASK_RESTRICTIONS|MP_IMGFLAG_DRAW_CALLBACK);
326 if(!vf->draw_slice) mpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
327 if(mpi->width!=w2 || mpi->height!=h){
328 // printf("vf.c: MPI parameters changed! %dx%d -> %dx%d \n", mpi->width,mpi->height,w2,h);
329 if(mpi->flags&MP_IMGFLAG_ALLOCATED){
330 if(mpi->width<w2 || mpi->height<h){
331 // need to re-allocate buffer memory:
332 free(mpi->planes[0]);
333 mpi->flags&=~MP_IMGFLAG_ALLOCATED;
334 mp_msg(MSGT_VFILTER,MSGL_V,"vf.c: have to REALLOCATE buffer memory :(\n");
336 // } else {
338 mpi->width=w2; mpi->chroma_width=(w2 + (1<<mpi->chroma_x_shift) - 1)>>mpi->chroma_x_shift;
339 mpi->height=h; mpi->chroma_height=(h + (1<<mpi->chroma_y_shift) - 1)>>mpi->chroma_y_shift;
342 if(!mpi->bpp) mp_image_setfmt(mpi,outfmt);
343 if(!(mpi->flags&MP_IMGFLAG_ALLOCATED) && mpi->type>MP_IMGTYPE_EXPORT){
345 // check libvo first!
346 if(vf->get_image) vf->get_image(vf,mpi);
348 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
349 // non-direct and not yet allocated image. allocate it!
350 if (!mpi->bpp) { // no way we can allocate this
351 mp_msg(MSGT_DECVIDEO, MSGL_FATAL,
352 "vf_get_image: Tried to allocate a format that can not be allocated!\n");
353 return NULL;
356 // check if codec prefer aligned stride:
357 if(mp_imgflag&MP_IMGFLAG_PREFER_ALIGNED_STRIDE){
358 int align=(mpi->flags&MP_IMGFLAG_PLANAR &&
359 mpi->flags&MP_IMGFLAG_YUV) ?
360 (8<<mpi->chroma_x_shift)-1 : 15; // -- maybe FIXME
361 w2=((w+align)&(~align));
362 if(mpi->width!=w2){
363 // we have to change width... check if we CAN co it:
364 int flags=vf->query_format(vf,outfmt); // should not fail
365 if(!(flags&3)) mp_msg(MSGT_DECVIDEO,MSGL_WARN,"??? vf_get_image{vf->query_format(outfmt)} failed!\n");
366 // printf("query -> 0x%X \n",flags);
367 if(flags&VFCAP_ACCEPT_STRIDE){
368 mpi->width=w2;
369 mpi->chroma_width=(w2 + (1<<mpi->chroma_x_shift) - 1)>>mpi->chroma_x_shift;
374 // IF09 - allocate space for 4. plane delta info - unused
375 if (mpi->imgfmt == IMGFMT_IF09)
377 mpi->planes[0]=memalign(64, mpi->bpp*mpi->width*(mpi->height+2)/8+
378 mpi->chroma_width*mpi->chroma_height);
379 /* export delta table */
380 mpi->planes[3]=mpi->planes[0]+(mpi->width*mpi->height)+2*(mpi->chroma_width*mpi->chroma_height);
382 else
383 mpi->planes[0]=memalign(64, mpi->bpp*mpi->width*(mpi->height+2)/8);
384 if(mpi->flags&MP_IMGFLAG_PLANAR){
385 // YV12/I420/YVU9/IF09. feel free to add other planar formats here...
386 //if(!mpi->stride[0])
387 mpi->stride[0]=mpi->width;
388 //if(!mpi->stride[1])
389 if(mpi->num_planes > 2){
390 mpi->stride[1]=mpi->stride[2]=mpi->chroma_width;
391 if(mpi->flags&MP_IMGFLAG_SWAPPED){
392 // I420/IYUV (Y,U,V)
393 mpi->planes[1]=mpi->planes[0]+mpi->width*mpi->height;
394 mpi->planes[2]=mpi->planes[1]+mpi->chroma_width*mpi->chroma_height;
395 } else {
396 // YV12,YVU9,IF09 (Y,V,U)
397 mpi->planes[2]=mpi->planes[0]+mpi->width*mpi->height;
398 mpi->planes[1]=mpi->planes[2]+mpi->chroma_width*mpi->chroma_height;
400 } else {
401 // NV12/NV21
402 mpi->stride[1]=mpi->chroma_width;
403 mpi->planes[1]=mpi->planes[0]+mpi->width*mpi->height;
405 } else {
406 //if(!mpi->stride[0])
407 mpi->stride[0]=mpi->width*mpi->bpp/8;
409 // printf("clearing img!\n");
410 vf_mpi_clear(mpi,0,0,mpi->width,mpi->height);
411 mpi->flags|=MP_IMGFLAG_ALLOCATED;
414 if(mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)
415 if(vf->start_slice) vf->start_slice(vf,mpi);
416 if(!(mpi->flags&MP_IMGFLAG_TYPE_DISPLAYED)){
417 mp_msg(MSGT_DECVIDEO,MSGL_V,"*** [%s] %s%s mp_image_t, %dx%dx%dbpp %s %s, %d bytes\n",
418 vf->info->name,
419 (mpi->type==MP_IMGTYPE_EXPORT)?"Exporting":
420 ((mpi->flags&MP_IMGFLAG_DIRECT)?"Direct Rendering":"Allocating"),
421 (mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)?" (slices)":"",
422 mpi->width,mpi->height,mpi->bpp,
423 (mpi->flags&MP_IMGFLAG_YUV)?"YUV":((mpi->flags&MP_IMGFLAG_SWAPPED)?"BGR":"RGB"),
424 (mpi->flags&MP_IMGFLAG_PLANAR)?"planar":"packed",
425 mpi->bpp*mpi->width*mpi->height/8);
426 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",
427 mpi->imgfmt, mpi->planes[0], mpi->planes[1], mpi->planes[2],
428 mpi->stride[0], mpi->stride[1], mpi->stride[2],
429 mpi->chroma_width, mpi->chroma_height, mpi->chroma_x_shift, mpi->chroma_y_shift);
430 mpi->flags|=MP_IMGFLAG_TYPE_DISPLAYED;
433 mpi->qscale = NULL;
435 mpi->usage_count++;
436 // printf("\rVF_MPI: %p %p %p %d %d %d \n",
437 // mpi->planes[0],mpi->planes[1],mpi->planes[2],
438 // mpi->stride[0],mpi->stride[1],mpi->stride[2]);
439 return mpi;
442 //============================================================================
444 // By default vf doesn't accept MPEGPES
445 static int vf_default_query_format(struct vf_instance* vf, unsigned int fmt){
446 if(fmt == IMGFMT_MPEGPES) return 0;
447 return vf_next_query_format(vf,fmt);
450 vf_instance_t* vf_open_plugin(struct MPOpts *opts, const vf_info_t* const* filter_list, vf_instance_t* next, const char *name, char **args){
451 vf_instance_t* vf;
452 int i;
453 for(i=0;;i++){
454 if(!filter_list[i]){
455 mp_tmsg(MSGT_VFILTER,MSGL_ERR,"Couldn't find video filter '%s'.\n",name);
456 return NULL; // no such filter!
458 if(!strcmp(filter_list[i]->name,name)) break;
460 vf = calloc(1, sizeof *vf);
461 vf->opts = opts;
462 vf->info=filter_list[i];
463 vf->next=next;
464 vf->config=vf_next_config;
465 vf->control=vf_next_control;
466 vf->query_format=vf_default_query_format;
467 vf->put_image=vf_next_put_image;
468 vf->default_caps=VFCAP_ACCEPT_STRIDE;
469 vf->default_reqs=0;
470 if(vf->info->opts) { // vf_vo get some special argument
471 const m_struct_t* st = vf->info->opts;
472 void* vf_priv = m_struct_alloc(st);
473 int n;
474 for(n = 0 ; args && args[2*n] ; n++)
475 m_struct_set(st,vf_priv,args[2*n],args[2*n+1]);
476 vf->priv = vf_priv;
477 args = NULL;
478 } else // Otherwise we should have the '_oldargs_'
479 if(args && !strcmp(args[0],"_oldargs_"))
480 args = (char**)args[1];
481 else
482 args = NULL;
483 if(vf->info->open(vf,(char*)args)>0) return vf; // Success!
484 free(vf);
485 mp_tmsg(MSGT_VFILTER,MSGL_ERR,"Couldn't open video filter '%s'.\n",name);
486 return NULL;
489 vf_instance_t* vf_open_filter(struct MPOpts *opts, vf_instance_t* next, const char *name, char **args){
490 if(args && strcmp(args[0],"_oldargs_")) {
491 int i,l = 0;
492 for(i = 0 ; args && args[2*i] ; i++)
493 l += 1 + strlen(args[2*i]) + 1 + strlen(args[2*i+1]);
494 l += strlen(name);
496 char str[l+1];
497 char* p = str;
498 p += sprintf(str,"%s",name);
499 for(i = 0 ; args && args[2*i] ; i++)
500 p += sprintf(p," %s=%s",args[2*i],args[2*i+1]);
501 mp_tmsg(MSGT_VFILTER,MSGL_INFO,"Opening video filter: " "[%s]\n",str);
503 } else if(strcmp(name,"vo")) {
504 if(args && strcmp(args[0],"_oldargs_") == 0)
505 mp_tmsg(MSGT_VFILTER,MSGL_INFO,"Opening video filter: "
506 "[%s=%s]\n", name,args[1]);
507 else
508 mp_tmsg(MSGT_VFILTER,MSGL_INFO,"Opening video filter: "
509 "[%s]\n", name);
511 return vf_open_plugin(opts, filter_list,next,name,args);
515 * \brief adds a filter before the last one (which should be the vo filter).
516 * \param vf start of the filter chain.
517 * \param name name of the filter to add.
518 * \param args argument list for the filter.
519 * \return pointer to the filter instance that was created.
521 vf_instance_t* vf_add_before_vo(vf_instance_t **vf, char *name, char **args) {
522 struct MPOpts *opts = (*vf)->opts;
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(opts, 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 struct MPOpts *opts = vf->opts;
540 const unsigned int* p;
541 unsigned int best=0;
542 int ret;
543 if((p=list)) while(*p){
544 ret=vf->query_format(vf,*p);
545 mp_msg(MSGT_VFILTER,MSGL_V,"[%s] query(%s) -> %d\n",vf->info->name,vo_format_name(*p),ret&3);
546 if(ret&2){ best=*p; break;} // no conversion -> bingo!
547 if(ret&1 && !best) best=*p; // best with conversion
548 ++p;
550 if(best) return best; // bingo, they have common csp!
551 // ok, then try with scale:
552 if(vf->info == &vf_info_scale) return 0; // avoid infinite recursion!
553 vf=vf_open_filter(opts, vf,"scale",NULL);
554 if(!vf) return 0; // failed to init "scale"
555 // try the preferred csp first:
556 if(preferred && vf->query_format(vf,preferred)) best=preferred; else
557 // try the list again, now with "scaler" :
558 if((p=list)) while(*p){
559 ret=vf->query_format(vf,*p);
560 mp_msg(MSGT_VFILTER,MSGL_V,"[%s] query(%s) -> %d\n",vf->info->name,vo_format_name(*p),ret&3);
561 if(ret&2){ best=*p; break;} // no conversion -> bingo!
562 if(ret&1 && !best) best=*p; // best with conversion
563 ++p;
565 if(best) *vfp=vf; // else uninit vf !FIXME!
566 return best;
569 void vf_clone_mpi_attributes(mp_image_t* dst, mp_image_t* src){
570 dst->pict_type= src->pict_type;
571 dst->fields = src->fields;
572 dst->qscale_type= src->qscale_type;
573 if(dst->width == src->width && dst->height == src->height){
574 dst->qstride= src->qstride;
575 dst->qscale= src->qscale;
579 void vf_queue_frame(vf_instance_t *vf, int (*func)(vf_instance_t *))
581 vf->continue_buffered_image = func;
584 // Output the next buffered image (if any) from the filter chain.
585 // The queue could be kept as a simple stack/list instead avoiding the
586 // looping here, but there's currently no good context variable where
587 // that could be stored so this was easier to implement.
589 int vf_output_queued_frame(vf_instance_t *vf)
591 while (1) {
592 int ret;
593 vf_instance_t *current;
594 vf_instance_t *last=NULL;
595 int (*tmp)(vf_instance_t *);
596 for (current = vf; current; current = current->next)
597 if (current->continue_buffered_image)
598 last = current;
599 if (!last)
600 return 0;
601 tmp = last->continue_buffered_image;
602 last->continue_buffered_image = NULL;
603 ret = tmp(last);
604 if (ret)
605 return ret;
611 * \brief Video config() function wrapper
613 * Blocks config() calls with different size or format for filters
614 * with VFCAP_CONSTANT
616 * First call is redirected to vf->config.
618 * In following calls, it verifies that the configuration parameters
619 * are unchanged, and returns either success or error.
622 int vf_config_wrapper(struct vf_instance* vf,
623 int width, int height, int d_width, int d_height,
624 unsigned int flags, unsigned int outfmt)
626 int r;
627 if ((vf->default_caps&VFCAP_CONSTANT) && vf->fmt.have_configured) {
628 if ((vf->fmt.orig_width != width)
629 || (vf->fmt.orig_height != height)
630 || (vf->fmt.orig_fmt != outfmt)) {
631 mp_tmsg(MSGT_VFILTER,MSGL_ERR,"\nNew video file has different resolution or colorspace than the previous one.\n");
632 return 0;
634 return 1;
636 vf->fmt.have_configured = 1;
637 vf->fmt.orig_height = height;
638 vf->fmt.orig_width = width;
639 vf->fmt.orig_fmt = outfmt;
640 r = vf->config(vf, width, height, d_width, d_height, flags, outfmt);
641 if (!r) vf->fmt.have_configured = 0;
642 return r;
645 int vf_next_config(struct vf_instance* vf,
646 int width, int height, int d_width, int d_height,
647 unsigned int voflags, unsigned int outfmt){
648 struct MPOpts *opts = vf->opts;
649 int miss;
650 int flags=vf->next->query_format(vf->next,outfmt);
651 if(!flags){
652 // hmm. colorspace mismatch!!!
653 // let's insert the 'scale' filter, it does the job for us:
654 vf_instance_t* vf2;
655 if(vf->next->info==&vf_info_scale) return 0; // scale->scale
656 vf2=vf_open_filter(opts, vf->next,"scale",NULL);
657 if(!vf2) return 0; // shouldn't happen!
658 vf->next=vf2;
659 flags=vf->next->query_format(vf->next,outfmt);
660 if(!flags){
661 mp_tmsg(MSGT_VFILTER,MSGL_ERR,"Cannot find matching colorspace, even by inserting 'scale' :(\n");
662 return 0; // FAIL
665 mp_msg(MSGT_VFILTER,MSGL_V,"REQ: flags=0x%X req=0x%X \n",flags,vf->default_reqs);
666 miss=vf->default_reqs - (flags&vf->default_reqs);
667 if(miss&VFCAP_ACCEPT_STRIDE){
668 // vf requires stride support but vf->next doesn't support it!
669 // let's insert the 'expand' filter, it does the job for us:
670 vf_instance_t* vf2=vf_open_filter(opts, vf->next,"expand",NULL);
671 if(!vf2) return 0; // shouldn't happen!
672 vf->next=vf2;
674 vf->next->w = width; vf->next->h = height;
675 return vf_config_wrapper(vf->next,width,height,d_width,d_height,voflags,outfmt);
678 int vf_next_control(struct vf_instance* vf, int request, void* data){
679 return vf->next->control(vf->next,request,data);
682 int vf_next_query_format(struct vf_instance* vf, unsigned int fmt){
683 int flags=vf->next->query_format(vf->next,fmt);
684 if(flags) flags|=vf->default_caps;
685 return flags;
688 int vf_next_put_image(struct vf_instance* vf,mp_image_t *mpi, double pts){
689 return vf->next->put_image(vf->next,mpi, pts);
692 void vf_next_draw_slice(struct vf_instance* vf,unsigned char** src, int * stride,int w, int h, int x, int y){
693 if (vf->next->draw_slice) {
694 vf->next->draw_slice(vf->next,src,stride,w,h,x,y);
695 return;
697 if (!vf->dmpi) {
698 mp_msg(MSGT_VFILTER,MSGL_ERR,"draw_slice: dmpi not stored by vf_%s\n", vf->info->name);
699 return;
701 if (!(vf->dmpi->flags & MP_IMGFLAG_PLANAR)) {
702 memcpy_pic(vf->dmpi->planes[0]+y*vf->dmpi->stride[0]+vf->dmpi->bpp/8*x,
703 src[0], vf->dmpi->bpp/8*w, h, vf->dmpi->stride[0], stride[0]);
704 return;
706 memcpy_pic(vf->dmpi->planes[0]+y*vf->dmpi->stride[0]+x, src[0],
707 w, h, vf->dmpi->stride[0], stride[0]);
708 memcpy_pic(vf->dmpi->planes[1]+(y>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[1]+(x>>vf->dmpi->chroma_x_shift),
709 src[1], w>>vf->dmpi->chroma_x_shift, h>>vf->dmpi->chroma_y_shift, vf->dmpi->stride[1], stride[1]);
710 memcpy_pic(vf->dmpi->planes[2]+(y>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[2]+(x>>vf->dmpi->chroma_x_shift),
711 src[2], w>>vf->dmpi->chroma_x_shift, h>>vf->dmpi->chroma_y_shift, vf->dmpi->stride[2], stride[2]);
714 //============================================================================
716 vf_instance_t *append_filters(vf_instance_t* last,
717 struct m_obj_settings *vf_settings)
719 struct MPOpts *opts = last->opts;
720 vf_instance_t* vf;
721 int i;
723 if(vf_settings) {
724 // We want to add them in the 'right order'
725 for(i = 0 ; vf_settings[i].name ; i++)
726 /* NOP */;
727 for(i-- ; i >= 0 ; i--) {
728 //printf("Open filter %s\n",vf_settings[i].name);
729 vf = vf_open_filter(opts, last,vf_settings[i].name,vf_settings[i].attribs);
730 if(vf) last=vf;
733 return last;
736 //============================================================================
738 void vf_uninit_filter(vf_instance_t* vf){
739 if(vf->uninit) vf->uninit(vf);
740 free_mp_image(vf->imgctx.static_images[0]);
741 free_mp_image(vf->imgctx.static_images[1]);
742 free_mp_image(vf->imgctx.temp_images[0]);
743 free_mp_image(vf->imgctx.export_images[0]);
744 free(vf);
747 void vf_uninit_filter_chain(vf_instance_t* vf){
748 while(vf){
749 vf_instance_t* next=vf->next;
750 vf_uninit_filter(vf);
751 vf=next;