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;
57 param->i_maxframes = 0;
58 param->i_level_idc = 40; /* level 4.0 is sufficient for 720x576 with
59 16 reference frames */
61 /* Encoder parameters */
62 param->i_frame_reference = 1;
63 param->i_keyint_max = 250;
64 param->i_keyint_min = 25;
66 param->i_scenecut_threshold = 40;
67 param->b_bframe_adaptive = 1;
68 param->i_bframe_bias = 0;
69 param->b_bframe_pyramid = 0;
71 param->b_deblocking_filter = 1;
72 param->i_deblocking_filter_alphac0 = 0;
73 param->i_deblocking_filter_beta = 0;
76 param->i_cabac_init_idc = -1;
79 param->rc.i_bitrate = 3000;
80 param->rc.i_rc_buffer_size = 0;
81 param->rc.i_rc_init_buffer = 0;
82 param->rc.i_rc_sens = 10;
83 param->rc.i_qp_constant = 26;
84 param->rc.i_qp_min = 0;
85 param->rc.i_qp_max = 51;
86 param->rc.i_qp_step = 4;
87 param->rc.f_ip_factor = 1.4;
88 param->rc.f_pb_factor = 1.3;
90 param->rc.b_stat_write = 0;
91 param->rc.psz_stat_out = "x264_2pass.log";
92 param->rc.b_stat_read = 0;
93 param->rc.psz_stat_in = "x264_2pass.log";
94 param->rc.psz_rc_eq = "blurCplx^(1-qComp)";
95 param->rc.f_qcompress = 0.6;
96 param->rc.f_qblur = 0.5;
97 param->rc.f_complexity_blur = 20;
100 param->pf_log = x264_log_default;
101 param->p_log_private = NULL;
102 param->i_log_level = X264_LOG_INFO;
105 param->analyse.intra = X264_ANALYSE_I4x4;
106 param->analyse.inter = X264_ANALYSE_I4x4 | X264_ANALYSE_PSUB16x16 | X264_ANALYSE_BSUB16x16;
107 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_TEMPORAL;
108 param->analyse.i_subpel_refine = 5;
109 param->analyse.i_mv_range = 512;
110 param->analyse.b_psnr = 1;
113 /****************************************************************************
115 ****************************************************************************/
116 void x264_log( x264_t *h, int i_level, const char *psz_fmt, ... )
118 if( i_level <= h->param.i_log_level )
121 va_start( arg, psz_fmt );
122 h->param.pf_log( h->param.p_log_private, i_level, psz_fmt, arg );
127 static void x264_log_default( void *p_unused, int i_level, const char *psz_fmt, va_list arg )
133 psz_prefix = "error";
135 case X264_LOG_WARNING:
136 psz_prefix = "warning";
142 psz_prefix = "debug";
145 psz_prefix = "unknown";
148 fprintf( stderr, "x264 [%s]: ", psz_prefix );
149 vfprintf( stderr, psz_fmt, arg );
152 /****************************************************************************
153 * x264_picture_alloc:
154 ****************************************************************************/
155 void x264_picture_alloc( x264_picture_t *pic, int i_csp, int i_width, int i_height )
157 pic->i_type = X264_TYPE_AUTO;
159 pic->img.i_csp = i_csp;
160 switch( i_csp & X264_CSP_MASK )
164 pic->img.i_plane = 3;
165 pic->img.plane[0] = x264_malloc( 3 * i_width * i_height / 2 );
166 pic->img.plane[1] = pic->img.plane[0] + i_width * i_height;
167 pic->img.plane[2] = pic->img.plane[1] + i_width * i_height / 4;
168 pic->img.i_stride[0] = i_width;
169 pic->img.i_stride[1] = i_width / 2;
170 pic->img.i_stride[2] = i_width / 2;
174 pic->img.i_plane = 3;
175 pic->img.plane[0] = x264_malloc( 2 * i_width * i_height );
176 pic->img.plane[1] = pic->img.plane[0] + i_width * i_height;
177 pic->img.plane[2] = pic->img.plane[1] + i_width * i_height / 2;
178 pic->img.i_stride[0] = i_width;
179 pic->img.i_stride[1] = i_width / 2;
180 pic->img.i_stride[2] = i_width / 2;
184 pic->img.i_plane = 3;
185 pic->img.plane[0] = x264_malloc( 3 * i_width * i_height );
186 pic->img.plane[1] = pic->img.plane[0] + i_width * i_height;
187 pic->img.plane[2] = pic->img.plane[1] + i_width * i_height;
188 pic->img.i_stride[0] = i_width;
189 pic->img.i_stride[1] = i_width;
190 pic->img.i_stride[2] = i_width;
194 pic->img.i_plane = 1;
195 pic->img.plane[0] = x264_malloc( 2 * i_width * i_height );
196 pic->img.i_stride[0] = 2 * i_width;
201 pic->img.i_plane = 1;
202 pic->img.plane[0] = x264_malloc( 3 * i_width * i_height );
203 pic->img.i_stride[0] = 3 * i_width;
207 pic->img.i_plane = 1;
208 pic->img.plane[0] = x264_malloc( 4 * i_width * i_height );
209 pic->img.i_stride[0] = 4 * i_width;
213 fprintf( stderr, "invalid CSP\n" );
214 pic->img.i_plane = 0;
219 /****************************************************************************
220 * x264_picture_clean:
221 ****************************************************************************/
222 void x264_picture_clean( x264_picture_t *pic )
224 x264_free( pic->img.plane[0] );
226 /* just to be safe */
227 memset( pic, 0, sizeof( x264_picture_t ) );
230 /****************************************************************************
232 ****************************************************************************/
233 int x264_nal_encode( void *p_data, int *pi_data, int b_annexeb, x264_nal_t *nal )
235 uint8_t *dst = p_data;
236 uint8_t *src = nal->p_payload;
237 uint8_t *end = &nal->p_payload[nal->i_payload];
241 /* FIXME this code doesn't check overflow */
245 /* long nal start code (we always use long ones)*/
253 *dst++ = ( 0x00 << 7 ) | ( nal->i_ref_idc << 5 ) | nal->i_type;
257 if( i_count == 2 && *src <= 0x03 )
272 *pi_data = dst - (uint8_t*)p_data;
277 /****************************************************************************
279 ****************************************************************************/
280 int x264_nal_decode( x264_nal_t *nal, void *p_data, int i_data )
282 uint8_t *src = p_data;
283 uint8_t *end = &src[i_data];
284 uint8_t *dst = nal->p_payload;
286 nal->i_type = src[0]&0x1f;
287 nal->i_ref_idc = (src[0] >> 5)&0x03;
293 if( src < end - 3 && src[0] == 0x00 && src[1] == 0x00 && src[2] == 0x03 )
304 nal->i_payload = dst - (uint8_t*)p_data;
310 /****************************************************************************
312 ****************************************************************************/
313 void *x264_malloc( int i_size )
316 /* Mac OS X always returns 16 bytes aligned memory */
317 return malloc( i_size );
318 #elif defined( HAVE_MALLOC_H )
319 return memalign( 16, i_size );
323 buf = (uint8_t *) malloc( i_size + 15 + sizeof( void ** ) +
325 align_buf = buf + 15 + sizeof( void ** ) + sizeof( int );
326 align_buf -= (long) align_buf & 15;
327 *( (void **) ( align_buf - sizeof( void ** ) ) ) = buf;
328 *( (int *) ( align_buf - sizeof( void ** ) - sizeof( int ) ) ) = i_size;
333 /****************************************************************************
335 ****************************************************************************/
336 void x264_free( void *p )
340 #if defined( HAVE_MALLOC_H ) || defined( SYS_MACOSX )
343 free( *( ( ( void **) p ) - 1 ) );
348 /****************************************************************************
350 ****************************************************************************/
351 void *x264_realloc( void *p, int i_size )
354 return realloc( p, i_size );
360 i_old_size = *( (int*) ( (uint8_t*) p ) - sizeof( void ** ) -
363 p_new = x264_malloc( i_size );
364 if( i_old_size > 0 && i_size > 0 )
366 memcpy( p_new, p, ( i_old_size < i_size ) ? i_old_size : i_size );