diff options
-rw-r--r-- | include/opus.h | 11 | ||||
-rw-r--r-- | src/opus_decoder.c | 14 | ||||
-rw-r--r-- | tests/test_opus_api.c | 4 |
3 files changed, 24 insertions, 5 deletions
diff --git a/include/opus.h b/include/opus.h index 623662d5..847a07c1 100644 --- a/include/opus.h +++ b/include/opus.h @@ -571,6 +571,17 @@ OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_nb_channels(const unsign OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_nb_frames(const unsigned char packet[], opus_int32 len) OPUS_ARG_NONNULL(1); /** Gets the number of samples of an Opus packet. + * @param [in] packet <tt>char*</tt>: Opus packet + * @param [in] len <tt>opus_int32</tt>: Length of packet + * @param [in] Fs <tt>opus_int32</tt>: Sampling rate in Hz. + * This must be a multiple of 400, or + * inaccurate results will be returned. + * @returns Number of samples + * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type + */ +OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_nb_samples(const unsigned char packet[], opus_int32 len, opus_int32 Fs) OPUS_ARG_NONNULL(1); + +/** Gets the number of samples of an Opus packet. * @param [in] dec <tt>OpusDecoder*</tt>: Decoder state * @param [in] packet <tt>char*</tt>: Opus packet * @param [in] len <tt>opus_int32</tt>: Length of packet diff --git a/src/opus_decoder.c b/src/opus_decoder.c index 67e8cdb5..cf838df7 100644 --- a/src/opus_decoder.c +++ b/src/opus_decoder.c @@ -1050,8 +1050,8 @@ int opus_packet_get_nb_frames(const unsigned char packet[], opus_int32 len) return packet[1]&0x3F; } -int opus_decoder_get_nb_samples(const OpusDecoder *dec, - const unsigned char packet[], opus_int32 len) +int opus_packet_get_nb_samples(const unsigned char packet[], opus_int32 len, + opus_int32 Fs) { int samples; int count = opus_packet_get_nb_frames(packet, len); @@ -1059,10 +1059,16 @@ int opus_decoder_get_nb_samples(const OpusDecoder *dec, if (count<0) return count; - samples = count*opus_packet_get_samples_per_frame(packet, dec->Fs); + samples = count*opus_packet_get_samples_per_frame(packet, Fs); /* Can't have more than 120 ms */ - if (samples*25 > dec->Fs*3) + if (samples*25 > Fs*3) return OPUS_INVALID_PACKET; else return samples; } + +int opus_decoder_get_nb_samples(const OpusDecoder *dec, + const unsigned char packet[], opus_int32 len) +{ + return opus_packet_get_nb_samples(packet, len, dec->Fs); +} diff --git a/tests/test_opus_api.c b/tests/test_opus_api.c index fa62e649..58c6d2b4 100644 --- a/tests/test_opus_api.c +++ b/tests/test_opus_api.c @@ -226,12 +226,14 @@ opus_int32 test_dec_api(void) VG_UNDEF(packet,sizeof(packet)); packet[0]=0; + if(opus_packet_get_nb_samples(packet,1,48000)!=480)test_failed(); if(opus_decoder_get_nb_samples(dec,packet,1)!=480)test_failed(); cfgs++; packet[0]=(63<<2)|3; packet[1]=63; + if(opus_packet_get_nb_samples(packet,2,48000)!=OPUS_INVALID_PACKET)test_failed(); if(opus_decoder_get_nb_samples(dec,packet,2)!=OPUS_INVALID_PACKET)test_failed(); - fprintf(stdout," opus_decoder_get_nb_samples() ................ OK.\n"); + fprintf(stdout," opus_{packet,decoder}_get_nb_samples() ................ OK.\n"); cfgs++; if(OPUS_BAD_ARG!=opus_packet_get_nb_frames(packet,0))test_failed(); |