【FFmpeg】avformat_write_header函数

FFmpeg相关记录:

示例工程:
【FFmpeg】调用ffmpeg库实现264软编
【FFmpeg】调用ffmpeg库实现264软解
【FFmpeg】调用ffmpeg库进行RTMP推流和拉流
【FFmpeg】调用ffmpeg库进行SDL2解码后渲染

流程分析:
【FFmpeg】编码链路上主要函数的简单分析
【FFmpeg】解码链路上主要函数的简单分析

结构体分析:
【FFmpeg】AVCodec结构体
【FFmpeg】AVCodecContext结构体
【FFmpeg】AVStream结构体
【FFmpeg】AVFormatContext结构体
【FFmpeg】AVIOContext结构体
【FFmpeg】AVPacket结构体

函数分析:
【FFmpeg】avformat_open_input函数
【FFmpeg】avformat_find_stream_info函数
【FFmpeg】avformat_alloc_output_context2函数

avformat_write_header内函数调用关系为
在这里插入图片描述

1.avformat_write_header

/**
 * Allocate the stream private data and write the stream header to
 * an output media file.
 *
 * @param s        Media file handle, must be allocated with
 *                 avformat_alloc_context().
 *                 Its \ref AVFormatContext.oformat "oformat" field must be set
 *                 to the desired output format;
 *                 Its \ref AVFormatContext.pb "pb" field must be set to an
 *                 already opened ::AVIOContext.
 * @param options  An ::AVDictionary filled with AVFormatContext and
 *                 muxer-private options.
 *                 On return this parameter will be destroyed and replaced with
 *                 a dict containing options that were not found. May be NULL.
 *
 * @retval AVSTREAM_INIT_IN_WRITE_HEADER On success, if the codec had not already been
 *                                       fully initialized in avformat_init_output().
 * @retval AVSTREAM_INIT_IN_INIT_OUTPUT  On success, if the codec had already been fully
 *                                       initialized in avformat_init_output().
 * @retval AVERROR                       A negative AVERROR on failure.
 *
 * @see av_opt_find, av_dict_set, avio_open, av_oformat_next, avformat_init_output.
 */
// 函数的功能:分配流私有数据并将流标头写入输出媒体文件
// c语言中,如果一个函数的返回值没有被使用,编译器会发出警告信息
// 而av_warn_unused_result宏的作用就是告诉编译器,这个函数的返回值虽然没有被使用
// 但是它是有意义的,不应该发出警告信息
av_warn_unused_result
int avformat_write_header(AVFormatContext *s, AVDictionary **options);

函数的定义位于libavformat\mux.c中,主要的工作流程为:
(1)如果没有进行初始化,则进行初始化输出(avformat_init_output)
(2)进行头信息的写入(write_header)
(3)如果流还没有被初始化,则写入pts(init_pts)

int avformat_write_header(AVFormatContext *s, AVDictionary **options)
{
    FFFormatContext *const si = ffformatcontext(s);
    int already_initialized = si->initialized;
    int streams_already_initialized = si->streams_initialized;
    int ret = 0;
	// ----- 1.如果没有初始化,则进行output的初始化 ----- //
    if (!already_initialized)
        if ((ret = avformat_init_output(s, options)) < 0)
            return ret;
	// ----- 2.进行头信息的写入 ----- //
    if (ffofmt(s->oformat)->write_header) {
        if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
			// 将写入的字节流标记为特定类型
			// 输出中省略长度为零的范围
            avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_HEADER);
        ret = ffofmt(s->oformat)->write_header(s);
        if (ret >= 0 && s->pb && s->pb->error < 0)
            ret = s->pb->error;
        if (ret < 0)
            goto fail;
        flush_if_needed(s);
    }
    if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
        avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_UNKNOWN);
	// ----- 3.如果流还没有被初始化,则初始化pts ----- //
    if (!si->streams_initialized) {
        if ((ret = init_pts(s)) < 0)
            goto fail;
    }

    return streams_already_initialized;

fail:
    deinit_muxer(s);
    return ret;
}

1.1 如果没有初始化,则进行output的初始化(avformat_init_output)

函数进行输出的初始化,主要调用函数init_muxer和init_pts。其中,init_muxer的初始化,准备将音频、视频和其他数据流混合成一个单一的文件格式;init_pts用于初始化解码后视频帧的时间戳(PTS),确保视频能够按照正确的时间顺序显示,当处理包含B帧的复杂编码时,init_pts的作用非常重要,因为B帧依赖多个参考帧,其解码和显示顺序不同。

int avformat_init_output(AVFormatContext *s, AVDictionary **options)
{
    FFFormatContext *const si = ffformatcontext(s);
    int ret = 0;
	// 初始化复用器
    if ((ret = init_muxer(s, options)) < 0)
        return ret;

    si->initialized = 1;
    si->streams_initialized = ret;
	// 初始化pts
    if (ffofmt(s->oformat)->init && ret) {
        if ((ret = init_pts(s)) < 0)
            return ret;

        return AVSTREAM_INIT_IN_INIT_OUTPUT;
    }

    return AVSTREAM_INIT_IN_WRITE_HEADER;
}

1.1.1 初始化复用器(init_muxer)

该函数用于初始化复用器,函数的定义位于libavformat\mux.c中,函数主要的工作流程为:
(1)将传入的options设置到AVFormatContext之中
(2)遍历每个AVStream,设置和检查相关信息
 (a)设置pts
 (b)检查音频的sample_rate和block_align
 (c)检查视频的长宽比
 (d)其他一些检查
(3)初始化格式(.init)
(4)如果初始化失败,会释放格式(.deinit)

static int init_muxer(AVFormatContext *s, AVDictionary **options)
{
    FFFormatContext *const si = ffformatcontext(s);
    AVDictionary *tmp = NULL;
    const FFOutputFormat *of = ffofmt(s->oformat);
    AVDictionaryEntry *e;
    static const unsigned default_codec_offsets[] = {
        [AVMEDIA_TYPE_VIDEO]    = offsetof(AVOutputFormat, video_codec),
        [AVMEDIA_TYPE_AUDIO]    = offsetof(AVOutputFormat, audio_codec),
        [AVMEDIA_TYPE_SUBTITLE] = offsetof(AVOutputFormat, subtitle_codec),
    };
    unsigned nb_type[FF_ARRAY_ELEMS(default_codec_offsets)] = { 0 };
    int ret = 0;

    if (options)
        av_dict_copy(&tmp, *options, 0);
	// 1.将传入的options设置到AVFormatContext之中
    if ((ret = av_opt_set_dict(s, &tmp)) < 0)
        goto fail;
    if (s->priv_data && s->oformat->priv_class && *(const AVClass**)s->priv_data==s->oformat->priv_class &&
        (ret = av_opt_set_dict2(s->priv_data, &tmp, AV_OPT_SEARCH_CHILDREN)) < 0)
        goto fail;

    if (!s->url && !(s->url = av_strdup(""))) {
        ret = AVERROR(ENOMEM);
        goto fail;
    }

    // some sanity checks
    // 完整性检查
    if (s->nb_streams == 0 && !(of->p.flags & AVFMT_NOSTREAMS)) {
        av_log(s, AV_LOG_ERROR, "No streams to mux were specified\n");
        ret = AVERROR(EINVAL);
        goto fail;
    }
	// 2.遍历每个AVStream,设置信息
    for (unsigned i = 0; i < s->nb_streams; i++) {
        AVStream          *const  st = s->streams[i];
        FFStream          *const sti = ffstream(st);
        AVCodecParameters *const par = st->codecpar;
        const AVCodecDescriptor *desc;
		// 设置pts信息
        if (!st->time_base.num) {
            /* fall back on the default timebase values */
            if (par->codec_type == AVMEDIA_TYPE_AUDIO && par->sample_rate)
                avpriv_set_pts_info(st, 64, 1, par->sample_rate);
            else
                avpriv_set_pts_info(st, 33, 1, 90000);
        }
		
        switch (par->codec_type) {
        // 检查音频的sample_rate和block_align
        case AVMEDIA_TYPE_AUDIO:
            if (par->sample_rate <= 0) {
                av_log(s, AV_LOG_ERROR, "sample rate not set\n");
                ret = AVERROR(EINVAL);
                goto fail;
            }

            if (!par->block_align)
                par->block_align = par->ch_layout.nb_channels *
                                   av_get_bits_per_sample(par->codec_id) >> 3;
            break;
        // 检查视频的长宽比
        case AVMEDIA_TYPE_VIDEO:
            if ((par->width <= 0 || par->height <= 0) &&
                !(of->p.flags & AVFMT_NODIMENSIONS)) {
                av_log(s, AV_LOG_ERROR, "dimensions not set\n");
                ret = AVERROR(EINVAL);
                goto fail;
            }
            if (av_cmp_q(st->sample_aspect_ratio, par->sample_aspect_ratio)
                && fabs(av_q2d(st->sample_aspect_ratio) - av_q2d(par->sample_aspect_ratio)) > 0.004*av_q2d(st->sample_aspect_ratio)
            ) {
                if (st->sample_aspect_ratio.num != 0 &&
                    st->sample_aspect_ratio.den != 0 &&
                    par->sample_aspect_ratio.num != 0 &&
                    par->sample_aspect_ratio.den != 0) {
                    av_log(s, AV_LOG_ERROR, "Aspect ratio mismatch between muxer "
                           "(%d/%d) and encoder layer (%d/%d)\n",
                           st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
                           par->sample_aspect_ratio.num,
                           par->sample_aspect_ratio.den);
                    ret = AVERROR(EINVAL);
                    goto fail;
                }
            }
            break;
        }
        // (1) FF_OFMT_FLAG_MAX_ONE_OF_EACH
        //	按照注释翻译过来,意思是:"如果设置此标志,则表示对于每种编解码器类型,其对应的默认编解码器都设置为只能混合一个该类型的流
        //	它进一步表明,编解码器类型没有默认编解码器或其默认编解码器为AV_CODEC_ID_NONE的流不能被混合"
        //	换句话说,在使用FFmpeg进行多媒体数据处理时,每种输出格式(OutputFormat)最多只能有一个实例被激活和使用
        // (2) FF_OFMT_FLAG_ONLY_DEFAULT_CODECS
        //	按照注释翻译,意思是:"如果设置了这个标志,那么唯一允许的音频/视频/字幕编解码器编号是
        //	AVOutputFormat.audio/video/subtitle_codec;如果后者中的任何一个未设置(即等于AV_CODEC_ID_NONE),
        //	则不支持相应类型的流。此外,没有默认编解码器字段的编解码器类型是不允许的"
        // 换句话说,如果定义了这个flag,那么只能使用默认的编解码器
        if (of->flags_internal & (FF_OFMT_FLAG_MAX_ONE_OF_EACH | FF_OFMT_FLAG_ONLY_DEFAULT_CODECS)) {
            enum AVCodecID default_codec_id = AV_CODEC_ID_NONE;
            unsigned nb;
            if ((unsigned)par->codec_type < FF_ARRAY_ELEMS(default_codec_offsets)) {
                nb = ++nb_type[par->codec_type];
                if (default_codec_offsets[par->codec_type])
                    default_codec_id = *(const enum AVCodecID*)((const char*)of + default_codec_offsets[par->codec_type]);
            }
            // 检查codec_id是否匹配
            if (of->flags_internal & FF_OFMT_FLAG_ONLY_DEFAULT_CODECS &&
                default_codec_id != AV_CODEC_ID_NONE && par->codec_id != default_codec_id) {
                av_log(s, AV_LOG_ERROR, "%s muxer supports only codec %s for type %s\n",
                       of->p.name, avcodec_get_name(default_codec_id), av_get_media_type_string(par->codec_type));
                ret = AVERROR(EINVAL);
                goto fail;
            } else if (default_codec_id == AV_CODEC_ID_NONE ||
                       (of->flags_internal & FF_OFMT_FLAG_MAX_ONE_OF_EACH && nb > 1)) {
                const char *type = av_get_media_type_string(par->codec_type);
                av_log(s, AV_LOG_ERROR, "%s muxer does not support %s stream of type %s\n",
                       of->p.name, default_codec_id == AV_CODEC_ID_NONE ? "any" : "more than one",
                       type ? type : "unknown");
                ret = AVERROR(EINVAL);
                goto fail;
            }
        }

#if FF_API_AVSTREAM_SIDE_DATA
FF_DISABLE_DEPRECATION_WARNINGS
        /* if the caller is using the deprecated AVStream side_data API,
         * copy its contents to AVStream.codecpar, giving it priority
           over existing side data in the latter */
        for (int i = 0; i < st->nb_side_data; i++) {
            const AVPacketSideData *sd_src = &st->side_data[i];
            AVPacketSideData *sd_dst;

            sd_dst = av_packet_side_data_new(&st->codecpar->coded_side_data,
                                             &st->codecpar->nb_coded_side_data,
                                             sd_src->type, sd_src->size, 0);
            if (!sd_dst) {
                ret = AVERROR(ENOMEM);
                goto fail;
            }
            memcpy(sd_dst->data, sd_src->data, sd_src->size);
        }
FF_ENABLE_DEPRECATION_WARNINGS
#endif

        desc = avcodec_descriptor_get(par->codec_id);
        if (desc && desc->props & AV_CODEC_PROP_REORDER)
            sti->reorder = 1;

        sti->is_intra_only = ff_is_intra_only(par->codec_id);
		// 设置codec_tag
        if (of->p.codec_tag) {
            if (   par->codec_tag
                && par->codec_id == AV_CODEC_ID_RAWVIDEO
                && (   av_codec_get_tag(of->p.codec_tag, par->codec_id) == 0
                    || av_codec_get_tag(of->p.codec_tag, par->codec_id) == MKTAG('r', 'a', 'w', ' '))
                && !validate_codec_tag(s, st)) {
                // the current rawvideo encoding system ends up setting
                // the wrong codec_tag for avi/mov, we override it here
                par->codec_tag = 0;
            }
            if (par->codec_tag) {
                if (!validate_codec_tag(s, st)) {
                    const uint32_t otag = av_codec_get_tag(s->oformat->codec_tag, par->codec_id);
                    av_log(s, AV_LOG_ERROR,
                           "Tag %s incompatible with output codec id '%d' (%s)\n",
                           av_fourcc2str(par->codec_tag), par->codec_id, av_fourcc2str(otag));
                    ret = AVERROR_INVALIDDATA;
                    goto fail;
                }
            } else
                par->codec_tag = av_codec_get_tag(of->p.codec_tag, par->codec_id);
        }

        if (par->codec_type != AVMEDIA_TYPE_ATTACHMENT &&
            par->codec_id != AV_CODEC_ID_SMPTE_2038)
            si->nb_interleaved_streams++;
    }
    si->interleave_packet = of->interleave_packet;
    if (!si->interleave_packet)
        si->interleave_packet = si->nb_interleaved_streams > 1 ?
                                    ff_interleave_packet_per_dts :
                                    ff_interleave_packet_passthrough;

    if (!s->priv_data && of->priv_data_size > 0) {
        s->priv_data = av_mallocz(of->priv_data_size);
        if (!s->priv_data) {
            ret = AVERROR(ENOMEM);
            goto fail;
        }
        if (of->p.priv_class) {
            *(const AVClass **)s->priv_data = of->p.priv_class;
            av_opt_set_defaults(s->priv_data);
            if ((ret = av_opt_set_dict2(s->priv_data, &tmp, AV_OPT_SEARCH_CHILDREN)) < 0)
                goto fail;
        }
    }

    /* set muxer identification string */
    if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
        av_dict_set(&s->metadata, "encoder", LIBAVFORMAT_IDENT, 0);
    } else {
        av_dict_set(&s->metadata, "encoder", NULL, 0);
    }

    for (e = NULL; e = av_dict_get(s->metadata, "encoder-", e, AV_DICT_IGNORE_SUFFIX); ) {
        av_dict_set(&s->metadata, e->key, NULL, 0);
    }

    if (options) {
         av_dict_free(options);
         *options = tmp;
    }
	// 3.初始化格式
	// 可以在这里分配数据,并在发送数据包之前设置任何需要设置的AVFormatContext或AVStream参数
	// 对于flv格式,这里会调用flv_init()
    if (of->init) {
        if ((ret = of->init(s)) < 0) {
            if (of->deinit)
                of->deinit(s); // 4.对于flv格式,这里会调用flv_deinit()
            return ret;
        }
        return ret == 0;
    }

    return 0;

fail:
    av_dict_free(&tmp);
    return ret;
}

下面以FLV格式为例,记录如何初始化format

1.1.1.1 初始化format(flv_init)

函数的定义位于libavformat\flvenc.c中,主要用于FLV格式的初始化。具体来说,会对数据源当中的每一条流进行解析,将stream中的信息设置到AVFormatContext中的priv_data之中。原先一直对priv_data不是很理解,从这里看,似乎这样的一个void类型数据能够用于不同类型的上下文的转换,例如下面代码中的FLContext

static int flv_init(struct AVFormatContext *s)
{
    int i;
    FLVContext *flv = s->priv_data;
	// 如果nb_stream超过了FLV_STREAM_TYPE_NB(4),说明当前的流存在错误
	// 正常FLV最多包含4条流,分别是视频、音频、字幕和DATA
    if (s->nb_streams > FLV_STREAM_TYPE_NB) {
        av_log(s, AV_LOG_ERROR, "invalid number of streams %d\n",
                s->nb_streams);
        return AVERROR(EINVAL);
    }
	// 遍历每一条流,将流当中的信息设置到flv当中
    for (i = 0; i < s->nb_streams; i++) {
        AVCodecParameters *par = s->streams[i]->codecpar;

        switch (par->codec_type) {
        // 视频
        case AVMEDIA_TYPE_VIDEO:
            if (s->streams[i]->avg_frame_rate.den &&
                s->streams[i]->avg_frame_rate.num) {
                flv->framerate = av_q2d(s->streams[i]->avg_frame_rate);
            }
            if (flv->video_par) {
                av_log(s, AV_LOG_ERROR,
                       "at most one video stream is supported in flv\n");
                return AVERROR(EINVAL);
            }
            flv->video_par = par;
            if (!ff_codec_get_tag(flv_video_codec_ids, par->codec_id))
                return unsupported_codec(s, "Video", par->codec_id);

            if (par->codec_id == AV_CODEC_ID_MPEG4 ||
                par->codec_id == AV_CODEC_ID_H263) {
                int error = s->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL;
                av_log(s, error ? AV_LOG_ERROR : AV_LOG_WARNING,
                       "Codec %s is not supported in the official FLV specification,\n", avcodec_get_name(par->codec_id));

                if (error) {
                    av_log(s, AV_LOG_ERROR,
                           "use vstrict=-1 / -strict -1 to use it anyway.\n");
                    return AVERROR(EINVAL);
                }
            } else if (par->codec_id == AV_CODEC_ID_VP6) {
                av_log(s, AV_LOG_WARNING,
                       "Muxing VP6 in flv will produce flipped video on playback.\n");
            }
            break;
        // 音频
        case AVMEDIA_TYPE_AUDIO:
            if (flv->audio_par) {
                av_log(s, AV_LOG_ERROR,
                       "at most one audio stream is supported in flv\n");
                return AVERROR(EINVAL);
            }
            flv->audio_par = par;
            if (get_audio_flags(s, par) < 0)
                return unsupported_codec(s, "Audio", par->codec_id);
            if (par->codec_id == AV_CODEC_ID_PCM_S16BE)
                av_log(s, AV_LOG_WARNING,
                       "16-bit big-endian audio in flv is valid but most likely unplayable (hardware dependent); use s16le\n");
            break;
        // 数据
        case AVMEDIA_TYPE_DATA:
            if (par->codec_id != AV_CODEC_ID_TEXT && par->codec_id != AV_CODEC_ID_NONE)
                return unsupported_codec(s, "Data", par->codec_id);
            flv->data_par = par;
            break;
        // 字幕
        case AVMEDIA_TYPE_SUBTITLE:
            if (par->codec_id != AV_CODEC_ID_TEXT) {
                av_log(s, AV_LOG_ERROR, "Subtitle codec '%s' for stream %d is not compatible with FLV\n",
                       avcodec_get_name(par->codec_id), i);
                return AVERROR_INVALIDDATA;
            }
            flv->data_par = par;
            break;
        default:
            av_log(s, AV_LOG_ERROR, "Codec type '%s' for stream %d is not compatible with FLV\n",
                   av_get_media_type_string(par->codec_type), i);
            return AVERROR(EINVAL);
        }
        avpriv_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
        flv->last_ts[i] = -1;
    }

    flv->delay = AV_NOPTS_VALUE;

    return 0;
}
1.1.1.2 释放format(flv_deinit)
static void flv_deinit(AVFormatContext *s)
{
    FLVContext *flv = s->priv_data;
    FLVFileposition *filepos = flv->head_filepositions;

    while (filepos) {
        FLVFileposition *next = filepos->next;
        av_free(filepos);
        filepos = next;
    }
    flv->filepositions = flv->head_filepositions = NULL;
    flv->filepositions_count = 0;
}

1.1.2 初始化pts(init_pts)

init_pts用于初始化解码或编码过程中的时间戳(Presentation Time Stamp, PTS)

static int init_pts(AVFormatContext *s)
{
    FFFormatContext *const si = ffformatcontext(s);

    /* init PTS generation */
    // 初始化pts生成
    for (unsigned i = 0; i < s->nb_streams; i++) {
        AVStream *const st = s->streams[i];
        FFStream *const sti = ffstream(st);
        int64_t den = AV_NOPTS_VALUE;

        switch (st->codecpar->codec_type) {
        case AVMEDIA_TYPE_AUDIO:
            den = (int64_t)st->time_base.num * st->codecpar->sample_rate;
            break;
        case AVMEDIA_TYPE_VIDEO:
            den = (int64_t)st->time_base.num * st->time_base.den;
            break;
        default:
            break;
        }

        if (den != AV_NOPTS_VALUE) {
            if (den <= 0)
                return AVERROR_INVALIDDATA;

            frac_init(&sti->priv_pts, 0, 0, den);
        }
    }

    si->avoid_negative_ts_status = AVOID_NEGATIVE_TS_UNKNOWN;
    if (s->avoid_negative_ts < 0) {
        av_assert2(s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_AUTO);
        if (s->oformat->flags & (AVFMT_TS_NEGATIVE | AVFMT_NOTIMESTAMPS)) {
            s->avoid_negative_ts = AVFMT_AVOID_NEG_TS_DISABLED;
            si->avoid_negative_ts_status = AVOID_NEGATIVE_TS_DISABLED;
        } else
            s->avoid_negative_ts = AVFMT_AVOID_NEG_TS_MAKE_NON_NEGATIVE;
    } else if (s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_DISABLED)
        si->avoid_negative_ts_status = AVOID_NEGATIVE_TS_DISABLED;

    return 0;
}

1.2 头信息的写入(write_header)

不同的FFOutputFormat会对应不同的写入方法,以FLV格式为例,会调用flv_write_header进行格式的写入

const FFOutputFormat ff_flv_muxer = {
    .p.name         = "flv",
    .p.long_name    = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),
    .p.mime_type    = "video/x-flv",
    .p.extensions   = "flv",
    .priv_data_size = sizeof(FLVContext),
    .p.audio_codec  = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_ADPCM_SWF,
    .p.video_codec  = AV_CODEC_ID_FLV1,
    .init           = flv_init,
    .write_header   = flv_write_header,	// 调用flv_write_header进行write_header
    .write_packet   = flv_write_packet,
    .write_trailer  = flv_write_trailer,
    .deinit         = flv_deinit,
    .check_bitstream= flv_check_bitstream,
    .p.codec_tag    = (const AVCodecTag* const []) {
                          flv_video_codec_ids, flv_audio_codec_ids, 0
                      },
    .p.flags        = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS |
                      AVFMT_TS_NONSTRICT,
    .p.priv_class   = &flv_muxer_class,
};

flv_write_header的定义如下

static int flv_write_header(AVFormatContext *s)
{
    int i;
    AVIOContext *pb = s->pb;
    FLVContext *flv = s->priv_data;
	// 开始写入
	// signature,文件标识
    avio_write(pb, "FLV", 3);
	// version,版本号
    avio_w8(pb, 1);
	// !! 的目的是将非0转为1
	// flags,前5位保留,第6位表示是否存在音频tag,第7位保留且必须为0
	// 第8位表示是否存在视频tag
    avio_w8(pb, FLV_HEADER_FLAG_HASAUDIO * !!flv->audio_par +
                FLV_HEADER_FLAG_HASVIDEO * !!flv->video_par);
	// header size,表示从file header开始到file body之间的字节数,
    avio_wb32(pb, 9);
	// header结束
	// previous tag size,表示前一个tag的长度
    avio_wb32(pb, 0);

    for (i = 0; i < s->nb_streams; i++)
		// codecpar->codec_tag为5,表示视频流采用了MPEG Video编码格式
        if (s->streams[i]->codecpar->codec_tag == 5) {
            avio_w8(pb, 8);     // message type
            avio_wb24(pb, 0);   // include flags
            avio_wb24(pb, 0);   // time stamp
            avio_wb32(pb, 0);   // reserved
            avio_wb32(pb, 11);  // size
            flv->reserved = 5;
        }

    if (flv->flags & FLV_NO_METADATA) {
        pb->seekable = 0;
    } else {
        write_metadata(s, 0);
    }
	// 写入codec的头
    for (i = 0; i < s->nb_streams; i++) {
        flv_write_codec_header(s, s->streams[i]->codecpar, 0);
    }

    flv->datastart_offset = avio_tell(pb);
    return 0;
}

flv_write_codec_header的定义如下

static void flv_write_codec_header(AVFormatContext* s, AVCodecParameters* par, int64_t ts) {
    int64_t data_size;
    AVIOContext *pb = s->pb;
    FLVContext *flv = s->priv_data;

    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) {
        int64_t pos;
        avio_w8(pb,
                par->codec_type == AVMEDIA_TYPE_VIDEO ?
                        FLV_TAG_TYPE_VIDEO : FLV_TAG_TYPE_AUDIO); // 写入codec_type
        avio_wb24(pb, 0); // size patched later
        put_timestamp(pb, ts);
        avio_wb24(pb, 0); // streamid
        pos = avio_tell(pb);
		// 写入AAC信息
        if (par->codec_id == AV_CODEC_ID_AAC) {
            avio_w8(pb, get_audio_flags(s, par));
            avio_w8(pb, 0); // AAC sequence header

            if (!par->extradata_size && (flv->flags & FLV_AAC_SEQ_HEADER_DETECT)) {
                PutBitContext pbc;
                int samplerate_index;
                int channels = par->ch_layout.nb_channels
                        - (par->ch_layout.nb_channels == 8 ? 1 : 0);
                uint8_t data[2];

                for (samplerate_index = 0; samplerate_index < 16;
                        samplerate_index++)
                    if (par->sample_rate
                            == ff_mpeg4audio_sample_rates[samplerate_index])
                        break;

                init_put_bits(&pbc, data, sizeof(data));
                put_bits(&pbc, 5, par->profile + 1); //profile
                put_bits(&pbc, 4, samplerate_index); //sample rate index
                put_bits(&pbc, 4, channels);
                put_bits(&pbc, 1, 0); //frame length - 1024 samples
                put_bits(&pbc, 1, 0); //does not depend on core coder
                put_bits(&pbc, 1, 0); //is not extension
                flush_put_bits(&pbc);

                avio_w8(pb, data[0]);
                avio_w8(pb, data[1]);

                av_log(s, AV_LOG_WARNING, "AAC sequence header: %02x %02x.\n",
                        data[0], data[1]);
            }
            avio_write(pb, par->extradata, par->extradata_size);
        } else { // 如果是视频编码格式
            if (par->codec_id == AV_CODEC_ID_HEVC) { // hevc
                avio_w8(pb, FLV_IS_EX_HEADER | PacketTypeSequenceStart | FLV_FRAME_KEY); // ExVideoTagHeader mode with PacketTypeSequenceStart
                avio_write(pb, "hvc1", 4);
            } else if (par->codec_id == AV_CODEC_ID_AV1 || par->codec_id == AV_CODEC_ID_VP9) { // av1和vp9
                avio_w8(pb, FLV_IS_EX_HEADER | PacketTypeSequenceStart | FLV_FRAME_KEY);
                avio_write(pb, par->codec_id == AV_CODEC_ID_AV1 ? "av01" : "vp09", 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)
                ff_isom_write_vpcc(s, pb, par->extradata, par->extradata_size, par);
            else
                ff_isom_write_avcc(pb, par->extradata, par->extradata_size);
        }
        data_size = avio_tell(pb) - pos;
        avio_seek(pb, -data_size - 10, SEEK_CUR);
        avio_wb24(pb, data_size);
        avio_skip(pb, data_size + 10 - 3);
        avio_wb32(pb, data_size + 11); // previous tag size
    }
}

2.小结

avformat_write_header用于写入头信息,大体来说,可以分为两个部分:(1)为写入信息做初始化,调用init_muxer、of->init实现;(2)初始化之后,使用write_header实现头信息的写入。初始化的信息比如时间戳pts,codec_tag等,write_header会实现写入头信息。这里的信息比较琐碎,暂时还不完全理解,后续在实际工程之中再做理解

CSDN : https://blog.csdn.net/weixin_42877471
Github : https://github.com/DoFulangChen

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/757585.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

LeetCode 207. 课程表

思路&#xff1a;这是一道拓扑排序问题&#xff0c;拓扑排序听起来可能有点复杂&#xff0c;但实际上它是个相当直观的概念。想象一下&#xff0c;你有很多事情要做&#xff0c;但有些事情必须在另一些事情完成之后才能开始&#xff0c;就像你得先穿上袜子再穿鞋子 拓扑排序就…

【UML用户指南】-23-对高级行为建模-状态机

目录 1、概述 2、状态 2.1、状态的组成 3、转移 3.1、转移的组成 4、高级状态和转移 4.1、进入效应和退出效应 4.2、内部转移 4.3、do活动 4.4、延迟事件 4.5、子状态机 5、子状态 5.1、非正交子状态 5.2、历史状态 5.3、正交子状态 6、分叉与汇合 7、主动对象…

Uboot重定位

Uboot重定位 一、重定位的意义二、介绍一些重定位相关的表项结构(节)三、uboot的重定位过程:一、重定位的意义 uboot的重定位有两次,第一次是在编译成镜像后,在makefile中调用进行处理的,其调用tools/riscv_prelink.c的代码进行重定位处理(主要就是对重定位表中的R_RIS…

Linux多进程和多线程(一)

进程 进程的概念 进程&#xff08;Process&#xff09;是操作系统对一个正在运行的程序的一种抽象。它是系统运行程序的最小单位&#xff0c;是资源分配和调度的基本单位。 进程的特点如下 进程是⼀个独⽴的可调度的活动, 由操作系统进⾏统⼀调度, 相应的任务会被调度到cpu …

[软件安装]Dev C++

一、下载Dev C软件安装包 1、官网下载官网 2、百度网盘下载压缩包 二、安装Dev C 1、解压Dev C软件安装包 2、找到【Dev-Cpp 5.11…】应用程序&#xff0c;右键选择【以管理员身份运行】它 3、设置语言 回到桌面&#xff0c;右键桌面上的【Dev C 5.11软件图标】&#xff0c…

vue插槽的简单使用

默认插槽 1.在Category中创建插槽 <slot>默认值<slot/> 2.在App中使用 <Category tittle"美食"> <ul ><li v-for"(l,index) in foods" :key"index">{{l}}</li></ul> </Category> 3.运行后的…

用英文介绍美国总统:Barack Obama First African-American President (2009 – 2017)

Barack Obama: First African-American President (2009 – 2017) Link: https://www.youtube.com/watch?vwHCBI3yypmE&listPLybg94GvOJ9E-ZM1U6PAjgPUmz-V4-Yja&index44 Introduction Barack Obama made history as the first African-American elected to the pre…

中国电信股份有限公司江西分公司招聘信息 7.5日截止

法律事务管理(南昌) 学历要求 本科及以上学历 岗位职责 1.依据国家法律、法规和相关规章规定,为公司其他部门提供日常法律服务与支持; 2.负责公司各类合同审核工作; 3.负责公司法律文件的起草和法律事务谈判; 4.围绕与公司业务有关的法律问题及法…

Alibaba Cloud Linux详解_操作系统兼容性_alinux稳定性全解析

Alibaba Cloud Linux是阿里云自研的稳定、安全、高性能的服务器Linux操作系统&#xff0c;完全兼容CentOS/RHEL生态和操作方式&#xff0c;又阿里云提供免费提供长期支持和维护LTS。Alibaba Cloud Linux是目前阿里云服务器最大规模使用的操作系统之一&#xff0c;可部署在Web网…

基于GWO灰狼优化的多目标优化算法matlab仿真

目录 1.程序功能描述 2.测试软件版本以及运行结果展示 3.核心程序 4.本算法原理 4.1灰狼优化算法原理 4.2 多目标优化问题(MOP)的帕累托最优解 4.3 基于GWO的多目标优化算法 5.完整程序 1.程序功能描述 基于GWO灰狼优化的多目标优化算法matlab仿真&#xff0c;目标函数…

华为数通——STP-RSTP-MSTP生成树

STP 为了提高网络可靠性&#xff0c;交换机之间常常会进行设备冗余&#xff08;备份&#xff09;&#xff0c;但这样会给交换网络带来环路风险&#xff0c;导致广播风暴以及MAC地址表不稳定等问题。 STP&#xff1a;生成树协议的作用就是为了解决避免二层环路&#xff0c;解决…

[NeurIPS2021] Deep Residual Learning in Spiking Neural Networks【文献精读、翻译】

深度残差学习在脉冲神经网络中的应用 Fang W, Yu Z, Chen Y, et al. Deep residual learning in spiking neural networks[J]. Advances in Neural Information Processing Systems, 2021, 34: 21056-21069. 摘要 深度脉冲神经网络 (SNNs) 因为使用离散的二进制激活和复杂的时…

FlowUs息流打造AI赋能下的知识库,信息深度挖掘与智能创作!FlowUs让你的数据资产更有价值

在AI时代的大潮中&#xff0c;FlowUs息流笔记类数据库凭借其强大的数据资产管理能力&#xff0c;正以前所未有的方式重塑着知识工作者的学习、研究与协作模式。当深厚的数据资产遇上AI的智能助力&#xff0c;无论是学术论文的撰写&#xff0c;还是高效提炼多人会议的核心观点&a…

web前端大作业-乡村扶贫、乡村振兴

文章目录 代码分析页面截图代码连接 代码分析 代码结构 主页index.html <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta http-equiv"X-UA-Compatible" content"IEedge"><meta…

STC89C52RC单片机设计的FM收音机+自动搜台+存储电台(程序+原理图+PCB)

资料下载地址&#xff1a;STC89C52RC单片机设计的FM收音机自动搜台存储电台&#xff08;程序原理图PCB) 1、实物图 2、部分程序 #include <reg52.h> #include "tea5767.h" #include "delay.h" #include "lcd1602.h" //K1:上一台 K2:下一…

Redis基础教程(二):redis数据类型

&#x1f49d;&#x1f49d;&#x1f49d;首先&#xff0c;欢迎各位来到我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里不仅可以有所收获&#xff0c;同时也能感受到一份轻松欢乐的氛围&#xff0c;祝你生活愉快&#xff01; &#x1f49d;&#x1f49…

电商平台数据爬取经验分享

一、引言 在电商领域&#xff0c;数据的重要性不言而喻。无论是市场趋势分析、竞争对手研究&#xff0c;还是用户行为洞察&#xff0c;都离不开数据的支持。而数据爬虫作为获取这些数据的重要工具&#xff0c;其技术的掌握和运用对于电商平台来说至关重要。本文将结合个人实际…

LoRaWAN网关源码分析(基础概念篇)

目录 一、简介 1、lora_gateway 2、packet_forwarder 二、目录结构 1、lora_gateway 2、packet_forwarder 一、简介 LoRaWAN网关的实现主要依赖两个源代码&#xff1a;lora_gateway和packet_forwarder。接下来&#xff0c;我们将从分析源代码入手&#xff0c;移植LoRaWAN源…

Ubuntu系统打包ISO镜像文件

本文以ubuntu20.04系统为例 1.Systemback简介 Systemback 是一个开源的系统备份和恢复工具&#xff0c;它主要用于 Linux 操作系统。Systemback 可以帮助用户创建完整的系统备份&#xff0c;包括操作系统、应用程序、用户数据等&#xff0c;并且可以在需要时将系统恢复到备份的…