static int rm_assemble_video_frame(AVFormatContext *s, RMContext *rm, AVPacket *pkt, int len)

{

    ByteIOContext *pb = &s->pb;

    int hdr, seq, pic_num, len2, pos;

    int type;

    int ssize;



    hdr = get_byte(pb); len--;

    type = hdr >> 6;

    switch(type){

    case 0: // slice

    case 2: // last slice

        seq = get_byte(pb); len--;

        len2 = get_num(pb, &len);

        pos = get_num(pb, &len);

        pic_num = get_byte(pb); len--;

        rm->remaining_len = len;

        break;

    case 1: //whole frame

        seq = get_byte(pb); len--;

        if(av_new_packet(pkt, len + 9) < 0)

            return AVERROR(EIO);

        pkt->data[0] = 0;

        AV_WL32(pkt->data + 1, 1);

        AV_WL32(pkt->data + 5, 0);

        get_buffer(pb, pkt->data + 9, len);

        rm->remaining_len = 0;

        return 0;

    case 3: //frame as a part of packet

        len2 = get_num(pb, &len);

        pos = get_num(pb, &len);

        pic_num = get_byte(pb); len--;

        rm->remaining_len = len - len2;

        if(av_new_packet(pkt, len2 + 9) < 0)

            return AVERROR(EIO);

        pkt->data[0] = 0;

        AV_WL32(pkt->data + 1, 1);

        AV_WL32(pkt->data + 5, 0);

        get_buffer(pb, pkt->data + 9, len2);

        return 0;

    }

    //now we have to deal with single slice



    if((seq & 0x7F) == 1 || rm->curpic_num != pic_num){

        rm->slices = ((hdr & 0x3F) << 1) + 1;

        ssize = len2 + 8*rm->slices + 1;

        rm->videobuf = av_realloc(rm->videobuf, ssize);

        rm->videobufsize = ssize;

        rm->videobufpos = 8*rm->slices + 1;

        rm->cur_slice = 0;

        rm->curpic_num = pic_num;

        rm->pktpos = url_ftell(pb);

    }

    if(type == 2){

        len = FFMIN(len, pos);

        pos = len2 - pos;

    }



    if(++rm->cur_slice > rm->slices)

        return 1;

    AV_WL32(rm->videobuf - 7 + 8*rm->cur_slice, 1);

    AV_WL32(rm->videobuf - 3 + 8*rm->cur_slice, rm->videobufpos - 8*rm->slices - 1);

    if(rm->videobufpos + len > rm->videobufsize)

        return 1;

    if (get_buffer(pb, rm->videobuf + rm->videobufpos, len) != len)

        return AVERROR(EIO);

    rm->videobufpos += len,

    rm->remaining_len-= len;



    if(type == 2 || (rm->videobufpos) == rm->videobufsize){

         rm->videobuf[0] = rm->cur_slice-1;

         if(av_new_packet(pkt, rm->videobufpos - 8*(rm->slices - rm->cur_slice)) < 0)

             return AVERROR(ENOMEM);

         memcpy(pkt->data, rm->videobuf, 1 + 8*rm->cur_slice);

         memcpy(pkt->data + 1 + 8*rm->cur_slice, rm->videobuf + 1 + 8*rm->slices, rm->videobufpos - 1 - 8*rm->slices);

         pkt->pts = AV_NOPTS_VALUE;

         pkt->pos = rm->pktpos;

         return 0;

    }



    return 1;

}

static int rm_assemble_video_frame(AVFormatContext *s, RMContext *rm, AVPacket *pkt, int len)

{

    ByteIOContext *pb = &s->pb;

    int hdr, seq, pic_num, len2, pos;

    int type;

    int ssize;



    hdr = get_byte(pb); len--;

    type = hdr >> 6;

    switch(type){

    case 0: 

    case 2: 

        seq = get_byte(pb); len--;

        len2 = get_num(pb, &len);

        pos = get_num(pb, &len);

        pic_num = get_byte(pb); len--;

        rm->remaining_len = len;

        break;

    case 1: 

        seq = get_byte(pb); len--;

        if(av_new_packet(pkt, len + 9) < 0)

            return AVERROR(EIO);

        pkt->data[0] = 0;

        AV_WL32(pkt->data + 1, 1);

        AV_WL32(pkt->data + 5, 0);

        get_buffer(pb, pkt->data + 9, len);

        rm->remaining_len = 0;

        return 0;

    case 3: 

        len2 = get_num(pb, &len);

        pos = get_num(pb, &len);

        pic_num = get_byte(pb); len--;

        rm->remaining_len = len - len2;

        if(av_new_packet(pkt, len2 + 9) < 0)

            return AVERROR(EIO);

        pkt->data[0] = 0;

        AV_WL32(pkt->data + 1, 1);

        AV_WL32(pkt->data + 5, 0);

        get_buffer(pb, pkt->data + 9, len2);

        return 0;

    }

    



    if((seq & 0x7F) == 1 || rm->curpic_num != pic_num){

        rm->slices = ((hdr & 0x3F) << 1) + 1;

        ssize = len2 + 8*rm->slices + 1;

        rm->videobuf = av_realloc(rm->videobuf, ssize);

        rm->videobufsize = ssize;

        rm->videobufpos = 8*rm->slices + 1;

        rm->cur_slice = 0;

        rm->curpic_num = pic_num;

        rm->pktpos = url_ftell(pb);

    }

    if(type == 2){

        len = FFMIN(len, pos);

        pos = len2 - pos;

    }



    if(++rm->cur_slice > rm->slices)

        return 1;

    AV_WL32(rm->videobuf - 7 + 8*rm->cur_slice, 1);

    AV_WL32(rm->videobuf - 3 + 8*rm->cur_slice, rm->videobufpos - 8*rm->slices - 1);

    if(rm->videobufpos + len > rm->videobufsize)

        return 1;

    if (get_buffer(pb, rm->videobuf + rm->videobufpos, len) != len)

        return AVERROR(EIO);

    rm->videobufpos += len,

    rm->remaining_len-= len;



    if(type == 2 || (rm->videobufpos) == rm->videobufsize){

         rm->videobuf[0] = rm->cur_slice-1;

         if(av_new_packet(pkt, rm->videobufpos - 8*(rm->slices - rm->cur_slice)) < 0)

             return AVERROR(ENOMEM);

         memcpy(pkt->data, rm->videobuf, 1 + 8*rm->cur_slice);

         memcpy(pkt->data + 1 + 8*rm->cur_slice, rm->videobuf + 1 + 8*rm->slices, rm->videobufpos - 1 - 8*rm->slices);

         pkt->pts = AV_NOPTS_VALUE;

         pkt->pos = rm->pktpos;

         return 0;

    }



    return 1;

}

        rm->videobuf = av_realloc(rm->videobuf, ssize);
static int FUNC_0(AVFormatContext *VAR_0, RMContext *VAR_1, AVPacket *VAR_2, int VAR_3)

{

    ByteIOContext *pb = &VAR_0->pb;

    int VAR_4, VAR_5, VAR_6, VAR_7, VAR_8;

    int VAR_9;

    int VAR_10;



    VAR_4 = get_byte(pb); VAR_3--;

    VAR_9 = VAR_4 >> 6;

    switch(VAR_9){

    case 0: 

    case 2: 

        VAR_5 = get_byte(pb); VAR_3--;

        VAR_7 = get_num(pb, &VAR_3);

        VAR_8 = get_num(pb, &VAR_3);

        VAR_6 = get_byte(pb); VAR_3--;

        VAR_1->remaining_len = VAR_3;

        break;

    case 1: 

        VAR_5 = get_byte(pb); VAR_3--;

        if(av_new_packet(VAR_2, VAR_3 + 9) < 0)

            return AVERROR(EIO);

        VAR_2->data[0] = 0;

        AV_WL32(VAR_2->data + 1, 1);

        AV_WL32(VAR_2->data + 5, 0);

        get_buffer(pb, VAR_2->data + 9, VAR_3);

        VAR_1->remaining_len = 0;

        return 0;

    case 3: 

        VAR_7 = get_num(pb, &VAR_3);

        VAR_8 = get_num(pb, &VAR_3);

        VAR_6 = get_byte(pb); VAR_3--;

        VAR_1->remaining_len = VAR_3 - VAR_7;

        if(av_new_packet(VAR_2, VAR_7 + 9) < 0)

            return AVERROR(EIO);

        VAR_2->data[0] = 0;

        AV_WL32(VAR_2->data + 1, 1);

        AV_WL32(VAR_2->data + 5, 0);

        get_buffer(pb, VAR_2->data + 9, VAR_7);

        return 0;

    }

    



    if((VAR_5 & 0x7F) == 1 || VAR_1->curpic_num != VAR_6){

        VAR_1->slices = ((VAR_4 & 0x3F) << 1) + 1;

        VAR_10 = VAR_7 + 8*VAR_1->slices + 1;

        VAR_1->videobuf = av_realloc(VAR_1->videobuf, VAR_10);

        VAR_1->videobufsize = VAR_10;

        VAR_1->videobufpos = 8*VAR_1->slices + 1;

        VAR_1->cur_slice = 0;

        VAR_1->curpic_num = VAR_6;

        VAR_1->pktpos = url_ftell(pb);

    }

    if(VAR_9 == 2){

        VAR_3 = FFMIN(VAR_3, VAR_8);

        VAR_8 = VAR_7 - VAR_8;

    }



    if(++VAR_1->cur_slice > VAR_1->slices)

        return 1;

    AV_WL32(VAR_1->videobuf - 7 + 8*VAR_1->cur_slice, 1);

    AV_WL32(VAR_1->videobuf - 3 + 8*VAR_1->cur_slice, VAR_1->videobufpos - 8*VAR_1->slices - 1);

    if(VAR_1->videobufpos + VAR_3 > VAR_1->videobufsize)

        return 1;

    if (get_buffer(pb, VAR_1->videobuf + VAR_1->videobufpos, VAR_3) != VAR_3)

        return AVERROR(EIO);

    VAR_1->videobufpos += VAR_3,

    VAR_1->remaining_len-= VAR_3;



    if(VAR_9 == 2 || (VAR_1->videobufpos) == VAR_1->videobufsize){

         VAR_1->videobuf[0] = VAR_1->cur_slice-1;

         if(av_new_packet(VAR_2, VAR_1->videobufpos - 8*(VAR_1->slices - VAR_1->cur_slice)) < 0)

             return AVERROR(ENOMEM);

         memcpy(VAR_2->data, VAR_1->videobuf, 1 + 8*VAR_1->cur_slice);

         memcpy(VAR_2->data + 1 + 8*VAR_1->cur_slice, VAR_1->videobuf + 1 + 8*VAR_1->slices, VAR_1->videobufpos - 1 - 8*VAR_1->slices);

         VAR_2->pts = AV_NOPTS_VALUE;

         VAR_2->VAR_8 = VAR_1->pktpos;

         return 0;

    }



    return 1;

}

static int FUNC_0(AVFormatContext *VAR_0, RMContext *VAR_1, AVPacket *VAR_2, int VAR_3)
{ ByteIOContext *pb = &VAR_0->pb; int VAR_4, VAR_5, VAR_6, VAR_7, VAR_8; int VAR_9; int VAR_10; VAR_4 = get_byte(pb); VAR_3--; VAR_9 = VAR_4 >> 6; switch(VAR_9){ case 0:
case 2:
VAR_5 = get_byte(pb); VAR_3--; VAR_7 = get_num(pb, &VAR_3); VAR_8 = get_num(pb, &VAR_3); VAR_6 = get_byte(pb); VAR_3--; VAR_1->remaining_len = VAR_3; break; case 1:
VAR_5 = get_byte(pb); VAR_3--; if(av_new_packet(VAR_2, VAR_3 + 9) < 0)
return AVERROR(EIO); VAR_2->data[0] = 0; AV_WL32(VAR_2->data + 1, 1); AV_WL32(VAR_2->data + 5, 0); get_buffer(pb, VAR_2->data + 9, VAR_3); VAR_1->remaining_len = 0; return 0; case 3:
VAR_7 = get_num(pb, &VAR_3); VAR_8 = get_num(pb, &VAR_3); VAR_6 = get_byte(pb); VAR_3--; VAR_1->remaining_len = VAR_3 - VAR_7; if(av_new_packet(VAR_2, VAR_7 + 9) < 0)
return AVERROR(EIO); VAR_2->data[0] = 0; AV_WL32(VAR_2->data + 1, 1); AV_WL32(VAR_2->data + 5, 0); get_buffer(pb, VAR_2->data + 9, VAR_7); return 0; } if((VAR_5 & 0x7F) == 1 || VAR_1->curpic_num != VAR_6){ VAR_1->slices = ((VAR_4 & 0x3F) << 1) + 1; VAR_10 = VAR_7 + 8*VAR_1->slices + 1; VAR_1->videobuf = av_realloc(VAR_1->videobuf, VAR_10); VAR_1->videobufsize = VAR_10; VAR_1->videobufpos = 8*VAR_1->slices + 1; VAR_1->cur_slice = 0; VAR_1->curpic_num = VAR_6; VAR_1->pktpos = url_ftell(pb); } if(VAR_9 == 2){ VAR_3 = FFMIN(VAR_3, VAR_8); VAR_8 = VAR_7 - VAR_8; } if(++VAR_1->cur_slice > VAR_1->slices)
return 1; AV_WL32(VAR_1->videobuf - 7 + 8*VAR_1->cur_slice, 1); AV_WL32(VAR_1->videobuf - 3 + 8*VAR_1->cur_slice, VAR_1->videobufpos - 8*VAR_1->slices - 1); if(VAR_1->videobufpos + VAR_3 > VAR_1->videobufsize)
return 1; if (get_buffer(pb, VAR_1->videobuf + VAR_1->videobufpos, VAR_3) != VAR_3)
return AVERROR(EIO); VAR_1->videobufpos += VAR_3,
VAR_1->remaining_len-= VAR_3; if(VAR_9 == 2 || (VAR_1->videobufpos) == VAR_1->videobufsize){ VAR_1->videobuf[0] = VAR_1->cur_slice-1; if(av_new_packet(VAR_2, VAR_1->videobufpos - 8*(VAR_1->slices - VAR_1->cur_slice)) < 0)
return AVERROR(ENOMEM); memcpy(VAR_2->data, VAR_1->videobuf, 1 + 8*VAR_1->cur_slice); memcpy(VAR_2->data + 1 + 8*VAR_1->cur_slice, VAR_1->videobuf + 1 + 8*VAR_1->slices, VAR_1->videobufpos - 1 - 8*VAR_1->slices); VAR_2->pts = AV_NOPTS_VALUE; VAR_2->VAR_8 = VAR_1->pktpos; return 0; } return 1; }