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