break;
int av_find_stream_info(AVFormatContext *ic){    int i, count, ret, got_picture, size, read_size;    AVCodec *codec;    AVStream *st;    AVPacket *pkt;    AVPicture picture;    AVPacketList *pktl=NULL, **ppktl;    short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];    UINT8 *ptr;    int min_read_size, max_read_size;    /* typical mpeg ts rate is 40 Mbits. DVD rate is about 10       Mbits. We read at most 0.1 second of file to find all streams */    /* XXX: base it on stream bitrate when possible */    if (ic->iformat == &mpegts_demux) {        /* maximum number of bytes we accept to read to find all the streams           in a file */        min_read_size = 3000000;    } else {        min_read_size = 125000;    }    /* max read size is 2 seconds of video max */    max_read_size = min_read_size * 20;    /* set initial codec state */    for(i=0;i<ic->nb_streams;i++) {        st = ic->streams[i];        if (has_codec_parameters(&st->codec))            st->codec_info_state = CSTATE_FOUND;        else            st->codec_info_state = CSTATE_NOTFOUND;        st->codec_info_nb_repeat_frames = 0;        st->codec_info_nb_real_frames = 0;    }    count = 0;    read_size = 0;    ppktl = &ic->packet_buffer;    for(;;) {        /* check if one codec still needs to be handled */        for(i=0;i<ic->nb_streams;i++) {            st = ic->streams[i];            if (st->codec_info_state != CSTATE_FOUND)                break;        }        if (i == ic->nb_streams) {            /* NOTE: if the format has no header, then we need to read               some packets to get most of the streams, so we cannot               stop here */            if (!(ic->iformat->flags & AVFMT_NOHEADER) ||                read_size >= min_read_size) {                /* if we found the info for all the codecs, we can stop */                ret = count;                break;            }        } else {            /* we did not get all the codec info, but we read too much data */            if (read_size >= max_read_size) {                ret = count;                break;            }        }        pktl = av_mallocz(sizeof(AVPacketList));        if (!pktl) {            ret = AVERROR_NOMEM;            break;        }        /* add the packet in the buffered packet list */        *ppktl = pktl;        ppktl = &pktl->next;        /* NOTE: a new stream can be added there if no header in file           (AVFMT_NOHEADER) */        pkt = &pktl->pkt;        if (ic->iformat->read_packet(ic, pkt) < 0) {            /* EOF or error */            ret = -1; /* we could not have all the codec parameters before EOF */            if ((ic->iformat->flags & AVFMT_NOHEADER) &&                i == ic->nb_streams)                ret = 0;            break;        }        read_size += pkt->size;        /* open new codecs */        for(i=0;i<ic->nb_streams;i++) {            st = ic->streams[i];            if (st->codec_info_state == CSTATE_NOTFOUND) {                /* set to found in case of error */                st->codec_info_state = CSTATE_FOUND;                 codec = avcodec_find_decoder(st->codec.codec_id);                if (codec) {                    ret = avcodec_open(&st->codec, codec);                    if (ret >= 0)                        st->codec_info_state = CSTATE_DECODING;                }            }        }        st = ic->streams[pkt->stream_index];        if (st->codec_info_state == CSTATE_DECODING) {            /* decode the data and update codec parameters */            ptr = pkt->data;            size = pkt->size;            while (size > 0) {                switch(st->codec.codec_type) {                case CODEC_TYPE_VIDEO:                    ret = avcodec_decode_video(&st->codec, &picture,                                                &got_picture, ptr, size);                    break;                case CODEC_TYPE_AUDIO:                    ret = avcodec_decode_audio(&st->codec, samples,                                                &got_picture, ptr, size);                    break;                default:                    ret = -1;                    break;                }                if (ret < 0) {                    /* if error, simply ignore because another packet                       may be OK */                    break;                }                if (got_picture) {                    /* we got the parameters - now we can stop                       examining this stream */                    /* XXX: add a codec info so that we can decide if                       the codec can repeat frames */                    if (st->codec.codec_id == CODEC_ID_MPEG1VIDEO &&                         ic->iformat != &mpegts_demux &&                        st->codec.sub_id == 2) {                        /* for mpeg2 video, we want to know the real                           frame rate, so we decode 40 frames. In mpeg                           TS case we do not do it because it would be                           too long */                        st->codec_info_nb_real_frames++;                        st->codec_info_nb_repeat_frames += st->codec.repeat_pict;#if 0                        /* XXX: testing */                        if ((st->codec_info_nb_real_frames % 24) == 23) {                            st->codec_info_nb_repeat_frames += 2;                        }#endif                        /* stop after 40 frames */                        if (st->codec_info_nb_real_frames >= 40) {                            st->r_frame_rate = (st->codec.frame_rate *                                                 st->codec_info_nb_real_frames) /                                (st->codec_info_nb_real_frames +                                  (st->codec_info_nb_repeat_frames >> 1));                            goto close_codec;                        }                    } else {                    close_codec:                        st->codec_info_state = CSTATE_FOUND;                        avcodec_close(&st->codec);                    }                    break;                }                ptr += ret;                size -= ret;            }        }        count++;    }    /* close each codec if there are opened */    for(i=0;i<ic->nb_streams;i++) {        st = ic->streams[i];        if (st->codec_info_state == CSTATE_DECODING)            avcodec_close(&st->codec);    }    /* set real frame rate info */    for(i=0;i<ic->nb_streams;i++) {        st = ic->streams[i];        if (st->codec.codec_type == CODEC_TYPE_VIDEO) {            if (!st->r_frame_rate)                st->r_frame_rate = st->codec.frame_rate;        }    }    return ret;}