libx264 编码示例
2019-04-15 本文已影响5人
smallest_one
目录
- 参考
- 示例说明
- 示例代码
1. 参考
2. 示例说明
示例来源于[1]。功能为把YUV420P的视频数据编码为H.264的压缩数据。

说明:
- x264_param_default_preset():与x264_param_default相同会设置参数集结构体x264_param_t的默认值,同时使用传入的preset和tune来修改默认值。支持的preset有
"ultrafast", "superfast", "veryfast", "faster", "fast", "medium", "slow", "slower", "veryslow", "placebo"
。 - Configure non-default params: 设置编码需要的一些参数如图像的宽、高、位深、色彩空间等。
- x264_param_apply_profile():指定编码使用的profile。不保证使用指定的profile,有可能会降低profile,设置成功时返回0。支持的profile有
"baseline", "main", "high", "high10", "high422", "high444"
。 - x264_picture_alloc():为图像结构体
x264_picture_t
分配内存,用于存储未压缩的数据。使用完需要调用x264_picture_clean()
释放资源。 - x264_encoder_open():打开编码器。使用完毕需要调用
x264_encoder_close()
关闭编码器。 - x264_encoder_encode():编码一帧图像,编码后的数据存储在x264_nal_t中。
- x264_encoder_delayed_frames:返回当前延迟的(缓冲的)帧的数量。
3. 示例代码
说明:
- 示例代码来源于[1],把数据的输入/输出由原来的“stdin/stdout”修改为了“从文件读和写入文件”。不修改之前发现输出的H.264文件有问题,怀疑不是H.264的数据的输出到了标准输出。
#include <stdint.h>
#include <stdio.h>
#include <x264.h>
#define FAIL_IF_ERROR( cond, ... )\
do\
{\
if( cond )\
{\
fprintf( stderr, __VA_ARGS__ );\
goto fail;\
}\
} while( 0 )
int main( int argc, char **argv )
{
int width, height;
x264_param_t param;
x264_picture_t pic;
x264_picture_t pic_out;
x264_t *h;
int i_frame = 0;
int i_frame_size;
x264_nal_t *nal;
int i_nal;
FILE *file_in = NULL;
FILE *file_out = NULL;
#ifdef _WIN32
_setmode( _fileno( stdin ), _O_BINARY );
_setmode( _fileno( stdout ), _O_BINARY );
_setmode( _fileno( stderr ), _O_BINARY );
#endif
FAIL_IF_ERROR( !(argc > 3), "Example usage: example <352x288> <input.yuv> <output.h264>\n" );
FAIL_IF_ERROR( 2 != sscanf( argv[1], "%dx%d", &width, &height ), "resolution not specified or incorrect\n" );
const char *in_filename = argv[2];
const char *out_filename = argv[3];
file_in = fopen(in_filename, "r+b");
if (!file_in) {
fprintf(stderr, "open input failed, %s\n", in_filename);
goto fail;
}
file_out = fopen(out_filename, "w+b");
if (!file_out) {
fprintf(stderr, "open output failed, %s\n", out_filename);
goto fail;
}
/* Get default params for preset/tuning */
if( x264_param_default_preset( ¶m, "medium", NULL ) < 0 )
goto fail;
/* Configure non-default params */
param.i_bitdepth = 8;
param.i_csp = X264_CSP_I420;
param.i_width = width;
param.i_height = height;
param.b_vfr_input = 0;
param.b_repeat_headers = 1;
param.b_annexb = 1;
/* Apply profile restrictions. */
if( x264_param_apply_profile( ¶m, "high" ) < 0 )
goto fail;
if( x264_picture_alloc( &pic, param.i_csp, param.i_width, param.i_height ) < 0 )
goto fail;
#undef fail
#define fail fail2
h = x264_encoder_open( ¶m );
if( !h )
goto fail;
#undef fail
#define fail fail3
int luma_size = width * height;
int chroma_size = luma_size / 4;
/* Encode frames */
for( ;; i_frame++ )
{
/* Read input frame */
if( fread( pic.img.plane[0], 1, luma_size, file_in ) != luma_size )
break;
if( fread( pic.img.plane[1], 1, chroma_size, file_in ) != chroma_size )
break;
if( fread( pic.img.plane[2], 1, chroma_size, file_in ) != chroma_size )
break;
pic.i_pts = i_frame;
i_frame_size = x264_encoder_encode( h, &nal, &i_nal, &pic, &pic_out );
if( i_frame_size < 0 )
goto fail;
else if( i_frame_size )
{
if( !fwrite( nal->p_payload, i_frame_size, 1, file_out ) )
goto fail;
}
}
/* Flush delayed frames */
while( x264_encoder_delayed_frames( h ) )
{
i_frame_size = x264_encoder_encode( h, &nal, &i_nal, NULL, &pic_out );
if( i_frame_size < 0 )
goto fail;
else if( i_frame_size )
{
if( !fwrite( nal->p_payload, i_frame_size, 1, file_out ) )
goto fail;
}
}
x264_encoder_close( h );
x264_picture_clean( &pic );
if (file_in)
fclose(file_in);
if (file_out)
fclose(file_out);
return 0;
#undef fail
fail3:
x264_encoder_close( h );
fail2:
x264_picture_clean( &pic );
fail:
if (file_in)
fclose(file_in);
if (file_out)
fclose(file_out);
return -1;
}