build: clean up remnants from internal FFmpeg build
[mplayer.git] / libmpcodecs / vf.c
blobe5ea6808ef502506dff3918e0dfa0163a1b82b50
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"
26 #include "mp_msg.h"
27 #include "m_option.h"
28 #include "m_struct.h"
31 #include "img_format.h"
32 #include "mp_image.h"
33 #include "vf.h"
35 #include "libvo/fastmemcpy.h"
36 #include "libavutil/mem.h"
38 extern const vf_info_t vf_info_vo;
39 extern const vf_info_t vf_info_rectangle;
40 extern const vf_info_t vf_info_bmovl;
41 extern const vf_info_t vf_info_crop;
42 extern const vf_info_t vf_info_expand;
43 extern const vf_info_t vf_info_pp;
44 extern const vf_info_t vf_info_scale;
45 extern const vf_info_t vf_info_format;
46 extern const vf_info_t vf_info_noformat;
47 extern const vf_info_t vf_info_flip;
48 extern const vf_info_t vf_info_rotate;
49 extern const vf_info_t vf_info_mirror;
50 extern const vf_info_t vf_info_palette;
51 extern const vf_info_t vf_info_lavc;
52 extern const vf_info_t vf_info_dvbscale;
53 extern const vf_info_t vf_info_cropdetect;
54 extern const vf_info_t vf_info_test;
55 extern const vf_info_t vf_info_noise;
56 extern const vf_info_t vf_info_yvu9;
57 extern const vf_info_t vf_info_lavcdeint;
58 extern const vf_info_t vf_info_eq;
59 extern const vf_info_t vf_info_eq2;
60 extern const vf_info_t vf_info_gradfun;
61 extern const vf_info_t vf_info_halfpack;
62 extern const vf_info_t vf_info_dint;
63 extern const vf_info_t vf_info_1bpp;
64 extern const vf_info_t vf_info_2xsai;
65 extern const vf_info_t vf_info_unsharp;
66 extern const vf_info_t vf_info_swapuv;
67 extern const vf_info_t vf_info_il;
68 extern const vf_info_t vf_info_fil;
69 extern const vf_info_t vf_info_boxblur;
70 extern const vf_info_t vf_info_sab;
71 extern const vf_info_t vf_info_smartblur;
72 extern const vf_info_t vf_info_perspective;
73 extern const vf_info_t vf_info_down3dright;
74 extern const vf_info_t vf_info_field;
75 extern const vf_info_t vf_info_denoise3d;
76 extern const vf_info_t vf_info_hqdn3d;
77 extern const vf_info_t vf_info_detc;
78 extern const vf_info_t vf_info_telecine;
79 extern const vf_info_t vf_info_tinterlace;
80 extern const vf_info_t vf_info_tfields;
81 extern const vf_info_t vf_info_ivtc;
82 extern const vf_info_t vf_info_ilpack;
83 extern const vf_info_t vf_info_dsize;
84 extern const vf_info_t vf_info_decimate;
85 extern const vf_info_t vf_info_softpulldown;
86 extern const vf_info_t vf_info_pullup;
87 extern const vf_info_t vf_info_filmdint;
88 extern const vf_info_t vf_info_framestep;
89 extern const vf_info_t vf_info_tile;
90 extern const vf_info_t vf_info_delogo;
91 extern const vf_info_t vf_info_remove_logo;
92 extern const vf_info_t vf_info_hue;
93 extern const vf_info_t vf_info_uspp;
94 extern const vf_info_t vf_info_fspp;
95 extern const vf_info_t vf_info_pp7;
96 extern const vf_info_t vf_info_yuvcsp;
97 extern const vf_info_t vf_info_kerndeint;
98 extern const vf_info_t vf_info_rgbtest;
99 extern const vf_info_t vf_info_qp;
100 extern const vf_info_t vf_info_phase;
101 extern const vf_info_t vf_info_divtc;
102 extern const vf_info_t vf_info_harddup;
103 extern const vf_info_t vf_info_softskip;
104 extern const vf_info_t vf_info_screenshot;
105 extern const vf_info_t vf_info_ass;
106 extern const vf_info_t vf_info_yadif;
107 extern const vf_info_t vf_info_blackframe;
108 extern const vf_info_t vf_info_geq;
109 extern const vf_info_t vf_info_ow;
110 extern const vf_info_t vf_info_fixpts;
111 extern const vf_info_t vf_info_stereo3d;
113 // list of available filters:
114 static const vf_info_t *const filter_list[] = {
115 &vf_info_rectangle,
116 #ifdef HAVE_POSIX_SELECT
117 &vf_info_bmovl,
118 #endif
119 &vf_info_crop,
120 &vf_info_expand,
121 &vf_info_scale,
122 // &vf_info_osd,
123 &vf_info_vo,
124 &vf_info_format,
125 &vf_info_noformat,
126 &vf_info_flip,
127 &vf_info_rotate,
128 &vf_info_mirror,
129 &vf_info_palette,
130 &vf_info_pp7,
132 #ifdef CONFIG_LIBPOSTPROC
133 &vf_info_pp,
134 #endif
135 &vf_info_lavc,
136 &vf_info_lavcdeint,
137 &vf_info_screenshot,
138 &vf_info_fspp,
139 &vf_info_uspp,
141 &vf_info_dvbscale,
142 &vf_info_cropdetect,
143 &vf_info_test,
144 &vf_info_noise,
145 &vf_info_yvu9,
146 &vf_info_eq,
147 &vf_info_eq2,
148 &vf_info_gradfun,
149 &vf_info_halfpack,
150 &vf_info_dint,
151 &vf_info_1bpp,
152 &vf_info_2xsai,
153 &vf_info_unsharp,
154 &vf_info_swapuv,
155 &vf_info_il,
156 &vf_info_fil,
157 &vf_info_boxblur,
158 &vf_info_sab,
159 &vf_info_smartblur,
160 &vf_info_perspective,
161 &vf_info_down3dright,
162 &vf_info_field,
163 &vf_info_denoise3d,
164 &vf_info_hqdn3d,
165 &vf_info_detc,
166 &vf_info_telecine,
167 &vf_info_tinterlace,
168 &vf_info_tfields,
169 &vf_info_ivtc,
170 &vf_info_ilpack,
171 &vf_info_dsize,
172 &vf_info_decimate,
173 &vf_info_softpulldown,
174 &vf_info_pullup,
175 &vf_info_filmdint,
176 &vf_info_framestep,
177 &vf_info_tile,
178 &vf_info_delogo,
179 &vf_info_remove_logo,
180 &vf_info_hue,
181 &vf_info_geq,
182 &vf_info_qp,
183 &vf_info_yuvcsp,
184 &vf_info_kerndeint,
185 &vf_info_rgbtest,
186 &vf_info_phase,
187 &vf_info_divtc,
188 &vf_info_harddup,
189 &vf_info_softskip,
190 #ifdef CONFIG_ASS
191 &vf_info_ass,
192 #endif
193 &vf_info_yadif,
194 &vf_info_blackframe,
195 &vf_info_ow,
196 &vf_info_fixpts,
197 &vf_info_stereo3d,
198 NULL
201 // For the vf option
202 const m_obj_list_t vf_obj_list = {
203 (void **)filter_list,
204 M_ST_OFF(vf_info_t, name),
205 M_ST_OFF(vf_info_t, info),
206 M_ST_OFF(vf_info_t, opts)
209 //============================================================================
210 // mpi stuff:
212 void vf_mpi_clear(mp_image_t *mpi, int x0, int y0, int w, int h)
214 int y;
215 if (mpi->flags & MP_IMGFLAG_PLANAR) {
216 y0 &= ~1;
217 h += h & 1;
218 if (x0 == 0 && w == mpi->width) {
219 // full width clear:
220 memset(mpi->planes[0] + mpi->stride[0] * y0, 0, mpi->stride[0] * h);
221 memset(mpi->planes[1] + mpi->stride[1] *(y0 >> mpi->chroma_y_shift),
222 128, mpi->stride[1] * (h >> mpi->chroma_y_shift));
223 memset(mpi->planes[2] + mpi->stride[2] *(y0 >> mpi->chroma_y_shift),
224 128, mpi->stride[2] * (h >> mpi->chroma_y_shift));
225 } else
226 for (y = y0; y < y0 + h; y += 2) {
227 memset(mpi->planes[0] + x0 + mpi->stride[0] * y, 0, w);
228 memset(mpi->planes[0] + x0 + mpi->stride[0] * (y + 1), 0, w);
229 memset(mpi->planes[1] + (x0 >> mpi->chroma_x_shift) +
230 mpi->stride[1] * (y >> mpi->chroma_y_shift),
231 128, (w >> mpi->chroma_x_shift));
232 memset(mpi->planes[2] + (x0 >> mpi->chroma_x_shift) +
233 mpi->stride[2] * (y >> mpi->chroma_y_shift),
234 128, (w >> mpi->chroma_x_shift));
236 return;
238 // packed:
239 for (y = y0; y < y0 + h; y++) {
240 unsigned char *dst = mpi->planes[0] + mpi->stride[0] * y +
241 (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)
255 p[i] = p[i + 1] = p[i + 2] = p[i + 3] = CLEAR_PACKEDYUV_PATTERN_SWAPPED;
256 for (; i < size; i++)
257 p[i] = CLEAR_PACKEDYUV_PATTERN_SWAPPED;
258 } else {
259 for (i = 0; i < size - 3; i += 4)
260 p[i] = p[i + 1] = p[i + 2] = p[i + 3] = CLEAR_PACKEDYUV_PATTERN;
261 for (; i < size; i++)
262 p[i] = CLEAR_PACKEDYUV_PATTERN;
264 } else
265 memset(dst, 0, (mpi->bpp >> 3) * w);
269 mp_image_t *vf_get_image(vf_instance_t *vf, unsigned int outfmt,
270 int mp_imgtype, int mp_imgflag, int w, int h)
272 mp_image_t *mpi = NULL;
273 int w2;
274 int number = mp_imgtype >> 16;
276 assert(w == -1 || w >= vf->w);
277 assert(h == -1 || h >= vf->h);
278 assert(vf->w > 0);
279 assert(vf->h > 0);
281 if (w == -1)
282 w = vf->w;
283 if (h == -1)
284 h = vf->h;
286 w2 = (mp_imgflag & MP_IMGFLAG_ACCEPT_ALIGNED_STRIDE) ?
287 ((w + 15) & (~15)) : w;
289 if (vf->put_image == vf_next_put_image) {
290 // passthru mode, if the filter uses the fallback/default put_image()
291 return vf_get_image(vf->next, outfmt, mp_imgtype, mp_imgflag, w, h);
294 // Note: we should call libvo first to check if it supports direct rendering
295 // and if not, then fallback to software buffers:
296 switch (mp_imgtype & 0xff) {
297 case MP_IMGTYPE_EXPORT:
298 if (!vf->imgctx.export_images[0])
299 vf->imgctx.export_images[0] = new_mp_image(w2, h);
300 mpi = vf->imgctx.export_images[0];
301 break;
302 case MP_IMGTYPE_STATIC:
303 if (!vf->imgctx.static_images[0])
304 vf->imgctx.static_images[0] = new_mp_image(w2, h);
305 mpi = vf->imgctx.static_images[0];
306 break;
307 case MP_IMGTYPE_TEMP:
308 if (!vf->imgctx.temp_images[0])
309 vf->imgctx.temp_images[0] = new_mp_image(w2, h);
310 mpi = vf->imgctx.temp_images[0];
311 break;
312 case MP_IMGTYPE_IPB:
313 if (!(mp_imgflag & MP_IMGFLAG_READABLE)) { // B frame:
314 if (!vf->imgctx.temp_images[0])
315 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])
321 vf->imgctx.static_images[vf->imgctx.static_idx] = new_mp_image(w2, h);
322 mpi = vf->imgctx.static_images[vf->imgctx.static_idx];
323 vf->imgctx.static_idx ^= 1;
324 break;
325 case MP_IMGTYPE_NUMBERED:
326 if (number == -1) {
327 int i;
328 for (i = 0; i < NUM_NUMBERED_MPI; i++)
329 if (!vf->imgctx.numbered_images[i] ||
330 !vf->imgctx.numbered_images[i]->usage_count)
331 break;
332 number = i;
334 if (number < 0 || number >= NUM_NUMBERED_MPI)
335 return NULL;
336 if (!vf->imgctx.numbered_images[number])
337 vf->imgctx.numbered_images[number] = new_mp_image(w2, h);
338 mpi = vf->imgctx.numbered_images[number];
339 mpi->number = number;
340 break;
342 if (mpi) {
343 mpi->type = mp_imgtype;
344 mpi->w = vf->w;
345 mpi->h = vf->h;
346 // keep buffer allocation status & color flags only:
347 mpi->flags &= MP_IMGFLAG_ALLOCATED | MP_IMGFLAG_TYPE_DISPLAYED |
348 MP_IMGFLAGMASK_COLORS;
349 // accept restrictions, draw_slice and palette flags only:
350 mpi->flags |= mp_imgflag & (MP_IMGFLAGMASK_RESTRICTIONS |
351 MP_IMGFLAG_DRAW_CALLBACK | MP_IMGFLAG_RGB_PALETTE);
352 if (!vf->draw_slice)
353 mpi->flags &= ~MP_IMGFLAG_DRAW_CALLBACK;
354 if (mpi->width != w2 || mpi->height != h) {
355 if (mpi->flags & MP_IMGFLAG_ALLOCATED) {
356 if (mpi->width < w2 || mpi->height < h) {
357 // need to re-allocate buffer memory:
358 av_free(mpi->planes[0]);
359 mpi->flags &= ~MP_IMGFLAG_ALLOCATED;
360 mp_msg(MSGT_VFILTER, MSGL_V,
361 "vf.c: have to REALLOCATE buffer memory :(\n");
364 mpi->width = w2;
365 mpi->chroma_width = (w2 + (1 << mpi->chroma_x_shift) - 1) >>
366 mpi->chroma_x_shift;
367 mpi->height = h;
368 mpi->chroma_height = (h + (1 << mpi->chroma_y_shift) - 1) >>
369 mpi->chroma_y_shift;
371 if (!mpi->bpp)
372 mp_image_setfmt(mpi, outfmt);
373 if (!(mpi->flags & MP_IMGFLAG_ALLOCATED) &&
374 mpi->type > MP_IMGTYPE_EXPORT) {
375 // check libvo first!
376 if (vf->get_image)
377 vf->get_image(vf, mpi);
379 if (!(mpi->flags & MP_IMGFLAG_DIRECT)) {
380 // non-direct and not yet allocated image. allocate it!
381 if (!mpi->bpp) { // no way we can allocate this
382 mp_msg(MSGT_DECVIDEO, MSGL_FATAL,
383 "vf_get_image: Tried to allocate a format that "
384 "can not be allocated!\n");
385 return NULL;
388 // check if codec prefer aligned stride:
389 if (mp_imgflag & MP_IMGFLAG_PREFER_ALIGNED_STRIDE) {
390 int align = (mpi->flags & MP_IMGFLAG_PLANAR &&
391 mpi->flags & MP_IMGFLAG_YUV) ?
392 (8 << mpi->chroma_x_shift) - 1 : 15; // OK?
393 w2 = ((w + align) & (~align));
394 if (mpi->width != w2) {
395 // we have to change width... check if we CAN co it:
396 int flags = vf->query_format(vf, outfmt);
397 // should not fail
398 if (!(flags & (VFCAP_CSP_SUPPORTED |
399 VFCAP_CSP_SUPPORTED_BY_HW)))
400 mp_msg(MSGT_DECVIDEO, MSGL_WARN,
401 "??? vf_get_image{vf->query_format(outfmt)} "
402 "failed!\n");
403 if (flags & VFCAP_ACCEPT_STRIDE) {
404 mpi->width = w2;
405 mpi->chroma_width =
406 (w2 + (1 << mpi->chroma_x_shift) - 1) >>
407 mpi->chroma_x_shift;
412 mp_image_alloc_planes(mpi);
413 vf_mpi_clear(mpi, 0, 0, mpi->width, mpi->height);
416 if (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)
417 if (vf->start_slice)
418 vf->start_slice(vf, mpi);
419 if (!(mpi->flags & MP_IMGFLAG_TYPE_DISPLAYED)) {
420 mp_msg(MSGT_DECVIDEO, MSGL_V,
421 "*** [%s] %s%s mp_image_t, %dx%dx%dbpp %s %s, %d bytes\n",
422 vf->info->name,
423 (mpi->type == MP_IMGTYPE_EXPORT) ? "Exporting" :
424 ((mpi->flags & MP_IMGFLAG_DIRECT) ?
425 "Direct Rendering" : "Allocating"),
426 (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK) ? " (slices)" : "",
427 mpi->width, mpi->height, mpi->bpp,
428 (mpi->flags & MP_IMGFLAG_YUV) ? "YUV" :
429 ((mpi->flags & MP_IMGFLAG_SWAPPED) ? "BGR" : "RGB"),
430 (mpi->flags & MP_IMGFLAG_PLANAR) ? "planar" : "packed",
431 mpi->bpp * mpi->width * mpi->height / 8);
432 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "(imgfmt: %x, planes: %p,%p,%p "
433 "strides: %d,%d,%d, chroma: %dx%d, shift: h:%d,v:%d)\n",
434 mpi->imgfmt, mpi->planes[0], mpi->planes[1], mpi->planes[2],
435 mpi->stride[0], mpi->stride[1], mpi->stride[2],
436 mpi->chroma_width, mpi->chroma_height,
437 mpi->chroma_x_shift, mpi->chroma_y_shift);
438 mpi->flags |= MP_IMGFLAG_TYPE_DISPLAYED;
440 mpi->qscale = NULL;
442 mpi->usage_count++;
443 return mpi;
446 //============================================================================
448 // By default vf doesn't accept MPEGPES
449 static int vf_default_query_format(struct vf_instance *vf, unsigned int fmt)
451 if (fmt == IMGFMT_MPEGPES)
452 return 0;
453 return vf_next_query_format(vf, fmt);
456 struct vf_instance *vf_open_plugin_noerr(struct MPOpts *opts,
457 const vf_info_t *const *filter_list,
458 vf_instance_t *next, const char *name,
459 char **args, int *retcode)
461 vf_instance_t *vf;
462 int i;
463 for (i = 0;; i++) {
464 if (!filter_list[i]) {
465 mp_tmsg(MSGT_VFILTER, MSGL_ERR,
466 "Couldn't find video filter '%s'.\n", name);
467 return NULL; // no such filter!
469 if (!strcmp(filter_list[i]->name, name))
470 break;
472 vf = calloc(1, sizeof *vf);
473 vf->opts = opts;
474 vf->info = filter_list[i];
475 vf->next = next;
476 vf->config = vf_next_config;
477 vf->control = vf_next_control;
478 vf->query_format = vf_default_query_format;
479 vf->put_image = vf_next_put_image;
480 vf->default_caps = VFCAP_ACCEPT_STRIDE;
481 vf->default_reqs = 0;
482 if (vf->info->opts) { // vf_vo get some special argument
483 const m_struct_t *st = vf->info->opts;
484 void *vf_priv = m_struct_alloc(st);
485 int n;
486 for (n = 0; args && args[2 * n]; n++)
487 m_struct_set(st, vf_priv, args[2 * n], bstr(args[2 * n + 1]));
488 vf->priv = vf_priv;
489 args = NULL;
490 } else // Otherwise we should have the '_oldargs_'
491 if (args && !strcmp(args[0], "_oldargs_"))
492 args = (char **)args[1];
493 else
494 args = NULL;
495 *retcode = vf->info->vf_open(vf, (char *)args);
496 if (*retcode > 0)
497 return vf;
498 free(vf);
499 return NULL;
502 struct vf_instance *vf_open_plugin(struct MPOpts *opts,
503 const vf_info_t *const *filter_list,
504 vf_instance_t *next, const char *name,
505 char **args)
507 struct vf_instance *vf = vf_open_plugin_noerr(opts, filter_list, next,
508 name, args, &(int){0});
509 if (!vf)
510 mp_tmsg(MSGT_VFILTER, MSGL_ERR, "Couldn't open video filter '%s'.\n",
511 name);
512 return vf;
515 vf_instance_t *vf_open_filter(struct MPOpts *opts, vf_instance_t *next,
516 const char *name, char **args)
518 if (args && strcmp(args[0], "_oldargs_")) {
519 int i, l = 0;
520 for (i = 0; args && args[2 * i]; i++)
521 l += 1 + strlen(args[2 * i]) + 1 + strlen(args[2 * i + 1]);
522 l += strlen(name);
524 char str[l + 1];
525 char *p = str;
526 p += sprintf(str, "%s", name);
527 for (i = 0; args && args[2 * i]; i++)
528 p += sprintf(p, " %s=%s", args[2 * i], args[2 * i + 1]);
529 mp_msg(MSGT_VFILTER, MSGL_INFO, "%s[%s]\n",
530 mp_gtext("Opening video filter: "), str);
532 } else if (strcmp(name, "vo")) {
533 if (args && strcmp(args[0], "_oldargs_") == 0)
534 mp_msg(MSGT_VFILTER, MSGL_INFO, "%s[%s=%s]\n",
535 mp_gtext("Opening video filter: "), name, args[1]);
536 else
537 mp_msg(MSGT_VFILTER, MSGL_INFO, "%s[%s]\n",
538 mp_gtext("Opening video filter: "), name);
540 return vf_open_plugin(opts, filter_list, next, name, args);
544 * \brief adds a filter before the last one (which should be the vo filter).
545 * \param vf start of the filter chain.
546 * \param name name of the filter to add.
547 * \param args argument list for the filter.
548 * \return pointer to the filter instance that was created.
550 vf_instance_t *vf_add_before_vo(vf_instance_t **vf, char *name, char **args)
552 struct MPOpts *opts = (*vf)->opts;
553 vf_instance_t *vo, *prev = NULL, *new;
554 // Find the last filter (should be vf_vo)
555 for (vo = *vf; vo->next; vo = vo->next)
556 prev = vo;
557 new = vf_open_filter(opts, vo, name, args);
558 if (prev)
559 prev->next = new;
560 else
561 *vf = new;
562 return new;
565 //============================================================================
567 unsigned int vf_match_csp(vf_instance_t **vfp, const unsigned int *list,
568 unsigned int preferred)
570 vf_instance_t *vf = *vfp;
571 struct MPOpts *opts = vf->opts;
572 const unsigned int *p;
573 unsigned int best = 0;
574 int ret;
575 if ((p = list))
576 while (*p) {
577 ret = vf->query_format(vf, *p);
578 mp_msg(MSGT_VFILTER, MSGL_V, "[%s] query(%s) -> %x\n",
579 vf->info->name, vo_format_name(*p), ret);
580 if (ret & VFCAP_CSP_SUPPORTED_BY_HW) {
581 best = *p;
582 break;
584 if (ret & VFCAP_CSP_SUPPORTED && !best)
585 best = *p;
586 ++p;
588 if (best)
589 return best; // bingo, they have common csp!
590 // ok, then try with scale:
591 if (vf->info == &vf_info_scale)
592 return 0; // avoid infinite recursion!
593 vf = vf_open_filter(opts, vf, "scale", NULL);
594 if (!vf)
595 return 0; // failed to init "scale"
596 // try the preferred csp first:
597 if (preferred && vf->query_format(vf, preferred))
598 best = preferred;
599 else
600 // try the list again, now with "scaler" :
601 if ((p = list))
602 while (*p) {
603 ret = vf->query_format(vf, *p);
604 mp_msg(MSGT_VFILTER, MSGL_V, "[%s] query(%s) -> %x\n",
605 vf->info->name, vo_format_name(*p), ret);
606 if (ret & VFCAP_CSP_SUPPORTED_BY_HW) {
607 best = *p;
608 break;
610 if (ret & VFCAP_CSP_SUPPORTED && !best)
611 best = *p;
612 ++p;
614 if (best)
615 *vfp = vf; // else uninit vf !FIXME!
616 return best;
619 void vf_clone_mpi_attributes(mp_image_t *dst, mp_image_t *src)
621 dst->pict_type = src->pict_type;
622 dst->fields = src->fields;
623 dst->qscale_type = src->qscale_type;
624 if (dst->width == src->width && dst->height == src->height) {
625 dst->qstride = src->qstride;
626 dst->qscale = src->qscale;
630 void vf_queue_frame(vf_instance_t *vf, int (*func)(vf_instance_t *))
632 vf->continue_buffered_image = func;
635 // Output the next buffered image (if any) from the filter chain.
636 // The queue could be kept as a simple stack/list instead avoiding the
637 // looping here, but there's currently no good context variable where
638 // that could be stored so this was easier to implement.
640 int vf_output_queued_frame(vf_instance_t *vf)
642 while (1) {
643 int ret;
644 vf_instance_t *current;
645 vf_instance_t *last = NULL;
646 int (*tmp)(vf_instance_t *);
647 for (current = vf; current; current = current->next)
648 if (current->continue_buffered_image)
649 last = current;
650 if (!last)
651 return 0;
652 tmp = last->continue_buffered_image;
653 last->continue_buffered_image = NULL;
654 ret = tmp(last);
655 if (ret)
656 return ret;
662 * \brief Video config() function wrapper
664 * Blocks config() calls with different size or format for filters
665 * with VFCAP_CONSTANT
667 * First call is redirected to vf->config.
669 * In following calls, it verifies that the configuration parameters
670 * are unchanged, and returns either success or error.
673 int vf_config_wrapper(struct vf_instance *vf,
674 int width, int height, int d_width, int d_height,
675 unsigned int flags, unsigned int outfmt)
677 int r;
678 if ((vf->default_caps & VFCAP_CONSTANT) && vf->fmt.have_configured) {
679 if ((vf->fmt.orig_width != width)
680 || (vf->fmt.orig_height != height)
681 || (vf->fmt.orig_fmt != outfmt)) {
682 mp_tmsg(MSGT_VFILTER, MSGL_ERR, "\nNew video file has different "
683 "resolution or colorspace than the previous one.\n");
684 return 0;
686 return 1;
688 vf->fmt.have_configured = 1;
689 vf->fmt.orig_height = height;
690 vf->fmt.orig_width = width;
691 vf->fmt.orig_fmt = outfmt;
692 r = vf->config(vf, width, height, d_width, d_height, flags, outfmt);
693 if (!r)
694 vf->fmt.have_configured = 0;
695 return r;
698 int vf_next_config(struct vf_instance *vf,
699 int width, int height, int d_width, int d_height,
700 unsigned int voflags, unsigned int outfmt)
702 struct MPOpts *opts = vf->opts;
703 int miss;
704 int flags = vf->next->query_format(vf->next, outfmt);
705 if (!flags) {
706 // hmm. colorspace mismatch!!!
707 // let's insert the 'scale' filter, it does the job for us:
708 vf_instance_t *vf2;
709 if (vf->next->info == &vf_info_scale)
710 return 0; // scale->scale
711 vf2 = vf_open_filter(opts, vf->next, "scale", NULL);
712 if (!vf2)
713 return 0; // shouldn't happen!
714 vf->next = vf2;
715 flags = vf->next->query_format(vf->next, outfmt);
716 if (!flags) {
717 mp_tmsg(MSGT_VFILTER, MSGL_ERR, "Cannot find matching colorspace, "
718 "even by inserting 'scale' :(\n");
719 return 0; // FAIL
722 mp_msg(MSGT_VFILTER, MSGL_V, "REQ: flags=0x%X req=0x%X \n",
723 flags, vf->default_reqs);
724 miss = vf->default_reqs - (flags & vf->default_reqs);
725 if (miss & VFCAP_ACCEPT_STRIDE) {
726 // vf requires stride support but vf->next doesn't support it!
727 // let's insert the 'expand' filter, it does the job for us:
728 vf_instance_t *vf2 = vf_open_filter(opts, vf->next, "expand", NULL);
729 if (!vf2)
730 return 0; // shouldn't happen!
731 vf->next = vf2;
733 vf->next->w = width;
734 vf->next->h = height;
735 return vf_config_wrapper(vf->next, width, height, d_width, d_height,
736 voflags, outfmt);
739 int vf_next_control(struct vf_instance *vf, int request, void *data)
741 return vf->next->control(vf->next, request, data);
744 int vf_next_query_format(struct vf_instance *vf, unsigned int fmt)
746 int flags = vf->next->query_format(vf->next, fmt);
747 if (flags)
748 flags |= vf->default_caps;
749 return flags;
752 int vf_next_put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
754 return vf->next->put_image(vf->next, mpi, pts);
757 void vf_next_draw_slice(struct vf_instance *vf, unsigned char **src,
758 int *stride, int w, int h, int x, int y)
760 if (vf->next->draw_slice) {
761 vf->next->draw_slice(vf->next, src, stride, w, h, x, y);
762 return;
764 if (!vf->dmpi) {
765 mp_msg(MSGT_VFILTER, MSGL_ERR,
766 "draw_slice: dmpi not stored by vf_%s\n", vf->info->name);
767 return;
769 if (!(vf->dmpi->flags & MP_IMGFLAG_PLANAR)) {
770 memcpy_pic(vf->dmpi->planes[0] + y * vf->dmpi->stride[0] +
771 vf->dmpi->bpp / 8 * x,
772 src[0], vf->dmpi->bpp / 8 * w, h, vf->dmpi->stride[0],
773 stride[0]);
774 return;
776 memcpy_pic(vf->dmpi->planes[0] + y * vf->dmpi->stride[0] + x, src[0],
777 w, h, vf->dmpi->stride[0], stride[0]);
778 memcpy_pic(vf->dmpi->planes[1]
779 + (y >> vf->dmpi->chroma_y_shift) * vf->dmpi->stride[1]
780 + (x >> vf->dmpi->chroma_x_shift),
781 src[1], w >> vf->dmpi->chroma_x_shift,
782 h >> vf->dmpi->chroma_y_shift, vf->dmpi->stride[1], stride[1]);
783 memcpy_pic(vf->dmpi->planes[2]
784 + (y >> vf->dmpi->chroma_y_shift) * vf->dmpi->stride[2]
785 + (x >> vf->dmpi->chroma_x_shift),
786 src[2], w >> vf->dmpi->chroma_x_shift,
787 h >> vf->dmpi->chroma_y_shift, vf->dmpi->stride[2], stride[2]);
790 //============================================================================
792 vf_instance_t *append_filters(vf_instance_t *last,
793 struct m_obj_settings *vf_settings)
795 struct MPOpts *opts = last->opts;
796 vf_instance_t *vf;
797 int i;
799 if (vf_settings) {
800 // We want to add them in the 'right order'
801 for (i = 0; vf_settings[i].name; i++)
802 /* NOP */;
803 for (i--; i >= 0; i--) {
804 //printf("Open filter %s\n",vf_settings[i].name);
805 vf = vf_open_filter(opts, last, vf_settings[i].name,
806 vf_settings[i].attribs);
807 if (vf)
808 last = vf;
811 return last;
814 //============================================================================
816 void vf_uninit_filter(vf_instance_t *vf)
818 if (vf->uninit)
819 vf->uninit(vf);
820 free_mp_image(vf->imgctx.static_images[0]);
821 free_mp_image(vf->imgctx.static_images[1]);
822 free_mp_image(vf->imgctx.temp_images[0]);
823 free_mp_image(vf->imgctx.export_images[0]);
824 for (int i = 0; i < NUM_NUMBERED_MPI; i++)
825 free_mp_image(vf->imgctx.numbered_images[i]);
826 free(vf);
829 void vf_uninit_filter_chain(vf_instance_t *vf)
831 while (vf) {
832 vf_instance_t *next = vf->next;
833 vf_uninit_filter(vf);
834 vf = next;
838 void vf_detc_init_pts_buf(struct vf_detc_pts_buf *p)
840 p->inpts_prev = MP_NOPTS_VALUE;
841 p->outpts_prev = MP_NOPTS_VALUE;
842 p->lastdelta = 0;
845 static double vf_detc_adjust_pts_internal(struct vf_detc_pts_buf *p,
846 double pts, bool reset_pattern,
847 bool skip_frame, double delta,
848 double boundfactor_minus,
849 double increasefactor,
850 double boundfactor_plus)
852 double newpts;
854 if (pts == MP_NOPTS_VALUE)
855 return pts;
857 if (delta <= 0) {
858 if (p->inpts_prev == MP_NOPTS_VALUE)
859 delta = 0;
860 else if (pts == p->inpts_prev)
861 delta = p->lastdelta;
862 else
863 delta = pts - p->inpts_prev;
865 p->inpts_prev = pts;
866 p->lastdelta = delta;
868 if (skip_frame)
869 return MP_NOPTS_VALUE;
871 /* detect bogus deltas and then passthru pts (possibly caused by seeking,
872 * or bad input) */
873 if (p->outpts_prev == MP_NOPTS_VALUE || reset_pattern || delta <= 0.0 ||
874 delta >= 0.5)
875 newpts = pts;
876 else {
877 // turn 5 frames into 4
878 newpts = p->outpts_prev + delta * increasefactor;
880 // bound to input pts in a sensible way; these numbers come because we
881 // map frames the following way when ivtc'ing:
882 // 0/30 -> 0/24 diff=0
883 // 1/30 -> 1/24 diff=1/120
884 // 2/30 -> -
885 // 3/30 -> 2/24 diff=-1/60
886 // 4/30 -> 3/24 diff=-1/120
887 if (newpts < pts - delta * boundfactor_minus)
888 newpts = pts - delta * boundfactor_minus;
889 if (newpts > pts + delta * boundfactor_plus)
890 newpts = pts + delta * boundfactor_plus;
891 if (newpts < p->outpts_prev)
892 newpts = p->outpts_prev; // damage control
894 p->outpts_prev = newpts;
896 return newpts;
899 double vf_detc_adjust_pts(struct vf_detc_pts_buf *p, double pts,
900 bool reset_pattern, bool skip_frame)
902 // standard telecine (see above)
903 return vf_detc_adjust_pts_internal(p, pts, reset_pattern, skip_frame,
904 0, 0.5, 1.25, 0.25);
907 double vf_softpulldown_adjust_pts(struct vf_detc_pts_buf *p, double pts,
908 bool reset_pattern, bool skip_frame,
909 int last_frame_duration)
911 // for the softpulldown filter we get:
912 // 0/60 -> 0/30
913 // 2/60 -> 1/30
914 // 5/60 -> 2/30
915 // 7/60 -> 3/30, 4/30
916 return vf_detc_adjust_pts_internal(p, pts, reset_pattern, skip_frame,
917 0, 1.0 / last_frame_duration,
918 2.0 / last_frame_duration,
919 1.0 / last_frame_duration);