[FFmpeg-devel] add HEVC codec into flv format
Steven Liu
lingjiujianke at gmail.com
Wed Jun 4 15:56:21 CEST 2014
Hi guys,
Let’s support HEVC into fly format.
Some people and some scene need to support hevc codec in flv format.
Perhaps this patch would useful..
Signed-off-by: Steven Liu <lingjiujianke at gmail.com>
---
libavformat/flv.h | 1 +
libavformat/flvdec.c | 10 ++++++++--
libavformat/flvenc.c | 18 ++++++++++++------
3 files changed, 21 insertions(+), 8 deletions(-)
diff --git a/libavformat/flv.h b/libavformat/flv.h
index db9468f..7e4c11d 100644
--- a/libavformat/flv.h
+++ b/libavformat/flv.h
@@ -109,6 +109,7 @@ enum {
FLV_CODECID_H264 = 7,
FLV_CODECID_REALH263= 8,
FLV_CODECID_MPEG4 = 9,
+ FLV_CODECID_HEVC = 10,
};
enum {
diff --git a/libavformat/flvdec.c b/libavformat/flvdec.c
index e113cc3..93592e2 100644
--- a/libavformat/flvdec.c
+++ b/libavformat/flvdec.c
@@ -217,6 +217,8 @@ static int flv_same_video_codec(AVCodecContext *vcodec, int flags)
return vcodec->codec_id == AV_CODEC_ID_VP6A;
case FLV_CODECID_H264:
return vcodec->codec_id == AV_CODEC_ID_H264;
+ case FLV_CODECID_HEVC:
+ return vcodec->codec_id == AV_CODEC_ID_HEVC;
default:
return vcodec->codec_tag == flv_codecid;
}
@@ -261,6 +263,9 @@ static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream,
case FLV_CODECID_MPEG4:
vcodec->codec_id = AV_CODEC_ID_MPEG4;
return 3;
+ case FLV_CODECID_HEVC:
+ vcodec->codec_id = AV_CODEC_ID_HEVC;
+ return 3;
default:
avpriv_request_sample(s, "Video codec (%x)", flv_codecid);
vcodec->codec_tag = flv_codecid;
@@ -919,10 +924,11 @@ retry_duration:
if (st->codec->codec_id == AV_CODEC_ID_AAC ||
st->codec->codec_id == AV_CODEC_ID_H264 ||
- st->codec->codec_id == AV_CODEC_ID_MPEG4) {
+ st->codec->codec_id == AV_CODEC_ID_MPEG4 ||
+ st->codec->codec_id == AV_CODEC_ID_HEVC) {
int type = avio_r8(s->pb);
size--;
- if (st->codec->codec_id == AV_CODEC_ID_H264 || st->codec->codec_id == AV_CODEC_ID_MPEG4) {
+ if (st->codec->codec_id == AV_CODEC_ID_H264 || st->codec->codec_id == AV_CODEC_ID_MPEG4 || st->codec->codec_id == AV_CODEC_ID_HEVC) {
// sign extension
int32_t cts = (avio_rb24(s->pb) + 0xff800000) ^ 0xff800000;
pts = dts + cts;
diff --git a/libavformat/flvenc.c b/libavformat/flvenc.c
index cebc25a..b51f75e 100644
--- a/libavformat/flvenc.c
+++ b/libavformat/flvenc.c
@@ -24,6 +24,7 @@
#include "libavutil/intfloat.h"
#include "libavutil/avassert.h"
#include "avc.h"
+#include "hevc.h"
#include "avformat.h"
#include "flv.h"
#include "internal.h"
@@ -40,6 +41,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 }
};
@@ -389,7 +391,7 @@ static int flv_write_header(AVFormatContext *s)
for (i = 0; i < s->nb_streams; i++) {
AVCodecContext *enc = s->streams[i]->codec;
- if (enc->codec_id == AV_CODEC_ID_AAC || enc->codec_id == AV_CODEC_ID_H264 || enc->codec_id == AV_CODEC_ID_MPEG4) {
+ if (enc->codec_id == AV_CODEC_ID_AAC || enc->codec_id == AV_CODEC_ID_H264 || enc->codec_id == AV_CODEC_ID_MPEG4 || enc->codec_id == AV_CODEC_ID_HEVC) {
int64_t pos;
avio_w8(pb, enc->codec_type == AVMEDIA_TYPE_VIDEO ?
FLV_TAG_TYPE_VIDEO : FLV_TAG_TYPE_AUDIO);
@@ -406,7 +408,11 @@ static int flv_write_header(AVFormatContext *s)
avio_w8(pb, enc->codec_tag | FLV_FRAME_KEY); // flags
avio_w8(pb, 0); // AVC sequence header
avio_wb24(pb, 0); // composition time
- ff_isom_write_avcc(pb, enc->extradata, enc->extradata_size);
+ if (enc->codec_id == AV_CODEC_ID_HEVC) {
+ ff_isom_write_hvcc(pb, enc->extradata, enc->extradata_size, 0);
+ } else {
+ ff_isom_write_avcc(pb, enc->extradata, enc->extradata_size);
+ }
}
data_size = avio_tell(pb) - pos;
avio_seek(pb, -data_size - 10, SEEK_CUR);
@@ -432,7 +438,7 @@ static int flv_write_trailer(AVFormatContext *s)
AVCodecContext *enc = s->streams[i]->codec;
FLVStreamContext *sc = s->streams[i]->priv_data;
if (enc->codec_type == AVMEDIA_TYPE_VIDEO &&
- (enc->codec_id == AV_CODEC_ID_H264 || enc->codec_id == AV_CODEC_ID_MPEG4))
+ (enc->codec_id == AV_CODEC_ID_H264 || enc->codec_id == AV_CODEC_ID_MPEG4 || enc->codec_id == AV_CODEC_ID_HEVC))
put_avc_eos_tag(pb, sc->last_ts);
}
@@ -466,7 +472,7 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
if (enc->codec_id == AV_CODEC_ID_VP6F || enc->codec_id == AV_CODEC_ID_VP6A ||
enc->codec_id == AV_CODEC_ID_VP6 || enc->codec_id == AV_CODEC_ID_AAC)
flags_size = 2;
- else if (enc->codec_id == AV_CODEC_ID_H264 || enc->codec_id == AV_CODEC_ID_MPEG4)
+ else if (enc->codec_id == AV_CODEC_ID_H264 || enc->codec_id == AV_CODEC_ID_MPEG4 || enc->codec_id == AV_CODEC_ID_HEVC)
flags_size = 5;
else
flags_size = 1;
@@ -499,7 +505,7 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
return AVERROR(EINVAL);
}
- if (enc->codec_id == AV_CODEC_ID_H264 || enc->codec_id == AV_CODEC_ID_MPEG4) {
+ if (enc->codec_id == AV_CODEC_ID_H264 || enc->codec_id == AV_CODEC_ID_MPEG4 || enc->codec_id == AV_CODEC_ID_HEVC) {
/* check if extradata looks like mp4 formated */
if (enc->extradata_size > 0 && *(uint8_t*)enc->extradata != 1)
if ((ret = ff_avc_parse_nal_units_buf(pkt->data, &data, &size)) < 0)
@@ -574,7 +580,7 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
(FFALIGN(enc->height, 16) - enc->height));
} else if (enc->codec_id == AV_CODEC_ID_AAC)
avio_w8(pb, 1); // AAC raw
- else if (enc->codec_id == AV_CODEC_ID_H264 || enc->codec_id == AV_CODEC_ID_MPEG4) {
+ else if (enc->codec_id == AV_CODEC_ID_H264 || enc->codec_id == AV_CODEC_ID_MPEG4 || enc->codec_id == AV_CODEC_ID_HEVC) {
avio_w8(pb, 1); // AVC NALU
avio_wb24(pb, pkt->pts - pkt->dts);
}
--
More information about the ffmpeg-devel
mailing list