cleanup: Silence compilation warnings on MinGW-w64
[mplayer.git] / libmpcodecs / vf.c
blobf4d6768531bb7c6a7b5f53eaf257e2b7ab330b37
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,
137 #ifdef CONFIG_LIBPOSTPROC
138 &vf_info_pp,
139 #endif
140 &vf_info_lavc,
141 &vf_info_lavcdeint,
142 &vf_info_screenshot,
143 &vf_info_fspp,
144 &vf_info_uspp,
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_mcdeint,
189 #endif
190 &vf_info_geq,
191 &vf_info_qp,
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)
223 int y;
224 if (mpi->flags & MP_IMGFLAG_PLANAR) {
225 y0 &= ~1;
226 h += h & 1;
227 if (x0 == 0 && w == mpi->width) {
228 // full width clear:
229 memset(mpi->planes[0] + mpi->stride[0] * y0, 0, mpi->stride[0] * h);
230 memset(mpi->planes[1] + mpi->stride[1] *(y0 >> mpi->chroma_y_shift),
231 128, mpi->stride[1] * (h >> mpi->chroma_y_shift));
232 memset(mpi->planes[2] + mpi->stride[2] *(y0 >> mpi->chroma_y_shift),
233 128, mpi->stride[2] * (h >> mpi->chroma_y_shift));
234 } else
235 for (y = y0; y < y0 + h; y += 2) {
236 memset(mpi->planes[0] + x0 + mpi->stride[0] * y, 0, w);
237 memset(mpi->planes[0] + x0 + mpi->stride[0] * (y + 1), 0, w);
238 memset(mpi->planes[1] + (x0 >> mpi->chroma_x_shift) +
239 mpi->stride[1] * (y >> mpi->chroma_y_shift),
240 128, (w >> mpi->chroma_x_shift));
241 memset(mpi->planes[2] + (x0 >> mpi->chroma_x_shift) +
242 mpi->stride[2] * (y >> mpi->chroma_y_shift),
243 128, (w >> mpi->chroma_x_shift));
245 return;
247 // packed:
248 for (y = y0; y < y0 + h; y++) {
249 unsigned char *dst = mpi->planes[0] + mpi->stride[0] * y +
250 (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)
264 p[i] = p[i + 1] = p[i + 2] = p[i + 3] = CLEAR_PACKEDYUV_PATTERN_SWAPPED;
265 for (; i < size; i++)
266 p[i] = CLEAR_PACKEDYUV_PATTERN_SWAPPED;
267 } else {
268 for (i = 0; i < size - 3; i += 4)
269 p[i] = p[i + 1] = p[i + 2] = p[i + 3] = CLEAR_PACKEDYUV_PATTERN;
270 for (; i < size; i++)
271 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,
279 int mp_imgtype, int mp_imgflag, int w, int h)
281 mp_image_t *mpi = NULL;
282 int w2;
283 int number = mp_imgtype >> 16;
285 assert(w == -1 || w >= vf->w);
286 assert(h == -1 || h >= vf->h);
287 assert(vf->w > 0);
288 assert(vf->h > 0);
290 if (w == -1)
291 w = vf->w;
292 if (h == -1)
293 h = vf->h;
295 w2 = (mp_imgflag & MP_IMGFLAG_ACCEPT_ALIGNED_STRIDE) ?
296 ((w + 15) & (~15)) : w;
298 if (vf->put_image == vf_next_put_image) {
299 // passthru mode, if the filter uses the fallback/default put_image()
300 return vf_get_image(vf->next, outfmt, mp_imgtype, mp_imgflag, w, h);
303 // Note: we should call libvo first to check if it supports direct rendering
304 // and if not, then fallback to software buffers:
305 switch (mp_imgtype & 0xff) {
306 case MP_IMGTYPE_EXPORT:
307 if (!vf->imgctx.export_images[0])
308 vf->imgctx.export_images[0] = new_mp_image(w2, h);
309 mpi = vf->imgctx.export_images[0];
310 break;
311 case MP_IMGTYPE_STATIC:
312 if (!vf->imgctx.static_images[0])
313 vf->imgctx.static_images[0] = new_mp_image(w2, h);
314 mpi = vf->imgctx.static_images[0];
315 break;
316 case MP_IMGTYPE_TEMP:
317 if (!vf->imgctx.temp_images[0])
318 vf->imgctx.temp_images[0] = new_mp_image(w2, h);
319 mpi = vf->imgctx.temp_images[0];
320 break;
321 case MP_IMGTYPE_IPB:
322 if (!(mp_imgflag & MP_IMGFLAG_READABLE)) { // B frame:
323 if (!vf->imgctx.temp_images[0])
324 vf->imgctx.temp_images[0] = new_mp_image(w2, h);
325 mpi = vf->imgctx.temp_images[0];
326 break;
328 case MP_IMGTYPE_IP:
329 if (!vf->imgctx.static_images[vf->imgctx.static_idx])
330 vf->imgctx.static_images[vf->imgctx.static_idx] = new_mp_image(w2, h);
331 mpi = vf->imgctx.static_images[vf->imgctx.static_idx];
332 vf->imgctx.static_idx ^= 1;
333 break;
334 case MP_IMGTYPE_NUMBERED:
335 if (number == -1) {
336 int i;
337 for (i = 0; i < NUM_NUMBERED_MPI; i++)
338 if (!vf->imgctx.numbered_images[i] ||
339 !vf->imgctx.numbered_images[i]->usage_count)
340 break;
341 number = i;
343 if (number < 0 || number >= NUM_NUMBERED_MPI)
344 return NULL;
345 if (!vf->imgctx.numbered_images[number])
346 vf->imgctx.numbered_images[number] = new_mp_image(w2, h);
347 mpi = vf->imgctx.numbered_images[number];
348 mpi->number = number;
349 break;
351 if (mpi) {
352 mpi->type = mp_imgtype;
353 mpi->w = vf->w;
354 mpi->h = vf->h;
355 // keep buffer allocation status & color flags only:
356 mpi->flags &= MP_IMGFLAG_ALLOCATED | MP_IMGFLAG_TYPE_DISPLAYED |
357 MP_IMGFLAGMASK_COLORS;
358 // accept restrictions, draw_slice and palette flags only:
359 mpi->flags |= mp_imgflag & (MP_IMGFLAGMASK_RESTRICTIONS |
360 MP_IMGFLAG_DRAW_CALLBACK | MP_IMGFLAG_RGB_PALETTE);
361 if (!vf->draw_slice)
362 mpi->flags &= ~MP_IMGFLAG_DRAW_CALLBACK;
363 if (mpi->width != w2 || mpi->height != h) {
364 if (mpi->flags & MP_IMGFLAG_ALLOCATED) {
365 if (mpi->width < w2 || mpi->height < h) {
366 // need to re-allocate buffer memory:
367 av_free(mpi->planes[0]);
368 mpi->flags &= ~MP_IMGFLAG_ALLOCATED;
369 mp_msg(MSGT_VFILTER, MSGL_V,
370 "vf.c: have to REALLOCATE buffer memory :(\n");
373 mpi->width = w2;
374 mpi->chroma_width = (w2 + (1 << mpi->chroma_x_shift) - 1) >>
375 mpi->chroma_x_shift;
376 mpi->height = h;
377 mpi->chroma_height = (h + (1 << mpi->chroma_y_shift) - 1) >>
378 mpi->chroma_y_shift;
380 if (!mpi->bpp)
381 mp_image_setfmt(mpi, outfmt);
382 if (!(mpi->flags & MP_IMGFLAG_ALLOCATED) &&
383 mpi->type > MP_IMGTYPE_EXPORT) {
384 // check libvo first!
385 if (vf->get_image)
386 vf->get_image(vf, mpi);
388 if (!(mpi->flags & MP_IMGFLAG_DIRECT)) {
389 // non-direct and not yet allocated image. allocate it!
390 if (!mpi->bpp) { // no way we can allocate this
391 mp_msg(MSGT_DECVIDEO, MSGL_FATAL,
392 "vf_get_image: Tried to allocate a format that "
393 "can not be allocated!\n");
394 return NULL;
397 // check if codec prefer aligned stride:
398 if (mp_imgflag & MP_IMGFLAG_PREFER_ALIGNED_STRIDE) {
399 int align = (mpi->flags & MP_IMGFLAG_PLANAR &&
400 mpi->flags & MP_IMGFLAG_YUV) ?
401 (8 << mpi->chroma_x_shift) - 1 : 15; // OK?
402 w2 = ((w + align) & (~align));
403 if (mpi->width != w2) {
404 // we have to change width... check if we CAN co it:
405 int flags = vf->query_format(vf, outfmt);
406 // should not fail
407 if (!(flags & (VFCAP_CSP_SUPPORTED |
408 VFCAP_CSP_SUPPORTED_BY_HW)))
409 mp_msg(MSGT_DECVIDEO, MSGL_WARN,
410 "??? vf_get_image{vf->query_format(outfmt)} "
411 "failed!\n");
412 if (flags & VFCAP_ACCEPT_STRIDE) {
413 mpi->width = w2;
414 mpi->chroma_width =
415 (w2 + (1 << mpi->chroma_x_shift) - 1) >>
416 mpi->chroma_x_shift;
421 mp_image_alloc_planes(mpi);
422 vf_mpi_clear(mpi, 0, 0, mpi->width, mpi->height);
425 if (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)
426 if (vf->start_slice)
427 vf->start_slice(vf, mpi);
428 if (!(mpi->flags & MP_IMGFLAG_TYPE_DISPLAYED)) {
429 mp_msg(MSGT_DECVIDEO, MSGL_V,
430 "*** [%s] %s%s mp_image_t, %dx%dx%dbpp %s %s, %d bytes\n",
431 vf->info->name,
432 (mpi->type == MP_IMGTYPE_EXPORT) ? "Exporting" :
433 ((mpi->flags & MP_IMGFLAG_DIRECT) ?
434 "Direct Rendering" : "Allocating"),
435 (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK) ? " (slices)" : "",
436 mpi->width, mpi->height, mpi->bpp,
437 (mpi->flags & MP_IMGFLAG_YUV) ? "YUV" :
438 ((mpi->flags & MP_IMGFLAG_SWAPPED) ? "BGR" : "RGB"),
439 (mpi->flags & MP_IMGFLAG_PLANAR) ? "planar" : "packed",
440 mpi->bpp * mpi->width * mpi->height / 8);
441 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "(imgfmt: %x, planes: %p,%p,%p "
442 "strides: %d,%d,%d, chroma: %dx%d, shift: h:%d,v:%d)\n",
443 mpi->imgfmt, mpi->planes[0], mpi->planes[1], mpi->planes[2],
444 mpi->stride[0], mpi->stride[1], mpi->stride[2],
445 mpi->chroma_width, mpi->chroma_height,
446 mpi->chroma_x_shift, mpi->chroma_y_shift);
447 mpi->flags |= MP_IMGFLAG_TYPE_DISPLAYED;
449 mpi->qscale = NULL;
451 mpi->usage_count++;
452 return mpi;
455 //============================================================================
457 // By default vf doesn't accept MPEGPES
458 static int vf_default_query_format(struct vf_instance *vf, unsigned int fmt)
460 if (fmt == IMGFMT_MPEGPES)
461 return 0;
462 return vf_next_query_format(vf, fmt);
465 struct vf_instance *vf_open_plugin_noerr(struct MPOpts *opts,
466 const vf_info_t *const *filter_list,
467 vf_instance_t *next, const char *name,
468 char **args, int *retcode)
470 vf_instance_t *vf;
471 int i;
472 for (i = 0;; i++) {
473 if (!filter_list[i]) {
474 mp_tmsg(MSGT_VFILTER, MSGL_ERR,
475 "Couldn't find video filter '%s'.\n", name);
476 return NULL; // no such filter!
478 if (!strcmp(filter_list[i]->name, name))
479 break;
481 vf = calloc(1, sizeof *vf);
482 vf->opts = opts;
483 vf->info = filter_list[i];
484 vf->next = next;
485 vf->config = vf_next_config;
486 vf->control = vf_next_control;
487 vf->query_format = vf_default_query_format;
488 vf->put_image = vf_next_put_image;
489 vf->default_caps = VFCAP_ACCEPT_STRIDE;
490 vf->default_reqs = 0;
491 if (vf->info->opts) { // vf_vo get some special argument
492 const m_struct_t *st = vf->info->opts;
493 void *vf_priv = m_struct_alloc(st);
494 int n;
495 for (n = 0; args && args[2 * n]; n++)
496 m_struct_set(st, vf_priv, args[2 * n], bstr(args[2 * n + 1]));
497 vf->priv = vf_priv;
498 args = NULL;
499 } else // Otherwise we should have the '_oldargs_'
500 if (args && !strcmp(args[0], "_oldargs_"))
501 args = (char **)args[1];
502 else
503 args = NULL;
504 *retcode = vf->info->vf_open(vf, (char *)args);
505 if (*retcode > 0)
506 return vf;
507 free(vf);
508 return NULL;
511 struct vf_instance *vf_open_plugin(struct MPOpts *opts,
512 const vf_info_t *const *filter_list,
513 vf_instance_t *next, const char *name,
514 char **args)
516 struct vf_instance *vf = vf_open_plugin_noerr(opts, filter_list, next,
517 name, args, &(int){0});
518 if (!vf)
519 mp_tmsg(MSGT_VFILTER, MSGL_ERR, "Couldn't open video filter '%s'.\n",
520 name);
521 return vf;
524 vf_instance_t *vf_open_filter(struct MPOpts *opts, vf_instance_t *next,
525 const char *name, char **args)
527 if (args && strcmp(args[0], "_oldargs_")) {
528 int i, l = 0;
529 for (i = 0; args && args[2 * i]; i++)
530 l += 1 + strlen(args[2 * i]) + 1 + strlen(args[2 * i + 1]);
531 l += strlen(name);
533 char str[l + 1];
534 char *p = str;
535 p += sprintf(str, "%s", name);
536 for (i = 0; args && args[2 * i]; i++)
537 p += sprintf(p, " %s=%s", args[2 * i], args[2 * i + 1]);
538 mp_msg(MSGT_VFILTER, MSGL_INFO, "%s[%s]\n",
539 mp_gtext("Opening video filter: "), str);
541 } else if (strcmp(name, "vo")) {
542 if (args && strcmp(args[0], "_oldargs_") == 0)
543 mp_msg(MSGT_VFILTER, MSGL_INFO, "%s[%s=%s]\n",
544 mp_gtext("Opening video filter: "), name, args[1]);
545 else
546 mp_msg(MSGT_VFILTER, MSGL_INFO, "%s[%s]\n",
547 mp_gtext("Opening video filter: "), name);
549 return vf_open_plugin(opts, filter_list, next, name, args);
553 * \brief adds a filter before the last one (which should be the vo filter).
554 * \param vf start of the filter chain.
555 * \param name name of the filter to add.
556 * \param args argument list for the filter.
557 * \return pointer to the filter instance that was created.
559 vf_instance_t *vf_add_before_vo(vf_instance_t **vf, char *name, char **args)
561 struct MPOpts *opts = (*vf)->opts;
562 vf_instance_t *vo, *prev = NULL, *new;
563 // Find the last filter (should be vf_vo)
564 for (vo = *vf; vo->next; vo = vo->next)
565 prev = vo;
566 new = vf_open_filter(opts, vo, name, args);
567 if (prev)
568 prev->next = new;
569 else
570 *vf = new;
571 return new;
574 //============================================================================
576 unsigned int vf_match_csp(vf_instance_t **vfp, const unsigned int *list,
577 unsigned int preferred)
579 vf_instance_t *vf = *vfp;
580 struct MPOpts *opts = vf->opts;
581 const unsigned int *p;
582 unsigned int best = 0;
583 int ret;
584 if ((p = list))
585 while (*p) {
586 ret = vf->query_format(vf, *p);
587 mp_msg(MSGT_VFILTER, MSGL_V, "[%s] query(%s) -> %x\n",
588 vf->info->name, vo_format_name(*p), ret);
589 if (ret & VFCAP_CSP_SUPPORTED_BY_HW) {
590 best = *p;
591 break;
593 if (ret & VFCAP_CSP_SUPPORTED && !best)
594 best = *p;
595 ++p;
597 if (best)
598 return best; // bingo, they have common csp!
599 // ok, then try with scale:
600 if (vf->info == &vf_info_scale)
601 return 0; // avoid infinite recursion!
602 vf = vf_open_filter(opts, vf, "scale", NULL);
603 if (!vf)
604 return 0; // failed to init "scale"
605 // try the preferred csp first:
606 if (preferred && vf->query_format(vf, preferred))
607 best = preferred;
608 else
609 // try the list again, now with "scaler" :
610 if ((p = list))
611 while (*p) {
612 ret = vf->query_format(vf, *p);
613 mp_msg(MSGT_VFILTER, MSGL_V, "[%s] query(%s) -> %x\n",
614 vf->info->name, vo_format_name(*p), ret);
615 if (ret & VFCAP_CSP_SUPPORTED_BY_HW) {
616 best = *p;
617 break;
619 if (ret & VFCAP_CSP_SUPPORTED && !best)
620 best = *p;
621 ++p;
623 if (best)
624 *vfp = vf; // else uninit vf !FIXME!
625 return best;
628 void vf_clone_mpi_attributes(mp_image_t *dst, mp_image_t *src)
630 dst->pict_type = src->pict_type;
631 dst->fields = src->fields;
632 dst->qscale_type = src->qscale_type;
633 if (dst->width == src->width && dst->height == src->height) {
634 dst->qstride = src->qstride;
635 dst->qscale = src->qscale;
639 void vf_queue_frame(vf_instance_t *vf, int (*func)(vf_instance_t *))
641 vf->continue_buffered_image = func;
644 // Output the next buffered image (if any) from the filter chain.
645 // The queue could be kept as a simple stack/list instead avoiding the
646 // looping here, but there's currently no good context variable where
647 // that could be stored so this was easier to implement.
649 int vf_output_queued_frame(vf_instance_t *vf)
651 while (1) {
652 int ret;
653 vf_instance_t *current;
654 vf_instance_t *last = NULL;
655 int (*tmp)(vf_instance_t *);
656 for (current = vf; current; current = current->next)
657 if (current->continue_buffered_image)
658 last = current;
659 if (!last)
660 return 0;
661 tmp = last->continue_buffered_image;
662 last->continue_buffered_image = NULL;
663 ret = tmp(last);
664 if (ret)
665 return ret;
671 * \brief Video config() function wrapper
673 * Blocks config() calls with different size or format for filters
674 * with VFCAP_CONSTANT
676 * First call is redirected to vf->config.
678 * In following calls, it verifies that the configuration parameters
679 * are unchanged, and returns either success or error.
682 int vf_config_wrapper(struct vf_instance *vf,
683 int width, int height, int d_width, int d_height,
684 unsigned int flags, unsigned int outfmt)
686 int r;
687 if ((vf->default_caps & VFCAP_CONSTANT) && vf->fmt.have_configured) {
688 if ((vf->fmt.orig_width != width)
689 || (vf->fmt.orig_height != height)
690 || (vf->fmt.orig_fmt != outfmt)) {
691 mp_tmsg(MSGT_VFILTER, MSGL_ERR, "\nNew video file has different "
692 "resolution or colorspace than the previous one.\n");
693 return 0;
695 return 1;
697 vf->fmt.have_configured = 1;
698 vf->fmt.orig_height = height;
699 vf->fmt.orig_width = width;
700 vf->fmt.orig_fmt = outfmt;
701 r = vf->config(vf, width, height, d_width, d_height, flags, outfmt);
702 if (!r)
703 vf->fmt.have_configured = 0;
704 return r;
707 int vf_next_config(struct vf_instance *vf,
708 int width, int height, int d_width, int d_height,
709 unsigned int voflags, unsigned int outfmt)
711 struct MPOpts *opts = vf->opts;
712 int miss;
713 int flags = vf->next->query_format(vf->next, outfmt);
714 if (!flags) {
715 // hmm. colorspace mismatch!!!
716 // let's insert the 'scale' filter, it does the job for us:
717 vf_instance_t *vf2;
718 if (vf->next->info == &vf_info_scale)
719 return 0; // scale->scale
720 vf2 = vf_open_filter(opts, vf->next, "scale", NULL);
721 if (!vf2)
722 return 0; // shouldn't happen!
723 vf->next = vf2;
724 flags = vf->next->query_format(vf->next, outfmt);
725 if (!flags) {
726 mp_tmsg(MSGT_VFILTER, MSGL_ERR, "Cannot find matching colorspace, "
727 "even by inserting 'scale' :(\n");
728 return 0; // FAIL
731 mp_msg(MSGT_VFILTER, MSGL_V, "REQ: flags=0x%X req=0x%X \n",
732 flags, vf->default_reqs);
733 miss = vf->default_reqs - (flags & vf->default_reqs);
734 if (miss & VFCAP_ACCEPT_STRIDE) {
735 // vf requires stride support but vf->next doesn't support it!
736 // let's insert the 'expand' filter, it does the job for us:
737 vf_instance_t *vf2 = vf_open_filter(opts, vf->next, "expand", NULL);
738 if (!vf2)
739 return 0; // shouldn't happen!
740 vf->next = vf2;
742 vf->next->w = width;
743 vf->next->h = height;
744 return vf_config_wrapper(vf->next, width, height, d_width, d_height,
745 voflags, outfmt);
748 int vf_next_control(struct vf_instance *vf, int request, void *data)
750 return vf->next->control(vf->next, request, data);
753 int vf_next_query_format(struct vf_instance *vf, unsigned int fmt)
755 int flags = vf->next->query_format(vf->next, fmt);
756 if (flags)
757 flags |= vf->default_caps;
758 return flags;
761 int vf_next_put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
763 return vf->next->put_image(vf->next, mpi, pts);
766 void vf_next_draw_slice(struct vf_instance *vf, unsigned char **src,
767 int *stride, int w, int h, int x, int y)
769 if (vf->next->draw_slice) {
770 vf->next->draw_slice(vf->next, src, stride, w, h, x, y);
771 return;
773 if (!vf->dmpi) {
774 mp_msg(MSGT_VFILTER, MSGL_ERR,
775 "draw_slice: dmpi not stored by vf_%s\n", vf->info->name);
776 return;
778 if (!(vf->dmpi->flags & MP_IMGFLAG_PLANAR)) {
779 memcpy_pic(vf->dmpi->planes[0] + y * vf->dmpi->stride[0] +
780 vf->dmpi->bpp / 8 * x,
781 src[0], vf->dmpi->bpp / 8 * w, h, vf->dmpi->stride[0],
782 stride[0]);
783 return;
785 memcpy_pic(vf->dmpi->planes[0] + y * vf->dmpi->stride[0] + x, src[0],
786 w, h, vf->dmpi->stride[0], stride[0]);
787 memcpy_pic(vf->dmpi->planes[1]
788 + (y >> vf->dmpi->chroma_y_shift) * vf->dmpi->stride[1]
789 + (x >> vf->dmpi->chroma_x_shift),
790 src[1], w >> vf->dmpi->chroma_x_shift,
791 h >> vf->dmpi->chroma_y_shift, vf->dmpi->stride[1], stride[1]);
792 memcpy_pic(vf->dmpi->planes[2]
793 + (y >> vf->dmpi->chroma_y_shift) * vf->dmpi->stride[2]
794 + (x >> vf->dmpi->chroma_x_shift),
795 src[2], w >> vf->dmpi->chroma_x_shift,
796 h >> vf->dmpi->chroma_y_shift, vf->dmpi->stride[2], stride[2]);
799 //============================================================================
801 vf_instance_t *append_filters(vf_instance_t *last,
802 struct m_obj_settings *vf_settings)
804 struct MPOpts *opts = last->opts;
805 vf_instance_t *vf;
806 int i;
808 if (vf_settings) {
809 // We want to add them in the 'right order'
810 for (i = 0; vf_settings[i].name; i++)
811 /* NOP */;
812 for (i--; i >= 0; i--) {
813 //printf("Open filter %s\n",vf_settings[i].name);
814 vf = vf_open_filter(opts, last, vf_settings[i].name,
815 vf_settings[i].attribs);
816 if (vf)
817 last = vf;
820 return last;
823 //============================================================================
825 void vf_uninit_filter(vf_instance_t *vf)
827 if (vf->uninit)
828 vf->uninit(vf);
829 free_mp_image(vf->imgctx.static_images[0]);
830 free_mp_image(vf->imgctx.static_images[1]);
831 free_mp_image(vf->imgctx.temp_images[0]);
832 free_mp_image(vf->imgctx.export_images[0]);
833 for (int i = 0; i < NUM_NUMBERED_MPI; i++)
834 free_mp_image(vf->imgctx.numbered_images[i]);
835 free(vf);
838 void vf_uninit_filter_chain(vf_instance_t *vf)
840 while (vf) {
841 vf_instance_t *next = vf->next;
842 vf_uninit_filter(vf);
843 vf = next;
847 void vf_detc_init_pts_buf(struct vf_detc_pts_buf *p)
849 p->inpts_prev = MP_NOPTS_VALUE;
850 p->outpts_prev = MP_NOPTS_VALUE;
851 p->lastdelta = 0;
854 static double vf_detc_adjust_pts_internal(struct vf_detc_pts_buf *p,
855 double pts, bool reset_pattern,
856 bool skip_frame, double delta,
857 double boundfactor_minus,
858 double increasefactor,
859 double boundfactor_plus)
861 double newpts;
863 if (pts == MP_NOPTS_VALUE)
864 return pts;
866 if (delta <= 0) {
867 if (p->inpts_prev == MP_NOPTS_VALUE)
868 delta = 0;
869 else if (pts == p->inpts_prev)
870 delta = p->lastdelta;
871 else
872 delta = pts - p->inpts_prev;
874 p->inpts_prev = pts;
875 p->lastdelta = delta;
877 if (skip_frame)
878 return MP_NOPTS_VALUE;
880 /* detect bogus deltas and then passthru pts (possibly caused by seeking,
881 * or bad input) */
882 if (p->outpts_prev == MP_NOPTS_VALUE || reset_pattern || delta <= 0.0 ||
883 delta >= 0.5)
884 newpts = pts;
885 else {
886 // turn 5 frames into 4
887 newpts = p->outpts_prev + delta * increasefactor;
889 // bound to input pts in a sensible way; these numbers come because we
890 // map frames the following way when ivtc'ing:
891 // 0/30 -> 0/24 diff=0
892 // 1/30 -> 1/24 diff=1/120
893 // 2/30 -> -
894 // 3/30 -> 2/24 diff=-1/60
895 // 4/30 -> 3/24 diff=-1/120
896 if (newpts < pts - delta * boundfactor_minus)
897 newpts = pts - delta * boundfactor_minus;
898 if (newpts > pts + delta * boundfactor_plus)
899 newpts = pts + delta * boundfactor_plus;
900 if (newpts < p->outpts_prev)
901 newpts = p->outpts_prev; // damage control
903 p->outpts_prev = newpts;
905 return newpts;
908 double vf_detc_adjust_pts(struct vf_detc_pts_buf *p, double pts,
909 bool reset_pattern, bool skip_frame)
911 // standard telecine (see above)
912 return vf_detc_adjust_pts_internal(p, pts, reset_pattern, skip_frame,
913 0, 0.5, 1.25, 0.25);
916 double vf_softpulldown_adjust_pts(struct vf_detc_pts_buf *p, double pts,
917 bool reset_pattern, bool skip_frame,
918 int last_frame_duration)
920 // for the softpulldown filter we get:
921 // 0/60 -> 0/30
922 // 2/60 -> 1/30
923 // 5/60 -> 2/30
924 // 7/60 -> 3/30, 4/30
925 return vf_detc_adjust_pts_internal(p, pts, reset_pattern, skip_frame,
926 0, 1.0 / last_frame_duration,
927 2.0 / last_frame_duration,
928 1.0 / last_frame_duration);