gst_plugins_base/gst/audioconvert/gstaudioquantize.c
branchRCL_3
changeset 30 7e817e7e631c
parent 29 567bb019e3e3
equal deleted inserted replaced
29:567bb019e3e3 30:7e817e7e631c
    34 #include <gst/gst.h>
    34 #include <gst/gst.h>
    35 #include <string.h>
    35 #include <string.h>
    36 #include <math.h>
    36 #include <math.h>
    37 #include "audioconvert.h"
    37 #include "audioconvert.h"
    38 #include "gstaudioquantize.h"
    38 #include "gstaudioquantize.h"
    39 
       
    40 #include "gstfastrandom.h"
       
    41 
    39 
    42 #define MAKE_QUANTIZE_FUNC_NAME(name)                                   \
    40 #define MAKE_QUANTIZE_FUNC_NAME(name)                                   \
    43 gst_audio_quantize_quantize_##name
    41 gst_audio_quantize_quantize_##name
    44 
    42 
    45 /* Quantize functions for gint32 as intermediate format */
    43 /* Quantize functions for gint32 as intermediate format */
   144 #define INIT_DITHER_RPDF_I()                                            \
   142 #define INIT_DITHER_RPDF_I()                                            \
   145   gint32 rand;                                                          \
   143   gint32 rand;                                                          \
   146   gint32 dither = (1<<(scale));
   144   gint32 dither = (1<<(scale));
   147 
   145 
   148 #define ADD_DITHER_RPDF_I()                                             \
   146 #define ADD_DITHER_RPDF_I()                                             \
   149         rand = gst_fast_random_int32_range (bias - dither,              \
   147         rand = g_rand_int_range (ctx->dither_random, bias - dither,     \
   150 	    bias + dither);                                             \
   148                bias + dither);                                          \
   151         if (rand > 0 && tmp > 0 && G_MAXINT32 - tmp <= rand)            \
   149         if (rand > 0 && tmp > 0 && G_MAXINT32 - tmp <= rand)            \
   152                 tmp = G_MAXINT32;                                       \
   150                 tmp = G_MAXINT32;                                       \
   153         else if (rand < 0 && tmp < 0 && G_MININT32 - tmp >= rand)       \
   151         else if (rand < 0 && tmp < 0 && G_MININT32 - tmp >= rand)       \
   154                 tmp = G_MININT32;                                       \
   152                 tmp = G_MININT32;                                       \
   155         else                                                            \
   153         else                                                            \
   157 
   155 
   158 #define INIT_DITHER_RPDF_F()                                            \
   156 #define INIT_DITHER_RPDF_F()                                            \
   159   gdouble dither = 1.0/(1U<<(32 - scale - 1));
   157   gdouble dither = 1.0/(1U<<(32 - scale - 1));
   160 
   158 
   161 #define ADD_DITHER_RPDF_F()                                             \
   159 #define ADD_DITHER_RPDF_F()                                             \
   162         tmp += gst_fast_random_double_range (- dither, dither);
   160         tmp += g_rand_double_range (ctx->dither_random, - dither,       \
       
   161                dither);
   163 
   162 
   164 #define INIT_DITHER_TPDF_I()                                            \
   163 #define INIT_DITHER_TPDF_I()                                            \
   165   gint32 rand;                                                          \
   164   gint32 rand;                                                          \
   166   gint32 dither = (1<<(scale - 1));                                     \
   165   gint32 dither = (1<<(scale - 1));                                     \
   167   bias = bias >> 1;
   166   bias = bias >> 1;
   168 
   167 
   169 #define ADD_DITHER_TPDF_I()                                             \
   168 #define ADD_DITHER_TPDF_I()                                             \
   170         rand = gst_fast_random_int32_range (bias - dither,              \
   169         rand = g_rand_int_range (ctx->dither_random, bias - dither,     \
   171                    bias + dither - 1)                                   \
   170                bias + dither - 1)                                           \
   172                + gst_fast_random_int32_range (bias - dither,            \
   171                + g_rand_int_range (ctx->dither_random, bias - dither,   \
   173                    bias + dither - 1);                                  \
   172                bias + dither - 1);                                          \
   174         if (rand > 0 && tmp > 0 && G_MAXINT32 - tmp <= rand)            \
   173         if (rand > 0 && tmp > 0 && G_MAXINT32 - tmp <= rand)            \
   175                 tmp = G_MAXINT32;                                       \
   174                 tmp = G_MAXINT32;                                       \
   176         else if (rand < 0 && tmp < 0 && G_MININT32 - tmp >= rand)       \
   175         else if (rand < 0 && tmp < 0 && G_MININT32 - tmp >= rand)       \
   177                 tmp = G_MININT32;                                       \
   176                 tmp = G_MININT32;                                       \
   178         else                                                            \
   177         else                                                            \
   180 
   179 
   181 #define INIT_DITHER_TPDF_F()                                            \
   180 #define INIT_DITHER_TPDF_F()                                            \
   182   gdouble dither = 1.0/(1U<<(32 - scale));
   181   gdouble dither = 1.0/(1U<<(32 - scale));
   183 
   182 
   184 #define ADD_DITHER_TPDF_F()                                             \
   183 #define ADD_DITHER_TPDF_F()                                             \
   185         tmp += gst_fast_random_double_range (- dither, dither)          \
   184         tmp += g_rand_double_range (ctx->dither_random, - dither,       \
   186                + gst_fast_random_double_range (- dither, dither);
   185                dither)                                                  \
       
   186                + g_rand_double_range (ctx->dither_random, - dither,     \
       
   187                dither);
   187 
   188 
   188 #define INIT_DITHER_TPDF_HF_I()                                         \
   189 #define INIT_DITHER_TPDF_HF_I()                                         \
   189   gint32 rand;                                                          \
   190   gint32 rand;                                                          \
   190   gint32 dither = (1<<(scale-1));                                       \
   191   gint32 dither = (1<<(scale-1));                                       \
   191   gint32 *last_random = (gint32 *) ctx->last_random, tmp_rand;          \
   192   gint32 *last_random = (gint32 *) ctx->last_random, tmp_rand;          \
   192   bias = bias >> 1;
   193   bias = bias >> 1;
   193 
   194 
   194 #define ADD_DITHER_TPDF_HF_I()                                          \
   195 #define ADD_DITHER_TPDF_HF_I()                                          \
   195         tmp_rand = gst_fast_random_int32_range (bias - dither,          \
   196         tmp_rand = g_rand_int_range (ctx->dither_random, bias - dither, \
   196                        bias + dither);                                  \
   197                    bias + dither); \
   197         rand = tmp_rand - last_random[chan_pos];                        \
   198         rand = tmp_rand - last_random[chan_pos];                        \
   198         last_random[chan_pos] = tmp_rand;                               \
   199         last_random[chan_pos] = tmp_rand;                               \
   199         if (rand > 0 && tmp > 0 && G_MAXINT32 - tmp <= rand)            \
   200         if (rand > 0 && tmp > 0 && G_MAXINT32 - tmp <= rand)            \
   200                 tmp = G_MAXINT32;                                       \
   201                 tmp = G_MAXINT32;                                       \
   201         else if (rand < 0 && tmp < 0 && G_MININT32 - tmp >= rand)       \
   202         else if (rand < 0 && tmp < 0 && G_MININT32 - tmp >= rand)       \
   210   gdouble rand;                                                         \
   211   gdouble rand;                                                         \
   211   gdouble dither = 1.0/(1U<<(32 - scale));                              \
   212   gdouble dither = 1.0/(1U<<(32 - scale));                              \
   212   gdouble *last_random = (gdouble *) ctx->last_random, tmp_rand;
   213   gdouble *last_random = (gdouble *) ctx->last_random, tmp_rand;
   213 
   214 
   214 #define ADD_DITHER_TPDF_HF_F()                                          \
   215 #define ADD_DITHER_TPDF_HF_F()                                          \
   215         tmp_rand = gst_fast_random_double_range (- dither, dither);     \
   216         tmp_rand = g_rand_double_range (ctx->dither_random, - dither,   \
       
   217                    dither);                                             \
   216         rand = tmp_rand - last_random[chan_pos];                        \
   218         rand = tmp_rand - last_random[chan_pos];                        \
   217         last_random[chan_pos] = tmp_rand;                               \
   219         last_random[chan_pos] = tmp_rand;                               \
   218         tmp += rand;
   220         tmp += rand;
   219 
   221 
   220 /* Noise shaping definitions.
   222 /* Noise shaping definitions.
   426     case NOISE_SHAPING_ERROR_FEEDBACK:
   428     case NOISE_SHAPING_ERROR_FEEDBACK:
   427     case NOISE_SHAPING_NONE:
   429     case NOISE_SHAPING_NONE:
   428     default:
   430     default:
   429       break;
   431       break;
   430   }
   432   }
       
   433   return;
   431 
   434 
   432   g_free (ctx->error_buf);
   435   g_free (ctx->error_buf);
   433   ctx->error_buf = NULL;
   436   ctx->error_buf = NULL;
   434   return;
   437   return;
   435 }
   438 }
   441     case DITHER_TPDF_HF:
   444     case DITHER_TPDF_HF:
   442       if (ctx->out.is_int)
   445       if (ctx->out.is_int)
   443         ctx->last_random = g_new0 (gint32, ctx->out.channels);
   446         ctx->last_random = g_new0 (gint32, ctx->out.channels);
   444       else
   447       else
   445         ctx->last_random = g_new0 (gdouble, ctx->out.channels);
   448         ctx->last_random = g_new0 (gdouble, ctx->out.channels);
       
   449       ctx->dither_random = g_rand_new ();
   446       break;
   450       break;
   447     case DITHER_RPDF:
   451     case DITHER_RPDF:
   448     case DITHER_TPDF:
   452     case DITHER_TPDF:
       
   453       ctx->dither_random = g_rand_new ();
   449       ctx->last_random = NULL;
   454       ctx->last_random = NULL;
   450       break;
   455       break;
   451     case DITHER_NONE:
   456     case DITHER_NONE:
   452     default:
   457     default:
       
   458       ctx->dither_random = NULL;
   453       ctx->last_random = NULL;
   459       ctx->last_random = NULL;
   454       break;
   460       break;
   455   }
   461   }
   456   return;
   462   return;
   457 }
   463 }
   458 
   464 
   459 static void
   465 static void
   460 gst_audio_quantize_free_dither (AudioConvertCtx * ctx)
   466 gst_audio_quantize_free_dither (AudioConvertCtx * ctx)
   461 {
   467 {
   462   g_free (ctx->last_random);
   468   g_free (ctx->last_random);
       
   469   if (ctx->dither_random)
       
   470     g_rand_free (ctx->dither_random);
   463 
   471 
   464   return;
   472   return;
   465 }
   473 }
   466 
   474 
   467 static void
   475 static void