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

{

    MpegDemuxContext *vobsub = s->priv_data;

    FFDemuxSubtitlesQueue *q;

    AVIOContext *pb = vobsub->sub_ctx->pb;

    int ret, psize, total_read = 0, i;

    AVPacket idx_pkt;



    int64_t min_ts = INT64_MAX;

    int sid = 0;

    for (i = 0; i < s->nb_streams; i++) {

        FFDemuxSubtitlesQueue *tmpq = &vobsub->q[i];

        int64_t ts = tmpq->subs[tmpq->current_sub_idx].pts;

        if (ts < min_ts) {

            min_ts = ts;

            sid = i;

        }

    }

    q = &vobsub->q[sid];

    ret = ff_subtitles_queue_read_packet(q, &idx_pkt);

    if (ret < 0)

        return ret;



    /* compute maximum packet size using the next packet position. This is

     * useful when the len in the header is non-sense */

    if (q->current_sub_idx < q->nb_subs) {

        psize = q->subs[q->current_sub_idx].pos - idx_pkt.pos;

    } else {

        int64_t fsize = avio_size(pb);

        psize = fsize < 0 ? 0xffff : fsize - idx_pkt.pos;

    }



    avio_seek(pb, idx_pkt.pos, SEEK_SET);



    av_init_packet(pkt);

    pkt->size = 0;

    pkt->data = NULL;



    do {

        int n, to_read, startcode;

        int64_t pts, dts;

        int64_t old_pos = avio_tell(pb), new_pos;

        int pkt_size;



        ret = mpegps_read_pes_header(vobsub->sub_ctx, NULL, &startcode, &pts, &dts);

        if (ret < 0) {

            if (pkt->size) // raise packet even if incomplete

                break;

            goto fail;

        }

        to_read = ret & 0xffff;

        new_pos = avio_tell(pb);

        pkt_size = ret + (new_pos - old_pos);



        /* this prevents reads above the current packet */

        if (total_read + pkt_size > psize)

            break;

        total_read += pkt_size;



        /* the current chunk doesn't match the stream index (unlikely) */

        if ((startcode & 0x1f) != idx_pkt.stream_index)

            break;



        ret = av_grow_packet(pkt, to_read);

        if (ret < 0)

            goto fail;



        n = avio_read(pb, pkt->data + (pkt->size - to_read), to_read);

        if (n < to_read)

            pkt->size -= to_read - n;

    } while (total_read < psize);



    pkt->pts = pkt->dts = idx_pkt.pts;

    pkt->pos = idx_pkt.pos;

    pkt->stream_index = idx_pkt.stream_index;



    av_free_packet(&idx_pkt);

    return 0;



fail:

    av_free_packet(pkt);

    av_free_packet(&idx_pkt);

    return ret;

}

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

{

    MpegDemuxContext *vobsub = s->priv_data;

    FFDemuxSubtitlesQueue *q;

    AVIOContext *pb = vobsub->sub_ctx->pb;

    int ret, psize, total_read = 0, i;

    AVPacket idx_pkt;



    int64_t min_ts = INT64_MAX;

    int sid = 0;

    for (i = 0; i < s->nb_streams; i++) {

        FFDemuxSubtitlesQueue *tmpq = &vobsub->q[i];

        int64_t ts = tmpq->subs[tmpq->current_sub_idx].pts;

        if (ts < min_ts) {

            min_ts = ts;

            sid = i;

        }

    }

    q = &vobsub->q[sid];

    ret = ff_subtitles_queue_read_packet(q, &idx_pkt);

    if (ret < 0)

        return ret;



    



    if (q->current_sub_idx < q->nb_subs) {

        psize = q->subs[q->current_sub_idx].pos - idx_pkt.pos;

    } else {

        int64_t fsize = avio_size(pb);

        psize = fsize < 0 ? 0xffff : fsize - idx_pkt.pos;

    }



    avio_seek(pb, idx_pkt.pos, SEEK_SET);



    av_init_packet(pkt);

    pkt->size = 0;

    pkt->data = NULL;



    do {

        int n, to_read, startcode;

        int64_t pts, dts;

        int64_t old_pos = avio_tell(pb), new_pos;

        int pkt_size;



        ret = mpegps_read_pes_header(vobsub->sub_ctx, NULL, &startcode, &pts, &dts);

        if (ret < 0) {

            if (pkt->size) 

                break;

            goto fail;

        }

        to_read = ret & 0xffff;

        new_pos = avio_tell(pb);

        pkt_size = ret + (new_pos - old_pos);



        

        if (total_read + pkt_size > psize)

            break;

        total_read += pkt_size;



        

        if ((startcode & 0x1f) != idx_pkt.stream_index)

            break;



        ret = av_grow_packet(pkt, to_read);

        if (ret < 0)

            goto fail;



        n = avio_read(pb, pkt->data + (pkt->size - to_read), to_read);

        if (n < to_read)

            pkt->size -= to_read - n;

    } while (total_read < psize);



    pkt->pts = pkt->dts = idx_pkt.pts;

    pkt->pos = idx_pkt.pos;

    pkt->stream_index = idx_pkt.stream_index;



    av_free_packet(&idx_pkt);

    return 0;



fail:

    av_free_packet(pkt);

    av_free_packet(&idx_pkt);

    return ret;

}

                break;         int64_t ts = tmpq->subs[tmpq->current_sub_idx].pts;
static int FUNC_0(AVFormatContext *VAR_0, AVPacket *VAR_1)

{

    MpegDemuxContext *vobsub = VAR_0->priv_data;

    FFDemuxSubtitlesQueue *q;

    AVIOContext *pb = vobsub->sub_ctx->pb;

    int VAR_2, VAR_3, VAR_4 = 0, VAR_5;

    AVPacket idx_pkt;



    int64_t min_ts = INT64_MAX;

    int VAR_6 = 0;

    for (VAR_5 = 0; VAR_5 < VAR_0->nb_streams; VAR_5++) {

        FFDemuxSubtitlesQueue *tmpq = &vobsub->q[VAR_5];

        int64_t ts = tmpq->subs[tmpq->current_sub_idx].pts;

        if (ts < min_ts) {

            min_ts = ts;

            VAR_6 = VAR_5;

        }

    }

    q = &vobsub->q[VAR_6];

    VAR_2 = ff_subtitles_queue_read_packet(q, &idx_pkt);

    if (VAR_2 < 0)

        return VAR_2;



    



    if (q->current_sub_idx < q->nb_subs) {

        VAR_3 = q->subs[q->current_sub_idx].pos - idx_pkt.pos;

    } else {

        int64_t fsize = avio_size(pb);

        VAR_3 = fsize < 0 ? 0xffff : fsize - idx_pkt.pos;

    }



    avio_seek(pb, idx_pkt.pos, SEEK_SET);



    av_init_packet(VAR_1);

    VAR_1->size = 0;

    VAR_1->data = NULL;



    do {

        int VAR_7, VAR_8, VAR_9;

        int64_t pts, dts;

        int64_t old_pos = avio_tell(pb), new_pos;

        int VAR_10;



        VAR_2 = mpegps_read_pes_header(vobsub->sub_ctx, NULL, &VAR_9, &pts, &dts);

        if (VAR_2 < 0) {

            if (VAR_1->size) 

                break;

            goto fail;

        }

        VAR_8 = VAR_2 & 0xffff;

        new_pos = avio_tell(pb);

        VAR_10 = VAR_2 + (new_pos - old_pos);



        

        if (VAR_4 + VAR_10 > VAR_3)

            break;

        VAR_4 += VAR_10;



        

        if ((VAR_9 & 0x1f) != idx_pkt.stream_index)

            break;



        VAR_2 = av_grow_packet(VAR_1, VAR_8);

        if (VAR_2 < 0)

            goto fail;



        VAR_7 = avio_read(pb, VAR_1->data + (VAR_1->size - VAR_8), VAR_8);

        if (VAR_7 < VAR_8)

            VAR_1->size -= VAR_8 - VAR_7;

    } while (VAR_4 < VAR_3);



    VAR_1->pts = VAR_1->dts = idx_pkt.pts;

    VAR_1->pos = idx_pkt.pos;

    VAR_1->stream_index = idx_pkt.stream_index;



    av_free_packet(&idx_pkt);

    return 0;



fail:

    av_free_packet(VAR_1);

    av_free_packet(&idx_pkt);

    return VAR_2;

}

static int FUNC_0(AVFormatContext *VAR_0, AVPacket *VAR_1)
{ MpegDemuxContext *vobsub = VAR_0->priv_data; FFDemuxSubtitlesQueue *q; AVIOContext *pb = vobsub->sub_ctx->pb; int VAR_2, VAR_3, VAR_4 = 0, VAR_5; AVPacket idx_pkt; int64_t min_ts = INT64_MAX; int VAR_6 = 0; for (VAR_5 = 0; VAR_5 < VAR_0->nb_streams; VAR_5++) { FFDemuxSubtitlesQueue *tmpq = &vobsub->q[VAR_5]; int64_t ts = tmpq->subs[tmpq->current_sub_idx].pts; if (ts < min_ts) { min_ts = ts; VAR_6 = VAR_5; } } q = &vobsub->q[VAR_6]; VAR_2 = ff_subtitles_queue_read_packet(q, &idx_pkt); if (VAR_2 < 0)
return VAR_2; if (q->current_sub_idx < q->nb_subs) { VAR_3 = q->subs[q->current_sub_idx].pos - idx_pkt.pos; } else { int64_t fsize = avio_size(pb); VAR_3 = fsize < 0 ? 0xffff : fsize - idx_pkt.pos; } avio_seek(pb, idx_pkt.pos, SEEK_SET); av_init_packet(VAR_1); VAR_1->size = 0; VAR_1->data = NULL; do { int VAR_7, VAR_8, VAR_9; int64_t pts, dts; int64_t old_pos = avio_tell(pb), new_pos; int VAR_10; VAR_2 = mpegps_read_pes_header(vobsub->sub_ctx, NULL, &VAR_9, &pts, &dts); if (VAR_2 < 0) { if (VAR_1->size)
break; goto fail; } VAR_8 = VAR_2 & 0xffff; new_pos = avio_tell(pb); VAR_10 = VAR_2 + (new_pos - old_pos); if (VAR_4 + VAR_10 > VAR_3)
break; VAR_4 += VAR_10; if ((VAR_9 & 0x1f) != idx_pkt.stream_index)
break; VAR_2 = av_grow_packet(VAR_1, VAR_8); if (VAR_2 < 0)
goto fail; VAR_7 = avio_read(pb, VAR_1->data + (VAR_1->size - VAR_8), VAR_8); if (VAR_7 < VAR_8)
VAR_1->size -= VAR_8 - VAR_7; } while (VAR_4 < VAR_3); VAR_1->pts = VAR_1->dts = idx_pkt.pts; VAR_1->pos = idx_pkt.pos; VAR_1->stream_index = idx_pkt.stream_index; av_free_packet(&idx_pkt); return 0; fail:
av_free_packet(VAR_1); av_free_packet(&idx_pkt); return VAR_2; }