From patchwork Thu Apr 16 23:54:34 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 19015 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 D35F7448684 for ; Fri, 17 Apr 2020 02:54:55 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BB52D68BCFC; Fri, 17 Apr 2020 02:54:55 +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 AB10F680055 for ; Fri, 17 Apr 2020 02:54:48 +0300 (EEST) Received: by mail-wr1-f68.google.com with SMTP id h26so871116wrb.7 for ; Thu, 16 Apr 2020 16:54:48 -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=1PID7Y3EY0/OiRXDPf7OvVaKsW5cEfC9iKORHXjBVf0=; b=OyJ82TU2YGGoxn9nCu710fWbZuAprgPWaAeEJtNU0r+AGGDriBlDsIRVQ6JggBE4pu 6/w645sVQx5aTkJRCiHW1xzv112NHKsnYw6pjHNfM32DWyAY5RGsNMumVfcRwwq5KGqs igbv2LA/Yad6wAY07oNapjvCYOMK4ime4BoRrA87DYYSMK5Fn7/bdnFh5FWGiF8XU6to kUsXfSfSBKvDloXKvf7Rhb6oPHfpTkbZ9HoZXVLitii2NinBPHmSwyDWbKOOr4FiCVly R4WNtAsdQugM3Y/amgNh09pkD65g7pKZNdL2lsur80p/SLfyFleajUWuKdGLn9Eh98yK eC2Q== 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=1PID7Y3EY0/OiRXDPf7OvVaKsW5cEfC9iKORHXjBVf0=; b=HJkKPMzDQwVzMMiJXvilsS64wYr7+8BAmVdVDJuV5HI6iiloYbV05J6uikweqSg6D9 uMq+byKQ2ovUNTH65RvlSDEjr3g2edLYSo3NoeeMYij0sfiDx2RwXC2CmeXocRrEJvlA LgV5FJJX6K+YYSOu+FAAzaXV3EzVjJvBzUtl4aPMwOvXLdbbHEXoovugD1bcdOHJn+0v K5lA1SUqyejs79CFZcqXFYDlE1F590I8A+tDhxxzxj5IhOCDbKJJ7W0gepm0fTuB6Cjg df1R8+IpAv0lJuMKMjjUZE4l/JpJRyZQBxdRoWWVa0xriWjTIb97GhSOzz5EZIHch0hZ Yeaw== X-Gm-Message-State: AGi0PuaSPaW0ZM2+VLTSTLhqmr/BygAat5g0ISVsZx+AyoGrFwcHNLi8 M7dpRJn1syXgh0Xw8QqP0rIzLT5B X-Google-Smtp-Source: APiQypIPoDE7P5WLcd7nSYp3Xq8sPfKJ2pCBLbx0sFFFb/63WumiskuTUWyeBmRZp/vylHj/ExM2Iw== X-Received: by 2002:adf:df01:: with SMTP id y1mr740155wrl.401.1587081287758; Thu, 16 Apr 2020 16:54:47 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1ab57.dynamic.kabel-deutschland.de. [188.193.171.87]) by smtp.gmail.com with ESMTPSA id i17sm5603488wml.23.2020.04.16.16.54.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Apr 2020 16:54:47 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Fri, 17 Apr 2020 01:54:34 +0200 Message-Id: <20200416235435.20409-2-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200415210614.29152-1-andreas.rheinhardt@gmail.com> References: <20200415210614.29152-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 4/5] avformat/matroskaenc: Refactor writing EBML lengths 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" This commit factors the ability to write ordinary EBML numbers out of the functions for writing EBML lengths. This is in preparation for future commits. Signed-off-by: Andreas Rheinhardt --- libavformat/matroskaenc.c | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c index 7cea813dc0..92cd819312 100644 --- a/libavformat/matroskaenc.c +++ b/libavformat/matroskaenc.c @@ -190,19 +190,38 @@ static void put_ebml_size_unknown(AVIOContext *pb, int bytes) } /** - * Calculate how many bytes are needed to represent the length field - * of an EBML element whose payload has a given length. + * Returns how many bytes are needed to represent a number + * as EBML variable length integer. */ -static int ebml_length_size(uint64_t length) +static int ebml_num_size(uint64_t num) { int bytes = 0; - length++; do { bytes++; - } while (length >>= 7); + } while (num >>= 7); return bytes; } +/** + * Calculate how many bytes are needed to represent the length field + * of an EBML element whose payload has a given length. + */ +static int ebml_length_size(uint64_t length) +{ + return ebml_num_size(length + 1); +} + +/** + * Write a number as EBML variable length integer on `bytes` bytes. + * `bytes` is taken literally without checking. + */ +static void put_ebml_num(AVIOContext *pb, uint64_t num, int bytes) +{ + num |= 1ULL << bytes * 7; + for (int i = bytes - 1; i >= 0; i--) + avio_w8(pb, (uint8_t)(num >> i * 8)); +} + /** * Write a length as EBML variable length integer. * @@ -211,7 +230,7 @@ static int ebml_length_size(uint64_t length) */ static void put_ebml_length(AVIOContext *pb, uint64_t length, int bytes) { - int i, needed_bytes = ebml_length_size(length); + int needed_bytes = ebml_length_size(length); // sizes larger than this are currently undefined in EBML av_assert0(length < (1ULL << 56) - 1); @@ -221,10 +240,7 @@ static void put_ebml_length(AVIOContext *pb, uint64_t length, int bytes) // The bytes needed to write the given size must not exceed // the bytes that we ought to use. av_assert0(bytes >= needed_bytes); - - length |= 1ULL << bytes * 7; - for (i = bytes - 1; i >= 0; i--) - avio_w8(pb, (uint8_t)(length >> i * 8)); + put_ebml_num(pb, length, bytes); } /**