Skip to content

Eynnzerr/h264_qpblock

Repository files navigation

h264_qpblock

What's this repository for?

This library allows you to encode regions of interest(ROIs) with multiple target QP value, instead of frame-level uniform QP adopted in traditional CQP H264.

H264 may achieve different qp for macroblocks by adaptive quantization, however sometimes you may not be satisfied with the decision made by encoder, then you can use this library to modify it.

What's good point of this repository?

This library is inspired by h264-roi, but with more advantages:

  • Remove dependency on libchaos and use purely C++ standard library instead
  • Use latest version of FFmpeg and x264, which means no deprecated API is used
  • Input mp4 for output mp4 instead of h264 for h264
  • Fix the bug that will lead to incorrect frame number count in h264-roi
  • Fix the bug that relative path will be parsed as absolute path in h264-roi
  • Fix the bug that B frames are not applied

h264-roi is a brilliant work, but the author stopped maintenance long time ago. So this repository is given birth to.

Dependency

  • FFmpeg 5.0.1 or above
  • x264 latest
  • libraries on which FFmpeg/x264 depend

Quick start

  1. Compile FFmpeg and x264 for headers and static/shared library. Note that FFmpeg should be configured to support x264(--enable-libx264)
  2. Create a new empty folder:
mkdir build
  1. use cmake to compile the project. Don't forget to edit CMakeLists.txt to change include & link path to your own path:
cmake ..
make
  1. modify permission for the executable if possible:
chmod 777 h264_qpblock
  1. set qp for regions in forms like:
h264_qpblock input_mp4_path output_mp4_path -baseqp 0~51 x1,y1,x2,y2:qp x3,y3,x4,y4:qp ...

Example:

h264_qpblock /home/h264-qpblock/in.mp4 /home/h264-qpblock/out.mp4 -baseqp 5 100,100,800,600:45 250,350,400,700:20

Note that regions are denoted by: top_left_x,top_left_y,bottom_right_x,bottom_right_y. Values of QP range from 0 to 51 as usual.

Warning

This library cannot 100% accurately set QP for regions since doing so needs modification of source code of x264. Though we do have one implementation for that, it needs a modified version of x264 for building. Besides, uniform qp in one region is not recommended since it conflicts with adaptive quantization of x264. So for now we may not bring it here.

As a workaround, this library changes QP of frame regions by adding offset based on decisions made by x264, which is adopted by h264-roi, but the overall effect is still good.

Other words

  • You can disable log output by setting log level in function main.
  • Any input video that has pixel format of yuv420p and is supported by FFmpeg is valid. Other pixel format may be implemented later.
  • Currently the frame resolution of output video is the same as that of input video. Custom resolution option may be implemented later.
  • This is my first time learning and trying C++. Forgive me for any unreasonable code if I've made ;)

About

H264 re-encoding with preset QP for ROIs

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published