GPUImageOutput.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. #import "GPUImageOutput.h"
  2. #import "GPUImageMovieWriter.h"
  3. #import "GPUImagePicture.h"
  4. #import <mach/mach.h>
  5. void runOnMainQueueWithoutDeadlocking(void (^block)(void))
  6. {
  7. if ([NSThread isMainThread])
  8. {
  9. block();
  10. }
  11. else
  12. {
  13. dispatch_sync(dispatch_get_main_queue(), block);
  14. }
  15. }
  16. void runSynchronouslyOnVideoProcessingQueue(void (^block)(void))
  17. {
  18. dispatch_queue_t videoProcessingQueue = [GPUImageContext sharedContextQueue];
  19. #if (!defined(__IPHONE_6_0) || (__IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0))
  20. if (dispatch_get_current_queue() == videoProcessingQueue)
  21. #else
  22. if (dispatch_get_specific([GPUImageContext contextKey]))
  23. #endif
  24. {
  25. block();
  26. }else
  27. {
  28. dispatch_sync(videoProcessingQueue, block);
  29. }
  30. }
  31. void runAsynchronouslyOnVideoProcessingQueue(void (^block)(void))
  32. {
  33. dispatch_queue_t videoProcessingQueue = [GPUImageContext sharedContextQueue];
  34. #if (!defined(__IPHONE_6_0) || (__IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0))
  35. if (dispatch_get_current_queue() == videoProcessingQueue)
  36. #else
  37. if (dispatch_get_specific([GPUImageContext contextKey]))
  38. #endif
  39. {
  40. block();
  41. }else
  42. {
  43. dispatch_async(videoProcessingQueue, block);
  44. }
  45. }
  46. void runSynchronouslyOnContextQueue(GPUImageContext *context, void (^block)(void))
  47. {
  48. dispatch_queue_t videoProcessingQueue = [context contextQueue];
  49. #if (!defined(__IPHONE_6_0) || (__IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0))
  50. if (dispatch_get_current_queue() == videoProcessingQueue)
  51. #else
  52. if (dispatch_get_specific([GPUImageContext contextKey]))
  53. #endif
  54. {
  55. block();
  56. }else
  57. {
  58. dispatch_sync(videoProcessingQueue, block);
  59. }
  60. }
  61. void runAsynchronouslyOnContextQueue(GPUImageContext *context, void (^block)(void))
  62. {
  63. dispatch_queue_t videoProcessingQueue = [context contextQueue];
  64. #if (!defined(__IPHONE_6_0) || (__IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0))
  65. if (dispatch_get_current_queue() == videoProcessingQueue)
  66. #else
  67. if (dispatch_get_specific([GPUImageContext contextKey]))
  68. #endif
  69. {
  70. block();
  71. }else
  72. {
  73. dispatch_async(videoProcessingQueue, block);
  74. }
  75. }
  76. void reportAvailableMemoryForGPUImage(NSString *tag)
  77. {
  78. if (!tag)
  79. tag = @"Default";
  80. struct task_basic_info info;
  81. mach_msg_type_number_t size = sizeof(info);
  82. kern_return_t kerr = task_info(mach_task_self(),
  83. TASK_BASIC_INFO,
  84. (task_info_t)&info,
  85. &size);
  86. if( kerr == KERN_SUCCESS ) {
  87. NSLog(@"%@ - Memory used: %u", tag, (unsigned int)info.resident_size); //in bytes
  88. } else {
  89. NSLog(@"%@ - Error: %s", tag, mach_error_string(kerr));
  90. }
  91. }
  92. @implementation GPUImageOutput
  93. @synthesize shouldSmoothlyScaleOutput = _shouldSmoothlyScaleOutput;
  94. @synthesize shouldIgnoreUpdatesToThisTarget = _shouldIgnoreUpdatesToThisTarget;
  95. @synthesize audioEncodingTarget = _audioEncodingTarget;
  96. @synthesize targetToIgnoreForUpdates = _targetToIgnoreForUpdates;
  97. @synthesize frameProcessingCompletionBlock = _frameProcessingCompletionBlock;
  98. @synthesize enabled = _enabled;
  99. @synthesize outputTextureOptions = _outputTextureOptions;
  100. #pragma mark -
  101. #pragma mark Initialization and teardown
  102. - (id)init;
  103. {
  104. if (!(self = [super init]))
  105. {
  106. return nil;
  107. }
  108. targets = [[NSMutableArray alloc] init];
  109. targetTextureIndices = [[NSMutableArray alloc] init];
  110. _enabled = YES;
  111. allTargetsWantMonochromeData = YES;
  112. usingNextFrameForImageCapture = NO;
  113. // set default texture options
  114. _outputTextureOptions.minFilter = GL_LINEAR;
  115. _outputTextureOptions.magFilter = GL_LINEAR;
  116. _outputTextureOptions.wrapS = GL_CLAMP_TO_EDGE;
  117. _outputTextureOptions.wrapT = GL_CLAMP_TO_EDGE;
  118. _outputTextureOptions.internalFormat = GL_RGBA;
  119. _outputTextureOptions.format = GL_BGRA;
  120. _outputTextureOptions.type = GL_UNSIGNED_BYTE;
  121. return self;
  122. }
  123. - (void)dealloc
  124. {
  125. [self removeAllTargets];
  126. }
  127. #pragma mark -
  128. #pragma mark Managing targets
  129. - (void)setInputFramebufferForTarget:(id<GPUImageInput>)target atIndex:(NSInteger)inputTextureIndex;
  130. {
  131. [target setInputFramebuffer:[self framebufferForOutput] atIndex:inputTextureIndex];
  132. }
  133. - (GPUImageFramebuffer *)framebufferForOutput;
  134. {
  135. return outputFramebuffer;
  136. }
  137. - (void)removeOutputFramebuffer;
  138. {
  139. outputFramebuffer = nil;
  140. }
  141. - (void)notifyTargetsAboutNewOutputTexture;
  142. {
  143. for (id<GPUImageInput> currentTarget in targets)
  144. {
  145. NSInteger indexOfObject = [targets indexOfObject:currentTarget];
  146. NSInteger textureIndex = [[targetTextureIndices objectAtIndex:indexOfObject] integerValue];
  147. [self setInputFramebufferForTarget:currentTarget atIndex:textureIndex];
  148. }
  149. }
  150. - (NSArray*)targets;
  151. {
  152. return [NSArray arrayWithArray:targets];
  153. }
  154. - (void)addTarget:(id<GPUImageInput>)newTarget;
  155. {
  156. NSInteger nextAvailableTextureIndex = [newTarget nextAvailableTextureIndex];
  157. [self addTarget:newTarget atTextureLocation:nextAvailableTextureIndex];
  158. if ([newTarget shouldIgnoreUpdatesToThisTarget])
  159. {
  160. _targetToIgnoreForUpdates = newTarget;
  161. }
  162. }
  163. - (void)addTarget:(id<GPUImageInput>)newTarget atTextureLocation:(NSInteger)textureLocation;
  164. {
  165. if([targets containsObject:newTarget])
  166. {
  167. return;
  168. }
  169. cachedMaximumOutputSize = CGSizeZero;
  170. runSynchronouslyOnVideoProcessingQueue(^{
  171. [self setInputFramebufferForTarget:newTarget atIndex:textureLocation];
  172. [targets addObject:newTarget];
  173. [targetTextureIndices addObject:[NSNumber numberWithInteger:textureLocation]];
  174. allTargetsWantMonochromeData = allTargetsWantMonochromeData && [newTarget wantsMonochromeInput];
  175. });
  176. }
  177. - (void)removeTarget:(id<GPUImageInput>)targetToRemove;
  178. {
  179. if(![targets containsObject:targetToRemove])
  180. {
  181. return;
  182. }
  183. if (_targetToIgnoreForUpdates == targetToRemove)
  184. {
  185. _targetToIgnoreForUpdates = nil;
  186. }
  187. cachedMaximumOutputSize = CGSizeZero;
  188. NSInteger indexOfObject = [targets indexOfObject:targetToRemove];
  189. NSInteger textureIndexOfTarget = [[targetTextureIndices objectAtIndex:indexOfObject] integerValue];
  190. runSynchronouslyOnVideoProcessingQueue(^{
  191. [targetToRemove setInputSize:CGSizeZero atIndex:textureIndexOfTarget];
  192. [targetToRemove setInputRotation:kGPUImageNoRotation atIndex:textureIndexOfTarget];
  193. [targetTextureIndices removeObjectAtIndex:indexOfObject];
  194. [targets removeObject:targetToRemove];
  195. [targetToRemove endProcessing];
  196. });
  197. }
  198. - (void)removeAllTargets;
  199. {
  200. cachedMaximumOutputSize = CGSizeZero;
  201. runSynchronouslyOnVideoProcessingQueue(^{
  202. for (id<GPUImageInput> targetToRemove in targets)
  203. {
  204. NSInteger indexOfObject = [targets indexOfObject:targetToRemove];
  205. NSInteger textureIndexOfTarget = [[targetTextureIndices objectAtIndex:indexOfObject] integerValue];
  206. [targetToRemove setInputSize:CGSizeZero atIndex:textureIndexOfTarget];
  207. [targetToRemove setInputRotation:kGPUImageNoRotation atIndex:textureIndexOfTarget];
  208. }
  209. [targets removeAllObjects];
  210. [targetTextureIndices removeAllObjects];
  211. allTargetsWantMonochromeData = YES;
  212. });
  213. }
  214. #pragma mark -
  215. #pragma mark Manage the output texture
  216. - (void)forceProcessingAtSize:(CGSize)frameSize;
  217. {
  218. }
  219. - (void)forceProcessingAtSizeRespectingAspectRatio:(CGSize)frameSize;
  220. {
  221. }
  222. #pragma mark -
  223. #pragma mark Still image processing
  224. - (void)useNextFrameForImageCapture;
  225. {
  226. }
  227. - (CGImageRef)newCGImageFromCurrentlyProcessedOutput;
  228. {
  229. return nil;
  230. }
  231. - (CGImageRef)newCGImageByFilteringCGImage:(CGImageRef)imageToFilter;
  232. {
  233. GPUImagePicture *stillImageSource = [[GPUImagePicture alloc] initWithCGImage:imageToFilter];
  234. [self useNextFrameForImageCapture];
  235. [stillImageSource addTarget:(id<GPUImageInput>)self];
  236. [stillImageSource processImage];
  237. CGImageRef processedImage = [self newCGImageFromCurrentlyProcessedOutput];
  238. [stillImageSource removeTarget:(id<GPUImageInput>)self];
  239. return processedImage;
  240. }
  241. - (BOOL)providesMonochromeOutput;
  242. {
  243. return NO;
  244. }
  245. #pragma mark -
  246. #pragma mark Platform-specific image output methods
  247. #if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
  248. - (UIImage *)imageFromCurrentFramebuffer;
  249. {
  250. UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
  251. UIImageOrientation imageOrientation = UIImageOrientationLeft;
  252. switch (deviceOrientation)
  253. {
  254. case UIDeviceOrientationPortrait:
  255. imageOrientation = UIImageOrientationUp;
  256. break;
  257. case UIDeviceOrientationPortraitUpsideDown:
  258. imageOrientation = UIImageOrientationDown;
  259. break;
  260. case UIDeviceOrientationLandscapeLeft:
  261. imageOrientation = UIImageOrientationLeft;
  262. break;
  263. case UIDeviceOrientationLandscapeRight:
  264. imageOrientation = UIImageOrientationRight;
  265. break;
  266. default:
  267. imageOrientation = UIImageOrientationUp;
  268. break;
  269. }
  270. return [self imageFromCurrentFramebufferWithOrientation:imageOrientation];
  271. }
  272. - (UIImage *)imageFromCurrentFramebufferWithOrientation:(UIImageOrientation)imageOrientation;
  273. {
  274. CGImageRef cgImageFromBytes = [self newCGImageFromCurrentlyProcessedOutput];
  275. UIImage *finalImage = [UIImage imageWithCGImage:cgImageFromBytes scale:1.0 orientation:imageOrientation];
  276. CGImageRelease(cgImageFromBytes);
  277. return finalImage;
  278. }
  279. - (UIImage *)imageByFilteringImage:(UIImage *)imageToFilter;
  280. {
  281. CGImageRef image = [self newCGImageByFilteringCGImage:[imageToFilter CGImage]];
  282. UIImage *processedImage = [UIImage imageWithCGImage:image scale:[imageToFilter scale] orientation:[imageToFilter imageOrientation]];
  283. CGImageRelease(image);
  284. return processedImage;
  285. }
  286. - (CGImageRef)newCGImageByFilteringImage:(UIImage *)imageToFilter
  287. {
  288. return [self newCGImageByFilteringCGImage:[imageToFilter CGImage]];
  289. }
  290. #else
  291. - (NSImage *)imageFromCurrentFramebuffer;
  292. {
  293. return [self imageFromCurrentFramebufferWithOrientation:UIImageOrientationLeft];
  294. }
  295. - (NSImage *)imageFromCurrentFramebufferWithOrientation:(UIImageOrientation)imageOrientation;
  296. {
  297. CGImageRef cgImageFromBytes = [self newCGImageFromCurrentlyProcessedOutput];
  298. NSImage *finalImage = [[NSImage alloc] initWithCGImage:cgImageFromBytes size:NSZeroSize];
  299. CGImageRelease(cgImageFromBytes);
  300. return finalImage;
  301. }
  302. - (NSImage *)imageByFilteringImage:(NSImage *)imageToFilter;
  303. {
  304. CGImageRef image = [self newCGImageByFilteringCGImage:[imageToFilter CGImageForProposedRect:NULL context:[NSGraphicsContext currentContext] hints:nil]];
  305. NSImage *processedImage = [[NSImage alloc] initWithCGImage:image size:NSZeroSize];
  306. CGImageRelease(image);
  307. return processedImage;
  308. }
  309. - (CGImageRef)newCGImageByFilteringImage:(NSImage *)imageToFilter
  310. {
  311. return [self newCGImageByFilteringCGImage:[imageToFilter CGImageForProposedRect:NULL context:[NSGraphicsContext currentContext] hints:nil]];
  312. }
  313. #endif
  314. #pragma mark -
  315. #pragma mark Accessors
  316. - (void)setAudioEncodingTarget:(GPUImageMovieWriter *)newValue;
  317. {
  318. _audioEncodingTarget = newValue;
  319. if( ! _audioEncodingTarget.hasAudioTrack )
  320. {
  321. _audioEncodingTarget.hasAudioTrack = YES;
  322. }
  323. }
  324. @end