From patchwork Tue Mar 2 13:53:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 26061 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 7DB7E44896E for ; Tue, 2 Mar 2021 15:53:37 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4B9C068AA16; Tue, 2 Mar 2021 15:53:37 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f42.google.com (mail-ej1-f42.google.com [209.85.218.42]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B0DB2688057 for ; Tue, 2 Mar 2021 15:53:30 +0200 (EET) Received: by mail-ej1-f42.google.com with SMTP id w1so35392794ejf.11 for ; Tue, 02 Mar 2021 05:53:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=kHUcZJFmjYSnzFgeG8ZYyWER8rpBerY2j80pwt9oqWQ=; b=HE/YyjeevwKA8W59XfQ9GekG+ay8gstNbp+d03acBLZiIMWinXeTkh4lO4cSVY4wIW clsE4rA/4HqLJt6eB0a1lzjzBlRYUDqZYFDQAat6iq6xoZWp18TF8cAUyRP55IUeebSx IAXwhIOv/GxMKd+xcnNAcFr9XYpMNn8+3ya9OWTEpZNfuFimNsnTH0mg7bHonlSvxLBS cJvs9HqRddGinrGy8rNrL9ilb9tj+88asZsCa2oyzJWuk6RbUKhlL+uEDtQ8Iuaf6JYJ 7vvUKf6nRY0OsOtUCIEA92XgET82tmd05vR9bS9XQ94Zk+gNMdTmBb3HucXy8zFI7zL7 qcAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=kHUcZJFmjYSnzFgeG8ZYyWER8rpBerY2j80pwt9oqWQ=; b=ATYn33PKssqp2XUP8Egu5FT7H/Sz4AbzX15ovDir/oO/JzgC+vokQ4avOUyB+MLawm 1LOnOPz8YANTN0/+D9eLf3LjitDdVIXibxkVD2KM2G0gHMrsmTZosw6UV9Qh8cNKgb21 x3hLAM4DhfuwwC9SRfspJWY9LZRjhpZFPXlkPCgK9IiZGKM+sml8/xMv1uQ1WBm4c/21 BzA2LsOKIoDbr746II+9/Arx0Eaj71M8my5eGOPzi/3w5OI0Z94IO07T9OhphZzzqXCx x0LSrKctcp9gOgqCWZxCUD3wG/2wy0s1qwlSv1a6sNfQDNBx2kmFygJK3cmVTcn15388 xP/w== X-Gm-Message-State: AOAM531ls+dRQr7mMizz9sI91VTl336RcPavhs80JXKkdMsL0ELCuZTJ u9bLDjXENdmW6+eBqKOQOioGmNwLHQw= X-Google-Smtp-Source: ABdhPJyvRdgVjYB6RJoHkg8hra0HkCi0ya1SMnffB5SFzQy5ZkoYCFQ50rVt93uyqwnLGAynKHlfEQ== X-Received: by 2002:a17:906:5607:: with SMTP id f7mr10413960ejq.262.1614693210085; Tue, 02 Mar 2021 05:53:30 -0800 (PST) Received: from localhost.localdomain ([109.227.52.18]) by smtp.gmail.com with ESMTPSA id l61sm20358174edl.37.2021.03.02.05.53.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Mar 2021 05:53:29 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Tue, 2 Mar 2021 14:53:21 +0100 Message-Id: <20210302135321.2230-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH] avcodec/speedhq: fix decoding non 444 subsampling when width is not multiple of 16 X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Paul B Mahol --- libavcodec/speedhq.c | 79 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 2 deletions(-) diff --git a/libavcodec/speedhq.c b/libavcodec/speedhq.c index 759fc6dfc5..5861b7f6b5 100644 --- a/libavcodec/speedhq.c +++ b/libavcodec/speedhq.c @@ -276,6 +276,78 @@ static inline int decode_dct_block(const SHQContext *s, GetBitContext *gb, int l return 0; } +static int decode_speedhq_border(const SHQContext *s, GetBitContext *gb, AVFrame *frame, int field_number, int line_stride) +{ + int linesize_y = frame->linesize[0] * line_stride; + int linesize_cb = frame->linesize[1] * line_stride; + int linesize_cr = frame->linesize[2] * line_stride; + int linesize_a; + int ret; + + if (s->alpha_type != SHQ_NO_ALPHA) + linesize_a = frame->linesize[3] * line_stride; + + for (int y = 0; y < frame->height; y += 16 * line_stride) { + int last_dc[4] = { 1024, 1024, 1024, 1024 }; + uint8_t *dest_y, *dest_cb, *dest_cr, *dest_a; + uint8_t last_alpha[16]; + int x = frame->width - 8; + + dest_y = frame->data[0] + frame->linesize[0] * (y + field_number) + x; + if (s->subsampling == SHQ_SUBSAMPLING_420) { + dest_cb = frame->data[1] + frame->linesize[1] * (y/2 + field_number) + x / 2; + dest_cr = frame->data[2] + frame->linesize[2] * (y/2 + field_number) + x / 2; + } else if (s->subsampling == SHQ_SUBSAMPLING_422) { + dest_cb = frame->data[1] + frame->linesize[1] * (y + field_number) + x / 2; + dest_cr = frame->data[2] + frame->linesize[2] * (y + field_number) + x / 2; + } + if (s->alpha_type != SHQ_NO_ALPHA) { + memset(last_alpha, 255, sizeof(last_alpha)); + dest_a = frame->data[3] + frame->linesize[3] * (y + field_number) + x; + } + + if ((ret = decode_dct_block(s, gb, last_dc, 0, dest_y, linesize_y)) < 0) + return ret; + if ((ret = decode_dct_block(s, gb, last_dc, 0, dest_y + 8, linesize_y)) < 0) + return ret; + if ((ret = decode_dct_block(s, gb, last_dc, 0, dest_y + 8 * linesize_y, linesize_y)) < 0) + return ret; + if ((ret = decode_dct_block(s, gb, last_dc, 0, dest_y + 8 * linesize_y + 8, linesize_y)) < 0) + return ret; + if ((ret = decode_dct_block(s, gb, last_dc, 1, dest_cb, linesize_cb)) < 0) + return ret; + if ((ret = decode_dct_block(s, gb, last_dc, 2, dest_cr, linesize_cr)) < 0) + return ret; + + if (s->subsampling != SHQ_SUBSAMPLING_420) { + if ((ret = decode_dct_block(s, gb, last_dc, 1, dest_cb + 8 * linesize_cb, linesize_cb)) < 0) + return ret; + if ((ret = decode_dct_block(s, gb, last_dc, 2, dest_cr + 8 * linesize_cr, linesize_cr)) < 0) + return ret; + } + + if (s->alpha_type == SHQ_RLE_ALPHA) { + /* Alpha coded using 16x8 RLE blocks. */ + if ((ret = decode_alpha_block(s, gb, last_alpha, dest_a, linesize_a)) < 0) + return ret; + if ((ret = decode_alpha_block(s, gb, last_alpha, dest_a + 8 * linesize_a, linesize_a)) < 0) + return ret; + } else if (s->alpha_type == SHQ_DCT_ALPHA) { + /* Alpha encoded exactly like luma. */ + if ((ret = decode_dct_block(s, gb, last_dc, 3, dest_a, linesize_a)) < 0) + return ret; + if ((ret = decode_dct_block(s, gb, last_dc, 3, dest_a + 8, linesize_a)) < 0) + return ret; + if ((ret = decode_dct_block(s, gb, last_dc, 3, dest_a + 8 * linesize_a, linesize_a)) < 0) + return ret; + if ((ret = decode_dct_block(s, gb, last_dc, 3, dest_a + 8 * linesize_a + 8, linesize_a)) < 0) + return ret; + } + } + + return 0; +} + static int decode_speedhq_field(const SHQContext *s, const uint8_t *buf, int buf_size, AVFrame *frame, int field_number, int start, int end, int line_stride) { int ret, slice_number, slice_offsets[5]; @@ -283,6 +355,7 @@ static int decode_speedhq_field(const SHQContext *s, const uint8_t *buf, int buf int linesize_cb = frame->linesize[1] * line_stride; int linesize_cr = frame->linesize[2] * line_stride; int linesize_a; + GetBitContext gb; if (s->alpha_type != SHQ_NO_ALPHA) linesize_a = frame->linesize[3] * line_stride; @@ -304,7 +377,6 @@ static int decode_speedhq_field(const SHQContext *s, const uint8_t *buf, int buf } for (slice_number = 0; slice_number < 4; slice_number++) { - GetBitContext gb; uint32_t slice_begin, slice_end; int x, y; @@ -333,7 +405,7 @@ static int decode_speedhq_field(const SHQContext *s, const uint8_t *buf, int buf dest_a = frame->data[3] + frame->linesize[3] * (y + field_number); } - for (x = 0; x < frame->width; x += 16) { + for (x = 0; x < frame->width - 8 * (s->subsampling != SHQ_SUBSAMPLING_444); x += 16) { /* Decode the four luma blocks. */ if ((ret = decode_dct_block(s, &gb, last_dc, 0, dest_y, linesize_y)) < 0) return ret; @@ -402,6 +474,9 @@ static int decode_speedhq_field(const SHQContext *s, const uint8_t *buf, int buf } } + if (s->subsampling != SHQ_SUBSAMPLING_444 && (frame->width & 15)) + return decode_speedhq_border(s, &gb, frame, field_number, line_stride); + return 0; }