Skip to content

Commit

Permalink
bits_per_coded_sample: wrote tests, changed docstring, and created wa…
Browse files Browse the repository at this point in the history
…rning
  • Loading branch information
JoeSchiff committed Mar 10, 2024
1 parent e7d20c6 commit ab18dd9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
13 changes: 11 additions & 2 deletions av/video/codeccontext.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,24 @@ 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
"""
return self.ptr.bits_per_coded_sample

@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()

Expand Down
32 changes: 32 additions & 0 deletions tests/test_codec_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit ab18dd9

Please sign in to comment.