github.com/bluenviron/mediacommon@v1.9.3/pkg/codecs/mpeg4audio/audio_specific_config_test.go (about)

     1  package mpeg4audio
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  )
     8  
     9  var audioSpecificConfigCases = []struct {
    10  	name string
    11  	enc  []byte
    12  	dec  AudioSpecificConfig
    13  }{
    14  	{
    15  		"aac-lc 16khz mono",
    16  		[]byte{0x14, 0x08},
    17  		AudioSpecificConfig{
    18  			Type:         ObjectTypeAACLC,
    19  			SampleRate:   16000,
    20  			ChannelCount: 1,
    21  		},
    22  	},
    23  	{
    24  		"aac-lc 44.1khz mono",
    25  		[]byte{0x12, 0x08},
    26  		AudioSpecificConfig{
    27  			Type:         ObjectTypeAACLC,
    28  			SampleRate:   44100,
    29  			ChannelCount: 1,
    30  		},
    31  	},
    32  	{
    33  		"aac-lc 44.1khz 5.1",
    34  		[]byte{0x12, 0x30},
    35  		AudioSpecificConfig{
    36  			Type:         ObjectTypeAACLC,
    37  			SampleRate:   44100,
    38  			ChannelCount: 6,
    39  		},
    40  	},
    41  	{
    42  		"aac-lc 48khz stereo",
    43  		[]byte{17, 144},
    44  		AudioSpecificConfig{
    45  			Type:         ObjectTypeAACLC,
    46  			SampleRate:   48000,
    47  			ChannelCount: 2,
    48  		},
    49  	},
    50  	{
    51  		"aac-lc 53khz stereo",
    52  		[]byte{0x17, 0x80, 0x67, 0x84, 0x10},
    53  		AudioSpecificConfig{
    54  			Type:         ObjectTypeAACLC,
    55  			SampleRate:   53000,
    56  			ChannelCount: 2,
    57  		},
    58  	},
    59  	{
    60  		"aac-lc 96khz stereo delay",
    61  		[]byte{0x10, 0x12, 0x0c, 0x08},
    62  		AudioSpecificConfig{
    63  			Type:               ObjectTypeAACLC,
    64  			SampleRate:         96000,
    65  			ChannelCount:       2,
    66  			DependsOnCoreCoder: true,
    67  			CoreCoderDelay:     385,
    68  		},
    69  	},
    70  	{
    71  		"aac-lc 44.1khz 8 chans",
    72  		[]byte{0x12, 0x38},
    73  		AudioSpecificConfig{
    74  			Type:         ObjectTypeAACLC,
    75  			SampleRate:   44100,
    76  			ChannelCount: 8,
    77  		},
    78  	},
    79  	{
    80  		"sbr (he-aac v1) 44.1khz mono",
    81  		[]byte{0x2b, 0x8a, 0x08, 0x00},
    82  		AudioSpecificConfig{
    83  			Type:                ObjectTypeAACLC,
    84  			SampleRate:          22050,
    85  			ChannelCount:        1,
    86  			ExtensionSampleRate: 44100,
    87  			ExtensionType:       ObjectTypeSBR,
    88  		},
    89  	},
    90  	{
    91  		"sbr (he-aac v1) 44.1khz stereo",
    92  		[]byte{0x2b, 0x92, 0x08, 0x00}, // the data from fdk_aac
    93  		AudioSpecificConfig{
    94  			Type:                ObjectTypeAACLC,
    95  			SampleRate:          22050,
    96  			ChannelCount:        2,
    97  			ExtensionSampleRate: 44100,
    98  			ExtensionType:       ObjectTypeSBR,
    99  		},
   100  	},
   101  	{
   102  		"ps (he-aac v2) 48khz stereo",
   103  		[]byte{0xeb, 0x09, 0x88, 0x00}, // the data from fdk_aac
   104  		AudioSpecificConfig{
   105  			Type:                ObjectTypeAACLC,
   106  			SampleRate:          24000,
   107  			ChannelCount:        1,
   108  			ExtensionSampleRate: 48000,
   109  			ExtensionType:       ObjectTypePS,
   110  		},
   111  	},
   112  }
   113  
   114  func TestAudioSpecificConfigUnmarshal(t *testing.T) {
   115  	for _, ca := range audioSpecificConfigCases {
   116  		t.Run(ca.name, func(t *testing.T) {
   117  			var dec AudioSpecificConfig
   118  			err := dec.Unmarshal(ca.enc)
   119  			require.NoError(t, err)
   120  			require.Equal(t, ca.dec, dec)
   121  		})
   122  	}
   123  }
   124  
   125  func TestAudioSpecificConfigMarshal(t *testing.T) {
   126  	for _, ca := range audioSpecificConfigCases {
   127  		t.Run(ca.name, func(t *testing.T) {
   128  			enc, err := ca.dec.Marshal()
   129  			require.NoError(t, err)
   130  			require.Equal(t, ca.enc, enc)
   131  		})
   132  	}
   133  }
   134  
   135  func TestAudioSpecificConfigMarshalErrors(t *testing.T) {
   136  	_, err := AudioSpecificConfig{
   137  		Type:         ObjectTypeAACLC,
   138  		SampleRate:   44100,
   139  		ChannelCount: 0,
   140  	}.Marshal()
   141  	require.Error(t, err)
   142  }
   143  
   144  func FuzzAudioSpecificConfigUnmarshal(f *testing.F) {
   145  	f.Fuzz(func(_ *testing.T, b []byte) {
   146  		var conf AudioSpecificConfig
   147  		conf.Unmarshal(b) //nolint:errcheck
   148  	})
   149  }