ios ffmpeg h264 encode

来源:互联网 发布:数据库实现教材管理 编辑:程序博客网 时间:2024/05/18 02:11

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

#import "FFMpegAVCEncode.h"#ifdef __cplusplusextern "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


原创粉丝点击