d3d11: handle VLC_CODEC_D3D11_OPAQUE_10B upload/download
[vlc.git] / modules / video_chroma / yuy2_i420.c
blob20df9cb358028c8f47637420404e8d625ac9929d
1 /*****************************************************************************
2 * yuy2_i420.c : Packed YUV 4:2:2 to Planar YUV conversion module for vlc
3 *****************************************************************************
4 * Copyright (C) 2007 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Antoine Cellerier <dionoea at videolan dot org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_filter.h>
35 #include <vlc_picture.h>
37 #define SRC_FOURCC "YUY2,YUNV,YVYU,UYVY,UYNV,Y422"
38 #define DEST_FOURCC "I420"
40 /*****************************************************************************
41 * Local and extern prototypes.
42 *****************************************************************************/
43 static int Activate ( vlc_object_t * );
45 static void YUY2_I420 ( filter_t *, picture_t *, picture_t * );
46 static void YVYU_I420 ( filter_t *, picture_t *, picture_t * );
47 static void UYVY_I420 ( filter_t *, picture_t *, picture_t * );
49 static picture_t *YUY2_I420_Filter ( filter_t *, picture_t * );
50 static picture_t *YVYU_I420_Filter ( filter_t *, picture_t * );
51 static picture_t *UYVY_I420_Filter ( filter_t *, picture_t * );
53 /*****************************************************************************
54 * Module descriptor
55 *****************************************************************************/
56 vlc_module_begin ()
57 set_description( N_("Conversions from " SRC_FOURCC " to " DEST_FOURCC) )
58 set_capability( "video converter", 80 )
59 set_callbacks( Activate, NULL )
60 vlc_module_end ()
62 /*****************************************************************************
63 * Activate: allocate a chroma function
64 *****************************************************************************
65 * This function allocates and initializes a chroma function
66 *****************************************************************************/
67 static int Activate( vlc_object_t *p_this )
69 filter_t *p_filter = (filter_t *)p_this;
71 if( p_filter->fmt_in.video.i_width & 1
72 || p_filter->fmt_in.video.i_height & 1 )
74 return -1;
77 if( p_filter->fmt_in.video.i_width != (p_filter->fmt_out.video.i_x_offset + p_filter->fmt_out.video.i_visible_width)
78 || p_filter->fmt_in.video.i_height != (p_filter->fmt_out.video.i_y_offset + p_filter->fmt_out.video.i_visible_height)
79 || p_filter->fmt_in.video.orientation != p_filter->fmt_out.video.orientation)
80 return -1;
82 switch( p_filter->fmt_out.video.i_chroma )
84 case VLC_CODEC_I420:
85 switch( p_filter->fmt_in.video.i_chroma )
87 case VLC_CODEC_YUYV:
88 p_filter->pf_video_filter = YUY2_I420_Filter;
89 break;
91 case VLC_CODEC_YVYU:
92 p_filter->pf_video_filter = YVYU_I420_Filter;
93 break;
95 case VLC_CODEC_UYVY:
96 p_filter->pf_video_filter = UYVY_I420_Filter;
97 break;
99 default:
100 return -1;
102 break;
104 default:
105 return -1;
107 return 0;
110 /* Following functions are local */
111 VIDEO_FILTER_WRAPPER( YUY2_I420 )
112 VIDEO_FILTER_WRAPPER( YVYU_I420 )
113 VIDEO_FILTER_WRAPPER( UYVY_I420 )
115 /*****************************************************************************
116 * YUY2_I420: packed YUY2 4:2:2 to planar YUV 4:2:0
117 *****************************************************************************/
118 static void YUY2_I420( filter_t *p_filter, picture_t *p_source,
119 picture_t *p_dest )
121 uint8_t *p_line = p_source->p->p_pixels;
123 uint8_t *p_y = p_dest->Y_PIXELS;
124 uint8_t *p_u = p_dest->U_PIXELS;
125 uint8_t *p_v = p_dest->V_PIXELS;
127 int i_x, i_y;
129 const int i_dest_margin = p_dest->p[0].i_pitch
130 - p_dest->p[0].i_visible_pitch
131 - p_filter->fmt_out.video.i_x_offset;
132 const int i_dest_margin_c = p_dest->p[1].i_pitch
133 - p_dest->p[1].i_visible_pitch
134 - ( p_filter->fmt_out.video.i_x_offset / 2 );
135 const int i_source_margin = p_source->p->i_pitch
136 - p_source->p->i_visible_pitch
137 - ( p_filter->fmt_in.video.i_x_offset * 2 );
139 bool b_skip = false;
141 for( i_y = (p_filter->fmt_out.video.i_y_offset + p_filter->fmt_out.video.i_visible_height) ; i_y-- ; )
143 if( b_skip )
145 for( i_x = (p_filter->fmt_out.video.i_x_offset + p_filter->fmt_out.video.i_visible_width) / 8 ; i_x-- ; )
147 #define C_YUYV_YUV422_skip( p_line, p_y, p_u, p_v ) \
148 *p_y++ = *p_line++; p_line++; \
149 *p_y++ = *p_line++; p_line++
150 C_YUYV_YUV422_skip( p_line, p_y, p_u, p_v );
151 C_YUYV_YUV422_skip( p_line, p_y, p_u, p_v );
152 C_YUYV_YUV422_skip( p_line, p_y, p_u, p_v );
153 C_YUYV_YUV422_skip( p_line, p_y, p_u, p_v );
155 for( i_x = ( (p_filter->fmt_out.video.i_x_offset + p_filter->fmt_out.video.i_visible_width) % 8 ) / 2; i_x-- ; )
157 C_YUYV_YUV422_skip( p_line, p_y, p_u, p_v );
160 else
162 for( i_x = (p_filter->fmt_out.video.i_x_offset + p_filter->fmt_out.video.i_visible_width) / 8 ; i_x-- ; )
164 #define C_YUYV_YUV422( p_line, p_y, p_u, p_v ) \
165 *p_y++ = *p_line++; *p_u++ = *p_line++; \
166 *p_y++ = *p_line++; *p_v++ = *p_line++
167 C_YUYV_YUV422( p_line, p_y, p_u, p_v );
168 C_YUYV_YUV422( p_line, p_y, p_u, p_v );
169 C_YUYV_YUV422( p_line, p_y, p_u, p_v );
170 C_YUYV_YUV422( p_line, p_y, p_u, p_v );
172 for( i_x = ( (p_filter->fmt_out.video.i_x_offset + p_filter->fmt_out.video.i_visible_width) % 8 ) / 2; i_x-- ; )
174 C_YUYV_YUV422( p_line, p_y, p_u, p_v );
176 p_u += i_dest_margin_c;
177 p_v += i_dest_margin_c;
179 p_line += i_source_margin;
180 p_y += i_dest_margin;
182 b_skip = !b_skip;
186 /*****************************************************************************
187 * YVYU_I420: packed YVYU 4:2:2 to planar YUV 4:2:0
188 *****************************************************************************/
189 static void YVYU_I420( filter_t *p_filter, picture_t *p_source,
190 picture_t *p_dest )
192 uint8_t *p_line = p_source->p->p_pixels;
194 uint8_t *p_y = p_dest->Y_PIXELS;
195 uint8_t *p_u = p_dest->U_PIXELS;
196 uint8_t *p_v = p_dest->V_PIXELS;
198 int i_x, i_y;
200 const int i_dest_margin = p_dest->p[0].i_pitch
201 - p_dest->p[0].i_visible_pitch
202 - p_filter->fmt_out.video.i_x_offset;
203 const int i_dest_margin_c = p_dest->p[1].i_pitch
204 - p_dest->p[1].i_visible_pitch
205 - ( p_filter->fmt_out.video.i_x_offset / 2 );
206 const int i_source_margin = p_source->p->i_pitch
207 - p_source->p->i_visible_pitch
208 - ( p_filter->fmt_in.video.i_x_offset * 2 );
210 bool b_skip = false;
212 for( i_y = (p_filter->fmt_out.video.i_y_offset + p_filter->fmt_out.video.i_visible_height) ; i_y-- ; )
214 if( b_skip )
216 for( i_x = (p_filter->fmt_out.video.i_x_offset + p_filter->fmt_out.video.i_visible_width) / 8 ; i_x-- ; )
218 #define C_YVYU_YUV422_skip( p_line, p_y, p_u, p_v ) \
219 *p_y++ = *p_line++; p_line++; \
220 *p_y++ = *p_line++; p_line++
221 C_YVYU_YUV422_skip( p_line, p_y, p_u, p_v );
222 C_YVYU_YUV422_skip( p_line, p_y, p_u, p_v );
223 C_YVYU_YUV422_skip( p_line, p_y, p_u, p_v );
224 C_YVYU_YUV422_skip( p_line, p_y, p_u, p_v );
226 for( i_x = ( (p_filter->fmt_out.video.i_x_offset + p_filter->fmt_out.video.i_visible_width) % 8 ) / 2; i_x-- ; )
228 C_YVYU_YUV422_skip( p_line, p_y, p_u, p_v );
231 else
233 for( i_x = (p_filter->fmt_out.video.i_x_offset + p_filter->fmt_out.video.i_visible_width) / 8 ; i_x-- ; )
235 #define C_YVYU_YUV422( p_line, p_y, p_u, p_v ) \
236 *p_y++ = *p_line++; *p_v++ = *p_line++; \
237 *p_y++ = *p_line++; *p_u++ = *p_line++
238 C_YVYU_YUV422( p_line, p_y, p_u, p_v );
239 C_YVYU_YUV422( p_line, p_y, p_u, p_v );
240 C_YVYU_YUV422( p_line, p_y, p_u, p_v );
241 C_YVYU_YUV422( p_line, p_y, p_u, p_v );
243 for( i_x = ( (p_filter->fmt_out.video.i_x_offset + p_filter->fmt_out.video.i_visible_width) % 8 ) / 2; i_x-- ; )
245 C_YVYU_YUV422( p_line, p_y, p_u, p_v );
247 p_u += i_dest_margin_c;
248 p_v += i_dest_margin_c;
250 p_line += i_source_margin;
251 p_y += i_dest_margin;
253 b_skip = !b_skip;
257 /*****************************************************************************
258 * UYVY_I420: packed UYVY 4:2:2 to planar YUV 4:2:0
259 *****************************************************************************/
260 static void UYVY_I420( filter_t *p_filter, picture_t *p_source,
261 picture_t *p_dest )
263 uint8_t *p_line = p_source->p->p_pixels;
265 uint8_t *p_y = p_dest->Y_PIXELS;
266 uint8_t *p_u = p_dest->U_PIXELS;
267 uint8_t *p_v = p_dest->V_PIXELS;
269 int i_x, i_y;
271 const int i_dest_margin = p_dest->p[0].i_pitch
272 - p_dest->p[0].i_visible_pitch
273 - p_filter->fmt_out.video.i_x_offset;
274 const int i_dest_margin_c = p_dest->p[1].i_pitch
275 - p_dest->p[1].i_visible_pitch
276 - ( p_filter->fmt_out.video.i_x_offset / 2 );
277 const int i_source_margin = p_source->p->i_pitch
278 - p_source->p->i_visible_pitch
279 - ( p_filter->fmt_in.video.i_x_offset * 2 );
281 bool b_skip = false;
283 for( i_y = (p_filter->fmt_out.video.i_y_offset + p_filter->fmt_out.video.i_visible_height) ; i_y-- ; )
285 if( b_skip )
287 for( i_x = (p_filter->fmt_out.video.i_x_offset + p_filter->fmt_out.video.i_visible_width) / 8 ; i_x-- ; )
289 #define C_UYVY_YUV422_skip( p_line, p_y, p_u, p_v ) \
290 *p_u++ = *p_line++; p_line++; \
291 *p_v++ = *p_line++; p_line++
292 C_UYVY_YUV422_skip( p_line, p_y, p_u, p_v );
293 C_UYVY_YUV422_skip( p_line, p_y, p_u, p_v );
294 C_UYVY_YUV422_skip( p_line, p_y, p_u, p_v );
295 C_UYVY_YUV422_skip( p_line, p_y, p_u, p_v );
297 for( i_x = ( (p_filter->fmt_out.video.i_x_offset + p_filter->fmt_out.video.i_visible_width) % 8 ) / 2; i_x-- ; )
299 C_UYVY_YUV422_skip( p_line, p_y, p_u, p_v );
302 else
304 for( i_x = (p_filter->fmt_out.video.i_x_offset + p_filter->fmt_out.video.i_visible_width) / 8 ; i_x-- ; )
306 #define C_UYVY_YUV422( p_line, p_y, p_u, p_v ) \
307 *p_u++ = *p_line++; *p_y++ = *p_line++; \
308 *p_v++ = *p_line++; *p_y++ = *p_line++
309 C_UYVY_YUV422( p_line, p_y, p_u, p_v );
310 C_UYVY_YUV422( p_line, p_y, p_u, p_v );
311 C_UYVY_YUV422( p_line, p_y, p_u, p_v );
312 C_UYVY_YUV422( p_line, p_y, p_u, p_v );
314 for( i_x = ( (p_filter->fmt_out.video.i_x_offset + p_filter->fmt_out.video.i_visible_width) % 8 ) / 2; i_x-- ; )
316 C_UYVY_YUV422( p_line, p_y, p_u, p_v );
318 p_y += i_dest_margin;
320 p_line += i_source_margin;
321 p_u += i_dest_margin_c;
322 p_v += i_dest_margin_c;
324 b_skip = !b_skip;