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() */
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 #if defined (MODULE_NAME_IS_i420_yuy2_altivec)
169 #define VEC_NEXT_LINES( ) \
171 p_line2 += p_dest->p->i_pitch; \
173 p_y2 += p_source->p[Y_PLANE].i_pitch;
175 #define VEC_LOAD_UV( ) \
176 u_vec = vec_ld( 0, p_u ); p_u += 16; \
177 v_vec = vec_ld( 0, p_v ); p_v += 16;
179 #define VEC_MERGE( a ) \
180 uv_vec = a( u_vec, v_vec ); \
181 y_vec = vec_ld( 0, p_y1 ); p_y1 += 16; \
182 vec_st( vec_mergeh( y_vec, uv_vec ), 0, p_line1 ); p_line1 += 16; \
183 vec_st( vec_mergel( y_vec, uv_vec ), 0, p_line1 ); p_line1 += 16; \
184 y_vec = vec_ld( 0, p_y2 ); p_y2 += 16; \
185 vec_st( vec_mergeh( y_vec, uv_vec ), 0, p_line2 ); p_line2 += 16; \
186 vec_st( vec_mergel( y_vec, uv_vec ), 0, p_line2 ); p_line2 += 16;
188 vector unsigned char u_vec;
189 vector unsigned char v_vec;
190 vector unsigned char uv_vec;
191 vector unsigned char y_vec;
193 if( !( ( p_vout->render.i_width % 32 ) |
194 ( p_vout->render.i_height % 2 ) ) )
196 /* Width is a multiple of 32, we take 2 lines at a time */
197 for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
200 for( i_x = p_vout->render.i_width / 32 ; i_x-- ; )
203 VEC_MERGE( vec_mergeh );
204 VEC_MERGE( vec_mergel );
208 else if( !( ( p_vout->render.i_width % 16 ) |
209 ( p_vout->render.i_height % 4 ) ) )
211 /* Width is only a multiple of 16, we take 4 lines at a time */
212 for( i_y = p_vout->render.i_height / 4 ; i_y-- ; )
214 /* Line 1 and 2, pixels 0 to ( width - 16 ) */
216 for( i_x = p_vout->render.i_width / 32 ; i_x-- ; )
219 VEC_MERGE( vec_mergeh );
220 VEC_MERGE( vec_mergel );
223 /* Line 1 and 2, pixels ( width - 16 ) to ( width ) */
225 VEC_MERGE( vec_mergeh );
227 /* Line 3 and 4, pixels 0 to 16 */
229 VEC_MERGE( vec_mergel );
231 /* Line 3 and 4, pixels 16 to ( width ) */
232 for( i_x = p_vout->render.i_width / 32 ; i_x-- ; )
235 VEC_MERGE( vec_mergeh );
236 VEC_MERGE( vec_mergel );
242 /* Crap, use the C version */
243 #undef VEC_NEXT_LINES
248 const int i_source_margin = p_source->p->i_pitch
249 - p_source->p->i_visible_pitch;
250 const int i_dest_margin = p_dest->p->i_pitch
251 - p_dest->p->i_visible_pitch;
253 for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
256 p_line2 += p_dest->p->i_pitch;
259 p_y2 += p_source->p[Y_PLANE].i_pitch;
261 for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
263 #if !defined (MODULE_NAME_IS_i420_yuy2_mmx)
269 MMX_CALL( MMX_YUV420_YUYV );
273 p_y1 += i_source_margin;
274 p_y2 += i_source_margin;
275 p_line1 += i_dest_margin;
276 p_line2 += i_dest_margin;
279 #if defined (MODULE_NAME_IS_i420_yuy2_altivec)
284 /*****************************************************************************
285 * I420_YVYU: planar YUV 4:2:0 to packed YVYU 4:2:2
286 *****************************************************************************/
287 #if !defined (MODULE_NAME_IS_i420_yuy2_altivec)
288 static void I420_YVYU( vout_thread_t *p_vout, picture_t *p_source,
291 uint8_t *p_line1, *p_line2 = p_dest->p->p_pixels;
292 uint8_t *p_y1, *p_y2 = p_source->Y_PIXELS;
293 uint8_t *p_u = p_source->U_PIXELS;
294 uint8_t *p_v = p_source->V_PIXELS;
298 const int i_source_margin = p_source->p->i_pitch
299 - p_source->p->i_visible_pitch;
300 const int i_dest_margin = p_dest->p->i_pitch
301 - p_dest->p->i_visible_pitch;
303 for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
306 p_line2 += p_dest->p->i_pitch;
309 p_y2 += p_source->p[Y_PLANE].i_pitch;
311 for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
313 #if defined (MODULE_NAME_IS_i420_yuy2)
319 MMX_CALL( MMX_YUV420_YVYU );
323 p_y1 += i_source_margin;
324 p_y2 += i_source_margin;
325 p_line1 += i_dest_margin;
326 p_line2 += i_dest_margin;
330 /*****************************************************************************
331 * I420_UYVY: planar YUV 4:2:0 to packed UYVY 4:2:2
332 *****************************************************************************/
333 static void I420_UYVY( vout_thread_t *p_vout, picture_t *p_source,
336 uint8_t *p_line1, *p_line2 = p_dest->p->p_pixels;
337 uint8_t *p_y1, *p_y2 = p_source->Y_PIXELS;
338 uint8_t *p_u = p_source->U_PIXELS;
339 uint8_t *p_v = p_source->V_PIXELS;
343 const int i_source_margin = p_source->p->i_pitch
344 - p_source->p->i_visible_pitch;
345 const int i_dest_margin = p_dest->p->i_pitch
346 - p_dest->p->i_visible_pitch;
348 for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
351 p_line2 += p_dest->p->i_pitch;
354 p_y2 += p_source->p[Y_PLANE].i_pitch;
356 for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
358 #if defined (MODULE_NAME_IS_i420_yuy2)
364 MMX_CALL( MMX_YUV420_UYVY );
368 p_y1 += i_source_margin;
369 p_y2 += i_source_margin;
370 p_line1 += i_dest_margin;
371 p_line2 += i_dest_margin;
375 /*****************************************************************************
376 * I420_IUYV: planar YUV 4:2:0 to interleaved packed UYVY 4:2:2
377 *****************************************************************************/
378 static void I420_IUYV( vout_thread_t *p_vout, picture_t *p_source,
382 msg_Err( p_vout, "I420_IUYV unimplemented, please harass <sam@zoy.org>" );
385 /*****************************************************************************
386 * I420_cyuv: planar YUV 4:2:0 to upside-down packed UYVY 4:2:2
387 *****************************************************************************/
388 static void I420_cyuv( vout_thread_t *p_vout, picture_t *p_source,
391 uint8_t *p_line1 = p_dest->p->p_pixels +
392 p_dest->p->i_lines * p_dest->p->i_pitch
393 + p_dest->p->i_pitch;
394 uint8_t *p_line2 = p_dest->p->p_pixels +
395 p_dest->p->i_lines * p_dest->p->i_pitch;
396 uint8_t *p_y1, *p_y2 = p_source->Y_PIXELS;
397 uint8_t *p_u = p_source->U_PIXELS;
398 uint8_t *p_v = p_source->V_PIXELS;
402 const int i_source_margin = p_source->p->i_pitch
403 - p_source->p->i_visible_pitch;
404 const int i_dest_margin = p_dest->p->i_pitch
405 - p_dest->p->i_visible_pitch;
407 for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
409 p_line1 -= 3 * p_dest->p->i_pitch;
410 p_line2 -= 3 * p_dest->p->i_pitch;
413 p_y2 += p_source->p[Y_PLANE].i_pitch;
415 for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
417 #if defined (MODULE_NAME_IS_i420_yuy2)
423 MMX_CALL( MMX_YUV420_UYVY );
427 p_y1 += i_source_margin;
428 p_y2 += i_source_margin;
429 p_line1 += i_dest_margin;
430 p_line2 += i_dest_margin;
433 #endif // !defined (MODULE_NAME_IS_i420_yuy2_altivec)
435 /*****************************************************************************
436 * I420_Y211: planar YUV 4:2:0 to packed YUYV 2:1:1
437 *****************************************************************************/
438 #if defined (MODULE_NAME_IS_i420_yuy2)
439 static void I420_Y211( vout_thread_t *p_vout, picture_t *p_source,
442 uint8_t *p_line1, *p_line2 = p_dest->p->p_pixels;
443 uint8_t *p_y1, *p_y2 = p_source->Y_PIXELS;
444 uint8_t *p_u = p_source->U_PIXELS;
445 uint8_t *p_v = p_source->V_PIXELS;
449 const int i_source_margin = p_source->p->i_pitch
450 - p_source->p->i_visible_pitch;
451 const int i_dest_margin = p_dest->p->i_pitch
452 - p_dest->p->i_visible_pitch;
454 for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
457 p_line2 += p_dest->p->i_pitch;
460 p_y2 += p_source->p[Y_PLANE].i_pitch;
462 for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
468 p_y1 += i_source_margin;
469 p_y2 += i_source_margin;
470 p_line1 += i_dest_margin;
471 p_line2 += i_dest_margin;