ffmpeg: set the video filters timebase to the demuxers.
[ffmpeg.git] / libavfilter / libmpcodecs / vf_hue.c
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 #include <math.h>
24
25 #include "config.h"
26 #include "mp_msg.h"
27 #include "cpudetect.h"
28
29 #include "img_format.h"
30 #include "mp_image.h"
31 #include "vf.h"
32
33 #include "libvo/video_out.h"
34
35 struct vf_priv_s {
36         uint8_t *buf[2];
37         float hue;
38         float saturation;
39 };
40
41 static void process_C(uint8_t *udst, uint8_t *vdst, uint8_t *usrc, uint8_t *vsrc, int dststride, int srcstride,
42                     int w, int h, float hue, float sat)
43 {
44         int i;
45         const int s= rint(sin(hue) * (1<<16) * sat);
46         const int c= rint(cos(hue) * (1<<16) * sat);
47
48         while (h--) {
49                 for (i = 0; i<w; i++)
50                 {
51                         const int u= usrc[i] - 128;
52                         const int v= vsrc[i] - 128;
53                         int new_u= (c*u - s*v + (1<<15) + (128<<16))>>16;
54                         int new_v= (s*u + c*v + (1<<15) + (128<<16))>>16;
55                         if(new_u & 768) new_u= (-new_u)>>31;
56                         if(new_v & 768) new_v= (-new_v)>>31;
57                         udst[i]= new_u;
58                         vdst[i]= new_v;
59                 }
60                 usrc += srcstride;
61                 vsrc += srcstride;
62                 udst += dststride;
63                 vdst += dststride;
64         }
65 }
66
67 static void (*process)(uint8_t *udst, uint8_t *vdst, uint8_t *usrc, uint8_t *vsrc, int dststride, int srcstride,
68                     int w, int h, float hue, float sat);
69
70 /* FIXME: add packed yuv version of process */
71
72 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
73 {
74         mp_image_t *dmpi;
75
76         dmpi=vf_get_image(vf->next, mpi->imgfmt,
77                           MP_IMGTYPE_EXPORT, 0,
78                           mpi->w, mpi->h);
79
80         dmpi->planes[0] = mpi->planes[0];
81         dmpi->stride[0] = mpi->stride[0];
82         dmpi->stride[1] = mpi->stride[1];
83         dmpi->stride[2] = mpi->stride[2];
84
85         if (!vf->priv->buf[0]){
86                 vf->priv->buf[0] = malloc(mpi->stride[1]*mpi->h >> mpi->chroma_y_shift);
87                 vf->priv->buf[1] = malloc(mpi->stride[2]*mpi->h >> mpi->chroma_y_shift);
88         }
89
90         if (vf->priv->hue == 0 && vf->priv->saturation == 1){
91                 dmpi->planes[1] = mpi->planes[1];
92                 dmpi->planes[2] = mpi->planes[2];
93         }else {
94                 dmpi->planes[1] = vf->priv->buf[0];
95                 dmpi->planes[2] = vf->priv->buf[1];
96                 process(dmpi->planes[1], dmpi->planes[2],
97                         mpi->planes[1], mpi->planes[2],
98                         dmpi->stride[1],mpi->stride[1],
99                         mpi->w>> mpi->chroma_x_shift, mpi->h>> mpi->chroma_y_shift,
100                         vf->priv->hue, vf->priv->saturation);
101         }
102
103         return vf_next_put_image(vf,dmpi, pts);
104 }
105
106 static int control(struct vf_instance *vf, int request, void* data)
107 {
108         vf_equalizer_t *eq;
109
110         switch (request) {
111         case VFCTRL_SET_EQUALIZER:
112                 eq = data;
113                 if (!strcmp(eq->item,"hue")) {
114                         vf->priv->hue = eq->value * M_PI / 100;
115                         return CONTROL_TRUE;
116                 } else if (!strcmp(eq->item,"saturation")) {
117                         vf->priv->saturation = (eq->value + 100)/100.0;
118                         return CONTROL_TRUE;
119                 }
120                 break;
121         case VFCTRL_GET_EQUALIZER:
122                 eq = data;
123                 if (!strcmp(eq->item,"hue")) {
124                         eq->value = rint(vf->priv->hue *100 / M_PI);
125                         return CONTROL_TRUE;
126                 }else if (!strcmp(eq->item,"saturation")) {
127                         eq->value = rint(vf->priv->saturation*100 - 100);
128                         return CONTROL_TRUE;
129                 }
130                 break;
131         }
132         return vf_next_control(vf, request, data);
133 }
134
135 static int query_format(struct vf_instance *vf, unsigned int fmt)
136 {
137         switch (fmt) {
138         case IMGFMT_YVU9:
139         case IMGFMT_IF09:
140         case IMGFMT_YV12:
141         case IMGFMT_I420:
142         case IMGFMT_IYUV:
143         case IMGFMT_CLPL:
144         case IMGFMT_444P:
145         case IMGFMT_422P:
146         case IMGFMT_411P:
147                 return vf_next_query_format(vf, fmt);
148         }
149         return 0;
150 }
151
152 static void uninit(struct vf_instance *vf)
153 {
154         free(vf->priv->buf[0]);
155         free(vf->priv->buf[1]);
156         free(vf->priv);
157 }
158
159 static int vf_open(vf_instance_t *vf, char *args)
160 {
161         vf->control=control;
162         vf->query_format=query_format;
163         vf->put_image=put_image;
164         vf->uninit=uninit;
165
166     vf->priv = malloc(sizeof(struct vf_priv_s));
167     memset(vf->priv, 0, sizeof(struct vf_priv_s));
168     sscanf(args, "%f:%f", &vf->priv->hue, &vf->priv->saturation);
169         vf->priv->hue *= M_PI / 180.0;
170
171         process = process_C;
172         return 1;
173 }
174
175 const vf_info_t vf_info_hue = {
176         "hue changer",
177         "hue",
178         "Michael Niedermayer",
179         "",
180         vf_open,
181 };