From patchwork Thu Sep 22 23:14:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 38189 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3b1c:b0:96:9ee8:5cfd with SMTP id c28csp69535pzh; Thu, 22 Sep 2022 16:15:48 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6SBw+yYjiMIw4FOFBO8s1jAZ0fSjlqXNGeFXU/oQFiXHnayeZCFUmKoVqI8/xAAs5nCd4t X-Received: by 2002:a17:907:724e:b0:77a:63:4a24 with SMTP id ds14-20020a170907724e00b0077a00634a24mr4735025ejc.35.1663888548304; Thu, 22 Sep 2022 16:15:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663888548; cv=none; d=google.com; s=arc-20160816; b=B2EgAmd3YaRmryYF3l3ugEzpYtPqCUhCMWZtTieB0LM8AJEkydkwpq8QSxt7wax8z5 Dj8iUI2mcbgsGNHnkN5YE5buRKpL8LY2MSq6aXeKlPsypePifuerhLIO0D7LFi2Xb3Wl ZeyxtdZx4k6n3a59x99cjlRh2u6tppiJ2D5czs+aM0jcAyLrg5noocVC5eMydq6WufOk CoV8io4Sat6nr1d+70OnlRRs+Ps/XhuMePD1y5zJsns7hoa/NX0M48xrTD+9kHvSkvS2 2Hbec+TlogMtycGFuU4LD8wY3YGCXOnEt8pIPEY0PX02LzJz7ixbRBnI+giCr/wBLFyg qTSg== 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:mime-version:references:in-reply-to:message-id :date:to:from:dkim-signature:delivered-to; bh=UC8uOfbVJTtUZXkJnbc2VSkZDz4FmQjq4B0f1CPpI5Q=; b=tYP295G21Tyn9GPrNyf4D9kMRuUzBs2Tfe7H1ufhIZxHoixfc9mAdGqHqP+Q7twjHl ZcBAdyHQTDvKhlM7sKnvhr0Gho02qzjRqT39XkgiSPTb5KtM0owKRO3W8rpNv7sr2RXJ oQ0uV7P4msiBVGkDGQB9PrIR/SkisVHccb5Uc+WVHC0SDrhkLbYzS5Tr1zZCCPBQGfaA +Z6Sq1wwGBR5ygbr4hR+aySu7UQeFjHGSXzWJe1tAIgxTqknB4fTdROMqtEZom2Zb8Fx sakPGFsFEv4pChLEATkgbgkKgMDcjQIJcadr5x0FECtaDpfE4qd5hyqWEPgl558BcgxK GcCA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=kVGmlQ52; 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 tz14-20020a170907c78e00b00730936657d1si5441101ejc.552.2022.09.22.16.15.43; Thu, 22 Sep 2022 16:15:48 -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=@gmail.com header.s=20210112 header.b=kVGmlQ52; 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 632BA68BADF; Fri, 23 Sep 2022 02:15:39 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-oa1-f47.google.com (mail-oa1-f47.google.com [209.85.160.47]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 97B5568B74D for ; Fri, 23 Sep 2022 02:15:32 +0300 (EEST) Received: by mail-oa1-f47.google.com with SMTP id 586e51a60fabf-1274ec87ad5so16179374fac.0 for ; Thu, 22 Sep 2022 16:15:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date; bh=esEuPtidliWLMqt18ca86XY8pgQLz5+FNxvE3v97uBw=; b=kVGmlQ52k8v2sEvIn5axqA0+fy2hIddwcN+PBoJtZAUbiwZacAJi8C1zWq9CEQjOGN xB9CG6qqYy6zvVv+Ea6BJf/9sfzjP8uJE8EDAlpzrLCWgNhF+EPoO6GKlh0qUnPh0v4A yEf+Ekhvc2gJeGbpK8zno8VI30Q2n6dWSoFQYjGja4F7RQ4TP74/G5hwxoyYSNJvssHu /ZqEAVdJ0gBqmTg5VaexNJzpLzRfq8uI6qTlVLJQEZsXLoJEvocUPo2Rn6SjMtJiPvzk 1SY9XEnEkXmxGTYJeZ59+H7m2+QN8oUtlmF0O5hAvBC5umTQJ5e9OJP1ONinxXdfJw7v mfKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date; bh=esEuPtidliWLMqt18ca86XY8pgQLz5+FNxvE3v97uBw=; b=hZvqOmEXOXyqSGFmHayy3HdPg/RM+brkvY2aNKRedUtfc+5Yql+PbxWxOWAHPji9RS mCWTOxTCldpUuyPveWRoExwCSvZEMxPj0RYxgBC3FCLy3Dzo18gF2kek3KC/+6AKzWAx BjqGZwC2qjDixBV4w+dnbQ+DSkHHapHf54Zd2HW/P/1pKu+mIfHXD5gl+u5fPAxcVqfO cDfMdHlpK8PjV+HsjxHWj0qc6a7JFvDlGGy+oPr7nTBCH+W/Vmk7eCoxoKhpHnLphLEt 07R6DJQ/SvTHsbr650GIVYvWorC6VtUrMaWDZBgAyJJlmJ6NHXFb0WkRMKhBL6X3Thl8 f87g== X-Gm-Message-State: ACrzQf0WJD0h5JsbdQQkkuEsvjIhfSMbnYxDWYGHPywxS4uK/7QT/ZEm bJM086oDQKSqgU5ZsP5ZjyFLKMePXiI= X-Received: by 2002:a05:6870:4291:b0:127:be13:2040 with SMTP id y17-20020a056870429100b00127be132040mr10204889oah.73.1663888530551; Thu, 22 Sep 2022 16:15:30 -0700 (PDT) Received: from localhost.localdomain ([191.97.187.183]) by smtp.gmail.com with ESMTPSA id n39-20020a056870242700b0011382da43aesm3956915oap.16.2022.09.22.16.15.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Sep 2022 16:15:29 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Thu, 22 Sep 2022 20:14:45 -0300 Message-Id: <20220922231445.6525-1-jamrial@gmail.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220921122159.1238-1-jamrial@gmail.com> References: <20220921122159.1238-1-jamrial@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2] avformat/cafenc: derive Opus frame size from the relevant stream parameters 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: YlKGn8ZBq1rn Use the stream duration as last resort, as an off-by-one result of the "st->duration / (caf->packets - 1)" calculation can break playback on some devices. Also, don't write the sample_rate value propagated by encoders like libopus. The sample rate of the audio fed to it is irrelevant for the container after being encoded. Fixes ticket #9930. Signed-off-by: James Almer --- libavformat/cafenc.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/libavformat/cafenc.c b/libavformat/cafenc.c index fedb430b17..b90811d46f 100644 --- a/libavformat/cafenc.c +++ b/libavformat/cafenc.c @@ -53,7 +53,11 @@ static uint32_t codec_flags(enum AVCodecID codec_id) { } } -static uint32_t samples_per_packet(enum AVCodecID codec_id, int channels, int block_align) { +static uint32_t samples_per_packet(const AVCodecParameters *par) { + enum AVCodecID codec_id = par->codec_id; + int channels = par->ch_layout.nb_channels, block_align = par->block_align; + int frame_size = par->frame_size, sample_rate = par->sample_rate; + switch (codec_id) { case AV_CODEC_ID_PCM_S8: case AV_CODEC_ID_PCM_S16LE: @@ -83,6 +87,8 @@ static uint32_t samples_per_packet(enum AVCodecID codec_id, int channels, int bl return 320; case AV_CODEC_ID_MP1: return 384; + case AV_CODEC_ID_OPUS: + return frame_size * 48000 / sample_rate; case AV_CODEC_ID_MP2: case AV_CODEC_ID_MP3: return 1152; @@ -110,7 +116,7 @@ static int caf_write_header(AVFormatContext *s) AVDictionaryEntry *t = NULL; unsigned int codec_tag = ff_codec_get_tag(ff_codec_caf_tags, par->codec_id); int64_t chunk_size = 0; - int frame_size = par->frame_size; + int frame_size = par->frame_size, sample_rate = par->sample_rate; if (s->nb_streams != 1) { av_log(s, AV_LOG_ERROR, "CAF files have exactly one stream\n"); @@ -139,7 +145,10 @@ static int caf_write_header(AVFormatContext *s) } if (par->codec_id != AV_CODEC_ID_MP3 || frame_size != 576) - frame_size = samples_per_packet(par->codec_id, par->ch_layout.nb_channels, par->block_align); + frame_size = samples_per_packet(par); + + if (par->codec_id == AV_CODEC_ID_OPUS) + sample_rate = 48000; ffio_wfourcc(pb, "caff"); //< mFileType avio_wb16(pb, 1); //< mFileVersion @@ -147,7 +156,7 @@ static int caf_write_header(AVFormatContext *s) ffio_wfourcc(pb, "desc"); //< Audio Description chunk avio_wb64(pb, 32); //< mChunkSize - avio_wb64(pb, av_double2int(par->sample_rate)); //< mSampleRate + avio_wb64(pb, av_double2int(sample_rate)); //< mSampleRate avio_wl32(pb, codec_tag); //< mFormatID avio_wb32(pb, codec_flags(par->codec_id)); //< mFormatFlags avio_wb32(pb, par->block_align); //< mBytesPerPacket @@ -248,7 +257,7 @@ static int caf_write_trailer(AVFormatContext *s) avio_seek(pb, caf->data, SEEK_SET); avio_wb64(pb, file_size - caf->data - 8); if (!par->block_align) { - int packet_size = samples_per_packet(par->codec_id, par->ch_layout.nb_channels, par->block_align); + int packet_size = samples_per_packet(par); if (!packet_size) { packet_size = st->duration / (caf->packets - 1); avio_seek(pb, FRAME_SIZE_OFFSET, SEEK_SET);