| 1 |
import cython |
| 2 |
from cython.cimports import libav as lib |
| 3 |
from cython.cimports.av.packet import Packet |
| 4 |
from cython.cimports.av.stream import Stream |
| 5 |
from cython.cimports.av.utils import avrational_to_fraction |
| 6 |
from cython.cimports.av.video.frame import VideoFrame |
| 7 |
from cython.cimports.libc.stdint import int32_t |
| 8 |
from cython.cimports.libc.string import memcpy |
| 9 |
|
| 10 |
|
| 11 |
@cython.final |
| 12 |
@cython.cclass |
| 13 |
class VideoStream(Stream): |
| 14 |
def __repr__(self): |
| 15 |
if self.codec_context is None: |
| 16 |
return f"<av.VideoStream #{self.index} video/<nocodec> at 0x{id(self):x}>" |
| 17 |
return ( |
| 18 |
f"<av.VideoStream #{self.index} {self.name}, " |
| 19 |
f"{self.format.name if self.format else None} {self.codec_context.width}x" |
| 20 |
f"{self.codec_context.height} at 0x{id(self):x}>" |
| 21 |
) |
| 22 |
|
| 23 |
def __getattr__(self, name): |
| 24 |
if name in ("framerate", "rate"): |
| 25 |
raise AttributeError( |
| 26 |
f"'{type(self).__name__}' object has no attribute '{name}'" |
| 27 |
) |
| 28 |
if self.codec_context is None: |
| 29 |
raise AttributeError( |
| 30 |
f"'{type(self).__name__}' object has no attribute '{name}'" |
| 31 |
) |
| 32 |
return getattr(self.codec_context, name) |
| 33 |
|
| 34 |
@cython.ccall |
| 35 |
def encode(self, frame: VideoFrame | None = None): |
| 36 |
""" |
| 37 |
Encode an :class:`.VideoFrame` and return a list of :class:`.Packet`. |
| 38 |
|
| 39 |
:rtype: list[Packet] |
| 40 |
|
| 41 |
.. seealso:: This is mostly a passthrough to :meth:`.CodecContext.encode`. |
| 42 |
""" |
| 43 |
|
| 44 |
packets = self.codec_context.encode(frame) |
| 45 |
packet: Packet |
| 46 |
for packet in packets: |
| 47 |
packet._stream = self |
| 48 |
packet.ptr.stream_index = self.ptr.index |
| 49 |
return packets |
| 50 |
|
| 51 |
@cython.ccall |
| 52 |
def decode(self, packet: Packet | None = None): |
| 53 |
""" |
| 54 |
Decode a :class:`.Packet` and return a list of :class:`.VideoFrame`. |
| 55 |
|
| 56 |
:rtype: list[VideoFrame] |
| 57 |
|
| 58 |
.. seealso:: This is a passthrough to :meth:`.CodecContext.decode`. |
| 59 |
""" |
| 60 |
return self.codec_context.decode(packet) |
| 61 |
|
| 62 |
@cython.cfunc |
| 63 |
def _finalize_for_output(self): |
| 64 |
Stream._finalize_for_output(self) |
| 65 |
# avcodec_parameters_from_context() overwrites codecpar.coded_side_data, |
| 66 |
# so inject the display matrix after it, before avformat_write_header(). |
| 67 |
if self.codec_context is not None and self._has_display_matrix: |
| 68 |
self._apply_display_matrix() |
| 69 |
|
| 70 |
@cython.cfunc |
| 71 |
def _apply_display_matrix(self): |
| 72 |
n: cython.int = 9 * cython.sizeof(int32_t) |
| 73 |
sd: cython.pointer[lib.AVPacketSideData] = lib.av_packet_side_data_new( |
| 74 |
cython.address(self.ptr.codecpar.coded_side_data), |
| 75 |
cython.address(self.ptr.codecpar.nb_coded_side_data), |
| 76 |
lib.AV_PKT_DATA_DISPLAYMATRIX, |
| 77 |
n, |
| 78 |
0, |
| 79 |
) |
| 80 |
if sd == cython.NULL: |
| 81 |
raise MemoryError("could not allocate display matrix side data") |
| 82 |
|
| 83 |
memcpy(sd.data, self._display_matrix, n) |
| 84 |
|
| 85 |
def set_display_matrix(self, matrix): |
| 86 |
"""Set the display matrix written to the container as coded side data. |
| 87 |
|
| 88 |
``matrix`` is a sequence of 9 integers in FFmpeg's ``AV_PKT_DATA_DISPLAYMATRIX`` |
| 89 |
layout, or ``None`` to clear. Must be called before the first frame is |
| 90 |
encoded. See :meth:`set_display_rotation` for a higher-level helper. |
| 91 |
""" |
| 92 |
if matrix is None: |
| 93 |
self._has_display_matrix = False |
| 94 |
return |
| 95 |
|
| 96 |
vals = [int(v) for v in matrix] |
| 97 |
if len(vals) != 9: |
| 98 |
raise ValueError("display matrix must have exactly 9 elements") |
| 99 |
i: cython.int |
| 100 |
for i in range(9): |
| 101 |
self._display_matrix[i] = vals[i] |
| 102 |
self._has_display_matrix = True |
| 103 |
|
| 104 |
def set_display_rotation(self, degrees, hflip=False, vflip=False): |
| 105 |
"""Set the container display matrix from a rotation and optional flips. |
| 106 |
|
| 107 |
``degrees`` is a counter-clockwise rotation (matching the value read back |
| 108 |
from :attr:`VideoFrame.rotation`); ``hflip`` / ``vflip`` mirror after it. |
| 109 |
Together these express all eight EXIF orientations. Must be called before |
| 110 |
the first frame is encoded. |
| 111 |
""" |
| 112 |
# av_display_rotation_set() takes a clockwise angle; negate so our public |
| 113 |
# `degrees` is counter-clockwise, matching VideoFrame.rotation on read. |
| 114 |
lib.av_display_rotation_set(self._display_matrix, -float(degrees)) |
| 115 |
lib.av_display_matrix_flip(self._display_matrix, bool(hflip), bool(vflip)) |
| 116 |
self._has_display_matrix = True |
| 117 |
|
| 118 |
@property |
| 119 |
def average_rate(self): |
| 120 |
""" |
| 121 |
The average frame rate of this video stream. |
| 122 |
|
| 123 |
This is calculated when the file is opened by looking at the first |
| 124 |
few frames and averaging their rate. |
| 125 |
|
| 126 |
:type: fractions.Fraction | None |
| 127 |
""" |
| 128 |
return avrational_to_fraction(cython.address(self.ptr.avg_frame_rate)) |
| 129 |
|
| 130 |
@property |
| 131 |
def base_rate(self): |
| 132 |
""" |
| 133 |
The base frame rate of this stream. |
| 134 |
|
| 135 |
This is calculated as the lowest framerate at which the timestamps of |
| 136 |
frames can be represented accurately. See :ffmpeg:`AVStream.r_frame_rate` |
| 137 |
for more. |
| 138 |
|
| 139 |
:type: fractions.Fraction | None |
| 140 |
""" |
| 141 |
return avrational_to_fraction(cython.address(self.ptr.r_frame_rate)) |
| 142 |
|
| 143 |
@property |
| 144 |
def guessed_rate(self): |
| 145 |
"""The guessed frame rate of this stream. |
| 146 |
|
| 147 |
This is a wrapper around :ffmpeg:`av_guess_frame_rate`, and uses multiple |
| 148 |
heuristics to decide what is "the" frame rate. |
| 149 |
|
| 150 |
:type: fractions.Fraction | None |
| 151 |
""" |
| 152 |
val: lib.AVRational = lib.av_guess_frame_rate( |
| 153 |
cython.NULL, self.ptr, cython.NULL |
| 154 |
) |
| 155 |
return avrational_to_fraction(cython.address(val)) |
| 156 |
|
| 157 |
@property |
| 158 |
def sample_aspect_ratio(self): |
| 159 |
"""The guessed sample aspect ratio (SAR) of this stream. |
| 160 |
|
| 161 |
This is a wrapper around :ffmpeg:`av_guess_sample_aspect_ratio`, and uses multiple |
| 162 |
heuristics to decide what is "the" sample aspect ratio. |
| 163 |
|
| 164 |
:type: fractions.Fraction | None |
| 165 |
""" |
| 166 |
sar: lib.AVRational = lib.av_guess_sample_aspect_ratio( |
| 167 |
self.container.ptr, self.ptr, cython.NULL |
| 168 |
) |
| 169 |
return avrational_to_fraction(cython.address(sar)) |
| 170 |
|
| 171 |
@property |
| 172 |
def display_aspect_ratio(self): |
| 173 |
"""The guessed display aspect ratio (DAR) of this stream. |
| 174 |
|
| 175 |
This is calculated from :meth:`.VideoStream.guessed_sample_aspect_ratio`. |
| 176 |
|
| 177 |
:type: fractions.Fraction | None |
| 178 |
""" |
| 179 |
dar = cython.declare(lib.AVRational) |
| 180 |
lib.av_reduce( |
| 181 |
cython.address(dar.num), |
| 182 |
cython.address(dar.den), |
| 183 |
self.format.width * self.sample_aspect_ratio.num, |
| 184 |
self.format.height * self.sample_aspect_ratio.den, |
| 185 |
1024 * 1024, |
| 186 |
) |
| 187 |
|
| 188 |
return avrational_to_fraction(cython.address(dar)) |