PyNvVideoCodec 2.0.4__cp314-cp314-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- PyNvVideoCodec/PyNvVideoCodec_121.cp314-win_amd64.pyd +0 -0
- PyNvVideoCodec/PyNvVideoCodec_130.cp314-win_amd64.pyd +0 -0
- PyNvVideoCodec/VersionCheck.cp314-win_amd64.pyd +0 -0
- PyNvVideoCodec/__init__.py +246 -0
- PyNvVideoCodec/avcodec-62.dll +0 -0
- PyNvVideoCodec/avdevice-62.dll +0 -0
- PyNvVideoCodec/avfilter-11.dll +0 -0
- PyNvVideoCodec/avformat-62.dll +0 -0
- PyNvVideoCodec/avutil-60.dll +0 -0
- PyNvVideoCodec/decoders/SimpleDecoder.py +230 -0
- PyNvVideoCodec/decoders/ThreadedDecoder.py +129 -0
- PyNvVideoCodec/decoders/__init__.py +22 -0
- PyNvVideoCodec/swresample-6.dll +0 -0
- PyNvVideoCodec/swscale-9.dll +0 -0
- PyNvVideoCodec/transcoder/Transcoder.py +55 -0
- PyNvVideoCodec/transcoder/__init__.py +22 -0
- benchmarks/cached_decoder_benchmark.py +813 -0
- benchmarks/frame_sampling_benchmark.py +765 -0
- benchmarks/requirements.txt +47 -0
- benchmarks/segmented_transcode_benchmark.py +1546 -0
- pynvvideocodec-2.0.4.data/data/external/ffmpeg/readme.txt +15 -0
- pynvvideocodec-2.0.4.data/data/external/ffmpeg/src/ffmpeg-8.1.1.tar.xz +0 -0
- pynvvideocodec-2.0.4.dist-info/METADATA +84 -0
- pynvvideocodec-2.0.4.dist-info/RECORD +65 -0
- pynvvideocodec-2.0.4.dist-info/WHEEL +5 -0
- pynvvideocodec-2.0.4.dist-info/licenses/LICENSE.txt +22 -0
- pynvvideocodec-2.0.4.dist-info/licenses/NOTICES.txt +167 -0
- pynvvideocodec-2.0.4.dist-info/top_level.txt +3 -0
- samples/advanced/decode.py +173 -0
- samples/advanced/decode_from_memory_buffer.py +204 -0
- samples/advanced/decode_perf.py +299 -0
- samples/advanced/decode_reconfigure.py +184 -0
- samples/advanced/decode_sei_msg.py +193 -0
- samples/advanced/decode_with_cuda_control.py +214 -0
- samples/advanced/decode_with_low_latency.py +183 -0
- samples/advanced/encode_config.json +9 -0
- samples/advanced/encode_config_lowlatency.json +11 -0
- samples/advanced/encode_perf.py +338 -0
- samples/advanced/encode_reconfigure.py +211 -0
- samples/advanced/encode_sei_msg.py +188 -0
- samples/advanced/simple_decode_stats.py +179 -0
- samples/advanced/transcode.py +288 -0
- samples/basic/create_video_segments.py +187 -0
- samples/basic/encode.py +347 -0
- samples/basic/encode_config.json +9 -0
- samples/basic/segments.txt +3 -0
- samples/basic/simple_decode_sampling.py +182 -0
- samples/basic/transcode_config.json +6 -0
- samples/jupyter/ReconfigureDecoder.png +0 -0
- samples/jupyter/SimpleDecoderAPIs.png +0 -0
- samples/jupyter/ThreadedDecoder.png +0 -0
- samples/jupyter/coco_classes.pickle +0 -0
- samples/jupyter/object_detection_tutorial.ipynb +762 -0
- samples/jupyter/simple_decode_tutorial.ipynb +651 -0
- samples/requirements.txt +78 -0
- samples/utils/Utils.py +405 -0
- samples/utils/__init__.py +36 -0
- samples/utils/decode_parser.py +642 -0
- samples/utils/decode_stats_writer.py +177 -0
- samples/utils/encode_parallel_utils.py +174 -0
- samples/utils/encode_parser.py +357 -0
- samples/utils/frame_utils.py +89 -0
- samples/utils/sei_message_parser.py +223 -0
- samples/utils/sei_parser.py +223 -0
- samples/utils/transcode_parser.py +218 -0
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
# This copyright notice applies to this file only
|
|
2
|
+
#
|
|
3
|
+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
4
|
+
# SPDX-License-Identifier: MIT
|
|
5
|
+
#
|
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining a
|
|
7
|
+
# copy of this software and associated documentation files (the "Software"),
|
|
8
|
+
# to deal in the Software without restriction, including without limitation
|
|
9
|
+
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
10
|
+
# and/or sell copies of the Software, and to permit persons to whom the
|
|
11
|
+
# Software is furnished to do so, subject to the following conditions:
|
|
12
|
+
#
|
|
13
|
+
# The above copyright notice and this permission notice shall be included in
|
|
14
|
+
# all copies or substantial portions of the Software.
|
|
15
|
+
#
|
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
19
|
+
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
21
|
+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
22
|
+
# DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
"""
|
|
25
|
+
Helper module for parsing encoder command line arguments.
|
|
26
|
+
|
|
27
|
+
This module provides standardized argument parsing for encoder sample applications.
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
import argparse
|
|
31
|
+
from pathlib import Path
|
|
32
|
+
from os.path import join, dirname
|
|
33
|
+
import json
|
|
34
|
+
import PyNvVideoCodec as nvc
|
|
35
|
+
|
|
36
|
+
# Maximum frames for performance testing to avoid OOM
|
|
37
|
+
PERF_MAX_FRAMES = 1000
|
|
38
|
+
|
|
39
|
+
def add_encode_args(parser: argparse.ArgumentParser, enforce_frame_limit: bool = False) -> None:
|
|
40
|
+
"""Add common encoder arguments to parser.
|
|
41
|
+
|
|
42
|
+
Args:
|
|
43
|
+
parser: The argument parser to add arguments to
|
|
44
|
+
enforce_frame_limit: If True, enforces max frame limit for perf testing
|
|
45
|
+
"""
|
|
46
|
+
parser.add_argument(
|
|
47
|
+
"-g", "--gpu_id",
|
|
48
|
+
type=int,
|
|
49
|
+
default=0,
|
|
50
|
+
help="Check nvidia-smi for available GPUs. Default: 0"
|
|
51
|
+
)
|
|
52
|
+
parser.add_argument(
|
|
53
|
+
"-i", "--raw_file_path",
|
|
54
|
+
type=Path,
|
|
55
|
+
required=True,
|
|
56
|
+
help="Raw video file to encode"
|
|
57
|
+
)
|
|
58
|
+
parser.add_argument(
|
|
59
|
+
"-o", "--encoded_file_path",
|
|
60
|
+
type=Path,
|
|
61
|
+
help="Encoded video file (write to). Default: <input_file_name>.<codec>"
|
|
62
|
+
)
|
|
63
|
+
parser.add_argument(
|
|
64
|
+
"-s", "--size",
|
|
65
|
+
type=str,
|
|
66
|
+
required=True,
|
|
67
|
+
help="WidthxHeight of raw frame (e.g., 1920x1080)"
|
|
68
|
+
)
|
|
69
|
+
parser.add_argument(
|
|
70
|
+
"-if", "--format",
|
|
71
|
+
type=str,
|
|
72
|
+
default="NV12",
|
|
73
|
+
help="Format of input file. Default: NV12"
|
|
74
|
+
)
|
|
75
|
+
parser.add_argument(
|
|
76
|
+
"-c", "--codec",
|
|
77
|
+
type=str,
|
|
78
|
+
help="Video codec (HEVC, H264, AV1). If not specified, uses the codec from config file."
|
|
79
|
+
)
|
|
80
|
+
parser.add_argument(
|
|
81
|
+
"-json", "--config_file",
|
|
82
|
+
type=str,
|
|
83
|
+
default=join(dirname(dirname(__file__)), "advanced", "encode_config.json"),
|
|
84
|
+
help="Path of JSON config file (default: encode_config.json in samples directory)"
|
|
85
|
+
)
|
|
86
|
+
help_text = "Number of frames to encode (0 for all frames). Default: 0"
|
|
87
|
+
if enforce_frame_limit:
|
|
88
|
+
help_text = f"Number of frames to encode (0-{PERF_MAX_FRAMES}, required for perf testing). Default: {PERF_MAX_FRAMES}"
|
|
89
|
+
|
|
90
|
+
parser.add_argument(
|
|
91
|
+
"-f", "--frame_count",
|
|
92
|
+
type=int,
|
|
93
|
+
default=PERF_MAX_FRAMES if enforce_frame_limit else 0,
|
|
94
|
+
help=help_text
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
def add_parallel_args(parser: argparse.ArgumentParser) -> None:
|
|
98
|
+
"""Add parallel processing arguments to parser."""
|
|
99
|
+
parser.add_argument(
|
|
100
|
+
"-m", "--mode",
|
|
101
|
+
type=str,
|
|
102
|
+
choices=["thread", "process"],
|
|
103
|
+
default="thread",
|
|
104
|
+
help="Parallel execution mode: 'thread' or 'process'"
|
|
105
|
+
)
|
|
106
|
+
parser.add_argument(
|
|
107
|
+
"-n", "--num_workers",
|
|
108
|
+
type=int,
|
|
109
|
+
default=1,
|
|
110
|
+
help="Number of parallel threads/processes. Default: 1"
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
def load_config(args) -> dict:
|
|
114
|
+
"""Load and process encoder configuration from JSON file and command line args."""
|
|
115
|
+
config = {}
|
|
116
|
+
if args.config_file:
|
|
117
|
+
with open(args.config_file) as jsonFile:
|
|
118
|
+
config = json.loads(jsonFile.read())
|
|
119
|
+
config["preset"] = config["preset"].upper()
|
|
120
|
+
|
|
121
|
+
# Set codec (use h264 as default if not specified)
|
|
122
|
+
args.codec = args.codec.lower() if args.codec is not None else "h264"
|
|
123
|
+
config["codec"] = args.codec
|
|
124
|
+
|
|
125
|
+
# Add GPU ID to config
|
|
126
|
+
config["gpu_id"] = args.gpu_id
|
|
127
|
+
|
|
128
|
+
# Process format
|
|
129
|
+
args.format = args.format.upper()
|
|
130
|
+
|
|
131
|
+
# Parse size
|
|
132
|
+
width, height = map(int, args.size.split("x"))
|
|
133
|
+
args.width = width
|
|
134
|
+
args.height = height
|
|
135
|
+
|
|
136
|
+
# Set default encoded_file_path if not provided (auto-generate from input file)
|
|
137
|
+
if not hasattr(args, 'encoded_file_path') or args.encoded_file_path is None:
|
|
138
|
+
args.encoded_file_path = args.raw_file_path.with_suffix(f'.{args.codec}')
|
|
139
|
+
|
|
140
|
+
return config
|
|
141
|
+
|
|
142
|
+
def add_unified_args(parser: argparse.ArgumentParser) -> None:
|
|
143
|
+
"""Add unified encoder specific arguments to parser."""
|
|
144
|
+
parser.add_argument(
|
|
145
|
+
"-m", "--buffer_mode",
|
|
146
|
+
type=str,
|
|
147
|
+
choices=["cpu", "gpu"],
|
|
148
|
+
default="gpu",
|
|
149
|
+
help="Buffer mode: 'cpu' for host memory buffers, 'gpu' for CUDA device buffers. Default: gpu"
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
def parse_perf_args(description: str = None) -> argparse.Namespace:
|
|
153
|
+
"""Parse arguments for performance testing applications.
|
|
154
|
+
|
|
155
|
+
These apps require frame count limits to prevent OOM errors.
|
|
156
|
+
Note: No output file argument as perf apps only measure performance without file output.
|
|
157
|
+
"""
|
|
158
|
+
parser = argparse.ArgumentParser(description=description)
|
|
159
|
+
|
|
160
|
+
# Add args manually without output file
|
|
161
|
+
parser.add_argument(
|
|
162
|
+
"-g", "--gpu_id",
|
|
163
|
+
type=int,
|
|
164
|
+
default=0,
|
|
165
|
+
help="Check nvidia-smi for available GPUs. Default: 0"
|
|
166
|
+
)
|
|
167
|
+
parser.add_argument(
|
|
168
|
+
"-i", "--raw_file_path",
|
|
169
|
+
type=Path,
|
|
170
|
+
required=True,
|
|
171
|
+
help="Raw video file to encode"
|
|
172
|
+
)
|
|
173
|
+
parser.add_argument(
|
|
174
|
+
"-s", "--size",
|
|
175
|
+
type=str,
|
|
176
|
+
required=True,
|
|
177
|
+
help="WidthxHeight of raw frame (e.g., 1920x1080)"
|
|
178
|
+
)
|
|
179
|
+
parser.add_argument(
|
|
180
|
+
"-if", "--format",
|
|
181
|
+
type=str,
|
|
182
|
+
default="NV12",
|
|
183
|
+
help="Format of input file. Default: NV12"
|
|
184
|
+
)
|
|
185
|
+
parser.add_argument(
|
|
186
|
+
"-c", "--codec",
|
|
187
|
+
type=str,
|
|
188
|
+
help="Video codec (HEVC, H264, AV1). If not specified, uses the codec from config file."
|
|
189
|
+
)
|
|
190
|
+
parser.add_argument(
|
|
191
|
+
"-json", "--config_file",
|
|
192
|
+
type=str,
|
|
193
|
+
default=join(dirname(dirname(__file__)), "advanced", "encode_config.json"),
|
|
194
|
+
help="Path of JSON config file (default: encode_config.json in samples directory)"
|
|
195
|
+
)
|
|
196
|
+
parser.add_argument(
|
|
197
|
+
"-f", "--frame_count",
|
|
198
|
+
type=int,
|
|
199
|
+
default=PERF_MAX_FRAMES,
|
|
200
|
+
help=f"Number of frames to encode (0-{PERF_MAX_FRAMES}, required for perf testing). Default: {PERF_MAX_FRAMES}"
|
|
201
|
+
)
|
|
202
|
+
|
|
203
|
+
add_parallel_args(parser)
|
|
204
|
+
args = parser.parse_args()
|
|
205
|
+
|
|
206
|
+
# Enforce frame limit for perf testing
|
|
207
|
+
if args.frame_count > PERF_MAX_FRAMES:
|
|
208
|
+
print(f"\nWarning: Requested {args.frame_count} frames exceeds limit. Using {PERF_MAX_FRAMES} frames.")
|
|
209
|
+
args.frame_count = PERF_MAX_FRAMES
|
|
210
|
+
elif args.frame_count <= 0:
|
|
211
|
+
args.frame_count = PERF_MAX_FRAMES
|
|
212
|
+
|
|
213
|
+
# Set encoded_file_path to None for perf apps (no output)
|
|
214
|
+
args.encoded_file_path = None
|
|
215
|
+
|
|
216
|
+
args.config = load_config(args)
|
|
217
|
+
return args
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
def parse_unified_args(description: str = None) -> argparse.Namespace:
|
|
221
|
+
"""Parse arguments for unified encoding applications.
|
|
222
|
+
|
|
223
|
+
These apps support unlimited frame counts and both CPU/GPU buffer modes.
|
|
224
|
+
"""
|
|
225
|
+
parser = argparse.ArgumentParser(description=description)
|
|
226
|
+
add_unified_args(parser)
|
|
227
|
+
add_encode_args(parser, enforce_frame_limit=False)
|
|
228
|
+
args = parser.parse_args()
|
|
229
|
+
args.config = load_config(args)
|
|
230
|
+
return args
|
|
231
|
+
|
|
232
|
+
def parse_sei_encode_args(description: str = None) -> argparse.Namespace:
|
|
233
|
+
"""Parse arguments for SEI encoding applications.
|
|
234
|
+
|
|
235
|
+
These apps use GPU buffer mode only and support unlimited frame counts.
|
|
236
|
+
Note: No buffer mode argument as these apps only support GPU buffer mode.
|
|
237
|
+
"""
|
|
238
|
+
parser = argparse.ArgumentParser(description=description)
|
|
239
|
+
|
|
240
|
+
# Add encode args manually without buffer mode
|
|
241
|
+
parser.add_argument(
|
|
242
|
+
"-g", "--gpu_id",
|
|
243
|
+
type=int,
|
|
244
|
+
default=0,
|
|
245
|
+
help="Check nvidia-smi for available GPUs. Default: 0"
|
|
246
|
+
)
|
|
247
|
+
parser.add_argument(
|
|
248
|
+
"-i", "--raw_file_path",
|
|
249
|
+
type=Path,
|
|
250
|
+
required=True,
|
|
251
|
+
help="Raw video file to encode"
|
|
252
|
+
)
|
|
253
|
+
parser.add_argument(
|
|
254
|
+
"-o", "--encoded_file_path",
|
|
255
|
+
type=Path,
|
|
256
|
+
help="Encoded video file (write to). Default: <input_file_name>.<codec>"
|
|
257
|
+
)
|
|
258
|
+
parser.add_argument(
|
|
259
|
+
"-s", "--size",
|
|
260
|
+
type=str,
|
|
261
|
+
required=True,
|
|
262
|
+
help="WidthxHeight of raw frame (e.g., 1920x1080)"
|
|
263
|
+
)
|
|
264
|
+
parser.add_argument(
|
|
265
|
+
"-if", "--format",
|
|
266
|
+
type=str,
|
|
267
|
+
default="NV12",
|
|
268
|
+
help="Format of input file. Default: NV12"
|
|
269
|
+
)
|
|
270
|
+
parser.add_argument(
|
|
271
|
+
"-c", "--codec",
|
|
272
|
+
type=str,
|
|
273
|
+
help="Video codec (HEVC, H264, AV1). If not specified, uses the codec from config file."
|
|
274
|
+
)
|
|
275
|
+
parser.add_argument(
|
|
276
|
+
"-json", "--config_file",
|
|
277
|
+
type=str,
|
|
278
|
+
default=join(dirname(dirname(__file__)), "advanced", "encode_config.json"),
|
|
279
|
+
help="Path of JSON config file (default: encode_config.json in samples directory)"
|
|
280
|
+
)
|
|
281
|
+
parser.add_argument(
|
|
282
|
+
"-f", "--frame_count",
|
|
283
|
+
type=int,
|
|
284
|
+
default=0,
|
|
285
|
+
help="Number of frames to encode (0 for all frames). Default: 0"
|
|
286
|
+
)
|
|
287
|
+
|
|
288
|
+
args = parser.parse_args()
|
|
289
|
+
args.config = load_config(args)
|
|
290
|
+
return args
|
|
291
|
+
|
|
292
|
+
def parse_reconfigure_encode_args(description: str = None) -> argparse.Namespace:
|
|
293
|
+
"""Parse arguments for encoder reconfiguration applications.
|
|
294
|
+
|
|
295
|
+
These apps use GPU buffer mode only and support unlimited frame counts.
|
|
296
|
+
Note: No buffer mode argument as these apps only support GPU buffer mode.
|
|
297
|
+
"""
|
|
298
|
+
parser = argparse.ArgumentParser(description=description)
|
|
299
|
+
|
|
300
|
+
# Add encode args without buffer mode
|
|
301
|
+
parser.add_argument(
|
|
302
|
+
"-g", "--gpu_id",
|
|
303
|
+
type=int,
|
|
304
|
+
default=0,
|
|
305
|
+
help="Check nvidia-smi for available GPUs. Default: 0"
|
|
306
|
+
)
|
|
307
|
+
parser.add_argument(
|
|
308
|
+
"-i", "--raw_file_path",
|
|
309
|
+
type=Path,
|
|
310
|
+
required=True,
|
|
311
|
+
help="Raw video file to encode"
|
|
312
|
+
)
|
|
313
|
+
parser.add_argument(
|
|
314
|
+
"-o", "--encoded_file_path",
|
|
315
|
+
type=Path,
|
|
316
|
+
help="Encoded video file (write to). Default: <input_file_name>.<codec>"
|
|
317
|
+
)
|
|
318
|
+
parser.add_argument(
|
|
319
|
+
"-s", "--size",
|
|
320
|
+
type=str,
|
|
321
|
+
required=True,
|
|
322
|
+
help="WidthxHeight of raw frame (e.g., 1920x1080)"
|
|
323
|
+
)
|
|
324
|
+
parser.add_argument(
|
|
325
|
+
"-if", "--format",
|
|
326
|
+
type=str,
|
|
327
|
+
default="NV12",
|
|
328
|
+
help="Format of input file. Default: NV12"
|
|
329
|
+
)
|
|
330
|
+
parser.add_argument(
|
|
331
|
+
"-c", "--codec",
|
|
332
|
+
type=str,
|
|
333
|
+
help="Video codec (HEVC, H264, AV1). If not specified, uses the codec from config file."
|
|
334
|
+
)
|
|
335
|
+
parser.add_argument(
|
|
336
|
+
"-json", "--config_file",
|
|
337
|
+
type=str,
|
|
338
|
+
default=join(dirname(dirname(__file__)), "advanced", "encode_config_lowlatency.json"),
|
|
339
|
+
help="Path of JSON config file (default: encode_config_lowlatency.json in samples directory)"
|
|
340
|
+
)
|
|
341
|
+
parser.add_argument(
|
|
342
|
+
"-f", "--frame_count",
|
|
343
|
+
type=int,
|
|
344
|
+
default=0,
|
|
345
|
+
help="Number of frames to encode (0 for all frames). Default: 0"
|
|
346
|
+
)
|
|
347
|
+
parser.add_argument(
|
|
348
|
+
"-tq", "--reconfigure_target_quality",
|
|
349
|
+
action="store_true",
|
|
350
|
+
help="Also reconfigure targetQuality when alternating bitrate (requires cq in config)"
|
|
351
|
+
)
|
|
352
|
+
|
|
353
|
+
args = parser.parse_args()
|
|
354
|
+
args.config = load_config(args)
|
|
355
|
+
return args
|
|
356
|
+
|
|
357
|
+
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# This copyright notice applies to this file only
|
|
2
|
+
#
|
|
3
|
+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
4
|
+
# SPDX-License-Identifier: MIT
|
|
5
|
+
#
|
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining a
|
|
7
|
+
# copy of this software and associated documentation files (the "Software"),
|
|
8
|
+
# to deal in the Software without restriction, including without limitation
|
|
9
|
+
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
10
|
+
# and/or sell copies of the Software, and to permit persons to whom the
|
|
11
|
+
# Software is furnished to do so, subject to the following conditions:
|
|
12
|
+
#
|
|
13
|
+
# The above copyright notice and this permission notice shall be included in
|
|
14
|
+
# all copies or substantial portions of the Software.
|
|
15
|
+
#
|
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
19
|
+
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
21
|
+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
22
|
+
# DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
"""
|
|
25
|
+
Helper module for frame and memory management in video encoding/decoding.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
import mmap
|
|
29
|
+
import sys
|
|
30
|
+
import pycuda.driver as cuda
|
|
31
|
+
|
|
32
|
+
def get_frame_size(width: int, height: int, surface_format: str) -> int:
|
|
33
|
+
"""
|
|
34
|
+
Calculate the size of a frame in bytes based on its dimensions and format.
|
|
35
|
+
|
|
36
|
+
Parameters:
|
|
37
|
+
width (int): Width of the frame
|
|
38
|
+
height (int): Height of the frame
|
|
39
|
+
surface_format (str): Format of the surface (e.g., "NV12", "ARGB", "YUV444")
|
|
40
|
+
|
|
41
|
+
Returns:
|
|
42
|
+
int: Size of the frame in bytes
|
|
43
|
+
"""
|
|
44
|
+
if surface_format in ("ARGB", "ABGR"):
|
|
45
|
+
return width * height * 4
|
|
46
|
+
elif surface_format == "YUV444":
|
|
47
|
+
return width * height * 3
|
|
48
|
+
elif surface_format == "YUV420":
|
|
49
|
+
return int(width * height * 3 / 2)
|
|
50
|
+
elif surface_format == "P010":
|
|
51
|
+
return int(width * height * 3 / 2 * 2)
|
|
52
|
+
elif surface_format == "YUV444_16BIT":
|
|
53
|
+
return int(width * height * 3 * 2)
|
|
54
|
+
elif surface_format == "NV16":
|
|
55
|
+
return width * height * 2
|
|
56
|
+
elif surface_format == "P210":
|
|
57
|
+
return width * height * 2 * 2
|
|
58
|
+
else: # Default NV12
|
|
59
|
+
return int(width * height * 3 / 2)
|
|
60
|
+
|
|
61
|
+
def create_memory_map(file_handle, access_read=True) -> mmap.mmap:
|
|
62
|
+
"""
|
|
63
|
+
Create a memory map for a file with cross-platform support.
|
|
64
|
+
|
|
65
|
+
Parameters:
|
|
66
|
+
file_handle: File object to memory map
|
|
67
|
+
access_read: If True, create read-only mapping, else read-write
|
|
68
|
+
|
|
69
|
+
Returns:
|
|
70
|
+
mmap.mmap: Memory mapped file object
|
|
71
|
+
"""
|
|
72
|
+
if sys.platform == 'win32':
|
|
73
|
+
return mmap.mmap(file_handle.fileno(), 0, access=mmap.ACCESS_READ if access_read else mmap.ACCESS_WRITE)
|
|
74
|
+
else:
|
|
75
|
+
return mmap.mmap(file_handle.fileno(), 0, prot=mmap.PROT_READ if access_read else mmap.PROT_WRITE)
|
|
76
|
+
|
|
77
|
+
def allocate_device_memory(host_data) -> cuda.DeviceAllocation:
|
|
78
|
+
"""
|
|
79
|
+
Allocate CUDA device memory and copy host data.
|
|
80
|
+
|
|
81
|
+
Parameters:
|
|
82
|
+
host_data: Data to copy to device
|
|
83
|
+
|
|
84
|
+
Returns:
|
|
85
|
+
cuda.DeviceAllocation: Allocated device memory with copied data
|
|
86
|
+
"""
|
|
87
|
+
device_data = cuda.mem_alloc(len(host_data))
|
|
88
|
+
cuda.memcpy_htod(device_data, host_data)
|
|
89
|
+
return device_data
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
# This copyright notice applies to this file only
|
|
2
|
+
#
|
|
3
|
+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
4
|
+
# SPDX-License-Identifier: MIT
|
|
5
|
+
#
|
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining a
|
|
7
|
+
# copy of this software and associated documentation files (the "Software"),
|
|
8
|
+
# to deal in the Software without restriction, including without limitation
|
|
9
|
+
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
10
|
+
# and/or sell copies of the Software, and to permit persons to whom the
|
|
11
|
+
# Software is furnished to do so, subject to the following conditions:
|
|
12
|
+
#
|
|
13
|
+
# The above copyright notice and this permission notice shall be included in
|
|
14
|
+
# all copies or substantial portions of the Software.
|
|
15
|
+
#
|
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
19
|
+
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
21
|
+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
22
|
+
# DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
"""
|
|
25
|
+
SEI message structures and parsing utilities.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
import ctypes
|
|
29
|
+
import pickle
|
|
30
|
+
import os
|
|
31
|
+
from os.path import join, dirname, abspath
|
|
32
|
+
import PyNvVideoCodec as nvc
|
|
33
|
+
|
|
34
|
+
# Constants
|
|
35
|
+
MAX_CLOCK_TS = 3
|
|
36
|
+
|
|
37
|
+
class TIMECODESET(ctypes.Structure):
|
|
38
|
+
"""Structure for time code set information."""
|
|
39
|
+
_fields_ = [
|
|
40
|
+
("time_offset_value", ctypes.c_uint32), # unsigned int
|
|
41
|
+
("n_frames", ctypes.c_uint16), # unsigned short
|
|
42
|
+
("clock_timestamp_flag", ctypes.c_uint8), # unsigned char
|
|
43
|
+
("units_field_based_flag", ctypes.c_uint8), # unsigned char
|
|
44
|
+
("counting_type", ctypes.c_uint8), # unsigned char
|
|
45
|
+
("full_timestamp_flag", ctypes.c_uint8), # unsigned char
|
|
46
|
+
("discontinuity_flag", ctypes.c_uint8), # unsigned char
|
|
47
|
+
("cnt_dropped_flag", ctypes.c_uint8), # unsigned char
|
|
48
|
+
("seconds_value", ctypes.c_uint8), # unsigned char
|
|
49
|
+
("minutes_value", ctypes.c_uint8), # unsigned char
|
|
50
|
+
("hours_value", ctypes.c_uint8), # unsigned char
|
|
51
|
+
("seconds_flag", ctypes.c_uint8), # unsigned char
|
|
52
|
+
("minutes_flag", ctypes.c_uint8), # unsigned char
|
|
53
|
+
("hours_flag", ctypes.c_uint8), # unsigned char
|
|
54
|
+
("time_offset_length", ctypes.c_uint8), # unsigned char
|
|
55
|
+
("reserved", ctypes.c_uint8), # unsigned char
|
|
56
|
+
]
|
|
57
|
+
|
|
58
|
+
def __repr__(self):
|
|
59
|
+
return (
|
|
60
|
+
"TIMECODESET(\n"
|
|
61
|
+
f" time_offset_value={self.time_offset_value},\n"
|
|
62
|
+
f" n_frames={self.n_frames},\n"
|
|
63
|
+
f" clock_timestamp_flag={self.clock_timestamp_flag},\n"
|
|
64
|
+
f" units_field_based_flag={self.units_field_based_flag},\n"
|
|
65
|
+
f" counting_type={self.counting_type},\n"
|
|
66
|
+
f" full_timestamp_flag={self.full_timestamp_flag},\n"
|
|
67
|
+
f" discontinuity_flag={self.discontinuity_flag},\n"
|
|
68
|
+
f" cnt_dropped_flag={self.cnt_dropped_flag},\n"
|
|
69
|
+
f" seconds_value={self.seconds_value},\n"
|
|
70
|
+
f" minutes_value={self.minutes_value},\n"
|
|
71
|
+
f" hours_value={self.hours_value},\n"
|
|
72
|
+
f" seconds_flag={self.seconds_flag},\n"
|
|
73
|
+
f" minutes_flag={self.minutes_flag},\n"
|
|
74
|
+
f" hours_flag={self.hours_flag},\n"
|
|
75
|
+
f" time_offset_length={self.time_offset_length},\n"
|
|
76
|
+
f" reserved={self.reserved}\n"
|
|
77
|
+
")"
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
class TIMECODE(ctypes.Structure):
|
|
81
|
+
"""Structure for time code information."""
|
|
82
|
+
_fields_ = [
|
|
83
|
+
("time_code_set", TIMECODESET * MAX_CLOCK_TS), # Array of TIMECODESET
|
|
84
|
+
("num_clock_ts", ctypes.c_uint8), # unsigned char
|
|
85
|
+
]
|
|
86
|
+
|
|
87
|
+
def __repr__(self):
|
|
88
|
+
time_code_set_repr = ",\n ".join(
|
|
89
|
+
repr(self.time_code_set[i]) for i in range(self.num_clock_ts)
|
|
90
|
+
)
|
|
91
|
+
return (
|
|
92
|
+
"TIMECODE(\n"
|
|
93
|
+
f" time_code_set=[\n {time_code_set_repr}\n ],\n"
|
|
94
|
+
f" num_clock_ts={self.num_clock_ts}\n"
|
|
95
|
+
")"
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
class SEICONTENTLIGHTLEVELINFO(ctypes.Structure):
|
|
99
|
+
"""Structure for content light level information."""
|
|
100
|
+
_fields_ = [
|
|
101
|
+
("max_content_light_level", ctypes.c_uint16), # unsigned short
|
|
102
|
+
("max_pic_average_light_level", ctypes.c_uint16), # unsigned short
|
|
103
|
+
("reserved", ctypes.c_uint32), # unsigned int
|
|
104
|
+
]
|
|
105
|
+
|
|
106
|
+
def __repr__(self):
|
|
107
|
+
return (
|
|
108
|
+
"SEICONTENTLIGHTLEVELINFO(\n"
|
|
109
|
+
f" max_content_light_level={self.max_content_light_level},\n"
|
|
110
|
+
f" max_pic_average_light_level={self.max_pic_average_light_level},\n"
|
|
111
|
+
f" reserved={self.reserved}\n"
|
|
112
|
+
")"
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
class SEIMASTERINGDISPLAYINFO(ctypes.Structure):
|
|
116
|
+
"""Structure for mastering display information."""
|
|
117
|
+
_fields_ = [
|
|
118
|
+
("display_primaries_x", ctypes.c_uint16 * 3), # Array of 3 unsigned short
|
|
119
|
+
("display_primaries_y", ctypes.c_uint16 * 3), # Array of 3 unsigned short
|
|
120
|
+
("white_point_x", ctypes.c_uint16), # unsigned short
|
|
121
|
+
("white_point_y", ctypes.c_uint16), # unsigned short
|
|
122
|
+
("max_display_mastering_luminance", ctypes.c_uint32), # unsigned int
|
|
123
|
+
("min_display_mastering_luminance", ctypes.c_uint32), # unsigned int
|
|
124
|
+
]
|
|
125
|
+
|
|
126
|
+
def __repr__(self):
|
|
127
|
+
return (
|
|
128
|
+
"SEIMASTERINGDISPLAYINFO(\n"
|
|
129
|
+
f" display_primaries_x={list(self.display_primaries_x)},\n"
|
|
130
|
+
f" display_primaries_y={list(self.display_primaries_y)},\n"
|
|
131
|
+
f" white_point_x={self.white_point_x},\n"
|
|
132
|
+
f" white_point_y={self.white_point_y},\n"
|
|
133
|
+
f" max_display_mastering_luminance={self.max_display_mastering_luminance},\n"
|
|
134
|
+
f" min_display_mastering_luminance={self.min_display_mastering_luminance}\n"
|
|
135
|
+
")"
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
class TIMECODEMPEG2(ctypes.Structure):
|
|
139
|
+
"""Structure for MPEG2 time code information."""
|
|
140
|
+
_fields_ = [
|
|
141
|
+
("drop_frame_flag", ctypes.c_uint8), # unsigned char
|
|
142
|
+
("time_code_hours", ctypes.c_uint8), # unsigned char
|
|
143
|
+
("time_code_minutes", ctypes.c_uint8), # unsigned char
|
|
144
|
+
("marker_bit", ctypes.c_uint8), # unsigned char
|
|
145
|
+
("time_code_seconds", ctypes.c_uint8), # unsigned char
|
|
146
|
+
("time_code_pictures", ctypes.c_uint8), # unsigned char
|
|
147
|
+
]
|
|
148
|
+
|
|
149
|
+
def __repr__(self):
|
|
150
|
+
return (
|
|
151
|
+
"TIMECODEMPEG2(\n"
|
|
152
|
+
f" drop_frame_flag={self.drop_frame_flag},\n"
|
|
153
|
+
f" time_code_hours={self.time_code_hours},\n"
|
|
154
|
+
f" time_code_minutes={self.time_code_minutes},\n"
|
|
155
|
+
f" marker_bit={self.marker_bit},\n"
|
|
156
|
+
f" time_code_seconds={self.time_code_seconds},\n"
|
|
157
|
+
f" time_code_pictures={self.time_code_pictures}\n"
|
|
158
|
+
")"
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
class SEIALTERNATIVETRANSFERCHARACTERISTICS(ctypes.Structure):
|
|
162
|
+
"""Structure for alternative transfer characteristics."""
|
|
163
|
+
_fields_ = [
|
|
164
|
+
("preferred_transfer_characteristics", ctypes.c_uint8), # unsigned char
|
|
165
|
+
]
|
|
166
|
+
|
|
167
|
+
def __repr__(self):
|
|
168
|
+
return f"SEIALTERNATIVETRANSFERCHARACTERISTICS(preferred_transfer_characteristics={self.preferred_transfer_characteristics})"
|
|
169
|
+
|
|
170
|
+
def write_sei_messages(sei_messages, sei_file_path, script_dir=None):
|
|
171
|
+
"""
|
|
172
|
+
Write SEI messages to output files.
|
|
173
|
+
|
|
174
|
+
This function writes SEI messages in three formats:
|
|
175
|
+
1. sei_message.bin - Raw binary SEI messages
|
|
176
|
+
2. sei_type_message.bin - Pickled SEI type information
|
|
177
|
+
3. sei_message.txt - Human-readable parsed SEI content
|
|
178
|
+
|
|
179
|
+
Parameters:
|
|
180
|
+
sei_messages (list): List of tuples containing (sei_info, sei_message)
|
|
181
|
+
sei_file_path (str): Path to output file (base name for all three files)
|
|
182
|
+
script_dir (str, optional): Directory for type message file. If None, uses sei_file_path's directory.
|
|
183
|
+
|
|
184
|
+
Returns:
|
|
185
|
+
None
|
|
186
|
+
"""
|
|
187
|
+
# Determine output paths
|
|
188
|
+
if script_dir is None:
|
|
189
|
+
script_dir = dirname(abspath(sei_file_path))
|
|
190
|
+
|
|
191
|
+
sei_type_message_path = join(script_dir, "sei_type_message.bin")
|
|
192
|
+
|
|
193
|
+
# Open files for storing SEI data
|
|
194
|
+
with open(sei_file_path, "wb") as file_message, \
|
|
195
|
+
open(sei_type_message_path, "wb") as file_type_message:
|
|
196
|
+
|
|
197
|
+
for sei_info, sei_message in sei_messages:
|
|
198
|
+
sei_type = sei_info["sei_type"]
|
|
199
|
+
sei_uncompressed = sei_info["sei_uncompressed"]
|
|
200
|
+
|
|
201
|
+
if sei_uncompressed == 1:
|
|
202
|
+
buffer = (ctypes.c_ubyte * len(sei_message))(*sei_message)
|
|
203
|
+
sei_struct = None
|
|
204
|
+
|
|
205
|
+
# Handle different SEI message types
|
|
206
|
+
if sei_type in (nvc.SEI_TYPE.TIME_CODE_H264, nvc.SEI_TYPE.TIME_CODE):
|
|
207
|
+
sei_struct = ctypes.cast(
|
|
208
|
+
buffer,
|
|
209
|
+
ctypes.POINTER(TIMECODEMPEG2 if codec_id == nvc.cudaVideoCodec.MPEG2 else TIMECODE)
|
|
210
|
+
).contents
|
|
211
|
+
elif sei_type == nvc.SEI_TYPE.MASTERING_DISPLAY_COLOR_VOLUME:
|
|
212
|
+
sei_struct = ctypes.cast(buffer, ctypes.POINTER(SEIMASTERINGDISPLAYINFO)).contents
|
|
213
|
+
elif sei_type == nvc.SEI_TYPE.CONTENT_LIGHT_LEVEL_INFO:
|
|
214
|
+
sei_struct = ctypes.cast(buffer, ctypes.POINTER(SEICONTENTLIGHTLEVELINFO)).contents
|
|
215
|
+
elif sei_type == nvc.SEI_TYPE.ALTERNATIVE_TRANSFER_CHARACTERISTICS:
|
|
216
|
+
sei_struct = ctypes.cast(buffer, ctypes.POINTER(SEIALTERNATIVETRANSFERCHARACTERISTICS)).contents
|
|
217
|
+
|
|
218
|
+
if sei_struct:
|
|
219
|
+
print(sei_struct)
|
|
220
|
+
|
|
221
|
+
file_message.write(bytearray(sei_message))
|
|
222
|
+
pickle.dump(sei_messages, file_type_message)
|
|
223
|
+
print(f"SEI message written to {sei_file_path}")
|