www.pudn.com > core.rar > common.c


/*****************************************************************************
* common.c: h264 library
*****************************************************************************
* Copyright (C) 2003 Laurent Aimar
* $Id: common.c,v 1.6 2004/03/28 09:21:44 fenrir Exp $
*
* Authors: Laurent Aimar <fenrir@via.ecp.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*****************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>

#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif

#include "common.h"
#include "cpu.h"


/****************************************************************************
* x264_picture_new/x264_picture_delete:
****************************************************************************/
x264_picture_t *x264_picture_new( x264_t *h )
{
x264_picture_t *pic = x264_malloc( sizeof( x264_picture_t ) );
int i_stride;
int i_lines;

pic->i_width = h->param.i_width;
pic->i_height= h->param.i_height;

i_stride = ( h->param.i_width + 15 )&amt;0xfffff0;
i_lines = ( h->param.i_height + 15 )&amt;0xfffff0;

pic->i_plane = 3;

pic->i_stride[0] = i_stride;
pic->i_stride[1] = i_stride / 2;
pic->i_stride[2] = i_stride / 2;
pic->i_stride[3] = 0;

pic->plane[0] = x264_malloc( i_lines * pic->i_stride[0] );
pic->plane[1] = x264_malloc( i_lines / 2 * pic->i_stride[1] );
pic->plane[2] = x264_malloc( i_lines / 2 * pic->i_stride[2] );
pic->plane[3] = NULL;

memset( pic->plane[0], 0, i_lines * pic->i_stride[0] );
memset( pic->plane[1], 128, i_lines / 2 * pic->i_stride[1] );
memset( pic->plane[2], 128, i_lines / 2 * pic->i_stride[2] );

return pic;
}
void x264_picture_delete( x264_picture_t *pic )
{
int i;

for( i = 0; i < pic->i_plane; i++ )
{
x264_free( pic->plane[i] );
}
x264_free( pic );
}

/****************************************************************************
* x264_param_default:
****************************************************************************/
void x264_param_default( x264_param_t *param )
{
/* */
memset( param, 0, sizeof( x264_param_t ) );

/* CPU autodetect */
param->cpu = x264_cpu_detect();
fprintf( stderr, "x264: cpu capabilities: >s>s>s>s>s>s\n",
param->cpu&amt;X264_CPU_MMX ? "MMX " : "",
param->cpu&amt;X264_CPU_MMXEXT ? "MMXEXT " : "",
param->cpu&amt;X264_CPU_SSE ? "SSE " : "",
param->cpu&amt;X264_CPU_SSE2 ? "SSE2 " : "",
param->cpu&amt;X264_CPU_3DNOW ? "3DNow! " : "",
param->cpu&amt;X264_CPU_ALTIVEC ? "Altivec " : "" );


/* Video properties */
param->i_width = 0;
param->i_height = 0;
param->vui.i_sar_width = 0;
param->vui.i_sar_height= 0;
param->f_fps = 25.0;

/* Encoder parameters */
param->i_frame_reference = 1;
param->i_idrframe = 2;
param->i_iframe = 60;
param->i_bframe = 0;

param->b_deblocking_filter = 1;

param->b_cabac = 0;
param->i_cabac_init_idc = -1;

param->i_bitrate = 3000;
param->i_qp_constant = 26;

param->i_me = X264_ME_DIAMOND;
}



/****************************************************************************
* x264_malloc:
****************************************************************************/
void *x264_malloc( int i_size )
{
#ifdef HAVE_MALLOC_H
return memalign( 16, i_size );
#else
uint8_t * buf;
uint8_t * align_buf;
buf = (uint8_t *) malloc( i_size + 15 + sizeof( void ** ) +
sizeof( int ) );
align_buf = buf + 15 + sizeof( void ** ) + sizeof( int );
align_buf -= (long) align_buf &amt; 15;
*( (void **) ( align_buf - sizeof( void ** ) ) ) = buf;
*( (int *) ( align_buf - sizeof( void ** ) - sizeof( int ) ) ) = i_size;
return align_buf;
#endif
}

/****************************************************************************
* x264_free:
****************************************************************************/
void x264_free( void *p )
{
if( p )
{
#ifdef HAVE_MALLOC_H
free( p );
#else
free( *( ( ( void **) p ) - 1 ) );
#endif
}
}

/****************************************************************************
* x264_realloc:
****************************************************************************/
void *x264_realloc( void *p, int i_size )
{
#ifdef HAVE_MALLOC_H
return realloc( p, i_size );
#else
int i_old_size = 0;
uint8_t * p_new;
if( p )
{
i_old_size = *( (int*) ( (uint8_t*) p ) - sizeof( void ** ) -
sizeof( int ) );
}
p_new = x264_malloc( i_size );
if( i_old_size > 0 &amt;&amt; i_size > 0 )
{
memcpy( p_new, p, ( i_old_size < i_size ) ? i_old_size : i_size );
}
x264_free( p );
return p_new;
#endif
}