From patchwork Wed Oct 5 19:37:37 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jon Toohill X-Patchwork-Id: 892 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.140.66 with SMTP id o63csp503011vsd; Wed, 5 Oct 2016 12:43:11 -0700 (PDT) X-Received: by 10.28.62.20 with SMTP id l20mr1376012wma.45.1475696591394; Wed, 05 Oct 2016 12:43:11 -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 e75si33078478wmg.2.2016.10.05.12.43.08; Wed, 05 Oct 2016 12:43:11 -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 D758C689B9C; Wed, 5 Oct 2016 22:42:51 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf0-f179.google.com (mail-pf0-f179.google.com [209.85.192.179]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id CEF66689A75 for ; Wed, 5 Oct 2016 22:42:44 +0300 (EEST) Received: by mail-pf0-f179.google.com with SMTP id e6so36556652pfk.1 for ; Wed, 05 Oct 2016 12:43:00 -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=5yVK0Lo7j+iQmrur7suPNpgc+SmexapC0SzHa1cP2IM=; b=MlHIzkyxh7lxhnGcTu/FlMxQU1Pw5n6yrMJAdFm6nYnGnWczoog2SevCW8Ie/Ceawk wS/GKSeji6gQeHvyIoE1vZ6YHlEky94ImYliJ49rkWGEMhvcbVBzSnpnmot5REc+m9Cu dMoJwqar1Z8Ww51vKdL2hFlJJAC91wOTUUiiNv/12ldw6ZS5YvQd5SESptZddGRi2EYV r4KErQ8MIcSsj/qjyWe3RSNqZyZL8C6W+f/0obM7hoPx3dcVks87fhNtoLBTHny/0NGP GiBhz+kPhQiY6bNycm0ALtpOSf+Hn/a7HilLGmONGKWkpNORUPGKXAWR5y7Gh4cEnP8e hnHQ== 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=5yVK0Lo7j+iQmrur7suPNpgc+SmexapC0SzHa1cP2IM=; b=bNo3TS2h+UL9pbkbi8fPoSdb3uoPLtEyV8kpMzySddu0dSFnS7CCBlURrXoLRFINS4 VXkkUdCqv5Bpb6gTGjoIJ4MH84Cr7uDvd9aVSfTibDM6+wpPBQ3nL2+gd5FxUmzxb0Sn 6j5X0S68ll/RhzKOKW5+aHxsizWZYIkUX30F4tabbIiEpdiNNLRldoYIHrDFCo87jcRI T42updpoaIRNddfC7qe3KAUPUYHbugsHWDqrLnFniAoCZvoa4buDi4S7OOGe2IDg/A5m CeVAXgiSK0xP7Sh6hDSIRelsIyg2jSLIFj+H4kohznTKd7Y7bXYBn10J85hmzdpe+QHC Oy3Q== X-Gm-Message-State: AA6/9RnTPxaCFIt7S8xiunlR6JssSqBbzicE270lWN0fRMNrZggNJXTeqB1tqaX/pJLUExL1 X-Received: by 10.98.42.216 with SMTP id q207mr9529264pfq.118.1475696273365; Wed, 05 Oct 2016 12:37:53 -0700 (PDT) Received: from mungolini.mtv.corp.google.com ([100.96.2.73]) by smtp.gmail.com with ESMTPSA id ra13sm16256249pac.29.2016.10.05.12.37.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 05 Oct 2016 12:37:52 -0700 (PDT) From: Jon Toohill To: ffmpeg-devel@ffmpeg.org Date: Wed, 5 Oct 2016 12:37:37 -0700 Message-Id: <1475696257-12390-3-git-send-email-jtoohill@google.com> X-Mailer: git-send-email 2.8.0.rc3.226.g39d4020 In-Reply-To: <1475696257-12390-1-git-send-email-jtoohill@google.com> References: <1475541908-21260-1-git-send-email-jtoohill@google.com> <1475696257-12390-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, 27 insertions(+), 7 deletions(-) diff --git a/libavformat/mp3enc.c b/libavformat/mp3enc.c index de63401..ddf4b93 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 = AV_RL32(side_data + 4) + 528 + 1; + if (!mp3->delay) + mp3->delay = FFMAX(AV_RL32(side_data) - 528 - 1, 0); + } else { + mp3->padding = 0; + } } } @@ -381,7 +392,7 @@ static void mp3_update_xing(AVFormatContext *s) AVReplayGain *rg; uint16_t tag_crc; uint8_t *toc; - int i, rg_size; + int i, rg_size, delay; /* replace "Xing" identification string with "Info" for CBR files. */ if (!mp3->has_variable_bitrate) @@ -422,6 +433,15 @@ static void mp3_update_xing(AVFormatContext *s) } } + /* write encoder delay/padding */ + if (mp3->delay >= 1 << 12) { + av_log(s, AV_LOG_WARNING, "Too many samples of initial padding.\n"); + } + if (mp3->padding >= 1 << 12) { + 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);