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 @implementation QTMovieView ( HBQTkitExt )
11 - (void) mouseMoved:(NSEvent *)theEvent
13 [super mouseMoved:theEvent];
17 @interface PreviewController (Private)
19 - (NSSize)optimalViewSizeForImageSize: (NSSize)imageSize;
20 - (void)resizeSheetForViewSize: (NSSize)viewSize;
21 - (void)setViewSize: (NSSize)viewSize;
22 - (BOOL)viewNeedsToResizeToSize: (NSSize)newSize;
26 @implementation PreviewController
30 if (self = [super initWithWindowNibName:@"PicturePreview"])
32 // NSWindowController likes to lazily load its window. However since
33 // this controller tries to set all sorts of outlets before the window
34 // is displayed, we need it to load immediately. The correct way to do
35 // this, according to the documentation, is simply to invoke the window
38 // If/when we switch a lot of this stuff to bindings, this can probably
42 fPicturePreviews = [[NSMutableDictionary dictionaryWithCapacity: HB_NUM_HBLIB_PICTURES] retain];
43 /* Init libhb with check for updates libhb style set to "0" so its ignored and lets sparkle take care of it */
44 int loggingLevel = [[[NSUserDefaults standardUserDefaults] objectForKey:@"LoggingLevel"] intValue];
45 fPreviewLibhb = hb_init(loggingLevel, 0);
53 //------------------------------------------------------------------------------------
54 // Displays and brings the picture window to the front
55 //------------------------------------------------------------------------------------
56 - (IBAction) showPreviewWindow: (id)sender
58 [self showWindow:sender];
59 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"PreviewWindowIsOpen"];
61 /* lets set the preview window to accept mouse moved events */
62 [fPreviewWindow setAcceptsMouseMovedEvents:YES];
64 [self pictureSliderChanged:nil];
65 [self startReceivingLibhbNotifications];
68 - (void)setHBController: (HBController *)controller
70 fHBController = controller;
75 [fPreviewWindow setDelegate:self];
76 if( ![[self window] setFrameUsingName:@"Preview"] )
77 [[self window] center];
78 [self setWindowFrameAutosaveName:@"Preview"];
79 [[self window] setExcludedFromWindowsMenu:YES];
81 /* lets set the preview window to accept mouse moved events */
82 [fPreviewWindow setAcceptsMouseMovedEvents:YES];
83 //[self pictureSliderChanged:nil];
84 [self startReceivingLibhbNotifications];
87 /* we set the progress indicator to not use threaded animation
88 * as it causes a conflict with the qtmovieview's controllerbar
90 [fMovieCreationProgressIndicator setUsesThreadedAnimation:NO];
92 /* Setup our layers for core animation */
93 [fPictureViewArea setWantsLayer:YES];
94 [fPictureView setWantsLayer:YES];
96 [fCancelPreviewMovieButton setWantsLayer:YES];
97 [fMovieCreationProgressIndicator setWantsLayer:YES];
99 [fPictureControlBox setWantsLayer:YES];
100 [fEncodingControlBox setWantsLayer:YES];
101 [fMovieView setWantsLayer:YES];
102 [fMovieView setHidden:YES];
103 [fMovieView setDelegate:self];
105 /* Since the xib has everything off center for easy acess
106 * we align our views and windows here we an align to anything
107 * since it will actually change later upon source load, but
108 * for convenience we will use the fPictureViewArea
111 /* Align the still preview image view to the picture box */
112 [fPictureView setFrameSize:[fPictureViewArea frame].size];
113 [fMovieView setFrameSize:[fPictureViewArea frame].size];
114 //[fPreviewWindow setFrameSize:[fPictureViewArea frame].size];
118 - (BOOL)acceptsMouseMovedEvents
123 - (void)windowWillClose:(NSNotification *)aNotification
125 /* Upon Closing the picture window, we make sure we clean up any
126 * preview movie that might be playing
128 hb_stop( fPreviewLibhb );
130 // Show the picture view
131 [fPictureView setHidden:NO];
132 [fMovieView pause:nil];
133 [fMovieView setHidden:YES];
134 [fMovieView setMovie:nil];
137 [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"PreviewWindowIsOpen"];
140 - (BOOL)windowShouldClose:(id)fPictureWindow
147 hb_stop(fPreviewLibhb);
148 if (fPreviewMoviePath)
150 [[NSFileManager defaultManager] removeFileAtPath:fPreviewMoviePath handler:nil];
151 [fPreviewMoviePath release];
154 [fLibhbTimer invalidate];
155 [fLibhbTimer release];
157 [fHudTimer invalidate];
160 [fPicturePreviews release];
161 [fFullScreenWindow release];
163 hb_close(&fPreviewLibhb);
168 - (void) SetHandle: (hb_handle_t *) handle
174 /* we set the preview length popup in seconds */
175 [fPreviewMovieLengthPopUp removeAllItems];
176 [fPreviewMovieLengthPopUp addItemWithTitle: @"5"];
177 [fPreviewMovieLengthPopUp addItemWithTitle: @"10"];
178 [fPreviewMovieLengthPopUp addItemWithTitle: @"15"];
179 [fPreviewMovieLengthPopUp addItemWithTitle: @"20"];
180 [fPreviewMovieLengthPopUp addItemWithTitle: @"25"];
181 [fPreviewMovieLengthPopUp addItemWithTitle: @"30"];
182 [fPreviewMovieLengthPopUp addItemWithTitle: @"35"];
183 [fPreviewMovieLengthPopUp addItemWithTitle: @"40"];
184 [fPreviewMovieLengthPopUp addItemWithTitle: @"45"];
185 [fPreviewMovieLengthPopUp addItemWithTitle: @"50"];
186 [fPreviewMovieLengthPopUp addItemWithTitle: @"55"];
187 [fPreviewMovieLengthPopUp addItemWithTitle: @"60"];
189 /* adjust the preview slider length */
190 /* We use our advance pref to determine how many previews we scanned */
191 int hb_num_previews = [[[NSUserDefaults standardUserDefaults] objectForKey:@"PreviewsNumber"] intValue];
192 [fPictureSlider setMaxValue: hb_num_previews - 1.0];
193 [fPictureSlider setNumberOfTickMarks: hb_num_previews];
195 if ([[NSUserDefaults standardUserDefaults] objectForKey:@"PreviewLength"])
197 [fPreviewMovieLengthPopUp selectItemWithTitle:[[NSUserDefaults standardUserDefaults] objectForKey:@"PreviewLength"]];
201 /* currently hard set default to 10 seconds */
202 [fPreviewMovieLengthPopUp selectItemAtIndex: 1];
206 - (void) SetTitle: (hb_title_t *) title
208 hb_job_t * job = title->job;
212 MaxOutputWidth = title->width - job->crop[2] - job->crop[3];
213 MaxOutputHeight = title->height - job->crop[0] - job->crop[1];
215 [self SettingsChanged: nil];
217 /* set the top of the hud controller boxes centered vertically with the origin of our window */
218 NSPoint hudControlBoxOrigin = [fPictureControlBox frame].origin;
219 hudControlBoxOrigin.y = ([[self window] frame].size.height / 2) - [fPictureControlBox frame].size.height;
220 [fPictureControlBox setFrameOrigin:hudControlBoxOrigin];
221 [fEncodingControlBox setFrameOrigin:hudControlBoxOrigin];
227 // Adjusts the window to draw the current picture (fPicture) adjusting its size as
228 // necessary to display as much of the picture as possible.
229 - (void) displayPreview
231 hb_job_t * job = fTitle->job;
232 /* lets make sure that the still picture view is not hidden and that
233 * the movie preview is
235 [fMovieView pause:nil];
236 [fMovieView setHidden:YES];
237 [fMovieView setMovie:nil];
238 [fMovieCreationProgressIndicator stopAnimation: nil];
239 [fMovieCreationProgressIndicator setHidden: YES];
241 [fPictureView setHidden:NO];
243 NSImage *fPreviewImage = [self imageForPicture: fPicture];
244 NSSize imageScaledSize = [fPreviewImage size];
245 [fPictureView setImage: fPreviewImage];
247 NSSize displaySize = NSMakeSize( ( CGFloat )fTitle->width, ( CGFloat )fTitle->height );
248 NSString *sizeInfoString;
249 /* Set the picture size display fields below the Preview Picture*/
250 if( fTitle->job->anamorphic.mode == 1 ) // Original PAR Implementation
252 output_width = fTitle->width-fTitle->job->crop[2]-fTitle->job->crop[3];
253 output_height = fTitle->height-fTitle->job->crop[0]-fTitle->job->crop[1];
254 display_width = output_width * fTitle->job->anamorphic.par_width / fTitle->job->anamorphic.par_height;
255 sizeInfoString = [NSString stringWithFormat:
256 @"Source: %dx%d, Output: %dx%d, Anamorphic: %dx%d Strict",
257 fTitle->width, fTitle->height, output_width, output_height, display_width, output_height];
259 displaySize.width = display_width;
260 displaySize.height = fTitle->height;
261 imageScaledSize.width = display_width;
262 imageScaledSize.height = output_height;
264 else if (fTitle->job->anamorphic.mode == 2) // Loose Anamorphic
266 hb_set_anamorphic_size(job, &output_width, &output_height, &output_par_width, &output_par_height);
267 display_width = output_width * output_par_width / output_par_height;
268 sizeInfoString = [NSString stringWithFormat:
269 @"Source: %dx%d, Output: %dx%d, Anamorphic: %dx%d Loose",
270 fTitle->width, fTitle->height, output_width, output_height, display_width, output_height];
272 displaySize.width = display_width;
273 displaySize.height = fTitle->height;
274 imageScaledSize.width = display_width;
275 imageScaledSize.height = output_height;
277 else if (fTitle->job->anamorphic.mode == 3) // Custom Anamorphic
279 hb_set_anamorphic_size(job, &output_width, &output_height, &output_par_width, &output_par_height);
280 display_width = output_width * output_par_width / output_par_height;
281 sizeInfoString = [NSString stringWithFormat:
282 @"Source: %dx%d, Output: %dx%d, Anamorphic: %dx%d Custom",
283 fTitle->width, fTitle->height, output_width, output_height, fTitle->job->anamorphic.dar_width, fTitle->job->anamorphic.dar_height];
285 displaySize.width = fTitle->job->anamorphic.dar_width + fTitle->job->crop[2] + fTitle->job->crop[3] ;
286 displaySize.height = fTitle->job->anamorphic.dar_height + fTitle->job->crop[0] + fTitle->job->crop[1];
287 imageScaledSize.width = (int)fTitle->job->anamorphic.dar_width;
288 imageScaledSize.height = (int)fTitle->job->height;
290 else // No Anamorphic
292 sizeInfoString = [NSString stringWithFormat:
293 @"Source: %dx%d, Output: %dx%d", fTitle->width, fTitle->height,
294 fTitle->job->width, fTitle->job->height];
296 displaySize.width = fTitle->width;
297 displaySize.height = fTitle->height;
298 imageScaledSize.width = fTitle->job->width;
299 imageScaledSize.height = fTitle->job->height;
304 NSSize viewSize = [self optimalViewSizeForImageSize:displaySize];
305 [self resizeSheetForViewSize:viewSize];
307 NSSize windowSize = [[self window] frame].size;
309 if (scaleToScreen == YES)
311 /* Note: this should probably become a utility function */
312 /* We are in Scale To Screen mode so, we have to get the ratio for height and width against the window
313 *size so we can scale from there.
315 CGFloat deltaWidth = imageScaledSize.width / displaySize.width;
316 CGFloat deltaHeight = imageScaledSize.height /displaySize.height;
317 NSSize windowSize = [[self window] frame].size;
318 CGFloat pictureAspectRatio = imageScaledSize.width / imageScaledSize.height;
320 /* Set our min size to the storage size */
322 minSize.width = fTitle->width;
323 minSize.height = fTitle->height;
325 /* Set delta's based on minimum size */
326 if (imageScaledSize.width < minSize.width)
328 deltaWidth = imageScaledSize.width / minSize.width;
335 if (imageScaledSize.height < minSize.height)
337 deltaHeight = imageScaledSize.height / minSize.height;
344 /* Now apply our deltas to the full screen view */
345 if (pictureAspectRatio > 1.0) // we are wider than taller, so expand the width to fill the area and scale the height
347 viewSize.width = windowSize.width * deltaWidth;
348 viewSize.height = viewSize.width / pictureAspectRatio;
353 viewSize.height = windowSize.height * deltaHeight;
354 viewSize.width = viewSize.height * pictureAspectRatio;
360 viewSize.width = viewSize.width - (viewSize.width - imageScaledSize.width);
361 viewSize.height = viewSize.height - (viewSize.height - imageScaledSize.height);
363 if (fTitle->width > windowSize.width || fTitle->height > windowSize.height)
365 CGFloat viewSizeAspect = viewSize.width / viewSize.height;
366 if (viewSizeAspect > 1.0) // we are wider than taller, so expand the width to fill the area and scale the height
368 viewSize.width = viewSize.width * (windowSize.width / fTitle->width) ;
369 viewSize.height = viewSize.width / viewSizeAspect;
373 viewSize.height = viewSize.height * (windowSize.height / fTitle->height);
374 viewSize.width = viewSize.height * viewSizeAspect;
380 [self setViewSize:viewSize];
382 NSString *scaleString;
383 CGFloat scale = ( ( CGFloat )[fPictureView frame].size.width) / ( ( CGFloat )imageScaledSize.width);
384 if (scale * 100.0 != 100)
386 scaleString = [NSString stringWithFormat:
387 NSLocalizedString( @" (%.0f%% actual size)",
388 @"String shown when a preview is scaled" ), scale * 100.0];
392 scaleString = @"(Actual size)";
395 if (scaleToScreen == YES)
397 scaleString = [scaleString stringByAppendingString:@" Scaled To Screen"];
399 /* Set the info fields in the hud controller */
400 [fInfoField setStringValue: [NSString stringWithFormat:
401 @"%@", sizeInfoString]];
403 [fscaleInfoField setStringValue: [NSString stringWithFormat:
404 @"%@", scaleString]];
405 /* Set the info field in the window title bar */
406 [[self window] setTitle:[NSString stringWithFormat: @"Preview - %@ %@",sizeInfoString, scaleString]];
409 - (IBAction) previewDurationPopUpChanged: (id) sender
412 [[NSUserDefaults standardUserDefaults] setObject:[fPreviewMovieLengthPopUp titleOfSelectedItem] forKey:@"PreviewLength"];
416 - (IBAction) SettingsChanged: (id) sender
418 // Purge the existing picture previews so they get recreated the next time
420 [self purgeImageCache];
421 [self pictureSliderChanged:nil];
424 - (IBAction) pictureSliderChanged: (id) sender
426 // Show the picture view
427 [fPictureView setHidden:NO];
428 [fMovieView pause:nil];
429 [fMovieView setHidden:YES];
430 [fMovieView setMovie:nil];
431 [fEncodingControlBox setHidden: YES];
433 int newPicture = [fPictureSlider intValue];
434 if (newPicture != fPicture)
436 fPicture = newPicture;
438 [self displayPreview];
442 - (IBAction)showPreviewPanel: (id)sender forTitle: (hb_title_t *)title
444 if ([fPreviewWindow isVisible])
446 [fPreviewWindow close];
450 [self showWindow:sender];
451 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"PreviewWindowIsOpen"];
452 [fPreviewWindow setAcceptsMouseMovedEvents:YES];
454 [self pictureSliderChanged:nil];
459 - (NSString*) pictureSizeInfoString
461 return [fInfoField stringValue];
464 - (IBAction)showPictureSettings:(id)sender
466 [fHBController showPicturePanel:self];
469 #pragma mark Hud Control Overlay
470 - (void) mouseMoved:(NSEvent *)theEvent
472 [super mouseMoved:theEvent];
473 NSPoint mouseLoc = [theEvent locationInWindow];
475 /* Test for mouse location to show/hide hud controls */
476 if( isEncoding == NO ) {
477 if( NSPointInRect( mouseLoc, [fPictureControlBox frame] ) )
479 [[fPictureControlBox animator] setHidden: NO];
482 else if( NSPointInRect( mouseLoc, [fPictureViewArea frame] ) )
484 [[fPictureControlBox animator] setHidden: NO];
485 [self startHudTimer];
488 [[fPictureControlBox animator] setHidden: YES];
492 - (void) startHudTimer
495 [fHudTimer invalidate];
498 fHudTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(hudTimerFired:) userInfo:nil repeats:YES];
502 - (void) stopHudTimer
506 [fHudTimer invalidate];
513 - (void) hudTimerFired: (NSTimer*)theTimer
516 if( hudTimerSeconds >= 10 ) {
517 [[fPictureControlBox animator] setHidden: YES];
524 - (IBAction)toggleScaleToScreen:(id)sender
526 if (scaleToScreen == YES)
529 /* make sure we are set to a still preview */
530 [self pictureSliderChanged:nil];
531 [fScaleToScreenToggleButton setTitle:@"Scale To Screen"];
536 /* make sure we are set to a still preview */
537 [self pictureSliderChanged:nil];
538 [fScaleToScreenToggleButton setTitle:@"Actual Scale"];
545 // Title-less windows normally don't receive key presses, override this
546 - (BOOL)canBecomeKeyWindow
551 // Title-less windows normally can't become main which means that another
552 // non-fullscreen window will have the "active" titlebar in expose. Bad, fix it.
553 - (BOOL)canBecomeMainWindow
559 - (IBAction)goWindowedScreen:(id)sender
562 /* Get the screen info to release the display but don't actually do
563 * it until the windowed screen is setup.
566 [self pictureSliderChanged:nil];
567 [fScaleToScreenToggleButton setTitle:@"<->"];
569 NSScreen* mainScreen = [NSScreen mainScreen];
570 NSDictionary* screenInfo = [mainScreen deviceDescription];
571 NSNumber* screenID = [screenInfo objectForKey:@"NSScreenNumber"];
572 CGDirectDisplayID displayID = (CGDirectDisplayID)[screenID longValue];
574 [fFullScreenWindow dealloc];
575 [fFullScreenWindow release];
578 [fPreviewWindow setContentView:fPictureViewArea];
579 [fPictureViewArea setNeedsDisplay:YES];
580 [self setWindow:fPreviewWindow];
583 [fPreviewWindow makeKeyAndOrderFront:self];
585 /* Set the window back to regular level */
586 [fPreviewWindow setLevel:NSNormalWindowLevel];
588 /* Set the isFullScreen flag back to NO */
591 /* make sure we are set to a still preview */
592 [self pictureSliderChanged:nil];
593 [self showPreviewWindow:nil];
595 /* Change the name of fFullScreenToggleButton appropriately */
596 //[fFullScreenToggleButton setTitle: @"Full Screen"];
597 // [fScaleToScreenToggleButton setHidden:YES];
598 /* set the picture settings pallete back to normal level */
599 [fHBController picturePanelWindowed];
601 /* Release the display now that the we are back in windowed mode */
602 CGDisplayRelease(displayID);
604 [fPreviewWindow setAcceptsMouseMovedEvents:YES];
605 //[fFullScreenWindow setAcceptsMouseMovedEvents:NO];
608 [self startHudTimer];
613 #pragma mark Still Preview Image Processing
616 // This function converts an image created by libhb (specified via pictureIndex) into
617 // an NSImage suitable for the GUI code to use. If removeBorders is YES,
618 // makeImageForPicture crops the image generated by libhb stripping off the gray
619 // border around the content. This is the low-level method that generates the image.
620 // -imageForPicture calls this function whenever it can't find an image in its cache.
621 + (NSImage *) makeImageForPicture: (int)pictureIndex
622 libhb:(hb_handle_t*)handle
623 title:(hb_title_t*)title
625 static uint8_t * buffer;
626 static int bufferSize;
628 // Make sure we have a big enough buffer to receive the image from libhb. libhb
629 int dstWidth = title->job->width;
630 int dstHeight = title->job->height;
633 newSize = dstWidth * dstHeight * 4;
634 if( bufferSize < newSize )
636 bufferSize = newSize;
637 buffer = (uint8_t *) realloc( buffer, bufferSize );
640 hb_get_preview( handle, title, pictureIndex, buffer );
642 // Create an NSBitmapImageRep and copy the libhb image into it, converting it from
643 // libhb's format to one suitable for NSImage. Along the way, we'll strip off the
644 // border around libhb's image.
646 // The image data returned by hb_get_preview is 4 bytes per pixel, BGRA format.
649 NSBitmapFormat bitmapFormat = (NSBitmapFormat)NSAlphaFirstBitmapFormat;
650 NSBitmapImageRep * imgrep = [[[NSBitmapImageRep alloc]
651 initWithBitmapDataPlanes:nil
655 samplesPerPixel:3 // ignore alpha
658 colorSpaceName:NSCalibratedRGBColorSpace
659 bitmapFormat:bitmapFormat
660 bytesPerRow:dstWidth * 4
661 bitsPerPixel:32] autorelease];
663 UInt32 * src = (UInt32 *)buffer;
664 UInt32 * dst = (UInt32 *)[imgrep bitmapData];
666 for (r = 0; r < dstHeight; r++)
668 for (c = 0; c < dstWidth; c++)
669 #if TARGET_RT_LITTLE_ENDIAN
670 *dst++ = Endian32_Swap(*src++);
676 NSImage * img = [[[NSImage alloc] initWithSize: NSMakeSize(dstWidth, dstHeight)] autorelease];
677 [img addRepresentation:imgrep];
682 // Returns the preview image for the specified index, retrieving it from its internal
683 // cache or by calling makeImageForPicture if it is not cached. Generally, you should
684 // use imageForPicture so that images are cached. Calling makeImageForPicture will
685 // always generate a new copy of the image.
686 - (NSImage *) imageForPicture: (int) pictureIndex
688 // The preview for the specified index may not currently exist, so this method
689 // generates it if necessary.
690 NSString * key = [NSString stringWithFormat:@"%d", pictureIndex];
691 NSImage * theImage = [fPicturePreviews objectForKey:key];
694 theImage = [PreviewController makeImageForPicture:pictureIndex libhb:fHandle title:fTitle];
695 [fPicturePreviews setObject:theImage forKey:key];
700 // Purges all images from the cache. The next call to imageForPicture will cause a new
701 // image to be generated.
702 - (void) purgeImageCache
704 [fPicturePreviews removeAllObjects];
709 #pragma mark Movie Preview
710 - (IBAction) createMoviePreview: (id) sender
714 /* Lets make sure the still picture previews are showing in case
715 * there is currently a movie showing */
716 [self pictureSliderChanged:nil];
718 /* Rip or Cancel ? */
720 hb_get_state2( fPreviewLibhb, &s );
722 if(sender == fCancelPreviewMovieButton && (s.state == HB_STATE_WORKING || s.state == HB_STATE_PAUSED))
724 hb_stop( fPreviewLibhb );
725 [fPictureView setHidden:NO];
726 [fMovieView pause:nil];
727 [fMovieView setHidden:YES];
728 [fMovieView setMovie:nil];
729 [fPictureSlider setHidden:NO];
736 /* we use controller.mm's prepareJobForPreview to go ahead and set all of our settings
737 * however, we want to use a temporary destination field of course
738 * so that we do not put our temp preview in the users chosen
741 hb_job_t * job = fTitle->job;
743 /* We run our current setting through prepeareJob in Controller.mm
744 * just as if it were a regular encode */
746 [fHBController prepareJobForPreview];
748 /* Destination file. We set this to our preview directory
749 * changing the extension appropriately.*/
750 if (fTitle->job->mux == HB_MUX_MP4) // MP4 file
752 /* we use .m4v for our mp4 files so that ac3 and chapters in mp4 will play properly */
753 fPreviewMoviePath = @"~/Library/Application Support/HandBrake/Previews/preview_temp.m4v";
755 else if (fTitle->job->mux == HB_MUX_MKV) // MKV file
757 fPreviewMoviePath = @"~/Library/Application Support/HandBrake/Previews/preview_temp.mkv";
759 else if (fTitle->job->mux == HB_MUX_AVI) // AVI file
761 fPreviewMoviePath = @"~/Library/Application Support/HandBrake/Previews/preview_temp.avi";
763 else if (fTitle->job->mux == HB_MUX_OGM) // OGM file
765 fPreviewMoviePath = @"~/Library/Application Support/HandBrake/Previews/preview_temp.ogm";
768 fPreviewMoviePath = [[fPreviewMoviePath stringByExpandingTildeInPath]retain];
770 /* See if there is an existing preview file, if so, delete it */
771 if( ![[NSFileManager defaultManager] fileExistsAtPath:fPreviewMoviePath] )
773 [[NSFileManager defaultManager] removeFileAtPath:fPreviewMoviePath
777 /* We now direct our preview encode to fPreviewMoviePath */
778 fTitle->job->file = [fPreviewMoviePath UTF8String];
780 /* We use our advance pref to determine how many previews to scan */
781 int hb_num_previews = [[[NSUserDefaults standardUserDefaults] objectForKey:@"PreviewsNumber"] intValue];
782 job->start_at_preview = fPicture + 1;
783 job->seek_points = hb_num_previews;
785 /* we use the preview duration popup to get the specified
786 * number of seconds for the preview encode.
789 job->pts_to_stop = [[fPreviewMovieLengthPopUp titleOfSelectedItem] intValue] * 90000LL;
791 /* lets go ahead and send it off to libhb
792 * Note: unlike a full encode, we only send 1 pass regardless if the final encode calls for 2 passes.
793 * this should suffice for a fairly accurate short preview and cuts our preview generation time in half.
794 * However we also need to take into account the indepth scan for subtitles.
797 * If scanning we need to do some extra setup of the job.
799 if( job->indepth_scan == 1 )
804 * When subtitle scan is enabled do a fast pre-scan job
805 * which will determine which subtitles to enable, if any.
808 x264opts_tmp = job->x264opts;
810 job->x264opts = NULL;
811 job->indepth_scan = 1;
813 * Add the pre-scan job
815 hb_add( fPreviewLibhb, job );
816 job->x264opts = x264opts_tmp;
818 /* Go ahead and perform the actual encoding preview scan */
819 job->indepth_scan = 0;
821 hb_add( fPreviewLibhb, job );
823 [fEncodingControlBox setHidden: NO];
824 [fPictureControlBox setHidden: YES];
826 [fMovieCreationProgressIndicator setHidden: NO];
827 [fPreviewMovieStatusField setHidden: NO];
831 /* Let fPreviewLibhb do the job */
832 hb_start( fPreviewLibhb );
836 - (void) startReceivingLibhbNotifications
840 fLibhbTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(libhbTimerFired:) userInfo:nil repeats:YES];
841 [fLibhbTimer retain];
845 - (void) stopReceivingLibhbNotifications
849 [fLibhbTimer invalidate];
850 [fLibhbTimer release];
854 - (void) libhbTimerFired: (NSTimer*)theTimer
857 hb_get_state( fPreviewLibhb, &s );
858 [self libhbStateChanged: s];
862 - (void) libhbStateChanged: (hb_state_t)state
864 switch( state.state )
867 case HB_STATE_SCANNING:
868 case HB_STATE_SCANDONE:
871 case HB_STATE_WORKING:
873 #define p state.param.working
875 NSMutableString * string;
876 /* Update text field */
877 string = [NSMutableString stringWithFormat: NSLocalizedString( @"Encoding preview: %.2f %%", @"" ), 100.0 * p.progress];
881 [string appendFormat:
882 NSLocalizedString( @" (%.2f fps, avg %.2f fps, ETA %02dh%02dm%02ds)", @"" ),
883 p.rate_cur, p.rate_avg, p.hours, p.minutes, p.seconds];
885 [fPreviewMovieStatusField setStringValue: string];
887 [fMovieCreationProgressIndicator setIndeterminate: NO];
889 [fMovieCreationProgressIndicator setDoubleValue: 100.0 * p.progress];
891 [fCreatePreviewMovieButton setTitle: @"Cancel Preview"];
898 #define p state.param.muxing
899 case HB_STATE_MUXING:
901 // Update fMovieCreationProgressIndicator
902 [fMovieCreationProgressIndicator setIndeterminate: YES];
903 [fMovieCreationProgressIndicator startAnimation: nil];
904 [fPreviewMovieStatusField setStringValue: [NSString stringWithFormat:
905 NSLocalizedString( @"Muxing Preview ...", @"" )]];
909 case HB_STATE_PAUSED:
910 [fMovieCreationProgressIndicator stopAnimation: nil];
913 case HB_STATE_WORKDONE:
915 // Delete all remaining jobs since libhb doesn't do this on its own.
917 while( ( job = hb_job(fPreviewLibhb, 0) ) )
918 hb_rem( fHandle, job );
920 [fPreviewMovieStatusField setStringValue: @""];
921 [fPreviewMovieStatusField setHidden: YES];
923 [fMovieCreationProgressIndicator stopAnimation: nil];
924 [fMovieCreationProgressIndicator setHidden: YES];
925 [fEncodingControlBox setHidden: YES];
928 // Show the movie view
929 [self showMoviePreview:fPreviewMoviePath];
930 [fCreatePreviewMovieButton setTitle: @"Live Preview"];
937 - (IBAction) showMoviePreview: (NSString *) path
939 /* Since the gray background for the still images is part of
940 * fPictureView, lets leave the picture view visible and postion
941 * the fMovieView over the image portion of fPictureView so
942 * we retain the gray cropping border we have already established
943 * with the still previews
946 /* Load the new movie into fMovieView */
951 NSURL *movieUrl = [NSURL fileURLWithPath:path];
952 NSDictionary *movieAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
953 movieUrl, QTMovieURLAttribute,
954 [NSNumber numberWithBool:NO], QTMovieAskUnresolvedDataRefsAttribute,
955 [NSNumber numberWithBool:YES], @"QTMovieOpenForPlaybackAttribute",
956 [NSNumber numberWithBool:NO], @"QTMovieOpenAsyncRequiredAttribute",
957 [NSNumber numberWithBool:NO], @"QTMovieOpenAsyncOKAttribute",
958 QTMovieApertureModeClean, QTMovieApertureModeAttribute,
961 aMovie = [[[QTMovie alloc] initWithAttributes:movieAttributes error:&outError] autorelease];
965 NSLog(@"Unable to open movie");
970 /* we get some size information from the preview movie */
971 NSSize movieSize= [[aMovie attributeForKey:QTMovieNaturalSizeAttribute] sizeValue];
972 movieBounds = [fMovieView movieBounds];
973 movieBounds.size.height = movieSize.height;
974 /* We also get our view size to use for scaling fMovieView's size */
975 NSSize scaledMovieViewSize = [fPictureView frame].size;
976 if ([fMovieView isControllerVisible])
978 CGFloat controllerBarHeight = [fMovieView controllerBarHeight];
979 if ( controllerBarHeight != 0 ) //Check if QTKit return a real value or not.
981 movieBounds.size.height += controllerBarHeight;
982 scaledMovieViewSize.height += controllerBarHeight;
986 movieBounds.size.height += 15;
987 scaledMovieViewSize.height += 15;
991 movieBounds.size.width = movieSize.width;
993 /* we need to account for an issue where the scaledMovieViewSize > the window size */
994 if (scaledMovieViewSize.height > [[self window] frame].size.height)
996 [fHBController writeToActivityLog: "showMoviePreview: Our window is not tall enough to show the controller bar ..."];
997 /*we need to scale the movie down vertically by 15 px to allow for the controller bar
998 * and scale the width accordingly.
1001 // FIX ME: currently trying to scale everything to show the controller bar does not work right.
1002 // Commented out til fixed, resulting issue when the movie is the full size of the window is no
1003 // controller bar is visible. Live Preview still plays fine though.
1005 CGFloat pictureAspectRatio = scaledMovieViewSize.width / scaledMovieViewSize.height;
1006 scaledMovieViewSize.height = [[self window] frame].size.height - 15;
1007 scaledMovieViewSize.width = scaledMovieViewSize.height * pictureAspectRatio;
1008 NSRect windowFrame = [[self window] frame];
1009 windowFrame.size.width = scaledMovieViewSize.width;
1010 windowFrame.size.height = scaledMovieViewSize.height + 15;
1011 [[self window] setFrame:windowFrame display:YES animate:YES];
1012 [fPictureView setFrameSize:scaledMovieViewSize];
1018 /* Scale the fMovieView to scaledMovieViewSize */
1019 [fMovieView setFrameSize:scaledMovieViewSize];
1021 /*set our origin try using fPictureViewArea or fPictureView */
1022 NSPoint origin = [fPictureView frame].origin;
1023 origin.x += trunc( ( [fPictureView frame].size.width -
1024 [fMovieView frame].size.width ) / 2.0 );
1025 origin.y += trunc( ( ( [fPictureView frame].size.height -
1026 [fMovieView frame].size.height ) / 2.0 ) - 7.5 );
1028 [fMovieView setFrameOrigin:origin];
1029 [fMovieView setMovie:aMovie];
1030 [fMovieView setHidden:NO];
1031 // to actually play the movie
1032 [fMovieView play:aMovie];
1040 @implementation PreviewController (Private)
1043 // -[PictureController(Private) optimalViewSizeForImageSize:]
1045 // Given the size of the preview image to be shown, returns the best possible
1046 // size for the view.
1048 - (NSSize)optimalViewSizeForImageSize: (NSSize)imageSize
1050 // The min size is 480x360
1051 CGFloat minWidth = 480.0;
1052 CGFloat minHeight = 360.0;
1054 NSSize screenSize = [[[self window] screen] visibleFrame].size;
1055 NSSize sheetSize = [[self window] frame].size;
1056 NSSize viewAreaSize = [fPictureViewArea frame].size;
1057 CGFloat paddingX = 0.00;
1058 CGFloat paddingY = 0.00;
1060 if (fTitle->width > screenSize.width || fTitle->height > screenSize.height)
1062 if (scaleToScreen == YES)
1064 paddingX = screenSize.width - imageSize.width;
1065 paddingY = screenSize.height - imageSize.height;
1070 paddingX = sheetSize.width - viewAreaSize.width;
1071 paddingY = sheetSize.height - viewAreaSize.height;
1078 maxWidth = screenSize.width - paddingX;
1079 maxHeight = screenSize.height - paddingY;
1081 NSSize resultSize = imageSize;
1082 CGFloat resultPar = resultSize.width / resultSize.height;
1084 //note, a mbp 15" at 1440 x 900 is a 1.6 ar
1085 CGFloat screenAspect = screenSize.width / screenSize.height;
1086 // Note, a standard dvd will use 720 x 480 which is a 1.5
1087 CGFloat viewAreaAspect = viewAreaSize.width / viewAreaSize.height;
1089 if (scaleToScreen == YES)
1092 if (screenAspect < viewAreaAspect)
1094 resultSize.width = screenSize.width;
1095 resultSize.height = (screenSize.width / viewAreaAspect);
1099 resultSize.height = screenSize.height;
1100 resultSize.width = resultSize.height * viewAreaAspect;
1104 else if ( resultSize.width > maxWidth || resultSize.height > maxHeight )
1106 // Source is larger than screen in one or more dimensions
1107 if ( resultPar > screenAspect )
1109 // Source aspect wider than screen aspect, snap to max width and vary height
1110 resultSize.width = maxWidth;
1111 resultSize.height = (maxWidth / resultPar);
1115 // Source aspect narrower than screen aspect, snap to max height vary width
1116 resultSize.height = maxHeight;
1117 resultSize.width = (maxHeight * resultPar);
1121 // If necessary, grow to minimum dimensions to ensure controls overlay is not obstructed
1122 if ( resultSize.width < minWidth )
1124 resultSize.width = minWidth;
1126 if ( resultSize.height < minHeight )
1128 resultSize.height = minHeight;
1137 // -[PictureController(Private) resizePanelForViewSize:animate:]
1139 // Resizes the entire window to accomodate a view of a particular size.
1141 - (void)resizeSheetForViewSize: (NSSize)viewSize
1143 // Figure out the deltas for the new frame area
1144 NSSize currentSize = [fPictureViewArea frame].size;
1145 CGFloat deltaX = viewSize.width - currentSize.width;
1146 CGFloat deltaY = viewSize.height - currentSize.height;
1148 // Now resize the whole panel by those same deltas, but don't exceed the min
1149 NSRect frame = [[self window] frame];
1150 NSSize maxSize = [[[self window] screen] visibleFrame].size;
1151 /* if we are not Scale To Screen, put an 85% of visible screen on the window */
1152 if (scaleToScreen == NO )
1154 maxSize.width = maxSize.width * 0.85;
1155 maxSize.height = maxSize.height * 0.85;
1158 /* Set our min size to the storage size */
1160 minSize.width = fTitle->width;
1161 minSize.height = fTitle->height;
1163 frame.size.width += deltaX;
1164 frame.size.height += deltaY;
1165 if( frame.size.width < minSize.width )
1167 frame.size.width = minSize.width;
1170 if( frame.size.height < minSize.height )
1172 frame.size.height = minSize.height;
1174 /* compare frame to max size of screen */
1176 if( frame.size.width > maxSize.width )
1178 frame.size.width = maxSize.width;
1181 if( frame.size.height > maxSize.height )
1183 frame.size.height = maxSize.height;
1190 // But now the sheet is off-center, so also shift the origin to center it and
1191 // keep the top aligned.
1192 if( frame.size.width != [[self window] frame].size.width )
1193 frame.origin.x -= (deltaX / 2.0);
1196 /* Since upon launch we can open up the preview window if it was open
1197 * the last time we quit (and at the size it was) we want to make
1198 * sure that upon resize we do not have the window off the screen
1199 * So check the origin against the screen origin and adjust if
1202 NSSize screenSize = [[[self window] screen] visibleFrame].size;
1203 NSPoint screenOrigin = [[[self window] screen] frame].origin;
1204 if (screenSize.height < frame.size.height)
1206 frame.size.height = screenSize.height;
1208 if (screenSize.width < frame.size.width)
1210 frame.size.width = screenSize.width;
1214 /* our origin is off the screen to the left*/
1215 if (frame.origin.x < screenOrigin.x)
1217 /* so shift our origin to the right */
1218 frame.origin.x = screenOrigin.x;
1220 else if ((frame.origin.x + frame.size.width) > (screenOrigin.x + screenSize.width))
1222 /* the right side of the preview is off the screen, so shift to the left */
1223 frame.origin.x = (screenOrigin.x + screenSize.width) - frame.size.width;
1226 [[self window] setFrame:frame display:YES animate:YES];
1232 // -[PictureController(Private) setViewSize:]
1234 // Changes the view's size and centers it vertically inside of its area.
1235 // Assumes resizeSheetForViewSize: has already been called.
1237 - (void)setViewSize: (NSSize)viewSize
1240 /* special case for scaleToScreen */
1241 NSSize screenSize = [[[self window] screen] visibleFrame].size;
1242 NSSize areaSize = [fPictureViewArea frame].size;
1243 NSSize pictureSize = [fPictureView frame].size;
1244 CGFloat viewSizeAspect = viewSize.width / viewSize.height;
1246 if (viewSize.width > areaSize.width || viewSize.height > areaSize.height)
1249 if (viewSizeAspect > 1.0) // we are wider than taller, so expand the width to fill the area and scale the height
1251 viewSize.width = areaSize.width;
1252 viewSize.height = viewSize.width / viewSizeAspect;
1256 viewSize.height = areaSize.height;
1257 viewSize.width = viewSize.height * viewSizeAspect;
1262 [fPictureView setFrameSize:viewSize];
1263 NSSize newAreaSize = [fPictureViewArea frame].size;
1266 // center it vertically and horizontally
1267 NSPoint origin = [fPictureViewArea frame].origin;
1268 origin.y += ([fPictureViewArea frame].size.height -
1269 [fPictureView frame].size.height) / 2.0;
1271 origin.x += ([fPictureViewArea frame].size.width -
1272 [fPictureView frame].size.width) / 2.0;
1274 origin.x = floor( origin.x );
1275 origin.y = floor( origin.y );
1277 [fPictureView setFrameOrigin:origin];
1282 - (BOOL)viewNeedsToResizeToSize: (NSSize)newSize
1284 NSSize viewSize = [fPictureViewArea frame].size;
1285 return (newSize.width != viewSize.width || newSize.height != viewSize.height);