Fixed and applied mplayer-tcdump.patch
[mplayer/kovensky.git] / libmpcodecs / vf.c
bloba36e73b62e9d6e4a3ea7a0baf7ee39764633ee51
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;
103 extern const vf_info_t vf_info_tcdump;
105 // list of available filters:
106 static const vf_info_t* const filter_list[]={
107 &vf_info_rectangle,
108 #ifdef HAVE_POSIX_SELECT
109 &vf_info_bmovl,
110 #endif
111 &vf_info_crop,
112 &vf_info_expand,
113 #ifdef CONFIG_LIBPOSTPROC
114 &vf_info_pp,
115 #endif
116 &vf_info_scale,
117 // &vf_info_osd,
118 &vf_info_vo,
119 &vf_info_format,
120 &vf_info_noformat,
121 &vf_info_yuy2,
122 &vf_info_flip,
123 &vf_info_rgb2bgr,
124 &vf_info_rotate,
125 &vf_info_mirror,
126 &vf_info_palette,
127 &vf_info_pp7,
128 #ifdef CONFIG_LIBAVCODEC
129 &vf_info_lavc,
130 &vf_info_lavcdeint,
131 &vf_info_screenshot,
132 #endif
133 #ifdef CONFIG_ZR
134 &vf_info_zrmjpeg,
135 #endif
136 &vf_info_dvbscale,
137 &vf_info_cropdetect,
138 &vf_info_test,
139 &vf_info_noise,
140 &vf_info_yvu9,
141 &vf_info_eq,
142 &vf_info_eq2,
143 &vf_info_gradfun,
144 &vf_info_halfpack,
145 &vf_info_dint,
146 &vf_info_1bpp,
147 &vf_info_2xsai,
148 &vf_info_unsharp,
149 &vf_info_swapuv,
150 &vf_info_il,
151 &vf_info_fil,
152 &vf_info_boxblur,
153 &vf_info_sab,
154 &vf_info_smartblur,
155 &vf_info_perspective,
156 &vf_info_down3dright,
157 &vf_info_field,
158 &vf_info_denoise3d,
159 &vf_info_hqdn3d,
160 &vf_info_detc,
161 &vf_info_telecine,
162 &vf_info_tinterlace,
163 &vf_info_tfields,
164 &vf_info_ivtc,
165 &vf_info_ilpack,
166 &vf_info_dsize,
167 &vf_info_decimate,
168 &vf_info_softpulldown,
169 &vf_info_pullup,
170 &vf_info_filmdint,
171 &vf_info_framestep,
172 &vf_info_tile,
173 &vf_info_delogo,
174 &vf_info_remove_logo,
175 &vf_info_hue,
176 #ifdef CONFIG_LIBAVCODEC_A
177 &vf_info_spp,
178 &vf_info_uspp,
179 &vf_info_fspp,
180 &vf_info_qp,
181 &vf_info_mcdeint,
182 &vf_info_geq,
183 #endif
184 &vf_info_yuvcsp,
185 &vf_info_kerndeint,
186 &vf_info_rgbtest,
187 &vf_info_phase,
188 &vf_info_divtc,
189 &vf_info_harddup,
190 &vf_info_softskip,
191 #ifdef CONFIG_ASS
192 &vf_info_ass,
193 #endif
194 &vf_info_yadif,
195 &vf_info_blackframe,
196 &vf_info_ow,
197 &vf_info_tcdump,
198 NULL
201 // For the vf option
202 const m_obj_list_t vf_obj_list = {
203 (void**)filter_list,
204 M_ST_OFF(vf_info_t,name),
205 M_ST_OFF(vf_info_t,info),
206 M_ST_OFF(vf_info_t,opts)
209 //============================================================================
210 // mpi stuff:
212 void vf_mpi_clear(mp_image_t* mpi,int x0,int y0,int w,int h){
213 int y;
214 if(mpi->flags&MP_IMGFLAG_PLANAR){
215 y0&=~1;h+=h&1;
216 if(x0==0 && w==mpi->width){
217 // full width clear:
218 memset(mpi->planes[0]+mpi->stride[0]*y0,0,mpi->stride[0]*h);
219 memset(mpi->planes[1]+mpi->stride[1]*(y0>>mpi->chroma_y_shift),128,mpi->stride[1]*(h>>mpi->chroma_y_shift));
220 memset(mpi->planes[2]+mpi->stride[2]*(y0>>mpi->chroma_y_shift),128,mpi->stride[2]*(h>>mpi->chroma_y_shift));
221 } else
222 for(y=y0;y<y0+h;y+=2){
223 memset(mpi->planes[0]+x0+mpi->stride[0]*y,0,w);
224 memset(mpi->planes[0]+x0+mpi->stride[0]*(y+1),0,w);
225 memset(mpi->planes[1]+(x0>>mpi->chroma_x_shift)+mpi->stride[1]*(y>>mpi->chroma_y_shift),128,(w>>mpi->chroma_x_shift));
226 memset(mpi->planes[2]+(x0>>mpi->chroma_x_shift)+mpi->stride[2]*(y>>mpi->chroma_y_shift),128,(w>>mpi->chroma_x_shift));
228 return;
230 // packed:
231 for(y=y0;y<y0+h;y++){
232 unsigned char* dst=mpi->planes[0]+mpi->stride[0]*y+(mpi->bpp>>3)*x0;
233 if(mpi->flags&MP_IMGFLAG_YUV){
234 unsigned int* p=(unsigned int*) dst;
235 int size=(mpi->bpp>>3)*w/4;
236 int i;
237 #ifdef WORDS_BIGENDIAN
238 #define CLEAR_PACKEDYUV_PATTERN 0x00800080
239 #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x80008000
240 #else
241 #define CLEAR_PACKEDYUV_PATTERN 0x80008000
242 #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x00800080
243 #endif
244 if(mpi->flags&MP_IMGFLAG_SWAPPED){
245 for(i=0;i<size-3;i+=4) p[i]=p[i+1]=p[i+2]=p[i+3]=CLEAR_PACKEDYUV_PATTERN_SWAPPED;
246 for(;i<size;i++) p[i]=CLEAR_PACKEDYUV_PATTERN_SWAPPED;
247 } else {
248 for(i=0;i<size-3;i+=4) p[i]=p[i+1]=p[i+2]=p[i+3]=CLEAR_PACKEDYUV_PATTERN;
249 for(;i<size;i++) p[i]=CLEAR_PACKEDYUV_PATTERN;
251 } else
252 memset(dst,0,(mpi->bpp>>3)*w);
256 mp_image_t* vf_get_image(vf_instance_t* vf, unsigned int outfmt, int mp_imgtype, int mp_imgflag, int w, int h){
257 mp_image_t* mpi=NULL;
258 int w2;
259 int number = mp_imgtype >> 16;
261 #ifdef MP_DEBUG
262 assert(w == -1 || w >= vf->w);
263 assert(h == -1 || h >= vf->h);
264 assert(vf->w > 0);
265 assert(vf->h > 0);
266 #endif
268 // fprintf(stderr, "get_image: %d:%d, vf: %d:%d\n", w,h,vf->w,vf->h);
270 if (w == -1) w = vf->w;
271 if (h == -1) h = vf->h;
273 w2=(mp_imgflag&MP_IMGFLAG_ACCEPT_ALIGNED_STRIDE)?((w+15)&(~15)):w;
275 if(vf->put_image==vf_next_put_image){
276 // passthru mode, if the filter uses the fallback/default put_image() code
277 return vf_get_image(vf->next,outfmt,mp_imgtype,mp_imgflag,w,h);
280 // Note: we should call libvo first to check if it supports direct rendering
281 // and if not, then fallback to software buffers:
282 switch(mp_imgtype & 0xff){
283 case MP_IMGTYPE_EXPORT:
284 if(!vf->imgctx.export_images[0]) vf->imgctx.export_images[0]=new_mp_image(w2,h);
285 mpi=vf->imgctx.export_images[0];
286 break;
287 case MP_IMGTYPE_STATIC:
288 if(!vf->imgctx.static_images[0]) vf->imgctx.static_images[0]=new_mp_image(w2,h);
289 mpi=vf->imgctx.static_images[0];
290 break;
291 case MP_IMGTYPE_TEMP:
292 if(!vf->imgctx.temp_images[0]) vf->imgctx.temp_images[0]=new_mp_image(w2,h);
293 mpi=vf->imgctx.temp_images[0];
294 break;
295 case MP_IMGTYPE_IPB:
296 if(!(mp_imgflag&MP_IMGFLAG_READABLE)){ // B frame:
297 if(!vf->imgctx.temp_images[0]) vf->imgctx.temp_images[0]=new_mp_image(w2,h);
298 mpi=vf->imgctx.temp_images[0];
299 break;
301 case MP_IMGTYPE_IP:
302 if(!vf->imgctx.static_images[vf->imgctx.static_idx]) vf->imgctx.static_images[vf->imgctx.static_idx]=new_mp_image(w2,h);
303 mpi=vf->imgctx.static_images[vf->imgctx.static_idx];
304 vf->imgctx.static_idx^=1;
305 break;
306 case MP_IMGTYPE_NUMBERED:
307 if (number == -1) {
308 int i;
309 for (i = 0; i < NUM_NUMBERED_MPI; i++)
310 if (!vf->imgctx.numbered_images[i] || !vf->imgctx.numbered_images[i]->usage_count)
311 break;
312 number = i;
314 if (number < 0 || number >= NUM_NUMBERED_MPI) return NULL;
315 if (!vf->imgctx.numbered_images[number]) vf->imgctx.numbered_images[number] = new_mp_image(w2,h);
316 mpi = vf->imgctx.numbered_images[number];
317 mpi->number = number;
318 break;
320 if(mpi){
321 mpi->type=mp_imgtype;
322 mpi->w=vf->w; mpi->h=vf->h;
323 // keep buffer allocation status & color flags only:
324 // mpi->flags&=~(MP_IMGFLAG_PRESERVE|MP_IMGFLAG_READABLE|MP_IMGFLAG_DIRECT);
325 mpi->flags&=MP_IMGFLAG_ALLOCATED|MP_IMGFLAG_TYPE_DISPLAYED|MP_IMGFLAGMASK_COLORS;
326 // accept restrictions & draw_slice flags only:
327 mpi->flags|=mp_imgflag&(MP_IMGFLAGMASK_RESTRICTIONS|MP_IMGFLAG_DRAW_CALLBACK);
328 if(!vf->draw_slice) mpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
329 if(mpi->width!=w2 || mpi->height!=h){
330 // printf("vf.c: MPI parameters changed! %dx%d -> %dx%d \n", mpi->width,mpi->height,w2,h);
331 if(mpi->flags&MP_IMGFLAG_ALLOCATED){
332 if(mpi->width<w2 || mpi->height<h){
333 // need to re-allocate buffer memory:
334 free(mpi->planes[0]);
335 mpi->flags&=~MP_IMGFLAG_ALLOCATED;
336 mp_msg(MSGT_VFILTER,MSGL_V,"vf.c: have to REALLOCATE buffer memory :(\n");
338 // } else {
340 mpi->width=w2; mpi->chroma_width=(w2 + (1<<mpi->chroma_x_shift) - 1)>>mpi->chroma_x_shift;
341 mpi->height=h; mpi->chroma_height=(h + (1<<mpi->chroma_y_shift) - 1)>>mpi->chroma_y_shift;
344 if(!mpi->bpp) mp_image_setfmt(mpi,outfmt);
345 if(!(mpi->flags&MP_IMGFLAG_ALLOCATED) && mpi->type>MP_IMGTYPE_EXPORT){
347 // check libvo first!
348 if(vf->get_image) vf->get_image(vf,mpi);
350 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
351 // non-direct and not yet allocated image. allocate it!
352 if (!mpi->bpp) { // no way we can allocate this
353 mp_msg(MSGT_DECVIDEO, MSGL_FATAL,
354 "vf_get_image: Tried to allocate a format that can not be allocated!\n");
355 return NULL;
358 // check if codec prefer aligned stride:
359 if(mp_imgflag&MP_IMGFLAG_PREFER_ALIGNED_STRIDE){
360 int align=(mpi->flags&MP_IMGFLAG_PLANAR &&
361 mpi->flags&MP_IMGFLAG_YUV) ?
362 (8<<mpi->chroma_x_shift)-1 : 15; // -- maybe FIXME
363 w2=((w+align)&(~align));
364 if(mpi->width!=w2){
365 // we have to change width... check if we CAN co it:
366 int flags=vf->query_format(vf,outfmt); // should not fail
367 if(!(flags&3)) mp_msg(MSGT_DECVIDEO,MSGL_WARN,"??? vf_get_image{vf->query_format(outfmt)} failed!\n");
368 // printf("query -> 0x%X \n",flags);
369 if(flags&VFCAP_ACCEPT_STRIDE){
370 mpi->width=w2;
371 mpi->chroma_width=(w2 + (1<<mpi->chroma_x_shift) - 1)>>mpi->chroma_x_shift;
376 // IF09 - allocate space for 4. plane delta info - unused
377 if (mpi->imgfmt == IMGFMT_IF09)
379 mpi->planes[0]=memalign(64, mpi->bpp*mpi->width*(mpi->height+2)/8+
380 mpi->chroma_width*mpi->chroma_height);
381 /* export delta table */
382 mpi->planes[3]=mpi->planes[0]+(mpi->width*mpi->height)+2*(mpi->chroma_width*mpi->chroma_height);
384 else
385 mpi->planes[0]=memalign(64, mpi->bpp*mpi->width*(mpi->height+2)/8);
386 if(mpi->flags&MP_IMGFLAG_PLANAR){
387 // YV12/I420/YVU9/IF09. feel free to add other planar formats here...
388 //if(!mpi->stride[0])
389 mpi->stride[0]=mpi->width;
390 //if(!mpi->stride[1])
391 if(mpi->num_planes > 2){
392 mpi->stride[1]=mpi->stride[2]=mpi->chroma_width;
393 if(mpi->flags&MP_IMGFLAG_SWAPPED){
394 // I420/IYUV (Y,U,V)
395 mpi->planes[1]=mpi->planes[0]+mpi->width*mpi->height;
396 mpi->planes[2]=mpi->planes[1]+mpi->chroma_width*mpi->chroma_height;
397 } else {
398 // YV12,YVU9,IF09 (Y,V,U)
399 mpi->planes[2]=mpi->planes[0]+mpi->width*mpi->height;
400 mpi->planes[1]=mpi->planes[2]+mpi->chroma_width*mpi->chroma_height;
402 } else {
403 // NV12/NV21
404 mpi->stride[1]=mpi->chroma_width;
405 mpi->planes[1]=mpi->planes[0]+mpi->width*mpi->height;
407 } else {
408 //if(!mpi->stride[0])
409 mpi->stride[0]=mpi->width*mpi->bpp/8;
411 // printf("clearing img!\n");
412 vf_mpi_clear(mpi,0,0,mpi->width,mpi->height);
413 mpi->flags|=MP_IMGFLAG_ALLOCATED;
416 if(mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)
417 if(vf->start_slice) vf->start_slice(vf,mpi);
418 if(!(mpi->flags&MP_IMGFLAG_TYPE_DISPLAYED)){
419 mp_msg(MSGT_DECVIDEO,MSGL_V,"*** [%s] %s%s mp_image_t, %dx%dx%dbpp %s %s, %d bytes\n",
420 vf->info->name,
421 (mpi->type==MP_IMGTYPE_EXPORT)?"Exporting":
422 ((mpi->flags&MP_IMGFLAG_DIRECT)?"Direct Rendering":"Allocating"),
423 (mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)?" (slices)":"",
424 mpi->width,mpi->height,mpi->bpp,
425 (mpi->flags&MP_IMGFLAG_YUV)?"YUV":((mpi->flags&MP_IMGFLAG_SWAPPED)?"BGR":"RGB"),
426 (mpi->flags&MP_IMGFLAG_PLANAR)?"planar":"packed",
427 mpi->bpp*mpi->width*mpi->height/8);
428 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",
429 mpi->imgfmt, mpi->planes[0], mpi->planes[1], mpi->planes[2],
430 mpi->stride[0], mpi->stride[1], mpi->stride[2],
431 mpi->chroma_width, mpi->chroma_height, mpi->chroma_x_shift, mpi->chroma_y_shift);
432 mpi->flags|=MP_IMGFLAG_TYPE_DISPLAYED;
435 mpi->qscale = NULL;
437 mpi->usage_count++;
438 // printf("\rVF_MPI: %p %p %p %d %d %d \n",
439 // mpi->planes[0],mpi->planes[1],mpi->planes[2],
440 // mpi->stride[0],mpi->stride[1],mpi->stride[2]);
441 return mpi;
444 //============================================================================
446 // By default vf doesn't accept MPEGPES
447 static int vf_default_query_format(struct vf_instance* vf, unsigned int fmt){
448 if(fmt == IMGFMT_MPEGPES) return 0;
449 return vf_next_query_format(vf,fmt);
452 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){
453 vf_instance_t* vf;
454 int i;
455 for(i=0;;i++){
456 if(!filter_list[i]){
457 mp_tmsg(MSGT_VFILTER,MSGL_ERR,"Couldn't find video filter '%s'.\n",name);
458 return NULL; // no such filter!
460 if(!strcmp(filter_list[i]->name,name)) break;
462 vf = calloc(1, sizeof *vf);
463 vf->opts = opts;
464 vf->info=filter_list[i];
465 vf->next=next;
466 vf->config=vf_next_config;
467 vf->control=vf_next_control;
468 vf->query_format=vf_default_query_format;
469 vf->put_image=vf_next_put_image;
470 vf->default_caps=VFCAP_ACCEPT_STRIDE;
471 vf->default_reqs=0;
472 if(vf->info->opts) { // vf_vo get some special argument
473 const m_struct_t* st = vf->info->opts;
474 void* vf_priv = m_struct_alloc(st);
475 int n;
476 for(n = 0 ; args && args[2*n] ; n++)
477 m_struct_set(st,vf_priv,args[2*n],args[2*n+1]);
478 vf->priv = vf_priv;
479 args = NULL;
480 } else // Otherwise we should have the '_oldargs_'
481 if(args && !strcmp(args[0],"_oldargs_"))
482 args = (char**)args[1];
483 else
484 args = NULL;
485 if(vf->info->open(vf,(char*)args)>0) return vf; // Success!
486 free(vf);
487 mp_tmsg(MSGT_VFILTER,MSGL_ERR,"Couldn't open video filter '%s'.\n",name);
488 return NULL;
491 vf_instance_t* vf_open_filter(struct MPOpts *opts, vf_instance_t* next, const char *name, char **args){
492 if(args && strcmp(args[0],"_oldargs_")) {
493 int i,l = 0;
494 for(i = 0 ; args && args[2*i] ; i++)
495 l += 1 + strlen(args[2*i]) + 1 + strlen(args[2*i+1]);
496 l += strlen(name);
498 char str[l+1];
499 char* p = str;
500 p += sprintf(str,"%s",name);
501 for(i = 0 ; args && args[2*i] ; i++)
502 p += sprintf(p," %s=%s",args[2*i],args[2*i+1]);
503 mp_tmsg(MSGT_VFILTER,MSGL_INFO,"Opening video filter: " "[%s]\n",str);
505 } else if(strcmp(name,"vo")) {
506 if(args && strcmp(args[0],"_oldargs_") == 0)
507 mp_tmsg(MSGT_VFILTER,MSGL_INFO,"Opening video filter: "
508 "[%s=%s]\n", name,args[1]);
509 else
510 mp_tmsg(MSGT_VFILTER,MSGL_INFO,"Opening video filter: "
511 "[%s]\n", name);
513 return vf_open_plugin(opts, filter_list,next,name,args);
517 * \brief adds a filter before the last one (which should be the vo filter).
518 * \param vf start of the filter chain.
519 * \param name name of the filter to add.
520 * \param args argument list for the filter.
521 * \return pointer to the filter instance that was created.
523 vf_instance_t* vf_add_before_vo(vf_instance_t **vf, char *name, char **args) {
524 struct MPOpts *opts = (*vf)->opts;
525 vf_instance_t *vo, *prev = NULL, *new;
526 // Find the last filter (should be vf_vo)
527 for (vo = *vf; vo->next; vo = vo->next)
528 prev = vo;
529 new = vf_open_filter(opts, vo, name, args);
530 if (prev)
531 prev->next = new;
532 else
533 *vf = new;
534 return new;
537 //============================================================================
539 unsigned int vf_match_csp(vf_instance_t** vfp,const unsigned int* list,unsigned int preferred){
540 vf_instance_t* vf=*vfp;
541 struct MPOpts *opts = vf->opts;
542 const unsigned int* p;
543 unsigned int best=0;
544 int ret;
545 if((p=list)) while(*p){
546 ret=vf->query_format(vf,*p);
547 mp_msg(MSGT_VFILTER,MSGL_V,"[%s] query(%s) -> %d\n",vf->info->name,vo_format_name(*p),ret&3);
548 if(ret&2){ best=*p; break;} // no conversion -> bingo!
549 if(ret&1 && !best) best=*p; // best with conversion
550 ++p;
552 if(best) return best; // bingo, they have common csp!
553 // ok, then try with scale:
554 if(vf->info == &vf_info_scale) return 0; // avoid infinite recursion!
555 vf=vf_open_filter(opts, vf,"scale",NULL);
556 if(!vf) return 0; // failed to init "scale"
557 // try the preferred csp first:
558 if(preferred && vf->query_format(vf,preferred)) best=preferred; else
559 // try the list again, now with "scaler" :
560 if((p=list)) while(*p){
561 ret=vf->query_format(vf,*p);
562 mp_msg(MSGT_VFILTER,MSGL_V,"[%s] query(%s) -> %d\n",vf->info->name,vo_format_name(*p),ret&3);
563 if(ret&2){ best=*p; break;} // no conversion -> bingo!
564 if(ret&1 && !best) best=*p; // best with conversion
565 ++p;
567 if(best) *vfp=vf; // else uninit vf !FIXME!
568 return best;
571 void vf_clone_mpi_attributes(mp_image_t* dst, mp_image_t* src){
572 dst->pict_type= src->pict_type;
573 dst->fields = src->fields;
574 dst->qscale_type= src->qscale_type;
575 if(dst->width == src->width && dst->height == src->height){
576 dst->qstride= src->qstride;
577 dst->qscale= src->qscale;
581 void vf_queue_frame(vf_instance_t *vf, int (*func)(vf_instance_t *))
583 vf->continue_buffered_image = func;
586 // Output the next buffered image (if any) from the filter chain.
587 // The queue could be kept as a simple stack/list instead avoiding the
588 // looping here, but there's currently no good context variable where
589 // that could be stored so this was easier to implement.
591 int vf_output_queued_frame(vf_instance_t *vf)
593 while (1) {
594 int ret;
595 vf_instance_t *current;
596 vf_instance_t *last=NULL;
597 int (*tmp)(vf_instance_t *);
598 for (current = vf; current; current = current->next)
599 if (current->continue_buffered_image)
600 last = current;
601 if (!last)
602 return 0;
603 tmp = last->continue_buffered_image;
604 last->continue_buffered_image = NULL;
605 ret = tmp(last);
606 if (ret)
607 return ret;
613 * \brief Video config() function wrapper
615 * Blocks config() calls with different size or format for filters
616 * with VFCAP_CONSTANT
618 * First call is redirected to vf->config.
620 * In following calls, it verifies that the configuration parameters
621 * are unchanged, and returns either success or error.
624 int vf_config_wrapper(struct vf_instance* vf,
625 int width, int height, int d_width, int d_height,
626 unsigned int flags, unsigned int outfmt)
628 int r;
629 if ((vf->default_caps&VFCAP_CONSTANT) && vf->fmt.have_configured) {
630 if ((vf->fmt.orig_width != width)
631 || (vf->fmt.orig_height != height)
632 || (vf->fmt.orig_fmt != outfmt)) {
633 mp_tmsg(MSGT_VFILTER,MSGL_ERR,"\nNew video file has different resolution or colorspace than the previous one.\n");
634 return 0;
636 return 1;
638 vf->fmt.have_configured = 1;
639 vf->fmt.orig_height = height;
640 vf->fmt.orig_width = width;
641 vf->fmt.orig_fmt = outfmt;
642 r = vf->config(vf, width, height, d_width, d_height, flags, outfmt);
643 if (!r) vf->fmt.have_configured = 0;
644 return r;
647 int vf_next_config(struct vf_instance* vf,
648 int width, int height, int d_width, int d_height,
649 unsigned int voflags, unsigned int outfmt){
650 struct MPOpts *opts = vf->opts;
651 int miss;
652 int flags=vf->next->query_format(vf->next,outfmt);
653 if(!flags){
654 // hmm. colorspace mismatch!!!
655 // let's insert the 'scale' filter, it does the job for us:
656 vf_instance_t* vf2;
657 if(vf->next->info==&vf_info_scale) return 0; // scale->scale
658 vf2=vf_open_filter(opts, vf->next,"scale",NULL);
659 if(!vf2) return 0; // shouldn't happen!
660 vf->next=vf2;
661 flags=vf->next->query_format(vf->next,outfmt);
662 if(!flags){
663 mp_tmsg(MSGT_VFILTER,MSGL_ERR,"Cannot find matching colorspace, even by inserting 'scale' :(\n");
664 return 0; // FAIL
667 mp_msg(MSGT_VFILTER,MSGL_V,"REQ: flags=0x%X req=0x%X \n",flags,vf->default_reqs);
668 miss=vf->default_reqs - (flags&vf->default_reqs);
669 if(miss&VFCAP_ACCEPT_STRIDE){
670 // vf requires stride support but vf->next doesn't support it!
671 // let's insert the 'expand' filter, it does the job for us:
672 vf_instance_t* vf2=vf_open_filter(opts, vf->next,"expand",NULL);
673 if(!vf2) return 0; // shouldn't happen!
674 vf->next=vf2;
676 vf->next->w = width; vf->next->h = height;
677 return vf_config_wrapper(vf->next,width,height,d_width,d_height,voflags,outfmt);
680 int vf_next_control(struct vf_instance* vf, int request, void* data){
681 return vf->next->control(vf->next,request,data);
684 int vf_next_query_format(struct vf_instance* vf, unsigned int fmt){
685 int flags=vf->next->query_format(vf->next,fmt);
686 if(flags) flags|=vf->default_caps;
687 return flags;
690 int vf_next_put_image(struct vf_instance* vf,mp_image_t *mpi, double pts){
691 return vf->next->put_image(vf->next,mpi, pts);
694 void vf_next_draw_slice(struct vf_instance* vf,unsigned char** src, int * stride,int w, int h, int x, int y){
695 if (vf->next->draw_slice) {
696 vf->next->draw_slice(vf->next,src,stride,w,h,x,y);
697 return;
699 if (!vf->dmpi) {
700 mp_msg(MSGT_VFILTER,MSGL_ERR,"draw_slice: dmpi not stored by vf_%s\n", vf->info->name);
701 return;
703 if (!(vf->dmpi->flags & MP_IMGFLAG_PLANAR)) {
704 memcpy_pic(vf->dmpi->planes[0]+y*vf->dmpi->stride[0]+vf->dmpi->bpp/8*x,
705 src[0], vf->dmpi->bpp/8*w, h, vf->dmpi->stride[0], stride[0]);
706 return;
708 memcpy_pic(vf->dmpi->planes[0]+y*vf->dmpi->stride[0]+x, src[0],
709 w, h, vf->dmpi->stride[0], stride[0]);
710 memcpy_pic(vf->dmpi->planes[1]+(y>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[1]+(x>>vf->dmpi->chroma_x_shift),
711 src[1], w>>vf->dmpi->chroma_x_shift, h>>vf->dmpi->chroma_y_shift, vf->dmpi->stride[1], stride[1]);
712 memcpy_pic(vf->dmpi->planes[2]+(y>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[2]+(x>>vf->dmpi->chroma_x_shift),
713 src[2], w>>vf->dmpi->chroma_x_shift, h>>vf->dmpi->chroma_y_shift, vf->dmpi->stride[2], stride[2]);
716 //============================================================================
718 vf_instance_t *append_filters(vf_instance_t* last,
719 struct m_obj_settings *vf_settings)
721 struct MPOpts *opts = last->opts;
722 vf_instance_t* vf;
723 int i;
725 if(vf_settings) {
726 // We want to add them in the 'right order'
727 for(i = 0 ; vf_settings[i].name ; i++)
728 /* NOP */;
729 for(i-- ; i >= 0 ; i--) {
730 //printf("Open filter %s\n",vf_settings[i].name);
731 vf = vf_open_filter(opts, last,vf_settings[i].name,vf_settings[i].attribs);
732 if(vf) last=vf;
735 return last;
738 //============================================================================
740 void vf_uninit_filter(vf_instance_t* vf){
741 if(vf->uninit) vf->uninit(vf);
742 free_mp_image(vf->imgctx.static_images[0]);
743 free_mp_image(vf->imgctx.static_images[1]);
744 free_mp_image(vf->imgctx.temp_images[0]);
745 free_mp_image(vf->imgctx.export_images[0]);
746 free(vf);
749 void vf_uninit_filter_chain(vf_instance_t* vf){
750 while(vf){
751 vf_instance_t* next=vf->next;
752 vf_uninit_filter(vf);
753 vf=next;