Merge "res_pjsip_mwi: Turn some NOTICEs and WARNINGs into debug 1s."
[asterisk/asterisk.git] / codecs / codec_resample.c
index 718ab88..1c73bf3 100644 (file)
@@ -1,9 +1,10 @@
 /*
  * Asterisk -- An open source telephony toolkit.
  *
- * Copyright (C) 2007, Digium, Inc.
+ * Copyright (C) 2011, Digium, Inc.
  *
  * Russell Bryant <russell@digium.com>
+ * David Vossel <dvossel@digium.com>
  *
  * See http://www.asterisk.org for more information about
  * the Asterisk project. Please do not directly contact
  * \file
  *
  * \brief Resample slinear audio
- *
+ * 
  * \ingroup codecs
  */
 
 /*** MODULEINFO
-       <depend>res_resample</depend>
+       <support_level>core</support_level>
  ***/
 
 #include "asterisk.h"
+#include "speex/speex_resampler.h"
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
-
-/* These are for SHRT_MAX and FLT_MAX -- { */
-#if defined(__Darwin__) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__CYGWIN__)
-#include <float.h>
-#else
-#include <values.h>
-#endif
-#include <limits.h>
-/* } */
+ASTERISK_REGISTER_FILE()
 
 #include "asterisk/module.h"
 #include "asterisk/translate.h"
-
-#include "libresample.h"
-
-#include "slin_resample_ex.h"
-
-#define RESAMPLER_QUALITY 0
-
-#define OUTBUF_SIZE   8096
-
-struct slin16_to_slin8_pvt {
-       void *resampler;
-       double resample_factor;
+#include "asterisk/slin.h"
+
+#define OUTBUF_SAMPLES   11520
+
+static struct ast_translator *translators;
+static int trans_size;
+static struct ast_codec codec_list[] = {
+       {
+               .name = "slin",
+               .type = AST_MEDIA_TYPE_AUDIO,
+               .sample_rate = 8000,
+       },
+       {
+               .name = "slin",
+               .type = AST_MEDIA_TYPE_AUDIO,
+               .sample_rate = 12000,
+       },
+       {
+               .name = "slin",
+               .type = AST_MEDIA_TYPE_AUDIO,
+               .sample_rate = 16000,
+       },
+       {
+               .name = "slin",
+               .type = AST_MEDIA_TYPE_AUDIO,
+               .sample_rate = 24000,
+       },
+       {
+               .name = "slin",
+               .type = AST_MEDIA_TYPE_AUDIO,
+               .sample_rate = 32000,
+       },
+       {
+               .name = "slin",
+               .type = AST_MEDIA_TYPE_AUDIO,
+               .sample_rate = 44100,
+       },
+       {
+               .name = "slin",
+               .type = AST_MEDIA_TYPE_AUDIO,
+               .sample_rate = 48000,
+       },
+       {
+               .name = "slin",
+               .type = AST_MEDIA_TYPE_AUDIO,
+               .sample_rate = 96000,
+       },
+       {
+               .name = "slin",
+               .type = AST_MEDIA_TYPE_AUDIO,
+               .sample_rate = 192000,
+       },
 };
 
-struct slin8_to_slin16_pvt {
-       void *resampler;
-       double resample_factor;
-};
-
-static int slin16_to_slin8_new(struct ast_trans_pvt *pvt)
+static int resamp_new(struct ast_trans_pvt *pvt)
 {
-       struct slin16_to_slin8_pvt *resamp_pvt = pvt->pvt;
-
-       resamp_pvt->resample_factor = 0.5;
+       int err;
 
-       if (!(resamp_pvt->resampler = resample_open(RESAMPLER_QUALITY, 0.5, 0.5)))
+       if (!(pvt->pvt = speex_resampler_init(1, pvt->t->src_codec.sample_rate, pvt->t->dst_codec.sample_rate, 5, &err))) {
                return -1;
+       }
 
-       return 0;
-}
-
-static int slin8_to_slin16_new(struct ast_trans_pvt *pvt)
-{
-       struct slin8_to_slin16_pvt *resamp_pvt = pvt->pvt;
-
-       resamp_pvt->resample_factor = 2.0;
-
-       if (!(resamp_pvt->resampler = resample_open(RESAMPLER_QUALITY, 2.0, 2.0)))
-               return -1;
+       ast_assert(pvt->f.subclass.format == NULL);
+       pvt->f.subclass.format = ao2_bump(ast_format_cache_get_slin_by_rate(pvt->t->dst_codec.sample_rate));
 
        return 0;
 }
 
-static void slin16_to_slin8_destroy(struct ast_trans_pvt *pvt)
-{
-       struct slin16_to_slin8_pvt *resamp_pvt = pvt->pvt;
-
-       if (resamp_pvt->resampler)
-               resample_close(resamp_pvt->resampler);
-}
-
-static void slin8_to_slin16_destroy(struct ast_trans_pvt *pvt)
+static void resamp_destroy(struct ast_trans_pvt *pvt)
 {
-       struct slin8_to_slin16_pvt *resamp_pvt = pvt->pvt;
+       SpeexResamplerState *resamp_pvt = pvt->pvt;
 
-       if (resamp_pvt->resampler)
-               resample_close(resamp_pvt->resampler);
+       speex_resampler_destroy(resamp_pvt);
 }
 
-static int resample_frame(struct ast_trans_pvt *pvt,
-       void *resampler, double resample_factor, struct ast_frame *f)
+static int resamp_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 {
-       int total_in_buf_used = 0;
-       int total_out_buf_used = 0;
-       int16_t *in_buf = (int16_t *) f->data;
-       int16_t *out_buf = (int16_t *) pvt->outbuf + pvt->samples;
-       float in_buf_f[f->samples];
-       float out_buf_f[2048];
-       int res = 0;
-       int i;
-
-       for (i = 0; i < f->samples; i++)
-               in_buf_f[i] = in_buf[i] * (FLT_MAX / SHRT_MAX);
-
-       while (total_in_buf_used < f->samples) {
-               int in_buf_used, out_buf_used;
-
-               out_buf_used = resample_process(resampler, resample_factor,
-                       &in_buf_f[total_in_buf_used], f->samples - total_in_buf_used,
-                       0, &in_buf_used,
-                       &out_buf_f[total_out_buf_used], ARRAY_LEN(out_buf_f) - total_out_buf_used);
+       SpeexResamplerState *resamp_pvt = pvt->pvt;
+       unsigned int out_samples = OUTBUF_SAMPLES - pvt->samples;
+       unsigned int in_samples;
 
-               if (out_buf_used < 0)
-                       break;
-
-               total_out_buf_used += out_buf_used;
-               total_in_buf_used += in_buf_used;
-
-               if (total_out_buf_used == ARRAY_LEN(out_buf_f)) {
-                       ast_log(LOG_ERROR, "Output buffer filled ... need to increase its size\n");
-                       res = -1;
-                       break;
-               }
+       if (!f->datalen) {
+               return -1;
        }
+       in_samples = f->datalen / 2;
 
-       for (i = 0; i < total_out_buf_used; i++)
-               out_buf[i] = out_buf_f[i] * (SHRT_MAX / FLT_MAX);       
-
-       pvt->samples += total_out_buf_used;
-       pvt->datalen += (total_out_buf_used * sizeof(int16_t));
-
-       return res;
-}
-
-static int slin16_to_slin8_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
-{
-       struct slin16_to_slin8_pvt *resamp_pvt = pvt->pvt;
-
-       return resample_frame(pvt, resamp_pvt->resampler, resamp_pvt->resample_factor, f);
-}
-
-static int slin8_to_slin16_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
-{
-       struct slin8_to_slin16_pvt *resamp_pvt = pvt->pvt;
+       speex_resampler_process_int(resamp_pvt,
+               0,
+               f->data.ptr,
+               &in_samples,
+               pvt->outbuf.i16 + pvt->samples,
+               &out_samples);
 
-       return resample_frame(pvt, resamp_pvt->resampler, resamp_pvt->resample_factor, f);
-}
-
-static struct ast_frame *slin16_to_slin8_sample(void)
-{
-       static struct ast_frame f = {
-               .frametype = AST_FRAME_VOICE,
-               .subclass = AST_FORMAT_SLINEAR16,
-               .datalen = sizeof(slin16_slin8_ex),
-               .samples = sizeof(slin16_slin8_ex) / sizeof(slin16_slin8_ex[0]),
-               .src = __PRETTY_FUNCTION__,
-               .data = slin16_slin8_ex,
-       };
-
-       return &f;
-}
+       pvt->samples += out_samples;
+       pvt->datalen += out_samples * 2;
 
-static struct ast_frame *slin8_to_slin16_sample(void)
-{
-       static struct ast_frame f = {
-               .frametype = AST_FRAME_VOICE,
-               .subclass = AST_FORMAT_SLINEAR,
-               .datalen = sizeof(slin8_slin16_ex),
-               .samples = sizeof(slin8_slin16_ex) / sizeof(slin8_slin16_ex[0]),
-               .src = __PRETTY_FUNCTION__,
-               .data = slin8_slin16_ex,
-       };
-
-       return &f;
+       return 0;
 }
 
-static struct ast_translator slin16_to_slin8 = {
-       .name = "slin16_to_slin8",
-       .srcfmt = AST_FORMAT_SLINEAR16,
-       .dstfmt = AST_FORMAT_SLINEAR,
-       .newpvt = slin16_to_slin8_new,
-       .destroy = slin16_to_slin8_destroy,
-       .framein = slin16_to_slin8_framein,
-       .sample = slin16_to_slin8_sample,
-       .desc_size = sizeof(struct slin16_to_slin8_pvt),
-       .buffer_samples = (OUTBUF_SIZE / sizeof(int16_t)),
-       .buf_size = OUTBUF_SIZE,
-};
-
-static struct ast_translator slin8_to_slin16 = {
-       .name = "slin8_to_slin16",
-       .srcfmt = AST_FORMAT_SLINEAR,
-       .dstfmt = AST_FORMAT_SLINEAR16,
-       .newpvt = slin8_to_slin16_new,
-       .destroy = slin8_to_slin16_destroy,
-       .framein = slin8_to_slin16_framein,
-       .sample = slin8_to_slin16_sample,
-       .desc_size = sizeof(struct slin8_to_slin16_pvt),
-       .buffer_samples = OUTBUF_SIZE,
-       .buf_size = OUTBUF_SIZE,
-};
-
 static int unload_module(void)
 {
        int res = 0;
+       int idx;
 
-       res |= ast_unregister_translator(&slin16_to_slin8);
-       res |= ast_unregister_translator(&slin8_to_slin16);
+       for (idx = 0; idx < trans_size; idx++) {
+               res |= ast_unregister_translator(&translators[idx]);
+       }
+       ast_free(translators);
 
        return res;
 }
@@ -228,9 +151,39 @@ static int unload_module(void)
 static int load_module(void)
 {
        int res = 0;
+       int x, y, idx = 0;
+
+       trans_size = ARRAY_LEN(codec_list) * (ARRAY_LEN(codec_list) - 1);
+       if (!(translators = ast_calloc(1, sizeof(struct ast_translator) * trans_size))) {
+               return AST_MODULE_LOAD_FAILURE;
+       }
 
-       res |= ast_register_translator(&slin16_to_slin8);
-       res |= ast_register_translator(&slin8_to_slin16);
+       for (x = 0; x < ARRAY_LEN(codec_list); x++) {
+               for (y = 0; y < ARRAY_LEN(codec_list); y++) {
+                       if (x == y) {
+                               continue;
+                       }
+                       translators[idx].newpvt = resamp_new;
+                       translators[idx].destroy = resamp_destroy;
+                       translators[idx].framein = resamp_framein;
+                       translators[idx].desc_size = 0;
+                       translators[idx].buffer_samples = OUTBUF_SAMPLES;
+                       translators[idx].buf_size = (OUTBUF_SAMPLES * sizeof(int16_t));
+                       memcpy(&translators[idx].src_codec, &codec_list[x], sizeof(struct ast_codec));
+                       memcpy(&translators[idx].dst_codec, &codec_list[y], sizeof(struct ast_codec));
+                       snprintf(translators[idx].name, sizeof(translators[idx].name), "slin %ukhz -> %ukhz",
+                               translators[idx].src_codec.sample_rate, translators[idx].dst_codec.sample_rate);
+                       res |= ast_register_translator(&translators[idx]);
+                       idx++;
+               }
+
+       }
+       /* in case ast_register_translator() failed, we call unload_module() and
+       ast_unregister_translator won't fail.*/
+       if (res) {
+               unload_module();
+               return AST_MODULE_LOAD_FAILURE;
+       }
 
        return AST_MODULE_LOAD_SUCCESS;
 }