织梦CMS - 轻松建站从此开始!

罗索

x264源码解析

落鹤生 发布于 2012-02-23 09:14 点击:次 
由main函数进入参数设置后开始Encode函数。。。这里只把Encode函数体解析下,借鉴了很多网上资源(即使不全是自己原话也是亲手打字上去的哦^_^),感谢网友。
TAG:

由main函数进入参数设置后开始Encode函数。。。这里只把Encode函数体解析下,借鉴了很多网上资源(即使不全是自己原话也是亲手打字上去的哦^_^),感谢网友。

    代码似懂非懂的注释了下,尽当资源存储吧吧,暂且发到博客大家讨论批评。

static int Encode( x264_param_t *param, cli_opt_t *opt )

{

    x264_t *h; //还不知道干啥的,这个结构也很烦,不压x264_param_t

    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_update_interval;

    char    buf[200];

 

    opt->b_progress &= param->i_log_level < X264_LOG_DEBUG;

    i_frame_total = p_get_frame_total( opt->hin );

    i_frame_total -= opt->i_seek;

    if( ( i_frame_total == 0 || param->i_frame_total < i_frame_total )

        && param->i_frame_total > 0 )

        i_frame_total = param->i_frame_total;

    param->i_frame_total = i_frame_total;

     //上面这段代码是实现,计算文件中的总共的帧数,并根据输入的参数初始帧的位置,

     //对i_frame_total做出修正,i_frame_total -= opt->i_seek,然后再根据param->i_frame_total,

     //对i_frame_total做出进一步的修正。

     //总体来说,就是对参数设置中的进行编码的帧数的总数进行修正和计算。

    i_update_interval = i_frame_total ? x264_clip3( i_frame_total / 1000, 1, 10 ) : 10;

    if( ( h = x264_encoder_open( param ) ) == NULL )//关键函数:x264_encoder_open( param ) 根据参数要求对encoder进行一系列的初始化,例如分配内存,值的初始化等。

    {

        …… ……(略)

    }

 

    if( p_set_outfile_param( opt->hout, param ) )//关键函数:p_set_outfile_param() 设置输出文件格式

    {

        …… ……(略)

    }

 

    /* Create a new pic */

     //关键函数:x264_picture_alloc() 按照色度空间分配内存,并返回内存的首地址作为指针

    if( x264_picture_alloc( &pic, X264_CSP_I420, param->i_width, param->i_height ) < 0 )

    {

        …… ……(略)

    }

 

     //关键函数:x264_mdate()     用于编码用时的计算,设定起始时间

    i_start = x264_mdate();

 

    /* Encode frames */

    for( i_frame = 0, i_file = 0; b_ctrl_c == 0 && (i_frame < i_frame_total || i_frame_total == 0); )

    {

         //p_read_frame() 按照h->hin提供的输入文件的地址,读入图像的内容到&pic提供的存储区的首地址

        if( p_read_frame( &pic, opt->hin, i_frame + opt->i_seek ) )

            break;

 

        pic.i_pts = (int64_t)i_frame * param->i_fps_den;

 

        if( opt->qpfile )

            parse_qpfile( opt, &pic, i_frame + opt->i_seek );//parse_qpfile() 为从指定的文件中读入qp的值留下的接口,qpfile为文件的首地址

        else

        {

            /* Do not force any parameters */

            pic.i_type = X264_TYPE_AUTO;

            pic.i_qpplus1 = 0;

        }

 

        i_frame_size = Encode_frame( h, opt->hout, &pic );//************核心程序

        …… ……(略)

 

        /* update status line (up to 1000 times per input file) */

         //用于显示整个编码过程的进度

        if( opt->b_progress && i_frame % i_update_interval == 0 )

        {

            int64_t i_elapsed = x264_mdate() - i_start;//编码使用的时间计算

            //帧率的计算

            double fps = i_elapsed > 0 ? i_frame * 1000000. / i_elapsed : 0;

            double bitrate = (double) i_file * 8 * param->i_fps_num / ( (double) param->i_fps_den * i_frame * 1000 );

             …… ……(略)

 

            SetConsoleTitle( buf );

            fflush( stderr ); // needed in windows

        }

    }

    ……   ………………

    // 后边的除了清理工作,其他的还不知道。

    return 0;

}

(zorru)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201202/15749.html]
本文出处:百度博客 作者:zorru 原文
顶一下
(1)
100%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容