static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb,

                                  AVCodecParameters *par,

                                  int native_id, int qt_id)

{

    AVIOContext *dyn_cp;

    uint8_t *codecpriv;

    int ret, codecpriv_size;



    ret = avio_open_dyn_buf(&dyn_cp);

    if (ret < 0)

        return ret;



    if (native_id) {

        ret = mkv_write_native_codecprivate(s, par, dyn_cp);

    } else if (par->codec_type == AVMEDIA_TYPE_VIDEO) {

        if (qt_id) {

            if (!par->codec_tag)

                par->codec_tag = ff_codec_get_tag(ff_codec_movvideo_tags,

                                                    par->codec_id);

            if (   ff_codec_get_id(ff_codec_movvideo_tags, par->codec_tag) == par->codec_id

                && (!par->extradata_size || ff_codec_get_id(ff_codec_movvideo_tags, AV_RL32(par->extradata + 4)) != par->codec_id)

            ) {

                int i;

                avio_wb32(dyn_cp, 0x5a + par->extradata_size);

                avio_wl32(dyn_cp, par->codec_tag);

                for(i = 0; i < 0x5a - 8; i++)

                    avio_w8(dyn_cp, 0);

            }

            avio_write(dyn_cp, par->extradata, par->extradata_size);

        } else {

            if (!ff_codec_get_tag(ff_codec_bmp_tags, par->codec_id))

                av_log(s, AV_LOG_WARNING, "codec %s is not supported by this format\n",

                       avcodec_get_name(par->codec_id));



            if (!par->codec_tag)

                par->codec_tag = ff_codec_get_tag(ff_codec_bmp_tags,

                                                  par->codec_id);

            if (!par->codec_tag && par->codec_id != AV_CODEC_ID_RAWVIDEO) {

                av_log(s, AV_LOG_ERROR, "No bmp codec tag found for codec %s\n",

                       avcodec_get_name(par->codec_id));

                ret = AVERROR(EINVAL);

            }



            ff_put_bmp_header(dyn_cp, par, ff_codec_bmp_tags, 0, 0);

        }

    } else if (par->codec_type == AVMEDIA_TYPE_AUDIO) {

        unsigned int tag;

        tag = ff_codec_get_tag(ff_codec_wav_tags, par->codec_id);

        if (!tag) {

            av_log(s, AV_LOG_ERROR, "No wav codec tag found for codec %s\n",

                   avcodec_get_name(par->codec_id));

            ret = AVERROR(EINVAL);

        }

        if (!par->codec_tag)

            par->codec_tag = tag;



        ff_put_wav_header(s, dyn_cp, par, FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX);

    }



    codecpriv_size = avio_close_dyn_buf(dyn_cp, &codecpriv);

    if (codecpriv_size)

        put_ebml_binary(pb, MATROSKA_ID_CODECPRIVATE, codecpriv,

                        codecpriv_size);

    av_free(codecpriv);

    return ret;

}

static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb,

                                  AVCodecParameters *par,

                                  int native_id, int qt_id)

{

    AVIOContext *dyn_cp;

    uint8_t *codecpriv;

    int ret, codecpriv_size;



    ret = avio_open_dyn_buf(&dyn_cp);

    if (ret < 0)

        return ret;



    if (native_id) {

        ret = mkv_write_native_codecprivate(s, par, dyn_cp);

    } else if (par->codec_type == AVMEDIA_TYPE_VIDEO) {

        if (qt_id) {

            if (!par->codec_tag)

                par->codec_tag = ff_codec_get_tag(ff_codec_movvideo_tags,

                                                    par->codec_id);

            if (   ff_codec_get_id(ff_codec_movvideo_tags, par->codec_tag) == par->codec_id

                && (!par->extradata_size || ff_codec_get_id(ff_codec_movvideo_tags, AV_RL32(par->extradata + 4)) != par->codec_id)

            ) {

                int i;

                avio_wb32(dyn_cp, 0x5a + par->extradata_size);

                avio_wl32(dyn_cp, par->codec_tag);

                for(i = 0; i < 0x5a - 8; i++)

                    avio_w8(dyn_cp, 0);

            }

            avio_write(dyn_cp, par->extradata, par->extradata_size);

        } else {

            if (!ff_codec_get_tag(ff_codec_bmp_tags, par->codec_id))

                av_log(s, AV_LOG_WARNING, "codec %s is not supported by this format\n",

                       avcodec_get_name(par->codec_id));



            if (!par->codec_tag)

                par->codec_tag = ff_codec_get_tag(ff_codec_bmp_tags,

                                                  par->codec_id);

            if (!par->codec_tag && par->codec_id != AV_CODEC_ID_RAWVIDEO) {

                av_log(s, AV_LOG_ERROR, "No bmp codec tag found for codec %s\n",

                       avcodec_get_name(par->codec_id));

                ret = AVERROR(EINVAL);

            }



            ff_put_bmp_header(dyn_cp, par, ff_codec_bmp_tags, 0, 0);

        }

    } else if (par->codec_type == AVMEDIA_TYPE_AUDIO) {

        unsigned int tag;

        tag = ff_codec_get_tag(ff_codec_wav_tags, par->codec_id);

        if (!tag) {

            av_log(s, AV_LOG_ERROR, "No wav codec tag found for codec %s\n",

                   avcodec_get_name(par->codec_id));

            ret = AVERROR(EINVAL);

        }

        if (!par->codec_tag)

            par->codec_tag = tag;



        ff_put_wav_header(s, dyn_cp, par, FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX);

    }



    codecpriv_size = avio_close_dyn_buf(dyn_cp, &codecpriv);

    if (codecpriv_size)

        put_ebml_binary(pb, MATROSKA_ID_CODECPRIVATE, codecpriv,

                        codecpriv_size);

    av_free(codecpriv);

    return ret;

}

static int FUNC_0(AVFormatContext *VAR_0, AVIOContext *VAR_1,

                                  AVCodecParameters *VAR_2,

                                  int VAR_3, int VAR_4)

{

    AVIOContext *dyn_cp;

    uint8_t *codecpriv;

    int VAR_5, VAR_6;



    VAR_5 = avio_open_dyn_buf(&dyn_cp);

    if (VAR_5 < 0)

        return VAR_5;



    if (VAR_3) {

        VAR_5 = mkv_write_native_codecprivate(VAR_0, VAR_2, dyn_cp);

    } else if (VAR_2->codec_type == AVMEDIA_TYPE_VIDEO) {

        if (VAR_4) {

            if (!VAR_2->codec_tag)

                VAR_2->codec_tag = ff_codec_get_tag(ff_codec_movvideo_tags,

                                                    VAR_2->codec_id);

            if (   ff_codec_get_id(ff_codec_movvideo_tags, VAR_2->codec_tag) == VAR_2->codec_id

                && (!VAR_2->extradata_size || ff_codec_get_id(ff_codec_movvideo_tags, AV_RL32(VAR_2->extradata + 4)) != VAR_2->codec_id)

            ) {

                int VAR_7;

                avio_wb32(dyn_cp, 0x5a + VAR_2->extradata_size);

                avio_wl32(dyn_cp, VAR_2->codec_tag);

                for(VAR_7 = 0; VAR_7 < 0x5a - 8; VAR_7++)

                    avio_w8(dyn_cp, 0);

            }

            avio_write(dyn_cp, VAR_2->extradata, VAR_2->extradata_size);

        } else {

            if (!ff_codec_get_tag(ff_codec_bmp_tags, VAR_2->codec_id))

                av_log(VAR_0, AV_LOG_WARNING, "codec %VAR_0 is not supported by this format\n",

                       avcodec_get_name(VAR_2->codec_id));



            if (!VAR_2->codec_tag)

                VAR_2->codec_tag = ff_codec_get_tag(ff_codec_bmp_tags,

                                                  VAR_2->codec_id);

            if (!VAR_2->codec_tag && VAR_2->codec_id != AV_CODEC_ID_RAWVIDEO) {

                av_log(VAR_0, AV_LOG_ERROR, "No bmp codec VAR_8 found for codec %VAR_0\n",

                       avcodec_get_name(VAR_2->codec_id));

                VAR_5 = AVERROR(EINVAL);

            }



            ff_put_bmp_header(dyn_cp, VAR_2, ff_codec_bmp_tags, 0, 0);

        }

    } else if (VAR_2->codec_type == AVMEDIA_TYPE_AUDIO) {

        unsigned int VAR_8;

        VAR_8 = ff_codec_get_tag(ff_codec_wav_tags, VAR_2->codec_id);

        if (!VAR_8) {

            av_log(VAR_0, AV_LOG_ERROR, "No wav codec VAR_8 found for codec %VAR_0\n",

                   avcodec_get_name(VAR_2->codec_id));

            VAR_5 = AVERROR(EINVAL);

        }

        if (!VAR_2->codec_tag)

            VAR_2->codec_tag = VAR_8;



        ff_put_wav_header(VAR_0, dyn_cp, VAR_2, FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX);

    }



    VAR_6 = avio_close_dyn_buf(dyn_cp, &codecpriv);

    if (VAR_6)

        put_ebml_binary(VAR_1, MATROSKA_ID_CODECPRIVATE, codecpriv,

                        VAR_6);

    av_free(codecpriv);

    return VAR_5;

}

static int FUNC_0(AVFormatContext *VAR_0, AVIOContext *VAR_1,
AVCodecParameters *VAR_2,
int VAR_3, int VAR_4)
{ AVIOContext *dyn_cp; uint8_t *codecpriv; int VAR_5, VAR_6; VAR_5 = avio_open_dyn_buf(&dyn_cp); if (VAR_5 < 0)
return VAR_5; if (VAR_3) { VAR_5 = mkv_write_native_codecprivate(VAR_0, VAR_2, dyn_cp); } else if (VAR_2->codec_type == AVMEDIA_TYPE_VIDEO) { if (VAR_4) { if (!VAR_2->codec_tag)
VAR_2->codec_tag = ff_codec_get_tag(ff_codec_movvideo_tags,
VAR_2->codec_id); if (   ff_codec_get_id(ff_codec_movvideo_tags, VAR_2->codec_tag) == VAR_2->codec_id
&& (!VAR_2->extradata_size || ff_codec_get_id(ff_codec_movvideo_tags, AV_RL32(VAR_2->extradata + 4)) != VAR_2->codec_id)
) { int VAR_7; avio_wb32(dyn_cp, 0x5a + VAR_2->extradata_size); avio_wl32(dyn_cp, VAR_2->codec_tag); for(VAR_7 = 0; VAR_7 < 0x5a - 8; VAR_7++) avio_w8(dyn_cp, 0); } avio_write(dyn_cp, VAR_2->extradata, VAR_2->extradata_size); } else { if (!ff_codec_get_tag(ff_codec_bmp_tags, VAR_2->codec_id))
av_log(VAR_0, AV_LOG_WARNING, "codec %VAR_0 is not supported by this format\n",
avcodec_get_name(VAR_2->codec_id)); if (!VAR_2->codec_tag)
VAR_2->codec_tag = ff_codec_get_tag(ff_codec_bmp_tags,
VAR_2->codec_id); if (!VAR_2->codec_tag && VAR_2->codec_id != AV_CODEC_ID_RAWVIDEO) { av_log(VAR_0, AV_LOG_ERROR, "No bmp codec VAR_8 found for codec %VAR_0\n",
avcodec_get_name(VAR_2->codec_id)); VAR_5 = AVERROR(EINVAL); } ff_put_bmp_header(dyn_cp, VAR_2, ff_codec_bmp_tags, 0, 0); } } else if (VAR_2->codec_type == AVMEDIA_TYPE_AUDIO) { unsigned int VAR_8; VAR_8 = ff_codec_get_tag(ff_codec_wav_tags, VAR_2->codec_id); if (!VAR_8) { av_log(VAR_0, AV_LOG_ERROR, "No wav codec VAR_8 found for codec %VAR_0\n",
avcodec_get_name(VAR_2->codec_id)); VAR_5 = AVERROR(EINVAL); } if (!VAR_2->codec_tag)
VAR_2->codec_tag = VAR_8; ff_put_wav_header(VAR_0, dyn_cp, VAR_2, FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX); } VAR_6 = avio_close_dyn_buf(dyn_cp, &codecpriv); if (VAR_6)
put_ebml_binary(VAR_1, MATROSKA_ID_CODECPRIVATE, codecpriv,
VAR_6); av_free(codecpriv); return VAR_5; }