赞
踩
iOS音视频开发相关文章:
音视频最强大的开源库非ffmpeg莫属,网上对ffmpeg总结的最好的是雷神的博客(http://blog.csdn.net/leixiaohua1020/article/details/15811977),本文简单介绍下ffmpeg视频解码的使用。
1、ffmpeg初始化
- - (void)videoDecoder_init {
-
- avcodec_register_all();
- // _videoFrame = av_frame_alloc();
- AVCodec *codec = avcodec_find_decoder(AV_CODEC_ID_H264);
- _videoCodecCtx = avcodec_alloc_context3(codec);
- int ret = avcodec_open2(_videoCodecCtx, codec, nil);
- if (ret != 0){
- NSLog(@"open codec failed :%d",ret);
- }
-
- _videoFrame = av_frame_alloc();
- av_init_packet(&_packet);
- }
_videoCodecCtx是ffmpeg编解码对象;
_videoFrame是解码后的图像帧,可从中生成image图像;
_packet是解码前的数据帧,包括I帧、P帧等
2、解码操作
- - (CGSize)videoDecoder_decodeToImage:(uint8_t *)nalBuffer size:(int)inSize {
-
- _packet.size = inSize;
- _packet.data = nalBuffer;
-
- CGSize frameSize = {0, 0};
-
- while (inSize > 0) {
-
- int gotframe = 0;
- int len = avcodec_decode_video2(_videoCodecCtx,
- _videoFrame,
- &gotframe,
- &_packet);
-
- if (len < 0) {
- NSLog(@"decode video error, skip packet");
- return frameSize;
- }
-
- inSize -= len;
- }
- frameSize.width = _videoCodecCtx->width;
- frameSize.height = _videoCodecCtx->height;
-
- _outputWidth = _videoCodecCtx->width;
- self.outputHeight = _videoCodecCtx->height;
-
- return frameSize;
- }
- - (UIImage *)currentImage {
-
- if (!_videoFrame->data[0]) {
- return nil;
- }
-
- [self convertFrameToRGB];
- return [self imageFromAVPicture:_picture width:_outputWidth height:_outputHeight];
- }
-
- - (void)convertFrameToRGB {
-
- sws_scale(_img_convert_ctx, (const uint8_t * const*)_videoFrame->data, _videoFrame->linesize, 0, _videoCodecCtx->height, _picture.data, _picture.linesize);
- }
-
- - (UIImage *)imageFromAVPicture:(AVPicture)pict width:(int)width height:(int)height {
-
- CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
- CFDataRef data = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, pict.data[0], pict.linesize[0] * height,kCFAllocatorNull);
- CGDataProviderRef provider = CGDataProviderCreateWithCFData(data);
- CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
- CGImageRef cgImage = CGImageCreate(width,
- height,
- 8,
- 24,
- pict.linesize[0],
- colorSpace,
- bitmapInfo,
- provider,
- NULL,
- YES,
- kCGRenderingIntentDefault);
- CGColorSpaceRelease(colorSpace);
- UIImage *image = [[UIImage alloc]initWithCGImage:cgImage];
-
- CGImageRelease(cgImage);
- CGDataProviderRelease(provider);
- CFRelease(data);
-
- return image;
- }
附上之前参考的ffmpeg解码播放的例子:ffmpeg解码播放:https://github.com/durfu/DFURTSPPlayer
另外,欢迎大家加入iOS音视频开发的QQ群:331753091
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。