Пример #1
0
        public static hb_geometry_s GetAnamorphicSizes(hb_geometry_s sourceGeometry, hb_geometry_settings_s uiGeometry)
        {
            hb_geometry_s geometry = new hb_geometry_s();

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

            return(geometry);
        }
Пример #2
0
        public RawPreviewData GetPreview(PreviewSettings settings, int previewNumber, bool deinterlace)
        {
            SourceTitle title = this.Titles.TitleList.FirstOrDefault(t => t.Index == settings.TitleNumber);

            // Create the Expected Output Geometry details for libhb.
            hb_geometry_settings_s uiGeometry = new hb_geometry_settings_s
            {
                crop      = new[] { settings.Cropping.Top, settings.Cropping.Bottom, settings.Cropping.Left, settings.Cropping.Right },
                itu_par   = 0,
                keep      = (int)AnamorphicFactory.KeepSetting.HB_KEEP_WIDTH + (settings.KeepDisplayAspect ? 0x04 : 0), // TODO Keep Width?
                maxWidth  = settings.MaxWidth,
                maxHeight = settings.MaxHeight,
                mode      = (int)(hb_anamorphic_mode_t)settings.Anamorphic,
                modulus   = settings.Modulus ?? 16,
                geometry  = new hb_geometry_s
                {
                    height = settings.Height,
                    width  = settings.Width,
                    par    = settings.Anamorphic != Anamorphic.Custom && settings.Anamorphic != Anamorphic.Automatic
                        ? new hb_rational_t {
                        den = title.Geometry.PAR.Den, num = title.Geometry.PAR.Num
                    }
                        : new hb_rational_t {
                        den = settings.PixelAspectY, num = settings.PixelAspectX
                    }
                }
            };

            // Fetch the image data from LibHb
            IntPtr     resultingImageStuct = hbFunctions.hb_get_preview2(this.Handle, settings.TitleNumber, previewNumber, ref uiGeometry, deinterlace ? 1 : 0);
            hb_image_s image = InteropUtilities.ToStructureFromPtr <hb_image_s>(resultingImageStuct);

            // Copy the filled image buffer to a managed array.
            int stride_width    = image.plane[0].stride;
            int stride_height   = image.plane[0].height_stride;
            int imageBufferSize = stride_width * stride_height;  // int imageBufferSize = outputWidth * outputHeight * 4;

            byte[] managedBuffer = new byte[imageBufferSize];
            Marshal.Copy(image.plane[0].data, managedBuffer, 0, imageBufferSize);

            RawPreviewData preview = new RawPreviewData(managedBuffer, stride_width, stride_height, image.width, image.height);

            // Close the image so we don't leak memory.
            IntPtr nativeJobPtrPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)));

            Marshal.WriteIntPtr(nativeJobPtrPtr, resultingImageStuct);
            hbFunctions.hb_image_close(nativeJobPtrPtr);
            Marshal.FreeHGlobal(nativeJobPtrPtr);

            return(preview);
        }
Пример #3
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
            });
        }
        /// <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();

            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
            });
        }
        public MemoryStream GetPreview(PreviewSettings settings, int previewNumber, bool deinterlace)
        {
            SourceTitle title = this.Titles.TitleList.FirstOrDefault(t => t.Index == settings.TitleNumber);

            // Create the Expected Output Geometry details for libhb.
            hb_geometry_settings_s uiGeometry = new hb_geometry_settings_s
            {
                crop      = new[] { settings.Cropping.Top, settings.Cropping.Bottom, settings.Cropping.Left, settings.Cropping.Right },
                itu_par   = 0,
                keep      = (int)AnamorphicFactory.KeepSetting.HB_KEEP_WIDTH + (settings.KeepDisplayAspect ? 0x04 : 0), // TODO Keep Width?
                maxWidth  = settings.MaxWidth,
                maxHeight = settings.MaxHeight,
                mode      = (int)(hb_anamorphic_mode_t)settings.Anamorphic,
                modulus   = settings.Modulus ?? 16,
                geometry  = new hb_geometry_s
                {
                    height = settings.Height,
                    width  = settings.Width,
                    par    = settings.Anamorphic != Anamorphic.Custom
                        ? new hb_rational_t {
                        den = title.Geometry.PAR.Den, num = title.Geometry.PAR.Num
                    }
                        : new hb_rational_t {
                        den = settings.PixelAspectY, num = settings.PixelAspectX
                    }
                }
            };

            // Fetch the image data from LibHb
            IntPtr     resultingImageStuct = HBFunctions.hb_get_preview2(this.hbHandle, settings.TitleNumber, previewNumber, ref uiGeometry, deinterlace ? 1 : 0);
            hb_image_s image = InteropUtilities.ToStructureFromPtr <hb_image_s>(resultingImageStuct);

            // Copy the filled image buffer to a managed array.
            int stride_width    = image.plane[0].stride;
            int stride_height   = image.plane[0].height_stride;
            int imageBufferSize = stride_width * stride_height;  // int imageBufferSize = outputWidth * outputHeight * 4;

            byte[] managedBuffer = new byte[imageBufferSize];
            Marshal.Copy(image.plane[0].data, managedBuffer, 0, imageBufferSize);

            var bitmap = new Bitmap(image.width, image.height);

            BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, image.width, image.height), ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb);

            IntPtr ptr = bitmapData.Scan0; // Pointer to the first pixel.

            for (int i = 0; i < image.height; i++)
            {
                try
                {
                    Marshal.Copy(managedBuffer, i * stride_width, ptr, stride_width);
                    ptr = IntPtr.Add(ptr, image.width * 4);
                }
                catch (Exception exc)
                {
                    Debug.WriteLine(exc); // In theory, this will allow a partial image display if this happens. TODO add better logging of this.
                }
            }

            bitmap.UnlockBits(bitmapData);

            // Close the image so we don't leak memory.
            IntPtr nativeJobPtrPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)));

            Marshal.WriteIntPtr(nativeJobPtrPtr, resultingImageStuct);
            HBFunctions.hb_image_close(nativeJobPtrPtr);
            Marshal.FreeHGlobal(nativeJobPtrPtr);

            // Converts Bitmap into MemoryStream for transport.
            var memoryStream = new MemoryStream();

            try
            {
                bitmap.Save(memoryStream, ImageFormat.Bmp);
            }
            finally
            {
                bitmap.Dispose();
            }

            return(memoryStream);
        }
Пример #6
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
            });
        }
        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
            });
        }
Пример #8
0
        public BitmapImage GetPreview(PreviewSettings settings, int previewNumber)
        {
            SourceTitle title = this.Titles.TitleList.FirstOrDefault(t => t.Index == settings.TitleNumber);

            Validate.NotNull(title, "GetPreview: Title should not have been null. This is probably a bug.");

            // Create the Expected Output Geometry details for libhb.
            hb_geometry_settings_s uiGeometry = new hb_geometry_settings_s
            {
                crop      = new[] { settings.Cropping.Top, settings.Cropping.Bottom, settings.Cropping.Left, settings.Cropping.Right },
                itu_par   = 0,
                keep      = (int)AnamorphicFactory.KeepSetting.HB_KEEP_WIDTH + (settings.KeepDisplayAspect ? 0x04 : 0), // TODO Keep Width?
                maxWidth  = settings.MaxWidth,
                maxHeight = settings.MaxHeight,
                mode      = (int)(hb_anamorphic_mode_t)settings.Anamorphic,
                modulus   = settings.Modulus ?? 16,
                geometry  = new hb_geometry_s
                {
                    height = settings.Height,
                    width  = settings.Width,
                    par    = settings.Anamorphic != Anamorphic.Custom
                        ? new hb_rational_t {
                        den = title.Geometry.PAR.Den, num = title.Geometry.PAR.Num
                    }
                        : new hb_rational_t {
                        den = settings.PixelAspectY, num = settings.PixelAspectX
                    }
                }
            };

            // Sanitize the input.
            Geometry resultGeometry = AnamorphicFactory.CreateGeometry(settings, new SourceVideoInfo(new Size(title.Geometry.Width, title.Geometry.Height), new Size(title.Geometry.PAR.Num, title.Geometry.PAR.Den)));
            int      width          = resultGeometry.Width * resultGeometry.PAR.Num / resultGeometry.PAR.Den;
            int      height         = resultGeometry.Height;

            uiGeometry.geometry.width   = width;
            uiGeometry.geometry.height  = height;
            uiGeometry.geometry.par.num = settings.PixelAspectX;
            uiGeometry.geometry.par.den = settings.PixelAspectY;

            // Fetch the image data from LibHb
            IntPtr     resultingImageStuct = HBFunctions.hb_get_preview2(this.hbHandle, settings.TitleNumber, previewNumber, ref uiGeometry, 0);
            hb_image_s image = InteropUtilities.ToStructureFromPtr <hb_image_s>(resultingImageStuct);

            // Copy the filled image buffer to a managed array.
            int stride_width    = image.plane[0].stride;
            int stride_height   = image.plane[0].height_stride;
            int imageBufferSize = stride_width * stride_height;  // int imageBufferSize = outputWidth * outputHeight * 4;

            byte[] managedBuffer = new byte[imageBufferSize];
            Marshal.Copy(image.plane[0].data, managedBuffer, 0, imageBufferSize);

            var        bitmap     = new Bitmap(width, height);
            BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb);

            IntPtr ptr = bitmapData.Scan0; // Pointer to the first pixel.

            for (int i = 0; i < image.height; i++)
            {
                try
                {
                    Marshal.Copy(managedBuffer, i * stride_width, ptr, stride_width);
                    ptr = IntPtr.Add(ptr, width * 4);
                }
                catch (Exception exc)
                {
                    Debug.WriteLine(exc); // In theory, this will allow a partial image display if this happens. TODO add better logging of this.
                }
            }

            bitmap.UnlockBits(bitmapData);

            // Close the image so we don't leak memory.
            IntPtr nativeJobPtrPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)));

            Marshal.WriteIntPtr(nativeJobPtrPtr, resultingImageStuct);
            HBFunctions.hb_image_close(nativeJobPtrPtr);
            Marshal.FreeHGlobal(nativeJobPtrPtr);

            // Create a Bitmap Image for display.
            using (var memoryStream = new MemoryStream())
            {
                try
                {
                    bitmap.Save(memoryStream, ImageFormat.Bmp);
                }
                finally
                {
                    bitmap.Dispose();
                }

                var wpfBitmap = new BitmapImage();
                wpfBitmap.BeginInit();
                wpfBitmap.CacheOption  = BitmapCacheOption.OnLoad;
                wpfBitmap.StreamSource = memoryStream;
                wpfBitmap.EndInit();
                wpfBitmap.Freeze();

                return(wpfBitmap);
            }
        }
Пример #9
0
 public void hb_set_anamorphic_size2(ref hb_geometry_s sourceGeometry, ref hb_geometry_settings_s uiGeometry, ref hb_geometry_s result)
 {
     HBFunctions.hb_set_anamorphic_size2(ref sourceGeometry, ref uiGeometry, ref result);
 }
Пример #10
0
 public IntPtr hb_get_preview_params_json(int title_idx, int preview_idx, int deinterlace, ref hb_geometry_settings_s settings)
 {
     return(HBFunctions.hb_get_preview_params_json(title_idx, preview_idx, deinterlace, ref settings));
 }
Пример #11
0
 public IntPtr hb_get_preview2(IntPtr hbHandle, int title_idx, int preview_idx, ref hb_geometry_settings_s geo, int deinterlace)
 {
     return(HBFunctions.hb_get_preview2(hbHandle, title_idx, preview_idx, ref geo, deinterlace));
 }