-identify output: print media start time
[mplayer/glamo.git] / libmpcodecs / vf.c
blob217e802a845a457447fb2cebb967a4d25f167230
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"
42 #include "libavutil/mem.h"
44 extern const vf_info_t vf_info_vo;
45 extern const vf_info_t vf_info_rectangle;
46 extern const vf_info_t vf_info_bmovl;
47 extern const vf_info_t vf_info_crop;
48 extern const vf_info_t vf_info_expand;
49 extern const vf_info_t vf_info_pp;
50 extern const vf_info_t vf_info_scale;
51 extern const vf_info_t vf_info_format;
52 extern const vf_info_t vf_info_noformat;
53 extern const vf_info_t vf_info_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 &vf_info_flip,
139 #ifdef CONFIG_LIBSWSCALE_INTERNALS
140 &vf_info_rgb2bgr,
141 #endif
142 &vf_info_rotate,
143 &vf_info_mirror,
144 #ifdef CONFIG_LIBSWSCALE_INTERNALS
145 &vf_info_palette,
146 #endif
147 &vf_info_pp7,
148 #ifdef CONFIG_LIBAVCODEC
149 &vf_info_lavc,
150 &vf_info_lavcdeint,
151 &vf_info_screenshot,
152 &vf_info_uspp,
153 #endif
154 #ifdef CONFIG_ZR
155 &vf_info_zrmjpeg,
156 #endif
157 &vf_info_dvbscale,
158 &vf_info_cropdetect,
159 &vf_info_test,
160 &vf_info_noise,
161 &vf_info_yvu9,
162 &vf_info_eq,
163 &vf_info_eq2,
164 &vf_info_gradfun,
165 #ifdef CONFIG_LIBSWSCALE_INTERNALS
166 &vf_info_halfpack,
167 #endif
168 &vf_info_dint,
169 &vf_info_1bpp,
170 &vf_info_2xsai,
171 &vf_info_unsharp,
172 &vf_info_swapuv,
173 &vf_info_il,
174 &vf_info_fil,
175 &vf_info_boxblur,
176 &vf_info_sab,
177 &vf_info_smartblur,
178 &vf_info_perspective,
179 &vf_info_down3dright,
180 &vf_info_field,
181 &vf_info_denoise3d,
182 &vf_info_hqdn3d,
183 &vf_info_detc,
184 &vf_info_telecine,
185 &vf_info_tinterlace,
186 &vf_info_tfields,
187 &vf_info_ivtc,
188 &vf_info_ilpack,
189 &vf_info_dsize,
190 &vf_info_decimate,
191 &vf_info_softpulldown,
192 &vf_info_pullup,
193 &vf_info_filmdint,
194 &vf_info_framestep,
195 &vf_info_tile,
196 &vf_info_delogo,
197 &vf_info_remove_logo,
198 &vf_info_hue,
199 #ifdef CONFIG_LIBAVCODEC_INTERNALS
200 &vf_info_spp,
201 &vf_info_fspp,
202 &vf_info_qp,
203 &vf_info_mcdeint,
204 &vf_info_geq,
205 #endif
206 &vf_info_yuvcsp,
207 &vf_info_kerndeint,
208 &vf_info_rgbtest,
209 &vf_info_phase,
210 &vf_info_divtc,
211 &vf_info_harddup,
212 &vf_info_softskip,
213 #ifdef CONFIG_ASS
214 &vf_info_ass,
215 #endif
216 &vf_info_yadif,
217 &vf_info_blackframe,
218 &vf_info_ow,
219 &vf_info_fixpts,
220 NULL
223 // For the vf option
224 const m_obj_list_t vf_obj_list = {
225 (void**)filter_list,
226 M_ST_OFF(vf_info_t,name),
227 M_ST_OFF(vf_info_t,info),
228 M_ST_OFF(vf_info_t,opts)
231 //============================================================================
232 // mpi stuff:
234 void vf_mpi_clear(mp_image_t* mpi,int x0,int y0,int w,int h){
235 int y;
236 if(mpi->flags&MP_IMGFLAG_PLANAR){
237 y0&=~1;h+=h&1;
238 if(x0==0 && w==mpi->width){
239 // full width clear:
240 memset(mpi->planes[0]+mpi->stride[0]*y0,0,mpi->stride[0]*h);
241 memset(mpi->planes[1]+mpi->stride[1]*(y0>>mpi->chroma_y_shift),128,mpi->stride[1]*(h>>mpi->chroma_y_shift));
242 memset(mpi->planes[2]+mpi->stride[2]*(y0>>mpi->chroma_y_shift),128,mpi->stride[2]*(h>>mpi->chroma_y_shift));
243 } else
244 for(y=y0;y<y0+h;y+=2){
245 memset(mpi->planes[0]+x0+mpi->stride[0]*y,0,w);
246 memset(mpi->planes[0]+x0+mpi->stride[0]*(y+1),0,w);
247 memset(mpi->planes[1]+(x0>>mpi->chroma_x_shift)+mpi->stride[1]*(y>>mpi->chroma_y_shift),128,(w>>mpi->chroma_x_shift));
248 memset(mpi->planes[2]+(x0>>mpi->chroma_x_shift)+mpi->stride[2]*(y>>mpi->chroma_y_shift),128,(w>>mpi->chroma_x_shift));
250 return;
252 // packed:
253 for(y=y0;y<y0+h;y++){
254 unsigned char* dst=mpi->planes[0]+mpi->stride[0]*y+(mpi->bpp>>3)*x0;
255 if(mpi->flags&MP_IMGFLAG_YUV){
256 unsigned int* p=(unsigned int*) dst;
257 int size=(mpi->bpp>>3)*w/4;
258 int i;
259 #if HAVE_BIGENDIAN
260 #define CLEAR_PACKEDYUV_PATTERN 0x00800080
261 #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x80008000
262 #else
263 #define CLEAR_PACKEDYUV_PATTERN 0x80008000
264 #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x00800080
265 #endif
266 if(mpi->flags&MP_IMGFLAG_SWAPPED){
267 for(i=0;i<size-3;i+=4) p[i]=p[i+1]=p[i+2]=p[i+3]=CLEAR_PACKEDYUV_PATTERN_SWAPPED;
268 for(;i<size;i++) p[i]=CLEAR_PACKEDYUV_PATTERN_SWAPPED;
269 } else {
270 for(i=0;i<size-3;i+=4) p[i]=p[i+1]=p[i+2]=p[i+3]=CLEAR_PACKEDYUV_PATTERN;
271 for(;i<size;i++) p[i]=CLEAR_PACKEDYUV_PATTERN;
273 } else
274 memset(dst,0,(mpi->bpp>>3)*w);
278 mp_image_t* vf_get_image(vf_instance_t* vf, unsigned int outfmt, int mp_imgtype, int mp_imgflag, int w, int h){
279 mp_image_t* mpi=NULL;
280 int w2;
281 int number = mp_imgtype >> 16;
283 #ifdef MP_DEBUG
284 assert(w == -1 || w >= vf->w);
285 assert(h == -1 || h >= vf->h);
286 assert(vf->w > 0);
287 assert(vf->h > 0);
288 #endif
290 // fprintf(stderr, "get_image: %d:%d, vf: %d:%d\n", w,h,vf->w,vf->h);
292 if (w == -1) w = vf->w;
293 if (h == -1) h = vf->h;
295 w2=(mp_imgflag&MP_IMGFLAG_ACCEPT_ALIGNED_STRIDE)?((w+15)&(~15)):w;
297 if(vf->put_image==vf_next_put_image){
298 // passthru mode, if the filter uses the fallback/default put_image() code
299 return vf_get_image(vf->next,outfmt,mp_imgtype,mp_imgflag,w,h);
302 // Note: we should call libvo first to check if it supports direct rendering
303 // and if not, then fallback to software buffers:
304 switch(mp_imgtype & 0xff){
305 case MP_IMGTYPE_EXPORT:
306 if(!vf->imgctx.export_images[0]) vf->imgctx.export_images[0]=new_mp_image(w2,h);
307 mpi=vf->imgctx.export_images[0];
308 break;
309 case MP_IMGTYPE_STATIC:
310 if(!vf->imgctx.static_images[0]) vf->imgctx.static_images[0]=new_mp_image(w2,h);
311 mpi=vf->imgctx.static_images[0];
312 break;
313 case MP_IMGTYPE_TEMP:
314 if(!vf->imgctx.temp_images[0]) vf->imgctx.temp_images[0]=new_mp_image(w2,h);
315 mpi=vf->imgctx.temp_images[0];
316 break;
317 case MP_IMGTYPE_IPB:
318 if(!(mp_imgflag&MP_IMGFLAG_READABLE)){ // B frame:
319 if(!vf->imgctx.temp_images[0]) vf->imgctx.temp_images[0]=new_mp_image(w2,h);
320 mpi=vf->imgctx.temp_images[0];
321 break;
323 case MP_IMGTYPE_IP:
324 if(!vf->imgctx.static_images[vf->imgctx.static_idx]) vf->imgctx.static_images[vf->imgctx.static_idx]=new_mp_image(w2,h);
325 mpi=vf->imgctx.static_images[vf->imgctx.static_idx];
326 vf->imgctx.static_idx^=1;
327 break;
328 case MP_IMGTYPE_NUMBERED:
329 if (number == -1) {
330 int i;
331 for (i = 0; i < NUM_NUMBERED_MPI; i++)
332 if (!vf->imgctx.numbered_images[i] || !vf->imgctx.numbered_images[i]->usage_count)
333 break;
334 number = i;
336 if (number < 0 || number >= NUM_NUMBERED_MPI) return NULL;
337 if (!vf->imgctx.numbered_images[number]) vf->imgctx.numbered_images[number] = new_mp_image(w2,h);
338 mpi = vf->imgctx.numbered_images[number];
339 mpi->number = number;
340 break;
342 if(mpi){
343 mpi->type=mp_imgtype;
344 mpi->w=vf->w; mpi->h=vf->h;
345 // keep buffer allocation status & color flags only:
346 // mpi->flags&=~(MP_IMGFLAG_PRESERVE|MP_IMGFLAG_READABLE|MP_IMGFLAG_DIRECT);
347 mpi->flags&=MP_IMGFLAG_ALLOCATED|MP_IMGFLAG_TYPE_DISPLAYED|MP_IMGFLAGMASK_COLORS;
348 // accept restrictions, draw_slice and palette flags only:
349 mpi->flags|=mp_imgflag&(MP_IMGFLAGMASK_RESTRICTIONS|MP_IMGFLAG_DRAW_CALLBACK|MP_IMGFLAG_RGB_PALETTE);
350 if(!vf->draw_slice) mpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
351 if(mpi->width!=w2 || mpi->height!=h){
352 // printf("vf.c: MPI parameters changed! %dx%d -> %dx%d \n", mpi->width,mpi->height,w2,h);
353 if(mpi->flags&MP_IMGFLAG_ALLOCATED){
354 if(mpi->width<w2 || mpi->height<h){
355 // need to re-allocate buffer memory:
356 av_free(mpi->planes[0]);
357 mpi->flags&=~MP_IMGFLAG_ALLOCATED;
358 mp_msg(MSGT_VFILTER,MSGL_V,"vf.c: have to REALLOCATE buffer memory :(\n");
360 // } else {
362 mpi->width=w2; mpi->chroma_width=(w2 + (1<<mpi->chroma_x_shift) - 1)>>mpi->chroma_x_shift;
363 mpi->height=h; mpi->chroma_height=(h + (1<<mpi->chroma_y_shift) - 1)>>mpi->chroma_y_shift;
366 if(!mpi->bpp) mp_image_setfmt(mpi,outfmt);
367 if(!(mpi->flags&MP_IMGFLAG_ALLOCATED) && mpi->type>MP_IMGTYPE_EXPORT){
369 // check libvo first!
370 if(vf->get_image) vf->get_image(vf,mpi);
372 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
373 // non-direct and not yet allocated image. allocate it!
374 if (!mpi->bpp) { // no way we can allocate this
375 mp_msg(MSGT_DECVIDEO, MSGL_FATAL,
376 "vf_get_image: Tried to allocate a format that can not be allocated!\n");
377 return NULL;
380 // check if codec prefer aligned stride:
381 if(mp_imgflag&MP_IMGFLAG_PREFER_ALIGNED_STRIDE){
382 int align=(mpi->flags&MP_IMGFLAG_PLANAR &&
383 mpi->flags&MP_IMGFLAG_YUV) ?
384 (8<<mpi->chroma_x_shift)-1 : 15; // -- maybe FIXME
385 w2=((w+align)&(~align));
386 if(mpi->width!=w2){
387 // we have to change width... check if we CAN co it:
388 int flags=vf->query_format(vf,outfmt); // should not fail
389 if(!(flags&3)) mp_msg(MSGT_DECVIDEO,MSGL_WARN,"??? vf_get_image{vf->query_format(outfmt)} failed!\n");
390 // printf("query -> 0x%X \n",flags);
391 if(flags&VFCAP_ACCEPT_STRIDE){
392 mpi->width=w2;
393 mpi->chroma_width=(w2 + (1<<mpi->chroma_x_shift) - 1)>>mpi->chroma_x_shift;
398 mp_image_alloc_planes(mpi);
399 // printf("clearing img!\n");
400 vf_mpi_clear(mpi,0,0,mpi->width,mpi->height);
403 if(mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)
404 if(vf->start_slice) vf->start_slice(vf,mpi);
405 if(!(mpi->flags&MP_IMGFLAG_TYPE_DISPLAYED)){
406 mp_msg(MSGT_DECVIDEO,MSGL_V,"*** [%s] %s%s mp_image_t, %dx%dx%dbpp %s %s, %d bytes\n",
407 vf->info->name,
408 (mpi->type==MP_IMGTYPE_EXPORT)?"Exporting":
409 ((mpi->flags&MP_IMGFLAG_DIRECT)?"Direct Rendering":"Allocating"),
410 (mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)?" (slices)":"",
411 mpi->width,mpi->height,mpi->bpp,
412 (mpi->flags&MP_IMGFLAG_YUV)?"YUV":((mpi->flags&MP_IMGFLAG_SWAPPED)?"BGR":"RGB"),
413 (mpi->flags&MP_IMGFLAG_PLANAR)?"planar":"packed",
414 mpi->bpp*mpi->width*mpi->height/8);
415 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",
416 mpi->imgfmt, mpi->planes[0], mpi->planes[1], mpi->planes[2],
417 mpi->stride[0], mpi->stride[1], mpi->stride[2],
418 mpi->chroma_width, mpi->chroma_height, mpi->chroma_x_shift, mpi->chroma_y_shift);
419 mpi->flags|=MP_IMGFLAG_TYPE_DISPLAYED;
422 mpi->qscale = NULL;
424 mpi->usage_count++;
425 // printf("\rVF_MPI: %p %p %p %d %d %d \n",
426 // mpi->planes[0],mpi->planes[1],mpi->planes[2],
427 // mpi->stride[0],mpi->stride[1],mpi->stride[2]);
428 return mpi;
431 //============================================================================
433 // By default vf doesn't accept MPEGPES
434 static int vf_default_query_format(struct vf_instance *vf, unsigned int fmt){
435 if(fmt == IMGFMT_MPEGPES) return 0;
436 return vf_next_query_format(vf,fmt);
439 struct vf_instance *vf_open_plugin_noerr(struct MPOpts *opts,
440 const vf_info_t * const *filter_list,
441 vf_instance_t *next, const char *name,
442 char **args, int *retcode)
444 vf_instance_t* vf;
445 int i;
446 for(i=0;;i++){
447 if(!filter_list[i]){
448 mp_tmsg(MSGT_VFILTER,MSGL_ERR,"Couldn't find video filter '%s'.\n",name);
449 return NULL; // no such filter!
451 if(!strcmp(filter_list[i]->name,name)) break;
453 vf = calloc(1, sizeof *vf);
454 vf->opts = opts;
455 vf->info=filter_list[i];
456 vf->next=next;
457 vf->config=vf_next_config;
458 vf->control=vf_next_control;
459 vf->query_format=vf_default_query_format;
460 vf->put_image=vf_next_put_image;
461 vf->default_caps=VFCAP_ACCEPT_STRIDE;
462 vf->default_reqs=0;
463 if(vf->info->opts) { // vf_vo get some special argument
464 const m_struct_t* st = vf->info->opts;
465 void* vf_priv = m_struct_alloc(st);
466 int n;
467 for(n = 0 ; args && args[2*n] ; n++)
468 m_struct_set(st,vf_priv,args[2*n],args[2*n+1]);
469 vf->priv = vf_priv;
470 args = NULL;
471 } else // Otherwise we should have the '_oldargs_'
472 if(args && !strcmp(args[0],"_oldargs_"))
473 args = (char**)args[1];
474 else
475 args = NULL;
476 *retcode = vf->info->vf_open(vf,(char*)args);
477 if (*retcode > 0)
478 return vf;
479 free(vf);
480 return NULL;
483 struct vf_instance *vf_open_plugin(struct MPOpts *opts,
484 const vf_info_t * const *filter_list,
485 vf_instance_t *next, const char *name,
486 char **args)
488 struct vf_instance *vf = vf_open_plugin_noerr(opts, filter_list, next,
489 name, args, &(int){0});
490 if (!vf)
491 mp_tmsg(MSGT_VFILTER, MSGL_ERR, "Couldn't open video filter '%s'.\n",
492 name);
493 return vf;
496 vf_instance_t* vf_open_filter(struct MPOpts *opts, vf_instance_t* next, const char *name, char **args){
497 if(args && strcmp(args[0],"_oldargs_")) {
498 int i,l = 0;
499 for(i = 0 ; args && args[2*i] ; i++)
500 l += 1 + strlen(args[2*i]) + 1 + strlen(args[2*i+1]);
501 l += strlen(name);
503 char str[l+1];
504 char* p = str;
505 p += sprintf(str,"%s",name);
506 for(i = 0 ; args && args[2*i] ; i++)
507 p += sprintf(p," %s=%s",args[2*i],args[2*i+1]);
508 mp_msg(MSGT_VFILTER, MSGL_INFO, "%s[%s]\n",
509 mp_gtext("Opening video filter: "), str);
511 } else if(strcmp(name,"vo")) {
512 if(args && strcmp(args[0],"_oldargs_") == 0)
513 mp_msg(MSGT_VFILTER, MSGL_INFO, "%s[%s=%s]\n",
514 mp_gtext("Opening video filter: "), name, args[1]);
515 else
516 mp_msg(MSGT_VFILTER, MSGL_INFO, "%s[%s]\n",
517 mp_gtext("Opening video filter: "), name);
519 return vf_open_plugin(opts, filter_list,next,name,args);
523 * \brief adds a filter before the last one (which should be the vo filter).
524 * \param vf start of the filter chain.
525 * \param name name of the filter to add.
526 * \param args argument list for the filter.
527 * \return pointer to the filter instance that was created.
529 vf_instance_t* vf_add_before_vo(vf_instance_t **vf, char *name, char **args) {
530 struct MPOpts *opts = (*vf)->opts;
531 vf_instance_t *vo, *prev = NULL, *new;
532 // Find the last filter (should be vf_vo)
533 for (vo = *vf; vo->next; vo = vo->next)
534 prev = vo;
535 new = vf_open_filter(opts, vo, name, args);
536 if (prev)
537 prev->next = new;
538 else
539 *vf = new;
540 return new;
543 //============================================================================
545 unsigned int vf_match_csp(vf_instance_t** vfp,const unsigned int* list,unsigned int preferred){
546 vf_instance_t* vf=*vfp;
547 struct MPOpts *opts = vf->opts;
548 const unsigned int* p;
549 unsigned int best=0;
550 int ret;
551 if((p=list)) while(*p){
552 ret=vf->query_format(vf,*p);
553 mp_msg(MSGT_VFILTER,MSGL_V,"[%s] query(%s) -> %d\n",vf->info->name,vo_format_name(*p),ret&3);
554 if(ret&2){ best=*p; break;} // no conversion -> bingo!
555 if(ret&1 && !best) best=*p; // best with conversion
556 ++p;
558 if(best) return best; // bingo, they have common csp!
559 // ok, then try with scale:
560 if(vf->info == &vf_info_scale) return 0; // avoid infinite recursion!
561 vf=vf_open_filter(opts, vf,"scale",NULL);
562 if(!vf) return 0; // failed to init "scale"
563 // try the preferred csp first:
564 if(preferred && vf->query_format(vf,preferred)) best=preferred; else
565 // try the list again, now with "scaler" :
566 if((p=list)) while(*p){
567 ret=vf->query_format(vf,*p);
568 mp_msg(MSGT_VFILTER,MSGL_V,"[%s] query(%s) -> %d\n",vf->info->name,vo_format_name(*p),ret&3);
569 if(ret&2){ best=*p; break;} // no conversion -> bingo!
570 if(ret&1 && !best) best=*p; // best with conversion
571 ++p;
573 if(best) *vfp=vf; // else uninit vf !FIXME!
574 return best;
577 void vf_clone_mpi_attributes(mp_image_t* dst, mp_image_t* src){
578 dst->pict_type= src->pict_type;
579 dst->fields = src->fields;
580 dst->qscale_type= src->qscale_type;
581 if(dst->width == src->width && dst->height == src->height){
582 dst->qstride= src->qstride;
583 dst->qscale= src->qscale;
587 void vf_queue_frame(vf_instance_t *vf, int (*func)(vf_instance_t *))
589 vf->continue_buffered_image = func;
592 // Output the next buffered image (if any) from the filter chain.
593 // The queue could be kept as a simple stack/list instead avoiding the
594 // looping here, but there's currently no good context variable where
595 // that could be stored so this was easier to implement.
597 int vf_output_queued_frame(vf_instance_t *vf)
599 while (1) {
600 int ret;
601 vf_instance_t *current;
602 vf_instance_t *last=NULL;
603 int (*tmp)(vf_instance_t *);
604 for (current = vf; current; current = current->next)
605 if (current->continue_buffered_image)
606 last = current;
607 if (!last)
608 return 0;
609 tmp = last->continue_buffered_image;
610 last->continue_buffered_image = NULL;
611 ret = tmp(last);
612 if (ret)
613 return ret;
619 * \brief Video config() function wrapper
621 * Blocks config() calls with different size or format for filters
622 * with VFCAP_CONSTANT
624 * First call is redirected to vf->config.
626 * In following calls, it verifies that the configuration parameters
627 * are unchanged, and returns either success or error.
630 int vf_config_wrapper(struct vf_instance *vf,
631 int width, int height, int d_width, int d_height,
632 unsigned int flags, unsigned int outfmt)
634 int r;
635 if ((vf->default_caps&VFCAP_CONSTANT) && vf->fmt.have_configured) {
636 if ((vf->fmt.orig_width != width)
637 || (vf->fmt.orig_height != height)
638 || (vf->fmt.orig_fmt != outfmt)) {
639 mp_tmsg(MSGT_VFILTER,MSGL_ERR,"\nNew video file has different resolution or colorspace than the previous one.\n");
640 return 0;
642 return 1;
644 vf->fmt.have_configured = 1;
645 vf->fmt.orig_height = height;
646 vf->fmt.orig_width = width;
647 vf->fmt.orig_fmt = outfmt;
648 r = vf->config(vf, width, height, d_width, d_height, flags, outfmt);
649 if (!r) vf->fmt.have_configured = 0;
650 return r;
653 int vf_next_config(struct vf_instance *vf,
654 int width, int height, int d_width, int d_height,
655 unsigned int voflags, unsigned int outfmt){
656 struct MPOpts *opts = vf->opts;
657 int miss;
658 int flags=vf->next->query_format(vf->next,outfmt);
659 if(!flags){
660 // hmm. colorspace mismatch!!!
661 // let's insert the 'scale' filter, it does the job for us:
662 vf_instance_t* vf2;
663 if(vf->next->info==&vf_info_scale) return 0; // scale->scale
664 vf2=vf_open_filter(opts, vf->next,"scale",NULL);
665 if(!vf2) return 0; // shouldn't happen!
666 vf->next=vf2;
667 flags=vf->next->query_format(vf->next,outfmt);
668 if(!flags){
669 mp_tmsg(MSGT_VFILTER,MSGL_ERR,"Cannot find matching colorspace, even by inserting 'scale' :(\n");
670 return 0; // FAIL
673 mp_msg(MSGT_VFILTER,MSGL_V,"REQ: flags=0x%X req=0x%X \n",flags,vf->default_reqs);
674 miss=vf->default_reqs - (flags&vf->default_reqs);
675 if(miss&VFCAP_ACCEPT_STRIDE){
676 // vf requires stride support but vf->next doesn't support it!
677 // let's insert the 'expand' filter, it does the job for us:
678 vf_instance_t* vf2=vf_open_filter(opts, vf->next,"expand",NULL);
679 if(!vf2) return 0; // shouldn't happen!
680 vf->next=vf2;
682 vf->next->w = width; vf->next->h = height;
683 return vf_config_wrapper(vf->next,width,height,d_width,d_height,voflags,outfmt);
686 int vf_next_control(struct vf_instance *vf, int request, void* data){
687 return vf->next->control(vf->next,request,data);
690 int vf_next_query_format(struct vf_instance *vf, unsigned int fmt){
691 int flags=vf->next->query_format(vf->next,fmt);
692 if(flags) flags|=vf->default_caps;
693 return flags;
696 int vf_next_put_image(struct vf_instance *vf,mp_image_t *mpi, double pts){
697 return vf->next->put_image(vf->next,mpi, pts);
700 void vf_next_draw_slice(struct vf_instance *vf,unsigned char** src, int * stride,int w, int h, int x, int y){
701 if (vf->next->draw_slice) {
702 vf->next->draw_slice(vf->next,src,stride,w,h,x,y);
703 return;
705 if (!vf->dmpi) {
706 mp_msg(MSGT_VFILTER,MSGL_ERR,"draw_slice: dmpi not stored by vf_%s\n", vf->info->name);
707 return;
709 if (!(vf->dmpi->flags & MP_IMGFLAG_PLANAR)) {
710 memcpy_pic(vf->dmpi->planes[0]+y*vf->dmpi->stride[0]+vf->dmpi->bpp/8*x,
711 src[0], vf->dmpi->bpp/8*w, h, vf->dmpi->stride[0], stride[0]);
712 return;
714 memcpy_pic(vf->dmpi->planes[0]+y*vf->dmpi->stride[0]+x, src[0],
715 w, h, vf->dmpi->stride[0], stride[0]);
716 memcpy_pic(vf->dmpi->planes[1]+(y>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[1]+(x>>vf->dmpi->chroma_x_shift),
717 src[1], w>>vf->dmpi->chroma_x_shift, h>>vf->dmpi->chroma_y_shift, vf->dmpi->stride[1], stride[1]);
718 memcpy_pic(vf->dmpi->planes[2]+(y>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[2]+(x>>vf->dmpi->chroma_x_shift),
719 src[2], w>>vf->dmpi->chroma_x_shift, h>>vf->dmpi->chroma_y_shift, vf->dmpi->stride[2], stride[2]);
722 //============================================================================
724 vf_instance_t *append_filters(vf_instance_t* last,
725 struct m_obj_settings *vf_settings)
727 struct MPOpts *opts = last->opts;
728 vf_instance_t* vf;
729 int i;
731 if(vf_settings) {
732 // We want to add them in the 'right order'
733 for(i = 0 ; vf_settings[i].name ; i++)
734 /* NOP */;
735 for(i-- ; i >= 0 ; i--) {
736 //printf("Open filter %s\n",vf_settings[i].name);
737 vf = vf_open_filter(opts, last,vf_settings[i].name,vf_settings[i].attribs);
738 if(vf) last=vf;
741 return last;
744 //============================================================================
746 void vf_uninit_filter(vf_instance_t* vf){
747 if(vf->uninit) vf->uninit(vf);
748 free_mp_image(vf->imgctx.static_images[0]);
749 free_mp_image(vf->imgctx.static_images[1]);
750 free_mp_image(vf->imgctx.temp_images[0]);
751 free_mp_image(vf->imgctx.export_images[0]);
752 for (int i = 0; i < NUM_NUMBERED_MPI; i++)
753 free_mp_image(vf->imgctx.numbered_images[i]);
754 free(vf);
757 void vf_uninit_filter_chain(vf_instance_t* vf){
758 while(vf){
759 vf_instance_t* next=vf->next;
760 vf_uninit_filter(vf);
761 vf=next;