+#pragma mark -
+#pragma mark Live Preview
+/* Note,this is much like prepareJob, but directly sets the job vars so Picture Preview
+ * can encode to its temp preview directory and playback. This is *not* used for any actual user
+ * encodes
+ */
+- (void) prepareJobForPreview
+{
+ hb_list_t * list = hb_get_titles( fHandle );
+ hb_title_t * title = (hb_title_t *) hb_list_item( list,
+ [fSrcTitlePopUp indexOfSelectedItem] );
+ hb_job_t * job = title->job;
+ hb_audio_config_t * audio;
+ /* set job->angle for libdvdnav */
+ job->angle = [fSrcAnglePopUp indexOfSelectedItem] + 1;
+ /* Chapter selection */
+ job->chapter_start = [fSrcChapterStartPopUp indexOfSelectedItem] + 1;
+ job->chapter_end = [fSrcChapterEndPopUp indexOfSelectedItem] + 1;
+
+ /* Format (Muxer) and Video Encoder */
+ job->mux = [[fDstFormatPopUp selectedItem] tag];
+ job->vcodec = [[fVidEncoderPopUp selectedItem] tag];
+
+ job->chapter_markers = 0;
+
+ if( job->vcodec & HB_VCODEC_X264 )
+ {
+ /* Set this flag to switch from Constant Quantizer(default) to Constant Rate Factor Thanks jbrjake
+ Currently only used with Constant Quality setting*/
+ if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DefaultCrf"] > 0 && [fVidQualityMatrix selectedRow] == 2)
+ {
+ job->crf = 1;
+ }
+
+ /* Below Sends x264 options to the core library if x264 is selected*/
+ /* Lets use this as per Nyx, Thanks Nyx!*/
+ job->x264opts = (char *)calloc(1024, 1); /* Fixme, this just leaks */
+ /* For previews we ignore the turbo option for the first pass of two since we only use 1 pass */
+ strcpy(job->x264opts, [[fAdvancedOptions optionsString] UTF8String]);
+
+
+ }
+
+ /* Video settings */
+ /* Set vfr to 0 as it's only on if using same as source in the framerate popup
+ * and detelecine is on, so we handle that in the logic below
+ */
+ job->vfr = 0;
+ if( [fVidRatePopUp indexOfSelectedItem] > 0 )
+ {
+ /* a specific framerate has been chosen */
+ job->vrate = 27000000;
+ job->vrate_base = hb_video_rates[[fVidRatePopUp indexOfSelectedItem]-1].rate;
+ /* We are not same as source so we set job->cfr to 1
+ * to enable constant frame rate since user has specified
+ * a specific framerate*/
+ job->cfr = 1;
+ }
+ else
+ {
+ /* We are same as source (variable) */
+ job->vrate = title->rate;
+ job->vrate_base = title->rate_base;
+ /* We are same as source so we set job->cfr to 0
+ * to enable true same as source framerate */
+ job->cfr = 0;
+ /* If we are same as source and we have detelecine on, we need to turn on
+ * job->vfr
+ */
+ if ([fPictureController detelecine] == 1)
+ {
+ job->vfr = 1;
+ }
+ }
+
+ switch( [fVidQualityMatrix selectedRow] )
+ {
+ case 0:
+ /* Target size.
+ Bitrate should already have been calculated and displayed
+ in fVidBitrateField, so let's just use it */
+ case 1:
+ job->vquality = -1.0;
+ job->vbitrate = [fVidBitrateField intValue];
+ break;
+ case 2:
+ job->vquality = [fVidQualityRFField floatValue];
+ job->vbitrate = 0;
+ break;
+ }
+
+ /* Subtitle settings */
+ NSMutableArray *subtitlesArray = nil;
+ subtitlesArray = [[NSMutableArray alloc] initWithArray:[fSubtitlesDelegate getSubtitleArray: subtitlesArray]];
+
+
+
+ int subtitle = nil;
+int force;
+int burned;
+int def;
+bool one_burned = FALSE;
+
+ int i = 0;
+ NSEnumerator *enumerator = [subtitlesArray objectEnumerator];
+ id tempObject;
+ while (tempObject = [enumerator nextObject])
+ {
+
+ subtitle = [[tempObject objectForKey:@"subtitleSourceTrackNum"] intValue];
+ force = [[tempObject objectForKey:@"subtitleTrackForced"] intValue];
+ burned = [[tempObject objectForKey:@"subtitleTrackBurned"] intValue];
+ def = [[tempObject objectForKey:@"subtitleTrackDefault"] intValue];
+
+ /* since the subtitleSourceTrackNum 0 is "None" in our array of the subtitle popups,
+ * we want to ignore it for display as well as encoding.
+ */
+ if (subtitle > 0)
+ {
+ /* if i is 0, then we are in the first item of the subtitles which we need to
+ * check for the "Foreign Audio Search" which would be subtitleSourceTrackNum of 1
+ * bearing in mind that for all tracks subtitleSourceTrackNum of 0 is None.
+ */
+
+ /* if we are on the first track and using "Foreign Audio Search" */
+ if (i == 0 && subtitle == 1)
+ {
+ /* NOTE: Currently foreign language search is borked for preview.
+ * Commented out but left in for initial commit. */
+
+
+ [self writeToActivityLog: "Foreign Language Search: %d", 1];
+
+ job->indepth_scan = 1;
+ if (burned == 1 || job->mux != HB_MUX_MP4)
+ {
+ if (burned != 1 && job->mux == HB_MUX_MKV)
+ {
+ job->select_subtitle_config.dest = hb_subtitle_config_s::PASSTHRUSUB;
+ }
+ else
+ {
+ job->select_subtitle_config.dest = hb_subtitle_config_s::RENDERSUB;
+ }
+
+ job->select_subtitle_config.force = force;
+ job->select_subtitle_config.default_track = def;
+
+ }
+
+
+ }
+ else
+ {
+
+ /* for the actual source tracks, we must subtract the non source entries so
+ * that the menu index matches the source subtitle_list index for convenience */
+ if (i == 0)
+ {
+ /* for the first track, the source tracks start at menu index 2 ( None is 0,
+ * Foreign Language Search is 1) so subtract 2 */
+ subtitle = subtitle - 2;
+ }
+ else
+ {
+ /* for all other tracks, the source tracks start at menu index 1 (None is 0)
+ * so subtract 1. */
+
+ subtitle = subtitle - 1;
+ }
+
+ /* We are setting a source subtitle so access the source subtitle info */
+ hb_subtitle_t * subt;
+
+ subt = (hb_subtitle_t *)hb_list_item(title->list_subtitle, subtitle);
+
+ /* if we are getting the subtitles from an external srt file */
+ if ([[tempObject objectForKey:@"subtitleSourceTrackType"] isEqualToString:@"SRT"])
+ {
+ hb_subtitle_config_t sub_config;
+
+ sub_config.offset = [[tempObject objectForKey:@"subtitleTrackSrtOffset"] intValue];
+
+ /* we need to srncpy file path and char code */
+ strncpy(sub_config.src_filename, [[tempObject objectForKey:@"subtitleSourceSrtFilePath"] UTF8String], 128);
+ strncpy(sub_config.src_codeset, [[tempObject objectForKey:@"subtitleTrackSrtCharCode"] UTF8String], 40);
+
+ sub_config.force = 0;
+ sub_config.dest = hb_subtitle_config_s::PASSTHRUSUB;
+ sub_config.default_track = def;
+
+ hb_srt_add( job, &sub_config, [[tempObject objectForKey:@"subtitleTrackSrtLanguageIso3"] UTF8String]);
+ }
+
+ if (subt != NULL)
+ {
+ [self writeToActivityLog: "Setting Subtitle: %s", subt];
+
+ hb_subtitle_config_t sub_config = subt->config;
+
+ if (!burned && job->mux == HB_MUX_MKV &&
+ subt->format == hb_subtitle_s::PICTURESUB)
+ {
+ sub_config.dest = hb_subtitle_config_s::PASSTHRUSUB;
+ }
+ else if (!burned && job->mux == HB_MUX_MP4 &&
+ subt->format == hb_subtitle_s::PICTURESUB)
+ {
+ // Skip any non-burned vobsubs when output is mp4
+ continue;
+ }
+ else if ( burned && subt->format == hb_subtitle_s::PICTURESUB )
+ {
+ // Only allow one subtitle to be burned into the video
+ if (one_burned)
+ continue;
+ one_burned = TRUE;
+ }
+ sub_config.force = force;
+ sub_config.default_track = def;
+ hb_subtitle_add( job, &sub_config, subtitle );
+ }
+
+ }
+ }
+ i++;
+ }
+
+
+
+[subtitlesArray autorelease];
+
+
+ /* Audio tracks and mixdowns */
+ /* Lets make sure there arent any erroneous audio tracks in the job list, so lets make sure its empty*/
+ int audiotrack_count = hb_list_count(job->list_audio);
+ for( int i = 0; i < audiotrack_count;i++)
+ {
+ hb_audio_t * temp_audio = (hb_audio_t*) hb_list_item( job->list_audio, 0 );
+ hb_list_rem(job->list_audio, temp_audio);
+ }
+ /* Now lets add our new tracks to the audio list here */
+ if ([fAudLang1PopUp indexOfSelectedItem] > 0)
+ {
+ audio = (hb_audio_config_t *) calloc(1, sizeof(*audio));
+ hb_audio_config_init(audio);
+ audio->in.track = [fAudLang1PopUp indexOfSelectedItem] - 1;
+ /* We go ahead and assign values to our audio->out.<properties> */
+ audio->out.track = [fAudLang1PopUp indexOfSelectedItem] - 1;
+ audio->out.codec = [[fAudTrack1CodecPopUp selectedItem] tag];
+ audio->out.mixdown = [[fAudTrack1MixPopUp selectedItem] tag];
+ audio->out.bitrate = [[fAudTrack1BitratePopUp selectedItem] tag];
+ audio->out.samplerate = [[fAudTrack1RatePopUp selectedItem] tag];
+ audio->out.dynamic_range_compression = [fAudTrack1DrcField floatValue];
+
+ hb_audio_add( job, audio );
+ free(audio);
+ }
+ if ([fAudLang2PopUp indexOfSelectedItem] > 0)
+ {
+ audio = (hb_audio_config_t *) calloc(1, sizeof(*audio));
+ hb_audio_config_init(audio);
+ audio->in.track = [fAudLang2PopUp indexOfSelectedItem] - 1;
+ /* We go ahead and assign values to our audio->out.<properties> */
+ audio->out.track = [fAudLang2PopUp indexOfSelectedItem] - 1;
+ audio->out.codec = [[fAudTrack2CodecPopUp selectedItem] tag];
+ audio->out.mixdown = [[fAudTrack2MixPopUp selectedItem] tag];
+ audio->out.bitrate = [[fAudTrack2BitratePopUp selectedItem] tag];
+ audio->out.samplerate = [[fAudTrack2RatePopUp selectedItem] tag];
+ audio->out.dynamic_range_compression = [fAudTrack2DrcField floatValue];
+
+ hb_audio_add( job, audio );
+ free(audio);
+
+ }
+
+ if ([fAudLang3PopUp indexOfSelectedItem] > 0)
+ {
+ audio = (hb_audio_config_t *) calloc(1, sizeof(*audio));
+ hb_audio_config_init(audio);
+ audio->in.track = [fAudLang3PopUp indexOfSelectedItem] - 1;
+ /* We go ahead and assign values to our audio->out.<properties> */
+ audio->out.track = [fAudLang3PopUp indexOfSelectedItem] - 1;
+ audio->out.codec = [[fAudTrack3CodecPopUp selectedItem] tag];
+ audio->out.mixdown = [[fAudTrack3MixPopUp selectedItem] tag];
+ audio->out.bitrate = [[fAudTrack3BitratePopUp selectedItem] tag];
+ audio->out.samplerate = [[fAudTrack3RatePopUp selectedItem] tag];
+ audio->out.dynamic_range_compression = [fAudTrack3DrcField floatValue];
+
+ hb_audio_add( job, audio );
+ free(audio);
+
+ }
+
+ if ([fAudLang4PopUp indexOfSelectedItem] > 0)
+ {
+ audio = (hb_audio_config_t *) calloc(1, sizeof(*audio));
+ hb_audio_config_init(audio);
+ audio->in.track = [fAudLang4PopUp indexOfSelectedItem] - 1;
+ /* We go ahead and assign values to our audio->out.<properties> */
+ audio->out.track = [fAudLang4PopUp indexOfSelectedItem] - 1;
+ audio->out.codec = [[fAudTrack4CodecPopUp selectedItem] tag];
+ audio->out.mixdown = [[fAudTrack4MixPopUp selectedItem] tag];
+ audio->out.bitrate = [[fAudTrack4BitratePopUp selectedItem] tag];
+ audio->out.samplerate = [[fAudTrack4RatePopUp selectedItem] tag];
+ audio->out.dynamic_range_compression = [fAudTrack4DrcField floatValue];
+
+ hb_audio_add( job, audio );
+ free(audio);
+
+ }
+
+
+
+ /* Filters */
+
+ /* Though Grayscale is not really a filter, per se
+ * we put it here since its in the filters panel
+ */
+
+ if ([fPictureController grayscale])
+ {
+ job->grayscale = 1;
+ }
+ else
+ {
+ job->grayscale = 0;
+ }
+
+ /* Initialize the filters list */
+ job->filters = hb_list_init();
+
+ /* Now lets call the filters if applicable.
+ * The order of the filters is critical
+ */
+
+ /* Detelecine */
+ if ([fPictureController detelecine] == 1)
+ {
+ hb_list_add( job->filters, &hb_filter_detelecine );
+ }
+ if ([fPictureController detelecine] == 2)
+ {
+ /* use a custom detelecine string */
+ hb_filter_detelecine.settings = (char *) [[fPictureController detelecineCustomString] UTF8String];
+ hb_list_add( job->filters, &hb_filter_detelecine );
+ }
+ if ([fPictureController useDecomb] == 1)
+ {
+ /* Decomb */
+ if ([fPictureController decomb] == 1)
+ {
+ /* Run old deinterlacer fd by default */
+ //hb_filter_decomb.settings = (char *) [[fPicSettingDecomb stringValue] UTF8String];
+ hb_list_add( job->filters, &hb_filter_decomb );
+ }
+ /* we add the custom string if present */
+ if ([fPictureController decomb] == 2)
+ {
+ /* use a custom decomb string */
+ hb_filter_decomb.settings = (char *) [[fPictureController decombCustomString] UTF8String];
+ hb_list_add( job->filters, &hb_filter_decomb );
+ }
+ }
+ else
+ {
+
+ /* Deinterlace */
+ if ([fPictureController deinterlace] == 1)
+ {
+ /* Run old deinterlacer fd by default */
+ hb_filter_deinterlace.settings = "-1";
+ hb_list_add( job->filters, &hb_filter_deinterlace );
+ }
+ else if ([fPictureController deinterlace] == 2)
+ {
+ /* Yadif mode 0 (without spatial deinterlacing.) */
+ hb_filter_deinterlace.settings = "2";
+ hb_list_add( job->filters, &hb_filter_deinterlace );
+ }
+ else if ([fPictureController deinterlace] == 3)
+ {
+ /* Yadif (with spatial deinterlacing) */
+ hb_filter_deinterlace.settings = "0";
+ hb_list_add( job->filters, &hb_filter_deinterlace );
+ }
+ else if ([fPictureController deinterlace] == 4)
+ {
+ /* we add the custom string if present */
+ hb_filter_deinterlace.settings = (char *) [[fPictureController deinterlaceCustomString] UTF8String];
+ hb_list_add( job->filters, &hb_filter_deinterlace );
+ }
+ }
+
+ /* Denoise */
+ if ([fPictureController denoise] == 1) // Weak in popup
+ {
+ hb_filter_denoise.settings = "2:1:2:3";
+ hb_list_add( job->filters, &hb_filter_denoise );
+ }
+ else if ([fPictureController denoise] == 2) // Medium in popup
+ {
+ hb_filter_denoise.settings = "3:2:2:3";
+ hb_list_add( job->filters, &hb_filter_denoise );
+ }
+ else if ([fPictureController denoise] == 3) // Strong in popup
+ {
+ hb_filter_denoise.settings = "7:7:5:5";
+ hb_list_add( job->filters, &hb_filter_denoise );
+ }
+ else if ([fPictureController denoise] == 4) // custom in popup
+ {
+ /* we add the custom string if present */
+ hb_filter_denoise.settings = (char *) [[fPictureController denoiseCustomString] UTF8String];
+ hb_list_add( job->filters, &hb_filter_denoise );
+ }
+
+ /* Deblock (uses pp7 default) */
+ /* NOTE: even though there is a valid deblock setting of 0 for the filter, for
+ * the macgui's purposes a value of 0 actually means to not even use the filter
+ * current hb_filter_deblock.settings valid ranges are from 5 - 15
+ */
+ if ([fPictureController deblock] != 0)
+ {
+ NSString *deblockStringValue = [NSString stringWithFormat: @"%d",[fPictureController deblock]];
+ hb_filter_deblock.settings = (char *) [deblockStringValue UTF8String];
+ hb_list_add( job->filters, &hb_filter_deblock );
+ }
+
+}
+