ad_libav: flush decoder output after last input packet
[mplayer.git] / libmpcodecs / vf.c
blob45a93ef0e9df9abf0d466720165afff7492eb389
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_fspp;
94 extern const vf_info_t vf_info_pp7;
95 extern const vf_info_t vf_info_yuvcsp;
96 extern const vf_info_t vf_info_kerndeint;
97 extern const vf_info_t vf_info_rgbtest;
98 extern const vf_info_t vf_info_qp;
99 extern const vf_info_t vf_info_phase;
100 extern const vf_info_t vf_info_divtc;
101 extern const vf_info_t vf_info_harddup;
102 extern const vf_info_t vf_info_softskip;
103 extern const vf_info_t vf_info_screenshot;
104 extern const vf_info_t vf_info_ass;
105 extern const vf_info_t vf_info_yadif;
106 extern const vf_info_t vf_info_blackframe;
107 extern const vf_info_t vf_info_geq;
108 extern const vf_info_t vf_info_ow;
109 extern const vf_info_t vf_info_fixpts;
110 extern const vf_info_t vf_info_stereo3d;
112 // list of available filters:
113 static const vf_info_t *const filter_list[] = {
114 &vf_info_rectangle,
115 #ifdef HAVE_POSIX_SELECT
116 &vf_info_bmovl,
117 #endif
118 &vf_info_crop,
119 &vf_info_expand,
120 &vf_info_scale,
121 // &vf_info_osd,
122 &vf_info_vo,
123 &vf_info_format,
124 &vf_info_noformat,
125 &vf_info_flip,
126 &vf_info_rotate,
127 &vf_info_mirror,
128 &vf_info_palette,
129 &vf_info_pp7,
131 #ifdef CONFIG_LIBPOSTPROC
132 &vf_info_pp,
133 #endif
134 &vf_info_lavc,
135 &vf_info_lavcdeint,
136 &vf_info_screenshot,
137 &vf_info_fspp,
139 &vf_info_dvbscale,
140 &vf_info_cropdetect,
141 &vf_info_test,
142 &vf_info_noise,
143 &vf_info_yvu9,
144 &vf_info_eq,
145 &vf_info_eq2,
146 &vf_info_gradfun,
147 &vf_info_halfpack,
148 &vf_info_dint,
149 &vf_info_1bpp,
150 &vf_info_2xsai,
151 &vf_info_unsharp,
152 &vf_info_swapuv,
153 &vf_info_il,
154 &vf_info_fil,
155 &vf_info_boxblur,
156 &vf_info_sab,
157 &vf_info_smartblur,
158 &vf_info_perspective,
159 &vf_info_down3dright,
160 &vf_info_field,
161 &vf_info_denoise3d,
162 &vf_info_hqdn3d,
163 &vf_info_detc,
164 &vf_info_telecine,
165 &vf_info_tinterlace,
166 &vf_info_tfields,
167 &vf_info_ivtc,
168 &vf_info_ilpack,
169 &vf_info_dsize,
170 &vf_info_decimate,
171 &vf_info_softpulldown,
172 &vf_info_pullup,
173 &vf_info_filmdint,
174 &vf_info_framestep,
175 &vf_info_tile,
176 &vf_info_delogo,
177 &vf_info_remove_logo,
178 &vf_info_hue,
179 &vf_info_geq,
180 &vf_info_qp,
181 &vf_info_yuvcsp,
182 &vf_info_kerndeint,
183 &vf_info_rgbtest,
184 &vf_info_phase,
185 &vf_info_divtc,
186 &vf_info_harddup,
187 &vf_info_softskip,
188 #ifdef CONFIG_ASS
189 &vf_info_ass,
190 #endif
191 &vf_info_yadif,
192 &vf_info_blackframe,
193 &vf_info_ow,
194 &vf_info_fixpts,
195 &vf_info_stereo3d,
196 NULL
199 // For the vf option
200 const m_obj_list_t vf_obj_list = {
201 (void **)filter_list,
202 M_ST_OFF(vf_info_t, name),
203 M_ST_OFF(vf_info_t, info),
204 M_ST_OFF(vf_info_t, opts)
207 //============================================================================
208 // mpi stuff:
210 void vf_mpi_clear(mp_image_t *mpi, int x0, int y0, int w, int h)
212 int y;
213 if (mpi->flags & MP_IMGFLAG_PLANAR) {
214 y0 &= ~1;
215 h += h & 1;
216 if (x0 == 0 && w == mpi->width) {
217 // full width clear:
218 memset(mpi->planes[0] + mpi->stride[0] * y0, 0, mpi->stride[0] * h);
219 memset(mpi->planes[1] + mpi->stride[1] *(y0 >> mpi->chroma_y_shift),
220 128, mpi->stride[1] * (h >> mpi->chroma_y_shift));
221 memset(mpi->planes[2] + mpi->stride[2] *(y0 >> mpi->chroma_y_shift),
222 128, mpi->stride[2] * (h >> mpi->chroma_y_shift));
223 } else
224 for (y = y0; y < y0 + h; y += 2) {
225 memset(mpi->planes[0] + x0 + mpi->stride[0] * y, 0, w);
226 memset(mpi->planes[0] + x0 + mpi->stride[0] * (y + 1), 0, w);
227 memset(mpi->planes[1] + (x0 >> mpi->chroma_x_shift) +
228 mpi->stride[1] * (y >> mpi->chroma_y_shift),
229 128, (w >> mpi->chroma_x_shift));
230 memset(mpi->planes[2] + (x0 >> mpi->chroma_x_shift) +
231 mpi->stride[2] * (y >> mpi->chroma_y_shift),
232 128, (w >> mpi->chroma_x_shift));
234 return;
236 // packed:
237 for (y = y0; y < y0 + h; y++) {
238 unsigned char *dst = mpi->planes[0] + mpi->stride[0] * y +
239 (mpi->bpp >> 3) * x0;
240 if (mpi->flags & MP_IMGFLAG_YUV) {
241 unsigned int *p = (unsigned int *) dst;
242 int size = (mpi->bpp >> 3) * w / 4;
243 int i;
244 #if HAVE_BIGENDIAN
245 #define CLEAR_PACKEDYUV_PATTERN 0x00800080
246 #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x80008000
247 #else
248 #define CLEAR_PACKEDYUV_PATTERN 0x80008000
249 #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x00800080
250 #endif
251 if (mpi->flags & MP_IMGFLAG_SWAPPED) {
252 for (i = 0; i < size - 3; i += 4)
253 p[i] = p[i + 1] = p[i + 2] = p[i + 3] = CLEAR_PACKEDYUV_PATTERN_SWAPPED;
254 for (; i < size; i++)
255 p[i] = CLEAR_PACKEDYUV_PATTERN_SWAPPED;
256 } else {
257 for (i = 0; i < size - 3; i += 4)
258 p[i] = p[i + 1] = p[i + 2] = p[i + 3] = CLEAR_PACKEDYUV_PATTERN;
259 for (; i < size; i++)
260 p[i] = CLEAR_PACKEDYUV_PATTERN;
262 } else
263 memset(dst, 0, (mpi->bpp >> 3) * w);
267 mp_image_t *vf_get_image(vf_instance_t *vf, unsigned int outfmt,
268 int mp_imgtype, int mp_imgflag, int w, int h)
270 mp_image_t *mpi = NULL;
271 int w2;
272 int number = mp_imgtype >> 16;
274 assert(w == -1 || w >= vf->w);
275 assert(h == -1 || h >= vf->h);
276 assert(vf->w > 0);
277 assert(vf->h > 0);
279 if (w == -1)
280 w = vf->w;
281 if (h == -1)
282 h = vf->h;
284 w2 = (mp_imgflag & MP_IMGFLAG_ACCEPT_ALIGNED_STRIDE) ?
285 ((w + 15) & (~15)) : w;
287 if (vf->put_image == vf_next_put_image) {
288 // passthru mode, if the filter uses the fallback/default put_image()
289 return vf_get_image(vf->next, outfmt, mp_imgtype, mp_imgflag, w, h);
292 // Note: we should call libvo first to check if it supports direct rendering
293 // and if not, then fallback to software buffers:
294 switch (mp_imgtype & 0xff) {
295 case MP_IMGTYPE_EXPORT:
296 if (!vf->imgctx.export_images[0])
297 vf->imgctx.export_images[0] = new_mp_image(w2, h);
298 mpi = vf->imgctx.export_images[0];
299 break;
300 case MP_IMGTYPE_STATIC:
301 if (!vf->imgctx.static_images[0])
302 vf->imgctx.static_images[0] = new_mp_image(w2, h);
303 mpi = vf->imgctx.static_images[0];
304 break;
305 case MP_IMGTYPE_TEMP:
306 if (!vf->imgctx.temp_images[0])
307 vf->imgctx.temp_images[0] = new_mp_image(w2, h);
308 mpi = vf->imgctx.temp_images[0];
309 break;
310 case MP_IMGTYPE_IPB:
311 if (!(mp_imgflag & MP_IMGFLAG_READABLE)) { // B frame:
312 if (!vf->imgctx.temp_images[0])
313 vf->imgctx.temp_images[0] = new_mp_image(w2, h);
314 mpi = vf->imgctx.temp_images[0];
315 break;
317 case MP_IMGTYPE_IP:
318 if (!vf->imgctx.static_images[vf->imgctx.static_idx])
319 vf->imgctx.static_images[vf->imgctx.static_idx] = new_mp_image(w2, h);
320 mpi = vf->imgctx.static_images[vf->imgctx.static_idx];
321 vf->imgctx.static_idx ^= 1;
322 break;
323 case MP_IMGTYPE_NUMBERED:
324 if (number == -1) {
325 int i;
326 for (i = 0; i < NUM_NUMBERED_MPI; i++)
327 if (!vf->imgctx.numbered_images[i] ||
328 !vf->imgctx.numbered_images[i]->usage_count)
329 break;
330 number = i;
332 if (number < 0 || number >= NUM_NUMBERED_MPI)
333 return NULL;
334 if (!vf->imgctx.numbered_images[number])
335 vf->imgctx.numbered_images[number] = new_mp_image(w2, h);
336 mpi = vf->imgctx.numbered_images[number];
337 mpi->number = number;
338 break;
340 if (mpi) {
341 mpi->type = mp_imgtype;
342 mpi->w = vf->w;
343 mpi->h = vf->h;
344 // keep buffer allocation status & color flags only:
345 mpi->flags &= MP_IMGFLAG_ALLOCATED | MP_IMGFLAG_TYPE_DISPLAYED |
346 MP_IMGFLAGMASK_COLORS;
347 // accept restrictions, draw_slice and palette flags only:
348 mpi->flags |= mp_imgflag & (MP_IMGFLAGMASK_RESTRICTIONS |
349 MP_IMGFLAG_DRAW_CALLBACK | MP_IMGFLAG_RGB_PALETTE);
350 if (!vf->draw_slice)
351 mpi->flags &= ~MP_IMGFLAG_DRAW_CALLBACK;
352 if (mpi->width != w2 || mpi->height != h) {
353 if (mpi->flags & MP_IMGFLAG_ALLOCATED) {
354 if (mpi->width < w2 || mpi->height < h) {
355 // need to re-allocate buffer memory:
356 av_free(mpi->planes[0]);
357 mpi->flags &= ~MP_IMGFLAG_ALLOCATED;
358 mp_msg(MSGT_VFILTER, MSGL_V,
359 "vf.c: have to REALLOCATE buffer memory :(\n");
362 mpi->width = w2;
363 mpi->chroma_width = (w2 + (1 << mpi->chroma_x_shift) - 1) >>
364 mpi->chroma_x_shift;
365 mpi->height = h;
366 mpi->chroma_height = (h + (1 << mpi->chroma_y_shift) - 1) >>
367 mpi->chroma_y_shift;
369 if (!mpi->bpp)
370 mp_image_setfmt(mpi, outfmt);
371 if (!(mpi->flags & MP_IMGFLAG_ALLOCATED) &&
372 mpi->type > MP_IMGTYPE_EXPORT) {
373 // check libvo first!
374 if (vf->get_image)
375 vf->get_image(vf, mpi);
377 if (!(mpi->flags & MP_IMGFLAG_DIRECT)) {
378 // non-direct and not yet allocated image. allocate it!
379 if (!mpi->bpp) { // no way we can allocate this
380 mp_msg(MSGT_DECVIDEO, MSGL_FATAL,
381 "vf_get_image: Tried to allocate a format that "
382 "can not be allocated!\n");
383 return NULL;
386 // check if codec prefer aligned stride:
387 if (mp_imgflag & MP_IMGFLAG_PREFER_ALIGNED_STRIDE) {
388 int align = (mpi->flags & MP_IMGFLAG_PLANAR &&
389 mpi->flags & MP_IMGFLAG_YUV) ?
390 (8 << mpi->chroma_x_shift) - 1 : 15; // OK?
391 w2 = ((w + align) & (~align));
392 if (mpi->width != w2) {
393 // we have to change width... check if we CAN co it:
394 int flags = vf->query_format(vf, outfmt);
395 // should not fail
396 if (!(flags & (VFCAP_CSP_SUPPORTED |
397 VFCAP_CSP_SUPPORTED_BY_HW)))
398 mp_msg(MSGT_DECVIDEO, MSGL_WARN,
399 "??? vf_get_image{vf->query_format(outfmt)} "
400 "failed!\n");
401 if (flags & VFCAP_ACCEPT_STRIDE) {
402 mpi->width = w2;
403 mpi->chroma_width =
404 (w2 + (1 << mpi->chroma_x_shift) - 1) >>
405 mpi->chroma_x_shift;
410 mp_image_alloc_planes(mpi);
411 vf_mpi_clear(mpi, 0, 0, mpi->width, mpi->height);
414 if (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)
415 if (vf->start_slice)
416 vf->start_slice(vf, mpi);
417 if (!(mpi->flags & MP_IMGFLAG_TYPE_DISPLAYED)) {
418 mp_msg(MSGT_DECVIDEO, MSGL_V,
419 "*** [%s] %s%s mp_image_t, %dx%dx%dbpp %s %s, %d bytes\n",
420 vf->info->name,
421 (mpi->type == MP_IMGTYPE_EXPORT) ? "Exporting" :
422 ((mpi->flags & MP_IMGFLAG_DIRECT) ?
423 "Direct Rendering" : "Allocating"),
424 (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK) ? " (slices)" : "",
425 mpi->width, mpi->height, mpi->bpp,
426 (mpi->flags & MP_IMGFLAG_YUV) ? "YUV" :
427 ((mpi->flags & MP_IMGFLAG_SWAPPED) ? "BGR" : "RGB"),
428 (mpi->flags & MP_IMGFLAG_PLANAR) ? "planar" : "packed",
429 mpi->bpp * mpi->width * mpi->height / 8);
430 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "(imgfmt: %x, planes: %p,%p,%p "
431 "strides: %d,%d,%d, chroma: %dx%d, shift: h:%d,v:%d)\n",
432 mpi->imgfmt, mpi->planes[0], mpi->planes[1], mpi->planes[2],
433 mpi->stride[0], mpi->stride[1], mpi->stride[2],
434 mpi->chroma_width, mpi->chroma_height,
435 mpi->chroma_x_shift, mpi->chroma_y_shift);
436 mpi->flags |= MP_IMGFLAG_TYPE_DISPLAYED;
438 mpi->qscale = NULL;
440 mpi->usage_count++;
441 return mpi;
444 //============================================================================
446 // By default vf doesn't accept MPEGPES
447 static int vf_default_query_format(struct vf_instance *vf, unsigned int fmt)
449 if (fmt == IMGFMT_MPEGPES)
450 return 0;
451 return vf_next_query_format(vf, fmt);
454 struct vf_instance *vf_open_plugin_noerr(struct MPOpts *opts,
455 const vf_info_t *const *filter_list,
456 vf_instance_t *next, const char *name,
457 char **args, int *retcode)
459 vf_instance_t *vf;
460 int i;
461 for (i = 0;; i++) {
462 if (!filter_list[i]) {
463 mp_tmsg(MSGT_VFILTER, MSGL_ERR,
464 "Couldn't find video filter '%s'.\n", name);
465 return NULL; // no such filter!
467 if (!strcmp(filter_list[i]->name, name))
468 break;
470 vf = calloc(1, sizeof *vf);
471 vf->opts = opts;
472 vf->info = filter_list[i];
473 vf->next = next;
474 vf->config = vf_next_config;
475 vf->control = vf_next_control;
476 vf->query_format = vf_default_query_format;
477 vf->put_image = vf_next_put_image;
478 vf->default_caps = VFCAP_ACCEPT_STRIDE;
479 vf->default_reqs = 0;
480 if (vf->info->opts) { // vf_vo get some special argument
481 const m_struct_t *st = vf->info->opts;
482 void *vf_priv = m_struct_alloc(st);
483 int n;
484 for (n = 0; args && args[2 * n]; n++)
485 m_struct_set(st, vf_priv, args[2 * n], bstr(args[2 * n + 1]));
486 vf->priv = vf_priv;
487 args = NULL;
488 } else // Otherwise we should have the '_oldargs_'
489 if (args && !strcmp(args[0], "_oldargs_"))
490 args = (char **)args[1];
491 else
492 args = NULL;
493 *retcode = vf->info->vf_open(vf, (char *)args);
494 if (*retcode > 0)
495 return vf;
496 free(vf);
497 return NULL;
500 struct vf_instance *vf_open_plugin(struct MPOpts *opts,
501 const vf_info_t *const *filter_list,
502 vf_instance_t *next, const char *name,
503 char **args)
505 struct vf_instance *vf = vf_open_plugin_noerr(opts, filter_list, next,
506 name, args, &(int){0});
507 if (!vf)
508 mp_tmsg(MSGT_VFILTER, MSGL_ERR, "Couldn't open video filter '%s'.\n",
509 name);
510 return vf;
513 vf_instance_t *vf_open_filter(struct MPOpts *opts, vf_instance_t *next,
514 const char *name, char **args)
516 if (args && strcmp(args[0], "_oldargs_")) {
517 int i, l = 0;
518 for (i = 0; args && args[2 * i]; i++)
519 l += 1 + strlen(args[2 * i]) + 1 + strlen(args[2 * i + 1]);
520 l += strlen(name);
522 char str[l + 1];
523 char *p = str;
524 p += sprintf(str, "%s", name);
525 for (i = 0; args && args[2 * i]; i++)
526 p += sprintf(p, " %s=%s", args[2 * i], args[2 * i + 1]);
527 mp_msg(MSGT_VFILTER, MSGL_INFO, "%s[%s]\n",
528 mp_gtext("Opening video filter: "), str);
530 } else if (strcmp(name, "vo")) {
531 if (args && strcmp(args[0], "_oldargs_") == 0)
532 mp_msg(MSGT_VFILTER, MSGL_INFO, "%s[%s=%s]\n",
533 mp_gtext("Opening video filter: "), name, args[1]);
534 else
535 mp_msg(MSGT_VFILTER, MSGL_INFO, "%s[%s]\n",
536 mp_gtext("Opening video filter: "), name);
538 return vf_open_plugin(opts, filter_list, next, name, args);
542 * \brief adds a filter before the last one (which should be the vo filter).
543 * \param vf start of the filter chain.
544 * \param name name of the filter to add.
545 * \param args argument list for the filter.
546 * \return pointer to the filter instance that was created.
548 vf_instance_t *vf_add_before_vo(vf_instance_t **vf, char *name, char **args)
550 struct MPOpts *opts = (*vf)->opts;
551 vf_instance_t *vo, *prev = NULL, *new;
552 // Find the last filter (should be vf_vo)
553 for (vo = *vf; vo->next; vo = vo->next)
554 prev = vo;
555 new = vf_open_filter(opts, vo, name, args);
556 if (prev)
557 prev->next = new;
558 else
559 *vf = new;
560 return new;
563 //============================================================================
565 unsigned int vf_match_csp(vf_instance_t **vfp, const unsigned int *list,
566 unsigned int preferred)
568 vf_instance_t *vf = *vfp;
569 struct MPOpts *opts = vf->opts;
570 const unsigned int *p;
571 unsigned int best = 0;
572 int ret;
573 if ((p = list))
574 while (*p) {
575 ret = vf->query_format(vf, *p);
576 mp_msg(MSGT_VFILTER, MSGL_V, "[%s] query(%s) -> %x\n",
577 vf->info->name, vo_format_name(*p), ret);
578 if (ret & VFCAP_CSP_SUPPORTED_BY_HW) {
579 best = *p;
580 break;
582 if (ret & VFCAP_CSP_SUPPORTED && !best)
583 best = *p;
584 ++p;
586 if (best)
587 return best; // bingo, they have common csp!
588 // ok, then try with scale:
589 if (vf->info == &vf_info_scale)
590 return 0; // avoid infinite recursion!
591 vf = vf_open_filter(opts, vf, "scale", NULL);
592 if (!vf)
593 return 0; // failed to init "scale"
594 // try the preferred csp first:
595 if (preferred && vf->query_format(vf, preferred))
596 best = preferred;
597 else
598 // try the list again, now with "scaler" :
599 if ((p = list))
600 while (*p) {
601 ret = vf->query_format(vf, *p);
602 mp_msg(MSGT_VFILTER, MSGL_V, "[%s] query(%s) -> %x\n",
603 vf->info->name, vo_format_name(*p), ret);
604 if (ret & VFCAP_CSP_SUPPORTED_BY_HW) {
605 best = *p;
606 break;
608 if (ret & VFCAP_CSP_SUPPORTED && !best)
609 best = *p;
610 ++p;
612 if (best)
613 *vfp = vf; // else uninit vf !FIXME!
614 return best;
617 void vf_clone_mpi_attributes(mp_image_t *dst, mp_image_t *src)
619 dst->pict_type = src->pict_type;
620 dst->fields = src->fields;
621 dst->qscale_type = src->qscale_type;
622 if (dst->width == src->width && dst->height == src->height) {
623 dst->qstride = src->qstride;
624 dst->qscale = src->qscale;
628 void vf_queue_frame(vf_instance_t *vf, int (*func)(vf_instance_t *))
630 vf->continue_buffered_image = func;
633 // Output the next buffered image (if any) from the filter chain.
634 // The queue could be kept as a simple stack/list instead avoiding the
635 // looping here, but there's currently no good context variable where
636 // that could be stored so this was easier to implement.
638 int vf_output_queued_frame(vf_instance_t *vf)
640 while (1) {
641 int ret;
642 vf_instance_t *current;
643 vf_instance_t *last = NULL;
644 int (*tmp)(vf_instance_t *);
645 for (current = vf; current; current = current->next)
646 if (current->continue_buffered_image)
647 last = current;
648 if (!last)
649 return 0;
650 tmp = last->continue_buffered_image;
651 last->continue_buffered_image = NULL;
652 ret = tmp(last);
653 if (ret)
654 return ret;
660 * \brief Video config() function wrapper
662 * Blocks config() calls with different size or format for filters
663 * with VFCAP_CONSTANT
665 * First call is redirected to vf->config.
667 * In following calls, it verifies that the configuration parameters
668 * are unchanged, and returns either success or error.
671 int vf_config_wrapper(struct vf_instance *vf,
672 int width, int height, int d_width, int d_height,
673 unsigned int flags, unsigned int outfmt)
675 int r;
676 if ((vf->default_caps & VFCAP_CONSTANT) && vf->fmt.have_configured) {
677 if ((vf->fmt.orig_width != width)
678 || (vf->fmt.orig_height != height)
679 || (vf->fmt.orig_fmt != outfmt)) {
680 mp_tmsg(MSGT_VFILTER, MSGL_ERR, "\nNew video file has different "
681 "resolution or colorspace than the previous one.\n");
682 return 0;
684 return 1;
686 vf->fmt.have_configured = 1;
687 vf->fmt.orig_height = height;
688 vf->fmt.orig_width = width;
689 vf->fmt.orig_fmt = outfmt;
690 r = vf->config(vf, width, height, d_width, d_height, flags, outfmt);
691 if (!r)
692 vf->fmt.have_configured = 0;
693 return r;
696 int vf_next_config(struct vf_instance *vf,
697 int width, int height, int d_width, int d_height,
698 unsigned int voflags, unsigned int outfmt)
700 struct MPOpts *opts = vf->opts;
701 int miss;
702 int flags = vf->next->query_format(vf->next, outfmt);
703 if (!flags) {
704 // hmm. colorspace mismatch!!!
705 // let's insert the 'scale' filter, it does the job for us:
706 vf_instance_t *vf2;
707 if (vf->next->info == &vf_info_scale)
708 return 0; // scale->scale
709 vf2 = vf_open_filter(opts, vf->next, "scale", NULL);
710 if (!vf2)
711 return 0; // shouldn't happen!
712 vf->next = vf2;
713 flags = vf->next->query_format(vf->next, outfmt);
714 if (!flags) {
715 mp_tmsg(MSGT_VFILTER, MSGL_ERR, "Cannot find matching colorspace, "
716 "even by inserting 'scale' :(\n");
717 return 0; // FAIL
720 mp_msg(MSGT_VFILTER, MSGL_V, "REQ: flags=0x%X req=0x%X \n",
721 flags, vf->default_reqs);
722 miss = vf->default_reqs - (flags & vf->default_reqs);
723 if (miss & VFCAP_ACCEPT_STRIDE) {
724 // vf requires stride support but vf->next doesn't support it!
725 // let's insert the 'expand' filter, it does the job for us:
726 vf_instance_t *vf2 = vf_open_filter(opts, vf->next, "expand", NULL);
727 if (!vf2)
728 return 0; // shouldn't happen!
729 vf->next = vf2;
731 vf->next->w = width;
732 vf->next->h = height;
733 return vf_config_wrapper(vf->next, width, height, d_width, d_height,
734 voflags, outfmt);
737 int vf_next_control(struct vf_instance *vf, int request, void *data)
739 return vf->next->control(vf->next, request, data);
742 int vf_next_query_format(struct vf_instance *vf, unsigned int fmt)
744 int flags = vf->next->query_format(vf->next, fmt);
745 if (flags)
746 flags |= vf->default_caps;
747 return flags;
750 int vf_next_put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
752 return vf->next->put_image(vf->next, mpi, pts);
755 void vf_next_draw_slice(struct vf_instance *vf, unsigned char **src,
756 int *stride, int w, int h, int x, int y)
758 if (vf->next->draw_slice) {
759 vf->next->draw_slice(vf->next, src, stride, w, h, x, y);
760 return;
762 if (!vf->dmpi) {
763 mp_msg(MSGT_VFILTER, MSGL_ERR,
764 "draw_slice: dmpi not stored by vf_%s\n", vf->info->name);
765 return;
767 if (!(vf->dmpi->flags & MP_IMGFLAG_PLANAR)) {
768 memcpy_pic(vf->dmpi->planes[0] + y * vf->dmpi->stride[0] +
769 vf->dmpi->bpp / 8 * x,
770 src[0], vf->dmpi->bpp / 8 * w, h, vf->dmpi->stride[0],
771 stride[0]);
772 return;
774 memcpy_pic(vf->dmpi->planes[0] + y * vf->dmpi->stride[0] + x, src[0],
775 w, h, vf->dmpi->stride[0], stride[0]);
776 memcpy_pic(vf->dmpi->planes[1]
777 + (y >> vf->dmpi->chroma_y_shift) * vf->dmpi->stride[1]
778 + (x >> vf->dmpi->chroma_x_shift),
779 src[1], w >> vf->dmpi->chroma_x_shift,
780 h >> vf->dmpi->chroma_y_shift, vf->dmpi->stride[1], stride[1]);
781 memcpy_pic(vf->dmpi->planes[2]
782 + (y >> vf->dmpi->chroma_y_shift) * vf->dmpi->stride[2]
783 + (x >> vf->dmpi->chroma_x_shift),
784 src[2], w >> vf->dmpi->chroma_x_shift,
785 h >> vf->dmpi->chroma_y_shift, vf->dmpi->stride[2], stride[2]);
788 //============================================================================
790 vf_instance_t *append_filters(vf_instance_t *last,
791 struct m_obj_settings *vf_settings)
793 struct MPOpts *opts = last->opts;
794 vf_instance_t *vf;
795 int i;
797 if (vf_settings) {
798 // We want to add them in the 'right order'
799 for (i = 0; vf_settings[i].name; i++)
800 /* NOP */;
801 for (i--; i >= 0; i--) {
802 //printf("Open filter %s\n",vf_settings[i].name);
803 vf = vf_open_filter(opts, last, vf_settings[i].name,
804 vf_settings[i].attribs);
805 if (vf)
806 last = vf;
809 return last;
812 //============================================================================
814 void vf_uninit_filter(vf_instance_t *vf)
816 if (vf->uninit)
817 vf->uninit(vf);
818 free_mp_image(vf->imgctx.static_images[0]);
819 free_mp_image(vf->imgctx.static_images[1]);
820 free_mp_image(vf->imgctx.temp_images[0]);
821 free_mp_image(vf->imgctx.export_images[0]);
822 for (int i = 0; i < NUM_NUMBERED_MPI; i++)
823 free_mp_image(vf->imgctx.numbered_images[i]);
824 free(vf);
827 void vf_uninit_filter_chain(vf_instance_t *vf)
829 while (vf) {
830 vf_instance_t *next = vf->next;
831 vf_uninit_filter(vf);
832 vf = next;
836 void vf_detc_init_pts_buf(struct vf_detc_pts_buf *p)
838 p->inpts_prev = MP_NOPTS_VALUE;
839 p->outpts_prev = MP_NOPTS_VALUE;
840 p->lastdelta = 0;
843 static double vf_detc_adjust_pts_internal(struct vf_detc_pts_buf *p,
844 double pts, bool reset_pattern,
845 bool skip_frame, double delta,
846 double boundfactor_minus,
847 double increasefactor,
848 double boundfactor_plus)
850 double newpts;
852 if (pts == MP_NOPTS_VALUE)
853 return pts;
855 if (delta <= 0) {
856 if (p->inpts_prev == MP_NOPTS_VALUE)
857 delta = 0;
858 else if (pts == p->inpts_prev)
859 delta = p->lastdelta;
860 else
861 delta = pts - p->inpts_prev;
863 p->inpts_prev = pts;
864 p->lastdelta = delta;
866 if (skip_frame)
867 return MP_NOPTS_VALUE;
869 /* detect bogus deltas and then passthru pts (possibly caused by seeking,
870 * or bad input) */
871 if (p->outpts_prev == MP_NOPTS_VALUE || reset_pattern || delta <= 0.0 ||
872 delta >= 0.5)
873 newpts = pts;
874 else {
875 // turn 5 frames into 4
876 newpts = p->outpts_prev + delta * increasefactor;
878 // bound to input pts in a sensible way; these numbers come because we
879 // map frames the following way when ivtc'ing:
880 // 0/30 -> 0/24 diff=0
881 // 1/30 -> 1/24 diff=1/120
882 // 2/30 -> -
883 // 3/30 -> 2/24 diff=-1/60
884 // 4/30 -> 3/24 diff=-1/120
885 if (newpts < pts - delta * boundfactor_minus)
886 newpts = pts - delta * boundfactor_minus;
887 if (newpts > pts + delta * boundfactor_plus)
888 newpts = pts + delta * boundfactor_plus;
889 if (newpts < p->outpts_prev)
890 newpts = p->outpts_prev; // damage control
892 p->outpts_prev = newpts;
894 return newpts;
897 double vf_detc_adjust_pts(struct vf_detc_pts_buf *p, double pts,
898 bool reset_pattern, bool skip_frame)
900 // standard telecine (see above)
901 return vf_detc_adjust_pts_internal(p, pts, reset_pattern, skip_frame,
902 0, 0.5, 1.25, 0.25);
905 double vf_softpulldown_adjust_pts(struct vf_detc_pts_buf *p, double pts,
906 bool reset_pattern, bool skip_frame,
907 int last_frame_duration)
909 // for the softpulldown filter we get:
910 // 0/60 -> 0/30
911 // 2/60 -> 1/30
912 // 5/60 -> 2/30
913 // 7/60 -> 3/30, 4/30
914 return vf_detc_adjust_pts_internal(p, pts, reset_pattern, skip_frame,
915 0, 1.0 / last_frame_duration,
916 2.0 / last_frame_duration,
917 1.0 / last_frame_duration);