]> git.sesse.net Git - vlc/blob - plugins/chroma/i420_yuy2.c
* ./plugins/chroma/i420_yuy2.c: margin support for the 420 planar to 422
[vlc] / plugins / chroma / i420_yuy2.c
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.7 2002/05/21 03:37:17 sam Exp $
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <errno.h>                                                 /* ENOMEM */
28 #include <string.h>                                            /* strerror() */
29 #include <stdlib.h>                                      /* malloc(), free() */
30
31 #include <videolan/vlc.h>
32
33 #include "video.h"
34 #include "video_output.h"
35
36 #include "i420_yuy2.h"
37
38 #define SRC_FOURCC  "I420,IYUV,YV12"
39
40 #if defined (MODULE_NAME_IS_chroma_i420_yuy2)
41 #    define DEST_FOURCC "YUY2,YUNV,YVYU,UYVY,UYNV,Y422,IUYV,cyuv,Y211"
42 #else
43 #    define DEST_FOURCC "YUY2,YUNV,YVYU,UYVY,UYNV,Y422,IUYV,cyuv"
44 #endif
45
46 /*****************************************************************************
47  * Local and extern prototypes.
48  *****************************************************************************/
49 static void chroma_getfunctions ( function_list_t * p_function_list );
50
51 static int  chroma_Init         ( vout_thread_t *p_vout );
52 static void chroma_End          ( vout_thread_t *p_vout );
53
54 static void I420_YUY2           ( vout_thread_t *, picture_t *, picture_t * );
55 static void I420_YVYU           ( vout_thread_t *, picture_t *, picture_t * );
56 static void I420_UYVY           ( vout_thread_t *, picture_t *, picture_t * );
57 static void I420_IUYV           ( vout_thread_t *, picture_t *, picture_t * );
58 static void I420_cyuv           ( vout_thread_t *, picture_t *, picture_t * );
59 #if defined (MODULE_NAME_IS_chroma_i420_yuy2)
60 static void I420_Y211           ( vout_thread_t *, picture_t *, picture_t * );
61 #endif
62
63 /*****************************************************************************
64  * Build configuration tree.
65  *****************************************************************************/
66 MODULE_CONFIG_START
67 MODULE_CONFIG_STOP
68
69 MODULE_INIT_START
70 #if defined (MODULE_NAME_IS_chroma_i420_yuy2)
71     SET_DESCRIPTION( _("conversions from " SRC_FOURCC " to " DEST_FOURCC) )
72     ADD_CAPABILITY( CHROMA, 80 )
73 #elif defined (MODULE_NAME_IS_chroma_i420_yuy2_mmx)
74     SET_DESCRIPTION( _("MMX conversions from " SRC_FOURCC " to " DEST_FOURCC) )
75     ADD_CAPABILITY( CHROMA, 100 )
76     ADD_REQUIREMENT( MMX )
77 #endif
78 MODULE_INIT_STOP
79
80 MODULE_ACTIVATE_START
81     chroma_getfunctions( &p_module->p_functions->chroma );
82 MODULE_ACTIVATE_STOP
83
84 MODULE_DEACTIVATE_START
85 MODULE_DEACTIVATE_STOP
86
87 /*****************************************************************************
88  * Functions exported as capabilities. They are declared as static so that
89  * we don't pollute the namespace too much.
90  *****************************************************************************/
91 static void chroma_getfunctions( function_list_t * p_function_list )
92 {
93     p_function_list->functions.chroma.pf_init = chroma_Init;
94     p_function_list->functions.chroma.pf_end  = chroma_End;
95 }
96
97 /*****************************************************************************
98  * chroma_Init: allocate a chroma function
99  *****************************************************************************
100  * This function allocates and initializes a chroma function
101  *****************************************************************************/
102 static int chroma_Init( vout_thread_t *p_vout )
103 {
104     if( p_vout->render.i_width & 1 || p_vout->render.i_height & 1 )
105     {
106         return -1;
107     }
108
109     switch( p_vout->render.i_chroma )
110     {
111         case FOURCC_YV12:
112         case FOURCC_I420:
113         case FOURCC_IYUV:
114             switch( p_vout->output.i_chroma )
115             {
116                 case FOURCC_YUY2:
117                 case FOURCC_YUNV:
118                     p_vout->chroma.pf_convert = I420_YUY2;
119                     break;
120
121                 case FOURCC_YVYU:
122                     p_vout->chroma.pf_convert = I420_YVYU;
123                     break;
124
125                 case FOURCC_UYVY:
126                 case FOURCC_UYNV:
127                 case FOURCC_Y422:
128                     p_vout->chroma.pf_convert = I420_UYVY;
129                     break;
130
131                 case FOURCC_IUYV:
132                     p_vout->chroma.pf_convert = I420_IUYV;
133                     break;
134
135                 case FOURCC_cyuv:
136                     p_vout->chroma.pf_convert = I420_cyuv;
137                     break;
138
139 #if defined (MODULE_NAME_IS_chroma_i420_yuy2)
140                 case FOURCC_Y211:
141                     p_vout->chroma.pf_convert = I420_Y211;
142                     break;
143 #endif
144
145                 default:
146                     return -1;
147             }
148             break;
149
150         default:
151             return -1;
152     }
153
154     return 0;
155 }
156
157 /*****************************************************************************
158  * chroma_End: free the chroma function
159  *****************************************************************************
160  * This function frees the previously allocated chroma function
161  *****************************************************************************/
162 static void chroma_End( vout_thread_t *p_vout )
163 {
164     ;
165 }
166
167 /* Following functions are local */
168
169 /*****************************************************************************
170  * I420_YUY2: planar YUV 4:2:0 to packed YUYV 4:2:2
171  *****************************************************************************/
172 static void I420_YUY2( vout_thread_t *p_vout, picture_t *p_source,
173                                               picture_t *p_dest )
174 {
175     u8 *p_line1, *p_line2 = p_dest->p->p_pixels;
176     u8 *p_y1, *p_y2 = p_source->Y_PIXELS;
177     u8 *p_u = p_source->U_PIXELS;
178     u8 *p_v = p_source->V_PIXELS;
179
180     int i_x, i_y;
181
182     const int i_source_margin = p_source->p->b_margin ?
183         p_source->p->i_pitch - p_source->p->i_visible_bytes : 0;
184     const int i_dest_margin = p_dest->p->b_margin ?
185         p_dest->p->i_pitch - p_dest->p->i_visible_bytes : 0;
186
187     for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
188     {
189         p_line1 = p_line2;
190         p_line2 += p_dest->p->i_pitch;
191
192         p_y1 = p_y2;
193         p_y2 += p_source->p[Y_PLANE].i_pitch;
194
195         for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
196         {
197 #if defined (MODULE_NAME_IS_chroma_i420_yuy2)
198             C_YUV420_YUYV( );
199             C_YUV420_YUYV( );
200             C_YUV420_YUYV( );
201             C_YUV420_YUYV( );
202 #else
203             MMX_CALL( MMX_YUV420_YUYV );
204 #endif
205         }
206
207         p_y1 += i_source_margin;
208         p_y2 += i_source_margin;
209         p_line1 += i_dest_margin;
210         p_line2 += i_dest_margin;
211     }
212 }
213
214 /*****************************************************************************
215  * I420_YVYU: planar YUV 4:2:0 to packed YVYU 4:2:2
216  *****************************************************************************/
217 static void I420_YVYU( vout_thread_t *p_vout, picture_t *p_source,
218                                               picture_t *p_dest )
219 {
220     u8 *p_line1, *p_line2 = p_dest->p->p_pixels;
221     u8 *p_y1, *p_y2 = p_source->Y_PIXELS;
222     u8 *p_u = p_source->U_PIXELS;
223     u8 *p_v = p_source->V_PIXELS;
224
225     int i_x, i_y;
226
227     const int i_source_margin = p_source->p->b_margin ?
228         p_source->p->i_pitch - p_source->p->i_visible_bytes : 0;
229     const int i_dest_margin = p_dest->p->b_margin ?
230         p_dest->p->i_pitch - p_dest->p->i_visible_bytes : 0;
231
232     for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
233     {
234         p_line1 = p_line2;
235         p_line2 += p_dest->p->i_pitch;
236
237         p_y1 = p_y2;
238         p_y2 += p_source->p[Y_PLANE].i_pitch;
239
240         for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
241         {
242 #if defined (MODULE_NAME_IS_chroma_i420_yuy2)
243             C_YUV420_YVYU( );
244             C_YUV420_YVYU( );
245             C_YUV420_YVYU( );
246             C_YUV420_YVYU( );
247 #else
248             MMX_CALL( MMX_YUV420_YVYU );
249 #endif
250         }
251
252         p_y1 += i_source_margin;
253         p_y2 += i_source_margin;
254         p_line1 += i_dest_margin;
255         p_line2 += i_dest_margin;
256     }
257 }
258
259 /*****************************************************************************
260  * I420_UYVY: planar YUV 4:2:0 to packed UYVY 4:2:2
261  *****************************************************************************/
262 static void I420_UYVY( vout_thread_t *p_vout, picture_t *p_source,
263                                               picture_t *p_dest )
264 {
265     u8 *p_line1, *p_line2 = p_dest->p->p_pixels;
266     u8 *p_y1, *p_y2 = p_source->Y_PIXELS;
267     u8 *p_u = p_source->U_PIXELS;
268     u8 *p_v = p_source->V_PIXELS;
269
270     int i_x, i_y;
271
272     const int i_source_margin = p_source->p->b_margin ?
273         p_source->p->i_pitch - p_source->p->i_visible_bytes : 0;
274     const int i_dest_margin = p_dest->p->b_margin ?
275         p_dest->p->i_pitch - p_dest->p->i_visible_bytes : 0;
276
277     for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
278     {
279         p_line1 = p_line2;
280         p_line2 += p_dest->p->i_pitch;
281
282         p_y1 = p_y2;
283         p_y2 += p_source->p[Y_PLANE].i_pitch;
284
285         for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
286         {
287 #if defined (MODULE_NAME_IS_chroma_i420_yuy2)
288             C_YUV420_UYVY( );
289             C_YUV420_UYVY( );
290             C_YUV420_UYVY( );
291             C_YUV420_UYVY( );
292 #else
293             MMX_CALL( MMX_YUV420_UYVY );
294 #endif
295         }
296
297         p_y1 += i_source_margin;
298         p_y2 += i_source_margin;
299         p_line1 += i_dest_margin;
300         p_line2 += i_dest_margin;
301     }
302 }
303
304 /*****************************************************************************
305  * I420_IUYV: planar YUV 4:2:0 to interleaved packed UYVY 4:2:2
306  *****************************************************************************/
307 static void I420_IUYV( vout_thread_t *p_vout, picture_t *p_source,
308                                               picture_t *p_dest )
309 {
310     /* FIXME: TODO ! */
311     intf_ErrMsg( "chroma error: I420_IUYV unimplemented, "
312                  "please harass <sam@zoy.org>" );
313 }
314
315 /*****************************************************************************
316  * I420_cyuv: planar YUV 4:2:0 to upside-down packed UYVY 4:2:2
317  *****************************************************************************/
318 static void I420_cyuv( vout_thread_t *p_vout, picture_t *p_source,
319                                               picture_t *p_dest )
320 {
321     u8 *p_line1 = p_dest->p->p_pixels + p_dest->p->i_lines * p_dest->p->i_pitch
322                                       + p_dest->p->i_pitch;
323     u8 *p_line2 = p_dest->p->p_pixels + p_dest->p->i_lines * p_dest->p->i_pitch;
324     u8 *p_y1, *p_y2 = p_source->Y_PIXELS;
325     u8 *p_u = p_source->U_PIXELS;
326     u8 *p_v = p_source->V_PIXELS;
327
328     int i_x, i_y;
329
330     const int i_source_margin = p_source->p->b_margin ?
331         p_source->p->i_pitch - p_source->p->i_visible_bytes : 0;
332     const int i_dest_margin = p_dest->p->b_margin ?
333         p_dest->p->i_pitch - p_dest->p->i_visible_bytes : 0;
334
335     for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
336     {
337         p_line1 -= 3 * p_dest->p->i_pitch;
338         p_line2 -= 3 * p_dest->p->i_pitch;
339
340         p_y1 = p_y2;
341         p_y2 += p_source->p[Y_PLANE].i_pitch;
342
343         for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
344         {
345 #if defined (MODULE_NAME_IS_chroma_i420_yuy2)
346             C_YUV420_UYVY( );
347             C_YUV420_UYVY( );
348             C_YUV420_UYVY( );
349             C_YUV420_UYVY( );
350 #else
351             MMX_CALL( MMX_YUV420_UYVY );
352 #endif
353         }
354
355         p_y1 += i_source_margin;
356         p_y2 += i_source_margin;
357         p_line1 += i_dest_margin;
358         p_line2 += i_dest_margin;
359     }
360 }
361
362 /*****************************************************************************
363  * I420_Y211: planar YUV 4:2:0 to packed YUYV 2:1:1
364  *****************************************************************************/
365 #if defined (MODULE_NAME_IS_chroma_i420_yuy2)
366 static void I420_Y211( vout_thread_t *p_vout, picture_t *p_source,
367                                               picture_t *p_dest )
368 {
369     u8 *p_line1, *p_line2 = p_dest->p->p_pixels;
370     u8 *p_y1, *p_y2 = p_source->Y_PIXELS;
371     u8 *p_u = p_source->U_PIXELS;
372     u8 *p_v = p_source->V_PIXELS;
373
374     int i_x, i_y;
375
376     const int i_source_margin = p_source->p->b_margin ?
377         p_source->p->i_pitch - p_source->p->i_visible_bytes : 0;
378     const int i_dest_margin = p_dest->p->b_margin ?
379         p_dest->p->i_pitch - p_dest->p->i_visible_bytes : 0;
380
381     for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
382     {
383         p_line1 = p_line2;
384         p_line2 += p_dest->p->i_pitch;
385
386         p_y1 = p_y2;
387         p_y2 += p_source->p[Y_PLANE].i_pitch;
388
389         for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
390         {
391             C_YUV420_Y211( );
392             C_YUV420_Y211( );
393         }
394
395         p_y1 += i_source_margin;
396         p_y2 += i_source_margin;
397         p_line1 += i_dest_margin;
398         p_line2 += i_dest_margin;
399     }
400 }
401 #endif
402