static int parse_presentation_segment(AVCodecContext *avctx,

                                      const uint8_t *buf, int buf_size,

                                      int64_t pts)

{

    PGSSubContext *ctx = avctx->priv_data;

    int i, state, ret;




    // Video descriptor

    int w = bytestream_get_be16(&buf);

    int h = bytestream_get_be16(&buf);



    uint16_t object_index;



    ctx->presentation.pts = pts;



    av_dlog(avctx, "Video Dimensions %dx%d\n",

            w, h);

    ret = ff_set_dimensions(avctx, w, h);

    if (ret < 0)

        return ret;



    /* Skip 1 bytes of unknown, frame rate */

    buf++;



    // Composition descriptor

    ctx->presentation.id_number = bytestream_get_be16(&buf);

    /*

     * state is a 2 bit field that defines pgs epoch boundaries

     * 00 - Normal, previously defined objects and palettes are still valid

     * 01 - Acquisition point, previous objects and palettes can be released

     * 10 - Epoch start, previous objects and palettes can be released

     * 11 - Epoch continue, previous objects and palettes can be released

     *

     * reserved 6 bits discarded

     */

    state = bytestream_get_byte(&buf) >> 6;

    if (state != 0) {

        flush_cache(avctx);

    }



    /*

     * skip palette_update_flag (0x80),

     */

    buf += 1;

    ctx->presentation.palette_id = bytestream_get_byte(&buf);

    ctx->presentation.object_count = bytestream_get_byte(&buf);

    if (ctx->presentation.object_count > MAX_OBJECT_REFS) {

        av_log(avctx, AV_LOG_ERROR,

               "Invalid number of presentation objects %d\n",

               ctx->presentation.object_count);

        ctx->presentation.object_count = 2;

        if (avctx->err_recognition & AV_EF_EXPLODE) {

            return AVERROR_INVALIDDATA;

        }

    }





    for (i = 0; i < ctx->presentation.object_count; i++)

    {



        if (buf_end - buf < 8) {

            av_log(avctx, AV_LOG_ERROR, "Insufficent space for object\n");

            ctx->presentation.object_count = i;

            return AVERROR_INVALIDDATA;

        }



        ctx->presentation.objects[i].id = bytestream_get_be16(&buf);

        ctx->presentation.objects[i].window_id = bytestream_get_byte(&buf);

        ctx->presentation.objects[i].composition_flag = bytestream_get_byte(&buf);



        ctx->presentation.objects[i].x = bytestream_get_be16(&buf);

        ctx->presentation.objects[i].y = bytestream_get_be16(&buf);



        // If cropping

        if (ctx->presentation.objects[i].composition_flag & 0x80) {

            ctx->presentation.objects[i].crop_x = bytestream_get_be16(&buf);

            ctx->presentation.objects[i].crop_y = bytestream_get_be16(&buf);

            ctx->presentation.objects[i].crop_w = bytestream_get_be16(&buf);

            ctx->presentation.objects[i].crop_h = bytestream_get_be16(&buf);

        }



        av_dlog(avctx, "Subtitle Placement x=%d, y=%d\n",

                ctx->presentation.objects[i].x, ctx->presentation.objects[i].y);



        if (ctx->presentation.objects[i].x > avctx->width ||

            ctx->presentation.objects[i].y > avctx->height) {

            av_log(avctx, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n",

                   ctx->presentation.objects[i].x,

                   ctx->presentation.objects[i].y,

                    avctx->width, avctx->height);

            ctx->presentation.objects[i].x = 0;

            ctx->presentation.objects[i].y = 0;

            if (avctx->err_recognition & AV_EF_EXPLODE) {

                return AVERROR_INVALIDDATA;

            }

        }

    }



    return 0;

}
static int parse_presentation_segment(AVCodecContext *avctx,

                                      const uint8_t *buf, int buf_size,

                                      int64_t pts)

{

    PGSSubContext *ctx = avctx->priv_data;

    int i, state, ret;




    

    int w = bytestream_get_be16(&buf);

    int h = bytestream_get_be16(&buf);



    uint16_t object_index;



    ctx->presentation.pts = pts;



    av_dlog(avctx, "Video Dimensions %dx%d\n",

            w, h);

    ret = ff_set_dimensions(avctx, w, h);

    if (ret < 0)

        return ret;



    

    buf++;



    

    ctx->presentation.id_number = bytestream_get_be16(&buf);

    

















    state = bytestream_get_byte(&buf) >> 6;

    if (state != 0) {

        flush_cache(avctx);

    }



    





    buf += 1;

    ctx->presentation.palette_id = bytestream_get_byte(&buf);

    ctx->presentation.object_count = bytestream_get_byte(&buf);

    if (ctx->presentation.object_count > MAX_OBJECT_REFS) {

        av_log(avctx, AV_LOG_ERROR,

               "Invalid number of presentation objects %d\n",

               ctx->presentation.object_count);

        ctx->presentation.object_count = 2;

        if (avctx->err_recognition & AV_EF_EXPLODE) {

            return AVERROR_INVALIDDATA;

        }

    }





    for (i = 0; i < ctx->presentation.object_count; i++)

    {



        if (buf_end - buf < 8) {

            av_log(avctx, AV_LOG_ERROR, "Insufficent space for object\n");

            ctx->presentation.object_count = i;

            return AVERROR_INVALIDDATA;

        }



        ctx->presentation.objects[i].id = bytestream_get_be16(&buf);

        ctx->presentation.objects[i].window_id = bytestream_get_byte(&buf);

        ctx->presentation.objects[i].composition_flag = bytestream_get_byte(&buf);



        ctx->presentation.objects[i].x = bytestream_get_be16(&buf);

        ctx->presentation.objects[i].y = bytestream_get_be16(&buf);



        

        if (ctx->presentation.objects[i].composition_flag & 0x80) {

            ctx->presentation.objects[i].crop_x = bytestream_get_be16(&buf);

            ctx->presentation.objects[i].crop_y = bytestream_get_be16(&buf);

            ctx->presentation.objects[i].crop_w = bytestream_get_be16(&buf);

            ctx->presentation.objects[i].crop_h = bytestream_get_be16(&buf);

        }



        av_dlog(avctx, "Subtitle Placement x=%d, y=%d\n",

                ctx->presentation.objects[i].x, ctx->presentation.objects[i].y);



        if (ctx->presentation.objects[i].x > avctx->width ||

            ctx->presentation.objects[i].y > avctx->height) {

            av_log(avctx, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n",

                   ctx->presentation.objects[i].x,

                   ctx->presentation.objects[i].y,

                    avctx->width, avctx->height);

            ctx->presentation.objects[i].x = 0;

            ctx->presentation.objects[i].y = 0;

            if (avctx->err_recognition & AV_EF_EXPLODE) {

                return AVERROR_INVALIDDATA;

            }

        }

    }



    return 0;

}
static int FUNC_0(AVCodecContext *VAR_0,

                                      const uint8_t *VAR_1, int VAR_2,

                                      int64_t VAR_3)

{

    PGSSubContext *ctx = VAR_0->priv_data;

    int VAR_4, VAR_5, VAR_6;




    

    int VAR_7 = bytestream_get_be16(&VAR_1);

    int VAR_8 = bytestream_get_be16(&VAR_1);



    uint16_t object_index;



    ctx->presentation.VAR_3 = VAR_3;



    av_dlog(VAR_0, "Video Dimensions %dx%d\n",

            VAR_7, VAR_8);

    VAR_6 = ff_set_dimensions(VAR_0, VAR_7, VAR_8);

    if (VAR_6 < 0)

        return VAR_6;



    

    VAR_1++;



    

    ctx->presentation.id_number = bytestream_get_be16(&VAR_1);

    

















    VAR_5 = bytestream_get_byte(&VAR_1) >> 6;

    if (VAR_5 != 0) {

        flush_cache(VAR_0);

    }



    





    VAR_1 += 1;

    ctx->presentation.palette_id = bytestream_get_byte(&VAR_1);

    ctx->presentation.object_count = bytestream_get_byte(&VAR_1);

    if (ctx->presentation.object_count > MAX_OBJECT_REFS) {

        av_log(VAR_0, AV_LOG_ERROR,

               "Invalid number of presentation objects %d\n",

               ctx->presentation.object_count);

        ctx->presentation.object_count = 2;

        if (VAR_0->err_recognition & AV_EF_EXPLODE) {

            return AVERROR_INVALIDDATA;

        }

    }





    for (VAR_4 = 0; VAR_4 < ctx->presentation.object_count; VAR_4++)

    {



        if (buf_end - VAR_1 < 8) {

            av_log(VAR_0, AV_LOG_ERROR, "Insufficent space for object\n");

            ctx->presentation.object_count = VAR_4;

            return AVERROR_INVALIDDATA;

        }



        ctx->presentation.objects[VAR_4].id = bytestream_get_be16(&VAR_1);

        ctx->presentation.objects[VAR_4].window_id = bytestream_get_byte(&VAR_1);

        ctx->presentation.objects[VAR_4].composition_flag = bytestream_get_byte(&VAR_1);



        ctx->presentation.objects[VAR_4].x = bytestream_get_be16(&VAR_1);

        ctx->presentation.objects[VAR_4].y = bytestream_get_be16(&VAR_1);



        

        if (ctx->presentation.objects[VAR_4].composition_flag & 0x80) {

            ctx->presentation.objects[VAR_4].crop_x = bytestream_get_be16(&VAR_1);

            ctx->presentation.objects[VAR_4].crop_y = bytestream_get_be16(&VAR_1);

            ctx->presentation.objects[VAR_4].crop_w = bytestream_get_be16(&VAR_1);

            ctx->presentation.objects[VAR_4].crop_h = bytestream_get_be16(&VAR_1);

        }



        av_dlog(VAR_0, "Subtitle Placement x=%d, y=%d\n",

                ctx->presentation.objects[VAR_4].x, ctx->presentation.objects[VAR_4].y);



        if (ctx->presentation.objects[VAR_4].x > VAR_0->width ||

            ctx->presentation.objects[VAR_4].y > VAR_0->height) {

            av_log(VAR_0, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n",

                   ctx->presentation.objects[VAR_4].x,

                   ctx->presentation.objects[VAR_4].y,

                    VAR_0->width, VAR_0->height);

            ctx->presentation.objects[VAR_4].x = 0;

            ctx->presentation.objects[VAR_4].y = 0;

            if (VAR_0->err_recognition & AV_EF_EXPLODE) {

                return AVERROR_INVALIDDATA;

            }

        }

    }



    return 0;

}
static int FUNC_0(AVCodecContext *VAR_0,
const uint8_t *VAR_1, int VAR_2,
int64_t VAR_3)
{ PGSSubContext *ctx = VAR_0->priv_data; int VAR_4, VAR_5, VAR_6; int VAR_7 = bytestream_get_be16(&VAR_1); int VAR_8 = bytestream_get_be16(&VAR_1); uint16_t object_index; ctx->presentation.VAR_3 = VAR_3; av_dlog(VAR_0, "Video Dimensions %dx%d\n",
VAR_7, VAR_8); VAR_6 = ff_set_dimensions(VAR_0, VAR_7, VAR_8); if (VAR_6 < 0)
return VAR_6; VAR_1++; ctx->presentation.id_number = bytestream_get_be16(&VAR_1); VAR_5 = bytestream_get_byte(&VAR_1) >> 6; if (VAR_5 != 0) { flush_cache(VAR_0); } VAR_1 += 1; ctx->presentation.palette_id = bytestream_get_byte(&VAR_1); ctx->presentation.object_count = bytestream_get_byte(&VAR_1); if (ctx->presentation.object_count > MAX_OBJECT_REFS) { av_log(VAR_0, AV_LOG_ERROR,
"Invalid number of presentation objects %d\n",
ctx->presentation.object_count); ctx->presentation.object_count = 2; if (VAR_0->err_recognition & AV_EF_EXPLODE) { return AVERROR_INVALIDDATA; } } for (VAR_4 = 0; VAR_4 < ctx->presentation.object_count; VAR_4++) { if (buf_end - VAR_1 < 8) { av_log(VAR_0, AV_LOG_ERROR, "Insufficent space for object\n"); ctx->presentation.object_count = VAR_4; return AVERROR_INVALIDDATA; } ctx->presentation.objects[VAR_4].id = bytestream_get_be16(&VAR_1); ctx->presentation.objects[VAR_4].window_id = bytestream_get_byte(&VAR_1); ctx->presentation.objects[VAR_4].composition_flag = bytestream_get_byte(&VAR_1); ctx->presentation.objects[VAR_4].x = bytestream_get_be16(&VAR_1); ctx->presentation.objects[VAR_4].y = bytestream_get_be16(&VAR_1); if (ctx->presentation.objects[VAR_4].composition_flag & 0x80) { ctx->presentation.objects[VAR_4].crop_x = bytestream_get_be16(&VAR_1); ctx->presentation.objects[VAR_4].crop_y = bytestream_get_be16(&VAR_1); ctx->presentation.objects[VAR_4].crop_w = bytestream_get_be16(&VAR_1); ctx->presentation.objects[VAR_4].crop_h = bytestream_get_be16(&VAR_1); } av_dlog(VAR_0, "Subtitle Placement x=%d, y=%d\n",
ctx->presentation.objects[VAR_4].x, ctx->presentation.objects[VAR_4].y); if (ctx->presentation.objects[VAR_4].x > VAR_0->width ||
ctx->presentation.objects[VAR_4].y > VAR_0->height) { av_log(VAR_0, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n",
ctx->presentation.objects[VAR_4].x,
ctx->presentation.objects[VAR_4].y,
VAR_0->width, VAR_0->height); ctx->presentation.objects[VAR_4].x = 0; ctx->presentation.objects[VAR_4].y = 0; if (VAR_0->err_recognition & AV_EF_EXPLODE) { return AVERROR_INVALIDDATA; } } } return 0; }