@@ -146,40 +146,6 @@ void CudaDeviceInterface::registerHardwareDeviceWithCodec(
146146 codecContext->hw_device_ctx = av_buffer_ref (hardwareDeviceCtx_.get ());
147147}
148148
149- void CudaDeviceInterface::setupEncodingContext (AVCodecContext* codecContext) {
150- TORCH_CHECK (
151- hardwareDeviceCtx_, " Hardware device context has not been initialized" );
152- TORCH_CHECK (codecContext != nullptr , " codecContext is null" );
153- // is there any way to preserve actual desired format?
154- // codecContext->sw_pix_fmt = codecContext->pix_fmt;
155- // Should we always produce AV_PIX_FMT_NV12?
156- codecContext->sw_pix_fmt = AV_PIX_FMT_NV12;
157- codecContext->pix_fmt = AV_PIX_FMT_CUDA;
158-
159- AVBufferRef* hwFramesCtxRef = av_hwframe_ctx_alloc (hardwareDeviceCtx_.get ());
160- TORCH_CHECK (
161- hwFramesCtxRef != nullptr ,
162- " Failed to allocate hardware frames context for codec" );
163-
164- AVHWFramesContext* hwFramesCtx =
165- reinterpret_cast <AVHWFramesContext*>(hwFramesCtxRef->data );
166- hwFramesCtx->format = codecContext->pix_fmt ;
167- hwFramesCtx->sw_format = codecContext->sw_pix_fmt ;
168- hwFramesCtx->width = codecContext->width ;
169- hwFramesCtx->height = codecContext->height ;
170-
171- int ret = av_hwframe_ctx_init (hwFramesCtxRef);
172- if (ret < 0 ) {
173- av_buffer_unref (&hwFramesCtxRef);
174- TORCH_CHECK (
175- false ,
176- " Failed to initialize CUDA frames context for codec: " ,
177- getFFMPEGErrorStringFromErrorCode (ret));
178- }
179-
180- codecContext->hw_frames_ctx = hwFramesCtxRef;
181- }
182-
183149UniqueAVFrame CudaDeviceInterface::maybeConvertAVFrameToNV12OrRGB24 (
184150 UniqueAVFrame& avFrame) {
185151 // We need FFmpeg filters to handle those conversion cases which are not
@@ -365,39 +331,10 @@ void CudaDeviceInterface::convertAVFrameToFrameOutput(
365331 avFrame, device_, nppCtx_, nvdecStream, preAllocatedOutputTensor);
366332}
367333
368- namespace {
369- // Helper function to check if a codec supports CUDA hardware acceleration
370- bool codecSupportsCudaHardware (const AVCodec* codec) {
371- const AVCodecHWConfig* config = nullptr ;
372- for (int j = 0 ; (config = avcodec_get_hw_config (codec, j)) != nullptr ; ++j) {
373- if (config->device_type == AV_HWDEVICE_TYPE_CUDA) {
374- return true ;
375- }
376- }
377- return false ;
378- }
379- } // namespace
380-
381334// inspired by https://github.com/FFmpeg/FFmpeg/commit/ad67ea9
382335// we have to do this because of an FFmpeg bug where hardware decoding is not
383336// appropriately set, so we just go off and find the matching codec for the CUDA
384337// device
385-
386- std::optional<const AVCodec*> CudaDeviceInterface::findEncoder (
387- const AVCodecID& codecId) {
388- void * i = nullptr ;
389- const AVCodec* codec = nullptr ;
390- while ((codec = av_codec_iterate (&i)) != nullptr ) {
391- if (codec->id != codecId || !av_codec_is_encoder (codec)) {
392- continue ;
393- }
394- if (codecSupportsCudaHardware (codec)) {
395- return codec;
396- }
397- }
398- return std::nullopt ;
399- }
400-
401338std::optional<const AVCodec*> CudaDeviceInterface::findDecoder (
402339 const AVCodecID& codecId) {
403340 void * i = nullptr ;
@@ -407,52 +344,18 @@ std::optional<const AVCodec*> CudaDeviceInterface::findDecoder(
407344 continue ;
408345 }
409346
410- if (codecSupportsCudaHardware (codec)) {
411- return codec;
347+ const AVCodecHWConfig* config = nullptr ;
348+ for (int j = 0 ; (config = avcodec_get_hw_config (codec, j)) != nullptr ;
349+ ++j) {
350+ if (config->device_type == AV_HWDEVICE_TYPE_CUDA) {
351+ return codec;
352+ }
412353 }
413354 }
414355
415356 return std::nullopt ;
416357}
417358
418- UniqueAVFrame CudaDeviceInterface::convertTensorToAVFrame (
419- const torch::Tensor& frame,
420- [[maybe_unused]] AVPixelFormat targetFormat,
421- int frameIndex,
422- AVCodecContext* codecContext) {
423- TORCH_CHECK (frame.is_cuda (), " CUDA device interface requires CUDA tensors" );
424- TORCH_CHECK (
425- frame.dim () == 3 && frame.size (0 ) == 3 ,
426- " Expected 3D RGB tensor (CHW format), got shape: " ,
427- frame.sizes ());
428-
429- UniqueAVFrame avFrame (av_frame_alloc ());
430- TORCH_CHECK (avFrame != nullptr , " Failed to allocate AVFrame" );
431-
432- avFrame->format = AV_PIX_FMT_CUDA;
433- avFrame->width = static_cast <int >(frame.size (2 ));
434- avFrame->height = static_cast <int >(frame.size (1 ));
435- avFrame->pts = frameIndex;
436-
437- int ret = av_hwframe_get_buffer (
438- codecContext ? codecContext->hw_frames_ctx : nullptr , avFrame.get (), 0 );
439- TORCH_CHECK (
440- ret >= 0 ,
441- " Failed to allocate hardware frame: " ,
442- getFFMPEGErrorStringFromErrorCode (ret));
443-
444- at::cuda::CUDAStream currentStream =
445- at::cuda::getCurrentCUDAStream (device_.index ());
446-
447- convertRGBTensorToNV12Frame (frame, avFrame, device_, nppCtx_, currentStream);
448-
449- // Set color properties to FFmpeg defaults
450- avFrame->colorspace = AVCOL_SPC_SMPTE170M; // BT.601
451- avFrame->color_range = AVCOL_RANGE_MPEG; // Limited range
452-
453- return avFrame;
454- }
455-
456359std::string CudaDeviceInterface::getDetails () {
457360 // Note: for this interface specifically the fallback is only known after a
458361 // frame has been decoded, not before: that's when FFmpeg decides to fallback,
0 commit comments