From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 96D1743FCA for ; Thu, 22 Sep 2022 23:15:41 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 220BD68BAB1; Fri, 23 Sep 2022 02:15:39 +0300 (EEST) 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-Google-Smtp-Source: AMsMyM6Si12yDcUm2RaREohHg71L0S2mmxE9iP8fbdySPGps06NPaSRsomwTxfVuA6KFQR7aapR1yw== 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 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: 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); -- 2.37.3 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".