X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;ds=sidebyside;f=libavcodec%2Felbg.c;h=85319db78e2be01873a8bf2f9585f9f8bb4d766e;hb=5b423887f4dcf2b200689e472b9e239a1cf86a6b;hp=c2393e70baa559157ddf2206147f679ce0d56f15;hpb=ddcf4b5d6d19fcbf4cb632a7d9a9be4ac9e6738f;p=ffmpeg diff --git a/libavcodec/elbg.c b/libavcodec/elbg.c index c2393e70baa..85319db78e2 100644 --- a/libavcodec/elbg.c +++ b/libavcodec/elbg.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2007 Vitor + * Copyright (C) 2007 Vitor Sessak * * This file is part of FFmpeg. * @@ -19,15 +19,15 @@ */ /** - * @file cbook_gen.c + * @file libavcodec/elbg.c * Codebook Generator using the ELBG algorithm */ #include +#include "libavutil/lfg.h" #include "elbg.h" #include "avcodec.h" -#include "random.h" #define DELTA_ERR_MAX 0.1 ///< Precision of the ELBG algorithm (as percentual error) @@ -52,7 +52,7 @@ typedef struct{ int *utility_inc; int *nearest_cb; int *points; - AVRandomState *rand_state; + AVLFG *rand_state; } elbg_data; static inline int distance_limited(int *a, int *b, int dim, int limit) @@ -105,9 +105,12 @@ 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_random(elbg->rand_state)%elbg->utility_inc[elbg->numCB-1]; + int r = av_lfg_get(elbg->rand_state)%elbg->utility_inc[elbg->numCB-1] + 1; while (elbg->utility_inc[i] < r) i++; + + assert(!elbg->cells[i]); + return i; } @@ -115,7 +118,7 @@ static int get_high_utility_cell(elbg_data *elbg) * Implementation of the simple LBG algorithm for just two codebooks */ static int simple_lbg(int dim, - int centroid[3][dim], + int *centroid[3], int newutility[3], int *points, cell *cells) @@ -186,7 +189,7 @@ static void get_new_centroids(elbg_data *elbg, int huc, int *newcentroid_i, * @param newcentroid A vector with the position of the new centroids */ static void shift_codebook(elbg_data *elbg, int *indexes, - int newcentroid[3][elbg->dim]) + int *newcentroid[3]) { cell *tempdata; cell **pp = &elbg->cells[indexes[2]]; @@ -246,8 +249,13 @@ static void try_shift_candidate(elbg_data *elbg, int idx[3]) int j, k, olderror=0, newerror, cont=0; int newutility[3]; int newcentroid[3][elbg->dim]; + int *newcentroid_ptrs[3]; cell *tempcell; + newcentroid_ptrs[0] = newcentroid[0]; + newcentroid_ptrs[1] = newcentroid[1]; + newcentroid_ptrs[2] = newcentroid[2]; + for (j=0; j<3; j++) olderror += elbg->utility[idx[j]]; @@ -269,11 +277,11 @@ static void try_shift_candidate(elbg_data *elbg, int idx[3]) newerror = newutility[2]; - newerror += simple_lbg(elbg->dim, newcentroid, newutility, elbg->points, + newerror += simple_lbg(elbg->dim, newcentroid_ptrs, newutility, elbg->points, elbg->cells[idx[1]]); if (olderror > newerror) { - shift_codebook(elbg, idx, newcentroid); + shift_codebook(elbg, idx, newcentroid_ptrs); elbg->error += newerror - olderror; @@ -301,7 +309,8 @@ static void do_shiftings(elbg_data *elbg) idx[1] = get_high_utility_cell(elbg); idx[2] = get_closest_codebook(elbg, idx[0]); - try_shift_candidate(elbg, idx); + if (idx[1] != idx[0] && idx[1] != idx[2]) + try_shift_candidate(elbg, idx); } } @@ -309,7 +318,7 @@ static void do_shiftings(elbg_data *elbg) void ff_init_elbg(int *points, int dim, int numpoints, int *codebook, int numCB, int max_steps, int *closest_cb, - AVRandomState *rand_state) + AVLFG *rand_state) { int i, k; @@ -336,7 +345,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, int numCB, int max_steps, int *closest_cb, - AVRandomState *rand_state) + AVLFG *rand_state) { int dist; elbg_data elbg_d;