int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
{
    int ret;

    if(s->maxsize>0){
        int64_t remaining= s->maxsize - avio_tell(s);
        if(remaining>=0)
            size= FFMIN(size, remaining);
    }

    ret= av_new_packet(pkt, size);

    if(ret<0)
        return ret;

    pkt->pos= avio_tell(s);

    ret= avio_read(s, pkt->data, size);
    if(ret<=0)
        av_free_packet(pkt);
    else
        av_shrink_packet(pkt, ret);

    return ret;
}