Exemple #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="HardwareDeviceInfo"/> class.
 /// </summary>
 /// <param name="config">The source configuration.</param>
 internal HardwareDeviceInfo(AVCodecHWConfig *config)
 {
     DeviceType      = config->device_type;
     PixelFormat     = config->pix_fmt;
     DeviceTypeName  = ffmpeg.av_hwdevice_get_type_name(DeviceType);
     PixelFormatName = ffmpeg.av_get_pix_fmt_name(PixelFormat);
 }
        private unsafe AVPixelFormat GetHWPixelFormat(AVHWDeviceType hwDevice, AVCodec *codec)
        {
            const int     AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX = 1;
            AVPixelFormat pixelFormat = AVPixelFormat.AV_PIX_FMT_NONE;

            for (int i = 0; ; i++)
            {
                AVCodecHWConfig *hwConfig = ffmpeg.avcodec_get_hw_config(codec, i);
                if (hwConfig == null)
                {
                    throw new Exception($"Failed to find compatible pixel format for {hwDevice}");
                }
                if ((hwConfig->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX) == 0 || hwConfig->device_type != hwDevice)
                {
                    continue;
                }

                AVHWFramesConstraints *hwConstraints = ffmpeg.av_hwdevice_get_hwframe_constraints(_pCodecContext->hw_device_ctx, hwConfig);
                if (hwConstraints != null)
                {
                    for (AVPixelFormat *p = hwConstraints->valid_sw_formats; *p != AVPixelFormat.AV_PIX_FMT_NONE; p++)
                    {
                        pixelFormat = *p;
                        if (ffmpeg.sws_isSupportedInput(pixelFormat) > 0)
                        {
                            break;
                        }
                        else
                        {
                            pixelFormat = AVPixelFormat.AV_PIX_FMT_NONE;
                        }
                    }

                    ffmpeg.av_hwframe_constraints_free(&hwConstraints);
                }

                if (pixelFormat != AVPixelFormat.AV_PIX_FMT_NONE)
                {
                    return(pixelFormat);
                }
            }
        }
Exemple #3
0
        public static bool CheckCodecSupport(AVCodec *codec)
        {
            for (int i = 0; ; i++)
            {
                AVCodecHWConfig *config = avcodec_get_hw_config(codec, i);
                if (config == null)
                {
                    break;
                }
                if ((config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX) == 0 || config->pix_fmt == AVPixelFormat.AV_PIX_FMT_NONE)
                {
                    continue;
                }

                if (config->device_type == HW_DEVICE && config->pix_fmt == HW_PIX_FMT)
                {
                    return(true);
                }
            }

            return(false);
        }
Exemple #4
0
        private void SetupHardwareDecoding(AVCodec *codec)
        {
            AVHWDeviceType hwtype;

            for (int i = 0; ; i++)
            {
                AVCodecHWConfig *config = ffmpeg.avcodec_get_hw_config(codec, i);
                if (config == null)
                {
                    LogMessage("Hardware decoder not supported for this codec.");
                    return;
                }

                if ((config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX) == AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX)
                {
                    _hwPixFmt = config->pix_fmt;
                    hwtype    = config->device_type;
                    break;
                }
            }

            ffmpeg.avcodec_parameters_to_context(_videoCodecContext, _videoStream->codecpar);
            _getFormatCallback             = GetPixelFormat;
            _videoCodecContext->get_format = _getFormatCallback;

            AVBufferRef *hwDeviceCtx = null;

            if (ffmpeg.av_hwdevice_ctx_create(&hwDeviceCtx, hwtype, null, null, 0) < 0)
            {
                LogMessage("Failed to create specified HW device.");
                _hwDeviceCtx = null;
                return;
            }

            _videoCodecContext->hw_device_ctx = ffmpeg.av_buffer_ref(hwDeviceCtx);
            _hwDeviceCtx = hwDeviceCtx;
            LogMessage("Using hardware decoder: " + hwtype);
        }
 public HardwareDecoderState(AVCodecHWConfig *hwConfig, AVPixelFormat found, AVBufferRef *hwDeviceContext)
 {
     this.hwConfig        = hwConfig;
     this.found           = found;
     this.hwDeviceContext = hwDeviceContext;
 }