Opened 6 months ago

Closed 5 months ago

#10689 closed enhancement (fixed)

[Android] Provide options to tweak h264_mediacodec encoder quality vs bitrate vs speed

Reported by: bubbleguuum Owned by:
Priority: normal Component: avcodec
Version: unspecified Keywords:
Cc: Blocked By:
Blocking: Reproduced by developer: no
Analyzed by developer: no

Description (last modified by bubbleguuum)

As of FFmpeg v6.1, there is very little that you can do to tweak the video quality output of the h264_mediacodec encoder vs the bitrate required (think the equivalent of the libx264 -preset option).

For improving quality, the only way I found is to increase the bitrate with the -b:v parameter.
When that parameter is specified, the encoder will honor it: it acts as a max bitrate setting.
The higher the bitrate specified with -b:v, the higher the quality.

In my experience, it requires a quite high bitrate for an acceptable video quality. For example, for 1080p output, at least 10Mbit is required for acceptable quality. At 5Mbit, output is very blocky. In that regard, it seems to operate very much like the libx264 ultrafast or superfast presets.

It would be great to have more control on quality (vs encoding speed and bitrate). I'm not super familiar with the MediaCodec encoder API but I believe there are settings to tweak quality. Although AFAIK, no h264 encoder implementation on Android support constant quality (thus '-bitrate_mode cq' encoder specific option is not supported) so something like -crf cannot probably be implemented. Only VBR and CBR are broadly supported.

Change History (4)

comment:1 by bubbleguuum, 6 months ago

Description: modified (diff)
Summary: [Android] Provide options to tweak h264_mediacodec encoder encoding quality vs bitrate vs encoding speed[Android] Provide options to tweak h264_mediacodec encoder quality vs bitrate vs speed

comment:2 by quinkblack, 5 months ago

https://ffmpeg.org/pipermail/ffmpeg-devel/2023-December/318497.html

For example:

./ffmpeg_g -hwaccel mediacodec -i input.mp4 -c:v hevc_mediacodec -bitrate_mode cq -global_quality:v 80 -pix_fmt nv12 output.mp4

AOSP doc doesn't say what's valid range for quality. It's device and codec specific. Although there is API to query that information, it's doesn't help much for user.

Looks like 0-100 is common for video codec. You can tweak and try from 20 to 80.

Last edited 5 months ago by quinkblack (previous) (diff)

comment:3 by bubbleguuum, 5 months ago

Thank you for this improvement.

Though only the hevc encoder seems to support cq and I have to use the h264 encoder in my app for compatibility reasons, so cannot use cq.
Controlling quality with -b:v works, just at the expense of higher bitrates.

comment:4 by quinkblack, 5 months ago

Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.