Opened 2 years ago

Closed 18 months ago

#11357 closed defect (fixed)

Inconsistent when decoding an H.264 bitstream with bi-prediction weighted prediction enabled on ARM and x86 devices

Reported by: Bin Peng Owned by: Michael Niedermayer <michael@niedermayer.cc>
Priority: normal Component: avcodec
Version: git-master Keywords: H264 decoder arm
Cc: Bin Peng Blocked By:
Blocking: Reproduced by developer: no
Analyzed by developer: no

Description (last modified by Bin Peng)

Summary of the bug:

When decoding a bitstream with weighted-bipred enabled, the output of FFmpeg on ARM and x86 platforms may differ.

How to reproduce:

using ffmpeg git-master built on aarch64 and test on aarch64 devices
% ffmpeg -i inter_weighted_bipred2.264 -f framemd5 -y md5_arm

using ffmpeg git-master built on x86 and test on x86 devices
% ffmpeg -i inter_weighted_bipred2.264 -f framemd5 -y md5_x86

md5_arm and md5_x86 would be different, but we expect the same decode result 
on different CPUs with the same input bit-stream.

Attachments (1)

inter_weighted_bipred2.264​ (5.1 KB ) - added by Bin Peng 2 years ago.
h264 bitstream with weighted-bipred enabled

Download all attachments as: .zip

Change History (9)

by Bin Peng, 2 years ago

Attachment: inter_weighted_bipred2.264​ added

h264 bitstream with weighted-bipred enabled

comment:1 by Bin Peng, 2 years ago

Description: modified (diff)

comment:2 by Bin Peng, 2 years ago

The reason for the inconsistency is that the value of STRIDE_ALIGN differs between platforms. On x86 platforms, it is greater than 16 (typically 32 or 64), while on ARM platforms, it is 16.
​https://github.com/FFmpeg/FFmpeg/blob/master/libavcodec/internal.h#L39

The STRIDE_ALIGN is set to the buffer stride of temporary buffers for U and V components in mc_part_weighted.

uint8_t *tmp_cb = sl->bipred_scratchpad;
uint8_t *tmp_cr = sl->bipred_scratchpad + (16 << pixel_shift);

​https://github.com/FFmpeg/FFmpeg/blob/master/libavcodec/h264_mb.c#L410

But the offset of temporary buffers for U and V components is hardcoded to 16.

If the buffer stride is 32 or 64 (as on x86 platforms), the U and V pixels can be interleaved row by row without overlapping, resulting in correct output. However, on ARM platforms where the stride is 16, the V component will overwrite part of the U component's pixels, leading to incorrect predicted pixels.

comment:3 by Bin Peng, 2 years ago

Summary: H.264 decoder bug on Arm → Inconsistency on ARM when decoding weighted-bipred enabled H.264 bitstream

comment:4 by Bin Peng, 2 years ago

Keywords: arm added

comment:5 by Bin Peng, 2 years ago

Summary: Inconsistency on ARM when decoding weighted-bipred enabled H.264 bitstream → Inconsistency on ARM and X86 when decoding H.264 bitstream with weighted-bipred enabled

comment:6 by Bin Peng, 2 years ago

Summary: Inconsistency on ARM and X86 when decoding H.264 bitstream with weighted-bipred enabled → Results may be inconsistent when decoding an H.264 bitstream with bi-prediction weighted prediction enabled on ARM and x86 devices

comment:7 by Bin Peng, 22 months ago

Summary: Results may be inconsistent when decoding an H.264 bitstream with bi-prediction weighted prediction enabled on ARM and x86 devices → Inconsistent when decoding an H.264 bitstream with bi-prediction weighted prediction enabled on ARM and x86 devices

comment:8 by Michael Niedermayer <michael@niedermayer.cc>, 18 months ago

Owner: set to Michael Niedermayer <michael@niedermayer.cc>
Resolution: → fixed
Status: new → closed

In 74fd2c3d/ffmpeg:

avcodec/h264_mb: Fix tmp_cr for arm

When decoding a bitstream with weighted-bipred enabled,
the results on ARM and x86 platforms may differ.

The reason for the inconsistency is that the value of
STRIDE_ALIGN differs between platforms. And STRIDE_ALIGN
is set to the buffer stride of temporary buffers for U
and V components in mc_part_weighted.

If the buffer stride is 32 or 64 (as on x86 platforms),
the U and V pixels can be interleaved row by row without
overlapping, resulting in correct output.
However, on ARM platforms where the stride is 16,
the V component did overwrite part of the U component's pixels,
leading to incorrect predicted pixels.

The bug can be reproduced by the following bitstream.

​https://trac.ffmpeg.org/attachment/ticket/11357/inter_weighted_bipred2.264

Fixes: ticket 11357
Commit-msg-mostly-by: Bin Peng <​pengbin@visionular.com>
Reviewed-by: Bin Peng <​pengbin@visionular.com>
Signed-off-by: Michael Niedermayer <​michael@niedermayer.cc>

Note: See TracTickets for help on using tickets.