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