From patchwork Sat Jul 6 14:18:02 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 13835 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 0B496449D3B for ; Sat, 6 Jul 2019 17:26:05 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E66DF68ACE4; Sat, 6 Jul 2019 17:26:04 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9D44968ACE4 for ; Sat, 6 Jul 2019 17:26:03 +0300 (EEST) Received: by mail-wm1-f52.google.com with SMTP id f17so12267976wme.2 for ; Sat, 06 Jul 2019 07:26:03 -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=w1lSeovRs94w85NpJYkQxCUXqr46BxjWCSebMsyfNNI=; b=DgH/KBrhjf0abD1GgeS04mHTH1Zol/EYh5pA1gSoB9ljvEYR8SMsbkLAWKmgLDxpW0 +mUmgTN3vW2mRqqbLWA7PR0y1FZbaH9iBVUFZCEIzwg5osThQS9YVRRhhvXS1lzRQQPY 8LxhOTue6e9ozTdp0dZC0c1CK/an9r230TV/7b9g3mHoH9lANvR2sMJPjooveE2rr61v LliwigoP4v9TgNKj3iYpAvJwhw+qXDS+xmuQjyZ8fiuIGJyLtBznmWyWPsK/chh8nxNC SyDIRme6mZdbRuLNLT0AmxMh3Nu7zHgANCio0jRk/8+FHYJae0kfSEnPxDkBlbi74S6L 3RLw== 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=w1lSeovRs94w85NpJYkQxCUXqr46BxjWCSebMsyfNNI=; b=RWy6/SJFD2S/GhLHrJLCXTVR0HVg9+P5XJ+fAI5834fwp3nUSeFHSYVh3wZpqF6JCk pvObSjWwOyrzsH4FrFnrH+RrP1qmt1yqmn0E7+bSnHtb1X9WL3ILYICJXtHi9Dy8QdxA pzAh7MlGbIkvgFfb+iC3SLXG8rLIlq2PSGQvrCUs7MJbawTsTRYVAgbOnECDSs2Q9wgZ 70OvkESc31V6r4y4tKJvr8oJeQFnZkFHuuy28di3Vi4Mpr+CRjLbb0oQS/hDDG/Epfdc cJ88WZaGCNRo/JiVhJSQemQ+RX7DQnpSHB9gQnUq+mzIbdE7+Js0lVNimh68/94c/Tge XIXQ== X-Gm-Message-State: APjAAAVDfYdkD/9BpJkDdOCd1LZVhhk7WvP2zGA+ZEZ0O0BsYY2HA/jb lgh5UhIjQbZMSrqAh/uPW4dpGunUX9k= X-Google-Smtp-Source: APXvYqy2kLi8CJcgNlJGRbdVOhrwDq0U+boYXePZP9tHMOO5G/ejHc5jO9mbS1tqWgSyP42u6RukIA== X-Received: by 2002:a1c:44d7:: with SMTP id r206mr8554179wma.164.1562422701258; Sat, 06 Jul 2019 07:18:21 -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.20 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Sat, 06 Jul 2019 07:18:20 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 6 Jul 2019 16:18:02 +0200 Message-Id: <20190706141804.12034-3-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 4/6] truehd_core: Miscellaneous improvements 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" 1. The loop counter of the substream_directory loop is always less than the number of substreams, yet within the loop it is checked whether it is less than FFMIN(3, s->hdr.num_substreams), although the check for < 3 would suffice. 2. In case the packet is a major sync packet, the last two bytes of the major sync structure were initialized to 0xff and then immediately overwritten afterwards without ever making use of the values just set. 3. When updating the parity_nibble during writing the new substream_directory, the parity_nibble is updated one byte at a time with bytes that might be read from the output packet's data. But one can do both bytes at the same time without resorting to the data just written by XOR'ing with the variable that contains the value that has just been written as a big endian number. This changes the intermediate value of parity_nibble, but in the end it just amounts to a reordering of the sum modulo two that will eventually be written as parity_nibble. Due to associativity and commutativity, this value is unchanged. 4. init_get_bits8 already checks that no overflow happens during the conversion of its argument from bytes to bits. ff_mlp_read_major_sync makes sure not to overread (the maximum size of a major_sync_info is 60 bytes anyway) and last_offset is < 2^13, so that no overflow in the calculation of size can happen, i.e. the check for whether size is >= 0 is unnecessary. But then size is completely unnecessary and can be removed. 5. In case the packet is just passed through, it is unnecessary to read the packet's dts. This is therefore postponed to when we know that the packet is not passed through. 6. Given that it seems overkill to use a bitreader just for one variable, the size of the input access unit is now read directly. 7. A substream's offset (of the end of the substream) is now stored as is (i.e. in units of words). These changes amount to a slight performance improvement: It improved from 5897 decicycles of ten runs with about 262144 runs each (including an insignificant amount -- about 20-25 usually of skips) to 5747 decicycles under the same conditions. Signed-off-by: Andreas Rheinhardt --- libavcodec/truehd_core_bsf.c | 39 ++++++++++++------------------------ 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/libavcodec/truehd_core_bsf.c b/libavcodec/truehd_core_bsf.c index f858c2d4d5..47684235db 100644 --- a/libavcodec/truehd_core_bsf.c +++ b/libavcodec/truehd_core_bsf.c @@ -42,12 +42,11 @@ static int truehd_core_filter(AVBSFContext *ctx, AVPacket *out) GetBitContext gbc; AccessUnit units[MAX_SUBSTREAMS]; AVPacket *in; - int ret, i, size, last_offset = 0; + int ret, i, last_offset = 0; int in_size, out_size; int have_header = 0; int substream_bits = 0; int end; - uint16_t dts; ret = ff_bsf_get_packet(ctx, &in); if (ret < 0) @@ -58,20 +57,12 @@ static int truehd_core_filter(AVBSFContext *ctx, AVPacket *out) goto fail; } - ret = init_get_bits(&gbc, in->data, 32); - if (ret < 0) - goto fail; - - skip_bits(&gbc, 4); - in_size = get_bits(&gbc, 12) * 2; + in_size = (AV_RB16(in->data) & 0xFFF) * 2; if (in_size < 4 || in_size > in->size) { ret = AVERROR_INVALIDDATA; goto fail; } - out_size = in_size; - dts = get_bits(&gbc, 16); - ret = init_get_bits8(&gbc, in->data + 4, in->size - 4); if (ret < 0) goto fail; @@ -91,26 +82,24 @@ static int truehd_core_filter(AVBSFContext *ctx, AVPacket *out) for (int j = 0; j < 4; j++) units[i].bits[j] = get_bits1(&gbc); - units[i].offset = get_bits(&gbc, 12) * 2; - if (i < FFMIN(s->hdr.num_substreams, 3)) { - last_offset = units[i].offset; + units[i].offset = get_bits(&gbc, 12); + if (i < 3) { + last_offset = units[i].offset * 2; substream_bits += 16; } if (units[i].bits[0]) { units[i].optional = get_bits(&gbc, 16); - if (i < FFMIN(s->hdr.num_substreams, 3)) + if (i < 3) substream_bits += 16; } } end = get_bits_count(&gbc); - size = ((end + 7) >> 3) + 4 + last_offset; - if (size >= 0 && size <= in->size) - out_size = size; + out_size = ((end + 7) >> 3) + 4 + last_offset; if (out_size < in_size) { int bpos = 0, reduce = (end - have_header * 28 * 8 - substream_bits) >> 4; - uint16_t parity_nibble = 0; + uint16_t parity_nibble, dts = AV_RB16(in->data + 2); uint16_t auheader; ret = av_new_packet(out, out_size); @@ -127,8 +116,6 @@ static int truehd_core_filter(AVBSFContext *ctx, AVPacket *out) 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; - out->data[26 + 4] = 0xff; - out->data[27 + 4] = 0xff; AV_WL16(out->data + 4 + 26, ff_mlp_checksum16(out->data + 4, 26)); } @@ -139,18 +126,18 @@ static int truehd_core_filter(AVBSFContext *ctx, AVPacket *out) substr_hdr |= (units[i].bits[1] << 14); substr_hdr |= (units[i].bits[2] << 13); substr_hdr |= (units[i].bits[3] << 12); - substr_hdr |= (units[i].offset / 2) & 0x0FFF; + substr_hdr |= units[i].offset; AV_WB16(out->data + have_header * 28 + 4 + bpos, substr_hdr); - parity_nibble ^= out->data[have_header * 28 + 4 + bpos++]; - parity_nibble ^= out->data[have_header * 28 + 4 + bpos++]; + parity_nibble ^= substr_hdr; + bpos += 2; if (units[i].bits[0]) { AV_WB16(out->data + have_header * 28 + 4 + bpos, units[i].optional); - parity_nibble ^= out->data[have_header * 28 + 4 + bpos++]; - parity_nibble ^= out->data[have_header * 28 + 4 + bpos++]; + parity_nibble ^= units[i].optional; + bpos += 2; } }