hevc的头信息保存在stsd的hev中,如下图,68 78 63 43对应的hvcC就是hevc的标识,阴影部分就是重要的vps,sps,pps数据。
ffmpeg中读取头hvcC信息,在mov.c中,如下:
{ MKTAG('h','v','c','C'), mov_read_glbl }, //碰到hvcC头,就用mov_read_glbl处理。
/**
* This function reads atom content and puts data in extradata without tag
* nor size unlike mov_read_extradata.
*/
static int mov_read_glbl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
AVStream *st;
int ret;
if (c->fc->nb_streams < 1)
return 0;
st = c->fc->streams[c->fc->nb_streams-1];
if ((uint64_t)atom.size > (1<<30))
return AVERROR_INVALIDDATA;
if (atom.size >= 10) {
// Broken files created by legacy versions of libavformat will
// wrap a whole fiel atom inside of a glbl atom.
unsigned size = avio_rb32(pb);
unsigned type = avio_rl32(pb);
avio_seek(pb, -8, SEEK_CUR);
if (type == MKTAG('f','i','e','l') && size == atom.size)
return mov_read_default(c, pb, atom);
}
// codecpar定义为AVCodecParameters *codecpar;
if (st->codecpar->extradata_size > 1 && st->codecpar->extradata) {
av_log(c, AV_LOG_WARNING, "ignoring multiple glbl\n");
return 0;
}
av_freep(&st->codecpar->extradata);
ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size);
if (ret < 0)
return ret;
return 0;
}
int ff_get_extradata(AVFormatContext *s, AVCodecParameters *par, AVIOContext *pb, int size)
{
int ret = ff_alloc_extradata(par, size); //分配size大小的空间,也就是st->codecpar->extradata_size为size。
if (ret < 0)
return ret;
ret = avio_read(pb, par->extradata, size); //将数据读到par->extradata,保存起来。
if (ret != size) {
av_freep(&par->extradata);
par->extradata_size = 0;
av_log(s, AV_LOG_ERROR, "Failed to read extradata of size %d\n", size);
return ret < 0 ? ret : AVERROR_INVALIDDATA;
}
return ret;
}