int av_stream_add_side_data(AVStream *st, enum AVPacketSideDataType type,

                            uint8_t *data, size_t size)

{

    AVPacketSideData *sd, *tmp;

    int i;



    for (i = 0; i < st->nb_side_data; i++) {

        sd = &st->side_data[i];



        if (sd->type == type) {

            av_freep(&sd->data);

            sd->data = data;

            sd->size = size;

            return 0;

        }

    }



    tmp = av_realloc_array(st->side_data, st->nb_side_data + 1, sizeof(*tmp));

    if (!tmp) {

        return AVERROR(ENOMEM);

    }



    st->side_data = tmp;

    st->nb_side_data++;



    sd = &st->side_data[st->nb_side_data - 1];

    sd->type = type;

    sd->data = data;

    sd->size = size;



    return 0;

}
