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