5afb6833627172eea18a8c829aff0c5ada040a62
[vlc.git] / modules / video_chroma / i422_yuy2.c
1 /*****************************************************************************
2  * i422_yuy2.c : YUV to YUV conversion module for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel 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 #include <string.h>                                            /* strerror() */
28 #include <stdlib.h>                                      /* malloc(), free() */
29
30 #include <vlc/vlc.h>
31 #include <vlc_vout.h>
32
33 #include "i422_yuy2.h"
34
35 #define SRC_FOURCC  "I422"
36 #if defined (MODULE_NAME_IS_i422_yuy2)
37 #    define DEST_FOURCC "YUY2,YUNV,YVYU,UYVY,UYNV,Y422,IUYV,cyuv,Y211"
38 #else
39 #    define DEST_FOURCC "YUY2,YUNV,YVYU,UYVY,UYNV,Y422,IUYV,cyuv"
40 #endif
41
42 /*****************************************************************************
43  * Local and extern prototypes.
44  *****************************************************************************/
45 static int  Activate ( vlc_object_t * );
46
47 static void I422_YUY2           ( vout_thread_t *, picture_t *, picture_t * );
48 static void I422_YVYU           ( vout_thread_t *, picture_t *, picture_t * );
49 static void I422_UYVY           ( vout_thread_t *, picture_t *, picture_t * );
50 static void I422_IUYV           ( vout_thread_t *, picture_t *, picture_t * );
51 static void I422_cyuv           ( vout_thread_t *, picture_t *, picture_t * );
52 #if defined (MODULE_NAME_IS_i422_yuy2)
53 static void I422_Y211           ( vout_thread_t *, picture_t *, picture_t * );
54 static void I422_Y211           ( vout_thread_t *, picture_t *, picture_t * );
55 static void I422_YV12           ( vout_thread_t *, picture_t *, picture_t * );
56 #endif
57
58 /*****************************************************************************
59  * Module descriptor
60  *****************************************************************************/
61 vlc_module_begin();
62 #if defined (MODULE_NAME_IS_i422_yuy2)
63     set_description( _("Conversions from " SRC_FOURCC " to " DEST_FOURCC) );
64     set_capability( "chroma", 80 );
65 #elif defined (MODULE_NAME_IS_i422_yuy2_mmx)
66     set_description( _("MMX conversions from " SRC_FOURCC " to " DEST_FOURCC) );
67     set_capability( "chroma", 100 );
68     add_requirement( MMX );
69 #endif
70     set_callbacks( Activate, NULL );
71 vlc_module_end();
72
73 /*****************************************************************************
74  * Activate: allocate a chroma function
75  *****************************************************************************
76  * This function allocates and initializes a chroma function
77  *****************************************************************************/
78 static int Activate( vlc_object_t *p_this )
79 {
80     vout_thread_t *p_vout = (vout_thread_t *)p_this;
81
82     if( p_vout->render.i_width & 1 || p_vout->render.i_height & 1 )
83     {
84         return -1;
85     }
86
87     switch( p_vout->render.i_chroma )
88     {
89         case VLC_FOURCC('I','4','2','2'):
90             switch( p_vout->output.i_chroma )
91             {
92                 case VLC_FOURCC('Y','U','Y','2'):
93                 case VLC_FOURCC('Y','U','N','V'):
94                     p_vout->chroma.pf_convert = I422_YUY2;
95                     break;
96
97                 case VLC_FOURCC('Y','V','Y','U'):
98                     p_vout->chroma.pf_convert = I422_YVYU;
99                     break;
100
101                 case VLC_FOURCC('U','Y','V','Y'):
102                 case VLC_FOURCC('U','Y','N','V'):
103                 case VLC_FOURCC('Y','4','2','2'):
104                     p_vout->chroma.pf_convert = I422_UYVY;
105                     break;
106
107                 case VLC_FOURCC('I','U','Y','V'):
108                     p_vout->chroma.pf_convert = I422_IUYV;
109                     break;
110
111                 case VLC_FOURCC('c','y','u','v'):
112                     p_vout->chroma.pf_convert = I422_cyuv;
113                     break;
114
115 #if defined (MODULE_NAME_IS_i422_yuy2)
116                 case VLC_FOURCC('Y','2','1','1'):
117                     p_vout->chroma.pf_convert = I422_Y211;
118                     break;
119
120                 case VLC_FOURCC('Y','V','1','2'):
121                     p_vout->chroma.pf_convert = I422_YV12;
122                     break;
123 #endif
124
125                 default:
126                     return -1;
127             }
128             break;
129
130         default:
131             return -1;
132     }
133     
134     return 0; 
135 }
136
137 /* Following functions are local */
138
139 /*****************************************************************************
140  * I422_YUY2: planar YUV 4:2:2 to packed YUY2 4:2:2
141  *****************************************************************************/
142 static void I422_YUY2( vout_thread_t *p_vout, picture_t *p_source,
143                                               picture_t *p_dest )
144 {
145     uint8_t *p_pixels = p_dest->p->p_pixels;
146     int      i_pitch  = p_dest->p->i_pitch;
147     uint8_t *p_y = p_source->Y_PIXELS;
148     uint8_t *p_u = p_source->U_PIXELS;
149     uint8_t *p_v = p_source->V_PIXELS;
150
151     int i_x, i_y;
152
153     for( i_y = p_vout->render.i_height ; i_y-- ; )
154     {
155         uint8_t *p_line = p_pixels;
156         for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
157         {
158 #if defined (MODULE_NAME_IS_i422_yuy2)
159             C_YUV422_YUYV( p_line, p_y, p_u, p_v );
160             C_YUV422_YUYV( p_line, p_y, p_u, p_v );
161             C_YUV422_YUYV( p_line, p_y, p_u, p_v );
162             C_YUV422_YUYV( p_line, p_y, p_u, p_v );
163 #else
164             __asm__( ".p2align 3" MMX_YUV422_YUYV
165                      : : "r" (p_line), "r" (p_y), "r" (p_u), "r" (p_v) ); 
166
167             p_line += 16; p_y += 8; p_u += 4; p_v += 4;
168 #endif
169         }
170         p_pixels += i_pitch;
171     }
172 }
173
174 /*****************************************************************************
175  * I422_YVYU: planar YUV 4:2:2 to packed YVYU 4:2:2
176  *****************************************************************************/
177 static void I422_YVYU( vout_thread_t *p_vout, picture_t *p_source,
178                                               picture_t *p_dest )
179 {
180     uint8_t *p_pixels = p_dest->p->p_pixels;
181     int      i_pitch  = p_dest->p->i_pitch;
182     uint8_t *p_y = p_source->Y_PIXELS;
183     uint8_t *p_u = p_source->U_PIXELS;
184     uint8_t *p_v = p_source->V_PIXELS;
185
186     int i_x, i_y;
187
188     for( i_y = p_vout->render.i_height ; i_y-- ; )
189     {
190         uint8_t *p_line = p_pixels;
191         for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
192         {
193 #if defined (MODULE_NAME_IS_i422_yuy2)
194             C_YUV422_YVYU( p_line, p_y, p_u, p_v );
195             C_YUV422_YVYU( p_line, p_y, p_u, p_v );
196             C_YUV422_YVYU( p_line, p_y, p_u, p_v );
197             C_YUV422_YVYU( p_line, p_y, p_u, p_v );
198 #else
199             __asm__( ".p2align 3" MMX_YUV422_YVYU
200                      : : "r" (p_line), "r" (p_y), "r" (p_u), "r" (p_v) ); 
201
202             p_line += 16; p_y += 8; p_u += 4; p_v += 4;
203 #endif
204         }
205         p_pixels += i_pitch;
206     }
207 }
208
209 /*****************************************************************************
210  * I422_UYVY: planar YUV 4:2:2 to packed UYVY 4:2:2
211  *****************************************************************************/
212 static void I422_UYVY( vout_thread_t *p_vout, picture_t *p_source,
213                                               picture_t *p_dest )
214 {
215     uint8_t *p_pixels = p_dest->p->p_pixels;
216     int      i_pitch  = p_dest->p->i_pitch;
217     uint8_t *p_y = p_source->Y_PIXELS;
218     uint8_t *p_u = p_source->U_PIXELS;
219     uint8_t *p_v = p_source->V_PIXELS;
220
221     int i_x, i_y;
222
223     for( i_y = p_vout->render.i_height ; i_y-- ; )
224     {
225         uint8_t *p_line = p_pixels;
226         for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
227         {
228 #if defined (MODULE_NAME_IS_i422_yuy2)
229             C_YUV422_UYVY( p_line, p_y, p_u, p_v );
230             C_YUV422_UYVY( p_line, p_y, p_u, p_v );
231             C_YUV422_UYVY( p_line, p_y, p_u, p_v );
232             C_YUV422_UYVY( p_line, p_y, p_u, p_v );
233 #else
234             __asm__( ".p2align 3" MMX_YUV422_UYVY
235                      : : "r" (p_line), "r" (p_y), "r" (p_u), "r" (p_v) ); 
236
237             p_line += 16; p_y += 8; p_u += 4; p_v += 4;
238 #endif
239         }
240         p_pixels += i_pitch;
241     }
242 }
243
244 /*****************************************************************************
245  * I422_IUYV: planar YUV 4:2:2 to interleaved packed IUYV 4:2:2
246  *****************************************************************************/
247 static void I422_IUYV( vout_thread_t *p_vout, picture_t *p_source,
248                                               picture_t *p_dest )
249 {
250     /* FIXME: TODO ! */
251     msg_Err( p_vout, "I422_IUYV unimplemented, please harass <sam@zoy.org>" );
252 }
253
254 /*****************************************************************************
255  * I422_cyuv: planar YUV 4:2:2 to upside-down packed UYVY 4:2:2
256  *****************************************************************************/
257 static void I422_cyuv( vout_thread_t *p_vout, picture_t *p_source,
258                                               picture_t *p_dest )
259 {
260     uint8_t *p_line = p_dest->p->p_pixels + p_dest->p->i_visible_lines * p_dest->p->i_pitch;
261     uint8_t *p_y = p_source->Y_PIXELS;
262     uint8_t *p_u = p_source->U_PIXELS;
263     uint8_t *p_v = p_source->V_PIXELS;
264
265     int i_x, i_y;
266
267     for( i_y = p_vout->render.i_height ; i_y-- ; )
268     {
269         for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
270         {
271             p_line -= 2 * p_dest->p->i_pitch;
272
273 #if defined (MODULE_NAME_IS_i422_yuy2)
274             C_YUV422_UYVY( p_line, p_y, p_u, p_v );
275             C_YUV422_UYVY( p_line, p_y, p_u, p_v );
276             C_YUV422_UYVY( p_line, p_y, p_u, p_v );
277             C_YUV422_UYVY( p_line, p_y, p_u, p_v );
278 #else
279             __asm__( ".p2align 3" MMX_YUV422_UYVY
280                      : : "r" (p_line), "r" (p_y), "r" (p_u), "r" (p_v) ); 
281
282             p_line += 16; p_y += 8; p_u += 4; p_v += 4;
283 #endif
284         }
285     }
286 }
287
288 /*****************************************************************************
289  * I422_Y211: planar YUV 4:2:2 to packed YUYV 2:1:1
290  *****************************************************************************/
291 #if defined (MODULE_NAME_IS_i422_yuy2)
292 static void I422_Y211( vout_thread_t *p_vout, picture_t *p_source,
293                                               picture_t *p_dest )
294 {
295     uint8_t *p_line = p_dest->p->p_pixels + p_dest->p->i_visible_lines * p_dest->p->i_pitch;
296     uint8_t *p_y = p_source->Y_PIXELS;
297     uint8_t *p_u = p_source->U_PIXELS;
298     uint8_t *p_v = p_source->V_PIXELS;
299
300     int i_x, i_y;
301
302     for( i_y = p_vout->render.i_height ; i_y-- ; )
303     {
304         for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
305         {
306             C_YUV422_Y211( p_line, p_y, p_u, p_v );
307             C_YUV422_Y211( p_line, p_y, p_u, p_v );
308         }
309     }
310 }
311 #endif
312
313
314 /*****************************************************************************
315  * I422_YV12: planar YUV 4:2:2 to planar YV12
316  *****************************************************************************/
317 #if defined (MODULE_NAME_IS_i422_yuy2)
318 static void I422_YV12( vout_thread_t *p_vout, picture_t *p_source,
319                                               picture_t *p_dest )
320 {
321     uint16_t i_dpy = p_dest->p[Y_PLANE].i_pitch;
322     uint16_t i_spy = p_source->p[Y_PLANE].i_pitch;
323     uint16_t i_dpuv = p_dest->p[U_PLANE].i_pitch;
324     uint16_t i_spuv = p_source->p[U_PLANE].i_pitch;
325     uint16_t i_width = p_vout->render.i_width;
326     uint16_t i_y = p_vout->render.i_height;
327     uint8_t *p_dy = p_dest->Y_PIXELS + (i_y-1)*i_dpy;
328     uint8_t *p_y = p_source->Y_PIXELS + (i_y-1)*i_spy;
329     uint8_t *p_du = p_dest->U_PIXELS + (i_y/2-1)*i_dpuv;
330     uint8_t *p_u = p_source->U_PIXELS + (i_y-1)*i_spuv;
331     uint8_t *p_dv = p_dest->V_PIXELS + (i_y/2-1)*i_dpuv;
332     uint8_t *p_v = p_source->V_PIXELS + (i_y-1)*i_spuv;
333     i_y /= 2;
334
335     for ( ; i_y--; )
336     {
337         memcpy(p_dy, p_y, i_width); p_dy -= i_dpy; p_y -= i_spy;
338         memcpy(p_dy, p_y, i_width); p_dy -= i_dpy; p_y -= i_spy;
339         memcpy(p_du, p_u, i_width/2); p_du -= i_dpuv; p_u -= 2*i_spuv;
340         memcpy(p_dv, p_v, i_width/2); p_dv -= i_dpuv; p_v -= 2*i_spuv;
341     }
342
343 }
344 #endif