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