Support for 8bit uncompressed in AVI files.
[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 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 #include <vlc/vlc.h>
29 #include <vlc_vout.h>
30
31 #define SRC_FOURCC  "GREY"
32 #define DEST_FOURCC "I420,YUY2"
33
34 /*****************************************************************************
35  * Local and extern prototypes.
36  *****************************************************************************/
37 static int  Activate ( vlc_object_t * );
38
39 static void GREY_I420           ( vout_thread_t *, picture_t *, picture_t * );
40 static void GREY_YUY2           ( vout_thread_t *, picture_t *, picture_t * );
41
42 /*****************************************************************************
43  * Module descriptor.
44  *****************************************************************************/
45 vlc_module_begin();
46     set_description( _("Conversions from " SRC_FOURCC " to " DEST_FOURCC) );
47     set_capability( "chroma", 80 );
48     set_callbacks( Activate, NULL );
49 vlc_module_end();
50
51 /*****************************************************************************
52  * Activate: allocate a chroma function
53  *****************************************************************************
54  * This function allocates and initializes a chroma function
55  *****************************************************************************/
56 static int Activate( vlc_object_t *p_this )
57 {
58     vout_thread_t *p_vout = (vout_thread_t *)p_this;
59
60     if( p_vout->render.i_width & 1 || p_vout->render.i_height & 1 )
61     {
62         return -1;
63     }
64
65     switch( p_vout->render.i_chroma )
66     {
67         case VLC_FOURCC('Y','8','0','0'):
68             p_vout->render.i_chroma = VLC_FOURCC('G','R','E','Y');
69         case VLC_FOURCC('G','R','E','Y'):
70             switch( p_vout->output.i_chroma )
71             {
72                 case VLC_FOURCC('I','4','2','0'):
73                     p_vout->chroma.pf_convert = GREY_I420;
74                     break;
75                 case VLC_FOURCC('Y','U','Y','2'):
76                     p_vout->chroma.pf_convert = GREY_YUY2;
77                     break;
78                 default:
79                     return -1;
80             }
81             break;
82
83         default:
84             return -1;
85     }
86
87     return 0;
88 }
89
90 /* Following functions are local */
91
92 /*****************************************************************************
93  * GREY_I420: 8-bit grayscale to planar YUV 4:2:0
94  *****************************************************************************/
95 static void GREY_I420( vout_thread_t *p_vout, picture_t *p_source,
96                                               picture_t *p_dest )
97 {
98     uint8_t *p_line = p_source->p->p_pixels;
99     uint8_t *p_y = p_dest->Y_PIXELS;
100     uint8_t *p_u = p_dest->U_PIXELS;
101     uint8_t *p_v = p_dest->V_PIXELS;
102
103     int i_x, i_y;
104
105     const int i_source_margin = p_source->p->i_pitch
106                                  - p_source->p->i_visible_pitch;
107     const int i_dest_margin = p_dest->p[0].i_pitch
108                                - p_dest->p[0].i_visible_pitch;
109     const int i_dest_margin_c = p_dest->p[1].i_pitch
110                                  - p_dest->p[1].i_visible_pitch;
111
112     for( i_y = p_vout->render.i_height / 2; i_y-- ; )
113     {
114         memset(p_u, 0x80, p_dest->p[1].i_visible_pitch);
115         p_u += i_dest_margin_c;
116
117         memset(p_v, 0x80, p_dest->p[1].i_visible_pitch);
118         p_v += i_dest_margin_c;
119     }
120
121     for( i_y = p_vout->render.i_height; i_y-- ; )
122     {
123         for( i_x = p_vout->render.i_width / 8; i_x-- ; )
124         {
125             *p_y++ = *p_line++; *p_y++ = *p_line++;
126             *p_y++ = *p_line++; *p_y++ = *p_line++;
127             *p_y++ = *p_line++; *p_y++ = *p_line++;
128             *p_y++ = *p_line++; *p_y++ = *p_line++;
129         }
130
131         for( i_x = p_vout->render.i_width % 8; i_x-- ; )
132         {
133             *p_y++ = *p_line++;
134         }
135
136         p_line += i_source_margin;
137         p_y += i_dest_margin;
138     }
139 }
140
141 /*****************************************************************************
142  * GREY_YUY2: 8-bit grayscale to packed YUY2
143  *****************************************************************************/
144 static void GREY_YUY2( vout_thread_t *p_vout, picture_t *p_source,
145                                               picture_t *p_dest )
146 {
147     uint8_t *p_in = p_source->p->p_pixels;
148     uint8_t *p_out = p_dest->p->p_pixels;
149
150     int i_x, i_y;
151
152     const int i_source_margin = p_source->p->i_pitch
153                                  - p_source->p->i_visible_pitch;
154     const int i_dest_margin = p_dest->p->i_pitch
155                                - p_dest->p->i_visible_pitch;
156
157     for( i_y = p_vout->render.i_height; i_y-- ; )
158     {
159         for( i_x = p_vout->render.i_width / 8; i_x-- ; )
160         {
161             *p_out++ = *p_in++; *p_out++ = 0x80;
162             *p_out++ = *p_in++; *p_out++ = 0x80;
163             *p_out++ = *p_in++; *p_out++ = 0x80;
164             *p_out++ = *p_in++; *p_out++ = 0x80;
165             *p_out++ = *p_in++; *p_out++ = 0x80;
166             *p_out++ = *p_in++; *p_out++ = 0x80;
167             *p_out++ = *p_in++; *p_out++ = 0x80;
168             *p_out++ = *p_in++; *p_out++ = 0x80;
169         }
170
171         for( i_x = (p_vout->render.i_width % 8) / 2; i_x-- ; )
172         {
173             *p_out++ = *p_in++; *p_out++ = 0x80;
174             *p_out++ = *p_in++; *p_out++ = 0x80;
175         }
176
177         p_in += i_source_margin;
178         p_out += i_dest_margin;
179     }
180 }
181