]> git.sesse.net Git - vlc/blobdiff - modules/visualization/glspectrum.c
Mark unreachable code on GCC even if NDEBUG
[vlc] / modules / visualization / glspectrum.c
index a361ef0beda3846546c24408a4805ff7741ef4b1..8efdb8f4cd57617e7646b88b6de7f5a6898a52ed 100644 (file)
@@ -29,8 +29,7 @@
 #include <vlc_common.h>
 #include <vlc_plugin.h>
 #include <vlc_aout.h>
-#include <vlc_vout.h>
-#include <vlc_vout_wrapper.h>
+#include <vlc_vout_window.h>
 #include <vlc_opengl.h>
 #include <vlc_filter.h>
 #include <vlc_rand.h>
@@ -40,6 +39,7 @@
 #include <math.h>
 
 #include "visual/fft.h"
+#include "visual/window.h"
 
 
 /*****************************************************************************
@@ -75,8 +75,6 @@ vlc_module_end()
 struct filter_sys_t
 {
     vlc_thread_t thread;
-    vlc_sem_t    ready;
-    bool         b_error;
 
     /* Audio data */
     unsigned i_channels;
@@ -85,25 +83,23 @@ struct filter_sys_t
     int16_t *p_prev_s16_buff;
 
     /* Opengl */
-    vout_thread_t  *p_vout;
-    vout_display_t *p_vd;
+    vlc_gl_t *gl;
 
     float f_rotationAngle;
     float f_rotationIncrement;
 
-    /* Window size */
-    int i_width;
-    int i_height;
+    /* FFT window parameters */
+    window_param wind_param;
 };
 
 
 static block_t *DoWork(filter_t *, block_t *);
 static void *Thread(void *);
 
-#define SPECTRUM_WIDTH 4.0
+#define SPECTRUM_WIDTH 4.f
 #define NB_BANDS 20
-#define ROTATION_INCREMENT 0.1
-#define BAR_DECREMENT 0.075
+#define ROTATION_INCREMENT .1f
+#define BAR_DECREMENT .075f
 #define ROTATION_MAX 20
 
 const GLfloat lightZeroColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
@@ -124,10 +120,6 @@ static int Open(vlc_object_t * p_this)
         return VLC_ENOMEM;
 
     /* Create the object for the thread */
-    vlc_sem_init(&p_sys->ready, 0);
-    p_sys->b_error = false;
-    p_sys->i_width = var_InheritInteger(p_filter, "glspectrum-width");
-    p_sys->i_height = var_InheritInteger(p_filter, "glspectrum-height");
     p_sys->i_channels = aout_FormatNbChannels(&p_filter->fmt_in.audio);
     p_sys->i_prev_nb_samples = 0;
     p_sys->p_prev_s16_buff = NULL;
@@ -135,24 +127,32 @@ static int Open(vlc_object_t * p_this)
     p_sys->f_rotationAngle = 0;
     p_sys->f_rotationIncrement = ROTATION_INCREMENT;
 
+    /* Fetch the FFT window parameters */
+    window_get_param( VLC_OBJECT( p_filter ), &p_sys->wind_param );
+
     /* Create the FIFO for the audio data. */
     p_sys->fifo = block_FifoNew();
     if (p_sys->fifo == NULL)
         goto error;
 
-    /* Create the thread */
-    if (vlc_clone(&p_sys->thread, Thread, p_filter,
-                  VLC_THREAD_PRIORITY_VIDEO))
-        goto error;
+    /* Create the openGL provider */
+    vout_window_cfg_t cfg = {
+        .width = var_InheritInteger(p_filter, "glspectrum-width"),
+        .height = var_InheritInteger(p_filter, "glspectrum-height"),
+    };
 
-    /* Wait for the displaying thread to be ready. */
-    vlc_sem_wait(&p_sys->ready);
-    if (p_sys->b_error)
+    p_sys->gl = vlc_gl_surface_Create(p_this, &cfg, NULL);
+    if (p_sys->gl == NULL)
     {
-        vlc_join(p_sys->thread, NULL);
+        block_FifoRelease(p_sys->fifo);
         goto error;
     }
 
+    /* Create the thread */
+    if (vlc_clone(&p_sys->thread, Thread, p_filter,
+                  VLC_THREAD_PRIORITY_VIDEO))
+        goto error;
+
     p_filter->fmt_in.audio.i_format = VLC_CODEC_FL32;
     p_filter->fmt_out.audio = p_filter->fmt_in.audio;
     p_filter->pf_audio_filter = DoWork;
@@ -160,7 +160,6 @@ static int Open(vlc_object_t * p_this)
     return VLC_SUCCESS;
 
 error:
-    vlc_sem_destroy(&p_sys->ready);
     free(p_sys);
     return VLC_EGENERIC;
 }
@@ -180,13 +179,9 @@ static void Close(vlc_object_t *p_this)
     vlc_join(p_sys->thread, NULL);
 
     /* Free the ressources */
-    vout_DeleteDisplay(p_sys->p_vd, NULL);
-    vlc_object_release(p_sys->p_vout);
-
+    vlc_gl_surface_Destroy(p_sys->gl);
     block_FifoRelease(p_sys->fifo);
     free(p_sys->p_prev_s16_buff);
-
-    vlc_sem_destroy(&p_sys->ready);
     free(p_sys);
 }
 
@@ -208,7 +203,7 @@ static block_t *DoWork(filter_t *p_filter, block_t *p_in_buf)
 /**
   * Init the OpenGL scene.
   **/
-static void initOpenGLScene()
+static void initOpenGLScene(void)
 {
     glEnable(GL_CULL_FACE);
     glEnable(GL_DEPTH_TEST);
@@ -240,42 +235,42 @@ static void initOpenGLScene()
 /**
  * Draw one bar of the Spectrum.
  */
-static void drawBar()
+static void drawBar(void)
 {
-    const float w = SPECTRUM_WIDTH / NB_BANDS - 0.05;
+    const float w = SPECTRUM_WIDTH / NB_BANDS - 0.05f;
 
     const GLfloat vertexCoords[] = {
-        0.0, 0.0, 0.0,   w, 0.0, 0.0,   0.0, 1.0, 0.0,
-        0.0, 1.0, 0.0,   w, 0.0, 0.0,   w  , 1.0, 0.0,
+        0.f, 0.f, 0.f,     w, 0.f, 0.f,   0.f, 1.f, 0.f,
+        0.f, 1.f, 0.f,     w, 0.f, 0.f,     w, 1.f, 0.f,
 
-        0.0, 0.0, -w,    0.0, 0.0, 0.0,   0.0, 1.0, -w,
-        0.0, 1.0, -w,    0.0, 0.0, 0.0,   0.0, 1.0, 0.0,
+        0.f, 0.f, -w,    0.f, 0.f, 0.f,   0.f, 1.f,  -w,
+        0.f, 1.f, -w,    0.f, 0.f, 0.f,   0.f, 1.f, 0.f,
 
-        w, 0.0, 0.0,     w, 0.0, -w,   w, 1.0, 0.0,
-        w, 1.0, 0.0,     w, 0.0, -w,   w, 1.0, -w,
+          w, 0.f, 0.f,     w, 0.f,  -w,     w, 1.f, 0.f,
+          w, 1.f, 0.f,     w, 0.f,  -w,     w, 1.f,  -w,
 
-        w, 0.0, -w,      0.0, 0.0, -w,  0.0, 1.0, -w,
-        0.0, 1.0, -w,    w, 1.0, -w,    w, 0.0, -w,
+          w, 0.f,  -w,   0.f, 0.f,  -w,   0.f, 1.f,  -w,
+        0.f, 1.f,  -w,     w, 1.f,  -w,     w, 0.f,  -w,
 
-        0.0, 1.0, 0.0,   w, 1.0, 0.0,   w, 1.0, -w,
-        0.0, 1.0, 0.0,   w, 1.0, -w,    0.0, 1.0, -w,
+        0.f, 1.f, 0.f,     w, 1.f, 0.f,     w, 1.f,  -w,
+        0.f, 1.f, 0.f,     w, 1.f,  -w,   0.f, 1.f,  -w,
     };
 
     const GLfloat normals[] = {
-        0.0, 0.0, 1.0,   0.0, 0.0, 1.0,   0.0, 0.0, 1.0,
-        0.0, 0.0, 1.0,   0.0, 0.0, 1.0,   0.0, 0.0, 1.0,
+        0.f, 0.f, 1.f,   0.f, 0.f, 1.f,   0.f, 0.f, 1.f,
+        0.f, 0.f, 1.f,   0.f, 0.f, 1.f,   0.f, 0.f, 1.f,
 
-        -1.0, 0.0, 0.0,   -1.0, 0.0, 0.0,   -1.0, 0.0, 0.0,
-        -1.0, 0.0, 0.0,   -1.0, 0.0, 0.0,   -1.0, 0.0, 0.0,
+        -1.f, 0.f, 0.f,   -1.f, 0.f, 0.f,   -1.f, 0.f, 0.f,
+        -1.f, 0.f, 0.f,   -1.f, 0.f, 0.f,   -1.f, 0.f, 0.f,
 
-        1.0, 0.0, 0.0,   1.0, 0.0, 0.0,   1.0, 0.0, 0.0,
-        1.0, 0.0, 0.0,   1.0, 0.0, 0.0,   1.0, 0.0, 0.0,
+        1.f, 0.f, 0.f,   1.f, 0.f, 0.f,   1.f, 0.f, 0.f,
+        1.f, 0.f, 0.f,   1.f, 0.f, 0.f,   1.f, 0.f, 0.f,
 
-        0.0, 0.0, -1.0,   0.0, 0.0, -1.0,   0.0, 0.0, -1.0,
-        0.0, 0.0, -1.0,   0.0, 0.0, -1.0,   0.0, 0.0, -1.0,
+        0.f, 0.f, -1.f,   0.f, 0.f, -1.f,   0.f, 0.f, -1.f,
+        0.f, 0.f, -1.f,   0.f, 0.f, -1.f,   0.f, 0.f, -1.f,
 
-        0.0, 1.0, 0.0,   0.0, 1.0, 0.0,   0.0, 1.0, 0.0,
-        0.0, 1.0, 0.0,   0.0, 1.0, 0.0,   0.0, 1.0, 0.0,
+        0.f, 1.f, 0.f,   0.f, 1.f, 0.f,   0.f, 1.f, 0.f,
+        0.f, 1.f, 0.f,   0.f, 1.f, 0.f,   0.f, 1.f, 0.f,
     };
 
     glVertexPointer(3, GL_FLOAT, 0, vertexCoords);
@@ -292,20 +287,20 @@ static void setBarColor(float f_height)
 {
     float r, b;
 
-#define BAR_MAX_HEIGHT 4.2
-    r = -1.0 + 2 / BAR_MAX_HEIGHT * f_height;
-    b = 2.0 - 2 / BAR_MAX_HEIGHT * f_height;
+#define BAR_MAX_HEIGHT 4.2f
+    r = -1.f + 2 / BAR_MAX_HEIGHT * f_height;
+    b = 2.f - 2 / BAR_MAX_HEIGHT * f_height;
 #undef BAR_MAX_HEIGHT
 
     /* Test the ranges. */
-    r = r > 1.0 ? 1.0 : r;
-    b = b > 1.0 ? 1.0 : b;
+    r = r > 1.f ? 1.f : r;
+    b = b > 1.f ? 1.f : b;
 
-    r = r < 0.0 ? 0.0 : r;
-    b = b < 0.0 ? 0.0 : b;
+    r = r < 0.f ? 0.f : r;
+    b = b < 0.f ? 0.f : b;
 
     /* Set the bar color. */
-    glColor4f(r, 0.0, b, 1.0);
+    glColor4f(r, 0.f, b, 1.f);
 }
 
 
@@ -316,7 +311,7 @@ static void setBarColor(float f_height)
 static void drawBars(float heights[])
 {
     glPushMatrix();
-    glTranslatef(-2.0, 0.0, 0.0);
+    glTranslatef(-2.f, 0.f, 0.f);
 
     glEnableClientState(GL_VERTEX_ARRAY);
     glEnableClientState(GL_NORMAL_ARRAY);
@@ -325,12 +320,12 @@ static void drawBars(float heights[])
     for (unsigned i = 0; i < NB_BANDS; ++i)
     {
         glPushMatrix();
-        glScalef(1.0, heights[i], 1.0);
+        glScalef(1.f, heights[i], 1.f);
         setBarColor(heights[i]);
         drawBar();
         glPopMatrix();
 
-        glTranslatef(w, 0.0, 0.0);
+        glTranslatef(w, 0.f, 0.f);
     }
 
     glDisableClientState(GL_VERTEX_ARRAY);
@@ -348,55 +343,11 @@ static void *Thread( void *p_data )
 {
     filter_t  *p_filter = (filter_t*)p_data;
     filter_sys_t *p_sys = p_filter->p_sys;
+    vlc_gl_t *gl = p_sys->gl;
 
-    video_format_t fmt;
-    vlc_gl_t *gl;
-    unsigned int i_last_width = 0;
-    unsigned int i_last_height = 0;
-
-    /* Create the openGL provider */
-    p_sys->p_vout =
-        (vout_thread_t *)vlc_object_create(p_filter, sizeof(vout_thread_t));
-    if (!p_sys->p_vout)
-        goto error;
-
-    /* Configure the video format for the opengl provider. */
-    video_format_Init(&fmt, 0);
-    video_format_Setup(&fmt, VLC_CODEC_RGB32,
-                       p_sys->i_width, p_sys->i_height, 0, 1 );
-    fmt.i_sar_num = 1;
-    fmt.i_sar_den = 1;
-
-    /* Init vout state. */
-    vout_display_state_t state;
-    memset(&state, 0, sizeof(state));
-    state.cfg.display.sar.num = 1;
-    state.cfg.display.sar.den = 1;
-    state.cfg.is_display_filled = true;
-    state.cfg.zoom.num = 1;
-    state.cfg.zoom.den = 1;
-    state.sar.num = 1;
-    state.sar.den = 1;
-
-    p_sys->p_vd = vout_NewDisplay(p_sys->p_vout, &fmt, &state,
-                                  "opengl", 1000000, 1000000);
-    if (!p_sys->p_vd)
-    {
-        vlc_object_release(p_sys->p_vout);
-        goto error;
-    }
-
-    gl = vout_GetDisplayOpengl(p_sys->p_vd);
-    if (!gl)
-    {
-        vout_DeleteDisplay(p_sys->p_vd, NULL);
-        vlc_object_release(p_sys->p_vout);
-        goto error;
-    }
-
-    vlc_sem_post(&p_sys->ready);
-
+    vlc_gl_MakeCurrent(gl);
     initOpenGLScene();
+    vlc_gl_ReleaseCurrent(gl);
 
     float height[NB_BANDS] = {0};
 
@@ -405,26 +356,18 @@ static void *Thread( void *p_data )
         block_t *block = block_FifoGet(p_sys->fifo);
 
         int canc = vlc_savecancel();
+        unsigned win_width, win_height;
 
-        /* Manage the events */
-        vout_ManageDisplay(p_sys->p_vd, true);
-        if (p_sys->p_vd->cfg->display.width != i_last_width ||
-            p_sys->p_vd->cfg->display.height != i_last_height)
-        {
-            /* FIXME it is not perfect as we will have black bands */
-            vout_display_place_t place;
-            vout_display_PlacePicture(&place, &p_sys->p_vd->source,
-                                      p_sys->p_vd->cfg, false);
-
-            i_last_width  = p_sys->p_vd->cfg->display.width;
-            i_last_height = p_sys->p_vd->cfg->display.height;
-        }
+        vlc_gl_MakeCurrent(gl);
+        if (vlc_gl_surface_CheckSize(gl, &win_width, &win_height))
+            glViewport(0, 0, win_width, win_height);
 
         /* Horizontal scale for 20-band equalizer */
         const unsigned xscale[] = {0,1,2,3,4,5,6,7,8,11,15,20,27,
                                    36,47,62,82,107,141,184,255};
 
         fft_state *p_state = NULL; /* internal FFT data */
+        DEFINE_WIND_CONTEXT(wind_ctx); /* internal window data */
 
         unsigned i, j;
         float p_output[FFT_BUFFER_SIZE];           /* Raw FFT Result  */
@@ -436,6 +379,11 @@ static void *Thread( void *p_data )
         int16_t  *p_buffs;                         /* int16_t converted buffer */
         int16_t  *p_s16_buff;                      /* int16_t converted buffer */
 
+        if (!block->i_nb_samples) {
+            msg_Err(p_filter, "no samples yet");
+            goto release;
+        }
+
         /* Allocate the buffer only if the number of samples change */
         if (block->i_nb_samples != p_sys->i_prev_nb_samples)
         {
@@ -455,7 +403,7 @@ static void *Thread( void *p_data )
         {
             union {float f; int32_t i;} u;
 
-            u.f = *p_buffl + 384.0;
+            u.f = *p_buffl + 384.f;
             if (u.i > 0x43c07fff)
                 *p_buffs = 32767;
             else if (u.i < 0x43bf8000)
@@ -471,6 +419,11 @@ static void *Thread( void *p_data )
             msg_Err(p_filter,"unable to initialize FFT transform");
             goto release;
         }
+        if (!window_init(FFT_BUFFER_SIZE, &p_sys->wind_param, &wind_ctx))
+        {
+            msg_Err(p_filter,"unable to initialize FFT window");
+            goto release;
+        }
         p_buffs = p_s16_buff;
         for (i = 0 ; i < FFT_BUFFER_SIZE; i++)
         {
@@ -481,6 +434,7 @@ static void *Thread( void *p_data )
             if (p_buffs >= &p_s16_buff[block->i_nb_samples * p_sys->i_channels])
                 p_buffs = p_s16_buff;
         }
+        window_scale_in_place (p_buffer1, &wind_ctx);
         fft_perform (p_buffer1, p_output, p_state);
 
         for (i = 0; i< FFT_BUFFER_SIZE; ++i)
@@ -503,7 +457,7 @@ static void *Thread( void *p_data )
                      y = p_dest[j];
             }
             /* Calculate the height of the bar */
-            float new_height = y != 0 ? log(y) * 0.4 : 0;
+            float new_height = y != 0 ? logf(y) * 0.4f : 0;
             height[i] = new_height > height[i]
                         ? new_height : height[i];
         }
@@ -532,16 +486,12 @@ static void *Thread( void *p_data )
         }
 
 release:
+        window_close(&wind_ctx);
         fft_close(p_state);
+        vlc_gl_ReleaseCurrent(gl);
         block_Release(block);
         vlc_restorecancel(canc);
     }
 
-    assert(0);
-
-error:
-    p_sys->b_error = true;
-    vlc_sem_post(&p_sys->ready);
-    return NULL;
+    vlc_assert_unreachable();
 }
-