From patchwork Sat May 30 16:05:18 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 20000 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 933F144A6AD for ; Sat, 30 May 2020 19:06:34 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 82C4468B038; Sat, 30 May 2020 19:06:34 +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 E9A7768AEBC for ; Sat, 30 May 2020 19:06:26 +0300 (EEST) Received: by mail-wr1-f68.google.com with SMTP id h5so1247857wrc.7 for ; Sat, 30 May 2020 09:06:26 -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=4WtmIVoFNLme2K0ElTDQi6n09sAM/ZsSGvp76uoNeZE=; b=LktALCR9Od8/pNhlmLhPzmZ1XUOVJ7TjT54SIkanhRDnNHyDORlbeHiT13GJ0z7r4x JWyTwMeKVcACj5V5z0YD7Mh+E/Uk83n+hxZvc9Ami0+TMt3ZV4r/vB7i2GWfIh4CaXIx yoTIllgpMaPeuulppQF8ejDN2tVs2p7nUMMTB27PBagVDSrQYy4K2Vjy3obAuKDGgdXW BQsc/AgkRmuVn4yrbvbx6SXt15YCT/IW56ygQd/xh0smsocP2qIf32l1Tp3bg2R2pfud 8DTj81EkH9ePrh9gm1+JDgVxpHFgQlr3y06tjhmV0ff8xRdw6LN0nw6A5IqBtRRPq63A OIRQ== 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=4WtmIVoFNLme2K0ElTDQi6n09sAM/ZsSGvp76uoNeZE=; b=nWQznnXRksI3zu87u3wEC0tDugFcooJ8RqmqgCMQh+VhlmOKdjPJ56vM22tgzRdkLU +vsZsL+hHK8nUZiyKbtealXeZdyleFFFiNmYS+sKxiwoTSZ3CHO4X5ScR4ErvhJcPfQa KslaOIBORtN8+qqFlGrQoYQQ6ZbFsBoONrD+h3RdqZIE2QpXhFyzBLb86XqhlwiJVWlU sbZJiS1WTzCeh9f5jt+0sys0pjb3GlCkEjfk+dhU+Bdc9KjdiKMNDtD+EcR2P+hNy2Uv mZS9BdGnklJZ+9AloUF9F0z72MS+ScP7JLsqF9ik1B2mXivJx9D6RSFMJ5Qyf1LednVl 4Zng== X-Gm-Message-State: AOAM532jD484nzSNz/k7pTRoTUR7gF0MzjtEUU9BBPuUc7ep2F8XGXnO bR4yVnSzXkNXdRT6HmD46R/2u1g6 X-Google-Smtp-Source: ABdhPJzaMratPgpsQXCXxG7O0coqLnfxyULPN4NURK1ZcWv2W3MgD3b85YCz6PtVrIbjjuy2zwvVgA== X-Received: by 2002:a5d:5483:: with SMTP id h3mr1296245wrv.10.1590854786019; Sat, 30 May 2020 09:06:26 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1ab57.dynamic.kabel-deutschland.de. [188.193.171.87]) by smtp.gmail.com with ESMTPSA id v27sm15186517wrv.81.2020.05.30.09.06.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 30 May 2020 09:06:25 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 30 May 2020 18:05:18 +0200 Message-Id: <20200530160541.29517-13-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200530160541.29517-1-andreas.rheinhardt@gmail.com> References: <20200530160541.29517-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 13/36] avcodec/mp3_header_decompress_bsf: Improve readability X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Up until now, the mp3_header_decompress bsf used a single variable for two purposes in a bitfield-like manner: To contain the bitrate index as well as whether the padding_bit is set. The former was contained in bits 1-4, while the latter was given by bit 0. This made the code hard to understand. This commit changes this by using dedicated variables for the bitrate index as well as for the offset of the old data in the new buffer. The latter implicitly contains whether the padding_bit is set. Also add const to the pointer to the source data; it is of course not modified. Signed-off-by: Andreas Rheinhardt --- libavcodec/mp3_header_decompress_bsf.c | 35 ++++++++++++++------------ 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/libavcodec/mp3_header_decompress_bsf.c b/libavcodec/mp3_header_decompress_bsf.c index fe021deed3..3d357dd27e 100644 --- a/libavcodec/mp3_header_decompress_bsf.c +++ b/libavcodec/mp3_header_decompress_bsf.c @@ -32,8 +32,8 @@ static int mp3_header_decompress(AVBSFContext *ctx, AVPacket *out) uint32_t header; int sample_rate= ctx->par_in->sample_rate; int sample_rate_index=0; - int lsf, mpeg25, bitrate_index, frame_size, ret; - uint8_t *buf; + int lsf, mpeg25, bitrate_index, frame_size, offset, ret; + const uint8_t *buf; int buf_size; ret = ff_bsf_get_packet(ctx, &in); @@ -69,23 +69,25 @@ static int mp3_header_decompress(AVBSFContext *ctx, AVPacket *out) sample_rate= avpriv_mpa_freq_tab[sample_rate_index] >> (lsf + mpeg25); //in case sample rate is a little off - for(bitrate_index=2; bitrate_index<30; bitrate_index++){ - frame_size = avpriv_mpa_bitrate_tab[lsf][2][bitrate_index>>1]; - frame_size = (frame_size * 144000) / (sample_rate << lsf) + (bitrate_index&1); - if(frame_size == buf_size + 4) - break; - if(frame_size == buf_size + 6) + for (bitrate_index = 1; bitrate_index < 15; bitrate_index++) { + frame_size = avpriv_mpa_bitrate_tab[lsf][2][bitrate_index]; + frame_size = (frame_size * 144000) / (sample_rate << lsf); + offset = frame_size - buf_size; + if (3 <= offset && offset <= 6) break; } - if(bitrate_index == 30){ + if (bitrate_index == 15) { av_log(ctx, AV_LOG_ERROR, "Could not find bitrate_index.\n"); ret = AVERROR(EINVAL); goto fail; } - header |= (bitrate_index&1)<<9; - header |= (bitrate_index>>1)<<12; - header |= (frame_size == buf_size + 4)<<16; + header |= bitrate_index << 12; + if (offset & 1) { + header |= 1 << 9; // padding_bit + frame_size++; + offset++; + } ret = av_new_packet(out, frame_size); if (ret < 0) @@ -95,10 +97,10 @@ static int mp3_header_decompress(AVBSFContext *ctx, AVPacket *out) av_packet_unref(out); goto fail; } - memcpy(out->data + frame_size - buf_size, buf, buf_size + AV_INPUT_BUFFER_PADDING_SIZE); + memcpy(out->data + offset, buf, buf_size); if(ctx->par_in->channels==2){ - uint8_t *p= out->data + frame_size - buf_size; + uint8_t *p = out->data + offset; if(lsf){ FFSWAP(int, p[1], p[2]); header |= (p[1] & 0xC0)>>2; @@ -109,10 +111,11 @@ static int mp3_header_decompress(AVBSFContext *ctx, AVPacket *out) } } - if (frame_size == buf_size + 6) { + if (offset == 6) { //FIXME actually set a correct crc instead of 0 AV_WN16(out->data + 4, 0); - } + } else + header |= 1 << 16; // protection_bit AV_WB32(out->data, header); ret = 0;