Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/FFmpeg/FFmpeg.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoman Beranek <roman.beranek@prusa3d.cz>2021-07-10 04:10:06 +0300
committerLynne <dev@lynne.ee>2021-07-12 10:54:25 +0300
commited240db9e3122d4dccbd1bee7d12cd50a0edc807 (patch)
treef6d61825d5bf5b629aba26c0dc0315f6d594a990 /libavformat/cafenc.c
parent1aa9dcd091ed9cebf06c4c6a9c96ff80a54722f4 (diff)
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.
Diffstat (limited to 'libavformat/cafenc.c')
-rw-r--r--libavformat/cafenc.c17
1 files 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);