]> git.sesse.net Git - x264/blob - common/common.c
check some mallocs' return value
[x264] / common / common.c
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 $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdarg.h>
27
28 #ifdef HAVE_MALLOC_H
29 #include <malloc.h>
30 #endif
31
32 #include "common.h"
33 #include "cpu.h"
34
35 static void x264_log_default( void *, int, const char *, va_list );
36
37 /****************************************************************************
38  * x264_param_default:
39  ****************************************************************************/
40 void    x264_param_default( x264_param_t *param )
41 {
42     /* */
43     memset( param, 0, sizeof( x264_param_t ) );
44
45     /* CPU autodetect */
46     param->cpu = x264_cpu_detect();
47     param->i_threads = 1;
48
49     /* Video properties */
50     param->i_csp           = X264_CSP_I420;
51     param->i_width         = 0;
52     param->i_height        = 0;
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;
63     param->i_fps_den       = 1;
64     param->i_level_idc     = 51; /* as close to "unrestricted" as we can get */
65
66     /* Encoder parameters */
67     param->i_frame_reference = 1;
68     param->i_keyint_max = 250;
69     param->i_keyint_min = 25;
70     param->i_bframe = 0;
71     param->i_scenecut_threshold = 40;
72     param->b_bframe_adaptive = 1;
73     param->i_bframe_bias = 0;
74     param->b_bframe_pyramid = 0;
75
76     param->b_deblocking_filter = 1;
77     param->i_deblocking_filter_alphac0 = 0;
78     param->i_deblocking_filter_beta = 0;
79
80     param->b_cabac = 1;
81     param->i_cabac_init_idc = 0;
82
83     param->rc.b_cbr = 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;
96
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;
106
107     /* Log */
108     param->pf_log = x264_log_default;
109     param->p_log_private = NULL;
110     param->i_log_level = X264_LOG_INFO;
111
112     /* */
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;
126
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 );
134
135     param->b_repeat_headers = 1;
136     param->b_aud = 0;
137 }
138
139 /****************************************************************************
140  * x264_log:
141  ****************************************************************************/
142 void x264_log( x264_t *h, int i_level, const char *psz_fmt, ... )
143 {
144     if( i_level <= h->param.i_log_level )
145     {
146         va_list arg;
147         va_start( arg, psz_fmt );
148         h->param.pf_log( h->param.p_log_private, i_level, psz_fmt, arg );
149         va_end( arg );
150     }
151 }
152
153 static void x264_log_default( void *p_unused, int i_level, const char *psz_fmt, va_list arg )
154 {
155     char *psz_prefix;
156     switch( i_level )
157     {
158         case X264_LOG_ERROR:
159             psz_prefix = "error";
160             break;
161         case X264_LOG_WARNING:
162             psz_prefix = "warning";
163             break;
164         case X264_LOG_INFO:
165             psz_prefix = "info";
166             break;
167         case X264_LOG_DEBUG:
168             psz_prefix = "debug";
169             break;
170         default:
171             psz_prefix = "unknown";
172             break;
173     }
174     fprintf( stderr, "x264 [%s]: ", psz_prefix );
175     vfprintf( stderr, psz_fmt, arg );
176 }
177
178 /****************************************************************************
179  * x264_picture_alloc:
180  ****************************************************************************/
181 void x264_picture_alloc( x264_picture_t *pic, int i_csp, int i_width, int i_height )
182 {
183     pic->i_type = X264_TYPE_AUTO;
184     pic->i_qpplus1 = 0;
185     pic->img.i_csp = i_csp;
186     switch( i_csp & X264_CSP_MASK )
187     {
188         case X264_CSP_I420:
189         case X264_CSP_YV12:
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;
197             break;
198
199         case X264_CSP_I422:
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;
207             break;
208
209         case X264_CSP_I444:
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;
217             break;
218
219         case X264_CSP_YUYV:
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;
223             break;
224
225         case X264_CSP_RGB:
226         case X264_CSP_BGR:
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;
230             break;
231
232         case X264_CSP_BGRA:
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;
236             break;
237
238         default:
239             fprintf( stderr, "invalid CSP\n" );
240             pic->img.i_plane = 0;
241             break;
242     }
243 }
244
245 /****************************************************************************
246  * x264_picture_clean:
247  ****************************************************************************/
248 void x264_picture_clean( x264_picture_t *pic )
249 {
250     x264_free( pic->img.plane[0] );
251
252     /* just to be safe */
253     memset( pic, 0, sizeof( x264_picture_t ) );
254 }
255
256 /****************************************************************************
257  * x264_nal_encode:
258  ****************************************************************************/
259 int x264_nal_encode( void *p_data, int *pi_data, int b_annexeb, x264_nal_t *nal )
260 {
261     uint8_t *dst = p_data;
262     uint8_t *src = nal->p_payload;
263     uint8_t *end = &nal->p_payload[nal->i_payload];
264
265     int i_count = 0;
266
267     /* FIXME this code doesn't check overflow */
268
269     if( b_annexeb )
270     {
271         /* long nal start code (we always use long ones)*/
272         *dst++ = 0x00;
273         *dst++ = 0x00;
274         *dst++ = 0x00;
275         *dst++ = 0x01;
276     }
277
278     /* nal header */
279     *dst++ = ( 0x00 << 7 ) | ( nal->i_ref_idc << 5 ) | nal->i_type;
280
281     while( src < end )
282     {
283         if( i_count == 2 && *src <= 0x03 )
284         {
285             *dst++ = 0x03;
286             i_count = 0;
287         }
288         if( *src == 0 )
289         {
290             i_count++;
291         }
292         else
293         {
294             i_count = 0;
295         }
296         *dst++ = *src++;
297     }
298     *pi_data = dst - (uint8_t*)p_data;
299
300     return *pi_data;
301 }
302
303 /****************************************************************************
304  * x264_nal_decode:
305  ****************************************************************************/
306 int x264_nal_decode( x264_nal_t *nal, void *p_data, int i_data )
307 {
308     uint8_t *src = p_data;
309     uint8_t *end = &src[i_data];
310     uint8_t *dst = nal->p_payload;
311
312     nal->i_type    = src[0]&0x1f;
313     nal->i_ref_idc = (src[0] >> 5)&0x03;
314
315     src++;
316
317     while( src < end )
318     {
319         if( src < end - 3 && src[0] == 0x00 && src[1] == 0x00  && src[2] == 0x03 )
320         {
321             *dst++ = 0x00;
322             *dst++ = 0x00;
323
324             src += 3;
325             continue;
326         }
327         *dst++ = *src++;
328     }
329
330     nal->i_payload = dst - (uint8_t*)p_data;
331     return 0;
332 }
333
334
335
336 /****************************************************************************
337  * x264_malloc:
338  ****************************************************************************/
339 void *x264_malloc( int i_size )
340 {
341 #ifdef SYS_MACOSX
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 );
346 #else
347     uint8_t * buf;
348     uint8_t * align_buf;
349     buf = (uint8_t *) malloc( i_size + 15 + sizeof( void ** ) +
350               sizeof( int ) );
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;
355     return align_buf;
356 #endif
357 }
358
359 /****************************************************************************
360  * x264_free:
361  ****************************************************************************/
362 void x264_free( void *p )
363 {
364     if( p )
365     {
366 #if defined( HAVE_MALLOC_H ) || defined( SYS_MACOSX )
367         free( p );
368 #else
369         free( *( ( ( void **) p ) - 1 ) );
370 #endif
371     }
372 }
373
374 /****************************************************************************
375  * x264_realloc:
376  ****************************************************************************/
377 void *x264_realloc( void *p, int i_size )
378 {
379 #ifdef HAVE_MALLOC_H
380     return realloc( p, i_size );
381 #else
382     int       i_old_size = 0;
383     uint8_t * p_new;
384     if( p )
385     {
386         i_old_size = *( (int*) ( (uint8_t*) p ) - sizeof( void ** ) -
387                          sizeof( int ) );
388     }
389     p_new = x264_malloc( i_size );
390     if( i_old_size > 0 && i_size > 0 )
391     {
392         memcpy( p_new, p, ( i_old_size < i_size ) ? i_old_size : i_size );
393     }
394     x264_free( p );
395     return p_new;
396 #endif
397 }
398
399 /****************************************************************************
400  * x264_reduce_fraction:
401  ****************************************************************************/
402 void x264_reduce_fraction( int *n, int *d )
403 {
404     int a = *n;
405     int b = *d;
406     int c;
407     if( !a || !b )
408         return;
409     c = a % b;
410     while(c)
411     {
412         a = b;
413         b = c;
414         c = a % b;
415     }
416     *n /= b;
417     *d /= b;
418 }
419
420 /****************************************************************************
421  * x264_slurp_file:
422  ****************************************************************************/
423 char *x264_slurp_file( const char *filename )
424 {
425     int b_error = 0;
426     int i_size;
427     char *buf;
428     FILE *fh = fopen( filename, "rb" );
429     if( !fh )
430         return NULL;
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;
434     if( b_error )
435         return NULL;
436     buf = x264_malloc( i_size+2 );
437     if( buf == NULL )
438         return NULL;
439     b_error |= fread( buf, 1, i_size, fh ) != i_size;
440     if( buf[i_size-1] != '\n' )
441         buf[i_size++] = '\n';
442     buf[i_size] = 0;
443     fclose( fh );
444     if( b_error )
445     {
446         x264_free( buf );
447         return NULL;
448     }
449     return buf;
450 }
451
452 /****************************************************************************
453  * x264_param2string:
454  ****************************************************************************/
455 char *x264_param2string( x264_param_t *p, int b_res )
456 {
457     char *buf = x264_malloc( 1000 );
458     char *s = buf;
459
460     if( b_res )
461     {
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 );
464     }
465
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 );
484
485     s += sprintf( s, " bframes=%d", p->i_bframe );
486     if( p->i_bframe )
487     {
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 );
492     }
493
494     s += sprintf( s, " keyint=%d keyint_min=%d scenecut=%d",
495                   p->i_keyint_max, p->i_keyint_min, p->i_scenecut_threshold );
496
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 )
501     {
502         if( p->rc.i_rf_constant )
503             s += sprintf( s, " crf=%d", p->rc.i_rf_constant );
504         else
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 );
516     }
517     else
518         s += sprintf( s, " qp=%d", p->rc.i_qp_constant );
519     if( p->rc.b_cbr || p->rc.i_qp_constant != 0 )
520     {
521         s += sprintf( s, " ip_ratio=%.2f", p->rc.f_ip_factor );
522         if( p->i_bframe )
523             s += sprintf( s, " pb_ratio=%.2f", p->rc.f_pb_factor );
524         if( p->rc.i_zones )
525             s += sprintf( s, " zones" );
526     }
527
528     return buf;
529 }
530