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 for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
267 #if !defined (MODULE_NAME_IS_i420_yuy2_mmx)
273 MMX_CALL( MMX_YUV420_YUYV );
277 p_y1 += i_source_margin;
278 p_y2 += i_source_margin;
279 p_line1 += i_dest_margin;
280 p_line2 += i_dest_margin;
283 #if defined (MODULE_NAME_IS_i420_yuy2_altivec)
288 /*****************************************************************************
289 * I420_YVYU: planar YUV 4:2:0 to packed YVYU 4:2:2
290 *****************************************************************************/
291 #if !defined (MODULE_NAME_IS_i420_yuy2_altivec)
292 static void I420_YVYU( vout_thread_t *p_vout, picture_t *p_source,
295 uint8_t *p_line1, *p_line2 = p_dest->p->p_pixels;
296 uint8_t *p_y1, *p_y2 = p_source->Y_PIXELS;
297 uint8_t *p_u = p_source->U_PIXELS;
298 uint8_t *p_v = p_source->V_PIXELS;
302 const int i_source_margin = p_source->p->i_pitch
303 - p_source->p->i_visible_pitch;
304 const int i_dest_margin = p_dest->p->i_pitch
305 - p_dest->p->i_visible_pitch;
307 for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
310 p_line2 += p_dest->p->i_pitch;
313 p_y2 += p_source->p[Y_PLANE].i_pitch;
315 for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
317 #if defined (MODULE_NAME_IS_i420_yuy2)
323 MMX_CALL( MMX_YUV420_YVYU );
327 p_y1 += i_source_margin;
328 p_y2 += i_source_margin;
329 p_line1 += i_dest_margin;
330 p_line2 += i_dest_margin;
334 /*****************************************************************************
335 * I420_UYVY: planar YUV 4:2:0 to packed UYVY 4:2:2
336 *****************************************************************************/
337 static void I420_UYVY( vout_thread_t *p_vout, picture_t *p_source,
340 uint8_t *p_line1, *p_line2 = p_dest->p->p_pixels;
341 uint8_t *p_y1, *p_y2 = p_source->Y_PIXELS;
342 uint8_t *p_u = p_source->U_PIXELS;
343 uint8_t *p_v = p_source->V_PIXELS;
347 const int i_source_margin = p_source->p->i_pitch
348 - p_source->p->i_visible_pitch;
349 const int i_dest_margin = p_dest->p->i_pitch
350 - p_dest->p->i_visible_pitch;
352 for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
355 p_line2 += p_dest->p->i_pitch;
358 p_y2 += p_source->p[Y_PLANE].i_pitch;
360 for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
362 #if defined (MODULE_NAME_IS_i420_yuy2)
368 MMX_CALL( MMX_YUV420_UYVY );
372 p_y1 += i_source_margin;
373 p_y2 += i_source_margin;
374 p_line1 += i_dest_margin;
375 p_line2 += i_dest_margin;
379 /*****************************************************************************
380 * I420_IUYV: planar YUV 4:2:0 to interleaved packed UYVY 4:2:2
381 *****************************************************************************/
382 static void I420_IUYV( vout_thread_t *p_vout, picture_t *p_source,
386 msg_Err( p_vout, "I420_IUYV unimplemented, please harass <sam@zoy.org>" );
389 /*****************************************************************************
390 * I420_cyuv: planar YUV 4:2:0 to upside-down packed UYVY 4:2:2
391 *****************************************************************************/
392 static void I420_cyuv( vout_thread_t *p_vout, picture_t *p_source,
395 uint8_t *p_line1 = p_dest->p->p_pixels +
396 p_dest->p->i_lines * p_dest->p->i_pitch
397 + p_dest->p->i_pitch;
398 uint8_t *p_line2 = p_dest->p->p_pixels +
399 p_dest->p->i_lines * p_dest->p->i_pitch;
400 uint8_t *p_y1, *p_y2 = p_source->Y_PIXELS;
401 uint8_t *p_u = p_source->U_PIXELS;
402 uint8_t *p_v = p_source->V_PIXELS;
406 const int i_source_margin = p_source->p->i_pitch
407 - p_source->p->i_visible_pitch;
408 const int i_dest_margin = p_dest->p->i_pitch
409 - p_dest->p->i_visible_pitch;
411 for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
413 p_line1 -= 3 * p_dest->p->i_pitch;
414 p_line2 -= 3 * p_dest->p->i_pitch;
417 p_y2 += p_source->p[Y_PLANE].i_pitch;
419 for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
421 #if defined (MODULE_NAME_IS_i420_yuy2)
427 MMX_CALL( MMX_YUV420_UYVY );
431 p_y1 += i_source_margin;
432 p_y2 += i_source_margin;
433 p_line1 += i_dest_margin;
434 p_line2 += i_dest_margin;
437 #endif // !defined (MODULE_NAME_IS_i420_yuy2_altivec)
439 /*****************************************************************************
440 * I420_Y211: planar YUV 4:2:0 to packed YUYV 2:1:1
441 *****************************************************************************/
442 #if defined (MODULE_NAME_IS_i420_yuy2)
443 static void I420_Y211( vout_thread_t *p_vout, picture_t *p_source,
446 uint8_t *p_line1, *p_line2 = p_dest->p->p_pixels;
447 uint8_t *p_y1, *p_y2 = p_source->Y_PIXELS;
448 uint8_t *p_u = p_source->U_PIXELS;
449 uint8_t *p_v = p_source->V_PIXELS;
453 const int i_source_margin = p_source->p->i_pitch
454 - p_source->p->i_visible_pitch;
455 const int i_dest_margin = p_dest->p->i_pitch
456 - p_dest->p->i_visible_pitch;
458 for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
461 p_line2 += p_dest->p->i_pitch;
464 p_y2 += p_source->p[Y_PLANE].i_pitch;
466 for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
472 p_y1 += i_source_margin;
473 p_y2 += i_source_margin;
474 p_line1 += i_dest_margin;
475 p_line2 += i_dest_margin;