| 1 |
import cython |
| 2 |
import cython.cimports.libav as lib |
| 3 |
from cython.cimports.av.codec.context import CodecContext |
| 4 |
from cython.cimports.av.codec.hwaccel import HWAccel |
| 5 |
from cython.cimports.av.error import err_check |
| 6 |
from cython.cimports.av.frame import Frame |
| 7 |
from cython.cimports.av.packet import Packet |
| 8 |
from cython.cimports.av.utils import avrational_to_fraction, to_avrational |
| 9 |
from cython.cimports.av.video.format import VideoFormat, get_pix_fmt, get_video_format |
| 10 |
from cython.cimports.av.video.frame import VideoFrame, alloc_video_frame |
| 11 |
from cython.cimports.av.video.reformatter import VideoReformatter |
| 12 |
from cython.cimports.libc.stdint import int64_t |
| 13 |
|
| 14 |
|
| 15 |
@cython.cfunc |
| 16 |
@cython.exceptval(check=False) |
| 17 |
def _get_hw_format( |
| 18 |
ctx: cython.pointer[lib.AVCodecContext], |
| 19 |
pix_fmts: cython.pointer[cython.const[lib.AVPixelFormat]], |
| 20 |
) -> lib.AVPixelFormat: |
| 21 |
# In the case where we requested accelerated decoding, the decoder first calls this function |
| 22 |
# with a list that includes both the hardware format and software formats. |
| 23 |
# First we try to pick the hardware format if it's in the list. |
| 24 |
# However, if the decoder fails to initialize the hardware, it will call this function again, |
| 25 |
# with only software formats in pix_fmts. We return ctx->sw_pix_fmt regardless in this case, |
| 26 |
# because that should be in the candidate list. If not, we are out of ideas anyways. |
| 27 |
private_data: cython.pointer[AVCodecPrivateData] = cython.cast( |
| 28 |
cython.pointer[AVCodecPrivateData], ctx.opaque |
| 29 |
) |
| 30 |
i: cython.int = 0 |
| 31 |
while pix_fmts[i] != -1: |
| 32 |
if pix_fmts[i] == private_data.hardware_pix_fmt: |
| 33 |
return pix_fmts[i] |
| 34 |
i += 1 |
| 35 |
return ( |
| 36 |
ctx.sw_pix_fmt if private_data.allow_software_fallback else lib.AV_PIX_FMT_NONE |
| 37 |
) |
| 38 |
|
| 39 |
|
| 40 |
@cython.final |
| 41 |
@cython.cclass |
| 42 |
class VideoCodecContext(CodecContext): |
| 43 |
@cython.cfunc |
| 44 |
def _init( |
| 45 |
self, |
| 46 |
ptr: cython.pointer[lib.AVCodecContext], |
| 47 |
codec: cython.pointer[cython.const[lib.AVCodec]], |
| 48 |
hwaccel: HWAccel | None, |
| 49 |
): |
| 50 |
CodecContext._init(self, ptr, codec, hwaccel) |
| 51 |
|
| 52 |
if hwaccel is not None: |
| 53 |
try: |
| 54 |
self.hwaccel_ctx = hwaccel.create(self.codec) |
| 55 |
self.ptr.hw_device_ctx = lib.av_buffer_ref(self.hwaccel_ctx.ptr) |
| 56 |
self.ptr.pix_fmt = self.hwaccel_ctx.config.ptr.pix_fmt |
| 57 |
self.ptr.get_format = _get_hw_format |
| 58 |
self._private_data.hardware_pix_fmt = ( |
| 59 |
self.hwaccel_ctx.config.ptr.pix_fmt |
| 60 |
) |
| 61 |
self._private_data.allow_software_fallback = ( |
| 62 |
self.hwaccel.allow_software_fallback |
| 63 |
) |
| 64 |
self.ptr.opaque = cython.address(self._private_data) |
| 65 |
except NotImplementedError: |
| 66 |
# Some streams may not have a hardware decoder. For example, many action |
| 67 |
# cam videos have a low resolution mjpeg stream, which is usually not |
| 68 |
# compatible with hardware decoders. |
| 69 |
# The user may have passed in a hwaccel because they want to decode the main |
| 70 |
# stream with it, so we shouldn't abort even if we find a stream that can't |
| 71 |
# be HW decoded. |
| 72 |
# If the user wants to make sure hwaccel is actually used, they can check with the |
| 73 |
# is_hwaccel() function on each stream's codec context. |
| 74 |
self.hwaccel_ctx = None |
| 75 |
|
| 76 |
@cython.cfunc |
| 77 |
def _prepare_frames_for_encode(self, input: Frame | None) -> list: |
| 78 |
if input is None or not input: |
| 79 |
return [None] |
| 80 |
|
| 81 |
vframe: VideoFrame = input |
| 82 |
if ( |
| 83 |
vframe.format.pix_fmt != self.pix_fmt |
| 84 |
or vframe.width != self.ptr.width |
| 85 |
or vframe.height != self.ptr.height |
| 86 |
): |
| 87 |
if not self.reformatter: |
| 88 |
self.reformatter = VideoReformatter() |
| 89 |
|
| 90 |
vframe = self.reformatter.reformat( |
| 91 |
vframe, |
| 92 |
self.ptr.width, |
| 93 |
self.ptr.height, |
| 94 |
self.format, |
| 95 |
threads=self.ptr.thread_count, |
| 96 |
) |
| 97 |
|
| 98 |
if vframe.ptr.pts == lib.AV_NOPTS_VALUE: |
| 99 |
vframe.ptr.pts = self.ptr.frame_num |
| 100 |
|
| 101 |
return [vframe] |
| 102 |
|
| 103 |
@cython.cfunc |
| 104 |
def _alloc_next_frame(self) -> Frame: |
| 105 |
return alloc_video_frame() |
| 106 |
|
| 107 |
@cython.cfunc |
| 108 |
def _setup_decoded_frame(self, frame: Frame, packet: Packet): |
| 109 |
CodecContext._setup_decoded_frame(self, frame, packet) |
| 110 |
vframe: VideoFrame = frame |
| 111 |
vframe._init_user_attributes() |
| 112 |
|
| 113 |
@cython.cfunc |
| 114 |
def _transfer_hwframe(self, frame: Frame): |
| 115 |
if self.hwaccel_ctx is None: |
| 116 |
return frame |
| 117 |
if frame.ptr.format != self.hwaccel_ctx.config.ptr.pix_fmt: |
| 118 |
# If we get a software frame, that means we are in software fallback mode, and don't actually |
| 119 |
# need to transfer. |
| 120 |
return frame |
| 121 |
|
| 122 |
if self.hwaccel_ctx.is_hw_owned: |
| 123 |
cython.cast(VideoFrame, frame)._device_id = self.hwaccel_ctx.device_id |
| 124 |
return frame |
| 125 |
|
| 126 |
frame_sw: Frame = self._alloc_next_frame() |
| 127 |
err_check(lib.av_hwframe_transfer_data(frame_sw.ptr, frame.ptr, 0)) |
| 128 |
# TODO: Is there anything else to transfer? |
| 129 |
frame_sw.pts = frame.pts |
| 130 |
return frame_sw |
| 131 |
|
| 132 |
@property |
| 133 |
def format(self): |
| 134 |
return get_video_format( |
| 135 |
cython.cast(lib.AVPixelFormat, self.ptr.pix_fmt), |
| 136 |
self.ptr.width, |
| 137 |
self.ptr.height, |
| 138 |
) |
| 139 |
|
| 140 |
@format.setter |
| 141 |
def format(self, format: VideoFormat): |
| 142 |
self.ptr.pix_fmt = format.pix_fmt |
| 143 |
self.ptr.width = format.width |
| 144 |
self.ptr.height = format.height |
| 145 |
|
| 146 |
@property |
| 147 |
def width(self): |
| 148 |
if self.ptr is cython.NULL: |
| 149 |
return 0 |
| 150 |
return self.ptr.width |
| 151 |
|
| 152 |
@width.setter |
| 153 |
def width(self, value: cython.uint): |
| 154 |
self.ptr.width = value |
| 155 |
|
| 156 |
@property |
| 157 |
def height(self): |
| 158 |
if self.ptr is cython.NULL: |
| 159 |
return 0 |
| 160 |
return self.ptr.height |
| 161 |
|
| 162 |
@height.setter |
| 163 |
def height(self, value: cython.uint): |
| 164 |
self.ptr.height = value |
| 165 |
|
| 166 |
@property |
| 167 |
def bits_per_coded_sample(self): |
| 168 |
""" |
| 169 |
The number of bits per sample in the codedwords. It's mandatory for this to be set for some formats to decode properly. |
| 170 |
|
| 171 |
Wraps :ffmpeg:`AVCodecContext.bits_per_coded_sample`. |
| 172 |
|
| 173 |
:type: int |
| 174 |
""" |
| 175 |
return self.ptr.bits_per_coded_sample |
| 176 |
|
| 177 |
@bits_per_coded_sample.setter |
| 178 |
def bits_per_coded_sample(self, value: cython.int): |
| 179 |
if self.is_encoder: |
| 180 |
raise ValueError("Not supported for encoders") |
| 181 |
|
| 182 |
self.ptr.bits_per_coded_sample = value |
| 183 |
|
| 184 |
@property |
| 185 |
def pix_fmt(self): |
| 186 |
""" |
| 187 |
The pixel format's name. |
| 188 |
|
| 189 |
:type: str | None |
| 190 |
""" |
| 191 |
desc: cython.pointer[cython.const[lib.AVPixFmtDescriptor]] = ( |
| 192 |
lib.av_pix_fmt_desc_get(cython.cast(lib.AVPixelFormat, self.ptr.pix_fmt)) |
| 193 |
) |
| 194 |
return cython.cast(str, desc.name) |
| 195 |
|
| 196 |
@pix_fmt.setter |
| 197 |
def pix_fmt(self, value): |
| 198 |
self.ptr.pix_fmt = get_pix_fmt(value) |
| 199 |
|
| 200 |
@property |
| 201 |
def framerate(self): |
| 202 |
""" |
| 203 |
The frame rate, in frames per second. |
| 204 |
|
| 205 |
:type: fractions.Fraction |
| 206 |
""" |
| 207 |
return avrational_to_fraction(cython.address(self.ptr.framerate)) |
| 208 |
|
| 209 |
@framerate.setter |
| 210 |
def framerate(self, value): |
| 211 |
to_avrational(value, cython.address(self.ptr.framerate)) |
| 212 |
|
| 213 |
@property |
| 214 |
def rate(self): |
| 215 |
"""Another name for :attr:`framerate`.""" |
| 216 |
return self.framerate |
| 217 |
|
| 218 |
@rate.setter |
| 219 |
def rate(self, value): |
| 220 |
self.framerate = value |
| 221 |
|
| 222 |
@property |
| 223 |
def gop_size(self): |
| 224 |
""" |
| 225 |
Sets the number of frames between keyframes. Used only for encoding. |
| 226 |
|
| 227 |
:type: int |
| 228 |
""" |
| 229 |
if self.is_decoder: |
| 230 |
raise RuntimeError("Cannot access 'gop_size' as a decoder") |
| 231 |
return self.ptr.gop_size |
| 232 |
|
| 233 |
@gop_size.setter |
| 234 |
def gop_size(self, value: cython.int): |
| 235 |
if self.is_decoder: |
| 236 |
raise RuntimeError("Cannot access 'gop_size' as a decoder") |
| 237 |
self.ptr.gop_size = value |
| 238 |
|
| 239 |
@property |
| 240 |
def sample_aspect_ratio(self): |
| 241 |
return avrational_to_fraction(cython.address(self.ptr.sample_aspect_ratio)) |
| 242 |
|
| 243 |
@sample_aspect_ratio.setter |
| 244 |
def sample_aspect_ratio(self, value): |
| 245 |
to_avrational(value, cython.address(self.ptr.sample_aspect_ratio)) |
| 246 |
|
| 247 |
@property |
| 248 |
def display_aspect_ratio(self): |
| 249 |
dar: lib.AVRational |
| 250 |
lib.av_reduce( |
| 251 |
cython.address(dar.num), |
| 252 |
cython.address(dar.den), |
| 253 |
self.ptr.width * self.ptr.sample_aspect_ratio.num, |
| 254 |
self.ptr.height * self.ptr.sample_aspect_ratio.den, |
| 255 |
1024 * 1024, |
| 256 |
) |
| 257 |
|
| 258 |
return avrational_to_fraction(cython.address(dar)) |
| 259 |
|
| 260 |
@property |
| 261 |
def has_b_frames(self): |
| 262 |
""" |
| 263 |
:type: bool |
| 264 |
""" |
| 265 |
return bool(self.ptr.has_b_frames) |
| 266 |
|
| 267 |
@property |
| 268 |
def reorder_depth(self): |
| 269 |
"""Raw ``has_b_frames`` value from FFmpeg (int, not bool). |
| 270 |
|
| 271 |
After :meth:`flush_buffers`, FFmpeg may reset the internal reorder |
| 272 |
heuristic. Set this to the known reorder depth *after* seeking to |
| 273 |
avoid dropped hierarchical B-frames. |
| 274 |
""" |
| 275 |
return self.ptr.has_b_frames |
| 276 |
|
| 277 |
@reorder_depth.setter |
| 278 |
def reorder_depth(self, value: cython.int): |
| 279 |
self.ptr.has_b_frames = value |
| 280 |
|
| 281 |
@property |
| 282 |
def coded_width(self): |
| 283 |
""" |
| 284 |
:type: int |
| 285 |
""" |
| 286 |
return self.ptr.coded_width |
| 287 |
|
| 288 |
@property |
| 289 |
def coded_height(self): |
| 290 |
""" |
| 291 |
:type: int |
| 292 |
""" |
| 293 |
return self.ptr.coded_height |
| 294 |
|
| 295 |
@property |
| 296 |
def color_range(self): |
| 297 |
""" |
| 298 |
Describes the signal range of the colorspace. |
| 299 |
|
| 300 |
Wraps :ffmpeg:`AVFrame.color_range`. |
| 301 |
|
| 302 |
:type: int |
| 303 |
""" |
| 304 |
return self.ptr.color_range |
| 305 |
|
| 306 |
@color_range.setter |
| 307 |
def color_range(self, value): |
| 308 |
self.ptr.color_range = value |
| 309 |
|
| 310 |
@property |
| 311 |
def color_primaries(self): |
| 312 |
""" |
| 313 |
Describes the RGB/XYZ matrix of the colorspace. |
| 314 |
|
| 315 |
Wraps :ffmpeg:`AVFrame.color_primaries`. |
| 316 |
|
| 317 |
:type: int |
| 318 |
""" |
| 319 |
return self.ptr.color_primaries |
| 320 |
|
| 321 |
@color_primaries.setter |
| 322 |
def color_primaries(self, value): |
| 323 |
self.ptr.color_primaries = value |
| 324 |
|
| 325 |
@property |
| 326 |
def color_trc(self): |
| 327 |
""" |
| 328 |
Describes the linearization function (a.k.a. transformation characteristics) of the colorspace. |
| 329 |
|
| 330 |
Wraps :ffmpeg:`AVFrame.color_trc`. |
| 331 |
|
| 332 |
:type: int |
| 333 |
""" |
| 334 |
return self.ptr.color_trc |
| 335 |
|
| 336 |
@color_trc.setter |
| 337 |
def color_trc(self, value): |
| 338 |
self.ptr.color_trc = value |
| 339 |
|
| 340 |
@property |
| 341 |
def colorspace(self): |
| 342 |
""" |
| 343 |
Describes the YUV/RGB transformation matrix of the colorspace. |
| 344 |
|
| 345 |
Wraps :ffmpeg:`AVFrame.colorspace`. |
| 346 |
|
| 347 |
:type: int |
| 348 |
""" |
| 349 |
return self.ptr.colorspace |
| 350 |
|
| 351 |
@colorspace.setter |
| 352 |
def colorspace(self, value): |
| 353 |
self.ptr.colorspace = value |
| 354 |
|
| 355 |
@property |
| 356 |
def max_b_frames(self): |
| 357 |
""" |
| 358 |
The maximum run of consecutive B frames when encoding a video. |
| 359 |
|
| 360 |
:type: int |
| 361 |
""" |
| 362 |
return self.ptr.max_b_frames |
| 363 |
|
| 364 |
@max_b_frames.setter |
| 365 |
def max_b_frames(self, value): |
| 366 |
self.ptr.max_b_frames = value |
| 367 |
|
| 368 |
@property |
| 369 |
def qmin(self): |
| 370 |
""" |
| 371 |
The minimum quantiser value of an encoded stream. |
| 372 |
|
| 373 |
Wraps :ffmpeg:`AVCodecContext.qmin`. |
| 374 |
|
| 375 |
:type: int |
| 376 |
""" |
| 377 |
return self.ptr.qmin |
| 378 |
|
| 379 |
@qmin.setter |
| 380 |
def qmin(self, value): |
| 381 |
self.ptr.qmin = value |
| 382 |
|
| 383 |
@property |
| 384 |
def qmax(self): |
| 385 |
""" |
| 386 |
The maximum quantiser value of an encoded stream. |
| 387 |
|
| 388 |
Wraps :ffmpeg:`AVCodecContext.qmax`. |
| 389 |
|
| 390 |
:type: int |
| 391 |
""" |
| 392 |
return self.ptr.qmax |
| 393 |
|
| 394 |
@qmax.setter |
| 395 |
def qmax(self, value): |
| 396 |
self.ptr.qmax = value |