@@ -8,6 +8,7 @@ use super::descriptor::{CodecDescriptor, CodecDescriptorIter};
88use super :: profile:: ProfileIter ;
99use super :: { Capabilities , Id } ;
1010use crate :: ffi:: * ;
11+ use crate :: AsPtr ;
1112use crate :: { media, utils} ;
1213
1314#[ cfg( feature = "ffmpeg_7_1" ) ]
@@ -17,18 +18,41 @@ pub fn list_descriptors() -> CodecDescriptorIter {
1718 CodecDescriptorIter :: new ( )
1819}
1920
20- pub type Audio = Codec < AudioType > ;
21- pub type Video = Codec < VideoType > ;
22- pub type Data = Codec < DataType > ;
23- pub type Subtitle = Codec < SubtitleType > ;
24- pub type Attachment = Codec < AttachmentType > ;
21+ pub type Audio < A > = Codec < A , AudioType > ;
22+ pub type Video < A > = Codec < A , VideoType > ;
23+ pub type Data < A > = Codec < A , DataType > ;
24+ pub type Subtitle < A > = Codec < A , SubtitleType > ;
25+ pub type Attachment < A > = Codec < A , AttachmentType > ;
26+
27+ pub type Decoder < T > = Codec < DecodingAction , T > ;
28+ pub type UnknownDecoder = Codec < DecodingAction , UnknownType > ;
29+ pub type AudioDecoder = Codec < DecodingAction , AudioType > ;
30+ pub type VideoDecoder = Codec < DecodingAction , VideoType > ;
31+ pub type DataDecoder = Codec < DecodingAction , DataType > ;
32+ pub type SubtitleDecoder = Codec < DecodingAction , SubtitleType > ;
33+ pub type AttachmentDecoder = Codec < DecodingAction , AttachmentType > ;
34+
35+ pub type Encoder < T > = Codec < EncodingAction , T > ;
36+ pub type UnknownEncoder = Codec < EncodingAction , UnknownType > ;
37+ pub type AudioEncoder = Codec < EncodingAction , AudioType > ;
38+ pub type VideoEncoder = Codec < EncodingAction , VideoType > ;
39+ pub type DataEncoder = Codec < EncodingAction , DataType > ;
40+ pub type SubtitleEncoder = Codec < EncodingAction , SubtitleType > ;
41+ pub type AttachmentEncoder = Codec < EncodingAction , AttachmentType > ;
2542
2643#[ derive( PartialEq , Eq , Copy , Clone ) ]
27- pub struct Codec < Type = UnknownType > {
44+ pub struct Codec < Action , Type > {
2845 ptr : NonNull < AVCodec > ,
29- _marker : PhantomData < Type > ,
46+ _marker : PhantomData < ( Action , Type ) > ,
3047}
3148
49+ #[ derive( PartialEq , Eq , Copy , Clone ) ]
50+ pub struct UnknownAction ;
51+ #[ derive( PartialEq , Eq , Copy , Clone ) ]
52+ pub struct DecodingAction ;
53+ #[ derive( PartialEq , Eq , Copy , Clone ) ]
54+ pub struct EncodingAction ;
55+
3256#[ derive( PartialEq , Eq , Copy , Clone ) ]
3357pub struct UnknownType ;
3458#[ derive( PartialEq , Eq , Copy , Clone ) ]
@@ -42,10 +66,10 @@ pub struct SubtitleType;
4266#[ derive( PartialEq , Eq , Copy , Clone ) ]
4367pub struct AttachmentType ;
4468
45- unsafe impl < T > Send for Codec < T > { }
46- unsafe impl < T > Sync for Codec < T > { }
69+ unsafe impl < A , T > Send for Codec < A , T > { }
70+ unsafe impl < A , T > Sync for Codec < A , T > { }
4771
48- impl Codec < UnknownType > {
72+ impl < A , T > Codec < A , T > {
4973 /// Create a new reference to a codec from a raw pointer.
5074 ///
5175 /// Returns `None` if `ptr` is null.
@@ -59,84 +83,18 @@ impl Codec<UnknownType> {
5983 // Helper function to easily convert to another codec type.
6084 // TODO: Does this need to be unsafe?
6185 /// Ensure that `self.medium()` is correct for `Codec<U>`.
62- fn as_other_codec < U > ( self ) -> Codec < U > {
86+ fn as_other_codec < U , B > ( & self ) -> Codec < U , B > {
6387 Codec {
6488 ptr : self . ptr ,
6589 _marker : PhantomData ,
6690 }
6791 }
6892
69- pub fn is_video ( & self ) -> bool {
70- self . medium ( ) == media:: Type :: Video
71- }
72-
73- pub fn video ( self ) -> Option < Video > {
74- if self . is_video ( ) {
75- Some ( self . as_other_codec ( ) )
76- } else {
77- None
78- }
79- }
80-
81- pub fn is_audio ( & self ) -> bool {
82- self . medium ( ) == media:: Type :: Audio
83- }
84-
85- pub fn audio ( self ) -> Option < Audio > {
86- if self . is_audio ( ) {
87- Some ( self . as_other_codec ( ) )
88- } else {
89- None
90- }
91- }
92-
93- pub fn is_data ( & self ) -> bool {
94- self . medium ( ) == media:: Type :: Data
95- }
96-
97- pub fn data ( self ) -> Option < Data > {
98- if self . is_data ( ) {
99- Some ( self . as_other_codec ( ) )
100- } else {
101- None
102- }
103- }
104-
105- pub fn is_subtitle ( & self ) -> bool {
106- self . medium ( ) == media:: Type :: Subtitle
107- }
108-
109- pub fn subtitle ( self ) -> Option < Subtitle > {
110- if self . is_subtitle ( ) {
111- Some ( self . as_other_codec ( ) )
112- } else {
113- None
114- }
115- }
116-
117- pub fn is_attachment ( & self ) -> bool {
118- self . medium ( ) == media:: Type :: Attachment
119- }
120-
121- pub fn attachment ( self ) -> Option < Attachment > {
122- if self . is_attachment ( ) {
123- Some ( self . as_other_codec ( ) )
124- } else {
125- None
126- }
127- }
128- }
129-
130- impl < T > Codec < T > {
131- pub fn as_ptr ( & self ) -> * const AVCodec {
132- self . ptr . as_ptr ( )
133- }
134-
135- pub fn is_encoder ( & self ) -> bool {
93+ pub fn is_encoder ( self ) -> bool {
13694 unsafe { av_codec_is_encoder ( self . as_ptr ( ) ) != 0 }
13795 }
13896
139- pub fn is_decoder ( & self ) -> bool {
97+ pub fn is_decoder ( self ) -> bool {
14098 unsafe { av_codec_is_decoder ( self . as_ptr ( ) ) != 0 }
14199 }
142100
@@ -182,7 +140,61 @@ impl<T> Codec<T> {
182140 }
183141}
184142
185- impl Codec < AudioType > {
143+ impl < A : Copy , T : Copy > Codec < A , T > {
144+ pub fn as_encoder ( & self ) -> Option < Encoder < T > > {
145+ self . is_encoder ( ) . then ( || self . as_other_codec ( ) )
146+ }
147+
148+ pub fn as_decoder ( & self ) -> Option < Decoder < T > > {
149+ self . is_decoder ( ) . then ( || self . as_other_codec ( ) )
150+ }
151+ }
152+
153+ impl < A > Codec < A , UnknownType > {
154+ pub fn is_video ( self ) -> bool {
155+ self . medium ( ) == media:: Type :: Video
156+ }
157+
158+ pub fn is_audio ( self ) -> bool {
159+ self . medium ( ) == media:: Type :: Audio
160+ }
161+
162+ pub fn is_data ( self ) -> bool {
163+ self . medium ( ) == media:: Type :: Data
164+ }
165+
166+ pub fn is_subtitle ( self ) -> bool {
167+ self . medium ( ) == media:: Type :: Subtitle
168+ }
169+
170+ pub fn is_attachment ( self ) -> bool {
171+ self . medium ( ) == media:: Type :: Attachment
172+ }
173+ }
174+
175+ impl < A : Copy > Codec < A , UnknownType > {
176+ pub fn video ( self ) -> Option < Codec < A , VideoType > > {
177+ self . is_video ( ) . then ( || self . as_other_codec ( ) )
178+ }
179+
180+ pub fn audio ( self ) -> Option < Codec < A , AudioType > > {
181+ self . is_audio ( ) . then ( || self . as_other_codec ( ) )
182+ }
183+
184+ pub fn data ( self ) -> Option < Codec < A , DataType > > {
185+ self . is_data ( ) . then ( || self . as_other_codec ( ) )
186+ }
187+
188+ pub fn subtitle ( self ) -> Option < Codec < A , SubtitleType > > {
189+ self . is_subtitle ( ) . then ( || self . as_other_codec ( ) )
190+ }
191+
192+ pub fn attachment ( self ) -> Option < Codec < A , AttachmentType > > {
193+ self . is_attachment ( ) . then ( || self . as_other_codec ( ) )
194+ }
195+ }
196+
197+ impl < A > Codec < A , AudioType > {
186198 /// Checks if the given sample rate is supported by this audio codec.
187199 #[ cfg( feature = "ffmpeg_7_1" ) ]
188200 pub fn supports_rate ( self , rate : libc:: c_int ) -> bool {
@@ -228,7 +240,7 @@ impl Codec<AudioType> {
228240 }
229241}
230242
231- impl Codec < VideoType > {
243+ impl < A > Codec < A , VideoType > {
232244 /// Checks if the given frame rate is supported by this video codec.
233245 #[ cfg( feature = "ffmpeg_7_1" ) ]
234246 pub fn supports_rate ( self , rate : crate :: Rational ) -> bool {
@@ -387,3 +399,9 @@ mod ch_layout {
387399 std:: mem:: zeroed ( )
388400 }
389401}
402+
403+ impl < A , T > AsPtr < AVCodec > for Codec < A , T > {
404+ fn as_ptr ( & self ) -> * const AVCodec {
405+ self . ptr . as_ptr ( )
406+ }
407+ }
0 commit comments