/* 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;
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;
return sdiff;
}
-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;
-
/**
* Initialize cel evaluators and set their source coordinates
*/
* 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;
/**
* 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;
/**
* 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;
}
}
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 *codebook = enc->tmp_codebook_buf;
int *closest_cb = enc->closest_cb;
- if (!codebook)
- return AVERROR(ENOMEM);
-
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:
- 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 = enc->points;
- int bias;
-
- if (!results4 || !yuvClusters) {
- ret = AVERROR(ENOMEM);
- goto out;
- }
/* Subsample YUV data */
- create_clusters(enc->frame_to_enc, roq->width, roq->height, yuvClusters);
+ create_clusters(enc->frame_to_enc, roq->width, roq->height, points);
- /* 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];
- }
+ codebooks->numCB4 = (enc->quake3_compat ? MAX_CBS_4x4-1 : MAX_CBS_4x4);
/* Create 4x4 codebooks */
- if ((ret = generate_codebook(enc, 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);
+ if ((ret = generate_codebook(enc, points, max, enc->results4,
+ 4, codebooks->numCB4)) < 0)
+ return ret;
/* Create 2x2 codebooks */
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(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 = 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(enc->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 */
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);
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->closest_cb =
av_malloc_array(roq->width * roq->height, 3 * sizeof(int));
- if (!enc->tmpData || !enc->this_motion4 || !enc->last_motion4 ||
+ if (!enc->this_motion4 || !enc->last_motion4 ||
!enc->this_motion8 || !enc->last_motion8 || !enc->closest_cb)
return AVERROR(ENOMEM);
.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,