From patchwork Fri Jan 28 05:16:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christopher Snowhill X-Patchwork-Id: 33894 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2c4e:0:0:0:0 with SMTP id x14csp126783iov; Thu, 27 Jan 2022 21:17:08 -0800 (PST) X-Google-Smtp-Source: ABdhPJzVKxwUM1WjCPlby2EOQvaM1URhW5i6jR/+fZR6svLy+3ddSXj+Jm/nEJeqDqHFCZtW8r+D X-Received: by 2002:a17:907:d28:: with SMTP id gn40mr807107ejc.750.1643347028435; Thu, 27 Jan 2022 21:17:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643347028; cv=none; d=google.com; s=arc-20160816; b=gCHaDpF1EeyO5WdEbA9u+cAm9aVLBzULGLFozfTCKyi2jGaucfEkkksxWgcSbUnTo6 BL0yfvQaicTA3uRER3lOgHUyFzwOA2kelExmmvsUW6j+vjGqcjYPGRTHOxp1ZZv4rTLh xTv7kROOhmuMYiiTlBJeQgheBmkHqnU06/FDNlthVOxMPu2RNCVolA+Pbpc9pGAOXJLr kTB9kU4UXNlq9VzUsfo0OOLaBv/B3chWnGr5n4S5hTAOzj/osDJYWU2PDrc4uWeiQbD/ 1KZJvOncf1+Z65zo7SrYle3VlSXOIZlMLH9bdt8+gZ73kZhM8IZ47JrAPKnmz4VPDs5U Cb9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:to:message-id:date:from:mime-version :dkim-signature:delivered-to; bh=h0YPT9AGDn/yhfMtBj4AMBCJE1RHUFtWz878YSPwOKA=; b=yZbedxkEbvamdEc+xFtXN8VQs0UWpuVfK/LAcMGC37N5UFllbmR7Gskd9IGsS8lD/T lWMRuMSN0yFEeoBOGrE37kVbcXN1ixPVCY9sA+G2BrUZMFjyRlWVcUyJ+B5PemDMzuFm OXhD7dxTb5GYKamGqXa06sVPtanCiPjFG2H/5pR0pvZT16Vjv5O2L2WOsb0zsqO0NelR DJCIc3Hsi2L+5/2lyxi241j15JKpxbA4078D9YUQiB7P3/QUYsX5l0zVQzX9epEEEkoR EFv2IVcDdKOJUtlOQ+YDUYPnJ5+YzS38hllMcRFkzZm3bDJPRecAMzwpVCSI5N6HUKTV 4oKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=dwlB6eAy; 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 sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id e26si2467449eds.21.2022.01.27.21.17.07; Thu, 27 Jan 2022 21:17:08 -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 header.s=20210112 header.b=dwlB6eAy; 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 sp=QUARANTINE 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 99E0768B237; Fri, 28 Jan 2022 07:17:03 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pj1-f54.google.com (mail-pj1-f54.google.com [209.85.216.54]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9FC9968A927 for ; Fri, 28 Jan 2022 07:16:57 +0200 (EET) Received: by mail-pj1-f54.google.com with SMTP id h12so5461848pjq.3 for ; Thu, 27 Jan 2022 21:16:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:from:date:message-id:subject:to; bh=eA7ktrz+SRZHuijfdP6dV7Hg7WPV8ankPG5WFAUF56o=; b=dwlB6eAyjnZUy0w2/beLk8HBKR6JRXVFpfCzfBaMKTrmurmNyF4utSPn+FPD2IPqv6 ns2W0DvKFk2L3VLE0d+xdMFHbvmozO+MksqGWexL1nSz0oM7AVKevLNsCocAycqdpqTC LW09HJx5dfYB9yBTLYTgJzFqglsFn0Wva8M824YyCD6JoQ6Ev1goNexjd6Kp5BEliH6h V6Ph+fR4wYXUg8jsN0NOE6NEDN/xopCIwMB2nkB99b9i9S8UwgYp6PyG4LpRqCauwnM6 YfCYWUw1xWnjA/2+aaNCy4t3loKlVG9pRuQ7JqhsxYjwLFK4ESu/Dpu73n1ciFlZR03O /wLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=eA7ktrz+SRZHuijfdP6dV7Hg7WPV8ankPG5WFAUF56o=; b=GGjRGUFcjA3cSjyhVLhb//7i6g786p656tke2jXVcK27hE8O7I82SDPSUT942J8D+Q Sj+PdJUBk81JIw8fuWCDJ4uxy+6N5P+wCr8NYS9k1u8TmhaNobF6Ol7pdfSiHrTPOlBE aZVvg5KzYnKhsl6j08DxFK49UQtdJLCdwBETGXUMTrUEIP0M5bAvJ6S5lNopSHUSSA9A FUCVxU/Q07aXNMBMyI3zeDMGUwg+Ps+2yQqX/s52o4sUin1AMzsD1a0JY688FzBWdtix d9zoFhFkm4MaznoqqoK6Usag8W8IVlCU652zdZXUoTd3VbekftJIspoenJD8YsF717Wu EZcw== X-Gm-Message-State: AOAM531fi376qjSIDMSaJqxO64+Zyd9gsrf88XvVoHSD1hKkYHZJyPwz K7kG5AM3B8Qpmklhrsm9f9i9eX0Qc7lw/dsOvkLaKV+l3yU= X-Received: by 2002:a17:902:dac2:: with SMTP id q2mr7092568plx.15.1643347015404; Thu, 27 Jan 2022 21:16:55 -0800 (PST) MIME-Version: 1.0 From: Christopher Snowhill Date: Thu, 27 Jan 2022 21:16:44 -0800 Message-ID: To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] [PATCH] avformat/mp3dec: Parse iTunes gapless info X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: HQcgBQT3vnxK Parse the ID3v2 iTunSMPB comment tag containing gapless decoding info, and also add the expected test results for the implementation. Signed-off-by: Christopher Snowhill --- Curses, Apple Mail is broken, too. Using git imap-send instead. If this doesn't work, I haven't a damn clue how to send email anywhere. I don't have any mail clients that won't munge my mail. Git send-email is broken on macOS 12.2 because it uses System Perl, and System Perl refuses to find CPAN installed IO::Socket::SSL. libavformat/mp3dec.c | 86 ++++++++++++++++++++++++++++++- tests/fate/gapless.mak | 3 ++ tests/ref/fate/gapless-mp3-itunes | 5 ++ 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 tests/ref/fate/gapless-mp3-itunes -- 2.34.1 diff --git a/libavformat/mp3dec.c b/libavformat/mp3dec.c index f617348b2e..d03174db03 100644 --- a/libavformat/mp3dec.c +++ b/libavformat/mp3dec.c @@ -313,6 +313,75 @@ 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 spf) +{ + uint32_t v; + AVDictionaryEntry *de; + FFStream *const sti = ffstream(st); + MP3DecContext *mp3 = s->priv_data; + size_t length; + uint32_t zero, start_pad, end_pad; + uint64_t last_eight_frames_offset; + int64_t temp_duration, file_size; + 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; + + file_size = avio_size(s->pb); + if (file_size < 0) + file_size = 0; + + if (sscanf(de->value, "%"PRIx32" %"PRIx32" %"PRIx32" %"PRIx64" %"PRIx32" %"PRIx64, &zero, &start_pad, &end_pad, &temp_duration, &zero, &last_eight_frames_offset) < 6 || + temp_duration < 0 || + start_pad > (576 * 2 * 32) || + end_pad > (576 * 2 * 64) || + (file_size && (last_eight_frames_offset >= (file_size - base - vbrtag_size)))) { + *duration = 0; + return; + } + + *duration = temp_duration; + + if (end_pad >= 528 + 1) { + mp3->start_pad = start_pad; + mp3->end_pad = end_pad - (528 + 1); + sti->start_skip_samples = mp3->start_pad + 528 + 1; + if (mp3->frames) { + sti->first_discard_sample = -mp3->end_pad + 528 + 1 + mp3->frames * (int64_t)spf; + sti->last_discard_sample = mp3->frames * (int64_t)spf; + } + if (!st->start_time) + st->start_time = av_rescale_q(sti->start_skip_samples, + (AVRational){1, c->sample_rate}, + st->time_base); + 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 */ @@ -321,8 +390,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); @@ -345,16 +416,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, spf); + + 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; } diff --git a/tests/fate/gapless.mak b/tests/fate/gapless.mak index 68a396e187..b8c93b984c 100644 --- a/tests/fate/gapless.mak +++ b/tests/fate/gapless.mak @@ -1,6 +1,9 @@ FATE_GAPLESS-$(CONFIG_MP3_DEMUXER) += fate-gapless-mp3 fate-gapless-mp3: CMD = gapless $(TARGET_SAMPLES)/gapless/gapless.mp3 "-c:a mp3" +FATE_GAPLESS-$(CONFIG_MP3_DEMUXER) += fate-gapless-mp3-itunes +fate-gapless-mp3-itunes: CMD = gapless $(TARGET_SAMPLES)/gapless/gapless-itunes.mp3 "-c:a mp3" + FATE_GAPLESSINFO_PROBE-$(CONFIG_MP3_DEMUXER) += fate-gapless-mp3-side-data fate-gapless-mp3-side-data: CMD = ffprobe_demux $(TARGET_SAMPLES)/gapless/gapless.mp3 diff --git a/tests/ref/fate/gapless-mp3-itunes b/tests/ref/fate/gapless-mp3-itunes new file mode 100644 index 0000000000..f3295e2652 --- /dev/null +++ b/tests/ref/fate/gapless-mp3-itunes @@ -0,0 +1,5 @@ +1e285368a8a176bc729712b06f724f21 *tests/data/fate/gapless-mp3-itunes.out-1 +cde648358dcd59911c4990ed59bf7d40 +4a39c54de9687ea8f66ea8204133c5c0 *tests/data/fate/gapless-mp3-itunes.out-2 +cde648358dcd59911c4990ed59bf7d40 +c8cd71407ac5bbfadf5ccf7b5a64430c *tests/data/fate/gapless-mp3-itunes.out-3