1 /*****************************************************************************
2 * i420_yuy2.c : YUV to YUV conversion module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
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() */
37 #include "i420_yuy2.h"
39 #define SRC_FOURCC "I420,IYUV,YV12"
41 #if defined (MODULE_NAME_IS_i420_yuy2)
42 # define DEST_FOURCC "YUY2,YUNV,YVYU,UYVY,UYNV,Y422,IUYV,cyuv,Y211"
43 #elif defined (MODULE_NAME_IS_i420_yuy2_mmx)
44 # define DEST_FOURCC "YUY2,YUNV,YVYU,UYVY,UYNV,Y422,IUYV,cyuv"
45 #elif defined (MODULE_NAME_IS_i420_yuy2_altivec)
46 # define DEST_FOURCC "YUY2,YUNV"
49 /*****************************************************************************
50 * Local and extern prototypes.
51 *****************************************************************************/
52 static int Activate ( vlc_object_t * );
54 static void I420_YUY2 ( vout_thread_t *, picture_t *, picture_t * );
55 #if !defined (MODULE_NAME_IS_i420_yuy2_altivec)
56 static void I420_YVYU ( vout_thread_t *, picture_t *, picture_t * );
57 static void I420_UYVY ( vout_thread_t *, picture_t *, picture_t * );
58 static void I420_IUYV ( vout_thread_t *, picture_t *, picture_t * );
59 static void I420_cyuv ( vout_thread_t *, picture_t *, picture_t * );
61 #if defined (MODULE_NAME_IS_i420_yuy2)
62 static void I420_Y211 ( vout_thread_t *, picture_t *, picture_t * );
65 #ifdef MODULE_NAME_IS_i420_yuy2_mmx
66 static uint64_t i_00ffw;
67 static uint64_t i_80w;
70 /*****************************************************************************
72 *****************************************************************************/
74 #if defined (MODULE_NAME_IS_i420_yuy2)
75 set_description( _("Conversions from " SRC_FOURCC " to " DEST_FOURCC) );
76 set_capability( "chroma", 80 );
77 #elif defined (MODULE_NAME_IS_i420_yuy2_mmx)
78 set_description( _("MMX conversions from " SRC_FOURCC " to " DEST_FOURCC) );
79 set_capability( "chroma", 100 );
80 add_requirement( MMX );
81 /* Initialize MMX-specific constants */
82 i_00ffw = 0x00ff00ff00ff00ffULL;
83 i_80w = 0x0000000080808080ULL;
84 #elif defined (MODULE_NAME_IS_i420_yuy2_altivec)
86 _("AltiVec conversions from " SRC_FOURCC " to " DEST_FOURCC) );
87 set_capability( "chroma", 100 );
88 add_requirement( ALTIVEC );
90 set_callbacks( Activate, NULL );
93 /*****************************************************************************
94 * Activate: allocate a chroma function
95 *****************************************************************************
96 * This function allocates and initializes a chroma function
97 *****************************************************************************/
98 static int Activate( vlc_object_t *p_this )
100 vout_thread_t *p_vout = (vout_thread_t *)p_this;
102 if( p_vout->render.i_width & 1 || p_vout->render.i_height & 1 )
107 switch( p_vout->render.i_chroma )
109 case VLC_FOURCC('Y','V','1','2'):
110 case VLC_FOURCC('I','4','2','0'):
111 case VLC_FOURCC('I','Y','U','V'):
112 switch( p_vout->output.i_chroma )
114 case VLC_FOURCC('Y','U','Y','2'):
115 case VLC_FOURCC('Y','U','N','V'):
116 p_vout->chroma.pf_convert = I420_YUY2;
119 #if !defined (MODULE_NAME_IS_i420_yuy2_altivec)
120 case VLC_FOURCC('Y','V','Y','U'):
121 p_vout->chroma.pf_convert = I420_YVYU;
124 case VLC_FOURCC('U','Y','V','Y'):
125 case VLC_FOURCC('U','Y','N','V'):
126 case VLC_FOURCC('Y','4','2','2'):
127 p_vout->chroma.pf_convert = I420_UYVY;
130 case VLC_FOURCC('I','U','Y','V'):
131 p_vout->chroma.pf_convert = I420_IUYV;
134 case VLC_FOURCC('c','y','u','v'):
135 p_vout->chroma.pf_convert = I420_cyuv;
139 #if defined (MODULE_NAME_IS_i420_yuy2)
140 case VLC_FOURCC('Y','2','1','1'):
141 p_vout->chroma.pf_convert = I420_Y211;
157 /* Following functions are local */
159 /*****************************************************************************
160 * I420_YUY2: planar YUV 4:2:0 to packed YUYV 4:2:2
161 *****************************************************************************/
162 static void I420_YUY2( vout_thread_t *p_vout, picture_t *p_source,
165 uint8_t *p_line1, *p_line2 = p_dest->p->p_pixels;
166 uint8_t *p_y1, *p_y2 = p_source->Y_PIXELS;
167 uint8_t *p_u = p_source->U_PIXELS;
168 uint8_t *p_v = p_source->V_PIXELS;
172 #if defined (MODULE_NAME_IS_i420_yuy2_altivec)
173 #define VEC_NEXT_LINES( ) \
175 p_line2 += p_dest->p->i_pitch; \
177 p_y2 += p_source->p[Y_PLANE].i_pitch;
179 #define VEC_LOAD_UV( ) \
180 u_vec = vec_ld( 0, p_u ); p_u += 16; \
181 v_vec = vec_ld( 0, p_v ); p_v += 16;
183 #define VEC_MERGE( a ) \
184 uv_vec = a( u_vec, v_vec ); \
185 y_vec = vec_ld( 0, p_y1 ); p_y1 += 16; \
186 vec_st( vec_mergeh( y_vec, uv_vec ), 0, p_line1 ); p_line1 += 16; \
187 vec_st( vec_mergel( y_vec, uv_vec ), 0, p_line1 ); p_line1 += 16; \
188 y_vec = vec_ld( 0, p_y2 ); p_y2 += 16; \
189 vec_st( vec_mergeh( y_vec, uv_vec ), 0, p_line2 ); p_line2 += 16; \
190 vec_st( vec_mergel( y_vec, uv_vec ), 0, p_line2 ); p_line2 += 16;
192 vector unsigned char u_vec;
193 vector unsigned char v_vec;
194 vector unsigned char uv_vec;
195 vector unsigned char y_vec;
197 if( !( ( p_vout->render.i_width % 32 ) |
198 ( p_vout->render.i_height % 2 ) ) )
200 /* Width is a multiple of 32, we take 2 lines at a time */
201 for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
204 for( i_x = p_vout->render.i_width / 32 ; i_x-- ; )
207 VEC_MERGE( vec_mergeh );
208 VEC_MERGE( vec_mergel );
212 else if( !( ( p_vout->render.i_width % 16 ) |
213 ( p_vout->render.i_height % 4 ) ) )
215 /* Width is only a multiple of 16, we take 4 lines at a time */
216 for( i_y = p_vout->render.i_height / 4 ; i_y-- ; )
218 /* Line 1 and 2, pixels 0 to ( width - 16 ) */
220 for( i_x = p_vout->render.i_width / 32 ; i_x-- ; )
223 VEC_MERGE( vec_mergeh );
224 VEC_MERGE( vec_mergel );
227 /* Line 1 and 2, pixels ( width - 16 ) to ( width ) */
229 VEC_MERGE( vec_mergeh );
231 /* Line 3 and 4, pixels 0 to 16 */
233 VEC_MERGE( vec_mergel );
235 /* Line 3 and 4, pixels 16 to ( width ) */
236 for( i_x = p_vout->render.i_width / 32 ; i_x-- ; )
239 VEC_MERGE( vec_mergeh );
240 VEC_MERGE( vec_mergel );
246 /* Crap, use the C version */
247 #undef VEC_NEXT_LINES
252 const int i_source_margin = p_source->p->i_pitch
253 - p_source->p->i_visible_pitch;
254 const int i_dest_margin = p_dest->p->i_pitch
255 - p_dest->p->i_visible_pitch;
257 for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
260 p_line2 += p_dest->p->i_pitch;
263 p_y2 += p_source->p[Y_PLANE].i_pitch;
265 #if !defined (MODULE_NAME_IS_i420_yuy2_mmx)
266 for( i_x = p_vout->render.i_width / 2 ; i_x-- ; )
271 for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
273 MMX_CALL( MMX_YUV420_YUYV );
275 for( i_x = ( p_vout->render.i_width % 8 ) / 2; i_x-- ; )
281 p_y1 += i_source_margin;
282 p_y2 += i_source_margin;
283 p_line1 += i_dest_margin;
284 p_line2 += i_dest_margin;
287 #if defined (MODULE_NAME_IS_i420_yuy2_altivec)
292 /*****************************************************************************
293 * I420_YVYU: planar YUV 4:2:0 to packed YVYU 4:2:2
294 *****************************************************************************/
295 #if !defined (MODULE_NAME_IS_i420_yuy2_altivec)
296 static void I420_YVYU( vout_thread_t *p_vout, picture_t *p_source,
299 uint8_t *p_line1, *p_line2 = p_dest->p->p_pixels;
300 uint8_t *p_y1, *p_y2 = p_source->Y_PIXELS;
301 uint8_t *p_u = p_source->U_PIXELS;
302 uint8_t *p_v = p_source->V_PIXELS;
306 const int i_source_margin = p_source->p->i_pitch
307 - p_source->p->i_visible_pitch;
308 const int i_dest_margin = p_dest->p->i_pitch
309 - p_dest->p->i_visible_pitch;
311 for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
314 p_line2 += p_dest->p->i_pitch;
317 p_y2 += p_source->p[Y_PLANE].i_pitch;
319 for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
321 #if defined (MODULE_NAME_IS_i420_yuy2)
327 MMX_CALL( MMX_YUV420_YVYU );
331 p_y1 += i_source_margin;
332 p_y2 += i_source_margin;
333 p_line1 += i_dest_margin;
334 p_line2 += i_dest_margin;
338 /*****************************************************************************
339 * I420_UYVY: planar YUV 4:2:0 to packed UYVY 4:2:2
340 *****************************************************************************/
341 static void I420_UYVY( vout_thread_t *p_vout, picture_t *p_source,
344 uint8_t *p_line1, *p_line2 = p_dest->p->p_pixels;
345 uint8_t *p_y1, *p_y2 = p_source->Y_PIXELS;
346 uint8_t *p_u = p_source->U_PIXELS;
347 uint8_t *p_v = p_source->V_PIXELS;
351 const int i_source_margin = p_source->p->i_pitch
352 - p_source->p->i_visible_pitch;
353 const int i_dest_margin = p_dest->p->i_pitch
354 - p_dest->p->i_visible_pitch;
356 for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
359 p_line2 += p_dest->p->i_pitch;
362 p_y2 += p_source->p[Y_PLANE].i_pitch;
364 for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
366 #if defined (MODULE_NAME_IS_i420_yuy2)
372 MMX_CALL( MMX_YUV420_UYVY );
376 p_y1 += i_source_margin;
377 p_y2 += i_source_margin;
378 p_line1 += i_dest_margin;
379 p_line2 += i_dest_margin;
383 /*****************************************************************************
384 * I420_IUYV: planar YUV 4:2:0 to interleaved packed UYVY 4:2:2
385 *****************************************************************************/
386 static void I420_IUYV( vout_thread_t *p_vout, picture_t *p_source,
390 msg_Err( p_vout, "I420_IUYV unimplemented, please harass <sam@zoy.org>" );
393 /*****************************************************************************
394 * I420_cyuv: planar YUV 4:2:0 to upside-down packed UYVY 4:2:2
395 *****************************************************************************/
396 static void I420_cyuv( vout_thread_t *p_vout, picture_t *p_source,
399 uint8_t *p_line1 = p_dest->p->p_pixels +
400 p_dest->p->i_lines * p_dest->p->i_pitch
401 + p_dest->p->i_pitch;
402 uint8_t *p_line2 = p_dest->p->p_pixels +
403 p_dest->p->i_lines * p_dest->p->i_pitch;
404 uint8_t *p_y1, *p_y2 = p_source->Y_PIXELS;
405 uint8_t *p_u = p_source->U_PIXELS;
406 uint8_t *p_v = p_source->V_PIXELS;
410 const int i_source_margin = p_source->p->i_pitch
411 - p_source->p->i_visible_pitch;
412 const int i_dest_margin = p_dest->p->i_pitch
413 - p_dest->p->i_visible_pitch;
415 for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
417 p_line1 -= 3 * p_dest->p->i_pitch;
418 p_line2 -= 3 * p_dest->p->i_pitch;
421 p_y2 += p_source->p[Y_PLANE].i_pitch;
423 for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
425 #if defined (MODULE_NAME_IS_i420_yuy2)
431 MMX_CALL( MMX_YUV420_UYVY );
435 p_y1 += i_source_margin;
436 p_y2 += i_source_margin;
437 p_line1 += i_dest_margin;
438 p_line2 += i_dest_margin;
441 #endif // !defined (MODULE_NAME_IS_i420_yuy2_altivec)
443 /*****************************************************************************
444 * I420_Y211: planar YUV 4:2:0 to packed YUYV 2:1:1
445 *****************************************************************************/
446 #if defined (MODULE_NAME_IS_i420_yuy2)
447 static void I420_Y211( vout_thread_t *p_vout, picture_t *p_source,
450 uint8_t *p_line1, *p_line2 = p_dest->p->p_pixels;
451 uint8_t *p_y1, *p_y2 = p_source->Y_PIXELS;
452 uint8_t *p_u = p_source->U_PIXELS;
453 uint8_t *p_v = p_source->V_PIXELS;
457 const int i_source_margin = p_source->p->i_pitch
458 - p_source->p->i_visible_pitch;
459 const int i_dest_margin = p_dest->p->i_pitch
460 - p_dest->p->i_visible_pitch;
462 for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
465 p_line2 += p_dest->p->i_pitch;
468 p_y2 += p_source->p[Y_PLANE].i_pitch;
470 for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
476 p_y1 += i_source_margin;
477 p_y2 += i_source_margin;
478 p_line1 += i_dest_margin;
479 p_line2 += i_dest_margin;