Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x264: add compilation script #84

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft

x264: add compilation script #84

wants to merge 10 commits into from

Conversation

niewim19
Copy link
Contributor

@niewim19 niewim19 commented Sep 2, 2024

Description

x264 is an open-source video codec available from here: https://www.videolan.org/developers/x264.html

This PR adds x264 script together with necessary patches in order to build this codec as CLI tool for video conversion. The codec was tested on ia32-generic-qemu and zyng7000-zturn platform. The testing is described in detail later in this description. This port creates static library and a CLI tool to run it. Static library is not installed into libs, but CLI application is installed.

Some good CLI parameters explanation: https://silentaperture.gitlab.io/mdbook-guide/encoding/x264.html

Motivation and Context

Patches

There are currently two patches necessary for the x264 to work:

input.patch

This patch deals with the fact, that current mmap() implementation in libphoenix needs size argument to be fully divisible by page size. This is discussed later in this PR. Issue related to this patch is: phoenix-rtos/phoenix-rtos-project#1155

configure.patch

This patch deals with the fact that Phoenix-RTOS does have malloc.h in libphoenix, but it is completely empty file (size=0), thus all usage of malloc.h functions fail. However configuration script assumes, that if LINUX platform is built, then malloc.h has to be present. Thats why this patch has to comment a line that defines HAVE_MALLOC_H` for compilation purpose.

Configure arguments

There are multiple arguments passed to configure script and some of them need an explanation:

  • --disable-avs, --disable-lavf, --disable-opencl force dynamic linking, so they are disabled.
  • --disable-asm allows for platform-specific assembly optimization, but it crashes the building process without any warning or error message. This was not investigated further.
  • --host=arm-linux - this flag tricks the compilation process that target platform has linux. This is later stripped to be just linux. The impact of this argument being used on non-arm platforms (ia32-generic) was not checked, however codec works on ia32-generic (this is still a weak proof...)
  • rest is self-explanatory or has sufficient explanation in configure --help

Warnings

The compilation process of this codec heavily pollutes the stdout with warnings of type:

./x264.h:40:4: warning: #warning You must include stdint.h or inttypes.h before x264.h [-Wcpp]
   40 | #  warning You must include stdint.h or inttypes.h before x264.h

This issue was not addressed.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Chore (refactoring, style fixes, git/CI config, submodule management, no code logic changes)

How Has This Been Tested?

This codec compilation was tested with success on ia32-generic-qemu and armv7a9-zynq7000-zturn platform.
A sample raw video file BUS_176x144_15_orig_01.yuv was used from https://engineering.purdue.edu/~reibman/ece634/Videos/YUV_videos/ because of it`s small size (~2MB).
The test command used was:

x264 --output BUS.h264 --fps 15 --preset ultrafast --input-res 176x144 BUS_176x144_15_orig_01.yuv

On both ia32 and zturn test platforms the codec exited with success. Only from zturn platform the converted video was downloaded back (using base64 text coding and UART transmission) and played with vlc video player. And there was a bus riding. So the video was converted successfully.

  • Already covered by automatic testing.
  • New test added: (add PR link here).
  • Tested by hand on: ia32-generic-qemu, armv7a9-zynq7000-zturn.

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing linter checks and tests passed.
  • My changes generate no new compilation warnings for any of the targets.

Special treatment

  • This PR needs additional PRs to work (list the PRs, preferably in merge-order).
  • I will merge this PR by myself when appropriate.

x264/patches/base.patch Outdated Show resolved Hide resolved
x264/patches/cpu.patch Outdated Show resolved Hide resolved
Copy link

github-actions bot commented Sep 2, 2024

Unit Test Results

7 700 tests  +240   6 985 ✅ +240   40m 3s ⏱️ + 3m 28s
  436 suites + 16     715 💤 ±  0 
    1 files   ±  0       0 ❌ ±  0 

Results for commit 6778959. ± Comparison against base commit b659aa2.

♻️ This comment has been updated with latest results.

@niewim19
Copy link
Contributor Author

niewim19 commented Sep 3, 2024

The issue with CLI silent-failing was traced to the mmap in input/input.c:x264_cli_mmap() function where there is an attempt to map a frame of video from input file. The mmap() fails which prevents the routine of accessing the first video frame. Overall this result in encoder to fail on x264.c:encode() in the:

    /* Encode frames */
    for( ; !b_ctrl_c && (i_frame < param->i_frame_total || !param->i_frame_total); i_frame++ )
    {
        if( filter.get_frame( opt->hin, &cli_pic, i_frame + opt->i_seek ) )
            break;

and this break does not set an error value to ret variable, thus failing with exit code 0.

An issue was made:

Adding files that allow for compilation of x264 codec with CLI tool.
Succesfull compialtion, unsuccesful in runtime, does not compress.

JIRA: PP-213
x264/patches/input.patch Outdated Show resolved Hide resolved
x264/patches/input.patch Outdated Show resolved Hide resolved
Adressing phoenix-rtos-project#1155 issue with mmap() `size` argument need
to be aligned to page size.

JIRA: PP-213
Previous implementation used `git clone` to obtain source code,
now it uses `wget` as with most other ports. No mirror is yet provided.

JIRA: PP-213
x264 extensively uses stack,
changed to 65536 bytes

JIRA: PP-213
Copy link
Member

@nalajcie nalajcie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is a draft, but still left some comments ;)

x264/patches/cpu.patch Outdated Show resolved Hide resolved
x264/patches/base.patch Outdated Show resolved Hide resolved

set -e

PREFIX_X264="${PREFIX_PROJECT}/phoenix-rtos-ports/x264"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to base new ports on this redesign: #78

I hope it will be merged soon @Darchiv

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for that! I didn`t notice this PR. I will wait for that PR to be merged and then redo this script.

x264/build.sh Outdated Show resolved Hide resolved
x264/build.sh Outdated Show resolved Hide resolved
x264/patches/input.patch Outdated Show resolved Hide resolved
x264/patches/input.patch Outdated Show resolved Hide resolved
linux distributions do have malloc.h which has memalign() function.
Phoenix-RTOS does have the malloc.h, but it is a stub.

JIRA: PP-213
Phoenix-RTOS does not support the x264 style of thread check,
so preliminary script can drop it and just use one thread.

This was made using patch, but there is configure option to disable it.

JIRA: PP-213
x264/build.sh Outdated Show resolved Hide resolved
@niewim19
Copy link
Contributor Author

niewim19 commented Sep 13, 2024

Tests were performed with and without --disable-asm flag for zynq7000-zturn platform. For decoding video with the following command:

/bin/x264 --output BUS.h264 --fps 15 --preset slow --input-res 176x144 etc/BUS_176x144_15_orig_01.yuv

the encoding speed was increased about 2x, from around 4,4 frames per second to around 9,1 frames per second.

For enabled asm the following frames per second encoding speeds were achieved for different presets:

  • ultrafast: 32,05
  • superfast: 30,09
  • veryfast: 27,17
  • faster: 21,16
  • fast: 17,09
  • medium: 14,98
  • slow: 9,12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants