]> git.sesse.net Git - vlc/blob - modules/video_chroma/i422_yuy2.c
058a010916b2ec3410507f25187e1c48c298ce2c
[vlc] / modules / video_chroma / i422_yuy2.c
1 /*****************************************************************************
2  * i422_yuy2.c : Planar YUV 4:2:2 to Packed YUV conversion module for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Damien Fouilleul <damienf@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_filter.h>
36 #include <vlc_vout.h>
37
38 #include "i422_yuy2.h"
39
40 #define SRC_FOURCC  "I422"
41 #if defined (MODULE_NAME_IS_i422_yuy2)
42 #    define DEST_FOURCC "YUY2,YUNV,YVYU,UYVY,UYNV,Y422,IUYV,cyuv,Y211"
43 #else
44 #    define DEST_FOURCC "YUY2,YUNV,YVYU,UYVY,UYNV,Y422,IUYV,cyuv"
45 #endif
46
47 /*****************************************************************************
48  * Local and extern prototypes.
49  *****************************************************************************/
50 static int  Activate ( vlc_object_t * );
51
52 static void I422_YUY2           ( filter_t *, picture_t *, picture_t * );
53 static void I422_YVYU           ( filter_t *, picture_t *, picture_t * );
54 static void I422_UYVY           ( filter_t *, picture_t *, picture_t * );
55 static void I422_IUYV           ( filter_t *, picture_t *, picture_t * );
56 static void I422_cyuv           ( filter_t *, picture_t *, picture_t * );
57 #if defined (MODULE_NAME_IS_i422_yuy2)
58 static void I422_Y211           ( filter_t *, picture_t *, picture_t * );
59 static void I422_Y211           ( filter_t *, picture_t *, picture_t * );
60 #endif
61
62 /*****************************************************************************
63  * Module descriptor
64  *****************************************************************************/
65 vlc_module_begin();
66 #if defined (MODULE_NAME_IS_i422_yuy2)
67     set_description( N_("Conversions from " SRC_FOURCC " to " DEST_FOURCC) );
68     set_capability( "chroma", 80 );
69 #elif defined (MODULE_NAME_IS_i422_yuy2_mmx)
70     set_description( N_("MMX conversions from " SRC_FOURCC " to " DEST_FOURCC) );
71     set_capability( "chroma", 100 );
72     add_requirement( MMX );
73 #elif defined (MODULE_NAME_IS_i422_yuy2_sse2)
74     set_description( N_("SSE2 conversions from " SRC_FOURCC " to " DEST_FOURCC) );
75     set_capability( "chroma", 120 );
76     add_requirement( SSE2 );
77 #endif
78     set_callbacks( Activate, NULL );
79 vlc_module_end();
80
81 /*****************************************************************************
82  * Activate: allocate a chroma function
83  *****************************************************************************
84  * This function allocates and initializes a chroma function
85  *****************************************************************************/
86 static int Activate( vlc_object_t *p_this )
87 {
88     filter_t *p_filter = (filter_t *)p_this;
89
90     if( p_filter->fmt_in.video.i_width & 1
91      || p_filter->fmt_in.video.i_height & 1 )
92     {
93         return -1;
94     }
95
96     switch( p_filter->fmt_in.video.i_chroma )
97     {
98         case VLC_FOURCC('I','4','2','2'):
99             switch( p_filter->fmt_out.video.i_chroma )
100             {
101                 case VLC_FOURCC('Y','U','Y','2'):
102                 case VLC_FOURCC('Y','U','N','V'):
103                     p_filter->pf_video_filter_io = I422_YUY2;
104                     break;
105
106                 case VLC_FOURCC('Y','V','Y','U'):
107                     p_filter->pf_video_filter_io = I422_YVYU;
108                     break;
109
110                 case VLC_FOURCC('U','Y','V','Y'):
111                 case VLC_FOURCC('U','Y','N','V'):
112                 case VLC_FOURCC('Y','4','2','2'):
113                     p_filter->pf_video_filter_io = I422_UYVY;
114                     break;
115
116                 case VLC_FOURCC('I','U','Y','V'):
117                     p_filter->pf_video_filter_io = I422_IUYV;
118                     break;
119
120                 case VLC_FOURCC('c','y','u','v'):
121                     p_filter->pf_video_filter_io = I422_cyuv;
122                     break;
123
124 #if defined (MODULE_NAME_IS_i422_yuy2)
125                 case VLC_FOURCC('Y','2','1','1'):
126                     p_filter->pf_video_filter_io = I422_Y211;
127                     break;
128 #endif
129
130                 default:
131                     return -1;
132             }
133             break;
134
135         default:
136             return -1;
137     }
138     return 0;
139 }
140
141 /* Following functions are local */
142
143 /*****************************************************************************
144  * I422_YUY2: planar YUV 4:2:2 to packed YUY2 4:2:2
145  *****************************************************************************/
146 static void I422_YUY2( filter_t *p_filter, picture_t *p_source,
147                                            picture_t *p_dest )
148 {
149     uint8_t *p_line = p_dest->p->p_pixels;
150     uint8_t *p_y = p_source->Y_PIXELS;
151     uint8_t *p_u = p_source->U_PIXELS;
152     uint8_t *p_v = p_source->V_PIXELS;
153
154     int i_x, i_y;
155
156     const int i_source_margin = p_source->p[0].i_pitch
157                                  - p_source->p[0].i_visible_pitch;
158     const int i_source_margin_c = p_source->p[1].i_pitch
159                                  - p_source->p[1].i_visible_pitch;
160     const int i_dest_margin = p_dest->p->i_pitch
161                                - p_dest->p->i_visible_pitch;
162
163 #if defined (MODULE_NAME_IS_i422_yuy2_sse2)
164
165     if( 0 == (15 & (p_source->p[Y_PLANE].i_pitch|p_dest->p->i_pitch|
166         ((intptr_t)p_line|(intptr_t)p_y))) )
167     {
168         /* use faster SSE2 aligned fetch and store */
169         for( i_y = p_filter->fmt_in.video.i_height ; i_y-- ; )
170         {
171             for( i_x = p_filter->fmt_in.video.i_width / 16 ; i_x-- ; )
172             {
173                 SSE2_CALL( SSE2_YUV422_YUYV_ALIGNED );
174             }
175             for( i_x = ( p_filter->fmt_in.video.i_width % 16 ) / 2; i_x-- ; )
176             {
177                 C_YUV422_YUYV( p_line, p_y, p_u, p_v );
178             }
179             p_y += i_source_margin;
180             p_u += i_source_margin_c;
181             p_v += i_source_margin_c;
182             p_line += i_dest_margin;
183         }
184     }
185     else {
186         /* use slower SSE2 unaligned fetch and store */
187         for( i_y = p_filter->fmt_in.video.i_height ; i_y-- ; )
188         {
189             for( i_x = p_filter->fmt_in.video.i_width / 16 ; i_x-- ; )
190             {
191                 SSE2_CALL( SSE2_YUV422_YUYV_UNALIGNED );
192             }
193             for( i_x = ( p_filter->fmt_in.video.i_width % 16 ) / 2; i_x-- ; )
194             {
195                 C_YUV422_YUYV( p_line, p_y, p_u, p_v );
196             }
197             p_y += i_source_margin;
198             p_u += i_source_margin_c;
199             p_v += i_source_margin_c;
200             p_line += i_dest_margin;
201         }
202     }
203     SSE2_END;
204
205 #else
206
207     for( i_y = p_filter->fmt_in.video.i_height ; i_y-- ; )
208     {
209         for( i_x = p_filter->fmt_in.video.i_width / 8 ; i_x-- ; )
210         {
211 #if defined (MODULE_NAME_IS_i422_yuy2)
212             C_YUV422_YUYV( p_line, p_y, p_u, p_v );
213             C_YUV422_YUYV( p_line, p_y, p_u, p_v );
214             C_YUV422_YUYV( p_line, p_y, p_u, p_v );
215             C_YUV422_YUYV( p_line, p_y, p_u, p_v );
216 #elif defined (MODULE_NAME_IS_i422_yuy2_mmx)
217             MMX_CALL( MMX_YUV422_YUYV );
218 #endif
219         }
220         for( i_x = ( p_filter->fmt_in.video.i_width % 8 ) / 2; i_x-- ; )
221         {
222             C_YUV422_YUYV( p_line, p_y, p_u, p_v );
223         }
224         p_y += i_source_margin;
225         p_u += i_source_margin_c;
226         p_v += i_source_margin_c;
227         p_line += i_dest_margin;
228     }
229 #if defined (MODULE_NAME_IS_i422_yuy2_mmx)
230     MMX_END;
231 #endif
232
233 #endif
234 }
235
236 /*****************************************************************************
237  * I422_YVYU: planar YUV 4:2:2 to packed YVYU 4:2:2
238  *****************************************************************************/
239 static void I422_YVYU( filter_t *p_filter, picture_t *p_source,
240                                            picture_t *p_dest )
241 {
242     uint8_t *p_line = p_dest->p->p_pixels;
243     uint8_t *p_y = p_source->Y_PIXELS;
244     uint8_t *p_u = p_source->U_PIXELS;
245     uint8_t *p_v = p_source->V_PIXELS;
246
247     int i_x, i_y;
248
249     const int i_source_margin = p_source->p[0].i_pitch
250                                  - p_source->p[0].i_visible_pitch;
251     const int i_source_margin_c = p_source->p[1].i_pitch
252                                  - p_source->p[1].i_visible_pitch;
253     const int i_dest_margin = p_dest->p->i_pitch
254                                - p_dest->p->i_visible_pitch;
255
256 #if defined (MODULE_NAME_IS_i422_yuy2_sse2)
257
258     if( 0 == (15 & (p_source->p[Y_PLANE].i_pitch|p_dest->p->i_pitch|
259         ((intptr_t)p_line|(intptr_t)p_y))) )
260     {
261         /* use faster SSE2 aligned fetch and store */
262         for( i_y = p_filter->fmt_in.video.i_height ; i_y-- ; )
263         {
264             for( i_x = p_filter->fmt_in.video.i_width / 16 ; i_x-- ; )
265             {
266                 SSE2_CALL( SSE2_YUV422_YVYU_ALIGNED );
267             }
268             for( i_x = ( p_filter->fmt_in.video.i_width % 16 ) / 2; i_x-- ; )
269             {
270                 C_YUV422_YVYU( p_line, p_y, p_u, p_v );
271             }
272             p_y += i_source_margin;
273             p_u += i_source_margin_c;
274             p_v += i_source_margin_c;
275             p_line += i_dest_margin;
276         }
277     }
278     else {
279         /* use slower SSE2 unaligned fetch and store */
280         for( i_y = p_filter->fmt_in.video.i_height ; i_y-- ; )
281         {
282             for( i_x = p_filter->fmt_in.video.i_width / 16 ; i_x-- ; )
283             {
284                 SSE2_CALL( SSE2_YUV422_YVYU_UNALIGNED );
285             }
286             for( i_x = ( p_filter->fmt_in.video.i_width % 16 ) / 2; i_x-- ; )
287             {
288                 C_YUV422_YVYU( p_line, p_y, p_u, p_v );
289             }
290             p_y += i_source_margin;
291             p_u += i_source_margin_c;
292             p_v += i_source_margin_c;
293             p_line += i_dest_margin;
294         }
295     }
296     SSE2_END;
297
298 #else
299
300     for( i_y = p_filter->fmt_in.video.i_height ; i_y-- ; )
301     {
302         for( i_x = p_filter->fmt_in.video.i_width / 8 ; i_x-- ; )
303         {
304 #if defined (MODULE_NAME_IS_i422_yuy2)
305             C_YUV422_YVYU( p_line, p_y, p_u, p_v );
306             C_YUV422_YVYU( p_line, p_y, p_u, p_v );
307             C_YUV422_YVYU( p_line, p_y, p_u, p_v );
308             C_YUV422_YVYU( p_line, p_y, p_u, p_v );
309 #elif defined (MODULE_NAME_IS_i422_yuy2_mmx)
310             MMX_CALL( MMX_YUV422_YVYU );
311 #endif
312         }
313         for( i_x = ( p_filter->fmt_in.video.i_width % 8 ) / 2; i_x-- ; )
314         {
315             C_YUV422_YVYU( p_line, p_y, p_u, p_v );
316         }
317         p_y += i_source_margin;
318         p_u += i_source_margin_c;
319         p_v += i_source_margin_c;
320         p_line += i_dest_margin;
321     }
322 #if defined (MODULE_NAME_IS_i422_yuy2_mmx)
323     MMX_END;
324 #endif
325
326 #endif
327 }
328
329 /*****************************************************************************
330  * I422_UYVY: planar YUV 4:2:2 to packed UYVY 4:2:2
331  *****************************************************************************/
332 static void I422_UYVY( filter_t *p_filter, picture_t *p_source,
333                                            picture_t *p_dest )
334 {
335     uint8_t *p_line = p_dest->p->p_pixels;
336     uint8_t *p_y = p_source->Y_PIXELS;
337     uint8_t *p_u = p_source->U_PIXELS;
338     uint8_t *p_v = p_source->V_PIXELS;
339
340     int i_x, i_y;
341
342     const int i_source_margin = p_source->p[0].i_pitch
343                                  - p_source->p[0].i_visible_pitch;
344     const int i_source_margin_c = p_source->p[1].i_pitch
345                                  - p_source->p[1].i_visible_pitch;
346     const int i_dest_margin = p_dest->p->i_pitch
347                                - p_dest->p->i_visible_pitch;
348
349 #if defined (MODULE_NAME_IS_i422_yuy2_sse2)
350
351     if( 0 == (15 & (p_source->p[Y_PLANE].i_pitch|p_dest->p->i_pitch|
352         ((intptr_t)p_line|(intptr_t)p_y))) )
353     {
354         /* use faster SSE2 aligned fetch and store */
355         for( i_y = p_filter->fmt_in.video.i_height ; i_y-- ; )
356         {
357             for( i_x = p_filter->fmt_in.video.i_width / 16 ; i_x-- ; )
358             {
359                 SSE2_CALL( SSE2_YUV422_UYVY_ALIGNED );
360             }
361             for( i_x = ( p_filter->fmt_in.video.i_width % 16 ) / 2; i_x-- ; )
362             {
363                 C_YUV422_UYVY( p_line, p_y, p_u, p_v );
364             }
365             p_y += i_source_margin;
366             p_u += i_source_margin_c;
367             p_v += i_source_margin_c;
368             p_line += i_dest_margin;
369         }
370     }
371     else {
372         /* use slower SSE2 unaligned fetch and store */
373         for( i_y = p_filter->fmt_in.video.i_height ; i_y-- ; )
374         {
375             for( i_x = p_filter->fmt_in.video.i_width / 16 ; i_x-- ; )
376             {
377                 SSE2_CALL( SSE2_YUV422_UYVY_UNALIGNED );
378             }
379             for( i_x = ( p_filter->fmt_in.video.i_width % 16 ) / 2; i_x-- ; )
380             {
381                 C_YUV422_UYVY( p_line, p_y, p_u, p_v );
382             }
383             p_y += i_source_margin;
384             p_u += i_source_margin_c;
385             p_v += i_source_margin_c;
386             p_line += i_dest_margin;
387         }
388     }
389     SSE2_END;
390
391 #else
392
393     for( i_y = p_filter->fmt_in.video.i_height ; i_y-- ; )
394     {
395         for( i_x = p_filter->fmt_in.video.i_width / 8 ; i_x-- ; )
396         {
397 #if defined (MODULE_NAME_IS_i422_yuy2)
398             C_YUV422_UYVY( p_line, p_y, p_u, p_v );
399             C_YUV422_UYVY( p_line, p_y, p_u, p_v );
400             C_YUV422_UYVY( p_line, p_y, p_u, p_v );
401             C_YUV422_UYVY( p_line, p_y, p_u, p_v );
402 #elif defined (MODULE_NAME_IS_i422_yuy2_mmx)
403             MMX_CALL( MMX_YUV422_UYVY );
404 #endif
405         }
406         for( i_x = ( p_filter->fmt_in.video.i_width % 8 ) / 2; i_x-- ; )
407         {
408             C_YUV422_UYVY( p_line, p_y, p_u, p_v );
409         }
410         p_y += i_source_margin;
411         p_u += i_source_margin_c;
412         p_v += i_source_margin_c;
413         p_line += i_dest_margin;
414     }
415 #if defined (MODULE_NAME_IS_i422_yuy2_mmx)
416     MMX_END;
417 #endif
418
419 #endif
420 }
421
422 /*****************************************************************************
423  * I422_IUYV: planar YUV 4:2:2 to interleaved packed IUYV 4:2:2
424  *****************************************************************************/
425 static void I422_IUYV( filter_t *p_filter, picture_t *p_source,
426                                            picture_t *p_dest )
427 {
428     VLC_UNUSED(p_source); VLC_UNUSED(p_dest);
429     /* FIXME: TODO ! */
430     msg_Err( p_filter, "I422_IUYV unimplemented, please harass <sam@zoy.org>" );
431 }
432
433 /*****************************************************************************
434  * I422_cyuv: planar YUV 4:2:2 to upside-down packed UYVY 4:2:2
435  *****************************************************************************/
436 static void I422_cyuv( filter_t *p_filter, picture_t *p_source,
437                                            picture_t *p_dest )
438 {
439     uint8_t *p_line = p_dest->p->p_pixels + p_dest->p->i_visible_lines * p_dest->p->i_pitch;
440     uint8_t *p_y = p_source->Y_PIXELS;
441     uint8_t *p_u = p_source->U_PIXELS;
442     uint8_t *p_v = p_source->V_PIXELS;
443
444     int i_x, i_y;
445
446     const int i_source_margin = p_source->p[0].i_pitch
447                                  - p_source->p[0].i_visible_pitch;
448     const int i_source_margin_c = p_source->p[1].i_pitch
449                                  - p_source->p[1].i_visible_pitch;
450     const int i_dest_margin = p_dest->p->i_pitch
451                                - p_dest->p->i_visible_pitch;
452
453 #if defined (MODULE_NAME_IS_i422_yuy2_sse2)
454
455     if( 0 == (15 & (p_source->p[Y_PLANE].i_pitch|p_dest->p->i_pitch|
456         ((intptr_t)p_line|(intptr_t)p_y))) )
457     {
458         /* use faster SSE2 aligned fetch and store */
459         for( i_y = p_filter->fmt_in.video.i_height ; i_y-- ; )
460         {
461             p_line -= 2 * p_dest->p->i_pitch;
462
463             for( i_x = p_filter->fmt_in.video.i_width / 16 ; i_x-- ; )
464             {
465                 SSE2_CALL( SSE2_YUV422_UYVY_ALIGNED );
466             }
467             for( i_x = ( p_filter->fmt_in.video.i_width % 16 ) / 2; i_x-- ; )
468             {
469                 C_YUV422_UYVY( p_line, p_y, p_u, p_v );
470             }
471             p_y += i_source_margin;
472             p_u += i_source_margin_c;
473             p_v += i_source_margin_c;
474             p_line += i_dest_margin;
475         }
476     }
477     else {
478         /* use slower SSE2 unaligned fetch and store */
479         for( i_y = p_filter->fmt_in.video.i_height ; i_y-- ; )
480         {
481             p_line -= 2 * p_dest->p->i_pitch;
482
483             for( i_x = p_filter->fmt_in.video.i_width / 16 ; i_x-- ; )
484             {
485                 SSE2_CALL( SSE2_YUV422_UYVY_UNALIGNED );
486             }
487             for( i_x = ( p_filter->fmt_in.video.i_width % 16 ) / 2; i_x-- ; )
488             {
489                 C_YUV422_UYVY( p_line, p_y, p_u, p_v );
490             }
491             p_y += i_source_margin;
492             p_u += i_source_margin_c;
493             p_v += i_source_margin_c;
494             p_line += i_dest_margin;
495         }
496     }
497     SSE2_END;
498
499 #else
500
501     for( i_y = p_filter->fmt_in.video.i_height ; i_y-- ; )
502     {
503         for( i_x = p_filter->fmt_in.video.i_width / 8 ; i_x-- ; )
504         {
505             p_line -= 2 * p_dest->p->i_pitch;
506
507 #if defined (MODULE_NAME_IS_i422_yuy2)
508             C_YUV422_UYVY( p_line, p_y, p_u, p_v );
509             C_YUV422_UYVY( p_line, p_y, p_u, p_v );
510             C_YUV422_UYVY( p_line, p_y, p_u, p_v );
511             C_YUV422_UYVY( p_line, p_y, p_u, p_v );
512 #elif defined (MODULE_NAME_IS_i422_yuy2_mmx)
513             MMX_CALL( MMX_YUV422_UYVY );
514 #endif
515         }
516         p_y += i_source_margin;
517         p_u += i_source_margin_c;
518         p_v += i_source_margin_c;
519         p_line += i_dest_margin;
520     }
521 #if defined (MODULE_NAME_IS_i422_yuy2_mmx)
522     MMX_END;
523 #elif defined (MODULE_NAME_IS_i422_yuy2_sse2)
524     SSE2_END;
525 #endif
526
527 #endif
528 }
529
530 /*****************************************************************************
531  * I422_Y211: planar YUV 4:2:2 to packed YUYV 2:1:1
532  *****************************************************************************/
533 #if defined (MODULE_NAME_IS_i422_yuy2)
534 static void I422_Y211( filter_t *p_filter, picture_t *p_source,
535                                            picture_t *p_dest )
536 {
537     uint8_t *p_line = p_dest->p->p_pixels + p_dest->p->i_visible_lines * p_dest->p->i_pitch;
538     uint8_t *p_y = p_source->Y_PIXELS;
539     uint8_t *p_u = p_source->U_PIXELS;
540     uint8_t *p_v = p_source->V_PIXELS;
541
542     int i_x, i_y;
543
544     for( i_y = p_filter->fmt_in.video.i_height ; i_y-- ; )
545     {
546         for( i_x = p_filter->fmt_in.video.i_width / 8 ; i_x-- ; )
547         {
548             C_YUV422_Y211( p_line, p_y, p_u, p_v );
549             C_YUV422_Y211( p_line, p_y, p_u, p_v );
550         }
551     }
552 }
553 #endif