x264编码后的文件保存

来源:互联网 发布:哑舍赤龙服淘宝 编辑:程序博客网 时间:2024/05/13 02:19

方法1:

在x264示例中的保存方法:

        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, stdout ) )
                goto fail;
        }


也就是说:i_frame_size :表示编码后的数据大小;

                   nal->p_payload:表示数据内容;


方法2:

x264_encoder_encode的函数注释:

/* x264_encoder_encode:
 *      encode one picture.
 *      *pi_nal is the number of NAL units outputtedin pp_nal.
 *      returns negative on error, zero if no NAL units returned.
 *      the payloads of all output NALs are guaranteed to be sequential in memory. */


int     x264_encoder_encode( x264_t *, x264_nal_t **pp_nal, int *pi_nal, x264_picture_t *pic_in, x264_picture_t *pic_out );

上面注释中有两句话比较重要:

1:

 *pi_nal is the number of NAL units outputted in pp_nal. :

表示 *pi_nal 是int类型,记录了当前编码后NAL数目;编码后的NAL保存在pp_nal中;

pp_nal 的类型是x264_nal_t **pp_nal,表示是指向指针的指针,也就是pp_nal是指向x264_nal_t链表的指针,每一个*pp_nal中保存一个NAL(也就是一个x264_nal_t 结构体);


2:the payloads of all output NALs are guaranteed to be sequential in memory;

这句话表示,NAL在内存是连续的,所以“方法1”是可行的;



方法2,是根据第一句话编写的:

        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 )

        {

               static FILE * f = fopen("encode_yuan_nal.h264","w+b"); 

for ( int i = 0; i < i_nal; i++ )
{
fwrite((nal + i)->p_payload, 1, (nal + i)->i_payload, f); 
}

        }


或者,可以简答的将 x264_nal_t **pp_nal 看做: x264_nal_t  *  pp_nal [ i_nal  ] ;


所以,这里,方法1 和 方法2 的结果是一样的,但是方法2的好处是在网络处理的时候,非常方便;



http://blog.csdn.net/chinabinlang/article/details/50249063