[gegl/video-rejuvenation] GeglAudioFragment: seal struct



commit 823075976c9e11b2fb932b67dea1a8d01515a06f
Author: Øyvind Kolås <pippin gimp org>
Date:   Sat Nov 21 00:16:21 2015 +0100

    GeglAudioFragment: seal struct

 bin/mrg-ui.c                              |    7 +++--
 examples/gegl-video.c                     |   23 +++++++++++-----
 gegl/property-types/gegl-audio-fragment.c |   43 +++++++++++++++++-----------
 gegl/property-types/gegl-audio-fragment.h |    6 ----
 operations/external/ff-load.c             |   40 +++++++++++++++-----------
 operations/external/ff-save.c             |   35 +++++++++++++++---------
 6 files changed, 91 insertions(+), 63 deletions(-)
---
diff --git a/bin/mrg-ui.c b/bin/mrg-ui.c
index 1009dcc..3deb2c7 100644
--- a/bin/mrg-ui.c
+++ b/bin/mrg-ui.c
@@ -1032,16 +1032,17 @@ static void gegl_ui (Mrg *mrg, void *data)
     gegl_node_get (o->load, "audio", &audio, "frame-rate", &fps, NULL);
     if (audio)
     {
-       if (audio->xsample_count > 0)
+       int sample_count = gegl_audio_fragment_get_sample_count (audio);
+       if (sample_count > 0)
        {
          int i;
          if (!audio_started)
          {
-          open_audio (audio->sample_rate);
+          open_audio (gegl_audio_fragment_get_sample_rate (audio));
            SDL_PauseAudio(0);
            audio_started = 1;
          }
-         for (i = 0; i < audio->xsample_count; i++)
+         for (i = 0; i < sample_count; i++)
          {
            sdl_add_audio_sample (0, audio->data[0][i], audio->data[1][i]);
          }
diff --git a/examples/gegl-video.c b/examples/gegl-video.c
index 86398f6..16e854c 100644
--- a/examples/gegl-video.c
+++ b/examples/gegl-video.c
@@ -277,10 +277,14 @@ main (gint    argc,
         {
         GeglAudioFragment *audio;
         gdouble fps;
+        int sample_count;
+        int sample_rate;
         gegl_node_get (load, "audio", &audio,
                              "frame-rate", &fps, NULL);
+        sample_count = gegl_audio_fragment_get_sample_count (audio);
+        sample_rate = gegl_audio_fragment_get_sample_rate (audio);
 
-        if (audio->xsample_count > 0)
+        if (sample_count > 0)
         {
           int i;
           if (!audio_started)
@@ -288,7 +292,7 @@ main (gint    argc,
              SDL_PauseAudio(0);
              audio_started = 1;
            }
-          for (i = 0; i < audio->xsample_count; i++)
+          for (i = 0; i < sample_count; i++)
           {
             audio_data[audio_len/2 + 0] = audio->data[0][i] * 32767.0;
             audio_data[audio_len/2 + 1] = audio->data[1][i] * 32767.0;
@@ -304,7 +308,7 @@ main (gint    argc,
 
            gegl_node_set (readbuf, "buffer", video_frame, NULL);
            gegl_node_process (display);
-           while ( (audio_pos / 4.0) / audio->sample_rate < (frame / fps) - 0.05 )
+           while ( (audio_pos / 4.0) / sample_rate < (frame / fps) - 0.05 )
             {
               g_usleep (500); /* sync audio */
             }
@@ -359,14 +363,19 @@ gegl_meta_set_audio (const char        *path,
   { 
     int i, c;
     GString *str = g_string_new ("");
+    int sample_count = gegl_audio_fragment_get_sample_count (audio);
+    int channels = gegl_audio_fragment_get_channels (audio);
     if (gexiv2_metadata_has_tag (e2m, "Xmp.xmp.GEGL"))
       gexiv2_metadata_clear_tag (e2m, "Xmp.xmp.GEGL");
 
-    g_string_append_printf (str, "%i %i %i %i", audio->sample_rate, audio->xchannels,
-                            audio->xchannel_layout, audio->xsample_count);
+    g_string_append_printf (str, "%i %i %i %i", 
+                              gegl_audio_fragment_get_sample_rate (audio),
+                              gegl_audio_fragment_get_channels (audio),
+                              gegl_audio_fragment_get_channel_layout (audio),
+                              gegl_audio_fragment_get_sample_count (audio));
 
-    for (i = 0; i < audio->xsample_count; i++)
-      for (c = 0; c < audio->xchannels; c++)
+    for (i = 0; i < sample_count; i++)
+      for (c = 0; c < channels; c++)
         g_string_append_printf (str, " %0.5f", audio->data[c][i]);
 
     gexiv2_metadata_set_tag_string (e2m, "Xmp.xmp.GeglAudio", str->str);
diff --git a/gegl/property-types/gegl-audio-fragment.c b/gegl/property-types/gegl-audio-fragment.c
index 146f179..8b019ba 100644
--- a/gegl/property-types/gegl-audio-fragment.c
+++ b/gegl/property-types/gegl-audio-fragment.c
@@ -30,7 +30,12 @@ enum
 
 struct _GeglAudioFragmentPrivate
 {
-  int foo;
+  int     max_samples;
+  int     sample_count;
+  int     channels;
+  int     channel_layout;
+  int     sample_rate;
+  int     pos;
 };
 
 static void      set_property (GObject      *gobject,
@@ -62,11 +67,11 @@ static void allocate_data (GeglAudioFragment *audio)
 {
   int i;
   deallocate_data (audio);
-  if (audio->xchannels * audio->max_samples == 0)
+  if (audio->priv->channels * audio->priv->max_samples == 0)
     return;
-  for (i = 0; i < audio->xchannels; i++)
+  for (i = 0; i < audio->priv->channels; i++)
   {
-    audio->data[i] = g_malloc (sizeof (float) * audio->max_samples);
+    audio->data[i] = g_malloc (sizeof (float) * audio->priv->max_samples);
   }
 }
 
@@ -75,7 +80,7 @@ static void
 gegl_audio_fragment_init (GeglAudioFragment *self)
 {
   self->priv = G_TYPE_INSTANCE_GET_PRIVATE ((self), GEGL_TYPE_AUDIO_FRAGMENT, GeglAudioFragmentPrivate);
-  self->max_samples = GEGL_MAX_AUDIO_SAMPLES;
+  self->priv->max_samples = GEGL_MAX_AUDIO_SAMPLES;
   allocate_data (self);
 }
 
@@ -153,7 +158,9 @@ void
 gegl_audio_fragment_set_max_samples (GeglAudioFragment *audio,
                                      int                max_samples)
 {
-  audio->max_samples = max_samples;
+  if (audio->priv->max_samples == max_samples)
+    return;
+  audio->priv->max_samples = max_samples;
   allocate_data (audio);
 }
 
@@ -161,14 +168,16 @@ void
 gegl_audio_fragment_set_sample_rate (GeglAudioFragment *audio,
                                      int                sample_rate)
 {
-  audio->sample_rate = sample_rate;
+  audio->priv->sample_rate = sample_rate;
 }
 
 void
 gegl_audio_fragment_set_channels (GeglAudioFragment *audio,
                                   int                channels)
 {
-  audio->xchannels = channels;
+  if (audio->priv->channels == channels)
+    return;
+  audio->priv->channels = channels;
   allocate_data (audio);
 }
 
@@ -176,58 +185,58 @@ void
 gegl_audio_fragment_set_channel_layout (GeglAudioFragment *audio,
                                         int                channel_layout)
 {
-  audio->xchannel_layout = channel_layout;
+  audio->priv->channel_layout = channel_layout;
 }
 
 void
 gegl_audio_fragment_set_sample_count (GeglAudioFragment *audio,
                                       int                samples)
 {
-  audio->xsample_count = samples;
+  audio->priv->sample_count = samples;
 }
 
 void
 gegl_audio_fragment_set_pos (GeglAudioFragment *audio,
                              int                pos)
 {
-  audio->pos = pos;
+  audio->priv->pos = pos;
 }
 
 int
 gegl_audio_fragment_get_max_samples (GeglAudioFragment *audio)
 {
-  return audio->max_samples;
+  return audio->priv->max_samples;
 }
 
 
 int
 gegl_audio_fragment_get_sample_rate (GeglAudioFragment *audio)
 {
-  return audio->sample_rate;
+  return audio->priv->sample_rate;
 }
 
 int
 gegl_audio_fragment_get_channels (GeglAudioFragment *audio)
 {
-  return audio->xchannels;
+  return audio->priv->channels;
 }
 
 int
 gegl_audio_fragment_get_sample_count (GeglAudioFragment *audio)
 {
-  return audio->xsample_count;
+  return audio->priv->sample_count;
 }
 
 int
 gegl_audio_fragment_get_pos     (GeglAudioFragment *audio)
 {
-  return audio->pos;
+  return audio->priv->pos;
 }
 
 int
 gegl_audio_fragment_get_channel_layout (GeglAudioFragment *audio)
 {
-  return audio->xchannel_layout;
+  return audio->priv->channel_layout;
 }
 
 /* --------------------------------------------------------------------------
diff --git a/gegl/property-types/gegl-audio-fragment.h b/gegl/property-types/gegl-audio-fragment.h
index 3d56c81..5ac49a2 100644
--- a/gegl/property-types/gegl-audio-fragment.h
+++ b/gegl/property-types/gegl-audio-fragment.h
@@ -100,12 +100,6 @@ typedef struct _GeglAudioFragmentPrivate GeglAudioFragmentPrivate;
 struct _GeglAudioFragment
 {
   GObject parent_instance;
-  int     max_samples;
-  int     xsample_count;
-  int     xchannels;
-  int     xchannel_layout;
-  int     sample_rate;
-  int     pos;
   float  *data[GEGL_MAX_AUDIO_CHANNELS];
   GeglAudioFragmentPrivate *priv;
 };
diff --git a/operations/external/ff-load.c b/operations/external/ff-load.c
index 44d184c..8f77c2e 100644
--- a/operations/external/ff-load.c
+++ b/operations/external/ff-load.c
@@ -272,8 +272,9 @@ decode_audio (GeglOperation *operation,
                   g_warning ("undealt with sample format\n");
                 }
                 gegl_audio_fragment_set_sample_count (af, sample_count);
-                af->pos = p->audio_pos;
-                p->audio_pos += af->xsample_count;
+                gegl_audio_fragment_set_pos (af, p->audio_pos);
+
+                p->audio_pos += sample_count;
                 p->audio_track = g_list_append (p->audio_track, af);
 
                 samples_left -= sample_count;
@@ -569,17 +570,20 @@ static void get_sample_data (Priv *p, long sample_no, float *left, float *right)
   for (; l; l = l->next)
   {
     GeglAudioFragment *af = l->data;
-    if (sample_no > af->pos + af->xsample_count)
+    int channels = gegl_audio_fragment_get_channels (af);
+    int pos = gegl_audio_fragment_get_pos (af);
+    int sample_count = gegl_audio_fragment_get_sample_count (af);
+    if (sample_no > pos + sample_count)
     {
       to_remove ++;
     }
 
-    if (af->pos <= sample_no &&
-        sample_no < af->pos + af->xsample_count)
+    if (pos <= sample_no &&
+        sample_no < pos + sample_count)
       {
-        int i = sample_no - af->pos;
+        int i = sample_no - pos;
         *left  = af->data[0][i];
-        if (af->xchannels == 1)
+        if (channels == 1)
           *right = af->data[0][i];
         else
           *right = af->data[1][i];
@@ -590,7 +594,9 @@ static void get_sample_data (Priv *p, long sample_no, float *left, float *right)
           for (l = p->audio_track; l; l = l->next)
           {
             GeglAudioFragment *af = l->data;
-            if (sample_no > af->pos + af->xsample_count)
+            int pos = gegl_audio_fragment_get_pos (af);
+            int sample_count = gegl_audio_fragment_get_sample_count (af);
+            if (sample_no > pos + sample_count)
             {
               p->audio_track = g_list_remove (p->audio_track, af);
               g_object_unref (af);
@@ -626,20 +632,20 @@ process (GeglOperation       *operation,
 
        if (p->audio_stream && p->audio_stream->codec) // XXX: remove second clause
         {
-          o->audio->sample_rate = p->audio_stream->codec->sample_rate;
-          o->audio->xchannels = 2;
-          o->audio->xchannel_layout = AV_CH_LAYOUT_STEREO;
-
-          gegl_audio_fragment_set_sample_count (o->audio, 
-               samples_per_frame (o->frame,
-               o->frame_rate, o->audio->sample_rate,
-               &sample_start));
+          int sample_count;
+          gegl_audio_fragment_set_sample_rate (o->audio, p->audio_stream->codec->sample_rate);
+          gegl_audio_fragment_set_channels    (o->audio, 2);
+          gegl_audio_fragment_set_channel_layout    (o->audio, GEGL_CH_LAYOUT_STEREO);
 
+          sample_count = samples_per_frame (o->frame,
+               o->frame_rate, p->audio_stream->codec->sample_rate,
+               &sample_start);
+          gegl_audio_fragment_set_sample_count (o->audio, sample_count);
 
          decode_audio (operation, p->prevpts, p->prevpts + 5.0);
           {
             int i;
-            for (i = 0; i < o->audio->xsample_count; i++)
+            for (i = 0; i < sample_count; i++)
             {
               get_sample_data (p, sample_start + i, &o->audio->data[0][i],
                                   &o->audio->data[1][i]);
diff --git a/operations/external/ff-save.c b/operations/external/ff-save.c
index 32f7030..1775235 100644
--- a/operations/external/ff-save.c
+++ b/operations/external/ff-save.c
@@ -125,17 +125,20 @@ static void get_sample_data (Priv *p, long sample_no, float *left, float *right)
   for (; l; l = l->next)
   {
     GeglAudioFragment *af = l->data;
-    if (sample_no > af->pos + af->xsample_count)
+    gint pos = gegl_audio_fragment_get_pos (af);
+    gint channels = gegl_audio_fragment_get_channels (af);
+    gint sample_count = gegl_audio_fragment_get_sample_count (af);
+    if (sample_no > pos + sample_count)
     {
       to_remove ++;
     }
 
-    if (af->pos <= sample_no &&
-        sample_no < af->pos + af->xsample_count)
+    if (pos <= sample_no &&
+        sample_no < pos + sample_count)
       {
-        int i = sample_no - af->pos;
+        int i = sample_no - pos;
         *left  = af->data[0][i];
-        if (af->xchannels == 1)
+        if (channels == 1)
           *right = af->data[0][i];
         else
           *right = af->data[1][i];
@@ -146,7 +149,9 @@ static void get_sample_data (Priv *p, long sample_no, float *left, float *right)
           for (l = p->audio_track; l; l = l->next)
           {
             GeglAudioFragment *af = l->data;
-            if (sample_no > af->pos + af->xsample_count)
+            gint pos = gegl_audio_fragment_get_pos (af);
+            gint sample_count = gegl_audio_fragment_get_sample_count (af);
+            if (sample_no > pos + sample_count)
             {
               p->audio_track = g_list_remove (p->audio_track, af);
               g_object_unref (af);
@@ -254,11 +259,11 @@ open_audio (GeglProperties *o, AVFormatContext * oc, AVStream * st)
   {
     if (o->audio)
     {
-      if (o->audio->sample_rate == 0)
+      if (gegl_audio_fragment_get_sample_rate (o->audio) == 0)
       {
-        o->audio->sample_rate = 48000; // XXX: should skip adding audiostream instead
+        gegl_audio_fragment_set_sample_rate (o->audio, 48000); // XXX: should skip adding audiostream instead
       }
-      p->audio_sample_rate = o->audio->sample_rate;
+      p->audio_sample_rate = gegl_audio_fragment_get_sample_rate (o->audio);
     }
   }
   c->sample_rate = p->audio_sample_rate;
@@ -326,14 +331,18 @@ write_audio_frame (GeglProperties *o, AVFormatContext * oc, AVStream * st)
   /* first we add incoming frames audio samples */
   {
     int i;
-    GeglAudioFragment *af = gegl_audio_fragment_new (o->audio->sample_rate, o->audio->xchannels, 
o->audio->xchannel_layout, o->audio->xsample_count);
-    for (i = 0; i < af->xsample_count; i++)
+    int sample_count = gegl_audio_fragment_get_sample_count (o->audio);
+    GeglAudioFragment *af = gegl_audio_fragment_new (gegl_audio_fragment_get_sample_rate (o->audio),
+                                                     gegl_audio_fragment_get_channels (o->audio),
+                                                     gegl_audio_fragment_get_channel_layout (o->audio),
+                                                     sample_count);
+    for (i = 0; i < sample_count; i++)
       {
         af->data[0][i] = o->audio->data[0][i];
         af->data[1][i] = o->audio->data[1][i];
       }
-    af->pos = p->audio_pos;
-    p->audio_pos += af->xsample_count;
+    gegl_audio_fragment_set_pos (af, p->audio_pos);
+    p->audio_pos += sample_count;
     p->audio_track = g_list_append (p->audio_track, af);
   }
 


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