1 /* $Id: HBPreviewController.mm,v 1.11 2005/08/01 15:10:44 titer Exp $
3 This file is part of the HandBrake source code.
4 Homepage: <http://handbrake.fr/>.
5 It may be used under the terms of the GNU General Public License. */
7 #import "HBPreviewController.h"
10 @interface PreviewController (Private)
12 - (NSSize)optimalViewSizeForImageSize: (NSSize)imageSize;
13 - (void)resizeSheetForViewSize: (NSSize)viewSize;
14 - (void)setViewSize: (NSSize)viewSize;
15 - (BOOL)viewNeedsToResizeToSize: (NSSize)newSize;
19 @implementation PreviewController
23 if (self = [super initWithWindowNibName:@"PicturePreview"])
25 // NSWindowController likes to lazily load its window. However since
26 // this controller tries to set all sorts of outlets before the window
27 // is displayed, we need it to load immediately. The correct way to do
28 // this, according to the documentation, is simply to invoke the window
31 // If/when we switch a lot of this stuff to bindings, this can probably
35 fPicturePreviews = [[NSMutableDictionary dictionaryWithCapacity: HB_NUM_HBLIB_PICTURES] retain];
36 /* Init libhb with check for updates libhb style set to "0" so its ignored and lets sparkle take care of it */
37 int loggingLevel = [[[NSUserDefaults standardUserDefaults] objectForKey:@"LoggingLevel"] intValue];
38 fPreviewLibhb = hb_init(loggingLevel, 0);
46 //------------------------------------------------------------------------------------
47 // Displays and brings the picture window to the front
48 //------------------------------------------------------------------------------------
49 - (IBAction) showPreviewWindow: (id)sender
51 [self showWindow:sender];
52 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"PreviewWindowIsOpen"];
54 /* lets set the preview window to accept mouse moved events */
55 [fPreviewWindow setAcceptsMouseMovedEvents:YES];
57 [self pictureSliderChanged:nil];
58 [self startReceivingLibhbNotifications];
61 - (void)setHBController: (HBController *)controller
63 fHBController = controller;
68 [fPreviewWindow setDelegate:self];
69 if( ![[self window] setFrameUsingName:@"Preview"] )
70 [[self window] center];
71 [self setWindowFrameAutosaveName:@"Preview"];
72 [[self window] setExcludedFromWindowsMenu:YES];
74 /* lets set the preview window to accept mouse moved events */
75 [fPreviewWindow setAcceptsMouseMovedEvents:YES];
76 //[self pictureSliderChanged:nil];
77 [self startReceivingLibhbNotifications];
81 /* we set the progress indicator to not use threaded animation
82 * as it causes a conflict with the qtmovieview's controllerbar
84 [fMovieCreationProgressIndicator setUsesThreadedAnimation:NO];
86 /* Setup our layers for core animation */
87 [fPictureViewArea setWantsLayer:YES];
88 [fPictureView setWantsLayer:YES];
90 [fMovieView setWantsLayer:YES];
92 [fCancelPreviewMovieButton setWantsLayer:YES];
93 [fMovieCreationProgressIndicator setWantsLayer:YES];
95 [fPictureControlBox setWantsLayer:YES];
96 [fPictureSlider setWantsLayer:YES];
97 [fFullScreenToggleButton setWantsLayer:YES];
98 [fPictureSettingsToggleButton setWantsLayer:YES];
99 [fScaleToScreenToggleButton setWantsLayer:YES];
100 [fCreatePreviewMovieButton setWantsLayer:YES];
102 [fEncodingControlBox setWantsLayer:YES];
104 [fShowPreviewMovieButton setWantsLayer:YES];
108 - (BOOL)acceptsMouseMovedEvents
113 - (void)windowWillClose:(NSNotification *)aNotification
117 /* Upon Closing the picture window, we make sure we clean up any
118 * preview movie that might be playing
121 hb_stop( fPreviewLibhb );
123 // Show the picture view
124 [fPictureView setHidden:NO];
125 [fMovieView pause:nil];
126 [fMovieView setHidden:YES];
130 [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"PreviewWindowIsOpen"];
133 - (BOOL)windowShouldClose:(id)fPictureWindow
141 hb_stop(fPreviewLibhb);
142 if (fPreviewMoviePath)
144 [[NSFileManager defaultManager] removeFileAtPath:fPreviewMoviePath handler:nil];
145 [fPreviewMoviePath release];
148 [fLibhbTimer invalidate];
149 [fLibhbTimer release];
151 [fHudTimer invalidate];
154 [fPicturePreviews release];
155 [fFullScreenWindow release];
160 - (void) SetHandle: (hb_handle_t *) handle
166 /* we set the preview length popup in seconds */
167 [fPreviewMovieLengthPopUp removeAllItems];
168 [fPreviewMovieLengthPopUp addItemWithTitle: @"5"];
169 [fPreviewMovieLengthPopUp addItemWithTitle: @"10"];
170 [fPreviewMovieLengthPopUp addItemWithTitle: @"15"];
171 [fPreviewMovieLengthPopUp addItemWithTitle: @"20"];
172 [fPreviewMovieLengthPopUp addItemWithTitle: @"25"];
173 [fPreviewMovieLengthPopUp addItemWithTitle: @"30"];
174 [fPreviewMovieLengthPopUp addItemWithTitle: @"35"];
175 [fPreviewMovieLengthPopUp addItemWithTitle: @"40"];
176 [fPreviewMovieLengthPopUp addItemWithTitle: @"45"];
177 [fPreviewMovieLengthPopUp addItemWithTitle: @"50"];
178 [fPreviewMovieLengthPopUp addItemWithTitle: @"55"];
179 [fPreviewMovieLengthPopUp addItemWithTitle: @"60"];
181 /* adjust the preview slider length */
182 /* We use our advance pref to determine how many previews we scanned */
183 int hb_num_previews = [[[NSUserDefaults standardUserDefaults] objectForKey:@"PreviewsNumber"] intValue];
184 [fPictureSlider setMaxValue: hb_num_previews - 1.0];
185 [fPictureSlider setNumberOfTickMarks: hb_num_previews];
187 if ([[NSUserDefaults standardUserDefaults] objectForKey:@"PreviewLength"])
189 [fPreviewMovieLengthPopUp selectItemWithTitle:[[NSUserDefaults standardUserDefaults] objectForKey:@"PreviewLength"]];
193 /* currently hard set default to 10 seconds */
194 [fPreviewMovieLengthPopUp selectItemAtIndex: 1];
198 - (void) SetTitle: (hb_title_t *) title
200 hb_job_t * job = title->job;
204 MaxOutputWidth = title->width - job->crop[2] - job->crop[3];
205 MaxOutputHeight = title->height - job->crop[0] - job->crop[1];
206 [self SettingsChanged: nil];
211 // Adjusts the window to draw the current picture (fPicture) adjusting its size as
212 // necessary to display as much of the picture as possible.
213 - (void) displayPreview
215 hb_job_t * job = fTitle->job;
216 /* lets make sure that the still picture view is not hidden and that
217 * the movie preview is
219 [fMovieView pause:nil];
220 [fMovieView setHidden:YES];
221 [fMovieCreationProgressIndicator stopAnimation: nil];
222 [fMovieCreationProgressIndicator setHidden: YES];
224 [fPictureView setHidden:NO];
225 [fPictureView setImage: [self imageForPicture: fPicture]];
227 NSSize displaySize = NSMakeSize( ( CGFloat )fTitle->width, ( CGFloat )fTitle->height );
228 /* Set the picture size display fields below the Preview Picture*/
229 if( fTitle->job->anamorphic.mode == 1 ) // Original PAR Implementation
231 output_width = fTitle->width-fTitle->job->crop[2]-fTitle->job->crop[3];
232 output_height = fTitle->height-fTitle->job->crop[0]-fTitle->job->crop[1];
233 display_width = output_width * fTitle->job->anamorphic.par_width / fTitle->job->anamorphic.par_height;
234 [fInfoField setStringValue:[NSString stringWithFormat:
235 @"Source: %dx%d, Output: %dx%d, Anamorphic: %dx%d Strict",
236 fTitle->width, fTitle->height, output_width, output_height, display_width, output_height]];
237 displaySize.width *= ( ( CGFloat )fTitle->job->anamorphic.par_width ) / ( ( CGFloat )fTitle->job->anamorphic.par_height );
239 else if (fTitle->job->anamorphic.mode == 2) // Loose Anamorphic
241 hb_set_anamorphic_size(job, &output_width, &output_height, &output_par_width, &output_par_height);
242 display_width = output_width * output_par_width / output_par_height;
243 [fInfoField setStringValue:[NSString stringWithFormat:
244 @"Source: %dx%d, Output: %dx%d, Anamorphic: %dx%d Loose",
245 fTitle->width, fTitle->height, output_width, output_height, display_width, output_height]];
247 displaySize.width = display_width;
249 else // No Anamorphic
251 [fInfoField setStringValue: [NSString stringWithFormat:
252 @"Source: %dx%d, Output: %dx%d", fTitle->width, fTitle->height,
253 fTitle->job->width, fTitle->job->height]];
257 NSSize viewSize = [self optimalViewSizeForImageSize:displaySize];
258 /* we also need to take into account scaling to full screen to activate switching the view size */
259 if( [self viewNeedsToResizeToSize:viewSize])
261 /* In the case of loose anamorphic, do not resize the window when scaling down */
262 // FIX ME: we need a new way to do this as we do not havefWidthField anymore
263 //if (fTitle->job->anamorphic.mode != 2 || [fWidthField intValue] == fTitle->width)
264 if (fTitle->job->anamorphic.mode != 2 || (fTitle->job->anamorphic.mode == 2 && output_width == fTitle->width))
266 [self resizeSheetForViewSize:viewSize];
267 [self setViewSize:viewSize];
272 // Show the scaled text (use the height to check since the width can vary
273 // with anamorphic video).
274 if( ( ( int )viewSize.height ) != fTitle->height )
276 CGFloat scale = viewSize.width / ( ( CGFloat ) fTitle->width );
277 NSString *scaleString = [NSString stringWithFormat:
278 NSLocalizedString( @" (Preview scaled to %.0f%% actual size)",
279 @"String shown when a preview is scaled" ),
281 [fscaleInfoField setStringValue: [NSString stringWithFormat:
282 @"%@", scaleString]];
287 [fscaleInfoField setStringValue: @""];
292 - (IBAction) previewDurationPopUpChanged: (id) sender
295 [[NSUserDefaults standardUserDefaults] setObject:[fPreviewMovieLengthPopUp titleOfSelectedItem] forKey:@"PreviewLength"];
299 - (IBAction) SettingsChanged: (id) sender
301 // Purge the existing picture previews so they get recreated the next time
303 [self purgeImageCache];
304 /* We actually call displayPreview now from pictureSliderChanged which keeps
305 * our picture preview slider in sync with the previews being shown
307 //[self displayPreview];
308 [self pictureSliderChanged:nil];
311 - (IBAction) pictureSliderChanged: (id) sender
313 // Show the picture view
314 [fPictureView setHidden:NO];
315 [fMovieView pause:nil];
316 [fMovieView setHidden:YES];
317 [fEncodingControlBox setHidden: YES];
319 int newPicture = [fPictureSlider intValue];
320 if (newPicture != fPicture)
322 fPicture = newPicture;
324 [self displayPreview];
328 - (IBAction)showPreviewPanel: (id)sender forTitle: (hb_title_t *)title
330 [self SetTitle:title];
332 if ([fPreviewWindow isVisible])
335 [fPreviewWindow close];
340 [self showWindow:sender];
341 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"PreviewWindowIsOpen"];
342 [fPreviewWindow setAcceptsMouseMovedEvents:YES];
346 [self pictureSliderChanged:nil];
347 [self startHudTimer];
353 - (NSString*) pictureSizeInfoString
355 return [fInfoField stringValue];
358 - (IBAction)showPictureSettings:(id)sender
360 [fHBController showPicturePanel:self];
363 #pragma mark Hud Control Overlay
364 - (void) mouseMoved:(NSEvent *)theEvent
366 [super mouseMoved:theEvent];
368 if (isEncoding == NO)
370 if (hudTimerSeconds == 0)
373 [self startHudTimer];
376 if (hudTimerSeconds > 20)
381 [self showHideHudControls];
387 - (void) startHudTimer
391 fHudTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(hudTimerFired:) userInfo:nil repeats:YES];
396 - (void) stopHudTimer
400 [fHudTimer invalidate];
407 - (void) hudTimerFired: (NSTimer*)theTimer
410 [self showHideHudControls];
414 - (void) showHideHudControls
416 /* Test for mouse location to show/hide hud controls */
419 NSRect controlBoxFrame;
420 targetFrame = [fPictureViewArea frame];
421 controlBoxFrame = [fPictureControlBox frame];
425 mouseLoc = [fFullScreenWindow mouseLocationOutsideOfEventStream];
426 [fScaleToScreenToggleButton setHidden:NO];
430 mouseLoc = [fPreviewWindow mouseLocationOutsideOfEventStream];
431 [fScaleToScreenToggleButton setHidden:YES];
434 /* if the pointer is inside the picture view areas but not
435 * in the controlbox, check the hudTimerSeconds to see if
436 * its in the allowable time span
438 if ( hudTimerSeconds > 0 && hudTimerSeconds < 20)
441 if (isEncoding == NO)
443 if (NSPointInRect (mouseLoc, controlBoxFrame))
445 /* Mouse is over the preview area so show hud controls so just
446 * reset the timer to keep the control box visible
448 [fPictureControlBox setHidden: NO];
452 /* Re-verify we are within the target frame */
453 if (NSPointInRect (mouseLoc, targetFrame))
455 /* Mouse is over the preview area so show hud controls */
456 [[fPictureControlBox animator] setHidden: NO];
457 /* increment our timer by one */
462 [[fPictureControlBox animator] setHidden: YES];
470 [[fPictureControlBox animator] setHidden: YES];
476 #pragma mark Fullscreen Mode
478 - (IBAction)toggleScreenMode:(id)sender
482 [self goFullScreen:nil];
486 [self goWindowedScreen:nil];
490 - (IBAction)toggleScaleToScreen:(id)sender
492 if (scaleToScreen == YES)
495 /* make sure we are set to a still preview */
496 [self pictureSliderChanged:nil];
497 [fScaleToScreenToggleButton setTitle:@"<->"];
502 /* make sure we are set to a still preview */
503 [self pictureSliderChanged:nil];
504 [fScaleToScreenToggleButton setTitle:@">-<"];
507 /* Actually perform the scaling */
509 NSSize displaySize = NSMakeSize( ( CGFloat )fTitle->width, ( CGFloat )fTitle->height );
510 NSSize viewSize = [self optimalViewSizeForImageSize:displaySize];
511 [self resizeSheetForViewSize:viewSize];
512 [self setViewSize:viewSize];
521 - (IBAction)goFullScreen:(id)sender
523 // Get the screen information.
524 NSScreen* mainScreen = [fPreviewWindow screen];
525 NSDictionary* screenInfo = [mainScreen deviceDescription];
526 NSNumber* screenID = [screenInfo objectForKey:@"NSScreenNumber"];
527 // Capture the screen.
528 CGDirectDisplayID displayID = (CGDirectDisplayID)[screenID longValue];
529 CGDisplayErr err = CGDisplayCapture(displayID);
531 if (err == CGDisplayNoErr)
534 /* make sure we are set to a still preview and not scaled to screen */
536 [self pictureSliderChanged:nil];
538 // Create the full-screen window.
539 //NSRect winRect = [mainScreen frame];
541 NSRect winRect = [fPictureViewArea frame];
543 fFullScreenWindow = [[NSWindow alloc] initWithContentRect:winRect
544 styleMask:NSBorderlessWindowMask
545 backing:NSBackingStoreBuffered
549 // Establish the window attributes.
550 [fFullScreenWindow setReleasedWhenClosed:NO];
551 [fFullScreenWindow setDisplaysWhenScreenProfileChanges:YES];
552 [fFullScreenWindow setDelegate:self];
554 /* insert a view into the new window */
555 [fFullScreenWindow setContentView:fPictureViewArea];
556 [fPictureViewArea setNeedsDisplay:YES];
558 /* Better to center the window using the screen's frame
559 * and the windows origin. Note that we should take into
560 * account the auto sizing and alignment that occurs in
561 * setViewSize each time the preview changes.
562 * Note: by using [fFullScreenWindow screen] (instead of
563 * [NSScreen mainScreen]) in referencing the screen
564 * coordinates, the full screen window will show up on
565 * whichever display was being used in windowed mode
566 * on multi-display systems
569 NSSize screenSize = [[fFullScreenWindow screen] frame].size;
570 NSSize windowSize = [fFullScreenWindow frame].size;
571 NSPoint windowOrigin = [fFullScreenWindow frame].origin;
573 /* Adjust our origin y (vertical) based on the screen height */
574 windowOrigin.y += (screenSize.height - windowSize.height) / 2.0;
575 windowOrigin.x += (screenSize.width - windowSize.width) / 2.0;
577 [fFullScreenWindow setFrameOrigin:windowOrigin];
579 /* lets kill the timer for now */
580 [self stopReceivingLibhbNotifications];
582 /* We need to retain the fPreviewWindow */
583 [fPreviewWindow retain];
585 [self setWindow:fFullScreenWindow];
587 // The window has to be above the level of the shield window.
588 int32_t shieldLevel = CGShieldingWindowLevel();
590 [fFullScreenWindow setLevel:shieldLevel];
593 [fFullScreenWindow makeKeyAndOrderFront:self];
596 /* Change the name of fFullScreenToggleButton appropriately */
597 [fFullScreenToggleButton setTitle: @"Windowed"];
599 /* Lets fire the timer back up for the hud controls, etc. */
600 [self startReceivingLibhbNotifications];
603 [fScaleToScreenToggleButton setHidden:NO];
605 /* make sure we are set to a still preview */
606 [self pictureSliderChanged:nil];
608 //[fPreviewWindow setAcceptsMouseMovedEvents:NO];
609 [fFullScreenWindow setAcceptsMouseMovedEvents:YES];
613 [self startHudTimer];
617 // Title-less windows normally don't receive key presses, override this
618 - (BOOL)canBecomeKeyWindow
623 // Title-less windows normally can't become main which means that another
624 // non-fullscreen window will have the "active" titlebar in expose. Bad, fix it.
625 - (BOOL)canBecomeMainWindow
631 - (IBAction)goWindowedScreen:(id)sender
634 /* Get the screen info to release the display but don't actually do
635 * it until the windowed screen is setup.
638 [self pictureSliderChanged:nil];
639 [fScaleToScreenToggleButton setTitle:@"<->"];
641 NSScreen* mainScreen = [NSScreen mainScreen];
642 NSDictionary* screenInfo = [mainScreen deviceDescription];
643 NSNumber* screenID = [screenInfo objectForKey:@"NSScreenNumber"];
644 CGDirectDisplayID displayID = (CGDirectDisplayID)[screenID longValue];
646 [fFullScreenWindow dealloc];
647 [fFullScreenWindow release];
650 [fPreviewWindow setContentView:fPictureViewArea];
651 [fPictureViewArea setNeedsDisplay:YES];
652 [self setWindow:fPreviewWindow];
655 [fPreviewWindow makeKeyAndOrderFront:self];
657 /* Set the window back to regular level */
658 [fPreviewWindow setLevel:NSNormalWindowLevel];
660 /* Set the isFullScreen flag back to NO */
663 /* make sure we are set to a still preview */
664 [self pictureSliderChanged:nil];
665 [self showPreviewWindow:nil];
667 /* Change the name of fFullScreenToggleButton appropriately */
668 [fFullScreenToggleButton setTitle: @"Full Screen"];
669 // [fScaleToScreenToggleButton setHidden:YES];
670 /* set the picture settings pallete back to normal level */
671 [fHBController picturePanelWindowed];
673 /* Release the display now that the we are back in windowed mode */
674 CGDisplayRelease(displayID);
676 [fPreviewWindow setAcceptsMouseMovedEvents:YES];
677 //[fFullScreenWindow setAcceptsMouseMovedEvents:NO];
680 [self startHudTimer];
685 #pragma mark Still Preview Image Processing
688 // This function converts an image created by libhb (specified via pictureIndex) into
689 // an NSImage suitable for the GUI code to use. If removeBorders is YES,
690 // makeImageForPicture crops the image generated by libhb stripping off the gray
691 // border around the content. This is the low-level method that generates the image.
692 // -imageForPicture calls this function whenever it can't find an image in its cache.
693 + (NSImage *) makeImageForPicture: (int)pictureIndex
694 libhb:(hb_handle_t*)handle
695 title:(hb_title_t*)title
696 removeBorders:(BOOL)removeBorders
700 // |<---------- title->width ----------->|
701 // | |<---- title->job->width ---->| |
703 // .......................................
704 // ....+-----------------------------+....
705 // ....| |....<-- gray border
708 // ....| |<------- image
714 // ....+-----------------------------+....
715 // .......................................
717 static uint8_t * buffer;
718 static int bufferSize;
720 // Make sure we have a big enough buffer to receive the image from libhb. libhb
721 // creates images with a one-pixel border around the original content. Hence we
722 // add 2 pixels horizontally and vertically to the buffer size.
723 int srcWidth = title->width + 2;
724 int srcHeight= title->height + 2;
726 newSize = srcWidth * srcHeight * 4;
727 if( bufferSize < newSize )
729 bufferSize = newSize;
730 buffer = (uint8_t *) realloc( buffer, bufferSize );
733 hb_get_preview( handle, title, pictureIndex, buffer );
735 // Create an NSBitmapImageRep and copy the libhb image into it, converting it from
736 // libhb's format to one suitable for NSImage. Along the way, we'll strip off the
737 // border around libhb's image.
739 // The image data returned by hb_get_preview is 4 bytes per pixel, BGRA format.
742 int dstWidth = title->job->width;
743 int dstHeight = title->job->height;
744 NSBitmapFormat bitmapFormat = (NSBitmapFormat)NSAlphaFirstBitmapFormat;
745 NSBitmapImageRep * imgrep = [[[NSBitmapImageRep alloc]
746 initWithBitmapDataPlanes:nil
750 samplesPerPixel:3 // ignore alpha
753 colorSpaceName:NSCalibratedRGBColorSpace
754 bitmapFormat:bitmapFormat
755 bytesPerRow:dstWidth * 4
756 bitsPerPixel:32] autorelease];
758 int borderTop = (srcHeight - dstHeight) / 2;
759 int borderLeft = (srcWidth - dstWidth) / 2;
761 UInt32 * src = (UInt32 *)buffer;
762 UInt32 * dst = (UInt32 *)[imgrep bitmapData];
763 src += borderTop * srcWidth; // skip top rows in src to get to first row of dst
764 src += borderLeft; // skip left pixels in src to get to first pixel of dst
765 for (int r = 0; r < dstHeight; r++)
767 for (int c = 0; c < dstWidth; c++)
768 #if TARGET_RT_LITTLE_ENDIAN
769 *dst++ = Endian32_Swap(*src++);
773 src += (srcWidth - dstWidth); // skip to next row in src
776 NSImage * img = [[[NSImage alloc] initWithSize: NSMakeSize(dstWidth, dstHeight)] autorelease];
777 [img addRepresentation:imgrep];
783 // Make sure we have big enough buffer
784 static uint8_t * buffer;
785 static int bufferSize;
788 newSize = ( title->width + 2 ) * (title->height + 2 ) * 4;
789 if( bufferSize < newSize )
791 bufferSize = newSize;
792 buffer = (uint8_t *) realloc( buffer, bufferSize );
795 hb_get_preview( handle, title, pictureIndex, buffer );
797 // The image data returned by hb_get_preview is 4 bytes per pixel, BGRA format.
798 // We'll copy that into an NSImage swapping it to ARGB in the process. Alpha is
800 int width = title->width + 2; // hblib adds a one-pixel border to the image
801 int height = title->height + 2;
802 int numPixels = width * height;
803 NSBitmapFormat bitmapFormat = (NSBitmapFormat)NSAlphaFirstBitmapFormat;
804 NSBitmapImageRep * imgrep = [[[NSBitmapImageRep alloc]
805 initWithBitmapDataPlanes:nil
809 samplesPerPixel:3 // ignore alpha
812 colorSpaceName:NSCalibratedRGBColorSpace
813 bitmapFormat:bitmapFormat
814 bytesPerRow:width * 4
815 bitsPerPixel:32] autorelease];
817 UInt32 * src = (UInt32 *)buffer;
818 UInt32 * dst = (UInt32 *)[imgrep bitmapData];
819 for (int i = 0; i < numPixels; i++)
820 #if TARGET_RT_LITTLE_ENDIAN
821 *dst++ = Endian32_Swap(*src++);
826 NSImage * img = [[[NSImage alloc] initWithSize: NSMakeSize(width, height)] autorelease];
827 [img addRepresentation:imgrep];
833 // Returns the preview image for the specified index, retrieving it from its internal
834 // cache or by calling makeImageForPicture if it is not cached. Generally, you should
835 // use imageForPicture so that images are cached. Calling makeImageForPicture will
836 // always generate a new copy of the image.
837 - (NSImage *) imageForPicture: (int) pictureIndex
839 // The preview for the specified index may not currently exist, so this method
840 // generates it if necessary.
841 NSString * key = [NSString stringWithFormat:@"%d", pictureIndex];
842 NSImage * theImage = [fPicturePreviews objectForKey:key];
845 theImage = [PreviewController makeImageForPicture:pictureIndex libhb:fHandle title:fTitle removeBorders: NO];
846 [fPicturePreviews setObject:theImage forKey:key];
851 // Purges all images from the cache. The next call to imageForPicture will cause a new
852 // image to be generated.
853 - (void) purgeImageCache
855 [fPicturePreviews removeAllObjects];
860 #pragma mark Movie Preview
861 - (IBAction) createMoviePreview: (id) sender
865 /* Lets make sure the still picture previews are showing in case
866 * there is currently a movie showing */
867 [self pictureSliderChanged:nil];
869 /* Rip or Cancel ? */
871 hb_get_state2( fPreviewLibhb, &s );
873 if(sender == fCancelPreviewMovieButton && (s.state == HB_STATE_WORKING || s.state == HB_STATE_PAUSED))
877 hb_stop( fPreviewLibhb );
878 [fPictureView setHidden:NO];
879 [fMovieView pause:nil];
880 [fMovieView setHidden:YES];
881 [fPictureSlider setHidden:NO];
888 /* we use controller.mm's prepareJobForPreview to go ahead and set all of our settings
889 * however, we want to use a temporary destination field of course
890 * so that we do not put our temp preview in the users chosen
893 hb_job_t * job = fTitle->job;
895 /* We run our current setting through prepeareJob in Controller.mm
896 * just as if it were a regular encode */
898 [fHBController prepareJobForPreview];
900 /* Destination file. We set this to our preview directory
901 * changing the extension appropriately.*/
902 if (fTitle->job->mux == HB_MUX_MP4) // MP4 file
904 /* we use .m4v for our mp4 files so that ac3 and chapters in mp4 will play properly */
905 fPreviewMoviePath = @"~/Library/Application Support/HandBrake/Previews/preview_temp.m4v";
907 else if (fTitle->job->mux == HB_MUX_MKV) // MKV file
909 fPreviewMoviePath = @"~/Library/Application Support/HandBrake/Previews/preview_temp.mkv";
911 else if (fTitle->job->mux == HB_MUX_AVI) // AVI file
913 fPreviewMoviePath = @"~/Library/Application Support/HandBrake/Previews/preview_temp.avi";
915 else if (fTitle->job->mux == HB_MUX_OGM) // OGM file
917 fPreviewMoviePath = @"~/Library/Application Support/HandBrake/Previews/preview_temp.ogm";
920 fPreviewMoviePath = [[fPreviewMoviePath stringByExpandingTildeInPath]retain];
922 /* See if there is an existing preview file, if so, delete it */
923 if( ![[NSFileManager defaultManager] fileExistsAtPath:fPreviewMoviePath] )
925 [[NSFileManager defaultManager] removeFileAtPath:fPreviewMoviePath
929 /* We now direct our preview encode to fPreviewMoviePath */
930 fTitle->job->file = [fPreviewMoviePath UTF8String];
932 /* We use our advance pref to determine how many previews to scan */
933 int hb_num_previews = [[[NSUserDefaults standardUserDefaults] objectForKey:@"PreviewsNumber"] intValue];
934 job->start_at_preview = fPicture + 1;
935 job->seek_points = hb_num_previews;
937 /* we use the preview duration popup to get the specified
938 * number of seconds for the preview encode.
941 job->pts_to_stop = [[fPreviewMovieLengthPopUp titleOfSelectedItem] intValue] * 90000LL;
943 /* lets go ahead and send it off to libhb
944 * Note: unlike a full encode, we only send 1 pass regardless if the final encode calls for 2 passes.
945 * this should suffice for a fairly accurate short preview and cuts our preview generation time in half.
947 hb_add( fPreviewLibhb, job );
949 [fEncodingControlBox setHidden: NO];
950 [fPictureControlBox setHidden: YES];
952 [fMovieCreationProgressIndicator setHidden: NO];
953 [fPreviewMovieStatusField setHidden: NO];
959 /* Let fPreviewLibhb do the job */
960 hb_start( fPreviewLibhb );
964 - (void) startReceivingLibhbNotifications
968 fLibhbTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(libhbTimerFired:) userInfo:nil repeats:YES];
969 [fLibhbTimer retain];
973 - (void) stopReceivingLibhbNotifications
977 [fLibhbTimer invalidate];
978 [fLibhbTimer release];
982 - (void) libhbTimerFired: (NSTimer*)theTimer
985 hb_get_state( fPreviewLibhb, &s );
986 [self libhbStateChanged: s];
990 - (void) libhbStateChanged: (hb_state_t &)state
992 switch( state.state )
995 case HB_STATE_SCANNING:
996 case HB_STATE_SCANDONE:
999 case HB_STATE_WORKING:
1001 #define p state.param.working
1003 NSMutableString * string;
1004 /* Update text field */
1005 string = [NSMutableString stringWithFormat: NSLocalizedString( @"Encoding preview: %.2f %%", @"" ), 100.0 * p.progress];
1007 if( p.seconds > -1 )
1009 [string appendFormat:
1010 NSLocalizedString( @" (%.2f fps, avg %.2f fps, ETA %02dh%02dm%02ds)", @"" ),
1011 p.rate_cur, p.rate_avg, p.hours, p.minutes, p.seconds];
1013 [fPreviewMovieStatusField setStringValue: string];
1015 [fMovieCreationProgressIndicator setIndeterminate: NO];
1017 [fMovieCreationProgressIndicator setDoubleValue: 100.0 * p.progress];
1019 [fCreatePreviewMovieButton setTitle: @"Cancel Preview"];
1026 #define p state.param.muxing
1027 case HB_STATE_MUXING:
1029 // Update fMovieCreationProgressIndicator
1030 [fMovieCreationProgressIndicator setIndeterminate: YES];
1031 [fMovieCreationProgressIndicator startAnimation: nil];
1032 [fPreviewMovieStatusField setStringValue: [NSString stringWithFormat:
1033 NSLocalizedString( @"Muxing Preview ...", @"" )]];
1037 case HB_STATE_PAUSED:
1038 [fMovieCreationProgressIndicator stopAnimation: nil];
1041 case HB_STATE_WORKDONE:
1043 // Delete all remaining jobs since libhb doesn't do this on its own.
1045 while( ( job = hb_job(fPreviewLibhb, 0) ) )
1046 hb_rem( fHandle, job );
1048 [fPreviewMovieStatusField setStringValue: @""];
1049 [fPreviewMovieStatusField setHidden: YES];
1051 [fMovieCreationProgressIndicator stopAnimation: nil];
1052 [fMovieCreationProgressIndicator setHidden: YES];
1053 [fEncodingControlBox setHidden: YES];
1055 /* we make sure the picture slider and preview match */
1056 [self pictureSliderChanged:nil];
1059 // Show the movie view
1062 [self showMoviePreview:fPreviewMoviePath];
1065 [fCreatePreviewMovieButton setTitle: @"Live Preview"];
1074 - (IBAction) showMoviePreview: (NSString *) path
1076 /* Since the gray background for the still images is part of
1077 * fPictureView, lets leave the picture view visible and postion
1078 * the fMovieView over the image portion of fPictureView so
1079 * we retain the gray cropping border we have already established
1080 * with the still previews
1082 [fMovieView setHidden:NO];
1084 /* Load the new movie into fMovieView */
1089 [fMovieView setControllerVisible: YES];
1090 /* let's make sure there is no movie currently set */
1091 [fMovieView setMovie:nil];
1093 aMovie = [QTMovie movieWithFile:path error:nil];
1095 /* we get some size information from the preview movie */
1096 NSSize movieSize= [[aMovie attributeForKey:QTMovieNaturalSizeAttribute] sizeValue];
1097 movieBounds = [fMovieView movieBounds];
1098 movieBounds.size.height = movieSize.height;
1100 if ([fMovieView isControllerVisible])
1101 movieBounds.size.height += [fMovieView controllerBarHeight];
1102 /* since for whatever the reason I cannot seem to get the [fMovieView controllerBarHeight]
1103 * For now just use 15 for additional height as it seems to line up well
1105 movieBounds.size.height += 15;
1107 movieBounds.size.width = movieSize.width;
1109 /* We need to find out if the preview movie needs to be scaled down so
1110 * that it doesn't overflow our available viewing container (just like for image
1111 * in -displayPreview) for HD sources, etc. [fPictureViewArea frame].size.height*/
1112 if( ((int)movieBounds.size.height) > [fPictureView frame].size.height || scaleToScreen == YES)
1114 /* The preview movie would be larger than the available viewing area
1115 * in the preview movie, so we go ahead and scale it down to the same size
1116 * as the still preview or we readjust our window to allow for the added height if need be
1118 NSSize displaySize = NSMakeSize( (float)movieBounds.size.width, (float)movieBounds.size.height );
1119 NSSize viewSize = [self optimalViewSizeForImageSize:displaySize];
1120 if( [self viewNeedsToResizeToSize:viewSize] )
1123 [self resizeSheetForViewSize:viewSize];
1124 [self setViewSize:viewSize];
1128 [fMovieView setPreservesAspectRatio:YES];
1129 [fMovieView setFrameSize:viewSize];
1133 /* Since the preview movie is smaller than the available viewing area
1134 * we can go ahead and use the preview movies native size */
1135 [fMovieView setFrameSize:movieBounds.size];
1142 // lets reposition the movie if need be
1144 NSPoint origin = [fPictureViewArea frame].origin;
1145 origin.x += trunc(([fPictureViewArea frame].size.width -
1146 [fMovieView frame].size.width) / 2.0);
1147 /* We need to detect whether or not we are currently less than the available height.*/
1148 if (movieBounds.size.height < [fPictureView frame].size.height)
1150 /* If we are, we are adding 15 to the height to allow for the controller bar so
1151 * we need to subtract half of that for the origin.y to get the controller bar
1152 * below the movie to it lines up vertically with where our still preview was
1154 origin.y += trunc((([fPictureViewArea frame].size.height -
1155 [fMovieView frame].size.height) / 2.0) - 7.5);
1159 /* if we are >= to the height of the picture view area, the controller bar
1160 * gets taken care of with picture resizing, so we do not want to offset the height
1162 origin.y += trunc(([fPictureViewArea frame].size.height -
1163 [fMovieView frame].size.height) / 2.0);
1165 [fMovieView setFrameOrigin:origin];
1167 [fMovieView setMovie:aMovie];
1168 /// to actually play the movie
1169 [fMovieView play:aMovie];
1181 @implementation PreviewController (Private)
1184 // -[PictureController(Private) optimalViewSizeForImageSize:]
1186 // Given the size of the preview image to be shown, returns the best possible
1187 // size for the view.
1189 - (NSSize)optimalViewSizeForImageSize: (NSSize)imageSize
1191 // The min size is 320x240
1192 CGFloat minWidth = 480.0;
1193 CGFloat minHeight = 360.0;
1195 NSSize screenSize = [[NSScreen mainScreen] frame].size;
1196 NSSize sheetSize = [[self window] frame].size;
1197 NSSize viewAreaSize = [fPictureViewArea frame].size;
1198 CGFloat paddingX = sheetSize.width - viewAreaSize.width;
1199 CGFloat paddingY = sheetSize.height - viewAreaSize.height;
1205 /* We are in full screen mode so lets use the full screen if we need to */
1206 maxWidth = screenSize.width - paddingX;
1207 maxHeight = screenSize.height - paddingY;
1211 // The max size of the view is when the sheet is taking up 85% of the screen.
1212 maxWidth = (0.85 * screenSize.width) - paddingX;
1213 maxHeight = (0.85 * screenSize.height) - paddingY;
1216 NSSize resultSize = imageSize;
1218 // Its better to have a view that's too small than a view that's too big, so
1219 // apply the maximum constraints last.
1220 if( resultSize.width < minWidth )
1222 resultSize.height *= (minWidth / resultSize.width);
1223 resultSize.width = minWidth;
1225 if( resultSize.height < minHeight )
1227 resultSize.width *= (minHeight / resultSize.height);
1228 resultSize.height = minHeight;
1230 if( resultSize.width > maxWidth )
1232 resultSize.height *= (maxWidth / resultSize.width);
1233 resultSize.width = maxWidth;
1235 if( resultSize.height > maxHeight )
1237 resultSize.width *= (maxHeight / resultSize.height);
1238 resultSize.height = maxHeight;
1241 if (scaleToScreen == YES)
1243 //CGFloat scaleToScreenWidth;
1244 //CGFloat scaleToScreenHeight;
1245 CGFloat screenAspect;
1246 CGFloat viewAreaAspect;
1247 //note, a mbp 15" at 1440 x 900 is a 1.6 ar
1248 screenAspect = screenSize.width / screenSize.height;
1250 // Note, a standard dvd will use 720 x 480 which is a 1.5
1251 viewAreaAspect = viewAreaSize.width / viewAreaSize.height;
1253 if (screenAspect < viewAreaAspect)
1255 resultSize.width = screenSize.width;
1256 resultSize.height = (screenSize.width / viewAreaAspect);
1260 resultSize.height = screenSize.height;
1261 resultSize.width = resultSize.height * viewAreaAspect;
1272 // -[PictureController(Private) resizePanelForViewSize:animate:]
1274 // Resizes the entire window to accomodate a view of a particular size.
1276 - (void)resizeSheetForViewSize: (NSSize)viewSize
1278 // Figure out the deltas for the new frame area
1279 NSSize currentSize = [fPictureViewArea frame].size;
1280 CGFloat deltaX = viewSize.width - currentSize.width;
1281 CGFloat deltaY = viewSize.height - currentSize.height;
1283 // Now resize the whole panel by those same deltas, but don't exceed the min
1284 NSRect frame = [[self window] frame];
1285 NSSize maxSize = [[self window] maxSize];
1286 NSSize minSize = [[self window] minSize];
1287 frame.size.width += deltaX;
1288 frame.size.height += deltaY;
1289 if( frame.size.width < minSize.width )
1291 frame.size.width = minSize.width;
1294 if( frame.size.height < minSize.height )
1296 frame.size.height = minSize.height;
1300 // But now the sheet is off-center, so also shift the origin to center it and
1301 // keep the top aligned.
1302 if( frame.size.width != [[self window] frame].size.width )
1303 frame.origin.x -= (deltaX / 2.0);
1307 if( frame.size.height != [[self window] frame].size.height )
1309 frame.origin.y -= (deltaY / 2.0);
1313 if( frame.size.height != [[self window] frame].size.height )
1314 frame.origin.y -= deltaY;
1317 [[self window] setFrame:frame display:YES animate:NO];
1321 /* Since upon launch we can open up the preview window if it was open
1322 * the last time we quit (and at the size it was) we want to make
1323 * sure that upon resize we do not have the window off the screen
1324 * So check the origin against the screen origin and adjust if
1327 NSSize screenSize = [[[self window] screen] frame].size;
1328 NSPoint screenOrigin = [[[self window] screen] frame].origin;
1329 /* our origin is off the screen to the left*/
1330 if (frame.origin.x < screenOrigin.x)
1332 /* so shift our origin to the right */
1333 frame.origin.x = screenOrigin.x;
1335 else if ((frame.origin.x + frame.size.width) > (screenOrigin.x + screenSize.width))
1337 /* the right side of the preview is off the screen, so shift to the left */
1338 frame.origin.x = (screenOrigin.x + screenSize.width) - frame.size.width;
1341 [[self window] setFrame:frame display:YES animate:YES];
1347 // -[PictureController(Private) setViewSize:]
1349 // Changes the view's size and centers it vertically inside of its area.
1350 // Assumes resizeSheetForViewSize: has already been called.
1352 - (void)setViewSize: (NSSize)viewSize
1354 [fPictureView setFrameSize:viewSize];
1356 // center it vertically
1357 NSPoint origin = [fPictureViewArea frame].origin;
1358 origin.y += ([fPictureViewArea frame].size.height -
1359 [fPictureView frame].size.height) / 2.0;
1360 [fPictureView setFrameOrigin:origin];
1362 NSPoint controlboxorigin = [fPictureView frame].origin;
1364 /* for now, put the origin.y 100 above the bottom of the fPictureView */
1365 controlboxorigin.y += 100;
1367 controlboxorigin.x += ([fPictureViewArea frame].size.width -
1368 [fPictureControlBox frame].size.width) / 2.0;
1369 /* requires that thefPictureControlBox and the fEncodingControlBox
1370 * are the same width to line up.
1372 [fPictureControlBox setFrameOrigin:controlboxorigin];
1373 [fEncodingControlBox setFrameOrigin:controlboxorigin];
1378 - (BOOL)viewNeedsToResizeToSize: (NSSize)newSize
1380 NSSize viewSize = [fPictureView frame].size;
1381 return (newSize.width != viewSize.width || newSize.height != viewSize.height);