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_INFO;
89 param->analyse.intra = X264_ANALYSE_I4x4;
90 param->analyse.inter = X264_ANALYSE_I4x4 | X264_ANALYSE_PSUB16x16;
93 /****************************************************************************
95 ****************************************************************************/
96 void x264_log( x264_t *h, int i_level, const char *psz_fmt, ... )
98 if( i_level <= h->param.i_log_level )
101 va_start( arg, psz_fmt );
102 h->param.pf_log( h->param.p_log_private, i_level, psz_fmt, arg );
107 static void x264_log_default( void *p_unused, int i_level, const char *psz_fmt, va_list arg )
113 psz_prefix = "error";
115 case X264_LOG_WARNING:
116 psz_prefix = "warning";
122 psz_prefix = "debug";
125 psz_prefix = "unknown";
128 fprintf( stderr, "x264 [%s]: ", psz_prefix );
129 vfprintf( stderr, psz_fmt, arg );
132 /****************************************************************************
133 * x264_picture_alloc:
134 ****************************************************************************/
135 void x264_picture_alloc( x264_picture_t *pic, int i_csp, int i_width, int i_height )
137 pic->i_type = X264_TYPE_AUTO;
139 pic->img.i_csp = i_csp;
140 switch( i_csp & X264_CSP_MASK )
144 pic->img.i_plane = 3;
145 pic->img.plane[0] = x264_malloc( 3 * i_width * i_height / 2 );
146 pic->img.plane[1] = pic->img.plane[0] + i_width * i_height;
147 pic->img.plane[2] = pic->img.plane[1] + i_width * i_height / 4;
148 pic->img.i_stride[0] = i_width;
149 pic->img.i_stride[1] = i_width / 2;
150 pic->img.i_stride[2] = i_width / 2;
154 pic->img.i_plane = 3;
155 pic->img.plane[0] = x264_malloc( 2 * i_width * i_height );
156 pic->img.plane[1] = pic->img.plane[0] + i_width * i_height;
157 pic->img.plane[2] = pic->img.plane[1] + i_width * i_height / 2;
158 pic->img.i_stride[0] = i_width;
159 pic->img.i_stride[1] = i_width / 2;
160 pic->img.i_stride[2] = i_width / 2;
164 pic->img.i_plane = 3;
165 pic->img.plane[0] = x264_malloc( 3 * i_width * i_height );
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;
168 pic->img.i_stride[0] = i_width;
169 pic->img.i_stride[1] = i_width;
170 pic->img.i_stride[2] = i_width;
174 pic->img.i_plane = 1;
175 pic->img.plane[0] = x264_malloc( 2 * i_width * i_height );
176 pic->img.i_stride[0] = 2 * i_width;
181 pic->img.i_plane = 1;
182 pic->img.plane[0] = x264_malloc( 3 * i_width * i_height );
183 pic->img.i_stride[0] = 3 * i_width;
187 pic->img.i_plane = 1;
188 pic->img.plane[0] = x264_malloc( 4 * i_width * i_height );
189 pic->img.i_stride[0] = 4 * i_width;
193 fprintf( stderr, "invalid CSP\n" );
194 pic->img.i_plane = 0;
199 /****************************************************************************
200 * x264_picture_clean:
201 ****************************************************************************/
202 void x264_picture_clean( x264_picture_t *pic )
204 x264_free( pic->img.plane[0] );
206 /* just to be safe */
207 memset( pic, 0, sizeof( x264_picture_t ) );
210 /****************************************************************************
212 ****************************************************************************/
213 int x264_nal_encode( void *p_data, int *pi_data, int b_annexeb, x264_nal_t *nal )
215 uint8_t *dst = p_data;
216 uint8_t *src = nal->p_payload;
217 uint8_t *end = &nal->p_payload[nal->i_payload];
221 /* FIXME this code doesn't check overflow */
225 /* long nal start code (we always use long ones)*/
233 *dst++ = ( 0x00 << 7 ) | ( nal->i_ref_idc << 5 ) | nal->i_type;
237 if( i_count == 2 && *src <= 0x03 )
252 *pi_data = dst - (uint8_t*)p_data;
257 /****************************************************************************
259 ****************************************************************************/
260 int x264_nal_decode( x264_nal_t *nal, void *p_data, int i_data )
262 uint8_t *src = p_data;
263 uint8_t *end = &src[i_data];
264 uint8_t *dst = nal->p_payload;
266 nal->i_type = src[0]&0x1f;
267 nal->i_ref_idc = (src[0] >> 5)&0x03;
273 if( src < end - 3 && src[0] == 0x00 && src[1] == 0x00 && src[2] == 0x03 )
284 nal->i_payload = dst - (uint8_t*)p_data;
290 /****************************************************************************
292 ****************************************************************************/
293 void *x264_malloc( int i_size )
296 return memalign( 16, i_size );
300 buf = (uint8_t *) malloc( i_size + 15 + sizeof( void ** ) +
302 align_buf = buf + 15 + sizeof( void ** ) + sizeof( int );
303 align_buf -= (long) align_buf & 15;
304 *( (void **) ( align_buf - sizeof( void ** ) ) ) = buf;
305 *( (int *) ( align_buf - sizeof( void ** ) - sizeof( int ) ) ) = i_size;
310 /****************************************************************************
312 ****************************************************************************/
313 void x264_free( void *p )
320 free( *( ( ( void **) p ) - 1 ) );
325 /****************************************************************************
327 ****************************************************************************/
328 void *x264_realloc( void *p, int i_size )
331 return realloc( p, i_size );
337 i_old_size = *( (int*) ( (uint8_t*) p ) - sizeof( void ** ) -
340 p_new = x264_malloc( i_size );
341 if( i_old_size > 0 && i_size > 0 )
343 memcpy( p_new, p, ( i_old_size < i_size ) ? i_old_size : i_size );