From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 307FE40187 for ; Sun, 4 Dec 2022 21:36:01 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4000A68B9E7; Sun, 4 Dec 2022 23:35:58 +0200 (EET) Received: from mail.overt.org (mail.overt.org [72.14.183.176]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 71E07688288 for ; Sun, 4 Dec 2022 23:35:51 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=overt.org; s=mail; t=1670189748; bh=izPAUIvn3Z8bwv6b8xpaaa0135Bz4sOLT4jKU5yV9rs=; h=From:To:Cc:Subject:Date:From; b=FifjWUttGtdjPAj94IKr3DG4ODbMYYJxMCMgcRns2JfCZk2bHWyvpwbNXZ5mpyJ5u wRwt9+wg4dbouCM5mKrqnP/4kr0VmahiTDr7WphKKWNfzTn6OK6dE6VMBMTUnDlrFJ Hib3xlrscEIO0Mux6OFieoXuWlkeT1Vixfo77xwBgcGoK3uN6Ryun6Z0wTqOjgQagI rtgCrEumnDpKkZ+8zyjXsc53W394eHGRiuk9uVc/OPLdv9LXYeecUvjE9FLwV7jdH7 DXV31mXfTfvCBTMddNVk71qR4Z2qPvc1nHtdeVl7usPv0gWAgPD3heoc5UIsJBKNu1 kt0U7yfRe1Q+g== Received: from authenticated-user (mail.overt.org [72.14.183.176]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mail.overt.org (Postfix) with ESMTPSA id 8B534629F5; Sun, 4 Dec 2022 15:35:48 -0600 (CST) From: Philip Langdale To: ffmpeg-devel@ffmpeg.org Date: Sun, 4 Dec 2022 13:34:58 -0800 Message-Id: <20221204213458.79978-1-philipl@overt.org> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] lavu/pixdesc: handle xv30be in av_[read|write]_image_line X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Philip Langdale Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: xv30be is an obnoxious format that I shouldn't have included in the first place. xv30 packs 3 10bit channels into 32bits and while our byte-oriented logic can handle Little Endian correctly, it cannot handle Big Endian. To avoid that, I marked xv30be as a bitstream format, but while that didn't produce FATE errors, it turns out that the existing read/write code silently produces incorrect results, which can be revealed via ubsan. In all likelyhood, the correct fix here is to remove the format. As this format is only used by Intel vaapi, it's only going to show up in LE form, so we could just drop the BE version. But I don't want to deal with creating a hole in the pixfmt list and all the weirdness that comes from that. Instead, I decided to write the correct read/write code for it. And that code isn't too bad, as long as it's specialised for this format, as the channels are all bit-aligned inside a 32bit word. Signed-off-by: Philip Langdale --- libavutil/pixdesc.c | 70 ++++++++++++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 20 deletions(-) diff --git a/libavutil/pixdesc.c b/libavutil/pixdesc.c index ca3e204a0b..62a2ae08d9 100644 --- a/libavutil/pixdesc.c +++ b/libavutil/pixdesc.c @@ -46,19 +46,35 @@ void av_read_image_line2(void *dst, uint32_t *dst32 = dst; if (flags & AV_PIX_FMT_FLAG_BITSTREAM) { - int skip = x * step + comp.offset; - const uint8_t *p = data[plane] + y * linesize[plane] + (skip >> 3); - int shift = 8 - depth - (skip & 7); + if (depth == 10) { + // Assume all channels are packed into a 32bit value + const uint8_t *byte_p = data[plane] + y * linesize[plane]; + const uint32_t *p = (uint32_t *)byte_p; - while (w--) { - int val = (*p >> shift) & mask; - if (read_pal_component) - val = data[1][4*val + c]; - shift -= step; - p -= shift >> 3; - shift &= 7; - if (dst_element_size == 4) *dst32++ = val; - else *dst16++ = val; + while (w--) { + int val = AV_RB32(p); + val = (val >> comp.offset) & mask; + if (read_pal_component) + val = data[1][4*val + c]; + if (dst_element_size == 4) *dst32++ = val; + else *dst16++ = val; + p++; + } + } else { + int skip = x * step + comp.offset; + const uint8_t *p = data[plane] + y * linesize[plane] + (skip >> 3); + int shift = 8 - depth - (skip & 7); + + while (w--) { + int val = (*p >> shift) & mask; + if (read_pal_component) + val = data[1][4*val + c]; + shift -= step; + p -= shift >> 3; + shift &= 7; + if (dst_element_size == 4) *dst32++ = val; + else *dst16++ = val; + } } } else { const uint8_t *p = data[plane] + y * linesize[plane] + @@ -109,15 +125,29 @@ void av_write_image_line2(const void *src, const uint16_t *src16 = src; if (flags & AV_PIX_FMT_FLAG_BITSTREAM) { - int skip = x * step + comp.offset; - uint8_t *p = data[plane] + y * linesize[plane] + (skip >> 3); - int shift = 8 - depth - (skip & 7); + if (depth == 10) { + // Assume all channels are packed into a 32bit value + const uint8_t *byte_p = data[plane] + y * linesize[plane]; + uint32_t *p = (uint32_t *)byte_p; + int offset = comp.offset; + uint32_t mask = ((1ULL << depth) - 1) << offset; - while (w--) { - *p |= (src_element_size == 4 ? *src32++ : *src16++) << shift; - shift -= step; - p -= shift >> 3; - shift &= 7; + while (w--) { + uint16_t val = src_element_size == 4 ? *src32++ : *src16++; + AV_WB32(p, (AV_RB32(p) & ~mask) | (val << offset)); + p++; + } + } else { + int skip = x * step + comp.offset; + uint8_t *p = data[plane] + y * linesize[plane] + (skip >> 3); + int shift = 8 - depth - (skip & 7); + + while (w--) { + *p |= (src_element_size == 4 ? *src32++ : *src16++) << shift; + shift -= step; + p -= shift >> 3; + shift &= 7; + } } } else { int shift = comp.shift; -- 2.37.2 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".