From patchwork Thu Dec 8 21:12:07 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: kode54@gmail.com X-Patchwork-Id: 1725 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.65.86 with SMTP id o83csp1102181vsa; Thu, 8 Dec 2016 13:30:47 -0800 (PST) X-Received: by 10.28.45.142 with SMTP id t136mr4183857wmt.110.1481232647027; Thu, 08 Dec 2016 13:30:47 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id m139si14868738wmb.129.2016.12.08.13.30.45; Thu, 08 Dec 2016 13:30:46 -0800 (PST) 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=@gmail.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; dmarc=fail (p=NONE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A3360689EDC; Thu, 8 Dec 2016 23:30:39 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-oi0-f67.google.com (mail-oi0-f67.google.com [209.85.218.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B8061689E99 for ; Thu, 8 Dec 2016 23:30:32 +0200 (EET) Received: by mail-oi0-f67.google.com with SMTP id m75so51790093oig.1 for ; Thu, 08 Dec 2016 13:30:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=R2Nyunry16vH2pj3PkU+jV2cS7ZuhjyZYLtS/cNQiWo=; b=mLYCx0f0XguRRPB4tQKEEutphjXQoiSTBn4TJ1l93DwkUtudZtT7ovJRmo8s9hIGfz yeacIGA4UulNZeFAvLrKwy7SqD34UrTRoLPfI58X6tdmwfGSBCcGmn0qYEdQ01aK2aiL kBKlyUmk+wr7B1jAUtD7WWkrMSyZpNqjolQWgYaLc1yxj9jNn/Y4zUmethPXpbOwjuKz xepA++byffFkGWtpjOU8vh6d9VuEgTx4769clGmOCVtWTG2Y20Ai+JWY1PxqzOwdjAam F3/ViQpaQcnTGWIcoJPU7lJSzpfAtY6kLezn1vRFZSbCyBPQG+xqQfrnuxzqTnsZU3yb r7GA== 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=R2Nyunry16vH2pj3PkU+jV2cS7ZuhjyZYLtS/cNQiWo=; b=NVIZwUbhQR5iYedfxV4IWhCCBQ2fNK99WXzLbGOmXVf/lAAuokc7wyK+0OhNnxANnk oy45UYu6dU2AG9oDDgPypZy1DJWJKDJQghqhl/W6/Z0AqrLer5G9Bbn0rJ+ghilxR7EN I6Wc59bR46a2gX2C0cskkU/ziM/fSRfrbhvosnoRN0lpQMg4uIkr5Tw3C1y+4JCiI2kv iZlg0q5uAQ597JStoILk3opqoIuFzBIofcBgfa4AZYXzgUbjTh9eT6mH6imntWs568KH EKi7IOQXWfqfs13qjj29VhoicnDIee911xRm51MNO2ipXAxgVpp1p4AQPJ5kjTChG+Gv bQzA== X-Gm-Message-State: AKaTC03mRqzB7ZOiBeQ8NnfHY70UTtf5u/rL1VHzSEcFYEEzJw6PJpwdl/36vizJ4b6AgA== X-Received: by 10.202.74.197 with SMTP id x188mr35962211oia.154.1481231558546; Thu, 08 Dec 2016 13:12:38 -0800 (PST) Received: from localhost.localdomain (99-11-158-33.lightspeed.irvnca.sbcglobal.net. [99.11.158.33]) by smtp.gmail.com with ESMTPSA id e63sm11650230oih.23.2016.12.08.13.12.37 (version=TLS1 cipher=AES128-SHA bits=128/128); Thu, 08 Dec 2016 13:12:38 -0800 (PST) From: Christopher Snowhill To: ffmpeg-devel@ffmpeg.org Date: Thu, 8 Dec 2016 13:12:07 -0800 Message-Id: <20161208211207.12297-2-kode54@gmail.com> X-Mailer: git-send-email 2.9.3 (Apple Git-75) In-Reply-To: <20161208211207.12297-1-kode54@gmail.com> References: <20160807100617.GN5460@nb4> <20161208211207.12297-1-kode54@gmail.com> Subject: [FFmpeg-devel] [PATCHv4] avformat: parse iTunes gapless information 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: Christopher Snowhill MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" --- libavformat/mp3dec.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/libavformat/mp3dec.c b/libavformat/mp3dec.c index 56c7f8c..47f4028 100644 --- a/libavformat/mp3dec.c +++ b/libavformat/mp3dec.c @@ -295,6 +295,59 @@ static void mp3_parse_vbri_tag(AVFormatContext *s, AVStream *st, int64_t base) } } +static void mp3_parse_itunes_tag(AVFormatContext *s, AVStream *st, MPADecodeHeader *c, int64_t base, int vbrtag_size, unsigned int *size, uint64_t *duration) +{ + uint32_t v; + AVDictionaryEntry *de; + MP3DecContext *mp3 = s->priv_data; + size_t length; + uint32_t zero, start_pad, end_pad; + uint64_t last_eight_frames_offset; + int i; + + if (!s->metadata || !(de = av_dict_get(s->metadata, "iTunSMPB", NULL, 0))) + return; + + length = strlen(de->value); + + /* Minimum length is one digit per field plus the whitespace, maximum length should depend on field type + * There are four fields we need in the first six, the rest are currently zero padding */ + if (length < (12 + 11) || length > (10 * 8 + 2 * 16 + 11)) + return; + + if (sscanf(de->value, "%"PRIx32" %"PRIx32" %"PRIx32" %"PRIx64" %"PRIx32" %"PRIx64, &zero, &start_pad, &end_pad, duration, &zero, &last_eight_frames_offset) < 6 || + duration < 0 || + start_pad > (576 * 2 * 32) || + end_pad > (576 * 2 * 64) || + last_eight_frames_offset >= (avio_size(s->pb) - base - vbrtag_size)) { + *duration = 0; + return; + } + + mp3->start_pad = (signed int) start_pad; + mp3->end_pad = (signed int) end_pad; + if (end_pad >= 528 + 1) + mp3->end_pad = end_pad - (528 + 1); + st->start_skip_samples = mp3->start_pad + 528 + 1; + av_log(s, AV_LOG_DEBUG, "pad %d %d\n", mp3->start_pad, mp3->end_pad); + if (!s->pb->seekable) + return; + + *size = (unsigned int) last_eight_frames_offset; + if (avio_seek(s->pb, base + vbrtag_size + last_eight_frames_offset, SEEK_SET) < 0) + return; + + for (i = 0; i < 8; i++) { + v = avio_rb32(s->pb); + if (ff_mpa_check_header(v) < 0) + return; + if (avpriv_mpegaudio_decode_header(c, v) != 0) + break; + *size += c->frame_size; + avio_skip(s->pb, c->frame_size - 4); + } +} + /** * Try to find Xing/Info/VBRI tags and compute duration from info therein */ @@ -303,8 +356,10 @@ static int mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, int64_t base) uint32_t v, spf; MPADecodeHeader c; int vbrtag_size = 0; + unsigned int size = 0; MP3DecContext *mp3 = s->priv_data; int ret; + uint64_t duration = 0; ffio_init_checksum(s->pb, ff_crcA001_update, 0); @@ -327,16 +382,29 @@ static int mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, int64_t base) mp3_parse_vbri_tag(s, st, base); if (!mp3->frames && !mp3->header_filesize) + vbrtag_size = 0; + + mp3_parse_itunes_tag(s, st, &c, base, vbrtag_size, &size, &duration); + + if (!mp3->frames && !size && !duration) return -1; /* Skip the vbr tag frame */ avio_seek(s->pb, base + vbrtag_size, SEEK_SET); - if (mp3->frames) + if (duration) + st->duration = av_rescale_q(duration, (AVRational){1, c.sample_rate}, st->time_base); + else if (mp3->frames) st->duration = av_rescale_q(mp3->frames, (AVRational){spf, c.sample_rate}, st->time_base); if (mp3->header_filesize && mp3->frames && !mp3->is_cbr) st->codecpar->bit_rate = av_rescale(mp3->header_filesize, 8 * c.sample_rate, mp3->frames * (int64_t)spf); + if (size) { + if (duration) + st->codecpar->bit_rate = av_rescale(size, 8 * c.sample_rate, duration); + else if (mp3->frames) + st->codecpar->bit_rate = av_rescale(size, 8 * c.sample_rate, mp3->frames * (int64_t)spf); + } return 0; }