1 /*****************************************************************************
2 * common.c: h264 library
3 *****************************************************************************
4 * Copyright (C) 2003 Laurent Aimar
5 * $Id: common.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 *****************************************************************************/
36 static void x264_log_default( void *, int, const char *, va_list );
38 /****************************************************************************
40 ****************************************************************************/
41 void x264_param_default( x264_param_t *param )
44 memset( param, 0, sizeof( x264_param_t ) );
47 param->cpu = x264_cpu_detect();
49 /* Video properties */
50 param->i_csp = X264_CSP_I420;
53 param->vui.i_sar_width = 0;
54 param->vui.i_sar_height= 0;
55 param->i_fps_num = 25;
58 /* Encoder parameters */
59 param->i_frame_reference = 1;
60 param->i_idrframe = 2;
64 param->b_deblocking_filter = 1;
65 param->i_deblocking_filter_alphac0 = 0;
66 param->i_deblocking_filter_beta = 0;
69 param->i_cabac_init_idc = -1;
72 param->i_bitrate = 3000;
73 param->i_rc_buffer_size = 0;
74 param->i_rc_init_buffer = 0;
75 param->i_rc_sens = 100;
76 param->i_qp_constant = 26;
80 param->f_ip_factor = 2.0;
81 param->f_pb_factor = 2.0;
84 param->pf_log = x264_log_default;
85 param->p_log_private = NULL;
86 param->i_log_level = X264_LOG_DEBUG;
89 param->analyse.intra = X264_ANALYSE_I4x4;
90 param->analyse.inter = X264_ANALYSE_I4x4 | X264_ANALYSE_PSUB16x16;
91 param->analyse.b_psnr = 1;
94 /****************************************************************************
96 ****************************************************************************/
97 void x264_log( x264_t *h, int i_level, const char *psz_fmt, ... )
99 if( i_level <= h->param.i_log_level )
102 va_start( arg, psz_fmt );
103 h->param.pf_log( h->param.p_log_private, i_level, psz_fmt, arg );
108 static void x264_log_default( void *p_unused, int i_level, const char *psz_fmt, va_list arg )
114 psz_prefix = "error";
116 case X264_LOG_WARNING:
117 psz_prefix = "warning";
123 psz_prefix = "debug";
126 psz_prefix = "unknown";
129 fprintf( stderr, "x264 [%s]: ", psz_prefix );
130 vfprintf( stderr, psz_fmt, arg );
133 /****************************************************************************
134 * x264_picture_alloc:
135 ****************************************************************************/
136 void x264_picture_alloc( x264_picture_t *pic, int i_csp, int i_width, int i_height )
138 pic->i_type = X264_TYPE_AUTO;
140 pic->img.i_csp = i_csp;
141 switch( i_csp & X264_CSP_MASK )
145 pic->img.i_plane = 3;
146 pic->img.plane[0] = x264_malloc( 3 * i_width * i_height / 2 );
147 pic->img.plane[1] = pic->img.plane[0] + i_width * i_height;
148 pic->img.plane[2] = pic->img.plane[1] + i_width * i_height / 4;
149 pic->img.i_stride[0] = i_width;
150 pic->img.i_stride[1] = i_width / 2;
151 pic->img.i_stride[2] = i_width / 2;
155 pic->img.i_plane = 3;
156 pic->img.plane[0] = x264_malloc( 2 * i_width * i_height );
157 pic->img.plane[1] = pic->img.plane[0] + i_width * i_height;
158 pic->img.plane[2] = pic->img.plane[1] + i_width * i_height / 2;
159 pic->img.i_stride[0] = i_width;
160 pic->img.i_stride[1] = i_width / 2;
161 pic->img.i_stride[2] = i_width / 2;
165 pic->img.i_plane = 3;
166 pic->img.plane[0] = x264_malloc( 3 * i_width * i_height );
167 pic->img.plane[1] = pic->img.plane[0] + i_width * i_height;
168 pic->img.plane[2] = pic->img.plane[1] + i_width * i_height;
169 pic->img.i_stride[0] = i_width;
170 pic->img.i_stride[1] = i_width;
171 pic->img.i_stride[2] = i_width;
175 pic->img.i_plane = 1;
176 pic->img.plane[0] = x264_malloc( 2 * i_width * i_height );
177 pic->img.i_stride[0] = 2 * i_width;
182 pic->img.i_plane = 1;
183 pic->img.plane[0] = x264_malloc( 3 * i_width * i_height );
184 pic->img.i_stride[0] = 3 * i_width;
188 pic->img.i_plane = 1;
189 pic->img.plane[0] = x264_malloc( 4 * i_width * i_height );
190 pic->img.i_stride[0] = 4 * i_width;
194 fprintf( stderr, "invalid CSP\n" );
195 pic->img.i_plane = 0;
200 /****************************************************************************
201 * x264_picture_clean:
202 ****************************************************************************/
203 void x264_picture_clean( x264_picture_t *pic )
205 x264_free( pic->img.plane[0] );
207 /* just to be safe */
208 memset( pic, 0, sizeof( x264_picture_t ) );
211 /****************************************************************************
213 ****************************************************************************/
214 int x264_nal_encode( void *p_data, int *pi_data, int b_annexeb, x264_nal_t *nal )
216 uint8_t *dst = p_data;
217 uint8_t *src = nal->p_payload;
218 uint8_t *end = &nal->p_payload[nal->i_payload];
222 /* FIXME this code doesn't check overflow */
226 /* long nal start code (we always use long ones)*/
234 *dst++ = ( 0x00 << 7 ) | ( nal->i_ref_idc << 5 ) | nal->i_type;
238 if( i_count == 2 && *src <= 0x03 )
253 *pi_data = dst - (uint8_t*)p_data;
258 /****************************************************************************
260 ****************************************************************************/
261 int x264_nal_decode( x264_nal_t *nal, void *p_data, int i_data )
263 uint8_t *src = p_data;
264 uint8_t *end = &src[i_data];
265 uint8_t *dst = nal->p_payload;
267 nal->i_type = src[0]&0x1f;
268 nal->i_ref_idc = (src[0] >> 5)&0x03;
274 if( src < end - 3 && src[0] == 0x00 && src[1] == 0x00 && src[2] == 0x03 )
285 nal->i_payload = dst - (uint8_t*)p_data;
291 /****************************************************************************
293 ****************************************************************************/
294 void *x264_malloc( int i_size )
297 return memalign( 16, i_size );
301 buf = (uint8_t *) malloc( i_size + 15 + sizeof( void ** ) +
303 align_buf = buf + 15 + sizeof( void ** ) + sizeof( int );
304 align_buf -= (long) align_buf & 15;
305 *( (void **) ( align_buf - sizeof( void ** ) ) ) = buf;
306 *( (int *) ( align_buf - sizeof( void ** ) - sizeof( int ) ) ) = i_size;
311 /****************************************************************************
313 ****************************************************************************/
314 void x264_free( void *p )
321 free( *( ( ( void **) p ) - 1 ) );
326 /****************************************************************************
328 ****************************************************************************/
329 void *x264_realloc( void *p, int i_size )
332 return realloc( p, i_size );
338 i_old_size = *( (int*) ( (uint8_t*) p ) - sizeof( void ** ) -
341 p_new = x264_malloc( i_size );
342 if( i_old_size > 0 && i_size > 0 )
344 memcpy( p_new, p, ( i_old_size < i_size ) ? i_old_size : i_size );