static int dvbsub_parse_region_segment(AVCodecContext *avctx,

                                       const uint8_t *buf, int buf_size)

{

    DVBSubContext *ctx = avctx->priv_data;



    const uint8_t *buf_end = buf + buf_size;

    int region_id, object_id;

    int av_unused version;

    DVBSubRegion *region;

    DVBSubObject *object;

    DVBSubObjectDisplay *display;

    int fill;

    int ret;



    if (buf_size < 10)

        return AVERROR_INVALIDDATA;



    region_id = *buf++;



    region = get_region(ctx, region_id);



    if (!region) {

        region = av_mallocz(sizeof(DVBSubRegion));

        if (!region)

            return AVERROR(ENOMEM);



        region->id = region_id;

        region->version = -1;



        region->next = ctx->region_list;

        ctx->region_list = region;




    version = ((*buf)>>4) & 15;

    fill = ((*buf++) >> 3) & 1;



    region->width = AV_RB16(buf);

    buf += 2;

    region->height = AV_RB16(buf);

    buf += 2;



    ret = av_image_check_size2(region->width, region->height, avctx->max_pixels, AV_PIX_FMT_PAL8, 0, avctx);





    if (ret < 0) {

        region->width= region->height= 0;

        return ret;




    if (region->width * region->height != region->buf_size) {

        av_free(region->pbuf);



        region->buf_size = region->width * region->height;



        region->pbuf = av_malloc(region->buf_size);

        if (!region->pbuf) {

            region->buf_size =

            region->width =

            region->height = 0;

            return AVERROR(ENOMEM);




        fill = 1;

        region->dirty = 0;




    region->depth = 1 << (((*buf++) >> 2) & 7);

    if(region->depth<2 || region->depth>8){

        av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);

        region->depth= 4;


    region->clut = *buf++;



    if (region->depth == 8) {

        region->bgcolor = *buf++;

        buf += 1;

    } else {

        buf += 1;



        if (region->depth == 4)

            region->bgcolor = (((*buf++) >> 4) & 15);

        else

            region->bgcolor = (((*buf++) >> 2) & 3);




    ff_dlog(avctx, "Region %d, (%dx%d)\n", region_id, region->width, region->height);



    if (fill) {

        memset(region->pbuf, region->bgcolor, region->buf_size);

        ff_dlog(avctx, "Fill region (%d)\n", region->bgcolor);




    delete_region_display_list(ctx, region);



    while (buf + 5 < buf_end) {

        object_id = AV_RB16(buf);

        buf += 2;



        object = get_object(ctx, object_id);



        if (!object) {

            object = av_mallocz(sizeof(DVBSubObject));

            if (!object)

                return AVERROR(ENOMEM);



            object->id = object_id;

            object->next = ctx->object_list;

            ctx->object_list = object;




        object->type = (*buf) >> 6;



        display = av_mallocz(sizeof(DVBSubObjectDisplay));

        if (!display)

            return AVERROR(ENOMEM);



        display->object_id = object_id;

        display->region_id = region_id;



        display->x_pos = AV_RB16(buf) & 0xfff;

        buf += 2;

        display->y_pos = AV_RB16(buf) & 0xfff;

        buf += 2;



        if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {

            display->fgcolor = *buf++;

            display->bgcolor = *buf++;




        display->region_list_next = region->display_list;

        region->display_list = display;



        display->object_list_next = object->display_list;

        object->display_list = display;




    return 0;

static int dvbsub_parse_region_segment(AVCodecContext *avctx,

                                       const uint8_t *buf, int buf_size)

{

    DVBSubContext *ctx = avctx->priv_data;



    const uint8_t *buf_end = buf + buf_size;

    int region_id, object_id;

    int av_unused version;

    DVBSubRegion *region;

    DVBSubObject *object;

    DVBSubObjectDisplay *display;

    int fill;

    int ret;



    if (buf_size < 10)

        return AVERROR_INVALIDDATA;



    region_id = *buf++;



    region = get_region(ctx, region_id);



    if (!region) {

        region = av_mallocz(sizeof(DVBSubRegion));

        if (!region)

            return AVERROR(ENOMEM);



        region->id = region_id;

        region->version = -1;



        region->next = ctx->region_list;

        ctx->region_list = region;




    version = ((*buf)>>4) & 15;

    fill = ((*buf++) >> 3) & 1;



    region->width = AV_RB16(buf);

    buf += 2;

    region->height = AV_RB16(buf);

    buf += 2;



    ret = av_image_check_size2(region->width, region->height, avctx->max_pixels, AV_PIX_FMT_PAL8, 0, avctx);





    if (ret < 0) {

        region->width= region->height= 0;

        return ret;




    if (region->width * region->height != region->buf_size) {

        av_free(region->pbuf);



        region->buf_size = region->width * region->height;



        region->pbuf = av_malloc(region->buf_size);

        if (!region->pbuf) {

            region->buf_size =

            region->width =

            region->height = 0;

            return AVERROR(ENOMEM);




        fill = 1;

        region->dirty = 0;




    region->depth = 1 << (((*buf++) >> 2) & 7);

    if(region->depth<2 || region->depth>8){

        av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);

        region->depth= 4;


    region->clut = *buf++;



    if (region->depth == 8) {

        region->bgcolor = *buf++;

        buf += 1;

    } else {

        buf += 1;



        if (region->depth == 4)

            region->bgcolor = (((*buf++) >> 4) & 15);

        else

            region->bgcolor = (((*buf++) >> 2) & 3);




    ff_dlog(avctx, "Region %d, (%dx%d)\n", region_id, region->width, region->height);



    if (fill) {

        memset(region->pbuf, region->bgcolor, region->buf_size);

        ff_dlog(avctx, "Fill region (%d)\n", region->bgcolor);




    delete_region_display_list(ctx, region);



    while (buf + 5 < buf_end) {

        object_id = AV_RB16(buf);

        buf += 2;



        object = get_object(ctx, object_id);



        if (!object) {

            object = av_mallocz(sizeof(DVBSubObject));

            if (!object)

                return AVERROR(ENOMEM);



            object->id = object_id;

            object->next = ctx->object_list;

            ctx->object_list = object;




        object->type = (*buf) >> 6;



        display = av_mallocz(sizeof(DVBSubObjectDisplay));

        if (!display)

            return AVERROR(ENOMEM);



        display->object_id = object_id;

        display->region_id = region_id;



        display->x_pos = AV_RB16(buf) & 0xfff;

        buf += 2;

        display->y_pos = AV_RB16(buf) & 0xfff;

        buf += 2;



        if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {

            display->fgcolor = *buf++;

            display->bgcolor = *buf++;




        display->region_list_next = region->display_list;

        region->display_list = display;



        display->object_list_next = object->display_list;

        object->display_list = display;




    return 0;

static int FUNC_0(AVCodecContext *VAR_0,

                                       const uint8_t *VAR_1, int VAR_2)

{

    DVBSubContext *ctx = VAR_0->priv_data;



    const uint8_t *VAR_3 = VAR_1 + VAR_2;

    int VAR_4, VAR_5;

    int VAR_6 version;

    DVBSubRegion *region;

    DVBSubObject *object;

    DVBSubObjectDisplay *display;

    int VAR_7;

    int VAR_8;



    if (VAR_2 < 10)

        return AVERROR_INVALIDDATA;



    VAR_4 = *VAR_1++;



    region = get_region(ctx, VAR_4);



    if (!region) {

        region = av_mallocz(sizeof(DVBSubRegion));

        if (!region)

            return AVERROR(ENOMEM);



        region->id = VAR_4;

        region->version = -1;



        region->next = ctx->region_list;

        ctx->region_list = region;




    version = ((*VAR_1)>>4) & 15;

    VAR_7 = ((*VAR_1++) >> 3) & 1;



    region->width = AV_RB16(VAR_1);

    VAR_1 += 2;

    region->height = AV_RB16(VAR_1);

    VAR_1 += 2;



    VAR_8 = av_image_check_size2(region->width, region->height, VAR_0->max_pixels, AV_PIX_FMT_PAL8, 0, VAR_0);





    if (VAR_8 < 0) {

        region->width= region->height= 0;

        return VAR_8;




    if (region->width * region->height != region->VAR_2) {

        av_free(region->pbuf);



        region->VAR_2 = region->width * region->height;



        region->pbuf = av_malloc(region->VAR_2);

        if (!region->pbuf) {

            region->VAR_2 =

            region->width =

            region->height = 0;

            return AVERROR(ENOMEM);




        VAR_7 = 1;

        region->dirty = 0;




    region->depth = 1 << (((*VAR_1++) >> 2) & 7);

    if(region->depth<2 || region->depth>8){

        av_log(VAR_0, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);

        region->depth= 4;


    region->clut = *VAR_1++;



    if (region->depth == 8) {

        region->bgcolor = *VAR_1++;

        VAR_1 += 1;

    } else {

        VAR_1 += 1;



        if (region->depth == 4)

            region->bgcolor = (((*VAR_1++) >> 4) & 15);

        else

            region->bgcolor = (((*VAR_1++) >> 2) & 3);




    ff_dlog(VAR_0, "Region %d, (%dx%d)\n", VAR_4, region->width, region->height);



    if (VAR_7) {

        memset(region->pbuf, region->bgcolor, region->VAR_2);

        ff_dlog(VAR_0, "Fill region (%d)\n", region->bgcolor);




    delete_region_display_list(ctx, region);



    while (VAR_1 + 5 < VAR_3) {

        VAR_5 = AV_RB16(VAR_1);

        VAR_1 += 2;



        object = get_object(ctx, VAR_5);



        if (!object) {

            object = av_mallocz(sizeof(DVBSubObject));

            if (!object)

                return AVERROR(ENOMEM);



            object->id = VAR_5;

            object->next = ctx->object_list;

            ctx->object_list = object;




        object->type = (*VAR_1) >> 6;



        display = av_mallocz(sizeof(DVBSubObjectDisplay));

        if (!display)

            return AVERROR(ENOMEM);



        display->VAR_5 = VAR_5;

        display->VAR_4 = VAR_4;



        display->x_pos = AV_RB16(VAR_1) & 0xfff;

        VAR_1 += 2;

        display->y_pos = AV_RB16(VAR_1) & 0xfff;

        VAR_1 += 2;



        if ((object->type == 1 || object->type == 2) && VAR_1+1 < VAR_3) {

            display->fgcolor = *VAR_1++;

            display->bgcolor = *VAR_1++;




        display->region_list_next = region->display_list;

        region->display_list = display;



        display->object_list_next = object->display_list;

        object->display_list = display;




    return 0;

static int FUNC_0(AVCodecContext *VAR_0,
const uint8_t *VAR_1, int VAR_2)
{ DVBSubContext *ctx = VAR_0->priv_data; const uint8_t *VAR_3 = VAR_1 + VAR_2; int VAR_4, VAR_5; int VAR_6 version; DVBSubRegion *region; DVBSubObject *object; DVBSubObjectDisplay *display; int VAR_7; int VAR_8; if (VAR_2 < 10)
return AVERROR_INVALIDDATA; VAR_4 = *VAR_1++; region = get_region(ctx, VAR_4); if (!region) { region = av_mallocz(sizeof(DVBSubRegion)); if (!region)
return AVERROR(ENOMEM); region->id = VAR_4; region->version = -1; region->next = ctx->region_list; ctx->region_list = region; version = ((*VAR_1)>>4) & 15; VAR_7 = ((*VAR_1++) >> 3) & 1; region->width = AV_RB16(VAR_1); VAR_1 += 2; region->height = AV_RB16(VAR_1); VAR_1 += 2; VAR_8 = av_image_check_size2(region->width, region->height, VAR_0->max_pixels, AV_PIX_FMT_PAL8, 0, VAR_0); if (VAR_8 < 0) { region->width= region->height= 0; return VAR_8; if (region->width * region->height != region->VAR_2) { av_free(region->pbuf); region->VAR_2 = region->width * region->height; region->pbuf = av_malloc(region->VAR_2); if (!region->pbuf) { region->VAR_2 =
region->width =
region->height = 0; return AVERROR(ENOMEM); VAR_7 = 1; region->dirty = 0; region->depth = 1 << (((*VAR_1++) >> 2) & 7); if(region->depth<2 || region->depth>8){ av_log(VAR_0, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth); region->depth= 4; region->clut = *VAR_1++; if (region->depth == 8) { region->bgcolor = *VAR_1++; VAR_1 += 1; } else { VAR_1 += 1; if (region->depth == 4)
region->bgcolor = (((*VAR_1++) >> 4) & 15); else
region->bgcolor = (((*VAR_1++) >> 2) & 3); ff_dlog(VAR_0, "Region %d, (%dx%d)\n", VAR_4, region->width, region->height); if (VAR_7) { memset(region->pbuf, region->bgcolor, region->VAR_2); ff_dlog(VAR_0, "Fill region (%d)\n", region->bgcolor); delete_region_display_list(ctx, region); while (VAR_1 + 5 < VAR_3) { VAR_5 = AV_RB16(VAR_1); VAR_1 += 2; object = get_object(ctx, VAR_5); if (!object) { object = av_mallocz(sizeof(DVBSubObject)); if (!object)
return AVERROR(ENOMEM); object->id = VAR_5; object->next = ctx->object_list; ctx->object_list = object; object->type = (*VAR_1) >> 6; display = av_mallocz(sizeof(DVBSubObjectDisplay)); if (!display)
return AVERROR(ENOMEM); display->VAR_5 = VAR_5; display->VAR_4 = VAR_4; display->x_pos = AV_RB16(VAR_1) & 0xfff; VAR_1 += 2; display->y_pos = AV_RB16(VAR_1) & 0xfff; VAR_1 += 2; if ((object->type == 1 || object->type == 2) && VAR_1+1 < VAR_3) { display->fgcolor = *VAR_1++; display->bgcolor = *VAR_1++; display->region_list_next = region->display_list; region->display_list = display; display->object_list_next = object->display_list; object->display_list = display; return 0;