History

avformat/flvenc: support mux hevc in enhanced flv

Steven Liu committed 3 years ago

Commit aa836541 parent 3d79afb3 files · +47 −11Browse files

Showing the changes to libavformat (3 of 3 files). Show all

Unified · Split

3 files
  1. libavformat/Makefile +1 −1
  2. libavformat/flv.h +15 −0
  3. libavformat/flvenc.c +31 −10
libavformat/Makefile+1−1
@@ -216,7 +216,7 @@ OBJS-$(CONFIG_FLAC_MUXER) += flacenc.o flacenc_header.o \ · 215 unmodified lines
216216 OBJS-$(CONFIG_FLIC_DEMUXER) += flic.o
217217 OBJS-$(CONFIG_FLV_DEMUXER) += flvdec.o
218218 OBJS-$(CONFIG_LIVE_FLV_DEMUXER) += flvdec.o
219−OBJS-$(CONFIG_FLV_MUXER) += flvenc.o avc.o
219+OBJS-$(CONFIG_FLV_MUXER) += flvenc.o avc.o hevc.o
220220 OBJS-$(CONFIG_FOURXM_DEMUXER) += 4xm.o
221221 OBJS-$(CONFIG_FRAMECRC_MUXER) += framecrcenc.o framehash.o
222222 OBJS-$(CONFIG_FRAMEHASH_MUXER) += hashenc.o framehash.o
libavformat/flv.h+15−0
@@ -35,6 +35,12 @@ · 34 unmodified lines
3535
3636 #define FLV_VIDEO_FRAMETYPE_OFFSET 4
3737
38+/* Extended VideoTagHeader
39+ * defined in reference link:
40+ * https://github.com/veovera/enhanced-rtmp/blob/main/enhanced-rtmp-v1.pdf
41+ * */
42+#define FLV_IS_EX_HEADER 0x80
43+
3844 /* bitmasks to isolate specific values */
3945 #define FLV_AUDIO_CHANNEL_MASK 0x01
4046 #define FLV_AUDIO_SAMPLESIZE_MASK 0x02
@@ -112,6 +118,15 @@ enum { · 71 unmodified lines
112118 FLV_CODECID_MPEG4 = 9,
113119 };
114120
121+enum {
122+ PacketTypeSequenceStart = 0,
123+ PacketTypeCodedFrames = 1,
124+ PacketTypeSequenceEnd = 2,
125+ PacketTypeCodedFramesX = 3,
126+ PacketTypeMetadata = 4,
127+ PacketTypeMPEG2TSSequenceStart = 5,
128+};
129+
115130 enum {
116131 FLV_FRAME_KEY = 1 << FLV_VIDEO_FRAMETYPE_OFFSET, ///< key frame (for AVC, a seekable frame)
117132 FLV_FRAME_INTER = 2 << FLV_VIDEO_FRAMETYPE_OFFSET, ///< inter frame (for AVC, a non-seekable frame)
libavformat/flvenc.c+31−10
@@ -28,6 +28,7 @@ · 27 unmodified lines
2828 #include "libavcodec/mpeg4audio.h"
2929 #include "avio.h"
3030 #include "avc.h"
31+#include "hevc.h"
3132 #include "avformat.h"
3233 #include "flv.h"
3334 #include "internal.h"
@@ -46,6 +47,7 @@ static const AVCodecTag flv_video_codec_ids[] = { · 12 unmodified lines
4647 { AV_CODEC_ID_VP6, FLV_CODECID_VP6 },
4748 { AV_CODEC_ID_VP6A, FLV_CODECID_VP6A },
4849 { AV_CODEC_ID_H264, FLV_CODECID_H264 },
50+ { AV_CODEC_ID_HEVC, MKBETAG('h', 'v', 'c', '1') },
4951 { AV_CODEC_ID_NONE, 0 }
5052 };
5153
@@ -489,7 +491,7 @@ static void flv_write_codec_header(AVFormatContext* s, AVCodecParameters* par, i · 437 unmodified lines
489491 FLVContext *flv = s->priv_data;
490492
491493 if (par->codec_id == AV_CODEC_ID_AAC || par->codec_id == AV_CODEC_ID_H264
492− || par->codec_id == AV_CODEC_ID_MPEG4) {
494+ || par->codec_id == AV_CODEC_ID_MPEG4 || par->codec_id == AV_CODEC_ID_HEVC) {
493495 int64_t pos;
494496 avio_w8(pb,
495497 par->codec_type == AVMEDIA_TYPE_VIDEO ?
@@ -532,10 +534,19 @@ static void flv_write_codec_header(AVFormatContext* s, AVCodecParameters* par, i · 36 unmodified lines
532534 }
533535 avio_write(pb, par->extradata, par->extradata_size);
534536 } else {
535− avio_w8(pb, par->codec_tag | FLV_FRAME_KEY); // flags
536− avio_w8(pb, 0); // AVC sequence header
537− avio_wb24(pb, 0); // composition time
538− ff_isom_write_avcc(pb, par->extradata, par->extradata_size);
537+ if (par->codec_id == AV_CODEC_ID_HEVC) {
538+ avio_w8(pb, FLV_IS_EX_HEADER | PacketTypeSequenceStart | FLV_FRAME_KEY); // ExVideoTagHeader mode with PacketTypeSequenceStart
539+ avio_write(pb, "hvc1", 4);
540+ } else {
541+ avio_w8(pb, par->codec_tag | FLV_FRAME_KEY); // flags
542+ avio_w8(pb, 0); // AVC sequence header
543+ avio_wb24(pb, 0); // composition time
544+ }
545+
546+ if (par->codec_id == AV_CODEC_ID_HEVC)
547+ ff_isom_write_hvcc(pb, par->extradata, par->extradata_size, 0);
548+ else
549+ ff_isom_write_avcc(pb, par->extradata, par->extradata_size);
539550 }
540551 data_size = avio_tell(pb) - pos;
541552 avio_seek(pb, -data_size - 10, SEEK_CUR);
@@ -821,6 +832,7 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt) · 279 unmodified lines
821832 unsigned ts;
822833 int size = pkt->size;
823834 uint8_t *data = NULL;
835+ uint8_t frametype = pkt->flags & AV_PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;
824836 int flags = -1, flags_size, ret = 0;
825837 int64_t cur_offset = avio_tell(pb);
826838
@@ -832,13 +844,13 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt) · 5 unmodified lines
832844 if (par->codec_id == AV_CODEC_ID_VP6F || par->codec_id == AV_CODEC_ID_VP6A ||
833845 par->codec_id == AV_CODEC_ID_VP6 || par->codec_id == AV_CODEC_ID_AAC)
834846 flags_size = 2;
835− else if (par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_MPEG4)
847+ else if (par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_MPEG4 || par->codec_id == AV_CODEC_ID_HEVC)
836848 flags_size = 5;
837849 else
838850 flags_size = 1;
839851
840852 if (par->codec_id == AV_CODEC_ID_AAC || par->codec_id == AV_CODEC_ID_H264
841− || par->codec_id == AV_CODEC_ID_MPEG4) {
853+ || par->codec_id == AV_CODEC_ID_MPEG4 || par->codec_id == AV_CODEC_ID_HEVC) {
842854 size_t side_size;
843855 uint8_t *side = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, &side_size);
844856 if (side && side_size > 0 && (side_size != par->extradata_size || memcmp(side, par->extradata, side_size))) {
@@ -858,7 +870,7 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt) · 13 unmodified lines
858870 "Packets are not in the proper order with respect to DTS\n");
859871 return AVERROR(EINVAL);
860872 }
861− if (par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_MPEG4) {
873+ if (par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_MPEG4 || par->codec_id == AV_CODEC_ID_HEVC) {
862874 if (pkt->pts == AV_NOPTS_VALUE) {
863875 av_log(s, AV_LOG_ERROR, "Packet is missing PTS\n");
864876 return AVERROR(EINVAL);
@@ -881,7 +893,7 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt) · 16 unmodified lines
881893
882894 flags = ff_codec_get_tag(flv_video_codec_ids, par->codec_id);
883895
884− flags |= pkt->flags & AV_PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;
896+ flags |= frametype;
885897 break;
886898 case AVMEDIA_TYPE_AUDIO:
887899 flags = get_audio_flags(s, par);
@@ -903,6 +915,10 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt) · 15 unmodified lines
903915 if (par->extradata_size > 0 && *(uint8_t*)par->extradata != 1)
904916 if ((ret = ff_avc_parse_nal_units_buf(pkt->data, &data, &size)) < 0)
905917 return ret;
918+ } else if (par->codec_id == AV_CODEC_ID_HEVC) {
919+ if (par->extradata_size > 0 && *(uint8_t*)par->extradata != 1)
920+ if ((ret = ff_hevc_annexb2mp4_buf(pkt->data, &data, &size, 0, NULL)) < 0)
921+ return ret;
906922 } else if (par->codec_id == AV_CODEC_ID_AAC && pkt->size > 2 &&
907923 (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
908924 if (!s->streams[pkt->stream_index]->nb_frames) {
@@ -964,7 +980,12 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt) · 55 unmodified lines
964980 avio_wb32(pb, data_size + 11);
965981 } else {
966982 av_assert1(flags>=0);
967− avio_w8(pb,flags);
983+ if (par->codec_id == AV_CODEC_ID_HEVC) {
984+ avio_w8(pb, FLV_IS_EX_HEADER | PacketTypeCodedFramesX | frametype); // ExVideoTagHeader mode with PacketTypeCodedFramesX
985+ avio_write(pb, "hvc1", 4);
986+ } else {
987+ avio_w8(pb, flags);
988+ }
968989 if (par->codec_id == AV_CODEC_ID_VP6)
969990 avio_w8(pb,0);
970991 if (par->codec_id == AV_CODEC_ID_VP6F || par->codec_id == AV_CODEC_ID_VP6A) {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t