From patchwork Mon Oct 17 21:55:04 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jon Toohill X-Patchwork-Id: 1041 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.140.133 with SMTP id o127csp463153vsd; Mon, 17 Oct 2016 14:55:27 -0700 (PDT) X-Received: by 10.194.116.225 with SMTP id jz1mr12702603wjb.224.1476741327921; Mon, 17 Oct 2016 14:55:27 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id o5si44176013wjf.4.2016.10.17.14.55.27; Mon, 17 Oct 2016 14:55:27 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0BC9D689203; Tue, 18 Oct 2016 00:55:20 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pa0-f45.google.com (mail-pa0-f45.google.com [209.85.220.45]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D6A8A68049C for ; Tue, 18 Oct 2016 00:55:12 +0300 (EEST) Received: by mail-pa0-f45.google.com with SMTP id ry6so65454599pac.3 for ; Mon, 17 Oct 2016 14:55:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=YfEg8j77TWAHED6IBRbYzmNYN6Ze1HfNw4/B6COwvsM=; b=Qv+JpQoVsmh+UtCwOpmZtN3KuMh2c8Ga1y7v0AHpUFGEFUxmSEFYZYKyLFsk+K+EFq orvLL8oH0D17dyZGUfdYdWSrruNC0hyYboPEKtZzIV5sU0RAnHSgF7XUdB0+z29lBDvT hgIANN65DzdHCU2uoifECDgtqYY4hJk7qsSX0woutpPT0fzhhGpyr7SXoTQhTxWNXx/C ix5TGLWkNjCURh6F+CABHUNOnIyDnwbU3ByfP3JRcE0g8VSVN1mKmsbnSAW4rru9VUjY cP871Nwf/yAxSz4RFj9IkNsJJ7WNyaN6vyS9+6Vao6EmFeCsLzciT4XrFrnA4zLr7yUs 39eg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=YfEg8j77TWAHED6IBRbYzmNYN6Ze1HfNw4/B6COwvsM=; b=YWGmIBgacd4XYwoMjyG/KeIjux6fjcHwVtOi4MpQORVaRxV6dFXvdjpH+VybOdZudO Gpn4CBj1E37zI0/WCI/VpgiCInKScvD4WCvjEZbkmxBrWXrMKffATdOqrZ4b0tnYmJ6v Ep8LVuxzQHxuTEV+HPdY6FVwL/SRO4l3sQHPQ54c17BR+r3V70ZW/Aarc/Eg8HnaBP3S Rr3twWn8wpW7dau8NUugwadW0sgaehFo2HpzBYSzsNoK+r9WVukl1R20rwp5RdPSIvYq weYyYngv6v6aPwhvaThyIHo9Mho3dTrzjJ73Pbx3W6NoGJnleZBmnO6jeRxXh4xbmtrK 81mA== X-Gm-Message-State: AA6/9RnEIPBH2Y/c3C0mihME2+bMA5SooGrhLLIRdem+1fRmiOWvTFmus6ReJYhZ0ztVkxd7 X-Received: by 10.66.178.78 with SMTP id cw14mr33398940pac.206.1476741313473; Mon, 17 Oct 2016 14:55:13 -0700 (PDT) Received: from mungolini.mtv.corp.google.com ([100.96.2.61]) by smtp.gmail.com with ESMTPSA id y125sm50309404pfg.61.2016.10.17.14.55.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 17 Oct 2016 14:55:11 -0700 (PDT) From: Jon Toohill To: ffmpeg-devel@ffmpeg.org Date: Mon, 17 Oct 2016 14:55:04 -0700 Message-Id: <1476741304-16328-3-git-send-email-jtoohill@google.com> X-Mailer: git-send-email 2.8.0.rc3.226.g39d4020 In-Reply-To: <1476741304-16328-1-git-send-email-jtoohill@google.com> References: <1475696257-12390-1-git-send-email-jtoohill@google.com> <1476741304-16328-1-git-send-email-jtoohill@google.com> Subject: [FFmpeg-devel] [PATCH 2/2] lavf/mp3enc: write encoder delay/padding upon closing 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: Jon Toohill MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" --- libavformat/mp3enc.c | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/libavformat/mp3enc.c b/libavformat/mp3enc.c index de63401..4c97fa1 100644 --- a/libavformat/mp3enc.c +++ b/libavformat/mp3enc.c @@ -111,6 +111,8 @@ typedef struct MP3Context { uint64_t bag[XING_NUM_BAGS]; int initial_bitrate; int has_variable_bitrate; + int delay; + int padding; /* index of the audio stream */ int audio_stream_idx; @@ -247,12 +249,7 @@ static int mp3_write_xing(AVFormatContext *s) ffio_fill(dyn_ctx, 0, 8); // empty replaygain fields avio_w8(dyn_ctx, 0); // unknown encoding flags avio_w8(dyn_ctx, 0); // unknown abr/minimal bitrate - - // encoder delay - if (par->initial_padding - 528 - 1 >= 1 << 12) { - av_log(s, AV_LOG_WARNING, "Too many samples of initial padding.\n"); - } - avio_wb24(dyn_ctx, FFMAX(par->initial_padding - 528 - 1, 0)<<12); + avio_wb24(dyn_ctx, 0); // empty encoder delay/padding avio_w8(dyn_ctx, 0); // misc avio_w8(dyn_ctx, 0); // mp3gain @@ -345,10 +342,24 @@ static int mp3_write_audio_packet(AVFormatContext *s, AVPacket *pkt) #endif if (mp3->xing_offset) { + uint8_t *side_data = NULL; + int side_data_size = 0; + mp3_xing_add_frame(mp3, pkt); mp3->audio_size += pkt->size; mp3->audio_crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE), mp3->audio_crc, pkt->data, pkt->size); + + side_data = av_packet_get_side_data(pkt, + AV_PKT_DATA_SKIP_SAMPLES, + &side_data_size); + if (side_data && side_data_size >= 10) { + mp3->padding = FFMAX(AV_RL32(side_data + 4) + 528 + 1, 0); + if (!mp3->delay) + mp3->delay = FFMAX(AV_RL32(side_data) - 528 - 1, 0); + } else { + mp3->padding = 0; + } } } @@ -422,6 +433,17 @@ static void mp3_update_xing(AVFormatContext *s) } } + /* write encoder delay/padding */ + if (mp3->delay >= 1 << 12) { + mp3->delay = (1 << 12) - 1; + av_log(s, AV_LOG_WARNING, "Too many samples of initial padding.\n"); + } + if (mp3->padding >= 1 << 12) { + mp3->padding = (1 << 12) - 1; + av_log(s, AV_LOG_WARNING, "Too many samples of trailing padding.\n"); + } + AV_WB24(mp3->xing_frame + mp3->xing_offset + 141, (mp3->delay << 12) + mp3->padding); + AV_WB32(mp3->xing_frame + mp3->xing_offset + XING_SIZE - 8, mp3->audio_size); AV_WB16(mp3->xing_frame + mp3->xing_offset + XING_SIZE - 4, mp3->audio_crc);