vo_svga: fix build after previous commit 6506d4ad
[mplayer/greg.git] / libmpcodecs / vf.c
blobeefdaa3c461c0a92937169c04c5f9cf5fe2808e8
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_rotate;
55 extern const vf_info_t vf_info_mirror;
56 extern const vf_info_t vf_info_palette;
57 extern const vf_info_t vf_info_lavc;
58 extern const vf_info_t vf_info_dvbscale;
59 extern const vf_info_t vf_info_cropdetect;
60 extern const vf_info_t vf_info_test;
61 extern const vf_info_t vf_info_noise;
62 extern const vf_info_t vf_info_yvu9;
63 extern const vf_info_t vf_info_lavcdeint;
64 extern const vf_info_t vf_info_eq;
65 extern const vf_info_t vf_info_eq2;
66 extern const vf_info_t vf_info_gradfun;
67 extern const vf_info_t vf_info_halfpack;
68 extern const vf_info_t vf_info_dint;
69 extern const vf_info_t vf_info_1bpp;
70 extern const vf_info_t vf_info_2xsai;
71 extern const vf_info_t vf_info_unsharp;
72 extern const vf_info_t vf_info_swapuv;
73 extern const vf_info_t vf_info_il;
74 extern const vf_info_t vf_info_fil;
75 extern const vf_info_t vf_info_boxblur;
76 extern const vf_info_t vf_info_sab;
77 extern const vf_info_t vf_info_smartblur;
78 extern const vf_info_t vf_info_perspective;
79 extern const vf_info_t vf_info_down3dright;
80 extern const vf_info_t vf_info_field;
81 extern const vf_info_t vf_info_denoise3d;
82 extern const vf_info_t vf_info_hqdn3d;
83 extern const vf_info_t vf_info_detc;
84 extern const vf_info_t vf_info_telecine;
85 extern const vf_info_t vf_info_tinterlace;
86 extern const vf_info_t vf_info_tfields;
87 extern const vf_info_t vf_info_ivtc;
88 extern const vf_info_t vf_info_ilpack;
89 extern const vf_info_t vf_info_dsize;
90 extern const vf_info_t vf_info_decimate;
91 extern const vf_info_t vf_info_softpulldown;
92 extern const vf_info_t vf_info_pullup;
93 extern const vf_info_t vf_info_filmdint;
94 extern const vf_info_t vf_info_framestep;
95 extern const vf_info_t vf_info_tile;
96 extern const vf_info_t vf_info_delogo;
97 extern const vf_info_t vf_info_remove_logo;
98 extern const vf_info_t vf_info_hue;
99 extern const vf_info_t vf_info_spp;
100 extern const vf_info_t vf_info_uspp;
101 extern const vf_info_t vf_info_fspp;
102 extern const vf_info_t vf_info_pp7;
103 extern const vf_info_t vf_info_yuvcsp;
104 extern const vf_info_t vf_info_kerndeint;
105 extern const vf_info_t vf_info_rgbtest;
106 extern const vf_info_t vf_info_qp;
107 extern const vf_info_t vf_info_phase;
108 extern const vf_info_t vf_info_divtc;
109 extern const vf_info_t vf_info_harddup;
110 extern const vf_info_t vf_info_softskip;
111 extern const vf_info_t vf_info_screenshot;
112 extern const vf_info_t vf_info_ass;
113 extern const vf_info_t vf_info_mcdeint;
114 extern const vf_info_t vf_info_yadif;
115 extern const vf_info_t vf_info_blackframe;
116 extern const vf_info_t vf_info_geq;
117 extern const vf_info_t vf_info_ow;
118 extern const vf_info_t vf_info_fixpts;
119 extern const vf_info_t vf_info_stereo3d;
121 // list of available filters:
122 static const vf_info_t* const filter_list[]={
123 &vf_info_rectangle,
124 #ifdef HAVE_POSIX_SELECT
125 &vf_info_bmovl,
126 #endif
127 &vf_info_crop,
128 &vf_info_expand,
129 &vf_info_scale,
130 // &vf_info_osd,
131 &vf_info_vo,
132 &vf_info_format,
133 &vf_info_noformat,
134 &vf_info_flip,
135 &vf_info_rotate,
136 &vf_info_mirror,
137 &vf_info_palette,
138 &vf_info_pp7,
139 #ifdef CONFIG_FFMPEG
140 &vf_info_pp,
141 &vf_info_lavc,
142 &vf_info_lavcdeint,
143 &vf_info_screenshot,
144 &vf_info_uspp,
145 #endif
146 &vf_info_dvbscale,
147 &vf_info_cropdetect,
148 &vf_info_test,
149 &vf_info_noise,
150 &vf_info_yvu9,
151 &vf_info_eq,
152 &vf_info_eq2,
153 &vf_info_gradfun,
154 &vf_info_halfpack,
155 &vf_info_dint,
156 &vf_info_1bpp,
157 &vf_info_2xsai,
158 &vf_info_unsharp,
159 &vf_info_swapuv,
160 &vf_info_il,
161 &vf_info_fil,
162 &vf_info_boxblur,
163 &vf_info_sab,
164 &vf_info_smartblur,
165 &vf_info_perspective,
166 &vf_info_down3dright,
167 &vf_info_field,
168 &vf_info_denoise3d,
169 &vf_info_hqdn3d,
170 &vf_info_detc,
171 &vf_info_telecine,
172 &vf_info_tinterlace,
173 &vf_info_tfields,
174 &vf_info_ivtc,
175 &vf_info_ilpack,
176 &vf_info_dsize,
177 &vf_info_decimate,
178 &vf_info_softpulldown,
179 &vf_info_pullup,
180 &vf_info_filmdint,
181 &vf_info_framestep,
182 &vf_info_tile,
183 &vf_info_delogo,
184 &vf_info_remove_logo,
185 &vf_info_hue,
186 #ifdef CONFIG_FFMPEG_INTERNALS
187 &vf_info_spp,
188 &vf_info_fspp,
189 &vf_info_qp,
190 &vf_info_mcdeint,
191 #endif
192 #ifdef CONFIG_FFMPEG_EVAL_API
193 &vf_info_geq,
194 #endif
195 &vf_info_yuvcsp,
196 &vf_info_kerndeint,
197 &vf_info_rgbtest,
198 &vf_info_phase,
199 &vf_info_divtc,
200 &vf_info_harddup,
201 &vf_info_softskip,
202 #ifdef CONFIG_ASS
203 &vf_info_ass,
204 #endif
205 &vf_info_yadif,
206 &vf_info_blackframe,
207 &vf_info_ow,
208 &vf_info_fixpts,
209 &vf_info_stereo3d,
210 NULL
213 // For the vf option
214 const m_obj_list_t vf_obj_list = {
215 (void**)filter_list,
216 M_ST_OFF(vf_info_t,name),
217 M_ST_OFF(vf_info_t,info),
218 M_ST_OFF(vf_info_t,opts)
221 //============================================================================
222 // mpi stuff:
224 void vf_mpi_clear(mp_image_t* mpi,int x0,int y0,int w,int h){
225 int y;
226 if(mpi->flags&MP_IMGFLAG_PLANAR){
227 y0&=~1;h+=h&1;
228 if(x0==0 && w==mpi->width){
229 // full width clear:
230 memset(mpi->planes[0]+mpi->stride[0]*y0,0,mpi->stride[0]*h);
231 memset(mpi->planes[1]+mpi->stride[1]*(y0>>mpi->chroma_y_shift),128,mpi->stride[1]*(h>>mpi->chroma_y_shift));
232 memset(mpi->planes[2]+mpi->stride[2]*(y0>>mpi->chroma_y_shift),128,mpi->stride[2]*(h>>mpi->chroma_y_shift));
233 } else
234 for(y=y0;y<y0+h;y+=2){
235 memset(mpi->planes[0]+x0+mpi->stride[0]*y,0,w);
236 memset(mpi->planes[0]+x0+mpi->stride[0]*(y+1),0,w);
237 memset(mpi->planes[1]+(x0>>mpi->chroma_x_shift)+mpi->stride[1]*(y>>mpi->chroma_y_shift),128,(w>>mpi->chroma_x_shift));
238 memset(mpi->planes[2]+(x0>>mpi->chroma_x_shift)+mpi->stride[2]*(y>>mpi->chroma_y_shift),128,(w>>mpi->chroma_x_shift));
240 return;
242 // packed:
243 for(y=y0;y<y0+h;y++){
244 unsigned char* dst=mpi->planes[0]+mpi->stride[0]*y+(mpi->bpp>>3)*x0;
245 if(mpi->flags&MP_IMGFLAG_YUV){
246 unsigned int* p=(unsigned int*) dst;
247 int size=(mpi->bpp>>3)*w/4;
248 int i;
249 #if HAVE_BIGENDIAN
250 #define CLEAR_PACKEDYUV_PATTERN 0x00800080
251 #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x80008000
252 #else
253 #define CLEAR_PACKEDYUV_PATTERN 0x80008000
254 #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x00800080
255 #endif
256 if(mpi->flags&MP_IMGFLAG_SWAPPED){
257 for(i=0;i<size-3;i+=4) p[i]=p[i+1]=p[i+2]=p[i+3]=CLEAR_PACKEDYUV_PATTERN_SWAPPED;
258 for(;i<size;i++) p[i]=CLEAR_PACKEDYUV_PATTERN_SWAPPED;
259 } else {
260 for(i=0;i<size-3;i+=4) p[i]=p[i+1]=p[i+2]=p[i+3]=CLEAR_PACKEDYUV_PATTERN;
261 for(;i<size;i++) p[i]=CLEAR_PACKEDYUV_PATTERN;
263 } else
264 memset(dst,0,(mpi->bpp>>3)*w);
268 mp_image_t* vf_get_image(vf_instance_t* vf, unsigned int outfmt, int mp_imgtype, int mp_imgflag, int w, int h){
269 mp_image_t* mpi=NULL;
270 int w2;
271 int number = mp_imgtype >> 16;
273 #ifdef MP_DEBUG
274 assert(w == -1 || w >= vf->w);
275 assert(h == -1 || h >= vf->h);
276 assert(vf->w > 0);
277 assert(vf->h > 0);
278 #endif
280 // fprintf(stderr, "get_image: %d:%d, vf: %d:%d\n", w,h,vf->w,vf->h);
282 if (w == -1) w = vf->w;
283 if (h == -1) h = vf->h;
285 w2=(mp_imgflag&MP_IMGFLAG_ACCEPT_ALIGNED_STRIDE)?((w+15)&(~15)):w;
287 if(vf->put_image==vf_next_put_image){
288 // passthru mode, if the filter uses the fallback/default put_image() code
289 return vf_get_image(vf->next,outfmt,mp_imgtype,mp_imgflag,w,h);
292 // Note: we should call libvo first to check if it supports direct rendering
293 // and if not, then fallback to software buffers:
294 switch(mp_imgtype & 0xff){
295 case MP_IMGTYPE_EXPORT:
296 if(!vf->imgctx.export_images[0]) vf->imgctx.export_images[0]=new_mp_image(w2,h);
297 mpi=vf->imgctx.export_images[0];
298 break;
299 case MP_IMGTYPE_STATIC:
300 if(!vf->imgctx.static_images[0]) vf->imgctx.static_images[0]=new_mp_image(w2,h);
301 mpi=vf->imgctx.static_images[0];
302 break;
303 case MP_IMGTYPE_TEMP:
304 if(!vf->imgctx.temp_images[0]) vf->imgctx.temp_images[0]=new_mp_image(w2,h);
305 mpi=vf->imgctx.temp_images[0];
306 break;
307 case MP_IMGTYPE_IPB:
308 if(!(mp_imgflag&MP_IMGFLAG_READABLE)){ // B frame:
309 if(!vf->imgctx.temp_images[0]) vf->imgctx.temp_images[0]=new_mp_image(w2,h);
310 mpi=vf->imgctx.temp_images[0];
311 break;
313 case MP_IMGTYPE_IP:
314 if(!vf->imgctx.static_images[vf->imgctx.static_idx]) vf->imgctx.static_images[vf->imgctx.static_idx]=new_mp_image(w2,h);
315 mpi=vf->imgctx.static_images[vf->imgctx.static_idx];
316 vf->imgctx.static_idx^=1;
317 break;
318 case MP_IMGTYPE_NUMBERED:
319 if (number == -1) {
320 int i;
321 for (i = 0; i < NUM_NUMBERED_MPI; i++)
322 if (!vf->imgctx.numbered_images[i] || !vf->imgctx.numbered_images[i]->usage_count)
323 break;
324 number = i;
326 if (number < 0 || number >= NUM_NUMBERED_MPI) return NULL;
327 if (!vf->imgctx.numbered_images[number]) vf->imgctx.numbered_images[number] = new_mp_image(w2,h);
328 mpi = vf->imgctx.numbered_images[number];
329 mpi->number = number;
330 break;
332 if(mpi){
333 mpi->type=mp_imgtype;
334 mpi->w=vf->w; mpi->h=vf->h;
335 // keep buffer allocation status & color flags only:
336 // mpi->flags&=~(MP_IMGFLAG_PRESERVE|MP_IMGFLAG_READABLE|MP_IMGFLAG_DIRECT);
337 mpi->flags&=MP_IMGFLAG_ALLOCATED|MP_IMGFLAG_TYPE_DISPLAYED|MP_IMGFLAGMASK_COLORS;
338 // accept restrictions, draw_slice and palette flags only:
339 mpi->flags|=mp_imgflag&(MP_IMGFLAGMASK_RESTRICTIONS|MP_IMGFLAG_DRAW_CALLBACK|MP_IMGFLAG_RGB_PALETTE);
340 if(!vf->draw_slice) mpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
341 if(mpi->width!=w2 || mpi->height!=h){
342 // printf("vf.c: MPI parameters changed! %dx%d -> %dx%d \n", mpi->width,mpi->height,w2,h);
343 if(mpi->flags&MP_IMGFLAG_ALLOCATED){
344 if(mpi->width<w2 || mpi->height<h){
345 // need to re-allocate buffer memory:
346 av_free(mpi->planes[0]);
347 mpi->flags&=~MP_IMGFLAG_ALLOCATED;
348 mp_msg(MSGT_VFILTER,MSGL_V,"vf.c: have to REALLOCATE buffer memory :(\n");
350 // } else {
352 mpi->width=w2; mpi->chroma_width=(w2 + (1<<mpi->chroma_x_shift) - 1)>>mpi->chroma_x_shift;
353 mpi->height=h; mpi->chroma_height=(h + (1<<mpi->chroma_y_shift) - 1)>>mpi->chroma_y_shift;
356 if(!mpi->bpp) mp_image_setfmt(mpi,outfmt);
357 if(!(mpi->flags&MP_IMGFLAG_ALLOCATED) && mpi->type>MP_IMGTYPE_EXPORT){
359 // check libvo first!
360 if(vf->get_image) vf->get_image(vf,mpi);
362 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
363 // non-direct and not yet allocated image. allocate it!
364 if (!mpi->bpp) { // no way we can allocate this
365 mp_msg(MSGT_DECVIDEO, MSGL_FATAL,
366 "vf_get_image: Tried to allocate a format that can not be allocated!\n");
367 return NULL;
370 // check if codec prefer aligned stride:
371 if(mp_imgflag&MP_IMGFLAG_PREFER_ALIGNED_STRIDE){
372 int align=(mpi->flags&MP_IMGFLAG_PLANAR &&
373 mpi->flags&MP_IMGFLAG_YUV) ?
374 (8<<mpi->chroma_x_shift)-1 : 15; // -- maybe FIXME
375 w2=((w+align)&(~align));
376 if(mpi->width!=w2){
377 // we have to change width... check if we CAN co it:
378 int flags=vf->query_format(vf,outfmt); // should not fail
379 if(!(flags&3)) mp_msg(MSGT_DECVIDEO,MSGL_WARN,"??? vf_get_image{vf->query_format(outfmt)} failed!\n");
380 // printf("query -> 0x%X \n",flags);
381 if(flags&VFCAP_ACCEPT_STRIDE){
382 mpi->width=w2;
383 mpi->chroma_width=(w2 + (1<<mpi->chroma_x_shift) - 1)>>mpi->chroma_x_shift;
388 mp_image_alloc_planes(mpi);
389 // printf("clearing img!\n");
390 vf_mpi_clear(mpi,0,0,mpi->width,mpi->height);
393 if(mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)
394 if(vf->start_slice) vf->start_slice(vf,mpi);
395 if(!(mpi->flags&MP_IMGFLAG_TYPE_DISPLAYED)){
396 mp_msg(MSGT_DECVIDEO,MSGL_V,"*** [%s] %s%s mp_image_t, %dx%dx%dbpp %s %s, %d bytes\n",
397 vf->info->name,
398 (mpi->type==MP_IMGTYPE_EXPORT)?"Exporting":
399 ((mpi->flags&MP_IMGFLAG_DIRECT)?"Direct Rendering":"Allocating"),
400 (mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)?" (slices)":"",
401 mpi->width,mpi->height,mpi->bpp,
402 (mpi->flags&MP_IMGFLAG_YUV)?"YUV":((mpi->flags&MP_IMGFLAG_SWAPPED)?"BGR":"RGB"),
403 (mpi->flags&MP_IMGFLAG_PLANAR)?"planar":"packed",
404 mpi->bpp*mpi->width*mpi->height/8);
405 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",
406 mpi->imgfmt, mpi->planes[0], mpi->planes[1], mpi->planes[2],
407 mpi->stride[0], mpi->stride[1], mpi->stride[2],
408 mpi->chroma_width, mpi->chroma_height, mpi->chroma_x_shift, mpi->chroma_y_shift);
409 mpi->flags|=MP_IMGFLAG_TYPE_DISPLAYED;
412 mpi->qscale = NULL;
414 mpi->usage_count++;
415 // printf("\rVF_MPI: %p %p %p %d %d %d \n",
416 // mpi->planes[0],mpi->planes[1],mpi->planes[2],
417 // mpi->stride[0],mpi->stride[1],mpi->stride[2]);
418 return mpi;
421 //============================================================================
423 // By default vf doesn't accept MPEGPES
424 static int vf_default_query_format(struct vf_instance *vf, unsigned int fmt){
425 if(fmt == IMGFMT_MPEGPES) return 0;
426 return vf_next_query_format(vf,fmt);
429 struct vf_instance *vf_open_plugin_noerr(struct MPOpts *opts,
430 const vf_info_t * const *filter_list,
431 vf_instance_t *next, const char *name,
432 char **args, int *retcode)
434 vf_instance_t* vf;
435 int i;
436 for(i=0;;i++){
437 if(!filter_list[i]){
438 mp_tmsg(MSGT_VFILTER,MSGL_ERR,"Couldn't find video filter '%s'.\n",name);
439 return NULL; // no such filter!
441 if(!strcmp(filter_list[i]->name,name)) break;
443 vf = calloc(1, sizeof *vf);
444 vf->opts = opts;
445 vf->info=filter_list[i];
446 vf->next=next;
447 vf->config=vf_next_config;
448 vf->control=vf_next_control;
449 vf->query_format=vf_default_query_format;
450 vf->put_image=vf_next_put_image;
451 vf->default_caps=VFCAP_ACCEPT_STRIDE;
452 vf->default_reqs=0;
453 if(vf->info->opts) { // vf_vo get some special argument
454 const m_struct_t* st = vf->info->opts;
455 void* vf_priv = m_struct_alloc(st);
456 int n;
457 for(n = 0 ; args && args[2*n] ; n++)
458 m_struct_set(st,vf_priv,args[2*n],args[2*n+1]);
459 vf->priv = vf_priv;
460 args = NULL;
461 } else // Otherwise we should have the '_oldargs_'
462 if(args && !strcmp(args[0],"_oldargs_"))
463 args = (char**)args[1];
464 else
465 args = NULL;
466 *retcode = vf->info->vf_open(vf,(char*)args);
467 if (*retcode > 0)
468 return vf;
469 free(vf);
470 return NULL;
473 struct vf_instance *vf_open_plugin(struct MPOpts *opts,
474 const vf_info_t * const *filter_list,
475 vf_instance_t *next, const char *name,
476 char **args)
478 struct vf_instance *vf = vf_open_plugin_noerr(opts, filter_list, next,
479 name, args, &(int){0});
480 if (!vf)
481 mp_tmsg(MSGT_VFILTER, MSGL_ERR, "Couldn't open video filter '%s'.\n",
482 name);
483 return vf;
486 vf_instance_t* vf_open_filter(struct MPOpts *opts, vf_instance_t* next, const char *name, char **args){
487 if(args && strcmp(args[0],"_oldargs_")) {
488 int i,l = 0;
489 for(i = 0 ; args && args[2*i] ; i++)
490 l += 1 + strlen(args[2*i]) + 1 + strlen(args[2*i+1]);
491 l += strlen(name);
493 char str[l+1];
494 char* p = str;
495 p += sprintf(str,"%s",name);
496 for(i = 0 ; args && args[2*i] ; i++)
497 p += sprintf(p," %s=%s",args[2*i],args[2*i+1]);
498 mp_msg(MSGT_VFILTER, MSGL_INFO, "%s[%s]\n",
499 mp_gtext("Opening video filter: "), str);
501 } else if(strcmp(name,"vo")) {
502 if(args && strcmp(args[0],"_oldargs_") == 0)
503 mp_msg(MSGT_VFILTER, MSGL_INFO, "%s[%s=%s]\n",
504 mp_gtext("Opening video filter: "), name, args[1]);
505 else
506 mp_msg(MSGT_VFILTER, MSGL_INFO, "%s[%s]\n",
507 mp_gtext("Opening video filter: "), name);
509 return vf_open_plugin(opts, filter_list,next,name,args);
513 * \brief adds a filter before the last one (which should be the vo filter).
514 * \param vf start of the filter chain.
515 * \param name name of the filter to add.
516 * \param args argument list for the filter.
517 * \return pointer to the filter instance that was created.
519 vf_instance_t* vf_add_before_vo(vf_instance_t **vf, char *name, char **args) {
520 struct MPOpts *opts = (*vf)->opts;
521 vf_instance_t *vo, *prev = NULL, *new;
522 // Find the last filter (should be vf_vo)
523 for (vo = *vf; vo->next; vo = vo->next)
524 prev = vo;
525 new = vf_open_filter(opts, vo, name, args);
526 if (prev)
527 prev->next = new;
528 else
529 *vf = new;
530 return new;
533 //============================================================================
535 unsigned int vf_match_csp(vf_instance_t** vfp,const unsigned int* list,unsigned int preferred){
536 vf_instance_t* vf=*vfp;
537 struct MPOpts *opts = vf->opts;
538 const unsigned int* p;
539 unsigned int best=0;
540 int ret;
541 if((p=list)) while(*p){
542 ret=vf->query_format(vf,*p);
543 mp_msg(MSGT_VFILTER,MSGL_V,"[%s] query(%s) -> %d\n",vf->info->name,vo_format_name(*p),ret&3);
544 if(ret&2){ best=*p; break;} // no conversion -> bingo!
545 if(ret&1 && !best) best=*p; // best with conversion
546 ++p;
548 if(best) return best; // bingo, they have common csp!
549 // ok, then try with scale:
550 if(vf->info == &vf_info_scale) return 0; // avoid infinite recursion!
551 vf=vf_open_filter(opts, vf,"scale",NULL);
552 if(!vf) return 0; // failed to init "scale"
553 // try the preferred csp first:
554 if(preferred && vf->query_format(vf,preferred)) best=preferred; else
555 // try the list again, now with "scaler" :
556 if((p=list)) while(*p){
557 ret=vf->query_format(vf,*p);
558 mp_msg(MSGT_VFILTER,MSGL_V,"[%s] query(%s) -> %d\n",vf->info->name,vo_format_name(*p),ret&3);
559 if(ret&2){ best=*p; break;} // no conversion -> bingo!
560 if(ret&1 && !best) best=*p; // best with conversion
561 ++p;
563 if(best) *vfp=vf; // else uninit vf !FIXME!
564 return best;
567 void vf_clone_mpi_attributes(mp_image_t* dst, mp_image_t* src){
568 dst->pict_type= src->pict_type;
569 dst->fields = src->fields;
570 dst->qscale_type= src->qscale_type;
571 if(dst->width == src->width && dst->height == src->height){
572 dst->qstride= src->qstride;
573 dst->qscale= src->qscale;
577 void vf_queue_frame(vf_instance_t *vf, int (*func)(vf_instance_t *))
579 vf->continue_buffered_image = func;
582 // Output the next buffered image (if any) from the filter chain.
583 // The queue could be kept as a simple stack/list instead avoiding the
584 // looping here, but there's currently no good context variable where
585 // that could be stored so this was easier to implement.
587 int vf_output_queued_frame(vf_instance_t *vf)
589 while (1) {
590 int ret;
591 vf_instance_t *current;
592 vf_instance_t *last=NULL;
593 int (*tmp)(vf_instance_t *);
594 for (current = vf; current; current = current->next)
595 if (current->continue_buffered_image)
596 last = current;
597 if (!last)
598 return 0;
599 tmp = last->continue_buffered_image;
600 last->continue_buffered_image = NULL;
601 ret = tmp(last);
602 if (ret)
603 return ret;
609 * \brief Video config() function wrapper
611 * Blocks config() calls with different size or format for filters
612 * with VFCAP_CONSTANT
614 * First call is redirected to vf->config.
616 * In following calls, it verifies that the configuration parameters
617 * are unchanged, and returns either success or error.
620 int vf_config_wrapper(struct vf_instance *vf,
621 int width, int height, int d_width, int d_height,
622 unsigned int flags, unsigned int outfmt)
624 int r;
625 if ((vf->default_caps&VFCAP_CONSTANT) && vf->fmt.have_configured) {
626 if ((vf->fmt.orig_width != width)
627 || (vf->fmt.orig_height != height)
628 || (vf->fmt.orig_fmt != outfmt)) {
629 mp_tmsg(MSGT_VFILTER,MSGL_ERR,"\nNew video file has different resolution or colorspace than the previous one.\n");
630 return 0;
632 return 1;
634 vf->fmt.have_configured = 1;
635 vf->fmt.orig_height = height;
636 vf->fmt.orig_width = width;
637 vf->fmt.orig_fmt = outfmt;
638 r = vf->config(vf, width, height, d_width, d_height, flags, outfmt);
639 if (!r) vf->fmt.have_configured = 0;
640 return r;
643 int vf_next_config(struct vf_instance *vf,
644 int width, int height, int d_width, int d_height,
645 unsigned int voflags, unsigned int outfmt){
646 struct MPOpts *opts = vf->opts;
647 int miss;
648 int flags=vf->next->query_format(vf->next,outfmt);
649 if(!flags){
650 // hmm. colorspace mismatch!!!
651 // let's insert the 'scale' filter, it does the job for us:
652 vf_instance_t* vf2;
653 if(vf->next->info==&vf_info_scale) return 0; // scale->scale
654 vf2=vf_open_filter(opts, vf->next,"scale",NULL);
655 if(!vf2) return 0; // shouldn't happen!
656 vf->next=vf2;
657 flags=vf->next->query_format(vf->next,outfmt);
658 if(!flags){
659 mp_tmsg(MSGT_VFILTER,MSGL_ERR,"Cannot find matching colorspace, even by inserting 'scale' :(\n");
660 return 0; // FAIL
663 mp_msg(MSGT_VFILTER,MSGL_V,"REQ: flags=0x%X req=0x%X \n",flags,vf->default_reqs);
664 miss=vf->default_reqs - (flags&vf->default_reqs);
665 if(miss&VFCAP_ACCEPT_STRIDE){
666 // vf requires stride support but vf->next doesn't support it!
667 // let's insert the 'expand' filter, it does the job for us:
668 vf_instance_t* vf2=vf_open_filter(opts, vf->next,"expand",NULL);
669 if(!vf2) return 0; // shouldn't happen!
670 vf->next=vf2;
672 vf->next->w = width; vf->next->h = height;
673 return vf_config_wrapper(vf->next,width,height,d_width,d_height,voflags,outfmt);
676 int vf_next_control(struct vf_instance *vf, int request, void* data){
677 return vf->next->control(vf->next,request,data);
680 int vf_next_query_format(struct vf_instance *vf, unsigned int fmt){
681 int flags=vf->next->query_format(vf->next,fmt);
682 if(flags) flags|=vf->default_caps;
683 return flags;
686 int vf_next_put_image(struct vf_instance *vf,mp_image_t *mpi, double pts){
687 return vf->next->put_image(vf->next,mpi, pts);
690 void vf_next_draw_slice(struct vf_instance *vf,unsigned char** src, int * stride,int w, int h, int x, int y){
691 if (vf->next->draw_slice) {
692 vf->next->draw_slice(vf->next,src,stride,w,h,x,y);
693 return;
695 if (!vf->dmpi) {
696 mp_msg(MSGT_VFILTER,MSGL_ERR,"draw_slice: dmpi not stored by vf_%s\n", vf->info->name);
697 return;
699 if (!(vf->dmpi->flags & MP_IMGFLAG_PLANAR)) {
700 memcpy_pic(vf->dmpi->planes[0]+y*vf->dmpi->stride[0]+vf->dmpi->bpp/8*x,
701 src[0], vf->dmpi->bpp/8*w, h, vf->dmpi->stride[0], stride[0]);
702 return;
704 memcpy_pic(vf->dmpi->planes[0]+y*vf->dmpi->stride[0]+x, src[0],
705 w, h, vf->dmpi->stride[0], stride[0]);
706 memcpy_pic(vf->dmpi->planes[1]+(y>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[1]+(x>>vf->dmpi->chroma_x_shift),
707 src[1], w>>vf->dmpi->chroma_x_shift, h>>vf->dmpi->chroma_y_shift, vf->dmpi->stride[1], stride[1]);
708 memcpy_pic(vf->dmpi->planes[2]+(y>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[2]+(x>>vf->dmpi->chroma_x_shift),
709 src[2], w>>vf->dmpi->chroma_x_shift, h>>vf->dmpi->chroma_y_shift, vf->dmpi->stride[2], stride[2]);
712 //============================================================================
714 vf_instance_t *append_filters(vf_instance_t* last,
715 struct m_obj_settings *vf_settings)
717 struct MPOpts *opts = last->opts;
718 vf_instance_t* vf;
719 int i;
721 if(vf_settings) {
722 // We want to add them in the 'right order'
723 for(i = 0 ; vf_settings[i].name ; i++)
724 /* NOP */;
725 for(i-- ; i >= 0 ; i--) {
726 //printf("Open filter %s\n",vf_settings[i].name);
727 vf = vf_open_filter(opts, last,vf_settings[i].name,vf_settings[i].attribs);
728 if(vf) last=vf;
731 return last;
734 //============================================================================
736 void vf_uninit_filter(vf_instance_t* vf){
737 if(vf->uninit) vf->uninit(vf);
738 free_mp_image(vf->imgctx.static_images[0]);
739 free_mp_image(vf->imgctx.static_images[1]);
740 free_mp_image(vf->imgctx.temp_images[0]);
741 free_mp_image(vf->imgctx.export_images[0]);
742 for (int i = 0; i < NUM_NUMBERED_MPI; i++)
743 free_mp_image(vf->imgctx.numbered_images[i]);
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;
755 void vf_detc_init_pts_buf(struct vf_detc_pts_buf *p)
757 p->inpts_prev = MP_NOPTS_VALUE;
758 p->outpts_prev = MP_NOPTS_VALUE;
759 p->lastdelta = 0;
762 static double vf_detc_adjust_pts_internal(struct vf_detc_pts_buf *p,
763 double pts, bool reset_pattern,
764 bool skip_frame, double delta,
765 double boundfactor_minus,
766 double increasefactor,
767 double boundfactor_plus)
769 double newpts;
771 if (pts == MP_NOPTS_VALUE)
772 return pts;
774 if (delta <= 0) {
775 if (p->inpts_prev == MP_NOPTS_VALUE)
776 delta = 0;
777 else if(pts == p->inpts_prev)
778 delta = p->lastdelta;
779 else
780 delta = pts - p->inpts_prev;
782 //mp_msg(MSGT_VFILTER, MSGL_INFO, "filmdint: (1) inpts %f (delta: %f, increase: %f)\n", pts, delta, delta * increasefactor);
783 p->inpts_prev = pts;
784 p->lastdelta = delta;
786 if (skip_frame)
787 return MP_NOPTS_VALUE;
789 // detect bogus deltas and then passthru pts (possibly caused by seeking, or bad input)
790 if (p->outpts_prev == MP_NOPTS_VALUE || reset_pattern || delta <= 0.0 || delta >= 0.5) {
791 newpts = pts;
792 } else {
793 // turn 5 frames into 4
794 newpts = p->outpts_prev + delta * increasefactor;
796 // bound to input pts in a sensible way; these numbers come because we
797 // map frames the following way when ivtc'ing:
798 // 0/30 -> 0/24 diff=0
799 // 1/30 -> 1/24 diff=1/120
800 // 2/30 -> -
801 // 3/30 -> 2/24 diff=-1/60
802 // 4/30 -> 3/24 diff=-1/120
803 if (newpts < pts - delta * boundfactor_minus)
804 newpts = pts - delta * boundfactor_minus;
805 if (newpts > pts + delta * boundfactor_plus)
806 newpts = pts + delta * boundfactor_plus;
807 if (newpts < p->outpts_prev)
808 newpts = p->outpts_prev; // damage control
810 //mp_msg(MSGT_VFILTER, MSGL_INFO, "filmdint: (2) outpts %f (delta: %f)\n", newpts, newpts - p->outpts_prev);
811 p->outpts_prev = newpts;
813 return newpts;
816 double vf_detc_adjust_pts(struct vf_detc_pts_buf *p, double pts,
817 bool reset_pattern, bool skip_frame)
819 // standard telecine (see above)
820 return vf_detc_adjust_pts_internal(p, pts, reset_pattern, skip_frame,
821 0, 0.5, 1.25, 0.25);
824 double vf_softpulldown_adjust_pts(struct vf_detc_pts_buf *p, double pts,
825 bool reset_pattern, bool skip_frame,
826 int last_frame_duration)
828 // for the softpulldown filter we get:
829 // 0/60 -> 0/30
830 // 2/60 -> 1/30
831 // 5/60 -> 2/30
832 // 7/60 -> 3/30, 4/30
833 return vf_detc_adjust_pts_internal(p, pts, reset_pattern, skip_frame,
834 0, 1.0 / last_frame_duration,
835 2.0 / last_frame_duration,
836 1.0 / last_frame_duration);