Chroma API change. Chromas are now normal video filters (almost).
[vlc.git] / modules / video_chroma / grey_yuv.c
1 /*****************************************************************************
2  * grey_yuv.c : grayscale to others conversion module for vlc
3  *****************************************************************************
4  * Copyright (C) 2007, 2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sam Hocevar <sam@zoy.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_filter.h>
35 #include <vlc_vout.h>
36
37 #define SRC_FOURCC  "GREY"
38 #define DEST_FOURCC "I420,YUY2"
39
40 /*****************************************************************************
41  * Local and extern prototypes.
42  *****************************************************************************/
43 static int  Activate ( vlc_object_t * );
44
45 static void GREY_I420( filter_t *, picture_t *, picture_t * );
46 static void GREY_YUY2( filter_t *, picture_t *, picture_t * );
47
48 /*****************************************************************************
49  * Module descriptor.
50  *****************************************************************************/
51 vlc_module_begin();
52     set_description( N_("Conversions from " SRC_FOURCC " to " DEST_FOURCC) );
53     set_capability( "chroma", 80 );
54     set_callbacks( Activate, NULL );
55 vlc_module_end();
56
57 /*****************************************************************************
58  * Activate: allocate a chroma function
59  *****************************************************************************
60  * This function allocates and initializes a chroma function
61  *****************************************************************************/
62 static int Activate( vlc_object_t *p_this )
63 {
64     filter_t *p_filter = (filter_t *)p_this;
65
66     if( p_filter->fmt_out.video.i_width & 1
67      || p_filter->fmt_out.video.i_height & 1 )
68     {
69         return -1;
70     }
71
72     switch( p_filter->fmt_in.video.i_chroma )
73     {
74         case VLC_FOURCC('Y','8','0','0'):
75             p_filter->fmt_in.video.i_chroma = VLC_FOURCC('G','R','E','Y');
76         case VLC_FOURCC('G','R','E','Y'):
77             switch( p_filter->fmt_out.video.i_chroma )
78             {
79                 case VLC_FOURCC('I','4','2','0'):
80                     p_filter->pf_video_filter_io = GREY_I420;
81                     break;
82                 case VLC_FOURCC('Y','U','Y','2'):
83                     p_filter->pf_video_filter_io = GREY_YUY2;
84                     break;
85                 default:
86                     return -1;
87             }
88             break;
89
90         default:
91             return -1;
92     }
93
94     return 0;
95 }
96
97 /* Following functions are local */
98
99 /*****************************************************************************
100  * GREY_I420: 8-bit grayscale to planar YUV 4:2:0
101  *****************************************************************************/
102 static void GREY_I420( filter_t *p_filter, picture_t *p_source,
103                                            picture_t *p_dest )
104 {
105     uint8_t *p_line = p_source->p->p_pixels;
106     uint8_t *p_y = p_dest->Y_PIXELS;
107     uint8_t *p_u = p_dest->U_PIXELS;
108     uint8_t *p_v = p_dest->V_PIXELS;
109
110     int i_x, i_y;
111
112     const int i_source_margin = p_source->p->i_pitch
113                                  - p_source->p->i_visible_pitch;
114     const int i_dest_margin = p_dest->p[0].i_pitch
115                                - p_dest->p[0].i_visible_pitch;
116     const int i_dest_margin_c = p_dest->p[1].i_pitch
117                                  - p_dest->p[1].i_visible_pitch;
118
119     for( i_y = p_filter->fmt_in.video.i_height / 2; i_y-- ; )
120     {
121         memset(p_u, 0x80, p_dest->p[1].i_visible_pitch);
122         p_u += i_dest_margin_c;
123
124         memset(p_v, 0x80, p_dest->p[1].i_visible_pitch);
125         p_v += i_dest_margin_c;
126     }
127
128     for( i_y = p_filter->fmt_in.video.i_height; i_y-- ; )
129     {
130         for( i_x = p_filter->fmt_in.video.i_width / 8; i_x-- ; )
131         {
132             *p_y++ = *p_line++; *p_y++ = *p_line++;
133             *p_y++ = *p_line++; *p_y++ = *p_line++;
134             *p_y++ = *p_line++; *p_y++ = *p_line++;
135             *p_y++ = *p_line++; *p_y++ = *p_line++;
136         }
137
138         for( i_x = p_filter->fmt_in.video.i_width % 8; i_x-- ; )
139         {
140             *p_y++ = *p_line++;
141         }
142
143         p_line += i_source_margin;
144         p_y += i_dest_margin;
145     }
146 }
147
148 /*****************************************************************************
149  * GREY_YUY2: 8-bit grayscale to packed YUY2
150  *****************************************************************************/
151 static void GREY_YUY2( filter_t *p_filter, picture_t *p_source,
152                                            picture_t *p_dest )
153 {
154     uint8_t *p_in = p_source->p->p_pixels;
155     uint8_t *p_out = p_dest->p->p_pixels;
156
157     int i_x, i_y;
158
159     const int i_source_margin = p_source->p->i_pitch
160                                  - p_source->p->i_visible_pitch;
161     const int i_dest_margin = p_dest->p->i_pitch
162                                - p_dest->p->i_visible_pitch;
163
164     for( i_y = p_filter->fmt_out.video.i_height; i_y-- ; )
165     {
166         for( i_x = p_filter->fmt_out.video.i_width / 8; i_x-- ; )
167         {
168             *p_out++ = *p_in++; *p_out++ = 0x80;
169             *p_out++ = *p_in++; *p_out++ = 0x80;
170             *p_out++ = *p_in++; *p_out++ = 0x80;
171             *p_out++ = *p_in++; *p_out++ = 0x80;
172             *p_out++ = *p_in++; *p_out++ = 0x80;
173             *p_out++ = *p_in++; *p_out++ = 0x80;
174             *p_out++ = *p_in++; *p_out++ = 0x80;
175             *p_out++ = *p_in++; *p_out++ = 0x80;
176         }
177
178         for( i_x = (p_filter->fmt_out.video.i_width % 8) / 2; i_x-- ; )
179         {
180             *p_out++ = *p_in++; *p_out++ = 0x80;
181             *p_out++ = *p_in++; *p_out++ = 0x80;
182         }
183
184         p_in += i_source_margin;
185         p_out += i_dest_margin;
186     }
187 }
188