(self, batch)
| 244 | |
| 245 | # docs_tag: begin_call_videobatchencoder_pyvideocodec |
| 246 | def __call__(self, batch): |
| 247 | self.cvcuda_perf.push_range("encoder.pyVideoCodec") |
| 248 | |
| 249 | # Get the name of the original video file read by the decoder. We would use |
| 250 | # the same filename to save the output video. |
| 251 | file_name = os.path.splitext(os.path.basename(batch.fileinfo))[0] |
| 252 | self.output_file_name = os.path.join(self.output_path, "out_%s.mp4" % file_name) |
| 253 | |
| 254 | assert isinstance(batch.data, torch.Tensor) |
| 255 | |
| 256 | # docs_tag: begin_alloc_cvcuda_videobatchencoder_pyvideocodec |
| 257 | # Check if we need to allocate the encoder for its first use. |
| 258 | if self.encoder is None: |
| 259 | self.encoder = nvVideoEncoder( |
| 260 | self.device_id, |
| 261 | batch.data.shape[3], |
| 262 | batch.data.shape[2], |
| 263 | self.fps, |
| 264 | self.output_file_name, |
| 265 | self.cuda_ctx, |
| 266 | self.cuda_stream, |
| 267 | "NV12", |
| 268 | ) |
| 269 | # docs_tag: end_alloc_cvcuda_videobatchencoder_pyvideocodec |
| 270 | |
| 271 | # docs_tag: begin_convert_videobatchencoder_pyvideocodec |
| 272 | |
| 273 | # Create 2 CVCUDA tensors: reformat NCHW->NHWC and color conversion RGB->YUV |
| 274 | current_batch_size = batch.data.shape[0] |
| 275 | height, width = batch.data.shape[2], batch.data.shape[3] |
| 276 | |
| 277 | # Allocate only for the first time or for the last batch. |
| 278 | if ( |
| 279 | not self.cvcuda_HWCtensor_batch |
| 280 | or current_batch_size != self.cvcuda_HWCtensor_batch.shape[0] |
| 281 | ): |
| 282 | self.cvcuda_HWCtensor_batch = cvcuda.Tensor( |
| 283 | (current_batch_size, height, width, 3), |
| 284 | cvcuda.Type.U8, |
| 285 | cvcuda.TensorLayout.NHWC, |
| 286 | ) |
| 287 | self.cvcuda_YUVtensor_batch = cvcuda.Tensor( |
| 288 | (current_batch_size, (height // 2) * 3, width, 1), |
| 289 | cvcuda.Type.U8, |
| 290 | cvcuda.TensorLayout.NHWC, |
| 291 | ) |
| 292 | |
| 293 | # Convert RGB to NV12, in batch, before sending it over to pyVideoCodec. |
| 294 | # Convert to CVCUDA tensor |
| 295 | cvcuda_tensor = cvcuda.as_tensor(batch.data, cvcuda.TensorLayout.NCHW) |
| 296 | |
| 297 | # Reformat NCHW to NHWC |
| 298 | cvcuda.reformat_into(self.cvcuda_HWCtensor_batch, cvcuda_tensor) |
| 299 | |
| 300 | # Color convert from RGB to YUV_NV12 |
| 301 | cvcuda.cvtcolor_into( |
| 302 | self.cvcuda_YUVtensor_batch, |
| 303 | self.cvcuda_HWCtensor_batch, |
nothing calls this directly
no test coverage detected