1 /*****************************************************************************
2 * csp.c: h264 encoder library
3 *****************************************************************************
4 * Copyright (C) 2004 Laurent Aimar
5 * $Id: csp.c,v 1.1 2004/06/03 19:27:06 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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 *****************************************************************************/
30 static inline void plane_copy( uint8_t *dst, int i_dst,
31 uint8_t *src, int i_src, int w, int h)
35 memcpy( dst, src, w );
40 static inline void plane_copy_vflip( uint8_t *dst, int i_dst,
41 uint8_t *src, int i_src, int w, int h)
43 plane_copy( dst, i_dst, src + (h -1)*i_src, -i_src, w, h );
46 static inline void plane_subsamplev2( uint8_t *dst, int i_dst,
47 uint8_t *src, int i_src, int w, int h)
54 for( i = 0; i < w; i++ )
56 *d++ = ( s[0] + s[i_src] + 1 ) >> 1;
64 static inline void plane_subsamplev2_vlip( uint8_t *dst, int i_dst,
65 uint8_t *src, int i_src, int w, int h)
67 plane_subsamplev2( dst, i_dst, src + (2*h-1)*i_src, -i_src, w, h );
70 static inline void plane_subsamplehv2( uint8_t *dst, int i_dst,
71 uint8_t *src, int i_src, int w, int h)
78 for( i = 0; i < w; i++ )
80 *d++ = ( s[0] + s[1] + s[i_src] + s[i_src+1] + 1 ) >> 2;
88 static inline void plane_subsamplehv2_vlip( uint8_t *dst, int i_dst,
89 uint8_t *src, int i_src, int w, int h)
91 plane_subsamplehv2( dst, i_dst, src + (2*h-1)*i_src, -i_src, w, h );
94 static void i420_to_i420( x264_frame_t *frm, x264_image_t *img,
95 int i_width, int i_height )
97 if( img->i_csp & X264_CSP_VFLIP )
99 plane_copy_vflip( frm->plane[0], frm->i_stride[0],
100 img->plane[0], img->i_stride[0],
102 plane_copy_vflip( frm->plane[1], frm->i_stride[1],
103 img->plane[1], img->i_stride[1],
104 i_width / 2, i_height / 2 );
105 plane_copy_vflip( frm->plane[2], frm->i_stride[2],
106 img->plane[2], img->i_stride[2],
107 i_width / 2, i_height / 2 );
111 plane_copy( frm->plane[0], frm->i_stride[0],
112 img->plane[0], img->i_stride[0],
114 plane_copy( frm->plane[1], frm->i_stride[1],
115 img->plane[1], img->i_stride[1],
116 i_width / 2, i_height / 2 );
117 plane_copy( frm->plane[2], frm->i_stride[2],
118 img->plane[2], img->i_stride[2],
119 i_width / 2, i_height / 2 );
123 static void yv12_to_i420( x264_frame_t *frm, x264_image_t *img,
124 int i_width, int i_height )
126 if( img->i_csp & X264_CSP_VFLIP )
128 plane_copy_vflip( frm->plane[0], frm->i_stride[0],
129 img->plane[0], img->i_stride[0],
131 plane_copy_vflip( frm->plane[2], frm->i_stride[2],
132 img->plane[1], img->i_stride[1],
133 i_width / 2, i_height / 2 );
134 plane_copy_vflip( frm->plane[1], frm->i_stride[1],
135 img->plane[2], img->i_stride[2],
136 i_width / 2, i_height / 2 );
140 plane_copy( frm->plane[0], frm->i_stride[0],
141 img->plane[0], img->i_stride[0],
143 plane_copy( frm->plane[2], frm->i_stride[2],
144 img->plane[1], img->i_stride[1],
145 i_width / 2, i_height / 2 );
146 plane_copy( frm->plane[1], frm->i_stride[1],
147 img->plane[2], img->i_stride[2],
148 i_width / 2, i_height / 2 );
152 static void i422_to_i420( x264_frame_t *frm, x264_image_t *img,
153 int i_width, int i_height )
155 if( img->i_csp & X264_CSP_VFLIP )
157 plane_copy_vflip( frm->plane[0], frm->i_stride[0],
158 img->plane[0], img->i_stride[0],
161 plane_subsamplev2_vlip( frm->plane[1], frm->i_stride[1],
162 img->plane[1], img->i_stride[1],
163 i_width / 2, i_height / 2 );
164 plane_subsamplev2_vlip( frm->plane[2], frm->i_stride[2],
165 img->plane[2], img->i_stride[2],
166 i_width / 2, i_height / 2 );
170 plane_copy( frm->plane[0], frm->i_stride[0],
171 img->plane[0], img->i_stride[0],
174 plane_subsamplev2( frm->plane[1], frm->i_stride[1],
175 img->plane[1], img->i_stride[1],
176 i_width / 2, i_height / 2 );
177 plane_subsamplev2( frm->plane[2], frm->i_stride[2],
178 img->plane[2], img->i_stride[2],
179 i_width / 2, i_height / 2 );
183 static void i444_to_i420( x264_frame_t *frm, x264_image_t *img,
184 int i_width, int i_height )
186 if( img->i_csp & X264_CSP_VFLIP )
188 plane_copy_vflip( frm->plane[0], frm->i_stride[0],
189 img->plane[0], img->i_stride[0],
192 plane_subsamplehv2_vlip( frm->plane[1], frm->i_stride[1],
193 img->plane[1], img->i_stride[1],
194 i_width / 2, i_height / 2 );
195 plane_subsamplehv2_vlip( frm->plane[2], frm->i_stride[2],
196 img->plane[2], img->i_stride[2],
197 i_width / 2, i_height / 2 );
201 plane_copy( frm->plane[0], frm->i_stride[0],
202 img->plane[0], img->i_stride[0],
205 plane_subsamplehv2( frm->plane[1], frm->i_stride[1],
206 img->plane[1], img->i_stride[1],
207 i_width / 2, i_height / 2 );
208 plane_subsamplehv2( frm->plane[2], frm->i_stride[2],
209 img->plane[2], img->i_stride[2],
210 i_width / 2, i_height / 2 );
213 static void yuyv_to_i420( x264_frame_t *frm, x264_image_t *img,
214 int i_width, int i_height )
216 uint8_t *src = img->plane[0];
217 int i_src= img->i_stride[0];
219 uint8_t *y = frm->plane[0];
220 uint8_t *u = frm->plane[1];
221 uint8_t *v = frm->plane[2];
223 if( img->i_csp & X264_CSP_VFLIP )
225 src += ( i_height - 1 ) * i_src;
229 for( ; i_height > 0; i_height -= 2 )
237 for( w = i_width; w > 0; w -= 2 )
242 *uu++ = ( ss[1] + ss[1+i_src] + 1 ) >> 1;
243 *vv++ = ( ss[3] + ss[3+i_src] + 1 ) >> 1;
248 y += frm->i_stride[0];
249 u += frm->i_stride[1];
250 v += frm->i_stride[2];
254 for( w = i_width; w > 0; w -= 2 )
261 y += frm->i_stride[0];
265 /* Same value than in XviD */
267 #define FIX(f) ((int)((f) * (1 << BITS) + 0.5))
269 #define Y_R FIX(0.257)
270 #define Y_G FIX(0.504)
271 #define Y_B FIX(0.098)
274 #define U_R FIX(0.148)
275 #define U_G FIX(0.291)
276 #define U_B FIX(0.439)
279 #define V_R FIX(0.439)
280 #define V_G FIX(0.368)
281 #define V_B FIX(0.071)
283 #define RGB_TO_I420( name, POS_R, POS_G, POS_B, S_RGB ) \
284 static void name( x264_frame_t *frm, x264_image_t *img, \
285 int i_width, int i_height ) \
287 uint8_t *src = img->plane[0]; \
288 int i_src= img->i_stride[0]; \
289 int i_y = frm->i_stride[0]; \
290 uint8_t *y = frm->plane[0]; \
291 uint8_t *u = frm->plane[1]; \
292 uint8_t *v = frm->plane[2]; \
294 if( img->i_csp & X264_CSP_VFLIP ) \
296 src += ( i_height - 1 ) * i_src; \
300 for( ; i_height > 0; i_height -= 2 ) \
308 for( w = i_width; w > 0; w -= 2 ) \
310 int cr = 0,cg = 0,cb = 0; \
314 cr = r = ss[POS_R]; \
315 cg = g = ss[POS_G]; \
316 cb = b = ss[POS_B]; \
318 yy[0] = Y_ADD + ((Y_R * r + Y_G * g + Y_B * b) >> BITS); \
320 cr+= r = ss[POS_R+i_src]; \
321 cg+= g = ss[POS_G+i_src]; \
322 cb+= b = ss[POS_B+i_src]; \
323 yy[i_y] = Y_ADD + ((Y_R * r + Y_G * g + Y_B * b) >> BITS); \
327 cr+= r = ss[POS_R]; \
328 cg+= g = ss[POS_G]; \
329 cb+= b = ss[POS_B]; \
331 yy[0] = Y_ADD + ((Y_R * r + Y_G * g + Y_B * b) >> BITS); \
333 cr+= r = ss[POS_R+i_src]; \
334 cg+= g = ss[POS_G+i_src]; \
335 cb+= b = ss[POS_B+i_src]; \
336 yy[i_y] = Y_ADD + ((Y_R * r + Y_G * g + Y_B * b) >> BITS); \
341 *uu++ = (uint8_t)(U_ADD + ((-U_R * cr - U_G * cg + U_B * cb) >> (BITS+2)) ); \
342 *vv++ = (uint8_t)(V_ADD + (( V_R * cr - V_G * cg - V_B * cb) >> (BITS+2)) ); \
346 y += 2*frm->i_stride[0]; \
347 u += frm->i_stride[1]; \
348 v += frm->i_stride[2]; \
352 RGB_TO_I420( rgb_to_i420, 0, 1, 2, 3 );
353 RGB_TO_I420( bgr_to_i420, 2, 1, 0, 3 );
354 RGB_TO_I420( bgra_to_i420, 2, 1, 0, 4 );
356 void x264_csp_init( int cpu, int i_csp, x264_csp_function_t *pf )
361 pf->i420 = i420_to_i420;
362 pf->i422 = i422_to_i420;
363 pf->i444 = i444_to_i420;
364 pf->yv12 = yv12_to_i420;
365 pf->yuyv = yuyv_to_i420;
366 pf->rgb = rgb_to_i420;
367 pf->bgr = bgr_to_i420;
368 pf->bgra = bgra_to_i420;
372 /* For now, can't happen */
373 fprintf( stderr, "arg in x264_csp_init\n" );