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();
50 /* Video properties */
51 param->i_csp = X264_CSP_I420;
54 param->vui.i_sar_width = 0;
55 param->vui.i_sar_height= 0;
56 param->vui.i_overscan = 0; /* undef */
57 param->vui.i_vidformat = 5; /* undef */
58 param->vui.b_fullrange = 0; /* off */
59 param->vui.i_colorprim = 2; /* undef */
60 param->vui.i_transfer = 2; /* undef */
61 param->vui.i_colmatrix = 2; /* undef */
62 param->vui.i_chroma_loc= 0; /* left center */
63 param->i_fps_num = 25;
65 param->i_level_idc = 51; /* as close to "unrestricted" as we can get */
67 /* Encoder parameters */
68 param->i_frame_reference = 1;
69 param->i_keyint_max = 250;
70 param->i_keyint_min = 25;
72 param->i_scenecut_threshold = 40;
73 param->b_bframe_adaptive = 1;
74 param->i_bframe_bias = 0;
75 param->b_bframe_pyramid = 0;
77 param->b_deblocking_filter = 1;
78 param->i_deblocking_filter_alphac0 = 0;
79 param->i_deblocking_filter_beta = 0;
82 param->i_cabac_init_idc = 0;
85 param->rc.i_bitrate = 1000;
86 param->rc.f_rate_tolerance = 1.0;
87 param->rc.i_vbv_max_bitrate = 0;
88 param->rc.i_vbv_buffer_size = 0;
89 param->rc.f_vbv_buffer_init = 0.9;
90 param->rc.i_qp_constant = 26;
91 param->rc.i_rf_constant = 0;
92 param->rc.i_qp_min = 10;
93 param->rc.i_qp_max = 51;
94 param->rc.i_qp_step = 4;
95 param->rc.f_ip_factor = 1.4;
96 param->rc.f_pb_factor = 1.3;
98 param->rc.b_stat_write = 0;
99 param->rc.psz_stat_out = "x264_2pass.log";
100 param->rc.b_stat_read = 0;
101 param->rc.psz_stat_in = "x264_2pass.log";
102 param->rc.psz_rc_eq = "blurCplx^(1-qComp)";
103 param->rc.f_qcompress = 0.6;
104 param->rc.f_qblur = 0.5;
105 param->rc.f_complexity_blur = 20;
106 param->rc.i_zones = 0;
109 param->pf_log = x264_log_default;
110 param->p_log_private = NULL;
111 param->i_log_level = X264_LOG_INFO;
114 param->analyse.intra = X264_ANALYSE_I4x4 | X264_ANALYSE_I8x8;
115 param->analyse.inter = X264_ANALYSE_I4x4 | X264_ANALYSE_I8x8
116 | X264_ANALYSE_PSUB16x16 | X264_ANALYSE_BSUB16x16;
117 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_TEMPORAL;
118 param->analyse.i_me_method = X264_ME_HEX;
119 param->analyse.i_me_range = 16;
120 param->analyse.i_subpel_refine = 5;
121 param->analyse.b_chroma_me = 1;
122 param->analyse.i_mv_range = -1; // set from level_idc
123 param->analyse.i_chroma_qp_offset = 0;
124 param->analyse.b_psnr = 1;
126 param->i_cqm_preset = X264_CQM_FLAT;
127 memset( param->cqm_4iy, 16, 16 );
128 memset( param->cqm_4ic, 16, 16 );
129 memset( param->cqm_4py, 16, 16 );
130 memset( param->cqm_4pc, 16, 16 );
131 memset( param->cqm_8iy, 16, 64 );
132 memset( param->cqm_8py, 16, 64 );
137 /****************************************************************************
139 ****************************************************************************/
140 void x264_log( x264_t *h, int i_level, const char *psz_fmt, ... )
142 if( i_level <= h->param.i_log_level )
145 va_start( arg, psz_fmt );
146 h->param.pf_log( h->param.p_log_private, i_level, psz_fmt, arg );
151 static void x264_log_default( void *p_unused, int i_level, const char *psz_fmt, va_list arg )
157 psz_prefix = "error";
159 case X264_LOG_WARNING:
160 psz_prefix = "warning";
166 psz_prefix = "debug";
169 psz_prefix = "unknown";
172 fprintf( stderr, "x264 [%s]: ", psz_prefix );
173 vfprintf( stderr, psz_fmt, arg );
176 /****************************************************************************
177 * x264_picture_alloc:
178 ****************************************************************************/
179 void x264_picture_alloc( x264_picture_t *pic, int i_csp, int i_width, int i_height )
181 pic->i_type = X264_TYPE_AUTO;
183 pic->img.i_csp = i_csp;
184 switch( i_csp & X264_CSP_MASK )
188 pic->img.i_plane = 3;
189 pic->img.plane[0] = x264_malloc( 3 * i_width * i_height / 2 );
190 pic->img.plane[1] = pic->img.plane[0] + i_width * i_height;
191 pic->img.plane[2] = pic->img.plane[1] + i_width * i_height / 4;
192 pic->img.i_stride[0] = i_width;
193 pic->img.i_stride[1] = i_width / 2;
194 pic->img.i_stride[2] = i_width / 2;
198 pic->img.i_plane = 3;
199 pic->img.plane[0] = x264_malloc( 2 * i_width * i_height );
200 pic->img.plane[1] = pic->img.plane[0] + i_width * i_height;
201 pic->img.plane[2] = pic->img.plane[1] + i_width * i_height / 2;
202 pic->img.i_stride[0] = i_width;
203 pic->img.i_stride[1] = i_width / 2;
204 pic->img.i_stride[2] = i_width / 2;
208 pic->img.i_plane = 3;
209 pic->img.plane[0] = x264_malloc( 3 * i_width * i_height );
210 pic->img.plane[1] = pic->img.plane[0] + i_width * i_height;
211 pic->img.plane[2] = pic->img.plane[1] + i_width * i_height;
212 pic->img.i_stride[0] = i_width;
213 pic->img.i_stride[1] = i_width;
214 pic->img.i_stride[2] = i_width;
218 pic->img.i_plane = 1;
219 pic->img.plane[0] = x264_malloc( 2 * i_width * i_height );
220 pic->img.i_stride[0] = 2 * i_width;
225 pic->img.i_plane = 1;
226 pic->img.plane[0] = x264_malloc( 3 * i_width * i_height );
227 pic->img.i_stride[0] = 3 * i_width;
231 pic->img.i_plane = 1;
232 pic->img.plane[0] = x264_malloc( 4 * i_width * i_height );
233 pic->img.i_stride[0] = 4 * i_width;
237 fprintf( stderr, "invalid CSP\n" );
238 pic->img.i_plane = 0;
243 /****************************************************************************
244 * x264_picture_clean:
245 ****************************************************************************/
246 void x264_picture_clean( x264_picture_t *pic )
248 x264_free( pic->img.plane[0] );
250 /* just to be safe */
251 memset( pic, 0, sizeof( x264_picture_t ) );
254 /****************************************************************************
256 ****************************************************************************/
257 int x264_nal_encode( void *p_data, int *pi_data, int b_annexeb, x264_nal_t *nal )
259 uint8_t *dst = p_data;
260 uint8_t *src = nal->p_payload;
261 uint8_t *end = &nal->p_payload[nal->i_payload];
265 /* FIXME this code doesn't check overflow */
269 /* long nal start code (we always use long ones)*/
277 *dst++ = ( 0x00 << 7 ) | ( nal->i_ref_idc << 5 ) | nal->i_type;
281 if( i_count == 2 && *src <= 0x03 )
296 *pi_data = dst - (uint8_t*)p_data;
301 /****************************************************************************
303 ****************************************************************************/
304 int x264_nal_decode( x264_nal_t *nal, void *p_data, int i_data )
306 uint8_t *src = p_data;
307 uint8_t *end = &src[i_data];
308 uint8_t *dst = nal->p_payload;
310 nal->i_type = src[0]&0x1f;
311 nal->i_ref_idc = (src[0] >> 5)&0x03;
317 if( src < end - 3 && src[0] == 0x00 && src[1] == 0x00 && src[2] == 0x03 )
328 nal->i_payload = dst - (uint8_t*)p_data;
334 /****************************************************************************
336 ****************************************************************************/
337 void *x264_malloc( int i_size )
340 /* Mac OS X always returns 16 bytes aligned memory */
341 return malloc( i_size );
342 #elif defined( HAVE_MALLOC_H )
343 return memalign( 16, i_size );
347 buf = (uint8_t *) malloc( i_size + 15 + sizeof( void ** ) +
349 align_buf = buf + 15 + sizeof( void ** ) + sizeof( int );
350 align_buf -= (long) align_buf & 15;
351 *( (void **) ( align_buf - sizeof( void ** ) ) ) = buf;
352 *( (int *) ( align_buf - sizeof( void ** ) - sizeof( int ) ) ) = i_size;
357 /****************************************************************************
359 ****************************************************************************/
360 void x264_free( void *p )
364 #if defined( HAVE_MALLOC_H ) || defined( SYS_MACOSX )
367 free( *( ( ( void **) p ) - 1 ) );
372 /****************************************************************************
374 ****************************************************************************/
375 void *x264_realloc( void *p, int i_size )
378 return realloc( p, i_size );
384 i_old_size = *( (int*) ( (uint8_t*) p ) - sizeof( void ** ) -
387 p_new = x264_malloc( i_size );
388 if( i_old_size > 0 && i_size > 0 )
390 memcpy( p_new, p, ( i_old_size < i_size ) ? i_old_size : i_size );
397 /****************************************************************************
399 ****************************************************************************/
400 char *x264_slurp_file( const char *filename )
405 FILE *fh = fopen( filename, "rb" );
408 b_error |= fseek( fh, 0, SEEK_END ) < 0;
409 b_error |= ( i_size = ftell( fh ) ) <= 0;
410 b_error |= fseek( fh, 0, SEEK_SET ) < 0;
413 buf = x264_malloc( i_size+2 );
416 b_error |= fread( buf, 1, i_size, fh ) != i_size;
417 if( buf[i_size-1] != '\n' )
418 buf[i_size++] = '\n';
429 /****************************************************************************
431 ****************************************************************************/
432 char *x264_param2string( x264_param_t *p, int b_res )
434 char *buf = x264_malloc( 1000 );
439 s += sprintf( s, "%dx%d ", p->i_width, p->i_height );
440 s += sprintf( s, "fps=%d/%d ", p->i_fps_num, p->i_fps_den );
443 s += sprintf( s, "cabac=%d", p->b_cabac );
444 s += sprintf( s, " ref=%d", p->i_frame_reference );
445 s += sprintf( s, " deblock=%d:%d:%d", p->b_deblocking_filter,
446 p->i_deblocking_filter_alphac0, p->i_deblocking_filter_beta );
447 s += sprintf( s, " analyse=%#x:%#x", p->analyse.intra, p->analyse.inter );
448 s += sprintf( s, " me=%s", x264_motion_est_names[ p->analyse.i_me_method ] );
449 s += sprintf( s, " subme=%d", p->analyse.i_subpel_refine );
450 s += sprintf( s, " brdo=%d", p->analyse.b_bframe_rdo );
451 s += sprintf( s, " mixed_ref=%d", p->analyse.b_mixed_references );
452 s += sprintf( s, " me_range=%d", p->analyse.i_me_range );
453 s += sprintf( s, " chroma_me=%d", p->analyse.b_chroma_me );
454 s += sprintf( s, " trellis=%d", p->analyse.i_trellis );
455 s += sprintf( s, " 8x8dct=%d", p->analyse.b_transform_8x8 );
456 s += sprintf( s, " cqm=%d", p->i_cqm_preset );
457 s += sprintf( s, " chroma_qp_offset=%d", p->analyse.i_chroma_qp_offset );
458 s += sprintf( s, " slices=%d", p->i_threads );
460 s += sprintf( s, " bframes=%d", p->i_bframe );
463 s += sprintf( s, " b_pyramid=%d b_adapt=%d b_bias=%d direct=%d wpredb=%d",
464 p->b_bframe_pyramid, p->b_bframe_adaptive, p->i_bframe_bias,
465 p->analyse.i_direct_mv_pred, p->analyse.b_weighted_bipred );
468 s += sprintf( s, " keyint=%d keyint_min=%d scenecut=%d",
469 p->i_keyint_max, p->i_keyint_min, p->i_scenecut_threshold );
471 s += sprintf( s, " pass=%d", p->rc.b_stat_read ? 2 : 1 );
472 if( p->rc.b_cbr || p->rc.i_rf_constant )
474 if( p->rc.i_rf_constant )
475 s += sprintf( s, " crf=%d", p->rc.i_rf_constant );
477 s += sprintf( s, " bitrate=%d ratetol=%.1f",
478 p->rc.i_bitrate, p->rc.f_rate_tolerance );
479 s += sprintf( s, " rceq='%s' qcomp=%.2f qpmin=%d qpmax=%d qpstep=%d",
480 p->rc.psz_rc_eq, p->rc.f_qcompress,
481 p->rc.i_qp_min, p->rc.i_qp_max, p->rc.i_qp_step );
482 if( p->rc.b_stat_read )
483 s += sprintf( s, " cplxblur=%.1f qblur=%.1f",
484 p->rc.f_complexity_blur, p->rc.f_qblur );
485 if( p->rc.i_vbv_max_bitrate && p->rc.i_vbv_buffer_size )
486 s += sprintf( s, " vbv_maxrate=%d vbv_bufsize=%d",
487 p->rc.i_vbv_max_bitrate, p->rc.i_vbv_buffer_size );
490 s += sprintf( s, " qp=%d", p->rc.i_qp_constant );
491 if( p->rc.b_cbr || p->rc.i_qp_constant != 0 )
493 s += sprintf( s, " ip_ratio=%.2f", p->rc.f_ip_factor );
495 s += sprintf( s, " pb_ratio=%.2f", p->rc.f_pb_factor );
497 s += sprintf( s, " zones" );