build: create and install .mo translation files
[mplayer/glamo.git] / libmpcodecs / vf.c
blob65c2f17a58a06ed196c2d9afb43e36610bad47b1
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 "m_option.h"
34 #include "m_struct.h"
37 #include "img_format.h"
38 #include "mp_image.h"
39 #include "vf.h"
41 #include "libvo/fastmemcpy.h"
43 extern const vf_info_t vf_info_vo;
44 extern const vf_info_t vf_info_rectangle;
45 extern const vf_info_t vf_info_bmovl;
46 extern const vf_info_t vf_info_crop;
47 extern const vf_info_t vf_info_expand;
48 extern const vf_info_t vf_info_pp;
49 extern const vf_info_t vf_info_scale;
50 extern const vf_info_t vf_info_format;
51 extern const vf_info_t vf_info_noformat;
52 extern const vf_info_t vf_info_yuy2;
53 extern const vf_info_t vf_info_flip;
54 extern const vf_info_t vf_info_rgb2bgr;
55 extern const vf_info_t vf_info_rotate;
56 extern const vf_info_t vf_info_mirror;
57 extern const vf_info_t vf_info_palette;
58 extern const vf_info_t vf_info_lavc;
59 extern const vf_info_t vf_info_zrmjpeg;
60 extern const vf_info_t vf_info_dvbscale;
61 extern const vf_info_t vf_info_cropdetect;
62 extern const vf_info_t vf_info_test;
63 extern const vf_info_t vf_info_noise;
64 extern const vf_info_t vf_info_yvu9;
65 extern const vf_info_t vf_info_lavcdeint;
66 extern const vf_info_t vf_info_eq;
67 extern const vf_info_t vf_info_eq2;
68 extern const vf_info_t vf_info_gradfun;
69 extern const vf_info_t vf_info_halfpack;
70 extern const vf_info_t vf_info_dint;
71 extern const vf_info_t vf_info_1bpp;
72 extern const vf_info_t vf_info_2xsai;
73 extern const vf_info_t vf_info_unsharp;
74 extern const vf_info_t vf_info_swapuv;
75 extern const vf_info_t vf_info_il;
76 extern const vf_info_t vf_info_fil;
77 extern const vf_info_t vf_info_boxblur;
78 extern const vf_info_t vf_info_sab;
79 extern const vf_info_t vf_info_smartblur;
80 extern const vf_info_t vf_info_perspective;
81 extern const vf_info_t vf_info_down3dright;
82 extern const vf_info_t vf_info_field;
83 extern const vf_info_t vf_info_denoise3d;
84 extern const vf_info_t vf_info_hqdn3d;
85 extern const vf_info_t vf_info_detc;
86 extern const vf_info_t vf_info_telecine;
87 extern const vf_info_t vf_info_tinterlace;
88 extern const vf_info_t vf_info_tfields;
89 extern const vf_info_t vf_info_ivtc;
90 extern const vf_info_t vf_info_ilpack;
91 extern const vf_info_t vf_info_dsize;
92 extern const vf_info_t vf_info_decimate;
93 extern const vf_info_t vf_info_softpulldown;
94 extern const vf_info_t vf_info_pullup;
95 extern const vf_info_t vf_info_filmdint;
96 extern const vf_info_t vf_info_framestep;
97 extern const vf_info_t vf_info_tile;
98 extern const vf_info_t vf_info_delogo;
99 extern const vf_info_t vf_info_remove_logo;
100 extern const vf_info_t vf_info_hue;
101 extern const vf_info_t vf_info_spp;
102 extern const vf_info_t vf_info_uspp;
103 extern const vf_info_t vf_info_fspp;
104 extern const vf_info_t vf_info_pp7;
105 extern const vf_info_t vf_info_yuvcsp;
106 extern const vf_info_t vf_info_kerndeint;
107 extern const vf_info_t vf_info_rgbtest;
108 extern const vf_info_t vf_info_qp;
109 extern const vf_info_t vf_info_phase;
110 extern const vf_info_t vf_info_divtc;
111 extern const vf_info_t vf_info_harddup;
112 extern const vf_info_t vf_info_softskip;
113 extern const vf_info_t vf_info_screenshot;
114 extern const vf_info_t vf_info_ass;
115 extern const vf_info_t vf_info_mcdeint;
116 extern const vf_info_t vf_info_yadif;
117 extern const vf_info_t vf_info_blackframe;
118 extern const vf_info_t vf_info_geq;
119 extern const vf_info_t vf_info_ow;
120 extern const vf_info_t vf_info_fixpts;
122 // list of available filters:
123 static const vf_info_t* const filter_list[]={
124 &vf_info_rectangle,
125 #ifdef HAVE_POSIX_SELECT
126 &vf_info_bmovl,
127 #endif
128 &vf_info_crop,
129 &vf_info_expand,
130 #ifdef CONFIG_LIBPOSTPROC
131 &vf_info_pp,
132 #endif
133 &vf_info_scale,
134 // &vf_info_osd,
135 &vf_info_vo,
136 &vf_info_format,
137 &vf_info_noformat,
138 #ifdef CONFIG_LIBSWSCALE_INTERNALS
139 &vf_info_yuy2,
140 #endif
141 &vf_info_flip,
142 #ifdef CONFIG_LIBSWSCALE_INTERNALS
143 &vf_info_rgb2bgr,
144 #endif
145 &vf_info_rotate,
146 &vf_info_mirror,
147 #ifdef CONFIG_LIBSWSCALE_INTERNALS
148 &vf_info_palette,
149 #endif
150 &vf_info_pp7,
151 #ifdef CONFIG_LIBAVCODEC
152 &vf_info_lavc,
153 &vf_info_lavcdeint,
154 &vf_info_screenshot,
155 &vf_info_uspp,
156 #endif
157 #ifdef CONFIG_ZR
158 &vf_info_zrmjpeg,
159 #endif
160 &vf_info_dvbscale,
161 &vf_info_cropdetect,
162 &vf_info_test,
163 &vf_info_noise,
164 &vf_info_yvu9,
165 &vf_info_eq,
166 &vf_info_eq2,
167 &vf_info_gradfun,
168 #ifdef CONFIG_LIBSWSCALE_INTERNALS
169 &vf_info_halfpack,
170 #endif
171 &vf_info_dint,
172 &vf_info_1bpp,
173 &vf_info_2xsai,
174 &vf_info_unsharp,
175 &vf_info_swapuv,
176 &vf_info_il,
177 &vf_info_fil,
178 &vf_info_boxblur,
179 &vf_info_sab,
180 &vf_info_smartblur,
181 &vf_info_perspective,
182 &vf_info_down3dright,
183 &vf_info_field,
184 &vf_info_denoise3d,
185 &vf_info_hqdn3d,
186 &vf_info_detc,
187 &vf_info_telecine,
188 &vf_info_tinterlace,
189 &vf_info_tfields,
190 &vf_info_ivtc,
191 &vf_info_ilpack,
192 &vf_info_dsize,
193 &vf_info_decimate,
194 &vf_info_softpulldown,
195 &vf_info_pullup,
196 &vf_info_filmdint,
197 &vf_info_framestep,
198 &vf_info_tile,
199 &vf_info_delogo,
200 &vf_info_remove_logo,
201 &vf_info_hue,
202 #ifdef CONFIG_LIBAVCODEC_INTERNALS
203 &vf_info_spp,
204 &vf_info_fspp,
205 &vf_info_qp,
206 &vf_info_mcdeint,
207 &vf_info_geq,
208 #endif
209 &vf_info_yuvcsp,
210 &vf_info_kerndeint,
211 &vf_info_rgbtest,
212 &vf_info_phase,
213 &vf_info_divtc,
214 &vf_info_harddup,
215 &vf_info_softskip,
216 #ifdef CONFIG_ASS
217 &vf_info_ass,
218 #endif
219 &vf_info_yadif,
220 &vf_info_blackframe,
221 &vf_info_ow,
222 &vf_info_fixpts,
223 NULL
226 // For the vf option
227 const m_obj_list_t vf_obj_list = {
228 (void**)filter_list,
229 M_ST_OFF(vf_info_t,name),
230 M_ST_OFF(vf_info_t,info),
231 M_ST_OFF(vf_info_t,opts)
234 //============================================================================
235 // mpi stuff:
237 void vf_mpi_clear(mp_image_t* mpi,int x0,int y0,int w,int h){
238 int y;
239 if(mpi->flags&MP_IMGFLAG_PLANAR){
240 y0&=~1;h+=h&1;
241 if(x0==0 && w==mpi->width){
242 // full width clear:
243 memset(mpi->planes[0]+mpi->stride[0]*y0,0,mpi->stride[0]*h);
244 memset(mpi->planes[1]+mpi->stride[1]*(y0>>mpi->chroma_y_shift),128,mpi->stride[1]*(h>>mpi->chroma_y_shift));
245 memset(mpi->planes[2]+mpi->stride[2]*(y0>>mpi->chroma_y_shift),128,mpi->stride[2]*(h>>mpi->chroma_y_shift));
246 } else
247 for(y=y0;y<y0+h;y+=2){
248 memset(mpi->planes[0]+x0+mpi->stride[0]*y,0,w);
249 memset(mpi->planes[0]+x0+mpi->stride[0]*(y+1),0,w);
250 memset(mpi->planes[1]+(x0>>mpi->chroma_x_shift)+mpi->stride[1]*(y>>mpi->chroma_y_shift),128,(w>>mpi->chroma_x_shift));
251 memset(mpi->planes[2]+(x0>>mpi->chroma_x_shift)+mpi->stride[2]*(y>>mpi->chroma_y_shift),128,(w>>mpi->chroma_x_shift));
253 return;
255 // packed:
256 for(y=y0;y<y0+h;y++){
257 unsigned char* dst=mpi->planes[0]+mpi->stride[0]*y+(mpi->bpp>>3)*x0;
258 if(mpi->flags&MP_IMGFLAG_YUV){
259 unsigned int* p=(unsigned int*) dst;
260 int size=(mpi->bpp>>3)*w/4;
261 int i;
262 #if HAVE_BIGENDIAN
263 #define CLEAR_PACKEDYUV_PATTERN 0x00800080
264 #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x80008000
265 #else
266 #define CLEAR_PACKEDYUV_PATTERN 0x80008000
267 #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x00800080
268 #endif
269 if(mpi->flags&MP_IMGFLAG_SWAPPED){
270 for(i=0;i<size-3;i+=4) p[i]=p[i+1]=p[i+2]=p[i+3]=CLEAR_PACKEDYUV_PATTERN_SWAPPED;
271 for(;i<size;i++) p[i]=CLEAR_PACKEDYUV_PATTERN_SWAPPED;
272 } else {
273 for(i=0;i<size-3;i+=4) p[i]=p[i+1]=p[i+2]=p[i+3]=CLEAR_PACKEDYUV_PATTERN;
274 for(;i<size;i++) p[i]=CLEAR_PACKEDYUV_PATTERN;
276 } else
277 memset(dst,0,(mpi->bpp>>3)*w);
281 mp_image_t* vf_get_image(vf_instance_t* vf, unsigned int outfmt, int mp_imgtype, int mp_imgflag, int w, int h){
282 mp_image_t* mpi=NULL;
283 int w2;
284 int number = mp_imgtype >> 16;
286 #ifdef MP_DEBUG
287 assert(w == -1 || w >= vf->w);
288 assert(h == -1 || h >= vf->h);
289 assert(vf->w > 0);
290 assert(vf->h > 0);
291 #endif
293 // fprintf(stderr, "get_image: %d:%d, vf: %d:%d\n", w,h,vf->w,vf->h);
295 if (w == -1) w = vf->w;
296 if (h == -1) h = vf->h;
298 w2=(mp_imgflag&MP_IMGFLAG_ACCEPT_ALIGNED_STRIDE)?((w+15)&(~15)):w;
300 if(vf->put_image==vf_next_put_image){
301 // passthru mode, if the filter uses the fallback/default put_image() code
302 return vf_get_image(vf->next,outfmt,mp_imgtype,mp_imgflag,w,h);
305 // Note: we should call libvo first to check if it supports direct rendering
306 // and if not, then fallback to software buffers:
307 switch(mp_imgtype & 0xff){
308 case MP_IMGTYPE_EXPORT:
309 if(!vf->imgctx.export_images[0]) vf->imgctx.export_images[0]=new_mp_image(w2,h);
310 mpi=vf->imgctx.export_images[0];
311 break;
312 case MP_IMGTYPE_STATIC:
313 if(!vf->imgctx.static_images[0]) vf->imgctx.static_images[0]=new_mp_image(w2,h);
314 mpi=vf->imgctx.static_images[0];
315 break;
316 case MP_IMGTYPE_TEMP:
317 if(!vf->imgctx.temp_images[0]) vf->imgctx.temp_images[0]=new_mp_image(w2,h);
318 mpi=vf->imgctx.temp_images[0];
319 break;
320 case MP_IMGTYPE_IPB:
321 if(!(mp_imgflag&MP_IMGFLAG_READABLE)){ // B frame:
322 if(!vf->imgctx.temp_images[0]) vf->imgctx.temp_images[0]=new_mp_image(w2,h);
323 mpi=vf->imgctx.temp_images[0];
324 break;
326 case MP_IMGTYPE_IP:
327 if(!vf->imgctx.static_images[vf->imgctx.static_idx]) vf->imgctx.static_images[vf->imgctx.static_idx]=new_mp_image(w2,h);
328 mpi=vf->imgctx.static_images[vf->imgctx.static_idx];
329 vf->imgctx.static_idx^=1;
330 break;
331 case MP_IMGTYPE_NUMBERED:
332 if (number == -1) {
333 int i;
334 for (i = 0; i < NUM_NUMBERED_MPI; i++)
335 if (!vf->imgctx.numbered_images[i] || !vf->imgctx.numbered_images[i]->usage_count)
336 break;
337 number = i;
339 if (number < 0 || number >= NUM_NUMBERED_MPI) return NULL;
340 if (!vf->imgctx.numbered_images[number]) vf->imgctx.numbered_images[number] = new_mp_image(w2,h);
341 mpi = vf->imgctx.numbered_images[number];
342 mpi->number = number;
343 break;
345 if(mpi){
346 mpi->type=mp_imgtype;
347 mpi->w=vf->w; mpi->h=vf->h;
348 // keep buffer allocation status & color flags only:
349 // mpi->flags&=~(MP_IMGFLAG_PRESERVE|MP_IMGFLAG_READABLE|MP_IMGFLAG_DIRECT);
350 mpi->flags&=MP_IMGFLAG_ALLOCATED|MP_IMGFLAG_TYPE_DISPLAYED|MP_IMGFLAGMASK_COLORS;
351 // accept restrictions, draw_slice and palette flags only:
352 mpi->flags|=mp_imgflag&(MP_IMGFLAGMASK_RESTRICTIONS|MP_IMGFLAG_DRAW_CALLBACK|MP_IMGFLAG_RGB_PALETTE);
353 if(!vf->draw_slice) mpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
354 if(mpi->width!=w2 || mpi->height!=h){
355 // printf("vf.c: MPI parameters changed! %dx%d -> %dx%d \n", mpi->width,mpi->height,w2,h);
356 if(mpi->flags&MP_IMGFLAG_ALLOCATED){
357 if(mpi->width<w2 || mpi->height<h){
358 // need to re-allocate buffer memory:
359 free(mpi->planes[0]);
360 mpi->flags&=~MP_IMGFLAG_ALLOCATED;
361 mp_msg(MSGT_VFILTER,MSGL_V,"vf.c: have to REALLOCATE buffer memory :(\n");
363 // } else {
365 mpi->width=w2; mpi->chroma_width=(w2 + (1<<mpi->chroma_x_shift) - 1)>>mpi->chroma_x_shift;
366 mpi->height=h; mpi->chroma_height=(h + (1<<mpi->chroma_y_shift) - 1)>>mpi->chroma_y_shift;
369 if(!mpi->bpp) mp_image_setfmt(mpi,outfmt);
370 if(!(mpi->flags&MP_IMGFLAG_ALLOCATED) && mpi->type>MP_IMGTYPE_EXPORT){
372 // check libvo first!
373 if(vf->get_image) vf->get_image(vf,mpi);
375 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
376 // non-direct and not yet allocated image. allocate it!
377 if (!mpi->bpp) { // no way we can allocate this
378 mp_msg(MSGT_DECVIDEO, MSGL_FATAL,
379 "vf_get_image: Tried to allocate a format that can not be allocated!\n");
380 return NULL;
383 // check if codec prefer aligned stride:
384 if(mp_imgflag&MP_IMGFLAG_PREFER_ALIGNED_STRIDE){
385 int align=(mpi->flags&MP_IMGFLAG_PLANAR &&
386 mpi->flags&MP_IMGFLAG_YUV) ?
387 (8<<mpi->chroma_x_shift)-1 : 15; // -- maybe FIXME
388 w2=((w+align)&(~align));
389 if(mpi->width!=w2){
390 // we have to change width... check if we CAN co it:
391 int flags=vf->query_format(vf,outfmt); // should not fail
392 if(!(flags&3)) mp_msg(MSGT_DECVIDEO,MSGL_WARN,"??? vf_get_image{vf->query_format(outfmt)} failed!\n");
393 // printf("query -> 0x%X \n",flags);
394 if(flags&VFCAP_ACCEPT_STRIDE){
395 mpi->width=w2;
396 mpi->chroma_width=(w2 + (1<<mpi->chroma_x_shift) - 1)>>mpi->chroma_x_shift;
401 mp_image_alloc_planes(mpi);
402 // printf("clearing img!\n");
403 vf_mpi_clear(mpi,0,0,mpi->width,mpi->height);
406 if(mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)
407 if(vf->start_slice) vf->start_slice(vf,mpi);
408 if(!(mpi->flags&MP_IMGFLAG_TYPE_DISPLAYED)){
409 mp_msg(MSGT_DECVIDEO,MSGL_V,"*** [%s] %s%s mp_image_t, %dx%dx%dbpp %s %s, %d bytes\n",
410 vf->info->name,
411 (mpi->type==MP_IMGTYPE_EXPORT)?"Exporting":
412 ((mpi->flags&MP_IMGFLAG_DIRECT)?"Direct Rendering":"Allocating"),
413 (mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)?" (slices)":"",
414 mpi->width,mpi->height,mpi->bpp,
415 (mpi->flags&MP_IMGFLAG_YUV)?"YUV":((mpi->flags&MP_IMGFLAG_SWAPPED)?"BGR":"RGB"),
416 (mpi->flags&MP_IMGFLAG_PLANAR)?"planar":"packed",
417 mpi->bpp*mpi->width*mpi->height/8);
418 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",
419 mpi->imgfmt, mpi->planes[0], mpi->planes[1], mpi->planes[2],
420 mpi->stride[0], mpi->stride[1], mpi->stride[2],
421 mpi->chroma_width, mpi->chroma_height, mpi->chroma_x_shift, mpi->chroma_y_shift);
422 mpi->flags|=MP_IMGFLAG_TYPE_DISPLAYED;
425 mpi->qscale = NULL;
427 mpi->usage_count++;
428 // printf("\rVF_MPI: %p %p %p %d %d %d \n",
429 // mpi->planes[0],mpi->planes[1],mpi->planes[2],
430 // mpi->stride[0],mpi->stride[1],mpi->stride[2]);
431 return mpi;
434 //============================================================================
436 // By default vf doesn't accept MPEGPES
437 static int vf_default_query_format(struct vf_instance* vf, unsigned int fmt){
438 if(fmt == IMGFMT_MPEGPES) return 0;
439 return vf_next_query_format(vf,fmt);
442 struct vf_instance *vf_open_plugin_noerr(struct MPOpts *opts,
443 const vf_info_t * const *filter_list,
444 vf_instance_t *next, const char *name,
445 char **args, int *retcode)
447 vf_instance_t* vf;
448 int i;
449 for(i=0;;i++){
450 if(!filter_list[i]){
451 mp_tmsg(MSGT_VFILTER,MSGL_ERR,"Couldn't find video filter '%s'.\n",name);
452 return NULL; // no such filter!
454 if(!strcmp(filter_list[i]->name,name)) break;
456 vf = calloc(1, sizeof *vf);
457 vf->opts = opts;
458 vf->info=filter_list[i];
459 vf->next=next;
460 vf->config=vf_next_config;
461 vf->control=vf_next_control;
462 vf->query_format=vf_default_query_format;
463 vf->put_image=vf_next_put_image;
464 vf->default_caps=VFCAP_ACCEPT_STRIDE;
465 vf->default_reqs=0;
466 if(vf->info->opts) { // vf_vo get some special argument
467 const m_struct_t* st = vf->info->opts;
468 void* vf_priv = m_struct_alloc(st);
469 int n;
470 for(n = 0 ; args && args[2*n] ; n++)
471 m_struct_set(st,vf_priv,args[2*n],args[2*n+1]);
472 vf->priv = vf_priv;
473 args = NULL;
474 } else // Otherwise we should have the '_oldargs_'
475 if(args && !strcmp(args[0],"_oldargs_"))
476 args = (char**)args[1];
477 else
478 args = NULL;
479 *retcode = vf->info->vf_open(vf,(char*)args);
480 if (*retcode > 0)
481 return vf;
482 free(vf);
483 return NULL;
486 struct vf_instance *vf_open_plugin(struct MPOpts *opts,
487 const vf_info_t * const *filter_list,
488 vf_instance_t *next, const char *name,
489 char **args)
491 struct vf_instance *vf = vf_open_plugin_noerr(opts, filter_list, next,
492 name, args, &(int){0});
493 if (!vf)
494 mp_tmsg(MSGT_VFILTER, MSGL_ERR, "Couldn't open video filter '%s'.\n",
495 name);
496 return vf;
499 vf_instance_t* vf_open_filter(struct MPOpts *opts, vf_instance_t* next, const char *name, char **args){
500 if(args && strcmp(args[0],"_oldargs_")) {
501 int i,l = 0;
502 for(i = 0 ; args && args[2*i] ; i++)
503 l += 1 + strlen(args[2*i]) + 1 + strlen(args[2*i+1]);
504 l += strlen(name);
506 char str[l+1];
507 char* p = str;
508 p += sprintf(str,"%s",name);
509 for(i = 0 ; args && args[2*i] ; i++)
510 p += sprintf(p," %s=%s",args[2*i],args[2*i+1]);
511 mp_msg(MSGT_VFILTER, MSGL_INFO, "%s[%s]\n",
512 mp_gtext("Opening video filter: "), str);
514 } else if(strcmp(name,"vo")) {
515 if(args && strcmp(args[0],"_oldargs_") == 0)
516 mp_msg(MSGT_VFILTER, MSGL_INFO, "%s[%s=%s]\n",
517 mp_gtext("Opening video filter: "), name, args[1]);
518 else
519 mp_msg(MSGT_VFILTER, MSGL_INFO, "%s[%s]\n",
520 mp_gtext("Opening video filter: "), name);
522 return vf_open_plugin(opts, filter_list,next,name,args);
526 * \brief adds a filter before the last one (which should be the vo filter).
527 * \param vf start of the filter chain.
528 * \param name name of the filter to add.
529 * \param args argument list for the filter.
530 * \return pointer to the filter instance that was created.
532 vf_instance_t* vf_add_before_vo(vf_instance_t **vf, char *name, char **args) {
533 struct MPOpts *opts = (*vf)->opts;
534 vf_instance_t *vo, *prev = NULL, *new;
535 // Find the last filter (should be vf_vo)
536 for (vo = *vf; vo->next; vo = vo->next)
537 prev = vo;
538 new = vf_open_filter(opts, vo, name, args);
539 if (prev)
540 prev->next = new;
541 else
542 *vf = new;
543 return new;
546 //============================================================================
548 unsigned int vf_match_csp(vf_instance_t** vfp,const unsigned int* list,unsigned int preferred){
549 vf_instance_t* vf=*vfp;
550 struct MPOpts *opts = vf->opts;
551 const unsigned int* p;
552 unsigned int best=0;
553 int ret;
554 if((p=list)) while(*p){
555 ret=vf->query_format(vf,*p);
556 mp_msg(MSGT_VFILTER,MSGL_V,"[%s] query(%s) -> %d\n",vf->info->name,vo_format_name(*p),ret&3);
557 if(ret&2){ best=*p; break;} // no conversion -> bingo!
558 if(ret&1 && !best) best=*p; // best with conversion
559 ++p;
561 if(best) return best; // bingo, they have common csp!
562 // ok, then try with scale:
563 if(vf->info == &vf_info_scale) return 0; // avoid infinite recursion!
564 vf=vf_open_filter(opts, vf,"scale",NULL);
565 if(!vf) return 0; // failed to init "scale"
566 // try the preferred csp first:
567 if(preferred && vf->query_format(vf,preferred)) best=preferred; else
568 // try the list again, now with "scaler" :
569 if((p=list)) while(*p){
570 ret=vf->query_format(vf,*p);
571 mp_msg(MSGT_VFILTER,MSGL_V,"[%s] query(%s) -> %d\n",vf->info->name,vo_format_name(*p),ret&3);
572 if(ret&2){ best=*p; break;} // no conversion -> bingo!
573 if(ret&1 && !best) best=*p; // best with conversion
574 ++p;
576 if(best) *vfp=vf; // else uninit vf !FIXME!
577 return best;
580 void vf_clone_mpi_attributes(mp_image_t* dst, mp_image_t* src){
581 dst->pict_type= src->pict_type;
582 dst->fields = src->fields;
583 dst->qscale_type= src->qscale_type;
584 if(dst->width == src->width && dst->height == src->height){
585 dst->qstride= src->qstride;
586 dst->qscale= src->qscale;
590 void vf_queue_frame(vf_instance_t *vf, int (*func)(vf_instance_t *))
592 vf->continue_buffered_image = func;
595 // Output the next buffered image (if any) from the filter chain.
596 // The queue could be kept as a simple stack/list instead avoiding the
597 // looping here, but there's currently no good context variable where
598 // that could be stored so this was easier to implement.
600 int vf_output_queued_frame(vf_instance_t *vf)
602 while (1) {
603 int ret;
604 vf_instance_t *current;
605 vf_instance_t *last=NULL;
606 int (*tmp)(vf_instance_t *);
607 for (current = vf; current; current = current->next)
608 if (current->continue_buffered_image)
609 last = current;
610 if (!last)
611 return 0;
612 tmp = last->continue_buffered_image;
613 last->continue_buffered_image = NULL;
614 ret = tmp(last);
615 if (ret)
616 return ret;
622 * \brief Video config() function wrapper
624 * Blocks config() calls with different size or format for filters
625 * with VFCAP_CONSTANT
627 * First call is redirected to vf->config.
629 * In following calls, it verifies that the configuration parameters
630 * are unchanged, and returns either success or error.
633 int vf_config_wrapper(struct vf_instance* vf,
634 int width, int height, int d_width, int d_height,
635 unsigned int flags, unsigned int outfmt)
637 int r;
638 if ((vf->default_caps&VFCAP_CONSTANT) && vf->fmt.have_configured) {
639 if ((vf->fmt.orig_width != width)
640 || (vf->fmt.orig_height != height)
641 || (vf->fmt.orig_fmt != outfmt)) {
642 mp_tmsg(MSGT_VFILTER,MSGL_ERR,"\nNew video file has different resolution or colorspace than the previous one.\n");
643 return 0;
645 return 1;
647 vf->fmt.have_configured = 1;
648 vf->fmt.orig_height = height;
649 vf->fmt.orig_width = width;
650 vf->fmt.orig_fmt = outfmt;
651 r = vf->config(vf, width, height, d_width, d_height, flags, outfmt);
652 if (!r) vf->fmt.have_configured = 0;
653 return r;
656 int vf_next_config(struct vf_instance* vf,
657 int width, int height, int d_width, int d_height,
658 unsigned int voflags, unsigned int outfmt){
659 struct MPOpts *opts = vf->opts;
660 int miss;
661 int flags=vf->next->query_format(vf->next,outfmt);
662 if(!flags){
663 // hmm. colorspace mismatch!!!
664 // let's insert the 'scale' filter, it does the job for us:
665 vf_instance_t* vf2;
666 if(vf->next->info==&vf_info_scale) return 0; // scale->scale
667 vf2=vf_open_filter(opts, vf->next,"scale",NULL);
668 if(!vf2) return 0; // shouldn't happen!
669 vf->next=vf2;
670 flags=vf->next->query_format(vf->next,outfmt);
671 if(!flags){
672 mp_tmsg(MSGT_VFILTER,MSGL_ERR,"Cannot find matching colorspace, even by inserting 'scale' :(\n");
673 return 0; // FAIL
676 mp_msg(MSGT_VFILTER,MSGL_V,"REQ: flags=0x%X req=0x%X \n",flags,vf->default_reqs);
677 miss=vf->default_reqs - (flags&vf->default_reqs);
678 if(miss&VFCAP_ACCEPT_STRIDE){
679 // vf requires stride support but vf->next doesn't support it!
680 // let's insert the 'expand' filter, it does the job for us:
681 vf_instance_t* vf2=vf_open_filter(opts, vf->next,"expand",NULL);
682 if(!vf2) return 0; // shouldn't happen!
683 vf->next=vf2;
685 vf->next->w = width; vf->next->h = height;
686 return vf_config_wrapper(vf->next,width,height,d_width,d_height,voflags,outfmt);
689 int vf_next_control(struct vf_instance* vf, int request, void* data){
690 return vf->next->control(vf->next,request,data);
693 int vf_next_query_format(struct vf_instance* vf, unsigned int fmt){
694 int flags=vf->next->query_format(vf->next,fmt);
695 if(flags) flags|=vf->default_caps;
696 return flags;
699 int vf_next_put_image(struct vf_instance* vf,mp_image_t *mpi, double pts){
700 return vf->next->put_image(vf->next,mpi, pts);
703 void vf_next_draw_slice(struct vf_instance* vf,unsigned char** src, int * stride,int w, int h, int x, int y){
704 if (vf->next->draw_slice) {
705 vf->next->draw_slice(vf->next,src,stride,w,h,x,y);
706 return;
708 if (!vf->dmpi) {
709 mp_msg(MSGT_VFILTER,MSGL_ERR,"draw_slice: dmpi not stored by vf_%s\n", vf->info->name);
710 return;
712 if (!(vf->dmpi->flags & MP_IMGFLAG_PLANAR)) {
713 memcpy_pic(vf->dmpi->planes[0]+y*vf->dmpi->stride[0]+vf->dmpi->bpp/8*x,
714 src[0], vf->dmpi->bpp/8*w, h, vf->dmpi->stride[0], stride[0]);
715 return;
717 memcpy_pic(vf->dmpi->planes[0]+y*vf->dmpi->stride[0]+x, src[0],
718 w, h, vf->dmpi->stride[0], stride[0]);
719 memcpy_pic(vf->dmpi->planes[1]+(y>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[1]+(x>>vf->dmpi->chroma_x_shift),
720 src[1], w>>vf->dmpi->chroma_x_shift, h>>vf->dmpi->chroma_y_shift, vf->dmpi->stride[1], stride[1]);
721 memcpy_pic(vf->dmpi->planes[2]+(y>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[2]+(x>>vf->dmpi->chroma_x_shift),
722 src[2], w>>vf->dmpi->chroma_x_shift, h>>vf->dmpi->chroma_y_shift, vf->dmpi->stride[2], stride[2]);
725 //============================================================================
727 vf_instance_t *append_filters(vf_instance_t* last,
728 struct m_obj_settings *vf_settings)
730 struct MPOpts *opts = last->opts;
731 vf_instance_t* vf;
732 int i;
734 if(vf_settings) {
735 // We want to add them in the 'right order'
736 for(i = 0 ; vf_settings[i].name ; i++)
737 /* NOP */;
738 for(i-- ; i >= 0 ; i--) {
739 //printf("Open filter %s\n",vf_settings[i].name);
740 vf = vf_open_filter(opts, last,vf_settings[i].name,vf_settings[i].attribs);
741 if(vf) last=vf;
744 return last;
747 //============================================================================
749 void vf_uninit_filter(vf_instance_t* vf){
750 if(vf->uninit) vf->uninit(vf);
751 free_mp_image(vf->imgctx.static_images[0]);
752 free_mp_image(vf->imgctx.static_images[1]);
753 free_mp_image(vf->imgctx.temp_images[0]);
754 free_mp_image(vf->imgctx.export_images[0]);
755 for (int i = 0; i < NUM_NUMBERED_MPI; i++)
756 free_mp_image(vf->imgctx.numbered_images[i]);
757 free(vf);
760 void vf_uninit_filter_chain(vf_instance_t* vf){
761 while(vf){
762 vf_instance_t* next=vf->next;
763 vf_uninit_filter(vf);
764 vf=next;