From patchwork Sat Oct 22 10:32:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 38901 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:4a86:b0:9d:28a3:170e with SMTP id fn6csp1222921pzb; Sat, 22 Oct 2022 03:34:27 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5i2EfClOUl/7BuS11TwcVWeadD+jxyR5Hu5h95R9PtlhusDEJOcSw+PDDV1zPr35lzofiQ X-Received: by 2002:a17:907:3188:b0:741:4bf7:ec1a with SMTP id xe8-20020a170907318800b007414bf7ec1amr19849206ejb.448.1666434867617; Sat, 22 Oct 2022 03:34:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666434867; cv=none; d=google.com; s=arc-20160816; b=goRu+s8VpNThijgIrjC4hHrw/y6bGqbZOs1saNhwGKOi5VMK1d7KNz1VqB2jNzSLdH dq5nEqBUHPL9vH8IfUGKhosBwVmig1UinQ7fp00NDjzZQvm7MCafcKx2eN03pQhI7nOg YckWzirKAKeb32tw34KxARe8jH/rZdHh4Z8gWsXV0N4tBrSNFFt9yne+989rJ5tci1Az +c2jmOC8svwuGNBdAPUS8h0xtWNV5e+c4MouiqWDCm9if69VtnErb1DPeTNkmOq2Q7M3 TlLgcoCam3nXmHfdUgENYUAGk+6/qf2Ax9Gf8WbuwoP/1af2jZalokHiZaQgR43RfxZC R6Jg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=0WzoowcFp/qmQVTlmF33XtqtuKrNPyItrBSBpG7tgcw=; b=UNchk75NltjCQqQiN8tsFEBN77/gCZZ5rCvH/vIM5UzjLmk7TmXKNJaNb7NhbPyf68 13tXrT3UC3P7H5ZPVX7d7J5v6huE/JaOPG0tWOA54mFZkg2dxrMdVDLxNv2P+foeqL0K Yi7lxFYHnWifGLgzZyJOG+cRLea2Wy/t9P0jsIbbwo996S5lU03WwSomP8m04H9KPcqv D376cNCLE13L9Ze2SBt3ZCoazWylh+gcbs/vYSBmt9Bl6dXgIWsWeZufpsMMHqoeTwZR Vy/HjkkbtcH1J1okIoah1qYvCQwWOO2lWkbq6Vr8ep8XBEhEq9nh+qMJwtEfa7CD+lNx 01zQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id m10-20020a50cc0a000000b00458d94f1a45si19579026edi.413.2022.10.22.03.34.26; Sat, 22 Oct 2022 03:34:27 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 191A768BE7F; Sat, 22 Oct 2022 13:34:23 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B990368BDAF for ; Sat, 22 Oct 2022 13:34:15 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id AEFED2404F8 for ; Sat, 22 Oct 2022 12:34:14 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id cEMRe9GFvBLJ for ; Sat, 22 Oct 2022 12:34:13 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 397E82404F5 for ; Sat, 22 Oct 2022 12:34:13 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 265613A13D9 for ; Sat, 22 Oct 2022 12:34:04 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Sat, 22 Oct 2022 12:32:56 +0200 Message-Id: <20221022103257.15463-3-anton@khirnov.net> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221022103257.15463-1-anton@khirnov.net> References: <20221022103257.15463-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v4 3/4] lavc/tests: add a cached bitstream reader test X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 0K5FAWGTF42w --- libavcodec/Makefile | 2 + libavcodec/tests/bitstream_be.c | 19 +++ libavcodec/tests/bitstream_le.c | 20 +++ libavcodec/tests/bitstream_template.c | 183 ++++++++++++++++++++++++++ tests/fate/libavcodec.mak | 10 ++ 5 files changed, 234 insertions(+) create mode 100644 libavcodec/tests/bitstream_be.c create mode 100644 libavcodec/tests/bitstream_le.c create mode 100644 libavcodec/tests/bitstream_template.c diff --git a/libavcodec/Makefile b/libavcodec/Makefile index b77fe0db8e..4314b47d65 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -1265,6 +1265,8 @@ SKIPHEADERS-$(CONFIG_ZLIB) += zlib_wrapper.h TESTPROGS = avcodec \ avpacket \ + bitstream_be \ + bitstream_le \ celp_math \ codec_desc \ htmlsubtitles \ diff --git a/libavcodec/tests/bitstream_be.c b/libavcodec/tests/bitstream_be.c new file mode 100644 index 0000000000..bc562ed3b1 --- /dev/null +++ b/libavcodec/tests/bitstream_be.c @@ -0,0 +1,19 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "bitstream_template.c" diff --git a/libavcodec/tests/bitstream_le.c b/libavcodec/tests/bitstream_le.c new file mode 100644 index 0000000000..ba9296c95a --- /dev/null +++ b/libavcodec/tests/bitstream_le.c @@ -0,0 +1,20 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#define BITSTREAM_LE +#include "bitstream_template.c" diff --git a/libavcodec/tests/bitstream_template.c b/libavcodec/tests/bitstream_template.c new file mode 100644 index 0000000000..13e92a31c6 --- /dev/null +++ b/libavcodec/tests/bitstream_template.c @@ -0,0 +1,183 @@ +/* + * cached bitstream reader test + * copyright (c) 2022 Anton Khirnov + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#define ASSERT_LEVEL 2 + +#include "libavutil/avassert.h" +#include "libavutil/lfg.h" +#include "libavutil/random_seed.h" + +#include "libavcodec/bitstream.h" +#include "libavcodec/defs.h" + +#ifdef BITSTREAM_LE +#define BITSTREAM_WRITER_LE +#endif +#include "libavcodec/put_bits.h" + +#define SIZE 157 + +enum Op { + OP_READ, + OP_READ_NZ, + OP_READ_BIT, + OP_READ_63, + OP_READ_64, + OP_READ_SIGNED, + OP_APPLY_SIGN, + OP_ALIGN, + OP_NB, +}; + +int main(int argc, char **argv) +{ + BitstreamContext bc; + PutBitContext pb; + AVLFG lfg; + + uint8_t buf[SIZE + AV_INPUT_BUFFER_PADDING_SIZE]; + uint8_t dst[SIZE + AV_INPUT_BUFFER_PADDING_SIZE]; + + uint32_t random_seed; + uint64_t val, val1; + int32_t sval; + unsigned count; + + /* generate random input, using a given or random seed */ + if (argc > 1) + random_seed = strtoul(argv[1], NULL, 0); + else + random_seed = av_get_random_seed(); + + fprintf(stderr, "Testing with LFG seed: %"PRIu32"\n", random_seed); + av_lfg_init(&lfg, random_seed); + + for (unsigned i = 0; i < SIZE; i++) + buf[i] = av_lfg_get(&lfg); + + bits_init8 (&bc, buf, SIZE); + init_put_bits(&pb, dst, SIZE); + + /* use a random sequence of bitreading operations to transfer data + * from BitstreamContext to PutBitContext */ + while (bits_left(&bc) > 0) { + enum Op op = av_lfg_get(&lfg) % OP_NB; + + switch (op) { + case OP_READ: + count = av_lfg_get(&lfg) % FFMIN(33, bits_left(&bc) + 1); + val1 = bits_peek(&bc, count); + val = bits_read(&bc, count); + + fprintf(stderr, "%d read %u: %"PRIu64"\n", bits_tell(&bc) - count, count, val); + + av_assert0(val == val1); + + put_bits64(&pb, count, val); + break; + case OP_READ_NZ: + count = av_lfg_get(&lfg) % FFMIN(33, bits_left(&bc) + 1); + count = FFMAX(count, 1); + val1 = bits_peek_nz(&bc, count); + val = bits_read_nz(&bc, count); + + fprintf(stderr, "%d read_nz %u: %"PRIu64"\n", bits_tell(&bc) - count, count, val); + + av_assert0(val == val1); + + put_bits64(&pb, count, val); + break; + case OP_READ_BIT: + val = bits_read_bit(&bc); + + fprintf(stderr, "%d read_bit: %"PRIu64"\n", bits_tell(&bc) - 1, val); + + put_bits(&pb, 1, val); + break; + case OP_READ_63: + count = av_lfg_get(&lfg) % FFMIN(64, bits_left(&bc) + 1); + val = bits_read_63(&bc, count); + + fprintf(stderr, "%d read_63 %u: %"PRIu64"\n", bits_tell(&bc) - count, count, val); + + put_bits64(&pb, count, val); + break; + case OP_READ_64: + count = av_lfg_get(&lfg) % FFMIN(65, bits_left(&bc) + 1); + val = bits_read_64(&bc, count); + + fprintf(stderr, "%d read_64 %u: %"PRIu64"\n", bits_tell(&bc) - count, count, val); + + put_bits64(&pb, count, val); + break; + case OP_READ_SIGNED: + count = av_lfg_get(&lfg) % FFMIN(33, bits_left(&bc) + 1); + sval = bits_read_signed(&bc, count); + + fprintf(stderr, "%d read_signed %u: %"PRId32"\n", bits_tell(&bc) - count, count, sval); + + if (count == 32) put_bits32(&pb, sval); + else put_sbits(&pb, count, sval); + break; + case OP_ALIGN: + count = (bits_tell(&bc) + 7) / 8 * 8 - bits_tell(&bc); + + fprintf(stderr, "%d align %u\n", bits_tell(&bc), count); + + put_bits(&pb, count, bits_peek(&bc, count)); + bits_align(&bc); + break; + case OP_APPLY_SIGN: + if (bits_left(&bc) < 2) + continue; + + count = av_lfg_get(&lfg) % FFMIN(32, bits_left(&bc)); + count = FFMAX(count, 1); + + if (!bits_peek(&bc, count)) + continue; + + val = bits_read(&bc, count); + sval = bits_apply_sign(&bc, val); + + fprintf(stderr, "%d apply_sign %u %"PRId32"\n", + bits_tell(&bc) - count - 1, count, sval); + + put_bits64(&pb, count, FFABS(sval)); + put_bits(&pb, 1, sval < 0); + + break; + default: + av_assert0(0); + } + } + + flush_put_bits(&pb); + + for (unsigned i = 0; i < SIZE; i++) + if (buf[i] != dst[i]) { + fprintf(stderr, "Mismatch at byte %u: %hhu %hhu; seed %"PRIu32"\n", + i, buf[i], dst[i], random_seed); + return 1; + } + + return 0; +} diff --git a/tests/fate/libavcodec.mak b/tests/fate/libavcodec.mak index aa199e0308..8f56fae3a8 100644 --- a/tests/fate/libavcodec.mak +++ b/tests/fate/libavcodec.mak @@ -3,6 +3,16 @@ fate-avpacket: libavcodec/tests/avpacket$(EXESUF) fate-avpacket: CMD = run libavcodec/tests/avpacket$(EXESUF) fate-avpacket: CMP = null +FATE_LIBAVCODEC-yes += fate-bitstream-be +fate-bitstream-be: libavcodec/tests/bitstream_be$(EXESUF) +fate-bitstream-be: CMD = run libavcodec/tests/bitstream_be$(EXESUF) +fate-bitstream-be: CMP = null + +FATE_LIBAVCODEC-yes += fate-bitstream-le +fate-bitstream-le: libavcodec/tests/bitstream_le$(EXESUF) +fate-bitstream-le: CMD = run libavcodec/tests/bitstream_le$(EXESUF) +fate-bitstream-le: CMP = null + FATE_LIBAVCODEC-$(CONFIG_CABAC) += fate-cabac fate-cabac: libavcodec/tests/cabac$(EXESUF) fate-cabac: CMD = run libavcodec/tests/cabac$(EXESUF)