コード例 #1
0
        /// <summary>
        /// The hb_set_anamorphic_size 2.
        /// </summary>
        /// <param name="job">
        /// The job.
        /// </param>
        /// <param name="title">
        /// The title.
        /// </param>
        /// <param name="setting">
        /// The setting.
        /// </param>
        /// <returns>
        /// The <see cref="AnamorphicResult"/>.
        /// </returns>
        public static AnamorphicResult hb_set_anamorphic_size2(PictureSettingsJob job, PictureSettingsTitle title, KeepSetting setting)
        {
            int settingMode = (int)setting + (job.KeepDisplayAspect ? 0x04 : 0);

            hb_geometry_settings_s uiGeometry = new hb_geometry_settings_s
            {
                crop = new[] { job.Crop.Top, job.Crop.Bottom, job.Crop.Left, job.Crop.Right },
                itu_par = 0,
                keep = settingMode,
                maxWidth = job.MaxWidth,
                maxHeight = job.MaxHeight,
                mode = (int)(hb_anamorphic_mode_t)job.AnamorphicMode,
                modulus = job.Modulus.HasValue ? job.Modulus.Value : 16,
                geometry =  new hb_geometry_s { height = job.Height, width =  job.Width, par = job.AnamorphicMode != Anamorphic.Custom ? new hb_rational_t { den = title.ParH, num = title.ParW } : new hb_rational_t { den = job.ParH, num = job.ParW }}
            };

            hb_geometry_s sourceGeometry = new hb_geometry_s
            {
                width = title.Width,
                height = title.Height,
                par = new hb_rational_t { den = title.ParH, num = title.ParW }
            };

            hb_geometry_s result = new hb_geometry_s();

            HBFunctions.hb_set_anamorphic_size2(ref sourceGeometry, ref uiGeometry, ref result);

            int outputWidth = result.width;
            int outputHeight = result.height;
            int outputParWidth = result.par.num;
            int outputParHeight = result.par.den;
            Debug.WriteLine("hb_set_anamorphic_size2: {0}x{1}", outputWidth, outputHeight);
            return new AnamorphicResult { OutputWidth = outputWidth, OutputHeight = outputHeight, OutputParWidth = outputParWidth, OutputParHeight = outputParHeight };
        }
コード例 #2
0
ファイル: PictureSize.cs プロジェクト: Irkinosor/HandBrake
        /// <summary>
        /// The hb_set_anamorphic_size 2.
        /// </summary>
        /// <param name="job">
        /// The job.
        /// </param>
        /// <param name="title">
        /// The title.
        /// </param>
        /// <param name="setting">
        /// The setting.
        /// </param>
        /// <returns>
        /// The <see cref="AnamorphicResult"/>.
        /// </returns>
        public static AnamorphicResult hb_set_anamorphic_size2(PictureSettingsJob job, PictureSettingsTitle title, KeepSetting setting)
        {
            int outputHeight    = 0;
            int outputParHeight = 0;
            int outputParWidth  = 0;
            int outputWidth     = 0;

            int settingMode = (int)setting + (job.KeepDisplayAspect ? 0x04 : 0);

            hb_ui_geometry_s uiGeometry = new hb_ui_geometry_s
            {
                crop = new[] { job.Crop.Top, job.Crop.Bottom, job.Crop.Left, job.Crop.Right },
                dar  = new hb_rational_t {
                    den = 0, num = 0
                },
                height    = job.Height,
                itu_par   = 0,
                keep      = settingMode,
                maxWidth  = job.MaxWidth,
                maxHeight = job.MaxHeight,
                mode      = (int)(hb_anamorphic_mode_t)job.AnamorphicMode,
                modulus   = job.Modulus.HasValue ? job.Modulus.Value : 16,
                par       = job.AnamorphicMode != Anamorphic.Custom ? new hb_rational_t {
                    den = title.ParH, num = title.ParW
                } : new hb_rational_t {
                    den = job.ParH, num = job.ParW
                },
                width = job.Width,
            };

            hb_geometry_s sourceGeometry = new hb_geometry_s
            {
                width  = title.Width,
                height = title.Height,
                par    = new hb_rational_t {
                    den = title.ParH, num = title.ParW
                }
            };

            hb_geometry_s result = new hb_geometry_s();

            HBFunctions.hb_set_anamorphic_size2(ref sourceGeometry, ref uiGeometry, ref result);

            outputWidth     = result.width;
            outputHeight    = result.height;
            outputParWidth  = result.par.den;
            outputParHeight = result.par.num;
            Debug.WriteLine("hb_set_anamorphic_size2: {0}x{1}", outputWidth, outputHeight);
            return(new AnamorphicResult {
                OutputWidth = outputWidth, OutputHeight = outputHeight, OutputParWidth = outputParWidth, OutputParHeight = outputParHeight
            });
        }
コード例 #3
0
ファイル: PictureSize.cs プロジェクト: sammys/HandBrakeMirror
        /// <summary>
        /// The hb_set_anamorphic_size_native.
        /// </summary>
        /// <param name="job">
        /// The job.
        /// </param>
        /// <param name="title">
        /// The title.
        /// </param>
        /// <returns>
        /// The <see cref="AnamorphicResult"/> object.
        /// </returns>
        public static AnamorphicResult hb_set_anamorphic_size(PictureSettingsJob job, PictureSettingsTitle title)
        {
            int outputHeight    = 0;
            int outputParHeight = 0;
            int outputParWidth  = 0;
            int outputWidth     = 0;

            hb_job_s nativeJob = new hb_job_s
            {
                modulus    = job.Modulus.HasValue ? job.Modulus.Value : 16,
                anamorphic =
                    new hb_anamorphic_substruct
                {
                    par_width           = job.ParW,
                    par_height          = job.ParH,
                    itu_par             = 0,
                    mode                = (int)job.AnamorphicMode,
                    dar_width           = 0,
                    dar_height          = 0,
                    keep_display_aspect = job.KeepDisplayAspect ? 1 : 0
                },
                maxWidth   = title.Width,
                maxHeight  = title.Height,
                keep_ratio = job.KeepDisplayAspect ? 1 : 0,
                width      = job.Width,
                height     = job.Height,
                crop       = new[] { job.Crop.Top, job.Crop.Bottom, job.Crop.Left, job.Crop.Right }
            };

            hb_title_s title_s = new hb_title_s
            {
                crop                = new[] { job.Crop.Top, job.Crop.Bottom, job.Crop.Left, job.Crop.Right },
                width               = title.Width,
                height              = title.Height,
                pixel_aspect_width  = title.ParW,
                pixel_aspect_height = title.ParH,
                aspect              = 0
            };

            IntPtr pointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(hb_title_s)));

            Marshal.StructureToPtr(title_s, pointer, false);
            nativeJob.title = pointer;

            HBFunctions.hb_set_anamorphic_size(
                ref nativeJob, ref outputWidth, ref outputHeight, ref outputParWidth, ref outputParHeight);

            return(new AnamorphicResult {
                OutputWidth = outputWidth, OutputHeight = outputHeight, OutputParWidth = outputParWidth, OutputParHeight = outputParHeight
            });
        }
コード例 #4
0
        public static RotateResult RotateGeometry(PictureSettingsJob job)
        {
            hb_geometry_crop_s geometry = new hb_geometry_crop_s();

            geometry.crop     = new[] { job.Crop.Top, job.Crop.Bottom, job.Crop.Left, job.Crop.Right };
            geometry.pad      = new[] { job.Pad.Top, job.Pad.Bottom, job.Pad.Left, job.Pad.Right };
            geometry.geometry = new hb_geometry_s
            {
                width  = job.Width,
                height = job.Height,
                par    = new hb_rational_t {
                    num = job.ParW, den = job.ParH
                }
            };

            // Undo any previous rotation so that we are back at 0.
            // Normally hflip is applied, then rotation.
            // To revert, must apply rotation then hflip.
            if (job.PreviousRotation.HasValue && job.PreviousRotation != 0)
            {
                HBFunctions.hb_rotate_geometry(ref geometry, ref geometry, 360 - job.PreviousRotation.Value, 0);
            }

            if (job.PreviousHflip.HasValue && job.PreviousHflip == 1)
            {
                HBFunctions.hb_rotate_geometry(ref geometry, ref geometry, 0, 1);
            }

            // Apply the new rotation and Horizontal flip value.
            HBFunctions.hb_rotate_geometry(ref geometry, ref geometry, job.RotateAngle, job.Hflip);

            RotateResult processedResult = new RotateResult
            {
                CropTop    = geometry.crop[0],
                CropBottom = geometry.crop[1],
                CropLeft   = geometry.crop[2],
                CropRight  = geometry.crop[3],
                PadTop     = geometry.pad[0],
                PadBottom  = geometry.pad[1],
                PadLeft    = geometry.pad[2],
                PadRight   = geometry.pad[3],
                Width      = geometry.geometry.width,
                Height     = geometry.geometry.height,
                ParNum     = geometry.geometry.par.num,
                ParDen     = geometry.geometry.par.den
            };

            return(processedResult);
        }
コード例 #5
0
ファイル: PictureSize.cs プロジェクト: Jesper87/HandBrake
        /// <summary>
        /// The hb_set_anamorphic_size_native.
        /// </summary>
        /// <param name="job">
        /// The job.
        /// </param>
        /// <param name="title">
        /// The title.
        /// </param>
        /// <returns>
        /// The <see cref="AnamorphicResult"/> object.
        /// </returns>
        public static AnamorphicResult hb_set_anamorphic_size(PictureSettingsJob job, PictureSettingsTitle title)
        {
            int outputHeight = 0;
            int outputParHeight = 0;
            int outputParWidth = 0;
            int outputWidth = 0;

            hb_job_s nativeJob = new hb_job_s
                                     {
                                         modulus = job.Modulus.HasValue ? job.Modulus.Value : 16,
                                         anamorphic =
                                             new hb_anamorphic_substruct
                                                 {
                                                     par_width = job.ParW,
                                                     par_height = job.ParH,
                                                     itu_par = 0,
                                                     mode = (int)job.AnamorphicMode,
                                                     dar_width = 0,
                                                     dar_height = 0,
                                                     keep_display_aspect = job.KeepDisplayAspect ? 1 : 0
                                                 },
                                         maxWidth = title.Width,
                                         maxHeight = title.Height,
                                         keep_ratio = job.KeepDisplayAspect ? 1 : 0,
                                         width = job.Width,
                                         height = job.Height,
                                         crop = new[] { job.Crop.Top, job.Crop.Bottom, job.Crop.Left, job.Crop.Right }
                                     };

            hb_title_s title_s = new hb_title_s
                                     {
                                         crop = new[] { job.Crop.Top, job.Crop.Bottom, job.Crop.Left, job.Crop.Right },
                                         width = title.Width,
                                         height = title.Height,
                                         pixel_aspect_width = title.ParW,
                                         pixel_aspect_height = title.ParH,
                                         aspect = 0
                                     };

            IntPtr pointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(hb_title_s)));
            Marshal.StructureToPtr(title_s, pointer, false);
            nativeJob.title = pointer;

            HBFunctions.hb_set_anamorphic_size(
                ref nativeJob, ref outputWidth, ref outputHeight, ref outputParWidth, ref outputParHeight);

            return new AnamorphicResult { OutputWidth = outputWidth, OutputHeight = outputHeight, OutputParWidth = outputParWidth, OutputParHeight = outputParHeight };
        }
コード例 #6
0
        /// <summary>
        /// The hb_set_anamorphic_size 2.
        /// </summary>
        /// <param name="job">
        /// The job.
        /// </param>
        /// <param name="title">
        /// The title.
        /// </param>
        /// <param name="setting">
        /// The setting.
        /// </param>
        /// <returns>
        /// The <see cref="AnamorphicResult"/>.
        /// </returns>
        public static AnamorphicResult hb_set_anamorphic_size2(PictureSettingsJob job, PictureSettingsTitle title, KeepSetting setting)
        {
            int settingMode = (int)setting + (job.KeepDisplayAspect ? 0x04 : 0);

            hb_geometry_settings_s uiGeometry = new hb_geometry_settings_s
            {
                crop      = new[] { job.Crop.Top, job.Crop.Bottom, job.Crop.Left, job.Crop.Right },
                itu_par   = 0,
                keep      = settingMode,
                maxWidth  = job.MaxWidth,
                maxHeight = job.MaxHeight,
                mode      = (int)job.AnamorphicMode,
                modulus   = job.Modulus.HasValue ? job.Modulus.Value : 16,
                geometry  = new hb_geometry_s {
                    height = job.Height, width = job.Width, par = job.AnamorphicMode != Anamorphic.Custom ? new hb_rational_t {
                        den = title.ParH, num = title.ParW
                    } : new hb_rational_t {
                        den = job.ParH, num = job.ParW
                    }
                }
            };

            hb_geometry_s sourceGeometry = new hb_geometry_s
            {
                width  = title.Width,
                height = title.Height,
                par    = new hb_rational_t {
                    den = title.ParH, num = title.ParW
                }
            };

            hb_geometry_s result = new hb_geometry_s();

            IHbFunctionsProvider provider    = IoC.Get <IHbFunctionsProvider>(); // TODO make this method non static and remove IoC call.
            IHbFunctions         hbFunctions = provider.GetHbFunctionsWrapper();

            hbFunctions.hb_set_anamorphic_size2(ref sourceGeometry, ref uiGeometry, ref result);

            int outputWidth     = result.width;
            int outputHeight    = result.height;
            int outputParWidth  = result.par.num;
            int outputParHeight = result.par.den;

            return(new AnamorphicResult {
                OutputWidth = outputWidth, OutputHeight = outputHeight, OutputParWidth = outputParWidth, OutputParHeight = outputParHeight
            });
        }
コード例 #7
0
        public static AnamorphicResult GetAnamorphicSize(PictureSettingsJob job, PictureSettingsTitle sourceTitle, KeepSetting keepSetting, FlagsSetting flagSetting)
        {
            hb_rational_t computedPar;

            switch (job.AnamorphicMode)
            {
            case Anamorphic.None:
                computedPar = new hb_rational_t {
                    den = 1, num = 1
                };
                break;

            case Anamorphic.Custom:
                computedPar = new hb_rational_t {
                    den = job.ParH, num = job.ParW
                };
                break;

            default:
                computedPar = new hb_rational_t {
                    den = sourceTitle.ParH, num = sourceTitle.ParW
                };
                break;
            }

            hb_geometry_settings_s uiGeometry = new hb_geometry_settings_s
            {
                crop      = new[] { job.Crop.Top, job.Crop.Bottom, job.Crop.Left, job.Crop.Right },
                pad       = new[] { job.Pad.Top, job.Pad.Bottom, job.Pad.Left, job.Pad.Right },
                flags     = (int)flagSetting,
                itu_par   = 0,
                keep      = (int)keepSetting,
                maxWidth  = job.MaxWidth,
                maxHeight = job.MaxHeight,
                mode      = (int)job.AnamorphicMode,
                modulus   = 2,
                geometry  = new hb_geometry_s {
                    height = job.Height, width = job.Width, par = computedPar
                },
                displayWidth = job.DarWidth
            };

            // If we are rotated, the source title geometry must also be rotated.
            int titleWidth, titleHeight, titleParW, titleParH;

            if (job.RotateAngle != 0)
            {
                PictureSettingsJob titleRepresentation = new PictureSettingsJob
                {
                    Width       = sourceTitle.Width,
                    Height      = sourceTitle.Height,
                    RotateAngle = job.RotateAngle,
                    Hflip       = job.Hflip,
                    ParW        = sourceTitle.ParW,
                    ParH        = sourceTitle.ParH,
                    Crop        = new Cropping(),
                    Pad         = new Padding()
                };
                RotateResult titleRotation = HandBrakePictureHelpers.RotateGeometry(titleRepresentation);
                titleWidth  = titleRotation.Width;
                titleHeight = titleRotation.Height;
                titleParW   = titleRotation.ParNum;
                titleParH   = titleRotation.ParDen;
            }
            else
            {
                titleWidth  = sourceTitle.Width;
                titleHeight = sourceTitle.Height;
                titleParW   = sourceTitle.ParW;
                titleParH   = sourceTitle.ParH;
            }

            hb_geometry_s sourceGeometry = new hb_geometry_s
            {
                width  = titleWidth,
                height = titleHeight,
                par    = new hb_rational_t {
                    den = titleParH, num = titleParW
                }
            };

            hb_geometry_s result = new hb_geometry_s();

            HBFunctions.hb_set_anamorphic_size2(ref sourceGeometry, ref uiGeometry, ref result);

            int outputWidth     = result.width;
            int outputHeight    = result.height;
            int outputParWidth  = result.par.num;
            int outputParHeight = result.par.den;

            return(new AnamorphicResult {
                OutputWidth = outputWidth, OutputHeight = outputHeight, OutputParWidth = outputParWidth, OutputParHeight = outputParHeight
            });
        }
コード例 #8
0
        public static AnamorphicResult hb_set_anamorphic_size2(PictureSettingsJob job, PictureSettingsTitle title, KeepSetting setting)
        {
            int settingMode = (int)setting + (job.KeepDisplayAspect ? 0x04 : 0);


            hb_rational_t computed_par = new hb_rational_t();

            switch (job.AnamorphicMode)
            {
            case Anamorphic.None:
                computed_par = new hb_rational_t {
                    den = 1, num = 1
                };
                break;

            case Anamorphic.Custom:
                computed_par = new hb_rational_t {
                    den = job.ParH, num = job.ParW
                };
                break;

            default:
                computed_par = new hb_rational_t {
                    den = title.ParH, num = title.ParW
                };
                break;
            }

            hb_geometry_settings_s uiGeometry = new hb_geometry_settings_s
            {
                crop      = new[] { job.Crop.Top, job.Crop.Bottom, job.Crop.Left, job.Crop.Right },
                itu_par   = 0,
                keep      = settingMode,
                maxWidth  = job.MaxWidth,
                maxHeight = job.MaxHeight,
                mode      = (int)job.AnamorphicMode,
                modulus   = job.Modulus.HasValue ? job.Modulus.Value : 16,
                geometry  = new hb_geometry_s {
                    height = job.Height, width = job.Width, par = computed_par
                }
            };

            hb_geometry_s sourceGeometry = new hb_geometry_s
            {
                width  = title.Width,
                height = title.Height,
                par    = new hb_rational_t {
                    den = title.ParH, num = title.ParW
                }
            };

            hb_geometry_s result = HandBrakePictureHelpers.GetAnamorphicSizes(sourceGeometry, uiGeometry);

            int outputWidth     = result.width;
            int outputHeight    = result.height;
            int outputParWidth  = result.par.num;
            int outputParHeight = result.par.den;

            return(new AnamorphicResult {
                OutputWidth = outputWidth, OutputHeight = outputHeight, OutputParWidth = outputParWidth, OutputParHeight = outputParHeight
            });
        }
コード例 #9
0
        /// <summary>
        /// Calculates job width and height for anamorphic content,
        /// * @param output_width Pointer to returned storage width
        /// * @param output_height Pointer to returned storage height
        /// * @param output_par_width Pointer to returned pixel width
        /// * @param output_par_height Pointer to returned pixel height
        /// </summary>
        /// <param name="job">
        /// The job.
        /// </param>
        /// <param name="title">
        /// The title.
        /// </param>
        /// <param name="output_width">
        /// The output_width.
        /// </param>
        /// <param name="output_height">
        /// The output_height.
        /// </param>
        /// <param name="output_par_width">
        /// The output_par_width.
        /// </param>
        /// <param name="output_par_height">
        /// The output_par_height.
        /// </param>
        public static void hb_set_anamorphic_size(PictureSettingsJob job, PictureSettingsTitle title,
            out int output_width, out int output_height,
            out int output_par_width, out int output_par_height)
        {
            /* Initialise the Output Width / Height */
            output_width = 0;
            output_height = 0;

            /* Set up some variables to make the math easier to follow. */
            int cropped_width = title.Width - job.Crop.Left - job.Crop.Right;
            int cropped_height = title.Height - job.Crop.Top - job.Crop.Bottom;
            double storage_aspect = cropped_width / (double)cropped_height;
            int mod = job.Modulus.HasValue ? job.Modulus.Value : 16;
            double aspect = title.Aspect;

            long pixel_aspect_width = job.ParW;
            long pixel_aspect_height = job.ParH;

            /* If a source was really NTSC or PAL and the user specified ITU PAR
               values, replace the standard PAR values with the ITU broadcast ones. */
            if (title.Width == 720 && job.ItuPar)
            {
                // convert aspect to a scaled integer so we can test for 16:9 & 4:3
                // aspect ratios ignoring insignificant differences in the LSBs of
                // the floating point representation.
                int iaspect = (int)Math.Round(aspect * 9.0, 0);  // int iaspect = aspect * 9.; TODO CHECK THIS

                /* Handle ITU PARs */
                if (title.Height == 480)
                {
                    /* It's NTSC */
                    if (iaspect == 16)
                    {
                        /* It's widescreen */
                        pixel_aspect_width = 40;
                        pixel_aspect_height = 33;
                    }
                    else if (iaspect == 12)
                    {
                        /* It's 4:3 */
                        pixel_aspect_width = 10;
                        pixel_aspect_height = 11;
                    }
                }
                else if (title.Height == 576)
                {
                    /* It's PAL */
                    if (iaspect == 16)
                    {
                        /* It's widescreen */
                        pixel_aspect_width = 16;
                        pixel_aspect_height = 11;
                    }
                    else if (iaspect == 12)
                    {
                        /* It's 4:3 */
                        pixel_aspect_width = 12;
                        pixel_aspect_height = 11;
                    }
                }
            }

            /* Figure out what width the source would display at. */
            int source_display_width = (int)(cropped_width * (double)pixel_aspect_width / (double)pixel_aspect_height);    // TODO Check casting

            /*
               3 different ways of deciding output dimensions:
                - 1: Strict anamorphic, preserve source dimensions
                - 2: Loose anamorphic, round to mod16 and preserve storage aspect ratio
                - 3: Power user anamorphic, specify everything
            */
            int width, height;
            int maxWidth, maxHeight;

            maxWidth = Macros.MultipleModDown(job.MaxWidth, mod);
            maxHeight = Macros.MultipleModDown(job.MaxHeight, mod);

            switch (job.AnamorphicMode)
            {
                case Anamorphic.Strict:
                    /* Strict anamorphic */
                    output_width = Macros.MultipleMod(cropped_width, 2);
                    output_height = Macros.MultipleMod(cropped_height, 2);
                    // adjust the source PAR for new width/height
                    // new PAR = source PAR * ( old width / new_width ) * ( new_height / old_height )
                    pixel_aspect_width = (long)title.ParW * cropped_width * (output_height);
                    pixel_aspect_height = (long)title.ParH * (output_width) * cropped_height;
                    break;

                case Anamorphic.Loose:
                    /* "Loose" anamorphic.
                        - Uses mod16-compliant dimensions,
                        - Allows users to set the width
                    */
                    width = job.Width;
                    // height: Gets set later, ignore user job->height value

                    /* Gotta handle bounding dimensions.
                       If the width is too big, just reset it with no rescaling.
                       Instead of using the aspect-scaled job height,
                       we need to see if the job width divided by the storage aspect
                       is bigger than the max. If so, set it to the max (this is sloppy).
                       If not, set job height to job width divided by storage aspect.
                    */

                    /* Time to get picture width that divide cleanly.*/
                    width = Macros.MultipleMod(width, mod);

                    if (maxWidth != 0 && (maxWidth < job.Width))
                        width = maxWidth;

                    /* Verify these new dimensions don't violate max height and width settings */
                    height = (int)Math.Round(((double)width / storage_aspect) + 0.5, 0);  // TODO Check Casting and rounding

                    /* Time to get picture height that divide cleanly.*/
                    height = Macros.MultipleMod(height, mod);

                    if (maxHeight != 0 && (maxHeight < height))
                    {
                        height = maxHeight;
                        width = (int)Math.Round(((double)height * storage_aspect) + 0.5, 0); // TODO Check asting and arounding.
                        width = Macros.MultipleMod(width, mod);
                    }

                    /* The film AR is the source's display width / cropped source height.
                       The output display width is the output height * film AR.
                       The output PAR is the output display width / output storage width. */
                    pixel_aspect_width = (long)height * cropped_width * pixel_aspect_width;
                    pixel_aspect_height = (long)width * cropped_height * pixel_aspect_height;

                    /* Pass the results back to the caller */
                    output_width = width;
                    output_height = height;
                    break;

                case Anamorphic.Custom:
                    /* Anamorphic 3: Power User Jamboree
                       - Set everything based on specified values */

                    /* Use specified storage dimensions */
                    storage_aspect = job.Width / (double)job.Height;
                    width = job.Width;
                    height = job.Height;

                    /* Time to get picture dimensions that divide cleanly.*/
                    width = Macros.MultipleMod(width, mod);
                    height = Macros.MultipleMod(height, mod);

                    /* Bind to max dimensions */
                    if (maxWidth != 0 && width > maxWidth)
                    {
                        width = maxWidth;
                        // If we are keeping the display aspect, then we are going
                        // to be modifying the PAR anyway.  So it's preferred
                        // to let the width/height stray some from the original
                        // requested storage aspect.
                        //
                        // But otherwise, PAR and DAR will change the least
                        // if we stay as close as possible to the requested
                        // storage aspect.
                        if (!job.KeepDisplayAspect)
                        {
                            height = (int)Math.Round(((double)width / storage_aspect) + 0.5, 0); // TODO Check rounding and casting
                            height = Macros.MultipleMod(height, mod);
                        }
                    }
                    if (maxHeight != 0 && height > maxHeight)
                    {
                        height = maxHeight;
                        // Ditto, see comment above
                        if (!job.KeepDisplayAspect)
                        {
                            width = (int)Math.Round(((double)height * storage_aspect) + 0.5, 0); // TODO check Rounding and casting
                            width = Macros.MultipleMod(width, mod);
                        }
                    }

                    /* That finishes the storage dimensions. On to display. */
                    if (job.DarWidth != 0 && job.DarHeight != 0)
                    {
                        /* We need to adjust the PAR to produce this aspect. */
                        pixel_aspect_width = (long)height * job.DarWidth / job.DarHeight;
                        pixel_aspect_height = width;
                    }
                    else
                    {
                        /* If we're doing ana 3 and not specifying a DAR, care needs to be taken.
                           This indicates a PAR is potentially being set by the interface. But
                           this is an output PAR, to correct a source, and it should not be assumed
                           that it properly creates a display aspect ratio when applied to the source,
                           which could easily be stored in a different resolution. */
                        if (job.KeepDisplayAspect)
                        {
                            /* We can ignore the possibility of a PAR change */
                            pixel_aspect_width = (int)Math.Round((long)height * ((double)source_display_width / (double)cropped_height), 0); // TODO Check rounding and casting
                            pixel_aspect_height = width;
                        }
                        else
                        {
                            int output_display_width = (int)Math.Round(width * (double)pixel_aspect_width / (double)pixel_aspect_height, 0); // TODO Check rounding and casting.
                            pixel_aspect_width = output_display_width;
                            pixel_aspect_height = width;
                        }
                    }

                    /* Back to caller */
                    output_width = width;
                    output_height = height;
                    break;
            }

            /* While x264 is smart enough to reduce fractions on its own, libavcodec
             * needs some help with the math, so lose superfluous factors. */
            hb_limit_rational64(out pixel_aspect_width, out pixel_aspect_height,
                                pixel_aspect_width, pixel_aspect_height, 65535);
            hb_reduce(out output_par_width, out output_par_height,
                       (int)pixel_aspect_width, (int)pixel_aspect_height);
        }