C - x264编码demo

以前的笔记转存到这里。

// x264t.cpp : Defines the entry point for the console application.
//

#include "stdint.h"

#ifndef _DEBUG
#pragma comment(lib, "libx264.lib")
#else
#pragma comment(lib, "libx264d.lib")
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "x264.h"


typedef char bool;
#define false 0;
#define true 1;

bool x264_encode_frame(x264_t *h,x264_param_t *param,
                                           x264_picture_t* pic_in,
                                           uint8_t* data_out,int* data_out_len,
                                           bool* key_frame);

int main(int argc, char* argv[])
{
        char *in_file = argv[1];
        char *out_file = argv[2];
        FILE  *fp_in;
        FILE  *fp_out;

        int width, height, frame_size, plane0_size,plane1_size,plane2_size;

        x264_param_t param;
        x264_t *h;
        x264_picture_t pic;
        uint8_t *buffer;
        int  actual_read_size;
        bool key_frame;

        if(argc >= 5)
        {
                sscanf(argv[3],"%dx%d",&width,&height);
        plane0_size = plane1_size = plane2_size = height*width;

                if(strcmp(argv[4],"444") == 0)
                {
                        param.i_csp = X264_CSP_I444;
                        x264_picture_alloc( &pic, X264_CSP_I444, width, height );
                }
                else if(strcmp(argv[4],"422") == 0)
                {
                        param.i_csp = X264_CSP_I422;
                        plane1_size = plane2_size = plane0_size/2;
                        x264_picture_alloc( &pic, X264_CSP_I422, width, height );
                }
                else /*if(strcmp(argv[4],"420") == 0)*/
                {
                        param.i_csp = X264_CSP_I420;
                        plane1_size = plane2_size = plane0_size/4;
                        x264_picture_alloc( &pic, X264_CSP_I420, width, height );
                }

                frame_size = plane0_size + plane1_size + plane2_size;;
                buffer = (uint8_t*)malloc(frame_size);

                x264_param_default( ¶m );
                //设置编码参数
                param.i_threads = 8;
                param.i_width = width;
                param.i_height = height;
                param.i_bframe=3;        
                param.i_frame_reference=3;
                param.rc.i_rc_method=X264_RC_CQP ;
                param.i_csp = X264_CSP_I420;
                param.b_visualize=1;
                param.i_log_level=X264_LOG_NONE;
                param.pf_log = printf;

                if( ( h = x264_encoder_open(¶m ) ) == NULL )
                {
                        return -1;
                }

                if((fp_in =fopen(in_file,"rb")) == NULL)
                {
                        puts("打开输入文件失败");
                        return -1;
                }

                if((fp_out =fopen(out_file,"wb")) == NULL)
                {
                        puts("打开输出文件失败");
                        return -1;
                }

                do
                {
                        actual_read_size = fread(buffer,frame_size,1,fp_in);

                        if(actual_read_size == 1)
                        {
                                actual_read_size= frame_size;
                                memcpy(pic.img.plane[0], buffer, plane0_size);
                                memcpy(pic.img.plane[1], buffer + plane0_size, plane1_size);
                                memcpy(pic.img.plane[2], buffer + plane0_size + plane1_size, plane2_size);

                                pic.i_pts = (int64_t)param.i_frame_total * param.i_fps_den;

                                x264_encode_frame(h,¶m,&pic,buffer,&actual_read_size,&key_frame);

                                fwrite(buffer,actual_read_size,1,fp_out);
                                flushall();
                        }
                        else
                        {
                                fseek( fp_in, 0, SEEK_SET );
                        }

                        param.i_frame_total ++;

                }while(1);


                x264_picture_clean( &pic );
                x264_encoder_close( h );
                free(buffer);
                fclose(fp_out);
                fclose(fp_in);
        }
        else
        {
                printf("%s","参数错误!");
        }
        return 0;
}

bool x264_encode_frame(x264_t *h, x264_param_t *param, x264_picture_t* pic_in, uint8_t* data_out,int* data_out_len, bool* key_frame )
{
        x264_picture_t pic_out;
        x264_nal_t *nal;
        int i_nal,i;
        int i_size = 0;
        int data_out_l = *data_out_len;
        *data_out_len = 0;

        if( x264_encoder_encode( h, &nal, &i_nal, pic_in, &pic_out ) < 0 )
        {
                return false;
        }

        for(i = 0; i < i_nal; i++ )
        {
                i_size = 0;
                if( ( i_size = x264_nal_encode( data_out + *data_out_len, &data_out_l, 1, &nal[i] ) ) > 0 )
                {
                        *data_out_len += i_size;
                        data_out_l -= i_size;
                }
        }

        if(pic_out.i_type==X264_TYPE_IDR)
                *key_frame = true;

        return true;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值