// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only

/*!
\page qtmultimedia-building-ffmpeg-windows.html
\title Building FFmpeg from source on Windows
\brief This document describes how to build FFmpeg from source code.

This page explains how to configure and build \l{FFmpeg} on Windows.
This involves:

\list
\li Get the FFmpeg source code.
\li Install the required dependencies.
\li Configure FFmpeg from the command line for MSVC or for MinGW-w64.
\li Build the development libraries.
\endlist

\section1 Get the FFmpeg source code

You can get the FFmpeg source code in these ways:

\list
    \li Download from the
        \l{https://ffmpeg.org/download.html#get-sources}{FFmpeg download page}.
    \li Clone from git. For example, this command clones the version 7.1 of the
        FFmpeg sources to \c{C:\FFmpeg\ffmpeg}.

        \badcode
            C:\FFmpeg> git clone --branch n7.1 https://git.ffmpeg.org/ffmpeg.git ffmpeg
        \endcode
\endlist

It is recommended to use the same FFmpeg version as documented in the
\l{Qt Multimedia}{Qt Multimedia main page}.

The following paragraphs assumes that you store the FFmpeg source code under
\c{C:\FFmpeg\ffmpeg}.

\section1 Prerequisites

To build FFmpeg, these tools and packages are required:

\list
    \li Microsoft Visual Studio compiler (MSVC) or MinGW-w64.
    \li \l{https://www.msys2.org/}{MSYS2}.
    \li MSYS packages (perl, make, yasm, and diffutils).
\endlist

See \l{Qt for Windows - Building from Source} for recommended compilers that are
also supported by Qt. You can install a supported MinGW-w64 compiler using the
Qt Online Installer.

\section2 Installing MSYS2

To install MSYS2, you can:

\list
\li Use the \l{https://www.msys2.org/#installation}{MSYS2 installer}.
\li Use the \l{https://learn.microsoft.com/en-us/windows/package-manager/}{winget}
    package manager.

    \badcode
        winget install msys2.msys2
    \endcode

\endlist

The instructions in this document rely on MSYS2 installed to \c{C:\msys64\},
which is the default path when using \c{winget}.

Once installed, start the \c{MSYS2 MINGW64} shell from the Windows start menu,
and use it to install the necessary libraries.

\badcode
pacman -S --noconfirm perl make yasm diffutils
\endcode

Before building FFmpeg, MSYS2 must have a compiler in its \c PATH. This is done
differently for MSVC and MinGW-w64. Prepare the MSYS2 environment for the
compiler you use.

\section2 Preparing the MSYS environment for building with Mingw-w64

For MinGW-w64 installed under \c{C:\Qt\Tools\mingw1310_64\bin}, you can add it
to the \c PATH by exporting the PATH environment variable.

\badcode
$ export PATH=/c/Qt/Tools/mingw1310_64/bin:$PATH
\endcode

Note that it is also possible to install MinGW-w64 using pacman.

\badcode
$ pacman -S mingw-w64-x86_64-toolchain
\endcode

\section2 Preparing the MSYS2 environment for the MSVC

Building FFmpeg with the MSVC compiler under MSYS2 requires that MSYS2 is
started with the appropriate C and C++ compiler in the path. For a 64 bit build,
you can:

\list
\li Start \b{Visual Studio x64 Native Tools Command Prompt} from the Windows
    start menu.
\li Enable the MSVC compiler from an existing command prompt by running:

    \badcode
    "C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Auxiliary\Build\vcvarsall.bat" amd64
    \endcode
\endlist

From this command prompt, launch the MSYS2 shell with the options to select the
MINGW64 environment and enable path inheritance.

\badcode
C:\msys64\msys2_shell.cmd -mingw64 -full-path
\endcode

This launches a new shell window where you can verify that the MSVC compiler is
available.

\badcode
$ which cl.exe
\endcode

\section1 Configuring and building FFmpeg

From the prepared MSYS2 shell, navigate to the \c{/c/FFmpeg} directory. In this
directory, create a \c{build} directory which will contain the FFmpeg build
artifacts, and navigate into it.

\badcode
$ cd /c/FFmpeg/build
\endcode

To configure FFmpeg for building with MinGW-w64, run:

\badcode
$ ../ffmpeg/configure --prefix=../install --disable-doc --enable-network --enable-shared
\endcode

To configure FFmpeg for building with MSVC, specify the toolchain in addition to
the other command line options:

\badcode
$ ../ffmpeg/configure --prefix=../install --disable-doc --enable-network --enable-shared --toolchain=msvc
\endcode

The \c{--prefix} argument specifies a path where the FFmpeg development
libraries are installed after building. The documentation is not needed, but
network features should be enabled. To build FFmpeg as static libraries, omit
the \c{--enable-shared} option.

Once the \c configure command finishes, build and install FFmpeg using the
\c make command.

\badcode
$ make -j install
\endcode

If the build completes without errors, FFmpeg development libraries are
installed in the \c{C:\FFmpeg\install} directory. If you build Qt Multimedia,
this path is stored in the \c FFMPEG_DIR variable used when configuring Qt
Multimedia.

*/
