shithub: mp3dec

Download patch

ref: a5b658d1d7b26d5856f9f1a642550ae3ff75a084
parent: c2a881c02b12a5d07a68fe4694741aceb1385e74
author: lieff <lieff@users.noreply.github.com>
date: Thu Feb 13 12:43:48 EST 2020

test: improve coverage
mp3dec_ex: fix mp3dec_ex_read read-by-blocks

--- a/minimp3_ex.h
+++ b/minimp3_ex.h
@@ -65,11 +65,9 @@
     uint64_t offset, samples, detected_samples, cur_sample, start_offset, end_offset;
     mp3dec_frame_info_t info;
     mp3d_sample_t buffer[MINIMP3_MAX_SAMPLES_PER_FRAME];
-#ifndef MINIMP3_NO_STDIO
-    int is_file;
-#endif
+    size_t input_consumed, input_filled;
+    int is_file, seek_method, vbr_tag_found;
     int free_format_bytes;
-    int seek_method, vbr_tag_found;
     int buffer_samples, buffer_consumed, to_skip, start_delay;
 } mp3dec_ex_t;
 
@@ -688,6 +686,8 @@
         dec->io->seek(dec->offset, dec->io->seek_data);
     dec->buffer_samples  = 0;
     dec->buffer_consumed = 0;
+    dec->input_consumed  = 0;
+    dec->input_filled    = 0;
     mp3dec_init(&dec->mp3d);
     return 0;
 }
@@ -696,7 +696,6 @@
 {
     uint64_t end_offset = dec->end_offset ? dec->end_offset : dec->file.size;
     size_t samples_requested = samples;
-    size_t filled = 0, consumed = 0;
     int eof = 0;
     mp3dec_frame_info_t frame_info;
     memset(&frame_info, 0, sizeof(frame_info));
@@ -723,23 +722,23 @@
         const uint8_t *dec_buf;
         if (dec->io)
         {
-            if (!eof && (filled - consumed) < MINIMP3_BUF_SIZE)
+            if (!eof && (dec->input_filled - dec->input_consumed) < MINIMP3_BUF_SIZE)
             {   /* keep minimum 10 consecutive mp3 frames (~16KB) worst case */
-                memmove((uint8_t*)dec->file.buffer, (uint8_t*)dec->file.buffer + consumed, filled - consumed);
-                filled -= consumed;
-                consumed = 0;
-                size_t readed = dec->io->read((uint8_t*)dec->file.buffer + filled, dec->file.size - filled, dec->io->read_data);
-                if (readed != (dec->file.size - filled))
+                memmove((uint8_t*)dec->file.buffer, (uint8_t*)dec->file.buffer + dec->input_consumed, dec->input_filled - dec->input_consumed);
+                dec->input_filled -= dec->input_consumed;
+                dec->input_consumed = 0;
+                size_t readed = dec->io->read((uint8_t*)dec->file.buffer + dec->input_filled, dec->file.size - dec->input_filled, dec->io->read_data);
+                if (readed != (dec->file.size - dec->input_filled))
                     eof = 1;
-                filled += readed;
+                dec->input_filled += readed;
                 if (eof)
-                    mp3dec_skip_id3v1((uint8_t*)dec->file.buffer, &filled);
+                    mp3dec_skip_id3v1((uint8_t*)dec->file.buffer, &dec->input_filled);
             }
-            dec_buf = dec->file.buffer + consumed;
-            if (!(filled - consumed))
+            dec_buf = dec->file.buffer + dec->input_consumed;
+            if (!(dec->input_filled - dec->input_consumed))
                 break;
-            dec->buffer_samples = mp3dec_decode_frame(&dec->mp3d, dec_buf, filled - consumed, dec->buffer, &frame_info);
-            consumed += frame_info.frame_bytes;
+            dec->buffer_samples = mp3dec_decode_frame(&dec->mp3d, dec_buf, dec->input_filled - dec->input_consumed, dec->buffer, &frame_info);
+            dec->input_consumed += frame_info.frame_bytes;
         } else
         {
             dec_buf = dec->file.buffer + dec->offset;
--- a/minimp3_test.c
+++ b/minimp3_test.c
@@ -113,7 +113,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, int position)
+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, int portion)
 {
     mp3dec_t mp3d;
     int i, res = -1, data_bytes, total_samples = 0, maxdiff = 0;
@@ -220,12 +220,26 @@
             ref_size -= skip_ref;
             mp3dec_ex_seek(&dec, position);
         }
-        readed = mp3dec_ex_read(&dec, info.buffer, info.samples);
-        if (readed != info.samples)
+        if (portion < 0)
         {
-            printf("error: mp3dec_ex_read() readed less than expected\n");
-            exit(1);
+            portion = (uint64_t)(info.samples + 150)*rand()/RAND_MAX;
+            printf("info: read by %d samples\n", portion);
         }
+        if (0 == portion)
+            portion = info.samples;
+        int samples = info.samples, samples_readed = 0;
+        while (samples)
+        {
+            int to_read = MINIMP3_MIN(samples, portion);
+            readed = mp3dec_ex_read(&dec, info.buffer + samples_readed, to_read);
+            if (readed != (size_t)to_read)
+            {
+                printf("error: mp3dec_ex_read() readed less than expected\n");
+                exit(1);
+            }
+            samples -= to_read;
+            samples_readed += to_read;
+        }
         readed = mp3dec_ex_read(&dec, info.buffer, 1);
         if (readed)
         {
@@ -332,7 +346,7 @@
 int main(int argc, char *argv[])
 #endif
 {
-    int wave_out = 0, mode = 0, position = 0, i, ref_size;
+    int wave_out = 0, mode = 0, position = 0, portion = 0, i, ref_size;
     for(i = 1; i < argc; i++)
     {
         if (argv[i][0] != '-')
@@ -341,6 +355,7 @@
         {
         case 'm': i++; if (i < argc) mode = atoi(argv[i]); break;
         case 's': i++; if (i < argc) position = atoi(argv[i]); break;
+        case 'p': i++; if (i < argc) portion  = atoi(argv[i]); break;
         default:
             printf("error: unrecognized option\n");
             return 1;
@@ -372,7 +387,7 @@
         printf("error: no file names given\n");
         return 1;
     }
-    decode_file(input_file_name, buf_ref, ref_size, file_out, wave_out, mode, position);
+    decode_file(input_file_name, buf_ref, ref_size, file_out, wave_out, mode, position, portion);
 #ifdef __AFL_HAVE_MANUAL_CONTROL
     }
 #endif
--- a/scripts/build.sh
+++ b/scripts/build.sh
@@ -11,22 +11,22 @@
 
 echo testing mp4 mode...
 gcc $CFLAGS -DMP4_MODE -o minimp3 minimp3_test.c -lm
-scripts/test_mode.sh 3 0
+scripts/test_mode.sh 3 0 0
 
 echo testing stream mode...
-scripts/test_mode.sh 6 -1
+scripts/test_mode.sh 6 -1 -1
 
 echo testing coverage x86 w sse...
 gcc -coverage -O0 -m32 -std=c89 -msse2 -DMINIMP3_TEST -DMINIMP3_NO_WAV -o minimp3 minimp3_test.c -lm
 scripts/test.sh
-scripts/test_mode.sh 1 0
-scripts/test_mode.sh 2 0
-scripts/test_mode.sh 3 0
-scripts/test_mode.sh 4 0
-scripts/test_mode.sh 5 0
-scripts/test_mode.sh 6 -1
-scripts/test_mode.sh 7 -1
-scripts/test_mode.sh 8 -1
+scripts/test_mode.sh 1 0 0
+scripts/test_mode.sh 2 0 0
+scripts/test_mode.sh 3 0 0
+scripts/test_mode.sh 4 0 0
+scripts/test_mode.sh 5 0 0
+scripts/test_mode.sh 6 -1 -1
+scripts/test_mode.sh 7 -1 -1
+scripts/test_mode.sh 8 -1 -1
 set +e
 ./minimp3
 ./minimp3 do_not_exist
@@ -44,14 +44,14 @@
 echo testing x64 with float output...
 gcc $CFLAGS -DMINIMP3_FLOAT_OUTPUT -o minimp3 minimp3_test.c -lm
 scripts/test.sh
-scripts/test_mode.sh 1 0
-scripts/test_mode.sh 2 0
-scripts/test_mode.sh 3 0
-scripts/test_mode.sh 4 0
-scripts/test_mode.sh 5 0
-scripts/test_mode.sh 6 -1
-scripts/test_mode.sh 7 -1
-scripts/test_mode.sh 8 -1
+scripts/test_mode.sh 1 0 0
+scripts/test_mode.sh 2 0 0
+scripts/test_mode.sh 3 0 0
+scripts/test_mode.sh 4 0 0
+scripts/test_mode.sh 5 0 0
+scripts/test_mode.sh 6 -1 -1
+scripts/test_mode.sh 7 -1 -1
+scripts/test_mode.sh 8 -1 -1
 
 echo testing arm w/o neon...
 arm-none-eabi-gcc $CFLAGS -mthumb -mcpu=arm9e -o minimp3_arm minimp3_test.c --specs=rdimon.specs -lm
--- a/scripts/test_mode.sh
+++ b/scripts/test_mode.sh
@@ -7,6 +7,7 @@
 APP=./minimp3
 MODE=$1
 POSITION=$2
+PORTION=$3
 VECTORS="vectors/l3-compl.bit vectors/l3-he_32khz.bit vectors/l3-he_44khz.bit vectors/l3-he_48khz.bit \
 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 \
@@ -18,9 +19,9 @@
 
 set +e
 for i in $VECTORS; do
-$APP -m $MODE -s $POSITION $i ${i%.*}.pcm
+$APP -m $MODE -s $POSITION -p $PORTION $i ${i%.*}.pcm
 retval=$?
-echo -m $MODE -s $POSITION $i exited with code=$retval
+echo -m $MODE -s $POSITION -p $PORTION $i exited with code=$retval
 if [ ! $retval -eq 0 ]; then
   exit 1
 fi