?? x264.c
字號:
/*****************************************************************************
* x264: h264 encoder/decoder testing program.
*****************************************************************************
* Copyright (C) 2003 Laurent Aimar
* $Id: x264.c,v 1.1 2004/06/03 19:24:12 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 <windows.h>
//#include <math.h>
//#define _GNU_SOURCE
#include "common.h"
#include "x264.h"
#define FRM_WIDTH 352
#define FRM_HEIGHT 288
#define FRM_NUM 29
#define SE_BUFF_SIZE 720*576*5
#define DATA_MAX 50000
uint8_t data[DATA_MAX];
extern int64_t x264_mdate( void );
typedef uint8_t *hnd_t;
typedef struct {
int b_decompress;
int b_progress;
int i_maxframes;
int i_seek;
hnd_t hin;
hnd_t hout;
} cli_opt_t;
unsigned char reTemp[FRM_WIDTH*FRM_HEIGHT*FRM_NUM*3/2];
unsigned char seTemp[SE_BUFF_SIZE];
/* input file operation function pointers */
static int (*p_read_frame)( x264_picture_t *p_pic, hnd_t handle, int i_frame, int i_width, int i_height );
static int read_frame_yuv( x264_picture_t *p_pic, hnd_t handle, int i_frame, int i_width, int i_height );
/* output file operation function pointers */
static int (*p_set_outfile_param)( hnd_t handle, x264_param_t *p_param );
static int (*p_write_nalu)( hnd_t handle, uint8_t *p_nal, int i_size );
static int (*p_set_eop)( hnd_t handle, x264_picture_t *p_picture );
static int set_param_bsf( hnd_t handle, x264_param_t *p_param );
static int write_nalu_bsf( hnd_t handle, uint8_t *p_nal, int i_size );
static int set_eop_bsf( hnd_t handle, x264_picture_t *p_picture );
static int64_t Encode( x264_param_t *param, cli_opt_t *opt );
/****************************************************************************
* main:
****************************************************************************/
main()
{
x264_param_t param;
cli_opt_t opt;
int start,stop;
/* 測試用 */
FILE *fr,*fw;
int64_t i_codelength;
fr=fopen("e:\\claire.cif","rb");
fread(reTemp,FRM_WIDTH*FRM_HEIGHT*3/2,FRM_NUM,fr);
fclose(fr);
/* 測試用完 */
x264_param_default( ¶m );
memset( &opt, 0, sizeof(cli_opt_t) );
/* Default input file driver */
p_read_frame = read_frame_yuv;
/* Default output file driver */
p_set_outfile_param = set_param_bsf;
p_write_nalu = write_nalu_bsf;
p_set_eop = set_eop_bsf;
opt.hout=seTemp;
param.i_width=FRM_WIDTH;
param.i_height=FRM_HEIGHT;
opt.hin=reTemp;
start=GetTickCount();
i_codelength=Encode( ¶m, &opt );
stop=GetTickCount();
printf("encode time %d\n",(stop-start));
/* 測試用 */
fw=fopen("e:\\out11.264","wb+");
fwrite(seTemp,i_codelength,1,fw);
fclose(fw);
/* 測試用完 */
}
static int Encode_frame( x264_t *h, hnd_t hout, x264_picture_t *pic , int64_t i_offset)
{
x264_picture_t pic_out;
x264_nal_t *nal;
int i_nal, i;
int i_file = 0;
hnd_t hoff=hout+i_offset;
/* Do not force any parameters */
if( pic )
{
pic->i_type = X264_TYPE_AUTO;
pic->i_qpplus1 = 0;
}
if( x264_encoder_encode( h, &nal, &i_nal, pic, &pic_out ) < 0 )
{
fprintf( stderr, "x264_encoder_encode failed\n" );
}
for( i = 0; i < i_nal; i++ )
{
int i_size;
int i_data;
i_data = DATA_MAX;
if( ( i_size = x264_nal_encode( data, &i_data, 1, &nal[i] ) ) > 0 )
{
i_file += p_write_nalu( hoff, data, i_size );
hoff +=i_size;
}
else if( i_size < 0 )
{
fprintf( stderr, "need to increase buffer size (size=%d)\n", -i_size );
}
}
if (i_nal)
p_set_eop( hout, &pic_out );
return i_file;
}
/*****************************************************************************
* Encode:
*****************************************************************************/
static int64_t Encode( x264_param_t *param, cli_opt_t *opt )
{
x264_t *h;
x264_picture_t pic;
int i_frame, i_frame_total;
int64_t i_start, i_end;
int64_t i_file;
int i_frame_size;
int i_progress;
i_frame_total = FRM_NUM;
if( ( h = x264_encoder_open( param ) ) == NULL )
{
fprintf( stderr, "x264_encoder_open failed\n" );
return -1;
}
if( p_set_outfile_param( opt->hout, param ) )
{
fprintf( stderr, "can't set outfile param\n" );
return -1;
}
/* Create a new pic */
x264_picture_alloc( &pic, X264_CSP_I420, param->i_width, param->i_height );
// i_start = x264_mdate();
/* Encode frames */
i_frame_total -= opt->i_seek;
if( opt->i_maxframes > 0 && opt->i_maxframes < i_frame_total )
i_frame_total = opt->i_maxframes;
for( i_frame = 0, i_file = 0, i_progress = 0;
i_frame < i_frame_total || i_frame_total == 0; )
{
if( p_read_frame( &pic, opt->hin, i_frame + opt->i_seek, param->i_width, param->i_height ) )
break;
pic.i_pts = i_frame * param->i_fps_den;
i_file += Encode_frame( h, opt->hout, &pic ,i_file );
i_frame++;
/* update status line (up to 1000 times per input file) */
if( opt->b_progress && param->i_log_level < X264_LOG_DEBUG &&
i_frame * 1000 / i_frame_total > i_progress )
{
// int64_t i_elapsed = x264_mdate() - i_start;
// double fps = i_elapsed > 0 ? i_frame * 1000000. / i_elapsed : 0;
i_progress = i_frame * 1000 / i_frame_total;
fprintf( stderr, "encoded frames: %d/%d (%.1f%%) \r", i_frame,
i_frame_total, (float)i_progress / 10);
fflush( stderr ); // needed in windows
}
}
/* Flush delayed B-frames */
/* do {
i_file +=
i_frame_size = Encode_frame( h, opt->hout, NULL ,i_file );
} while( i_frame_size );*/
// i_end = x264_mdate();
x264_picture_clean( &pic );
x264_encoder_close( h );
fprintf( stderr, "\n" );
if( i_frame > 0 )
{
double fps = (double)i_frame * (double)1000000 /
(double)( i_end - i_start );
fprintf( stderr, "encoded %d frames, %.2f fps, %.2f kb/s\n", i_frame, fps,
(double) i_file * 8 * param->i_fps_num / ( param->i_fps_den * i_frame * 1000 ) );
}
return i_file;
}
/* raw 420 yuv file operation */
static int read_frame_yuv( x264_picture_t *p_pic, hnd_t handle, int i_frame, int i_width, int i_height )
{
static int prev_frame = -1;
memcpy(p_pic->img.plane[0],handle+i_frame*i_width*i_height*3/2,i_width*i_height);
memcpy(p_pic->img.plane[1],handle+i_frame*i_width*i_height*3/2+i_width*i_height,i_width*i_height/4);
memcpy(p_pic->img.plane[2],handle+i_frame*i_width*i_height*3/2+i_width*i_height*5/4,i_width*i_height/4);
/*FILE *f = (FILE *)handle;
if( i_frame != prev_frame+1 )
if( fseek( f, i_frame * i_width * i_height * 3 / 2, SEEK_SET ) )
return -1;
if( fread( p_pic->img.plane[0], 1, i_width * i_height, f ) <= 0
|| fread( p_pic->img.plane[1], 1, i_width * i_height / 4, f ) <= 0
|| fread( p_pic->img.plane[2], 1, i_width * i_height / 4, f ) <= 0 )
return -1;*/
prev_frame = i_frame;
return 0;
}
static int set_param_bsf( hnd_t handle, x264_param_t *p_param )
{
return 0;
}
static int write_nalu_bsf( hnd_t handle, uint8_t *p_nalu, int i_size )
{
memcpy(handle,p_nalu,i_size);
return i_size;
/*if (fwrite(p_nalu, i_size, 1, (FILE *)handle) > 0)
return i_size;
return -1;*/
}
static int set_eop_bsf( hnd_t handle, x264_picture_t *p_picture )
{
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -