Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp3471347ybf; Tue, 3 Mar 2020 06:38:05 -0800 (PST) X-Google-Smtp-Source: ADFU+vvkkbEA1IHqPD3vi94+PbqjPlWJoogYjh4RF7HJxG+aeA39wTXidHSNfqFoyMP2hThumKZ2 X-Received: by 2002:a9d:2f05:: with SMTP id h5mr3663513otb.316.1583246285609; Tue, 03 Mar 2020 06:38:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583246285; cv=none; d=google.com; s=arc-20160816; b=lQLRtlBMsIEXDX0hUy36qOqTnSBjfAvKrRWtayvO2RNH7z+p7QAdRNUoNcZey/x9jl xnGWD3cKrBroi6lmGEK9PSxMcGQsBDYwWISf713L9vCPPUelscrwnkBoZgvRtWv3yAyh ccfUMKnSNy5PyQdns/o96Xakoa4jaYHeJaOWaS9vgUORjNCjEhRsoZKbLpVLkcc020Ja s1MF2OZUGl9gQzRSdaJGYExzcQS035XTk0CRH+Pb0vu/jLTlVbATjAmQPyUn5n6OCSaW f07fHdXtYr8gU7yy7upjgnfRABBuMuQbGlS6g78X196frBZi8RCW1/Bz6GX7WPaqEPot uEaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=I21Kq4omIiuvyI+q1sEWfUXp3zFBaxsCmxFd55nT6Nc=; b=PHCjOGB3AdFoqVZ3ZWpo7I/ES8EcOz3NsADQvzYDnK/UubbC3OzXvqh7tSaRRn0lf+ ss+7wKO2DtcvusxEh2suBRVX4t+/d+FmtUX7/lpgWT8kZkxXKIOzP0MJlYCwgF2mKhmD 25Q1KudywLca12RnWxwafZVq6kn2m80T2FAT0Way5U4KloDE7YXA9Yrxpb7hCkrMIFQ5 yLuWZ1ziNtGaa1QjZ6RwYSi8IgZePne/WrU9wwYFpTm3kjN6YBGddkApxgLvMFX+rBFv oH2VMAtUNHusnkeOJ7U5g5peMtMIiBtDPfCNA1OWFe/zlT/PiU1IXcMj6I8zVVdgT+pJ 02Pg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@baylibre-com.20150623.gappssmtp.com header.s=20150623 header.b=nxLcwmlS; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v6si8371388oia.200.2020.03.03.06.37.53; Tue, 03 Mar 2020 06:38:05 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@baylibre-com.20150623.gappssmtp.com header.s=20150623 header.b=nxLcwmlS; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729617AbgCCOhm (ORCPT + 99 others); Tue, 3 Mar 2020 09:37:42 -0500 Received: from mail-wr1-f66.google.com ([209.85.221.66]:34310 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728741AbgCCOhm (ORCPT ); Tue, 3 Mar 2020 09:37:42 -0500 Received: by mail-wr1-f66.google.com with SMTP id z15so4672724wrl.1 for ; Tue, 03 Mar 2020 06:37:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=I21Kq4omIiuvyI+q1sEWfUXp3zFBaxsCmxFd55nT6Nc=; b=nxLcwmlSwzbT9LjOoX3uevnYPtP2GITTh8LVaVdm6+RT3dnCxKjfFh8kyPnMpwiOxM 5uss3JhQqj46EyDXlUg0TVsrPdBxi2quB02VHNy/TymVj5XdZLnMmz+LZ1pBgGeH4FPu LcJyK1iz5U5bqtSMJ85zECaQL51uk1TCsfnL8e17vQX4HZlf+jnWI2w2GxlAvk2TNJmU jUncowMn3vqHnJ+DPD2FOV4GhP496L2uej+ESflr3VXxXQ2OXC4psjVsfwQrlwTyozmd QN6hIUzvYX1L9oaYO69VDdkuw5Nvvgjafghzqluhjkk6RDsdJeWuqKI5PlB01BfXUIw4 pM2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=I21Kq4omIiuvyI+q1sEWfUXp3zFBaxsCmxFd55nT6Nc=; b=I/MYwxak3YzUa9qzx1vXnQVh+7vwuduKYD0g9JKWoXtxO94uo4WrnTI6Rx8bKqRnk9 a0ZjHbLA7JbXJfqJ/sd7iAs5u4bfPnWnYbaOoZd0wJK7x2E6O6maVPw946IdWG2g/IQ/ jya45fh0qgj5oNmVBg0E6fneLhmhKLerAEb54esz8X2dunmutcm23GQwa+gkSfuh4d4I EeNvTywk5BC1x7uA5UIlOzHwxkWbo44OrsCvngLTXt4cbJjPWLhsaSBa6/fuHnZ4RUMd CkZ+xq6yzp7ZT1vSbEq8QsDEoXse5miPZDQvNrZiiucUoRFuViWpGZH5JY93oWtl/rBo RSYQ== X-Gm-Message-State: ANhLgQ0we8difT4eP7wcov2fw9cfsv1UKnnZtqc+tOpPqu8HLt+QFgwQ lxhhUku0Id5pBmen4GI4y5/dvw== X-Received: by 2002:adf:c445:: with SMTP id a5mr5752925wrg.14.1583246258140; Tue, 03 Mar 2020 06:37:38 -0800 (PST) Received: from bender.baylibre.local (laubervilliers-658-1-213-31.w90-63.abo.wanadoo.fr. [90.63.244.31]) by smtp.gmail.com with ESMTPSA id l4sm4652779wmf.38.2020.03.03.06.37.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Mar 2020 06:37:37 -0800 (PST) From: Neil Armstrong To: mchehab@kernel.org, hans.verkuil@cisco.com Cc: Maxime Jourdan , linux-media@vger.kernel.org, linux-amlogic@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Neil Armstrong , Kevin Hilman Subject: [PATCH v6 4/5] media: meson: vdec: add VP9 input support Date: Tue, 3 Mar 2020 15:37:31 +0100 Message-Id: <20200303143732.762-5-narmstrong@baylibre.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20200303143732.762-1-narmstrong@baylibre.com> References: <20200303143732.762-1-narmstrong@baylibre.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Maxime Jourdan Amlogic VP9 decoder requires an additional 16-byte payload before every frame header. The source buffer is updated in-place, then given to the Parser FIFO DMA. The FIFO DMA copies the blocks into the 16MiB parser ring buffer, then parses and copies the slice into the decoder "workspace". Signed-off-by: Maxime Jourdan Signed-off-by: Neil Armstrong Tested-by: Kevin Hilman --- drivers/staging/media/meson/vdec/esparser.c | 150 +++++++++++++++++++- 1 file changed, 146 insertions(+), 4 deletions(-) diff --git a/drivers/staging/media/meson/vdec/esparser.c b/drivers/staging/media/meson/vdec/esparser.c index adc5c1e81a4c..4a9aad3fafeb 100644 --- a/drivers/staging/media/meson/vdec/esparser.c +++ b/drivers/staging/media/meson/vdec/esparser.c @@ -52,6 +52,7 @@ #define PARSER_VIDEO_HOLE 0x90 #define SEARCH_PATTERN_LEN 512 +#define VP9_HEADER_SIZE 16 static DECLARE_WAIT_QUEUE_HEAD(wq); static int search_done; @@ -74,14 +75,121 @@ static irqreturn_t esparser_isr(int irq, void *dev) return IRQ_HANDLED; } +/** + * VP9 frame headers need to be appended by a 16-byte long + * Amlogic custom header + */ +static int vp9_update_header(struct amvdec_core *core, struct vb2_buffer *buf) +{ + u8 *dp; + u8 marker; + int dsize; + int num_frames, cur_frame; + int cur_mag, mag, mag_ptr; + int frame_size[8], tot_frame_size[8]; + int total_datasize = 0; + int new_frame_size; + unsigned char *old_header = NULL; + + dp = (uint8_t *)vb2_plane_vaddr(buf, 0); + dsize = vb2_get_plane_payload(buf, 0); + + if (dsize == vb2_plane_size(buf, 0)) { + dev_warn(core->dev, "%s: unable to update header\n", __func__); + return 0; + } + + marker = dp[dsize - 1]; + if ((marker & 0xe0) == 0xc0) { + num_frames = (marker & 0x7) + 1; + mag = ((marker >> 3) & 0x3) + 1; + mag_ptr = dsize - mag * num_frames - 2; + if (dp[mag_ptr] != marker) + return 0; + + mag_ptr++; + for (cur_frame = 0; cur_frame < num_frames; cur_frame++) { + frame_size[cur_frame] = 0; + for (cur_mag = 0; cur_mag < mag; cur_mag++) { + frame_size[cur_frame] |= + (dp[mag_ptr] << (cur_mag * 8)); + mag_ptr++; + } + if (cur_frame == 0) + tot_frame_size[cur_frame] = + frame_size[cur_frame]; + else + tot_frame_size[cur_frame] = + tot_frame_size[cur_frame - 1] + + frame_size[cur_frame]; + total_datasize += frame_size[cur_frame]; + } + } else { + num_frames = 1; + frame_size[0] = dsize; + tot_frame_size[0] = dsize; + total_datasize = dsize; + } + + new_frame_size = total_datasize + num_frames * VP9_HEADER_SIZE; + + if (new_frame_size >= vb2_plane_size(buf, 0)) { + dev_warn(core->dev, "%s: unable to update header\n", __func__); + return 0; + } + + for (cur_frame = num_frames - 1; cur_frame >= 0; cur_frame--) { + int framesize = frame_size[cur_frame]; + int framesize_header = framesize + 4; + int oldframeoff = tot_frame_size[cur_frame] - framesize; + int outheaderoff = oldframeoff + cur_frame * VP9_HEADER_SIZE; + u8 *fdata = dp + outheaderoff; + u8 *old_framedata = dp + oldframeoff; + + memmove(fdata + VP9_HEADER_SIZE, old_framedata, framesize); + + fdata[0] = (framesize_header >> 24) & 0xff; + fdata[1] = (framesize_header >> 16) & 0xff; + fdata[2] = (framesize_header >> 8) & 0xff; + fdata[3] = (framesize_header >> 0) & 0xff; + fdata[4] = ((framesize_header >> 24) & 0xff) ^ 0xff; + fdata[5] = ((framesize_header >> 16) & 0xff) ^ 0xff; + fdata[6] = ((framesize_header >> 8) & 0xff) ^ 0xff; + fdata[7] = ((framesize_header >> 0) & 0xff) ^ 0xff; + fdata[8] = 0; + fdata[9] = 0; + fdata[10] = 0; + fdata[11] = 1; + fdata[12] = 'A'; + fdata[13] = 'M'; + fdata[14] = 'L'; + fdata[15] = 'V'; + + if (!old_header) { + /* nothing */ + } else if (old_header > fdata + 16 + framesize) { + dev_dbg(core->dev, "%s: data has gaps, setting to 0\n", + __func__); + memset(fdata + 16 + framesize, 0, + (old_header - fdata + 16 + framesize)); + } else if (old_header < fdata + 16 + framesize) { + dev_err(core->dev, "%s: data overwritten\n", __func__); + } + old_header = fdata; + } + + return new_frame_size; +} + /* Pad the packet to at least 4KiB bytes otherwise the VDEC unit won't trigger * ISRs. * Also append a start code 000001ff at the end to trigger * the ESPARSER interrupt. */ -static u32 esparser_pad_start_code(struct amvdec_core *core, struct vb2_buffer *vb) +static u32 esparser_pad_start_code(struct amvdec_core *core, + struct vb2_buffer *vb, + u32 payload_size) { - u32 payload_size = vb2_get_plane_payload(vb, 0); u32 pad_size = 0; u8 *vaddr = vb2_plane_vaddr(vb, 0); @@ -186,13 +294,35 @@ esparser_queue(struct amvdec_session *sess, struct vb2_v4l2_buffer *vbuf) int ret; struct vb2_buffer *vb = &vbuf->vb2_buf; struct amvdec_core *core = sess->core; + struct amvdec_codec_ops *codec_ops = sess->fmt_out->codec_ops; u32 payload_size = vb2_get_plane_payload(vb, 0); dma_addr_t phy = vb2_dma_contig_plane_dma_addr(vb, 0); + u32 num_dst_bufs = 0; u32 offset; u32 pad_size; - if (esparser_vififo_get_free_space(sess) < payload_size) + /* + * When max ref frame is held by VP9, this should be -= 3 to prevent a + * shortage of CAPTURE buffers on the decoder side. + * For the future, a good enhancement of the way this is handled could + * be to notify new capture buffers to the decoding modules, so that + * they could pause when there is no capture buffer available and + * resume on this notification. + */ + if (sess->fmt_out->pixfmt == V4L2_PIX_FMT_VP9) { + if (codec_ops->num_pending_bufs) + num_dst_bufs = codec_ops->num_pending_bufs(sess); + + num_dst_bufs += v4l2_m2m_num_dst_bufs_ready(sess->m2m_ctx); + if (sess->fmt_out->pixfmt == V4L2_PIX_FMT_VP9) + num_dst_bufs -= 3; + + if (esparser_vififo_get_free_space(sess) < payload_size || + atomic_read(&sess->esparser_queued_bufs) >= num_dst_bufs) + return -EAGAIN; + } else if (esparser_vififo_get_free_space(sess) < payload_size) { return -EAGAIN; + } v4l2_m2m_src_buf_remove_by_buf(sess->m2m_ctx, vbuf); @@ -206,7 +336,19 @@ esparser_queue(struct amvdec_session *sess, struct vb2_v4l2_buffer *vbuf) vbuf->field = V4L2_FIELD_NONE; vbuf->sequence = sess->sequence_out++; - pad_size = esparser_pad_start_code(core, vb); + if (sess->fmt_out->pixfmt == V4L2_PIX_FMT_VP9) { + payload_size = vp9_update_header(core, vb); + + /* If unable to alter buffer to add headers */ + if (payload_size == 0) { + amvdec_remove_ts(sess, vb->timestamp); + v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR); + + return 0; + } + } + + pad_size = esparser_pad_start_code(core, vb, payload_size); ret = esparser_write_data(core, phy, payload_size + pad_size); if (ret <= 0) { -- 2.22.0