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


##########
arch/sim/src/sim/sim_alsa.c:
##########
@@ -116,6 +177,32 @@ 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,
+  AUDCODEC_DEC,
+  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,
+  AUDCODEC_DEC | AUDCODEC_ENC,
+  sim_audio_pcm_init,
+  NULL,
+  sim_audio_pcm_process,
+  sim_audio_pcm_uninit,
+};
+
+static const struct sim_codec_ops_s g_codec_s[] =

Review Comment:
   can we
   ```suggestion
   static const struct sim_codec_ops_s *g_codec_s[] =
   ```
   and use reference to `g_pcm_codec` and `g_mp3_dec` instead of copy?



##########
arch/sim/src/sim/sim_alsa.c:
##########
@@ -702,6 +855,147 @@ static int sim_mixer_open(struct sim_audio_s *priv)
   return 0;
 }
 
+static 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));

Review Comment:
   should we use kmalloc?



##########
arch/sim/src/sim/sim_alsa.c:
##########
@@ -702,6 +855,147 @@ static int sim_mixer_open(struct sim_audio_s *priv)
   return 0;
 }
 
+static 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);
+
+  codec->out = malloc(sizeof(codec->synth.pcm.samples));

Review Comment:
   ditto



##########
arch/sim/src/sim/sim_alsa.c:
##########
@@ -702,6 +855,147 @@ static int sim_mixer_open(struct sim_audio_s *priv)
   return 0;
 }
 
+static 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);
+
+  codec->out = malloc(sizeof(codec->synth.pcm.samples));
+  if (codec->out == NULL)
+    {
+      goto out;
+    }
+
+  return codec;
+
+out:
+  mad_synth_finish(&(codec->synth));
+  mad_frame_finish(&(codec->frame));
+  mad_stream_finish(&(codec->stream));
+  free(codec);
+
+  return NULL;
+}
+
+static int sim_audio_mp3_samples(void *handle)
+{
+  struct sim_decoder_mp3_s *codec = (struct sim_decoder_mp3_s *)handle;
+
+  return sizeof(codec->synth.pcm.samples[0]) / sizeof(mad_fixed_t);
+}
+
+static int sim_audio_mp3_decode(void *handle,
+                                uint8_t *in, uint32_t insize,
+                                uint8_t **out, uint32_t *outsize)
+{
+  struct sim_decoder_mp3_s *codec = (struct sim_decoder_mp3_s *)handle;
+  const mad_fixed_t *right_ch;
+  const mad_fixed_t *left_ch;
+  int nchannels;
+  int nsamples;
+  uint8_t *ptr;
+  int i = 0;
+  int ret;
+
+  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 ret;
+    }
+
+  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];
+
+  ptr = codec->out;
+  while (nsamples--)
+    {
+      int sample;
+
+      /* output sample(s) in 16-bit signed little-endian PCM */
+
+      sample = sim_audio_mp3_scale(*left_ch++);
+      ptr[i]     = (sample >> 0) & 0xff;
+      ptr[i + 1] = (sample >> 8) & 0xff;
+
+      if (nchannels == 2)
+        {
+          sample = sim_audio_mp3_scale(*right_ch++);
+          ptr[i + 2] = (sample >> 0) & 0xff;

Review Comment:
   ```suggestion
             sample     = sim_audio_mp3_scale(*right_ch++);
             ptr[i + 2] = (sample >> 0) & 0xff;
   ```



##########
arch/sim/src/sim/sim_alsa.c:
##########
@@ -702,6 +855,147 @@ static int sim_mixer_open(struct sim_audio_s *priv)
   return 0;
 }
 
+static 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);
+
+  codec->out = malloc(sizeof(codec->synth.pcm.samples));
+  if (codec->out == NULL)
+    {
+      goto out;
+    }
+
+  return codec;
+
+out:
+  mad_synth_finish(&(codec->synth));
+  mad_frame_finish(&(codec->frame));
+  mad_stream_finish(&(codec->stream));
+  free(codec);
+
+  return NULL;
+}
+
+static int sim_audio_mp3_samples(void *handle)
+{
+  struct sim_decoder_mp3_s *codec = (struct sim_decoder_mp3_s *)handle;
+
+  return sizeof(codec->synth.pcm.samples[0]) / sizeof(mad_fixed_t);
+}
+
+static int sim_audio_mp3_decode(void *handle,
+                                uint8_t *in, uint32_t insize,
+                                uint8_t **out, uint32_t *outsize)
+{
+  struct sim_decoder_mp3_s *codec = (struct sim_decoder_mp3_s *)handle;
+  const mad_fixed_t *right_ch;
+  const mad_fixed_t *left_ch;
+  int nchannels;
+  int nsamples;
+  uint8_t *ptr;
+  int i = 0;
+  int ret;
+
+  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 ret;
+    }
+
+  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];
+
+  ptr = codec->out;
+  while (nsamples--)
+    {
+      int sample;
+
+      /* output sample(s) in 16-bit signed little-endian PCM */
+
+      sample = sim_audio_mp3_scale(*left_ch++);
+      ptr[i]     = (sample >> 0) & 0xff;

Review Comment:
   ```suggestion
         sample     = sim_audio_mp3_scale(*left_ch++);
         ptr[i]     = (sample >> 0) & 0xff;
   ```



##########
arch/sim/src/sim/sim_alsa.c:
##########
@@ -702,6 +855,147 @@ static int sim_mixer_open(struct sim_audio_s *priv)
   return 0;
 }
 
+static 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()

Review Comment:
   ```suggestion
   static void *sim_audio_mp3_init(void)
   ```



-- 
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