github.com/bluenviron/mediacommon@v1.9.3/pkg/codecs/opus/packet_duration.go (about) 1 package opus 2 3 import ( 4 "time" 5 ) 6 7 var frameSizes = [32]int{ 8 480, 960, 1920, 2880, // Silk NB 9 480, 960, 1920, 2880, // Silk MB 10 480, 960, 1920, 2880, // Silk WB 11 480, 960, // Hybrid SWB 12 480, 960, // Hybrid FB 13 120, 240, 480, 960, // CELT NB 14 120, 240, 480, 960, // CELT NB 15 120, 240, 480, 960, // CELT NB 16 120, 240, 480, 960, // CELT NB 17 } 18 19 // PacketDuration returns the duration of an Opus packet. 20 // Specification: RFC6716, 3.1 21 func PacketDuration(pkt []byte) time.Duration { 22 if len(pkt) == 0 { 23 return 0 24 } 25 26 frameDuration := frameSizes[pkt[0]>>3] 27 28 frameCount := 0 29 switch pkt[0] & 3 { 30 case 0: 31 frameCount = 1 32 case 1: 33 frameCount = 2 34 case 2: 35 frameCount = 2 36 case 3: 37 if len(pkt) < 2 { 38 return 0 39 } 40 frameCount = int(pkt[1] & 63) 41 } 42 43 return (time.Duration(frameDuration) * time.Duration(frameCount) * time.Millisecond) / 48 44 }