FFmpeg修改源码支持H265

一. 安装FFmpeg

如果已经安装,建议先卸载.

brew uninstall ffmpeg

然后用以下命令安装.

brew install ffmpeg --with-fdk-aac --with-tools --with-ffplay --with-freetype --with-libass --with-libvorbis --with-opus --with-libvpx --with-x265

此时我们用 ffplay 一个h265地址是无法播放的。因为flv里面不支持

二. 下载源码

git clone 以下地址,

https://github.com/FFmpeg/FFmpeg

切换到 release/4.0版本。

三. 修改源码

应用我们的patch

patch -p1 < /flv_hevc.patch

flv.hevc.path 如下:

 diff --git a/libavformat/flv.h b/libavformat/flv.h
index df5ce3d17f..97324cca46 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    = 12,
 };
 
 enum {
    
    
diff --git a/libavformat/flvdec.c b/libavformat/flvdec.c
index 51c4cd6cec..5c2650c7a0 100644
--- a/libavformat/flvdec.c
+++ b/libavformat/flvdec.c
@@ -36,6 +36,7 @@
 #include "internal.h"
 #include "avio_internal.h"
 #include "flv.h"
+#include "hevc.h"
 
 #define VALIDATE_INDEX_TS_THRESH 2500
 
@@ -238,6 +239,8 @@ static int flv_same_video_codec(AVCodecParameters *vpar, int flags)
         return vpar->codec_id == AV_CODEC_ID_VP6A;
     case FLV_CODECID_H264:
         return vpar->codec_id == AV_CODEC_ID_H264;
+    case FLV_CODECID_HEVC:
+        return vpar->codec_id == AV_CODEC_ID_HEVC;
     default:
         return vpar->codec_tag == flv_codecid;
     }
@@ -279,6 +282,10 @@ static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream,
         par->codec_id = AV_CODEC_ID_H264;
         vstream->need_parsing = AVSTREAM_PARSE_HEADERS;
         return 3;     // not 4, reading packet type will consume one byte
+    case FLV_CODECID_HEVC:
+        par->codec_id = AV_CODEC_ID_HEVC;
+        vstream->need_parsing = AVSTREAM_PARSE_HEADERS;
+        return 3;     // not 4, reading packet type will consume one byte
     case FLV_CODECID_MPEG4:
         par->codec_id = AV_CODEC_ID_MPEG4;
         return 3;
@@ -1046,10 +1053,12 @@ retry_duration:
 
     if (st->codecpar->codec_id == AV_CODEC_ID_AAC ||
         st->codecpar->codec_id == AV_CODEC_ID_H264 ||
-        st->codecpar->codec_id == AV_CODEC_ID_MPEG4) {
    
    
+        st->codecpar->codec_id == AV_CODEC_ID_MPEG4 ||
+        st->codecpar->codec_id == AV_CODEC_ID_HEVC) {
    
    
         int type = avio_r8(s->pb);
         size--;
-        if (st->codecpar->codec_id == AV_CODEC_ID_H264 || st->codecpar->codec_id == AV_CODEC_ID_MPEG4) {
    
    
+        if (st->codecpar->codec_id == AV_CODEC_ID_H264 || st->codecpar->codec_id == AV_CODEC_ID_MPEG4 ||
+            st->codecpar->codec_id == AV_CODEC_ID_HEVC) {
    
    
             // sign extension
             int32_t cts = (avio_rb24(s->pb) + 0xff800000) ^ 0xff800000;
             pts = dts + cts;
@@ -1065,7 +1074,7 @@ 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_H264 || st->codecpar->codec_id == AV_CODEC_ID_HEVC)) {
    
    
             AVDictionaryEntry *t;
 
             if (st->codecpar->extradata) {
    
    
diff --git a/libavformat/flvenc.c b/libavformat/flvenc.c
index d62ff70933..aaf7fb762b 100644
--- a/libavformat/flvenc.c
+++ b/libavformat/flvenc.c
@@ -43,6 +43,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 }
 };
 
@@ -489,7 +490,12 @@ static int flv_write_header(AVFormatContext *s)
                 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)
+                    ff_isom_write_hvcc(pb, par->extradata, par->extradata_size, 0);
+                else
+                    ff_isom_write_avcc(pb, par->extradata, par->extradata_size);
+// liangliang
+                // ff_isom_write_avcc(pb, par->extradata, par->extradata_size);
             }
             data_size = avio_tell(pb) - pos;
             avio_seek(pb, -data_size - 10, SEEK_CUR);
@@ -515,7 +521,8 @@ static int flv_write_trailer(AVFormatContext *s)
         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_avc_eos_tag(pb, sc->last_ts);
     }
 
@@ -549,7 +556,8 @@ 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;
@@ -598,7 +606,11 @@ 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_AAC && pkt->size > 2 &&
+    } 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) {
    
    
         av_log(s, AV_LOG_ERROR, "Malformed AAC bitstream detected: "
@@ -670,7 +682,8 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
                              (FFALIGN(par->height, 16) - par->height));
         } else if (par->codec_id == AV_CODEC_ID_AAC)
             avio_w8(pb, 1); // AAC raw
-        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) {
    
    
             avio_w8(pb, 1); // AVC NALU
             avio_wb24(pb, pkt->pts - pkt->dts);
         }

四. 重新编译

依然在ffmpeg目录下

./configure --prefix=/Users/tomxiang/develop/ffmpeg

如果需要编译VMAF打分模型需要加入:
 

./configure --prefix=/Users/only/Documents/ffmpegN/install --enable-shared --disable-static --enable-version3 --enable-libvmaf

make
make install

如果出现错误:nasm/yasm not found or too old. Use –disable-x86asm for a crippled build.
请先执行:
brew install yasm

此时develop/ffmpeg里面出现了.

在这里插入图片描述

五.执行

进入bin .

./ffplay http://flv265-3954.liveplay.myqcloud.com/live/3954_290383906.flv\?bizid\=3954\&txSecret\=8586ab1960216ba84ba19c006c8d1f25\&txTime\=5baabbc2

现在即可支持H265播放

在编译ffmpeg时出现错误,请参考下面文档:
https://lvv.me/posts/2020/04/14_build_ffmpeg/

猜你喜欢

转载自blog.csdn.net/weixin_45581597/article/details/127727870