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 *****************************************************************************/
35 static void x264_log_default( void *, int, const char *, va_list );
37 /****************************************************************************
39 ****************************************************************************/
40 void x264_param_default( x264_param_t *param )
43 memset( param, 0, sizeof( x264_param_t ) );
46 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->vui.i_overscan = 0; /* undef */
56 param->vui.i_vidformat = 5; /* undef */
57 param->vui.b_fullrange = 0; /* off */
58 param->vui.i_colorprim = 2; /* undef */
59 param->vui.i_transfer = 2; /* undef */
60 param->vui.i_colmatrix = 2; /* undef */
61 param->vui.i_chroma_loc= 0; /* left center */
62 param->i_fps_num = 25;
64 param->i_level_idc = 51; /* as close to "unrestricted" as we can get */
66 /* Encoder parameters */
67 param->i_frame_reference = 1;
68 param->i_keyint_max = 250;
69 param->i_keyint_min = 25;
71 param->i_scenecut_threshold = 40;
72 param->b_bframe_adaptive = 1;
73 param->i_bframe_bias = 0;
74 param->b_bframe_pyramid = 0;
76 param->b_deblocking_filter = 1;
77 param->i_deblocking_filter_alphac0 = 0;
78 param->i_deblocking_filter_beta = 0;
81 param->i_cabac_init_idc = 0;
84 param->rc.i_bitrate = 0;
85 param->rc.f_rate_tolerance = 1.0;
86 param->rc.i_vbv_max_bitrate = 0;
87 param->rc.i_vbv_buffer_size = 0;
88 param->rc.f_vbv_buffer_init = 0.9;
89 param->rc.i_qp_constant = 26;
90 param->rc.i_rf_constant = 0;
91 param->rc.i_qp_min = 10;
92 param->rc.i_qp_max = 51;
93 param->rc.i_qp_step = 4;
94 param->rc.f_ip_factor = 1.4;
95 param->rc.f_pb_factor = 1.3;
97 param->rc.b_stat_write = 0;
98 param->rc.psz_stat_out = "x264_2pass.log";
99 param->rc.b_stat_read = 0;
100 param->rc.psz_stat_in = "x264_2pass.log";
101 param->rc.psz_rc_eq = "blurCplx^(1-qComp)";
102 param->rc.f_qcompress = 0.6;
103 param->rc.f_qblur = 0.5;
104 param->rc.f_complexity_blur = 20;
105 param->rc.i_zones = 0;
108 param->pf_log = x264_log_default;
109 param->p_log_private = NULL;
110 param->i_log_level = X264_LOG_INFO;
113 param->analyse.intra = X264_ANALYSE_I4x4 | X264_ANALYSE_I8x8;
114 param->analyse.inter = X264_ANALYSE_I4x4 | X264_ANALYSE_I8x8
115 | X264_ANALYSE_PSUB16x16 | X264_ANALYSE_BSUB16x16;
116 param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_SPATIAL;
117 param->analyse.i_me_method = X264_ME_HEX;
118 param->analyse.i_me_range = 16;
119 param->analyse.i_subpel_refine = 5;
120 param->analyse.b_chroma_me = 1;
121 param->analyse.i_mv_range = -1; // set from level_idc
122 param->analyse.i_chroma_qp_offset = 0;
123 param->analyse.b_fast_pskip = 1;
124 param->analyse.b_dct_decimate = 1;
125 param->analyse.b_psnr = 1;
127 param->i_cqm_preset = X264_CQM_FLAT;
128 memset( param->cqm_4iy, 16, 16 );
129 memset( param->cqm_4ic, 16, 16 );
130 memset( param->cqm_4py, 16, 16 );
131 memset( param->cqm_4pc, 16, 16 );
132 memset( param->cqm_8iy, 16, 64 );
133 memset( param->cqm_8py, 16, 64 );
135 param->b_repeat_headers = 1;
139 /****************************************************************************
141 ****************************************************************************/
142 void x264_log( x264_t *h, int i_level, const char *psz_fmt, ... )
144 if( i_level <= h->param.i_log_level )
147 va_start( arg, psz_fmt );
148 h->param.pf_log( h->param.p_log_private, i_level, psz_fmt, arg );
153 static void x264_log_default( void *p_unused, int i_level, const char *psz_fmt, va_list arg )
159 psz_prefix = "error";
161 case X264_LOG_WARNING:
162 psz_prefix = "warning";
168 psz_prefix = "debug";
171 psz_prefix = "unknown";
174 fprintf( stderr, "x264 [%s]: ", psz_prefix );
175 vfprintf( stderr, psz_fmt, arg );
178 /****************************************************************************
179 * x264_picture_alloc:
180 ****************************************************************************/
181 void x264_picture_alloc( x264_picture_t *pic, int i_csp, int i_width, int i_height )
183 pic->i_type = X264_TYPE_AUTO;
185 pic->img.i_csp = i_csp;
186 switch( i_csp & X264_CSP_MASK )
190 pic->img.i_plane = 3;
191 pic->img.plane[0] = x264_malloc( 3 * i_width * i_height / 2 );
192 pic->img.plane[1] = pic->img.plane[0] + i_width * i_height;
193 pic->img.plane[2] = pic->img.plane[1] + i_width * i_height / 4;
194 pic->img.i_stride[0] = i_width;
195 pic->img.i_stride[1] = i_width / 2;
196 pic->img.i_stride[2] = i_width / 2;
200 pic->img.i_plane = 3;
201 pic->img.plane[0] = x264_malloc( 2 * i_width * i_height );
202 pic->img.plane[1] = pic->img.plane[0] + i_width * i_height;
203 pic->img.plane[2] = pic->img.plane[1] + i_width * i_height / 2;
204 pic->img.i_stride[0] = i_width;
205 pic->img.i_stride[1] = i_width / 2;
206 pic->img.i_stride[2] = i_width / 2;
210 pic->img.i_plane = 3;
211 pic->img.plane[0] = x264_malloc( 3 * i_width * i_height );
212 pic->img.plane[1] = pic->img.plane[0] + i_width * i_height;
213 pic->img.plane[2] = pic->img.plane[1] + i_width * i_height;
214 pic->img.i_stride[0] = i_width;
215 pic->img.i_stride[1] = i_width;
216 pic->img.i_stride[2] = i_width;
220 pic->img.i_plane = 1;
221 pic->img.plane[0] = x264_malloc( 2 * i_width * i_height );
222 pic->img.i_stride[0] = 2 * i_width;
227 pic->img.i_plane = 1;
228 pic->img.plane[0] = x264_malloc( 3 * i_width * i_height );
229 pic->img.i_stride[0] = 3 * i_width;
233 pic->img.i_plane = 1;
234 pic->img.plane[0] = x264_malloc( 4 * i_width * i_height );
235 pic->img.i_stride[0] = 4 * i_width;
239 fprintf( stderr, "invalid CSP\n" );
240 pic->img.i_plane = 0;
245 /****************************************************************************
246 * x264_picture_clean:
247 ****************************************************************************/
248 void x264_picture_clean( x264_picture_t *pic )
250 x264_free( pic->img.plane[0] );
252 /* just to be safe */
253 memset( pic, 0, sizeof( x264_picture_t ) );
256 /****************************************************************************
258 ****************************************************************************/
259 int x264_nal_encode( void *p_data, int *pi_data, int b_annexeb, x264_nal_t *nal )
261 uint8_t *dst = p_data;
262 uint8_t *src = nal->p_payload;
263 uint8_t *end = &nal->p_payload[nal->i_payload];
267 /* FIXME this code doesn't check overflow */
271 /* long nal start code (we always use long ones)*/
279 *dst++ = ( 0x00 << 7 ) | ( nal->i_ref_idc << 5 ) | nal->i_type;
283 if( i_count == 2 && *src <= 0x03 )
298 *pi_data = dst - (uint8_t*)p_data;
303 /****************************************************************************
305 ****************************************************************************/
306 int x264_nal_decode( x264_nal_t *nal, void *p_data, int i_data )
308 uint8_t *src = p_data;
309 uint8_t *end = &src[i_data];
310 uint8_t *dst = nal->p_payload;
312 nal->i_type = src[0]&0x1f;
313 nal->i_ref_idc = (src[0] >> 5)&0x03;
319 if( src < end - 3 && src[0] == 0x00 && src[1] == 0x00 && src[2] == 0x03 )
330 nal->i_payload = dst - (uint8_t*)p_data;
336 /****************************************************************************
338 ****************************************************************************/
339 void *x264_malloc( int i_size )
342 /* Mac OS X always returns 16 bytes aligned memory */
343 return malloc( i_size );
344 #elif defined( HAVE_MALLOC_H )
345 return memalign( 16, i_size );
349 buf = (uint8_t *) malloc( i_size + 15 + sizeof( void ** ) +
351 align_buf = buf + 15 + sizeof( void ** ) + sizeof( int );
352 align_buf -= (long) align_buf & 15;
353 *( (void **) ( align_buf - sizeof( void ** ) ) ) = buf;
354 *( (int *) ( align_buf - sizeof( void ** ) - sizeof( int ) ) ) = i_size;
359 /****************************************************************************
361 ****************************************************************************/
362 void x264_free( void *p )
366 #if defined( HAVE_MALLOC_H ) || defined( SYS_MACOSX )
369 free( *( ( ( void **) p ) - 1 ) );
374 /****************************************************************************
376 ****************************************************************************/
377 void *x264_realloc( void *p, int i_size )
380 return realloc( p, i_size );
386 i_old_size = *( (int*) ( (uint8_t*) p ) - sizeof( void ** ) -
389 p_new = x264_malloc( i_size );
390 if( i_old_size > 0 && i_size > 0 )
392 memcpy( p_new, p, ( i_old_size < i_size ) ? i_old_size : i_size );
399 /****************************************************************************
400 * x264_reduce_fraction:
401 ****************************************************************************/
402 void x264_reduce_fraction( int *n, int *d )
420 /****************************************************************************
422 ****************************************************************************/
423 char *x264_slurp_file( const char *filename )
428 FILE *fh = fopen( filename, "rb" );
431 b_error |= fseek( fh, 0, SEEK_END ) < 0;
432 b_error |= ( i_size = ftell( fh ) ) <= 0;
433 b_error |= fseek( fh, 0, SEEK_SET ) < 0;
436 buf = x264_malloc( i_size+2 );
439 b_error |= fread( buf, 1, i_size, fh ) != i_size;
440 if( buf[i_size-1] != '\n' )
441 buf[i_size++] = '\n';
452 /****************************************************************************
454 ****************************************************************************/
455 char *x264_param2string( x264_param_t *p, int b_res )
457 char *buf = x264_malloc( 1000 );
462 s += sprintf( s, "%dx%d ", p->i_width, p->i_height );
463 s += sprintf( s, "fps=%d/%d ", p->i_fps_num, p->i_fps_den );
466 s += sprintf( s, "cabac=%d", p->b_cabac );
467 s += sprintf( s, " ref=%d", p->i_frame_reference );
468 s += sprintf( s, " deblock=%d:%d:%d", p->b_deblocking_filter,
469 p->i_deblocking_filter_alphac0, p->i_deblocking_filter_beta );
470 s += sprintf( s, " analyse=%#x:%#x", p->analyse.intra, p->analyse.inter );
471 s += sprintf( s, " me=%s", x264_motion_est_names[ p->analyse.i_me_method ] );
472 s += sprintf( s, " subme=%d", p->analyse.i_subpel_refine );
473 s += sprintf( s, " brdo=%d", p->analyse.b_bframe_rdo );
474 s += sprintf( s, " mixed_ref=%d", p->analyse.b_mixed_references );
475 s += sprintf( s, " me_range=%d", p->analyse.i_me_range );
476 s += sprintf( s, " chroma_me=%d", p->analyse.b_chroma_me );
477 s += sprintf( s, " trellis=%d", p->analyse.i_trellis );
478 s += sprintf( s, " 8x8dct=%d", p->analyse.b_transform_8x8 );
479 s += sprintf( s, " cqm=%d", p->i_cqm_preset );
480 s += sprintf( s, " chroma_qp_offset=%d", p->analyse.i_chroma_qp_offset );
481 s += sprintf( s, " slices=%d", p->i_threads );
482 s += sprintf( s, " nr=%d", p->analyse.i_noise_reduction );
483 s += sprintf( s, " decimate=%d", p->analyse.b_dct_decimate );
485 s += sprintf( s, " bframes=%d", p->i_bframe );
488 s += sprintf( s, " b_pyramid=%d b_adapt=%d b_bias=%d direct=%d wpredb=%d bime=%d",
489 p->b_bframe_pyramid, p->b_bframe_adaptive, p->i_bframe_bias,
490 p->analyse.i_direct_mv_pred, p->analyse.b_weighted_bipred,
491 p->analyse.b_bidir_me );
494 s += sprintf( s, " keyint=%d keyint_min=%d scenecut=%d",
495 p->i_keyint_max, p->i_keyint_min, p->i_scenecut_threshold );
497 s += sprintf( s, " rc=%s", p->rc.b_stat_read && p->rc.b_cbr ? "2pass" :
498 p->rc.b_cbr ? p->rc.i_vbv_buffer_size ? "cbr" : "abr" :
499 p->rc.i_rf_constant ? "crf" : "cqp" );
500 if( p->rc.b_cbr || p->rc.i_rf_constant )
502 if( p->rc.i_rf_constant )
503 s += sprintf( s, " crf=%d", p->rc.i_rf_constant );
505 s += sprintf( s, " bitrate=%d ratetol=%.1f",
506 p->rc.i_bitrate, p->rc.f_rate_tolerance );
507 s += sprintf( s, " rceq='%s' qcomp=%.2f qpmin=%d qpmax=%d qpstep=%d",
508 p->rc.psz_rc_eq, p->rc.f_qcompress,
509 p->rc.i_qp_min, p->rc.i_qp_max, p->rc.i_qp_step );
510 if( p->rc.b_stat_read )
511 s += sprintf( s, " cplxblur=%.1f qblur=%.1f",
512 p->rc.f_complexity_blur, p->rc.f_qblur );
513 if( p->rc.i_vbv_buffer_size )
514 s += sprintf( s, " vbv_maxrate=%d vbv_bufsize=%d",
515 p->rc.i_vbv_max_bitrate, p->rc.i_vbv_buffer_size );
518 s += sprintf( s, " qp=%d", p->rc.i_qp_constant );
519 if( p->rc.b_cbr || p->rc.i_qp_constant != 0 )
521 s += sprintf( s, " ip_ratio=%.2f", p->rc.f_ip_factor );
523 s += sprintf( s, " pb_ratio=%.2f", p->rc.f_pb_factor );
525 s += sprintf( s, " zones" );