shithub: mp3dec

Download patch

ref: fb9083c5f8e0aea1059585f22366e14f9bf5b0a0
parent: 4a08619d8df4e9c8cc3a353b9fd4a99c63c06779
author: lieff <lieff@users.noreply.github.com>
date: Mon Feb 3 18:59:42 EST 2020

support seek in test

--- a/minimp3_test.c
+++ b/minimp3_test.c
@@ -95,7 +95,7 @@
     return 0;
 }
 
-static void decode_file(const char *input_file_name, const unsigned char *buf_ref, int ref_size, FILE *file_out, const int wave_out, const int mode)
+static void decode_file(const char *input_file_name, const unsigned char *buf_ref, int ref_size, FILE *file_out, const int wave_out, const int mode, int position)
 {
     mp3dec_t mp3d;
     int i, res = -1, data_bytes, total_samples = 0, maxdiff = 0;
@@ -102,6 +102,7 @@
     double MSE = 0.0, psnr;
 
     mp3dec_file_info_t info;
+    memset(&info, 0, sizeof(info));
     if (MODE_LOAD == mode)
     {
         res = mp3dec_load(&mp3d, input_file_name, &info, 0, 0);
@@ -109,7 +110,6 @@
     {
         frames_iterate_data d = { &mp3d, &info, 0 };
         mp3dec_init(&mp3d);
-        memset(&info, 0, sizeof(info));
         res = mp3dec_iterate(input_file_name, frames_iterate_cb, &d);
     } else if (MODE_STREAM == mode)
     {
@@ -117,7 +117,22 @@
         res = mp3dec_ex_open(&dec, input_file_name, MP3D_SEEK_TO_SAMPLE);
         info.samples = dec.samples;
         info.buffer  = malloc(dec.samples*sizeof(int16_t));
-        mp3dec_ex_read(&dec, info.buffer, dec.samples);
+        info.hz      = dec.info.hz;
+        info.channels = dec.info.channels;
+        if (position < 0)
+        {
+            position = (uint64_t)info.samples*rand()/RAND_MAX;
+            printf("info: seek to %d/%d\n", position, (int)info.samples);
+        }
+        if (position)
+        {
+            info.samples -= MINIMP3_MIN(info.samples, (size_t)position);
+            int skip_ref = MINIMP3_MIN((size_t)ref_size, position*sizeof(int16_t));
+            buf_ref  += skip_ref;
+            ref_size -= skip_ref;
+            mp3dec_ex_seek(&dec, position);
+        }
+        mp3dec_ex_read(&dec, info.buffer, info.samples);
     } else
     {
         printf("error: unknown mode");
@@ -206,7 +221,7 @@
 int main(int argc, char *argv[])
 #endif
 {
-    int wave_out = 0, mode = 0, i, ref_size;
+    int wave_out = 0, mode = 0, position = 0, i, ref_size;
     for(i = 1; i < argc; i++)
     {
         if (argv[i][0] != '-')
@@ -214,6 +229,7 @@
         switch (argv[i][1])
         {
         case 'm': i++; if (i < argc) mode = atoi(argv[i]); break;
+        case 's': i++; if (i < argc) position = atoi(argv[i]); break;
         default:
             printf("error: unrecognized option\n");
             return 1;
@@ -245,7 +261,7 @@
         printf("error: no file names given\n");
         return 1;
     }
-    decode_file(input_file_name, buf_ref, ref_size, file_out, wave_out, mode);
+    decode_file(input_file_name, buf_ref, ref_size, file_out, wave_out, mode, position);
 #ifdef __AFL_HAVE_MANUAL_CONTROL
     }
 #endif
--- a/scripts/build.sh
+++ b/scripts/build.sh
@@ -11,11 +11,10 @@
 
 echo testing mp4 mode...
 gcc $CFLAGS -DMP4_MODE -o minimp3 minimp3_test.c -lm
-scripts/test_mode.sh 1
+scripts/test_mode.sh 1 0
 
 echo testing stream mode...
-gcc $CFLAGS -DMP4_MODE -o minimp3 minimp3_test.c -lm
-scripts/test_mode.sh 2
+scripts/test_mode.sh 2 0
 
 echo testing coverage x86 w sse...
 gcc -coverage -O0 -m32 -std=c89 -msse2 -DMINIMP3_TEST -DMINIMP3_NO_WAV -o minimp3 minimp3_test.c -lm
--- a/scripts/test_mode.sh
+++ b/scripts/test_mode.sh
@@ -6,6 +6,7 @@
 
 APP=./minimp3
 MODE=$1
+POSITION=$2
 
 set +e
 for i in vectors/l3-compl.bit vectors/l3-he_32khz.bit vectors/l3-he_44khz.bit vectors/l3-he_48khz.bit \
@@ -12,9 +13,9 @@
 vectors/l3-hecommon.bit vectors/l3-he_mode.bit vectors/l3-si.bit vectors/l3-si_block.bit vectors/l3-si_huff.bit \
 vectors/l3-sin1k0db.bit vectors/l3-test45.bit vectors/l3-test46.bit vectors/M2L3_bitrate_16_all.bit \
 vectors/M2L3_bitrate_22_all.bit vectors/M2L3_bitrate_24_all.bit vectors/M2L3_compl24.bit vectors/M2L3_noise.bit; do
-$APP -m $MODE $i ${i%.*}.pcm
+$APP -m $MODE -s $POSITION $i ${i%.*}.pcm
 retval=$?
-echo $i exited with code=$retval
+echo -m $MODE -s $POSITION $i exited with code=$retval
 if [ ! $retval -eq 0 ]; then
   exit 1
 fi