Skip to content

Commit ac772f1

Browse files
committed
Use new config api
1 parent 06298f6 commit ac772f1

File tree

3 files changed

+78
-42
lines changed

3 files changed

+78
-42
lines changed

av/codec/codec.py

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -182,56 +182,74 @@ def id(self):
182182
@property
183183
def frame_rates(self):
184184
"""A list of supported frame rates (:class:`fractions.Fraction`), or ``None``."""
185-
if not self.ptr.supported_framerates:
185+
out: cython.p_void = cython.NULL
186+
num: cython.int = 0
187+
lib.avcodec_get_supported_config(
188+
cython.NULL,
189+
self.ptr,
190+
lib.AV_CODEC_CONFIG_FRAME_RATE,
191+
0,
192+
cython.address(out),
193+
cython.address(num),
194+
)
195+
if not out:
186196
return
187-
188-
ret: list = []
189-
i: cython.int = 0
190-
while self.ptr.supported_framerates[i].denum:
191-
ret.append(
192-
avrational_to_fraction(cython.address(self.ptr.supported_framerates[i]))
193-
)
194-
i += 1
195-
return ret
197+
rates = cython.cast(cython.pointer[lib.AVRational], out)
198+
return [avrational_to_fraction(cython.address(rates[i])) for i in range(num)]
196199

197200
@property
198201
def audio_rates(self):
199202
"""A list of supported audio sample rates (``int``), or ``None``."""
200-
if not self.ptr.supported_samplerates:
203+
out: cython.p_void = cython.NULL
204+
num: cython.int = 0
205+
lib.avcodec_get_supported_config(
206+
cython.NULL,
207+
self.ptr,
208+
lib.AV_CODEC_CONFIG_SAMPLE_RATE,
209+
0,
210+
cython.address(out),
211+
cython.address(num),
212+
)
213+
if not out:
201214
return
202-
203-
ret: list = []
204-
i: cython.int = 0
205-
while self.ptr.supported_samplerates[i]:
206-
ret.append(self.ptr.supported_samplerates[i])
207-
i += 1
208-
return ret
215+
rates = cython.cast(cython.pointer[cython.int], out)
216+
return [rates[i] for i in range(num)]
209217

210218
@property
211219
def video_formats(self):
212220
"""A list of supported :class:`.VideoFormat`, or ``None``."""
213-
if not self.ptr.pix_fmts:
221+
out: cython.p_void = cython.NULL
222+
num: cython.int = 0
223+
lib.avcodec_get_supported_config(
224+
cython.NULL,
225+
self.ptr,
226+
lib.AV_CODEC_CONFIG_PIX_FORMAT,
227+
0,
228+
cython.address(out),
229+
cython.address(num),
230+
)
231+
if not out:
214232
return
215-
216-
ret: list = []
217-
i: cython.int = 0
218-
while self.ptr.pix_fmts[i] != -1:
219-
ret.append(get_video_format(self.ptr.pix_fmts[i], 0, 0))
220-
i += 1
221-
return ret
233+
fmts = cython.cast(cython.pointer[lib.AVPixelFormat], out)
234+
return [get_video_format(fmts[i], 0, 0) for i in range(num)]
222235

223236
@property
224237
def audio_formats(self):
225238
"""A list of supported :class:`.AudioFormat`, or ``None``."""
226-
if not self.ptr.sample_fmts:
239+
out: cython.p_void = cython.NULL
240+
num: cython.int = 0
241+
lib.avcodec_get_supported_config(
242+
cython.NULL,
243+
self.ptr,
244+
lib.AV_CODEC_CONFIG_SAMPLE_FORMAT,
245+
0,
246+
cython.address(out),
247+
cython.address(num),
248+
)
249+
if not out:
227250
return
228-
229-
ret: list = []
230-
i: cython.int = 0
231-
while self.ptr.sample_fmts[i] != -1:
232-
ret.append(get_audio_format(self.ptr.sample_fmts[i]))
233-
i += 1
234-
return ret
251+
fmts = cython.cast(cython.pointer[lib.AVSampleFormat], out)
252+
return [get_audio_format(fmts[i]) for i in range(num)]
235253

236254
@property
237255
def hardware_configs(self):

av/container/output.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,17 @@ def add_stream(self, codec_name, rate=None, options: dict | None = None, **kwarg
8787

8888
# Some sane audio defaults
8989
elif codec.type == lib.AVMEDIA_TYPE_AUDIO:
90-
ctx.sample_fmt = codec.sample_fmts[0]
90+
out: cython.p_void = cython.NULL
91+
lib.avcodec_get_supported_config(
92+
cython.NULL,
93+
codec,
94+
lib.AV_CODEC_CONFIG_SAMPLE_FORMAT,
95+
0,
96+
cython.address(out),
97+
cython.NULL,
98+
)
99+
if out:
100+
ctx.sample_fmt = cython.cast(cython.pointer[lib.AVSampleFormat], out)[0]
91101
ctx.bit_rate = kwargs.pop("bit_rate", 0)
92102
ctx.bit_rate_tolerance = kwargs.pop("bit_rate_tolerance", 32000)
93103
try:

include/avcodec.pxd

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,19 +179,27 @@ cdef extern from "libavcodec/avcodec.h" nogil:
179179
char *long_name
180180
AVMediaType type
181181
AVCodecID id
182-
183182
int capabilities
184-
185-
AVRational* supported_framerates
186-
AVSampleFormat* sample_fmts
187-
AVPixelFormat* pix_fmts
188-
int* supported_samplerates
189-
190183
AVClass *priv_class
191184

192185
cdef int av_codec_is_encoder(AVCodec*)
193186
cdef int av_codec_is_decoder(AVCodec*)
194187

188+
cdef enum AVCodecConfig:
189+
AV_CODEC_CONFIG_PIX_FORMAT
190+
AV_CODEC_CONFIG_FRAME_RATE
191+
AV_CODEC_CONFIG_SAMPLE_RATE
192+
AV_CODEC_CONFIG_SAMPLE_FORMAT
193+
194+
cdef int avcodec_get_supported_config(
195+
const AVCodecContext *avctx,
196+
const AVCodec *codec,
197+
AVCodecConfig config,
198+
unsigned flags,
199+
const void **out_configs,
200+
int *out_num_configs,
201+
)
202+
195203
cdef struct AVProfile:
196204
int profile
197205
char *name

0 commit comments

Comments
 (0)