diff mbox series

[FFmpeg-devel,1/6] avformat/movenc: add PCM in mp4 support

Message ID tencent_F4D559A71A38DA96514D00A012CDFFCFE107@qq.com
State New
Headers show
Series add PCM in mp4 support | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Zhao Zhili Feb. 24, 2023, 12:25 p.m. UTC
From: Zhao Zhili <zhilizhao@tencent.com>

It's defined by ISO/IEC 23003-5.

Fixes ticket #10185

Signed-off-by: Zhao Zhili <zhilizhao@tencent.com>
---
 libavformat/movenc.c | 64 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 63 insertions(+), 1 deletion(-)

Comments

Tomas Härdin Feb. 24, 2023, 9:41 a.m. UTC | #1
fre 2023-02-24 klockan 20:25 +0800 skrev Zhao Zhili:
> +static int is_mp4_pcm_codec(enum AVCodecID codec)
> +{
> +    static const enum AVCodecID codec_list[] = {
> +        AV_CODEC_ID_PCM_S16BE,
> +        AV_CODEC_ID_PCM_S16LE,
> +        AV_CODEC_ID_PCM_S24BE,
> +        AV_CODEC_ID_PCM_S24LE,
> +        AV_CODEC_ID_PCM_S32BE,
> +        AV_CODEC_ID_PCM_S32LE,
> +
> +        AV_CODEC_ID_PCM_F32BE,
> +        AV_CODEC_ID_PCM_F32LE,
> +        AV_CODEC_ID_PCM_F64BE,
> +        AV_CODEC_ID_PCM_F64LE,
> +    };
> +
> +    for (int i = 0; i < FF_ARRAY_ELEMS(codec_list); i++) {
> +        if (codec == codec_list[i])
> +            return 1;
> +    }

A switch() with multiple case statements in a row would be more
concise.

The rest looks OK

/Tomas
Zhao Zhili Feb. 24, 2023, 6:29 p.m. UTC | #2
On Fri, 2023-02-24 at 10:41 +0100, Tomas Härdin wrote:
> fre 2023-02-24 klockan 20:25 +0800 skrev Zhao Zhili:
> > +static int is_mp4_pcm_codec(enum AVCodecID codec)
> > +{
> > +    static const enum AVCodecID codec_list[] = {
> > +        AV_CODEC_ID_PCM_S16BE,
> > +        AV_CODEC_ID_PCM_S16LE,
> > +        AV_CODEC_ID_PCM_S24BE,
> > +        AV_CODEC_ID_PCM_S24LE,
> > +        AV_CODEC_ID_PCM_S32BE,
> > +        AV_CODEC_ID_PCM_S32LE,
> > +
> > +        AV_CODEC_ID_PCM_F32BE,
> > +        AV_CODEC_ID_PCM_F32LE,
> > +        AV_CODEC_ID_PCM_F64BE,
> > +        AV_CODEC_ID_PCM_F64LE,
> > +    };
> > +
> > +    for (int i = 0; i < FF_ARRAY_ELEMS(codec_list); i++) {
> > +        if (codec == codec_list[i])
> > +            return 1;
> > +    }
> 
> A switch() with multiple case statements in a row would be more
> concise.

Fixed in v2.

> 
> The rest looks OK
> 
> /Tomas
> 
> _______________________________________________
> 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".
diff mbox series

Patch

diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index c4fcb5f8b1..99b93c1d5b 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -1179,6 +1179,51 @@  static int mov_write_btrt_tag(AVIOContext *pb, MOVTrack *track)
     return update_size(pb, pos);
 }
 
+static int is_mp4_pcm_codec(enum AVCodecID codec)
+{
+    static const enum AVCodecID codec_list[] = {
+        AV_CODEC_ID_PCM_S16BE,
+        AV_CODEC_ID_PCM_S16LE,
+        AV_CODEC_ID_PCM_S24BE,
+        AV_CODEC_ID_PCM_S24LE,
+        AV_CODEC_ID_PCM_S32BE,
+        AV_CODEC_ID_PCM_S32LE,
+
+        AV_CODEC_ID_PCM_F32BE,
+        AV_CODEC_ID_PCM_F32LE,
+        AV_CODEC_ID_PCM_F64BE,
+        AV_CODEC_ID_PCM_F64LE,
+    };
+
+    for (int i = 0; i < FF_ARRAY_ELEMS(codec_list); i++) {
+        if (codec == codec_list[i])
+            return 1;
+    }
+
+    return 0;
+}
+
+static int mov_write_pcmc_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
+{
+    int64_t pos = avio_tell(pb);
+    int format_flags;
+
+    avio_wb32(pb, 0); /* size */
+    ffio_wfourcc(pb, "pcmC");
+    avio_wb32(pb, 0); /* version & flags */
+
+    /* 0x01: indicates little-endian format */
+    format_flags = (track->par->codec_id == AV_CODEC_ID_PCM_F32LE ||
+                    track->par->codec_id == AV_CODEC_ID_PCM_F64LE ||
+                    track->par->codec_id == AV_CODEC_ID_PCM_S16LE ||
+                    track->par->codec_id == AV_CODEC_ID_PCM_S24LE ||
+                    track->par->codec_id == AV_CODEC_ID_PCM_S32LE);
+    avio_w8(pb, format_flags);
+    avio_w8(pb, track->par->bits_per_raw_sample);
+
+    return update_size(pb, pos);
+}
+
 static int mov_write_audio_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
 {
     int64_t pos = avio_tell(pb);
@@ -1243,7 +1288,8 @@  static int mov_write_audio_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContex
         } else { /* reserved for mp4/3gp */
             avio_wb16(pb, track->par->ch_layout.nb_channels);
             if (track->par->codec_id == AV_CODEC_ID_FLAC ||
-                track->par->codec_id == AV_CODEC_ID_ALAC) {
+                track->par->codec_id == AV_CODEC_ID_ALAC ||
+                is_mp4_pcm_codec(track->par->codec_id)) {
                 avio_wb16(pb, track->par->bits_per_raw_sample);
             } else {
                 avio_wb16(pb, 16);
@@ -1307,6 +1353,8 @@  static int mov_write_audio_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContex
         ret = mov_write_dmlp_tag(s, pb, track);
     else if (track->vos_len > 0)
         ret = mov_write_glbl_tag(pb, track);
+    else if (track->mode == MODE_MP4 && is_mp4_pcm_codec(track->par->codec_id))
+        ret = mov_write_pcmc_tag(s, pb, track);
 
     if (ret < 0)
         return ret;
@@ -7744,6 +7792,20 @@  static const AVCodecTag codec_mp4_tags[] = {
     { AV_CODEC_ID_MPEGH_3D_AUDIO,  MKTAG('m', 'h', 'm', '1') },
     { AV_CODEC_ID_TTML,            MOV_MP4_TTML_TAG          },
     { AV_CODEC_ID_TTML,            MOV_ISMV_TTML_TAG         },
+
+    /* ISO/IEC 23003-5 integer formats */
+    { AV_CODEC_ID_PCM_S16BE,       MKTAG('i', 'p', 'c', 'm') },
+    { AV_CODEC_ID_PCM_S16LE,       MKTAG('i', 'p', 'c', 'm') },
+    { AV_CODEC_ID_PCM_S24BE,       MKTAG('i', 'p', 'c', 'm') },
+    { AV_CODEC_ID_PCM_S24LE,       MKTAG('i', 'p', 'c', 'm') },
+    { AV_CODEC_ID_PCM_S32BE,       MKTAG('i', 'p', 'c', 'm') },
+    { AV_CODEC_ID_PCM_S32LE,       MKTAG('i', 'p', 'c', 'm') },
+    /* ISO/IEC 23003-5 floating-point formats */
+    { AV_CODEC_ID_PCM_F32BE,       MKTAG('f', 'p', 'c', 'm') },
+    { AV_CODEC_ID_PCM_F32LE,       MKTAG('f', 'p', 'c', 'm') },
+    { AV_CODEC_ID_PCM_F64BE,       MKTAG('f', 'p', 'c', 'm') },
+    { AV_CODEC_ID_PCM_F64LE,       MKTAG('f', 'p', 'c', 'm') },
+
     { AV_CODEC_ID_NONE,               0 },
 };
 #if CONFIG_MP4_MUXER || CONFIG_PSP_MUXER