[FFmpeg-devel] [PATCH] Add enhanced rtmp codec vp8 to flv format

Mondain mondain at gmail.com
Mon Aug 5 17:01:37 EEST 2024


Fixes: Reading and writing of VP8 media in FLV files, to match
existing enhanced codecs

Signed-off-by: Paul Gregoire <mondain at gmail.com>
---
 libavformat/flvdec.c | 12 +++++++++--
 libavformat/flvenc.c | 51 +++++++++++++++++++++++++++++---------------
 2 files changed, 44 insertions(+), 19 deletions(-)

diff --git a/libavformat/flvdec.c b/libavformat/flvdec.c
index 22a9b9e4a7..2383682a63 100644
--- a/libavformat/flvdec.c
+++ b/libavformat/flvdec.c
@@ -340,6 +340,8 @@ static int flv_same_video_codec(AVCodecParameters
*vpar, uint32_t flv_codecid)
         return vpar->codec_id == AV_CODEC_ID_AV1;
     case MKBETAG('v', 'p', '0', '9'):
         return vpar->codec_id == AV_CODEC_ID_VP9;
+    case MKBETAG('v', 'p', '0', '8'):
+        return vpar->codec_id == AV_CODEC_ID_VP8;
     case FLV_CODECID_H263:
         return vpar->codec_id == AV_CODEC_ID_FLV1;
     case FLV_CODECID_SCREEN:
@@ -378,6 +380,10 @@ static int flv_set_video_codec(AVFormatContext
*s, AVStream *vstream,
         par->codec_id = AV_CODEC_ID_VP9;
         vstreami->need_parsing = AVSTREAM_PARSE_HEADERS;
         break;
+    case MKBETAG('v', 'p', '0', '8'):
+        par->codec_id = AV_CODEC_ID_VP8;
+        vstreami->need_parsing = AVSTREAM_PARSE_HEADERS;
+        break;
     case FLV_CODECID_H263:
         par->codec_id = AV_CODEC_ID_FLV1;
         break;
@@ -1443,7 +1449,8 @@ retry_duration:
         st->codecpar->codec_id == AV_CODEC_ID_MPEG4 ||
         st->codecpar->codec_id == AV_CODEC_ID_HEVC ||
         st->codecpar->codec_id == AV_CODEC_ID_AV1 ||
-        st->codecpar->codec_id == AV_CODEC_ID_VP9) {
+        st->codecpar->codec_id == AV_CODEC_ID_VP9 ||
+        st->codecpar->codec_id == AV_CODEC_ID_VP8) {
         int type = 0;
         if (enhanced_flv && stream_type == FLV_STREAM_TYPE_VIDEO) {
             type = flags & 0x0F;
@@ -1481,7 +1488,8 @@ retry_duration:
         }
         if (type == 0 && (!st->codecpar->extradata ||
st->codecpar->codec_id == AV_CODEC_ID_AAC ||
             st->codecpar->codec_id == AV_CODEC_ID_H264 ||
st->codecpar->codec_id == AV_CODEC_ID_HEVC ||
-            st->codecpar->codec_id == AV_CODEC_ID_AV1 ||
st->codecpar->codec_id == AV_CODEC_ID_VP9)) {
+            st->codecpar->codec_id == AV_CODEC_ID_AV1 ||
st->codecpar->codec_id == AV_CODEC_ID_VP9 ||
+            st->codecpar->codec_id == AV_CODEC_ID_VP8)) {
             AVDictionaryEntry *t;

             if (st->codecpar->extradata) {
diff --git a/libavformat/flvenc.c b/libavformat/flvenc.c
index f34df61c0e..32678e3136 100644
--- a/libavformat/flvenc.c
+++ b/libavformat/flvenc.c
@@ -54,6 +54,7 @@ static const AVCodecTag flv_video_codec_ids[] = {
     { AV_CODEC_ID_H264,     FLV_CODECID_H264 },
     { AV_CODEC_ID_HEVC,     MKBETAG('h', 'v', 'c', '1') },
     { AV_CODEC_ID_AV1,      MKBETAG('a', 'v', '0', '1') },
+    { AV_CODEC_ID_VP8,      MKBETAG('v', 'p', '0', '8') },
     { AV_CODEC_ID_VP9,      MKBETAG('v', 'p', '0', '9') },
     { AV_CODEC_ID_NONE,     0 }
 };
@@ -497,7 +498,7 @@ static void
flv_write_metadata_packet(AVFormatContext *s, AVCodecParameters *par

     if (flv->metadata_pkt_written) return;
     if (par->codec_id == AV_CODEC_ID_HEVC || par->codec_id ==
AV_CODEC_ID_AV1 ||
-        par->codec_id == AV_CODEC_ID_VP9) {
+        par->codec_id == AV_CODEC_ID_VP8 || par->codec_id == AV_CODEC_ID_VP9) {
         int flags_size = 5;
         side_data = av_packet_side_data_get(par->coded_side_data,
par->nb_coded_side_data,
                                             AV_PKT_DATA_CONTENT_LIGHT_LEVEL);
@@ -519,12 +520,18 @@ static void
flv_write_metadata_packet(AVFormatContext *s, AVCodecParameters *par
         put_timestamp(pb, ts); //ts = pkt->dts, gen
         avio_wb24(pb, flv->reserved);

-        if (par->codec_id == AV_CODEC_ID_HEVC) {
-            avio_w8(pb, FLV_IS_EX_HEADER | PacketTypeMetadata|
FLV_FRAME_VIDEO_INFO_CMD); // ExVideoTagHeader mode with
PacketTypeMetadata
+        if (par->codec_id == AV_CODEC_ID_VP8) {
+            avio_w8(pb, FLV_IS_EX_HEADER | PacketTypeMetadata |
FLV_FRAME_VIDEO_INFO_CMD); // ExVideoTagHeader mode with
PacketTypeMetadata
+            avio_write(pb, "vp08", 4);
+        } else if (par->codec_id == AV_CODEC_ID_VP9) {
+            avio_w8(pb, FLV_IS_EX_HEADER | PacketTypeMetadata |
FLV_FRAME_VIDEO_INFO_CMD);
+            avio_write(pb, "vp09", 4);
+        } else if (par->codec_id == AV_CODEC_ID_HEVC) {
+            avio_w8(pb, FLV_IS_EX_HEADER | PacketTypeMetadata |
FLV_FRAME_VIDEO_INFO_CMD);
             avio_write(pb, "hvc1", 4);
-        } else if (par->codec_id == AV_CODEC_ID_AV1 || par->codec_id
== AV_CODEC_ID_VP9) {
-            avio_w8(pb, FLV_IS_EX_HEADER | PacketTypeMetadata|
FLV_FRAME_VIDEO_INFO_CMD);
-            avio_write(pb, par->codec_id == AV_CODEC_ID_AV1 ? "av01"
: "vp09", 4);
+        } else if (par->codec_id == AV_CODEC_ID_AV1) {
+            avio_w8(pb, FLV_IS_EX_HEADER | PacketTypeMetadata |
FLV_FRAME_VIDEO_INFO_CMD);
+            avio_write(pb, "av01", 4);
         }

         avio_w8(pb, AMF_DATA_TYPE_STRING);
@@ -639,7 +646,8 @@ static void
flv_write_codec_header(AVFormatContext* s, AVCodecParameters* par, i

     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_HEVC
-            || par->codec_id == AV_CODEC_ID_AV1 || par->codec_id ==
AV_CODEC_ID_VP9) {
+            || par->codec_id == AV_CODEC_ID_AV1 || par->codec_id ==
AV_CODEC_ID_VP9
+            || par->codec_id == AV_CODEC_ID_VP8) {
         int64_t pos;
         avio_w8(pb,
                 par->codec_type == AVMEDIA_TYPE_VIDEO ?
@@ -682,23 +690,28 @@ static void
flv_write_codec_header(AVFormatContext* s, AVCodecParameters* par, i
             }
             avio_write(pb, par->extradata, par->extradata_size);
         } else {
-            if (par->codec_id == AV_CODEC_ID_HEVC) {
+            if (par->codec_id == AV_CODEC_ID_VP8) {
                 avio_w8(pb, FLV_IS_EX_HEADER |
PacketTypeSequenceStart | FLV_FRAME_KEY); // ExVideoTagHeader mode
with PacketTypeSequenceStart
+                avio_write(pb, "vp08", 4);
+            } else if (par->codec_id == AV_CODEC_ID_VP9) {
+                avio_w8(pb, FLV_IS_EX_HEADER |
PacketTypeSequenceStart | FLV_FRAME_KEY);
+                avio_write(pb, "vp09", 4);
+            } else if (par->codec_id == AV_CODEC_ID_HEVC) {
+                avio_w8(pb, FLV_IS_EX_HEADER |
PacketTypeSequenceStart | FLV_FRAME_KEY);
                 avio_write(pb, "hvc1", 4);
-            } else if (par->codec_id == AV_CODEC_ID_AV1 ||
par->codec_id == AV_CODEC_ID_VP9) {
+            } else if (par->codec_id == AV_CODEC_ID_AV1) {
                 avio_w8(pb, FLV_IS_EX_HEADER |
PacketTypeSequenceStart | FLV_FRAME_KEY);
-                avio_write(pb, par->codec_id == AV_CODEC_ID_AV1 ?
"av01" : "vp09", 4);
+                avio_write(pb, "av01", 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 if (par->codec_id == AV_CODEC_ID_AV1)
                 ff_isom_write_av1c(pb, par->extradata, par->extradata_size, 1);
-            else if (par->codec_id == AV_CODEC_ID_VP9)
+            else if (par->codec_id == AV_CODEC_ID_VP9 ||
par->codec_id == AV_CODEC_ID_VP8)
                 ff_isom_write_vpcc(s, pb, par->extradata,
par->extradata_size, par);
             else
                 ff_isom_write_avcc(pb, par->extradata, par->extradata_size);
@@ -1001,7 +1014,7 @@ static int flv_write_packet(AVFormatContext *s,
AVPacket *pkt)
         flags_size = 2;
     else if (par->codec_id == AV_CODEC_ID_H264 || par->codec_id ==
AV_CODEC_ID_MPEG4 ||
              par->codec_id == AV_CODEC_ID_HEVC || par->codec_id ==
AV_CODEC_ID_AV1 ||
-             par->codec_id == AV_CODEC_ID_VP9)
+             par->codec_id == AV_CODEC_ID_VP9 || par->codec_id ==
AV_CODEC_ID_VP8)
         flags_size = 5;
     else
         flags_size = 1;
@@ -1011,7 +1024,8 @@ static int flv_write_packet(AVFormatContext *s,
AVPacket *pkt)

     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_HEVC
-            || par->codec_id == AV_CODEC_ID_AV1 || par->codec_id ==
AV_CODEC_ID_VP9) {
+            || par->codec_id == AV_CODEC_ID_AV1 || par->codec_id ==
AV_CODEC_ID_VP9
+            || par->codec_id == AV_CODEC_ID_VP8) {
         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))) {
@@ -1034,7 +1048,7 @@ static int flv_write_packet(AVFormatContext *s,
AVPacket *pkt)
     }
     if (par->codec_id == AV_CODEC_ID_H264 || par->codec_id ==
AV_CODEC_ID_MPEG4 ||
         par->codec_id == AV_CODEC_ID_HEVC ||  par->codec_id ==
AV_CODEC_ID_AV1 ||
-        par->codec_id == AV_CODEC_ID_VP9) {
+        par->codec_id == AV_CODEC_ID_VP9 || par->codec_id == AV_CODEC_ID_VP8) {
         if (pkt->pts == AV_NOPTS_VALUE) {
             av_log(s, AV_LOG_ERROR, "Packet is missing PTS\n");
             return AVERROR(EINVAL);
@@ -1150,9 +1164,12 @@ static int flv_write_packet(AVFormatContext *s,
AVPacket *pkt)
             avio_write(pb, "hvc1", 4);
             if (pkttype == PacketTypeCodedFrames)
                 avio_wb24(pb, pkt->pts - pkt->dts);
-        } else if (par->codec_id == AV_CODEC_ID_AV1 || par->codec_id
== AV_CODEC_ID_VP9) {
+        } else if (par->codec_id == AV_CODEC_ID_AV1) {
+            avio_w8(pb, FLV_IS_EX_HEADER | PacketTypeCodedFrames | frametype);
+            avio_write(pb, "av01", 4);
+        } else if (par->codec_id == AV_CODEC_ID_VP9 || par->codec_id
== AV_CODEC_ID_VP8) {
             avio_w8(pb, FLV_IS_EX_HEADER | PacketTypeCodedFrames | frametype);
-            avio_write(pb, par->codec_id == AV_CODEC_ID_AV1 ? "av01"
: "vp09", 4);
+            avio_write(pb, par->codec_id == AV_CODEC_ID_VP8 ? "vp08"
: "vp09", 4);
         } else {
             avio_w8(pb, flags);
         }
-- 
2.34.1


-- 
Paul Gregoire
about.me/mondain


More information about the ffmpeg-devel mailing list