Implements opus_packet_get_nb_samples()

This commit is contained in:
Jean-Marc Valin 2012-12-04 15:45:31 -05:00
parent 512d849c24
commit d0fd9d4baa
3 changed files with 24 additions and 5 deletions

View file

@ -570,6 +570,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); 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. /** Gets the number of samples of an Opus packet.
* @param [in] dec <tt>OpusDecoder*</tt>: Decoder state * @param [in] dec <tt>OpusDecoder*</tt>: Decoder state
* @param [in] packet <tt>char*</tt>: Opus packet * @param [in] packet <tt>char*</tt>: Opus packet

View file

@ -1050,8 +1050,8 @@ int opus_packet_get_nb_frames(const unsigned char packet[], opus_int32 len)
return packet[1]&0x3F; return packet[1]&0x3F;
} }
int opus_decoder_get_nb_samples(const OpusDecoder *dec, int opus_packet_get_nb_samples(const unsigned char packet[], opus_int32 len,
const unsigned char packet[], opus_int32 len) opus_int32 Fs)
{ {
int samples; int samples;
int count = opus_packet_get_nb_frames(packet, len); 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) if (count<0)
return count; 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 */ /* Can't have more than 120 ms */
if (samples*25 > dec->Fs*3) if (samples*25 > Fs*3)
return OPUS_INVALID_PACKET; return OPUS_INVALID_PACKET;
else else
return samples; 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);
}

View file

@ -226,12 +226,14 @@ opus_int32 test_dec_api(void)
VG_UNDEF(packet,sizeof(packet)); VG_UNDEF(packet,sizeof(packet));
packet[0]=0; 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(); if(opus_decoder_get_nb_samples(dec,packet,1)!=480)test_failed();
cfgs++; cfgs++;
packet[0]=(63<<2)|3; packet[0]=(63<<2)|3;
packet[1]=63; 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(); 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++; cfgs++;
if(OPUS_BAD_ARG!=opus_packet_get_nb_frames(packet,0))test_failed(); if(OPUS_BAD_ARG!=opus_packet_get_nb_frames(packet,0))test_failed();