[FFmpeg-cvslog] cafenc: fill in avg. packet size later if unknown

Roman Beranek git at videolan.org
Mon Jul 12 10:55:56 EEST 2021


ffmpeg | branch: master | Roman Beranek <roman.beranek at prusa3d.cz> | Sat Jul 10 03:10:06 2021 +0200| [ed240db9e3122d4dccbd1bee7d12cd50a0edc807] | committer: Lynne

cafenc: fill in avg. packet size later if unknown

Frame size of Opus stream was previously presumed here to be 960 samples
(20ms), however sizes of 120, 240, 480, 1920, and 2880 are also allowed.
It can also alter on a per-packet basis and even multiple frames may be
present in a single packet according to the specification, for the sake
of simplicity however, let us assume that this doesn't occur.

Because the mFramesPerPacket field, representing the number of samples
per packet in the ffmpeg terminilogy, is the key factor in calculating
packet durations and all that follows from that (index, bitrate, ...),
it is crucial to get right.

Therefore, if the packet size is not available ahead of time (as it is in
the case of Opus), calculate an average from the stream duration once we
know how many packets there are and update the filed in the header.

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=ed240db9e3122d4dccbd1bee7d12cd50a0edc807
---

 libavformat/cafenc.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/libavformat/cafenc.c b/libavformat/cafenc.c
index 422beaf93d..816e978945 100644
--- a/libavformat/cafenc.c
+++ b/libavformat/cafenc.c
@@ -26,6 +26,8 @@
 #include "libavutil/intfloat.h"
 #include "libavutil/dict.h"
 
+#define FRAME_SIZE_OFFSET 40
+
 typedef struct {
     int64_t data;
     uint8_t *pkt_sizes;
@@ -81,8 +83,6 @@ 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 960;
     case AV_CODEC_ID_MP2:
     case AV_CODEC_ID_MP3:
         return 1152;
@@ -240,19 +240,26 @@ static int caf_write_trailer(AVFormatContext *s)
 {
     CAFContext *caf = s->priv_data;
     AVIOContext *pb = s->pb;
-    AVCodecParameters *par = s->streams[0]->codecpar;
+    AVStream *st = s->streams[0];
+    AVCodecParameters *par = st->codecpar;
 
     if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
         int64_t file_size = avio_tell(pb);
 
         avio_seek(pb, caf->data, SEEK_SET);
         avio_wb64(pb, file_size - caf->data - 8);
-        avio_seek(pb, file_size, SEEK_SET);
         if (!par->block_align) {
+            int packet_size = samples_per_packet(par->codec_id, par->channels, par->block_align);
+            if (!packet_size) {
+                packet_size = st->duration / (caf->packets - 1);
+                avio_seek(pb, FRAME_SIZE_OFFSET, SEEK_SET);
+                avio_wb32(pb, packet_size);
+            }
+            avio_seek(pb, file_size, SEEK_SET);
             ffio_wfourcc(pb, "pakt");
             avio_wb64(pb, caf->size_entries_used + 24);
             avio_wb64(pb, caf->packets); ///< mNumberPackets
-            avio_wb64(pb, caf->packets * samples_per_packet(par->codec_id, par->channels, par->block_align)); ///< mNumberValidFrames
+            avio_wb64(pb, caf->packets * packet_size); ///< mNumberValidFrames
             avio_wb32(pb, 0); ///< mPrimingFrames
             avio_wb32(pb, 0); ///< mRemainderFrames
             avio_write(pb, caf->pkt_sizes, caf->size_entries_used);



More information about the ffmpeg-cvslog mailing list