]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/roqvideoenc.c
Support the new lossless mode.
[ffmpeg] / libavcodec / roqvideoenc.c
index 58371824f786bfc22aa326890e65ee5835c2a544..3b405ccc90b1518d337fa1b4e8e121583f6d5057 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * RoQ Video Encoder.
  *
- * Copyright (C) 2007 Vitor <vitor1001@gmail.com>
+ * Copyright (C) 2007 Vitor Sessak <vitor1001@gmail.com>
  * Copyright (C) 2004-2007 Eric Lasota
  *    Based on RoQ specs (C) 2001 Tim Ferguson
  *
@@ -24,7 +24,7 @@
 
 /**
  * @file roqvideoenc.c
- * Id RoQ encoder by Vitor. Based on the Switchblade3 library and the
+ * id RoQ encoder by Vitor. Based on the Switchblade3 library and the
  * Switchblade3 FFmpeg glue by Eric Lasota.
  */
 
@@ -125,7 +125,7 @@ static inline int eval_sse(uint8_t *a, uint8_t *b, int count)
 // FIXME Could use DSPContext.sse, but it is not so speed critical (used
 // just for motion estimation).
 static int block_sse(uint8_t **buf1, uint8_t **buf2, int x1, int y1, int x2,
-                     int y2, int stride, int size)
+                     int y2, int *stride1, int *stride2, int size)
 {
     int i, k;
     int sse=0;
@@ -133,8 +133,8 @@ static int block_sse(uint8_t **buf1, uint8_t **buf2, int x1, int y1, int x2,
     for (k=0; k<3; k++) {
         int bias = (k ? CHROMA_BIAS : 4);
         for (i=0; i<size; i++)
-            sse += bias*eval_sse(buf1[k] + (y1+i)*stride + x1,
-                                 buf2[k] + (y2+i)*stride + x2, size);
+            sse += bias*eval_sse(buf1[k] + (y1+i)*stride1[k] + x1,
+                                 buf2[k] + (y2+i)*stride2[k] + x2, size);
     }
 
     return sse;
@@ -159,7 +159,9 @@ static int eval_motion_dist(RoqContext *enc, int x, int y, motion_vect vect,
         return INT_MAX;
 
     return block_sse(enc->frame_to_enc->data, enc->last_frame->data, x, y,
-                     mx, my, enc->y_stride, size);
+                     mx, my,
+                     enc->frame_to_enc->linesize, enc->last_frame->linesize,
+                     size);
 }
 
 /**
@@ -188,20 +190,20 @@ typedef struct
     int subCels[4];
     motion_vect motion;
     int cbEntry;
-} subcel_evaluation_t;
+} SubcelEvaluation;
 
 typedef struct
 {
     int eval_dist[4];
     int best_coding;
 
-    subcel_evaluation_t subCels[4];
+    SubcelEvaluation subCels[4];
 
     motion_vect motion;
     int cbEntry;
 
     int sourceX, sourceY;
-} cel_evaluation_t;
+} CelEvaluation;
 
 typedef struct
 {
@@ -212,14 +214,14 @@ typedef struct
     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];
-} roq_codebooks_t;
+} RoqCodebooks;
 
 /**
  * Temporary vars
  */
 typedef struct
 {
-    cel_evaluation_t *cel_evals;
+    CelEvaluation *cel_evals;
 
     int f2i4[MAX_CBS_4x4];
     int i2f4[MAX_CBS_4x4];
@@ -231,20 +233,20 @@ typedef struct
     int numCB4;
     int numCB2;
 
-    roq_codebooks_t codebooks;
+    RoqCodebooks codebooks;
 
     int *closest_cb2;
     int used_option[4];
-} roq_tempdata_t;
+} RoqTempdata;
 
 /**
  * Initializes cel evaluators and sets their source coordinates
  */
-static void create_cel_evals(RoqContext *enc, roq_tempdata_t *tempData)
+static void create_cel_evals(RoqContext *enc, RoqTempdata *tempData)
 {
     int n=0, x, y, i;
 
-    tempData->cel_evals = av_malloc(enc->width*enc->height/64 * sizeof(cel_evaluation_t));
+    tempData->cel_evals = av_malloc(enc->width*enc->height/64 * sizeof(CelEvaluation));
 
     /* Map to the ROQ quadtree order */
     for (y=0; y<enc->height; y+=16)
@@ -261,12 +263,13 @@ static void create_cel_evals(RoqContext *enc, roq_tempdata_t *tempData)
 static void get_frame_mb(AVFrame *frame, int x, int y, uint8_t mb[], int dim)
 {
     int i, j, cp;
-    int stride = frame->linesize[0];
 
-    for (cp=0; cp<3; cp++)
+    for (cp=0; cp<3; cp++) {
+        int stride = frame->linesize[cp];
         for (i=0; i<dim; i++)
             for (j=0; j<dim; j++)
                 *mb++ = frame->data[cp][(y+i)*stride + x + j];
+    }
 }
 
 /**
@@ -392,8 +395,8 @@ static void motion_search(RoqContext *enc, int blocksize)
 /**
  * Gets distortion for all options available to a subcel
  */
-static void gather_data_for_subcel(subcel_evaluation_t *subcel, int x,
-                                   int y, RoqContext *enc, roq_tempdata_t *tempData)
+static void gather_data_for_subcel(SubcelEvaluation *subcel, int x,
+                                   int y, RoqContext *enc, RoqTempdata *tempData)
 {
     uint8_t mb4[4*4*3];
     uint8_t mb2[2*2*3];
@@ -414,7 +417,10 @@ static void gather_data_for_subcel(subcel_evaluation_t *subcel, int x,
     if (enc->framesSinceKeyframe >= 2)
         subcel->eval_dist[RoQ_ID_MOT] = block_sse(enc->frame_to_enc->data,
                                                   enc->current_frame->data, x,
-                                                  y, x, y, enc->y_stride, 4);
+                                                  y, x, y,
+                                                  enc->frame_to_enc->linesize,
+                                                  enc->current_frame->linesize,
+                                                  4);
     else
         subcel->eval_dist[RoQ_ID_MOT] = INT_MAX;
 
@@ -453,8 +459,8 @@ static void gather_data_for_subcel(subcel_evaluation_t *subcel, int x,
 /**
  * Gets distortion for all options available to a cel
  */
-static void gather_data_for_cel(cel_evaluation_t *cel, RoqContext *enc,
-                                roq_tempdata_t *tempData)
+static void gather_data_for_cel(CelEvaluation *cel, RoqContext *enc,
+                                RoqTempdata *tempData)
 {
     uint8_t mb8[8*8*3];
     int index = cel->sourceY*enc->width/64 + cel->sourceX/8;
@@ -476,7 +482,8 @@ static void gather_data_for_cel(cel_evaluation_t *cel, RoqContext *enc,
                                                enc->current_frame->data,
                                                cel->sourceX, cel->sourceY,
                                                cel->sourceX, cel->sourceY,
-                                               enc->y_stride, 8);
+                                               enc->frame_to_enc->linesize,
+                                               enc->current_frame->linesize,8);
     else
         cel->eval_dist[RoQ_ID_MOT] = INT_MAX;
 
@@ -526,7 +533,7 @@ static void gather_data_for_cel(cel_evaluation_t *cel, RoqContext *enc,
         }
 }
 
-static void remap_codebooks(RoqContext *enc, roq_tempdata_t *tempData)
+static void remap_codebooks(RoqContext *enc, RoqTempdata *tempData)
 {
     int i, j, idx=0;
 
@@ -558,7 +565,7 @@ static void remap_codebooks(RoqContext *enc, roq_tempdata_t *tempData)
 /**
  * Write codebook chunk
  */
-static void write_codebooks(RoqContext *enc, roq_tempdata_t *tempData)
+static void write_codebooks(RoqContext *enc, RoqTempdata *tempData)
 {
     int i, j;
     uint8_t **outp= &enc->out_buf;
@@ -601,7 +608,7 @@ typedef struct
 /* NOTE: Typecodes must be spooled AFTER arguments!! */
 static void write_typecode(CodingSpool *s, uint8_t type)
 {
-    s->typeSpool |= ((type) & 3) << (14 - s->typeSpoolLength);
+    s->typeSpool |= (type & 3) << (14 - s->typeSpoolLength);
     s->typeSpoolLength += 2;
     if (s->typeSpoolLength == 16) {
         bytestream_put_le16(s->pout, s->typeSpool);
@@ -613,7 +620,7 @@ static void write_typecode(CodingSpool *s, uint8_t type)
     }
 }
 
-static void reconstruct_and_encode_image(RoqContext *enc, roq_tempdata_t *tempData, int w, int h, int numBlocks)
+static void reconstruct_and_encode_image(RoqContext *enc, RoqTempdata *tempData, int w, int h, int numBlocks)
 {
     int i, j, k;
     int x, y;
@@ -621,7 +628,7 @@ static void reconstruct_and_encode_image(RoqContext *enc, roq_tempdata_t *tempDa
     int dist=0;
 
     roq_qcell *qcell;
-    cel_evaluation_t *eval;
+    CelEvaluation *eval;
 
     CodingSpool spool;
 
@@ -737,7 +744,9 @@ static void reconstruct_and_encode_image(RoqContext *enc, roq_tempdata_t *tempDa
     av_log(enc->avctx, AV_LOG_ERROR, "Expected distortion: %i Actual: %i\n",
            dist,
            block_sse(fdata, cdata, 0, 0, 0, 0,
-                     enc->y_stride, enc->width));  //WARNING: Square dimensions implied...
+                     enc->frame_to_enc->linesize,
+                     enc->current_frame->linesize,
+                     enc->width));  //WARNING: Square dimensions implied...
 #endif
 }
 
@@ -754,6 +763,7 @@ static inline void frame_block_to_cell(uint8_t *block, uint8_t **data,
         for (j=0; j<2; j++) {
             int x = (top+i)*stride[0] + left + j;
             *block++ = data[0][x];
+            x = (top+i)*stride[1] + left + j;
             u       += data[1][x];
             v       += data[2][x];
         }
@@ -779,7 +789,7 @@ static void create_clusters(AVFrame *frame, int w, int h, uint8_t *yuvClusters)
         }
 }
 
-static void generate_codebook(RoqContext *enc, roq_tempdata_t *tempdata,
+static void generate_codebook(RoqContext *enc, RoqTempdata *tempdata,
                               int *points, int inputCount, roq_cell *results,
                               int size, int cbsize)
 {
@@ -814,10 +824,10 @@ static void generate_codebook(RoqContext *enc, roq_tempdata_t *tempdata,
     av_free(codebook);
 }
 
-static void generate_new_codebooks(RoqContext *enc, roq_tempdata_t *tempData)
+static void generate_new_codebooks(RoqContext *enc, RoqTempdata *tempData)
 {
     int i,j;
-    roq_codebooks_t *codebooks = &tempData->codebooks;
+    RoqCodebooks *codebooks = &tempData->codebooks;
     int max = enc->width*enc->height/16;
     uint8_t mb2[3*4];
     roq_cell *results4 = av_malloc(sizeof(roq_cell)*MAX_CBS_4x4*4);
@@ -870,7 +880,7 @@ static void generate_new_codebooks(RoqContext *enc, roq_tempdata_t *tempData)
 
 static void roq_encode_video(RoqContext *enc)
 {
-    roq_tempdata_t tempData;
+    RoqTempdata tempData;
     int i;
 
     memset(&tempData, 0, sizeof(tempData));
@@ -901,6 +911,8 @@ static void roq_encode_video(RoqContext *enc)
     reconstruct_and_encode_image(enc, &tempData, enc->width, enc->height,
                                  enc->width*enc->height/64);
 
+    enc->avctx->coded_frame = enc->current_frame;
+
     /* Rotate frame history */
     FFSWAP(AVFrame *, enc->current_frame, enc->last_frame);
     FFSWAP(motion_vect *, enc->last_motion4, enc->this_motion4);
@@ -989,8 +1001,6 @@ static int roq_encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_s
     uint8_t *buf_start = buf;
 
     enc->out_buf = buf;
-    enc->y_stride = frame->linesize[0];
-    enc->c_stride = frame->linesize[1];
     enc->avctx = avctx;
 
     enc->frame_to_enc = frame;
@@ -1057,5 +1067,6 @@ AVCodec roq_encoder =
     roq_encode_frame,
     roq_encode_end,
     .supported_framerates = (AVRational[]){{30,1}, {0,0}},
-    .pix_fmts = (enum PixelFormat[]){PIX_FMT_YUV444P, -1},
+    .pix_fmts = (enum PixelFormat[]){PIX_FMT_YUV444P, PIX_FMT_NONE},
+    .long_name = NULL_IF_CONFIG_SMALL("id RoQ video"),
 };