shithub: mp3dec

Download patch

ref: 4ca9d6e7768e0ab1cd69bf10761cd568f8d26641
parent: 4e79fda638f72248d827d4a985b1cf43b56f5f50
author: lieff <lieff@users.noreply.github.com>
date: Wed Aug 22 14:15:16 EDT 2018

support MINIMP3_FLOAT_OUTPUT in minimp3_ex.h and test

--- a/minimp3_ex.h
+++ b/minimp3_ex.h
@@ -14,7 +14,7 @@
 
 typedef struct
 {
-    int16_t *buffer;
+    mp3d_sample_t *buffer;
     size_t samples; /* channels included, byte size = samples*sizeof(int16_t) */
     int channels, hz, layer, avg_bitrate_kbps;
 } mp3dec_file_info_t;
@@ -78,7 +78,7 @@
 void mp3dec_load_buf(mp3dec_t *dec, const uint8_t *buf, size_t buf_size, mp3dec_file_info_t *info, MP3D_PROGRESS_CB progress_cb, void *user_data)
 {
     size_t orig_buf_size = buf_size;
-    short pcm[MINIMP3_MAX_SAMPLES_PER_FRAME];
+    mp3d_sample_t pcm[MINIMP3_MAX_SAMPLES_PER_FRAME];
     mp3dec_frame_info_t frame_info;
     memset(info, 0, sizeof(*info));
     memset(&frame_info, 0, sizeof(frame_info));
@@ -102,12 +102,12 @@
     if (!samples)
         return;
     samples *= frame_info.channels;
-    size_t allocated = (buf_size/frame_info.frame_bytes)*samples*2 + MINIMP3_MAX_SAMPLES_PER_FRAME*2;
+    size_t allocated = (buf_size/frame_info.frame_bytes)*samples*sizeof(mp3d_sample_t) + MINIMP3_MAX_SAMPLES_PER_FRAME*sizeof(mp3d_sample_t);
     info->buffer = malloc(allocated);
     if (!info->buffer)
         return;
     info->samples = samples;
-    memcpy(info->buffer, pcm, info->samples*2);
+    memcpy(info->buffer, pcm, info->samples*sizeof(mp3d_sample_t));
     /* save info */
     info->channels = frame_info.channels;
     info->hz       = frame_info.hz;
@@ -118,7 +118,7 @@
     int frame_bytes;
     do
     {
-        if ((allocated - info->samples*2) < MINIMP3_MAX_SAMPLES_PER_FRAME*2)
+        if ((allocated - info->samples*sizeof(mp3d_sample_t)) < MINIMP3_MAX_SAMPLES_PER_FRAME*sizeof(mp3d_sample_t))
         {
             allocated *= 2;
             info->buffer = realloc(info->buffer, allocated);
@@ -145,8 +145,8 @@
         }
     } while (frame_bytes);
     /* reallocate to normal buffer size */
-    if (allocated != info->samples*2)
-        info->buffer = realloc(info->buffer, info->samples*2);
+    if (allocated != info->samples*sizeof(mp3d_sample_t))
+        info->buffer = realloc(info->buffer, info->samples*sizeof(mp3d_sample_t));
     info->avg_bitrate_kbps = avg_bitrate_kbps/frames;
 }
 
--- a/minimp3_test.c
+++ b/minimp3_test.c
@@ -75,7 +75,7 @@
     d->info->hz       = info->hz;
     d->info->layer    = info->layer;
     /*printf("%d %d %d\n", frame_size, (int)offset, info->channels);*/
-    if ((d->allocated - d->info->samples*2) < MINIMP3_MAX_SAMPLES_PER_FRAME*2)
+    if ((d->allocated - d->info->samples*sizeof(mp3d_sample_t)) < MINIMP3_MAX_SAMPLES_PER_FRAME*sizeof(mp3d_sample_t))
     {
         if (!d->allocated)
             d->allocated = 1024*1024;
@@ -111,6 +111,13 @@
         printf("error: file not found or read error");
         exit(1);
     }
+#ifdef MINIMP3_FLOAT_OUTPUT
+    int16_t *buffer = malloc(info.samples*sizeof(short));
+    mp3dec_f32_to_s16(info.buffer, buffer, info.samples);
+    free(info.buffer);
+#else
+    int16_t *buffer = info.buffer;
+#endif
 #ifndef MINIMP3_NO_WAV
     if (wave_out && file_out)
         fwrite(wav_header(0, 0, 0, 0), 1, 44, file_out);
@@ -123,7 +130,7 @@
             int max_samples = MINIMP3_MIN((size_t)ref_size/2, info.samples);
             for (i = 0; i < max_samples; i++)
             {
-                int MSEtemp = abs((int)info.buffer[i] - (int)(short)read16le(&buf_ref[i*sizeof(short)]));
+                int MSEtemp = abs((int)buffer[i] - (int)(short)read16le(&buf_ref[i*sizeof(short)]));
                 if (MSEtemp > maxdiff)
                     maxdiff = MSEtemp;
                 MSE += (float)MSEtemp*(float)MSEtemp;
@@ -130,8 +137,8 @@
             }
         }
         if (file_out)
-            fwrite(info.buffer, info.samples, sizeof(int16_t), file_out);
-        free(info.buffer);
+            fwrite(buffer, info.samples, sizeof(int16_t), file_out);
+        free(buffer);
     }
 
 #ifndef LIBFUZZER