Libav
roqvideoenc.c
Go to the documentation of this file.
1 /*
2  * RoQ Video Encoder.
3  *
4  * Copyright (C) 2007 Vitor Sessak <vitor1001@gmail.com>
5  * Copyright (C) 2004-2007 Eric Lasota
6  * Based on RoQ specs (C) 2001 Tim Ferguson
7  *
8  * This file is part of Libav.
9  *
10  * Libav is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * Libav is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with Libav; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24 
31 /*
32  * COSTS:
33  * Level 1:
34  * SKIP - 2 bits
35  * MOTION - 2 + 8 bits
36  * CODEBOOK - 2 + 8 bits
37  * SUBDIVIDE - 2 + combined subcel cost
38  *
39  * Level 2:
40  * SKIP - 2 bits
41  * MOTION - 2 + 8 bits
42  * CODEBOOK - 2 + 8 bits
43  * SUBDIVIDE - 2 + 4*8 bits
44  *
45  * Maximum cost: 138 bits per cel
46  *
47  * Proper evaluation requires LCD fraction comparison, which requires
48  * Squared Error (SE) loss * savings increase
49  *
50  * Maximum savings increase: 136 bits
51  * Maximum SE loss without overflow: 31580641
52  * Components in 8x8 supercel: 192
53  * Maximum SE precision per component: 164482
54  * >65025, so no truncation is needed (phew)
55  */
56 
57 #include <string.h>
58 
59 #include "libavutil/attributes.h"
60 #include "roqvideo.h"
61 #include "bytestream.h"
62 #include "elbg.h"
63 #include "internal.h"
64 #include "mathops.h"
65 
66 #define CHROMA_BIAS 1
67 
72 #define MAX_CBS_4x4 255
73 
74 #define MAX_CBS_2x2 256
75 
76 /* The cast is useful when multiplying it by INT_MAX */
77 #define ROQ_LAMBDA_SCALE ((uint64_t) FF_LAMBDA_SCALE)
78 
79 /* Macroblock support functions */
80 static void unpack_roq_cell(roq_cell *cell, uint8_t u[4*3])
81 {
82  memcpy(u , cell->y, 4);
83  memset(u+4, cell->u, 4);
84  memset(u+8, cell->v, 4);
85 }
86 
87 static void unpack_roq_qcell(uint8_t cb2[], roq_qcell *qcell, uint8_t u[4*4*3])
88 {
89  int i,cp;
90  static const int offsets[4] = {0, 2, 8, 10};
91 
92  for (cp=0; cp<3; cp++)
93  for (i=0; i<4; i++) {
94  u[4*4*cp + offsets[i] ] = cb2[qcell->idx[i]*2*2*3 + 4*cp ];
95  u[4*4*cp + offsets[i]+1] = cb2[qcell->idx[i]*2*2*3 + 4*cp+1];
96  u[4*4*cp + offsets[i]+4] = cb2[qcell->idx[i]*2*2*3 + 4*cp+2];
97  u[4*4*cp + offsets[i]+5] = cb2[qcell->idx[i]*2*2*3 + 4*cp+3];
98  }
99 }
100 
101 
102 static void enlarge_roq_mb4(uint8_t base[3*16], uint8_t u[3*64])
103 {
104  int x,y,cp;
105 
106  for(cp=0; cp<3; cp++)
107  for(y=0; y<8; y++)
108  for(x=0; x<8; x++)
109  *u++ = base[(y/2)*4 + (x/2) + 16*cp];
110 }
111 
112 static inline int square(int x)
113 {
114  return x*x;
115 }
116 
117 static inline int eval_sse(const uint8_t *a, const uint8_t *b, int count)
118 {
119  int diff=0;
120 
121  while(count--)
122  diff += square(*b++ - *a++);
123 
124  return diff;
125 }
126 
127 // FIXME Could use DSPContext.sse, but it is not so speed critical (used
128 // just for motion estimation).
129 static int block_sse(uint8_t * const *buf1, uint8_t * const *buf2, int x1, int y1,
130  int x2, int y2, const int *stride1, const int *stride2, int size)
131 {
132  int i, k;
133  int sse=0;
134 
135  for (k=0; k<3; k++) {
136  int bias = (k ? CHROMA_BIAS : 4);
137  for (i=0; i<size; i++)
138  sse += bias*eval_sse(buf1[k] + (y1+i)*stride1[k] + x1,
139  buf2[k] + (y2+i)*stride2[k] + x2, size);
140  }
141 
142  return sse;
143 }
144 
145 static int eval_motion_dist(RoqContext *enc, int x, int y, motion_vect vect,
146  int size)
147 {
148  int mx=vect.d[0];
149  int my=vect.d[1];
150 
151  if (mx < -7 || mx > 7)
152  return INT_MAX;
153 
154  if (my < -7 || my > 7)
155  return INT_MAX;
156 
157  mx += x;
158  my += y;
159 
160  if ((unsigned) mx > enc->width-size || (unsigned) my > enc->height-size)
161  return INT_MAX;
162 
163  return block_sse(enc->frame_to_enc->data, enc->last_frame->data, x, y,
164  mx, my,
166  size);
167 }
168 
172 static inline int squared_diff_macroblock(uint8_t a[], uint8_t b[], int size)
173 {
174  int cp, sdiff=0;
175 
176  for(cp=0;cp<3;cp++) {
177  int bias = (cp ? CHROMA_BIAS : 4);
178  sdiff += bias*eval_sse(a, b, size*size);
179  a += size*size;
180  b += size*size;
181  }
182 
183  return sdiff;
184 }
185 
186 typedef struct SubcelEvaluation {
187  int eval_dist[4];
190 
191  int subCels[4];
193  int cbEntry;
195 
196 typedef struct CelEvaluation {
197  int eval_dist[4];
199 
201 
203  int cbEntry;
204 
205  int sourceX, sourceY;
206 } CelEvaluation;
207 
208 typedef struct RoqCodebooks {
209  int numCB4;
210  int numCB2;
211  int usedCB2[MAX_CBS_2x2];
212  int usedCB4[MAX_CBS_4x4];
213  uint8_t unpacked_cb2[MAX_CBS_2x2*2*2*3];
214  uint8_t unpacked_cb4[MAX_CBS_4x4*4*4*3];
215  uint8_t unpacked_cb4_enlarged[MAX_CBS_4x4*8*8*3];
216 } RoqCodebooks;
217 
221 typedef struct RoqTempData
222 {
224 
225  int f2i4[MAX_CBS_4x4];
226  int i2f4[MAX_CBS_4x4];
227  int f2i2[MAX_CBS_2x2];
228  int i2f2[MAX_CBS_2x2];
229 
231 
232  int numCB4;
233  int numCB2;
234 
236 
238  int used_option[4];
239 } RoqTempdata;
240 
244 static int create_cel_evals(RoqContext *enc, RoqTempdata *tempData)
245 {
246  int n=0, x, y, i;
247 
248  tempData->cel_evals = av_malloc(enc->width*enc->height/64 * sizeof(CelEvaluation));
249  if (!tempData->cel_evals)
250  return AVERROR(ENOMEM);
251 
252  /* Map to the ROQ quadtree order */
253  for (y=0; y<enc->height; y+=16)
254  for (x=0; x<enc->width; x+=16)
255  for(i=0; i<4; i++) {
256  tempData->cel_evals[n ].sourceX = x + (i&1)*8;
257  tempData->cel_evals[n++].sourceY = y + (i&2)*4;
258  }
259 
260  return 0;
261 }
262 
266 static void get_frame_mb(const AVFrame *frame, int x, int y, uint8_t mb[], int dim)
267 {
268  int i, j, cp;
269 
270  for (cp=0; cp<3; cp++) {
271  int stride = frame->linesize[cp];
272  for (i=0; i<dim; i++)
273  for (j=0; j<dim; j++)
274  *mb++ = frame->data[cp][(y+i)*stride + x + j];
275  }
276 }
277 
281 static int index_mb(uint8_t cluster[], uint8_t cb[], int numCB,
282  int *outIndex, int dim)
283 {
284  int i, lDiff = INT_MAX, pick=0;
285 
286  /* Diff against the others */
287  for (i=0; i<numCB; i++) {
288  int diff = squared_diff_macroblock(cluster, cb + i*dim*dim*3, dim);
289  if (diff < lDiff) {
290  lDiff = diff;
291  pick = i;
292  }
293  }
294 
295  *outIndex = pick;
296  return lDiff;
297 }
298 
299 #define EVAL_MOTION(MOTION) \
300  do { \
301  diff = eval_motion_dist(enc, j, i, MOTION, blocksize); \
302  \
303  if (diff < lowestdiff) { \
304  lowestdiff = diff; \
305  bestpick = MOTION; \
306  } \
307  } while(0)
308 
309 static void motion_search(RoqContext *enc, int blocksize)
310 {
311  static const motion_vect offsets[8] = {
312  {{ 0,-1}},
313  {{ 0, 1}},
314  {{-1, 0}},
315  {{ 1, 0}},
316  {{-1, 1}},
317  {{ 1,-1}},
318  {{-1,-1}},
319  {{ 1, 1}},
320  };
321 
322  int diff, lowestdiff, oldbest;
323  int off[3];
324  motion_vect bestpick = {{0,0}};
325  int i, j, k, offset;
326 
327  motion_vect *last_motion;
328  motion_vect *this_motion;
329  motion_vect vect, vect2;
330 
331  int max=(enc->width/blocksize)*enc->height/blocksize;
332 
333  if (blocksize == 4) {
334  last_motion = enc->last_motion4;
335  this_motion = enc->this_motion4;
336  } else {
337  last_motion = enc->last_motion8;
338  this_motion = enc->this_motion8;
339  }
340 
341  for (i=0; i<enc->height; i+=blocksize)
342  for (j=0; j<enc->width; j+=blocksize) {
343  lowestdiff = eval_motion_dist(enc, j, i, (motion_vect) {{0,0}},
344  blocksize);
345  bestpick.d[0] = 0;
346  bestpick.d[1] = 0;
347 
348  if (blocksize == 4)
349  EVAL_MOTION(enc->this_motion8[(i/8)*(enc->width/8) + j/8]);
350 
351  offset = (i/blocksize)*enc->width/blocksize + j/blocksize;
352  if (offset < max && offset >= 0)
353  EVAL_MOTION(last_motion[offset]);
354 
355  offset++;
356  if (offset < max && offset >= 0)
357  EVAL_MOTION(last_motion[offset]);
358 
359  offset = (i/blocksize + 1)*enc->width/blocksize + j/blocksize;
360  if (offset < max && offset >= 0)
361  EVAL_MOTION(last_motion[offset]);
362 
363  off[0]= (i/blocksize)*enc->width/blocksize + j/blocksize - 1;
364  off[1]= off[0] - enc->width/blocksize + 1;
365  off[2]= off[1] + 1;
366 
367  if (i) {
368 
369  for(k=0; k<2; k++)
370  vect.d[k]= mid_pred(this_motion[off[0]].d[k],
371  this_motion[off[1]].d[k],
372  this_motion[off[2]].d[k]);
373 
374  EVAL_MOTION(vect);
375  for(k=0; k<3; k++)
376  EVAL_MOTION(this_motion[off[k]]);
377  } else if(j)
378  EVAL_MOTION(this_motion[off[0]]);
379 
380  vect = bestpick;
381 
382  oldbest = -1;
383  while (oldbest != lowestdiff) {
384  oldbest = lowestdiff;
385  for (k=0; k<8; k++) {
386  vect2 = vect;
387  vect2.d[0] += offsets[k].d[0];
388  vect2.d[1] += offsets[k].d[1];
389  EVAL_MOTION(vect2);
390  }
391  vect = bestpick;
392  }
393  offset = (i/blocksize)*enc->width/blocksize + j/blocksize;
394  this_motion[offset] = bestpick;
395  }
396 }
397 
401 static void gather_data_for_subcel(SubcelEvaluation *subcel, int x,
402  int y, RoqContext *enc, RoqTempdata *tempData)
403 {
404  uint8_t mb4[4*4*3];
405  uint8_t mb2[2*2*3];
406  int cluster_index;
407  int i, best_dist;
408 
409  static const int bitsUsed[4] = {2, 10, 10, 34};
410 
411  if (enc->framesSinceKeyframe >= 1) {
412  subcel->motion = enc->this_motion4[y*enc->width/16 + x/4];
413 
414  subcel->eval_dist[RoQ_ID_FCC] =
415  eval_motion_dist(enc, x, y,
416  enc->this_motion4[y*enc->width/16 + x/4], 4);
417  } else
418  subcel->eval_dist[RoQ_ID_FCC] = INT_MAX;
419 
420  if (enc->framesSinceKeyframe >= 2)
422  enc->current_frame->data, x,
423  y, x, y,
424  enc->frame_to_enc->linesize,
425  enc->current_frame->linesize,
426  4);
427  else
428  subcel->eval_dist[RoQ_ID_MOT] = INT_MAX;
429 
430  cluster_index = y*enc->width/16 + x/4;
431 
432  get_frame_mb(enc->frame_to_enc, x, y, mb4, 4);
433 
434  subcel->eval_dist[RoQ_ID_SLD] = index_mb(mb4,
435  tempData->codebooks.unpacked_cb4,
436  tempData->codebooks.numCB4,
437  &subcel->cbEntry, 4);
438 
439  subcel->eval_dist[RoQ_ID_CCC] = 0;
440 
441  for(i=0;i<4;i++) {
442  subcel->subCels[i] = tempData->closest_cb2[cluster_index*4+i];
443 
444  get_frame_mb(enc->frame_to_enc, x+2*(i&1),
445  y+(i&2), mb2, 2);
446 
447  subcel->eval_dist[RoQ_ID_CCC] +=
448  squared_diff_macroblock(tempData->codebooks.unpacked_cb2 + subcel->subCels[i]*2*2*3, mb2, 2);
449  }
450 
451  best_dist = INT_MAX;
452  for (i=0; i<4; i++)
453  if (ROQ_LAMBDA_SCALE*subcel->eval_dist[i] + enc->lambda*bitsUsed[i] <
454  best_dist) {
455  subcel->best_coding = i;
456  subcel->best_bit_use = bitsUsed[i];
457  best_dist = ROQ_LAMBDA_SCALE*subcel->eval_dist[i] +
458  enc->lambda*bitsUsed[i];
459  }
460 }
461 
466  RoqTempdata *tempData)
467 {
468  uint8_t mb8[8*8*3];
469  int index = cel->sourceY*enc->width/64 + cel->sourceX/8;
470  int i, j, best_dist, divide_bit_use;
471 
472  int bitsUsed[4] = {2, 10, 10, 0};
473 
474  if (enc->framesSinceKeyframe >= 1) {
475  cel->motion = enc->this_motion8[index];
476 
477  cel->eval_dist[RoQ_ID_FCC] =
478  eval_motion_dist(enc, cel->sourceX, cel->sourceY,
479  enc->this_motion8[index], 8);
480  } else
481  cel->eval_dist[RoQ_ID_FCC] = INT_MAX;
482 
483  if (enc->framesSinceKeyframe >= 2)
485  enc->current_frame->data,
486  cel->sourceX, cel->sourceY,
487  cel->sourceX, cel->sourceY,
488  enc->frame_to_enc->linesize,
489  enc->current_frame->linesize,8);
490  else
491  cel->eval_dist[RoQ_ID_MOT] = INT_MAX;
492 
493  get_frame_mb(enc->frame_to_enc, cel->sourceX, cel->sourceY, mb8, 8);
494 
495  cel->eval_dist[RoQ_ID_SLD] =
497  tempData->codebooks.numCB4, &cel->cbEntry, 8);
498 
499  gather_data_for_subcel(cel->subCels + 0, cel->sourceX+0, cel->sourceY+0, enc, tempData);
500  gather_data_for_subcel(cel->subCels + 1, cel->sourceX+4, cel->sourceY+0, enc, tempData);
501  gather_data_for_subcel(cel->subCels + 2, cel->sourceX+0, cel->sourceY+4, enc, tempData);
502  gather_data_for_subcel(cel->subCels + 3, cel->sourceX+4, cel->sourceY+4, enc, tempData);
503 
504  cel->eval_dist[RoQ_ID_CCC] = 0;
505  divide_bit_use = 0;
506  for (i=0; i<4; i++) {
507  cel->eval_dist[RoQ_ID_CCC] +=
508  cel->subCels[i].eval_dist[cel->subCels[i].best_coding];
509  divide_bit_use += cel->subCels[i].best_bit_use;
510  }
511 
512  best_dist = INT_MAX;
513  bitsUsed[3] = 2 + divide_bit_use;
514 
515  for (i=0; i<4; i++)
516  if (ROQ_LAMBDA_SCALE*cel->eval_dist[i] + enc->lambda*bitsUsed[i] <
517  best_dist) {
518  cel->best_coding = i;
519  best_dist = ROQ_LAMBDA_SCALE*cel->eval_dist[i] +
520  enc->lambda*bitsUsed[i];
521  }
522 
523  tempData->used_option[cel->best_coding]++;
524  tempData->mainChunkSize += bitsUsed[cel->best_coding];
525 
526  if (cel->best_coding == RoQ_ID_SLD)
527  tempData->codebooks.usedCB4[cel->cbEntry]++;
528 
529  if (cel->best_coding == RoQ_ID_CCC)
530  for (i=0; i<4; i++) {
531  if (cel->subCels[i].best_coding == RoQ_ID_SLD)
532  tempData->codebooks.usedCB4[cel->subCels[i].cbEntry]++;
533  else if (cel->subCels[i].best_coding == RoQ_ID_CCC)
534  for (j=0; j<4; j++)
535  tempData->codebooks.usedCB2[cel->subCels[i].subCels[j]]++;
536  }
537 }
538 
539 static void remap_codebooks(RoqContext *enc, RoqTempdata *tempData)
540 {
541  int i, j, idx=0;
542 
543  /* Make remaps for the final codebook usage */
544  for (i=0; i<MAX_CBS_4x4; i++) {
545  if (tempData->codebooks.usedCB4[i]) {
546  tempData->i2f4[i] = idx;
547  tempData->f2i4[idx] = i;
548  for (j=0; j<4; j++)
549  tempData->codebooks.usedCB2[enc->cb4x4[i].idx[j]]++;
550  idx++;
551  }
552  }
553 
554  tempData->numCB4 = idx;
555 
556  idx = 0;
557  for (i=0; i<MAX_CBS_2x2; i++) {
558  if (tempData->codebooks.usedCB2[i]) {
559  tempData->i2f2[i] = idx;
560  tempData->f2i2[idx] = i;
561  idx++;
562  }
563  }
564  tempData->numCB2 = idx;
565 
566 }
567 
571 static void write_codebooks(RoqContext *enc, RoqTempdata *tempData)
572 {
573  int i, j;
574  uint8_t **outp= &enc->out_buf;
575 
576  if (tempData->numCB2) {
577  bytestream_put_le16(outp, RoQ_QUAD_CODEBOOK);
578  bytestream_put_le32(outp, tempData->numCB2*6 + tempData->numCB4*4);
579  bytestream_put_byte(outp, tempData->numCB4);
580  bytestream_put_byte(outp, tempData->numCB2);
581 
582  for (i=0; i<tempData->numCB2; i++) {
583  bytestream_put_buffer(outp, enc->cb2x2[tempData->f2i2[i]].y, 4);
584  bytestream_put_byte(outp, enc->cb2x2[tempData->f2i2[i]].u);
585  bytestream_put_byte(outp, enc->cb2x2[tempData->f2i2[i]].v);
586  }
587 
588  for (i=0; i<tempData->numCB4; i++)
589  for (j=0; j<4; j++)
590  bytestream_put_byte(outp, tempData->i2f2[enc->cb4x4[tempData->f2i4[i]].idx[j]]);
591 
592  }
593 }
594 
595 static inline uint8_t motion_arg(motion_vect mot)
596 {
597  uint8_t ax = 8 - ((uint8_t) mot.d[0]);
598  uint8_t ay = 8 - ((uint8_t) mot.d[1]);
599  return ((ax&15)<<4) | (ay&15);
600 }
601 
602 typedef struct CodingSpool {
605  uint8_t argumentSpool[64];
608 } CodingSpool;
609 
610 /* NOTE: Typecodes must be spooled AFTER arguments!! */
611 static void write_typecode(CodingSpool *s, uint8_t type)
612 {
613  s->typeSpool |= (type & 3) << (14 - s->typeSpoolLength);
614  s->typeSpoolLength += 2;
615  if (s->typeSpoolLength == 16) {
616  bytestream_put_le16(s->pout, s->typeSpool);
618  s->args - s->argumentSpool);
619  s->typeSpoolLength = 0;
620  s->typeSpool = 0;
621  s->args = s->argumentSpool;
622  }
623 }
624 
625 static void reconstruct_and_encode_image(RoqContext *enc, RoqTempdata *tempData, int w, int h, int numBlocks)
626 {
627  int i, j, k;
628  int x, y;
629  int subX, subY;
630  int dist=0;
631 
632  roq_qcell *qcell;
633  CelEvaluation *eval;
634 
635  CodingSpool spool;
636 
637  spool.typeSpool=0;
638  spool.typeSpoolLength=0;
639  spool.args = spool.argumentSpool;
640  spool.pout = &enc->out_buf;
641 
642  if (tempData->used_option[RoQ_ID_CCC]%2)
643  tempData->mainChunkSize+=8; //FIXME
644 
645  /* Write the video chunk header */
646  bytestream_put_le16(&enc->out_buf, RoQ_QUAD_VQ);
647  bytestream_put_le32(&enc->out_buf, tempData->mainChunkSize/8);
648  bytestream_put_byte(&enc->out_buf, 0x0);
649  bytestream_put_byte(&enc->out_buf, 0x0);
650 
651  for (i=0; i<numBlocks; i++) {
652  eval = tempData->cel_evals + i;
653 
654  x = eval->sourceX;
655  y = eval->sourceY;
656  dist += eval->eval_dist[eval->best_coding];
657 
658  switch (eval->best_coding) {
659  case RoQ_ID_MOT:
660  write_typecode(&spool, RoQ_ID_MOT);
661  break;
662 
663  case RoQ_ID_FCC:
664  bytestream_put_byte(&spool.args, motion_arg(eval->motion));
665 
666  write_typecode(&spool, RoQ_ID_FCC);
667  ff_apply_motion_8x8(enc, x, y,
668  eval->motion.d[0], eval->motion.d[1]);
669  break;
670 
671  case RoQ_ID_SLD:
672  bytestream_put_byte(&spool.args, tempData->i2f4[eval->cbEntry]);
673  write_typecode(&spool, RoQ_ID_SLD);
674 
675  qcell = enc->cb4x4 + eval->cbEntry;
676  ff_apply_vector_4x4(enc, x , y , enc->cb2x2 + qcell->idx[0]);
677  ff_apply_vector_4x4(enc, x+4, y , enc->cb2x2 + qcell->idx[1]);
678  ff_apply_vector_4x4(enc, x , y+4, enc->cb2x2 + qcell->idx[2]);
679  ff_apply_vector_4x4(enc, x+4, y+4, enc->cb2x2 + qcell->idx[3]);
680  break;
681 
682  case RoQ_ID_CCC:
683  write_typecode(&spool, RoQ_ID_CCC);
684 
685  for (j=0; j<4; j++) {
686  subX = x + 4*(j&1);
687  subY = y + 2*(j&2);
688 
689  switch(eval->subCels[j].best_coding) {
690  case RoQ_ID_MOT:
691  break;
692 
693  case RoQ_ID_FCC:
694  bytestream_put_byte(&spool.args,
695  motion_arg(eval->subCels[j].motion));
696 
697  ff_apply_motion_4x4(enc, subX, subY,
698  eval->subCels[j].motion.d[0],
699  eval->subCels[j].motion.d[1]);
700  break;
701 
702  case RoQ_ID_SLD:
703  bytestream_put_byte(&spool.args,
704  tempData->i2f4[eval->subCels[j].cbEntry]);
705 
706  qcell = enc->cb4x4 + eval->subCels[j].cbEntry;
707 
708  ff_apply_vector_2x2(enc, subX , subY ,
709  enc->cb2x2 + qcell->idx[0]);
710  ff_apply_vector_2x2(enc, subX+2, subY ,
711  enc->cb2x2 + qcell->idx[1]);
712  ff_apply_vector_2x2(enc, subX , subY+2,
713  enc->cb2x2 + qcell->idx[2]);
714  ff_apply_vector_2x2(enc, subX+2, subY+2,
715  enc->cb2x2 + qcell->idx[3]);
716  break;
717 
718  case RoQ_ID_CCC:
719  for (k=0; k<4; k++) {
720  int cb_idx = eval->subCels[j].subCels[k];
721  bytestream_put_byte(&spool.args,
722  tempData->i2f2[cb_idx]);
723 
724  ff_apply_vector_2x2(enc, subX + 2*(k&1), subY + (k&2),
725  enc->cb2x2 + cb_idx);
726  }
727  break;
728  }
729  write_typecode(&spool, eval->subCels[j].best_coding);
730  }
731  break;
732  }
733  }
734 
735  /* Flush the remainder of the argument/type spool */
736  while (spool.typeSpoolLength)
737  write_typecode(&spool, 0x0);
738 }
739 
740 
744 static inline void frame_block_to_cell(uint8_t *block, uint8_t * const *data,
745  int top, int left, const int *stride)
746 {
747  int i, j, u=0, v=0;
748 
749  for (i=0; i<2; i++)
750  for (j=0; j<2; j++) {
751  int x = (top+i)*stride[0] + left + j;
752  *block++ = data[0][x];
753  x = (top+i)*stride[1] + left + j;
754  u += data[1][x];
755  v += data[2][x];
756  }
757 
758  *block++ = (u+2)/4;
759  *block++ = (v+2)/4;
760 }
761 
765 static void create_clusters(const AVFrame *frame, int w, int h, uint8_t *yuvClusters)
766 {
767  int i, j, k, l;
768 
769  for (i=0; i<h; i+=4)
770  for (j=0; j<w; j+=4) {
771  for (k=0; k < 2; k++)
772  for (l=0; l < 2; l++)
773  frame_block_to_cell(yuvClusters + (l + 2*k)*6, frame->data,
774  i+2*k, j+2*l, frame->linesize);
775  yuvClusters += 24;
776  }
777 }
778 
779 static int generate_codebook(RoqContext *enc, RoqTempdata *tempdata,
780  int *points, int inputCount, roq_cell *results,
781  int size, int cbsize)
782 {
783  int i, j, k, ret = 0;
784  int c_size = size*size/4;
785  int *buf;
786  int *codebook = av_malloc(6*c_size*cbsize*sizeof(int));
787  int *closest_cb;
788 
789  if (!codebook)
790  return AVERROR(ENOMEM);
791 
792  if (size == 4) {
793  closest_cb = av_malloc(6*c_size*inputCount*sizeof(int));
794  if (!closest_cb) {
795  ret = AVERROR(ENOMEM);
796  goto out;
797  }
798  } else
799  closest_cb = tempdata->closest_cb2;
800 
801  ret = ff_init_elbg(points, 6 * c_size, inputCount, codebook,
802  cbsize, 1, closest_cb, &enc->randctx);
803  if (ret < 0)
804  goto out;
805  ret = ff_do_elbg(points, 6 * c_size, inputCount, codebook,
806  cbsize, 1, closest_cb, &enc->randctx);
807  if (ret < 0)
808  goto out;
809 
810  buf = codebook;
811  for (i=0; i<cbsize; i++)
812  for (k=0; k<c_size; k++) {
813  for(j=0; j<4; j++)
814  results->y[j] = *buf++;
815 
816  results->u = (*buf++ + CHROMA_BIAS/2)/CHROMA_BIAS;
817  results->v = (*buf++ + CHROMA_BIAS/2)/CHROMA_BIAS;
818  results++;
819  }
820 out:
821  if (size == 4)
822  av_free(closest_cb);
823  av_free(codebook);
824  return ret;
825 }
826 
827 static int generate_new_codebooks(RoqContext *enc, RoqTempdata *tempData)
828 {
829  int i, j, ret = 0;
830  RoqCodebooks *codebooks = &tempData->codebooks;
831  int max = enc->width*enc->height/16;
832  uint8_t mb2[3*4];
833  roq_cell *results4 = av_malloc(sizeof(roq_cell)*MAX_CBS_4x4*4);
834  uint8_t *yuvClusters=av_malloc(sizeof(int)*max*6*4);
835  int *points = av_malloc(max*6*4*sizeof(int));
836  int bias;
837 
838  if (!results4 || !yuvClusters || !points) {
839  ret = AVERROR(ENOMEM);
840  goto out;
841  }
842 
843  /* Subsample YUV data */
844  create_clusters(enc->frame_to_enc, enc->width, enc->height, yuvClusters);
845 
846  /* Cast to integer and apply chroma bias */
847  for (i=0; i<max*24; i++) {
848  bias = ((i%6)<4) ? 1 : CHROMA_BIAS;
849  points[i] = bias*yuvClusters[i];
850  }
851 
852  /* Create 4x4 codebooks */
853  if ((ret = generate_codebook(enc, tempData, points, max,
854  results4, 4, MAX_CBS_4x4)) < 0)
855  goto out;
856 
857  codebooks->numCB4 = MAX_CBS_4x4;
858 
859  tempData->closest_cb2 = av_malloc(max*4*sizeof(int));
860  if (!tempData->closest_cb2) {
861  ret = AVERROR(ENOMEM);
862  goto out;
863  }
864 
865  /* Create 2x2 codebooks */
866  if ((ret = generate_codebook(enc, tempData, points, max * 4,
867  enc->cb2x2, 2, MAX_CBS_2x2)) < 0)
868  goto out;
869 
870  codebooks->numCB2 = MAX_CBS_2x2;
871 
872  /* Unpack 2x2 codebook clusters */
873  for (i=0; i<codebooks->numCB2; i++)
874  unpack_roq_cell(enc->cb2x2 + i, codebooks->unpacked_cb2 + i*2*2*3);
875 
876  /* Index all 4x4 entries to the 2x2 entries, unpack, and enlarge */
877  for (i=0; i<codebooks->numCB4; i++) {
878  for (j=0; j<4; j++) {
879  unpack_roq_cell(&results4[4*i + j], mb2);
880  index_mb(mb2, codebooks->unpacked_cb2, codebooks->numCB2,
881  &enc->cb4x4[i].idx[j], 2);
882  }
883  unpack_roq_qcell(codebooks->unpacked_cb2, enc->cb4x4 + i,
884  codebooks->unpacked_cb4 + i*4*4*3);
885  enlarge_roq_mb4(codebooks->unpacked_cb4 + i*4*4*3,
886  codebooks->unpacked_cb4_enlarged + i*8*8*3);
887  }
888 out:
889  av_free(yuvClusters);
890  av_free(points);
891  av_free(results4);
892  return ret;
893 }
894 
895 static int roq_encode_video(RoqContext *enc)
896 {
897  RoqTempdata *tempData = enc->tmpData;
898  int i, ret;
899 
900  memset(tempData, 0, sizeof(*tempData));
901 
902  ret = create_cel_evals(enc, tempData);
903  if (ret < 0)
904  return ret;
905 
906  ret = generate_new_codebooks(enc, tempData);
907  if (ret < 0)
908  return ret;
909 
910  if (enc->framesSinceKeyframe >= 1) {
911  motion_search(enc, 8);
912  motion_search(enc, 4);
913  }
914 
915  retry_encode:
916  for (i=0; i<enc->width*enc->height/64; i++)
917  gather_data_for_cel(tempData->cel_evals + i, enc, tempData);
918 
919  /* Quake 3 can't handle chunks bigger than 65535 bytes */
920  if (tempData->mainChunkSize/8 > 65535) {
921  av_log(enc->avctx, AV_LOG_ERROR,
922  "Warning, generated a frame too big (%d > 65535), "
923  "try using a smaller qscale value.\n",
924  tempData->mainChunkSize/8);
925  enc->lambda *= 1.5;
926  tempData->mainChunkSize = 0;
927  memset(tempData->used_option, 0, sizeof(tempData->used_option));
928  memset(tempData->codebooks.usedCB4, 0,
929  sizeof(tempData->codebooks.usedCB4));
930  memset(tempData->codebooks.usedCB2, 0,
931  sizeof(tempData->codebooks.usedCB2));
932 
933  goto retry_encode;
934  }
935 
936  remap_codebooks(enc, tempData);
937 
938  write_codebooks(enc, tempData);
939 
940  reconstruct_and_encode_image(enc, tempData, enc->width, enc->height,
941  enc->width*enc->height/64);
942 
943  /* Rotate frame history */
944  FFSWAP(AVFrame *, enc->current_frame, enc->last_frame);
947 
948  av_free(tempData->cel_evals);
949  av_free(tempData->closest_cb2);
950 
951  enc->framesSinceKeyframe++;
952 
953  return 0;
954 }
955 
957 {
958  RoqContext *enc = avctx->priv_data;
959 
961  av_frame_free(&enc->last_frame);
962 
963  av_free(enc->tmpData);
964  av_free(enc->this_motion4);
965  av_free(enc->last_motion4);
966  av_free(enc->this_motion8);
967  av_free(enc->last_motion8);
968 
969  return 0;
970 }
971 
973 {
974  RoqContext *enc = avctx->priv_data;
975 
976  av_lfg_init(&enc->randctx, 1);
977 
978  enc->avctx = avctx;
979 
980  enc->framesSinceKeyframe = 0;
981  if ((avctx->width & 0xf) || (avctx->height & 0xf)) {
982  av_log(avctx, AV_LOG_ERROR, "Dimensions must be divisible by 16\n");
983  return -1;
984  }
985 
986  if (((avctx->width)&(avctx->width-1))||((avctx->height)&(avctx->height-1)))
987  av_log(avctx, AV_LOG_ERROR, "Warning: dimensions not power of two\n");
988 
989  enc->width = avctx->width;
990  enc->height = avctx->height;
991 
992  enc->framesSinceKeyframe = 0;
993  enc->first_frame = 1;
994 
995  enc->last_frame = av_frame_alloc();
996  enc->current_frame = av_frame_alloc();
997  if (!enc->last_frame || !enc->current_frame) {
998  roq_encode_end(avctx);
999  return AVERROR(ENOMEM);
1000  }
1001 
1002  enc->tmpData = av_malloc(sizeof(RoqTempdata));
1003 
1004  enc->this_motion4 =
1005  av_mallocz((enc->width*enc->height/16)*sizeof(motion_vect));
1006 
1007  enc->last_motion4 =
1008  av_malloc ((enc->width*enc->height/16)*sizeof(motion_vect));
1009 
1010  enc->this_motion8 =
1011  av_mallocz((enc->width*enc->height/64)*sizeof(motion_vect));
1012 
1013  enc->last_motion8 =
1014  av_malloc ((enc->width*enc->height/64)*sizeof(motion_vect));
1015 
1016  return 0;
1017 }
1018 
1020 {
1021  /* ROQ info chunk */
1022  bytestream_put_le16(&enc->out_buf, RoQ_INFO);
1023 
1024  /* Size: 8 bytes */
1025  bytestream_put_le32(&enc->out_buf, 8);
1026 
1027  /* Unused argument */
1028  bytestream_put_byte(&enc->out_buf, 0x00);
1029  bytestream_put_byte(&enc->out_buf, 0x00);
1030 
1031  /* Width */
1032  bytestream_put_le16(&enc->out_buf, enc->width);
1033 
1034  /* Height */
1035  bytestream_put_le16(&enc->out_buf, enc->height);
1036 
1037  /* Unused in Quake 3, mimics the output of the real encoder */
1038  bytestream_put_byte(&enc->out_buf, 0x08);
1039  bytestream_put_byte(&enc->out_buf, 0x00);
1040  bytestream_put_byte(&enc->out_buf, 0x04);
1041  bytestream_put_byte(&enc->out_buf, 0x00);
1042 }
1043 
1044 static int roq_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
1045  const AVFrame *frame, int *got_packet)
1046 {
1047  RoqContext *enc = avctx->priv_data;
1048  int size, ret;
1049 
1050  enc->avctx = avctx;
1051 
1052  enc->frame_to_enc = frame;
1053 
1054  if (frame->quality)
1055  enc->lambda = frame->quality - 1;
1056  else
1057  enc->lambda = 2*ROQ_LAMBDA_SCALE;
1058 
1059  /* 138 bits max per 8x8 block +
1060  * 256 codebooks*(6 bytes 2x2 + 4 bytes 4x4) + 8 bytes frame header */
1061  size = ((enc->width * enc->height / 64) * 138 + 7) / 8 + 256 * (6 + 4) + 8;
1062  if ((ret = ff_alloc_packet(pkt, size)) < 0) {
1063  av_log(avctx, AV_LOG_ERROR, "Error getting output packet with size %d.\n", size);
1064  return ret;
1065  }
1066  enc->out_buf = pkt->data;
1067 
1068  /* Check for I-frame */
1069  if (enc->framesSinceKeyframe == avctx->gop_size)
1070  enc->framesSinceKeyframe = 0;
1071 
1072  if (enc->first_frame) {
1073  /* Alloc memory for the reconstruction data (we must know the stride
1074  for that) */
1075  if (ff_get_buffer(avctx, enc->current_frame, 0) ||
1076  ff_get_buffer(avctx, enc->last_frame, 0)) {
1077  av_log(avctx, AV_LOG_ERROR, " RoQ: get_buffer() failed\n");
1078  return -1;
1079  }
1080 
1081  /* Before the first video frame, write a "video info" chunk */
1083 
1084  enc->first_frame = 0;
1085  }
1086 
1087  /* Encode the actual frame */
1088  ret = roq_encode_video(enc);
1089  if (ret < 0)
1090  return ret;
1091 
1092  pkt->size = enc->out_buf - pkt->data;
1093  if (enc->framesSinceKeyframe == 1)
1094  pkt->flags |= AV_PKT_FLAG_KEY;
1095  *got_packet = 1;
1096 
1097  return 0;
1098 }
1099 
1101  .name = "roqvideo",
1102  .long_name = NULL_IF_CONFIG_SMALL("id RoQ video"),
1103  .type = AVMEDIA_TYPE_VIDEO,
1104  .id = AV_CODEC_ID_ROQ,
1105  .priv_data_size = sizeof(RoqContext),
1106  .init = roq_encode_init,
1107  .encode2 = roq_encode_frame,
1108  .close = roq_encode_end,
1109  .supported_framerates = (const AVRational[]){ {30,1}, {0,0} },
1110  .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV444P,
1111  AV_PIX_FMT_NONE },
1112 };
SubcelEvaluation subCels[4]
Definition: roqvideoenc.c:200
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
int d[2]
Definition: roqvideo.h:39
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
motion_vect * this_motion4
Definition: roqvideo.h:61
const AVFrame * frame_to_enc
Definition: roqvideo.h:69
#define EVAL_MOTION(MOTION)
Definition: roqvideoenc.c:299
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:64
static int eval_motion_dist(RoqContext *enc, int x, int y, motion_vect vect, int size)
Definition: roqvideoenc.c:145
motion_vect motion
Definition: roqvideoenc.c:202
void ff_apply_vector_2x2(RoqContext *ri, int x, int y, roq_cell *cell)
Definition: roqvideo.c:41
int size
Definition: avcodec.h:1347
int f2i4[MAX_CBS_4x4]
Definition: roqvideoenc.c:225
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
static void remap_codebooks(RoqContext *enc, RoqTempdata *tempData)
Definition: roqvideoenc.c:539
#define RoQ_ID_FCC
Definition: roqvideo.h:81
static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride)
#define RoQ_INFO
Definition: roqvideo.h:74
AVCodec ff_roq_encoder
Definition: roqvideoenc.c:1100
motion_vect * this_motion8
Definition: roqvideo.h:64
AVFrame * current_frame
Definition: roqvideo.h:48
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:3120
Macro definitions for various function/variable attributes.
motion_vect * last_motion4
Definition: roqvideo.h:62
#define MAX_CBS_4x4
Maximum number of generated 4x4 codebooks.
Definition: roqvideoenc.c:72
int width
Definition: roqvideo.h:55
#define RoQ_ID_MOT
Definition: roqvideo.h:80
AVLFG randctx
Definition: roqvideo.h:58
static int16_t block[64]
Definition: dct.c:97
static int create_cel_evals(RoqContext *enc, RoqTempdata *tempData)
Initialize cel evaluators and set their source coordinates.
Definition: roqvideoenc.c:244
static void gather_data_for_subcel(SubcelEvaluation *subcel, int x, int y, RoqContext *enc, RoqTempdata *tempData)
Get distortion for all options available to a subcel.
Definition: roqvideoenc.c:401
static void roq_write_video_info_chunk(RoqContext *enc)
Definition: roqvideoenc.c:1019
uint8_t
#define av_cold
Definition: attributes.h:66
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:68
#define RoQ_QUAD_CODEBOOK
Definition: roqvideo.h:75
#define b
Definition: input.c:52
RoqCodebooks codebooks
Definition: roqvideoenc.c:235
uint8_t * args
Definition: roqvideoenc.c:606
unsigned int framesSinceKeyframe
Definition: roqvideo.h:67
int usedCB4[MAX_CBS_4x4]
Definition: roqvideoenc.c:212
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:1346
int ff_init_elbg(int *points, int dim, int numpoints, int *codebook, int numCB, int max_steps, int *closest_cb, AVLFG *rand_state)
Initialize the **codebook vector for the elbg algorithm.
Definition: elbg.c:326
uint8_t ** pout
Definition: roqvideoenc.c:607
#define ROQ_LAMBDA_SCALE
Definition: roqvideoenc.c:77
uint8_t unpacked_cb2[MAX_CBS_2x2 *2 *2 *3]
Definition: roqvideoenc.c:213
void ff_apply_motion_4x4(RoqContext *ri, int x, int y, int deltax, int deltay)
Definition: roqvideo.c:133
void ff_apply_motion_8x8(RoqContext *ri, int x, int y, int deltax, int deltay)
Definition: roqvideo.c:139
#define MAX_CBS_2x2
Maximum number of 2x2 codebooks.
Definition: roqvideoenc.c:74
int mainChunkSize
Definition: roqvideoenc.c:230
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1378
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:190
#define CHROMA_BIAS
Definition: roqvideoenc.c:66
static void unpack_roq_cell(roq_cell *cell, uint8_t u[4 *3])
Definition: roqvideoenc.c:80
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:80
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
roq_qcell cb4x4[256]
Definition: roqvideo.h:52
const char * name
Name of the codec implementation.
Definition: avcodec.h:3127
static int square(int x)
Definition: roqvideoenc.c:112
static int index_mb(uint8_t cluster[], uint8_t cb[], int numCB, int *outIndex, int dim)
Find the codebook with the lowest distortion from an image.
Definition: roqvideoenc.c:281
#define RoQ_ID_CCC
Definition: roqvideo.h:83
unsigned char u
Definition: roqvideo.h:31
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1352
static int block_sse(uint8_t *const *buf1, uint8_t *const *buf2, int x1, int y1, int x2, int y2, const int *stride1, const int *stride2, int size)
Definition: roqvideoenc.c:129
static void frame_block_to_cell(uint8_t *block, uint8_t *const *data, int top, int left, const int *stride)
Create a single YUV cell from a 2x2 section of the image.
Definition: roqvideoenc.c:744
static void write_codebooks(RoqContext *enc, RoqTempdata *tempData)
Write codebook chunk.
Definition: roqvideoenc.c:571
static void write_typecode(CodingSpool *s, uint8_t type)
Definition: roqvideoenc.c:611
static int squared_diff_macroblock(uint8_t a[], uint8_t b[], int size)
Definition: roqvideoenc.c:172
In the ELBG jargon, a cell is the set of points that are closest to a codebook entry.
Definition: elbg.c:38
int width
picture width / height.
Definition: avcodec.h:1580
struct RoqTempData * tmpData
Definition: roqvideo.h:71
int typeSpoolLength
Definition: roqvideoenc.c:604
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
Definition: frame.h:239
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1211
static void motion_search(RoqContext *enc, int blocksize)
Definition: roqvideoenc.c:309
static uint8_t motion_arg(motion_vect mot)
Definition: roqvideoenc.c:595
static int roq_encode_video(RoqContext *enc)
Definition: roqvideoenc.c:895
if(ac->has_optimized_func)
int idx[4]
Definition: roqvideo.h:35
static av_cold int roq_encode_end(AVCodecContext *avctx)
Definition: roqvideoenc.c:956
int first_frame
Definition: roqvideo.h:49
Temporary vars.
Definition: roqvideoenc.c:221
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:158
int i2f2[MAX_CBS_2x2]
Definition: roqvideoenc.c:228
main external API structure.
Definition: avcodec.h:1409
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:589
#define RoQ_ID_SLD
Definition: roqvideo.h:82
unsigned char y[4]
Definition: roqvideo.h:30
static int eval_sse(const uint8_t *a, const uint8_t *b, int count)
Definition: roqvideoenc.c:117
uint64_t lambda
Definition: roqvideo.h:59
int index
Definition: gxfenc.c:72
rational number numerator/denominator
Definition: rational.h:43
static int generate_new_codebooks(RoqContext *enc, RoqTempdata *tempData)
Definition: roqvideoenc.c:827
#define mid_pred
Definition: mathops.h:99
int dim
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:30
#define u(width,...)
static void gather_data_for_cel(CelEvaluation *cel, RoqContext *enc, RoqTempdata *tempData)
Get distortion for all options available to a cel.
Definition: roqvideoenc.c:465
int used_option[4]
Definition: roqvideoenc.c:238
int ff_do_elbg(int *points, int dim, int numpoints, int *codebook, int numCB, int max_steps, int *closest_cb, AVLFG *rand_state)
Implementation of the Enhanced LBG Algorithm Based on the paper "Neural Networks 14:1219-1237" that c...
Definition: elbg.c:360
uint8_t argumentSpool[64]
Definition: roqvideoenc.c:605
static int roq_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: roqvideoenc.c:1044
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:146
static av_cold int roq_encode_init(AVCodecContext *avctx)
Definition: roqvideoenc.c:972
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:1606
static int generate_codebook(RoqContext *enc, RoqTempdata *tempdata, int *points, int inputCount, roq_cell *results, int size, int cbsize)
Definition: roqvideoenc.c:779
CelEvaluation * cel_evals
Definition: roqvideoenc.c:223
AVCodecContext * avctx
Definition: roqvideo.h:46
uint8_t * out_buf
Definition: roqvideo.h:70
common internal api header.
static void get_frame_mb(const AVFrame *frame, int x, int y, uint8_t mb[], int dim)
Get macroblocks from parts of the image.
Definition: roqvideoenc.c:266
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:582
motion_vect * last_motion8
Definition: roqvideo.h:65
motion_vect motion
Definition: roqvideoenc.c:192
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition: bytestream.h:365
int i2f4[MAX_CBS_4x4]
Definition: roqvideoenc.c:226
#define RoQ_QUAD_VQ
Definition: roqvideo.h:76
void * priv_data
Definition: avcodec.h:1451
static void enlarge_roq_mb4(uint8_t base[3 *16], uint8_t u[3 *64])
Definition: roqvideoenc.c:102
void ff_apply_vector_4x4(RoqContext *ri, int x, int y, roq_cell *cell)
Definition: roqvideo.c:71
roq_cell cb2x2[256]
Definition: roqvideo.h:51
AVFrame * last_frame
Definition: roqvideo.h:47
static void create_clusters(const AVFrame *frame, int w, int h, uint8_t *yuvClusters)
Create YUV clusters for the entire image.
Definition: roqvideoenc.c:765
FILE * out
Definition: movenc.c:54
int usedCB2[MAX_CBS_2x2]
Definition: roqvideoenc.c:211
uint8_t unpacked_cb4[MAX_CBS_4x4 *4 *4 *3]
Definition: roqvideoenc.c:214
static void unpack_roq_qcell(uint8_t cb2[], roq_qcell *qcell, uint8_t u[4 *4 *3])
Definition: roqvideoenc.c:87
#define FFSWAP(type, a, b)
Definition: common.h:69
int f2i2[MAX_CBS_2x2]
Definition: roqvideoenc.c:227
AVPixelFormat
Pixel format.
Definition: pixfmt.h:57
This structure stores compressed data.
Definition: avcodec.h:1323
uint8_t unpacked_cb4_enlarged[MAX_CBS_4x4 *8 *8 *3]
Definition: roqvideoenc.c:215
unsigned char v
Definition: roqvideo.h:31
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:211
int eval_dist[4]
Definition: roqvideoenc.c:197
int * closest_cb2
Definition: roqvideoenc.c:237
int height
Definition: roqvideo.h:55
static void reconstruct_and_encode_image(RoqContext *enc, RoqTempdata *tempData, int w, int h, int numBlocks)
Definition: roqvideoenc.c:625