iOS中摄像头录像并输出x265视频

今天尝试了一下使用开源的X265 Library在iOS设备上进行编码,实际结果不尽人意,效率十分渣,当然这有很大一部分因为是本人并没有进行任何效率上的优化,但是按照本人手上的iPad Air2的转码效率来看,感觉想做到实时进行高清晰度的x265软编码直播之类的感觉很难实现,不过辛苦了一天还是把过程mark下来吧。

下载x265 library源码

下载链接

编译出iOS能使用的x265 library

在网上找了下,基本上是木有iOS的编译方法,不过把x265源码下载下来之后,在源码的build目录下可以找到一个XCode的文件夹,文件夹下有一个make-project.sh的文件,该文件是官方用于创建Mac OS下使用的libx265库的XCode项目的脚本,因为我对cmake并不是太了解,网上又木有找到相关资料的情况下,只能参考着Mac OS项目创建一个iOS的项目来编译了。

iOS中摄像头录像并输出x265视频_第1张图片
1.png

只需要在文件目录下使用终端运行脚本,就可以生成Mac OS使用的XCode项目。

chmod +x make-project.sh
./make-prioject.sh

然后按照着该项目包含的h、m文件,创建了一个拥有相同h、m文件的iOS项目,实际就是把x265 Mac OS项目中包含的h、m文件都复制到iOS项目下并引入iO
S项目,同时在对应的Target中,Copy Files增加x265.h和x265_config.h文件,这两个文件需要暴露出来,在使用的时候主要是使用x265.h这个文件的方法,同时增加一个.pch文件,在Mac OS项目Build Setting中的预编译宏设置中有一些参数的设置,iOS项目这边我把它搬到.pch这里来了,看参数意义感觉有一些是有用的,有一些应该没啥用,不过我也照搬过来了,最后只要准确参照着Mac OS项目的文件内容,它有的我们iOS项目也有,它没的我们也别加进来,那么这个iOS项目是能够编译成功的,编译的时候记得选择Generic iOS Device,并且在真机使用。

iOS中摄像头录像并输出x265视频_第2张图片
2.png
iOS中摄像头录像并输出x265视频_第3张图片
3.png
4.png

将编译好的x265 library加入项目中

我又创建了一个新的iOS项目,用于实验这个文章主题,然后我们就引入上面编译出来的x265 library到这个项目吧,直接拖进来,然后增加一个libstdc++的库引用,x265库用到的。

iOS中摄像头录像并输出x265视频_第4张图片
5.png

图中的CameraOutpuxX265的项目就是我创建用于实验文章主题的iOS项目,时间问题,我就只是简单的创建了一个Single View Application的项目,XCode->New->iOS->Single View Application,然后拖入编译好的x265 library,图中框着的x265-iOS就是我编译好的x265库,然后Linked Frameworks and Libraries那增加一个libstdc++库,因为x265库是用c++编写的,基本的c++ std库要加上。

准备的东西都准备好了,开始具体的实现了...

从iOS设备的摄像头获取视频源数据

如何获取摄像头的数据,这个文章有很多,我就贴贴代码,代码整个复制黏贴到新建的项目的ViewController.m下,然后在Main.Storyboard中的ViewController上增加一个View,这个View控件与ViewController.m中的viewCapture连接上,项目跑起来,应该就能看到摄像头拍摄的内容了。

ViewController.m

@interface ViewController ()
@property (nonatomic,weak) IBOutlet UIView *viewCapture;
@property (nonatomic,strong) AVCaptureVideoPreviewLayer *captureVideoPreviewLayer;
@property (nonatomic,strong) AVCaptureSession *captureSession;
@property (nonatomic,strong) AVCaptureConnection *captureVideoConnection;

...
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initCapture];
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self start];
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [self stop];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initCapture
{
    self.captureSession = [[AVCaptureSession alloc] init];
    
    AVCaptureDevice* inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:nil];
    [self.captureSession addInput:captureInput];
    
    AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc] init];
    [captureOutput setAlwaysDiscardsLateVideoFrames:YES];
    [captureOutput setSampleBufferDelegate:self queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)];
    
    NSString* key = (NSString *)kCVPixelBufferPixelFormatTypeKey;
    NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange];//Pixel Format NV12
    NSDictionary *videoSettings = [NSDictionary dictionaryWithObject:value forKey:key];
    [captureOutput setVideoSettings:videoSettings];
    
    [self.captureSession setSessionPreset:AVCaptureSessionPreset352x288];
    [self.captureSession addOutput:captureOutput];
    
    [self setCaptureVideoConnection:[captureOutput connectionWithMediaType:AVMediaTypeVideo]];
    
    [self setCaptureVideoPreviewLayer:[AVCaptureVideoPreviewLayer layerWithSession:self.captureSession]];
    [self.captureVideoPreviewLayer setFrame:self.view.bounds];
    [self.captureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspect];
    [self.captureVideoPreviewLayer connection];
    [self.viewCapture.layer addSublayer:self.captureVideoPreviewLayer];
}

-(void)start
{
    [self.captureSession startRunning];
}

-(void)stop
{
    [self.captureSession stopRunning];
}

#pragma mark - AVCaptureVideoDataOutputSampleBufferDelegate

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
    if (connection == self.captureVideoConnection) {
        
        CVPixelBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
        
        if (CVPixelBufferLockBaseAddress(imageBuffer, 0) == kCVReturnSuccess) {
            //这里就是实时获取到的摄像头源数据
        }
        
        CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
    }
}
@end

将摄像头获取到的源数据转为x265可进行编码压缩的源数据

在这里要说的是,x265这个库的使用,我是学习以下文章的
最简单的视频编码器:基于libx265(编码YUV为H.265)

  • 啥是NV12,这个另外自己百度了,因为我不是专门搞视频的,开始也不知道,专门百度了下,当然你还可以Google、Bing、搜狗...反正容易点来说,iOS摄像头录下的每一帧原始数据,可以指定的几种类型当中,包括这种NV12的,这编文章使用的就是这种NV12

  • 啥是yuv420,这个也继续自己搜索看看吧,简单来说就是x265 library这个库进行编码压缩原数据的时候,可输入处理的几种原数据中的一种,这编文章就选用这种格式了,因为我看到上面推荐的文章用的这个,其他我也不会...

接下来我们主要关注在上面ViewController.m代码的AVCaptureVideoDataOutputSampleBufferDelegate中:

#pragma mark - AVCaptureVideoDataOutputSampleBufferDelegate

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
    if (connection == self.captureVideoConnection) {
        
        CVPixelBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
        
        if (CVPixelBufferLockBaseAddress(imageBuffer, 0) == kCVReturnSuccess) {
            //这里就是实时获取到的摄像头源数据
        }
        
        CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
    }
}

在获取到视频源数据之后,我们着手要做的就是把摄像头获取到的数据(NV12数据)转为x265库能进行编码压缩的源数据(yuv420),从摄像头直接拿下来的数据是不能直接交给x265开源库来处理的,所以增加以下方法:

-(NSData*)convertYUV420FromNV12ImageBuffer:(CVPixelBufferRef)imageBuffer {
    
    UInt8 *bufferbasePtr = (UInt8 *)CVPixelBufferGetBaseAddress(imageBuffer);
    UInt8 *bufferPtr = (UInt8 *)CVPixelBufferGetBaseAddressOfPlane(imageBuffer,0);
    UInt8 *bufferPtr1 = (UInt8 *)CVPixelBufferGetBaseAddressOfPlane(imageBuffer,1);
    size_t buffeSize = CVPixelBufferGetDataSize(imageBuffer);
    size_t width = CVPixelBufferGetWidth(imageBuffer);
    size_t height = CVPixelBufferGetHeight(imageBuffer);
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
    size_t bytesrow0 = CVPixelBufferGetBytesPerRowOfPlane(imageBuffer,0);
    size_t bytesrow1  = CVPixelBufferGetBytesPerRowOfPlane(imageBuffer,1);
    size_t bytesrow2 = CVPixelBufferGetBytesPerRowOfPlane(imageBuffer,2);
    size_t yuv420_len = sizeof(UInt8) * width * height * 3 / 2;
    UInt8 *yuv420_data = malloc(yuv420_len); // buffer to store YUV with layout YYYYYYYYUUVV
    
    /* convert NV12 data to YUV420*/
    UInt8 *pY = bufferPtr ;
    UInt8 *pUV = bufferPtr1;
    UInt8 *pU = yuv420_data + width * height;
    UInt8 *pV = pU + width * height / 4;
    for(int i = 0; i < height; i++)
    {
        memcpy(yuv420_data + i * width, pY + i * bytesrow0, width);
    }
    for(int j = 0; j < height / 2; j++)
    {
        for(int i =0; i < width / 2; i++)
        {
            *(pU++) = pUV[i << 1];
            *(pV++) = pUV[(i << 1) + 1];
        }
        pUV += bytesrow1;
    }
    
    NSData *yuv420Frame = [NSData dataWithBytes:yuv420_data length:yuv420_len];
    
    free(yuv420_data);
    
    return yuv420Frame;
}

可以看到,这个方法就是把从摄像头获取到imageBuffer转为yuv420格式的数据,并封装为一个iOS的NSData的方法。

好了,视频数据也准备好了,接下来我们来初始化x265库。

x265 初始化

直接贴代码,增加两个属性,因为都不是OC对象,所以我给了他们assign修饰词,在ViewController的delloc方法中,记得要对其进行release操作,主要关注x265Param和x265Encoder,x265Param时设置编码的一些参数的,更具体的可以进到x265Param的头文件看看属性说明,x265Encoder就是我们的编码器啦,下面是具体初始化编码器的我方法,东西不多,直接看代码就能明白。

  • 其实这些代码一直都是ViewController.m文件下的代码,只是为了关注我说明的内容,我贴出来的都是删除掉了上面说到其他内容的代码的
@interface ViewController ()

...
@property (strong) NSMutableArray *yuv420Frames;//用于保存从摄像头获取到并转为yuv420的数组
@property (strong) NSMutableData *dataX265;//用于保存从yuv420数据转为x265视频数据

@property (nonatomic,assign) x265_param *x265Param;
@property (nonatomic,assign) x265_encoder *x265Encoder;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initData];
    ...
}

-(void)initData {
    
    [self setYuv420Frames:[NSMutableArray array]];
    [self setDataX265:[NSMutableData data]];
    
    //根据视频实际分辨率设定
    int width = 352;
    int height = 288;
    
    self.x265Param = x265_param_alloc();
    x265_param_default(self.x265Param);
    self.x265Param->bRepeatHeaders = 1;
    self.x265Param->internalCsp = X265_CSP_I420;//输入yuv420格式
    self.x265Param->sourceWidth = width;
    self.x265Param->sourceHeight = height;
    self.x265Param->fpsNum = 18;
    self.x265Param->fpsDenom = 1;
    
    self.x265Encoder = x265_encoder_open(self.x265Param);
}

-(void)dealloc {
    
    if (self.x265Encoder) {
        x265_encoder_close(self.x265Encoder);
    }
    
    if (self.x265Param) {
        x265_param_free(self.x265Param);
    }
}

...
@end

使用x265进行转码

继续贴代码,这个方法就是把yuv420的数据通过x265Encoder进行转码,并且appendData到self.dataX265中,实际流程上来说 摄像头获取到的数据->转yuv420数据->转x265数据,方法如下:

-(void)encodeX265FromYuv420Frame:(NSData*)yuv420Frame {
    
    UInt8 *yuv420_buf = (UInt8*)yuv420Frame.bytes;
    size_t yuv420_len = yuv420Frame.length;
    
    //encode x265
    x265_picture *x265Pic = NULL;
    char *x265PicBuf = NULL;
    
    int width = self.x265Param->sourceWidth;
    int height = self.x265Param->sourceHeight;
    int pixeSize = width * height;
    
    x265Pic = x265_picture_alloc();
    x265_picture_init(self.x265Param, x265Pic);
    
    x265PicBuf = malloc(sizeof(char) * pixeSize * 3 / 2);
    x265Pic->planes[0] = x265PicBuf;
    x265Pic->planes[1] = x265PicBuf + pixeSize;
    x265Pic->planes[2] = x265PicBuf + pixeSize * 5 / 4;
    x265Pic->stride[0] = width;
    x265Pic->stride[1] = width / 2;
    x265Pic->stride[2] = width / 2;
    
    memcpy(x265Pic->planes[0], yuv420_buf, pixeSize);
    memcpy(x265Pic->planes[1], yuv420_buf + pixeSize, pixeSize / 4);
    memcpy(x265Pic->planes[2], yuv420_buf + pixeSize * 5 / 4, pixeSize / 4);
    
    x265_nal *x265NalPp = NULL;
    uint32_t x265NalPi = 0;
    x265_encoder_encode(self.x265Encoder, &x265NalPp, &x265NalPi, x265Pic, NULL);
    
    for (int i = 0; i < x265NalPi; i++) {
        
        uint8_t* payload = x265NalPp[i].payload;
        uint32_t sizeBytes = x265NalPp[i].sizeBytes;
        [self.dataX265 appendBytes:payload length:sizeBytes];
    }
    
    x265_encoder_encode(self.x265Encoder, &x265NalPp, &x265NalPi, NULL, NULL);
    
    for (int i = 0; i < x265NalPi; i++) {
        
        uint8_t* payload = x265NalPp[i].payload;
        uint32_t sizeBytes = x265NalPp[i].sizeBytes;
        [self.dataX265 appendBytes:payload length:sizeBytes];
    }
    
    x265_picture_free(x265Pic);
    free(x265PicBuf);
}

转出来的就是x265的每一帧的数据啦,把数据拼接起来输出保存为文件,就是我们的x265视频了。

这个实验项目我上传到了Github,下面是地址:
https://github.com/ljx09195117/CameraOutoutX265-iOS

x265的编译项目我就不上传了,希望能找到更标准的编译方法,这次就写这么多吧~

你可能感兴趣的:(iOS中摄像头录像并输出x265视频)