[longomatch] Fix conversion of video files with different sizes



commit 8a4af987913ed0c06dd9391bd3cf2fcf84504a99
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date:   Mon Feb 16 17:06:07 2015 +0100

    Fix conversion of video files with different sizes
    
    When the video converter needs to convert videos with different
    video sizes, pixel-aspect-ratio must be forced to 1/1 so the caps
    remain constants before the encoder and we let videoscale add
    black borders to keep the DAR

 .../Interfaces/Multimedia/IVideoConverter.cs       |    2 +-
 .../Converter/GstVideoConverter.cs                 |    6 ++--
 .../Services/RenderingJobsManager.cs               |    3 +-
 libcesarplayer/gst-video-encoder.c                 |   28 +++++++++++++++++++-
 libcesarplayer/gst-video-encoder.h                 |    5 +++-
 libcesarplayer/test-encoder.c                      |   14 +++++++++-
 6 files changed, 50 insertions(+), 8 deletions(-)
---
diff --git a/LongoMatch.Core/Interfaces/Multimedia/IVideoConverter.cs 
b/LongoMatch.Core/Interfaces/Multimedia/IVideoConverter.cs
index a86b1d0..c4a4845 100644
--- a/LongoMatch.Core/Interfaces/Multimedia/IVideoConverter.cs
+++ b/LongoMatch.Core/Interfaces/Multimedia/IVideoConverter.cs
@@ -37,7 +37,7 @@ namespace LongoMatch.Core.Interfaces.Multimedia
 
                void Cancel();
                
-               void AddFile(string filename, long duration);
+               void AddFile(string filename, long duration, uint width, uint height, double par);
                
                EncodingSettings EncodingSettings {set;}
                
diff --git a/LongoMatch.Multimedia/Converter/GstVideoConverter.cs 
b/LongoMatch.Multimedia/Converter/GstVideoConverter.cs
index 830a853..667209a 100644
--- a/LongoMatch.Multimedia/Converter/GstVideoConverter.cs
+++ b/LongoMatch.Multimedia/Converter/GstVideoConverter.cs
@@ -228,11 +228,11 @@ namespace LongoMatch.Video.Converter {
                }
                
                [DllImport("libcesarplayer.dll")]
-               static extern bool gst_video_encoder_add_file (IntPtr raw, IntPtr filename, long duration);
+               static extern bool gst_video_encoder_add_file (IntPtr raw, IntPtr filename, long duration, 
uint width, uint height, double par);
                
-               public void AddFile (string filename, long duration) {
+               public void AddFile (string filename, long duration, uint width, uint height, double par) {
                        IntPtr file = GLib.Marshaller.StringToPtrGStrdup(filename);
-                       gst_video_encoder_add_file (Handle, file, duration);
+                       gst_video_encoder_add_file (Handle, file, duration, width, height, par);
                }
 
                [DllImport("libcesarplayer.dll")]
diff --git a/LongoMatch.Services/Services/RenderingJobsManager.cs 
b/LongoMatch.Services/Services/RenderingJobsManager.cs
index b1ca4ff..39dfd38 100644
--- a/LongoMatch.Services/Services/RenderingJobsManager.cs
+++ b/LongoMatch.Services/Services/RenderingJobsManager.cs
@@ -162,7 +162,8 @@ namespace LongoMatch.Services
                        videoConverter.Error += OnError;
                        
                        foreach (MediaFile file in job.InputFiles) {
-                               videoConverter.AddFile (file.FilePath, file.Duration.MSeconds);
+                               videoConverter.AddFile (file.FilePath, file.Duration.MSeconds,
+                                                       file.VideoWidth, file.VideoHeight, file.Par);
                        }
                        
                        try {
diff --git a/libcesarplayer/gst-video-encoder.c b/libcesarplayer/gst-video-encoder.c
index 56ff1f7..4009d73 100644
--- a/libcesarplayer/gst-video-encoder.c
+++ b/libcesarplayer/gst-video-encoder.c
@@ -75,6 +75,10 @@ struct GstVideoEncoderPrivate
   GstClockTime total_duration;
   guint update_id;
   guint64 last_buf_ts;
+  gboolean size_changes;
+  guint last_height;
+  guint last_width;
+  guint last_par;
 };
 
 static GObjectClass *parent_class = NULL;
@@ -106,6 +110,9 @@ gst_video_encoder_init (GstVideoEncoder * object)
   priv->output_width = 640;
   priv->audio_quality = 50;
   priv->video_quality = 50;
+  priv->last_width = 0;
+  priv->last_height = 0;
+  priv->size_changes = FALSE;
   priv->video_encoder_type = VIDEO_ENCODER_VP8;
   priv->audio_encoder_type = AUDIO_ENCODER_VORBIS;
   priv->video_muxer_type = VIDEO_MUXER_WEBM;
@@ -253,6 +260,7 @@ gst_video_encoder_create_encoder_bin (GstVideoEncoder * gve)
   g_object_set (vqueue, "max-size-bytes", 0, "max-size-buffers", 0,
       "max-size-time", 5 * GST_SECOND, NULL);
 
+
   /* Set caps for the encoding resolution */
   video_caps = gst_caps_new_simple ("video/x-raw-yuv", NULL);
   if (gve->priv->output_width != 0) {
@@ -263,10 +271,18 @@ gst_video_encoder_create_encoder_bin (GstVideoEncoder * gve)
     gst_caps_set_simple (video_caps, "height", G_TYPE_INT,
         gve->priv->output_height, NULL);
   }
+
   if (gve->priv->output_height == 0 || gve->priv->output_width == 0) {
     gst_caps_set_simple (video_caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
         1, 1, NULL);
   }
+
+  if (gve->priv->size_changes) {
+    gst_caps_set_simple (video_caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
+        1, 1, NULL);
+    g_object_set (videoscale, "add-borders", TRUE, NULL);
+  }
+
   /* Set caps for the encoding framerate */
   if (gve->priv->fps_n != 0 && gve->priv->fps_d != 0) {
     gst_caps_set_simple (video_caps, "framerate", GST_TYPE_FRACTION,
@@ -666,7 +682,7 @@ gst_video_encoder_start (GstVideoEncoder * gve)
 
 void
 gst_video_encoder_add_file (GstVideoEncoder * gve, const gchar * file,
-    guint64 duration)
+    guint64 duration, guint width, guint height, gdouble par)
 {
   gchar *uri;
   g_return_if_fail (gve != NULL);
@@ -677,6 +693,16 @@ gst_video_encoder_add_file (GstVideoEncoder * gve, const gchar * file,
   if (uri == NULL) {
     GST_ERROR_OBJECT (gve, "Invalid filename %s", file);
   }
+  if ((gve->priv->last_width != 0 || gve->priv->last_height != 0) &&
+      (gve->priv->last_width != width ||
+      gve->priv->last_height != height ||
+      gve->priv->last_par != par)) {
+    gve->priv->size_changes = TRUE;
+  }
+  gve->priv->last_width = width;
+  gve->priv->last_height = height;
+  gve->priv->last_par = par;
+
   gve->priv->input_files = g_list_append (gve->priv->input_files, uri);
   gve->priv->total_duration += duration * GST_MSECOND;
 }
diff --git a/libcesarplayer/gst-video-encoder.h b/libcesarplayer/gst-video-encoder.h
index c6d1b42..4528ebd 100644
--- a/libcesarplayer/gst-video-encoder.h
+++ b/libcesarplayer/gst-video-encoder.h
@@ -83,7 +83,10 @@ EXPORT void gst_video_encoder_set_encoding_format              (GstVideoEncoder
 
 EXPORT void gst_video_encoder_add_file                         (GstVideoEncoder * gve,
                                                                 const gchar * file,
-                                                                guint64 duration);
+                                                                guint64 duration,
+                                                                guint width,
+                                                                guint height,
+                                                                gdouble par);
 
 EXPORT gboolean gst_video_encoder_dump_graph                   (GstVideoEncoder *gve);
 
diff --git a/libcesarplayer/test-encoder.c b/libcesarplayer/test-encoder.c
index d0620f8..355660c 100644
--- a/libcesarplayer/test-encoder.c
+++ b/libcesarplayer/test-encoder.c
@@ -72,7 +72,19 @@ main (int argc, char *argv[])
       audio_encoder, video_muxer, 500, 128, 240, 180, 25, 1);
 
   for (i=2; i < argc; i++) {
-    gst_video_encoder_add_file (encoder, argv[i], 1000);
+    guint64 duration;
+    guint width, height, fps_n, fps_d, par_n, par_d;
+    gchar *container, *video_codec, *audio_codec;
+    GError *err=NULL;
+
+    lgm_discover_uri (argv[i], &duration, &width, &height, &fps_n,
+        &fps_d, &par_n, &par_d, &container, &video_codec, &audio_codec, &err);
+    if (err != NULL) {
+      g_print ("Error parsing file %s \n", argv[i]);
+      exit (1);
+    }
+    gst_video_encoder_add_file (encoder, argv[i], duration / GST_MSECOND, width, height,
+        (gdouble)par_n/par_d);
   }
 
   loop = g_main_loop_new (NULL, FALSE);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]