ios libx264 h264 encode

#import "X264Encode.h"
#include "x264.h"

#define KEY_FRAME_INTERVAL 30

typedef struct
{
    x264_param_t * param;
    x264_t *handle;
    x264_picture_t * picture;
    x264_nal_t  *nal;
} X264EncodeParam;

@interface X264Encode(){
    X264EncodeParam* _x264Param;
    char* _h264Buf;
    long _frameCnt;
}

@end


@implementation X264Encode

- (int)initEncoder:(int)width height:(int)height bitrate:(int)bitrate{
    
    _frameCnt = 0;
    _h264Buf = (char*)malloc(width*height);
    
    _x264Param = (X264EncodeParam *) malloc(sizeof(X264EncodeParam));
    _x264Param->param = (x264_param_t *) malloc(sizeof(x264_param_t));
    _x264Param->picture = (x264_picture_t *) malloc(sizeof(x264_picture_t));
    x264_param_default(_x264Param->param); //set default param
    //_x264Param->param->rc.i_rc_method = X264_RC_CQP;
    _x264Param->param->rc.i_rc_method = X264_RC_ABR ;
    _x264Param->param->rc.i_bitrate = bitrate;
    _x264Param->param->i_log_level = X264_LOG_NONE;
    _x264Param->param->i_width = width; //set frame width
    _x264Param->param->i_height = height; //set frame height
    _x264Param->param->rc.i_lookahead =0;
    _x264Param->param->i_bframe=0;
    _x264Param->param->i_fps_num =5;
    _x264Param->param->i_fps_den = 1;
    if ((_x264Param->handle = x264_encoder_open(_x264Param->param)) == 0) {
        return -1;
    }
    
    x264_nal_t* p_nal;
    int i_nal;
    x264_encoder_headers(_x264Param->handle, &p_nal, &i_nal);
    int i;
    for(i=0;i<i_nal;i++)
    {
        if(p_nal[i].i_type == NAL_SPS)
        {
            //TOTO:返回sps,p_nal[i].p_payload,p_nal[i].i_payload
        }
        if(p_nal[i].i_type == NAL_PPS)
        {
            //TOTO:返回pps,p_nal[i].p_payload,p_nal[i].i_payload
        }
    }
    
    /* Create a new pic */
    x264_picture_alloc(_x264Param->picture, X264_CSP_I420, _x264Param->param->i_width,
                       _x264Param->param->i_height);
    
    return 0;
}

- (void)encodeSampleBufToH264:(CMSampleBufferRef)sampleBuffer{
    
}

- (void)encodeBufToH264:(char*)yuv420p{

    x264_picture_t pic_out;
    int i_data=0;
    int nNal=-1;
    int len=0;
    int i=0;
    int nPicSize = _x264Param->param->i_width*_x264Param->param->i_height;
    
    memcpy(_x264Param->picture->img.plane[0],yuv420p,nPicSize);
    memcpy(_x264Param->picture->img.plane[1],yuv420p+nPicSize,nPicSize/4);
    memcpy(_x264Param->picture->img.plane[2],yuv420p+(nPicSize*5/4),nPicSize/4);
    
    if (_frameCnt % KEY_FRAME_INTERVAL == 0) {
        _x264Param->picture->i_type = X264_TYPE_IDR;
    }else{
        _x264Param->picture->i_type = X264_TYPE_AUTO;
    }
    _frameCnt++;
    
    if( x264_encoder_encode( _x264Param->handle, &(_x264Param->nal), &nNal, _x264Param->picture ,&pic_out) < 0 )
    {
        return;
    }
    
    char* pTmpOut = _h264Buf;
    for (i = 0; i < nNal; i++){
        
        memcpy(pTmpOut, _x264Param->nal[i].p_payload, _x264Param->nal[i].i_payload);
        pTmpOut += _x264Param->nal[i].i_payload;
        len += _x264Param->nal[i].i_payload;
        //TODO:回调编码数据
    }
    printf("Succeed to encode frame: %5ld\tsize:%5d\n", _frameCnt, len);
}

- (void)free{
    
    if(_x264Param->picture)
    {
        x264_picture_clean(_x264Param->picture);
        free(_x264Param->picture);
        _x264Param->picture  = NULL;
    }
    if(_x264Param->param)
    {
        free(_x264Param->param);
        _x264Param->param = NULL;
    }
    if(_x264Param->handle)
    {
        x264_encoder_close(_x264Param->handle);
    }
    if (_x264Param) {
        free(_x264Param);
        _x264Param = NULL;
    }
    if (_h264Buf) {
        free(_h264Buf);
        _h264Buf = NULL;
    }
}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值