[FFmpeg-devel] [PATCH v2] libavformat/movenc: add support for HEVC in .3gp

hisunzhenliang at outlook.com hisunzhenliang at outlook.com
Fri Dec 11 05:38:12 EET 2020


From: SunZhenliang <hisunzhenliang at outlook.com>

Just add HEVC's tag in 3gp tag list and it works to support HEVC in
3gp files.

Signed-off-by: SunZhenliang <hisunzhenliang at outlook.com>
---
 libavformat/movenc.c | 33 +++++++++++++++++++++++++--------
 1 file changed, 25 insertions(+), 8 deletions(-)

diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index 18fa3f9b5e..c100dd0269 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -4923,7 +4923,7 @@ static int mov_write_mdat_tag(AVIOContext *pb, MOVMuxContext *mov)
 }
 
 static void mov_write_ftyp_tag_internal(AVIOContext *pb, AVFormatContext *s,
-                                        int has_h264, int has_video, int write_minor)
+                                        int has_h2645, int has_video, int write_minor)
 {
     MOVMuxContext *mov = s->priv_data;
     int minor = 0x200;
@@ -4931,11 +4931,11 @@ static void mov_write_ftyp_tag_internal(AVIOContext *pb, AVFormatContext *s,
     if (mov->major_brand && strlen(mov->major_brand) >= 4)
         ffio_wfourcc(pb, mov->major_brand);
     else if (mov->mode == MODE_3GP) {
-        ffio_wfourcc(pb, has_h264 ? "3gp6"  : "3gp4");
-        minor =     has_h264 ?   0x100 :   0x200;
+        ffio_wfourcc(pb, has_h2645 ? "3gp6"  : "3gp4");
+        minor =     has_h2645 ?   0x100 :   0x200;
     } else if (mov->mode & MODE_3G2) {
-        ffio_wfourcc(pb, has_h264 ? "3g2b"  : "3g2a");
-        minor =     has_h264 ? 0x20000 : 0x10000;
+        ffio_wfourcc(pb, has_h2645 ? "3g2b"  : "3g2a");
+        minor =     has_h2645 ? 0x20000 : 0x10000;
     } else if (mov->mode == MODE_PSP)
         ffio_wfourcc(pb, "MSNV");
     else if (mov->mode == MODE_MP4 && mov->flags & FF_MOV_FLAG_FRAGMENT &&
@@ -4964,7 +4964,9 @@ static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
 {
     MOVMuxContext *mov = s->priv_data;
     int64_t pos = avio_tell(pb);
-    int has_h264 = 0, has_av1 = 0, has_video = 0;
+    int has_h264 = 0, has_h265 = 0, has_av1 = 0, has_video = 0;
+    int h265_codec_tag_hev1 = 0;
+    int h265_codec_tag_hvc1 = 0;
     int i;
 
     for (i = 0; i < s->nb_streams; i++) {
@@ -4975,6 +4977,13 @@ static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
             has_video = 1;
         if (st->codecpar->codec_id == AV_CODEC_ID_H264)
             has_h264 = 1;
+        if (st->codecpar->codec_id == AV_CODEC_ID_H265){
+            has_h265 = 1;
+            if (st->codecpar->codec_tag == MKTAG('h','e','v','1'))
+                h265_codec_tag_hev1 = st->codecpar->codec_tag;
+            else
+                h265_codec_tag_hvc1 = st->codecpar->codec_tag;
+        }
         if (st->codecpar->codec_id == AV_CODEC_ID_AV1)
             has_av1 = 1;
     }
@@ -4983,9 +4992,9 @@ static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
     ffio_wfourcc(pb, "ftyp");
 
     // Write major brand
-    mov_write_ftyp_tag_internal(pb, s, has_h264, has_video, 1);
+    mov_write_ftyp_tag_internal(pb, s, has_h264 || has_h265 , has_video, 1);
     // Write the major brand as the first compatible brand as well
-    mov_write_ftyp_tag_internal(pb, s, has_h264, has_video, 0);
+    mov_write_ftyp_tag_internal(pb, s, has_h264 || has_h265, has_video, 0);
 
     // Write compatible brands, ensuring that we don't write the major brand as a
     // compatible brand a second time.
@@ -5018,6 +5027,12 @@ static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
             ffio_wfourcc(pb, "iso2");
             if (has_h264)
                 ffio_wfourcc(pb, "avc1");
+            if (has_h265){
+                if (h265_codec_tag_hev1)
+                    ffio_wfourcc(pb, "hev1");
+                if (h265_codec_tag_hvc1)
+                    ffio_wfourcc(pb, "hvc1");
+            }
         }
     }
 
@@ -7103,6 +7118,8 @@ static int mov_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
 static const AVCodecTag codec_3gp_tags[] = {
     { AV_CODEC_ID_H263,     MKTAG('s','2','6','3') },
     { AV_CODEC_ID_H264,     MKTAG('a','v','c','1') },
+    { AV_CODEC_ID_HEVC,     MKTAG('h','e','v','1') },
+    { AV_CODEC_ID_HEVC,     MKTAG('h','v','c','1') },
     { AV_CODEC_ID_MPEG4,    MKTAG('m','p','4','v') },
     { AV_CODEC_ID_AAC,      MKTAG('m','p','4','a') },
     { AV_CODEC_ID_AMR_NB,   MKTAG('s','a','m','r') },
-- 
2.20.1



More information about the ffmpeg-devel mailing list