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.
31 #include "img_format.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
[] = {
115 #ifdef HAVE_POSIX_SELECT
131 #ifdef CONFIG_LIBPOSTPROC
158 &vf_info_perspective
,
159 &vf_info_down3dright
,
171 &vf_info_softpulldown
,
177 &vf_info_remove_logo
,
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 //============================================================================
210 void vf_mpi_clear(mp_image_t
*mpi
, int x0
, int y0
, int w
, int h
)
213 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) {
216 if (x0
== 0 && w
== mpi
->width
) {
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
));
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
));
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;
245 #define CLEAR_PACKEDYUV_PATTERN 0x00800080
246 #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x80008000
248 #define CLEAR_PACKEDYUV_PATTERN 0x80008000
249 #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x00800080
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
;
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
;
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
;
272 int number
= mp_imgtype
>> 16;
274 assert(w
== -1 || w
>= vf
->w
);
275 assert(h
== -1 || 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];
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];
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];
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];
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;
323 case MP_IMGTYPE_NUMBERED
:
326 for (i
= 0; i
< NUM_NUMBERED_MPI
; i
++)
327 if (!vf
->imgctx
.numbered_images
[i
] ||
328 !vf
->imgctx
.numbered_images
[i
]->usage_count
)
332 if (number
< 0 || number
>= NUM_NUMBERED_MPI
)
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
;
341 mpi
->type
= mp_imgtype
;
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
);
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");
363 mpi
->chroma_width
= (w2
+ (1 << mpi
->chroma_x_shift
) - 1) >>
366 mpi
->chroma_height
= (h
+ (1 << mpi
->chroma_y_shift
) - 1) >>
370 mp_image_setfmt(mpi
, outfmt
);
371 if (!(mpi
->flags
& MP_IMGFLAG_ALLOCATED
) &&
372 mpi
->type
> MP_IMGTYPE_EXPORT
) {
373 // check libvo first!
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");
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
);
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)} "
401 if (flags
& VFCAP_ACCEPT_STRIDE
) {
404 (w2
+ (1 << mpi
->chroma_x_shift
) - 1) >>
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
)
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",
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
;
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
)
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
)
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
))
470 vf
= calloc(1, sizeof *vf
);
472 vf
->info
= filter_list
[i
];
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
);
484 for (n
= 0; args
&& args
[2 * n
]; n
++)
485 m_struct_set(st
, vf_priv
, args
[2 * n
], bstr(args
[2 * n
+ 1]));
488 } else // Otherwise we should have the '_oldargs_'
489 if (args
&& !strcmp(args
[0], "_oldargs_"))
490 args
= (char **)args
[1];
493 *retcode
= vf
->info
->vf_open(vf
, (char *)args
);
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
,
505 struct vf_instance
*vf
= vf_open_plugin_noerr(opts
, filter_list
, next
,
506 name
, args
, &(int){0});
508 mp_tmsg(MSGT_VFILTER
, MSGL_ERR
, "Couldn't open video filter '%s'.\n",
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_")) {
518 for (i
= 0; args
&& args
[2 * i
]; i
++)
519 l
+= 1 + strlen(args
[2 * i
]) + 1 + strlen(args
[2 * i
+ 1]);
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]);
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
)
555 new = vf_open_filter(opts
, vo
, name
, args
);
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;
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
) {
582 if (ret
& VFCAP_CSP_SUPPORTED
&& !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
);
593 return 0; // failed to init "scale"
594 // try the preferred csp first:
595 if (preferred
&& vf
->query_format(vf
, preferred
))
598 // try the list again, now with "scaler" :
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
) {
608 if (ret
& VFCAP_CSP_SUPPORTED
&& !best
)
613 *vfp
= vf
; // else uninit vf !FIXME!
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
)
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
)
650 tmp
= last
->continue_buffered_image
;
651 last
->continue_buffered_image
= NULL
;
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
)
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");
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
);
692 vf
->fmt
.have_configured
= 0;
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
;
702 int flags
= vf
->next
->query_format(vf
->next
, outfmt
);
704 // hmm. colorspace mismatch!!!
705 // let's insert the 'scale' filter, it does the job for us:
707 if (vf
->next
->info
== &vf_info_scale
)
708 return 0; // scale->scale
709 vf2
= vf_open_filter(opts
, vf
->next
, "scale", NULL
);
711 return 0; // shouldn't happen!
713 flags
= vf
->next
->query_format(vf
->next
, outfmt
);
715 mp_tmsg(MSGT_VFILTER
, MSGL_ERR
, "Cannot find matching colorspace, "
716 "even by inserting 'scale' :(\n");
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
);
728 return 0; // shouldn't happen!
732 vf
->next
->h
= height
;
733 return vf_config_wrapper(vf
->next
, width
, height
, d_width
, d_height
,
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
);
746 flags
|= vf
->default_caps
;
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
);
763 mp_msg(MSGT_VFILTER
, MSGL_ERR
,
764 "draw_slice: dmpi not stored by vf_%s\n", vf
->info
->name
);
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],
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
;
798 // We want to add them in the 'right order'
799 for (i
= 0; vf_settings
[i
].name
; i
++)
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
);
812 //============================================================================
814 void vf_uninit_filter(vf_instance_t
*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
]);
827 void vf_uninit_filter_chain(vf_instance_t
*vf
)
830 vf_instance_t
*next
= vf
->next
;
831 vf_uninit_filter(vf
);
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
;
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
)
852 if (pts
== MP_NOPTS_VALUE
)
856 if (p
->inpts_prev
== MP_NOPTS_VALUE
)
858 else if (pts
== p
->inpts_prev
)
859 delta
= p
->lastdelta
;
861 delta
= pts
- p
->inpts_prev
;
864 p
->lastdelta
= delta
;
867 return MP_NOPTS_VALUE
;
869 /* detect bogus deltas and then passthru pts (possibly caused by seeking,
871 if (p
->outpts_prev
== MP_NOPTS_VALUE
|| reset_pattern
|| delta
<= 0.0 ||
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
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
;
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
,
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:
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
);