一、前言
最近在做直播(監(jiān)控類)的項目甫菠,剛開始一竅不通挠铲,各種難啊,沒辦法寂诱,總得做啊拂苹,于是就查資料,一步一步痰洒,最后總算是做出來了瓢棒,下面就先講一下利用ffmpeg解碼H264視頻流這一塊。首先在iOS平臺配置ffmpeg就不再詳解丘喻,具體請看:
https://cnbin.github.io/blog/2015/05/19/iospei-zhi-ffmpegkuang-jia/ 這篇博客脯宿,我在項目中使用的ffmpeg版本號是3.1,我在看上篇博客配置ffmpeg時泉粉,按步驟一步一步來连霉,還是出現(xiàn)了.a文件是紅色的情況,后來我就把.a文件重新導(dǎo)入項目中搀继,總算是好了窘面,真是不易啊。
二叽躯、解碼H264視頻流
1.首先創(chuàng)建一個文件專門用來解碼柜候,在DDH264Decoder.h文件中對外暴露以下三個方法:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "libavcodec/avcodec.h"
@interface DDH264Decoder : NSObject
/* 初始化解碼器 */
- (BOOL)initH264DecoderWithWidth:(int)width height:(int)height;
/* 解碼視頻數(shù)據(jù)并且返回圖片 */
- (void)H264decoderWithVideoData:(NSData *)VideoData completion:(void (^)(AVPicture picture))completion;
/* 釋放解碼器 */
- (void)releaseH264Decoder;
2.在.m文件中實現(xiàn)所暴露的方法
#import "DDH264Decoder.h"
#import "libswscale/swscale.h"
#include <libavformat/avformat.h>
#import <AVFoundation/AVFoundation.h>
@interface DDH264Decoder ()
@property (assign, nonatomic) AVFrame *frame;
@property (assign, nonatomic) AVCodec *codec;
@property (assign, nonatomic) AVCodecContext *codecCtx;
@property (assign, nonatomic) AVPacket packet;
@property (assign, nonatomic) AVFormatContext *formatCtx;
@end
@implementation DDH264Decoder
/**
* 初始化視頻解碼器
*
* @param width 寬度
* @param height 高度
*
* @return YES:解碼成功
*/
- (BOOL)initH264DecoderWithWidth:(int)width height:(int)height {
av_register_all();
avformat_network_init();
self.codec = avcodec_find_decoder(AV_CODEC_ID_H264);
av_init_packet(&_packet);
if (self.codec != nil) {
self.codecCtx = avcodec_alloc_context3(self.codec);
// 每包一個視頻幀
self.codecCtx->frame_number = 1;
self.codecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
// 視頻的寬度和高度
self.codecCtx->width = width;
self.codecCtx->height = height;
// 打開codec
if (avcodec_open2(self.codecCtx, self.codec, NULL) >= 0) {
self.frame = av_frame_alloc();
if (self.frame == NULL) {
return NO;
}
}
}
return (BOOL)self.frame;
}
/**
* 視頻解碼
*
* @param data 被解碼視頻數(shù)據(jù)
*
* @return 圖片
*/
- (void)H264decoderWithVideoData:(NSData *)VideoData completion:(void (^)(AVPicture))completion {
@autoreleasepool {
_packet.data = (uint8_t *)VideoData.bytes;
_packet.size = (int)VideoData.length;
int getPicture;
avcodec_send_packet(_codecCtx, &_packet);
getPicture = avcodec_receive_frame(self.codecCtx, self.frame);
av_packet_unref(&_packet);
if (getPicture == 0) {
AVPicture picture;
avpicture_alloc(&picture, AV_PIX_FMT_RGB24, self.codecCtx->width, self.codecCtx->height);
struct SwsContext *img_convert_ctx = sws_getContext(self.codecCtx->width,
self.codecCtx->height,
AV_PIX_FMT_YUV420P,
self.codecCtx->width,
self.codecCtx->height,
AV_PIX_FMT_RGB24,
SWS_FAST_BILINEAR,
NULL,
NULL,
NULL);
// 圖像處理
sws_scale(img_convert_ctx, (const uint8_t* const*)self.frame->data, self.frame->linesize, 0, self.codecCtx->height, picture.data, picture.linesize);
sws_freeContext(img_convert_ctx);
img_convert_ctx = NULL;
if (completion) {
completion(picture);
}
avpicture_free(&picture);
}
}
}
/**
* 釋放視頻解碼器
*/
- (void)releaseH264Decoder {
if(self.codecCtx) {
avcodec_close(self.codecCtx);
avcodec_free_context(&_codecCtx);
self.codecCtx = NULL;
}
if(self.frame) {
av_frame_free(&_frame);
self.frame = NULL;
}
av_packet_unref(&_packet);
}
注意:使用完后,一定要釋放沈条,要不然會內(nèi)存泄漏。
3.在控制器中對解碼后的視頻數(shù)據(jù)進行處理
我是在另外一個文件中谍夭,對數(shù)據(jù)進行了另一層加工及處理,在這里只寫在控制器中對解碼后的數(shù)據(jù)進行顯示憨募。
- (void)dealAVPicture:(AVPicture)picture width:(int)width height:(int)height {
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CFDataRef refData = CFDataCreate(kCFAllocatorDefault, picture.data[0], picture.linesize[0] * height);
CGDataProviderRef refProvider = CGDataProviderCreateWithCFData(refData);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGImageRef refImage = CGImageCreate(width,
height,
8,
24,
picture.linesize[0],
colorSpace,
bitmapInfo,
refProvider,
NULL,
NO,
kCGRenderingIntentDefault);
UIImage *targetImage = [UIImage imageWithCGImage:refImage];
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
[XSLToast hideLoadingAnimation:NO inView:weakSelf.panelImageView];
weakSelf.panelImageView.image = targetImage;
});
CGColorSpaceRelease(colorSpace);
CGImageRelease(refImage);
CGDataProviderRelease(refProvider);
CFRelease(refData);
}