# Build and Run x265 codec on Arm servers

## In this learning path

- [Introduction](https://learn.arm.com/learning-paths/servers-and-cloud-computing/codec/)
- [Build and Run x265 codec on Arm servers](https://learn.arm.com/learning-paths/servers-and-cloud-computing/codec/x265/)
- [Next Steps](https://learn.arm.com/learning-paths/servers-and-cloud-computing/codec/_next-steps/)

## Install necessary software packages
`x265` is an open-source H.265/HEVC encoder that offers very high compression efficiency and performance. There have been significant efforts to optimize the open-source libx265 implementation of the H.265 encoder on Arm Neoverse platforms which supports Neon instructions. The optimized code is available on [Bitbucket](https://bitbucket.org/multicoreware/x265_git/).

Install GCC for your Arm Linux distribution. Refer to the [install guide](https://learn.arm.com/install-guides/gcc/native/) for additional information.

Install CMake and the other dependencies:
```
sudo apt update
sudo apt install wget git cmake cmake-curses-gui build-essential -y
```
If you are running on x86, also install `yasm`
```
sudo apt install  yasm -y
```

## Download and build x265 source
Clone the repository and navigate to the `linux` directory.
```
git clone https://bitbucket.org/multicoreware/x265_git.git
cd x265_git/build/linux
```
Run the interactive bash script to change the default flags for the build. Then, run `make`.
```
./make-Makefiles.bash
make
```
> **Note:** If you encounter an error regarding an unknown value, you can change the `ENABLE_NEON_I8MM` flag to `OFF` using the interactive bash script in the previous step.
```
__output__ error: unknown value ‘armv9-a+i8mm+sve2’ for ‘-march’
```
For detailed instructions refer to this [README](https://bitbucket.org/multicoreware/x265_git/src/master/build/README.txt).

## Download video streams to run x265 on and measure performance
Use two sequences from the [Alliance for Open Media codec test set](https://media.xiph.org/video/aomctc/test_set/). These Y4M files are designed for codec testing and can be read directly by the `x265` command-line application.

Download the 360p and 1080p sequences:
```
wget https://media.xiph.org/video/aomctc/test_set/a4_360p/BlueSky_360p25_v2.y4m
wget https://media.xiph.org/video/aomctc/test_set/a2_2k/Motorcycle_1920x1080_30fps_8bit.y4m
```
The Blue Sky sequence has no use restrictions or copyright. The Motorcycle sequence is copyright Neil Birkbeck and licensed under [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/).

## Run x265 on the sample videos
To benchmark the performance of `x265` over 50 frames of the 360p video, run:
```
./x265 --y4m --preset ultrafast --frames 50 BlueSky_360p25_v2.y4m --output outfile-360.265 --frame-threads 1 --no-wpp --pools ','
```
To benchmark the performance using the 1080p video, run:
```
./x265 --y4m --preset ultrafast --frames 50 Motorcycle_1920x1080_30fps_8bit.y4m --output outfile-1080.265 --frame-threads 1 --no-wpp --pools ','
```
You can vary the preset settings on the different resolution images and measure the impact on performance.

For full usage instructions, refer to the [command line documentation](https://x265.readthedocs.io/en/master/cli.html), or see the help:
```
./x265 --help
```

## View results
At the beginning of each run, confirm that the output identifies the input as `y4m`, reports the expected resolution, and lists the Arm CPU capabilities used by the encoder. The capabilities vary by server.

The output from the 1080p encode is similar to:
```
__output__ y4m  [info]: 1920x1080 fps 30/1 i420p8 sar 1:1 frames 0 - 49 of 130
__output__ raw  [info]: output file: outfile-1080.265
__output__ x265 [info]: HEVC encoder version 4.2+37-b81f650e2
__output__ x265 [info]: build info [Linux][GCC 13.3.0][64 bit] 8bit
__output__ x265 [info]: using cpu capabilities: NEON SVE SVE2 Neon_DotProd Neon_I8MM SVE2_BitPerm
__output__ x265 [info]: Main profile, Level-4 (Main tier)
```
At the end of each run, `x265` reports that it encoded 50 frames and displays the encoding frame rate in frames per second. It also creates `outfile-360.265` or `outfile-1080.265` in the current directory.

The encoding results are suitable for checking that the workflow completes.
