Statistics
| Branch: | Revision:

ffmpeg / libavfilter / libmpcodecs / vf_eq.c @ e4852fb3

History | View | Annotate | Download (7.64 KB)

1
/*
2
 * This file is part of MPlayer.
3
 *
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.
8
 *
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.
13
 *
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.
17
 */
18

    
19
#include <stdio.h>
20
#include <stdlib.h>
21
#include <string.h>
22
#include <inttypes.h>
23

    
24
#include "config.h"
25
#include "mp_msg.h"
26
#include "cpudetect.h"
27

    
28
#include "img_format.h"
29
#include "mp_image.h"
30
#include "vf.h"
31

    
32
#include "libvo/video_out.h"
33

    
34
#include "m_option.h"
35
#include "m_struct.h"
36

    
37
static struct vf_priv_s {
38
        unsigned char *buf;
39
        int brightness;
40
        int contrast;
41
} const vf_priv_dflt = {
42
  NULL,
43
  0,
44
  0
45
};
46

    
47
#if HAVE_MMX
48
static void process_MMX(unsigned char *dest, int dstride, unsigned char *src, int sstride,
49
                    int w, int h, int brightness, int contrast)
50
{
51
        int i;
52
        int pel;
53
        int dstep = dstride-w;
54
        int sstep = sstride-w;
55
        short brvec[4];
56
        short contvec[4];
57

    
58
        contrast = ((contrast+100)*256*16)/100;
59
        brightness = ((brightness+100)*511)/200-128 - contrast/32;
60

    
61
        brvec[0] = brvec[1] = brvec[2] = brvec[3] = brightness;
62
        contvec[0] = contvec[1] = contvec[2] = contvec[3] = contrast;
63

    
64
        while (h--) {
65
                __asm__ volatile (
66
                        "movq (%5), %%mm3 \n\t"
67
                        "movq (%6), %%mm4 \n\t"
68
                        "pxor %%mm0, %%mm0 \n\t"
69
                        "movl %4, %%eax\n\t"
70
                        ASMALIGN(4)
71
                        "1: \n\t"
72
                        "movq (%0), %%mm1 \n\t"
73
                        "movq (%0), %%mm2 \n\t"
74
                        "punpcklbw %%mm0, %%mm1 \n\t"
75
                        "punpckhbw %%mm0, %%mm2 \n\t"
76
                        "psllw $4, %%mm1 \n\t"
77
                        "psllw $4, %%mm2 \n\t"
78
                        "pmulhw %%mm4, %%mm1 \n\t"
79
                        "pmulhw %%mm4, %%mm2 \n\t"
80
                        "paddw %%mm3, %%mm1 \n\t"
81
                        "paddw %%mm3, %%mm2 \n\t"
82
                        "packuswb %%mm2, %%mm1 \n\t"
83
                        "add $8, %0 \n\t"
84
                        "movq %%mm1, (%1) \n\t"
85
                        "add $8, %1 \n\t"
86
                        "decl %%eax \n\t"
87
                        "jnz 1b \n\t"
88
                        : "=r" (src), "=r" (dest)
89
                        : "0" (src), "1" (dest), "r" (w>>3), "r" (brvec), "r" (contvec)
90
                        : "%eax"
91
                );
92

    
93
                for (i = w&7; i; i--)
94
                {
95
                        pel = ((*src++* contrast)>>12) + brightness;
96
                        if(pel&768) pel = (-pel)>>31;
97
                        *dest++ = pel;
98
                }
99

    
100
                src += sstep;
101
                dest += dstep;
102
        }
103
        __asm__ volatile ( "emms \n\t" ::: "memory" );
104
}
105
#endif
106

    
107
static void process_C(unsigned char *dest, int dstride, unsigned char *src, int sstride,
108
                    int w, int h, int brightness, int contrast)
109
{
110
        int i;
111
        int pel;
112
        int dstep = dstride-w;
113
        int sstep = sstride-w;
114

    
115
        contrast = ((contrast+100)*256*256)/100;
116
        brightness = ((brightness+100)*511)/200-128 - contrast/512;
117

    
118
        while (h--) {
119
                for (i = w; i; i--)
120
                {
121
                        pel = ((*src++* contrast)>>16) + brightness;
122
                        if(pel&768) pel = (-pel)>>31;
123
                        *dest++ = pel;
124
                }
125
                src += sstep;
126
                dest += dstep;
127
        }
128
}
129

    
130
static void (*process)(unsigned char *dest, int dstride, unsigned char *src, int sstride,
131
                       int w, int h, int brightness, int contrast);
132

    
133
/* FIXME: add packed yuv version of process */
134

    
135
static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
136
{
137
        mp_image_t *dmpi;
138

    
139
        dmpi=vf_get_image(vf->next, mpi->imgfmt,
140
                          MP_IMGTYPE_EXPORT, 0,
141
                          mpi->w, mpi->h);
142

    
143
        dmpi->stride[0] = mpi->stride[0];
144
        dmpi->planes[1] = mpi->planes[1];
145
        dmpi->planes[2] = mpi->planes[2];
146
        dmpi->stride[1] = mpi->stride[1];
147
        dmpi->stride[2] = mpi->stride[2];
148

    
149
        if (!vf->priv->buf) vf->priv->buf = malloc(mpi->stride[0]*mpi->h);
150

    
151
        if ((vf->priv->brightness == 0) && (vf->priv->contrast == 0))
152
                dmpi->planes[0] = mpi->planes[0];
153
        else {
154
                dmpi->planes[0] = vf->priv->buf;
155
                process(dmpi->planes[0], dmpi->stride[0],
156
                        mpi->planes[0], mpi->stride[0],
157
                        mpi->w, mpi->h, vf->priv->brightness,
158
                        vf->priv->contrast);
159
        }
160

    
161
        return vf_next_put_image(vf,dmpi, pts);
162
}
163

    
164
static int control(struct vf_instance *vf, int request, void* data)
165
{
166
        vf_equalizer_t *eq;
167

    
168
        switch (request) {
169
        case VFCTRL_SET_EQUALIZER:
170
                eq = data;
171
                if (!strcmp(eq->item,"brightness")) {
172
                        vf->priv->brightness = eq->value;
173
                        return CONTROL_TRUE;
174
                }
175
                else if (!strcmp(eq->item,"contrast")) {
176
                        vf->priv->contrast = eq->value;
177
                        return CONTROL_TRUE;
178
                }
179
                break;
180
        case VFCTRL_GET_EQUALIZER:
181
                eq = data;
182
                if (!strcmp(eq->item,"brightness")) {
183
                        eq->value = vf->priv->brightness;
184
                        return CONTROL_TRUE;
185
                }
186
                else if (!strcmp(eq->item,"contrast")) {
187
                        eq->value = vf->priv->contrast;
188
                        return CONTROL_TRUE;
189
                }
190
                break;
191
        }
192
        return vf_next_control(vf, request, data);
193
}
194

    
195
static int query_format(struct vf_instance *vf, unsigned int fmt)
196
{
197
        switch (fmt) {
198
        case IMGFMT_YVU9:
199
        case IMGFMT_IF09:
200
        case IMGFMT_YV12:
201
        case IMGFMT_I420:
202
        case IMGFMT_IYUV:
203
        case IMGFMT_CLPL:
204
        case IMGFMT_Y800:
205
        case IMGFMT_Y8:
206
        case IMGFMT_NV12:
207
        case IMGFMT_NV21:
208
        case IMGFMT_444P:
209
        case IMGFMT_422P:
210
        case IMGFMT_411P:
211
                return vf_next_query_format(vf, fmt);
212
        }
213
        return 0;
214
}
215

    
216
static void uninit(struct vf_instance *vf)
217
{
218
        free(vf->priv->buf);
219
        free(vf->priv);
220
}
221

    
222
static int vf_open(vf_instance_t *vf, char *args)
223
{
224
        vf->control=control;
225
        vf->query_format=query_format;
226
        vf->put_image=put_image;
227
        vf->uninit=uninit;
228

    
229
        process = process_C;
230
#if HAVE_MMX
231
        if(gCpuCaps.hasMMX) process = process_MMX;
232
#endif
233

    
234
        return 1;
235
}
236

    
237
#define ST_OFF(f) M_ST_OFF(struct vf_priv_s,f)
238
static const m_option_t vf_opts_fields[] = {
239
  {"brightness", ST_OFF(brightness), CONF_TYPE_INT, M_OPT_RANGE,-100 ,100, NULL},
240
  {"contrast", ST_OFF(contrast), CONF_TYPE_INT, M_OPT_RANGE,-100 ,100, NULL},
241
  { NULL, NULL, 0, 0, 0, 0,  NULL }
242
};
243

    
244
static const m_struct_t vf_opts = {
245
  "eq",
246
  sizeof(struct vf_priv_s),
247
  &vf_priv_dflt,
248
  vf_opts_fields
249
};
250

    
251
const vf_info_t vf_info_eq = {
252
        "soft video equalizer",
253
        "eq",
254
        "Richard Felker",
255
        "",
256
        vf_open,
257
        &vf_opts
258
};