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