@@ -69,24 +69,47 @@ func newEncoder(ctx *ff.AVFormatContext, stream_id int, param Parameters) (*enco
69
69
switch codec .Type () {
70
70
case ff .AVMEDIA_TYPE_AUDIO :
71
71
encoder .t = AUDIO
72
- // TODO: Check codec supports this configuration
73
72
74
- // Set codec parameters
75
- if err := codecctx .SetChannelLayout (par .audiopar .Ch ); err != nil {
73
+ // Choose sample format
74
+ if sampleformat , err := ff .AVCodec_supported_sampleformat (codec , par .audiopar .SampleFormat ); err != nil {
75
+ ff .AVCodec_free_context (codecctx )
76
+ return nil , err
77
+ } else {
78
+ codecctx .SetSampleFormat (sampleformat )
79
+ }
80
+
81
+ // Choose sample rate
82
+ if samplerate , err := ff .AVCodec_supported_samplerate (codec , par .audiopar .Samplerate ); err != nil {
83
+ ff .AVCodec_free_context (codecctx )
84
+ return nil , err
85
+ } else {
86
+ codecctx .SetSampleRate (samplerate )
87
+ }
88
+
89
+ // Choose channel layout
90
+ if channellayout , err := ff .AVCodec_supported_channellayout (codec , par .audiopar .Ch ); err != nil {
91
+ ff .AVCodec_free_context (codecctx )
92
+ return nil , err
93
+ } else if err := codecctx .SetChannelLayout (channellayout ); err != nil {
76
94
ff .AVCodec_free_context (codecctx )
77
95
return nil , err
78
96
}
79
- codecctx .SetSampleFormat (par .audiopar .SampleFormat )
80
- codecctx .SetSampleRate (par .audiopar .Samplerate )
81
97
82
98
// Set stream parameters
83
99
encoder .stream .SetTimeBase (ff .AVUtil_rational (1 , par .audiopar .Samplerate ))
100
+
84
101
case ff .AVMEDIA_TYPE_VIDEO :
85
102
encoder .t = VIDEO
86
- // TODO: Check codec supports this configuration
103
+
104
+ // Choose pixel format
105
+ if pixelformat , err := ff .AVCodec_supported_pixelformat (codec , par .videopar .PixelFormat ); err != nil {
106
+ ff .AVCodec_free_context (codecctx )
107
+ return nil , err
108
+ } else {
109
+ codecctx .SetPixFmt (pixelformat )
110
+ }
87
111
88
112
// Set codec parameters
89
- codecctx .SetPixFmt (par .videopar .PixelFormat )
90
113
codecctx .SetWidth (par .videopar .Width )
91
114
codecctx .SetHeight (par .videopar .Height )
92
115
0 commit comments