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
阅读全文
0 0
- ios ffmpeg h264 encode
- ios libx264 h264 encode
- ios ffmpeg h264 decode
- ios ffmpeg(libfdk-aac) aac encode
- ios ffmpeg 实时视频压缩(主要是H264)
- iOS FFmpeg实时YUV420P编码H264
- H264 encode and decode
- H264--4 encode
- yuv encode h264 test
- ffmpeg h264
- FFMpeg Encode Video
- ffmpeg encode/decode example
- H264 encode and decode overview
- H264 encode and decode overview
- H264 encode and decode overview
- h264 encode and decode success
- 来源于 原创 分类 ios 5.0 ffmpeg decode h264 stream
- ios 6.1下ffmpeg真机库带h264编码解码
- 用程序实现更新PO的价格
- ios控件——UITarBarController的简单使用
- caffe 图片格式
- linux 管道命令
- [Leetcode-94中序-144前序-145后序]【Stack】递归和非递归方式遍历二叉树
- ios ffmpeg h264 encode
- jsp生成校验码
- python学习笔记3——列表和元组
- python中如何给散点图上面的特定点做标记
- CentOS下防火墙的配置
- 数据库总结
- Banner轮播图使用
- 软件测试过程的持续改进
- Struts2入门demo(maven项目)