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;
63 param->i_scenecut_threshold = 40;
65 param->b_deblocking_filter = 1;
66 param->i_deblocking_filter_alphac0 = 0;
67 param->i_deblocking_filter_beta = 0;
70 param->i_cabac_init_idc = -1;
73 param->rc.i_bitrate = 3000;
74 param->rc.i_rc_buffer_size = 0;
75 param->rc.i_rc_init_buffer = 0;
76 param->rc.i_rc_sens = 100;
77 param->rc.i_qp_constant = 26;
78 param->rc.i_qp_min = 0;
79 param->rc.i_qp_max = 51;
80 param->rc.i_qp_step = 4;
81 param->rc.f_ip_factor = 2.0;
82 param->rc.f_pb_factor = 2.0;
84 param->rc.b_stat_write = 0;
85 param->rc.psz_stat_out = "x264_2pass.log";
86 param->rc.b_stat_read = 0;
87 param->rc.psz_stat_in = "x264_2pass.log";
88 param->rc.psz_rc_eq = "(tex^qComp)*(avgTex^(1-qComp))";
89 param->rc.f_qcompress = 0.6;
90 param->rc.f_qblur = 0.5;
93 param->pf_log = x264_log_default;
94 param->p_log_private = NULL;
95 param->i_log_level = X264_LOG_DEBUG;
98 param->analyse.intra = X264_ANALYSE_I4x4;
99 param->analyse.inter = X264_ANALYSE_I4x4 | X264_ANALYSE_PSUB16x16;
100 param->analyse.i_subpel_refine = 1;
101 param->analyse.b_psnr = 1;
104 /****************************************************************************
106 ****************************************************************************/
107 void x264_log( x264_t *h, int i_level, const char *psz_fmt, ... )
109 if( i_level <= h->param.i_log_level )
112 va_start( arg, psz_fmt );
113 h->param.pf_log( h->param.p_log_private, i_level, psz_fmt, arg );
118 static void x264_log_default( void *p_unused, int i_level, const char *psz_fmt, va_list arg )
124 psz_prefix = "error";
126 case X264_LOG_WARNING:
127 psz_prefix = "warning";
133 psz_prefix = "debug";
136 psz_prefix = "unknown";
139 fprintf( stderr, "x264 [%s]: ", psz_prefix );
140 vfprintf( stderr, psz_fmt, arg );
143 /****************************************************************************
144 * x264_picture_alloc:
145 ****************************************************************************/
146 void x264_picture_alloc( x264_picture_t *pic, int i_csp, int i_width, int i_height )
148 pic->i_type = X264_TYPE_AUTO;
150 pic->img.i_csp = i_csp;
151 switch( i_csp & X264_CSP_MASK )
155 pic->img.i_plane = 3;
156 pic->img.plane[0] = x264_malloc( 3 * i_width * i_height / 2 );
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 / 4;
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( 2 * 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 / 2;
169 pic->img.i_stride[0] = i_width;
170 pic->img.i_stride[1] = i_width / 2;
171 pic->img.i_stride[2] = i_width / 2;
175 pic->img.i_plane = 3;
176 pic->img.plane[0] = x264_malloc( 3 * i_width * i_height );
177 pic->img.plane[1] = pic->img.plane[0] + i_width * i_height;
178 pic->img.plane[2] = pic->img.plane[1] + i_width * i_height;
179 pic->img.i_stride[0] = i_width;
180 pic->img.i_stride[1] = i_width;
181 pic->img.i_stride[2] = i_width;
185 pic->img.i_plane = 1;
186 pic->img.plane[0] = x264_malloc( 2 * i_width * i_height );
187 pic->img.i_stride[0] = 2 * i_width;
192 pic->img.i_plane = 1;
193 pic->img.plane[0] = x264_malloc( 3 * i_width * i_height );
194 pic->img.i_stride[0] = 3 * i_width;
198 pic->img.i_plane = 1;
199 pic->img.plane[0] = x264_malloc( 4 * i_width * i_height );
200 pic->img.i_stride[0] = 4 * i_width;
204 fprintf( stderr, "invalid CSP\n" );
205 pic->img.i_plane = 0;
210 /****************************************************************************
211 * x264_picture_clean:
212 ****************************************************************************/
213 void x264_picture_clean( x264_picture_t *pic )
215 x264_free( pic->img.plane[0] );
217 /* just to be safe */
218 memset( pic, 0, sizeof( x264_picture_t ) );
221 /****************************************************************************
223 ****************************************************************************/
224 int x264_nal_encode( void *p_data, int *pi_data, int b_annexeb, x264_nal_t *nal )
226 uint8_t *dst = p_data;
227 uint8_t *src = nal->p_payload;
228 uint8_t *end = &nal->p_payload[nal->i_payload];
232 /* FIXME this code doesn't check overflow */
236 /* long nal start code (we always use long ones)*/
244 *dst++ = ( 0x00 << 7 ) | ( nal->i_ref_idc << 5 ) | nal->i_type;
248 if( i_count == 2 && *src <= 0x03 )
263 *pi_data = dst - (uint8_t*)p_data;
268 /****************************************************************************
270 ****************************************************************************/
271 int x264_nal_decode( x264_nal_t *nal, void *p_data, int i_data )
273 uint8_t *src = p_data;
274 uint8_t *end = &src[i_data];
275 uint8_t *dst = nal->p_payload;
277 nal->i_type = src[0]&0x1f;
278 nal->i_ref_idc = (src[0] >> 5)&0x03;
284 if( src < end - 3 && src[0] == 0x00 && src[1] == 0x00 && src[2] == 0x03 )
295 nal->i_payload = dst - (uint8_t*)p_data;
301 /****************************************************************************
303 ****************************************************************************/
304 void *x264_malloc( int i_size )
307 return memalign( 16, i_size );
311 buf = (uint8_t *) malloc( i_size + 15 + sizeof( void ** ) +
313 align_buf = buf + 15 + sizeof( void ** ) + sizeof( int );
314 align_buf -= (long) align_buf & 15;
315 *( (void **) ( align_buf - sizeof( void ** ) ) ) = buf;
316 *( (int *) ( align_buf - sizeof( void ** ) - sizeof( int ) ) ) = i_size;
321 /****************************************************************************
323 ****************************************************************************/
324 void x264_free( void *p )
331 free( *( ( ( void **) p ) - 1 ) );
336 /****************************************************************************
338 ****************************************************************************/
339 void *x264_realloc( void *p, int i_size )
342 return realloc( p, i_size );
348 i_old_size = *( (int*) ( (uint8_t*) p ) - sizeof( void ** ) -
351 p_new = x264_malloc( i_size );
352 if( i_old_size > 0 && i_size > 0 )
354 memcpy( p_new, p, ( i_old_size < i_size ) ? i_old_size : i_size );