diff options
Diffstat (limited to 'libavcodec/elbg.c')
-rw-r--r-- | libavcodec/elbg.c | 55 |
1 files changed, 33 insertions, 22 deletions
diff --git a/libavcodec/elbg.c b/libavcodec/elbg.c index 0aa8e16165..9bbb6d88ac 100644 --- a/libavcodec/elbg.c +++ b/libavcodec/elbg.c @@ -1,20 +1,20 @@ /* * Copyright (C) 2007 Vitor Sessak <vitor1001@gmail.com> * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * - * Libav is distributed in the hope that it will be useful, + * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -25,6 +25,7 @@ #include <string.h> +#include "libavutil/avassert.h" #include "libavutil/common.h" #include "libavutil/lfg.h" #include "elbg.h" @@ -50,7 +51,7 @@ typedef struct{ int *codebook; cell **cells; int *utility; - int *utility_inc; + int64_t *utility_inc; int *nearest_cb; int *points; AVLFG *rand_state; @@ -107,11 +108,20 @@ static int get_high_utility_cell(elbg_data *elbg) { int i=0; /* Using linear search, do binary if it ever turns to be speed critical */ - int r = av_lfg_get(elbg->rand_state)%elbg->utility_inc[elbg->numCB-1] + 1; - while (elbg->utility_inc[i] < r) + uint64_t r; + + if (elbg->utility_inc[elbg->numCB-1] < INT_MAX) { + r = av_lfg_get(elbg->rand_state) % (unsigned int)elbg->utility_inc[elbg->numCB-1] + 1; + } else { + r = av_lfg_get(elbg->rand_state); + r = (av_lfg_get(elbg->rand_state) + (r<<32)) % elbg->utility_inc[elbg->numCB-1] + 1; + } + + while (elbg->utility_inc[i] < r) { i++; + } - assert(elbg->cells[i]); + av_assert2(elbg->cells[i]); return i; } @@ -189,7 +199,7 @@ static void get_new_centroids(elbg_data *elbg, int huc, int *newcentroid_i, /** * Add the points in the low utility cell to its closest cell. Split the high - * utility cell, putting the separed points in the (now empty) low utility + * utility cell, putting the separate points in the (now empty) low utility * cell. * * @param elbg Internal elbg data @@ -226,7 +236,8 @@ static void shift_codebook(elbg_data *elbg, int *indexes, static void evaluate_utility_inc(elbg_data *elbg) { - int i, inc=0; + int i; + int64_t inc=0; for (i=0; i < elbg->numCB; i++) { if (elbg->numCB*elbg->utility[i] > elbg->error) @@ -323,7 +334,7 @@ static void do_shiftings(elbg_data *elbg) #define BIG_PRIME 433494437LL -void ff_init_elbg(int *points, int dim, int numpoints, int *codebook, +void avpriv_init_elbg(int *points, int dim, int numpoints, int *codebook, int numCB, int max_steps, int *closest_cb, AVLFG *rand_state) { @@ -332,14 +343,14 @@ void ff_init_elbg(int *points, int dim, int numpoints, int *codebook, if (numpoints > 24*numCB) { /* ELBG is very costly for a big number of points. So if we have a lot of them, get a good initial codebook to save on iterations */ - int *temp_points = av_malloc(dim*(numpoints/8)*sizeof(int)); + int *temp_points = av_malloc_array(dim, (numpoints/8)*sizeof(int)); for (i=0; i<numpoints/8; i++) { k = (i*BIG_PRIME) % numpoints; memcpy(temp_points + i*dim, points + k*dim, dim*sizeof(int)); } - ff_init_elbg(temp_points, dim, numpoints/8, codebook, numCB, 2*max_steps, closest_cb, rand_state); - ff_do_elbg(temp_points, dim, numpoints/8, codebook, numCB, 2*max_steps, closest_cb, rand_state); + avpriv_init_elbg(temp_points, dim, numpoints/8, codebook, numCB, 2*max_steps, closest_cb, rand_state); + avpriv_do_elbg(temp_points, dim, numpoints/8, codebook, numCB, 2*max_steps, closest_cb, rand_state); av_free(temp_points); @@ -350,7 +361,7 @@ void ff_init_elbg(int *points, int dim, int numpoints, int *codebook, } -void ff_do_elbg(int *points, int dim, int numpoints, int *codebook, +void avpriv_do_elbg(int *points, int dim, int numpoints, int *codebook, int numCB, int max_steps, int *closest_cb, AVLFG *rand_state) { @@ -358,9 +369,9 @@ void ff_do_elbg(int *points, int dim, int numpoints, int *codebook, elbg_data elbg_d; elbg_data *elbg = &elbg_d; int i, j, k, last_error, steps=0; - int *dist_cb = av_malloc(numpoints*sizeof(int)); - int *size_part = av_malloc(numCB*sizeof(int)); - cell *list_buffer = av_malloc(numpoints*sizeof(cell)); + int *dist_cb = av_malloc_array(numpoints, sizeof(int)); + int *size_part = av_malloc_array(numCB, sizeof(int)); + cell *list_buffer = av_malloc_array(numpoints, sizeof(cell)); cell *free_cells; int best_dist, best_idx = 0; @@ -368,12 +379,12 @@ void ff_do_elbg(int *points, int dim, int numpoints, int *codebook, elbg->dim = dim; elbg->numCB = numCB; elbg->codebook = codebook; - elbg->cells = av_malloc(numCB*sizeof(cell *)); - elbg->utility = av_malloc(numCB*sizeof(int)); + elbg->cells = av_malloc_array(numCB, sizeof(cell *)); + elbg->utility = av_malloc_array(numCB, sizeof(int)); elbg->nearest_cb = closest_cb; elbg->points = points; - elbg->utility_inc = av_malloc(numCB*sizeof(int)); - elbg->scratchbuf = av_malloc(5*dim*sizeof(int)); + elbg->utility_inc = av_malloc_array(numCB, sizeof(*elbg->utility_inc)); + elbg->scratchbuf = av_malloc_array(5*dim, sizeof(int)); elbg->rand_state = rand_state; |