From patchwork Tue Oct 20 07:53:52 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 23110 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 BA1FD44B69B for ; Tue, 20 Oct 2020 11:18:11 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 918B568B86B; Tue, 20 Oct 2020 11:18:11 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lj1-f196.google.com (mail-lj1-f196.google.com [209.85.208.196]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id BB1A468B753 for ; Tue, 20 Oct 2020 11:18:04 +0300 (EEST) Received: by mail-lj1-f196.google.com with SMTP id y16so1080635ljk.1 for ; Tue, 20 Oct 2020 01:18:04 -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:mime-version :content-transfer-encoding; bh=1FiXGdccTuOz7iwOpHq0BRSsjzFcgT/iL4wexrBGHjY=; b=ByjDJgrvqU8ad+A80PTzTo6h9aEBO+DzN+8vrnw9cMXtE1FtzbsqOu+gE3cOT+8U/t jlVtCiUugNFoXrbovEGrGhN5BdLEmeJVIgTrlwWGEejjJQZYZaNISlI+feQswqU7uwZZ /+kmVKdFFo97y3bZ4T7W6RGxeaI0L1NHnQykRX1J/Iyn258OQ+RG60NkuJKFaE2tUCki JCsVvOHyIRdQ/3RgP5wkM5CcY02o3OFe2koC2xz0tEhqH4Ms0l9+TVoz1UcgztZJ0Pbk T9pck36p2GJWIJ0BTXGHmSLYVGjJcl51N4kXtNyEiaDwqLWyN7ePrJbl/EwUQX10AtTh 4TUA== 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:mime-version :content-transfer-encoding; bh=1FiXGdccTuOz7iwOpHq0BRSsjzFcgT/iL4wexrBGHjY=; b=ohqXiKmbBBHyRg+EwhVggUEx/1qLQb7LGa5JD8V0SKSadHRPYZVESnB9pnHc/HvvRY ICRNAqOasMCW3IE6scXsjdsWOU2bi5VbXFscXRz9aVgZA+WHXx+UFrpAzEfb5kDlE9Br jtZnwwX7QLqY16kU+3PKLiSzbt/BZ49TJ7aK4ZkneQN/bpE5NT7slxQpecvRPO7KlgNf 2edo2S0ZSwKTJJBKHpYpljTJjZMjlakuk3ELL2k/F/lC83p6ar+Y8Vqg2V74IUyLb+Jb yd3AyuYrHFfnWNhaH8L1kosMkD4cOtv6GiGPOU+gkTdi8I1+RPggxGAXFXa3e0RdgQZ7 q0xQ== X-Gm-Message-State: AOAM533YFkvT2tgvMErDIWpCUXksZX33g4V6s2hx1HVXrVYH8InlwfS4 02q7EM3tRt1MuCr60qQ8vqHDF60iMGI= X-Google-Smtp-Source: ABdhPJy155NYhd3pgpWK7yTioKdBpnqa/uzdtvg4HnQ/PwSZIZQDINyx2IvjgQyfoMJCg+d8GxehGg== X-Received: by 2002:adf:a354:: with SMTP id d20mr2082148wrb.29.1603180444243; Tue, 20 Oct 2020 00:54:04 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1aa4b.dynamic.kabel-deutschland.de. [188.193.170.75]) by smtp.gmail.com with ESMTPSA id 130sm1533554wmd.18.2020.10.20.00.54.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Oct 2020 00:54:03 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Tue, 20 Oct 2020 09:53:52 +0200 Message-Id: <20201020075356.185676-1-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/4] avcodec/vp3: Fix memleak upon init failure 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" Up until now, there was no cleanup in case initializing the Theora VLC tables failed, leading to memleaks. This commit gets rid of them by setting the FF_CODEC_CAP_INIT_CLEANUP flag for all decoders in vp3.c; this also allows to remove some (now redundant) cleanup code. Signed-off-by: Andreas Rheinhardt --- libavcodec/vp3.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/libavcodec/vp3.c b/libavcodec/vp3.c index 6fe1ca46a3..0d8d1ff80f 100644 --- a/libavcodec/vp3.c +++ b/libavcodec/vp3.c @@ -2287,7 +2287,6 @@ static av_cold int allocate_tables(AVCodecContext *avctx) !s->superblock_fragments || !s->macroblock_coding || !s->dc_pred_row || !s->motion_val[0] || !s->motion_val[1]) { - vp3_decode_end(avctx); return -1; } @@ -2302,12 +2301,8 @@ static av_cold int init_frames(Vp3DecodeContext *s) s->last_frame.f = av_frame_alloc(); s->golden_frame.f = av_frame_alloc(); - if (!s->current_frame.f || !s->last_frame.f || !s->golden_frame.f) { - av_frame_free(&s->current_frame.f); - av_frame_free(&s->last_frame.f); - av_frame_free(&s->golden_frame.f); + if (!s->current_frame.f || !s->last_frame.f || !s->golden_frame.f) return AVERROR(ENOMEM); - } return 0; } @@ -3221,7 +3216,8 @@ AVCodec ff_theora_decoder = { AV_CODEC_CAP_FRAME_THREADS, .flush = vp3_decode_flush, .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context), - .caps_internal = FF_CODEC_CAP_EXPORTS_CROPPING | FF_CODEC_CAP_ALLOCATE_PROGRESS, + .caps_internal = FF_CODEC_CAP_EXPORTS_CROPPING | FF_CODEC_CAP_ALLOCATE_PROGRESS | + FF_CODEC_CAP_INIT_CLEANUP, }; #endif @@ -3238,7 +3234,7 @@ AVCodec ff_vp3_decoder = { AV_CODEC_CAP_FRAME_THREADS, .flush = vp3_decode_flush, .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context), - .caps_internal = FF_CODEC_CAP_ALLOCATE_PROGRESS, + .caps_internal = FF_CODEC_CAP_ALLOCATE_PROGRESS | FF_CODEC_CAP_INIT_CLEANUP, }; #if CONFIG_VP4_DECODER @@ -3255,6 +3251,6 @@ AVCodec ff_vp4_decoder = { AV_CODEC_CAP_FRAME_THREADS, .flush = vp3_decode_flush, .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context), - .caps_internal = FF_CODEC_CAP_ALLOCATE_PROGRESS, + .caps_internal = FF_CODEC_CAP_ALLOCATE_PROGRESS | FF_CODEC_CAP_INIT_CLEANUP, }; #endif From patchwork Tue Oct 20 07:53:53 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 23107 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 42FAF449EBD for ; Tue, 20 Oct 2020 10:59:36 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 208C268B7CB; Tue, 20 Oct 2020 10:59:36 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f66.google.com (mail-wr1-f66.google.com [209.85.221.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D875268B567 for ; Tue, 20 Oct 2020 10:59:29 +0300 (EEST) Received: by mail-wr1-f66.google.com with SMTP id h5so911192wrv.7 for ; Tue, 20 Oct 2020 00:59:29 -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=Cbc04bZ6P4pj4AGO1yYgL42PVOgFomI55cFayWo13Fg=; b=eN3DhmszN+WDyo3o1RGNnvgIC+2G+G7BSGEEpDpugmea46SCUp86poPGvoCirkbxJz eUNqxRf9wOqJ65Y69YVDZi7+YWxtzbwtOUz/9lUImNT3jqbkLuMPLUXfZ3kf/SLiDZ7s jc2sTtL7CBX/W2J9reHgrRKUrn1Y9TxKy0hpCBSWCV6aP5lMXO/1WSKdEwqEUtl7kZdt 3SyJDH75Gnkz5mVzvdTxt8Hp23jUDcwHLzl94AZgG4mHVt8S76viSkjt4UPZN3mWngPl YXq0w5cpPRqnqQjqGT0mc6M9rExTaqgHSkYtDgGQiAT5hMoRnZjgPpGImVh1BVeXM9bO QMMg== 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=Cbc04bZ6P4pj4AGO1yYgL42PVOgFomI55cFayWo13Fg=; b=jDlL6KIQ/O+Jw+JFdqz7Jd7nmVjw1V9AXdVjdDOVe+jSmhvtJiVcPMH1/pxQ6/28mv NJ8qB8yuFKbr/M1B6gTfzZrxs/02y3Eo7jSAW9vlB272Ckg0NCqraIMFZ8rPWL7QylGs MBAQp1dInmasbPPiIxGJDampfUUc2QZV/OWs8wdN8Lt/RxCFw5HHIHhvzmayWAlvXB6b ftjBHZAg94xqzmnWfP8W9rSIfsAfbKA4XdiXtr+66XJQqlxyhe4DCbdqFtMc4JUjHAkZ MRVjAEv1UHDt6eRHo+INpIsw5a93CjAkIxmmMiiZRSdKsU/ah8YwsZv64HMQn0h++4y9 Vb+Q== X-Gm-Message-State: AOAM533BmU3OZ3omFOXL/qVQzLqEoJRZtaCCVtRz6Yj01SNa2KybQTfy HmPJVeXaTAFm1ydHBzLg2ZEGMnEMnM4= X-Google-Smtp-Source: ABdhPJzwmBnw6TbHVrjWy+FFk7UtXJa+BaRZpAUXeKTxKyhVbLYAoeo+EXZtKOiAX/sy1dKd2Mzy6w== X-Received: by 2002:adf:f182:: with SMTP id h2mr1959410wro.199.1603180768870; Tue, 20 Oct 2020 00:59:28 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1aa4b.dynamic.kabel-deutschland.de. [188.193.170.75]) by smtp.gmail.com with ESMTPSA id 130sm1533554wmd.18.2020.10.20.00.59.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Oct 2020 00:59:28 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Tue, 20 Oct 2020 09:53:53 +0200 Message-Id: <20201020075356.185676-2-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201020075356.185676-1-andreas.rheinhardt@gmail.com> References: <20201020075356.185676-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/4] avcodec/vp3: Check allocations of VLCs 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" It would lead to crashs lateron if they failed. Signed-off-by: Andreas Rheinhardt --- libavcodec/vp3.c | 112 +++++++++++++++++++++++++++-------------------- 1 file changed, 64 insertions(+), 48 deletions(-) diff --git a/libavcodec/vp3.c b/libavcodec/vp3.c index 0d8d1ff80f..7ee65c8062 100644 --- a/libavcodec/vp3.c +++ b/libavcodec/vp3.c @@ -2414,57 +2414,67 @@ static av_cold int vp3_decode_init(AVCodecContext *avctx) if (s->version < 2) { for (i = 0; i < 16; i++) { /* DC histograms */ - init_vlc(&s->dc_vlc[i], 11, 32, - &dc_bias[i][0][1], 4, 2, - &dc_bias[i][0][0], 4, 2, 0); + if ((ret = init_vlc(&s->dc_vlc[i], 11, 32, + &dc_bias[i][0][1], 4, 2, + &dc_bias[i][0][0], 4, 2, 0)) < 0) + return ret; /* group 1 AC histograms */ - init_vlc(&s->ac_vlc_1[i], 11, 32, - &ac_bias_0[i][0][1], 4, 2, - &ac_bias_0[i][0][0], 4, 2, 0); + if ((ret = init_vlc(&s->ac_vlc_1[i], 11, 32, + &ac_bias_0[i][0][1], 4, 2, + &ac_bias_0[i][0][0], 4, 2, 0)) < 0) + return ret; /* group 2 AC histograms */ - init_vlc(&s->ac_vlc_2[i], 11, 32, - &ac_bias_1[i][0][1], 4, 2, - &ac_bias_1[i][0][0], 4, 2, 0); + if ((ret = init_vlc(&s->ac_vlc_2[i], 11, 32, + &ac_bias_1[i][0][1], 4, 2, + &ac_bias_1[i][0][0], 4, 2, 0)) < 0) + return ret; /* group 3 AC histograms */ - init_vlc(&s->ac_vlc_3[i], 11, 32, - &ac_bias_2[i][0][1], 4, 2, - &ac_bias_2[i][0][0], 4, 2, 0); + if ((ret = init_vlc(&s->ac_vlc_3[i], 11, 32, + &ac_bias_2[i][0][1], 4, 2, + &ac_bias_2[i][0][0], 4, 2, 0)) < 0) + return ret; /* group 4 AC histograms */ - init_vlc(&s->ac_vlc_4[i], 11, 32, - &ac_bias_3[i][0][1], 4, 2, - &ac_bias_3[i][0][0], 4, 2, 0); + if ((ret = init_vlc(&s->ac_vlc_4[i], 11, 32, + &ac_bias_3[i][0][1], 4, 2, + &ac_bias_3[i][0][0], 4, 2, 0)) < 0) + return ret; } #if CONFIG_VP4_DECODER } else { /* version >= 2 */ for (i = 0; i < 16; i++) { /* DC histograms */ - init_vlc(&s->dc_vlc[i], 11, 32, - &vp4_dc_bias[i][0][1], 4, 2, - &vp4_dc_bias[i][0][0], 4, 2, 0); + if ((ret = init_vlc(&s->dc_vlc[i], 11, 32, + &vp4_dc_bias[i][0][1], 4, 2, + &vp4_dc_bias[i][0][0], 4, 2, 0)) < 0) + return ret; /* group 1 AC histograms */ - init_vlc(&s->ac_vlc_1[i], 11, 32, - &vp4_ac_bias_0[i][0][1], 4, 2, - &vp4_ac_bias_0[i][0][0], 4, 2, 0); + if ((ret = init_vlc(&s->ac_vlc_1[i], 11, 32, + &vp4_ac_bias_0[i][0][1], 4, 2, + &vp4_ac_bias_0[i][0][0], 4, 2, 0)) < 0) + return ret; /* group 2 AC histograms */ - init_vlc(&s->ac_vlc_2[i], 11, 32, - &vp4_ac_bias_1[i][0][1], 4, 2, - &vp4_ac_bias_1[i][0][0], 4, 2, 0); + if ((ret = init_vlc(&s->ac_vlc_2[i], 11, 32, + &vp4_ac_bias_1[i][0][1], 4, 2, + &vp4_ac_bias_1[i][0][0], 4, 2, 0)) < 0) + return ret; /* group 3 AC histograms */ - init_vlc(&s->ac_vlc_3[i], 11, 32, - &vp4_ac_bias_2[i][0][1], 4, 2, - &vp4_ac_bias_2[i][0][0], 4, 2, 0); + if ((ret = init_vlc(&s->ac_vlc_3[i], 11, 32, + &vp4_ac_bias_2[i][0][1], 4, 2, + &vp4_ac_bias_2[i][0][0], 4, 2, 0)) < 0) + return ret; /* group 4 AC histograms */ - init_vlc(&s->ac_vlc_4[i], 11, 32, - &vp4_ac_bias_3[i][0][1], 4, 2, - &vp4_ac_bias_3[i][0][0], 4, 2, 0); + if ((ret = init_vlc(&s->ac_vlc_4[i], 11, 32, + &vp4_ac_bias_3[i][0][1], 4, 2, + &vp4_ac_bias_3[i][0][0], 4, 2, 0)) < 0) + return ret; } #endif } @@ -2502,34 +2512,40 @@ static av_cold int vp3_decode_init(AVCodecContext *avctx) } } - init_vlc(&s->superblock_run_length_vlc, 6, 34, - &superblock_run_length_vlc_table[0][1], 4, 2, - &superblock_run_length_vlc_table[0][0], 4, 2, 0); + if ((ret = init_vlc(&s->superblock_run_length_vlc, 6, 34, + &superblock_run_length_vlc_table[0][1], 4, 2, + &superblock_run_length_vlc_table[0][0], 4, 2, 0)) < 0) + return ret; - init_vlc(&s->fragment_run_length_vlc, 5, 30, - &fragment_run_length_vlc_table[0][1], 4, 2, - &fragment_run_length_vlc_table[0][0], 4, 2, 0); + if ((ret = init_vlc(&s->fragment_run_length_vlc, 5, 30, + &fragment_run_length_vlc_table[0][1], 4, 2, + &fragment_run_length_vlc_table[0][0], 4, 2, 0)) < 0) + return ret; - init_vlc(&s->mode_code_vlc, 3, 8, - &mode_code_vlc_table[0][1], 2, 1, - &mode_code_vlc_table[0][0], 2, 1, 0); + if ((ret = init_vlc(&s->mode_code_vlc, 3, 8, + &mode_code_vlc_table[0][1], 2, 1, + &mode_code_vlc_table[0][0], 2, 1, 0)) < 0) + return ret; - init_vlc(&s->motion_vector_vlc, 6, 63, - &motion_vector_vlc_table[0][1], 2, 1, - &motion_vector_vlc_table[0][0], 2, 1, 0); + if ((ret = init_vlc(&s->motion_vector_vlc, 6, 63, + &motion_vector_vlc_table[0][1], 2, 1, + &motion_vector_vlc_table[0][0], 2, 1, 0)) < 0) + return ret; #if CONFIG_VP4_DECODER for (j = 0; j < 2; j++) for (i = 0; i < 7; i++) - init_vlc(&s->vp4_mv_vlc[j][i], 6, 63, - &vp4_mv_vlc[j][i][0][1], 4, 2, - &vp4_mv_vlc[j][i][0][0], 4, 2, 0); + if ((ret = init_vlc(&s->vp4_mv_vlc[j][i], 6, 63, + &vp4_mv_vlc[j][i][0][1], 4, 2, + &vp4_mv_vlc[j][i][0][0], 4, 2, 0)) < 0) + return ret; /* version >= 2 */ for (i = 0; i < 2; i++) - init_vlc(&s->block_pattern_vlc[i], 3, 14, - &vp4_block_pattern_vlc[i][0][1], 2, 1, - &vp4_block_pattern_vlc[i][0][0], 2, 1, 0); + if ((ret = init_vlc(&s->block_pattern_vlc[i], 3, 14, + &vp4_block_pattern_vlc[i][0][1], 2, 1, + &vp4_block_pattern_vlc[i][0][0], 2, 1, 0)) < 0) + return ret; #endif return allocate_tables(avctx); From patchwork Tue Oct 20 07:53:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 23108 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 EACAE449EBD for ; Tue, 20 Oct 2020 10:59:38 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id CAC0968B818; Tue, 20 Oct 2020 10:59:38 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f68.google.com (mail-wr1-f68.google.com [209.85.221.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 24F3568B7E4 for ; Tue, 20 Oct 2020 10:59:31 +0300 (EEST) Received: by mail-wr1-f68.google.com with SMTP id b8so938667wrn.0 for ; Tue, 20 Oct 2020 00:59:31 -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=xrssNOlk0PIg+/NsquqBMosr4ExlALuBwneSzahors0=; b=awQb/vLuzFKcmIacL4etONaIp+TxypIAQfRSvmvzW3fNV4j4lTMZLHp7go4F4ipJ5U GcCnYoA8kaDCRQbqqQCslw8iqdMalEWGJR4vDubJfQ7YCc2LYSGbueE8XlR0rSshz3Bg tbWNw1URMmYIPMG8ueiJ6xbvRQrRmP60So9+vFEHjxRdAMH4pgL+xBxWNRZWf08R8401 fDFB+I3VmyR8RVs+P7MrvumrMsPEGM3CoKFD4Rd2Y82U3wo5sLbiDv6SCRts2C7svkwe TUMOB+9rOTqZQsNO6b8xQ86HD8a0eueM6LNWyencv05nmxEeisNYT7h/tXUjxEIT9X3X Tgcg== 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=xrssNOlk0PIg+/NsquqBMosr4ExlALuBwneSzahors0=; b=n6OYcsUC8LNE0XSZxV5eLJoBOYMy3DPynJgCytMR6c2R6Fbgh+XQNKw2BfpRRUPWiq DKj6UmoDLE2EGAuB4o4I5vfzegwh9VUPEgsQEi4R3v2qYhlg+9X2eV7VRK5zzYxNMsGp iPwWyZVmX0M56KPNtSM/Gv/q2G9T4WGBKXD8zExWnBPcBIiAPvTVi87vv5qm5pYSw9pc l4138xjfDl6Qct1uymGw7bpUZOAgNpsHcrl89+0WdIPIZfmY4WuswMiI9Z1iyLZ/Cxrm b6/XKv/THGrBzywcw8tz7IXX1lxJ52IxAJnMHWMDpW9QW6q7rZgxUIlIVBCUX6PmUPKQ 11IA== X-Gm-Message-State: AOAM5330OpAY+IiaFdQCgrrguSkNXxis91aqWvYIiyVxLKVqlWbW9uLT 4qvHSClqxPy1Eqn8+rre18lWph+xYmU= X-Google-Smtp-Source: ABdhPJwm0JUXk70wWjSFEv004kd9CdJ3l0HMFNuais7liQCtGswomZi7FFd2y//Xd8V0hKfaVliUvg== X-Received: by 2002:adf:e849:: with SMTP id d9mr2129293wrn.25.1603180769916; Tue, 20 Oct 2020 00:59:29 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1aa4b.dynamic.kabel-deutschland.de. [188.193.170.75]) by smtp.gmail.com with ESMTPSA id 130sm1533554wmd.18.2020.10.20.00.59.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Oct 2020 00:59:29 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Tue, 20 Oct 2020 09:53:54 +0200 Message-Id: <20201020075356.185676-3-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201020075356.185676-1-andreas.rheinhardt@gmail.com> References: <20201020075356.185676-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/4] avcodec/vp3: Unify initializing and freeing VLC tables 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" Signed-off-by: Andreas Rheinhardt --- libavcodec/vp3.c | 163 ++++++++++++------------------------------- libavcodec/vp3data.h | 22 ++---- libavcodec/vp4data.h | 22 ++---- 3 files changed, 55 insertions(+), 152 deletions(-) diff --git a/libavcodec/vp3.c b/libavcodec/vp3.c index 7ee65c8062..e629a18b8e 100644 --- a/libavcodec/vp3.c +++ b/libavcodec/vp3.c @@ -257,11 +257,9 @@ typedef struct Vp3DecodeContext { int *nkf_coded_fragment_list; int num_kf_coded_fragment[3]; - VLC dc_vlc[16]; - VLC ac_vlc_1[16]; - VLC ac_vlc_2[16]; - VLC ac_vlc_3[16]; - VLC ac_vlc_4[16]; + /* The first 16 of the following VLCs are for the dc coefficients; + the others are four groups of 16 VLCs each for ac coefficients. */ + VLC xc_vlc[80]; VLC superblock_run_length_vlc; /* version < 2 */ VLC fragment_run_length_vlc; /* version < 2 */ @@ -347,13 +345,8 @@ static av_cold int vp3_decode_end(AVCodecContext *avctx) av_frame_free(&s->last_frame.f); av_frame_free(&s->golden_frame.f); - for (i = 0; i < 16; i++) { - ff_free_vlc(&s->dc_vlc[i]); - ff_free_vlc(&s->ac_vlc_1[i]); - ff_free_vlc(&s->ac_vlc_2[i]); - ff_free_vlc(&s->ac_vlc_3[i]); - ff_free_vlc(&s->ac_vlc_4[i]); - } + for (i = 0; i < 80; i++) + ff_free_vlc(&s->xc_vlc[i]); ff_free_vlc(&s->superblock_run_length_vlc); ff_free_vlc(&s->fragment_run_length_vlc); @@ -1314,7 +1307,7 @@ static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb) dc_c_table = get_bits(gb, 4); /* unpack the Y plane DC coefficients */ - residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0, + residual_eob_run = unpack_vlcs(s, gb, &s->xc_vlc[dc_y_table], 0, 0, residual_eob_run); if (residual_eob_run < 0) return residual_eob_run; @@ -1325,11 +1318,11 @@ static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb) reverse_dc_prediction(s, 0, s->fragment_width[0], s->fragment_height[0]); /* unpack the C plane DC coefficients */ - residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0, + residual_eob_run = unpack_vlcs(s, gb, &s->xc_vlc[dc_c_table], 0, 1, residual_eob_run); if (residual_eob_run < 0) return residual_eob_run; - residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0, + residual_eob_run = unpack_vlcs(s, gb, &s->xc_vlc[dc_c_table], 0, 2, residual_eob_run); if (residual_eob_run < 0) return residual_eob_run; @@ -1350,20 +1343,24 @@ static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb) /* build tables of AC VLC tables */ for (i = 1; i <= 5; i++) { - y_tables[i] = &s->ac_vlc_1[ac_y_table]; - c_tables[i] = &s->ac_vlc_1[ac_c_table]; + /* AC VLC table group 1 */ + y_tables[i] = &s->xc_vlc[ac_y_table + 16]; + c_tables[i] = &s->xc_vlc[ac_c_table + 16]; } for (i = 6; i <= 14; i++) { - y_tables[i] = &s->ac_vlc_2[ac_y_table]; - c_tables[i] = &s->ac_vlc_2[ac_c_table]; + /* AC VLC table group 2 */ + y_tables[i] = &s->xc_vlc[ac_y_table + 32]; + c_tables[i] = &s->xc_vlc[ac_c_table + 32]; } for (i = 15; i <= 27; i++) { - y_tables[i] = &s->ac_vlc_3[ac_y_table]; - c_tables[i] = &s->ac_vlc_3[ac_c_table]; + /* AC VLC table group 3 */ + y_tables[i] = &s->xc_vlc[ac_y_table + 48]; + c_tables[i] = &s->xc_vlc[ac_c_table + 48]; } for (i = 28; i <= 63; i++) { - y_tables[i] = &s->ac_vlc_4[ac_y_table]; - c_tables[i] = &s->ac_vlc_4[ac_c_table]; + /* AC VLC table group 4 */ + y_tables[i] = &s->xc_vlc[ac_y_table + 64]; + c_tables[i] = &s->xc_vlc[ac_c_table + 64]; } /* decode all AC coefficients */ @@ -1542,23 +1539,28 @@ static int vp4_unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb) /* build tables of DC/AC VLC tables */ - tables[0][0] = &s->dc_vlc[dc_y_table]; - tables[1][0] = &s->dc_vlc[dc_c_table]; + /* DC table group */ + tables[0][0] = &s->xc_vlc[dc_y_table]; + tables[1][0] = &s->xc_vlc[dc_c_table]; for (i = 1; i <= 5; i++) { - tables[0][i] = &s->ac_vlc_1[ac_y_table]; - tables[1][i] = &s->ac_vlc_1[ac_c_table]; + /* AC VLC table group 1 */ + tables[0][i] = &s->xc_vlc[ac_y_table + 16]; + tables[1][i] = &s->xc_vlc[ac_c_table + 16]; } for (i = 6; i <= 14; i++) { - tables[0][i] = &s->ac_vlc_2[ac_y_table]; - tables[1][i] = &s->ac_vlc_2[ac_c_table]; + /* AC VLC table group 2 */ + tables[0][i] = &s->xc_vlc[ac_y_table + 32]; + tables[1][i] = &s->xc_vlc[ac_c_table + 32]; } for (i = 15; i <= 27; i++) { - tables[0][i] = &s->ac_vlc_3[ac_y_table]; - tables[1][i] = &s->ac_vlc_3[ac_c_table]; + /* AC VLC table group 3 */ + tables[0][i] = &s->xc_vlc[ac_y_table + 48]; + tables[1][i] = &s->xc_vlc[ac_c_table + 48]; } for (i = 28; i <= 63; i++) { - tables[0][i] = &s->ac_vlc_4[ac_y_table]; - tables[1][i] = &s->ac_vlc_4[ac_c_table]; + /* AC VLC table group 4 */ + tables[0][i] = &s->xc_vlc[ac_y_table + 64]; + tables[1][i] = &s->xc_vlc[ac_c_table + 64]; } vp4_set_tokens_base(s); @@ -2412,103 +2414,28 @@ static av_cold int vp3_decode_init(AVCodecContext *avctx) /* init VLC tables */ if (s->version < 2) { - for (i = 0; i < 16; i++) { - /* DC histograms */ - if ((ret = init_vlc(&s->dc_vlc[i], 11, 32, - &dc_bias[i][0][1], 4, 2, - &dc_bias[i][0][0], 4, 2, 0)) < 0) - return ret; - - /* group 1 AC histograms */ - if ((ret = init_vlc(&s->ac_vlc_1[i], 11, 32, - &ac_bias_0[i][0][1], 4, 2, - &ac_bias_0[i][0][0], 4, 2, 0)) < 0) - return ret; - - /* group 2 AC histograms */ - if ((ret = init_vlc(&s->ac_vlc_2[i], 11, 32, - &ac_bias_1[i][0][1], 4, 2, - &ac_bias_1[i][0][0], 4, 2, 0)) < 0) - return ret; - - /* group 3 AC histograms */ - if ((ret = init_vlc(&s->ac_vlc_3[i], 11, 32, - &ac_bias_2[i][0][1], 4, 2, - &ac_bias_2[i][0][0], 4, 2, 0)) < 0) - return ret; - - /* group 4 AC histograms */ - if ((ret = init_vlc(&s->ac_vlc_4[i], 11, 32, - &ac_bias_3[i][0][1], 4, 2, - &ac_bias_3[i][0][0], 4, 2, 0)) < 0) + for (i = 0; i < 80; i++) { + if ((ret = init_vlc(&s->xc_vlc[i], 11, 32, + &vp3_bias[i][0][1], 4, 2, + &vp3_bias[i][0][0], 4, 2, 0)) < 0) return ret; } #if CONFIG_VP4_DECODER } else { /* version >= 2 */ - for (i = 0; i < 16; i++) { - /* DC histograms */ - if ((ret = init_vlc(&s->dc_vlc[i], 11, 32, - &vp4_dc_bias[i][0][1], 4, 2, - &vp4_dc_bias[i][0][0], 4, 2, 0)) < 0) - return ret; - - /* group 1 AC histograms */ - if ((ret = init_vlc(&s->ac_vlc_1[i], 11, 32, - &vp4_ac_bias_0[i][0][1], 4, 2, - &vp4_ac_bias_0[i][0][0], 4, 2, 0)) < 0) - return ret; - - /* group 2 AC histograms */ - if ((ret = init_vlc(&s->ac_vlc_2[i], 11, 32, - &vp4_ac_bias_1[i][0][1], 4, 2, - &vp4_ac_bias_1[i][0][0], 4, 2, 0)) < 0) - return ret; - - /* group 3 AC histograms */ - if ((ret = init_vlc(&s->ac_vlc_3[i], 11, 32, - &vp4_ac_bias_2[i][0][1], 4, 2, - &vp4_ac_bias_2[i][0][0], 4, 2, 0)) < 0) - return ret; - - /* group 4 AC histograms */ - if ((ret = init_vlc(&s->ac_vlc_4[i], 11, 32, - &vp4_ac_bias_3[i][0][1], 4, 2, - &vp4_ac_bias_3[i][0][0], 4, 2, 0)) < 0) + for (i = 0; i < 80; i++) { + if ((ret = init_vlc(&s->xc_vlc[i], 11, 32, + &vp4_bias[i][0][1], 4, 2, + &vp4_bias[i][0][0], 4, 2, 0)) < 0) return ret; } #endif } } else { - for (i = 0; i < 16; i++) { - /* DC histograms */ - if (init_vlc(&s->dc_vlc[i], 11, 32, + for (i = 0; i < 80; i++) { + if (init_vlc(&s->xc_vlc[i], 11, 32, &s->huffman_table[i][0][1], 8, 4, &s->huffman_table[i][0][0], 8, 4, 0) < 0) goto vlc_fail; - - /* group 1 AC histograms */ - if (init_vlc(&s->ac_vlc_1[i], 11, 32, - &s->huffman_table[i + 16][0][1], 8, 4, - &s->huffman_table[i + 16][0][0], 8, 4, 0) < 0) - goto vlc_fail; - - /* group 2 AC histograms */ - if (init_vlc(&s->ac_vlc_2[i], 11, 32, - &s->huffman_table[i + 16 * 2][0][1], 8, 4, - &s->huffman_table[i + 16 * 2][0][0], 8, 4, 0) < 0) - goto vlc_fail; - - /* group 3 AC histograms */ - if (init_vlc(&s->ac_vlc_3[i], 11, 32, - &s->huffman_table[i + 16 * 3][0][1], 8, 4, - &s->huffman_table[i + 16 * 3][0][0], 8, 4, 0) < 0) - goto vlc_fail; - - /* group 4 AC histograms */ - if (init_vlc(&s->ac_vlc_4[i], 11, 32, - &s->huffman_table[i + 16 * 4][0][1], 8, 4, - &s->huffman_table[i + 16 * 4][0][0], 8, 4, 0) < 0) - goto vlc_fail; } } diff --git a/libavcodec/vp3data.h b/libavcodec/vp3data.h index 3f24d5f7f6..060d4965cf 100644 --- a/libavcodec/vp3data.h +++ b/libavcodec/vp3data.h @@ -442,7 +442,7 @@ static const int16_t *const coeff_tables[32] = { coeff_table_token_31 }; -static const uint16_t dc_bias[16][32][2] = { +static const uint16_t vp3_bias[80][32][2] = { { /* DC bias table 0 */ { 0x2D, 6 }, { 0x26, 7 }, @@ -986,10 +986,7 @@ static const uint16_t dc_bias[16][32][2] = { { 0x151, 9 }, { 0x150, 9 }, { 0x2A9, 10 } - } -}; - -static const uint16_t ac_bias_0[16][32][2] = { + }, { /* AC bias group 1, table 0 */ { 0x8, 5 }, { 0x25, 7 }, @@ -1533,10 +1530,7 @@ static const uint16_t ac_bias_0[16][32][2] = { { 0x38, 7 }, { 0x3D, 6 }, { 0x79, 7 } - } -}; - -static const uint16_t ac_bias_1[16][32][2] = { + }, { /* AC bias group 2, table 0 */ { 0xB, 5 }, { 0x2B, 7 }, @@ -2080,10 +2074,7 @@ static const uint16_t ac_bias_1[16][32][2] = { { 0xB, 6 }, { 0x5F, 7 }, { 0xBD, 8 } - } -}; - -static const uint16_t ac_bias_2[16][32][2] = { + }, { /* AC bias group 3, table 0 */ { 0x3, 4 }, { 0x9, 6 }, @@ -2627,10 +2618,7 @@ static const uint16_t ac_bias_2[16][32][2] = { { 0x1B, 5 }, { 0x35B7, 14 }, { 0x35B6, 14 } - } -}; - -static const uint16_t ac_bias_3[16][32][2] = { + }, { /* AC bias group 4, table 0 */ { 0x0, 3 }, { 0x10, 5 }, diff --git a/libavcodec/vp4data.h b/libavcodec/vp4data.h index 69a009eff1..356fa25748 100644 --- a/libavcodec/vp4data.h +++ b/libavcodec/vp4data.h @@ -368,7 +368,7 @@ static const uint16_t vp4_mv_vlc[2][7][63][2] = { } }; -static const uint16_t vp4_dc_bias[16][32][2] = { +static const uint16_t vp4_bias[80][32][2] = { { /* DC bias table 0 */ { 0xC, 5 }, { 0x70, 7 }, { 0x1CA, 9 }, { 0x1CB, 9 }, { 0x391, 10 }, { 0x1C9B, 13 }, { 0x3935, 14 }, { 0x71, 7 }, @@ -528,10 +528,7 @@ static const uint16_t vp4_dc_bias[16][32][2] = { { 0xC4, 8 }, { 0x5B9, 11 }, { 0x5B8, 11 }, { 0x11, 5 }, { 0x36, 6 }, { 0x5F, 7 }, { 0x1E, 7 }, { 0x63, 7 }, { 0x6F, 7 }, { 0x1F, 7 }, { 0xB6, 8 }, { 0x16F, 9 } - } -}; - -static const uint16_t vp4_ac_bias_0[16][32][2] = { + }, { /* AC bias group 1, table 0 */ { 0x6, 5 }, { 0x1E, 7 }, { 0x1CC, 9 }, { 0x1CE, 9 }, { 0x734, 11 }, { 0x1CD5, 13 }, { 0x1CD4, 13 }, { 0x18, 5 }, @@ -691,10 +688,7 @@ static const uint16_t vp4_ac_bias_0[16][32][2] = { { 0x3A0, 12 }, { 0x3A3, 12 }, { 0x3A2, 12 }, { 0x5, 3 }, { 0x2, 4 }, { 0x1F, 5 }, { 0x1D, 5 }, { 0x3C, 6 }, { 0x18, 5 }, { 0xF, 6 }, { 0x6, 5 }, { 0x5, 6 } - } -}; - -static const uint16_t vp4_ac_bias_1[16][32][2] = { + }, { /* AC bias group 2, table 0 */ { 0x4, 5 }, { 0xF5, 8 }, { 0x182, 9 }, { 0x60F, 11 }, { 0x1839, 13 }, { 0x1838, 13 }, { 0x183B, 13 }, { 0x13, 5 }, @@ -854,10 +848,7 @@ static const uint16_t vp4_ac_bias_1[16][32][2] = { { 0x288, 13 }, { 0x28B, 13 }, { 0x28A, 13 }, { 0xF, 4 }, { 0x1D, 5 }, { 0x13, 5 }, { 0x1, 5 }, { 0x0, 5 }, { 0x3, 3 }, { 0x1A, 5 }, { 0x72, 7 }, { 0xE7, 8 } - } -}; - -static const uint16_t vp4_ac_bias_2[16][32][2] = { + }, { /* AC bias group 3, table 0 */ { 0x9, 5 }, { 0x15, 7 }, { 0x28, 8 }, { 0x52, 9 }, { 0x29A, 12 }, { 0x537, 13 }, { 0x536, 13 }, { 0xA, 5 }, @@ -1017,10 +1008,7 @@ static const uint16_t vp4_ac_bias_2[16][32][2] = { { 0x3872, 14 }, { 0x3879, 14 }, { 0x3878, 14 }, { 0x3C, 6 }, { 0x73, 7 }, { 0x2A, 6 }, { 0x3D, 6 }, { 0x2B, 6 }, { 0x1F, 5 }, { 0xD, 4 }, { 0x1C3E, 13 }, { 0x1C3D, 13 } - } -}; - -static const uint16_t vp4_ac_bias_3[16][32][2] = { + }, { /* AC bias group 4, table 0 */ { 0x7, 4 }, { 0xF, 6 }, { 0xBB, 8 }, { 0xBA, 8 }, { 0x5CF, 11 }, { 0x173A, 13 }, { 0x2E77, 14 }, { 0x29, 6 }, From patchwork Tue Oct 20 07:53:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 23109 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 2290F449EBD for ; Tue, 20 Oct 2020 10:59:40 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0AEDD68B7FF; Tue, 20 Oct 2020 10:59:40 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f66.google.com (mail-wr1-f66.google.com [209.85.221.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2313768B811 for ; Tue, 20 Oct 2020 10:59:32 +0300 (EEST) Received: by mail-wr1-f66.google.com with SMTP id x7so927531wrl.3 for ; Tue, 20 Oct 2020 00:59:32 -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=fAZMCRMsroRwi7BVDvWGOZjK2UvhkbzBL8Kic1aMHLk=; b=vdAScN4UUajbCtJYKHjN2UqggTo2UNhTz6erZK20+lSNUkfFf2KZNDB1GxQoNv021X NSufCF+Xe4Ur99U0fIK9BeyqHKr3UBHNlOjmmu2MhVDnSUSiNpIPLuCg5E3Se987gjY5 wNm/e3ddy3eqGG1xTFkvKk1d0Bh4UUFeAli4L5zIYp04HuK7XbxeeCXakX5S+ASzfLiR kGeoprN3c0E6MQGyNN4J0m1uMmzGuPxqDwR3sxQ116a+x/ysZh4gA+EBoAWNM/+Ls9rp EOaaIcWJ6jpKbFRSCsUzTV+OYr1uQNJwg3rvqqTvlS+wuofvjLG9oujCsat3FnhpLJLc 9aIw== 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=fAZMCRMsroRwi7BVDvWGOZjK2UvhkbzBL8Kic1aMHLk=; b=XuZ+zNl3vosNdlseUaRYymLDpqKoepx2Pd3F1w0w1aK9P2FGxUA4of9adFPCjpmif4 VhJWNDkBor1hsLO+lRnI6FbbSflIu36Y8VpprUwQuLflNdwNmmH5QUWUC7lW+S240WXT LA9nZNrFektVGsUJoZmdu23E+knoZt1Wf3Hu1IH1X/GAtQwyZYEwtsMqihTMxy8nBtzC pnWSPEQMcwf9/ahtQF1aWdPAbN1RvmmiCkC5Ze8WsZ1Ls+rnbL2KK4F8TvcMW06g/hqK A+VV0dfJxf1gGoV0Mj7ZN5bLs92TzVoGT6+dTXcXy5kTGRFsAcXc3xpYfX2sti0AA+y5 Oc2Q== X-Gm-Message-State: AOAM533caM/Vog0KSSrQTXGstMLSXPVr0xZlxXaUhtvyIhDyC5UtbQCs c28NE1CNlSZDErNVcJB3l7SCw2yw1V0= X-Google-Smtp-Source: ABdhPJy42y45Mz5/Epi8AmvRP7Nkm349ozvlYXDbtBrr9x4GwyuxfV7yqSXyNSUOkJxMzK+RfLBDLg== X-Received: by 2002:a5d:6052:: with SMTP id j18mr2029008wrt.306.1603180771156; Tue, 20 Oct 2020 00:59:31 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1aa4b.dynamic.kabel-deutschland.de. [188.193.170.75]) by smtp.gmail.com with ESMTPSA id 130sm1533554wmd.18.2020.10.20.00.59.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Oct 2020 00:59:30 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Tue, 20 Oct 2020 09:53:55 +0200 Message-Id: <20201020075356.185676-4-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201020075356.185676-1-andreas.rheinhardt@gmail.com> References: <20201020075356.185676-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 4/4] avcodec/vp3: Make parsing Theora Huffman tables more spec-compliant 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" Theora allows to use custom Huffman tables which are coded in the bitstream as a tree: Whether the next node is a leaf or not is coded in a bit; each node itself contains a five bit token. Each tree can contain at most 32 leafs; typically they contain exactly 32 with the 32 symbols forming a permutation of 0..31. Yet the standard does not impose either of these requirements. It explicitly allows less than 32 leafs and multiple codes with the same token. But our decoder used an algorithm that required the codes->token mapping to be injective and that also presumed that there be at least two leafs: Instead of using an array for codes, tokens and code lengths, the decoder only had arrays for codes and code lengths. The code and length for a given token were stored in entry[token]. As no symbols table was used when initializing the VLC, the default one applied and therefore the entry[token] got the symbol token (if the length of said entry is >0). Yet if multiple codes had the same token, the codes and lengths from the later token would overwrite the earlier codes and lengths. Furthermore, less than 32 leafs could also lead to problems: Namely if this was not the first time Huffman tables have been parsed in which case the array is not zeroed initially so that old entries could make the new table invalid. libtheora seems to always use 32 leafs and no duplicate tokens; I am not aware of any existing valid files that do not. This is fixed by using a codes, symbols and lengths array when initializing the VLC. In order to reduce the amount of stuff kept in the context only the symbols and lengths (which both fit into an uint8_t) are kept in the context; the codes are derived from the lengths immediately before creating the tables. There is now only one thing left which is not spec-compliant: Trees with only one node (which has length zero) are not supported by ff_init_vlc_sparse() yet. Signed-off-by: Andreas Rheinhardt --- My initial aim with this was btw to switch all the callers ff_init_vlc_sparse() with bits_size != 1 to use only uint8_t for the bits (== lengths) to simplify reading them in ff_init_vlc_sparse(). libavcodec/vp3.c | 85 ++++++++++++++++++++++++------------------------ 1 file changed, 43 insertions(+), 42 deletions(-) diff --git a/libavcodec/vp3.c b/libavcodec/vp3.c index e629a18b8e..a6e9d44302 100644 --- a/libavcodec/vp3.c +++ b/libavcodec/vp3.c @@ -155,6 +155,15 @@ typedef struct { #define MIN_DEQUANT_VAL 2 +typedef struct HuffEntry { + uint8_t len, sym; +} HuffEntry; + +typedef struct HuffTable { + HuffEntry entries[32]; + uint8_t nb_entries; +} HuffTable; + typedef struct Vp3DecodeContext { AVCodecContext *avctx; int theora, theora_tables, theora_header; @@ -285,11 +294,7 @@ typedef struct Vp3DecodeContext { uint8_t *edge_emu_buffer; /* Huffman decode */ - int hti; - unsigned int hbits; - int entries; - int huff_code_size; - uint32_t huffman_table[80][32][2]; + HuffTable huffman_table[80]; uint8_t filter_limit_values[64]; DECLARE_ALIGNED(8, int, bounding_values_array)[256 + 2]; @@ -2309,6 +2314,20 @@ static av_cold int init_frames(Vp3DecodeContext *s) return 0; } +static av_cold int theora_init_huffman_tables(VLC *vlc, const HuffTable *huff) +{ + uint32_t code = 0, codes[32]; + + for (unsigned i = 0; i < huff->nb_entries; i++) { + codes[i] = code >> (31 - huff->entries[i].len); + code += 0x80000000U >> huff->entries[i].len; + } + return ff_init_vlc_sparse(vlc, 11, huff->nb_entries, + &huff->entries[0].len, sizeof(huff->entries[0]), 1, + codes, 4, 4, + &huff->entries[0].sym, sizeof(huff->entries[0]), 1, 0); +} + static av_cold int vp3_decode_init(AVCodecContext *avctx) { Vp3DecodeContext *s = avctx->priv_data; @@ -2432,10 +2451,9 @@ static av_cold int vp3_decode_init(AVCodecContext *avctx) } } else { for (i = 0; i < 80; i++) { - if (init_vlc(&s->xc_vlc[i], 11, 32, - &s->huffman_table[i][0][1], 8, 4, - &s->huffman_table[i][0][0], 8, 4, 0) < 0) - goto vlc_fail; + ret = theora_init_huffman_tables(&s->xc_vlc[i], &s->huffman_table[i]); + if (ret < 0) + return ret; } } @@ -2476,10 +2494,6 @@ static av_cold int vp3_decode_init(AVCodecContext *avctx) #endif return allocate_tables(avctx); - -vlc_fail: - av_log(avctx, AV_LOG_FATAL, "Invalid huffman table\n"); - return -1; } /// Release and shuffle frames after decode finishes @@ -2811,36 +2825,30 @@ error: return ret; } -static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb) +static int read_huffman_tree(HuffTable *huff, GetBitContext *gb, int length, + AVCodecContext *avctx) { - Vp3DecodeContext *s = avctx->priv_data; - if (get_bits1(gb)) { int token; - if (s->entries >= 32) { /* overflow */ + if (huff->nb_entries >= 32) { /* overflow */ av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n"); return -1; } token = get_bits(gb, 5); - ff_dlog(avctx, "hti %d hbits %x token %d entry : %d size %d\n", - s->hti, s->hbits, token, s->entries, s->huff_code_size); - s->huffman_table[s->hti][token][0] = s->hbits; - s->huffman_table[s->hti][token][1] = s->huff_code_size; - s->entries++; + ff_dlog(avctx, "code length %d, curr entry %d, token %d\n", + length, huff->nb_entries, token); + huff->entries[huff->nb_entries++] = (HuffEntry){ length, token }; } else { - if (s->huff_code_size >= 32) { /* overflow */ + /* The following bound follows from the fact that nb_entries <= 32. */ + if (length >= 31) { /* overflow */ av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n"); return -1; } - s->huff_code_size++; - s->hbits <<= 1; - if (read_huffman_tree(avctx, gb)) + length++; + if (read_huffman_tree(huff, gb, length, avctx)) return -1; - s->hbits |= 1; - if (read_huffman_tree(avctx, gb)) + if (read_huffman_tree(huff, gb, length, avctx)) return -1; - s->hbits >>= 1; - s->huff_code_size--; } return 0; } @@ -2965,7 +2973,7 @@ static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb) static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb) { Vp3DecodeContext *s = avctx->priv_data; - int i, n, matrices, inter, plane; + int i, n, matrices, inter, plane, ret; if (!s->theora_header) return AVERROR_INVALIDDATA; @@ -3057,17 +3065,10 @@ static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb) } /* Huffman tables */ - for (s->hti = 0; s->hti < 80; s->hti++) { - s->entries = 0; - s->huff_code_size = 1; - if (!get_bits1(gb)) { - s->hbits = 0; - if (read_huffman_tree(avctx, gb)) - return -1; - s->hbits = 1; - if (read_huffman_tree(avctx, gb)) - return -1; - } + for (int i = 0; i < 80; i++) { + s->huffman_table[i].nb_entries = 0; + if ((ret = read_huffman_tree(&s->huffman_table[i], gb, 0, avctx)) < 0) + return ret; } s->theora_tables = 1;