From patchwork Thu Mar 25 15:49:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 26597 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 4CA1F44B722 for ; Thu, 25 Mar 2021 17:50:13 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 20EFD689F2C; Thu, 25 Mar 2021 17:50:13 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f50.google.com (mail-ed1-f50.google.com [209.85.208.50]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E418068090B for ; Thu, 25 Mar 2021 17:50:06 +0200 (EET) Received: by mail-ed1-f50.google.com with SMTP id h13so2958549eds.5 for ; Thu, 25 Mar 2021 08:50:06 -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:reply-to:mime-version :content-transfer-encoding; bh=xr/eqk5p16jTYjdIs8CJWYjgR8Rb5iG470eDNn6HwGk=; b=RrLlcMvKRV4O1aBUJHHA9wZ7z91KdS4uQrY2oLyl0xYw9GSB2kqJvARrT1vdumrPjE gBFK+XhPGdIzRfrs3nJyXBjLa79wcz9vzbpsGJxhjUjKU5PBSetkjAnPiqmpAYOEhyqs /PPvJsOvOXtMZZMB46TxQZpYmdpYrzzLHvMiW/RSkno9Y+hl9ZEN+68zL39/AtAS1hVd AZ0coK25+qZTltmOjcQU3d7NDeLlHHVncx3o8OJIvc6wSSQi99gHuSyTJNdZrSEm5lrE 6fhNtuBgsqErIMJMq3Cuh8kPE3MyMoMf1P6TF1jTuSzkgZYm/1FlAEmilhDlI79nvTuS QCGg== 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:reply-to :mime-version:content-transfer-encoding; bh=xr/eqk5p16jTYjdIs8CJWYjgR8Rb5iG470eDNn6HwGk=; b=rgheQ0DH4Xx/YZCnGZysBzdk7asWg9xAd+j0IDdUUrVsAFEG/GTIdyTq4SY9jKPqw/ kLutRm0lHhXo6IljJ8NqwUuxNqXfmdlsd+UOFAjbMHoOsR2+zaDn82vIkcJOZLgmUN9/ AacinRt2a+gfvpY+uRhcNtRbqIMne5dtNj+CE1PxOPiHpiq3b5IJoMDTgbH081dju/0z FJOVL1dOnTtqM2UiEDR7iQ/Cthy5XiXVvZRojQ0ADYSwd0DHxfCnBJ5FUwsHU/C5Fvkj vP1On74e9qigUbj9p3XEWDaUpMSB87nK49w1i0KgtqQCK5/23FdcG2Qaeb1AvBygaopY S5/w== X-Gm-Message-State: AOAM531eRWpv8JIchixyhg9WF77NuE6DHS1VTcUHiAt3iL89Ke7tnyhD Czv7sFgPJ4C5TN8ho1MMZ3bELF9MQZ79Mg== X-Google-Smtp-Source: ABdhPJxhdmtlVnHJHbmIKV/OMQf3IUosS4BGsEheYdS6lrzpUeE61QJwXWpZ3Qem7xCtoIUGFi/IiA== X-Received: by 2002:a05:6402:5203:: with SMTP id s3mr10003361edd.79.1616687405394; Thu, 25 Mar 2021 08:50:05 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc08960.dynamic.kabel-deutschland.de. [188.192.137.96]) by smtp.gmail.com with ESMTPSA id o6sm2859103edw.24.2021.03.25.08.50.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Mar 2021 08:50:04 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 25 Mar 2021 16:49:45 +0100 Message-Id: <20210325154956.2405162-1-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 01/12] avcodec/vorbisenc: Remove always-false check 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 PutBitContext is big enough: It has just been initialized to 8192B. Signed-off-by: Andreas Rheinhardt --- libavcodec/vorbisenc.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/libavcodec/vorbisenc.c b/libavcodec/vorbisenc.c index 18a679f2dc..f8a08f816f 100644 --- a/libavcodec/vorbisenc.c +++ b/libavcodec/vorbisenc.c @@ -1135,11 +1135,6 @@ static int vorbis_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, init_put_bits(&pb, avpkt->data, avpkt->size); - if (pb.size_in_bits - put_bits_count(&pb) < 1 + ilog(venc->nmodes - 1)) { - av_log(avctx, AV_LOG_ERROR, "output buffer is too small\n"); - return AVERROR(EINVAL); - } - put_bits(&pb, 1, 0); // magic bit put_bits(&pb, ilog(venc->nmodes - 1), 1); // Mode for current frame From patchwork Thu Mar 25 15:49:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 26598 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 5B27A44B722 for ; Thu, 25 Mar 2021 17:50:38 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4490F68A9C9; Thu, 25 Mar 2021 17:50:38 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f44.google.com (mail-ed1-f44.google.com [209.85.208.44]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 82D8768A9A8 for ; Thu, 25 Mar 2021 17:50:32 +0200 (EET) Received: by mail-ed1-f44.google.com with SMTP id y6so2987224eds.1 for ; Thu, 25 Mar 2021 08:50: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:reply-to :mime-version:content-transfer-encoding; bh=nj2Uu0gAMsWyMS4VGz/vxHErYBpyFtpz741WAunVZ90=; b=k5JwWZuP+Ubbgz3MGoIxU6IZhIi8FkNoGK2y9opss5hsthUY9Ow1gIFgAsoRSTSbcC QDWyuyExAmV0XnylOeHc0dpR6e6Fv07IGbfuvwjeRPV0Q1ZyQbhAWKB4Q9olSVcPwSwk JauNK9s60YDR7CscRpEFgRiKSgIGNIqNu4B0ZVP/LkZFYkadGpcsqw0WqaC/195X3auh abPwOxKyE7AIHtPf76NxeCkoV9eDN9ejDKIwpgLCkoi6L00wGx4pXbKaTV1q8Giw+KOl LNrZPBfZwt8AVFXDuIZ41HJRWd/AgORw3Fj3GRPfYJ6tjpWHgH4YOAKUMl9D7iXt4KtJ ADRA== 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:reply-to:mime-version:content-transfer-encoding; bh=nj2Uu0gAMsWyMS4VGz/vxHErYBpyFtpz741WAunVZ90=; b=Hg95viZifWpHMt/bbWupG6wmYu0XKXJGJ6lDu36x7Zinavwcm+FyyqMegEhictLSy4 QcjLStwAtuffxUjZwFjw36f7kDhHFQEo0J4VgBSJwcq+hmt2/Sv+LjVjsCb5y/2Z3cHS nea2qw0BScODBD8muTCwa2JYLQurhnKOl/H2lcz3o9aN1tXlyJKV54CHPzFxj6MsVE3U odw/gPJhpi7yebYw/IVBSCAM5eM+9rONIP8+dGIjGMxttqX8easqKr36V6n7XVM7qgNv yTZQrrAKFQwgaCvmr5HHSNP6cTnecyOXscH7+3H+DpLxOPmG6pau+qbRPV3aC+AeJuqG inKQ== X-Gm-Message-State: AOAM531ycdUHzkSdCYAVHr0JhA8wGBxvkdAzXUiwXO9Nd0C22VbbGbHZ m+xDNAzLy5ZQz1lQn+ih1Hf1k9RrPSGJdA== X-Google-Smtp-Source: ABdhPJyBwHVYdUxqPmMdqQFi6vP1fF99qdsgqppv+HaH3ve6XTR49Q48c+DgrUMMd11yo/ifrMB5Xw== X-Received: by 2002:a50:fd15:: with SMTP id i21mr9540479eds.384.1616687431912; Thu, 25 Mar 2021 08:50:31 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc08960.dynamic.kabel-deutschland.de. [188.192.137.96]) by smtp.gmail.com with ESMTPSA id o6sm2859103edw.24.2021.03.25.08.50.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Mar 2021 08:50:31 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 25 Mar 2021 16:49:46 +0100 Message-Id: <20210325154956.2405162-2-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210325154956.2405162-1-andreas.rheinhardt@gmail.com> References: <20210325154956.2405162-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 02/12] avcodec/sonic: Remove outdated outcommented line 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" Compilation would fail if it were outcommented as it refers to a nonexistent PutBitContext. Signed-off-by: Andreas Rheinhardt --- libavcodec/sonic.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/libavcodec/sonic.c b/libavcodec/sonic.c index 32e94d24f6..8339799dd3 100644 --- a/libavcodec/sonic.c +++ b/libavcodec/sonic.c @@ -832,8 +832,6 @@ static int sonic_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, return ret; } -// av_log(avctx, AV_LOG_DEBUG, "used bytes: %d\n", (put_bits_count(&pb)+7)/8); - avpkt->size = ff_rac_terminate(&c, 0); *got_packet_ptr = 1; return 0; From patchwork Thu Mar 25 15:49:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 26599 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 5CD3944B722 for ; Thu, 25 Mar 2021 17:50:40 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 40AF568AAD5; Thu, 25 Mar 2021 17:50:40 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f50.google.com (mail-ej1-f50.google.com [209.85.218.50]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 73D5A68AA89 for ; Thu, 25 Mar 2021 17:50:33 +0200 (EET) Received: by mail-ej1-f50.google.com with SMTP id u21so3657328ejo.13 for ; Thu, 25 Mar 2021 08:50:33 -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:reply-to :mime-version:content-transfer-encoding; bh=zVFIwzQgcjRy9of+XaQohlkvWes/jS8cst5HnOZjqUI=; b=Ki0TcEk2i7omfz8dpf26nr1zbcKihaohlH4u+Tdi0MIv8RbN822avSyUe0bk7HaaJy enCNKmHUM0y0ZFSzHG7FA4SsWZ2AFaoAajsPI0bY457AfE+QzqIY8AEyXjgVO/OKLIee EFMss/urGg29/cmRRHfYsDdOzYbGATf2+gPHJT3oAp1VYhJcHd4HBrZd4cQ7tciq0tj/ S1CdaYdvb4AEPDjlk/wcFSZSSUpHWpXoQPmU4WJnlMiGpTMAlnooD0vxlVP73To/uR/n NSw9TGW0/Ae1hPNnUhjjx2Wyw9TfhY5tLnVlSjM6lGdf46VSlL5l2BqcGusPU+SWtmIJ T/QA== 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:reply-to:mime-version:content-transfer-encoding; bh=zVFIwzQgcjRy9of+XaQohlkvWes/jS8cst5HnOZjqUI=; b=XYKFqUcXfxTy82LOzBOq7biTa1t+51WmItR3yNJ8OfvEeJ4hm+amMcYHGIU0Kg8ivh 6zRFPTAMgIm0yugRN9M3BMX1/yUdauNf6ozPnzVq+vYN2hyvmNKUzoZsWtgg3olY839R ZyO4AvHQSBWP66tfXl5oiuRmStftMOxqdrhFqGYWCllOPgB6HaZ2byfJeA/WfIILyn68 uDQacQJMmXGdtOo4MfoNnCMtr/OiZ5Ed+Rx9/tKIhZvQafmujfkYRku0tgQ8nkKZOo7I OmEUJ5u8sRhfzLiiXZQE337gmWiWcVNlo6MSw2Lvo6Rxtgn/xIJvKmJe3zISW3KyVkwA hx5w== X-Gm-Message-State: AOAM532XPuWKjinSK+WhiinY5dscqAqPkaSHnJo9U14T07kmDgtqz/y5 jizK1g/9OKfTprc7jOr/Hg6R/EVQpaKY/Q== X-Google-Smtp-Source: ABdhPJz2N8DoMiIIlylIeZX+u6v0gWWd+L419BOvM5ozpMru6i/utTz52zCz7wAoW4bbbrCBZL7tGQ== X-Received: by 2002:a17:906:5e4a:: with SMTP id b10mr10256842eju.116.1616687432656; Thu, 25 Mar 2021 08:50:32 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc08960.dynamic.kabel-deutschland.de. [188.192.137.96]) by smtp.gmail.com with ESMTPSA id o6sm2859103edw.24.2021.03.25.08.50.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Mar 2021 08:50:32 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 25 Mar 2021 16:49:47 +0100 Message-Id: <20210325154956.2405162-3-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210325154956.2405162-1-andreas.rheinhardt@gmail.com> References: <20210325154956.2405162-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 03/12] avcodec/mlpenc: Avoid redundant temporary PutBitContext 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" We are already word-aligned here, so one can just as well flush the main PutBitContext. Signed-off-by: Andreas Rheinhardt --- libavcodec/mlpenc.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libavcodec/mlpenc.c b/libavcodec/mlpenc.c index 9782cb8261..40db76f350 100644 --- a/libavcodec/mlpenc.c +++ b/libavcodec/mlpenc.c @@ -1067,7 +1067,7 @@ static uint8_t *write_substrs(MLPEncodeContext *ctx, uint8_t *buf, int buf_size, RestartHeader *rh = &ctx->restart_header [substr]; int substr_restart_frame = restart_frame; uint8_t parity, checksum; - PutBitContext pb, tmpb; + PutBitContext pb; int params_changed; ctx->cur_restart_header = rh; @@ -1117,9 +1117,9 @@ static uint8_t *write_substrs(MLPEncodeContext *ctx, uint8_t *buf, int buf_size, put_bits(&pb, 32, END_OF_STREAM); } - /* Data must be flushed for the checksum and parity to be correct. */ - tmpb = pb; - flush_put_bits(&tmpb); + /* Data must be flushed for the checksum and parity to be correct; + * notice that we already are word-aligned here. */ + flush_put_bits(&pb); parity = ff_mlp_calculate_parity(buf, put_bits_count(&pb) >> 3) ^ 0xa9; checksum = ff_mlp_checksum8 (buf, put_bits_count(&pb) >> 3); From patchwork Thu Mar 25 15:49:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 26600 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 7EC6244B722 for ; Thu, 25 Mar 2021 17:50:42 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 6455E68AB29; Thu, 25 Mar 2021 17:50:42 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f53.google.com (mail-ed1-f53.google.com [209.85.208.53]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3686768AACC for ; Thu, 25 Mar 2021 17:50:34 +0200 (EET) Received: by mail-ed1-f53.google.com with SMTP id dm8so2977596edb.2 for ; Thu, 25 Mar 2021 08:50:34 -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:reply-to :mime-version:content-transfer-encoding; bh=e4FiIJa5VIbfo6oxcoD19qlKQJLvsEbMTaah4tVz7AY=; b=cR+DoW4z7T4/55YEUzKnzievOdEfBWVRtABaURpMAz33hBBeRMKy37PLxslJ9hYDKJ Rm+n5QPWfiI3LpWKg1TPgtf8W1eRsV82aFBzrGdRdDQf1vpk68gPUd4Cr7fLCjfuPXCy 1xlCEBHSgG0iL5nyNI/ft/pB+VY44wz30fbD4j3YGMDotBeHzpXoYlKNgX+JPnMwbG6w dfUTSEzadWBptNmAl/zafu1BHBP4jKajV+z9OkNuJ+GoAL1w/LNTaE416+Pw63Ta1riI epg3ziY28WsPUiBHIaphnG+6xPZJ/i7NHNv9eTUpG+Is/in2+8LBfgnksEI22mI6eZG9 dJxA== 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:reply-to:mime-version:content-transfer-encoding; bh=e4FiIJa5VIbfo6oxcoD19qlKQJLvsEbMTaah4tVz7AY=; b=qWJIn/+9hbMv0nn3Tk2RvXontZk1Jgu9NAfImzs2tldTUlbQBnhlxiu0jt9tMDq6YP Sg29/W6MdvGCmL1lhu9g0PG/MysoonA0sWCe5uWV1+o91skp65K9BJsi3WnDbHObk+Z2 T6IRmywwjN0de+ewRgJ0V7ltLlR6OymljkurhkzqfndRmJtd4dDaP1f8bwv9LTh0jpZQ bJf0qKVsmsLzQhObB5weZeUBk4cuLmxdaP1RASZ/HyJsKINrJtKdWmsWl73aVgqvgAT9 WDr4jII9sohomiXqfSym+UdRiaAw4qaSQvpmtkEmGK6S8t1F0Pj9YqEhaeD7LIxUjxyf 6VTw== X-Gm-Message-State: AOAM533J/quKO1qkOU25CMLqqRXvZMPT+l38Y0eg0l0Dzm3QVMvBv2i3 D2qAspeC67CXy/gk8oOtj04fultBbtHzPg== X-Google-Smtp-Source: ABdhPJzM4BAC/TGy2YvfSjhYIYX6eS6uY0lahqMFZtmn1h6p5nLwxoyuZvvxSMQCHD3+hsXEaVdSzQ== X-Received: by 2002:aa7:cd0e:: with SMTP id b14mr10087070edw.354.1616687433487; Thu, 25 Mar 2021 08:50:33 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc08960.dynamic.kabel-deutschland.de. [188.192.137.96]) by smtp.gmail.com with ESMTPSA id o6sm2859103edw.24.2021.03.25.08.50.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Mar 2021 08:50:33 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 25 Mar 2021 16:49:48 +0100 Message-Id: <20210325154956.2405162-4-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210325154956.2405162-1-andreas.rheinhardt@gmail.com> References: <20210325154956.2405162-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 04/12] avcodec/dvenc: Remove dead error message 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 PutBits API checks the available space before every write, so this check for overread is dead. Signed-off-by: Andreas Rheinhardt --- libavcodec/dvenc.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/libavcodec/dvenc.c b/libavcodec/dvenc.c index 233e2b68c7..134315a834 100644 --- a/libavcodec/dvenc.c +++ b/libavcodec/dvenc.c @@ -980,11 +980,6 @@ static int dv_encode_video_segment(AVCodecContext *avctx, void *arg) int size = pbs[j].size_in_bits >> 3; flush_put_bits(&pbs[j]); pos = put_bits_count(&pbs[j]) >> 3; - if (pos > size) { - av_log(avctx, AV_LOG_ERROR, - "bitstream written beyond buffer size\n"); - return -1; - } memset(pbs[j].buf + pos, 0xff, size - pos); } From patchwork Thu Mar 25 15:49:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 26601 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 5403844B722 for ; Thu, 25 Mar 2021 17:50:44 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 2AC5068AAC7; Thu, 25 Mar 2021 17:50:44 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f41.google.com (mail-ej1-f41.google.com [209.85.218.41]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0AF2D687FE0 for ; Thu, 25 Mar 2021 17:50:35 +0200 (EET) Received: by mail-ej1-f41.google.com with SMTP id k10so3773262ejg.0 for ; Thu, 25 Mar 2021 08:50:35 -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:reply-to :mime-version:content-transfer-encoding; bh=Tyt2eJi+oafMjjBPvA1TgKuO6cEySW4aGtimWOSh6AU=; b=f152CfogqbE2pEnTPVustvRWeoXxgHFaVoIkB5i7OGGaq5Pr9/hkt+ao0bl8xaOpvl dBLrHdwa00CLgMf8F0xYBP/K3NOPalTwJVife/JucjabhHUcIxCMkTtEs3Oslzzduxik yhtpAmlYYXpJk0WLkhu/ApTmBk0lhFok3+uYn+cDBkhvpdJ4GzdD6TseFPgWJUrEheb7 3SF/QRgMmLZGLHSgy5vhkJmiJzxibWy9VRKyetsn07R4JnOmPJgyXlhpHMnl+nZ7mF/g HHTi+WnzUJWpJfpLKiEEXg+7AM0f+xcJpz8JkESebNAzB5tbmR2L/qWLGDLCyZ6VqUHm pBqQ== 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:reply-to:mime-version:content-transfer-encoding; bh=Tyt2eJi+oafMjjBPvA1TgKuO6cEySW4aGtimWOSh6AU=; b=Yf939AAlex2hqz/9Z6lNkJBLNaPbuxkoVHH9MfukLHsRHMZKvJfKBXfMhICJpgM/C+ jDRUknqWZO2wi7zUVY0m40S8OP3VkKVxPtUp3sXj1pOwUtrh3FijVg9S5Qxb8F1jXiuY soZdoAIL4+u5Vf2Ed8PIzpw8g1JlsxO+cpwrvYhMt/MufRVvw+1grOZ5ueSHaUhpJN60 wpj04h8opf7h4YHZnIflz/nrc9UD2+gP7G/B4RighyjguZ7tTNiZdIVLMtOx5g/12QKl CDM7ELAZFyZ3yerpdLuXE/q5iN9pz6KHd17CHWHnP+u66upQbymRHHJoke3SKkskuHzw KaYg== X-Gm-Message-State: AOAM532IXijBnZUPI9HhyFV5aIBli40MG9Cbg5Ae4uFbGJBko1LGa8LY JlLgAwZ8poQmponEwgPADQlZ38DDK506dw== X-Google-Smtp-Source: ABdhPJwePYIQ/Aci0o4xDcmtIq8a1cB6Kmnt4zT6HQSsahARS5vYS53UyJn1advdGus3wZ0CP6ChyQ== X-Received: by 2002:a17:906:7c48:: with SMTP id g8mr10525995ejp.138.1616687434318; Thu, 25 Mar 2021 08:50:34 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc08960.dynamic.kabel-deutschland.de. [188.192.137.96]) by smtp.gmail.com with ESMTPSA id o6sm2859103edw.24.2021.03.25.08.50.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Mar 2021 08:50:34 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 25 Mar 2021 16:49:49 +0100 Message-Id: <20210325154956.2405162-5-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210325154956.2405162-1-andreas.rheinhardt@gmail.com> References: <20210325154956.2405162-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 05/12] avcodec/vorbisenc, wmavoice: Use put_bits_left() where appropriate 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/vorbisenc.c | 4 ++-- libavcodec/wmavoice.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libavcodec/vorbisenc.c b/libavcodec/vorbisenc.c index f8a08f816f..080017e2bf 100644 --- a/libavcodec/vorbisenc.c +++ b/libavcodec/vorbisenc.c @@ -155,7 +155,7 @@ static inline int put_codeword(PutBitContext *pb, vorbis_enc_codebook *cb, av_assert2(entry >= 0); av_assert2(entry < cb->nentries); av_assert2(cb->lens[entry]); - if (pb->size_in_bits - put_bits_count(pb) < cb->lens[entry]) + if (put_bits_left(pb) < cb->lens[entry]) return AVERROR(EINVAL); put_bits(pb, cb->lens[entry], cb->codewords[entry]); return 0; @@ -798,7 +798,7 @@ static int floor_encode(vorbis_enc_context *venc, vorbis_enc_floor *fc, int coded[MAX_FLOOR_VALUES]; // first 2 values are unused int i, counter; - if (pb->size_in_bits - put_bits_count(pb) < 1 + 2 * ilog(range - 1)) + if (put_bits_left(pb) < 1 + 2 * ilog(range - 1)) return AVERROR(EINVAL); put_bits(pb, 1, 1); // non zero put_bits(pb, ilog(range - 1), posts[0]); diff --git a/libavcodec/wmavoice.c b/libavcodec/wmavoice.c index fbdb865360..e76807faa5 100644 --- a/libavcodec/wmavoice.c +++ b/libavcodec/wmavoice.c @@ -1881,7 +1881,7 @@ static void copy_bits(PutBitContext *pb, rmn_bits = rmn_bytes = get_bits_left(gb); if (rmn_bits < nbits) return; - if (nbits > pb->size_in_bits - put_bits_count(pb)) + if (nbits > put_bits_left(pb)) return; rmn_bits &= 7; rmn_bytes >>= 3; if ((rmn_bits = FFMIN(rmn_bits, nbits)) > 0) From patchwork Thu Mar 25 15:49:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 26603 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 5A76D44B722 for ; Thu, 25 Mar 2021 17:50:47 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3EF3C68AB81; Thu, 25 Mar 2021 17:50:47 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f46.google.com (mail-ej1-f46.google.com [209.85.218.46]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C68A668AAFB for ; Thu, 25 Mar 2021 17:50:35 +0200 (EET) Received: by mail-ej1-f46.google.com with SMTP id u9so3707412ejj.7 for ; Thu, 25 Mar 2021 08:50:35 -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:reply-to :mime-version:content-transfer-encoding; bh=StILkUjP3Ybp3120PLrMtkjmDYFmAtUeAsLrKngHB5M=; b=ZRnlTdn5kUMT67JTMmht9P0u/9sGOEpnLNJMp1QiavUUPZa0kHk2tup1vSUII/jW6G 8PZjw3qrNRSEzBUr2Oaw1FlyyrFa/9KnFUTZMfDshdYhvaE2Q4WRC4fDn48pjwG08tzI xxXH3D2xms/K1tdZfQls5HFIQ+Y7caTVeqmOKvSHUJLfWLE5edaB5+z61u3ZTdbvu/sT 1wkkng5F2OIz3qhc6GrL5q4GjwCLgILomRuLzW6ul9SPcT0LkILIUjfrHKi/A5g3yOD0 CX8SNEOjrPlO3eqfyIrSLUqY6nlutORSr3OSzGD++L5BFJeZ/uNptg+O6ubUblDBTWHr 4P0A== 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:reply-to:mime-version:content-transfer-encoding; bh=StILkUjP3Ybp3120PLrMtkjmDYFmAtUeAsLrKngHB5M=; b=bQVVx8c0bfHVO7x2RSGwP48pjgSDDQHuTpYIw2wIuXjwHPU56JOciwS3CCWpdKIlwv eHaCbWZUgvR4H06yKeygMcWFg3YodQPlX4JovMlElyMjVoomQbC4t8vcv9MbycQ/8s31 h9Mi5lb8EwpOxWGx28lbcztyBbIVxvVBZ/HIdPyqM87m835xPQwpSoOG26DIUW1dC21S GSMBocdoauB8QFi854hLlAr0GP+OZLj0XAuAKtEIaWUI42PVZRzBB20D+engeLgtv1O0 FOmfpljZkr2MBZernO2jMgA35PsQfYGZS0y3RklDYVjEvWQb1EIaHVRFmyInD3COGYIz kQqg== X-Gm-Message-State: AOAM532K3c3WNGNufJ4uusVaeuiGNm4MIuL09DOSXOpnUrmXy4rMCWAM Cd0FV/R7h7xJ9lqf8R5BFyUmWx+uKaKSpw== X-Google-Smtp-Source: ABdhPJwapCgTHMWgGrOMZuogiCp9lAp/99hMigXAi7WnO7DXnfeSs0CqLG4By4C0JhQf+xyhPpZ2Sg== X-Received: by 2002:a17:906:33d9:: with SMTP id w25mr10672155eja.413.1616687435039; Thu, 25 Mar 2021 08:50:35 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc08960.dynamic.kabel-deutschland.de. [188.192.137.96]) by smtp.gmail.com with ESMTPSA id o6sm2859103edw.24.2021.03.25.08.50.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Mar 2021 08:50:34 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 25 Mar 2021 16:49:50 +0100 Message-Id: <20210325154956.2405162-6-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210325154956.2405162-1-andreas.rheinhardt@gmail.com> References: <20210325154956.2405162-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 06/12] avcodec/put_bits: Add functions for amount of bytes written/left 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" Often a caller doesn't want the amount of bits written via a PutBitContext, but the amount of bytes. This in particular happens after one has flushed the PutBitContext (e.g. at the end of encoding, when one wants to know the actual packet size). The current way of doing this is with put_bits_count(pb)/8 (or (put_bits_count(pb) + 7)/8). Yet this has some issues: It contains implicit multiplications and divisions by 8 with a cast in between; it obscurs the intent; and it restricts the size of the buffer to (currently) INT_MAX/8 (or to 1/8 of the maximum of whatever put_bits_count() returns), although said restriction is not really necessary for users that don't need a bitcount. Corresponding functions for the amount of bytes left have also been addded. Signed-off-by: Andreas Rheinhardt --- libavcodec/put_bits.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/libavcodec/put_bits.h b/libavcodec/put_bits.h index cd66a82a44..e8bc86a82c 100644 --- a/libavcodec/put_bits.h +++ b/libavcodec/put_bits.h @@ -85,6 +85,26 @@ static inline int put_bits_count(PutBitContext *s) return (s->buf_ptr - s->buf) * 8 + BUF_BITS - s->bit_left; } +/** + * @return the number of bytes output so far; may only be called + * when the PutBitContext is freshly initialized or flushed. + */ +static inline int put_bytes_output(const PutBitContext *s) +{ + av_assert2(s->bit_left == BUF_BITS); + return s->buf_ptr - s->buf; +} + +/** + * @param round_up When set, the number of bits written so far will be + * rounded up to the next byte. + * @return the number of bytes output so far. + */ +static inline int put_bytes_count(const PutBitContext *s, int round_up) +{ + return s->buf_ptr - s->buf + ((BUF_BITS - s->bit_left + (round_up ? 7 : 0)) >> 3); +} + /** * Rebase the bit writer onto a reallocated buffer. * @@ -111,6 +131,16 @@ static inline int put_bits_left(PutBitContext* s) return (s->buf_end - s->buf_ptr) * 8 - BUF_BITS + s->bit_left; } +/** + * @param round_up When set, the number of bits written will be + * rounded up to the next byte. + * @return the number of bytes left. + */ +static inline int put_bytes_left(const PutBitContext *s, int round_up) +{ + return s->buf_end - s->buf_ptr - ((BUF_BITS - s->bit_left + (round_up ? 7 : 0)) >> 3); +} + /** * Pad the end of the output stream with zeros. */ From patchwork Thu Mar 25 15:49:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 26605 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 64F9F44B722 for ; Thu, 25 Mar 2021 17:50:49 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 52D9C68AB79; Thu, 25 Mar 2021 17:50:49 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f41.google.com (mail-ej1-f41.google.com [209.85.218.41]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 8389568A9A8 for ; Thu, 25 Mar 2021 17:50:36 +0200 (EET) Received: by mail-ej1-f41.google.com with SMTP id a7so3728016ejs.3 for ; Thu, 25 Mar 2021 08:50:36 -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:reply-to :mime-version:content-transfer-encoding; bh=wSJlso4jk23n+5bd8joO4eQ0Jwyv4V3Ua8LvXSP2wuM=; b=M9/cKWrbo7PW3OP0+Je4/jqDxbkqstMc4RNs6TzuxqjEanbqAfHXqfPVqwzdT+rJTZ HnaMl8YwBqfTsLshHJc2yjmlXB/0dzOgXvPizfA2ANIj5vGAnAd0a9YNHIRr2X4mi3zf T894XPmQEfMl74sXFlXmJ6quZA1J3EkWWFmiegX4ZxqTHxhmCe+Me3a6Dhuv8eVYRxgW VBzNoieILDUYjS1wyk46VC6OgMWx0n7jXBCOlrpDo6DALL1NU+LVLzDTlC4IvLbK9S6j 9an4y26EDVpHx+5wJ6vS7eyH+C9Od8/lua427QGGd/6Z+cYaADNZ68fEvOfUsynWy624 SICg== 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:reply-to:mime-version:content-transfer-encoding; bh=wSJlso4jk23n+5bd8joO4eQ0Jwyv4V3Ua8LvXSP2wuM=; b=JOT21I7rUtrW1HkBfC475dIe0vy8UV0MK2ozK4rp8IcgacxS1S+FDzK3TA3q+LpcCw ijF0eMcnyP6MXeZwXpr7nDfZbo5yrqPvDUyRH5Q0iw+hVLYu0FhfuWia0mOJjQHPI6UL aIqQc8rYsCf/sLtZTIcdsSqtSVQDx6AimoPJ5A5jfT35xT/yuY3AM+Eun3zlOxm5pYiB 47zsq43behRB6fNl0AHUuqsqy4exPEuBNiPbj2mrZyxel67goz8qaJLDPXr8n+CGkhBH G9T1d9NV71ctN8i66NvSlHWA4mqxIh4gxcQnKf1B5vEusqFsFu3PI38F3p7/Ut7MWU51 +Bmw== X-Gm-Message-State: AOAM530VIdGm5ipn2IJO2s5Nw4Gaq73+IIeWwG6E64VFRdadEvgzIKIw lofhlubFpbjt+TLlaiPeF9BHJNkfhkbNqA== X-Google-Smtp-Source: ABdhPJwNlTZBl6d04tIiq3+I8yg9tycbxnXgI8Ro4Go3uKtGgcqhLef+vaOmAKAJJD+3jzi0FzA3qw== X-Received: by 2002:a17:907:2b03:: with SMTP id gc3mr10238085ejc.448.1616687435738; Thu, 25 Mar 2021 08:50:35 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc08960.dynamic.kabel-deutschland.de. [188.192.137.96]) by smtp.gmail.com with ESMTPSA id o6sm2859103edw.24.2021.03.25.08.50.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Mar 2021 08:50:35 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 25 Mar 2021 16:49:51 +0100 Message-Id: <20210325154956.2405162-7-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210325154956.2405162-1-andreas.rheinhardt@gmail.com> References: <20210325154956.2405162-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 07/12] avcodec: Remove cumbersome way of checking for amount of bytes left 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" Several encoders used code like the following to check for the amount of bytes left in a PutBitContext: pb->buf_end - pb->buf - (put_bits_count(pb) >> 3) Besides the fact that using the pointers directly might pose a maintainence burden in the future this also leads to suboptimal code: The above code reads all three pointers (buf, buf_ptr and buf_end), but touching buf is unnecessary and switching to put_bytes_left() automatically fixes this. Signed-off-by: Andreas Rheinhardt --- libavcodec/asvenc.c | 2 +- libavcodec/ffv1enc_template.c | 2 +- libavcodec/huffyuvenc.c | 9 ++++----- libavcodec/ljpegenc.c | 4 ++-- libavcodec/mpegvideo_enc.c | 13 ++++++------- libavcodec/svq1enc.c | 3 +-- libavcodec/xsubenc.c | 2 +- 7 files changed, 16 insertions(+), 19 deletions(-) diff --git a/libavcodec/asvenc.c b/libavcodec/asvenc.c index 718ffa1836..2d8c310521 100644 --- a/libavcodec/asvenc.c +++ b/libavcodec/asvenc.c @@ -167,7 +167,7 @@ static inline int encode_mb(ASV1Context *a, int16_t block[6][64]) { int i; - av_assert0(a->pb.buf_end - a->pb.buf - (put_bits_count(&a->pb) >> 3) >= MAX_MB_SIZE); + av_assert0(put_bytes_left(&a->pb, 0) >= MAX_MB_SIZE); if (a->avctx->codec_id == AV_CODEC_ID_ASV1) { for (i = 0; i < 6; i++) diff --git a/libavcodec/ffv1enc_template.c b/libavcodec/ffv1enc_template.c index bc0add5ed7..8a4a387923 100644 --- a/libavcodec/ffv1enc_template.c +++ b/libavcodec/ffv1enc_template.c @@ -37,7 +37,7 @@ static av_always_inline int RENAME(encode_line)(FFV1Context *s, int w, return AVERROR_INVALIDDATA; } } else { - if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < w * 4) { + if (put_bytes_left(&s->pb, 0) < w * 4) { av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n"); return AVERROR_INVALIDDATA; } diff --git a/libavcodec/huffyuvenc.c b/libavcodec/huffyuvenc.c index 28a5534535..2882433db5 100644 --- a/libavcodec/huffyuvenc.c +++ b/libavcodec/huffyuvenc.c @@ -443,7 +443,7 @@ static int encode_422_bitstream(HYuvContext *s, int offset, int count) const uint8_t *u = s->temp[1] + offset / 2; const uint8_t *v = s->temp[2] + offset / 2; - if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < 2 * 4 * count) { + if (put_bytes_left(&s->pb, 0) < 2 * 4 * count) { av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n"); return -1; } @@ -495,7 +495,7 @@ static int encode_plane_bitstream(HYuvContext *s, int width, int plane) { int i, count = width/2; - if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < count * s->bps / 2) { + if (put_bytes_left(&s->pb, 0) < count * s->bps / 2) { av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n"); return -1; } @@ -657,7 +657,7 @@ static int encode_gray_bitstream(HYuvContext *s, int count) { int i; - if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < 4 * count) { + if (put_bytes_left(&s->pb, 0) < 4 * count) { av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n"); return -1; } @@ -702,8 +702,7 @@ static inline int encode_bgra_bitstream(HYuvContext *s, int count, int planes) { int i; - if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < - 4 * planes * count) { + if (put_bytes_left(&s->pb, 0) < 4 * planes * count) { av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n"); return -1; } diff --git a/libavcodec/ljpegenc.c b/libavcodec/ljpegenc.c index 3c68c08a3c..056b80b4b5 100644 --- a/libavcodec/ljpegenc.c +++ b/libavcodec/ljpegenc.c @@ -86,7 +86,7 @@ FF_ENABLE_DEPRECATION_WARNINGS const int modified_predictor = y ? s->pred : 1; uint8_t *ptr = frame->data[0] + (linesize * y); - if (pb->buf_end - pb->buf - (put_bits_count(pb) >> 3) < width * 4 * 4) { + if (put_bytes_left(pb, 0) < width * 4 * 4) { av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n"); return -1; } @@ -211,7 +211,7 @@ FF_ENABLE_DEPRECATION_WARNINGS #endif for (mb_y = 0; mb_y < mb_height; mb_y++) { - if (pb->buf_end - pb->buf - (put_bits_count(pb) >> 3) < + if (put_bytes_left(pb, 0) < mb_width * 4 * 3 * s->hsample[0] * s->vsample[0]) { av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n"); return -1; diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c index 7eecfce27c..8731b0ad31 100644 --- a/libavcodec/mpegvideo_enc.c +++ b/libavcodec/mpegvideo_enc.c @@ -1993,8 +1993,7 @@ FF_ENABLE_DEPRECATION_WARNINGS stuffing_count = ff_vbv_update(s, s->frame_bits); s->stuffing_bits = 8*stuffing_count; if (stuffing_count) { - if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < - stuffing_count + 50) { + if (put_bytes_left(&s->pb, 0) < stuffing_count + 50) { av_log(avctx, AV_LOG_ERROR, "stuffing too large\n"); return -1; } @@ -2911,7 +2910,7 @@ static void update_mb_info(MpegEncContext *s, int startcode) int ff_mpv_reallocate_putbitbuffer(MpegEncContext *s, size_t threshold, size_t size_increase) { - if ( s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < threshold + if (put_bytes_left(&s->pb, 0) < threshold && s->slice_context_count == 1 && s->pb.buf == s->avctx->internal->byte_buffer) { int lastgob_pos = s->ptr_lastgob - s->pb.buf; @@ -2940,7 +2939,7 @@ int ff_mpv_reallocate_putbitbuffer(MpegEncContext *s, size_t threshold, size_t s s->ptr_lastgob = s->pb.buf + lastgob_pos; s->vbv_delay_ptr = s->pb.buf + vbv_pos; } - if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < threshold) + if (put_bytes_left(&s->pb, 0) < threshold) return AVERROR(EINVAL); return 0; } @@ -3032,13 +3031,13 @@ static int encode_thread(AVCodecContext *c, void *arg){ + s->mb_width*MAX_MB_BYTES; ff_mpv_reallocate_putbitbuffer(s, MAX_MB_BYTES, size_increase); - if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < MAX_MB_BYTES){ + if (put_bytes_left(&s->pb, 0) < MAX_MB_BYTES){ av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n"); return -1; } if(s->data_partitioning){ - if( s->pb2 .buf_end - s->pb2 .buf - (put_bits_count(&s-> pb2)>>3) < MAX_MB_BYTES - || s->tex_pb.buf_end - s->tex_pb.buf - (put_bits_count(&s->tex_pb )>>3) < MAX_MB_BYTES){ + if (put_bytes_left(&s->pb2, 0) < MAX_MB_BYTES || + put_bytes_left(&s->tex_pb, 0) < MAX_MB_BYTES) { av_log(s->avctx, AV_LOG_ERROR, "encoded partitioned frame too large\n"); return -1; } diff --git a/libavcodec/svq1enc.c b/libavcodec/svq1enc.c index 4fac0c26e5..53f0b40a21 100644 --- a/libavcodec/svq1enc.c +++ b/libavcodec/svq1enc.c @@ -372,8 +372,7 @@ static int svq1_encode_plane(SVQ1EncContext *s, int plane, int score[4] = { 0, 0, 0, 0 }, best; uint8_t *temp = s->scratchbuf; - if (s->pb.buf_end - s->pb.buf - - (put_bits_count(&s->pb) >> 3) < 3000) { // FIXME: check size + if (put_bytes_left(&s->pb, 0) < 3000) { // FIXME: check size av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n"); return -1; } diff --git a/libavcodec/xsubenc.c b/libavcodec/xsubenc.c index ad7144db2d..36c5ab7223 100644 --- a/libavcodec/xsubenc.c +++ b/libavcodec/xsubenc.c @@ -63,7 +63,7 @@ static int xsub_encode_rle(PutBitContext *pb, const uint8_t *bitmap, x0 = 0; while (x0 < w) { // Make sure we have enough room for at least one run and padding - if (pb->size_in_bits - put_bits_count(pb) < 7*8) + if (put_bytes_left(pb, 1) < 7) return AVERROR_BUFFER_TOO_SMALL; x1 = x0; From patchwork Thu Mar 25 15:49:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 26606 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 A407344B722 for ; Thu, 25 Mar 2021 17:50:50 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 87A3768AA62; Thu, 25 Mar 2021 17:50:50 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f47.google.com (mail-ej1-f47.google.com [209.85.218.47]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 1FE0468AAC7 for ; Thu, 25 Mar 2021 17:50:37 +0200 (EET) Received: by mail-ej1-f47.google.com with SMTP id ce10so3711532ejb.6 for ; Thu, 25 Mar 2021 08:50:37 -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:reply-to :mime-version:content-transfer-encoding; bh=OjjIZPei1LY64DpCJFChunL8ucCC3DKMhDc67nCs+mQ=; b=CkjvAr6KqZU7V5LXcsZdJ1KBLA2C11vNf3BVt6SXZZ9VAkPSvONB91My1CZ1d6FIAP iTZvXBg0lQfZMJNplLRfMTCLKEEpr5ttC45GjT/JPdCFF0M93lZsnY17gVPUmVD3PM7Y yB27pK1br6HjSW92ASds3u1zH0GTDkSn/R9CgoIPVvEwm0YdoExNmjaf/qq+rJpOPxUR zcora0mFjhSm71n3Y9lHZIBpVMQQUI8x9eVN4El9G4PkkzRUYT2W62/0wnBGwJtWA0fd K6SxlL+F8HrDzEmBZpQQENv7rLrODHqT8W6Hf5nfKed7dNTgbfXC4TPWjbvPSQtRGnSu G5OQ== 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:reply-to:mime-version:content-transfer-encoding; bh=OjjIZPei1LY64DpCJFChunL8ucCC3DKMhDc67nCs+mQ=; b=FOY5d0LvRq0QaNcZrpZPV/ElsvnHcCOM8D4vBYjw4SCHPE1J9JU8q9TT3akkLo8y17 qDyRRDPjazyuJh2AwcO9LqGBGlDPFcGozlQcZdIMC87D+ye9ckeGdPpRQ1omihWSmDqo TjyqBbgG/F3613jyj2aDmwcMEVMC3/W+GeSISAVHehQPu6Rdt9gytHsDwPIWbPx0KUCg KanXEZKt3bZOyGSsPeBSuQj8d0j2GITEXZ42KJyFhpHbORPigW3xLdPpBsvmYHOuCoS5 T06PlNgJkaXTPFxKPKBaHAMptjUH+OqZmY+5pwCpLb9j9oLksSMqrJq5HUxHTpRVdKtr LE8w== X-Gm-Message-State: AOAM530XHo1txiSnZnKCnzTiEztCwldDLDK7Ezpo6XaqYNpk6wXDBOMV SHLXTS2YPyAjJsEYlCiCehxBR5h10hfplw== X-Google-Smtp-Source: ABdhPJwXB59c5NpqVy7MOGYwtnTU0LPf8jWspNIfba/WKkAp/tEj3uQveKEr8Zm3ND3WpsFU1sCkFQ== X-Received: by 2002:a17:906:f84b:: with SMTP id ks11mr10098713ejb.296.1616687436494; Thu, 25 Mar 2021 08:50:36 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc08960.dynamic.kabel-deutschland.de. [188.192.137.96]) by smtp.gmail.com with ESMTPSA id o6sm2859103edw.24.2021.03.25.08.50.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Mar 2021 08:50:36 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 25 Mar 2021 16:49:52 +0100 Message-Id: <20210325154956.2405162-8-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210325154956.2405162-1-andreas.rheinhardt@gmail.com> References: <20210325154956.2405162-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 08/12] avcodec/utvideoenc: Don't use bitcounts when byte-aligned 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" Despite write_huff_codes() receiving an ordinary buffer (not a PutBitContext), it returned the amount of data written in bits, not in bytes. This has been changed: There is now no intermediate bitcount any more. Signed-off-by: Andreas Rheinhardt --- libavcodec/utvideoenc.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/libavcodec/utvideoenc.c b/libavcodec/utvideoenc.c index 5c87eb50ac..32c204a898 100644 --- a/libavcodec/utvideoenc.c +++ b/libavcodec/utvideoenc.c @@ -398,13 +398,11 @@ static int write_huff_codes(uint8_t *src, uint8_t *dst, int dst_size, if (count) put_bits(&pb, 32 - count, 0); - /* Get the amount of bits written */ - count = put_bits_count(&pb); - /* Flush the rest with zeroes */ flush_put_bits(&pb); - return count; + /* Return the amount of bytes written */ + return put_bytes_output(&pb); } static int encode_plane(AVCodecContext *avctx, uint8_t *src, @@ -512,11 +510,11 @@ static int encode_plane(AVCodecContext *avctx, uint8_t *src, /* * Write the huffman codes to a buffer, - * get the offset in bits and convert to bytes. + * get the offset in bytes. */ offset += write_huff_codes(dst + sstart * width, c->slice_bits, width * height + 4, width, - send - sstart, he) >> 3; + send - sstart, he); slice_len = offset - slice_len; From patchwork Thu Mar 25 15:49:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 26607 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 8827F44B722 for ; Thu, 25 Mar 2021 17:50:51 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 6E92768AB70; Thu, 25 Mar 2021 17:50:51 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f51.google.com (mail-ed1-f51.google.com [209.85.208.51]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 05EB768A9A8 for ; Thu, 25 Mar 2021 17:50:38 +0200 (EET) Received: by mail-ed1-f51.google.com with SMTP id y6so2987557eds.1 for ; Thu, 25 Mar 2021 08:50:37 -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:reply-to :mime-version:content-transfer-encoding; bh=thoSxlu+qDVshOwCoLuXWg2yxhKLva84X2lW+CGd33o=; b=JOe+xSv7YHN+rizaqhQrmblCX/+FhK/9ESY4bW+rCtR9JQQObNBqDpWFCbjBh76+xe CaMtB7JvRDGUfXjJFslKilZG3Jlrss8ImPwPNK470rLeHiYQQwqpCKkS3KtLgUU54jVG pwnwYIl02e8XeXRI5rfIRvBednZkWbuCsHXX6QkroANxRvRcD6UBK77s/s6bUj1pKICr i4jYee81Lz2hNTvhsdSNAfaXXMOfky7RTFdCWC9Ge3Vt+f8XUfRhgavLELP0G7gBakNw k/T7oe3wOOEfC1onFwJoiM736KTHsl4yJA3hcv6TqQaHPNyg/gdLa8lNzBbBymR/90eq L01A== 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:reply-to:mime-version:content-transfer-encoding; bh=thoSxlu+qDVshOwCoLuXWg2yxhKLva84X2lW+CGd33o=; b=hVWqyTft6rtOIIh1BV64gU9Riij1LY3glysveo6Js3HtXDSK4APdDTfF0OjPxFfI7U lFwFEbnI0wjg8RIHHRAGquC30mis3khKcY1ntHmYewAoAzvqjfrxC8Nf+gsX7nju5H3U s8xSPlFLQ8GKgcc+0RwJCfWPFU+j/3Fds2cYm8iw6MwxIY56Pzwkzd2JrJvassZnkctx ivdW7rNZ96mFZ6DxJ6aYRNPmqWew4YT1HVvoLCPwWljAYmeh3F6SAhulGoywK0C/anij h6j0jeEmd938OxLyHmbczpvduSXPXpMG+yySd1E5hUDYRJW/p3XAhhMtYME2LcMeixRk /y0A== X-Gm-Message-State: AOAM531D8oftRQNSblZxwKq4yW2Q3LzCB81KRPL3P4IJEbz/yFJ9n/WI Ua/d0r+53E/lP44qiK97vXrAMwY/7uGONg== X-Google-Smtp-Source: ABdhPJxlfuNwET+T2ZbobH3m9cGyhWCKakwjm1SyCYM7xpFbLpH646PWy+oXJTZ2CK8417j95wZCWw== X-Received: by 2002:a05:6402:1d19:: with SMTP id dg25mr9924823edb.218.1616687437379; Thu, 25 Mar 2021 08:50:37 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc08960.dynamic.kabel-deutschland.de. [188.192.137.96]) by smtp.gmail.com with ESMTPSA id o6sm2859103edw.24.2021.03.25.08.50.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Mar 2021 08:50:36 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 25 Mar 2021 16:49:53 +0100 Message-Id: <20210325154956.2405162-9-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210325154956.2405162-1-andreas.rheinhardt@gmail.com> References: <20210325154956.2405162-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 09/12] avcodec/proresenc_kostya: Factor flushing PutBitContext out 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 function to write an ordinary (luma or chroma) plane as well as the function for writing an alpha plane have some similarities: They record the initial bitposition (despite said position always being byte-aligned), flush the PutBitContext themselves and return the amount of bytes they wrote. This commit factors this out; it also replaces bitpositions by bytepositions and it avoids recording the initial byteposition because said information is already available from the position at the end of the last plane. Signed-off-by: Andreas Rheinhardt --- libavcodec/proresenc_kostya.c | 36 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/libavcodec/proresenc_kostya.c b/libavcodec/proresenc_kostya.c index 0e70163bcc..d8edd12f34 100644 --- a/libavcodec/proresenc_kostya.c +++ b/libavcodec/proresenc_kostya.c @@ -464,23 +464,17 @@ static void encode_acs(PutBitContext *pb, int16_t *blocks, } } -static int encode_slice_plane(ProresContext *ctx, PutBitContext *pb, +static void encode_slice_plane(ProresContext *ctx, PutBitContext *pb, const uint16_t *src, ptrdiff_t linesize, int mbs_per_slice, int16_t *blocks, int blocks_per_mb, int plane_size_factor, const int16_t *qmat) { - int blocks_per_slice, saved_pos; - - saved_pos = put_bits_count(pb); - blocks_per_slice = mbs_per_slice * blocks_per_mb; + int blocks_per_slice = mbs_per_slice * blocks_per_mb; encode_dcs(pb, blocks, blocks_per_slice, qmat[0]); encode_acs(pb, blocks, blocks_per_slice, plane_size_factor, ctx->scantable, qmat); - flush_put_bits(pb); - - return (put_bits_count(pb) - saved_pos) >> 3; } static void put_alpha_diff(PutBitContext *pb, int cur, int prev, int abits) @@ -516,14 +510,13 @@ static void put_alpha_run(PutBitContext *pb, int run) } // todo alpha quantisation for high quants -static int encode_alpha_plane(ProresContext *ctx, PutBitContext *pb, +static void encode_alpha_plane(ProresContext *ctx, PutBitContext *pb, int mbs_per_slice, uint16_t *blocks, int quant) { const int abits = ctx->alpha_bits; const int mask = (1 << abits) - 1; const int num_coeffs = mbs_per_slice * 256; - int saved_pos = put_bits_count(pb); int prev = mask, cur; int idx = 0; int run = 0; @@ -544,8 +537,6 @@ static int encode_alpha_plane(ProresContext *ctx, PutBitContext *pb, } while (idx < num_coeffs); if (run) put_alpha_run(pb, run); - flush_put_bits(pb); - return (put_bits_count(pb) - saved_pos) >> 3; } static int encode_slice(AVCodecContext *avctx, const AVFrame *pic, @@ -611,24 +602,23 @@ static int encode_slice(AVCodecContext *avctx, const AVFrame *pic, ctx->blocks[0], ctx->emu_buf, mbs_per_slice, num_cblocks, is_chroma); if (!is_chroma) {/* luma quant */ - sizes[i] = encode_slice_plane(ctx, pb, src, linesize, - mbs_per_slice, ctx->blocks[0], - num_cblocks, plane_factor, - qmat); + encode_slice_plane(ctx, pb, src, linesize, + mbs_per_slice, ctx->blocks[0], + num_cblocks, plane_factor, qmat); } else { /* chroma plane */ - sizes[i] = encode_slice_plane(ctx, pb, src, linesize, - mbs_per_slice, ctx->blocks[0], - num_cblocks, plane_factor, - qmat_chroma); + encode_slice_plane(ctx, pb, src, linesize, + mbs_per_slice, ctx->blocks[0], + num_cblocks, plane_factor, qmat_chroma); } } else { get_alpha_data(ctx, src, linesize, xp, yp, pwidth, avctx->height / ctx->pictures_per_frame, ctx->blocks[0], mbs_per_slice, ctx->alpha_bits); - sizes[i] = encode_alpha_plane(ctx, pb, mbs_per_slice, - ctx->blocks[0], quant); + encode_alpha_plane(ctx, pb, mbs_per_slice, ctx->blocks[0], quant); } - total_size += sizes[i]; + flush_put_bits(pb); + sizes[i] = put_bytes_output(pb) - total_size; + total_size = put_bytes_output(pb); if (put_bits_left(pb) < 0) { av_log(avctx, AV_LOG_ERROR, "Underestimated required buffer size.\n"); From patchwork Thu Mar 25 15:49:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 26608 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 5896F44B722 for ; Thu, 25 Mar 2021 17:50:52 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 47CE968ABB9; Thu, 25 Mar 2021 17:50:52 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f41.google.com (mail-ej1-f41.google.com [209.85.218.41]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2487668A98D for ; Thu, 25 Mar 2021 17:50:39 +0200 (EET) Received: by mail-ej1-f41.google.com with SMTP id hq27so3692951ejc.9 for ; Thu, 25 Mar 2021 08:50:39 -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:reply-to :mime-version:content-transfer-encoding; bh=RlFAQVANAMFcc8NEJ+mZmjLI8oS5lRfmRSuYcEfugVM=; b=VELcMUQF4YJZWYDGR0UV/Y4RfLV4VmXuYnBv7g58bV+Kj27jr2S/y7+tepi4qcbgng F5JSsQY0gNsj3gN3Y/0XC0UYcHV7P4YIB5kGGQ1/q1P+dU7j+3YAh9T6yZqxEl1rzce8 Ev/PZ/jnAjbFpC02qqZBoSD2ybamYEiRMxYruRTETnJGb371UsFQwTEfwnv/gp2aJi0v xyL78bL9PgRVNiTdWtLuRcCesQ6zSeenKZXxntuJOASVLAk2eTUkegwDkVzv30XUgrNU DGl26OBCstBUOc+FXjh0YncwGs0EFypdGK6l0TK7Zb5A4NgWfzCTo8sDa9Steeb+omnV nJaA== 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:reply-to:mime-version:content-transfer-encoding; bh=RlFAQVANAMFcc8NEJ+mZmjLI8oS5lRfmRSuYcEfugVM=; b=R7c3l5kQQyHMOqWpEFSSVVerOMOj3ItLNszTt0DG1kq6dGU5C91pzk71Zo2/OH1Jyu p0w6o4EY7aQB4DzBc3rwpu18t1BgmVxt7Q4d2x+WfL+e+KlsR3sxsD2XnKPgUTGvslyV XgPLHhwz94MIgqXwPX5qGxlb/XkxacZ6S5JGhMdJ+tP56GsWhof/37fltWIBf8CRAWJm 7+MF4hbRn+eLKkJaWbl0871aZ/cM1Kn0OGQe2ZD9lEx6mmOJIRkPYvZgRMSBIPsWb5iF tgg1GqaQRB6BfH7UV7Ch//e2s/pKTdEr8hdw8BR3NwIrMHBFMnT0FebeC7O7IkPKzqLO +jMA== X-Gm-Message-State: AOAM5302zOPDN0MdITn9D4p3kbZUFrn7saHethPQ+dURTkXow7wWe2Vi 3SB/XPKyq9doCylYrRKNBe3/gj66CDFufA== X-Google-Smtp-Source: ABdhPJw69Xukh6XIzKGb1xii+8otWb3hx5yRbH0UU3z5Nt7HHQ/fBiS8Wa9c5MmvJJA7MbWZProWZg== X-Received: by 2002:a17:907:7684:: with SMTP id jv4mr10424611ejc.231.1616687438525; Thu, 25 Mar 2021 08:50:38 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc08960.dynamic.kabel-deutschland.de. [188.192.137.96]) by smtp.gmail.com with ESMTPSA id o6sm2859103edw.24.2021.03.25.08.50.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Mar 2021 08:50:37 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 25 Mar 2021 16:49:54 +0100 Message-Id: <20210325154956.2405162-10-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210325154956.2405162-1-andreas.rheinhardt@gmail.com> References: <20210325154956.2405162-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 10/12] avcodec/dvenc: Avoid using PutBitContext fields directly 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" Also avoid using bitcounts in case one is actually byte-aligned. Signed-off-by: Andreas Rheinhardt --- libavcodec/dvenc.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/libavcodec/dvenc.c b/libavcodec/dvenc.c index 134315a834..e0679c538c 100644 --- a/libavcodec/dvenc.c +++ b/libavcodec/dvenc.c @@ -976,11 +976,8 @@ static int dv_encode_video_segment(AVCodecContext *avctx, void *arg) } for (j = 0; j < 5 * s->sys->bpm; j++) { - int pos; - int size = pbs[j].size_in_bits >> 3; flush_put_bits(&pbs[j]); - pos = put_bits_count(&pbs[j]) >> 3; - memset(pbs[j].buf + pos, 0xff, size - pos); + memset(put_bits_ptr(&pbs[j]), 0xff, put_bytes_left(&pbs[j], 0)); } if (DV_PROFILE_IS_HD(s->sys)) From patchwork Thu Mar 25 15:49:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 26602 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 EF5FB44B722 for ; Thu, 25 Mar 2021 17:50:45 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D3FE468AB18; Thu, 25 Mar 2021 17:50:45 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f49.google.com (mail-ej1-f49.google.com [209.85.218.49]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9572668AAEA for ; Thu, 25 Mar 2021 17:50:40 +0200 (EET) Received: by mail-ej1-f49.google.com with SMTP id kt15so3671878ejb.12 for ; Thu, 25 Mar 2021 08:50:40 -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:reply-to :mime-version:content-transfer-encoding; bh=1VpgXghRl9WGlAiRiVm3VLry9QIA88kzMXRQWf7xILo=; b=AEaT/Uj2NhpW/j/xazru/6BubTRIOtnO1wY+psDfbLOZx0fYdMRq5CAeywDKh4qhxC qNHp6oxn7ku3J3AcHsEhCYE8v1L/TGAgYyZcRGcM1vI+CzeKe5YwOEDuR5/a73HvNLHm 78tVoDMaBjuuSQNumFvjtw75uQ1V+1fIvGN00PsYJ0ABpWCVk6DCz+A6EDXyX6sJkY93 Wjav1zcanrmfO0SQhDGMJIas2IcxIIxWPcF8tim6OqI8aNghIfQALBLS39su8fmRgMa9 aBVbaS2vy5SWDeEOhXtvglOx7I0hcoGiZah03bFiAmZMWuSvgeub/0b/DkVtczTsvfSH dHOQ== 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:reply-to:mime-version:content-transfer-encoding; bh=1VpgXghRl9WGlAiRiVm3VLry9QIA88kzMXRQWf7xILo=; b=VMin1b/SE3Enayff12+7qIaXVCenGzY8ySRXEO4vYdCT0J/OpHeEAuNcA8Ayu6OIGT Bqq107MGj1Ui5xABtesXiHw/dx50rwBsF/7VOsIYaFp0OgVbCuDORrxmGCA6YIO1bWxy I5RGDh54OzCBdwOyHk7w0wLqRdkYytlNXeRMbLYZXQHnQ49SKRBP4U1IJt8BPr9uIxM9 PVPtpkmNDIxCkrQZHBSeSYygH1vCJUIOp+XAcyJrl02ctJq0NWN7ufSg0FT6fL3aH6Kc rurq14o5Qyy4jN83q0nKDhZdG/IS+BChHIXtij3ZazM9kh0GQ7n+nT3BjZl6D8bnBvWv wISg== X-Gm-Message-State: AOAM531OIlZPsoHfV7CDVBcl1fVrK9+DmdGpg0uUvHizJQK2uzcvaMrj yjzwxunvMVUciiY+WiaTmg01PNKpGyTodQ== X-Google-Smtp-Source: ABdhPJyZFRvdZZkZiCMVHFLW22DGaDzmqHShu9xcZjj3sdo7xOaD442xy+e8l6w1TIB7YHcpEshBig== X-Received: by 2002:a17:906:5597:: with SMTP id y23mr10313568ejp.165.1616687439391; Thu, 25 Mar 2021 08:50:39 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc08960.dynamic.kabel-deutschland.de. [188.192.137.96]) by smtp.gmail.com with ESMTPSA id o6sm2859103edw.24.2021.03.25.08.50.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Mar 2021 08:50:38 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 25 Mar 2021 16:49:55 +0100 Message-Id: <20210325154956.2405162-11-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210325154956.2405162-1-andreas.rheinhardt@gmail.com> References: <20210325154956.2405162-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 11/12] Avoid intermediate bitcount for number of bytes in PutBitContext 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/aacenc.c | 4 ++-- libavcodec/alacenc.c | 2 +- libavcodec/asvenc.c | 2 +- libavcodec/cbs.c | 2 +- libavcodec/cfhdenc.c | 2 +- libavcodec/cljrenc.c | 2 +- libavcodec/dca.c | 2 +- libavcodec/dcaenc.c | 2 +- libavcodec/ffv1enc.c | 2 +- libavcodec/flacenc.c | 6 +++--- libavcodec/hevc_ps_enc.c | 2 +- libavcodec/lzwenc.c | 2 +- libavcodec/magicyuvenc.c | 6 ++---- libavcodec/mjpegenc.c | 2 +- libavcodec/mjpegenc_common.c | 7 ++----- libavcodec/mlpenc.c | 8 ++++---- libavcodec/mpeg12enc.c | 2 +- libavcodec/mpeg4videoenc.c | 2 +- libavcodec/mpegaudioenc_template.c | 8 ++++---- libavcodec/mpegvideo_enc.c | 11 ++++++----- libavcodec/proresenc_anatoliy.c | 2 +- libavcodec/rpzaenc.c | 2 +- libavcodec/sbcenc.c | 2 +- libavcodec/sonic.c | 2 +- libavcodec/svq1enc.c | 2 +- libavcodec/tests/cabac.c | 2 +- libavcodec/ttaenc.c | 2 +- libavcodec/vorbisenc.c | 8 ++++---- libavcodec/wavpackenc.c | 4 ++-- libavcodec/wmaenc.c | 2 +- libavcodec/xsubenc.c | 4 ++-- libavfilter/vf_signature.c | 2 +- libavformat/latmenc.c | 2 +- libavformat/movenc.c | 2 +- 34 files changed, 55 insertions(+), 59 deletions(-) diff --git a/libavcodec/aacenc.c b/libavcodec/aacenc.c index 070a2e706a..6dc68eb9b0 100644 --- a/libavcodec/aacenc.c +++ b/libavcodec/aacenc.c @@ -115,7 +115,7 @@ static int put_audio_specific_config(AVCodecContext *avctx) put_bits(&pb, 5, AOT_SBR); put_bits(&pb, 1, 0); flush_put_bits(&pb); - avctx->extradata_size = put_bits_count(&pb) >> 3; + avctx->extradata_size = put_bytes_output(&pb); return 0; } @@ -881,6 +881,7 @@ static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, flush_put_bits(&s->pb); s->last_frame_pb_count = put_bits_count(&s->pb); + avpkt->size = put_bytes_output(&s->pb); s->lambda_sum += s->lambda; s->lambda_count++; @@ -888,7 +889,6 @@ static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts, &avpkt->duration); - avpkt->size = put_bits_count(&s->pb) >> 3; *got_packet_ptr = 1; return 0; } diff --git a/libavcodec/alacenc.c b/libavcodec/alacenc.c index 9d135d1350..ecdd46cac3 100644 --- a/libavcodec/alacenc.c +++ b/libavcodec/alacenc.c @@ -485,7 +485,7 @@ static int write_frame(AlacEncodeContext *s, AVPacket *avpkt, put_bits(pb, 3, TYPE_END); flush_put_bits(pb); - return put_bits_count(pb) >> 3; + return put_bytes_output(pb); } static av_always_inline int get_max_frame_size(int frame_size, int ch, int bps) diff --git a/libavcodec/asvenc.c b/libavcodec/asvenc.c index 2d8c310521..c33a3b4681 100644 --- a/libavcodec/asvenc.c +++ b/libavcodec/asvenc.c @@ -290,7 +290,7 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, else flush_put_bits_le(&a->pb); AV_WN32(put_bits_ptr(&a->pb), 0); - size = (put_bits_count(&a->pb) + 31) / 32; + size = (put_bytes_output(&a->pb) + 3) / 4; if (avctx->codec_id == AV_CODEC_ID_ASV1) { a->bbdsp.bswap_buf((uint32_t *) pkt->data, diff --git a/libavcodec/cbs.c b/libavcodec/cbs.c index c7f69845fb..bbfafb6530 100644 --- a/libavcodec/cbs.c +++ b/libavcodec/cbs.c @@ -346,7 +346,7 @@ static int cbs_write_unit_data(CodedBitstreamContext *ctx, flush_put_bits(&pbc); - ret = ff_cbs_alloc_unit_data(unit, put_bits_count(&pbc) / 8); + ret = ff_cbs_alloc_unit_data(unit, put_bytes_output(&pbc)); if (ret < 0) return ret; diff --git a/libavcodec/cfhdenc.c b/libavcodec/cfhdenc.c index 42bbf99c96..0369164fab 100644 --- a/libavcodec/cfhdenc.c +++ b/libavcodec/cfhdenc.c @@ -766,7 +766,7 @@ static int cfhd_encode_frame(AVCodecContext *avctx, AVPacket *pkt, put_bits(pb, cb[512].size, cb[512].bits); flush_put_bits(pb); - bytestream2_skip_p(pby, put_bits_count(pb) >> 3); + bytestream2_skip_p(pby, put_bytes_output(pb)); padd = (4 - (bytestream2_tell_p(pby) & 3)) & 3; while (padd--) bytestream2_put_byte(pby, 0); diff --git a/libavcodec/cljrenc.c b/libavcodec/cljrenc.c index a3718259d1..fe58f50f09 100644 --- a/libavcodec/cljrenc.c +++ b/libavcodec/cljrenc.c @@ -89,7 +89,7 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, flush_put_bits(&pb); - pkt->size = put_bits_count(&pb) / 8; + pkt->size = put_bytes_output(&pb); pkt->flags |= AV_PKT_FLAG_KEY; *got_packet = 1; return 0; diff --git a/libavcodec/dca.c b/libavcodec/dca.c index a0729e61ab..b2152524a5 100644 --- a/libavcodec/dca.c +++ b/libavcodec/dca.c @@ -82,7 +82,7 @@ int avpriv_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst, put_bits(&pb, 14, tmp); } flush_put_bits(&pb); - return (put_bits_count(&pb) + 7) >> 3; + return put_bytes_output(&pb); default: return AVERROR_INVALIDDATA; } diff --git a/libavcodec/dcaenc.c b/libavcodec/dcaenc.c index a40d2e0ca0..63e6dfb780 100644 --- a/libavcodec/dcaenc.c +++ b/libavcodec/dcaenc.c @@ -1213,8 +1213,8 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *avpkt, flush_put_bits(&c->pb); avpkt->pts = frame->pts; + avpkt->size = put_bytes_output(&c->pb); avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples); - avpkt->size = put_bits_count(&c->pb) >> 3; *got_packet_ptr = 1; return 0; } diff --git a/libavcodec/ffv1enc.c b/libavcodec/ffv1enc.c index 611b250e96..cdad65a4f9 100644 --- a/libavcodec/ffv1enc.c +++ b/libavcodec/ffv1enc.c @@ -1239,7 +1239,7 @@ FF_ENABLE_DEPRECATION_WARNINGS bytes = ff_rac_terminate(&fs->c, 1); } else { flush_put_bits(&fs->pb); // FIXME: nicer padding - bytes = fs->ac_byte_count + (put_bits_count(&fs->pb) + 7) / 8; + bytes = fs->ac_byte_count + put_bytes_output(&fs->pb); } if (i > 0 || f->version > 2) { av_assert0(bytes < pkt->size / f->slice_count); diff --git a/libavcodec/flacenc.c b/libavcodec/flacenc.c index 8e7dbc52b4..34fcd95795 100644 --- a/libavcodec/flacenc.c +++ b/libavcodec/flacenc.c @@ -1234,7 +1234,7 @@ static void write_frame_header(FlacEncodeContext *s) flush_put_bits(&s->pb); crc = av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, s->pb.buf, - put_bits_count(&s->pb) >> 3); + put_bytes_output(&s->pb)); put_bits(&s->pb, 8, crc); } @@ -1304,7 +1304,7 @@ static void write_frame_footer(FlacEncodeContext *s) int crc; flush_put_bits(&s->pb); crc = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, s->pb.buf, - put_bits_count(&s->pb)>>3)); + put_bytes_output(&s->pb))); put_bits(&s->pb, 16, crc); flush_put_bits(&s->pb); } @@ -1316,7 +1316,7 @@ static int write_frame(FlacEncodeContext *s, AVPacket *avpkt) write_frame_header(s); write_subframes(s); write_frame_footer(s); - return put_bits_count(&s->pb) >> 3; + return put_bytes_output(&s->pb); } diff --git a/libavcodec/hevc_ps_enc.c b/libavcodec/hevc_ps_enc.c index 5449f81c82..47f252dd2c 100644 --- a/libavcodec/hevc_ps_enc.c +++ b/libavcodec/hevc_ps_enc.c @@ -115,7 +115,7 @@ int ff_hevc_encode_nal_vps(HEVCVPS *vps, unsigned int id, put_bits(&pb, 1, 1); // stop bit flush_put_bits(&pb); - data_size = put_bits_count(&pb) / 8; + data_size = put_bytes_output(&pb); return data_size; } diff --git a/libavcodec/lzwenc.c b/libavcodec/lzwenc.c index e693d74272..6c45f7cc37 100644 --- a/libavcodec/lzwenc.c +++ b/libavcodec/lzwenc.c @@ -189,7 +189,7 @@ static void clearTable(LZWEncodeState * s) * @return Number of bytes written */ static int writtenBytes(LZWEncodeState *s){ - int ret = put_bits_count(&s->pb) >> 3; + int ret = put_bytes_count(&s->pb, 0); ret -= s->output_bytes; s->output_bytes += ret; return ret; diff --git a/libavcodec/magicyuvenc.c b/libavcodec/magicyuvenc.c index d53fe6f328..7ec3c963bf 100644 --- a/libavcodec/magicyuvenc.c +++ b/libavcodec/magicyuvenc.c @@ -400,11 +400,9 @@ static int encode_slice(uint8_t *src, uint8_t *dst, int dst_size, if (count) put_bits(&pb, 32 - count, 0); - count = put_bits_count(&pb); - flush_put_bits(&pb); - return count >> 3; + return put_bytes_output(&pb); } static int magy_encode_frame(AVCodecContext *avctx, AVPacket *pkt, @@ -499,7 +497,7 @@ static int magy_encode_frame(AVCodecContext *avctx, AVPacket *pkt, AV_CEIL_RSHIFT(frame->height, s->vshift[i]), &s->pb, s->he[i]); } - s->tables_size = (put_bits_count(&s->pb) + 7) >> 3; + s->tables_size = put_bytes_count(&s->pb, 1); bytestream2_skip_p(&pb, s->tables_size); for (i = 0; i < s->planes; i++) { diff --git a/libavcodec/mjpegenc.c b/libavcodec/mjpegenc.c index 22af094da7..df3aaaf26b 100644 --- a/libavcodec/mjpegenc.c +++ b/libavcodec/mjpegenc.c @@ -218,7 +218,7 @@ int ff_mjpeg_encode_stuffing(MpegEncContext *s) if ((s->avctx->active_thread_type & FF_THREAD_SLICE) && mb_y < s->mb_height - 1) put_marker(pbc, RST0 + (mb_y&7)); - s->esc_pos = put_bits_count(pbc) >> 3; + s->esc_pos = put_bytes_count(pbc, 0); fail: for (int i = 0; i < 3; i++) diff --git a/libavcodec/mjpegenc_common.c b/libavcodec/mjpegenc_common.c index 12dd7be2e8..3eae9b7d0f 100644 --- a/libavcodec/mjpegenc_common.c +++ b/libavcodec/mjpegenc_common.c @@ -325,7 +325,7 @@ end: MpegEncContext *s = avctx->priv_data; av_assert0(avctx->codec->priv_data_size == sizeof(MpegEncContext)); - s->esc_pos = put_bits_count(pb) >> 3; + s->esc_pos = put_bytes_count(pb, 0); for(i=1; islice_context_count; i++) s->thread_context[i]->esc_pos = 0; } @@ -343,10 +343,7 @@ void ff_mjpeg_escape_FF(PutBitContext *pb, int start) put_bits(pb, pad, (1<>= 3; + size = put_bytes_output(pb) - start; ff_count=0; for(i=0; i> 3) ^ 0xa9; - checksum = ff_mlp_checksum8 (buf, put_bits_count(&pb) >> 3); + parity = ff_mlp_calculate_parity(buf, put_bytes_output(&pb)) ^ 0xa9; + checksum = ff_mlp_checksum8 (buf, put_bytes_output(&pb)); put_bits(&pb, 8, parity ); put_bits(&pb, 8, checksum); flush_put_bits(&pb); - end += put_bits_count(&pb) >> 3; + end += put_bytes_output(&pb); substream_data_len[substr] = end; - buf += put_bits_count(&pb) >> 3; + buf += put_bytes_output(&pb); } ctx->major_cur_subblock_index += ctx->major_filter_state_subblock + 1; diff --git a/libavcodec/mpeg12enc.c b/libavcodec/mpeg12enc.c index a05c2db6cb..5676caef87 100644 --- a/libavcodec/mpeg12enc.c +++ b/libavcodec/mpeg12enc.c @@ -439,7 +439,7 @@ void ff_mpeg1_encode_picture_header(MpegEncContext *s, int picture_number) (s->picture_number - s->gop_picture_number) & 0x3ff); put_bits(&s->pb, 3, s->pict_type); - s->vbv_delay_ptr = s->pb.buf + put_bits_count(&s->pb) / 8; + s->vbv_delay_ptr = s->pb.buf + put_bytes_count(&s->pb, 0); put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */ // RAL: Forward f_code also needed for B-frames diff --git a/libavcodec/mpeg4videoenc.c b/libavcodec/mpeg4videoenc.c index a879d2fd18..2bf65305ad 100644 --- a/libavcodec/mpeg4videoenc.c +++ b/libavcodec/mpeg4videoenc.c @@ -1314,7 +1314,7 @@ static av_cold int encode_init(AVCodecContext *avctx) // ff_mpeg4_stuffing(&s->pb); ? flush_put_bits(&s->pb); - s->avctx->extradata_size = (put_bits_count(&s->pb) + 7) >> 3; + s->avctx->extradata_size = put_bytes_output(&s->pb); } return 0; } diff --git a/libavcodec/mpegaudioenc_template.c b/libavcodec/mpegaudioenc_template.c index 12f7a098e6..8e6e20c358 100644 --- a/libavcodec/mpegaudioenc_template.c +++ b/libavcodec/mpegaudioenc_template.c @@ -736,9 +736,6 @@ static void encode_frame(MpegAudioContext *s, /* padding */ for(i=0;ipb); + avpkt->size = put_bytes_output(&s->pb); + if (frame->pts != AV_NOPTS_VALUE) avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->initial_padding); - avpkt->size = put_bits_count(&s->pb) / 8; *got_packet_ptr = 1; return 0; } diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c index 8731b0ad31..81f832fe4b 100644 --- a/libavcodec/mpegvideo_enc.c +++ b/libavcodec/mpegvideo_enc.c @@ -2889,12 +2889,12 @@ static void update_mb_info(MpegEncContext *s, int startcode) { if (!s->mb_info) return; - if (put_bits_count(&s->pb) - s->prev_mb_info*8 >= s->mb_info*8) { + if (put_bytes_count(&s->pb, 0) - s->prev_mb_info >= s->mb_info) { s->mb_info_size += 12; s->prev_mb_info = s->last_mb_info; } if (startcode) { - s->prev_mb_info = put_bits_count(&s->pb)/8; + s->prev_mb_info = put_bytes_count(&s->pb, 0); /* This might have incremented mb_info_size above, and we return without * actually writing any info into that slot yet. But in that case, * this will be called again at the start of the after writing the @@ -2902,7 +2902,7 @@ static void update_mb_info(MpegEncContext *s, int startcode) return; } - s->last_mb_info = put_bits_count(&s->pb)/8; + s->last_mb_info = put_bytes_count(&s->pb, 0); if (!s->mb_info_size) s->mb_info_size += 12; write_mb_info(s); @@ -3057,7 +3057,8 @@ static int encode_thread(AVCodecContext *c, void *arg){ if(s->rtp_mode){ int current_packet_size, is_gob_start; - current_packet_size= ((put_bits_count(&s->pb)+7)>>3) - (s->ptr_lastgob - s->pb.buf); + current_packet_size = put_bytes_count(&s->pb, 1) + - (s->ptr_lastgob - s->pb.buf); is_gob_start = s->rtp_payload_size && current_packet_size >= s->rtp_payload_size && @@ -3094,7 +3095,7 @@ static int encode_thread(AVCodecContext *c, void *arg){ current_packet_size= put_bits_ptr(&s->pb) - s->ptr_lastgob; if (s->error_rate && s->resync_mb_x + s->resync_mb_y > 0) { - int r= put_bits_count(&s->pb)/8 + s->picture_number + 16 + s->mb_x + s->mb_y; + int r = put_bytes_count(&s->pb, 0) + s->picture_number + 16 + s->mb_x + s->mb_y; int d = 100 / s->error_rate; if(r % d == 0){ current_packet_size=0; diff --git a/libavcodec/proresenc_anatoliy.c b/libavcodec/proresenc_anatoliy.c index c34e55f873..588df5984c 100644 --- a/libavcodec/proresenc_anatoliy.c +++ b/libavcodec/proresenc_anatoliy.c @@ -460,7 +460,7 @@ static av_always_inline int encode_alpha_slice_data(AVCodecContext *avctx, int8_ if (run) put_alpha_run(&pb, run); flush_put_bits(&pb); - *a_data_size = put_bits_count(&pb) >> 3; + *a_data_size = put_bytes_output(&pb); if (put_bits_left(&pb) < 0) { av_log(avctx, AV_LOG_ERROR, diff --git a/libavcodec/rpzaenc.c b/libavcodec/rpzaenc.c index baf067c205..eeb82716c9 100644 --- a/libavcodec/rpzaenc.c +++ b/libavcodec/rpzaenc.c @@ -802,7 +802,7 @@ static int rpza_encode_frame(AVCodecContext *avctx, AVPacket *pkt, flush_put_bits(&s->pb); - av_shrink_packet(pkt, put_bits_count(&s->pb) >> 3); + av_shrink_packet(pkt, put_bytes_output(&s->pb)); buf = pkt->data; // write header opcode diff --git a/libavcodec/sbcenc.c b/libavcodec/sbcenc.c index 47a136e4e5..33de8edab4 100644 --- a/libavcodec/sbcenc.c +++ b/libavcodec/sbcenc.c @@ -188,7 +188,7 @@ static size_t sbc_pack_frame(AVPacket *avpkt, struct sbc_frame *frame, flush_put_bits(&pb); - return (put_bits_count(&pb) + 7) / 8; + return put_bytes_output(&pb); } static int sbc_encode_init(AVCodecContext *avctx) diff --git a/libavcodec/sonic.c b/libavcodec/sonic.c index 8339799dd3..c2a0a9953e 100644 --- a/libavcodec/sonic.c +++ b/libavcodec/sonic.c @@ -687,7 +687,7 @@ static av_cold int sonic_encode_init(AVCodecContext *avctx) put_bits(&pb, 1, 0); // XXX FIXME: no custom tap quant table flush_put_bits(&pb); - avctx->extradata_size = put_bits_count(&pb)/8; + avctx->extradata_size = put_bytes_output(&pb); av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d.%d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n", s->version, s->minor_version, s->lossless, s->decorrelation, s->num_taps, s->block_align, s->frame_size, s->downsampling); diff --git a/libavcodec/svq1enc.c b/libavcodec/svq1enc.c index 53f0b40a21..2b74f009e0 100644 --- a/libavcodec/svq1enc.c +++ b/libavcodec/svq1enc.c @@ -652,7 +652,7 @@ FF_ENABLE_DEPRECATION_WARNINGS flush_put_bits(&s->pb); - pkt->size = put_bits_count(&s->pb) / 8; + pkt->size = put_bytes_output(&s->pb); if (s->pict_type == AV_PICTURE_TYPE_I) pkt->flags |= AV_PKT_FLAG_KEY; *got_packet = 1; diff --git a/libavcodec/tests/cabac.c b/libavcodec/tests/cabac.c index a8bd131c95..b84a96ce81 100644 --- a/libavcodec/tests/cabac.c +++ b/libavcodec/tests/cabac.c @@ -120,7 +120,7 @@ static int put_cabac_terminate(CABACTestContext *c, int bit) flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong } - return (put_bits_count(&c->pb)+7)>>3; + return put_bytes_count(&c->pb, 1); } /** diff --git a/libavcodec/ttaenc.c b/libavcodec/ttaenc.c index ac8a432873..bb4734e013 100644 --- a/libavcodec/ttaenc.c +++ b/libavcodec/ttaenc.c @@ -182,7 +182,7 @@ pkt_alloc: } flush_put_bits(&pb); - out_bytes = put_bits_count(&pb) >> 3; + out_bytes = put_bytes_output(&pb); put_bits32(&pb, av_crc(s->crc_table, UINT32_MAX, avpkt->data, out_bytes) ^ UINT32_MAX); flush_put_bits(&pb); diff --git a/libavcodec/vorbisenc.c b/libavcodec/vorbisenc.c index 080017e2bf..1b0ab3cadb 100644 --- a/libavcodec/vorbisenc.c +++ b/libavcodec/vorbisenc.c @@ -637,7 +637,7 @@ static int put_main_header(vorbis_enc_context *venc, uint8_t **out) put_bits(&pb, 1, 1); // framing flush_put_bits(&pb); - hlens[0] = put_bits_count(&pb) >> 3; + hlens[0] = put_bytes_output(&pb); buffer_len -= hlens[0]; p += hlens[0]; @@ -651,7 +651,7 @@ static int put_main_header(vorbis_enc_context *venc, uint8_t **out) put_bits(&pb, 1, 1); // framing flush_put_bits(&pb); - hlens[1] = put_bits_count(&pb) >> 3; + hlens[1] = put_bytes_output(&pb); buffer_len -= hlens[1]; p += hlens[1]; @@ -725,7 +725,7 @@ static int put_main_header(vorbis_enc_context *venc, uint8_t **out) put_bits(&pb, 1, 1); // framing flush_put_bits(&pb); - hlens[2] = put_bits_count(&pb) >> 3; + hlens[2] = put_bytes_output(&pb); len = hlens[0] + hlens[1] + hlens[2]; p = *out = av_mallocz(64 + len + len/255); @@ -1180,7 +1180,7 @@ static int vorbis_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, } flush_put_bits(&pb); - avpkt->size = put_bits_count(&pb) >> 3; + avpkt->size = put_bytes_output(&pb); ff_af_queue_remove(&venc->afq, frame_size, &avpkt->pts, &avpkt->duration); diff --git a/libavcodec/wavpackenc.c b/libavcodec/wavpackenc.c index 0a798438bc..0c82633a72 100644 --- a/libavcodec/wavpackenc.c +++ b/libavcodec/wavpackenc.c @@ -2777,7 +2777,7 @@ static int wavpack_encode_block(WavPackEncodeContext *s, } encode_flush(s); flush_put_bits(&s->pb); - data_size = put_bits_count(&s->pb) >> 3; + data_size = put_bytes_output(&s->pb); bytestream2_put_le24(&pb, (data_size + 1) >> 1); bytestream2_skip_p(&pb, data_size); if (data_size & 1) @@ -2791,7 +2791,7 @@ static int wavpack_encode_block(WavPackEncodeContext *s, else pack_int32(s, s->orig_l, s->orig_r, nb_samples); flush_put_bits(&s->pb); - data_size = put_bits_count(&s->pb) >> 3; + data_size = put_bytes_output(&s->pb); bytestream2_put_le24(&pb, (data_size + 5) >> 1); bytestream2_put_le32(&pb, s->crc_x); bytestream2_skip_p(&pb, data_size); diff --git a/libavcodec/wmaenc.c b/libavcodec/wmaenc.c index 6a7e23d016..4ad37cc70b 100644 --- a/libavcodec/wmaenc.c +++ b/libavcodec/wmaenc.c @@ -408,7 +408,7 @@ static int encode_superframe(AVCodecContext *avctx, AVPacket *avpkt, return AVERROR(EINVAL); } av_assert0((put_bits_count(&s->pb) & 7) == 0); - i= avctx->block_align - (put_bits_count(&s->pb)+7)/8; + i = avctx->block_align - put_bytes_count(&s->pb, 0); av_assert0(i>=0); while(i--) put_bits(&s->pb, 8, 'N'); diff --git a/libavcodec/xsubenc.c b/libavcodec/xsubenc.c index 36c5ab7223..9139159c98 100644 --- a/libavcodec/xsubenc.c +++ b/libavcodec/xsubenc.c @@ -197,7 +197,7 @@ FF_ENABLE_DEPRECATION_WARNINGS h->rects[0]->linesize[0] * 2, h->rects[0]->w, (h->rects[0]->h + 1) >> 1)) return AVERROR_BUFFER_TOO_SMALL; - bytestream_put_le16(&rlelenptr, put_bits_count(&pb) >> 3); // Length of first field + bytestream_put_le16(&rlelenptr, put_bytes_count(&pb, 0)); // Length of first field if (xsub_encode_rle(&pb, h->rects[0]->data[0] + h->rects[0]->linesize[0], h->rects[0]->linesize[0] * 2, @@ -211,7 +211,7 @@ FF_ENABLE_DEPRECATION_WARNINGS flush_put_bits(&pb); - return hdr - buf + put_bits_count(&pb)/8; + return hdr - buf + put_bytes_output(&pb); } static av_cold int xsub_encoder_init(AVCodecContext *avctx) diff --git a/libavfilter/vf_signature.c b/libavfilter/vf_signature.c index 32a6405e14..bf5973b867 100644 --- a/libavfilter/vf_signature.c +++ b/libavfilter/vf_signature.c @@ -561,7 +561,7 @@ static int binary_export(AVFilterContext *ctx, StreamContext *sc, const char* fi } flush_put_bits(&buf); - fwrite(buffer, 1, put_bits_count(&buf)/8, f); + fwrite(buffer, 1, put_bytes_output(&buf), f); fclose(f); av_freep(&buffer); return 0; diff --git a/libavformat/latmenc.c b/libavformat/latmenc.c index 91e96e90b0..5488361bde 100644 --- a/libavformat/latmenc.c +++ b/libavformat/latmenc.c @@ -225,7 +225,7 @@ static int latm_write_packet(AVFormatContext *s, AVPacket *pkt) flush_put_bits(&bs); - len = put_bits_count(&bs) >> 3; + len = put_bytes_output(&bs); if (len > 0x1fff) goto too_large; diff --git a/libavformat/movenc.c b/libavformat/movenc.c index 6790fe6c45..66ac34dfb6 100644 --- a/libavformat/movenc.c +++ b/libavformat/movenc.c @@ -579,7 +579,7 @@ static int mov_write_eac3_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *tra } } flush_put_bits(&pbc); - size = put_bits_count(&pbc) >> 3; + size = put_bytes_output(&pbc); avio_wb32(pb, size + 8); ffio_wfourcc(pb, "dec3"); From patchwork Thu Mar 25 15:49:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 26604 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 8169744B722 for ; Thu, 25 Mar 2021 17:50:48 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 6B81568AB8F; Thu, 25 Mar 2021 17:50:48 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f53.google.com (mail-ed1-f53.google.com [209.85.208.53]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 38F3A68A817 for ; Thu, 25 Mar 2021 17:50:41 +0200 (EET) Received: by mail-ed1-f53.google.com with SMTP id b16so2959480eds.7 for ; Thu, 25 Mar 2021 08:50:41 -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:reply-to :mime-version:content-transfer-encoding; bh=ASJS4Gt91MPaBkMNCtuaSq5AaHGUy90nZtkCg1/yvHg=; b=TD7Uf0zMJ9usuVIFiCRkmcV7V1iNs01liCK+v6oenwXJTsApEfzStqbeBYcRSJxo2T izXSYC8ANlKDYNs2qBhIOd98aj8XGNuAlU8vDzaIbYS/UpzuzWRsqo4lz//6jjZ6tuHQ SM2oWUn8f1eHnVbD5T5o1uwQXa4FtkKF6ID+njgspDHJuStL3BGnpjLntElBQAn/E5MD Xar6DLY9V9Jmk8aTocbnWJIgmjxrxAEpi7v6Fz23RhXeh9Rt8J7G/VuKg3N72i9onUlq n9L2NLfucwCAzU0AmEjRyE23pXAn0kyc0cRJ4NGm5Ie55zmsAENSISv2kFdNlSpyNNfA Y5lA== 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:reply-to:mime-version:content-transfer-encoding; bh=ASJS4Gt91MPaBkMNCtuaSq5AaHGUy90nZtkCg1/yvHg=; b=CrGp2DsaR3pXTOYW3kpc03Xlq3l82QixTcnThJkTqlt+jNd+bCURKONpAON/oxYyBs idzGaOWR1vlGIBV2ER44jMFCHmdQ37XHH4F8JITmOBrCUrK4piVXWihD7ZyemCrbEiqq n+RsB8W0zpKKWqDxbq8UYkeHIkszJYpwZvfGNB9jPwf/S1KFhIxjtjQDDBjqprHCmbdf Tg05DWPhAyklawg80k/8HRjyB9KS7czOKnaK1wmvuVdhjFSmx6DZuJtDJUceB/swf1Gw KO0tH0KhfNtzr8PXFxa7Fh8xVkaH2YeCcn8ZDc+tF1vQsaGUyDJVejWfsrrS3kJKF27D fVsA== X-Gm-Message-State: AOAM533twF7He4/iMEOTTdD4L6ZItcgReZG8rbH01p4HH9+Nrrrck+TM DR8G+BgvITjLeWtQNYGLln7tF03BoZb+Lg== X-Google-Smtp-Source: ABdhPJx6kirDcVsggVrrhdnMxvjYkxhz9JHlIN7pUoxiv24RJ6FlCQTqY7YdvCzTVlKXXh0cUlUN3A== X-Received: by 2002:aa7:c857:: with SMTP id g23mr9909680edt.86.1616687440409; Thu, 25 Mar 2021 08:50:40 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc08960.dynamic.kabel-deutschland.de. [188.192.137.96]) by smtp.gmail.com with ESMTPSA id o6sm2859103edw.24.2021.03.25.08.50.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Mar 2021 08:50:39 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 25 Mar 2021 16:49:56 +0100 Message-Id: <20210325154956.2405162-12-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210325154956.2405162-1-andreas.rheinhardt@gmail.com> References: <20210325154956.2405162-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 12/12] avcodec/put_bits: Don't set size_in_bits, fix overflow 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" A PutBitContext has a field called size_in_bits which is set to the context's bitsize init_put_bits(); but it isn't used at all (the PutBits API uses pointers directly and not bit indexes), so remove it (due to ABI concerns the actual element is only removed at the next bump). Furthermore, the multiplication inherent in setting this field can lead to undefined integer overflows. This is particularly true for FFV1, which uses a very big worst-case buffer (37*4*width*height; even ordinary 1080p triggers an overflow). Ticket #8350 is about this overflow which this commit fixes. This means that the effective range of the PutBits API is no longer restricted by the /8 as long as one isn't using put_bits_(count|left). Signed-off-by: Andreas Rheinhardt --- 1. One can use the full range of int (or of whatever type we choose) and check for whether enough bits are left with a function like this: static inline int put_bits_is_left(const PutBitContext *s, int n) { s->buf_end - s->buf_ptr >= (BUF_BITS - s->bit_left + n + 7) / 8; } (This presumes that n is not nearly the maximum of its type.) 2. I see three more ways in which the PutBits API can be improved: a) Don't use av_log/asserts in case of overreads; instead handle it via a context field for overread similar to the bytestream API. b) Integrate the PutBits API and the bytestream API: When one knows that the current position in the bitstream is byte-aligned and the PutBits context is flushed, one should be able to use the PutBits API with an API similar to the one for PutByteContexts. ff_mjpeg_encode_picture_header() is an example where this would be beneficial. c) If the output buffer is suitably padded, one could allow slight overwrites. In this case the check s->buf_end - s->buf_ptr >= sizeof(BitBuf) could be replaced by s->buf_ptr < s->buf_end (one could also add a new pointer to the context for the effective end (s->buf_end - FFMIN(buffer_size, sizeof(BitBuf))) and compare with that instead, but this seems to be a bit too involved). What do others think about this? libavcodec/put_bits.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/libavcodec/put_bits.h b/libavcodec/put_bits.h index e8bc86a82c..15c2650724 100644 --- a/libavcodec/put_bits.h +++ b/libavcodec/put_bits.h @@ -52,7 +52,9 @@ typedef struct PutBitContext { BitBuf bit_buf; int bit_left; uint8_t *buf, *buf_ptr, *buf_end; +#if LIBAVCODEC_VERSION_MAJOR < 59 int size_in_bits; +#endif } PutBitContext; /** @@ -69,7 +71,6 @@ static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, buffer = NULL; } - s->size_in_bits = 8 * buffer_size; s->buf = buffer; s->buf_end = s->buf + buffer_size; s->buf_ptr = s->buf; @@ -120,7 +121,6 @@ static inline void rebase_put_bits(PutBitContext *s, uint8_t *buffer, s->buf_end = buffer + buffer_size; s->buf_ptr = buffer + (s->buf_ptr - s->buf); s->buf = buffer; - s->size_in_bits = 8 * buffer_size; } /** @@ -414,7 +414,6 @@ static inline void set_put_bits_buffer_size(PutBitContext *s, int size) { av_assert0(size <= INT_MAX/8 - BUF_BITS); s->buf_end = s->buf + size; - s->size_in_bits = 8*size; } /**