2014 snapchat source code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
3.1 KiB

  1. //
  2. // SCProcessingModuleUtils.m
  3. // Snapchat
  4. //
  5. // Created by Brian Ng on 11/10/17.
  6. //
  7. #import "SCProcessingModuleUtils.h"
  8. #import <SCFoundation/SCLog.h>
  9. @import CoreImage;
  10. @implementation SCProcessingModuleUtils
  11. + (CVPixelBufferRef)pixelBufferFromImage:(CIImage *)image
  12. bufferPool:(CVPixelBufferPoolRef)bufferPool
  13. context:(CIContext *)context
  14. {
  15. CVReturn result;
  16. if (bufferPool == NULL) {
  17. NSDictionary *pixelAttributes = @{
  18. (NSString *) kCVPixelBufferIOSurfacePropertiesKey : @{}, (NSString *)
  19. kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange), (NSString *)
  20. kCVPixelBufferWidthKey : @(image.extent.size.width), (NSString *)
  21. kCVPixelBufferHeightKey : @(image.extent.size.height)
  22. };
  23. result = CVPixelBufferPoolCreate(kCFAllocatorDefault, NULL,
  24. (__bridge CFDictionaryRef _Nullable)(pixelAttributes), &bufferPool);
  25. if (result != kCVReturnSuccess) {
  26. SCLogGeneralError(@"[Processing Pipeline] Error creating pixel buffer pool %i", result);
  27. return NULL;
  28. }
  29. }
  30. CVPixelBufferRef resultBuffer = NULL;
  31. result = CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, bufferPool, &resultBuffer);
  32. if (result == kCVReturnSuccess) {
  33. [context render:image toCVPixelBuffer:resultBuffer];
  34. } else {
  35. SCLogGeneralError(@"[Processing Pipeline] Error creating pixel buffer from pool %i", result);
  36. }
  37. return resultBuffer;
  38. }
  39. + (CMSampleBufferRef)sampleBufferFromImage:(CIImage *)image
  40. oldSampleBuffer:(CMSampleBufferRef)oldSampleBuffer
  41. bufferPool:(CVPixelBufferPoolRef)bufferPool
  42. context:(CIContext *)context
  43. {
  44. CVPixelBufferRef pixelBuffer =
  45. [SCProcessingModuleUtils pixelBufferFromImage:image bufferPool:bufferPool context:context];
  46. if (!pixelBuffer) {
  47. SCLogGeneralError(@"[Processing Pipeline] Error creating new pixel buffer from image");
  48. return oldSampleBuffer;
  49. }
  50. CMSampleBufferRef newSampleBuffer = NULL;
  51. CMSampleTimingInfo timimgInfo = kCMTimingInfoInvalid;
  52. CMSampleBufferGetSampleTimingInfo(oldSampleBuffer, 0, &timimgInfo);
  53. CMVideoFormatDescriptionRef videoInfo = NULL;
  54. OSStatus status = CMVideoFormatDescriptionCreateForImageBuffer(NULL, pixelBuffer, &videoInfo);
  55. if (status != noErr) {
  56. SCLogGeneralError(@"[Processing Pipeline] Error creating video format description %i", (int)status);
  57. CVPixelBufferRelease(pixelBuffer);
  58. return oldSampleBuffer;
  59. }
  60. status = CMSampleBufferCreateForImageBuffer(kCFAllocatorDefault, pixelBuffer, true, NULL, NULL, videoInfo,
  61. &timimgInfo, &newSampleBuffer);
  62. if (status != noErr) {
  63. SCLogGeneralError(@"[Processing Pipeline] Error creating CMSampleBuffer %i", (int)status);
  64. CVPixelBufferRelease(pixelBuffer);
  65. return oldSampleBuffer;
  66. }
  67. CVPixelBufferRelease(pixelBuffer);
  68. return newSampleBuffer;
  69. }
  70. @end