- av_packet_unref(&ipkt);
- int ret = av_read_frame(fmt_ctx, &ipkt);
- if( ret == AVERROR_EOF ) break;
- if( ret != 0 ) continue;
- if( !ipkt.data ) continue;
- if( ipkt.stream_index != st->index ) continue;
- while( ipkt.size > 0 ) {
- int got_frame = 0;
- ret = avcodec_decode_video2(st->codec, ipic, &got_frame, &ipkt);
- if( ret <= 0 ) break;
- if( got_frame )
- return ipic;
- ipkt.data += ret;
- ipkt.size -= ret;
+ if( need_packet ) {
+ if( eof ) return 0;
+ AVPacket *pkt = &ipkt;
+ av_packet_unref(pkt);
+ int ret = av_read_frame(fmt_ctx, pkt);
+ if( ret < 0 ) {
+ if( ret != AVERROR_EOF ) return 0;
+ ret = 0; eof = 1; pkt = 0;
+ }
+ if( pkt ) {
+ if( pkt->stream_index != st->index ) continue;
+ if( !pkt->data || !pkt->size ) continue;
+ }
+ avcodec_send_packet(ctx, pkt);
+ need_packet = 0;