Eyevinn / mp4ff

Library and tools for parsing and writing MP4 files including video, audio and subtitles. The focus is on fragmented files. Includes mp4ff-info, mp4ff-encrypt, mp4ff-decrypt and other tools.
MIT License
445 stars 81 forks source link

Files with omitted DecSpecificInfo lead to panic #372

Closed alttagil closed 2 weeks ago

alttagil commented 4 weeks ago

I also found that if in the mp4 file DecSpecificInfo is omitted it leads to a panic during parsing mp4 file, but According to a specs it is legit. And also there was a bug in ffmpeg with led to omitting it. So I want to have an ability parse them and then decide what should I do with it. Here's the diff with suggestions to be included to mp4ff lib:

--- a/mp4/descriptors.go
+++ b/mp4/descriptors.go
@@ -379,6 +379,11 @@ func DecodeDecoderConfigDescriptor(tag byte, sr bits.SliceReader, maxNrBytes int

        currPos := sr.GetPos()
        nrBytesLeft := int(size) - (currPos - dataStart)
+
+       if nrBytesLeft == 0 {
+               return &dd, nil
+       }
+
        desc, err := DecodeDescriptor(sr, nrBytesLeft)
        if err != nil {
                return nil, fmt.Errorf("failed to decode DecSpecificInfoDescriptor: %w", err)
@@ -417,7 +422,10 @@ func (d *DecoderConfigDescriptor) Type() string {
 }

 func (d *DecoderConfigDescriptor) Size() uint64 {
-       size := 13 + d.DecSpecificInfo.SizeSize()
+       size := uint64(13)
+       if d.DecSpecificInfo != nil {
+               size += d.DecSpecificInfo.SizeSize()
+       }
        for _, od := range d.OtherDescriptors {
                size += od.SizeSize()
        }
@@ -437,10 +445,15 @@ func (d *DecoderConfigDescriptor) EncodeSW(sw bits.SliceWriter) error {
        sw.WriteUint32(streamTypeAndBufferSizeDB)
        sw.WriteUint32(d.MaxBitrate)
        sw.WriteUint32(d.AvgBitrate)
-       err := d.DecSpecificInfo.EncodeSW(sw)
-       if err != nil {
-               return err
+
+       var err error
+       if d.DecSpecificInfo != nil {
+               err = d.DecSpecificInfo.EncodeSW(sw)
+               if err != nil {
+                       return err
+               }
        }
+
        for _, desc := range d.OtherDescriptors {
                err = desc.EncodeSW(sw)
                if err != nil {
tobbee commented 3 weeks ago

Hi @alttagil,

You're right that the DecoderSpecificInfo is optional. I'll fix that.

Thanks!