]> git.sesse.net Git - vlc/blob - modules/video_chroma/i422_yuy2.c
shm: fix capture with non page-size frame size (maybe fixes #7579)
[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_cpu.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 static picture_t *I422_YUY2_Filter  ( filter_t *, picture_t * );
58 static picture_t *I422_YVYU_Filter  ( filter_t *, picture_t * );
59 static picture_t *I422_UYVY_Filter  ( filter_t *, picture_t * );
60 static picture_t *I422_IUYV_Filter  ( filter_t *, picture_t * );
61 static picture_t *I422_cyuv_Filter  ( filter_t *, picture_t * );
62 #if defined (MODULE_NAME_IS_i422_yuy2)
63 static void I422_Y211               ( filter_t *, picture_t *, picture_t * );
64 static picture_t *I422_Y211_Filter  ( filter_t *, picture_t * );
65 #endif
66
67 /*****************************************************************************
68  * Module descriptor
69  *****************************************************************************/
70 vlc_module_begin ()
71 #if defined (MODULE_NAME_IS_i422_yuy2)
72     set_description( N_("Conversions from " SRC_FOURCC " to " DEST_FOURCC) )
73     set_capability( "video filter2", 80 )
74 # define vlc_CPU_capable() (true)
75 # define VLC_TARGET
76 #elif defined (MODULE_NAME_IS_i422_yuy2_mmx)
77     set_description( N_("MMX conversions from " SRC_FOURCC " to " DEST_FOURCC) )
78     set_capability( "video filter2", 100 )
79 # define vlc_CPU_capable() vlc_CPU_MMX()
80 # define VLC_TARGET VLC_MMX
81 #elif defined (MODULE_NAME_IS_i422_yuy2_sse2)
82     set_description( N_("SSE2 conversions from " SRC_FOURCC " to " DEST_FOURCC) )
83     set_capability( "video filter2", 120 )
84 # define vlc_CPU_capable() vlc_CPU_SSE2()
85 # define VLC_TARGET VLC_SSE
86 #endif
87     set_callbacks( Activate, NULL )
88 vlc_module_end ()
89
90 /*****************************************************************************
91  * Activate: allocate a chroma function
92  *****************************************************************************
93  * This function allocates and initializes a chroma function
94  *****************************************************************************/
95 static int Activate( vlc_object_t *p_this )
96 {
97     filter_t *p_filter = (filter_t *)p_this;
98
99     if( !vlc_CPU_capable() )
100         return VLC_EGENERIC;
101     if( p_filter->fmt_in.video.i_width & 1
102      || p_filter->fmt_in.video.i_height & 1 )
103     {
104         return -1;
105     }
106
107     switch( p_filter->fmt_in.video.i_chroma )
108     {
109         case VLC_CODEC_I422:
110             switch( p_filter->fmt_out.video.i_chroma )
111             {
112                 case VLC_CODEC_YUYV:
113                     p_filter->pf_video_filter = I422_YUY2_Filter;
114                     break;
115
116                 case VLC_CODEC_YVYU:
117                     p_filter->pf_video_filter = I422_YVYU_Filter;
118                     break;
119
120                 case VLC_CODEC_UYVY:
121                     p_filter->pf_video_filter = I422_UYVY_Filter;
122                     break;
123
124                 case VLC_FOURCC('I','U','Y','V'):
125                     p_filter->pf_video_filter = I422_IUYV_Filter;
126                     break;
127
128                 case VLC_CODEC_CYUV:
129                     p_filter->pf_video_filter = I422_cyuv_Filter;
130                     break;
131
132 #if defined (MODULE_NAME_IS_i422_yuy2)
133                 case VLC_CODEC_Y211:
134                     p_filter->pf_video_filter = I422_Y211_Filter;
135                     break;
136 #endif
137
138                 default:
139                     return -1;
140             }
141             break;
142
143         default:
144             return -1;
145     }
146     return 0;
147 }
148
149 /* Following functions are local */
150
151 VIDEO_FILTER_WRAPPER( I422_YUY2 )
152 VIDEO_FILTER_WRAPPER( I422_YVYU )
153 VIDEO_FILTER_WRAPPER( I422_UYVY )
154 VIDEO_FILTER_WRAPPER( I422_IUYV )
155 VIDEO_FILTER_WRAPPER( I422_cyuv )
156 #if defined (MODULE_NAME_IS_i422_yuy2)
157 VIDEO_FILTER_WRAPPER( I422_Y211 )
158 #endif
159
160 /*****************************************************************************
161  * I422_YUY2: planar YUV 4:2:2 to packed YUY2 4:2:2
162  *****************************************************************************/
163 VLC_TARGET
164 static void I422_YUY2( filter_t *p_filter, picture_t *p_source,
165                                            picture_t *p_dest )
166 {
167     uint8_t *p_line = p_dest->p->p_pixels;
168     uint8_t *p_y = p_source->Y_PIXELS;
169     uint8_t *p_u = p_source->U_PIXELS;
170     uint8_t *p_v = p_source->V_PIXELS;
171
172     int i_x, i_y;
173
174     const int i_source_margin = p_source->p[0].i_pitch
175                                  - p_source->p[0].i_visible_pitch;
176     const int i_source_margin_c = p_source->p[1].i_pitch
177                                  - p_source->p[1].i_visible_pitch;
178     const int i_dest_margin = p_dest->p->i_pitch
179                                - p_dest->p->i_visible_pitch;
180
181 #if defined (MODULE_NAME_IS_i422_yuy2_sse2)
182
183     if( 0 == (15 & (p_source->p[Y_PLANE].i_pitch|p_dest->p->i_pitch|
184         ((intptr_t)p_line|(intptr_t)p_y))) )
185     {
186         /* use faster SSE2 aligned 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_ALIGNED );
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     else {
204         /* use slower SSE2 unaligned fetch and store */
205         for( i_y = p_filter->fmt_in.video.i_height ; i_y-- ; )
206         {
207             for( i_x = p_filter->fmt_in.video.i_width / 16 ; i_x-- ; )
208             {
209                 SSE2_CALL( SSE2_YUV422_YUYV_UNALIGNED );
210             }
211             for( i_x = ( p_filter->fmt_in.video.i_width % 16 ) / 2; i_x-- ; )
212             {
213                 C_YUV422_YUYV( p_line, p_y, p_u, p_v );
214             }
215             p_y += i_source_margin;
216             p_u += i_source_margin_c;
217             p_v += i_source_margin_c;
218             p_line += i_dest_margin;
219         }
220     }
221     SSE2_END;
222
223 #else
224
225     for( i_y = p_filter->fmt_in.video.i_height ; i_y-- ; )
226     {
227         for( i_x = p_filter->fmt_in.video.i_width / 8 ; i_x-- ; )
228         {
229 #if defined (MODULE_NAME_IS_i422_yuy2)
230             C_YUV422_YUYV( p_line, p_y, p_u, p_v );
231             C_YUV422_YUYV( p_line, p_y, p_u, p_v );
232             C_YUV422_YUYV( p_line, p_y, p_u, p_v );
233             C_YUV422_YUYV( p_line, p_y, p_u, p_v );
234 #elif defined (MODULE_NAME_IS_i422_yuy2_mmx)
235             MMX_CALL( MMX_YUV422_YUYV );
236 #endif
237         }
238         for( i_x = ( p_filter->fmt_in.video.i_width % 8 ) / 2; i_x-- ; )
239         {
240             C_YUV422_YUYV( p_line, p_y, p_u, p_v );
241         }
242         p_y += i_source_margin;
243         p_u += i_source_margin_c;
244         p_v += i_source_margin_c;
245         p_line += i_dest_margin;
246     }
247 #if defined (MODULE_NAME_IS_i422_yuy2_mmx)
248     MMX_END;
249 #endif
250
251 #endif
252 }
253
254 /*****************************************************************************
255  * I422_YVYU: planar YUV 4:2:2 to packed YVYU 4:2:2
256  *****************************************************************************/
257 VLC_TARGET
258 static void I422_YVYU( filter_t *p_filter, picture_t *p_source,
259                                            picture_t *p_dest )
260 {
261     uint8_t *p_line = p_dest->p->p_pixels;
262     uint8_t *p_y = p_source->Y_PIXELS;
263     uint8_t *p_u = p_source->U_PIXELS;
264     uint8_t *p_v = p_source->V_PIXELS;
265
266     int i_x, i_y;
267
268     const int i_source_margin = p_source->p[0].i_pitch
269                                  - p_source->p[0].i_visible_pitch;
270     const int i_source_margin_c = p_source->p[1].i_pitch
271                                  - p_source->p[1].i_visible_pitch;
272     const int i_dest_margin = p_dest->p->i_pitch
273                                - p_dest->p->i_visible_pitch;
274
275 #if defined (MODULE_NAME_IS_i422_yuy2_sse2)
276
277     if( 0 == (15 & (p_source->p[Y_PLANE].i_pitch|p_dest->p->i_pitch|
278         ((intptr_t)p_line|(intptr_t)p_y))) )
279     {
280         /* use faster SSE2 aligned fetch and store */
281         for( i_y = p_filter->fmt_in.video.i_height ; i_y-- ; )
282         {
283             for( i_x = p_filter->fmt_in.video.i_width / 16 ; i_x-- ; )
284             {
285                 SSE2_CALL( SSE2_YUV422_YVYU_ALIGNED );
286             }
287             for( i_x = ( p_filter->fmt_in.video.i_width % 16 ) / 2; i_x-- ; )
288             {
289                 C_YUV422_YVYU( p_line, p_y, p_u, p_v );
290             }
291             p_y += i_source_margin;
292             p_u += i_source_margin_c;
293             p_v += i_source_margin_c;
294             p_line += i_dest_margin;
295         }
296     }
297     else {
298         /* use slower SSE2 unaligned fetch and store */
299         for( i_y = p_filter->fmt_in.video.i_height ; i_y-- ; )
300         {
301             for( i_x = p_filter->fmt_in.video.i_width / 16 ; i_x-- ; )
302             {
303                 SSE2_CALL( SSE2_YUV422_YVYU_UNALIGNED );
304             }
305             for( i_x = ( p_filter->fmt_in.video.i_width % 16 ) / 2; i_x-- ; )
306             {
307                 C_YUV422_YVYU( p_line, p_y, p_u, p_v );
308             }
309             p_y += i_source_margin;
310             p_u += i_source_margin_c;
311             p_v += i_source_margin_c;
312             p_line += i_dest_margin;
313         }
314     }
315     SSE2_END;
316
317 #else
318
319     for( i_y = p_filter->fmt_in.video.i_height ; i_y-- ; )
320     {
321         for( i_x = p_filter->fmt_in.video.i_width / 8 ; i_x-- ; )
322         {
323 #if defined (MODULE_NAME_IS_i422_yuy2)
324             C_YUV422_YVYU( p_line, p_y, p_u, p_v );
325             C_YUV422_YVYU( p_line, p_y, p_u, p_v );
326             C_YUV422_YVYU( p_line, p_y, p_u, p_v );
327             C_YUV422_YVYU( p_line, p_y, p_u, p_v );
328 #elif defined (MODULE_NAME_IS_i422_yuy2_mmx)
329             MMX_CALL( MMX_YUV422_YVYU );
330 #endif
331         }
332         for( i_x = ( p_filter->fmt_in.video.i_width % 8 ) / 2; i_x-- ; )
333         {
334             C_YUV422_YVYU( p_line, p_y, p_u, p_v );
335         }
336         p_y += i_source_margin;
337         p_u += i_source_margin_c;
338         p_v += i_source_margin_c;
339         p_line += i_dest_margin;
340     }
341 #if defined (MODULE_NAME_IS_i422_yuy2_mmx)
342     MMX_END;
343 #endif
344
345 #endif
346 }
347
348 /*****************************************************************************
349  * I422_UYVY: planar YUV 4:2:2 to packed UYVY 4:2:2
350  *****************************************************************************/
351 VLC_TARGET
352 static void I422_UYVY( filter_t *p_filter, picture_t *p_source,
353                                            picture_t *p_dest )
354 {
355     uint8_t *p_line = p_dest->p->p_pixels;
356     uint8_t *p_y = p_source->Y_PIXELS;
357     uint8_t *p_u = p_source->U_PIXELS;
358     uint8_t *p_v = p_source->V_PIXELS;
359
360     int i_x, i_y;
361
362     const int i_source_margin = p_source->p[0].i_pitch
363                                  - p_source->p[0].i_visible_pitch;
364     const int i_source_margin_c = p_source->p[1].i_pitch
365                                  - p_source->p[1].i_visible_pitch;
366     const int i_dest_margin = p_dest->p->i_pitch
367                                - p_dest->p->i_visible_pitch;
368
369 #if defined (MODULE_NAME_IS_i422_yuy2_sse2)
370
371     if( 0 == (15 & (p_source->p[Y_PLANE].i_pitch|p_dest->p->i_pitch|
372         ((intptr_t)p_line|(intptr_t)p_y))) )
373     {
374         /* use faster SSE2 aligned fetch and store */
375         for( i_y = p_filter->fmt_in.video.i_height ; i_y-- ; )
376         {
377             for( i_x = p_filter->fmt_in.video.i_width / 16 ; i_x-- ; )
378             {
379                 SSE2_CALL( SSE2_YUV422_UYVY_ALIGNED );
380             }
381             for( i_x = ( p_filter->fmt_in.video.i_width % 16 ) / 2; i_x-- ; )
382             {
383                 C_YUV422_UYVY( p_line, p_y, p_u, p_v );
384             }
385             p_y += i_source_margin;
386             p_u += i_source_margin_c;
387             p_v += i_source_margin_c;
388             p_line += i_dest_margin;
389         }
390     }
391     else {
392         /* use slower SSE2 unaligned fetch and store */
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 / 16 ; i_x-- ; )
396             {
397                 SSE2_CALL( SSE2_YUV422_UYVY_UNALIGNED );
398             }
399             for( i_x = ( p_filter->fmt_in.video.i_width % 16 ) / 2; i_x-- ; )
400             {
401                 C_YUV422_UYVY( p_line, p_y, p_u, p_v );
402             }
403             p_y += i_source_margin;
404             p_u += i_source_margin_c;
405             p_v += i_source_margin_c;
406             p_line += i_dest_margin;
407         }
408     }
409     SSE2_END;
410
411 #else
412
413     for( i_y = p_filter->fmt_in.video.i_height ; i_y-- ; )
414     {
415         for( i_x = p_filter->fmt_in.video.i_width / 8 ; i_x-- ; )
416         {
417 #if defined (MODULE_NAME_IS_i422_yuy2)
418             C_YUV422_UYVY( p_line, p_y, p_u, p_v );
419             C_YUV422_UYVY( p_line, p_y, p_u, p_v );
420             C_YUV422_UYVY( p_line, p_y, p_u, p_v );
421             C_YUV422_UYVY( p_line, p_y, p_u, p_v );
422 #elif defined (MODULE_NAME_IS_i422_yuy2_mmx)
423             MMX_CALL( MMX_YUV422_UYVY );
424 #endif
425         }
426         for( i_x = ( p_filter->fmt_in.video.i_width % 8 ) / 2; i_x-- ; )
427         {
428             C_YUV422_UYVY( p_line, p_y, p_u, p_v );
429         }
430         p_y += i_source_margin;
431         p_u += i_source_margin_c;
432         p_v += i_source_margin_c;
433         p_line += i_dest_margin;
434     }
435 #if defined (MODULE_NAME_IS_i422_yuy2_mmx)
436     MMX_END;
437 #endif
438
439 #endif
440 }
441
442 /*****************************************************************************
443  * I422_IUYV: planar YUV 4:2:2 to interleaved packed IUYV 4:2:2
444  *****************************************************************************/
445 static void I422_IUYV( filter_t *p_filter, picture_t *p_source,
446                                            picture_t *p_dest )
447 {
448     VLC_UNUSED(p_source); VLC_UNUSED(p_dest);
449     /* FIXME: TODO ! */
450     msg_Err( p_filter, "I422_IUYV unimplemented, please harass <sam@zoy.org>" );
451 }
452
453 /*****************************************************************************
454  * I422_cyuv: planar YUV 4:2:2 to upside-down packed UYVY 4:2:2
455  *****************************************************************************/
456 VLC_TARGET
457 static void I422_cyuv( filter_t *p_filter, picture_t *p_source,
458                                            picture_t *p_dest )
459 {
460     uint8_t *p_line = p_dest->p->p_pixels + p_dest->p->i_visible_lines * p_dest->p->i_pitch;
461     uint8_t *p_y = p_source->Y_PIXELS;
462     uint8_t *p_u = p_source->U_PIXELS;
463     uint8_t *p_v = p_source->V_PIXELS;
464
465     int i_x, i_y;
466
467     const int i_source_margin = p_source->p[0].i_pitch
468                                  - p_source->p[0].i_visible_pitch;
469     const int i_source_margin_c = p_source->p[1].i_pitch
470                                  - p_source->p[1].i_visible_pitch;
471     const int i_dest_margin = p_dest->p->i_pitch
472                                - p_dest->p->i_visible_pitch;
473
474 #if defined (MODULE_NAME_IS_i422_yuy2_sse2)
475
476     if( 0 == (15 & (p_source->p[Y_PLANE].i_pitch|p_dest->p->i_pitch|
477         ((intptr_t)p_line|(intptr_t)p_y))) )
478     {
479         /* use faster SSE2 aligned fetch and store */
480         for( i_y = p_filter->fmt_in.video.i_height ; i_y-- ; )
481         {
482             p_line -= 2 * p_dest->p->i_pitch;
483
484             for( i_x = p_filter->fmt_in.video.i_width / 16 ; i_x-- ; )
485             {
486                 SSE2_CALL( SSE2_YUV422_UYVY_ALIGNED );
487             }
488             for( i_x = ( p_filter->fmt_in.video.i_width % 16 ) / 2; i_x-- ; )
489             {
490                 C_YUV422_UYVY( p_line, p_y, p_u, p_v );
491             }
492             p_y += i_source_margin;
493             p_u += i_source_margin_c;
494             p_v += i_source_margin_c;
495             p_line += i_dest_margin;
496         }
497     }
498     else {
499         /* use slower SSE2 unaligned fetch and store */
500         for( i_y = p_filter->fmt_in.video.i_height ; i_y-- ; )
501         {
502             p_line -= 2 * p_dest->p->i_pitch;
503
504             for( i_x = p_filter->fmt_in.video.i_width / 16 ; i_x-- ; )
505             {
506                 SSE2_CALL( SSE2_YUV422_UYVY_UNALIGNED );
507             }
508             for( i_x = ( p_filter->fmt_in.video.i_width % 16 ) / 2; i_x-- ; )
509             {
510                 C_YUV422_UYVY( p_line, p_y, p_u, p_v );
511             }
512             p_y += i_source_margin;
513             p_u += i_source_margin_c;
514             p_v += i_source_margin_c;
515             p_line += i_dest_margin;
516         }
517     }
518     SSE2_END;
519
520 #else
521
522     for( i_y = p_filter->fmt_in.video.i_height ; i_y-- ; )
523     {
524         for( i_x = p_filter->fmt_in.video.i_width / 8 ; i_x-- ; )
525         {
526             p_line -= 2 * p_dest->p->i_pitch;
527
528 #if defined (MODULE_NAME_IS_i422_yuy2)
529             C_YUV422_UYVY( p_line, p_y, p_u, p_v );
530             C_YUV422_UYVY( p_line, p_y, p_u, p_v );
531             C_YUV422_UYVY( p_line, p_y, p_u, p_v );
532             C_YUV422_UYVY( p_line, p_y, p_u, p_v );
533 #elif defined (MODULE_NAME_IS_i422_yuy2_mmx)
534             MMX_CALL( MMX_YUV422_UYVY );
535 #endif
536         }
537         p_y += i_source_margin;
538         p_u += i_source_margin_c;
539         p_v += i_source_margin_c;
540         p_line += i_dest_margin;
541     }
542 #if defined (MODULE_NAME_IS_i422_yuy2_mmx)
543     MMX_END;
544 #elif defined (MODULE_NAME_IS_i422_yuy2_sse2)
545     SSE2_END;
546 #endif
547
548 #endif
549 }
550
551 /*****************************************************************************
552  * I422_Y211: planar YUV 4:2:2 to packed YUYV 2:1:1
553  *****************************************************************************/
554 #if defined (MODULE_NAME_IS_i422_yuy2)
555 static void I422_Y211( filter_t *p_filter, picture_t *p_source,
556                                            picture_t *p_dest )
557 {
558     uint8_t *p_line = p_dest->p->p_pixels + p_dest->p->i_visible_lines * p_dest->p->i_pitch;
559     uint8_t *p_y = p_source->Y_PIXELS;
560     uint8_t *p_u = p_source->U_PIXELS;
561     uint8_t *p_v = p_source->V_PIXELS;
562
563     int i_x, i_y;
564
565     for( i_y = p_filter->fmt_in.video.i_height ; i_y-- ; )
566     {
567         for( i_x = p_filter->fmt_in.video.i_width / 8 ; i_x-- ; )
568         {
569             C_YUV422_Y211( p_line, p_y, p_u, p_v );
570             C_YUV422_Y211( p_line, p_y, p_u, p_v );
571         }
572     }
573 }
574 #endif