]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/roqvideoenc.c
avutil/mem: Also poison new av_realloc-allocated blocks
[ffmpeg] / libavcodec / roqvideoenc.c
index f52c1f5454d72390dc9a955626d746d80f930cb7..b755d5e3a5d7f3be69651fb7e2c830be309e905b 100644 (file)
 /* 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;
@@ -93,7 +145,13 @@ typedef struct RoqEncContext {
 
     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
@@ -207,78 +265,23 @@ static inline int squared_diff_macroblock(uint8_t a[], uint8_t b[], int size)
     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;
@@ -423,9 +426,10 @@ static void motion_search(RoqEncContext *enc, int blocksize)
  * 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;
@@ -464,7 +468,7 @@ static void gather_data_for_subcel(SubcelEvaluation *subcel, int x,
     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);
@@ -487,10 +491,10 @@ static void gather_data_for_subcel(SubcelEvaluation *subcel, int x,
 /**
  * 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;
@@ -522,10 +526,10 @@ static void gather_data_for_cel(CelEvaluation *cel, RoqEncContext *enc,
         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;
@@ -562,9 +566,10 @@ static void gather_data_for_cel(CelEvaluation *cel, RoqEncContext *enc,
         }
 }
 
-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 */
@@ -595,9 +600,10 @@ static void remap_codebooks(RoqEncContext *enc, RoqTempdata *tempData)
 /**
  * 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;
 
@@ -651,10 +657,10 @@ static void write_typecode(CodingSpool *s, uint8_t type)
 }
 
 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;
@@ -680,7 +686,7 @@ static void reconstruct_and_encode_image(RoqEncContext *enc,
     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;
@@ -772,7 +778,7 @@ static void reconstruct_and_encode_image(RoqEncContext *enc,
 /**
  * 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;
@@ -786,14 +792,14 @@ static inline void frame_block_to_cell(uint8_t *block, uint8_t * const *data,
             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;
 
@@ -801,42 +807,30 @@ static void create_clusters(const AVFrame *frame, int w, int h, uint8_t *yuvClus
         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++)
@@ -848,56 +842,32 @@ static int generate_codebook(RoqEncContext *enc, RoqTempdata *tempdata,
             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;
 
@@ -908,7 +878,7 @@ static int generate_new_codebooks(RoqEncContext *enc, RoqTempdata *tempData)
     /* 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);
         }
@@ -917,26 +887,19 @@ static int generate_new_codebooks(RoqEncContext *enc, RoqTempdata *tempData)
         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;
 
@@ -947,7 +910,7 @@ static int roq_encode_video(RoqEncContext *enc)
 
  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) {
@@ -970,11 +933,11 @@ static int roq_encode_video(RoqEncContext *enc)
         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 */
@@ -982,9 +945,6 @@ static int roq_encode_video(RoqEncContext *enc)
     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;
@@ -997,7 +957,8 @@ static av_cold int roq_encode_end(AVCodecContext *avctx)
     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);
@@ -1040,8 +1001,6 @@ static av_cold int roq_encode_init(AVCodecContext *avctx)
     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));
 
@@ -1054,11 +1013,18 @@ static av_cold int roq_encode_init(AVCodecContext *avctx)
     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)
@@ -1153,7 +1119,7 @@ static const AVClass roq_class = {
     .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,