Skip to content

Commit

Permalink
Opus Audio packet level statistics
Browse files Browse the repository at this point in the history
Move the logic for calculating opus audio statistics from the
standalone OpusStatistics class, into OpusAudioData. Based
on work from Gagravarr#14 from @andrm
  • Loading branch information
Gagravarr committed Aug 17, 2015
1 parent a8a3a07 commit f3b24c2
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 43 deletions.
55 changes: 55 additions & 0 deletions core/src/main/java/org/gagravarr/opus/OpusAudioData.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
* Raw, compressed audio data
*/
public class OpusAudioData extends OggStreamAudioData implements OpusPacket {
private int numFrames = -1;
private int numSamples = -1;

public OpusAudioData(OggPacket pkt) {
super(pkt);
}
Expand All @@ -30,4 +33,56 @@ public OpusAudioData(byte[] data) {
protected boolean isEndOfStream() {
return getOggPacket().isEndOfStream();
}

public int getNumberOfFrames() {
if (numFrames == -1) {
calculateStructure();
}
return numFrames;
}
public int getNumberOfSamples() {
if (numSamples == -1) {
calculateStructure();
}
return numSamples;
}

private void calculateStructure() {
byte[] d = getData();
numFrames = packet_get_nb_frames(d);
numSamples = numFrames * packet_get_samples_per_frame(d, 48000);
}

private static int packet_get_samples_per_frame(byte[] data, int fs) {
int audiosize;
if ((data[0]&0x80) != 0) {
audiosize = ((data[0]>>3)&0x3);
audiosize = (fs<<audiosize)/400;
} else if ((data[0]&0x60) == 0x60) {
audiosize = ((data[0]&0x08) != 0) ? fs/50 : fs/100;
} else {
audiosize = ((data[0]>>3)&0x3);
if (audiosize == 3)
audiosize = fs*60/1000;
else
audiosize = (fs<<audiosize)/100;
}
return audiosize;
}

private static int packet_get_nb_frames(byte[] packet) {
int count = 0;
if (packet.length < 1) {
return -1;
}
count = packet[0]&0x3;
if (count==0)
return 1;
else if (count!=3)
return 2;
else if (packet.length<2)
return -4;
else
return packet[1]&0x3F;
}
}
49 changes: 6 additions & 43 deletions core/src/main/java/org/gagravarr/opus/OpusStatistics.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,59 +141,22 @@ else if (getAudioPacketsCount() == 0) {
return;
}

int spp = packet_get_nb_frames(d);
spp *= packet_get_samples_per_frame(d, 48000);
if(spp<120 || spp>5760 || (spp%120)!=0) {
int samples = audioData.getNumberOfSamples();
if (samples<120 || samples>5760 || (samples%120) != 0) {
System.err.println("WARNING: Invalid packet TOC in stream with sid "+sid);
return;
}
total_samples += spp;
page_samples += spp;
total_samples += samples;
page_samples += samples;
total_packets++;
//last_packet_duration = spp;
if (max_packet_duration<spp) max_packet_duration=spp;
if (min_packet_duration>spp) min_packet_duration = spp;
if (max_packet_duration<samples) max_packet_duration = samples;
if (min_packet_duration>samples) min_packet_duration = samples;
if (max_packet_bytes<d.length) max_packet_bytes = d.length;
if (min_packet_bytes>d.length) min_packet_bytes = d.length;
}


private static int packet_get_samples_per_frame(byte[] data, int Fs) {
int audiosize;
if ((data[0]&0x80) != 0)
{
audiosize = ((data[0]>>3)&0x3);
audiosize = (Fs<<audiosize)/400;
} else if ((data[0]&0x60) == 0x60)
{
audiosize = ((data[0]&0x08) != 0) ? Fs/50 : Fs/100;
} else {
audiosize = ((data[0]>>3)&0x3);
if (audiosize == 3)
audiosize = Fs*60/1000;
else
audiosize = (Fs<<audiosize)/100;
}
return audiosize;

}

private static int packet_get_nb_frames(byte[] packet) {
int count = 0;
if (packet.length < 1) {
return -1;
}
count = packet[0]&0x3;
if (count==0)
return 1;
else if (count!=3)
return 2;
else if (packet.length<2)
return -4;
else
return packet[1]&0x3F;
}

public double getMaxPacketDuration() {
return (max_packet_duration/48.0);
}
Expand Down

0 comments on commit f3b24c2

Please sign in to comment.