|
| 1 | +package mp4 |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/binary" |
| 5 | + "errors" |
| 6 | + "io" |
| 7 | +) |
| 8 | + |
| 9 | +// SaioBox - Sample Auxiliary Information Offsets Box (saiz) (in stbl or traf box) |
| 10 | +type SaioBox struct { |
| 11 | + Box *FullBox |
| 12 | + AuxInfoType string // Used for Common Encryption Scheme (4-bytes uint32 according to spec) |
| 13 | + AuxInfoTypeParameter uint32 |
| 14 | + Offset []int64 |
| 15 | +} |
| 16 | + |
| 17 | +func (s *SaioBox) Decode(r io.Reader, size uint32) error { |
| 18 | + if _, err := s.Box.Decode(r); err != nil { |
| 19 | + return err |
| 20 | + } |
| 21 | + buf := make([]byte, size-12) |
| 22 | + if _, err := io.ReadFull(r, buf); err != nil { |
| 23 | + return err |
| 24 | + } |
| 25 | + var n int |
| 26 | + flags := uint32(s.Box.Flags[0])<<16 | uint32(s.Box.Flags[1])<<8 | uint32(s.Box.Flags[2]) |
| 27 | + if flags&0x01 != 0 { |
| 28 | + s.AuxInfoType = string(buf[n:n+4]) |
| 29 | + n += 4 |
| 30 | + s.AuxInfoTypeParameter = binary.BigEndian.Uint32(buf[n:]) |
| 31 | + n += 4 |
| 32 | + } |
| 33 | + entryCount := binary.BigEndian.Uint32(buf[n:]) |
| 34 | + n += 4 |
| 35 | + if s.Box.Version == 0 { |
| 36 | + for i := uint32(0); i < entryCount; i++ { |
| 37 | + s.Offset = append(s.Offset, int64(binary.BigEndian.Uint32(buf[n:]))) |
| 38 | + n += 4 |
| 39 | + } |
| 40 | + } else { |
| 41 | + for i := uint32(0); i < entryCount; i++ { |
| 42 | + s.Offset = append(s.Offset, int64(binary.BigEndian.Uint64(buf[n:]))) |
| 43 | + n += 8 |
| 44 | + } |
| 45 | + } |
| 46 | + return nil |
| 47 | +} |
| 48 | + |
| 49 | +func decodeSaioBox(demuxer *MovDemuxer, size uint32) error { |
| 50 | + saio := SaioBox{Box: new(FullBox)} |
| 51 | + err := saio.Decode(demuxer.reader, size) |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + if demuxer.currentTrack == nil { |
| 56 | + return errors.New("current track is nil") |
| 57 | + } |
| 58 | + if len(saio.Offset) > 0 && len(demuxer.currentTrack.subSamples) == 0 { |
| 59 | + var currentOffset int64 |
| 60 | + currentOffset, err = demuxer.reader.Seek(0, io.SeekCurrent) |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + demuxer.reader.Seek(demuxer.moofOffset+saio.Offset[0], io.SeekStart) |
| 65 | + saiz := demuxer.currentTrack.lastSaiz |
| 66 | + for i := uint32(0); i < saiz.SampleCount; i++ { |
| 67 | + sampleSize := saiz.DefaultSampleInfoSize |
| 68 | + if saiz.DefaultSampleInfoSize == 0 { |
| 69 | + sampleSize = saiz.SampleInfo[i] |
| 70 | + } |
| 71 | + buf := make([]byte, sampleSize) |
| 72 | + demuxer.reader.Read(buf) |
| 73 | + var se sencEntry |
| 74 | + se.iv = make([]byte, 16) |
| 75 | + copy(se.iv, buf[:8]) |
| 76 | + if sampleSize == 8 { |
| 77 | + demuxer.currentTrack.subSamples = append(demuxer.currentTrack.subSamples, se) |
| 78 | + continue |
| 79 | + } |
| 80 | + n := 8 |
| 81 | + sampleCount := binary.BigEndian.Uint16(buf[n:]) |
| 82 | + n += 2 |
| 83 | + |
| 84 | + se.subSamples = make([]subSampleEntry, sampleCount) |
| 85 | + for j := 0; j < int(sampleCount); j++ { |
| 86 | + se.subSamples[j].bytesOfClearData = binary.BigEndian.Uint16(buf[n:]) |
| 87 | + n += 2 |
| 88 | + se.subSamples[j].bytesOfProtectedData = binary.BigEndian.Uint32(buf[n:]) |
| 89 | + n += 4 |
| 90 | + } |
| 91 | + demuxer.currentTrack.subSamples = append(demuxer.currentTrack.subSamples, se) |
| 92 | + } |
| 93 | + demuxer.reader.Seek(currentOffset, io.SeekStart) |
| 94 | + } |
| 95 | + return nil |
| 96 | +} |
0 commit comments