static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)

{

    AVIContext *avi = s->priv_data;

    ByteIOContext *pb = &s->pb;

    int n, d[8], size;

    offset_t i;

    void* dstr;



    memset(d, -1, sizeof(int)*8);

   

    if (avi->dv_demux) {

        size = dv_get_packet(avi->dv_demux, pkt);

	if (size >= 0)

	    return size;

    }

        

    for(i=url_ftell(pb); !url_feof(pb); i++) {

        int j;



	if (i >= avi->movi_end) {

	    if (avi->is_odml) {

		url_fskip(pb, avi->riff_end - i);

	        avi->riff_end = avi->movi_end = url_filesize(url_fileno(pb));

	    } else

	        break;

	}



        for(j=0; j<7; j++)

            d[j]= d[j+1];

        d[7]= get_byte(pb);

        

        size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24);

        

        //parse ix##

        n= (d[2] - '0') * 10 + (d[3] - '0');

        if(    d[2] >= '0' && d[2] <= '9'

            && d[3] >= '0' && d[3] <= '9'

            && d[0] == 'i' && d[1] == 'x'

            && n < s->nb_streams

            && i + size <= avi->movi_end){

            

            url_fskip(pb, size);

        }



	//parse JUNK

        if(d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K' &&

           i + size <= avi->movi_end) {

            

            url_fskip(pb, size);

        }

        

        //parse ##dc/##wb

        n= (d[0] - '0') * 10 + (d[1] - '0');

        if(    d[0] >= '0' && d[0] <= '9'

            && d[1] >= '0' && d[1] <= '9'

            && ((d[2] == 'd' && d[3] == 'c') || 

	        (d[2] == 'w' && d[3] == 'b') || 

		(d[2] == 'd' && d[3] == 'b') ||

		(d[2] == '_' && d[3] == '_'))

            && n < s->nb_streams

            && i + size <= avi->movi_end) {

        

            av_new_packet(pkt, size);

            get_buffer(pb, pkt->data, size);

            if (size & 1) {

                get_byte(pb);

		size++;

	    }

	

	    if (avi->dv_demux) {

	        dstr = pkt->destruct;

	        size = dv_produce_packet(avi->dv_demux, pkt,

		                         pkt->data, pkt->size);

		pkt->destruct = dstr;

                pkt->flags |= PKT_FLAG_KEY;

	    } else {

                AVStream *st;

                AVIStream *ast;

                st = s->streams[n];

                ast = st->priv_data;

                

                /* XXX: how to handle B frames in avi ? */

                pkt->dts = ast->frame_offset;

//                pkt->dts += ast->start;

                if(ast->sample_size)

                    pkt->dts /= ast->sample_size;

//av_log(NULL, AV_LOG_DEBUG, "dts:%Ld offset:%d %d/%d smpl_siz:%d base:%d st:%d size:%d\n", pkt->dts, ast->frame_offset, ast->scale, ast->rate, ast->sample_size, AV_TIME_BASE, n, size);

                pkt->stream_index = n;

                /* FIXME: We really should read index for that */

                if (st->codec.codec_type == CODEC_TYPE_VIDEO) {

                    if (ast->frame_offset < ast->nb_index_entries) {

                        if (ast->index_entries[ast->frame_offset].flags & AVIIF_INDEX)

                            pkt->flags |= PKT_FLAG_KEY; 

                    } else {

                        /* if no index, better to say that all frames

                           are key frames */

                        pkt->flags |= PKT_FLAG_KEY;

                    }

                } else {

                    pkt->flags |= PKT_FLAG_KEY; 

                }

                if(ast->sample_size)

                    ast->frame_offset += pkt->size;

                else

                    ast->frame_offset++;

	    }

            return size;

        }

    }

    return -1;

}

static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)

{

    AVIContext *avi = s->priv_data;

    ByteIOContext *pb = &s->pb;

    int n, d[8], size;

    offset_t i;

    void* dstr;



    memset(d, -1, sizeof(int)*8);

   

    if (avi->dv_demux) {

        size = dv_get_packet(avi->dv_demux, pkt);

	if (size >= 0)

	    return size;

    }

        

    for(i=url_ftell(pb); !url_feof(pb); i++) {

        int j;



	if (i >= avi->movi_end) {

	    if (avi->is_odml) {

		url_fskip(pb, avi->riff_end - i);

	        avi->riff_end = avi->movi_end = url_filesize(url_fileno(pb));

	    } else

	        break;

	}



        for(j=0; j<7; j++)

            d[j]= d[j+1];

        d[7]= get_byte(pb);

        

        size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24);

        

        

        n= (d[2] - '0') * 10 + (d[3] - '0');

        if(    d[2] >= '0' && d[2] <= '9'

            && d[3] >= '0' && d[3] <= '9'

            && d[0] == 'i' && d[1] == 'x'

            && n < s->nb_streams

            && i + size <= avi->movi_end){

            

            url_fskip(pb, size);

        }



	

        if(d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K' &&

           i + size <= avi->movi_end) {

            

            url_fskip(pb, size);

        }

        

        

        n= (d[0] - '0') * 10 + (d[1] - '0');

        if(    d[0] >= '0' && d[0] <= '9'

            && d[1] >= '0' && d[1] <= '9'

            && ((d[2] == 'd' && d[3] == 'c') || 

	        (d[2] == 'w' && d[3] == 'b') || 

		(d[2] == 'd' && d[3] == 'b') ||

		(d[2] == '_' && d[3] == '_'))

            && n < s->nb_streams

            && i + size <= avi->movi_end) {

        

            av_new_packet(pkt, size);

            get_buffer(pb, pkt->data, size);

            if (size & 1) {

                get_byte(pb);

		size++;

	    }

	

	    if (avi->dv_demux) {

	        dstr = pkt->destruct;

	        size = dv_produce_packet(avi->dv_demux, pkt,

		                         pkt->data, pkt->size);

		pkt->destruct = dstr;

                pkt->flags |= PKT_FLAG_KEY;

	    } else {

                AVStream *st;

                AVIStream *ast;

                st = s->streams[n];

                ast = st->priv_data;

                

                

                pkt->dts = ast->frame_offset;



                if(ast->sample_size)

                    pkt->dts /= ast->sample_size;



                pkt->stream_index = n;

                

                if (st->codec.codec_type == CODEC_TYPE_VIDEO) {

                    if (ast->frame_offset < ast->nb_index_entries) {

                        if (ast->index_entries[ast->frame_offset].flags & AVIIF_INDEX)

                            pkt->flags |= PKT_FLAG_KEY; 

                    } else {

                        



                        pkt->flags |= PKT_FLAG_KEY;

                    }

                } else {

                    pkt->flags |= PKT_FLAG_KEY; 

                }

                if(ast->sample_size)

                    ast->frame_offset += pkt->size;

                else

                    ast->frame_offset++;

	    }

            return size;

        }

    }

    return -1;

}

static int FUNC_0(AVFormatContext *VAR_0, AVPacket *VAR_1)

{

    AVIContext *avi = VAR_0->priv_data;

    ByteIOContext *pb = &VAR_0->pb;

    int VAR_2, VAR_3[8], VAR_4;

    offset_t i;

    void* VAR_5;



    memset(VAR_3, -1, sizeof(int)*8);

   

    if (avi->dv_demux) {

        VAR_4 = dv_get_packet(avi->dv_demux, VAR_1);

	if (VAR_4 >= 0)

	    return VAR_4;

    }

        

    for(i=url_ftell(pb); !url_feof(pb); i++) {

        int j;



	if (i >= avi->movi_end) {

	    if (avi->is_odml) {

		url_fskip(pb, avi->riff_end - i);

	        avi->riff_end = avi->movi_end = url_filesize(url_fileno(pb));

	    } else

	        break;

	}



        for(j=0; j<7; j++)

            VAR_3[j]= VAR_3[j+1];

        VAR_3[7]= get_byte(pb);

        

        VAR_4= VAR_3[4] + (VAR_3[5]<<8) + (VAR_3[6]<<16) + (VAR_3[7]<<24);

        

        

        VAR_2= (VAR_3[2] - '0') * 10 + (VAR_3[3] - '0');

        if(    VAR_3[2] >= '0' && VAR_3[2] <= '9'

            && VAR_3[3] >= '0' && VAR_3[3] <= '9'

            && VAR_3[0] == 'i' && VAR_3[1] == 'x'

            && VAR_2 < VAR_0->nb_streams

            && i + VAR_4 <= avi->movi_end){

            

            url_fskip(pb, VAR_4);

        }



	

        if(VAR_3[0] == 'J' && VAR_3[1] == 'U' && VAR_3[2] == 'N' && VAR_3[3] == 'K' &&

           i + VAR_4 <= avi->movi_end) {

            

            url_fskip(pb, VAR_4);

        }

        

        

        VAR_2= (VAR_3[0] - '0') * 10 + (VAR_3[1] - '0');

        if(    VAR_3[0] >= '0' && VAR_3[0] <= '9'

            && VAR_3[1] >= '0' && VAR_3[1] <= '9'

            && ((VAR_3[2] == 'VAR_3' && VAR_3[3] == 'c') || 

	        (VAR_3[2] == 'w' && VAR_3[3] == 'b') || 

		(VAR_3[2] == 'VAR_3' && VAR_3[3] == 'b') ||

		(VAR_3[2] == '_' && VAR_3[3] == '_'))

            && VAR_2 < VAR_0->nb_streams

            && i + VAR_4 <= avi->movi_end) {

        

            av_new_packet(VAR_1, VAR_4);

            get_buffer(pb, VAR_1->data, VAR_4);

            if (VAR_4 & 1) {

                get_byte(pb);

		VAR_4++;

	    }

	

	    if (avi->dv_demux) {

	        VAR_5 = VAR_1->destruct;

	        VAR_4 = dv_produce_packet(avi->dv_demux, VAR_1,

		                         VAR_1->data, VAR_1->VAR_4);

		VAR_1->destruct = VAR_5;

                VAR_1->flags |= PKT_FLAG_KEY;

	    } else {

                AVStream *st;

                AVIStream *ast;

                st = VAR_0->streams[VAR_2];

                ast = st->priv_data;

                

                

                VAR_1->dts = ast->frame_offset;



                if(ast->sample_size)

                    VAR_1->dts /= ast->sample_size;



                VAR_1->stream_index = VAR_2;

                

                if (st->codec.codec_type == CODEC_TYPE_VIDEO) {

                    if (ast->frame_offset < ast->nb_index_entries) {

                        if (ast->index_entries[ast->frame_offset].flags & AVIIF_INDEX)

                            VAR_1->flags |= PKT_FLAG_KEY; 

                    } else {

                        



                        VAR_1->flags |= PKT_FLAG_KEY;

                    }

                } else {

                    VAR_1->flags |= PKT_FLAG_KEY; 

                }

                if(ast->sample_size)

                    ast->frame_offset += VAR_1->VAR_4;

                else

                    ast->frame_offset++;

	    }

            return VAR_4;

        }

    }

    return -1;

}

static int FUNC_0(AVFormatContext *VAR_0, AVPacket *VAR_1)
{ AVIContext *avi = VAR_0->priv_data; ByteIOContext *pb = &VAR_0->pb; int VAR_2, VAR_3[8], VAR_4; offset_t i; void* VAR_5; memset(VAR_3, -1, sizeof(int)*8); if (avi->dv_demux) { VAR_4 = dv_get_packet(avi->dv_demux, VAR_1); if (VAR_4 >= 0)
return VAR_4; } for(i=url_ftell(pb); !url_feof(pb); i++) { int j; if (i >= avi->movi_end) { if (avi->is_odml) { url_fskip(pb, avi->riff_end - i); avi->riff_end = avi->movi_end = url_filesize(url_fileno(pb)); } else break; } for(j=0; j<7; j++) VAR_3[j]= VAR_3[j+1]; VAR_3[7]= get_byte(pb); VAR_4= VAR_3[4] + (VAR_3[5]<<8) + (VAR_3[6]<<16) + (VAR_3[7]<<24); VAR_2= (VAR_3[2] - '0') * 10 + (VAR_3[3] - '0'); if(    VAR_3[2] >= '0' && VAR_3[2] <= '9'
&& VAR_3[3] >= '0' && VAR_3[3] <= '9'
&& VAR_3[0] == 'i' && VAR_3[1] == 'x'
&& VAR_2 < VAR_0->nb_streams
&& i + VAR_4 <= avi->movi_end){ url_fskip(pb, VAR_4); } if(VAR_3[0] == 'J' && VAR_3[1] == 'U' && VAR_3[2] == 'N' && VAR_3[3] == 'K' &&
i + VAR_4 <= avi->movi_end) { url_fskip(pb, VAR_4); } VAR_2= (VAR_3[0] - '0') * 10 + (VAR_3[1] - '0'); if(    VAR_3[0] >= '0' && VAR_3[0] <= '9'
&& VAR_3[1] >= '0' && VAR_3[1] <= '9'
&& ((VAR_3[2] == 'VAR_3' && VAR_3[3] == 'c') ||
(VAR_3[2] == 'w' && VAR_3[3] == 'b') ||
(VAR_3[2] == 'VAR_3' && VAR_3[3] == 'b') ||
(VAR_3[2] == '_' && VAR_3[3] == '_'))
&& VAR_2 < VAR_0->nb_streams
&& i + VAR_4 <= avi->movi_end) { av_new_packet(VAR_1, VAR_4); get_buffer(pb, VAR_1->data, VAR_4); if (VAR_4 & 1) { get_byte(pb); VAR_4++; } if (avi->dv_demux) { VAR_5 = VAR_1->destruct; VAR_4 = dv_produce_packet(avi->dv_demux, VAR_1,
VAR_1->data, VAR_1->VAR_4); VAR_1->destruct = VAR_5; VAR_1->flags |= PKT_FLAG_KEY; } else { AVStream *st; AVIStream *ast; st = VAR_0->streams[VAR_2]; ast = st->priv_data; VAR_1->dts = ast->frame_offset; if(ast->sample_size)
VAR_1->dts /= ast->sample_size; VAR_1->stream_index = VAR_2; if (st->codec.codec_type == CODEC_TYPE_VIDEO) { if (ast->frame_offset < ast->nb_index_entries) { if (ast->index_entries[ast->frame_offset].flags & AVIIF_INDEX)
VAR_1->flags |= PKT_FLAG_KEY; } else { VAR_1->flags |= PKT_FLAG_KEY; } } else { VAR_1->flags |= PKT_FLAG_KEY; } if(ast->sample_size)
ast->frame_offset += VAR_1->VAR_4; else
ast->frame_offset++; } return VAR_4; } } return -1; }