static int teletext_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt)

{

    TeletextContext *ctx = avctx->priv_data;

    AVSubtitle      *sub = data;

    int             ret = 0;



    if (!ctx->vbi) {

        if (!(ctx->vbi = vbi_decoder_new()))

            return AVERROR(ENOMEM);

        if (!vbi_event_handler_add(ctx->vbi, VBI_EVENT_TTX_PAGE, handler, ctx)) {

            vbi_decoder_delete(ctx->vbi);

            ctx->vbi = NULL;

            return AVERROR(ENOMEM);

        }

    }



    if (avctx->pkt_timebase.den && pkt->pts != AV_NOPTS_VALUE)

        ctx->pts = av_rescale_q(pkt->pts, avctx->pkt_timebase, AV_TIME_BASE_Q);



    if (pkt->size) {

        int lines;

        const int full_pes_size = pkt->size + 45; /* PES header is 45 bytes */



        // We allow unreasonably big packets, even if the standard only allows a max size of 1472

        if (full_pes_size < 184 || full_pes_size > 65504 || full_pes_size % 184 != 0)

            return AVERROR_INVALIDDATA;



        ctx->handler_ret = pkt->size;



        if (data_identifier_is_teletext(*pkt->data)) {

            if ((lines = slice_to_vbi_lines(ctx, pkt->data + 1, pkt->size - 1)) < 0)

                return lines;

            av_dlog(avctx, "ctx=%p buf_size=%d lines=%u pkt_pts=%7.3f\n",

                    ctx, pkt->size, lines, (double)pkt->pts/90000.0);

            if (lines > 0) {

#ifdef DEBUG

                int i;

                av_log(avctx, AV_LOG_DEBUG, "line numbers:");

                for(i = 0; i < lines; i++)

                    av_log(avctx, AV_LOG_DEBUG, " %d", ctx->sliced[i].line);

                av_log(avctx, AV_LOG_DEBUG, "\n");

#endif

                vbi_decode(ctx->vbi, ctx->sliced, lines, 0.0);

                ctx->lines_processed += lines;

            }

        }

        ctx->pts = AV_NOPTS_VALUE;

        ret = ctx->handler_ret;

    }



    if (ret < 0)

        return ret;



    // is there a subtitle to pass?

    if (ctx->nb_pages) {

        int i;

        sub->format = ctx->format_id;

        sub->start_display_time = 0;

        sub->end_display_time = ctx->sub_duration;

        sub->num_rects = 0;

        sub->pts = ctx->pages->pts;



        if (ctx->pages->sub_rect->type != SUBTITLE_NONE) {

            sub->rects = av_malloc(sizeof(*sub->rects));

            if (sub->rects) {

                sub->num_rects = 1;

                sub->rects[0] = ctx->pages->sub_rect;

            } else {

                ret = AVERROR(ENOMEM);

            }

        } else {

            av_log(avctx, AV_LOG_DEBUG, "sending empty sub\n");

            sub->rects = NULL;

        }

        if (!sub->rects) // no rect was passed

            subtitle_rect_free(&ctx->pages->sub_rect);



        for (i = 0; i < ctx->nb_pages - 1; i++)

            ctx->pages[i] = ctx->pages[i + 1];

        ctx->nb_pages--;



        if (ret >= 0)

            *data_size = 1;

    } else

        *data_size = 0;



    return ret;

}

static int teletext_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt)

{

    TeletextContext *ctx = avctx->priv_data;

    AVSubtitle      *sub = data;

    int             ret = 0;



    if (!ctx->vbi) {

        if (!(ctx->vbi = vbi_decoder_new()))

            return AVERROR(ENOMEM);

        if (!vbi_event_handler_add(ctx->vbi, VBI_EVENT_TTX_PAGE, handler, ctx)) {

            vbi_decoder_delete(ctx->vbi);

            ctx->vbi = NULL;

            return AVERROR(ENOMEM);

        }

    }



    if (avctx->pkt_timebase.den && pkt->pts != AV_NOPTS_VALUE)

        ctx->pts = av_rescale_q(pkt->pts, avctx->pkt_timebase, AV_TIME_BASE_Q);



    if (pkt->size) {

        int lines;

        const int full_pes_size = pkt->size + 45; 



        

        if (full_pes_size < 184 || full_pes_size > 65504 || full_pes_size % 184 != 0)

            return AVERROR_INVALIDDATA;



        ctx->handler_ret = pkt->size;



        if (data_identifier_is_teletext(*pkt->data)) {

            if ((lines = slice_to_vbi_lines(ctx, pkt->data + 1, pkt->size - 1)) < 0)

                return lines;

            av_dlog(avctx, "ctx=%p buf_size=%d lines=%u pkt_pts=%7.3f\n",

                    ctx, pkt->size, lines, (double)pkt->pts/90000.0);

            if (lines > 0) {

#ifdef DEBUG

                int i;

                av_log(avctx, AV_LOG_DEBUG, "line numbers:");

                for(i = 0; i < lines; i++)

                    av_log(avctx, AV_LOG_DEBUG, " %d", ctx->sliced[i].line);

                av_log(avctx, AV_LOG_DEBUG, "\n");

#endif

                vbi_decode(ctx->vbi, ctx->sliced, lines, 0.0);

                ctx->lines_processed += lines;

            }

        }

        ctx->pts = AV_NOPTS_VALUE;

        ret = ctx->handler_ret;

    }



    if (ret < 0)

        return ret;



    

    if (ctx->nb_pages) {

        int i;

        sub->format = ctx->format_id;

        sub->start_display_time = 0;

        sub->end_display_time = ctx->sub_duration;

        sub->num_rects = 0;

        sub->pts = ctx->pages->pts;



        if (ctx->pages->sub_rect->type != SUBTITLE_NONE) {

            sub->rects = av_malloc(sizeof(*sub->rects));

            if (sub->rects) {

                sub->num_rects = 1;

                sub->rects[0] = ctx->pages->sub_rect;

            } else {

                ret = AVERROR(ENOMEM);

            }

        } else {

            av_log(avctx, AV_LOG_DEBUG, "sending empty sub\n");

            sub->rects = NULL;

        }

        if (!sub->rects) 

            subtitle_rect_free(&ctx->pages->sub_rect);



        for (i = 0; i < ctx->nb_pages - 1; i++)

            ctx->pages[i] = ctx->pages[i + 1];

        ctx->nb_pages--;



        if (ret >= 0)

            *data_size = 1;

    } else

        *data_size = 0;



    return ret;

}

static int FUNC_0(AVCodecContext *VAR_0, void *VAR_1, int *VAR_2, AVPacket *VAR_3)

{

    TeletextContext *ctx = VAR_0->priv_data;

    AVSubtitle      *sub = VAR_1;

    int             VAR_4 = 0;



    if (!ctx->vbi) {

        if (!(ctx->vbi = vbi_decoder_new()))

            return AVERROR(ENOMEM);

        if (!vbi_event_handler_add(ctx->vbi, VBI_EVENT_TTX_PAGE, handler, ctx)) {

            vbi_decoder_delete(ctx->vbi);

            ctx->vbi = NULL;

            return AVERROR(ENOMEM);

        }

    }



    if (VAR_0->pkt_timebase.den && VAR_3->pts != AV_NOPTS_VALUE)

        ctx->pts = av_rescale_q(VAR_3->pts, VAR_0->pkt_timebase, AV_TIME_BASE_Q);



    if (VAR_3->size) {

        int VAR_5;

        const int VAR_6 = VAR_3->size + 45; 



        

        if (VAR_6 < 184 || VAR_6 > 65504 || VAR_6 % 184 != 0)

            return AVERROR_INVALIDDATA;



        ctx->handler_ret = VAR_3->size;



        if (data_identifier_is_teletext(*VAR_3->VAR_1)) {

            if ((VAR_5 = slice_to_vbi_lines(ctx, VAR_3->VAR_1 + 1, VAR_3->size - 1)) < 0)

                return VAR_5;

            av_dlog(VAR_0, "ctx=%p buf_size=%d VAR_5=%u pkt_pts=%7.3f\n",

                    ctx, VAR_3->size, VAR_5, (double)VAR_3->pts/90000.0);

            if (VAR_5 > 0) {

#ifdef DEBUG

                int VAR_7;

                av_log(VAR_0, AV_LOG_DEBUG, "line numbers:");

                for(VAR_7 = 0; VAR_7 < VAR_5; VAR_7++)

                    av_log(VAR_0, AV_LOG_DEBUG, " %d", ctx->sliced[VAR_7].line);

                av_log(VAR_0, AV_LOG_DEBUG, "\n");

#endif

                vbi_decode(ctx->vbi, ctx->sliced, VAR_5, 0.0);

                ctx->lines_processed += VAR_5;

            }

        }

        ctx->pts = AV_NOPTS_VALUE;

        VAR_4 = ctx->handler_ret;

    }



    if (VAR_4 < 0)

        return VAR_4;



    

    if (ctx->nb_pages) {

        int VAR_7;

        sub->format = ctx->format_id;

        sub->start_display_time = 0;

        sub->end_display_time = ctx->sub_duration;

        sub->num_rects = 0;

        sub->pts = ctx->pages->pts;



        if (ctx->pages->sub_rect->type != SUBTITLE_NONE) {

            sub->rects = av_malloc(sizeof(*sub->rects));

            if (sub->rects) {

                sub->num_rects = 1;

                sub->rects[0] = ctx->pages->sub_rect;

            } else {

                VAR_4 = AVERROR(ENOMEM);

            }

        } else {

            av_log(VAR_0, AV_LOG_DEBUG, "sending empty sub\n");

            sub->rects = NULL;

        }

        if (!sub->rects) 

            subtitle_rect_free(&ctx->pages->sub_rect);



        for (VAR_7 = 0; VAR_7 < ctx->nb_pages - 1; VAR_7++)

            ctx->pages[VAR_7] = ctx->pages[VAR_7 + 1];

        ctx->nb_pages--;



        if (VAR_4 >= 0)

            *VAR_2 = 1;

    } else

        *VAR_2 = 0;



    return VAR_4;

}

static int FUNC_0(AVCodecContext *VAR_0, void *VAR_1, int *VAR_2, AVPacket *VAR_3)
{ TeletextContext *ctx = VAR_0->priv_data; AVSubtitle      *sub = VAR_1; int             VAR_4 = 0; if (!ctx->vbi) { if (!(ctx->vbi = vbi_decoder_new()))
return AVERROR(ENOMEM); if (!vbi_event_handler_add(ctx->vbi, VBI_EVENT_TTX_PAGE, handler, ctx)) { vbi_decoder_delete(ctx->vbi); ctx->vbi = NULL; return AVERROR(ENOMEM); } } if (VAR_0->pkt_timebase.den && VAR_3->pts != AV_NOPTS_VALUE)
ctx->pts = av_rescale_q(VAR_3->pts, VAR_0->pkt_timebase, AV_TIME_BASE_Q); if (VAR_3->size) { int VAR_5; const int VAR_6 = VAR_3->size + 45; if (VAR_6 < 184 || VAR_6 > 65504 || VAR_6 % 184 != 0)
return AVERROR_INVALIDDATA; ctx->handler_ret = VAR_3->size; if (data_identifier_is_teletext(*VAR_3->VAR_1)) { if ((VAR_5 = slice_to_vbi_lines(ctx, VAR_3->VAR_1 + 1, VAR_3->size - 1)) < 0)
return VAR_5; av_dlog(VAR_0, "ctx=%p buf_size=%d VAR_5=%u pkt_pts=%7.3f\n",
ctx, VAR_3->size, VAR_5, (double)VAR_3->pts/90000.0); if (VAR_5 > 0) { #ifdef DEBUG
int VAR_7; av_log(VAR_0, AV_LOG_DEBUG, "line numbers:"); for(VAR_7 = 0; VAR_7 < VAR_5; VAR_7++) av_log(VAR_0, AV_LOG_DEBUG, " %d", ctx->sliced[VAR_7].line); av_log(VAR_0, AV_LOG_DEBUG, "\n"); #endif
vbi_decode(ctx->vbi, ctx->sliced, VAR_5, 0.0); ctx->lines_processed += VAR_5; } } ctx->pts = AV_NOPTS_VALUE; VAR_4 = ctx->handler_ret; } if (VAR_4 < 0)
return VAR_4; if (ctx->nb_pages) { int VAR_7; sub->format = ctx->format_id; sub->start_display_time = 0; sub->end_display_time = ctx->sub_duration; sub->num_rects = 0; sub->pts = ctx->pages->pts; if (ctx->pages->sub_rect->type != SUBTITLE_NONE) { sub->rects = av_malloc(sizeof(*sub->rects)); if (sub->rects) { sub->num_rects = 1; sub->rects[0] = ctx->pages->sub_rect; } else { VAR_4 = AVERROR(ENOMEM); } } else { av_log(VAR_0, AV_LOG_DEBUG, "sending empty sub\n"); sub->rects = NULL; } if (!sub->rects)
subtitle_rect_free(&ctx->pages->sub_rect); for (VAR_7 = 0; VAR_7 < ctx->nb_pages - 1; VAR_7++) ctx->pages[VAR_7] = ctx->pages[VAR_7 + 1]; ctx->nb_pages--; if (VAR_4 >= 0)
*VAR_2 = 1; } else *VAR_2 = 0; return VAR_4; }