static int decode(AVCodecContext *avctx, void *data, int *data_size,

                  AVPacket *avpkt)

{

    const uint8_t *buf = avpkt->data;

    int buf_size       = avpkt->size;



    const uint8_t *buf_end;

    uint8_t       segment_type;

    int           segment_length;

    int i, ret;



    av_dlog(avctx, "PGS sub packet:\n");



    for (i = 0; i < buf_size; i++) {

        av_dlog(avctx, "%02x ", buf[i]);

        if (i % 16 == 15)

            av_dlog(avctx, "\n");

    }



    if (i & 15)

        av_dlog(avctx, "\n");



    *data_size = 0;



    /* Ensure that we have received at a least a segment code and segment length */

    if (buf_size < 3)

        return -1;



    buf_end = buf + buf_size;



    /* Step through buffer to identify segments */

    while (buf < buf_end) {

        segment_type   = bytestream_get_byte(&buf);

        segment_length = bytestream_get_be16(&buf);



        av_dlog(avctx, "Segment Length %d, Segment Type %x\n", segment_length, segment_type);



        if (segment_type != DISPLAY_SEGMENT && segment_length > buf_end - buf)

            break;



        switch (segment_type) {

        case PALETTE_SEGMENT:

            parse_palette_segment(avctx, buf, segment_length);

            break;

        case PICTURE_SEGMENT:

            parse_picture_segment(avctx, buf, segment_length);

            break;

        case PRESENTATION_SEGMENT:

            ret = parse_presentation_segment(avctx, buf, segment_length, avpkt->pts);

            if (ret < 0)

                return ret;

            break;

        case WINDOW_SEGMENT:

            /*

             * Window Segment Structure (No new information provided):

             *     2 bytes: Unknown,

             *     2 bytes: X position of subtitle,

             *     2 bytes: Y position of subtitle,

             *     2 bytes: Width of subtitle,

             *     2 bytes: Height of subtitle.

             */

            break;

        case DISPLAY_SEGMENT:

            *data_size = display_end_segment(avctx, data, buf, segment_length);

            break;

        default:

            av_log(avctx, AV_LOG_ERROR, "Unknown subtitle segment type 0x%x, length %d\n",

                   segment_type, segment_length);

            break;

        }



        buf += segment_length;

    }



    return buf_size;

}

static int decode(AVCodecContext *avctx, void *data, int *data_size,

                  AVPacket *avpkt)

{

    const uint8_t *buf = avpkt->data;

    int buf_size       = avpkt->size;



    const uint8_t *buf_end;

    uint8_t       segment_type;

    int           segment_length;

    int i, ret;



    av_dlog(avctx, "PGS sub packet:\n");



    for (i = 0; i < buf_size; i++) {

        av_dlog(avctx, "%02x ", buf[i]);

        if (i % 16 == 15)

            av_dlog(avctx, "\n");

    }



    if (i & 15)

        av_dlog(avctx, "\n");



    *data_size = 0;



    

    if (buf_size < 3)

        return -1;



    buf_end = buf + buf_size;



    

    while (buf < buf_end) {

        segment_type   = bytestream_get_byte(&buf);

        segment_length = bytestream_get_be16(&buf);



        av_dlog(avctx, "Segment Length %d, Segment Type %x\n", segment_length, segment_type);



        if (segment_type != DISPLAY_SEGMENT && segment_length > buf_end - buf)

            break;



        switch (segment_type) {

        case PALETTE_SEGMENT:

            parse_palette_segment(avctx, buf, segment_length);

            break;

        case PICTURE_SEGMENT:

            parse_picture_segment(avctx, buf, segment_length);

            break;

        case PRESENTATION_SEGMENT:

            ret = parse_presentation_segment(avctx, buf, segment_length, avpkt->pts);

            if (ret < 0)

                return ret;

            break;

        case WINDOW_SEGMENT:

            















            break;

        case DISPLAY_SEGMENT:

            *data_size = display_end_segment(avctx, data, buf, segment_length);

            break;

        default:

            av_log(avctx, AV_LOG_ERROR, "Unknown subtitle segment type 0x%x, length %d\n",

                   segment_type, segment_length);

            break;

        }



        buf += segment_length;

    }



    return buf_size;

}

static int FUNC_0(AVCodecContext *VAR_0, void *VAR_1, int *VAR_2,

                  AVPacket *VAR_3)

{

    const uint8_t *VAR_4 = VAR_3->VAR_1;

    int VAR_5       = VAR_3->size;



    const uint8_t *VAR_6;

    uint8_t       segment_type;

    int           VAR_7;

    int VAR_8, VAR_9;



    av_dlog(VAR_0, "PGS sub packet:\n");



    for (VAR_8 = 0; VAR_8 < VAR_5; VAR_8++) {

        av_dlog(VAR_0, "%02x ", VAR_4[VAR_8]);

        if (VAR_8 % 16 == 15)

            av_dlog(VAR_0, "\n");

    }



    if (VAR_8 & 15)

        av_dlog(VAR_0, "\n");



    *VAR_2 = 0;



    

    if (VAR_5 < 3)

        return -1;



    VAR_6 = VAR_4 + VAR_5;



    

    while (VAR_4 < VAR_6) {

        segment_type   = bytestream_get_byte(&VAR_4);

        VAR_7 = bytestream_get_be16(&VAR_4);



        av_dlog(VAR_0, "Segment Length %d, Segment Type %x\n", VAR_7, segment_type);



        if (segment_type != DISPLAY_SEGMENT && VAR_7 > VAR_6 - VAR_4)

            break;



        switch (segment_type) {

        case PALETTE_SEGMENT:

            parse_palette_segment(VAR_0, VAR_4, VAR_7);

            break;

        case PICTURE_SEGMENT:

            parse_picture_segment(VAR_0, VAR_4, VAR_7);

            break;

        case PRESENTATION_SEGMENT:

            VAR_9 = parse_presentation_segment(VAR_0, VAR_4, VAR_7, VAR_3->pts);

            if (VAR_9 < 0)

                return VAR_9;

            break;

        case WINDOW_SEGMENT:

            















            break;

        case DISPLAY_SEGMENT:

            *VAR_2 = display_end_segment(VAR_0, VAR_1, VAR_4, VAR_7);

            break;

        default:

            av_log(VAR_0, AV_LOG_ERROR, "Unknown subtitle segment type 0x%x, length %d\n",

                   segment_type, VAR_7);

            break;

        }



        VAR_4 += VAR_7;

    }



    return VAR_5;

}

static int FUNC_0(AVCodecContext *VAR_0, void *VAR_1, int *VAR_2,
AVPacket *VAR_3)
{ const uint8_t *VAR_4 = VAR_3->VAR_1; int VAR_5       = VAR_3->size; const uint8_t *VAR_6; uint8_t       segment_type; int           VAR_7; int VAR_8, VAR_9; av_dlog(VAR_0, "PGS sub packet:\n"); for (VAR_8 = 0; VAR_8 < VAR_5; VAR_8++) { av_dlog(VAR_0, "%02x ", VAR_4[VAR_8]); if (VAR_8 % 16 == 15)
av_dlog(VAR_0, "\n"); } if (VAR_8 & 15)
av_dlog(VAR_0, "\n"); *VAR_2 = 0; if (VAR_5 < 3)
return -1; VAR_6 = VAR_4 + VAR_5; while (VAR_4 < VAR_6) { segment_type   = bytestream_get_byte(&VAR_4); VAR_7 = bytestream_get_be16(&VAR_4); av_dlog(VAR_0, "Segment Length %d, Segment Type %x\n", VAR_7, segment_type); if (segment_type != DISPLAY_SEGMENT && VAR_7 > VAR_6 - VAR_4)
break; switch (segment_type) { case PALETTE_SEGMENT:
parse_palette_segment(VAR_0, VAR_4, VAR_7); break; case PICTURE_SEGMENT:
parse_picture_segment(VAR_0, VAR_4, VAR_7); break; case PRESENTATION_SEGMENT:
VAR_9 = parse_presentation_segment(VAR_0, VAR_4, VAR_7, VAR_3->pts); if (VAR_9 < 0)
return VAR_9; break; case WINDOW_SEGMENT:
break; case DISPLAY_SEGMENT:
*VAR_2 = display_end_segment(VAR_0, VAR_1, VAR_4, VAR_7); break; default:
av_log(VAR_0, AV_LOG_ERROR, "Unknown subtitle segment type 0x%x, length %d\n",
segment_type, VAR_7); break; } VAR_4 += VAR_7; } return VAR_5; }