From patchwork Sat Jul 6 14:18:04 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 13837 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 0E58E449D3B for ; Sat, 6 Jul 2019 17:26:15 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id EB5F668AD1E; Sat, 6 Jul 2019 17:26:14 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f67.google.com (mail-wm1-f67.google.com [209.85.128.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 97C1768AD19 for ; Sat, 6 Jul 2019 17:26:13 +0300 (EEST) Received: by mail-wm1-f67.google.com with SMTP id v19so12251450wmj.5 for ; Sat, 06 Jul 2019 07:26:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=aD0d2Vz5fESu4tu5UMP9cPQ4Kv+09KW1fDgjQOqu9mw=; b=qIHOOpubf03zO6Zf3CqL79OYrLS6XUE/N0fTA7oSRLx3M5EOg6vRu4mldFNchVpxiG OF8U2Rvnzyu5ldSdNiUhjcQCjhX/J203G64Ai74miN9hl85BfVJGyOYmPizcnTSKXRyq wxkgMUvvSI4pKY7OtbhWsKOxq9KOiDKPF9/XCY+lVTasBZktcqEB64zzdmAooO+z+46M uFZVh+4FnKchrviDazP949PLXYOvkM/uQC4pVSC39KcOAjPUMfgo8/NJa5/WdKvqxxhq 78MCe++PB5KGrIGFoRsia+sMIzLOGKAozqiByw5iNQ9SlHU2dCzJ0ozFWitv2jZSiblf Ao9Q== 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=aD0d2Vz5fESu4tu5UMP9cPQ4Kv+09KW1fDgjQOqu9mw=; b=hWy/NfiI2bDFOccEHgQfUNsH9qetOKiz1oGcmGUSpOa39TqgD/QkyqUINNI9ovJiJS NOmA7c5/XM8sY5QXN2NwdlQ+JCXJxmAeFPFv6hLbPEFknVB7yB76UfinHhWjKJXVoUD7 H1ekGvuSlIkwPj9Fw2b6vJsTTOvY1SJr/vdyU/IpQlXhrJg0OnlFne7YVf62Qx9XIpGc RwfrA/fveIxBHZMp7LbVqM0QF47osA2eGBFZaeDSm776Liw6kimUmmK60eKu1nBZkVwo gB6UlcOOOyzsvjCoB9SWXel2jLQ+iSJ1/2Fw9tFUHEo0jJ00Iz8P9eDjz3c1sX/KsLw7 qn7g== X-Gm-Message-State: APjAAAVoaD5YtEX6KV4BJtzkVJL/e5ZVrv1DBj/P1MAKsoqZ71UJnYNv LmkbVWCuiZajk0yGPrQLT0GartHQJOM= X-Google-Smtp-Source: APXvYqwKYNSpdYStrjk8TXwujg/p/U0B5rOgOQoF/NH1DVXHyORI1fJQI4/UbZ4iiYKYtvhfseUJOA== X-Received: by 2002:a1c:dc46:: with SMTP id t67mr7667093wmg.159.1562422702906; Sat, 06 Jul 2019 07:18:22 -0700 (PDT) Received: from localhost.localdomain (ipbcc08b8f.dynamic.kabel-deutschland.de. [188.192.139.143]) by smtp.gmail.com with ESMTPSA id z7sm1331566wrh.67.2019.07.06.07.18.22 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Sat, 06 Jul 2019 07:18:22 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 6 Jul 2019 16:18:04 +0200 Message-Id: <20190706141804.12034-5-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190706141804.12034-1-andreas.rheinhardt@gmail.com> References: <20190706135911.11986-1-andreas.rheinhardt@gmail.com> <20190706141804.12034-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 6/6] truehd_core: Switch to in-place modifications 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 Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" The truehd_core bitstream filter decreases the sizes of the major_sync_info structure (if present), of the substream_directory and of the substreams themselves. As a consequence, there is enough space available in front of the actual substream data for the new header, so that one only needs to modify the header in front of the actual data (which apart from shrinking is left untouched) and the packet's size and buffer pointer (after having made sure that the packet is writable). This and switching to bsf_get_packet_ref also removed the need for having separate packets for in- and output. Even if the input is not writable, there are noticable performance improvements: The average of 10 iterations of processing a file with 262144 runs each (inlcuding about 20 skips per iteration) went down from 5669 to 4362 decicycles. If the input is writable, it goes down to 1363 decicycles. Signed-off-by: Andreas Rheinhardt --- libavcodec/truehd_core_bsf.c | 64 +++++++++++++++++------------------- 1 file changed, 31 insertions(+), 33 deletions(-) diff --git a/libavcodec/truehd_core_bsf.c b/libavcodec/truehd_core_bsf.c index 8ea80d3015..dbd05b34ca 100644 --- a/libavcodec/truehd_core_bsf.c +++ b/libavcodec/truehd_core_bsf.c @@ -36,34 +36,33 @@ typedef struct TrueHDCoreContext { MLPHeaderInfo hdr; } TrueHDCoreContext; -static int truehd_core_filter(AVBSFContext *ctx, AVPacket *out) +static int truehd_core_filter(AVBSFContext *ctx, AVPacket *pkt) { TrueHDCoreContext *s = ctx->priv_data; GetBitContext gbc; AccessUnit units[MAX_SUBSTREAMS]; - AVPacket *in; int ret, i, last_offset = 0; int in_size, out_size; int have_header = 0; int substream_bytes = 0; int end; - ret = ff_bsf_get_packet(ctx, &in); + ret = ff_bsf_get_packet_ref(ctx, pkt); if (ret < 0) return ret; - if (in->size < 4) { + if (pkt->size < 4) { ret = AVERROR_INVALIDDATA; goto fail; } - in_size = (AV_RB16(in->data) & 0xFFF) * 2; - if (in_size < 4 || in_size > in->size) { + in_size = (AV_RB16(pkt->data) & 0xFFF) * 2; + if (in_size < 4 || in_size > pkt->size) { ret = AVERROR_INVALIDDATA; goto fail; } - ret = init_get_bits8(&gbc, in->data + 4, in->size - 4); + ret = init_get_bits8(&gbc, pkt->data + 4, pkt->size - 4); if (ret < 0) goto fail; @@ -99,27 +98,31 @@ static int truehd_core_filter(AVBSFContext *ctx, AVPacket *out) out_size = end + 4 + last_offset; if (out_size < in_size) { int bpos = 0, reduce = end - have_header * 28 - substream_bytes; - uint16_t parity_nibble, dts = AV_RB16(in->data + 2); + uint16_t parity_nibble, dts = AV_RB16(pkt->data + 2); uint16_t auheader; + uint8_t header[28]; - av_assert1(reduce % 2 == 0); + av_assert1(reduce >= 0 && reduce % 2 == 0); - ret = av_new_packet(out, out_size); + if (have_header) { + memcpy(header, pkt->data + 4, 28); + header[16] = (header[16] & 0x0c) | (FFMIN(s->hdr.num_substreams, 3) << 4); + header[17] &= 0x7f; + header[25] &= 0xfe; + AV_WL16(header + 26, ff_mlp_checksum16(header, 26)); + } + + pkt->data += reduce; + out_size -= reduce; + pkt->size = out_size; + + ret = av_packet_make_writable(pkt); if (ret < 0) goto fail; - AV_WB16(out->data + 2, dts); + AV_WB16(pkt->data + 2, dts); parity_nibble = dts; - out->size -= reduce; - parity_nibble ^= out->size / 2; - - if (have_header) { - memcpy(out->data + 4, in->data + 4, 28); - out->data[16 + 4] = (out->data[16 + 4] & 0x0c) | (FFMIN(s->hdr.num_substreams, 3) << 4); - out->data[17 + 4]&= 0x7f; - out->data[25 + 4] = out->data[25 + 4] & 0xfe; - AV_WL16(out->data + 4 + 26, ff_mlp_checksum16(out->data + 4, 26)); - } + parity_nibble ^= out_size / 2; for (i = 0; i < FFMIN(s->hdr.num_substreams, 3); i++) { uint16_t substr_hdr = 0; @@ -130,13 +133,13 @@ static int truehd_core_filter(AVBSFContext *ctx, AVPacket *out) substr_hdr |= (units[i].bits[3] << 12); substr_hdr |= units[i].offset; - AV_WB16(out->data + have_header * 28 + 4 + bpos, substr_hdr); + AV_WB16(pkt->data + have_header * 28 + 4 + bpos, substr_hdr); parity_nibble ^= substr_hdr; bpos += 2; if (units[i].bits[0]) { - AV_WB16(out->data + have_header * 28 + 4 + bpos, units[i].optional); + AV_WB16(pkt->data + have_header * 28 + 4 + bpos, units[i].optional); parity_nibble ^= units[i].optional; bpos += 2; @@ -147,22 +150,17 @@ static int truehd_core_filter(AVBSFContext *ctx, AVPacket *out) parity_nibble ^= parity_nibble >> 4; parity_nibble &= 0xF; - memcpy(out->data + have_header * 28 + 4 + bpos, - in->data + 4 + end, - out_size - (4 + end)); auheader = (parity_nibble ^ 0xF) << 12; - auheader |= (out->size / 2) & 0x0fff; - AV_WB16(out->data, auheader); + auheader |= (out_size / 2) & 0x0fff; + AV_WB16(pkt->data, auheader); - ret = av_packet_copy_props(out, in); - } else { - av_packet_move_ref(out, in); + if (have_header) + memcpy(pkt->data + 4, header, 28); } fail: if (ret < 0) - av_packet_unref(out); - av_packet_free(&in); + av_packet_unref(pkt); return ret; }