From ab18dd95773cb681bf19d4a7c2013f9d8f7621cc Mon Sep 17 00:00:00 2001 From: JoeUgly <41972063+JoeUgly@users.noreply.github.com> Date: Sun, 10 Mar 2024 11:57:51 -0400 Subject: [PATCH] bits_per_coded_sample: wrote tests, changed docstring, and created warning --- av/video/codeccontext.pyx | 13 +++++++++++-- tests/test_codec_context.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/av/video/codeccontext.pyx b/av/video/codeccontext.pyx index 700c91279..d8d7c6ef3 100644 --- a/av/video/codeccontext.pyx +++ b/av/video/codeccontext.pyx @@ -100,8 +100,12 @@ cdef class VideoCodecContext(CodecContext): @property def bits_per_coded_sample(self): """ - The number of bits per sample in the codedwords, basically the bitrate per sample. - It is mandatory for this to be set for some formats to decode them. + bits per sample/pixel from the demuxer (needed for huffyuv). + + encoding: Set by libavcodec. + decoding: Set by user. + + Wraps :ffmpeg:`AVCodecContext::bits_per_coded_sample` :type: int """ @@ -109,6 +113,11 @@ cdef class VideoCodecContext(CodecContext): @bits_per_coded_sample.setter def bits_per_coded_sample(self, int value): + if self.is_encoder: + warnings.warn( + "Setting VideoCodecContext.bits_per_coded_sample for encoders is deprecated.", + AVDeprecationWarning + ) self.ptr.bits_per_coded_sample = value self._build_format() diff --git a/tests/test_codec_context.py b/tests/test_codec_context.py index 0be4ed621..81d80f4c9 100644 --- a/tests/test_codec_context.py +++ b/tests/test_codec_context.py @@ -174,6 +174,38 @@ def _assert_parse(self, codec_name, path): self.assertEqual(len(parsed_source), len(full_source)) self.assertEqual(full_source, parsed_source) + def test_bits_per_coded_sample(self): + with av.open(fate_suite("qtrle/aletrek-rle.mov")) as container: + stream = container.streams.video[0] + stream.codec_context.bits_per_coded_sample = 32 + + for packet in container.demux(stream): + for frame in packet.decode(): + pass + self.assertEqual(packet.stream.codec_context.bits_per_coded_sample, 32) + + with av.open(fate_suite("qtrle/aletrek-rle.mov")) as container: + stream = container.streams.video[0] + stream.codec_context.bits_per_coded_sample = 31 + + with self.assertRaises(av.error.InvalidDataError): + for packet in container.demux(stream): + for frame in packet.decode(): + pass + self.assertEqual( + packet.stream.codec_context.bits_per_coded_sample, 31 + ) + + with av.open(self.sandboxed("output.mov"), "w") as output: + stream = output.add_stream("qtrle") + + with warnings.catch_warnings(record=True) as captured: + stream.codec_context.bits_per_coded_sample = 32 + self.assertEqual( + captured[0].message.args[0], + "Setting VideoCodecContext.bits_per_coded_sample for encoders is deprecated.", + ) + class TestEncoding(TestCase): def test_encoding_png(self):