Skip to content

Commit

Permalink
Fix AudioLayout's "copy" constructor (fixes: #1434)
Browse files Browse the repository at this point in the history
There are several places where `AudioLayout` instances can be created from
another instance, for example the `AudioCodecContext.layout` setter, but
this variant of the constructor is broken.
  • Loading branch information
jlaine committed Jun 21, 2024
1 parent 84d758d commit 2c81355
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion av/audio/layout.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ cdef class AudioLayout:
elif isinstance(layout, str):
c_layout = lib.av_get_channel_layout(layout)
elif isinstance(layout, AudioLayout):
c_layout = layout.layout
c_layout = (<AudioLayout>layout).layout
else:
raise TypeError("layout must be str or int")

Expand Down
9 changes: 7 additions & 2 deletions tests/test_audiolayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@


class TestAudioLayout(TestCase):
def test_stereo_properties(self):
def test_stereo_from_str(self):
layout = AudioLayout("stereo")
self._test_stereo(layout)

def test_2channel_properties(self) -> None:
def test_stereo_from_int(self):
layout = AudioLayout(2)
self._test_stereo(layout)

def test_stereo_from_layout(self):
layout = AudioLayout("stereo")
layout2 = AudioLayout(layout)
self._test_stereo(layout2)

def test_channel_counts(self):
self.assertRaises(ValueError, AudioLayout, -1)
self.assertRaises(ValueError, AudioLayout, 9)
Expand Down
11 changes: 7 additions & 4 deletions tests/test_codec_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from unittest import SkipTest

import av
from av import AudioResampler, Codec, Packet
from av import AudioLayout, AudioResampler, Codec, Packet
from av.codec.codec import UnknownCodecError
from av.video.frame import PictureType

Expand Down Expand Up @@ -393,6 +393,12 @@ def test_encoding_mp2(self):
maxDiff = None

def audio_encoding(self, codec_name):
self._audio_encoding(codec_name=codec_name, channel_layout="stereo")
self._audio_encoding(
codec_name=codec_name, channel_layout=AudioLayout("stereo")
)

def _audio_encoding(self, *, codec_name, channel_layout):
try:
codec = Codec(codec_name, "w")
except UnknownCodecError:
Expand All @@ -404,14 +410,12 @@ def audio_encoding(self, codec_name):

sample_fmt = ctx.codec.audio_formats[-1].name
sample_rate = 48000
channel_layout = "stereo"
channels = 2

ctx.time_base = Fraction(1) / sample_rate
ctx.sample_rate = sample_rate
ctx.format = sample_fmt
ctx.layout = channel_layout
ctx.channels = channels

ctx.open()

Expand Down Expand Up @@ -549,7 +553,6 @@ def audio_encoding(self, codec_name):
ctx.sample_rate = sample_rate
ctx.format = sample_fmt
ctx.layout = channel_layout
ctx.channels = channels
ctx.open()

result_samples = 0
Expand Down

0 comments on commit 2c81355

Please sign in to comment.