用于录制电影的iOS缩放

时间:2015-03-04 12:54:03

标签: ios avfoundation avassetwriter core-media

我正在使用AVAssetWriter来录制电影帧。

我想知道如何缩放/缩放一个帧(它可以在电影的开头设置,它不必在每帧的基础上......)

我认为我应该可以使用CMSampleBuffer附加的assetWriterInput来做到这一点

Psydocode:

CMSampleBufferRef scaledSampleBuffer = [self scale:sampleBuffer];
[_videoWriterInput appendSampleBuffer:scaledSampleBuffer];

我在此发现了一些stackoverflow问题:

这个应该创建一个vImage_Buffer,但我不知道如何将其添加到我的_videoWriterInput中:

Stackoverflow

我试图将其转换回来:

    NSInteger cropX0 = 100,
        cropY0 = 100,
        cropHeight = 100,
        cropWidth = 100,
        outWidth = 480,
        outHeight = 480;

    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    CVPixelBufferLockBaseAddress(imageBuffer,0);
    void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);

    vImage_Buffer inBuff;
    inBuff.height = cropHeight;
    inBuff.width = cropWidth;
    inBuff.rowBytes = bytesPerRow;

    NSInteger startpos = cropY0*bytesPerRow+4*cropX0;
    inBuff.data = baseAddress+startpos;

    unsigned char *outImg= (unsigned char*)malloc(4*outWidth*outHeight);
    vImage_Buffer outBuff = {outImg, outHeight, outWidth, 4*outWidth};

    vImage_Error err = vImageScale_ARGB8888(&inBuff, &outBuff, NULL, 0);
    if (err != kvImageNoError) NSLog(@" error %ld", err);


    CVPixelBufferUnlockBaseAddress(imageBuffer,0);

    CVPixelBufferRef pixelBuffer;
    CVPixelBufferCreate(kCFAllocatorSystemDefault, 480, 480, kCVPixelFormatType_32BGRA, NULL, &pixelBuffer);

    CVPixelBufferLockBaseAddress( pixelBuffer, 0 );

    vImage_CGImageFormat format = {
        .bitsPerComponent = 8,
        .bitsPerPixel = 32,
        .bitmapInfo = kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst,  //BGRX8888
        .colorSpace = NULL,    //sRGB
    };

    vImageBuffer_CopyToCVPixelBuffer(&outBuff,
                                     &format,
                                     pixelBuffer,
                                     NULL,
                                     NULL,
                                     kvImageNoFlags);


    CVPixelBufferUnlockBaseAddress( pixelBuffer, 0 );

    CMSampleTimingInfo sampleTime = {
        .duration = CMSampleBufferGetDuration(sampleBuffer),
        .presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer),
        .decodeTimeStamp = CMSampleBufferGetDecodeTimeStamp(sampleBuffer)
    };

    CMVideoFormatDescriptionRef videoInfo = NULL;
    CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault, pixelBuffer, &videoInfo);

    CMSampleBufferRef oBuf;
    CMSampleBufferCreateForImageBuffer(kCFAllocatorDefault, pixelBuffer, true, NULL, NULL, videoInfo, &sampleTime, &oBuf);

但这也不起作用(损坏的帧)

从其他Stackoverflow文章复制到一起,我想出了这段代码,但它会导致视频损坏:

    CGAffineTransform transform = CGAffineTransformMakeScale(2, 2); // zoom 2x

    CIImage *ciImage = [CIImage imageWithCVPixelBuffer:CMSampleBufferGetImageBuffer(sampleBuffer)
                                               options:[NSDictionary dictionaryWithObjectsAndKeys:[NSNull null], kCIImageColorSpace, nil]];
    ciImage = [[ciImage imageByApplyingTransform:transform] imageByCroppingToRect:CGRectMake(0, 0, 480, 480)];

    CVPixelBufferRef pixelBuffer;
    CVPixelBufferCreate(kCFAllocatorSystemDefault, 480, 480, kCVPixelFormatType_32BGRA, NULL, &pixelBuffer);

    CVPixelBufferLockBaseAddress( pixelBuffer, 0 );

    CIContext * ciContext = [CIContext contextWithOptions: nil];
    [ciContext render:ciImage toCVPixelBuffer:pixelBuffer];
    CVPixelBufferUnlockBaseAddress( pixelBuffer, 0 );

    CMSampleTimingInfo sampleTime = {
        .duration = CMSampleBufferGetDuration(sampleBuffer),
        .presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer),
        .decodeTimeStamp = CMSampleBufferGetDecodeTimeStamp(sampleBuffer)
    };

    CMVideoFormatDescriptionRef videoInfo = NULL;
    CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault, pixelBuffer, &videoInfo);

    CMSampleBufferRef oBuf;
    CMSampleBufferCreateForImageBuffer(kCFAllocatorDefault, pixelBuffer, true, NULL, NULL, videoInfo, &sampleTime, &oBuf);

有什么想法吗?

0 个答案:

没有答案