static AVFrame *get_palette_frame(AVFilterContext *ctx)

{

    AVFrame *out;

    PaletteGenContext *s = ctx->priv;

    AVFilterLink *outlink = ctx->outputs[0];

    double ratio;

    int box_id = 0;

    struct range_box *box;



    /* reference only the used colors from histogram */

    s->refs = load_color_refs(s->histogram, s->nb_refs);

    if (!s->refs) {

        av_log(ctx, AV_LOG_ERROR, "Unable to allocate references for %d different colors\n", s->nb_refs);

        return NULL;

    }



    /* create the palette frame */

    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);

    if (!out)

        return NULL;

    out->pts = 0;



    /* set first box for 0..nb_refs */

    box = &s->boxes[box_id];

    box->len = s->nb_refs;

    box->sorted_by = -1;

    box->color = get_avg_color(s->refs, box);

    box->variance = -1;

    s->nb_boxes = 1;



    while (box && box->len > 1) {

        int i, rr, gr, br, longest;

        uint64_t median, box_weight = 0;



        /* compute the box weight (sum all the weights of the colors in the

         * range) and its boundings */

        uint8_t min[3] = {0xff, 0xff, 0xff};

        uint8_t max[3] = {0x00, 0x00, 0x00};

        for (i = box->start; i < box->start + box->len; i++) {

            const struct color_ref *ref = s->refs[i];

            const uint32_t rgb = ref->color;

            const uint8_t r = rgb >> 16 & 0xff, g = rgb >> 8 & 0xff, b = rgb & 0xff;

            min[0] = FFMIN(r, min[0]), max[0] = FFMAX(r, max[0]);

            min[1] = FFMIN(g, min[1]), max[1] = FFMAX(g, max[1]);

            min[2] = FFMIN(b, min[2]), max[2] = FFMAX(b, max[2]);

            box_weight += ref->count;

        }



        /* define the axis to sort by according to the widest range of colors */

        rr = max[0] - min[0];

        gr = max[1] - min[1];

        br = max[2] - min[2];

        longest = 1; // pick green by default (the color the eye is the most sensitive to)

        if (br >= rr && br >= gr) longest = 2;

        if (rr >= gr && rr >= br) longest = 0;

        if (gr >= rr && gr >= br) longest = 1; // prefer green again



        av_dlog(ctx, "box #%02X [%6d..%-6d] (%6d) w:%-6"PRIu64" ranges:[%2x %2x %2x] sort by %c (already sorted:%c) ",

                box_id, box->start, box->start + box->len - 1, box->len, box_weight,

                rr, gr, br, "rgb"[longest], box->sorted_by == longest ? 'y':'n');



        /* sort the range by its longest axis if it's not already sorted */

        if (box->sorted_by != longest) {

            cmp_func cmpf = cmp_funcs[longest];

            AV_QSORT(&s->refs[box->start], box->len, const struct color_ref *, cmpf);

            box->sorted_by = longest;

        }



        /* locate the median where to split */

        median = (box_weight + 1) >> 1;

        box_weight = 0;

        /* if you have 2 boxes, the maximum is actually #0: you must have at

         * least 1 color on each side of the split, hence the -2 */

        for (i = box->start; i < box->start + box->len - 2; i++) {

            box_weight += s->refs[i]->count;

            if (box_weight > median)

                break;

        }

        av_dlog(ctx, "split @ i=%-6d with w=%-6"PRIu64" (target=%6"PRIu64")\n", i, box_weight, median);

        split_box(s, box, i);



        box_id = get_next_box_id_to_split(s);

        box = box_id >= 0 ? &s->boxes[box_id] : NULL;

    }



    ratio = set_colorquant_ratio_meta(out, s->nb_boxes, s->nb_refs);

    av_log(ctx, AV_LOG_INFO, "%d%s colors generated out of %d colors; ratio=%f\n",

           s->nb_boxes, s->reserve_transparent ? "(+1)" : "", s->nb_refs, ratio);



    qsort(s->boxes, s->nb_boxes, sizeof(*s->boxes), cmp_color);



    write_palette(ctx, out);



    return out;

}

static AVFrame *get_palette_frame(AVFilterContext *ctx)

{

    AVFrame *out;

    PaletteGenContext *s = ctx->priv;

    AVFilterLink *outlink = ctx->outputs[0];

    double ratio;

    int box_id = 0;

    struct range_box *box;



    

    s->refs = load_color_refs(s->histogram, s->nb_refs);

    if (!s->refs) {

        av_log(ctx, AV_LOG_ERROR, "Unable to allocate references for %d different colors\n", s->nb_refs);

        return NULL;

    }



    

    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);

    if (!out)

        return NULL;

    out->pts = 0;



    

    box = &s->boxes[box_id];

    box->len = s->nb_refs;

    box->sorted_by = -1;

    box->color = get_avg_color(s->refs, box);

    box->variance = -1;

    s->nb_boxes = 1;



    while (box && box->len > 1) {

        int i, rr, gr, br, longest;

        uint64_t median, box_weight = 0;



        



        uint8_t min[3] = {0xff, 0xff, 0xff};

        uint8_t max[3] = {0x00, 0x00, 0x00};

        for (i = box->start; i < box->start + box->len; i++) {

            const struct color_ref *ref = s->refs[i];

            const uint32_t rgb = ref->color;

            const uint8_t r = rgb >> 16 & 0xff, g = rgb >> 8 & 0xff, b = rgb & 0xff;

            min[0] = FFMIN(r, min[0]), max[0] = FFMAX(r, max[0]);

            min[1] = FFMIN(g, min[1]), max[1] = FFMAX(g, max[1]);

            min[2] = FFMIN(b, min[2]), max[2] = FFMAX(b, max[2]);

            box_weight += ref->count;

        }



        

        rr = max[0] - min[0];

        gr = max[1] - min[1];

        br = max[2] - min[2];

        longest = 1; 

        if (br >= rr && br >= gr) longest = 2;

        if (rr >= gr && rr >= br) longest = 0;

        if (gr >= rr && gr >= br) longest = 1; 



        av_dlog(ctx, "box #%02X [%6d..%-6d] (%6d) w:%-6"PRIu64" ranges:[%2x %2x %2x] sort by %c (already sorted:%c) ",

                box_id, box->start, box->start + box->len - 1, box->len, box_weight,

                rr, gr, br, "rgb"[longest], box->sorted_by == longest ? 'y':'n');



        

        if (box->sorted_by != longest) {

            cmp_func cmpf = cmp_funcs[longest];

            AV_QSORT(&s->refs[box->start], box->len, const struct color_ref *, cmpf);

            box->sorted_by = longest;

        }



        

        median = (box_weight + 1) >> 1;

        box_weight = 0;

        



        for (i = box->start; i < box->start + box->len - 2; i++) {

            box_weight += s->refs[i]->count;

            if (box_weight > median)

                break;

        }

        av_dlog(ctx, "split @ i=%-6d with w=%-6"PRIu64" (target=%6"PRIu64")\n", i, box_weight, median);

        split_box(s, box, i);



        box_id = get_next_box_id_to_split(s);

        box = box_id >= 0 ? &s->boxes[box_id] : NULL;

    }



    ratio = set_colorquant_ratio_meta(out, s->nb_boxes, s->nb_refs);

    av_log(ctx, AV_LOG_INFO, "%d%s colors generated out of %d colors; ratio=%f\n",

           s->nb_boxes, s->reserve_transparent ? "(+1)" : "", s->nb_refs, ratio);



    qsort(s->boxes, s->nb_boxes, sizeof(*s->boxes), cmp_color);



    write_palette(ctx, out);



    return out;

}

static AVFrame *FUNC_0(AVFilterContext *ctx)

{

    AVFrame *out;

    PaletteGenContext *s = ctx->priv;

    AVFilterLink *outlink = ctx->outputs[0];

    double VAR_0;

    int VAR_1 = 0;

    struct range_box *VAR_2;



    

    s->refs = load_color_refs(s->histogram, s->nb_refs);

    if (!s->refs) {

        av_log(ctx, AV_LOG_ERROR, "Unable to allocate references for %d different colors\n", s->nb_refs);

        return NULL;

    }



    

    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);

    if (!out)

        return NULL;

    out->pts = 0;



    

    VAR_2 = &s->boxes[VAR_1];

    VAR_2->len = s->nb_refs;

    VAR_2->sorted_by = -1;

    VAR_2->color = get_avg_color(s->refs, VAR_2);

    VAR_2->variance = -1;

    s->nb_boxes = 1;



    while (VAR_2 && VAR_2->len > 1) {

        int VAR_3, VAR_4, VAR_5, VAR_6, VAR_7;

        uint64_t median, box_weight = 0;



        



        uint8_t min[3] = {0xff, 0xff, 0xff};

        uint8_t max[3] = {0x00, 0x00, 0x00};

        for (VAR_3 = VAR_2->start; VAR_3 < VAR_2->start + VAR_2->len; VAR_3++) {

            const struct color_ref *VAR_8 = s->refs[VAR_3];

            const uint32_t VAR_9 = VAR_8->color;

            const uint8_t VAR_10 = VAR_9 >> 16 & 0xff, g = VAR_9 >> 8 & 0xff, b = VAR_9 & 0xff;

            min[0] = FFMIN(VAR_10, min[0]), max[0] = FFMAX(VAR_10, max[0]);

            min[1] = FFMIN(g, min[1]), max[1] = FFMAX(g, max[1]);

            min[2] = FFMIN(b, min[2]), max[2] = FFMAX(b, max[2]);

            box_weight += VAR_8->count;

        }



        

        VAR_4 = max[0] - min[0];

        VAR_5 = max[1] - min[1];

        VAR_6 = max[2] - min[2];

        VAR_7 = 1; 

        if (VAR_6 >= VAR_4 && VAR_6 >= VAR_5) VAR_7 = 2;

        if (VAR_4 >= VAR_5 && VAR_4 >= VAR_6) VAR_7 = 0;

        if (VAR_5 >= VAR_4 && VAR_5 >= VAR_6) VAR_7 = 1; 



        av_dlog(ctx, "VAR_2 #%02X [%6d..%-6d] (%6d) w:%-6"PRIu64" ranges:[%2x %2x %2x] sort by %c (already sorted:%c) ",

                VAR_1, VAR_2->start, VAR_2->start + VAR_2->len - 1, VAR_2->len, box_weight,

                VAR_4, VAR_5, VAR_6, "VAR_9"[VAR_7], VAR_2->sorted_by == VAR_7 ? 'y':'n');



        

        if (VAR_2->sorted_by != VAR_7) {

            cmp_func cmpf = cmp_funcs[VAR_7];

            AV_QSORT(&s->refs[VAR_2->start], VAR_2->len, const struct color_ref *, cmpf);

            VAR_2->sorted_by = VAR_7;

        }



        

        median = (box_weight + 1) >> 1;

        box_weight = 0;

        



        for (VAR_3 = VAR_2->start; VAR_3 < VAR_2->start + VAR_2->len - 2; VAR_3++) {

            box_weight += s->refs[VAR_3]->count;

            if (box_weight > median)

                break;

        }

        av_dlog(ctx, "split @ VAR_3=%-6d with w=%-6"PRIu64" (target=%6"PRIu64")\n", VAR_3, box_weight, median);

        split_box(s, VAR_2, VAR_3);



        VAR_1 = get_next_box_id_to_split(s);

        VAR_2 = VAR_1 >= 0 ? &s->boxes[VAR_1] : NULL;

    }



    VAR_0 = set_colorquant_ratio_meta(out, s->nb_boxes, s->nb_refs);

    av_log(ctx, AV_LOG_INFO, "%d%s colors generated out of %d colors; VAR_0=%f\n",

           s->nb_boxes, s->reserve_transparent ? "(+1)" : "", s->nb_refs, VAR_0);



    qsort(s->boxes, s->nb_boxes, sizeof(*s->boxes), cmp_color);



    write_palette(ctx, out);



    return out;

}

static AVFrame *FUNC_0(AVFilterContext *ctx)
{ AVFrame *out; PaletteGenContext *s = ctx->priv; AVFilterLink *outlink = ctx->outputs[0]; double VAR_0; int VAR_1 = 0; struct range_box *VAR_2; s->refs = load_color_refs(s->histogram, s->nb_refs); if (!s->refs) { av_log(ctx, AV_LOG_ERROR, "Unable to allocate references for %d different colors\n", s->nb_refs); return NULL; } out = ff_get_video_buffer(outlink, outlink->w, outlink->h); if (!out)
return NULL; out->pts = 0; VAR_2 = &s->boxes[VAR_1]; VAR_2->len = s->nb_refs; VAR_2->sorted_by = -1; VAR_2->color = get_avg_color(s->refs, VAR_2); VAR_2->variance = -1; s->nb_boxes = 1; while (VAR_2 && VAR_2->len > 1) { int VAR_3, VAR_4, VAR_5, VAR_6, VAR_7; uint64_t median, box_weight = 0; uint8_t min[3] = {0xff, 0xff, 0xff}; uint8_t max[3] = {0x00, 0x00, 0x00}; for (VAR_3 = VAR_2->start; VAR_3 < VAR_2->start + VAR_2->len; VAR_3++) { const struct color_ref *VAR_8 = s->refs[VAR_3]; const uint32_t VAR_9 = VAR_8->color; const uint8_t VAR_10 = VAR_9 >> 16 & 0xff, g = VAR_9 >> 8 & 0xff, b = VAR_9 & 0xff; min[0] = FFMIN(VAR_10, min[0]), max[0] = FFMAX(VAR_10, max[0]); min[1] = FFMIN(g, min[1]), max[1] = FFMAX(g, max[1]); min[2] = FFMIN(b, min[2]), max[2] = FFMAX(b, max[2]); box_weight += VAR_8->count; } VAR_4 = max[0] - min[0]; VAR_5 = max[1] - min[1]; VAR_6 = max[2] - min[2]; VAR_7 = 1; if (VAR_6 >= VAR_4 && VAR_6 >= VAR_5) VAR_7 = 2; if (VAR_4 >= VAR_5 && VAR_4 >= VAR_6) VAR_7 = 0; if (VAR_5 >= VAR_4 && VAR_5 >= VAR_6) VAR_7 = 1; av_dlog(ctx, "VAR_2 #%02X [%6d..%-6d] (%6d) w:%-6"PRIu64" ranges:[%2x %2x %2x] sort by %c (already sorted:%c) ",
VAR_1, VAR_2->start, VAR_2->start + VAR_2->len - 1, VAR_2->len, box_weight,
VAR_4, VAR_5, VAR_6, "VAR_9"[VAR_7], VAR_2->sorted_by == VAR_7 ? 'y':'n'); if (VAR_2->sorted_by != VAR_7) { cmp_func cmpf = cmp_funcs[VAR_7]; AV_QSORT(&s->refs[VAR_2->start], VAR_2->len, const struct color_ref *, cmpf); VAR_2->sorted_by = VAR_7; } median = (box_weight + 1) >> 1; box_weight = 0; for (VAR_3 = VAR_2->start; VAR_3 < VAR_2->start + VAR_2->len - 2; VAR_3++) { box_weight += s->refs[VAR_3]->count; if (box_weight > median)
break; } av_dlog(ctx, "split @ VAR_3=%-6d with w=%-6"PRIu64" (target=%6"PRIu64")\n", VAR_3, box_weight, median); split_box(s, VAR_2, VAR_3); VAR_1 = get_next_box_id_to_split(s); VAR_2 = VAR_1 >= 0 ? &s->boxes[VAR_1] : NULL; } VAR_0 = set_colorquant_ratio_meta(out, s->nb_boxes, s->nb_refs); av_log(ctx, AV_LOG_INFO, "%d%s colors generated out of %d colors; VAR_0=%f\n", s->nb_boxes, s->reserve_transparent ? "(+1)" : "", s->nb_refs, VAR_0); qsort(s->boxes, s->nb_boxes, sizeof(*s->boxes), cmp_color); write_palette(ctx, out); return out; }