Create CGContext from CVImageBuffer - Context is nil for specific image buffer sizes

When I tried to create CGContext from CVImageBuffer with below code

CGContext(data: CVPixelBufferGetBaseAddress(pixelBuffer),
                            width: width,
                            height: height,
                            bitsPerComponent: 8,
                            bytesPerRow: CVPixelBufferGetBytesPerRow(pixelBuffer),
                            space: CGColorSpaceCreateDeviceRGB(),
                            bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedFirst.rawValue).union(.byteOrder32Little).rawValue)

CGContext is nil with below Error

CGBitmapContextCreate: invalid data bytes/row: should be at least 8640 for 8 integer bits/component, 3 components, kCGImageAlphaPremultipliedFirst.

Note: This happens only with specific image buffer size like 4k, etc. And 1080p and 720p works fine.

Appreciate your help in advance!

Create CGContext from CVImageBuffer - Context is nil for specific image buffer sizes
 
 
Q