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