]> git.sesse.net Git - x264/blob - common/common.c
New option: "B-frame pyramid" keeps the middle of 2+ consecutive B-frames as a refere...
[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
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->i_fps_num       = 25;
56     param->i_fps_den       = 1;
57     param->i_maxframes     = 0;
58     param->i_level_idc     = 40; /* level 4.0 is sufficient for 720x576 with 
59                                     16 reference frames */
60
61     /* Encoder parameters */
62     param->i_frame_reference = 1;
63     param->i_keyint_max = 250;
64     param->i_keyint_min = 25;
65     param->i_bframe = 0;
66     param->i_scenecut_threshold = 40;
67     param->b_bframe_adaptive = 1;
68     param->i_bframe_bias = 0;
69     param->b_bframe_pyramid = 0;
70
71     param->b_deblocking_filter = 1;
72     param->i_deblocking_filter_alphac0 = 0;
73     param->i_deblocking_filter_beta = 0;
74
75     param->b_cabac = 1;
76     param->i_cabac_init_idc = -1;
77
78     param->rc.b_cbr = 0;
79     param->rc.i_bitrate = 3000;
80     param->rc.i_rc_buffer_size = 0;
81     param->rc.i_rc_init_buffer = 0;
82     param->rc.i_rc_sens = 10;
83     param->rc.i_qp_constant = 26;
84     param->rc.i_qp_min = 0;
85     param->rc.i_qp_max = 51;
86     param->rc.i_qp_step = 4;
87     param->rc.f_ip_factor = 1.4;
88     param->rc.f_pb_factor = 1.3;
89
90     param->rc.b_stat_write = 0;
91     param->rc.psz_stat_out = "x264_2pass.log";
92     param->rc.b_stat_read = 0;
93     param->rc.psz_stat_in = "x264_2pass.log";
94     param->rc.psz_rc_eq = "blurCplx^(1-qComp)";
95     param->rc.f_qcompress = 0.6;
96     param->rc.f_qblur = 0.5;
97     param->rc.f_complexity_blur = 20;
98
99     /* Log */
100     param->pf_log = x264_log_default;
101     param->p_log_private = NULL;
102     param->i_log_level = X264_LOG_INFO;
103
104     /* */
105     param->analyse.intra = X264_ANALYSE_I4x4;
106     param->analyse.inter = X264_ANALYSE_I4x4 | X264_ANALYSE_PSUB16x16 | X264_ANALYSE_BSUB16x16;
107     param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_TEMPORAL;
108     param->analyse.i_subpel_refine = 5;
109     param->analyse.i_mv_range = 512;
110     param->analyse.b_psnr = 1;
111 }
112
113 /****************************************************************************
114  * x264_log:
115  ****************************************************************************/
116 void x264_log( x264_t *h, int i_level, const char *psz_fmt, ... )
117 {
118     if( i_level <= h->param.i_log_level )
119     {
120         va_list arg;
121         va_start( arg, psz_fmt );
122         h->param.pf_log( h->param.p_log_private, i_level, psz_fmt, arg );
123         va_end( arg );
124     }
125 }
126
127 static void x264_log_default( void *p_unused, int i_level, const char *psz_fmt, va_list arg )
128 {
129     char *psz_prefix;
130     switch( i_level )
131     {
132         case X264_LOG_ERROR:
133             psz_prefix = "error";
134             break;
135         case X264_LOG_WARNING:
136             psz_prefix = "warning";
137             break;
138         case X264_LOG_INFO:
139             psz_prefix = "info";
140             break;
141         case X264_LOG_DEBUG:
142             psz_prefix = "debug";
143             break;
144         default:
145             psz_prefix = "unknown";
146             break;
147     }
148     fprintf( stderr, "x264 [%s]: ", psz_prefix );
149     vfprintf( stderr, psz_fmt, arg );
150 }
151
152 /****************************************************************************
153  * x264_picture_alloc:
154  ****************************************************************************/
155 void x264_picture_alloc( x264_picture_t *pic, int i_csp, int i_width, int i_height )
156 {
157     pic->i_type = X264_TYPE_AUTO;
158     pic->i_qpplus1 = 0;
159     pic->img.i_csp = i_csp;
160     switch( i_csp & X264_CSP_MASK )
161     {
162         case X264_CSP_I420:
163         case X264_CSP_YV12:
164             pic->img.i_plane = 3;
165             pic->img.plane[0] = x264_malloc( 3 * i_width * i_height / 2 );
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 / 4;
168             pic->img.i_stride[0] = i_width;
169             pic->img.i_stride[1] = i_width / 2;
170             pic->img.i_stride[2] = i_width / 2;
171             break;
172
173         case X264_CSP_I422:
174             pic->img.i_plane = 3;
175             pic->img.plane[0] = x264_malloc( 2 * i_width * i_height );
176             pic->img.plane[1] = pic->img.plane[0] + i_width * i_height;
177             pic->img.plane[2] = pic->img.plane[1] + i_width * i_height / 2;
178             pic->img.i_stride[0] = i_width;
179             pic->img.i_stride[1] = i_width / 2;
180             pic->img.i_stride[2] = i_width / 2;
181             break;
182
183         case X264_CSP_I444:
184             pic->img.i_plane = 3;
185             pic->img.plane[0] = x264_malloc( 3 * i_width * i_height );
186             pic->img.plane[1] = pic->img.plane[0] + i_width * i_height;
187             pic->img.plane[2] = pic->img.plane[1] + i_width * i_height;
188             pic->img.i_stride[0] = i_width;
189             pic->img.i_stride[1] = i_width;
190             pic->img.i_stride[2] = i_width;
191             break;
192
193         case X264_CSP_YUYV:
194             pic->img.i_plane = 1;
195             pic->img.plane[0] = x264_malloc( 2 * i_width * i_height );
196             pic->img.i_stride[0] = 2 * i_width;
197             break;
198
199         case X264_CSP_RGB:
200         case X264_CSP_BGR:
201             pic->img.i_plane = 1;
202             pic->img.plane[0] = x264_malloc( 3 * i_width * i_height );
203             pic->img.i_stride[0] = 3 * i_width;
204             break;
205
206         case X264_CSP_BGRA:
207             pic->img.i_plane = 1;
208             pic->img.plane[0] = x264_malloc( 4 * i_width * i_height );
209             pic->img.i_stride[0] = 4 * i_width;
210             break;
211
212         default:
213             fprintf( stderr, "invalid CSP\n" );
214             pic->img.i_plane = 0;
215             break;
216     }
217 }
218
219 /****************************************************************************
220  * x264_picture_clean:
221  ****************************************************************************/
222 void x264_picture_clean( x264_picture_t *pic )
223 {
224     x264_free( pic->img.plane[0] );
225
226     /* just to be safe */
227     memset( pic, 0, sizeof( x264_picture_t ) );
228 }
229
230 /****************************************************************************
231  * x264_nal_encode:
232  ****************************************************************************/
233 int x264_nal_encode( void *p_data, int *pi_data, int b_annexeb, x264_nal_t *nal )
234 {
235     uint8_t *dst = p_data;
236     uint8_t *src = nal->p_payload;
237     uint8_t *end = &nal->p_payload[nal->i_payload];
238
239     int i_count = 0;
240
241     /* FIXME this code doesn't check overflow */
242
243     if( b_annexeb )
244     {
245         /* long nal start code (we always use long ones)*/
246         *dst++ = 0x00;
247         *dst++ = 0x00;
248         *dst++ = 0x00;
249         *dst++ = 0x01;
250     }
251
252     /* nal header */
253     *dst++ = ( 0x00 << 7 ) | ( nal->i_ref_idc << 5 ) | nal->i_type;
254
255     while( src < end )
256     {
257         if( i_count == 2 && *src <= 0x03 )
258         {
259             *dst++ = 0x03;
260             i_count = 0;
261         }
262         if( *src == 0 )
263         {
264             i_count++;
265         }
266         else
267         {
268             i_count = 0;
269         }
270         *dst++ = *src++;
271     }
272     *pi_data = dst - (uint8_t*)p_data;
273
274     return *pi_data;
275 }
276
277 /****************************************************************************
278  * x264_nal_decode:
279  ****************************************************************************/
280 int x264_nal_decode( x264_nal_t *nal, void *p_data, int i_data )
281 {
282     uint8_t *src = p_data;
283     uint8_t *end = &src[i_data];
284     uint8_t *dst = nal->p_payload;
285
286     nal->i_type    = src[0]&0x1f;
287     nal->i_ref_idc = (src[0] >> 5)&0x03;
288
289     src++;
290
291     while( src < end )
292     {
293         if( src < end - 3 && src[0] == 0x00 && src[1] == 0x00  && src[2] == 0x03 )
294         {
295             *dst++ = 0x00;
296             *dst++ = 0x00;
297
298             src += 3;
299             continue;
300         }
301         *dst++ = *src++;
302     }
303
304     nal->i_payload = dst - (uint8_t*)p_data;
305     return 0;
306 }
307
308
309
310 /****************************************************************************
311  * x264_malloc:
312  ****************************************************************************/
313 void *x264_malloc( int i_size )
314 {
315 #ifdef SYS_MACOSX
316     /* Mac OS X always returns 16 bytes aligned memory */
317     return malloc( i_size );
318 #elif defined( HAVE_MALLOC_H )
319     return memalign( 16, i_size );
320 #else
321     uint8_t * buf;
322     uint8_t * align_buf;
323     buf = (uint8_t *) malloc( i_size + 15 + sizeof( void ** ) +
324               sizeof( int ) );
325     align_buf = buf + 15 + sizeof( void ** ) + sizeof( int );
326     align_buf -= (long) align_buf & 15;
327     *( (void **) ( align_buf - sizeof( void ** ) ) ) = buf;
328     *( (int *) ( align_buf - sizeof( void ** ) - sizeof( int ) ) ) = i_size;
329     return align_buf;
330 #endif
331 }
332
333 /****************************************************************************
334  * x264_free:
335  ****************************************************************************/
336 void x264_free( void *p )
337 {
338     if( p )
339     {
340 #if defined( HAVE_MALLOC_H ) || defined( SYS_MACOSX )
341         free( p );
342 #else
343         free( *( ( ( void **) p ) - 1 ) );
344 #endif
345     }
346 }
347
348 /****************************************************************************
349  * x264_realloc:
350  ****************************************************************************/
351 void *x264_realloc( void *p, int i_size )
352 {
353 #ifdef HAVE_MALLOC_H
354     return realloc( p, i_size );
355 #else
356     int       i_old_size = 0;
357     uint8_t * p_new;
358     if( p )
359     {
360         i_old_size = *( (int*) ( (uint8_t*) p ) - sizeof( void ** ) -
361                          sizeof( int ) );
362     }
363     p_new = x264_malloc( i_size );
364     if( i_old_size > 0 && i_size > 0 )
365     {
366         memcpy( p_new, p, ( i_old_size < i_size ) ? i_old_size : i_size );
367     }
368     x264_free( p );
369     return p_new;
370 #endif
371 }
372