GPUImageOutput.m 12 KB

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