[FFmpeg-devel] [PATCH] Properly store sampling rate for FLAC in mp4

Jean-Yves Avenard jyavenard at gmail.com
Fri Aug 25 14:25:23 EEST 2017


>From 9baa7166fa96ed6beac9146c7e3b4dcf425a67d0 Mon Sep 17 00:00:00 2001
From: Jean-Yves Avenard <jyavenard at mozilla.com>
Date: Fri, 25 Aug 2017 13:11:28 +0200
Subject: [PATCH] Properly store sampling rate for FLAC in mp4

Fixes ticket #6609

Signed-off-by: Jean-Yves Avenard <jyavenard at mozilla.com>
---
 libavformat/movenc.c | 28 +++++++++++++++++++++++++---
 1 file changed, 25 insertions(+), 3 deletions(-)

diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index 10b959ad02..aa4a9c962a 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -1028,9 +1028,31 @@ static int mov_write_audio_tag(AVFormatContext
*s, AVIOContext *pb, MOVMuxContex
         avio_wb16(pb, 0); /* packet size (= 0) */
         if (track->par->codec_id == AV_CODEC_ID_OPUS)
             avio_wb16(pb, 48000);
-        else
-            avio_wb16(pb, track->par->sample_rate <= UINT16_MAX ?
-                          track->par->sample_rate : 0);
+        else {
+            uint32_t rate;
+            if (track->par->codec_id == AV_CODEC_ID_FLAC) {
+                /* When the bitstream's native sample rate is greater
+                 than the maximum expressible value of 65535 Hz,
+                 the samplerate field shall hold the greatest
+                 expressible regular division of that rate. I.e.
+                 the samplerate field shall hold 48000.0 for
+                 native sample rates of 96 and 192 kHz. In the
+                 case of unusual sample rates which do not have
+                 an expressible regular division, the maximum value
+                 of 65535.0 Hz should be used. */
+                rate = track->par->sample_rate;
+                while (rate > UINT16_MAX && (rate & 1) == 0) {
+                    rate = rate >> 1;
+                }
+                if (rate > UINT16_MAX) {
+                    rate = UINT16_MAX;
+                }
+            } else {
+                rate = track->par->sample_rate <= UINT16_MAX ?
+                       track->par->sample_rate : 0;
+            }
+            avio_wb16(pb, rate);
+        }
         avio_wb16(pb, 0); /* Reserved */
     }

-- 
2.11.0 (Apple Git-81)


More information about the ffmpeg-devel mailing list