static int xiph_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 ident, fragmented, tdt, num_pkts, pkt_len;



    if (!buf) {

        if (!data->split_buf || data->split_pos + 2 > data->split_buf_len ||

            data->split_pkts <= 0) {

            av_log(ctx, AV_LOG_ERROR, "No more data to return\n");

            return AVERROR_INVALIDDATA;

        }

        pkt_len = AV_RB16(data->split_buf + data->split_pos);

        data->split_pos += 2;

        if (data->split_pos + pkt_len > data->split_buf_len) {

            av_log(ctx, AV_LOG_ERROR, "Not enough data to return\n");

            return AVERROR_INVALIDDATA;

        }

        if (av_new_packet(pkt, pkt_len)) {

            av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");

            return AVERROR(ENOMEM);

        }

        pkt->stream_index = st->index;

        memcpy(pkt->data, data->split_buf + data->split_pos, pkt_len);

        data->split_pos += pkt_len;

        data->split_pkts--;

        return data->split_pkts > 0;

    }



    if (len < 6 || len > INT_MAX/2) {

        av_log(ctx, AV_LOG_ERROR, "Invalid %d byte packet\n", len);

        return AVERROR_INVALIDDATA;

    }



    // read xiph rtp headers

    ident       = AV_RB24(buf);

    fragmented  = buf[3] >> 6;

    tdt         = (buf[3] >> 4) & 3;

    num_pkts    = buf[3] & 0xf;

    pkt_len     = AV_RB16(buf + 4);



    if (pkt_len > len - 6) {

        av_log(ctx, AV_LOG_ERROR,

               "Invalid packet length %d in %d byte packet\n", pkt_len,

               len);

        return AVERROR_INVALIDDATA;

    }



    if (ident != data->ident) {

        av_log(ctx, AV_LOG_ERROR,

               "Unimplemented Xiph SDP configuration change detected\n");

        return AVERROR_PATCHWELCOME;

    }



    if (tdt) {

        av_log(ctx, AV_LOG_ERROR,

               "Unimplemented RTP Xiph packet settings (%d,%d,%d)\n",

               fragmented, tdt, num_pkts);

        return AVERROR_PATCHWELCOME;

    }



    buf += 6; // move past header bits

    len -= 6;



    if (fragmented == 0) {

        if (av_new_packet(pkt, pkt_len)) {

            av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");

            return AVERROR(ENOMEM);

        }

        pkt->stream_index = st->index;

        memcpy(pkt->data, buf, pkt_len);

        buf += pkt_len;

        len -= pkt_len;

        num_pkts--;



        if (num_pkts > 0) {

            if (len > data->split_buf_size || !data->split_buf) {

                av_freep(&data->split_buf);

                data->split_buf_size = 2 * len;

                data->split_buf = av_malloc(data->split_buf_size);

                if (!data->split_buf) {

                    av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");

                    av_free_packet(pkt);

                    return AVERROR(ENOMEM);

                }

            }

            memcpy(data->split_buf, buf, len);

            data->split_buf_len = len;

            data->split_pos = 0;

            data->split_pkts = num_pkts;

            return 1;

        }



        return 0;



    } else if (fragmented == 1) {

        // start of xiph data fragment

        int res;



        // end packet has been lost somewhere, so drop buffered data

        ffio_free_dyn_buf(&data->fragment);



        if((res = avio_open_dyn_buf(&data->fragment)) < 0)

            return res;



        avio_write(data->fragment, buf, pkt_len);

        data->timestamp = *timestamp;



    } else {

        av_assert1(fragmented < 4);

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

            // skip if fragmented timestamp is incorrect;

            // a start packet has been lost somewhere

            ffio_free_dyn_buf(&data->fragment);

            av_log(ctx, AV_LOG_ERROR, "RTP timestamps don't match!\n");

            return AVERROR_INVALIDDATA;

        }

        if (!data->fragment) {

            av_log(ctx, AV_LOG_WARNING,

                   "Received packet without a start fragment; dropping.\n");

            return AVERROR(EAGAIN);

        }



        // copy data to fragment buffer

        avio_write(data->fragment, buf, pkt_len);



        if (fragmented == 3) {

            // end of xiph data packet

            int ret = ff_rtp_finalize_packet(pkt, &data->fragment, st->index);

            if (ret < 0) {

                av_log(ctx, AV_LOG_ERROR,

                       "Error occurred when getting fragment buffer.");

                return ret;

            }



            return 0;

        }

    }



   return AVERROR(EAGAIN);

}

static int xiph_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 ident, fragmented, tdt, num_pkts, pkt_len;



    if (!buf) {

        if (!data->split_buf || data->split_pos + 2 > data->split_buf_len ||

            data->split_pkts <= 0) {

            av_log(ctx, AV_LOG_ERROR, "No more data to return\n");

            return AVERROR_INVALIDDATA;

        }

        pkt_len = AV_RB16(data->split_buf + data->split_pos);

        data->split_pos += 2;

        if (data->split_pos + pkt_len > data->split_buf_len) {

            av_log(ctx, AV_LOG_ERROR, "Not enough data to return\n");

            return AVERROR_INVALIDDATA;

        }

        if (av_new_packet(pkt, pkt_len)) {

            av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");

            return AVERROR(ENOMEM);

        }

        pkt->stream_index = st->index;

        memcpy(pkt->data, data->split_buf + data->split_pos, pkt_len);

        data->split_pos += pkt_len;

        data->split_pkts--;

        return data->split_pkts > 0;

    }



    if (len < 6 || len > INT_MAX/2) {

        av_log(ctx, AV_LOG_ERROR, "Invalid %d byte packet\n", len);

        return AVERROR_INVALIDDATA;

    }



    

    ident       = AV_RB24(buf);

    fragmented  = buf[3] >> 6;

    tdt         = (buf[3] >> 4) & 3;

    num_pkts    = buf[3] & 0xf;

    pkt_len     = AV_RB16(buf + 4);



    if (pkt_len > len - 6) {

        av_log(ctx, AV_LOG_ERROR,

               "Invalid packet length %d in %d byte packet\n", pkt_len,

               len);

        return AVERROR_INVALIDDATA;

    }



    if (ident != data->ident) {

        av_log(ctx, AV_LOG_ERROR,

               "Unimplemented Xiph SDP configuration change detected\n");

        return AVERROR_PATCHWELCOME;

    }



    if (tdt) {

        av_log(ctx, AV_LOG_ERROR,

               "Unimplemented RTP Xiph packet settings (%d,%d,%d)\n",

               fragmented, tdt, num_pkts);

        return AVERROR_PATCHWELCOME;

    }



    buf += 6; 

    len -= 6;



    if (fragmented == 0) {

        if (av_new_packet(pkt, pkt_len)) {

            av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");

            return AVERROR(ENOMEM);

        }

        pkt->stream_index = st->index;

        memcpy(pkt->data, buf, pkt_len);

        buf += pkt_len;

        len -= pkt_len;

        num_pkts--;



        if (num_pkts > 0) {

            if (len > data->split_buf_size || !data->split_buf) {

                av_freep(&data->split_buf);

                data->split_buf_size = 2 * len;

                data->split_buf = av_malloc(data->split_buf_size);

                if (!data->split_buf) {

                    av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");

                    av_free_packet(pkt);

                    return AVERROR(ENOMEM);

                }

            }

            memcpy(data->split_buf, buf, len);

            data->split_buf_len = len;

            data->split_pos = 0;

            data->split_pkts = num_pkts;

            return 1;

        }



        return 0;



    } else if (fragmented == 1) {

        

        int res;



        

        ffio_free_dyn_buf(&data->fragment);



        if((res = avio_open_dyn_buf(&data->fragment)) < 0)

            return res;



        avio_write(data->fragment, buf, pkt_len);

        data->timestamp = *timestamp;



    } else {

        av_assert1(fragmented < 4);

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

            

            

            ffio_free_dyn_buf(&data->fragment);

            av_log(ctx, AV_LOG_ERROR, "RTP timestamps don't match!\n");

            return AVERROR_INVALIDDATA;

        }

        if (!data->fragment) {

            av_log(ctx, AV_LOG_WARNING,

                   "Received packet without a start fragment; dropping.\n");

            return AVERROR(EAGAIN);

        }



        

        avio_write(data->fragment, buf, pkt_len);



        if (fragmented == 3) {

            

            int ret = ff_rtp_finalize_packet(pkt, &data->fragment, st->index);

            if (ret < 0) {

                av_log(ctx, AV_LOG_ERROR,

                       "Error occurred when getting fragment buffer.");

                return ret;

            }



            return 0;

        }

    }



   return AVERROR(EAGAIN);

}

static 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;



    if (!VAR_5) {

        if (!VAR_1->split_buf || VAR_1->split_pos + 2 > VAR_1->split_buf_len ||

            VAR_1->split_pkts <= 0) {

            av_log(VAR_0, AV_LOG_ERROR, "No more VAR_1 to return\n");

            return AVERROR_INVALIDDATA;

        }

        VAR_13 = AV_RB16(VAR_1->split_buf + VAR_1->split_pos);

        VAR_1->split_pos += 2;

        if (VAR_1->split_pos + VAR_13 > VAR_1->split_buf_len) {

            av_log(VAR_0, AV_LOG_ERROR, "Not enough VAR_1 to return\n");

            return AVERROR_INVALIDDATA;

        }

        if (av_new_packet(VAR_3, VAR_13)) {

            av_log(VAR_0, AV_LOG_ERROR, "Out of memory.\n");

            return AVERROR(ENOMEM);

        }

        VAR_3->stream_index = VAR_2->index;

        memcpy(VAR_3->VAR_1, VAR_1->split_buf + VAR_1->split_pos, VAR_13);

        VAR_1->split_pos += VAR_13;

        VAR_1->split_pkts--;

        return VAR_1->split_pkts > 0;

    }



    if (VAR_6 < 6 || VAR_6 > INT_MAX/2) {

        av_log(VAR_0, AV_LOG_ERROR, "Invalid %d byte packet\n", VAR_6);

        return AVERROR_INVALIDDATA;

    }



    

    VAR_9       = AV_RB24(VAR_5);

    VAR_10  = VAR_5[3] >> 6;

    VAR_11         = (VAR_5[3] >> 4) & 3;

    VAR_12    = VAR_5[3] & 0xf;

    VAR_13     = AV_RB16(VAR_5 + 4);



    if (VAR_13 > VAR_6 - 6) {

        av_log(VAR_0, AV_LOG_ERROR,

               "Invalid packet length %d in %d byte packet\n", VAR_13,

               VAR_6);

        return AVERROR_INVALIDDATA;

    }



    if (VAR_9 != VAR_1->VAR_9) {

        av_log(VAR_0, AV_LOG_ERROR,

               "Unimplemented Xiph SDP configuration change detected\n");

        return AVERROR_PATCHWELCOME;

    }



    if (VAR_11) {

        av_log(VAR_0, AV_LOG_ERROR,

               "Unimplemented RTP Xiph packet settings (%d,%d,%d)\n",

               VAR_10, VAR_11, VAR_12);

        return AVERROR_PATCHWELCOME;

    }



    VAR_5 += 6; 

    VAR_6 -= 6;



    if (VAR_10 == 0) {

        if (av_new_packet(VAR_3, VAR_13)) {

            av_log(VAR_0, AV_LOG_ERROR, "Out of memory.\n");

            return AVERROR(ENOMEM);

        }

        VAR_3->stream_index = VAR_2->index;

        memcpy(VAR_3->VAR_1, VAR_5, VAR_13);

        VAR_5 += VAR_13;

        VAR_6 -= VAR_13;

        VAR_12--;



        if (VAR_12 > 0) {

            if (VAR_6 > VAR_1->split_buf_size || !VAR_1->split_buf) {

                av_freep(&VAR_1->split_buf);

                VAR_1->split_buf_size = 2 * VAR_6;

                VAR_1->split_buf = av_malloc(VAR_1->split_buf_size);

                if (!VAR_1->split_buf) {

                    av_log(VAR_0, AV_LOG_ERROR, "Out of memory.\n");

                    av_free_packet(VAR_3);

                    return AVERROR(ENOMEM);

                }

            }

            memcpy(VAR_1->split_buf, VAR_5, VAR_6);

            VAR_1->split_buf_len = VAR_6;

            VAR_1->split_pos = 0;

            VAR_1->split_pkts = VAR_12;

            return 1;

        }



        return 0;



    } else if (VAR_10 == 1) {

        

        int VAR_14;



        

        ffio_free_dyn_buf(&VAR_1->fragment);



        if((VAR_14 = avio_open_dyn_buf(&VAR_1->fragment)) < 0)

            return VAR_14;



        avio_write(VAR_1->fragment, VAR_5, VAR_13);

        VAR_1->VAR_4 = *VAR_4;



    } else {

        av_assert1(VAR_10 < 4);

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

            

            

            ffio_free_dyn_buf(&VAR_1->fragment);

            av_log(VAR_0, AV_LOG_ERROR, "RTP timestamps don't match!\n");

            return AVERROR_INVALIDDATA;

        }

        if (!VAR_1->fragment) {

            av_log(VAR_0, AV_LOG_WARNING,

                   "Received packet without a start fragment; dropping.\n");

            return AVERROR(EAGAIN);

        }



        

        avio_write(VAR_1->fragment, VAR_5, VAR_13);



        if (VAR_10 == 3) {

            

            int VAR_15 = ff_rtp_finalize_packet(VAR_3, &VAR_1->fragment, VAR_2->index);

            if (VAR_15 < 0) {

                av_log(VAR_0, AV_LOG_ERROR,

                       "Error occurred when getting fragment buffer.");

                return VAR_15;

            }



            return 0;

        }

    }



   return AVERROR(EAGAIN);

}

static 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; if (!VAR_5) { if (!VAR_1->split_buf || VAR_1->split_pos + 2 > VAR_1->split_buf_len ||
VAR_1->split_pkts <= 0) { av_log(VAR_0, AV_LOG_ERROR, "No more VAR_1 to return\n"); return AVERROR_INVALIDDATA; } VAR_13 = AV_RB16(VAR_1->split_buf + VAR_1->split_pos); VAR_1->split_pos += 2; if (VAR_1->split_pos + VAR_13 > VAR_1->split_buf_len) { av_log(VAR_0, AV_LOG_ERROR, "Not enough VAR_1 to return\n"); return AVERROR_INVALIDDATA; } if (av_new_packet(VAR_3, VAR_13)) { av_log(VAR_0, AV_LOG_ERROR, "Out of memory.\n"); return AVERROR(ENOMEM); } VAR_3->stream_index = VAR_2->index; memcpy(VAR_3->VAR_1, VAR_1->split_buf + VAR_1->split_pos, VAR_13); VAR_1->split_pos += VAR_13; VAR_1->split_pkts--; return VAR_1->split_pkts > 0; } if (VAR_6 < 6 || VAR_6 > INT_MAX/2) { av_log(VAR_0, AV_LOG_ERROR, "Invalid %d byte packet\n", VAR_6); return AVERROR_INVALIDDATA; } VAR_9       = AV_RB24(VAR_5); VAR_10  = VAR_5[3] >> 6; VAR_11         = (VAR_5[3] >> 4) & 3; VAR_12    = VAR_5[3] & 0xf; VAR_13     = AV_RB16(VAR_5 + 4); if (VAR_13 > VAR_6 - 6) { av_log(VAR_0, AV_LOG_ERROR,
"Invalid packet length %d in %d byte packet\n", VAR_13,
VAR_6); return AVERROR_INVALIDDATA; } if (VAR_9 != VAR_1->VAR_9) { av_log(VAR_0, AV_LOG_ERROR,
"Unimplemented Xiph SDP configuration change detected\n"); return AVERROR_PATCHWELCOME; } if (VAR_11) { av_log(VAR_0, AV_LOG_ERROR,
"Unimplemented RTP Xiph packet settings (%d,%d,%d)\n",
VAR_10, VAR_11, VAR_12); return AVERROR_PATCHWELCOME; } VAR_5 += 6; VAR_6 -= 6; if (VAR_10 == 0) { if (av_new_packet(VAR_3, VAR_13)) { av_log(VAR_0, AV_LOG_ERROR, "Out of memory.\n"); return AVERROR(ENOMEM); } VAR_3->stream_index = VAR_2->index; memcpy(VAR_3->VAR_1, VAR_5, VAR_13); VAR_5 += VAR_13; VAR_6 -= VAR_13; VAR_12--; if (VAR_12 > 0) { if (VAR_6 > VAR_1->split_buf_size || !VAR_1->split_buf) { av_freep(&VAR_1->split_buf); VAR_1->split_buf_size = 2 * VAR_6; VAR_1->split_buf = av_malloc(VAR_1->split_buf_size); if (!VAR_1->split_buf) { av_log(VAR_0, AV_LOG_ERROR, "Out of memory.\n"); av_free_packet(VAR_3); return AVERROR(ENOMEM); } } memcpy(VAR_1->split_buf, VAR_5, VAR_6); VAR_1->split_buf_len = VAR_6; VAR_1->split_pos = 0; VAR_1->split_pkts = VAR_12; return 1; } return 0; } else if (VAR_10 == 1) { int VAR_14; ffio_free_dyn_buf(&VAR_1->fragment); if((VAR_14 = avio_open_dyn_buf(&VAR_1->fragment)) < 0)
return VAR_14; avio_write(VAR_1->fragment, VAR_5, VAR_13); VAR_1->VAR_4 = *VAR_4; } else { av_assert1(VAR_10 < 4); if (VAR_1->VAR_4 != *VAR_4) { ffio_free_dyn_buf(&VAR_1->fragment); av_log(VAR_0, AV_LOG_ERROR, "RTP timestamps don't match!\n"); return AVERROR_INVALIDDATA; } if (!VAR_1->fragment) { av_log(VAR_0, AV_LOG_WARNING,
"Received packet without a start fragment; dropping.\n"); return AVERROR(EAGAIN); } avio_write(VAR_1->fragment, VAR_5, VAR_13); if (VAR_10 == 3) { int VAR_15 = ff_rtp_finalize_packet(VAR_3, &VAR_1->fragment, VAR_2->index); if (VAR_15 < 0) { av_log(VAR_0, AV_LOG_ERROR,
"Error occurred when getting fragment buffer."); return VAR_15; } return 0; } } return AVERROR(EAGAIN); }