Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.48 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1793639008; b=dMnGV/iC6zXyWm1QWXPWFstudpcPM/RbvH4Manthea+C0f8p1hvtniedZV0nJz/zGnje ZBwZy9pq3YbQms5JypX5RQRYEFz/RmFXvlgkcD5vz/v/2Cpp4z5Jxnw1kJZE6ZMR0Mtpj wORlMEOGdPquK5V88FpTTsLYx7BrvRdpH7TX76DY92nE59P6V+BboyG8cQyF2ac7g0Go1 L6JKJQmWA+MAmvcqDYMBJC4n3Q7T3RlBaI/ac6qU7I0MQtRaeJf/1+PgPeZ4tixcQRKUz Bc4lQd18ZB4HtlYk2Rd6YQQw8kUSdHcW71IAxgC8UPHC+EaRSa8sjL+J09DKDNNMvcQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793639008; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=ifkMz93jDcWFK+TugNjNJ0UJbkiZmfoGYcbnJzIO7y8=; b=uMPZ7WViE0egVFyodvtlobpRz6QQZxG5noh8Dc592GsRngS80+zB8lpighRFlvCK7uA5 M9b32iXBMNXVYrNvmhF/kVHjWEWxzstn5OB5au+ysGKLr5F5J0oIP5E4EH0UNVZSMkiI/ SUt7eM3KvaiAXJGTzA3cv9z2j5jSjj32WNjhdAb27Ur7V32xx34DRvX342xzPnam1iB3z f7rybQOqfEhCfPNfxg4OkktUz2akDHQeL7ExDuZdrT63WWSOSlZD+X/EPKwXIxkOe2RFH VANUR672q9neFwQc10fVYKdb77MgR6NuKs04Q0aF+1h+8hBI+g6OUvZnG2Z96v9k+hA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=qVk3pJjW; arc=none smtp.remote-ip=209.85.221.48 Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4B660606F3F1 for ; Mon, 2 Nov 2026 17:03:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4B660606F3F1 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=qVk3pJjW Received: by mail-wr1-f48.google.com with SMTP id ffacd0b85a97d-48dfdd0678dso2284830f8f.0 for ; Mon, 02 Nov 2026 09:03:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1793639006; x=1794243806; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=ifkMz93jDcWFK+TugNjNJ0UJbkiZmfoGYcbnJzIO7y8=; b=qVk3pJjWENdYxwRng4MR2O7ujCm/hvY6qCx7utAd/gfiAMP+K3FE3rVOWLCrSxbdTU reuh38kVNX4AvroOcx2TMCKTtTQFsf8Wsn7+khj/zzo3xIOd8W1dh8JV0fANS6p4mVQg Pka6wUN/So+D6yd0kpbQTrBzu7in8Sx50qlMFIreS7RFbryWOu9wMty5AL/RNBh9m2Yo p8TZt4lTXGgIwvzN6N9iBRlXnJbLQ8sm47kZXEGracmf+6U5JQvV5K99dze+mYDSTnLy 1KBctuXdAkUMSTPyJXNxPphBOjbTxbScAHWDn2cwDP/MK+G9+PzxJDqSo67uFPdTvRsD +Ovg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793639006; x=1794243806; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=ifkMz93jDcWFK+TugNjNJ0UJbkiZmfoGYcbnJzIO7y8=; b=PZaZdPSZkiXpBKCX747Pkj6qDtf/b97MxAO+YsO7jlzMKYIYmY1JF1ZNx4fD1WSiYr slxJE7tfIBYEL5bmyLZNFjhnIMBwSPJYcpH7PVfq9DZkFe7yAH+EUwsWBKT1SwLgYfxA n6PwnR13WqutOHXWFOZyesk19nsnWG6+QCsNFo6CyrbTx4Q51i+bgefNgpY+Y1P19Gxq JaGnhzwRFYP+mdxXWTWIG+/NWhDadIb8UcATcJaPYaCT1nV3QIkbyr1bcv6P+zuTuuY/ 4WJjlSs5MoUZ1QHEsriMWArr3+xwBw9EesMo2NKtEB5PyviTsHZx31tLkYAm+CEQwFXy cg9Q== X-Forwarded-Encrypted: i=1; AKwUvBxXLCgWEvsb0Amfja11TTWuwygpKOQvj5OkCJdekQjUVaq258z+IN9uPQpyJqcqMFk42M1p3d5OOwxreQ==@vger.kernel.org X-Gm-Message-State: APuU4Z+3OQ22YKGa5qqp2SO3zHU3iMvYaNJCJGHJoXbDqVUZzbWSQ9+b lqB4MC8xuP0QXqdVdC1yvcEAMCo7Z1f/YidAfvDJz0ebHxxYPF5cKHMLhHIW5w== X-Gm-Gg: AYBFou3ARslVGaXDIaU6lGZcR/U2HjSRV88poa+BtzHPDr0MihS50YRWnevhKIB/Zlq sMZw4bmZk7LRRNe1Q98mIE7KWqqGYf209MDulDGBO4pOJ+fJrFUb2L9v45e1X/m2CcR/S6123aO woAZbFIbANGXsSWu56j3dULXY9W4R7VRNk+F6QMtia5XYrffWNWaCQ/od9BOIbLlCXCzwQiD8EF YydQSghHTsG03O9aE/RCe01PF62sdmlt/pXoA5FEJHo8OQJ9VvCDEbe75bzJgft0BLz1eVvz0fl BF20tMY6Dm5gVgAF9Ga6/+zNGnLr/Fp7XSZ+LvOQa8G9rrZentZkkDjSNZtsFVjm51H8Wr0j7fY SMTW6ZzOV9GJevOt3dKUu7Bem/wEn9wapSsmRg8xyb2V9kPo/hoGotp1rBmPrbB2wq+bNxef7QI YxmdG7/fvVz905Uz9WGg58OuooXxecpxpLo/9zdRo7ILufSwNI8ynpklksFtFCq8PRD1jp803cP d7mPpak+aCk6WNgwMzwV4a7rCkXQYrxq4VTCLBETq+hqMcGHSd5vyHJjZKw2Jf5PDV1Q0/Qn6gV 7Q2jHrZyYSWwSElmM58tTD6Lg5jwoqtxjc43rMCkq5z9DQ+AGgUBLoz5npqDzdJWsj0iBOERbj0 KI9RUSUhKYecE/BNOfV0KI/HXx8EnrXRBObeczEEo3M758Gojc0sZVL5ujVhw9jgLS4p2SpbXT/ r3oZUvnIIpJ0LDtgoWAv5mFZUm7vvWD3Sh8l9m8f7jw5WffOqr36y3MEPJtPY704XveIyQAUdKQ dh0vnO0N1sxwG0xk7INC2/uKBv1/A9Z1rLqlwizRS6XF2BU0A== X-Received: by 2002:a5d:5f84:0:b0:48d:eec6:308b with SMTP id ffacd0b85a97d-48e6e4f561bmr25233093f8f.21.1793639005534; Mon, 02 Nov 2026 09:03:25 -0800 (PST) Received: from jernej-laptop (194-152-27-116.dynamic.telemach.net. [194.152.27.116]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-48e71a4d3cesm23362309f8f.7.2026.11.02.09.03.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Nov 2026 09:03:25 -0800 (PST) From: Jernej Skrabec To: Mauro Carvalho Chehab , Hans Verkuil Cc: Maxime Ripard , Paul Kocialkowski , Greg Kroah-Hartman , Chen-Yu Tsai , Samuel Holland , Nicolas Dufresne , linux-media@vger.kernel.org, linux-staging@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-sunxi@lists.linux.dev, linux-kernel@vger.kernel.org, Jernej Skrabec Subject: [PATCH 05/10] staging: media: cedrus: mpeg2: derive the field order of field pictures Date: Mon, 2 Nov 2026 18:02:58 +0100 Message-ID: <20261102170303.1973892-6-jernej.skrabec@gmail.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261102170303.1973892-1-jernej.skrabec@gmail.com> References: <20261102170303.1973892-1-jernej.skrabec@gmail.com> X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The hardware uses the top field first bit of the picture header to tell the first field of a frame from the second one. However, top_field_first is always 0 in field pictures, so userspace passes V4L2_MPEG2_PIC_FLAG_TOP_FIELD_FIRST cleared, and the second field of every top field first pair is predicted from the wrong reference. Detect the second field in the driver instead: it has the opposite parity of the previous field and is decoded into the same capture buffer. This works with unmodified userspace. Fixes: 50e761516f2b ("media: platform: Add Cedrus VPU decoder driver") Signed-off-by: Jernej Skrabec --- drivers/staging/media/sunxi/cedrus/cedrus.h | 5 +++ .../staging/media/sunxi/cedrus/cedrus_mpeg2.c | 32 +++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/drivers/staging/media/sunxi/cedrus/cedrus.h b/drivers/staging/media/sunxi/cedrus/cedrus.h index 54f867949fba..a523b56a9b8c 100644 --- a/drivers/staging/media/sunxi/cedrus/cedrus.h +++ b/drivers/staging/media/sunxi/cedrus/cedrus.h @@ -144,6 +144,11 @@ struct cedrus_ctx { void *entry_points_buf; dma_addr_t entry_points_buf_addr; } h265; + struct { + bool pair_pending; + unsigned int pair_dst; + u8 pair_structure; + } mpeg2; struct { unsigned int last_frame_p_type; unsigned int last_filter_type; diff --git a/drivers/staging/media/sunxi/cedrus/cedrus_mpeg2.c b/drivers/staging/media/sunxi/cedrus/cedrus_mpeg2.c index ca66a3c6b830..4c8accee5389 100644 --- a/drivers/staging/media/sunxi/cedrus/cedrus_mpeg2.c +++ b/drivers/staging/media/sunxi/cedrus/cedrus_mpeg2.c @@ -58,7 +58,7 @@ static int cedrus_mpeg2_setup(struct cedrus_ctx *ctx, struct cedrus_run *run) struct vb2_queue *vq; const u8 *matrix; unsigned int i; - bool field; + bool field, tff; u32 reg; seq = run->mpeg2.sequence; @@ -87,6 +87,26 @@ static int cedrus_mpeg2_setup(struct cedrus_ctx *ctx, struct cedrus_run *run) cedrus_write(dev, VE_DEC_MPEG_IQMINPUT, reg); } + /* + * top_field_first is always 0 in field pictures, but the hardware + * uses this bit to tell the first field of a frame from the second. + * A field is the second one when it has the opposite parity of the + * previous field and is decoded into the same capture buffer. + */ + if (pic->picture_structure == V4L2_MPEG2_PIC_FRAME) { + tff = !!(pic->flags & V4L2_MPEG2_PIC_FLAG_TOP_FIELD_FIRST); + ctx->codec.mpeg2.pair_pending = false; + } else { + bool second = ctx->codec.mpeg2.pair_pending && + ctx->codec.mpeg2.pair_structure != pic->picture_structure && + ctx->codec.mpeg2.pair_dst == run->dst->vb2_buf.index; + + tff = (pic->picture_structure == V4L2_MPEG2_PIC_TOP_FIELD) != second; + ctx->codec.mpeg2.pair_pending = !second; + ctx->codec.mpeg2.pair_structure = pic->picture_structure; + ctx->codec.mpeg2.pair_dst = run->dst->vb2_buf.index; + } + /* Set MPEG picture header. */ reg = VE_DEC_MPEG_MP12HDR_SLICE_TYPE(pic->picture_coding_type); @@ -96,7 +116,7 @@ static int cedrus_mpeg2_setup(struct cedrus_ctx *ctx, struct cedrus_run *run) reg |= VE_DEC_MPEG_MP12HDR_F_CODE(1, 1, pic->f_code[1][1]); reg |= VE_DEC_MPEG_MP12HDR_INTRA_DC_PRECISION(pic->intra_dc_precision); reg |= VE_DEC_MPEG_MP12HDR_INTRA_PICTURE_STRUCTURE(pic->picture_structure); - reg |= VE_DEC_MPEG_MP12HDR_TOP_FIELD_FIRST(pic->flags & V4L2_MPEG2_PIC_FLAG_TOP_FIELD_FIRST); + reg |= VE_DEC_MPEG_MP12HDR_TOP_FIELD_FIRST(tff); reg |= VE_DEC_MPEG_MP12HDR_FRAME_PRED_FRAME_DCT(pic->flags & V4L2_MPEG2_PIC_FLAG_FRAME_PRED_DCT); reg |= VE_DEC_MPEG_MP12HDR_CONCEALMENT_MOTION_VECTORS(pic->flags & V4L2_MPEG2_PIC_FLAG_CONCEALMENT_MV); reg |= VE_DEC_MPEG_MP12HDR_Q_SCALE_TYPE(pic->flags & V4L2_MPEG2_PIC_FLAG_Q_SCALE_TYPE); @@ -186,6 +206,13 @@ static int cedrus_mpeg2_setup(struct cedrus_ctx *ctx, struct cedrus_run *run) return 0; } +static int cedrus_mpeg2_start(struct cedrus_ctx *ctx) +{ + memset(&ctx->codec.mpeg2, 0, sizeof(ctx->codec.mpeg2)); + + return 0; +} + static void cedrus_mpeg2_trigger(struct cedrus_ctx *ctx) { struct cedrus_dev *dev = ctx->dev; @@ -203,5 +230,6 @@ struct cedrus_dec_ops cedrus_dec_ops_mpeg2 = { .irq_disable = cedrus_mpeg2_irq_disable, .irq_status = cedrus_mpeg2_irq_status, .setup = cedrus_mpeg2_setup, + .start = cedrus_mpeg2_start, .trigger = cedrus_mpeg2_trigger, }; -- 2.43.0