1 /*****************************************************************************
2 * i420_yuy2.c : YUV to YUV conversion module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
5 * $Id: i420_yuy2.c,v 1.6 2004/01/26 16:54:56 titer Exp $
7 * Authors: Samuel Hocevar <sam@zoy.org>
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <string.h> /* strerror() */
28 #include <stdlib.h> /* malloc(), free() */
33 #include "i420_yuy2.h"
35 #define SRC_FOURCC "I420,IYUV,YV12"
37 #if defined (MODULE_NAME_IS_i420_yuy2)
38 # define DEST_FOURCC "YUY2,YUNV,YVYU,UYVY,UYNV,Y422,IUYV,cyuv,Y211"
39 #elif defined (MODULE_NAME_IS_i420_yuy2_mmx)
40 # define DEST_FOURCC "YUY2,YUNV,YVYU,UYVY,UYNV,Y422,IUYV,cyuv"
41 #elif defined (MODULE_NAME_IS_i420_yuy2_altivec)
42 # define DEST_FOURCC "YUY2,YUNV"
45 /*****************************************************************************
46 * Local and extern prototypes.
47 *****************************************************************************/
48 static int Activate ( vlc_object_t * );
50 static void I420_YUY2 ( vout_thread_t *, picture_t *, picture_t * );
51 #if !defined (MODULE_NAME_IS_i420_yuy2_altivec)
52 static void I420_YVYU ( vout_thread_t *, picture_t *, picture_t * );
53 static void I420_UYVY ( vout_thread_t *, picture_t *, picture_t * );
54 static void I420_IUYV ( vout_thread_t *, picture_t *, picture_t * );
55 static void I420_cyuv ( vout_thread_t *, picture_t *, picture_t * );
57 #if defined (MODULE_NAME_IS_i420_yuy2)
58 static void I420_Y211 ( vout_thread_t *, picture_t *, picture_t * );
61 #ifdef MODULE_NAME_IS_i420_yuy2_mmx
62 static uint64_t i_00ffw;
63 static uint64_t i_80w;
66 /*****************************************************************************
68 *****************************************************************************/
70 #if defined (MODULE_NAME_IS_i420_yuy2)
71 set_description( _("Conversions from " SRC_FOURCC " to " DEST_FOURCC) );
72 set_capability( "chroma", 80 );
73 #elif defined (MODULE_NAME_IS_i420_yuy2_mmx)
74 set_description( _("MMX conversions from " SRC_FOURCC " to " DEST_FOURCC) );
75 set_capability( "chroma", 100 );
76 add_requirement( MMX );
77 /* Initialize MMX-specific constants */
78 i_00ffw = 0x00ff00ff00ff00ffULL;
79 i_80w = 0x0000000080808080ULL;
80 #elif defined (MODULE_NAME_IS_i420_yuy2_altivec)
82 _("Altivec conversions from " SRC_FOURCC " to " DEST_FOURCC) );
83 set_capability( "chroma", 100 );
84 add_requirement( ALTIVEC );
86 set_callbacks( Activate, NULL );
89 /*****************************************************************************
90 * Activate: allocate a chroma function
91 *****************************************************************************
92 * This function allocates and initializes a chroma function
93 *****************************************************************************/
94 static int Activate( vlc_object_t *p_this )
96 vout_thread_t *p_vout = (vout_thread_t *)p_this;
98 if( p_vout->render.i_width & 1 || p_vout->render.i_height & 1 )
103 switch( p_vout->render.i_chroma )
105 case VLC_FOURCC('Y','V','1','2'):
106 case VLC_FOURCC('I','4','2','0'):
107 case VLC_FOURCC('I','Y','U','V'):
108 switch( p_vout->output.i_chroma )
110 case VLC_FOURCC('Y','U','Y','2'):
111 case VLC_FOURCC('Y','U','N','V'):
112 p_vout->chroma.pf_convert = I420_YUY2;
115 #if !defined (MODULE_NAME_IS_i420_yuy2_altivec)
116 case VLC_FOURCC('Y','V','Y','U'):
117 p_vout->chroma.pf_convert = I420_YVYU;
120 case VLC_FOURCC('U','Y','V','Y'):
121 case VLC_FOURCC('U','Y','N','V'):
122 case VLC_FOURCC('Y','4','2','2'):
123 p_vout->chroma.pf_convert = I420_UYVY;
126 case VLC_FOURCC('I','U','Y','V'):
127 p_vout->chroma.pf_convert = I420_IUYV;
130 case VLC_FOURCC('c','y','u','v'):
131 p_vout->chroma.pf_convert = I420_cyuv;
135 #if defined (MODULE_NAME_IS_i420_yuy2)
136 case VLC_FOURCC('Y','2','1','1'):
137 p_vout->chroma.pf_convert = I420_Y211;
153 /* Following functions are local */
155 /*****************************************************************************
156 * I420_YUY2: planar YUV 4:2:0 to packed YUYV 4:2:2
157 *****************************************************************************/
158 static void I420_YUY2( vout_thread_t *p_vout, picture_t *p_source,
161 uint8_t *p_line1, *p_line2 = p_dest->p->p_pixels;
162 uint8_t *p_y1, *p_y2 = p_source->Y_PIXELS;
163 uint8_t *p_u = p_source->U_PIXELS;
164 uint8_t *p_v = p_source->V_PIXELS;
168 const int i_source_margin = p_source->p->i_pitch
169 - p_source->p->i_visible_pitch;
170 const int i_dest_margin = p_dest->p->i_pitch
171 - p_dest->p->i_visible_pitch;
173 #if defined (MODULE_NAME_IS_i420_yuy2_altivec)
174 vector unsigned char u_vec;
175 vector unsigned char v_vec;
176 vector unsigned char uv_vec;
177 vector unsigned char y_vec;
181 for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
184 p_line2 += p_dest->p->i_pitch;
187 p_y2 += p_source->p[Y_PLANE].i_pitch;
189 #if defined (MODULE_NAME_IS_i420_yuy2_altivec)
190 /* FIXME Thats only works for sizes multiple of 16 */
191 for( i_x = p_vout->render.i_width / 16 ; i_x-- ; )
195 u_vec = vec_ld( 0, p_u ); p_u += 16;
196 v_vec = vec_ld( 0, p_v ); p_v += 16;
197 uv_vec = vec_mergeh( u_vec, v_vec );
201 uv_vec = vec_mergel( u_vec, v_vec );
203 y_vec = vec_ld( 0, p_y1 ); p_y1 += 16;
204 vec_st( vec_mergeh( y_vec, uv_vec ), 0, p_line1 ); p_line1 += 16;
205 vec_st( vec_mergel( y_vec, uv_vec ), 0, p_line1 ); p_line1 += 16;
206 y_vec = vec_ld( 0, p_y2 ); p_y2 += 16;
207 vec_st( vec_mergeh( y_vec, uv_vec ), 0, p_line2 ); p_line2 += 16;
208 vec_st( vec_mergel( y_vec, uv_vec ), 0, p_line2 ); p_line2 += 16;
212 for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
214 #if defined (MODULE_NAME_IS_i420_yuy2)
220 MMX_CALL( MMX_YUV420_YUYV );
225 p_y1 += i_source_margin;
226 p_y2 += i_source_margin;
227 p_line1 += i_dest_margin;
228 p_line2 += i_dest_margin;
232 /*****************************************************************************
233 * I420_YVYU: planar YUV 4:2:0 to packed YVYU 4:2:2
234 *****************************************************************************/
235 #if !defined (MODULE_NAME_IS_i420_yuy2_altivec)
236 static void I420_YVYU( vout_thread_t *p_vout, picture_t *p_source,
239 uint8_t *p_line1, *p_line2 = p_dest->p->p_pixels;
240 uint8_t *p_y1, *p_y2 = p_source->Y_PIXELS;
241 uint8_t *p_u = p_source->U_PIXELS;
242 uint8_t *p_v = p_source->V_PIXELS;
246 const int i_source_margin = p_source->p->i_pitch
247 - p_source->p->i_visible_pitch;
248 const int i_dest_margin = p_dest->p->i_pitch
249 - p_dest->p->i_visible_pitch;
251 for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
254 p_line2 += p_dest->p->i_pitch;
257 p_y2 += p_source->p[Y_PLANE].i_pitch;
259 for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
261 #if defined (MODULE_NAME_IS_i420_yuy2)
267 MMX_CALL( MMX_YUV420_YVYU );
271 p_y1 += i_source_margin;
272 p_y2 += i_source_margin;
273 p_line1 += i_dest_margin;
274 p_line2 += i_dest_margin;
278 /*****************************************************************************
279 * I420_UYVY: planar YUV 4:2:0 to packed UYVY 4:2:2
280 *****************************************************************************/
281 static void I420_UYVY( vout_thread_t *p_vout, picture_t *p_source,
284 uint8_t *p_line1, *p_line2 = p_dest->p->p_pixels;
285 uint8_t *p_y1, *p_y2 = p_source->Y_PIXELS;
286 uint8_t *p_u = p_source->U_PIXELS;
287 uint8_t *p_v = p_source->V_PIXELS;
291 const int i_source_margin = p_source->p->i_pitch
292 - p_source->p->i_visible_pitch;
293 const int i_dest_margin = p_dest->p->i_pitch
294 - p_dest->p->i_visible_pitch;
296 for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
299 p_line2 += p_dest->p->i_pitch;
302 p_y2 += p_source->p[Y_PLANE].i_pitch;
304 for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
306 #if defined (MODULE_NAME_IS_i420_yuy2)
312 MMX_CALL( MMX_YUV420_UYVY );
316 p_y1 += i_source_margin;
317 p_y2 += i_source_margin;
318 p_line1 += i_dest_margin;
319 p_line2 += i_dest_margin;
323 /*****************************************************************************
324 * I420_IUYV: planar YUV 4:2:0 to interleaved packed UYVY 4:2:2
325 *****************************************************************************/
326 static void I420_IUYV( vout_thread_t *p_vout, picture_t *p_source,
330 msg_Err( p_vout, "I420_IUYV unimplemented, please harass <sam@zoy.org>" );
333 /*****************************************************************************
334 * I420_cyuv: planar YUV 4:2:0 to upside-down packed UYVY 4:2:2
335 *****************************************************************************/
336 static void I420_cyuv( vout_thread_t *p_vout, picture_t *p_source,
339 uint8_t *p_line1 = p_dest->p->p_pixels +
340 p_dest->p->i_lines * p_dest->p->i_pitch
341 + p_dest->p->i_pitch;
342 uint8_t *p_line2 = p_dest->p->p_pixels +
343 p_dest->p->i_lines * p_dest->p->i_pitch;
344 uint8_t *p_y1, *p_y2 = p_source->Y_PIXELS;
345 uint8_t *p_u = p_source->U_PIXELS;
346 uint8_t *p_v = p_source->V_PIXELS;
350 const int i_source_margin = p_source->p->i_pitch
351 - p_source->p->i_visible_pitch;
352 const int i_dest_margin = p_dest->p->i_pitch
353 - p_dest->p->i_visible_pitch;
355 for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
357 p_line1 -= 3 * p_dest->p->i_pitch;
358 p_line2 -= 3 * p_dest->p->i_pitch;
361 p_y2 += p_source->p[Y_PLANE].i_pitch;
363 for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
365 #if defined (MODULE_NAME_IS_i420_yuy2)
371 MMX_CALL( MMX_YUV420_UYVY );
375 p_y1 += i_source_margin;
376 p_y2 += i_source_margin;
377 p_line1 += i_dest_margin;
378 p_line2 += i_dest_margin;
381 #endif // !defined (MODULE_NAME_IS_i420_yuy2_altivec)
383 /*****************************************************************************
384 * I420_Y211: planar YUV 4:2:0 to packed YUYV 2:1:1
385 *****************************************************************************/
386 #if defined (MODULE_NAME_IS_i420_yuy2)
387 static void I420_Y211( vout_thread_t *p_vout, picture_t *p_source,
390 uint8_t *p_line1, *p_line2 = p_dest->p->p_pixels;
391 uint8_t *p_y1, *p_y2 = p_source->Y_PIXELS;
392 uint8_t *p_u = p_source->U_PIXELS;
393 uint8_t *p_v = p_source->V_PIXELS;
397 const int i_source_margin = p_source->p->i_pitch
398 - p_source->p->i_visible_pitch;
399 const int i_dest_margin = p_dest->p->i_pitch
400 - p_dest->p->i_visible_pitch;
402 for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
405 p_line2 += p_dest->p->i_pitch;
408 p_y2 += p_source->p[Y_PLANE].i_pitch;
410 for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
416 p_y1 += i_source_margin;
417 p_y2 += i_source_margin;
418 p_line1 += i_dest_margin;
419 p_line2 += i_dest_margin;