-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathvideodecodetorch_yuv.py
215 lines (183 loc) · 6.26 KB
/
videodecodetorch_yuv.py
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
import datetime
import sys
import argparse
import os.path
import torch
import pyRocVideoDecode.decoder as dec
import pyRocVideoDecode.demuxer as dmx
def Decoder(
input_file_path,
output_file_path,
device_id,
mem_type,
b_force_zero_latency,
crop_rect):
# demuxer instance
demuxer = dmx.demuxer(input_file_path)
# get the used coded id
codec_id = dec.GetRocDecCodecID(demuxer.GetCodecId())
# decoder instance
viddec = dec.decoder(
codec_id,
device_id,
mem_type,
b_force_zero_latency,
crop_rect,
0,
0,
1000)
# Get GPU device information
cfg = viddec.GetGpuInfo()
# check if codec is supported
if (viddec.IsCodecSupported(device_id, codec_id, demuxer.GetBitDepth()) == False):
print("ERROR: Codec is not supported on this GPU " + cfg.device_name)
exit()
# print some GPU info out
print("\ninfo: Input file: " +
input_file_path +
'\n' +
"info: Using GPU device " +
str(device_id) +
" - " +
cfg.device_name +
"[" +
cfg.gcn_arch_name +
"] on PCI bus " +
str(cfg.pci_bus_id) +
":" +
str(cfg.pci_domain_id) +
"." +
str(cfg.pci_device_id))
print("info: decoding started, please wait! \n")
# -----------------
# The decoding loop
# -----------------
n_frame = 0
total_dec_time = 0.0
while True:
start_time = datetime.datetime.now()
packet = demuxer.DemuxFrame()
n_frame_returned = viddec.DecodeFrame(packet)
for i in range(n_frame_returned):
viddec.GetFrameYuv(packet, True) # 'True' for splitting YUV into Y and UV planes
# Y Plane torch tensor
y_tensor = torch.from_dlpack(packet.ext_buf[0].__dlpack__(packet))
# U/V Plane torch tensor
uv_tensor = torch.from_dlpack(packet.ext_buf[1].__dlpack__(packet))
# TODO: some tensor work
# save Y or UV tensor to file, with original decoded Size
if (output_file_path is not None):
surface_info = viddec.GetOutputSurfaceInfo()
viddec.SaveFrameToFile(
output_file_path,
y_tensor.data_ptr(),
surface_info)
# release frame
viddec.ReleaseFrame(packet)
# measure after completing a whole frame
end_time = datetime.datetime.now()
time_per_frame = end_time - start_time
total_dec_time = total_dec_time + time_per_frame.total_seconds()
# increament frames counter
n_frame += n_frame_returned
if (packet.bitstream_size <= 0): # EOF: no more to decode
break
# beyond the decoding loop
n_frame += viddec.GetNumOfFlushedFrames()
print("info: Total frame decoded: " + str(n_frame))
if (output_file_path is None):
if (n_frame > 0 and total_dec_time > 0):
time_per_frame = (total_dec_time / n_frame) * 1000
frame_per_second = n_frame / total_dec_time
print("info: avg decoding time per frame: " +"{0:0.2f}".format(round(time_per_frame, 2)) + " ms")
print("info: avg frame per second: " +"{0:0.2f}".format(round(frame_per_second,2)) +"\n")
else:
print("info: frame count= ", n_frame)
# print tensor details
print("Y Tensor Shape: ", packet.ext_buf[0].shape)
print("Y Tensor Strides: ", packet.ext_buf[0].strides)
print("Y Tensor dType: ", packet.ext_buf[0].dtype)
print("Y Tensor Device: ", packet.ext_buf[0].__dlpack_device__(), "\n")
print("UV Tensor Shape: ", packet.ext_buf[1].shape)
print("UV Tensor Strides: ", packet.ext_buf[1].strides)
print("UV Tensor dType: ", packet.ext_buf[1].dtype)
print("UV Tensor Device: ", packet.ext_buf[1].__dlpack_device__(), "\n")
if __name__ == "__main__":
# get passed arguments
parser = argparse.ArgumentParser(
description='PyRocDecode Video Decode Arguments')
parser.add_argument(
'-i',
'--input',
type=str,
help='Input File Path - required',
required=True)
parser.add_argument(
'-o',
'--output',
type=str,
help='Output File Path - optional',
required=False)
parser.add_argument(
'-y',
'--yplane',
type=str,
default='yes',
choices=['yes', 'no'],
help='Save which Plane Y or U/V- optional, default \'yes\' to save the Y plane, \'no\' means save the U/V plane',
required=False)
parser.add_argument(
'-d',
'--device',
type=int,
default=0,
help='GPU device ID - optional, default 0',
required=False)
parser.add_argument(
'-m',
'--mem_type',
type=int,
default=1,
help='mem_type of output surfce - 0: Internal 1: dev_copied 2: host_copied 3: MEM not mapped, optional, default 0',
required=False)
parser.add_argument(
'-z',
'--zero_latency',
type=str,
default='no',
choices=['yes', 'no'],
help='Force zero latency',
required=False)
parser.add_argument(
'-crop',
'--crop_rect',
nargs=4,
type=int,
help='Crop rectangle (left, top, right, bottom), optional, default: no cropping',
required=False)
try:
args = parser.parse_args()
except BaseException:
sys.exit()
# get params
input_file_path = args.input
output_file_path = args.output
device_id = args.device
mem_type = args.mem_type
b_force_zero_latency = args.zero_latency.upper()
crop_rect = args.crop_rect
# handel params
mem_type = 0 if (mem_type < 0 or mem_type > 3) else mem_type
b_force_zero_latency = True if b_force_zero_latency == 'YES' else False
if not os.path.exists(input_file_path): # Input file (must exist)
print("ERROR: input file doesn't exist.")
exit()
# torch GPU
print("\nPyTorch Using: ", torch.cuda.get_device_name(0))
Decoder(
input_file_path,
output_file_path,
device_id,
mem_type,
b_force_zero_latency,
crop_rect)