2014-04-29 13:07:21 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
|
|
|
* Parts based on fragments of vo_vdpau.c: Copyright (C) 2009 Uoti Urpala
|
|
|
|
*
|
|
|
|
* mpv is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* mpv is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with mpv. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "common/common.h"
|
|
|
|
#include "common/msg.h"
|
|
|
|
#include "options/m_option.h"
|
|
|
|
|
|
|
|
#include "video/img_format.h"
|
|
|
|
#include "video/mp_image.h"
|
|
|
|
#include "video/hwdec.h"
|
|
|
|
#include "video/vdpau.h"
|
|
|
|
#include "video/vdpau_mixer.h"
|
|
|
|
#include "vf.h"
|
|
|
|
|
|
|
|
// Note: this filter does no actual filtering; it merely sets appropriate
|
|
|
|
// flags on vdpau images (mp_vdpau_mixer_frame) to do the appropriate
|
|
|
|
// processing on the final rendering process in the VO.
|
|
|
|
|
|
|
|
struct vf_priv_s {
|
|
|
|
struct mp_vdpau_ctx *ctx;
|
|
|
|
|
|
|
|
// This is needed to supply past/future fields and to calculate the
|
|
|
|
// interpolated timestamp.
|
|
|
|
struct mp_image *buffered[3];
|
|
|
|
int num_buffered;
|
|
|
|
|
|
|
|
int prev_pos; // last field that was output
|
|
|
|
|
2014-04-29 13:16:06 +00:00
|
|
|
int def_deintmode;
|
|
|
|
int deint_enabled;
|
2014-04-29 13:07:21 +00:00
|
|
|
struct mp_vdpau_mixer_opts opts;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void forget_frames(struct vf_instance *vf)
|
|
|
|
{
|
|
|
|
struct vf_priv_s *p = vf->priv;
|
|
|
|
for (int n = 0; n < p->num_buffered; n++)
|
|
|
|
talloc_free(p->buffered[n]);
|
|
|
|
p->num_buffered = 0;
|
|
|
|
p->prev_pos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define FIELD_VALID(p, f) ((f) >= 0 && (f) < (p)->num_buffered * 2)
|
|
|
|
|
2014-05-25 14:01:33 +00:00
|
|
|
static VdpVideoSurface ref_field(struct vf_priv_s *p,
|
2014-04-29 13:07:21 +00:00
|
|
|
struct mp_vdpau_mixer_frame *frame, int pos)
|
|
|
|
{
|
|
|
|
if (!FIELD_VALID(p, pos))
|
|
|
|
return VDP_INVALID_HANDLE;
|
|
|
|
struct mp_image *mpi = mp_image_new_ref(p->buffered[pos / 2]);
|
video: introduce failure path for image allocations
Until now, failure to allocate image data resulted in a crash (i.e.
abort() was called). This was intentional, because it's pretty silly to
degrade playback, and in almost all situations, the OOM will probably
kill you anyway. (And then there's the standard Linux overcommit
behavior, which also will kill you at some point.)
But I changed my opinion, so here we go. This change does not affect
_all_ memory allocations, just image data. Now in most failure cases,
the output will just be skipped. For video filters, this coincidentally
means that failure is treated as EOF (because the playback core assumes
EOF if nothing comes out of the video filter chain). In other
situations, output might be in some way degraded, like skipping frames,
not scaling OSD, and such.
Functions whose return values changed semantics:
mp_image_alloc
mp_image_new_copy
mp_image_new_ref
mp_image_make_writeable
mp_image_setrefp
mp_image_to_av_frame_and_unref
mp_image_from_av_frame
mp_image_new_external_ref
mp_image_new_custom_ref
mp_image_pool_make_writeable
mp_image_pool_get
mp_image_pool_new_copy
mp_vdpau_mixed_frame_create
vf_alloc_out_image
vf_make_out_image_writeable
glGetWindowScreenshot
2014-06-17 20:43:43 +00:00
|
|
|
if (!mpi)
|
|
|
|
return VDP_INVALID_HANDLE;
|
2014-04-29 13:07:21 +00:00
|
|
|
talloc_steal(frame, mpi);
|
|
|
|
return (uintptr_t)mpi->planes[3];
|
|
|
|
}
|
|
|
|
|
|
|
|
// pos==0 means last field of latest frame, 1 earlier field of latest frame,
|
|
|
|
// 2 last field of previous frame and so on
|
|
|
|
static bool output_field(struct vf_instance *vf, int pos)
|
|
|
|
{
|
|
|
|
struct vf_priv_s *p = vf->priv;
|
|
|
|
|
|
|
|
if (!FIELD_VALID(p, pos))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
struct mp_image *mpi = mp_vdpau_mixed_frame_create(p->buffered[pos / 2]);
|
video: introduce failure path for image allocations
Until now, failure to allocate image data resulted in a crash (i.e.
abort() was called). This was intentional, because it's pretty silly to
degrade playback, and in almost all situations, the OOM will probably
kill you anyway. (And then there's the standard Linux overcommit
behavior, which also will kill you at some point.)
But I changed my opinion, so here we go. This change does not affect
_all_ memory allocations, just image data. Now in most failure cases,
the output will just be skipped. For video filters, this coincidentally
means that failure is treated as EOF (because the playback core assumes
EOF if nothing comes out of the video filter chain). In other
situations, output might be in some way degraded, like skipping frames,
not scaling OSD, and such.
Functions whose return values changed semantics:
mp_image_alloc
mp_image_new_copy
mp_image_new_ref
mp_image_make_writeable
mp_image_setrefp
mp_image_to_av_frame_and_unref
mp_image_from_av_frame
mp_image_new_external_ref
mp_image_new_custom_ref
mp_image_pool_make_writeable
mp_image_pool_get
mp_image_pool_new_copy
mp_vdpau_mixed_frame_create
vf_alloc_out_image
vf_make_out_image_writeable
glGetWindowScreenshot
2014-06-17 20:43:43 +00:00
|
|
|
if (!mpi)
|
|
|
|
return false; // skip output on OOM
|
2014-04-29 13:07:21 +00:00
|
|
|
struct mp_vdpau_mixer_frame *frame = mp_vdpau_mixed_frame_get(mpi);
|
|
|
|
|
|
|
|
frame->field = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME;
|
|
|
|
if (p->opts.deint) {
|
2015-04-23 20:06:14 +00:00
|
|
|
int top_field_first = !!(mpi->fields & MP_IMGFIELD_TOP_FIRST);
|
2014-04-29 13:07:21 +00:00
|
|
|
frame->field = top_field_first ^ (pos & 1) ?
|
|
|
|
VDP_VIDEO_MIXER_PICTURE_STRUCTURE_BOTTOM_FIELD:
|
|
|
|
VDP_VIDEO_MIXER_PICTURE_STRUCTURE_TOP_FIELD;
|
|
|
|
}
|
|
|
|
|
2014-05-25 14:01:33 +00:00
|
|
|
frame->future[0] = ref_field(p, frame, pos - 1);
|
|
|
|
frame->current = ref_field(p, frame, pos);
|
|
|
|
frame->past[0] = ref_field(p, frame, pos + 1);
|
|
|
|
frame->past[1] = ref_field(p, frame, pos + 2);
|
2014-04-29 13:07:21 +00:00
|
|
|
|
|
|
|
frame->opts = p->opts;
|
|
|
|
|
|
|
|
mpi->planes[3] = (void *)(uintptr_t)frame->current;
|
|
|
|
|
|
|
|
// Interpolate timestamps of extra fields (these always have even indexes)
|
|
|
|
int idx = pos / 2;
|
|
|
|
if (idx > 0 && !(pos & 1) && p->opts.deint >= 2) {
|
|
|
|
double pts1 = p->buffered[idx - 1]->pts;
|
|
|
|
double pts2 = p->buffered[idx]->pts;
|
|
|
|
double diff = pts1 - pts2;
|
|
|
|
mpi->pts = diff > 0 && diff < 0.5 ? (pts1 + pts2) / 2 : pts2;
|
|
|
|
}
|
|
|
|
|
|
|
|
vf_add_output_frame(vf, mpi);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int filter_ext(struct vf_instance *vf, struct mp_image *mpi)
|
|
|
|
{
|
|
|
|
struct vf_priv_s *p = vf->priv;
|
2014-05-04 08:54:31 +00:00
|
|
|
int maxbuffer = p->opts.deint >= 2 ? 3 : 2;
|
2014-04-29 13:07:21 +00:00
|
|
|
bool eof = !mpi;
|
|
|
|
|
2014-05-04 08:50:32 +00:00
|
|
|
if (mpi) {
|
|
|
|
struct mp_image *new = mp_vdpau_upload_video_surface(p->ctx, mpi);
|
|
|
|
talloc_free(mpi);
|
|
|
|
if (!new)
|
2014-04-29 13:10:57 +00:00
|
|
|
return -1;
|
2014-05-04 08:50:32 +00:00
|
|
|
mpi = new;
|
2014-04-29 13:07:21 +00:00
|
|
|
|
2014-05-04 08:54:31 +00:00
|
|
|
if (mp_vdpau_mixed_frame_get(mpi)) {
|
2014-04-29 13:10:57 +00:00
|
|
|
MP_ERR(vf, "Can't apply vdpaupp filter multiple times.\n");
|
|
|
|
vf_add_output_frame(vf, mpi);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-04-29 13:16:06 +00:00
|
|
|
while (p->num_buffered >= maxbuffer) {
|
2014-04-29 13:07:21 +00:00
|
|
|
talloc_free(p->buffered[p->num_buffered - 1]);
|
|
|
|
p->num_buffered--;
|
|
|
|
}
|
|
|
|
for (int n = p->num_buffered; n > 0; n--)
|
|
|
|
p->buffered[n] = p->buffered[n - 1];
|
|
|
|
p->buffered[0] = mpi;
|
|
|
|
p->num_buffered++;
|
|
|
|
p->prev_pos += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
int current = p->prev_pos - 1;
|
2014-05-04 08:54:31 +00:00
|
|
|
if (!FIELD_VALID(p, current))
|
2014-04-29 13:07:21 +00:00
|
|
|
break;
|
|
|
|
// No field-splitting deinterlace -> only output first field (odd index)
|
2014-05-04 08:54:31 +00:00
|
|
|
if ((current & 1) || p->opts.deint >= 2) {
|
|
|
|
// Wait for enough future frames being buffered.
|
|
|
|
// (Past frames are always around if available at all.)
|
|
|
|
if (!eof && !FIELD_VALID(p, current - 1))
|
|
|
|
break;
|
2014-04-29 13:07:21 +00:00
|
|
|
if (!output_field(vf, current))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p->prev_pos = current;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int reconfig(struct vf_instance *vf, struct mp_image_params *in,
|
|
|
|
struct mp_image_params *out)
|
|
|
|
{
|
|
|
|
forget_frames(vf);
|
|
|
|
*out = *in;
|
|
|
|
out->imgfmt = IMGFMT_VDPAU;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int query_format(struct vf_instance *vf, unsigned int fmt)
|
|
|
|
{
|
2014-04-29 13:10:57 +00:00
|
|
|
if (fmt == IMGFMT_VDPAU || mp_vdpau_get_format(fmt, NULL, NULL))
|
2014-04-29 13:07:21 +00:00
|
|
|
return vf_next_query_format(vf, IMGFMT_VDPAU);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int control(vf_instance_t *vf, int request, void *data)
|
|
|
|
{
|
2014-04-29 13:16:06 +00:00
|
|
|
struct vf_priv_s *p = vf->priv;
|
|
|
|
|
2014-04-29 13:07:21 +00:00
|
|
|
switch (request) {
|
|
|
|
case VFCTRL_SEEK_RESET:
|
|
|
|
forget_frames(vf);
|
|
|
|
return CONTROL_OK;
|
2014-04-29 13:16:06 +00:00
|
|
|
case VFCTRL_GET_DEINTERLACE:
|
|
|
|
*(int *)data = !!p->deint_enabled;
|
|
|
|
return true;
|
|
|
|
case VFCTRL_SET_DEINTERLACE:
|
|
|
|
p->deint_enabled = !!*(int *)data;
|
|
|
|
p->opts.deint = p->deint_enabled ? p->def_deintmode : 0;
|
|
|
|
return true;
|
2014-04-29 13:07:21 +00:00
|
|
|
}
|
|
|
|
return CONTROL_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uninit(struct vf_instance *vf)
|
|
|
|
{
|
|
|
|
forget_frames(vf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vf_open(vf_instance_t *vf)
|
|
|
|
{
|
|
|
|
struct vf_priv_s *p = vf->priv;
|
|
|
|
|
|
|
|
vf->reconfig = reconfig;
|
|
|
|
vf->filter_ext = filter_ext;
|
|
|
|
vf->filter = NULL;
|
|
|
|
vf->query_format = query_format;
|
|
|
|
vf->control = control;
|
|
|
|
vf->uninit = uninit;
|
|
|
|
|
2015-01-22 14:32:23 +00:00
|
|
|
if (!vf->hwdec)
|
|
|
|
return 0;
|
2014-04-29 13:07:21 +00:00
|
|
|
hwdec_request_api(vf->hwdec, "vdpau");
|
2015-01-22 14:32:23 +00:00
|
|
|
p->ctx = vf->hwdec->hwctx ? vf->hwdec->hwctx->vdpau_ctx : NULL;
|
2014-04-29 13:07:21 +00:00
|
|
|
if (!p->ctx)
|
|
|
|
return 0;
|
|
|
|
|
2014-04-29 13:16:06 +00:00
|
|
|
p->def_deintmode = p->opts.deint;
|
|
|
|
if (!p->deint_enabled)
|
|
|
|
p->opts.deint = 0;
|
|
|
|
|
2014-04-29 13:07:21 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define OPT_BASE_STRUCT struct vf_priv_s
|
|
|
|
static const m_option_t vf_opts_fields[] = {
|
2014-04-29 13:16:06 +00:00
|
|
|
OPT_CHOICE("deint-mode", opts.deint, 0,
|
|
|
|
({"first-field", 1},
|
2014-04-29 13:07:21 +00:00
|
|
|
{"bob", 2},
|
|
|
|
{"temporal", 3},
|
|
|
|
{"temporal-spatial", 4}),
|
|
|
|
OPTDEF_INT(3)),
|
2014-04-29 13:16:06 +00:00
|
|
|
OPT_FLAG("deint", deint_enabled, 0),
|
2014-04-29 13:07:21 +00:00
|
|
|
OPT_FLAG("chroma-deint", opts.chroma_deint, 0, OPTDEF_INT(1)),
|
|
|
|
OPT_FLAG("pullup", opts.pullup, 0),
|
|
|
|
OPT_FLOATRANGE("denoise", opts.denoise, 0, 0, 1),
|
|
|
|
OPT_FLOATRANGE("sharpen", opts.sharpen, 0, -1, 1),
|
|
|
|
OPT_INTRANGE("hqscaling", opts.hqscaling, 0, 0, 9),
|
|
|
|
{0}
|
|
|
|
};
|
|
|
|
|
|
|
|
const vf_info_t vf_info_vdpaupp = {
|
|
|
|
.description = "vdpau postprocessing",
|
|
|
|
.name = "vdpaupp",
|
|
|
|
.open = vf_open,
|
|
|
|
.priv_size = sizeof(struct vf_priv_s),
|
|
|
|
.options = vf_opts_fields,
|
|
|
|
};
|