aboutsummaryrefslogtreecommitdiffstats
path: root/libavfilter/libmpcodecs/vf_dint.c
blob: ac5bf54a545ff75cf612d99cacf55f3f7e1df100 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * 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.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>

#include "config.h"
#include "mp_msg.h"

#include "mp_image.h"
#include "img_format.h"
#include "vf.h"

struct vf_priv_s {
  float sense; // first parameter
  float level; // second parameter
  unsigned int imgfmt;
  int diff;
  uint32_t max;
//  int dfr;
//  int rdfr;
  int was_dint;
  mp_image_t *pmpi; // previous mpi
};

#define MAXROWSIZE 1200

static int config (struct vf_instance *vf,
        int width, int height, int d_width, int d_height,
        unsigned int flags, unsigned int outfmt)
{
    int rowsize;

    vf->priv->pmpi = vf_get_image (vf->next, outfmt, MP_IMGTYPE_TEMP,
                                   0, width, height);
    if (!(vf->priv->pmpi->flags & MP_IMGFLAG_PLANAR) &&
        outfmt != IMGFMT_RGB32 && outfmt != IMGFMT_BGR32 &&
        outfmt != IMGFMT_RGB24 && outfmt != IMGFMT_BGR24 &&
        outfmt != IMGFMT_RGB16 && outfmt != IMGFMT_BGR16)
    {
      mp_msg (MSGT_VFILTER, MSGL_WARN, "Drop-interlaced filter doesn't support this outfmt :(\n");
      return 0;
    }
    vf->priv->imgfmt = outfmt;
    // recalculate internal values
    rowsize = vf->priv->pmpi->width;
    if (rowsize > MAXROWSIZE) rowsize = MAXROWSIZE;
    vf->priv->max = vf->priv->level * vf->priv->pmpi->height * rowsize / 2;
    if (vf->priv->pmpi->flags & MP_IMGFLAG_PLANAR) // planar YUV
      vf->priv->diff = vf->priv->sense * 256;
    else
      vf->priv->diff = vf->priv->sense * (1 << (vf->priv->pmpi->bpp/3));
    if (vf->priv->diff < 0) vf->priv->diff = 0;
    if (!(vf->priv->pmpi->flags & MP_IMGFLAG_PLANAR) &&
        vf->priv->pmpi->bpp < 24 && vf->priv->diff > 31)
      vf->priv->diff = 31;
    mp_msg (MSGT_VFILTER, MSGL_INFO, "Drop-interlaced: %dx%d diff %d / level %u\n",
           vf->priv->pmpi->width, vf->priv->pmpi->height,
           vf->priv->diff, (unsigned int)vf->priv->max);
//    vf->priv->rdfr = vf->priv->dfr = 0;
    vf->priv->was_dint = 0;
    return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
}

static int put_image (struct vf_instance *vf, mp_image_t *mpi, double pts)
{
    int8_t rrow0[MAXROWSIZE];
    int8_t rrow1[MAXROWSIZE];
    int8_t rrow2[MAXROWSIZE];
    int8_t *row0 = rrow0, *row1 = rrow1, *row2 = rrow2/*, *row3 = rrow3*/;
    int rowsize = mpi->width;
    uint32_t nok = 0, max = vf->priv->max;
    int diff = vf->priv->diff;
    int i, j;
    register int n1, n2;
    unsigned char *cur0, *prv0;
    register unsigned char *cur, *prv;

    if (rowsize > MAXROWSIZE) rowsize = MAXROWSIZE;
    // check if nothing to do
    if (mpi->imgfmt == vf->priv->imgfmt)
    {
      cur0 = mpi->planes[0] + mpi->stride[0];
      prv0 = mpi->planes[0];
      for (j = 1; j < mpi->height && nok <= max; j++)
      {
        cur = cur0;
        prv = prv0;
        // analyse row (row0)
        if (mpi->flags & MP_IMGFLAG_PLANAR) // planar YUV - check luminance
          for (i = 0; i < rowsize; i++)
          {
            if (cur[0] - prv[0] > diff)
              row0[i] = 1;
            else if (cur[0] - prv[0] < -diff)
              row0[i] = -1;
            else
              row0[i] = 0;
            cur++;
            prv++;
            // check if row0 is 1 but row1 is 0, and row2 is 1 or row2 is 0
            // but row3 is 1 so it's interlaced ptr (nok++)
            if (j > 2 && row0[i] > 0 && (row1[i] < 0 || (!row1[i] && row2[i] < 0)) &&
                (++nok) > max)
              break;
          }
        else if (mpi->bpp < 24) // RGB/BGR 16 - check all colors
          for (i = 0; i < rowsize; i++)
          {
            n1 = cur[0] + (cur[1]<<8);
            n2 = prv[0] + (prv[1]<<8);
            if ((n1&0x1f) - (n2&0x1f) > diff ||
                ((n1>>5)&0x3f) - ((n2>>5)&0x3f) > diff ||
                ((n1>>11)&0x1f) - ((n2>>11)&0x1f) > diff)
              row0[i] = 1;
            else if ((n1&0x1f) - (n2&0x1f) < -diff ||
                     ((n1>>5)&0x3f) - ((n2>>5)&0x3f) < -diff ||
                     ((n1>>11)&0x1f) - ((n2>>11)&0x1f) < -diff)
              row0[i] = -1;
            else
              row0[i] = 0;
            cur += 2;
            prv += 2;
            // check if row0 is 1 but row1 is 0, and row2 is 1 or row2 is 0
            // but row3 is 1 so it's interlaced ptr (nok++)
            if (j > 2 && row0[i] > 0 && (row1[i] < 0 || (!row1[i] && row2[i] < 0)) &&
                (++nok) > max)
              break;
          }
        else // RGB/BGR 24/32
          for (i = 0; i < rowsize; i++)
          {
            if (cur[0] - prv[0] > diff ||
                cur[1] - prv[1] > diff ||
                cur[2] - prv[2] > diff)
              row0[i] = 1;
            else if (prv[0] - cur[0] > diff ||
                     prv[1] - cur[1] > diff ||
                     prv[2] - cur[2] > diff)
              row0[i] = -1;
            else
              row0[i] = 0;
            cur += mpi->bpp/8;
            prv += mpi->bpp/8;
            // check if row0 is 1 but row1 is 0, and row2 is 1 or row2 is 0
            // but row3 is 1 so it's interlaced ptr (nok++)
            if (j > 2 && row0[i] > 0 && (row1[i] < 0 || (!row1[i] && row2[i] < 0)) &&
                (++nok) > max)
              break;
          }
        cur0 += mpi->stride[0];
        prv0 += mpi->stride[0];
        // rotate rows
        cur = row2;
        row2 = row1;
        row1 = row0;
        row0 = cur;
      }
    }
    // check if number of interlaced is above of max
    if (nok > max)
    {
//    vf->priv->dfr++;
      if (vf->priv->was_dint < 1) // can skip at most one frame!
      {
        vf->priv->was_dint++;
//      vf->priv->rdfr++;
//      mp_msg (MSGT_VFILTER, MSGL_INFO, "DI:%d/%d ", vf->priv->rdfr, vf->priv->dfr);
        return 0;
      }
    }
    vf->priv->was_dint = 0;
//    mp_msg (MSGT_VFILTER, MSGL_INFO, "DI:%d/%d ", vf->priv->rdfr, vf->priv->dfr);
    return vf_next_put_image (vf, mpi, pts);
}

static int vf_open(vf_instance_t *vf, char *args){
    vf->config = config;
    vf->put_image = put_image;
//  vf->default_reqs=VFCAP_ACCEPT_STRIDE;
    vf->priv = malloc (sizeof(struct vf_priv_s));
    vf->priv->sense = 0.1;
    vf->priv->level = 0.15;
    vf->priv->pmpi = NULL;
    if (args)
      sscanf (args, "%f:%f", &vf->priv->sense, &vf->priv->level);
    return 1;
}

const vf_info_t vf_info_dint = {
    "drop interlaced frames",
    "dint",
    "A.G.",
    "",
    vf_open,
    NULL
};