void ff_init_elbg(int *points, int dim, int numpoints, int *codebook, int numCB, int max_steps, int *closest_cb, AVLFG *rand_state) { int i, k; 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)); 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); av_free(temp_points); } else // If not, initialize the codebook with random positions for (i=0; i < numCB; i++) memcpy(codebook + i*dim, points + ((i*BIG_PRIME)%numpoints)*dim, dim*sizeof(int)); }
static void generate_codebook(RoqContext *enc, RoqTempdata *tempdata, int *points, int inputCount, roq_cell *results, int size, int cbsize) { int i, j, k; int c_size = size*size/4; int *buf; int *codebook = av_malloc(6*c_size*cbsize*sizeof(int)); int *closest_cb; if (size == 4) closest_cb = av_malloc(6*c_size*inputCount*sizeof(int)); else closest_cb = tempdata->closest_cb2; ff_init_elbg(points, 6*c_size, inputCount, codebook, cbsize, 1, closest_cb, &enc->randctx); ff_do_elbg(points, 6*c_size, inputCount, codebook, cbsize, 1, closest_cb, &enc->randctx); if (size == 4) av_free(closest_cb); buf = codebook; for (i=0; i<cbsize; i++) for (k=0; k<c_size; k++) { for(j=0; j<4; j++) results->y[j] = *buf++; results->u = (*buf++ + CHROMA_BIAS/2)/CHROMA_BIAS; results->v = (*buf++ + CHROMA_BIAS/2)/CHROMA_BIAS; results++; } av_free(codebook); }
static int a64multi_encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data) { A64Context *c = avctx->priv_data; AVFrame *pict = data; AVFrame *const p = (AVFrame *) & c->picture; int frame; int a; uint8_t colrammap[256]; int *charmap = c->mc_charmap; int *meta = c->mc_meta_charset; int *best_cb = c->mc_best_cb; int frm_size = 0x400 + 0x400 * c->mc_use_5col; int req_size; /* it is the last frame so prepare to flush */ if (!data) c->mc_lifetime = c->mc_frame_counter; req_size = 0x800 + frm_size * c->mc_lifetime; if (req_size > buf_size) { av_log(avctx, AV_LOG_ERROR, "buf size too small (need %d, got %d)\n", req_size, buf_size); return -1; } /* fill up mc_meta_charset with framedata until lifetime exceeds */ if (c->mc_frame_counter < c->mc_lifetime) { *p = *pict; p->pict_type = FF_I_TYPE; p->key_frame = 1; to_meta_with_crop(avctx, p, meta + 32000 * c->mc_frame_counter); c->mc_frame_counter++; /* lifetime is not reached */ return 0; } /* lifetime exceeded so now convert X frames at once */ if (c->mc_frame_counter == c->mc_lifetime && c->mc_lifetime > 0) { c->mc_frame_counter = 0; ff_init_elbg(meta, 32, 1000 * c-> mc_lifetime, best_cb, 256, 5, charmap, &c->randctx); ff_do_elbg (meta, 32, 1000 * c-> mc_lifetime, best_cb, 256, 5, charmap, &c->randctx); render_charset(avctx, buf, colrammap); for (frame = 0; frame < c->mc_lifetime; frame++) { for (a = 0; a < 1000; a++) { buf[0x800 + a] = charmap[a]; if (c->mc_use_5col) buf[0xc00 + a] = colrammap[charmap[a]]; } buf += frm_size; charmap += 1000; } return req_size; } return 0; }