| 1 |
from enum import IntEnum |
| 2 |
|
| 3 |
import cython |
| 4 |
from cython.cimports import libav as lib |
| 5 |
from cython.cimports.av.sidedata.sidedata import SideData |
| 6 |
from cython.cimports.libc.stdint import uint8_t |
| 7 |
|
| 8 |
|
| 9 |
class VideoEncParamsType(IntEnum): |
| 10 |
NONE = lib.AV_VIDEO_ENC_PARAMS_NONE |
| 11 |
VP9 = lib.AV_VIDEO_ENC_PARAMS_VP9 |
| 12 |
H264 = lib.AV_VIDEO_ENC_PARAMS_H264 |
| 13 |
MPEG2 = lib.AV_VIDEO_ENC_PARAMS_MPEG2 |
| 14 |
|
| 15 |
|
| 16 |
@cython.final |
| 17 |
@cython.cclass |
| 18 |
class VideoEncParams(SideData): |
| 19 |
def __repr__(self): |
| 20 |
return f"<av.sidedata.VideoEncParams, nb_blocks={self.nb_blocks}, codec_type={self.codec_type}, qp={self.qp}>" |
| 21 |
|
| 22 |
@property |
| 23 |
def nb_blocks(self): |
| 24 |
""" |
| 25 |
Number of blocks in the array |
| 26 |
May be 0, in which case no per-block information is present. In this case |
| 27 |
the values of blocks_offset / block_size are unspecified and should not |
| 28 |
be accessed. |
| 29 |
""" |
| 30 |
return cython.cast( |
| 31 |
cython.pointer[lib.AVVideoEncParams], self.ptr.data |
| 32 |
).nb_blocks |
| 33 |
|
| 34 |
@property |
| 35 |
def blocks_offset(self): |
| 36 |
""" |
| 37 |
Offset in bytes from the beginning of this structure at which the array of blocks starts. |
| 38 |
""" |
| 39 |
return cython.cast( |
| 40 |
cython.pointer[lib.AVVideoEncParams], self.ptr.data |
| 41 |
).blocks_offset |
| 42 |
|
| 43 |
@property |
| 44 |
def block_size(self): |
| 45 |
""" |
| 46 |
Size of each block in bytes. May not match sizeof(AVVideoBlockParams). |
| 47 |
""" |
| 48 |
return cython.cast( |
| 49 |
cython.pointer[lib.AVVideoEncParams], self.ptr.data |
| 50 |
).block_size |
| 51 |
|
| 52 |
@property |
| 53 |
def codec_type(self): |
| 54 |
""" |
| 55 |
Type of the parameters (the codec they are used with). |
| 56 |
""" |
| 57 |
t: lib.AVVideoEncParamsType = cython.cast( |
| 58 |
cython.pointer[lib.AVVideoEncParams], self.ptr.data |
| 59 |
).type |
| 60 |
return VideoEncParamsType(cython.cast(cython.int, t)) |
| 61 |
|
| 62 |
@property |
| 63 |
def qp(self): |
| 64 |
""" |
| 65 |
Base quantisation parameter for the frame. The final quantiser for a |
| 66 |
given block in a given plane is obtained from this value, possibly |
| 67 |
combined with `delta_qp` and the per-block delta in a manner |
| 68 |
documented for each type. |
| 69 |
""" |
| 70 |
return cython.cast(cython.pointer[lib.AVVideoEncParams], self.ptr.data).qp |
| 71 |
|
| 72 |
@property |
| 73 |
def delta_qp(self): |
| 74 |
""" |
| 75 |
Quantisation parameter offset from the base (per-frame) qp for a given |
| 76 |
plane (first index) and AC/DC coefficients (second index). |
| 77 |
""" |
| 78 |
p: cython.pointer[lib.AVVideoEncParams] = cython.cast( |
| 79 |
cython.pointer[lib.AVVideoEncParams], self.ptr.data |
| 80 |
) |
| 81 |
return [[p.delta_qp[i][j] for j in range(2)] for i in range(4)] |
| 82 |
|
| 83 |
def block_params(self, idx): |
| 84 |
""" |
| 85 |
Get the encoding parameters for a given block |
| 86 |
""" |
| 87 |
# Validate given index |
| 88 |
if idx < 0 or idx >= self.nb_blocks: |
| 89 |
raise ValueError("Expected idx in range [0, nb_blocks)") |
| 90 |
|
| 91 |
return VideoBlockParams(self, idx) |
| 92 |
|
| 93 |
def qp_map(self): |
| 94 |
""" |
| 95 |
Convenience method that creates a 2-D map with the quantization parameters per macroblock. |
| 96 |
Only for MPEG2 and H264 encoded videos. |
| 97 |
""" |
| 98 |
import numpy as np |
| 99 |
|
| 100 |
mb_h: cython.int = (self.frame.ptr.height + 15) // 16 |
| 101 |
mb_w: cython.int = (self.frame.ptr.width + 15) // 16 |
| 102 |
nb_mb: cython.int = mb_h * mb_w |
| 103 |
block_idx, x, y = cython.declare(cython.int) |
| 104 |
block: VideoBlockParams |
| 105 |
|
| 106 |
if self.nb_blocks != nb_mb: |
| 107 |
raise RuntimeError( |
| 108 |
"Expected frame size to match number of blocks in side data" |
| 109 |
) |
| 110 |
|
| 111 |
type: lib.AVVideoEncParamsType = cython.cast( |
| 112 |
cython.pointer[lib.AVVideoEncParams], self.ptr.data |
| 113 |
).type |
| 114 |
if ( |
| 115 |
type != lib.AVVideoEncParamsType.AV_VIDEO_ENC_PARAMS_MPEG2 |
| 116 |
and type != lib.AVVideoEncParamsType.AV_VIDEO_ENC_PARAMS_H264 |
| 117 |
): |
| 118 |
raise ValueError("Expected MPEG2 or H264") |
| 119 |
|
| 120 |
# Create a 2-D map with the number of macroblocks |
| 121 |
map = np.empty((mb_h, mb_w), dtype=np.int32) |
| 122 |
|
| 123 |
# Fill map with quantization parameter per macroblock |
| 124 |
for block_idx in range(nb_mb): |
| 125 |
block = VideoBlockParams(self, block_idx) |
| 126 |
y = block.src_y // 16 |
| 127 |
x = block.src_x // 16 |
| 128 |
map[y, x] = self.qp + block.delta_qp |
| 129 |
|
| 130 |
return map |
| 131 |
|
| 132 |
|
| 133 |
@cython.final |
| 134 |
@cython.cclass |
| 135 |
class VideoBlockParams: |
| 136 |
def __init__(self, video_enc_params: VideoEncParams, idx: cython.int) -> None: |
| 137 |
base: cython.pointer[uint8_t] = cython.cast( |
| 138 |
cython.pointer[uint8_t], video_enc_params.ptr.data |
| 139 |
) |
| 140 |
offset: cython.Py_ssize_t = ( |
| 141 |
video_enc_params.blocks_offset + idx * video_enc_params.block_size |
| 142 |
) |
| 143 |
self.ptr = cython.cast(cython.pointer[lib.AVVideoBlockParams], base + offset) |
| 144 |
|
| 145 |
def __repr__(self): |
| 146 |
return f"<av.sidedata.VideoBlockParams, src=({self.src_x}, {self.src_y}), size={self.w}x{self.h}, delta_qp={self.delta_qp}>" |
| 147 |
|
| 148 |
@property |
| 149 |
def src_x(self): |
| 150 |
""" |
| 151 |
Horizontal distance in luma pixels from the top-left corner of the visible frame |
| 152 |
to the top-left corner of the block. |
| 153 |
Can be negative if top/right padding is present on the coded frame. |
| 154 |
""" |
| 155 |
return self.ptr.src_x |
| 156 |
|
| 157 |
@property |
| 158 |
def src_y(self): |
| 159 |
""" |
| 160 |
Vertical distance in luma pixels from the top-left corner of the visible frame |
| 161 |
to the top-left corner of the block. |
| 162 |
Can be negative if top/right padding is present on the coded frame. |
| 163 |
""" |
| 164 |
return self.ptr.src_y |
| 165 |
|
| 166 |
@property |
| 167 |
def w(self): |
| 168 |
""" |
| 169 |
Width of the block in luma pixels |
| 170 |
""" |
| 171 |
return self.ptr.w |
| 172 |
|
| 173 |
@property |
| 174 |
def h(self): |
| 175 |
""" |
| 176 |
Height of the block in luma pixels |
| 177 |
""" |
| 178 |
return self.ptr.h |
| 179 |
|
| 180 |
@property |
| 181 |
def delta_qp(self): |
| 182 |
""" |
| 183 |
Difference between this block's final quantization parameter and the |
| 184 |
corresponding per-frame value. |
| 185 |
""" |
| 186 |
return self.ptr.delta_qp |