xiaoxiang781216 commented on code in PR #7717:
URL: https://github.com/apache/nuttx/pull/7717#discussion_r1035619614


##########
arch/sim/src/sim/sim_alsa.c:
##########
@@ -393,13 +489,13 @@ static int sim_audio_configure(struct audio_lowerhalf_s 
*dev,
 
       case AUDIO_TYPE_OUTPUT:
       case AUDIO_TYPE_INPUT:
-

Review Comment:
   revert



##########
arch/sim/src/sim/sim_alsa.c:
##########
@@ -31,17 +31,35 @@
 #include <debug.h>
 
 #include <alsa/asoundlib.h>
+#include <mad.h>
 
 /****************************************************************************
  * Pre-processor Definitions
  ****************************************************************************/
 
-#define AUDMIN(a,b)     ((a) > (b) ? (b) : (a))
+#define AUDMIN(a,b) ((a) > (b) ? (b) : (a))

Review Comment:
   revert



##########
arch/sim/src/sim/sim_alsa.c:
##########
@@ -702,6 +836,158 @@ static int sim_mixer_open(struct sim_audio_s *priv)
   return 0;
 }
 
+static signed int sim_audio_mp3_scale(mad_fixed_t sample)
+{
+  sample += (1L << (MAD_F_FRACBITS - 16));
+
+  if (sample >= MAD_F_ONE)
+    sample = MAD_F_ONE - 1;
+  else if (sample < -MAD_F_ONE)
+    sample = -MAD_F_ONE;
+
+  return sample >> (MAD_F_FRACBITS + 1 - 16);
+}
+
+static void *sim_audio_mp3_init()
+{
+  struct sim_decoder_mp3_s *codec;
+
+  codec = malloc(sizeof(struct sim_decoder_mp3_s));
+  if (codec == NULL)
+    {
+      return NULL;
+    }
+
+  mad_stream_init(&codec->stream);
+  mad_frame_init(&codec->frame);
+  mad_synth_init(&codec->synth);
+
+  return codec;
+}
+
+static int sim_audio_mp3_samples(void *handle)
+{
+  struct sim_decoder_mp3_s *codec = (struct sim_decoder_mp3_s *) handle;
+
+  if (codec == NULL)
+    {
+      return -EINVAL;
+    }
+
+  return sizeof(codec->synth.pcm.samples[0]) / sizeof(mad_fixed_t);
+}
+
+static int sim_audio_mp3_decode(void *handle,
+                                unsigned char *in, unsigned int insize,
+                                unsigned char *out, unsigned int *outsize)
+{
+  struct sim_decoder_mp3_s *codec = (struct sim_decoder_mp3_s *) handle;
+  mad_fixed_t const *left_ch, *right_ch;
+  int nchannels;
+  int nsamples;
+  int i = 0;
+  int ret;
+
+  if (codec == NULL || in == NULL || out == NULL || outsize == NULL)
+    {
+      return -EINVAL;
+    }
+
+  mad_stream_buffer(&codec->stream, in, insize);
+  ret = mad_frame_decode(&codec->frame, &codec->stream);
+  if (ret < 0)
+    {
+      aerr("%s mp3 decode failed error %d\n", __func__, codec->stream.error);
+      return -ENOSYS;
+    }
+
+  mad_synth_frame(&codec->synth, &codec->frame);
+
+  nchannels = codec->synth.pcm.channels;
+  nsamples  = codec->synth.pcm.length;
+  left_ch   = codec->synth.pcm.samples[0];
+  right_ch  = codec->synth.pcm.samples[1];
+
+  while (nsamples--)
+    {
+      signed int sample;
+
+      /* output sample(s) in 16-bit signed little-endian PCM */
+
+      sample = sim_audio_mp3_scale(*left_ch++);
+      out[i]     = (sample >> 0) & 0xff;
+      out[i + 1] = (sample >> 8) & 0xff;
+
+      if (nchannels == 2)
+        {
+          sample = sim_audio_mp3_scale(*right_ch++);
+          out[i + 2] = (sample >> 0) & 0xff;
+          out[i + 3] = (sample >> 8) & 0xff;
+        }
+
+      i += sizeof(short) * nchannels;
+    }
+
+  *outsize = codec->synth.pcm.length;
+  return 0;
+}
+
+static void sim_audio_mp3_uninit(void *handle)
+{
+  struct sim_decoder_mp3_s *codec = (struct sim_decoder_mp3_s *) handle;
+
+  if (codec == NULL)
+    {
+      return;
+    }
+
+  mad_synth_finish(&(codec->synth));
+  mad_frame_finish(&(codec->frame));
+  mad_stream_finish(&(codec->stream));
+
+  free(codec);
+}
+
+static void *sim_audio_pcm_init()
+{
+  struct sim_codec_pcm_s *codec;
+
+  codec = malloc(sizeof(struct sim_codec_pcm_s));
+  if (codec == NULL)
+    {
+      return NULL;
+    }
+
+  return codec;
+}
+
+static int sim_audio_pcm_process(void *handle,
+                                  unsigned char *in, unsigned int insize,
+                                  unsigned char *out, unsigned int *outsize)
+{
+  struct sim_codec_pcm_s *codec = (struct sim_codec_pcm_s *) handle;

Review Comment:
   remove directly memcpy



##########
arch/sim/src/sim/sim_alsa.c:
##########
@@ -31,17 +31,35 @@
 #include <debug.h>
 
 #include <alsa/asoundlib.h>
+#include <mad.h>
 
 /****************************************************************************
  * Pre-processor Definitions
  ****************************************************************************/
 
-#define AUDMIN(a,b)     ((a) > (b) ? (b) : (a))
+#define AUDMIN(a,b) ((a) > (b) ? (b) : (a))
 
 /****************************************************************************
  * Private Types
  ****************************************************************************/
 
+struct sim_codec_ops_s
+{
+  uint8_t format;
+  void    *(*init)(void);
+  int      (*get_samples)(void *handle);
+  int      (*process)(void *handle, unsigned char *in, unsigned int insize,
+                      unsigned char *out, unsigned int *outsize);
+  void     (*uninit)(void *handle);
+};
+
+struct sim_codec_fmts_s
+{
+  uint8_t  format;
+  bool     decode;

Review Comment:
   why not merge to sim_codec_ops_s



##########
arch/sim/src/sim/sim_alsa.c:
##########
@@ -702,6 +836,158 @@ static int sim_mixer_open(struct sim_audio_s *priv)
   return 0;
 }
 
+static signed int sim_audio_mp3_scale(mad_fixed_t sample)
+{
+  sample += (1L << (MAD_F_FRACBITS - 16));
+
+  if (sample >= MAD_F_ONE)
+    sample = MAD_F_ONE - 1;
+  else if (sample < -MAD_F_ONE)
+    sample = -MAD_F_ONE;
+
+  return sample >> (MAD_F_FRACBITS + 1 - 16);
+}
+
+static void *sim_audio_mp3_init()
+{
+  struct sim_decoder_mp3_s *codec;
+
+  codec = malloc(sizeof(struct sim_decoder_mp3_s));
+  if (codec == NULL)
+    {
+      return NULL;
+    }
+
+  mad_stream_init(&codec->stream);
+  mad_frame_init(&codec->frame);
+  mad_synth_init(&codec->synth);
+
+  return codec;
+}
+
+static int sim_audio_mp3_samples(void *handle)
+{
+  struct sim_decoder_mp3_s *codec = (struct sim_decoder_mp3_s *) handle;

Review Comment:
   struct sim_decoder_mp3_s *codec = (struct sim_decoder_mp3_s *)handle;



##########
arch/sim/src/sim/sim_alsa.c:
##########
@@ -702,6 +836,158 @@ static int sim_mixer_open(struct sim_audio_s *priv)
   return 0;
 }
 
+static signed int sim_audio_mp3_scale(mad_fixed_t sample)
+{
+  sample += (1L << (MAD_F_FRACBITS - 16));
+
+  if (sample >= MAD_F_ONE)
+    sample = MAD_F_ONE - 1;
+  else if (sample < -MAD_F_ONE)
+    sample = -MAD_F_ONE;
+
+  return sample >> (MAD_F_FRACBITS + 1 - 16);
+}
+
+static void *sim_audio_mp3_init()
+{
+  struct sim_decoder_mp3_s *codec;
+
+  codec = malloc(sizeof(struct sim_decoder_mp3_s));
+  if (codec == NULL)
+    {
+      return NULL;
+    }
+
+  mad_stream_init(&codec->stream);
+  mad_frame_init(&codec->frame);
+  mad_synth_init(&codec->synth);
+
+  return codec;
+}
+
+static int sim_audio_mp3_samples(void *handle)
+{
+  struct sim_decoder_mp3_s *codec = (struct sim_decoder_mp3_s *) handle;
+
+  if (codec == NULL)
+    {
+      return -EINVAL;
+    }
+
+  return sizeof(codec->synth.pcm.samples[0]) / sizeof(mad_fixed_t);
+}
+
+static int sim_audio_mp3_decode(void *handle,
+                                unsigned char *in, unsigned int insize,
+                                unsigned char *out, unsigned int *outsize)
+{
+  struct sim_decoder_mp3_s *codec = (struct sim_decoder_mp3_s *) handle;

Review Comment:
   remove space before handle



##########
arch/sim/src/sim/sim_alsa.c:
##########
@@ -702,6 +836,158 @@ static int sim_mixer_open(struct sim_audio_s *priv)
   return 0;
 }
 
+static signed int sim_audio_mp3_scale(mad_fixed_t sample)
+{
+  sample += (1L << (MAD_F_FRACBITS - 16));
+
+  if (sample >= MAD_F_ONE)
+    sample = MAD_F_ONE - 1;
+  else if (sample < -MAD_F_ONE)
+    sample = -MAD_F_ONE;
+
+  return sample >> (MAD_F_FRACBITS + 1 - 16);
+}
+
+static void *sim_audio_mp3_init()
+{
+  struct sim_decoder_mp3_s *codec;
+
+  codec = malloc(sizeof(struct sim_decoder_mp3_s));
+  if (codec == NULL)
+    {
+      return NULL;
+    }
+
+  mad_stream_init(&codec->stream);
+  mad_frame_init(&codec->frame);
+  mad_synth_init(&codec->synth);
+
+  return codec;
+}
+
+static int sim_audio_mp3_samples(void *handle)
+{
+  struct sim_decoder_mp3_s *codec = (struct sim_decoder_mp3_s *) handle;
+
+  if (codec == NULL)
+    {
+      return -EINVAL;
+    }
+
+  return sizeof(codec->synth.pcm.samples[0]) / sizeof(mad_fixed_t);
+}
+
+static int sim_audio_mp3_decode(void *handle,
+                                unsigned char *in, unsigned int insize,
+                                unsigned char *out, unsigned int *outsize)
+{
+  struct sim_decoder_mp3_s *codec = (struct sim_decoder_mp3_s *) handle;
+  mad_fixed_t const *left_ch, *right_ch;
+  int nchannels;
+  int nsamples;
+  int i = 0;
+  int ret;
+
+  if (codec == NULL || in == NULL || out == NULL || outsize == NULL)
+    {
+      return -EINVAL;
+    }
+
+  mad_stream_buffer(&codec->stream, in, insize);
+  ret = mad_frame_decode(&codec->frame, &codec->stream);
+  if (ret < 0)
+    {
+      aerr("%s mp3 decode failed error %d\n", __func__, codec->stream.error);
+      return -ENOSYS;
+    }
+
+  mad_synth_frame(&codec->synth, &codec->frame);
+
+  nchannels = codec->synth.pcm.channels;
+  nsamples  = codec->synth.pcm.length;
+  left_ch   = codec->synth.pcm.samples[0];
+  right_ch  = codec->synth.pcm.samples[1];
+
+  while (nsamples--)
+    {
+      signed int sample;
+
+      /* output sample(s) in 16-bit signed little-endian PCM */
+
+      sample = sim_audio_mp3_scale(*left_ch++);
+      out[i]     = (sample >> 0) & 0xff;
+      out[i + 1] = (sample >> 8) & 0xff;
+
+      if (nchannels == 2)
+        {
+          sample = sim_audio_mp3_scale(*right_ch++);
+          out[i + 2] = (sample >> 0) & 0xff;
+          out[i + 3] = (sample >> 8) & 0xff;
+        }
+
+      i += sizeof(short) * nchannels;
+    }
+
+  *outsize = codec->synth.pcm.length;
+  return 0;
+}
+
+static void sim_audio_mp3_uninit(void *handle)
+{
+  struct sim_decoder_mp3_s *codec = (struct sim_decoder_mp3_s *) handle;
+
+  if (codec == NULL)
+    {
+      return;
+    }
+
+  mad_synth_finish(&(codec->synth));
+  mad_frame_finish(&(codec->frame));
+  mad_stream_finish(&(codec->stream));
+
+  free(codec);
+}
+
+static void *sim_audio_pcm_init()
+{
+  struct sim_codec_pcm_s *codec;
+
+  codec = malloc(sizeof(struct sim_codec_pcm_s));
+  if (codec == NULL)
+    {
+      return NULL;
+    }
+
+  return codec;
+}
+
+static int sim_audio_pcm_process(void *handle,
+                                  unsigned char *in, unsigned int insize,
+                                  unsigned char *out, unsigned int *outsize)
+{
+  struct sim_codec_pcm_s *codec = (struct sim_codec_pcm_s *) handle;
+
+  if (codec == NULL || in == NULL || out == NULL || outsize == NULL)
+    {
+      return -EINVAL;
+    }
+
+  memcpy(out, in, insize);
+  return 0;
+}
+
+static void  sim_audio_pcm_uninit(void *handle)
+{
+  struct sim_codec_pcm_s *codec = (struct sim_codec_pcm_s *) handle;

Review Comment:
   free(handle) directly



##########
arch/sim/src/sim/sim_alsa.c:
##########
@@ -702,6 +836,158 @@ static int sim_mixer_open(struct sim_audio_s *priv)
   return 0;
 }
 
+static signed int sim_audio_mp3_scale(mad_fixed_t sample)

Review Comment:
   signed int -> int



##########
arch/sim/src/sim/sim_alsa.c:
##########
@@ -702,6 +836,158 @@ static int sim_mixer_open(struct sim_audio_s *priv)
   return 0;
 }
 
+static signed int sim_audio_mp3_scale(mad_fixed_t sample)
+{
+  sample += (1L << (MAD_F_FRACBITS - 16));
+
+  if (sample >= MAD_F_ONE)
+    sample = MAD_F_ONE - 1;
+  else if (sample < -MAD_F_ONE)
+    sample = -MAD_F_ONE;
+
+  return sample >> (MAD_F_FRACBITS + 1 - 16);
+}
+
+static void *sim_audio_mp3_init()
+{
+  struct sim_decoder_mp3_s *codec;
+
+  codec = malloc(sizeof(struct sim_decoder_mp3_s));
+  if (codec == NULL)
+    {
+      return NULL;
+    }
+
+  mad_stream_init(&codec->stream);
+  mad_frame_init(&codec->frame);
+  mad_synth_init(&codec->synth);
+
+  return codec;
+}
+
+static int sim_audio_mp3_samples(void *handle)
+{
+  struct sim_decoder_mp3_s *codec = (struct sim_decoder_mp3_s *) handle;
+
+  if (codec == NULL)

Review Comment:
   remove



##########
arch/sim/src/sim/sim_alsa.c:
##########
@@ -702,6 +836,158 @@ static int sim_mixer_open(struct sim_audio_s *priv)
   return 0;
 }
 
+static signed int sim_audio_mp3_scale(mad_fixed_t sample)
+{
+  sample += (1L << (MAD_F_FRACBITS - 16));
+
+  if (sample >= MAD_F_ONE)
+    sample = MAD_F_ONE - 1;
+  else if (sample < -MAD_F_ONE)
+    sample = -MAD_F_ONE;
+
+  return sample >> (MAD_F_FRACBITS + 1 - 16);
+}
+
+static void *sim_audio_mp3_init()
+{
+  struct sim_decoder_mp3_s *codec;
+
+  codec = malloc(sizeof(struct sim_decoder_mp3_s));
+  if (codec == NULL)
+    {
+      return NULL;
+    }
+
+  mad_stream_init(&codec->stream);
+  mad_frame_init(&codec->frame);
+  mad_synth_init(&codec->synth);
+
+  return codec;
+}
+
+static int sim_audio_mp3_samples(void *handle)
+{
+  struct sim_decoder_mp3_s *codec = (struct sim_decoder_mp3_s *) handle;
+
+  if (codec == NULL)
+    {
+      return -EINVAL;
+    }
+
+  return sizeof(codec->synth.pcm.samples[0]) / sizeof(mad_fixed_t);
+}
+
+static int sim_audio_mp3_decode(void *handle,
+                                unsigned char *in, unsigned int insize,
+                                unsigned char *out, unsigned int *outsize)
+{
+  struct sim_decoder_mp3_s *codec = (struct sim_decoder_mp3_s *) handle;
+  mad_fixed_t const *left_ch, *right_ch;
+  int nchannels;
+  int nsamples;
+  int i = 0;
+  int ret;
+
+  if (codec == NULL || in == NULL || out == NULL || outsize == NULL)
+    {
+      return -EINVAL;
+    }
+
+  mad_stream_buffer(&codec->stream, in, insize);
+  ret = mad_frame_decode(&codec->frame, &codec->stream);
+  if (ret < 0)
+    {
+      aerr("%s mp3 decode failed error %d\n", __func__, codec->stream.error);
+      return -ENOSYS;
+    }
+
+  mad_synth_frame(&codec->synth, &codec->frame);
+
+  nchannels = codec->synth.pcm.channels;
+  nsamples  = codec->synth.pcm.length;
+  left_ch   = codec->synth.pcm.samples[0];
+  right_ch  = codec->synth.pcm.samples[1];
+
+  while (nsamples--)
+    {
+      signed int sample;
+
+      /* output sample(s) in 16-bit signed little-endian PCM */
+
+      sample = sim_audio_mp3_scale(*left_ch++);
+      out[i]     = (sample >> 0) & 0xff;
+      out[i + 1] = (sample >> 8) & 0xff;
+
+      if (nchannels == 2)
+        {
+          sample = sim_audio_mp3_scale(*right_ch++);
+          out[i + 2] = (sample >> 0) & 0xff;
+          out[i + 3] = (sample >> 8) & 0xff;
+        }
+
+      i += sizeof(short) * nchannels;
+    }
+
+  *outsize = codec->synth.pcm.length;
+  return 0;
+}
+
+static void sim_audio_mp3_uninit(void *handle)
+{
+  struct sim_decoder_mp3_s *codec = (struct sim_decoder_mp3_s *) handle;
+
+  if (codec == NULL)
+    {
+      return;
+    }
+
+  mad_synth_finish(&(codec->synth));
+  mad_frame_finish(&(codec->frame));
+  mad_stream_finish(&(codec->stream));
+
+  free(codec);
+}
+
+static void *sim_audio_pcm_init()
+{
+  struct sim_codec_pcm_s *codec;
+
+  codec = malloc(sizeof(struct sim_codec_pcm_s));

Review Comment:
   return malloc(sizeof(struct sim_codec_pcm_s));



##########
arch/sim/src/sim/sim_alsa.c:
##########
@@ -702,6 +836,158 @@ static int sim_mixer_open(struct sim_audio_s *priv)
   return 0;
 }
 
+static signed int sim_audio_mp3_scale(mad_fixed_t sample)
+{
+  sample += (1L << (MAD_F_FRACBITS - 16));
+
+  if (sample >= MAD_F_ONE)
+    sample = MAD_F_ONE - 1;
+  else if (sample < -MAD_F_ONE)
+    sample = -MAD_F_ONE;
+
+  return sample >> (MAD_F_FRACBITS + 1 - 16);
+}
+
+static void *sim_audio_mp3_init()
+{
+  struct sim_decoder_mp3_s *codec;
+
+  codec = malloc(sizeof(struct sim_decoder_mp3_s));
+  if (codec == NULL)
+    {
+      return NULL;
+    }
+
+  mad_stream_init(&codec->stream);
+  mad_frame_init(&codec->frame);
+  mad_synth_init(&codec->synth);
+
+  return codec;
+}
+
+static int sim_audio_mp3_samples(void *handle)
+{
+  struct sim_decoder_mp3_s *codec = (struct sim_decoder_mp3_s *) handle;
+
+  if (codec == NULL)
+    {
+      return -EINVAL;
+    }
+
+  return sizeof(codec->synth.pcm.samples[0]) / sizeof(mad_fixed_t);
+}
+
+static int sim_audio_mp3_decode(void *handle,
+                                unsigned char *in, unsigned int insize,
+                                unsigned char *out, unsigned int *outsize)
+{
+  struct sim_decoder_mp3_s *codec = (struct sim_decoder_mp3_s *) handle;
+  mad_fixed_t const *left_ch, *right_ch;
+  int nchannels;
+  int nsamples;
+  int i = 0;
+  int ret;
+
+  if (codec == NULL || in == NULL || out == NULL || outsize == NULL)
+    {
+      return -EINVAL;
+    }
+
+  mad_stream_buffer(&codec->stream, in, insize);
+  ret = mad_frame_decode(&codec->frame, &codec->stream);
+  if (ret < 0)
+    {
+      aerr("%s mp3 decode failed error %d\n", __func__, codec->stream.error);
+      return -ENOSYS;
+    }
+
+  mad_synth_frame(&codec->synth, &codec->frame);
+
+  nchannels = codec->synth.pcm.channels;
+  nsamples  = codec->synth.pcm.length;
+  left_ch   = codec->synth.pcm.samples[0];
+  right_ch  = codec->synth.pcm.samples[1];
+
+  while (nsamples--)
+    {
+      signed int sample;
+
+      /* output sample(s) in 16-bit signed little-endian PCM */
+
+      sample = sim_audio_mp3_scale(*left_ch++);
+      out[i]     = (sample >> 0) & 0xff;
+      out[i + 1] = (sample >> 8) & 0xff;
+
+      if (nchannels == 2)
+        {
+          sample = sim_audio_mp3_scale(*right_ch++);
+          out[i + 2] = (sample >> 0) & 0xff;
+          out[i + 3] = (sample >> 8) & 0xff;
+        }
+
+      i += sizeof(short) * nchannels;
+    }
+
+  *outsize = codec->synth.pcm.length;
+  return 0;
+}
+
+static void sim_audio_mp3_uninit(void *handle)
+{
+  struct sim_decoder_mp3_s *codec = (struct sim_decoder_mp3_s *) handle;

Review Comment:
   remove extra space before handle



##########
arch/sim/src/sim/sim_alsa.c:
##########
@@ -702,6 +836,158 @@ static int sim_mixer_open(struct sim_audio_s *priv)
   return 0;
 }
 
+static signed int sim_audio_mp3_scale(mad_fixed_t sample)
+{
+  sample += (1L << (MAD_F_FRACBITS - 16));

Review Comment:
   sample += 1L << (MAD_F_FRACBITS - 16);



##########
arch/sim/src/sim/sim_alsa.c:
##########
@@ -116,6 +162,31 @@ static const struct audio_ops_s g_sim_audio_ops =
   .release       = sim_audio_release,
 };
 
+static const struct sim_codec_ops_s g_mp3_dec =
+{
+  AUDIO_FMT_MP3,
+  sim_audio_mp3_init,
+  sim_audio_mp3_samples,
+  sim_audio_mp3_decode,
+  sim_audio_mp3_uninit
+};
+
+static const struct sim_codec_ops_s g_pcm_codec =
+{
+  AUDIO_FMT_PCM,
+  sim_audio_pcm_init,
+  NULL,
+  sim_audio_pcm_process,
+  sim_audio_pcm_uninit,
+};
+
+static struct sim_codec_fmts_s g_codec_s[] =

Review Comment:
   add const



##########
arch/sim/src/sim/sim_alsa.c:
##########
@@ -702,6 +836,158 @@ static int sim_mixer_open(struct sim_audio_s *priv)
   return 0;
 }
 
+static signed int sim_audio_mp3_scale(mad_fixed_t sample)
+{
+  sample += (1L << (MAD_F_FRACBITS - 16));
+
+  if (sample >= MAD_F_ONE)
+    sample = MAD_F_ONE - 1;
+  else if (sample < -MAD_F_ONE)
+    sample = -MAD_F_ONE;
+
+  return sample >> (MAD_F_FRACBITS + 1 - 16);
+}
+
+static void *sim_audio_mp3_init()
+{
+  struct sim_decoder_mp3_s *codec;
+
+  codec = malloc(sizeof(struct sim_decoder_mp3_s));
+  if (codec == NULL)
+    {
+      return NULL;
+    }
+
+  mad_stream_init(&codec->stream);
+  mad_frame_init(&codec->frame);
+  mad_synth_init(&codec->synth);
+
+  return codec;
+}
+
+static int sim_audio_mp3_samples(void *handle)
+{
+  struct sim_decoder_mp3_s *codec = (struct sim_decoder_mp3_s *) handle;
+
+  if (codec == NULL)
+    {
+      return -EINVAL;
+    }
+
+  return sizeof(codec->synth.pcm.samples[0]) / sizeof(mad_fixed_t);
+}
+
+static int sim_audio_mp3_decode(void *handle,
+                                unsigned char *in, unsigned int insize,
+                                unsigned char *out, unsigned int *outsize)
+{
+  struct sim_decoder_mp3_s *codec = (struct sim_decoder_mp3_s *) handle;
+  mad_fixed_t const *left_ch, *right_ch;
+  int nchannels;
+  int nsamples;
+  int i = 0;
+  int ret;
+
+  if (codec == NULL || in == NULL || out == NULL || outsize == NULL)
+    {
+      return -EINVAL;
+    }
+
+  mad_stream_buffer(&codec->stream, in, insize);
+  ret = mad_frame_decode(&codec->frame, &codec->stream);
+  if (ret < 0)
+    {
+      aerr("%s mp3 decode failed error %d\n", __func__, codec->stream.error);
+      return -ENOSYS;

Review Comment:
   return ret



##########
arch/sim/src/sim/sim_alsa.c:
##########
@@ -702,6 +836,158 @@ static int sim_mixer_open(struct sim_audio_s *priv)
   return 0;
 }
 
+static signed int sim_audio_mp3_scale(mad_fixed_t sample)
+{
+  sample += (1L << (MAD_F_FRACBITS - 16));
+
+  if (sample >= MAD_F_ONE)
+    sample = MAD_F_ONE - 1;
+  else if (sample < -MAD_F_ONE)
+    sample = -MAD_F_ONE;
+
+  return sample >> (MAD_F_FRACBITS + 1 - 16);
+}
+
+static void *sim_audio_mp3_init()
+{
+  struct sim_decoder_mp3_s *codec;
+
+  codec = malloc(sizeof(struct sim_decoder_mp3_s));
+  if (codec == NULL)
+    {
+      return NULL;
+    }
+
+  mad_stream_init(&codec->stream);
+  mad_frame_init(&codec->frame);
+  mad_synth_init(&codec->synth);
+
+  return codec;
+}
+
+static int sim_audio_mp3_samples(void *handle)
+{
+  struct sim_decoder_mp3_s *codec = (struct sim_decoder_mp3_s *) handle;
+
+  if (codec == NULL)
+    {
+      return -EINVAL;
+    }
+
+  return sizeof(codec->synth.pcm.samples[0]) / sizeof(mad_fixed_t);
+}
+
+static int sim_audio_mp3_decode(void *handle,
+                                unsigned char *in, unsigned int insize,
+                                unsigned char *out, unsigned int *outsize)
+{
+  struct sim_decoder_mp3_s *codec = (struct sim_decoder_mp3_s *) handle;
+  mad_fixed_t const *left_ch, *right_ch;

Review Comment:
   const mad_fixed_t *left_ch;
   const mad_fixed_t  *right_ch;



##########
arch/sim/src/sim/sim_alsa.c:
##########
@@ -702,6 +836,158 @@ static int sim_mixer_open(struct sim_audio_s *priv)
   return 0;
 }
 
+static signed int sim_audio_mp3_scale(mad_fixed_t sample)
+{
+  sample += (1L << (MAD_F_FRACBITS - 16));
+
+  if (sample >= MAD_F_ONE)
+    sample = MAD_F_ONE - 1;
+  else if (sample < -MAD_F_ONE)
+    sample = -MAD_F_ONE;
+
+  return sample >> (MAD_F_FRACBITS + 1 - 16);
+}
+
+static void *sim_audio_mp3_init()
+{
+  struct sim_decoder_mp3_s *codec;
+
+  codec = malloc(sizeof(struct sim_decoder_mp3_s));
+  if (codec == NULL)
+    {
+      return NULL;
+    }
+
+  mad_stream_init(&codec->stream);
+  mad_frame_init(&codec->frame);
+  mad_synth_init(&codec->synth);
+
+  return codec;
+}
+
+static int sim_audio_mp3_samples(void *handle)
+{
+  struct sim_decoder_mp3_s *codec = (struct sim_decoder_mp3_s *) handle;
+
+  if (codec == NULL)
+    {
+      return -EINVAL;
+    }
+
+  return sizeof(codec->synth.pcm.samples[0]) / sizeof(mad_fixed_t);
+}
+
+static int sim_audio_mp3_decode(void *handle,
+                                unsigned char *in, unsigned int insize,
+                                unsigned char *out, unsigned int *outsize)
+{
+  struct sim_decoder_mp3_s *codec = (struct sim_decoder_mp3_s *) handle;
+  mad_fixed_t const *left_ch, *right_ch;
+  int nchannels;
+  int nsamples;
+  int i = 0;
+  int ret;
+
+  if (codec == NULL || in == NULL || out == NULL || outsize == NULL)
+    {
+      return -EINVAL;
+    }
+
+  mad_stream_buffer(&codec->stream, in, insize);
+  ret = mad_frame_decode(&codec->frame, &codec->stream);
+  if (ret < 0)
+    {
+      aerr("%s mp3 decode failed error %d\n", __func__, codec->stream.error);
+      return -ENOSYS;
+    }
+
+  mad_synth_frame(&codec->synth, &codec->frame);
+
+  nchannels = codec->synth.pcm.channels;
+  nsamples  = codec->synth.pcm.length;
+  left_ch   = codec->synth.pcm.samples[0];
+  right_ch  = codec->synth.pcm.samples[1];
+
+  while (nsamples--)
+    {
+      signed int sample;
+
+      /* output sample(s) in 16-bit signed little-endian PCM */
+
+      sample = sim_audio_mp3_scale(*left_ch++);
+      out[i]     = (sample >> 0) & 0xff;
+      out[i + 1] = (sample >> 8) & 0xff;
+
+      if (nchannels == 2)
+        {
+          sample = sim_audio_mp3_scale(*right_ch++);
+          out[i + 2] = (sample >> 0) & 0xff;
+          out[i + 3] = (sample >> 8) & 0xff;
+        }
+
+      i += sizeof(short) * nchannels;
+    }
+
+  *outsize = codec->synth.pcm.length;
+  return 0;
+}
+
+static void sim_audio_mp3_uninit(void *handle)
+{
+  struct sim_decoder_mp3_s *codec = (struct sim_decoder_mp3_s *) handle;
+
+  if (codec == NULL)

Review Comment:
   remove, let's trust caller



##########
arch/sim/src/sim/sim_alsa.c:
##########
@@ -702,6 +836,158 @@ static int sim_mixer_open(struct sim_audio_s *priv)
   return 0;
 }
 
+static signed int sim_audio_mp3_scale(mad_fixed_t sample)
+{
+  sample += (1L << (MAD_F_FRACBITS - 16));
+
+  if (sample >= MAD_F_ONE)
+    sample = MAD_F_ONE - 1;
+  else if (sample < -MAD_F_ONE)
+    sample = -MAD_F_ONE;
+
+  return sample >> (MAD_F_FRACBITS + 1 - 16);
+}
+
+static void *sim_audio_mp3_init()
+{
+  struct sim_decoder_mp3_s *codec;
+
+  codec = malloc(sizeof(struct sim_decoder_mp3_s));
+  if (codec == NULL)
+    {
+      return NULL;
+    }
+
+  mad_stream_init(&codec->stream);
+  mad_frame_init(&codec->frame);
+  mad_synth_init(&codec->synth);
+
+  return codec;
+}
+
+static int sim_audio_mp3_samples(void *handle)
+{
+  struct sim_decoder_mp3_s *codec = (struct sim_decoder_mp3_s *) handle;
+
+  if (codec == NULL)
+    {
+      return -EINVAL;
+    }
+
+  return sizeof(codec->synth.pcm.samples[0]) / sizeof(mad_fixed_t);
+}
+
+static int sim_audio_mp3_decode(void *handle,
+                                unsigned char *in, unsigned int insize,
+                                unsigned char *out, unsigned int *outsize)
+{
+  struct sim_decoder_mp3_s *codec = (struct sim_decoder_mp3_s *) handle;
+  mad_fixed_t const *left_ch, *right_ch;
+  int nchannels;
+  int nsamples;
+  int i = 0;
+  int ret;
+
+  if (codec == NULL || in == NULL || out == NULL || outsize == NULL)

Review Comment:
   remove let's trust caller



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to