Skip to content

Commit

Permalink
vf_delogo: allow to change the rectangle based on the time
Browse files Browse the repository at this point in the history
git-svn-id: svn://git.mplayerhq.hu/mplayer/trunk@33488 b3059339-0415-0410-9bf9-f77b7e298cf2
  • Loading branch information
cigaes committed May 23, 2011
1 parent 7e51c2e commit ec2ca56
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 17 deletions.
3 changes: 3 additions & 0 deletions Changelog
Expand Up @@ -9,6 +9,9 @@ MPlayer (1.0)
Demuxers:
* experimental support for using binary Quicktime codecs with -demuxer lavf.

Filters:
* delogo: allow to change the rectangle based on the time.

Other:
* experimental support for PGS (BluRay-compatible), DVB and XSUB subtitles.
* experimental af_cmdline slave command to change e.g. audio equalizer options at runtime.
Expand Down
4 changes: 4 additions & 0 deletions DOCS/man/en/mplayer.1
Expand Up @@ -7699,6 +7699,10 @@ width and height of the cleared rectangle
Thickness of the fuzzy edge of the rectangle (added to w and h).
When set to \-1, a green rectangle is drawn on the screen to
simplify finding the right x,y,w,h parameters.
.IPs file=<file>
You can specify a text file to load the coordinates from.
Each line must have a timestamp (in seconds, and in ascending order) and the
"x:y:w:h:t" coordinates (t can be omitted).
.RE
.PD 1
.
Expand Down
147 changes: 130 additions & 17 deletions libmpcodecs/vf_delogo.c
Expand Up @@ -24,6 +24,8 @@
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <limits.h>
#include <errno.h>
#include <math.h>

#include "mp_msg.h"
Expand All @@ -41,14 +43,63 @@
static struct vf_priv_s {
unsigned int outfmt;
int xoff, yoff, lw, lh, band, show;
const char *file;
struct timed_rectangle {
int ts, x, y, w, h, b;
} *timed_rect;
int n_timed_rect;
int cur_timed_rect;
} const vf_priv_dflt = {
0,
0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0,
NULL, NULL, 0, 0,
};

#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#define MAX(a,b) (((a) > (b)) ? (a) : (b))

/**
* Adjust the coordinates to suit the band width
* Also print a notice in verbose mode
*/
static void fix_band(struct vf_priv_s *p)
{
p->show = 0;
if (p->band < 0) {
p->band = 4;
p->show = 1;
}
p->lw += p->band*2;
p->lh += p->band*2;
p->xoff -= p->band;
p->yoff -= p->band;
mp_msg(MSGT_VFILTER, MSGL_V, "delogo: %d x %d, %d x %d, band = %d\n",
p->xoff, p->yoff, p->lw, p->lh, p->band);
}

static void update_sub(struct vf_priv_s *p, double pts)
{
int ipts = pts * 1000;
int tr = p->cur_timed_rect;
while (tr < p->n_timed_rect - 1 && ipts >= p->timed_rect[tr + 1].ts)
tr++;
while (tr >= 0 && ipts < p->timed_rect[tr].ts)
tr--;
if (tr == p->cur_timed_rect)
return;
p->cur_timed_rect = tr;
if (tr >= 0) {
p->xoff = p->timed_rect[tr].x;
p->yoff = p->timed_rect[tr].y;
p->lw = p->timed_rect[tr].w;
p->lh = p->timed_rect[tr].h;
p->band = p->timed_rect[tr].b;
} else {
p->xoff = p->yoff = p->lw = p->lh = p->band = 0;
}
fix_band(p);
}

static void delogo(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int width, int height,
int logo_x, int logo_y, int logo_w, int logo_h, int band, int show, int direct) {
int y, x;
Expand Down Expand Up @@ -154,6 +205,8 @@ static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
}
dmpi= vf->dmpi;

if (vf->priv->timed_rect)
update_sub(vf->priv, pts);
delogo(dmpi->planes[0], mpi->planes[0], dmpi->stride[0], mpi->stride[0], mpi->w, mpi->h,
vf->priv->xoff, vf->priv->yoff, vf->priv->lw, vf->priv->lh, vf->priv->band, vf->priv->show,
mpi->flags&MP_IMGFLAG_DIRECT);
Expand Down Expand Up @@ -196,30 +249,89 @@ static const unsigned int fmt_list[]={
0
};

static int load_timed_rectangles(struct vf_priv_s *delogo)
{
FILE *f;
char line[LINE_MAX];
int lineno = 0, p;
double ts, last_ts = 0;
struct timed_rectangle *rect = NULL, *nr;
int n_rect = 0, alloc_rect = 0;

f = fopen(delogo->file, "r");
if (!f) {
mp_msg(MSGT_VFILTER, MSGL_ERR, "delogo: unable to load %s: %s\n",
delogo->file, strerror(errno));
return -1;
}
while (fgets(line, sizeof(line), f)) {
lineno++;
if (*line == '#' || *line == '\n')
continue;
if (n_rect == alloc_rect) {
if (alloc_rect > INT_MAX / 2 / (int)sizeof(*rect)) {
mp_msg(MSGT_VFILTER, MSGL_WARN,
"delogo: too many rectangles\n");
goto load_error;
}
alloc_rect = alloc_rect ? 2 * alloc_rect : 256;
nr = realloc(rect, alloc_rect * sizeof(*rect));
if (!nr) {
mp_msg(MSGT_VFILTER, MSGL_WARN, "delogo: out of memory\n");
goto load_error;
}
rect = nr;
}
nr = rect + n_rect;
memset(nr, 0, sizeof(*nr));
p = sscanf(line, "%lf %d:%d:%d:%d:%d",
&ts, &nr->x, &nr->y, &nr->w, &nr->h, &nr->b);
if ((p == 2 && !nr->x) || p == 5 || p == 6) {
if (ts <= last_ts)
mp_msg(MSGT_VFILTER, MSGL_WARN, "delogo: %s:%d: wrong time\n",
delogo->file, lineno);
nr->ts = 1000 * ts + 0.5;
n_rect++;
} else {
mp_msg(MSGT_VFILTER, MSGL_WARN, "delogo: %s:%d: syntax error\n",
delogo->file, lineno);
}
}
fclose(f);
if (!n_rect) {
mp_msg(MSGT_VFILTER, MSGL_ERR, "delogo: %s: no rectangles found\n",
delogo->file);
free(rect);
return -1;
}
nr = realloc(rect, n_rect * sizeof(*rect));
if (nr)
rect = nr;
delogo->timed_rect = rect;
delogo->n_timed_rect = n_rect;
return 0;

load_error:
free(rect);
fclose(f);
return -1;
}

static int vf_open(vf_instance_t *vf, char *args){
vf->config=config;
vf->put_image=put_image;
vf->get_image=get_image;
vf->query_format=query_format;
vf->uninit=uninit;

mp_msg(MSGT_VFILTER, MSGL_V, "delogo: %d x %d, %d x %d, band = %d\n",
vf->priv->xoff, vf->priv->yoff,
vf->priv->lw, vf->priv->lh,
vf->priv->band);

vf->priv->show = 0;

if (vf->priv->band < 0) {
vf->priv->band = 4;
vf->priv->show = 1;
if (vf->priv->file) {
if (load_timed_rectangles(vf->priv))
return 0;
mp_msg(MSGT_VFILTER, MSGL_V, "delogo: %d from %s\n",
vf->priv->n_timed_rect, vf->priv->file);
vf->priv->cur_timed_rect = -1;
}


vf->priv->lw += vf->priv->band*2;
vf->priv->lh += vf->priv->band*2;
vf->priv->xoff -= vf->priv->band;
vf->priv->yoff -= vf->priv->band;
fix_band(vf->priv);

// check csp:
vf->priv->outfmt=vf_match_csp(&vf->next,fmt_list,IMGFMT_YV12);
Expand All @@ -240,6 +352,7 @@ static const m_option_t vf_opts_fields[] = {
{ "h", ST_OFF(lh), CONF_TYPE_INT, 0, 0, 0, NULL },
{ "t", ST_OFF(band), CONF_TYPE_INT, 0, 0, 0, NULL },
{ "band", ST_OFF(band), CONF_TYPE_INT, 0, 0, 0, NULL }, // alias
{ "file", ST_OFF(file), CONF_TYPE_STRING, 0, 0, 0, NULL },
{ NULL, NULL, 0, 0, 0, 0, NULL }
};

Expand Down

0 comments on commit ec2ca56

Please sign in to comment.