Files
FFmpeg/libavcodec/h2645_vui.c
Jun Zhao e598463b3d avcodec/h2645_vui: interpret a degenerate SAR as unspecified
Per ITU-T H.264 (ISO/IEC 14496-10) Annex E.2.1 and ITU-T H.265
(ISO/IEC 23008-2) Annex E.3.1, when sar_width or sar_height is zero
the sample aspect ratio shall be considered unspecified. Internally
ffmpeg represents an unspecified SAR as 0/1, while fractions with a
zero denominator are not handled properly (den=0 is silently changed
to den=1 in h264_ps.c, turning an invalid 20480/0 into a "valid" but
impossibly extreme 20480/1); so we bridge the gap by replacing x/0
with 0/1 at the VUI parsing layer.

An av_log warning is added so an invalid SAR in the bitstream is
diagnosed rather than silently overwritten.

This fixes a problem with some video files provided by game
OddBallers when executed with Wine/Proton, which report SAR 20480/0.

Based on patch by Giovanni Mascellani <gmascellani@codeweavers.com>.
Fixes: ticket #23321

Signed-off-by: Jun Zhao <barryjzhao@tencent.com>
2026-06-07 18:55:16 +00:00

101 lines
4.2 KiB
C

/*
* Common H.264/HEVC VUI Parameter decoding
*
* Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
* Copyright (C) 2012 - 2013 Guillaume Martres
* Copyright (C) 2012 - 2013 Mickael Raulet
* Copyright (C) 2012 - 2013 Gildas Cocherel
* Copyright (C) 2013 Vittorio Giovara
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "libavutil/log.h"
#include "libavutil/pixdesc.h"
#include "get_bits.h"
#include "golomb.h"
#include "h2645data.h"
#include "h2645_vui.h"
#define EXTENDED_SAR 255
void ff_h2645_decode_common_vui_params(GetBitContext *gb, H2645VUI *vui, void *logctx)
{
av_log(logctx, AV_LOG_DEBUG, "Decoding VUI\n");
vui->aspect_ratio_info_present_flag = get_bits1(gb);
if (vui->aspect_ratio_info_present_flag) {
vui->aspect_ratio_idc = get_bits(gb, 8);
if (vui->aspect_ratio_idc < FF_ARRAY_ELEMS(ff_h2645_pixel_aspect))
vui->sar = ff_h2645_pixel_aspect[vui->aspect_ratio_idc];
else if (vui->aspect_ratio_idc == EXTENDED_SAR) {
vui->sar.num = get_bits(gb, 16);
vui->sar.den = get_bits(gb, 16);
if (!vui->sar.num || !vui->sar.den) {
av_log(logctx, AV_LOG_WARNING,
"Invalid SAR %d/%d in bitstream, treating as unspecified.\n",
vui->sar.num, vui->sar.den);
vui->sar = (AVRational){ 0, 1 };
}
} else
av_log(logctx, AV_LOG_WARNING,
"Unknown SAR index: %u.\n", vui->aspect_ratio_idc);
} else
vui->sar = (AVRational){ 0, 1 };
vui->overscan_info_present_flag = get_bits1(gb);
if (vui->overscan_info_present_flag)
vui->overscan_appropriate_flag = get_bits1(gb);
vui->video_signal_type_present_flag = get_bits1(gb);
if (vui->video_signal_type_present_flag) {
vui->video_format = get_bits(gb, 3);
vui->video_full_range_flag = get_bits1(gb);
vui->colour_description_present_flag = get_bits1(gb);
if (vui->colour_description_present_flag) {
vui->colour_primaries = get_bits(gb, 8);
vui->transfer_characteristics = get_bits(gb, 8);
vui->matrix_coeffs = get_bits(gb, 8);
// Set invalid values to "unspecified"
if (vui->colour_primaries == AVCOL_PRI_RESERVED0 ||
vui->colour_primaries == AVCOL_PRI_RESERVED ||
!av_color_primaries_name(vui->colour_primaries))
vui->colour_primaries = AVCOL_PRI_UNSPECIFIED;
if (vui->transfer_characteristics == AVCOL_TRC_RESERVED0 ||
vui->transfer_characteristics == AVCOL_TRC_RESERVED ||
!av_color_transfer_name(vui->transfer_characteristics))
vui->transfer_characteristics = AVCOL_TRC_UNSPECIFIED;
if (vui->matrix_coeffs == AVCOL_SPC_RESERVED ||
!av_color_space_name(vui->matrix_coeffs))
vui->matrix_coeffs = AVCOL_SPC_UNSPECIFIED;
}
}
vui->chroma_loc_info_present_flag = get_bits1(gb);
if (vui->chroma_loc_info_present_flag) {
vui->chroma_sample_loc_type_top_field = get_ue_golomb_31(gb);
vui->chroma_sample_loc_type_bottom_field = get_ue_golomb_31(gb);
if (vui->chroma_sample_loc_type_top_field <= 5U)
vui->chroma_location = vui->chroma_sample_loc_type_top_field + 1;
else
vui->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
} else
vui->chroma_location = AVCHROMA_LOC_LEFT;
}