ios ffmpeg h264 encode

47 篇文章 0 订阅
18 篇文章 0 订阅

简单封装了下,ffmepg需要包含libx264。

#import "FFMpegAVCEncode.h"

#ifdef __cplusplus
extern "C" {
#endif
    
#include <libavutil/opt.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
    
#ifdef __cplusplus
};
#endif


@implementation FFMpegAVCEncode
{
    AVCodecContext                      *_pCodecCtx;
    AVCodec                             *_pCodec;
    AVPacket                             _pkt;
    uint8_t                             *_pictureBuf;
    AVFrame                             *_pFrame;
    int                                  _ySize;
    int                                  _frameCnt;
    
    int                                  _frameWidth; // 编码的图像宽度
    int                                  _frameHeight; // 编码的图像高度
}



- (int)initEncoder:(int)width height:(int)height bitrate:(int)bitrate
{
    _frameCnt = 0;
    _frameWidth = width;
    _frameHeight = height;
    
    avcodec_register_all();

    enum AVCodecID codec_id=AV_CODEC_ID_H264;
    _pCodec = avcodec_find_encoder(codec_id);
    if (!_pCodec) {
        printf("Codec not found\n");
        return -1;
    }
    _pCodecCtx = avcodec_alloc_context3(_pCodec);
    if (!_pCodecCtx) {
        printf("Could not allocate video codec context\n");
        return -1;
    }

    _pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
    _pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
    _pCodecCtx->width = _frameWidth;
    _pCodecCtx->height = _frameHeight;
    _pCodecCtx->time_base.num = 1;
    _pCodecCtx->time_base.den = 15;
    _pCodecCtx->bit_rate = bitrate;
    _pCodecCtx->gop_size = 10;
    // H264
    // pCodecCtx->me_range = 16;
    // pCodecCtx->max_qdiff = 4;
    // pCodecCtx->qcompress = 0.6;
    _pCodecCtx->qmin = 10;
    _pCodecCtx->qmax = 51;
    
    // Optional Param
    _pCodecCtx->max_b_frames=1;
    
    // Set Option
    AVDictionary *param = 0;
    
    // H.264
    if(_pCodecCtx->codec_id == AV_CODEC_ID_H264) {
        
        av_dict_set(¶m, "preset", "slow", 0);
        av_dict_set(¶m, "tune", "zerolatency", 0);
        // av_dict_set(¶m, "profile", "main", 0);
    }
    
    
    if (!_pCodec) {
        printf("Can not find encoder! \n");
        return -1;
    }
    
    int ret = 0;
    if ((ret = avcodec_open2(_pCodecCtx, _pCodec,¶m)) < 0) {
        printf("Failed to open encoder! \n");
        return -1;
    }
    
    _pFrame = av_frame_alloc();
    avpicture_fill((AVPicture *)_pFrame, _pictureBuf, _pCodecCtx->pix_fmt, _pCodecCtx->width, _pCodecCtx->height);
    
    _ySize = _pCodecCtx->width * _pCodecCtx->height;
    
    return 0;
}


-(void)encodeBufToH264:(char *)yuv420p{
    _pictureBuf = yuv420p;
    _pFrame->data[0] = _pictureBuf;              // Y
    _pFrame->data[1] = _pictureBuf+ _ySize;      // U
    _pFrame->data[2] = _pictureBuf+ _ySize*5/4;  // V
    
    // PTS
    _pFrame->pts = _frameCnt;
    int got_picture = 0;
    
    // Encode
    _pFrame->width = _frameWidth;
    _pFrame->height = _frameHeight;
    _pFrame->format = AV_PIX_FMT_YUV420P;
    
    int ret = avcodec_encode_video2(_pCodecCtx, &_pkt, _pFrame, &got_picture);
    if(ret < 0) {
        printf("Failed to encode! \n");
    }
    if (got_picture==1) {
        printf("Succeed to encode frame: %5d\tsize:%5d\n", _frameCnt, _pkt.size);
        //TODO:h264数据回调
        _frameCnt++;
        av_free_packet(&_pkt);
    }
}

- (void)free
{
    //Flush Encoder
    int ret = [self flushEncoder];
    if (ret < 0) {
        
        printf("Flushing encoder failed\n");
    }
    avcodec_close(_pCodecCtx);
    av_free(_pCodecCtx);
    av_freep(&_pFrame->data[0]);
    av_frame_free(&_pFrame);
}

-(int)flushEncoder
{
    int ret;
    int got_frame;
    AVPacket enc_pkt;
    
    while (1) {
        enc_pkt.data = NULL;
        enc_pkt.size = 0;
        av_init_packet(&enc_pkt);

        ret = avcodec_encode_video2(_pCodecCtx, &enc_pkt, NULL, &got_frame);
        av_frame_free(NULL);
        if (ret < 0)
            break;
        if (!got_frame){
            ret=0;
            break;
        }
        //TODO:h264数据回调
        printf("Flush Encoder: Succeed to encode 1 frame!\tsize:%5d\n",enc_pkt.size);
    }
    return ret;
}

@end


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值