2010-01-30 16:57:40 +00:00
|
|
|
/*
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer 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.
|
|
|
|
*
|
|
|
|
* MPlayer 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 MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2004-03-29 04:39:04 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
2005-11-18 14:39:25 +00:00
|
|
|
#include "config.h"
|
2013-08-06 20:41:30 +00:00
|
|
|
#include "mpvcore/mp_msg.h"
|
2013-12-03 21:29:30 +00:00
|
|
|
#include "mpvcore/m_option.h"
|
2004-03-29 04:39:04 +00:00
|
|
|
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "video/img_format.h"
|
|
|
|
#include "video/mp_image.h"
|
2004-03-29 04:39:04 +00:00
|
|
|
#include "vf.h"
|
|
|
|
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "video/memcpy_pic.h"
|
2004-03-29 04:39:04 +00:00
|
|
|
|
2013-12-03 21:29:30 +00:00
|
|
|
#include "vf_lavfi.h"
|
|
|
|
|
2004-03-29 04:39:04 +00:00
|
|
|
enum mode { PROGRESSIVE, TOP_FIRST, BOTTOM_FIRST,
|
|
|
|
TOP_FIRST_ANALYZE, BOTTOM_FIRST_ANALYZE,
|
|
|
|
ANALYZE, FULL_ANALYZE, AUTO, AUTO_ANALYZE };
|
|
|
|
|
|
|
|
#define fixed_mode(p) ((p)<=BOTTOM_FIRST)
|
|
|
|
|
|
|
|
struct vf_priv_s
|
|
|
|
{
|
2013-12-03 21:29:30 +00:00
|
|
|
int mode;
|
2004-03-29 04:39:04 +00:00
|
|
|
int verbose;
|
|
|
|
unsigned char *buf[3];
|
2013-12-03 21:29:30 +00:00
|
|
|
struct vf_lw_opts *lw_opts;
|
2004-03-29 04:39:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy fields from either current or buffered previous frame to the
|
|
|
|
* output and store the current frame unmodified to the buffer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void do_plane(unsigned char *to, unsigned char *from,
|
|
|
|
int w, int h, int ts, int fs,
|
|
|
|
unsigned char **bufp, enum mode mode)
|
|
|
|
{
|
|
|
|
unsigned char *buf, *end;
|
|
|
|
int top;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2004-03-29 04:39:04 +00:00
|
|
|
if(!*bufp)
|
|
|
|
{
|
|
|
|
mode=PROGRESSIVE;
|
|
|
|
if(!(*bufp=malloc(h*w))) return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(end=to+h*ts, buf=*bufp, top=1; to<end; from+=fs, to+=ts, buf+=w, top^=1)
|
|
|
|
{
|
2012-11-09 00:31:49 +00:00
|
|
|
memcpy(to, mode==(top?BOTTOM_FIRST:TOP_FIRST)?buf:from, w);
|
|
|
|
memcpy(buf, from, w);
|
2004-03-29 04:39:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This macro interpolates the value of both fields at a point halfway
|
|
|
|
* between lines and takes the squared difference. In field resolution
|
|
|
|
* the point is a quarter pixel below a line in one field and a quarter
|
|
|
|
* pixel above a line in other.
|
|
|
|
*
|
|
|
|
* (the result is actually multiplied by 25)
|
|
|
|
*/
|
|
|
|
|
2005-12-05 01:28:59 +00:00
|
|
|
#define diff(a, as, b, bs) (t=((*a-b[bs])<<2)+a[as<<1]-b[-bs], t*t)
|
2004-03-29 04:39:04 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Find which field combination has the smallest average squared difference
|
|
|
|
* between the fields.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static enum mode analyze_plane(unsigned char *old, unsigned char *new,
|
|
|
|
int w, int h, int os, int ns, enum mode mode,
|
2013-07-22 22:45:23 +00:00
|
|
|
int unused, int fields)
|
2004-03-29 04:39:04 +00:00
|
|
|
{
|
|
|
|
double bdiff, pdiff, tdiff, scale;
|
|
|
|
int bdif, tdif, pdif;
|
|
|
|
int top, t;
|
|
|
|
unsigned char *end, *rend;
|
|
|
|
|
|
|
|
if(mode==AUTO)
|
|
|
|
mode=fields&MP_IMGFIELD_ORDERED?fields&MP_IMGFIELD_TOP_FIRST?
|
|
|
|
TOP_FIRST:BOTTOM_FIRST:PROGRESSIVE;
|
|
|
|
else if(mode==AUTO_ANALYZE)
|
|
|
|
mode=fields&MP_IMGFIELD_ORDERED?fields&MP_IMGFIELD_TOP_FIRST?
|
|
|
|
TOP_FIRST_ANALYZE:BOTTOM_FIRST_ANALYZE:FULL_ANALYZE;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2004-03-29 04:39:04 +00:00
|
|
|
if(fixed_mode(mode))
|
|
|
|
bdiff=pdiff=tdiff=65536.0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bdiff=pdiff=tdiff=0.0;
|
|
|
|
|
|
|
|
for(end=new+(h-2)*ns, new+=ns, old+=os, top=0;
|
|
|
|
new<end; new+=ns-w, old+=os-w, top^=1)
|
|
|
|
{
|
|
|
|
pdif=tdif=bdif=0;
|
|
|
|
|
|
|
|
switch(mode)
|
|
|
|
{
|
|
|
|
case TOP_FIRST_ANALYZE:
|
|
|
|
if(top)
|
|
|
|
for(rend=new+w; new<rend; new++, old++)
|
|
|
|
pdif+=diff(new, ns, new, ns),
|
|
|
|
tdif+=diff(new, ns, old, os);
|
|
|
|
else
|
|
|
|
for(rend=new+w; new<rend; new++, old++)
|
|
|
|
pdif+=diff(new, ns, new, ns),
|
|
|
|
tdif+=diff(old, os, new, ns);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BOTTOM_FIRST_ANALYZE:
|
|
|
|
if(top)
|
|
|
|
for(rend=new+w; new<rend; new++, old++)
|
|
|
|
pdif+=diff(new, ns, new, ns),
|
|
|
|
bdif+=diff(old, os, new, ns);
|
|
|
|
else
|
|
|
|
for(rend=new+w; new<rend; new++, old++)
|
|
|
|
pdif+=diff(new, ns, new, ns),
|
|
|
|
bdif+=diff(new, ns, old, os);
|
|
|
|
break;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2004-03-29 04:39:04 +00:00
|
|
|
case ANALYZE:
|
|
|
|
if(top)
|
|
|
|
for(rend=new+w; new<rend; new++, old++)
|
|
|
|
tdif+=diff(new, ns, old, os),
|
|
|
|
bdif+=diff(old, os, new, ns);
|
|
|
|
else
|
|
|
|
for(rend=new+w; new<rend; new++, old++)
|
|
|
|
bdif+=diff(new, ns, old, os),
|
|
|
|
tdif+=diff(old, os, new, ns);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: /* FULL_ANALYZE */
|
|
|
|
if(top)
|
|
|
|
for(rend=new+w; new<rend; new++, old++)
|
|
|
|
pdif+=diff(new, ns, new, ns),
|
|
|
|
tdif+=diff(new, ns, old, os),
|
|
|
|
bdif+=diff(old, os, new, ns);
|
|
|
|
else
|
|
|
|
for(rend=new+w; new<rend; new++, old++)
|
|
|
|
pdif+=diff(new, ns, new, ns),
|
|
|
|
bdif+=diff(new, ns, old, os),
|
|
|
|
tdif+=diff(old, os, new, ns);
|
|
|
|
}
|
|
|
|
|
|
|
|
pdiff+=(double)pdif;
|
|
|
|
tdiff+=(double)tdif;
|
|
|
|
bdiff+=(double)bdif;
|
2009-07-06 23:26:13 +00:00
|
|
|
}
|
2004-03-29 04:39:04 +00:00
|
|
|
|
|
|
|
scale=1.0/(w*(h-3))/25.0;
|
|
|
|
pdiff*=scale;
|
|
|
|
tdiff*=scale;
|
|
|
|
bdiff*=scale;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2004-03-29 04:39:04 +00:00
|
|
|
if(mode==TOP_FIRST_ANALYZE)
|
|
|
|
bdiff=65536.0;
|
|
|
|
else if(mode==BOTTOM_FIRST_ANALYZE)
|
|
|
|
tdiff=65536.0;
|
|
|
|
else if(mode==ANALYZE)
|
|
|
|
pdiff=65536.0;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2004-03-29 04:39:04 +00:00
|
|
|
if(bdiff<pdiff && bdiff<tdiff)
|
|
|
|
mode=BOTTOM_FIRST;
|
|
|
|
else if(tdiff<pdiff && tdiff<bdiff)
|
|
|
|
mode=TOP_FIRST;
|
|
|
|
else
|
|
|
|
mode=PROGRESSIVE;
|
|
|
|
}
|
|
|
|
|
2006-03-27 07:22:48 +00:00
|
|
|
if( mp_msg_test(MSGT_VFILTER,MSGL_V) )
|
2004-03-29 04:39:04 +00:00
|
|
|
{
|
2006-03-31 00:15:47 +00:00
|
|
|
mp_msg(MSGT_VFILTER, MSGL_INFO, "%c", mode==BOTTOM_FIRST?'b':mode==TOP_FIRST?'t':'p');
|
|
|
|
if(tdiff==65536.0) mp_msg(MSGT_VFILTER, MSGL_INFO," N/A "); else mp_msg(MSGT_VFILTER, MSGL_INFO," %8.2f", tdiff);
|
|
|
|
if(bdiff==65536.0) mp_msg(MSGT_VFILTER, MSGL_INFO," N/A "); else mp_msg(MSGT_VFILTER, MSGL_INFO," %8.2f", bdiff);
|
|
|
|
if(pdiff==65536.0) mp_msg(MSGT_VFILTER, MSGL_INFO," N/A "); else mp_msg(MSGT_VFILTER, MSGL_INFO," %8.2f", pdiff);
|
|
|
|
mp_msg(MSGT_VFILTER, MSGL_INFO," \n");
|
2004-03-29 04:39:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
video/filter: change filter API, use refcounting, remove filter DR
Change the entire filter API to use reference counted images instead
of vf_get_image().
Remove filter "direct rendering". This was useful for vf_expand and (in
rare cases) vf_sub: DR allowed these filters to pass a cropped image to
the filters before them. Then, on filtering, the image was "uncropped",
so that black bars could be added around the image without copying. This
means that in some cases, vf_expand will be slower (-vf gradfun,expand
for example).
Note that another form of DR used for in-place filters has been replaced
by simpler logic. Instead of trying to do DR, filters can check if the
image is writeable (with mp_image_is_writeable()), and do true in-place
if that's the case. This affects filters like vf_gradfun and vf_sub.
Everything has to support strides now. If something doesn't, making a
copy of the image data is required.
2012-11-05 13:25:04 +00:00
|
|
|
static struct mp_image *filter(struct vf_instance *vf, struct mp_image *mpi)
|
2004-03-29 04:39:04 +00:00
|
|
|
{
|
|
|
|
enum mode mode;
|
|
|
|
|
video/filter: change filter API, use refcounting, remove filter DR
Change the entire filter API to use reference counted images instead
of vf_get_image().
Remove filter "direct rendering". This was useful for vf_expand and (in
rare cases) vf_sub: DR allowed these filters to pass a cropped image to
the filters before them. Then, on filtering, the image was "uncropped",
so that black bars could be added around the image without copying. This
means that in some cases, vf_expand will be slower (-vf gradfun,expand
for example).
Note that another form of DR used for in-place filters has been replaced
by simpler logic. Instead of trying to do DR, filters can check if the
image is writeable (with mp_image_is_writeable()), and do true in-place
if that's the case. This affects filters like vf_gradfun and vf_sub.
Everything has to support strides now. If something doesn't, making a
copy of the image data is required.
2012-11-05 13:25:04 +00:00
|
|
|
struct mp_image *dmpi = vf_alloc_out_image(vf);
|
|
|
|
mp_image_copy_attributes(dmpi, mpi);
|
2004-03-29 04:39:04 +00:00
|
|
|
|
2012-12-26 20:29:24 +00:00
|
|
|
int pw[MP_MAX_PLANES] = {0};
|
|
|
|
for (int p = 0; p < mpi->num_planes; p++)
|
2013-04-09 22:10:59 +00:00
|
|
|
pw[p] = ((mpi->w * mpi->fmt.bpp[p] + 7) / 8) >> mpi->fmt.xs[p];
|
2004-03-29 04:39:04 +00:00
|
|
|
|
|
|
|
mode=vf->priv->mode;
|
|
|
|
|
|
|
|
if(!vf->priv->buf[0])
|
|
|
|
mode=PROGRESSIVE;
|
|
|
|
else
|
|
|
|
mode=analyze_plane(vf->priv->buf[0], mpi->planes[0],
|
2012-12-26 20:29:24 +00:00
|
|
|
pw[0], dmpi->h, pw[0], mpi->stride[0], mode,
|
2004-03-29 04:39:04 +00:00
|
|
|
vf->priv->verbose, mpi->fields);
|
|
|
|
|
2012-12-26 20:29:24 +00:00
|
|
|
for (int p = 0; p < mpi->num_planes; p++) {
|
|
|
|
do_plane(dmpi->planes[p], mpi->planes[p], pw[p], dmpi->plane_h[p],
|
|
|
|
dmpi->stride[p], mpi->stride[p],
|
|
|
|
&vf->priv->buf[p], mode);
|
|
|
|
}
|
2004-03-29 04:39:04 +00:00
|
|
|
|
video/filter: change filter API, use refcounting, remove filter DR
Change the entire filter API to use reference counted images instead
of vf_get_image().
Remove filter "direct rendering". This was useful for vf_expand and (in
rare cases) vf_sub: DR allowed these filters to pass a cropped image to
the filters before them. Then, on filtering, the image was "uncropped",
so that black bars could be added around the image without copying. This
means that in some cases, vf_expand will be slower (-vf gradfun,expand
for example).
Note that another form of DR used for in-place filters has been replaced
by simpler logic. Instead of trying to do DR, filters can check if the
image is writeable (with mp_image_is_writeable()), and do true in-place
if that's the case. This affects filters like vf_gradfun and vf_sub.
Everything has to support strides now. If something doesn't, making a
copy of the image data is required.
2012-11-05 13:25:04 +00:00
|
|
|
talloc_free(mpi);
|
|
|
|
return dmpi;
|
2004-03-29 04:39:04 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 14:15:55 +00:00
|
|
|
static void uninit(struct vf_instance *vf)
|
2004-03-29 04:39:04 +00:00
|
|
|
{
|
2012-10-26 18:05:30 +00:00
|
|
|
if (!vf->priv)
|
|
|
|
return;
|
2004-03-29 04:39:04 +00:00
|
|
|
free(vf->priv->buf[0]);
|
|
|
|
free(vf->priv->buf[1]);
|
|
|
|
free(vf->priv->buf[2]);
|
|
|
|
free(vf->priv);
|
|
|
|
}
|
|
|
|
|
2012-12-26 20:29:24 +00:00
|
|
|
static int query_format(struct vf_instance *vf, unsigned int fmt)
|
|
|
|
{
|
|
|
|
if (IMGFMT_IS_HWACCEL(fmt))
|
|
|
|
return 0;
|
|
|
|
struct mp_imgfmt_desc desc = mp_imgfmt_get_desc(fmt);
|
|
|
|
if (desc.num_planes > 3)
|
|
|
|
return 0;
|
|
|
|
return vf_next_query_format(vf, fmt);
|
|
|
|
}
|
|
|
|
|
2013-12-03 21:29:30 +00:00
|
|
|
static const char *get_lavfi_mode(int mode)
|
|
|
|
{
|
|
|
|
switch (mode) {
|
|
|
|
case PROGRESSIVE: return "p";
|
|
|
|
case TOP_FIRST: return "t";
|
|
|
|
case BOTTOM_FIRST: return "b";
|
|
|
|
case TOP_FIRST_ANALYZE: return "T";
|
|
|
|
case BOTTOM_FIRST_ANALYZE: return "B";
|
|
|
|
case ANALYZE: return "u";
|
|
|
|
case FULL_ANALYZE: return "U";
|
|
|
|
case AUTO: return "a";
|
|
|
|
case AUTO_ANALYZE: return "A";
|
|
|
|
default: return "?";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-21 13:40:49 +00:00
|
|
|
static int vf_open(vf_instance_t *vf, char *args)
|
2004-03-29 04:39:04 +00:00
|
|
|
{
|
video/filter: change filter API, use refcounting, remove filter DR
Change the entire filter API to use reference counted images instead
of vf_get_image().
Remove filter "direct rendering". This was useful for vf_expand and (in
rare cases) vf_sub: DR allowed these filters to pass a cropped image to
the filters before them. Then, on filtering, the image was "uncropped",
so that black bars could be added around the image without copying. This
means that in some cases, vf_expand will be slower (-vf gradfun,expand
for example).
Note that another form of DR used for in-place filters has been replaced
by simpler logic. Instead of trying to do DR, filters can check if the
image is writeable (with mp_image_is_writeable()), and do true in-place
if that's the case. This affects filters like vf_gradfun and vf_sub.
Everything has to support strides now. If something doesn't, making a
copy of the image data is required.
2012-11-05 13:25:04 +00:00
|
|
|
vf->filter = filter;
|
2004-03-29 04:39:04 +00:00
|
|
|
vf->uninit = uninit;
|
2012-12-26 20:29:24 +00:00
|
|
|
vf->query_format = query_format;
|
2004-03-29 04:39:04 +00:00
|
|
|
|
2013-12-03 21:29:30 +00:00
|
|
|
if (vf_lw_set_graph(vf, vf->priv->lw_opts, "phase", "%s",
|
|
|
|
get_lavfi_mode(vf->priv->mode)) >= 0)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2004-03-29 04:39:04 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-12-03 21:29:30 +00:00
|
|
|
#define OPT_BASE_STRUCT struct vf_priv_s
|
2007-12-02 14:57:15 +00:00
|
|
|
const vf_info_t vf_info_phase =
|
2013-12-03 21:29:30 +00:00
|
|
|
{
|
2013-10-23 17:06:42 +00:00
|
|
|
.description = "phase shift fields",
|
|
|
|
.name = "phase",
|
|
|
|
.open = vf_open,
|
2013-12-03 21:29:30 +00:00
|
|
|
.priv_size = sizeof(struct vf_priv_s),
|
|
|
|
.priv_defaults = &(const struct vf_priv_s){
|
|
|
|
.mode = AUTO_ANALYZE,
|
|
|
|
},
|
|
|
|
.options = (const struct m_option[]){
|
|
|
|
OPT_CHOICE("mode", mode, 0,
|
|
|
|
({"t", TOP_FIRST},
|
|
|
|
{"a", AUTO},
|
|
|
|
{"b", BOTTOM_FIRST},
|
|
|
|
{"u", ANALYZE},
|
|
|
|
{"T", TOP_FIRST_ANALYZE},
|
|
|
|
{"A", AUTO_ANALYZE},
|
|
|
|
{"B", BOTTOM_FIRST_ANALYZE},
|
|
|
|
{"U", FULL_ANALYZE},
|
|
|
|
{"p", PROGRESSIVE})),
|
|
|
|
OPT_FLAG("v", verbose, 0),
|
|
|
|
OPT_SUBSTRUCT("", lw_opts, vf_lw_conf, 0),
|
|
|
|
{0}
|
|
|
|
},
|
|
|
|
};
|