Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions av/audio/frame.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,12 @@ cdef class AudioFrame(Frame):
align
))

self._init_planes(AudioPlane)

def __dealloc__(self):
lib.av_freep(&self._buffer)

cdef _init_user_attributes(self):
self.layout = get_audio_layout(0, self.ptr.channel_layout)
self.format = get_audio_format(<lib.AVSampleFormat>self.ptr.format)
self._init_planes(AudioPlane)

def __repr__(self):
return '<av.%s %d, pts=%s, %d samples at %dHz, %s, %s at 0x%x>' % (
Expand Down Expand Up @@ -131,6 +128,19 @@ cdef class AudioFrame(Frame):
plane.update(array[i, :])
return frame

@property
def planes(self):
"""
A tuple of :class:`~av.audio.plane.AudioPlane`.

:type: tuple
"""
cdef int plane_count = 0
while self.ptr.extended_data[plane_count]:
plane_count += 1

return tuple([AudioPlane(self, i) for i in range(plane_count)])

property samples:
"""
Number of audio samples (per channel).
Expand Down
10 changes: 0 additions & 10 deletions av/frame.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,6 @@ cdef class Frame(object):

cdef readonly int index

cdef readonly tuple planes
"""
A tuple of :class:`~av.audio.plane.AudioPlane` or :class:`~av.video.plane.VideoPlane` objects.

:type: tuple
"""

cdef _init_planes(self, cls=?)
cdef int _max_plane_count(self)

cdef _copy_internal_attributes(self, Frame source, bint data_layout=?)

cdef _init_user_attributes(self)
28 changes: 0 additions & 28 deletions av/frame.pyx
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
from libc.limits cimport INT_MAX

from cpython cimport Py_INCREF, PyTuple_New, PyTuple_SET_ITEM

from av.plane cimport Plane
from av.utils cimport avrational_to_fraction, to_avrational

from fractions import Fraction
Expand Down Expand Up @@ -33,29 +28,6 @@ cdef class Frame(object):
self.pts,
)

cdef _init_planes(self, cls=Plane):

# We need to detect which planes actually exist, but also contrain
# ourselves to the maximum plane count (as determined only by VideoFrames
# so far), in case the library implementation does not set the last
# plane to NULL.
cdef int max_plane_count = self._max_plane_count()
cdef int plane_count = 0
while plane_count < max_plane_count and self.ptr.extended_data[plane_count]:
plane_count += 1

self.planes = PyTuple_New(plane_count)
for i in range(plane_count):
# We are constructing this tuple manually, but since Cython does
# not understand reference stealing we must manually Py_INCREF
# so that when Cython Py_DECREFs it doesn't release our object.
plane = cls(self, i)
Py_INCREF(plane)
PyTuple_SET_ITEM(self.planes, i, plane)

cdef int _max_plane_count(self):
return INT_MAX

cdef _copy_internal_attributes(self, Frame source, bint data_layout=True):
"""Mimic another frame."""
self.index = source.index
Expand Down
22 changes: 18 additions & 4 deletions av/video/frame.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,8 @@ cdef class VideoFrame(Frame):

self._init_user_attributes()

cdef int _max_plane_count(self):
return self.format.ptr.nb_components

cdef _init_user_attributes(self):
self.format = get_video_format(<lib.AVPixelFormat>self.ptr.format, self.ptr.width, self.ptr.height)
self._init_planes(VideoPlane)

def __dealloc__(self):
# The `self._buffer` member is only set if *we* allocated the buffer in `_init`,
Expand Down Expand Up @@ -274,6 +270,24 @@ cdef class VideoFrame(Frame):

return frame

@property
def planes(self):
"""
A tuple of :class:`~av.video.plane.VideoPlane` objects.

:type: tuple
"""
# We need to detect which planes actually exist, but also contrain
# ourselves to the maximum plane count (as determined only by VideoFrames
# so far), in case the library implementation does not set the last
# plane to NULL.
cdef int max_plane_count = self.format.ptr.nb_components
cdef int plane_count = 0
while plane_count < max_plane_count and self.ptr.extended_data[plane_count]:
plane_count += 1

return tuple([VideoPlane(self, i) for i in range(plane_count)])

property width:
"""Width of the image, in pixels."""
def __get__(self): return self.ptr.width
Expand Down