-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathframereader.py
More file actions
286 lines (224 loc) · 9.09 KB
/
framereader.py
File metadata and controls
286 lines (224 loc) · 9.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
###
### Copyright (C) 2018-2019 Intel Corporation
###
### SPDX-License-Identifier: BSD-3-Clause
###
import numpy
def read_frame_422H(fd, width, height):
width2 = (width + 1) // 2
size = width * height
size2 = width2 * height
y = numpy.fromfile(fd, dtype=numpy.uint8, count=size).reshape((height,width))
u = numpy.fromfile(fd, dtype=numpy.uint8, count=size2).reshape((height,width2))
v = numpy.fromfile(fd, dtype=numpy.uint8, count=size2).reshape((height,width2))
return y, u, v
def read_frame_422V(fd, width, height):
height2 = (height + 1) // 2
size = width * height
size2 = width * height2
y = numpy.fromfile(fd, dtype=numpy.uint8, count=size).reshape((height,width))
u = numpy.fromfile(fd, dtype=numpy.uint8, count=size2).reshape((height2,width))
v = numpy.fromfile(fd, dtype=numpy.uint8, count=size2).reshape((height2,width))
return y, u, v
def read_frame_444P(fd, width, height):
size = width * height
y = numpy.fromfile(fd, dtype=numpy.uint8, count=size).reshape((height,width))
u = numpy.fromfile(fd, dtype=numpy.uint8, count=size).reshape((height,width))
v = numpy.fromfile(fd, dtype=numpy.uint8, count=size).reshape((height,width))
return y, u, v
def read_frame_I420(fd, width, height):
width2 = (width + 1) // 2
height2 = (height + 1) // 2
size = width * height
size2 = width2 * height2
y = numpy.fromfile(fd, dtype=numpy.uint8, count=size).reshape((height, width))
u = numpy.fromfile(fd, dtype=numpy.uint8, count=size2).reshape((height2, width2))
v = numpy.fromfile(fd, dtype=numpy.uint8, count=size2).reshape((height2, width2))
return y, u, v
def read_frame_Y800(fd, width, height):
size = width * height
y = numpy.fromfile(fd, dtype=numpy.uint8, count=size).reshape((height, width))
return y, None, None
def read_frame_YV12(fd, width, height):
width2 = (width + 1) // 2
height2 = (height + 1) // 2
size = width * height
size2 = width2 * height2
y = numpy.fromfile(fd, dtype=numpy.uint8, count=size).reshape((height, width))
v = numpy.fromfile(fd, dtype=numpy.uint8, count=size2).reshape((height2, width2))
u = numpy.fromfile(fd, dtype=numpy.uint8, count=size2).reshape((height2, width2))
return y, u, v
def read_frame_NV12(fd, width, height):
width2 = (width + 1) // 2
height2 = (height + 1) // 2
size = width * height
size2 = width2 * height2 * 2
y = numpy.fromfile(fd, dtype=numpy.uint8, count=size).reshape((height, width))
uv = numpy.fromfile(fd, dtype=numpy.uint8, count=size2)
return y, uv[0::2].reshape((height2, width2)), uv[1::2].reshape((height2, width2))
def read_frame_P010(fd, width, height):
width2 = (width + 1) // 2
height2 = (height + 1) // 2
size = width * height
size2 = width2 * height2 * 2
y = numpy.fromfile(fd, dtype=numpy.uint16, count=size).reshape((height, width)) >> 6
uv = numpy.fromfile(fd, dtype=numpy.uint16, count=size2) >> 6
return y, uv[0::2].reshape((height2, width2)), uv[1::2].reshape((height2, width2))
def read_frame_I010(fd, width, height):
width2 = (width + 1) // 2
height2 = (height + 1) // 2
size = width * height
size2 = width2 * height2
y = numpy.fromfile(fd, dtype=numpy.uint16, count=size).reshape((height, width)) & 0x03ff
v = numpy.fromfile(fd, dtype=numpy.uint16, count=size2).reshape((height2, width2)) & 0x03ff
u = numpy.fromfile(fd, dtype=numpy.uint16, count=size2).reshape((height2, width2)) & 0x03ff
return y, u, v
def read_frame_P012(fd, width, height):
width2 = (width + 1) // 2
height2 = (height + 1) // 2
size = width * height
size2 = width2 * height2 * 2
y = numpy.fromfile(fd, dtype=numpy.uint16, count=size).reshape((height, width)) >> 4
uv = numpy.fromfile(fd, dtype=numpy.uint16, count=size2) >> 4
return y, uv[0::2].reshape((height2, width2)), uv[1::2].reshape((height2, width2))
def read_frame_AYUV(fd, width, height):
size = width * height * 4
ayuv = numpy.fromfile(fd, dtype=numpy.uint8, count=size)
# This uses Mircosoft ayuv definition
# https://docs.microsoft.com/en-us/windows/win32/medfound/recommended-8-bit-yuv-formats-for-video-rendering#ayuv
# For yuv 444 8bit format:
# 1) on test cases define in vaapi-fits-full/vaapi-fits, we follow to use mircosoft ayuv
# 2) on iHD, it uses as ayuv, and follows Mircosoft definition ayuv;
# 3) on ffmpeg, it uses as vuya, which is same as microsoft AYUV except the alpha channel.
# 4) on gstreamer, it use as vuya, it is byte order define;
# so we will do map from ayuv->vuya
a = ayuv[3::4].reshape((height, width))
y = ayuv[2::4].reshape((height, width))
u = ayuv[1::4].reshape((height, width))
v = ayuv[0::4].reshape((height, width))
return y, u, v
def read_frame_VUYA(fd, width, height):
size = width * height * 4
ayuv = numpy.fromfile(fd, dtype=numpy.uint8, count=size)
a = ayuv[3::4].reshape((height, width))
y = ayuv[2::4].reshape((height, width))
u = ayuv[1::4].reshape((height, width))
v = ayuv[0::4].reshape((height, width))
return y, u, v
def read_frame_YUY2(fd, width, height):
size = width * height * 2
yuv = numpy.fromfile(fd, dtype=numpy.uint8, count=size)
# frames with odd width and height produce an odd number of bytes
# in uv components and therefore cannot be effectively reshaped
return yuv[0::2].reshape((height, width)), yuv[1::4], yuv[3::4]
def read_frame_ARGB(fd, width, height):
size = width * height * 4
argb = numpy.fromfile(fd, dtype=numpy.uint8, count=size)
a = argb[0::4].reshape((height, width))
r = argb[1::4].reshape((height, width))
g = argb[2::4].reshape((height, width))
b = argb[3::4].reshape((height, width))
return r, g, b
def read_frame_BGRA(fd, width, height):
size = width * height * 4
argb = numpy.fromfile(fd, dtype=numpy.uint8, count=size)
a = argb[3::4].reshape((height, width))
r = argb[2::4].reshape((height, width))
g = argb[1::4].reshape((height, width))
b = argb[0::4].reshape((height, width))
return r, g, b
def read_frame_BGRX(fd, width, height):
size = width * height * 4
bgrx = numpy.fromfile(fd, dtype=numpy.uint8, count=size)
x = bgrx[3::4].reshape((height, width))
r = bgrx[2::4].reshape((height, width))
g = bgrx[1::4].reshape((height, width))
b = bgrx[0::4].reshape((height, width))
return r, g, b
def read_frame_Y210(fd, width, height):
#https://docs.microsoft.com/en-us/windows/win32/medfound/10-bit-and-16-bit-yuv-video-formats#422-formats
#Y210 (each pair of pixels is stored as an array of four WORD)
# Y0 U Y1 V
# word0 word1 word2 word3
#bits 15-0 bits15-0 bits15-0 bits15-0
#
#The 16bit representations described here use little-endian WORD values for each channel
#bits: 15 14 13 ... ... 5 4 3 2 1 0
#data: valid data ... 0 0 0 0 0 0
size = width * height * 2
y210 = numpy.fromfile(fd, dtype=numpy.uint16, count=size) >> 6
# frames with odd width and height produce an odd number of bytes
# in uv components and therefore cannot be effectively reshaped
y = y210[0::2].reshape((height, width))
u = y210[1::4]
v = y210[3::4]
return y, u, v
def read_frame_Y212(fd, width, height):
#https://docs.microsoft.com/en-us/windows/win32/medfound/10-bit-and-16-bit-yuv-video-formats#422-formats
#y212 64bit
#Y0 bit[15:0]
#U bit[31:16]
#Y1 bit[47:32]
#V bit[63:48]
size = width * height * 2
y212 = numpy.fromfile(fd, dtype=numpy.uint16, count=size) >> 4
#bit 0-15
y = y212[0::2].reshape((height, width))
#bit 16-31
u = y212[1::4]
#bit 32-47
v = y212[3::4]
return y, u, v
def read_frame_Y410(fd, width, height):
#https://docs.microsoft.com/en-us/windows/win32/medfound/10-bit-and-16-bit-yuv-video-formats
#y410 32bit
#U bit[9:0]
#Y bit[19:10]
#V bit[29:20]
#A bit[31:30]
y410 = numpy.fromfile(fd, dtype=numpy.uint32, count=width*height)
#bit 0-9
u = (y410 & 0x3ff).reshape((height, width)).astype(numpy.uint16)
#bit 10-19
y = ((y410 >> 10) & 0x3ff).reshape((height, width)).astype(numpy.uint16)
#bit 20-30
v = ((y410 >> 20) & 0x3ff).reshape((height, width)).astype(numpy.uint16)
return y, u, v
def read_frame_Y412(fd, width, height):
#https://docs.microsoft.com/en-us/windows/win32/medfound/10-bit-and-16-bit-yuv-video-formats
#y412 64bit
#U bit[15:0]
#Y bit[31:16]
#V bit[47:32]
#A bit[63:48]
size = width * height * 4
y412 = numpy.fromfile(fd, dtype=numpy.uint16, count=size)
#bit 0-15
u = y412[0::4].reshape((height, width)) & 0x0fff
#bit 16-31
y = y412[1::4].reshape((height, width)) & 0x0fff
#bit 32-47
v = y412[2::4].reshape((height, width)) & 0x0fff
return y, u, v
FrameReaders = {
"I420" : read_frame_I420,
"422H" : read_frame_422H,
"422V" : read_frame_422V,
"444P" : read_frame_444P,
"NV12" : read_frame_NV12,
"YV12" : read_frame_YV12,
"P010" : read_frame_P010,
"I010" : read_frame_I010,
"P012" : read_frame_P012,
"Y800" : read_frame_Y800,
"YUY2" : read_frame_YUY2,
"AYUV" : read_frame_AYUV,
"VUYA" : read_frame_VUYA,
"ARGB" : read_frame_ARGB,
"Y210" : read_frame_Y210,
"Y212" : read_frame_Y212,
"Y410" : read_frame_Y410,
"Y412" : read_frame_Y412,
"BGRA" : read_frame_BGRA,
"BGRX" : read_frame_BGRX,
}