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