Skip to content

Commit

Permalink
fixed a bug
Browse files Browse the repository at this point in the history
fixed a bug that would manifest as a crash when resizing during a transcode to a hap format
  • Loading branch information
mrRay committed Apr 23, 2018
1 parent a372dd8 commit 52e2430
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 9 deletions.
6 changes: 3 additions & 3 deletions AVF Batch Converter/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleGetInfoString</key>
<string>1.6.6</string>
<string>1.6.7</string>
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundleIdentifier</key>
Expand All @@ -19,11 +19,11 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.6.6</string>
<string>1.6.7</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1.6.6</string>
<string>1.6.7</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSHumanReadableCopyright</key>
Expand Down
15 changes: 15 additions & 0 deletions AVF Batch Converter/VVAVFTranscoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,21 @@ - (void) transcodeFileAtPath:(NSString *)src toPath:(NSString *)dst {
NSString *exportCodecString = [baseVideoExportSettings objectForKey:AVVideoCodecKey];
//OSType exportCodec = (exportCodecString==nil) ? 0 : VVPackFourCC_fromChar((char *)[exportCodecString UTF8String]);
//OSType trackCodec = CMFormatDescriptionGetMediaSubType(trackFmt);

// if the export settings specify a resolution, and that resolution doesn't match this track's resolution, we need to perform the transcode step
CMFormatDescriptionRef trackFmt = (CMFormatDescriptionRef)[formatDescriptions objectAtIndex:0];
NSNumber *tmpNum = nil;
NSSize exportSize = NSMakeSize(-1,-1);
CMVideoDimensions vidDims = CMVideoFormatDescriptionGetDimensions(trackFmt);
NSSize trackSize = NSMakeSize(vidDims.width, vidDims.height);
tmpNum = [baseVideoExportSettings objectForKey:AVVideoWidthKey];
exportSize.width = [tmpNum doubleValue];
tmpNum = [baseVideoExportSettings objectForKey:AVVideoHeightKey];
exportSize.height = [tmpNum doubleValue];
if (exportSize.width>0 && exportSize.height>0 && !NSEqualSizes(exportSize,trackSize)) {
transcodeThisTrack = YES;
}

// if the export settings don't specify a codec, skip the transcode on the track
if (exportCodecString==nil) {
//NSLog(@"\t\texport directions explicitly state to skip transcode on track %@",trackPtr);
Expand Down
4 changes: 2 additions & 2 deletions HapInAVFoundation/AVAssetWriterHapInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ This class is the main interface for using AVFoundation to encode and output vid
NSSize exportDXTImgSize; // 'exportImgSize' rounded up to a multiple of 4
unsigned int exportChunkCounts[2];
BOOL exportHighQualityFlag; // NO by default, YES if the quality slider is > .8 in hap or hap alpha codecs
size_t exportSliceCount;
size_t exportSliceHeight;
size_t exportSliceCount; // 1 by default/if slicing is disabled. if slicing is enabled, this is the # of slices.
size_t exportSliceHeight; // calculated using image height + slice count. the number of rows of pixels in a slice.

OSType encoderInputPxlFmts[2]; // the encoder wants pixels of a particular format. this is the format they want.
uint32_t encoderInputPxlFmtBytesPerRow[2]; // the number of bytes per row in the buffers created to convert to 'encoderInputPxlFmts'
Expand Down
47 changes: 44 additions & 3 deletions HapInAVFoundation/AVAssetWriterHapInput.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#import "AVAssetWriterHapInput.h"
#import <Accelerate/Accelerate.h>
#include "HapPlatform.h"
#import "HapCodecSubTypes.h"
#import "PixelFormats.h"
Expand Down Expand Up @@ -379,22 +380,58 @@ - (BOOL) appendPixelBuffer:(CVPixelBufferRef)pb withPresentationTime:(CMTime)t a
//NSLog(@"%s",__func__);
// if there's a pixel buffer to encode, let's take care of that first
if (pb!=NULL) {
// lock the base address of the pixel buffer, get a ptr to the raw pixel data- i'll either be converting it or encoding it
// lock the base address of the pixel buffer
CVPixelBufferLockBaseAddress(pb,kHapCodecCVPixelBufferLockFlags);

// get the size of the image in the cvpixelbuffer, we need to compare it to the export size to determine if we need to do a resize...
NSSize pbSize = NSMakeSize(CVPixelBufferGetWidth(pb),CVPixelBufferGetHeight(pb));

// get a ptr to the raw pixel data- i'll either be resizing/converting it or encoding this ptr.
void *sourceBuffer = CVPixelBufferGetBaseAddress(pb);
size_t sourceBufferBytesPerRow = CVPixelBufferGetBytesPerRow(pb);

// allocate the resize buffer if i need it
void *resizeBuffer = nil;
if (!NSEqualSizes(pbSize, exportImgSize)) {
// make a vImage struct for the pixel buffer we were passed
vImage_Buffer pbImage = {
.data = sourceBuffer,
.height = pbSize.height,
.width = pbSize.width,
.rowBytes = sourceBufferBytesPerRow
};
// make the resize buffer
size_t resizeBufferSize = encoderInputPxlFmtBytesPerRow[0] * exportImgSize.height;
resizeBuffer = CFAllocatorAllocate(_HIAVFMemPoolAllocator, resizeBufferSize, 0);
// make a vImage struct for the resize buffer we just allocated
vImage_Buffer resizeImage = {
.data = resizeBuffer,
.height = exportImgSize.height,
.width = exportImgSize.width,
.rowBytes = encoderInputPxlFmtBytesPerRow[0]
};
// scale the pixel buffer's vImage to the resize buffer's vImage
vImage_Error vErr = vImageScale_ARGB8888(&pbImage, &resizeImage, NULL, kvImageHighQualityResampling | kvImageDoNotTile);
if (vErr != kvImageNoError)
NSLog(@"\t\terr %ld scaling image in %s",vErr,__func__);
else {
// update the sourceBuffer- we just resized the buffer we were passed, so it should have the same pixel format
sourceBuffer = resizeImage.data;
sourceBufferBytesPerRow = resizeImage.rowBytes;
}
}

// allocate any format conversion buffers i may need
void *_formatConvertBuffers[2];
_formatConvertBuffers[0] = nil;
_formatConvertBuffers[1] = nil;
void **formatConvertBuffers = _formatConvertBuffers; // this exists because we can't refer to arrays on the stack from within blocks

// allocate any format conversion buffers i may need
for (int i=0; i<exportPixelFormatsCount; ++i) {
if (sourceFormat != encoderInputPxlFmts[i]) {
formatConvertBuffers[i] = CFAllocatorAllocate(_HIAVFMemPoolAllocator, formatConvertPoolLengths[i], 0);
}
}

// allocate the DXT buffer (or buffers) i'll be creating
void *dxtBuffer = CFAllocatorAllocate(_HIAVFMemPoolAllocator, dxtBufferPoolLengths[0], 0);
void *dxtAlphaBuffer = NULL;
Expand Down Expand Up @@ -691,6 +728,10 @@ - (BOOL) appendPixelBuffer:(CVPixelBufferRef)pb withPresentationTime:(CMTime)t a
CFAllocatorDeallocate(_HIAVFMemPoolAllocator, dxtAlphaBuffer);
dxtAlphaBuffer = nil;
}
if (resizeBuffer != nil) {
CFAllocatorDeallocate(_HIAVFMemPoolAllocator, resizeBuffer);
resizeBuffer = nil;
}
if (formatConvertBuffers[0]!=nil) {
CFAllocatorDeallocate(_HIAVFMemPoolAllocator, formatConvertBuffers[0]);
formatConvertBuffers[0] = nil;
Expand Down
2 changes: 1 addition & 1 deletion HapInAVFoundation/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.5.2</string>
<string>1.5.3</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down

0 comments on commit 52e2430

Please sign in to comment.