@@ -329,11 +329,42 @@ void CudaDeviceInterface::convertAVFrameToFrameOutput(
329329 avFrame, device_, nppCtx_, nvdecStream, preAllocatedOutputTensor);
330330}
331331
332+ namespace {
333+ // Helper function to check if a codec supports CUDA hardware acceleration
334+ bool codecSupportsCudaHardware (const AVCodec* codec) {
335+ const AVCodecHWConfig* config = nullptr ;
336+ for (int j = 0 ; (config = avcodec_get_hw_config (codec, j)) != nullptr ; ++j) {
337+ if (config->device_type == AV_HWDEVICE_TYPE_CUDA) {
338+ return true ;
339+ }
340+ }
341+ return false ;
342+ }
343+ } // namespace
344+
332345// inspired by https://github.com/FFmpeg/FFmpeg/commit/ad67ea9
333346// we have to do this because of an FFmpeg bug where hardware decoding is not
334347// appropriately set, so we just go off and find the matching codec for the CUDA
335348// device
336- std::optional<const AVCodec*> CudaDeviceInterface::findCodec (
349+
350+ std::optional<const AVCodec*> CudaDeviceInterface::findEncoder (
351+ const AVCodecID& codecId) {
352+ void * i = nullptr ;
353+ const AVCodec* codec = nullptr ;
354+ while ((codec = av_codec_iterate (&i)) != nullptr ) {
355+ if (codec->id != codecId || !av_codec_is_encoder (codec)) {
356+ continue ;
357+ }
358+
359+ if (codecSupportsCudaHardware (codec)) {
360+ return codec;
361+ }
362+ }
363+
364+ return std::nullopt ;
365+ }
366+
367+ std::optional<const AVCodec*> CudaDeviceInterface::findDecoder (
337368 const AVCodecID& codecId) {
338369 void * i = nullptr ;
339370 const AVCodec* codec = nullptr ;
@@ -342,12 +373,8 @@ std::optional<const AVCodec*> CudaDeviceInterface::findCodec(
342373 continue ;
343374 }
344375
345- const AVCodecHWConfig* config = nullptr ;
346- for (int j = 0 ; (config = avcodec_get_hw_config (codec, j)) != nullptr ;
347- ++j) {
348- if (config->device_type == AV_HWDEVICE_TYPE_CUDA) {
349- return codec;
350- }
376+ if (codecSupportsCudaHardware (codec)) {
377+ return codec;
351378 }
352379 }
353380
0 commit comments