/* The cast is useful when multiplying it by INT_MAX */
#define ROQ_LAMBDA_SCALE ((uint64_t) FF_LAMBDA_SCALE)
+typedef struct RoqCodebooks {
+ int numCB4;
+ int numCB2;
+ int usedCB2[MAX_CBS_2x2];
+ int usedCB4[MAX_CBS_4x4];
+ uint8_t unpacked_cb2[MAX_CBS_2x2*2*2*3];
+ uint8_t unpacked_cb4[MAX_CBS_4x4*4*4*3];
+ uint8_t unpacked_cb4_enlarged[MAX_CBS_4x4*8*8*3];
+} RoqCodebooks;
+
+/**
+ * Temporary vars
+ */
+typedef struct RoqTempData
+{
+ int f2i4[MAX_CBS_4x4];
+ int i2f4[MAX_CBS_4x4];
+ int f2i2[MAX_CBS_2x2];
+ int i2f2[MAX_CBS_2x2];
+
+ int mainChunkSize;
+
+ int numCB4;
+ int numCB2;
+
+ RoqCodebooks codebooks;
+
+ int used_option[4];
+} RoqTempData;
+
+typedef struct SubcelEvaluation {
+ int eval_dist[4];
+ int best_bit_use;
+ int best_coding;
+
+ int subCels[4];
+ motion_vect motion;
+ int cbEntry;
+} SubcelEvaluation;
+
+typedef struct CelEvaluation {
+ int eval_dist[4];
+ int best_coding;
+
+ SubcelEvaluation subCels[4];
+
+ motion_vect motion;
+ int cbEntry;
+
+ int sourceX, sourceY;
+} CelEvaluation;
+
typedef struct RoqEncContext {
RoqContext common;
AVLFG randctx;
const AVFrame *frame_to_enc;
uint8_t *out_buf;
- struct RoqTempData *tmpData;
+ RoqTempData tmp_data;
+ roq_cell results4[4 * MAX_CBS_4x4];
+ int tmp_codebook_buf[FFMAX(24 * MAX_CBS_4x4, 6 * MAX_CBS_2x2)];
+
+ CelEvaluation *cel_evals;
+ int *closest_cb;
+ int *points; // Allocated together with closest_cb
int first_frame;
int quake3_compat; // Quake 3 compatibility option
return sdiff;
}
-typedef struct SubcelEvaluation {
- int eval_dist[4];
- int best_bit_use;
- int best_coding;
-
- int subCels[4];
- motion_vect motion;
- int cbEntry;
-} SubcelEvaluation;
-
-typedef struct CelEvaluation {
- int eval_dist[4];
- int best_coding;
-
- SubcelEvaluation subCels[4];
-
- motion_vect motion;
- int cbEntry;
-
- int sourceX, sourceY;
-} CelEvaluation;
-
-typedef struct RoqCodebooks {
- int numCB4;
- int numCB2;
- int usedCB2[MAX_CBS_2x2];
- int usedCB4[MAX_CBS_4x4];
- uint8_t unpacked_cb2[MAX_CBS_2x2*2*2*3];
- uint8_t unpacked_cb4[MAX_CBS_4x4*4*4*3];
- uint8_t unpacked_cb4_enlarged[MAX_CBS_4x4*8*8*3];
-} RoqCodebooks;
-
-/**
- * Temporary vars
- */
-typedef struct RoqTempData
-{
- CelEvaluation *cel_evals;
-
- int f2i4[MAX_CBS_4x4];
- int i2f4[MAX_CBS_4x4];
- int f2i2[MAX_CBS_2x2];
- int i2f2[MAX_CBS_2x2];
-
- int mainChunkSize;
-
- int numCB4;
- int numCB2;
-
- RoqCodebooks codebooks;
-
- int *closest_cb2;
- int used_option[4];
-} RoqTempdata;
-
/**
* Initialize cel evaluators and set their source coordinates
*/
-static int create_cel_evals(RoqContext *enc, RoqTempdata *tempData)
+static int create_cel_evals(RoqEncContext *enc)
{
- int n=0, x, y, i;
+ RoqContext *const roq = &enc->common;
- tempData->cel_evals = av_malloc_array(enc->width*enc->height/64, sizeof(CelEvaluation));
- if (!tempData->cel_evals)
+ enc->cel_evals = av_malloc_array(roq->width * roq->height / 64, sizeof(CelEvaluation));
+ if (!enc->cel_evals)
return AVERROR(ENOMEM);
/* Map to the ROQ quadtree order */
- for (y=0; y<enc->height; y+=16)
- for (x=0; x<enc->width; x+=16)
- for(i=0; i<4; i++) {
- tempData->cel_evals[n ].sourceX = x + (i&1)*8;
- tempData->cel_evals[n++].sourceY = y + (i&2)*4;
+ for (int y = 0, n = 0; y < roq->height; y += 16)
+ for (int x = 0; x < roq->width; x += 16)
+ for(int i = 0; i < 4; i++) {
+ enc->cel_evals[n ].sourceX = x + (i&1)*8;
+ enc->cel_evals[n++].sourceY = y + (i&2)*4;
}
return 0;
* Get distortion for all options available to a subcel
*/
static void gather_data_for_subcel(SubcelEvaluation *subcel, int x,
- int y, RoqEncContext *enc, RoqTempdata *tempData)
+ int y, RoqEncContext *enc)
{
RoqContext *const roq = &enc->common;
+ RoqTempData *const tempData = &enc->tmp_data;
uint8_t mb4[4*4*3];
uint8_t mb2[2*2*3];
int cluster_index;
subcel->eval_dist[RoQ_ID_CCC] = 0;
for(i=0;i<4;i++) {
- subcel->subCels[i] = tempData->closest_cb2[cluster_index*4+i];
+ subcel->subCels[i] = enc->closest_cb[cluster_index*4+i];
get_frame_mb(enc->frame_to_enc, x+2*(i&1),
y+(i&2), mb2, 2);
/**
* Get distortion for all options available to a cel
*/
-static void gather_data_for_cel(CelEvaluation *cel, RoqEncContext *enc,
- RoqTempdata *tempData)
+static void gather_data_for_cel(CelEvaluation *cel, RoqEncContext *enc)
{
RoqContext *const roq = &enc->common;
+ RoqTempData *const tempData = &enc->tmp_data;
uint8_t mb8[8*8*3];
int index = cel->sourceY * roq->width / 64 + cel->sourceX/8;
int i, j, best_dist, divide_bit_use;
index_mb(mb8, tempData->codebooks.unpacked_cb4_enlarged,
tempData->codebooks.numCB4, &cel->cbEntry, 8);
- gather_data_for_subcel(cel->subCels + 0, cel->sourceX+0, cel->sourceY+0, enc, tempData);
- gather_data_for_subcel(cel->subCels + 1, cel->sourceX+4, cel->sourceY+0, enc, tempData);
- gather_data_for_subcel(cel->subCels + 2, cel->sourceX+0, cel->sourceY+4, enc, tempData);
- gather_data_for_subcel(cel->subCels + 3, cel->sourceX+4, cel->sourceY+4, enc, tempData);
+ gather_data_for_subcel(cel->subCels + 0, cel->sourceX+0, cel->sourceY+0, enc);
+ gather_data_for_subcel(cel->subCels + 1, cel->sourceX+4, cel->sourceY+0, enc);
+ gather_data_for_subcel(cel->subCels + 2, cel->sourceX+0, cel->sourceY+4, enc);
+ gather_data_for_subcel(cel->subCels + 3, cel->sourceX+4, cel->sourceY+4, enc);
cel->eval_dist[RoQ_ID_CCC] = 0;
divide_bit_use = 0;
}
}
-static void remap_codebooks(RoqEncContext *enc, RoqTempdata *tempData)
+static void remap_codebooks(RoqEncContext *enc)
{
RoqContext *const roq = &enc->common;
+ RoqTempData *const tempData = &enc->tmp_data;
int i, j, idx=0;
/* Make remaps for the final codebook usage */
/**
* Write codebook chunk
*/
-static void write_codebooks(RoqEncContext *enc, RoqTempdata *tempData)
+static void write_codebooks(RoqEncContext *enc)
{
RoqContext *const roq = &enc->common;
+ RoqTempData *const tempData = &enc->tmp_data;
int i, j;
uint8_t **outp= &enc->out_buf;
}
static void reconstruct_and_encode_image(RoqEncContext *enc,
- RoqTempdata *tempData,
int w, int h, int numBlocks)
{
RoqContext *const roq = &enc->common;
+ RoqTempData *const tempData = &enc->tmp_data;
int i, j, k;
int x, y;
int subX, subY;
bytestream_put_byte(&enc->out_buf, 0x0);
for (i=0; i<numBlocks; i++) {
- eval = tempData->cel_evals + i;
+ eval = enc->cel_evals + i;
x = eval->sourceX;
y = eval->sourceY;
/**
* Create a single YUV cell from a 2x2 section of the image
*/
-static inline void frame_block_to_cell(uint8_t *block, uint8_t * const *data,
+static inline void frame_block_to_cell(int *block, uint8_t * const *data,
int top, int left, const int *stride)
{
int i, j, u=0, v=0;
v += data[2][x];
}
- *block++ = (u+2)/4;
- *block++ = (v+2)/4;
+ *block++ = (u + 2) / 4 * CHROMA_BIAS;
+ *block++ = (v + 2) / 4 * CHROMA_BIAS;
}
/**
* Create YUV clusters for the entire image
*/
-static void create_clusters(const AVFrame *frame, int w, int h, uint8_t *yuvClusters)
+static void create_clusters(const AVFrame *frame, int w, int h, int *points)
{
int i, j, k, l;
for (j=0; j<w; j+=4) {
for (k=0; k < 2; k++)
for (l=0; l < 2; l++)
- frame_block_to_cell(yuvClusters + (l + 2*k)*6, frame->data,
+ frame_block_to_cell(points + (l + 2*k)*6, frame->data,
i+2*k, j+2*l, frame->linesize);
- yuvClusters += 24;
+ points += 24;
}
}
-static int generate_codebook(RoqEncContext *enc, RoqTempdata *tempdata,
+static int generate_codebook(RoqEncContext *enc,
int *points, int inputCount, roq_cell *results,
int size, int cbsize)
{
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 (!codebook)
- return AVERROR(ENOMEM);
-
- if (size == 4) {
- closest_cb = av_malloc_array(6*c_size, inputCount*sizeof(int));
- if (!closest_cb) {
- ret = AVERROR(ENOMEM);
- goto out;
- }
- } else
- closest_cb = tempdata->closest_cb2;
+ int *codebook = enc->tmp_codebook_buf;
+ int *closest_cb = enc->closest_cb;
ret = avpriv_init_elbg(points, 6 * c_size, inputCount, codebook,
cbsize, 1, closest_cb, &enc->randctx);
if (ret < 0)
- goto out;
+ return ret;
ret = avpriv_do_elbg(points, 6 * c_size, inputCount, codebook,
cbsize, 1, closest_cb, &enc->randctx);
if (ret < 0)
- goto out;
+ return ret;
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;
+ return 0;
}
-static int generate_new_codebooks(RoqEncContext *enc, RoqTempdata *tempData)
+static int generate_new_codebooks(RoqEncContext *enc)
{
int i, j, ret = 0;
- RoqCodebooks *codebooks = &tempData->codebooks;
+ RoqCodebooks *codebooks = &enc->tmp_data.codebooks;
RoqContext *const roq = &enc->common;
int max = roq->width * roq->height / 16;
uint8_t mb2[3*4];
- roq_cell *results4 = av_malloc(sizeof(roq_cell)*MAX_CBS_4x4*4);
- uint8_t *yuvClusters=av_malloc_array(max, sizeof(int)*6*4);
- int *points = av_malloc_array(max, 6*4*sizeof(int));
- int bias;
-
- if (!results4 || !yuvClusters || !points) {
- ret = AVERROR(ENOMEM);
- goto out;
- }
+ int *points = enc->points;
/* Subsample YUV data */
- create_clusters(enc->frame_to_enc, roq->width, roq->height, yuvClusters);
-
- /* Cast to integer and apply chroma bias */
- for (i=0; i<max*24; i++) {
- bias = ((i%6)<4) ? 1 : CHROMA_BIAS;
- points[i] = bias*yuvClusters[i];
- }
-
- /* Create 4x4 codebooks */
- if ((ret = generate_codebook(enc, tempData, points, max,
- results4, 4, (enc->quake3_compat ? MAX_CBS_4x4-1 : MAX_CBS_4x4))) < 0)
- goto out;
+ create_clusters(enc->frame_to_enc, roq->width, roq->height, points);
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 4x4 codebooks */
+ if ((ret = generate_codebook(enc, points, max, enc->results4,
+ 4, codebooks->numCB4)) < 0)
+ return ret;
/* Create 2x2 codebooks */
- if ((ret = generate_codebook(enc, tempData, points, max * 4,
+ if ((ret = generate_codebook(enc, points, max * 4,
roq->cb2x2, 2, MAX_CBS_2x2)) < 0)
- goto out;
+ return ret;
codebooks->numCB2 = MAX_CBS_2x2;
/* Index all 4x4 entries to the 2x2 entries, unpack, and enlarge */
for (i=0; i<codebooks->numCB4; i++) {
for (j=0; j<4; j++) {
- unpack_roq_cell(&results4[4*i + j], mb2);
+ unpack_roq_cell(&enc->results4[4*i + j], mb2);
index_mb(mb2, codebooks->unpacked_cb2, codebooks->numCB2,
&roq->cb4x4[i].idx[j], 2);
}
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;
+
+ return 0;
}
static int roq_encode_video(RoqEncContext *enc)
{
- RoqTempdata *tempData = enc->tmpData;
+ RoqTempData *const tempData = &enc->tmp_data;
RoqContext *const roq = &enc->common;
int ret;
memset(tempData, 0, sizeof(*tempData));
- ret = create_cel_evals(roq, tempData);
- if (ret < 0)
- return ret;
-
- ret = generate_new_codebooks(enc, tempData);
+ ret = generate_new_codebooks(enc);
if (ret < 0)
return ret;
retry_encode:
for (int i = 0; i < roq->width * roq->height / 64; i++)
- gather_data_for_cel(tempData->cel_evals + i, enc, tempData);
+ gather_data_for_cel(enc->cel_evals + i, enc);
/* Quake 3 can't handle chunks bigger than 65535 bytes */
if (tempData->mainChunkSize/8 > 65535 && enc->quake3_compat) {
goto retry_encode;
}
- remap_codebooks(enc, tempData);
+ remap_codebooks(enc);
- write_codebooks(enc, tempData);
+ write_codebooks(enc);
- reconstruct_and_encode_image(enc, tempData, roq->width, roq->height,
+ reconstruct_and_encode_image(enc, roq->width, roq->height,
roq->width * roq->height / 64);
/* Rotate frame history */
FFSWAP(motion_vect *, enc->last_motion4, enc->this_motion4);
FFSWAP(motion_vect *, enc->last_motion8, enc->this_motion8);
- av_freep(&tempData->cel_evals);
- av_freep(&tempData->closest_cb2);
-
enc->framesSinceKeyframe++;
return 0;
av_frame_free(&enc->common.current_frame);
av_frame_free(&enc->common.last_frame);
- av_freep(&enc->tmpData);
+ av_freep(&enc->cel_evals);
+ av_freep(&enc->closest_cb);
av_freep(&enc->this_motion4);
av_freep(&enc->last_motion4);
av_freep(&enc->this_motion8);
if (!roq->last_frame || !roq->current_frame)
return AVERROR(ENOMEM);
- enc->tmpData = av_malloc(sizeof(RoqTempdata));
-
enc->this_motion4 =
av_mallocz_array(roq->width * roq->height / 16, sizeof(motion_vect));
enc->last_motion8 =
av_malloc_array (roq->width * roq->height / 64, sizeof(motion_vect));
- if (!enc->tmpData || !enc->this_motion4 || !enc->last_motion4 ||
- !enc->this_motion8 || !enc->last_motion8)
+ /* 4x4 codebook needs 6 * 4 * 4 / 4 * width * height / 16 * sizeof(int);
+ * and so does the points buffer. */
+ enc->closest_cb =
+ av_malloc_array(roq->width * roq->height, 3 * sizeof(int));
+
+ if (!enc->this_motion4 || !enc->last_motion4 ||
+ !enc->this_motion8 || !enc->last_motion8 || !enc->closest_cb)
return AVERROR(ENOMEM);
- return 0;
+ enc->points = enc->closest_cb + roq->width * roq->height * 3 / 2;
+
+ return create_cel_evals(enc);
}
static void roq_write_video_info_chunk(RoqEncContext *enc)
.version = LIBAVUTIL_VERSION_INT,
};
-AVCodec ff_roq_encoder = {
+const AVCodec ff_roq_encoder = {
.name = "roqvideo",
.long_name = NULL_IF_CONFIG_SMALL("id RoQ video"),
.type = AVMEDIA_TYPE_VIDEO,