DDEV supports custom Dockerfiles, so you can add almost anything you want to the web container. See docs.
This .ddev/web-build/Dockerfile will install the mcrypt extension from pecl. It uses the techniques in the links in the question to build php-mcrypt for the PHP version in DDEV_PHP_VERSION (which is provided by the build process).
If you wanted to install a different pecl extension, you might need just a few less packages, but the idea is the same.
RUN disable_xdebug
RUN if [ ! -f /usr/bin/sed ]; then ln -sf /bin/sed /usr/bin/sed; fi
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y -o Dpkg::Options::="--force-confnew" --no-install-recommends --no-install-suggests build-essential make autoconf libc-dev pkg-config php-pear php${DDEV_PHP_VERSION}-dev libmcrypt-dev
# The "echo" below just forces accepting the "automatic" configuration, the same as hitting <RETURN>
RUN echo | sudo pecl install mcrypt
# Because php${DDEV_PHP_VERSION}-mcrypt is already installed in web container we can just copy its mcrypt.ini
RUN cp /etc/php/7.1/mods-available/mcrypt.ini /etc/php/${DDEV_PHP_VERSION}/mods-available/ && phpenmod mcrypt