From e85a6f5cbee9edd7ceb10e05db6652872cf8d8d2 Mon Sep 17 00:00:00 2001 From: Jean-Marc Valin Date: Mon, 19 Nov 2012 23:21:43 -0500 Subject: [PATCH] Makes opus_demo rubust to the encoder using variable frame duration Also, the encode+decode mode now produces an output of the same size as the original. --- src/opus_demo.c | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/opus_demo.c b/src/opus_demo.c index 09b12a33..ae6ccf38 100644 --- a/src/opus_demo.c +++ b/src/opus_demo.c @@ -221,6 +221,8 @@ int main(int argc, char *argv[]) short *in, *out; int application=OPUS_APPLICATION_AUDIO; double bits=0.0, bits_max=0.0, bits_act=0.0, bits2=0.0, nrg; + double tot_samples=0; + opus_uint64 tot_in, tot_out; int bandwidth=-1; const char *bandwidth_string; int lost = 0, lost_prev = 1; @@ -239,6 +241,8 @@ int main(int argc, char *argv[]) int curr_mode=0; int curr_mode_count=0; int mode_switch_time = 48000; + int nb_encoded; + int remaining=0; if (argc < 5 ) { @@ -246,6 +250,7 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } + tot_in=tot_out=0; fprintf(stderr, "%s\n", opus_get_version_string()); args = 1; @@ -617,22 +622,28 @@ int main(int argc, char *argv[]) opus_encoder_ctl(enc, OPUS_SET_FORCE_CHANNELS(mode_list[curr_mode][3])); frame_size = mode_list[curr_mode][2]; } - err = fread(fbytes, sizeof(short)*channels, frame_size, fin); + err = fread(fbytes, sizeof(short)*channels, frame_size-remaining, fin); curr_read = err; + tot_in += curr_read; for(i=0;i0 && rand()%100 < packet_loss_perc); @@ -703,6 +715,11 @@ int main(int argc, char *argv[]) } if (output_samples>0) { + if (!decode_only && tot_out + output_samples > tot_in) + { + stop=1; + output_samples = tot_in-tot_out; + } if (output_samples>skip) { int i; for(i=0;i<(output_samples-skip)*channels;i++) @@ -716,6 +733,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "Error writing.\n"); return EXIT_FAILURE; } + tot_out += output_samples-skip; } if (output_samples