ref: d0fd9d4baa84ba11b1e9057987008977b3709607
parent: 512d849c24b3ae708fb15c86a047c56d2591ab46
author: Jean-Marc Valin <[email protected]>
date: Tue Dec 4 10:45:31 EST 2012
Implements opus_packet_get_nb_samples()
--- a/include/opus.h
+++ b/include/opus.h
@@ -571,6 +571,17 @@
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
--- a/src/opus_decoder.c
+++ b/src/opus_decoder.c
@@ -1050,8 +1050,8 @@
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 @@
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);
}
--- a/tests/test_opus_api.c
+++ b/tests/test_opus_api.c
@@ -226,12 +226,14 @@
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();