int ff_h261_handle_packet(AVFormatContext *ctx, PayloadContext *data,

                          AVStream *st, AVPacket *pkt, uint32_t *timestamp,

                          const uint8_t *buf, int len, uint16_t seq, int flags)

{

    int sbit, ebit, gobn, mbap, quant;

    int res;



    //av_log(ctx, AV_LOG_DEBUG, "got h261 RTP packet with time: %u\n", timestamp);



    /* drop data of previous packets in case of non-continuous (loss) packet stream */

    if (data->buf && data->timestamp != *timestamp) {

        h261_free_dyn_buffer(&data->buf);

    }



    /* sanity check for size of input packet */

    if (len < 5 /* 4 bytes header and 1 byte payload at least */) {

        av_log(ctx, AV_LOG_ERROR, "Too short H.261 RTP packet\n");

        return AVERROR_INVALIDDATA;

    }



    /*

      decode the H.261 payload header according to section 4.1 of RFC 4587:

      (uses 4 bytes between RTP header and H.261 stream per packet)



         0                   1                   2                   3

         0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1

        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

        |SBIT |EBIT |I|V| GOBN  |   MBAP  |  QUANT  |  HMVD   |  VMVD   |

        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+



           Start bit position (SBIT): 3 bits

           End bit position (EBIT): 3 bits

           INTRA-frame encoded data (I): 1 bit

           Motion Vector flag (V): 1 bit

           GOB number (GOBN): 4 bits

           Macroblock address predictor (MBAP): 5 bits

           Quantizer (QUANT): 5 bits

           Horizontal motion vector data (HMVD): 5 bits

           Vertical motion vector data (VMVD): 5 bits



    */

    sbit  =  (buf[0] >> 5) & 0x07;

    ebit  =  (buf[0] >> 2) & 0x07;

    gobn  =  (buf[1] >> 4) & 0x0f;

    mbap  = ((buf[1] << 1) & 0x1e) | ((buf[1] >> 7) & 0x01);

    quant =  (buf[1] >> 4) & 0x0f;



    /* pass the H.261 payload header and continue with the actual payload */

    buf += RTP_H261_PAYLOAD_HEADER_SIZE;

    len -= RTP_H261_PAYLOAD_HEADER_SIZE;



    /* start frame buffering with new dynamic buffer */

    if (!data->buf) {

        /* sanity check: a new frame starts with gobn=0, sbit=0, mbap=0, uqnat=0 */

        if (!gobn  && !sbit && !mbap && !quant){

            res = avio_open_dyn_buf(&data->buf);

            if (res < 0)

                return res;

            /* update the timestamp in the frame packet with the one from the RTP packet */

            data->timestamp = *timestamp;

        } else {

            /* frame not started yet, need more packets */

            return AVERROR(EAGAIN);

        }

    }



    /* do the "byte merging" at the boundaries of two consecutive frame fragments */

    if (data->endbyte_bits || sbit) {

        if (data->endbyte_bits == sbit) {

            data->endbyte |= buf[0] & (0xff >> sbit);

            data->endbyte_bits = 0;

            buf++;

            len--;

            avio_w8(data->buf, data->endbyte);

        } else {

            /* ebit/sbit values inconsistent, assuming packet loss */

            GetBitContext gb;

            init_get_bits(&gb, buf, len*8 - ebit);

            skip_bits(&gb, sbit);

            if (data->endbyte_bits) {

                data->endbyte |= get_bits(&gb, 8 - data->endbyte_bits);

                avio_w8(data->buf, data->endbyte);

            }

            while (get_bits_left(&gb) >= 8)

                avio_w8(data->buf, get_bits(&gb, 8));

            data->endbyte_bits = get_bits_left(&gb);

            if (data->endbyte_bits)

                data->endbyte = get_bits(&gb, data->endbyte_bits) <<

                                (8 - data->endbyte_bits);

            ebit = 0;

            len = 0;

        }

    }

    if (ebit) {

        if (len > 0)

            avio_write(data->buf, buf, len - 1);

        data->endbyte_bits = 8 - ebit;

        data->endbyte = buf[len - 1] & (0xff << ebit);

    } else {

        avio_write(data->buf, buf, len);

    }



    /* RTP marker bit means: last fragment of current frame was received;

       otherwise, an additional fragment is needed for the current frame */

    if (!(flags & RTP_FLAG_MARKER))

        return AVERROR(EAGAIN);



    /* write the completed last byte from the "byte merging" */

    if (data->endbyte_bits)

        avio_w8(data->buf, data->endbyte);

    data->endbyte_bits = 0;



    /* close frame buffering and create resulting A/V packet */

    res = ff_rtp_finalize_packet(pkt, &data->buf, st->index);

    if (res < 0)

        return res;



    return 0;

}

int ff_h261_handle_packet(AVFormatContext *ctx, PayloadContext *data,

                          AVStream *st, AVPacket *pkt, uint32_t *timestamp,

                          const uint8_t *buf, int len, uint16_t seq, int flags)

{

    int sbit, ebit, gobn, mbap, quant;

    int res;



    



    

    if (data->buf && data->timestamp != *timestamp) {

        h261_free_dyn_buffer(&data->buf);

    }



    

    if (len < 5 ) {

        av_log(ctx, AV_LOG_ERROR, "Too short H.261 RTP packet\n");

        return AVERROR_INVALIDDATA;

    }



    









































    sbit  =  (buf[0] >> 5) & 0x07;

    ebit  =  (buf[0] >> 2) & 0x07;

    gobn  =  (buf[1] >> 4) & 0x0f;

    mbap  = ((buf[1] << 1) & 0x1e) | ((buf[1] >> 7) & 0x01);

    quant =  (buf[1] >> 4) & 0x0f;



    

    buf += RTP_H261_PAYLOAD_HEADER_SIZE;

    len -= RTP_H261_PAYLOAD_HEADER_SIZE;



    

    if (!data->buf) {

        

        if (!gobn  && !sbit && !mbap && !quant){

            res = avio_open_dyn_buf(&data->buf);

            if (res < 0)

                return res;

            

            data->timestamp = *timestamp;

        } else {

            

            return AVERROR(EAGAIN);

        }

    }



    

    if (data->endbyte_bits || sbit) {

        if (data->endbyte_bits == sbit) {

            data->endbyte |= buf[0] & (0xff >> sbit);

            data->endbyte_bits = 0;

            buf++;

            len--;

            avio_w8(data->buf, data->endbyte);

        } else {

            

            GetBitContext gb;

            init_get_bits(&gb, buf, len*8 - ebit);

            skip_bits(&gb, sbit);

            if (data->endbyte_bits) {

                data->endbyte |= get_bits(&gb, 8 - data->endbyte_bits);

                avio_w8(data->buf, data->endbyte);

            }

            while (get_bits_left(&gb) >= 8)

                avio_w8(data->buf, get_bits(&gb, 8));

            data->endbyte_bits = get_bits_left(&gb);

            if (data->endbyte_bits)

                data->endbyte = get_bits(&gb, data->endbyte_bits) <<

                                (8 - data->endbyte_bits);

            ebit = 0;

            len = 0;

        }

    }

    if (ebit) {

        if (len > 0)

            avio_write(data->buf, buf, len - 1);

        data->endbyte_bits = 8 - ebit;

        data->endbyte = buf[len - 1] & (0xff << ebit);

    } else {

        avio_write(data->buf, buf, len);

    }



    



    if (!(flags & RTP_FLAG_MARKER))

        return AVERROR(EAGAIN);



    

    if (data->endbyte_bits)

        avio_w8(data->buf, data->endbyte);

    data->endbyte_bits = 0;



    

    res = ff_rtp_finalize_packet(pkt, &data->buf, st->index);

    if (res < 0)

        return res;



    return 0;

}

    mbap  = ((buf[1] << 1) & 0x1e) | ((buf[1] >> 7) & 0x01);     quant =  (buf[1] >> 4) & 0x0f;
int FUNC_0(AVFormatContext *VAR_0, PayloadContext *VAR_1,

                          AVStream *VAR_2, AVPacket *VAR_3, uint32_t *VAR_4,

                          const uint8_t *VAR_5, int VAR_6, uint16_t VAR_7, int VAR_8)

{

    int VAR_9, VAR_10, VAR_11, VAR_12, VAR_13;

    int VAR_14;



    



    

    if (VAR_1->VAR_5 && VAR_1->VAR_4 != *VAR_4) {

        h261_free_dyn_buffer(&VAR_1->VAR_5);

    }



    

    if (VAR_6 < 5 ) {

        av_log(VAR_0, AV_LOG_ERROR, "Too short H.261 RTP packet\n");

        return AVERROR_INVALIDDATA;

    }



    









































    VAR_9  =  (VAR_5[0] >> 5) & 0x07;

    VAR_10  =  (VAR_5[0] >> 2) & 0x07;

    VAR_11  =  (VAR_5[1] >> 4) & 0x0f;

    VAR_12  = ((VAR_5[1] << 1) & 0x1e) | ((VAR_5[1] >> 7) & 0x01);

    VAR_13 =  (VAR_5[1] >> 4) & 0x0f;



    

    VAR_5 += RTP_H261_PAYLOAD_HEADER_SIZE;

    VAR_6 -= RTP_H261_PAYLOAD_HEADER_SIZE;



    

    if (!VAR_1->VAR_5) {

        

        if (!VAR_11  && !VAR_9 && !VAR_12 && !VAR_13){

            VAR_14 = avio_open_dyn_buf(&VAR_1->VAR_5);

            if (VAR_14 < 0)

                return VAR_14;

            

            VAR_1->VAR_4 = *VAR_4;

        } else {

            

            return AVERROR(EAGAIN);

        }

    }



    

    if (VAR_1->endbyte_bits || VAR_9) {

        if (VAR_1->endbyte_bits == VAR_9) {

            VAR_1->endbyte |= VAR_5[0] & (0xff >> VAR_9);

            VAR_1->endbyte_bits = 0;

            VAR_5++;

            VAR_6--;

            avio_w8(VAR_1->VAR_5, VAR_1->endbyte);

        } else {

            

            GetBitContext gb;

            init_get_bits(&gb, VAR_5, VAR_6*8 - VAR_10);

            skip_bits(&gb, VAR_9);

            if (VAR_1->endbyte_bits) {

                VAR_1->endbyte |= get_bits(&gb, 8 - VAR_1->endbyte_bits);

                avio_w8(VAR_1->VAR_5, VAR_1->endbyte);

            }

            while (get_bits_left(&gb) >= 8)

                avio_w8(VAR_1->VAR_5, get_bits(&gb, 8));

            VAR_1->endbyte_bits = get_bits_left(&gb);

            if (VAR_1->endbyte_bits)

                VAR_1->endbyte = get_bits(&gb, VAR_1->endbyte_bits) <<

                                (8 - VAR_1->endbyte_bits);

            VAR_10 = 0;

            VAR_6 = 0;

        }

    }

    if (VAR_10) {

        if (VAR_6 > 0)

            avio_write(VAR_1->VAR_5, VAR_5, VAR_6 - 1);

        VAR_1->endbyte_bits = 8 - VAR_10;

        VAR_1->endbyte = VAR_5[VAR_6 - 1] & (0xff << VAR_10);

    } else {

        avio_write(VAR_1->VAR_5, VAR_5, VAR_6);

    }



    



    if (!(VAR_8 & RTP_FLAG_MARKER))

        return AVERROR(EAGAIN);



    

    if (VAR_1->endbyte_bits)

        avio_w8(VAR_1->VAR_5, VAR_1->endbyte);

    VAR_1->endbyte_bits = 0;



    

    VAR_14 = ff_rtp_finalize_packet(VAR_3, &VAR_1->VAR_5, VAR_2->index);

    if (VAR_14 < 0)

        return VAR_14;



    return 0;

}

int FUNC_0(AVFormatContext *VAR_0, PayloadContext *VAR_1,
AVStream *VAR_2, AVPacket *VAR_3, uint32_t *VAR_4,
const uint8_t *VAR_5, int VAR_6, uint16_t VAR_7, int VAR_8)
{ int VAR_9, VAR_10, VAR_11, VAR_12, VAR_13; int VAR_14; if (VAR_1->VAR_5 && VAR_1->VAR_4 != *VAR_4) { h261_free_dyn_buffer(&VAR_1->VAR_5); } if (VAR_6 < 5 ) { av_log(VAR_0, AV_LOG_ERROR, "Too short H.261 RTP packet\n"); return AVERROR_INVALIDDATA; } VAR_9  =  (VAR_5[0] >> 5) & 0x07; VAR_10  =  (VAR_5[0] >> 2) & 0x07; VAR_11  =  (VAR_5[1] >> 4) & 0x0f; VAR_12  = ((VAR_5[1] << 1) & 0x1e) | ((VAR_5[1] >> 7) & 0x01); VAR_13 =  (VAR_5[1] >> 4) & 0x0f; VAR_5 += RTP_H261_PAYLOAD_HEADER_SIZE; VAR_6 -= RTP_H261_PAYLOAD_HEADER_SIZE; if (!VAR_1->VAR_5) { if (!VAR_11  && !VAR_9 && !VAR_12 && !VAR_13){ VAR_14 = avio_open_dyn_buf(&VAR_1->VAR_5); if (VAR_14 < 0)
return VAR_14; VAR_1->VAR_4 = *VAR_4; } else { return AVERROR(EAGAIN); } } if (VAR_1->endbyte_bits || VAR_9) { if (VAR_1->endbyte_bits == VAR_9) { VAR_1->endbyte |= VAR_5[0] & (0xff >> VAR_9); VAR_1->endbyte_bits = 0; VAR_5++; VAR_6--; avio_w8(VAR_1->VAR_5, VAR_1->endbyte); } else { GetBitContext gb; init_get_bits(&gb, VAR_5, VAR_6*8 - VAR_10); skip_bits(&gb, VAR_9); if (VAR_1->endbyte_bits) { VAR_1->endbyte |= get_bits(&gb, 8 - VAR_1->endbyte_bits); avio_w8(VAR_1->VAR_5, VAR_1->endbyte); } while (get_bits_left(&gb) >= 8)
avio_w8(VAR_1->VAR_5, get_bits(&gb, 8)); VAR_1->endbyte_bits = get_bits_left(&gb); if (VAR_1->endbyte_bits)
VAR_1->endbyte = get_bits(&gb, VAR_1->endbyte_bits) <<
(8 - VAR_1->endbyte_bits); VAR_10 = 0; VAR_6 = 0; } } if (VAR_10) { if (VAR_6 > 0)
avio_write(VAR_1->VAR_5, VAR_5, VAR_6 - 1); VAR_1->endbyte_bits = 8 - VAR_10; VAR_1->endbyte = VAR_5[VAR_6 - 1] & (0xff << VAR_10); } else { avio_write(VAR_1->VAR_5, VAR_5, VAR_6); } if (!(VAR_8 & RTP_FLAG_MARKER))
return AVERROR(EAGAIN); if (VAR_1->endbyte_bits)
avio_w8(VAR_1->VAR_5, VAR_1->endbyte); VAR_1->endbyte_bits = 0; VAR_14 = ff_rtp_finalize_packet(VAR_3, &VAR_1->VAR_5, VAR_2->index); if (VAR_14 < 0)
return VAR_14; return 0; }