libx264 编码示例

作者: smallest_one | 来源:发表于2019-04-15 11:46 被阅读5次

目录

  1. 参考
  2. 示例说明
  3. 示例代码

1. 参考

2. 示例说明

示例来源于[1]。功能为把YUV420P的视频数据编码为H.264的压缩数据。


x264_encode.png

说明:

  • 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. 示例代码来源于[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( &param, "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( &param, "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( &param );
    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;
}

相关文章

  • libx264 编码示例

    目录 参考 示例说明 示例代码 1. 参考 [1] code.videolan.org/videolan/x264...

  • FFmpeg MP4视频切片成TS m3u8播放系列(1)

    解析视频编码的时候提是错误 Unknown encoder 'libx264': 这个是缺少libx264库 视频...

  • FFmpeg转码(1)

    FFmpeg软编码(H.264) 查看libx264编码信息 x264编码参数 H.264编码举例: 1. 编码器...

  • libx264开发笔记(一):libx264介绍、海思平台移植编

    前言   在编译ffmpeg时,使用到h264编码时是需要依赖libx264的,本文章是将将libx264作为静态...

  • FFmpeg+x264编译

    在FFmpeg使用到编码时要使用到libx264,通过FFmpeg+libx264可实现视频编码效果。下面就是各个...

  • iOS FFmpeg+libx264包的产生

    在FFmpeg使用到编码时要使用到libx264,通过FFmpeg+libx264可实现视频编码效果。下面就是各个...

  • FFmpeg+x264编译

    在FFmpeg使用到编码时要使用到libx264,通过FFmpeg+libx264可实现视频编码效果。下面就是各个...

  • FFmpeg h264编码

    通过FFmpeg进行h264的编码流程:1.通过名字(h264编码器名字是libx264)查找编码器:avcode...

  • FFmpeg流媒体(5)

    FFmpeg推多路流 1、管道方式输出多路流 如上,音频编码为AAC,视频编码为libx264,输出格式为FLV,...

  • LibX264编码库

    H264简介 H.264从1999年开始,到2003年形成草案,最后在2007年定稿有待核实。在ITU的标准里称为...

网友评论

    本文标题:libx264 编码示例

    本文链接:https://www.haomeiwen.com/subject/udvywqtx.html