/**
* Initialize cel evaluators and set their source coordinates
*/
-static void create_cel_evals(RoqContext *enc, RoqTempdata *tempData)
+static int create_cel_evals(RoqContext *enc, RoqTempdata *tempData)
{
int n=0, x, y, i;
tempData->cel_evals = av_malloc_array(enc->width*enc->height/64, sizeof(CelEvaluation));
+ if (!tempData->cel_evals)
+ return AVERROR(ENOMEM);
/* Map to the ROQ quadtree order */
for (y=0; y<enc->height; y+=16)
tempData->cel_evals[n ].sourceX = x + (i&1)*8;
tempData->cel_evals[n++].sourceY = y + (i&2)*4;
}
+
+ return 0;
}
/**
}
}
-static void generate_codebook(RoqContext *enc, RoqTempdata *tempdata,
- int *points, int inputCount, roq_cell *results,
- int size, int cbsize)
+static int generate_codebook(RoqContext *enc, RoqTempdata *tempdata,
+ int *points, int inputCount, roq_cell *results,
+ int size, int cbsize)
{
- int i, j, k;
+ int i, j, k, ret = 0;
int c_size = size*size/4;
int *buf;
int *codebook = av_malloc_array(6*c_size, cbsize*sizeof(int));
int *closest_cb;
- if (size == 4)
+ if (!codebook)
+ return AVERROR(ENOMEM);
+
+ if (size == 4) {
closest_cb = av_malloc_array(6*c_size, inputCount*sizeof(int));
- else
+ if (!closest_cb) {
+ ret = AVERROR(ENOMEM);
+ goto out;
+ }
+ } else
closest_cb = tempdata->closest_cb2;
- avpriv_init_elbg(points, 6*c_size, inputCount, codebook, cbsize, 1, closest_cb, &enc->randctx);
- avpriv_do_elbg(points, 6*c_size, inputCount, codebook, cbsize, 1, closest_cb, &enc->randctx);
-
- if (size == 4)
- av_free(closest_cb);
+ ret = avpriv_init_elbg(points, 6 * c_size, inputCount, codebook,
+ cbsize, 1, closest_cb, &enc->randctx);
+ if (ret < 0)
+ goto out;
+ ret = avpriv_do_elbg(points, 6 * c_size, inputCount, codebook,
+ cbsize, 1, closest_cb, &enc->randctx);
+ if (ret < 0)
+ goto out;
buf = codebook;
for (i=0; i<cbsize; i++)
results->v = (*buf++ + CHROMA_BIAS/2)/CHROMA_BIAS;
results++;
}
-
+out:
+ if (size == 4)
+ av_free(closest_cb);
av_free(codebook);
+ return ret;
}
-static void generate_new_codebooks(RoqContext *enc, RoqTempdata *tempData)
+static int generate_new_codebooks(RoqContext *enc, RoqTempdata *tempData)
{
- int i,j;
+ int i, j, ret = 0;
RoqCodebooks *codebooks = &tempData->codebooks;
int max = enc->width*enc->height/16;
uint8_t mb2[3*4];
int *points = av_malloc_array(max, 6*4*sizeof(int));
int bias;
+ if (!results4 || !yuvClusters || !points) {
+ ret = AVERROR(ENOMEM);
+ goto out;
+ }
+
/* Subsample YUV data */
create_clusters(enc->frame_to_enc, enc->width, enc->height, yuvClusters);
}
/* Create 4x4 codebooks */
- generate_codebook(enc, tempData, points, max, results4, 4, (enc->quake3_compat ? MAX_CBS_4x4-1 : MAX_CBS_4x4));
+ if ((ret = generate_codebook(enc, tempData, points, max,
+ results4, 4, (enc->quake3_compat ? MAX_CBS_4x4-1 : MAX_CBS_4x4))) < 0)
+ goto out;
codebooks->numCB4 = (enc->quake3_compat ? MAX_CBS_4x4-1 : MAX_CBS_4x4);
tempData->closest_cb2 = av_malloc_array(max, 4*sizeof(int));
+ if (!tempData->closest_cb2) {
+ ret = AVERROR(ENOMEM);
+ goto out;
+ }
/* Create 2x2 codebooks */
- generate_codebook(enc, tempData, points, max*4, enc->cb2x2, 2, MAX_CBS_2x2);
+ if ((ret = generate_codebook(enc, tempData, points, max * 4,
+ enc->cb2x2, 2, MAX_CBS_2x2)) < 0)
+ goto out;
codebooks->numCB2 = MAX_CBS_2x2;
enlarge_roq_mb4(codebooks->unpacked_cb4 + i*4*4*3,
codebooks->unpacked_cb4_enlarged + i*8*8*3);
}
-
+out:
av_free(yuvClusters);
av_free(points);
av_free(results4);
+ return ret;
}
static int roq_encode_video(RoqContext *enc)
{
RoqTempdata *tempData = enc->tmpData;
- int i;
+ int i, ret;
memset(tempData, 0, sizeof(*tempData));
- create_cel_evals(enc, tempData);
+ ret = create_cel_evals(enc, tempData);
+ if (ret < 0)
+ return ret;
- generate_new_codebooks(enc, tempData);
+ ret = generate_new_codebooks(enc, tempData);
+ if (ret < 0)
+ return ret;
if (enc->framesSinceKeyframe >= 1) {
motion_search(enc, 8);
enc->last_motion8 =
av_malloc_array ((enc->width*enc->height/64), sizeof(motion_vect));
+ if (!enc->tmpData || !enc->this_motion4 || !enc->last_motion4 ||
+ !enc->this_motion8 || !enc->last_motion8) {
+ roq_encode_end(avctx);
+ return AVERROR(ENOMEM);
+ }
+
return 0;
}
}
/* Encode the actual frame */
- if ((ret = roq_encode_video(enc)) < 0)
+ ret = roq_encode_video(enc);
+ if (ret < 0)
return ret;
pkt->size = enc->out_buf - pkt->data;