[FFmpeg-devel] [PATCH v6 1/4] avformat/flvenc: Add support for HEVC over flv in muxer

Steven Liu lq at chinaffmpeg.org
Wed Apr 12 10:30:57 EEST 2023


Implements HEVC according to Enhanced FLV spec found at
https://github.com/veovera/enhanced-rtmp

And it has beed supported by OBS, Simple Realtime Server, mpegts.js.
And the enhanced FLV documentation contributors include
Jean-Baptiste Kempf (FFmpeg, VideoLAN).
So this should be support by ffmpeg too.

Signed-off-by: Steven Liu <lq at chinaffmpeg.org>
---
 libavformat/flv.h    | 16 ++++++++++++
 libavformat/flvenc.c | 62 +++++++++++++++++++++++++++++++++-----------
 2 files changed, 63 insertions(+), 15 deletions(-)

diff --git a/libavformat/flv.h b/libavformat/flv.h
index 3571b90279..44d3b04ff9 100644
--- a/libavformat/flv.h
+++ b/libavformat/flv.h
@@ -35,6 +35,12 @@
 
 #define FLV_VIDEO_FRAMETYPE_OFFSET   4
 
+/* Extended VideoTagHeader
+ * defined in reference link:
+ * https://github.com/veovera/enhanced-rtmp/blob/main/enhanced-rtmp-v1.pdf
+ * */
+#define FLV_IS_EX_HEADER          0x80
+
 /* bitmasks to isolate specific values */
 #define FLV_AUDIO_CHANNEL_MASK    0x01
 #define FLV_AUDIO_SAMPLESIZE_MASK 0x02
@@ -110,6 +116,16 @@ enum {
     FLV_CODECID_H264    = 7,
     FLV_CODECID_REALH263= 8,
     FLV_CODECID_MPEG4   = 9,
+    FLV_CODECID_HEVC    = 10,
+};
+
+enum {
+    PacketTypeSequenceStart         = 0,
+    PacketTypeCodedFrames           = 1,
+    PacketTypeSequenceEnd           = 2,
+    PacketTypeCodedFramesX          = 3,
+    PacketTypeMetadata              = 4,
+    PacketTypeMPEG2TSSequenceStart  = 5,
 };
 
 enum {
diff --git a/libavformat/flvenc.c b/libavformat/flvenc.c
index fbf7eabaf8..620ad08ca1 100644
--- a/libavformat/flvenc.c
+++ b/libavformat/flvenc.c
@@ -28,6 +28,7 @@
 #include "libavcodec/mpeg4audio.h"
 #include "avio.h"
 #include "avc.h"
+#include "hevc.h"
 #include "avformat.h"
 #include "flv.h"
 #include "internal.h"
@@ -46,6 +47,7 @@ static const AVCodecTag flv_video_codec_ids[] = {
     { AV_CODEC_ID_VP6,      FLV_CODECID_VP6 },
     { AV_CODEC_ID_VP6A,     FLV_CODECID_VP6A },
     { AV_CODEC_ID_H264,     FLV_CODECID_H264 },
+    { AV_CODEC_ID_HEVC,     FLV_CODECID_HEVC },
     { AV_CODEC_ID_NONE,     0 }
 };
 
@@ -238,16 +240,24 @@ static void put_timestamp(AVIOContext *pb, int64_t ts) {
 static void put_eos_tag(AVIOContext *pb, unsigned ts, enum AVCodecID codec_id)
 {
     uint32_t tag = ff_codec_get_tag(flv_video_codec_ids, codec_id);
+    uint32_t flvtag_size = 16;      /* Size of FLV tag */
     /* ub[4] FrameType = 1, ub[4] CodecId */
     tag |= 1 << 4;
     avio_w8(pb, FLV_TAG_TYPE_VIDEO);
     avio_wb24(pb, 5);               /* Tag Data Size */
     put_timestamp(pb, ts);
     avio_wb24(pb, 0);               /* StreamId = 0 */
-    avio_w8(pb, tag);
-    avio_w8(pb, 2);                 /* AVC end of sequence */
-    avio_wb24(pb, 0);               /* Always 0 for AVC EOS. */
-    avio_wb32(pb, 16);              /* Size of FLV tag */
+    if (codec_id == AV_CODEC_ID_HEVC) {
+        avio_w8(pb, FLV_IS_EX_HEADER | PacketTypeSequenceEnd);
+        avio_write(pb, "hvc1", 4);
+        flvtag_size = 19;
+    } else {
+        avio_w8(pb, tag);
+        avio_w8(pb, 2);             /* AVC end of sequence */
+        flvtag_size = 16;
+    }
+    avio_wb24(pb, 0);               /* Always 0 for EOS. */
+    avio_wb32(pb, flvtag_size);     /* Size of FLV tag */
 }
 
 static void put_amf_double(AVIOContext *pb, double d)
@@ -492,7 +502,7 @@ static void flv_write_codec_header(AVFormatContext* s, AVCodecParameters* par, i
     FLVContext *flv = s->priv_data;
 
     if (par->codec_id == AV_CODEC_ID_AAC || par->codec_id == AV_CODEC_ID_H264
-            || par->codec_id == AV_CODEC_ID_MPEG4) {
+            || par->codec_id == AV_CODEC_ID_MPEG4 || par->codec_id == AV_CODEC_ID_HEVC) {
         int64_t pos;
         avio_w8(pb,
                 par->codec_type == AVMEDIA_TYPE_VIDEO ?
@@ -535,10 +545,19 @@ static void flv_write_codec_header(AVFormatContext* s, AVCodecParameters* par, i
             }
             avio_write(pb, par->extradata, par->extradata_size);
         } else {
-            avio_w8(pb, par->codec_tag | FLV_FRAME_KEY); // flags
-            avio_w8(pb, 0); // AVC sequence header
-            avio_wb24(pb, 0); // composition time
-            ff_isom_write_avcc(pb, par->extradata, par->extradata_size);
+            if (par->codec_id == AV_CODEC_ID_HEVC) {
+                avio_w8(pb, FLV_IS_EX_HEADER | PacketTypeSequenceStart); // ExVideoTagHeader mode with PacketTypeSequenceStart
+                avio_write(pb, "hvc1", 4);
+            } else {
+                avio_w8(pb, par->codec_tag | FLV_FRAME_KEY); // flags
+                avio_w8(pb, 0); // AVC sequence header
+                avio_wb24(pb, 0); // composition time
+            }
+
+            if (par->codec_id == AV_CODEC_ID_HEVC)
+                ff_isom_write_hvcc(pb, par->extradata, par->extradata_size, 0);
+            else
+                ff_isom_write_avcc(pb, par->extradata, par->extradata_size);
         }
         data_size = avio_tell(pb) - pos;
         avio_seek(pb, -data_size - 10, SEEK_CUR);
@@ -628,7 +647,8 @@ static int flv_init(struct AVFormatContext *s)
                 return unsupported_codec(s, "Video", par->codec_id);
 
             if (par->codec_id == AV_CODEC_ID_MPEG4 ||
-                par->codec_id == AV_CODEC_ID_H263) {
+                par->codec_id == AV_CODEC_ID_H263 ||
+                par->codec_id == AV_CODEC_ID_HEVC) {
                 int error = s->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL;
                 av_log(s, error ? AV_LOG_ERROR : AV_LOG_WARNING,
                        "Codec %s is not supported in the official FLV specification,\n", avcodec_get_name(par->codec_id));
@@ -785,7 +805,8 @@ end:
             AVCodecParameters *par = s->streams[i]->codecpar;
             FLVStreamContext *sc = s->streams[i]->priv_data;
             if (par->codec_type == AVMEDIA_TYPE_VIDEO &&
-                    (par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_MPEG4))
+                    (par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_MPEG4 ||
+                     par->codec_id == AV_CODEC_ID_HEVC))
                 put_eos_tag(pb, sc->last_ts, par->codec_id);
         }
     }
@@ -836,13 +857,13 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
     if (par->codec_id == AV_CODEC_ID_VP6F || par->codec_id == AV_CODEC_ID_VP6A ||
         par->codec_id == AV_CODEC_ID_VP6  || par->codec_id == AV_CODEC_ID_AAC)
         flags_size = 2;
-    else if (par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_MPEG4)
+    else if (par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_MPEG4 || par->codec_id == AV_CODEC_ID_HEVC)
         flags_size = 5;
     else
         flags_size = 1;
 
     if (par->codec_id == AV_CODEC_ID_AAC || par->codec_id == AV_CODEC_ID_H264
-            || par->codec_id == AV_CODEC_ID_MPEG4) {
+            || par->codec_id == AV_CODEC_ID_MPEG4 || par->codec_id == AV_CODEC_ID_HEVC) {
         size_t side_size;
         uint8_t *side = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, &side_size);
         if (side && side_size > 0 && (side_size != par->extradata_size || memcmp(side, par->extradata, side_size))) {
@@ -862,7 +883,7 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
                "Packets are not in the proper order with respect to DTS\n");
         return AVERROR(EINVAL);
     }
-    if (par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_MPEG4) {
+    if (par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_MPEG4 || par->codec_id == AV_CODEC_ID_HEVC) {
         if (pkt->pts == AV_NOPTS_VALUE) {
             av_log(s, AV_LOG_ERROR, "Packet is missing PTS\n");
             return AVERROR(EINVAL);
@@ -907,6 +928,10 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
         if (par->extradata_size > 0 && *(uint8_t*)par->extradata != 1)
             if ((ret = ff_avc_parse_nal_units_buf(pkt->data, &data, &size)) < 0)
                 return ret;
+    } else if (par->codec_id == AV_CODEC_ID_HEVC) {
+        if (par->extradata_size > 0 && *(uint8_t*)par->extradata != 1)
+            if ((ret = ff_hevc_annexb2mp4_buf(pkt->data, &data, &size, 0, NULL)) < 0)
+                return ret;
     } else if (par->codec_id == AV_CODEC_ID_AAC && pkt->size > 2 &&
                (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
         if (!s->streams[pkt->stream_index]->nb_frames) {
@@ -968,7 +993,12 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
         avio_wb32(pb, data_size + 11);
     } else {
         av_assert1(flags>=0);
-        avio_w8(pb,flags);
+        if (par->codec_id == AV_CODEC_ID_HEVC) {
+            avio_w8(pb, FLV_IS_EX_HEADER | PacketTypeCodedFrames); // ExVideoTagHeader mode with PacketTypeCodedFrames
+            avio_write(pb, "hvc1", 4);
+        } else {
+            avio_w8(pb, flags);
+        }
         if (par->codec_id == AV_CODEC_ID_VP6)
             avio_w8(pb,0);
         if (par->codec_id == AV_CODEC_ID_VP6F || par->codec_id == AV_CODEC_ID_VP6A) {
@@ -982,6 +1012,8 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
         else if (par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_MPEG4) {
             avio_w8(pb, 1); // AVC NALU
             avio_wb24(pb, pkt->pts - pkt->dts);
+        } else if (par->codec_id == AV_CODEC_ID_HEVC) {
+            avio_wb24(pb, pkt->pts - pkt->dts);
         }
 
         avio_write(pb, data ? data : pkt->data, size);
-- 
2.40.0



More information about the ffmpeg-devel mailing list