FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/roqvideoenc.c
Date: 2021-09-23 20:34:37
Exec Total Coverage
Lines: 504 531 94.9%
Branches: 223 262 85.1%

Line Branch Exec Source
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 FFmpeg.
9 *
10 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 /**
26 * @file
27 * id RoQ encoder by Vitor. Based on the Switchblade3 library and the
28 * Switchblade3 FFmpeg glue by Eric Lasota.
29 */
30
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 "libavutil/lfg.h"
61 #include "libavutil/opt.h"
62 #include "roqvideo.h"
63 #include "bytestream.h"
64 #include "elbg.h"
65 #include "encode.h"
66 #include "internal.h"
67 #include "mathops.h"
68
69 #define CHROMA_BIAS 1
70
71 /**
72 * Maximum number of generated 4x4 codebooks. Can't be 256 to workaround a
73 * Quake 3 bug.
74 */
75 #define MAX_CBS_4x4 256
76
77 #define MAX_CBS_2x2 256 ///< Maximum number of 2x2 codebooks.
78
79 /* The cast is useful when multiplying it by INT_MAX */
80 #define ROQ_LAMBDA_SCALE ((uint64_t) FF_LAMBDA_SCALE)
81
82 typedef struct RoqCodebooks {
83 int numCB4;
84 int numCB2;
85 int usedCB2[MAX_CBS_2x2];
86 int usedCB4[MAX_CBS_4x4];
87 uint8_t unpacked_cb2[MAX_CBS_2x2*2*2*3];
88 uint8_t unpacked_cb4[MAX_CBS_4x4*4*4*3];
89 uint8_t unpacked_cb4_enlarged[MAX_CBS_4x4*8*8*3];
90 } RoqCodebooks;
91
92 /**
93 * Temporary vars
94 */
95 typedef struct RoqTempData
96 {
97 int f2i4[MAX_CBS_4x4];
98 int i2f4[MAX_CBS_4x4];
99 int f2i2[MAX_CBS_2x2];
100 int i2f2[MAX_CBS_2x2];
101
102 int mainChunkSize;
103
104 int numCB4;
105 int numCB2;
106
107 RoqCodebooks codebooks;
108
109 int used_option[4];
110 } RoqTempData;
111
112 typedef struct SubcelEvaluation {
113 int eval_dist[4];
114 int best_bit_use;
115 int best_coding;
116
117 int subCels[4];
118 motion_vect motion;
119 int cbEntry;
120 } SubcelEvaluation;
121
122 typedef struct CelEvaluation {
123 int eval_dist[4];
124 int best_coding;
125
126 SubcelEvaluation subCels[4];
127
128 motion_vect motion;
129 int cbEntry;
130
131 int sourceX, sourceY;
132 } CelEvaluation;
133
134 typedef struct RoqEncContext {
135 RoqContext common;
136 struct ELBGContext *elbg;
137 AVLFG randctx;
138 uint64_t lambda;
139
140 motion_vect *this_motion4;
141 motion_vect *last_motion4;
142
143 motion_vect *this_motion8;
144 motion_vect *last_motion8;
145
146 unsigned int framesSinceKeyframe;
147
148 const AVFrame *frame_to_enc;
149 uint8_t *out_buf;
150 RoqTempData tmp_data;
151 roq_cell results4[4 * MAX_CBS_4x4];
152 int tmp_codebook_buf[FFMAX(24 * MAX_CBS_4x4, 6 * MAX_CBS_2x2)];
153
154 CelEvaluation *cel_evals;
155 int *closest_cb;
156 int *points; // Allocated together with closest_cb
157
158 int first_frame;
159 int quake3_compat; // Quake 3 compatibility option
160 } RoqEncContext;
161
162 /* Macroblock support functions */
163 26796 static void unpack_roq_cell(roq_cell *cell, uint8_t u[4*3])
164 {
165 26796 memcpy(u , cell->y, 4);
166 26796 memset(u+4, cell->u, 4);
167 26796 memset(u+8, cell->v, 4);
168 26796 }
169
170 5355 static void unpack_roq_qcell(uint8_t cb2[], roq_qcell *qcell, uint8_t u[4*4*3])
171 {
172 int i,cp;
173 static const int offsets[4] = {0, 2, 8, 10};
174
175
2/2
✓ Branch 0 taken 16065 times.
✓ Branch 1 taken 5355 times.
21420 for (cp=0; cp<3; cp++)
176
2/2
✓ Branch 0 taken 64260 times.
✓ Branch 1 taken 16065 times.
80325 for (i=0; i<4; i++) {
177 64260 u[4*4*cp + offsets[i] ] = cb2[qcell->idx[i]*2*2*3 + 4*cp ];
178 64260 u[4*4*cp + offsets[i]+1] = cb2[qcell->idx[i]*2*2*3 + 4*cp+1];
179 64260 u[4*4*cp + offsets[i]+4] = cb2[qcell->idx[i]*2*2*3 + 4*cp+2];
180 64260 u[4*4*cp + offsets[i]+5] = cb2[qcell->idx[i]*2*2*3 + 4*cp+3];
181 }
182 5355 }
183
184
185 5355 static void enlarge_roq_mb4(uint8_t base[3*16], uint8_t u[3*64])
186 {
187 int x,y,cp;
188
189
2/2
✓ Branch 0 taken 16065 times.
✓ Branch 1 taken 5355 times.
21420 for(cp=0; cp<3; cp++)
190
2/2
✓ Branch 0 taken 128520 times.
✓ Branch 1 taken 16065 times.
144585 for(y=0; y<8; y++)
191
2/2
✓ Branch 0 taken 1028160 times.
✓ Branch 1 taken 128520 times.
1156680 for(x=0; x<8; x++)
192 1028160 *u++ = base[(y/2)*4 + (x/2) + 16*cp];
193 5355 }
194
195 5107919472 static inline int square(int x)
196 {
197 5107919472 return x*x;
198 }
199
200 260413116 static inline int eval_sse(const uint8_t *a, const uint8_t *b, int count)
201 {
202 260413116 int diff=0;
203
204
2/2
✓ Branch 0 taken 5107919472 times.
✓ Branch 1 taken 260413116 times.
5368332588 while(count--)
205 5107919472 diff += square(*b++ - *a++);
206
207 260413116 return diff;
208 }
209
210 // FIXME Could use DSPContext.sse, but it is not so speed critical (used
211 // just for motion estimation).
212 3971991 static int block_sse(uint8_t * const *buf1, uint8_t * const *buf2, int x1, int y1,
213 int x2, int y2, const int *stride1, const int *stride2, int size)
214 {
215 int i, k;
216 3971991 int sse=0;
217
218
2/2
✓ Branch 0 taken 11915973 times.
✓ Branch 1 taken 3971991 times.
15887964 for (k=0; k<3; k++) {
219
2/2
✓ Branch 0 taken 7943982 times.
✓ Branch 1 taken 3971991 times.
11915973 int bias = (k ? CHROMA_BIAS : 4);
220
2/2
✓ Branch 0 taken 56757228 times.
✓ Branch 1 taken 11915973 times.
68673201 for (i=0; i<size; i++)
221 56757228 sse += bias*eval_sse(buf1[k] + (y1+i)*stride1[k] + x1,
222 56757228 buf2[k] + (y2+i)*stride2[k] + x2, size);
223 }
224
225 3971991 return sse;
226 }
227
228 3880940 static int eval_motion_dist(RoqEncContext *enc, int x, int y, motion_vect vect,
229 int size)
230 {
231 3880940 RoqContext *const roq = &enc->common;
232 3880940 int mx=vect.d[0];
233 3880940 int my=vect.d[1];
234
235
4/4
✓ Branch 0 taken 3872078 times.
✓ Branch 1 taken 8862 times.
✓ Branch 2 taken 6102 times.
✓ Branch 3 taken 3865976 times.
3880940 if (mx < -7 || mx > 7)
236 14964 return INT_MAX;
237
238
4/4
✓ Branch 0 taken 3859140 times.
✓ Branch 1 taken 6836 times.
✓ Branch 2 taken 5510 times.
✓ Branch 3 taken 3853630 times.
3865976 if (my < -7 || my > 7)
239 12346 return INT_MAX;
240
241 3853630 mx += x;
242 3853630 my += y;
243
244
4/4
✓ Branch 0 taken 3836514 times.
✓ Branch 1 taken 17116 times.
✓ Branch 2 taken 17723 times.
✓ Branch 3 taken 3818791 times.
3853630 if ((unsigned) mx > roq->width-size || (unsigned) my > roq->height-size)
245 34839 return INT_MAX;
246
247 3818791 return block_sse(enc->frame_to_enc->data, roq->last_frame->data, x, y,
248 mx, my,
249 3818791 enc->frame_to_enc->linesize, roq->last_frame->linesize,
250 size);
251 }
252
253 /**
254 * @return distortion between two macroblocks
255 */
256 67885296 static inline int squared_diff_macroblock(uint8_t a[], uint8_t b[], int size)
257 {
258 67885296 int cp, sdiff=0;
259
260
2/2
✓ Branch 0 taken 203655888 times.
✓ Branch 1 taken 67885296 times.
271541184 for(cp=0;cp<3;cp++) {
261
2/2
✓ Branch 0 taken 135770592 times.
✓ Branch 1 taken 67885296 times.
203655888 int bias = (cp ? CHROMA_BIAS : 4);
262 203655888 sdiff += bias*eval_sse(a, b, size*size);
263 203655888 a += size*size;
264 203655888 b += size*size;
265 }
266
267 67885296 return sdiff;
268 }
269
270 /**
271 * Initialize cel evaluators and set their source coordinates
272 */
273 4 static int create_cel_evals(RoqEncContext *enc)
274 {
275 4 RoqContext *const roq = &enc->common;
276
277 4 enc->cel_evals = av_malloc_array(roq->width * roq->height / 64, sizeof(CelEvaluation));
278
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!enc->cel_evals)
279 return AVERROR(ENOMEM);
280
281 /* Map to the ROQ quadtree order */
282
2/2
✓ Branch 0 taken 86 times.
✓ Branch 1 taken 4 times.
90 for (int y = 0, n = 0; y < roq->height; y += 16)
283
2/2
✓ Branch 0 taken 2212 times.
✓ Branch 1 taken 86 times.
2298 for (int x = 0; x < roq->width; x += 16)
284
2/2
✓ Branch 0 taken 8848 times.
✓ Branch 1 taken 2212 times.
11060 for(int i = 0; i < 4; i++) {
285 8848 enc->cel_evals[n ].sourceX = x + (i&1)*8;
286 8848 enc->cel_evals[n++].sourceY = y + (i&2)*4;
287 }
288
289 4 return 0;
290 }
291
292 /**
293 * Get macroblocks from parts of the image
294 */
295 1015056 static void get_frame_mb(const AVFrame *frame, int x, int y, uint8_t mb[], int dim)
296 {
297 int i, j, cp;
298
299
2/2
✓ Branch 0 taken 3045168 times.
✓ Branch 1 taken 1015056 times.
4060224 for (cp=0; cp<3; cp++) {
300 3045168 int stride = frame->linesize[cp];
301
2/2
✓ Branch 0 taken 8120448 times.
✓ Branch 1 taken 3045168 times.
11165616 for (i=0; i<dim; i++)
302
2/2
✓ Branch 0 taken 27841536 times.
✓ Branch 1 taken 8120448 times.
35961984 for (j=0; j<dim; j++)
303 27841536 *mb++ = frame->data[cp][(y+i)*stride + x + j];
304 }
305 1015056 }
306
307 /**
308 * Find the codebook with the lowest distortion from an image
309 */
310 263100 static int index_mb(uint8_t cluster[], uint8_t cb[], int numCB,
311 int *outIndex, int dim)
312 {
313 263100 int i, lDiff = INT_MAX, pick=0;
314
315 /* Diff against the others */
316
2/2
✓ Branch 0 taken 67111920 times.
✓ Branch 1 taken 263100 times.
67375020 for (i=0; i<numCB; i++) {
317 67111920 int diff = squared_diff_macroblock(cluster, cb + i*dim*dim*3, dim);
318
2/2
✓ Branch 0 taken 1698384 times.
✓ Branch 1 taken 65413536 times.
67111920 if (diff < lDiff) {
319 1698384 lDiff = diff;
320 1698384 pick = i;
321 }
322 }
323
324 263100 *outIndex = pick;
325 263100 return lDiff;
326 }
327
328 #define EVAL_MOTION(MOTION) \
329 do { \
330 diff = eval_motion_dist(enc, j, i, MOTION, blocksize); \
331 \
332 if (diff < lowestdiff) { \
333 lowestdiff = diff; \
334 bestpick = MOTION; \
335 } \
336 } while(0)
337
338 34 static void motion_search(RoqEncContext *enc, int blocksize)
339 {
340 static const motion_vect offsets[8] = {
341 {{ 0,-1}},
342 {{ 0, 1}},
343 {{-1, 0}},
344 {{ 1, 0}},
345 {{-1, 1}},
346 {{ 1,-1}},
347 {{-1,-1}},
348 {{ 1, 1}},
349 };
350
351 34 RoqContext *const roq = &enc->common;
352 int diff, lowestdiff, oldbest;
353 int off[3];
354 34 motion_vect bestpick = {{0,0}};
355 int i, j, k, offset;
356
357 motion_vect *last_motion;
358 motion_vect *this_motion;
359 motion_vect vect, vect2;
360 34 const int max = (roq->width / blocksize) * roq->height / blocksize;
361
362
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 17 times.
34 if (blocksize == 4) {
363 17 last_motion = enc->last_motion4;
364 17 this_motion = enc->this_motion4;
365 } else {
366 17 last_motion = enc->last_motion8;
367 17 this_motion = enc->this_motion8;
368 }
369
370
2/2
✓ Branch 0 taken 2256 times.
✓ Branch 1 taken 34 times.
2290 for (i = 0; i< roq->height; i += blocksize)
371
2/2
✓ Branch 0 taken 197440 times.
✓ Branch 1 taken 2256 times.
199696 for (j = 0; j < roq->width; j += blocksize) {
372 197440 lowestdiff = eval_motion_dist(enc, j, i, (motion_vect) {{0,0}},
373 blocksize);
374 197440 bestpick.d[0] = 0;
375 197440 bestpick.d[1] = 0;
376
377
2/2
✓ Branch 0 taken 157952 times.
✓ Branch 1 taken 39488 times.
197440 if (blocksize == 4)
378
2/2
✓ Branch 1 taken 87865 times.
✓ Branch 2 taken 70087 times.
157952 EVAL_MOTION(enc->this_motion8[(i/8) * (roq->width/8) + j/8]);
379
380 197440 offset = (i/blocksize) * roq->width / blocksize + j / blocksize;
381
2/4
✓ Branch 0 taken 197440 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 197440 times.
✗ Branch 3 not taken.
197440 if (offset < max && offset >= 0)
382
2/2
✓ Branch 1 taken 24235 times.
✓ Branch 2 taken 173205 times.
197440 EVAL_MOTION(last_motion[offset]);
383
384 197440 offset++;
385
3/4
✓ Branch 0 taken 197406 times.
✓ Branch 1 taken 34 times.
✓ Branch 2 taken 197406 times.
✗ Branch 3 not taken.
197440 if (offset < max && offset >= 0)
386
2/2
✓ Branch 1 taken 8590 times.
✓ Branch 2 taken 188816 times.
197406 EVAL_MOTION(last_motion[offset]);
387
388 197440 offset = (i/blocksize + 1) * roq->width / blocksize + j / blocksize;
389
3/4
✓ Branch 0 taken 194896 times.
✓ Branch 1 taken 2544 times.
✓ Branch 2 taken 194896 times.
✗ Branch 3 not taken.
197440 if (offset < max && offset >= 0)
390
2/2
✓ Branch 1 taken 5992 times.
✓ Branch 2 taken 188904 times.
194896 EVAL_MOTION(last_motion[offset]);
391
392 197440 off[0]= (i/blocksize) * roq->width / blocksize + j/blocksize - 1;
393 197440 off[1]= off[0] - roq->width / blocksize + 1;
394 197440 off[2]= off[1] + 1;
395
396
2/2
✓ Branch 0 taken 194896 times.
✓ Branch 1 taken 2544 times.
197440 if (i) {
397
398
2/2
✓ Branch 0 taken 389792 times.
✓ Branch 1 taken 194896 times.
584688 for(k=0; k<2; k++)
399 389792 vect.d[k]= mid_pred(this_motion[off[0]].d[k],
400 389792 this_motion[off[1]].d[k],
401 389792 this_motion[off[2]].d[k]);
402
403
2/2
✓ Branch 1 taken 20139 times.
✓ Branch 2 taken 174757 times.
194896 EVAL_MOTION(vect);
404
2/2
✓ Branch 0 taken 584688 times.
✓ Branch 1 taken 194896 times.
779584 for(k=0; k<3; k++)
405
2/2
✓ Branch 1 taken 19990 times.
✓ Branch 2 taken 564698 times.
584688 EVAL_MOTION(this_motion[off[k]]);
406
2/2
✓ Branch 0 taken 2510 times.
✓ Branch 1 taken 34 times.
2544 } else if(j)
407
2/2
✓ Branch 1 taken 287 times.
✓ Branch 2 taken 2223 times.
2510 EVAL_MOTION(this_motion[off[0]]);
408
409 197440 vect = bestpick;
410
411 197440 oldbest = -1;
412
2/2
✓ Branch 0 taken 244534 times.
✓ Branch 1 taken 197440 times.
441974 while (oldbest != lowestdiff) {
413 244534 oldbest = lowestdiff;
414
2/2
✓ Branch 0 taken 1956272 times.
✓ Branch 1 taken 244534 times.
2200806 for (k=0; k<8; k++) {
415 1956272 vect2 = vect;
416 1956272 vect2.d[0] += offsets[k].d[0];
417 1956272 vect2.d[1] += offsets[k].d[1];
418
2/2
✓ Branch 1 taken 55065 times.
✓ Branch 2 taken 1901207 times.
1956272 EVAL_MOTION(vect2);
419 }
420 244534 vect = bestpick;
421 }
422 197440 offset = (i/blocksize) * roq->width / blocksize + j/blocksize;
423 197440 this_motion[offset] = bestpick;
424 }
425 34 }
426
427 /**
428 * Get distortion for all options available to a subcel
429 */
430 193344 static void gather_data_for_subcel(SubcelEvaluation *subcel, int x,
431 int y, RoqEncContext *enc)
432 {
433 193344 RoqContext *const roq = &enc->common;
434 193344 RoqTempData *const tempData = &enc->tmp_data;
435 uint8_t mb4[4*4*3];
436 uint8_t mb2[2*2*3];
437 int cluster_index;
438 int i, best_dist;
439
440 static const int bitsUsed[4] = {2, 10, 10, 34};
441
442
2/2
✓ Branch 0 taken 157952 times.
✓ Branch 1 taken 35392 times.
193344 if (enc->framesSinceKeyframe >= 1) {
443 157952 subcel->motion = enc->this_motion4[y * roq->width / 16 + x / 4];
444
445 157952 subcel->eval_dist[RoQ_ID_FCC] =
446 157952 eval_motion_dist(enc, x, y,
447 157952 enc->this_motion4[y * roq->width / 16 + x / 4], 4);
448 } else
449 35392 subcel->eval_dist[RoQ_ID_FCC] = INT_MAX;
450
451
2/2
✓ Branch 0 taken 122560 times.
✓ Branch 1 taken 70784 times.
193344 if (enc->framesSinceKeyframe >= 2)
452 122560 subcel->eval_dist[RoQ_ID_MOT] = block_sse(enc->frame_to_enc->data,
453 122560 roq->current_frame->data, x,
454 y, x, y,
455 122560 enc->frame_to_enc->linesize,
456 122560 roq->current_frame->linesize,
457 4);
458 else
459 70784 subcel->eval_dist[RoQ_ID_MOT] = INT_MAX;
460
461 193344 cluster_index = y * roq->width / 16 + x / 4;
462
463 193344 get_frame_mb(enc->frame_to_enc, x, y, mb4, 4);
464
465 386688 subcel->eval_dist[RoQ_ID_SLD] = index_mb(mb4,
466 193344 tempData->codebooks.unpacked_cb4,
467 tempData->codebooks.numCB4,
468 &subcel->cbEntry, 4);
469
470 193344 subcel->eval_dist[RoQ_ID_CCC] = 0;
471
472
2/2
✓ Branch 0 taken 773376 times.
✓ Branch 1 taken 193344 times.
966720 for(i=0;i<4;i++) {
473 773376 subcel->subCels[i] = enc->closest_cb[cluster_index*4+i];
474
475 773376 get_frame_mb(enc->frame_to_enc, x+2*(i&1),
476 773376 y+(i&2), mb2, 2);
477
478 773376 subcel->eval_dist[RoQ_ID_CCC] +=
479 773376 squared_diff_macroblock(tempData->codebooks.unpacked_cb2 + subcel->subCels[i]*2*2*3, mb2, 2);
480 }
481
482 193344 best_dist = INT_MAX;
483
2/2
✓ Branch 0 taken 773376 times.
✓ Branch 1 taken 193344 times.
966720 for (i=0; i<4; i++)
484
2/2
✓ Branch 0 taken 353674 times.
✓ Branch 1 taken 419702 times.
773376 if (ROQ_LAMBDA_SCALE*subcel->eval_dist[i] + enc->lambda*bitsUsed[i] <
485 best_dist) {
486 353674 subcel->best_coding = i;
487 353674 subcel->best_bit_use = bitsUsed[i];
488 353674 best_dist = ROQ_LAMBDA_SCALE*subcel->eval_dist[i] +
489 353674 enc->lambda*bitsUsed[i];
490 }
491 193344 }
492
493 /**
494 * Get distortion for all options available to a cel
495 */
496 48336 static void gather_data_for_cel(CelEvaluation *cel, RoqEncContext *enc)
497 {
498 48336 RoqContext *const roq = &enc->common;
499 48336 RoqTempData *const tempData = &enc->tmp_data;
500 uint8_t mb8[8*8*3];
501 48336 int index = cel->sourceY * roq->width / 64 + cel->sourceX/8;
502 int i, j, best_dist, divide_bit_use;
503
504 48336 int bitsUsed[4] = {2, 10, 10, 0};
505
506
2/2
✓ Branch 0 taken 39488 times.
✓ Branch 1 taken 8848 times.
48336 if (enc->framesSinceKeyframe >= 1) {
507 39488 cel->motion = enc->this_motion8[index];
508
509 39488 cel->eval_dist[RoQ_ID_FCC] =
510 39488 eval_motion_dist(enc, cel->sourceX, cel->sourceY,
511 39488 enc->this_motion8[index], 8);
512 } else
513 8848 cel->eval_dist[RoQ_ID_FCC] = INT_MAX;
514
515
2/2
✓ Branch 0 taken 30640 times.
✓ Branch 1 taken 17696 times.
48336 if (enc->framesSinceKeyframe >= 2)
516 30640 cel->eval_dist[RoQ_ID_MOT] = block_sse(enc->frame_to_enc->data,
517 30640 roq->current_frame->data,
518 cel->sourceX, cel->sourceY,
519 cel->sourceX, cel->sourceY,
520 30640 enc->frame_to_enc->linesize,
521 30640 roq->current_frame->linesize,8);
522 else
523 17696 cel->eval_dist[RoQ_ID_MOT] = INT_MAX;
524
525 48336 get_frame_mb(enc->frame_to_enc, cel->sourceX, cel->sourceY, mb8, 8);
526
527 48336 cel->eval_dist[RoQ_ID_SLD] =
528 48336 index_mb(mb8, tempData->codebooks.unpacked_cb4_enlarged,
529 tempData->codebooks.numCB4, &cel->cbEntry, 8);
530
531 48336 gather_data_for_subcel(cel->subCels + 0, cel->sourceX+0, cel->sourceY+0, enc);
532 48336 gather_data_for_subcel(cel->subCels + 1, cel->sourceX+4, cel->sourceY+0, enc);
533 48336 gather_data_for_subcel(cel->subCels + 2, cel->sourceX+0, cel->sourceY+4, enc);
534 48336 gather_data_for_subcel(cel->subCels + 3, cel->sourceX+4, cel->sourceY+4, enc);
535
536 48336 cel->eval_dist[RoQ_ID_CCC] = 0;
537 48336 divide_bit_use = 0;
538
2/2
✓ Branch 0 taken 193344 times.
✓ Branch 1 taken 48336 times.
241680 for (i=0; i<4; i++) {
539 193344 cel->eval_dist[RoQ_ID_CCC] +=
540 193344 cel->subCels[i].eval_dist[cel->subCels[i].best_coding];
541 193344 divide_bit_use += cel->subCels[i].best_bit_use;
542 }
543
544 48336 best_dist = INT_MAX;
545 48336 bitsUsed[3] = 2 + divide_bit_use;
546
547
2/2
✓ Branch 0 taken 193344 times.
✓ Branch 1 taken 48336 times.
241680 for (i=0; i<4; i++)
548
2/2
✓ Branch 0 taken 102893 times.
✓ Branch 1 taken 90451 times.
193344 if (ROQ_LAMBDA_SCALE*cel->eval_dist[i] + enc->lambda*bitsUsed[i] <
549 best_dist) {
550 102893 cel->best_coding = i;
551 102893 best_dist = ROQ_LAMBDA_SCALE*cel->eval_dist[i] +
552 102893 enc->lambda*bitsUsed[i];
553 }
554
555 48336 tempData->used_option[cel->best_coding]++;
556 48336 tempData->mainChunkSize += bitsUsed[cel->best_coding];
557
558
2/2
✓ Branch 0 taken 2641 times.
✓ Branch 1 taken 45695 times.
48336 if (cel->best_coding == RoQ_ID_SLD)
559 2641 tempData->codebooks.usedCB4[cel->cbEntry]++;
560
561
2/2
✓ Branch 0 taken 31845 times.
✓ Branch 1 taken 16491 times.
48336 if (cel->best_coding == RoQ_ID_CCC)
562
2/2
✓ Branch 0 taken 127380 times.
✓ Branch 1 taken 31845 times.
159225 for (i=0; i<4; i++) {
563
2/2
✓ Branch 0 taken 5734 times.
✓ Branch 1 taken 121646 times.
127380 if (cel->subCels[i].best_coding == RoQ_ID_SLD)
564 5734 tempData->codebooks.usedCB4[cel->subCels[i].cbEntry]++;
565
2/2
✓ Branch 0 taken 65638 times.
✓ Branch 1 taken 56008 times.
121646 else if (cel->subCels[i].best_coding == RoQ_ID_CCC)
566
2/2
✓ Branch 0 taken 262552 times.
✓ Branch 1 taken 65638 times.
328190 for (j=0; j<4; j++)
567 262552 tempData->codebooks.usedCB2[cel->subCels[i].subCels[j]]++;
568 }
569 48336 }
570
571 21 static void remap_codebooks(RoqEncContext *enc)
572 {
573 21 RoqContext *const roq = &enc->common;
574 21 RoqTempData *const tempData = &enc->tmp_data;
575 21 int i, j, idx=0;
576
577 /* Make remaps for the final codebook usage */
578
3/4
✓ Branch 0 taken 5376 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5355 times.
✓ Branch 3 taken 21 times.
5376 for (i=0; i<(enc->quake3_compat ? MAX_CBS_4x4-1 : MAX_CBS_4x4); i++) {
579
2/2
✓ Branch 0 taken 1635 times.
✓ Branch 1 taken 3720 times.
5355 if (tempData->codebooks.usedCB4[i]) {
580 1635 tempData->i2f4[i] = idx;
581 1635 tempData->f2i4[idx] = i;
582
2/2
✓ Branch 0 taken 6540 times.
✓ Branch 1 taken 1635 times.
8175 for (j=0; j<4; j++)
583 6540 tempData->codebooks.usedCB2[roq->cb4x4[i].idx[j]]++;
584 1635 idx++;
585 }
586 }
587
588 21 tempData->numCB4 = idx;
589
590 21 idx = 0;
591
2/2
✓ Branch 0 taken 5376 times.
✓ Branch 1 taken 21 times.
5397 for (i=0; i<MAX_CBS_2x2; i++) {
592
2/2
✓ Branch 0 taken 5375 times.
✓ Branch 1 taken 1 times.
5376 if (tempData->codebooks.usedCB2[i]) {
593 5375 tempData->i2f2[i] = idx;
594 5375 tempData->f2i2[idx] = i;
595 5375 idx++;
596 }
597 }
598 21 tempData->numCB2 = idx;
599
600 21 }
601
602 /**
603 * Write codebook chunk
604 */
605 21 static void write_codebooks(RoqEncContext *enc)
606 {
607 21 RoqContext *const roq = &enc->common;
608 21 RoqTempData *const tempData = &enc->tmp_data;
609 int i, j;
610 21 uint8_t **outp= &enc->out_buf;
611
612
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 if (tempData->numCB2) {
613 21 bytestream_put_le16(outp, RoQ_QUAD_CODEBOOK);
614 21 bytestream_put_le32(outp, tempData->numCB2*6 + tempData->numCB4*4);
615 21 bytestream_put_byte(outp, tempData->numCB4);
616 21 bytestream_put_byte(outp, tempData->numCB2);
617
618
2/2
✓ Branch 0 taken 5375 times.
✓ Branch 1 taken 21 times.
5396 for (i=0; i<tempData->numCB2; i++) {
619 5375 bytestream_put_buffer(outp, roq->cb2x2[tempData->f2i2[i]].y, 4);
620 5375 bytestream_put_byte(outp, roq->cb2x2[tempData->f2i2[i]].u);
621 5375 bytestream_put_byte(outp, roq->cb2x2[tempData->f2i2[i]].v);
622 }
623
624
2/2
✓ Branch 0 taken 1635 times.
✓ Branch 1 taken 21 times.
1656 for (i=0; i<tempData->numCB4; i++)
625
2/2
✓ Branch 0 taken 6540 times.
✓ Branch 1 taken 1635 times.
8175 for (j=0; j<4; j++)
626 6540 bytestream_put_byte(outp, tempData->i2f2[roq->cb4x4[tempData->f2i4[i]].idx[j]]);
627
628 }
629 21 }
630
631 59269 static inline uint8_t motion_arg(motion_vect mot)
632 {
633 59269 uint8_t ax = 8 - ((uint8_t) mot.d[0]);
634 59269 uint8_t ay = 8 - ((uint8_t) mot.d[1]);
635 59269 return ((ax&15)<<4) | (ay&15);
636 }
637
638 typedef struct CodingSpool {
639 int typeSpool;
640 int typeSpoolLength;
641 uint8_t argumentSpool[64];
642 uint8_t *args;
643 uint8_t **pout;
644 } CodingSpool;
645
646 /* NOTE: Typecodes must be spooled AFTER arguments!! */
647 175760 static void write_typecode(CodingSpool *s, uint8_t type)
648 {
649 175760 s->typeSpool |= (type & 3) << (14 - s->typeSpoolLength);
650 175760 s->typeSpoolLength += 2;
651
2/2
✓ Branch 0 taken 21970 times.
✓ Branch 1 taken 153790 times.
175760 if (s->typeSpoolLength == 16) {
652 21970 bytestream_put_le16(s->pout, s->typeSpool);
653 21970 bytestream_put_buffer(s->pout, s->argumentSpool,
654 21970 s->args - s->argumentSpool);
655 21970 s->typeSpoolLength = 0;
656 21970 s->typeSpool = 0;
657 21970 s->args = s->argumentSpool;
658 }
659 175760 }
660
661 21 static void reconstruct_and_encode_image(RoqEncContext *enc,
662 int w, int h, int numBlocks)
663 {
664 21 RoqContext *const roq = &enc->common;
665 21 RoqTempData *const tempData = &enc->tmp_data;
666 int i, j, k;
667 int x, y;
668 int subX, subY;
669 21 int dist=0;
670
671 roq_qcell *qcell;
672 CelEvaluation *eval;
673
674 CodingSpool spool;
675
676 21 spool.typeSpool=0;
677 21 spool.typeSpoolLength=0;
678 21 spool.args = spool.argumentSpool;
679 21 spool.pout = &enc->out_buf;
680
681
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 10 times.
21 if (tempData->used_option[RoQ_ID_CCC]%2)
682 11 tempData->mainChunkSize+=8; //FIXME
683
684 /* Write the video chunk header */
685 21 bytestream_put_le16(&enc->out_buf, RoQ_QUAD_VQ);
686 21 bytestream_put_le32(&enc->out_buf, tempData->mainChunkSize/8);
687 21 bytestream_put_byte(&enc->out_buf, 0x0);
688 21 bytestream_put_byte(&enc->out_buf, 0x0);
689
690
2/2
✓ Branch 0 taken 48336 times.
✓ Branch 1 taken 21 times.
48357 for (i=0; i<numBlocks; i++) {
691 48336 eval = enc->cel_evals + i;
692
693 48336 x = eval->sourceX;
694 48336 y = eval->sourceY;
695 48336 dist += eval->eval_dist[eval->best_coding];
696
697
4/5
✓ Branch 0 taken 10068 times.
✓ Branch 1 taken 3782 times.
✓ Branch 2 taken 2641 times.
✓ Branch 3 taken 31845 times.
✗ Branch 4 not taken.
48336 switch (eval->best_coding) {
698 10068 case RoQ_ID_MOT:
699 10068 write_typecode(&spool, RoQ_ID_MOT);
700 10068 break;
701
702 3782 case RoQ_ID_FCC:
703 3782 bytestream_put_byte(&spool.args, motion_arg(eval->motion));
704
705 3782 write_typecode(&spool, RoQ_ID_FCC);
706 3782 ff_apply_motion_8x8(roq, x, y,
707 eval->motion.d[0], eval->motion.d[1]);
708 3782 break;
709
710 2641 case RoQ_ID_SLD:
711 2641 bytestream_put_byte(&spool.args, tempData->i2f4[eval->cbEntry]);
712 2641 write_typecode(&spool, RoQ_ID_SLD);
713
714 2641 qcell = roq->cb4x4 + eval->cbEntry;
715 2641 ff_apply_vector_4x4(roq, x , y , roq->cb2x2 + qcell->idx[0]);
716 2641 ff_apply_vector_4x4(roq, x+4, y , roq->cb2x2 + qcell->idx[1]);
717 2641 ff_apply_vector_4x4(roq, x , y+4, roq->cb2x2 + qcell->idx[2]);
718 2641 ff_apply_vector_4x4(roq, x+4, y+4, roq->cb2x2 + qcell->idx[3]);
719 2641 break;
720
721 31845 case RoQ_ID_CCC:
722 31845 write_typecode(&spool, RoQ_ID_CCC);
723
724
2/2
✓ Branch 0 taken 127380 times.
✓ Branch 1 taken 31845 times.
159225 for (j=0; j<4; j++) {
725 127380 subX = x + 4*(j&1);
726 127380 subY = y + 2*(j&2);
727
728
4/5
✓ Branch 0 taken 521 times.
✓ Branch 1 taken 55487 times.
✓ Branch 2 taken 5734 times.
✓ Branch 3 taken 65638 times.
✗ Branch 4 not taken.
127380 switch(eval->subCels[j].best_coding) {
729 521 case RoQ_ID_MOT:
730 521 break;
731
732 55487 case RoQ_ID_FCC:
733 55487 bytestream_put_byte(&spool.args,
734 55487 motion_arg(eval->subCels[j].motion));
735
736 55487 ff_apply_motion_4x4(roq, subX, subY,
737 eval->subCels[j].motion.d[0],
738 eval->subCels[j].motion.d[1]);
739 55487 break;
740
741 5734 case RoQ_ID_SLD:
742 5734 bytestream_put_byte(&spool.args,
743 5734 tempData->i2f4[eval->subCels[j].cbEntry]);
744
745 5734 qcell = roq->cb4x4 + eval->subCels[j].cbEntry;
746
747 5734 ff_apply_vector_2x2(roq, subX , subY ,
748 5734 roq->cb2x2 + qcell->idx[0]);
749 5734 ff_apply_vector_2x2(roq, subX+2, subY ,
750 5734 roq->cb2x2 + qcell->idx[1]);
751 5734 ff_apply_vector_2x2(roq, subX , subY+2,
752 5734 roq->cb2x2 + qcell->idx[2]);
753 5734 ff_apply_vector_2x2(roq, subX+2, subY+2,
754 5734 roq->cb2x2 + qcell->idx[3]);
755 5734 break;
756
757 65638 case RoQ_ID_CCC:
758
2/2
✓ Branch 0 taken 262552 times.
✓ Branch 1 taken 65638 times.
328190 for (k=0; k<4; k++) {
759 262552 int cb_idx = eval->subCels[j].subCels[k];
760 262552 bytestream_put_byte(&spool.args,
761 262552 tempData->i2f2[cb_idx]);
762
763 262552 ff_apply_vector_2x2(roq, subX + 2*(k&1), subY + (k&2),
764 262552 roq->cb2x2 + cb_idx);
765 }
766 65638 break;
767 }
768 127380 write_typecode(&spool, eval->subCels[j].best_coding);
769 }
770 31845 break;
771 }
772 }
773
774 /* Flush the remainder of the argument/type spool */
775
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 21 times.
65 while (spool.typeSpoolLength)
776 44 write_typecode(&spool, 0x0);
777 21 }
778
779
780 /**
781 * Create a single YUV cell from a 2x2 section of the image
782 */
783 773376 static inline void frame_block_to_cell(int *block, uint8_t * const *data,
784 int top, int left, const int *stride)
785 {
786 773376 int i, j, u=0, v=0;
787
788
2/2
✓ Branch 0 taken 1546752 times.
✓ Branch 1 taken 773376 times.
2320128 for (i=0; i<2; i++)
789
2/2
✓ Branch 0 taken 3093504 times.
✓ Branch 1 taken 1546752 times.
4640256 for (j=0; j<2; j++) {
790 3093504 int x = (top+i)*stride[0] + left + j;
791 3093504 *block++ = data[0][x];
792 3093504 x = (top+i)*stride[1] + left + j;
793 3093504 u += data[1][x];
794 3093504 v += data[2][x];
795 }
796
797 773376 *block++ = (u + 2) / 4 * CHROMA_BIAS;
798 773376 *block++ = (v + 2) / 4 * CHROMA_BIAS;
799 773376 }
800
801 /**
802 * Create YUV clusters for the entire image
803 */
804 21 static void create_clusters(const AVFrame *frame, int w, int h, int *points)
805 {
806 int i, j, k, l;
807
808
2/2
✓ Branch 0 taken 1848 times.
✓ Branch 1 taken 21 times.
1869 for (i=0; i<h; i+=4)
809
2/2
✓ Branch 0 taken 193344 times.
✓ Branch 1 taken 1848 times.
195192 for (j=0; j<w; j+=4) {
810
2/2
✓ Branch 0 taken 386688 times.
✓ Branch 1 taken 193344 times.
580032 for (k=0; k < 2; k++)
811
2/2
✓ Branch 0 taken 773376 times.
✓ Branch 1 taken 386688 times.
1160064 for (l=0; l < 2; l++)
812 773376 frame_block_to_cell(points + (l + 2*k)*6, frame->data,
813 773376 i+2*k, j+2*l, frame->linesize);
814 193344 points += 24;
815 }
816 21 }
817
818 42 static int generate_codebook(RoqEncContext *enc,
819 int *points, int inputCount, roq_cell *results,
820 int size, int cbsize)
821 {
822 42 int i, j, k, ret = 0;
823 42 int c_size = size*size/4;
824 int *buf;
825 42 int *codebook = enc->tmp_codebook_buf;
826 42 int *closest_cb = enc->closest_cb;
827
828 42 ret = avpriv_elbg_do(&enc->elbg, points, 6 * c_size, inputCount, codebook,
829 cbsize, 1, closest_cb, &enc->randctx);
830
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (ret < 0)
831 return ret;
832
833 42 buf = codebook;
834
2/2
✓ Branch 0 taken 10731 times.
✓ Branch 1 taken 42 times.
10773 for (i=0; i<cbsize; i++)
835
2/2
✓ Branch 0 taken 26796 times.
✓ Branch 1 taken 10731 times.
37527 for (k=0; k<c_size; k++) {
836
2/2
✓ Branch 0 taken 107184 times.
✓ Branch 1 taken 26796 times.
133980 for(j=0; j<4; j++)
837 107184 results->y[j] = *buf++;
838
839 26796 results->u = (*buf++ + CHROMA_BIAS/2)/CHROMA_BIAS;
840 26796 results->v = (*buf++ + CHROMA_BIAS/2)/CHROMA_BIAS;
841 26796 results++;
842 }
843 42 return 0;
844 }
845
846 21 static int generate_new_codebooks(RoqEncContext *enc)
847 {
848 21 int i, j, ret = 0;
849 21 RoqCodebooks *codebooks = &enc->tmp_data.codebooks;
850 21 RoqContext *const roq = &enc->common;
851 21 int max = roq->width * roq->height / 16;
852 uint8_t mb2[3*4];
853 21 int *points = enc->points;
854
855 /* Subsample YUV data */
856 21 create_clusters(enc->frame_to_enc, roq->width, roq->height, points);
857
858
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 codebooks->numCB4 = (enc->quake3_compat ? MAX_CBS_4x4-1 : MAX_CBS_4x4);
859
860 /* Create 4x4 codebooks */
861
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
21 if ((ret = generate_codebook(enc, points, max, enc->results4,
862 4, codebooks->numCB4)) < 0)
863 return ret;
864
865 /* Create 2x2 codebooks */
866
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if ((ret = generate_codebook(enc, points, max * 4,
867 21 roq->cb2x2, 2, MAX_CBS_2x2)) < 0)
868 return ret;
869
870 21 codebooks->numCB2 = MAX_CBS_2x2;
871
872 /* Unpack 2x2 codebook clusters */
873
2/2
✓ Branch 0 taken 5376 times.
✓ Branch 1 taken 21 times.
5397 for (i=0; i<codebooks->numCB2; i++)
874 5376 unpack_roq_cell(roq->cb2x2 + i, codebooks->unpacked_cb2 + i*2*2*3);
875
876 /* Index all 4x4 entries to the 2x2 entries, unpack, and enlarge */
877
2/2
✓ Branch 0 taken 5355 times.
✓ Branch 1 taken 21 times.
5376 for (i=0; i<codebooks->numCB4; i++) {
878
2/2
✓ Branch 0 taken 21420 times.
✓ Branch 1 taken 5355 times.
26775 for (j=0; j<4; j++) {
879 21420 unpack_roq_cell(&enc->results4[4*i + j], mb2);
880 21420 index_mb(mb2, codebooks->unpacked_cb2, codebooks->numCB2,
881 &roq->cb4x4[i].idx[j], 2);
882 }
883 5355 unpack_roq_qcell(codebooks->unpacked_cb2, roq->cb4x4 + i,
884 5355 codebooks->unpacked_cb4 + i*4*4*3);
885 5355 enlarge_roq_mb4(codebooks->unpacked_cb4 + i*4*4*3,
886 5355 codebooks->unpacked_cb4_enlarged + i*8*8*3);
887 }
888
889 21 return 0;
890 }
891
892 21 static int roq_encode_video(RoqEncContext *enc)
893 {
894 21 RoqTempData *const tempData = &enc->tmp_data;
895 21 RoqContext *const roq = &enc->common;
896 int ret;
897
898 21 memset(tempData, 0, sizeof(*tempData));
899
900 21 ret = generate_new_codebooks(enc);
901
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (ret < 0)
902 return ret;
903
904
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 17 times.
21 if (enc->framesSinceKeyframe >= 1) {
905 17 motion_search(enc, 8);
906 17 motion_search(enc, 4);
907 }
908
909 4 retry_encode:
910
2/2
✓ Branch 0 taken 48336 times.
✓ Branch 1 taken 21 times.
48357 for (int i = 0; i < roq->width * roq->height / 64; i++)
911 48336 gather_data_for_cel(enc->cel_evals + i, enc);
912
913 /* Quake 3 can't handle chunks bigger than 65535 bytes */
914
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
21 if (tempData->mainChunkSize/8 > 65535 && enc->quake3_compat) {
915 if (enc->lambda > 100000) {
916 av_log(roq->avctx, AV_LOG_ERROR, "Cannot encode video in Quake compatible form\n");
917 return AVERROR(EINVAL);
918 }
919 av_log(roq->avctx, AV_LOG_ERROR,
920 "Warning, generated a frame too big for Quake (%d > 65535), "
921 "now switching to a bigger qscale value.\n",
922 tempData->mainChunkSize/8);
923 enc->lambda *= 1.5;
924 tempData->mainChunkSize = 0;
925 memset(tempData->used_option, 0, sizeof(tempData->used_option));
926 memset(tempData->codebooks.usedCB4, 0,
927 sizeof(tempData->codebooks.usedCB4));
928 memset(tempData->codebooks.usedCB2, 0,
929 sizeof(tempData->codebooks.usedCB2));
930
931 goto retry_encode;
932 }
933
934 21 remap_codebooks(enc);
935
936 21 write_codebooks(enc);
937
938 21 reconstruct_and_encode_image(enc, roq->width, roq->height,
939 21 roq->width * roq->height / 64);
940
941 /* Rotate frame history */
942 21 FFSWAP(AVFrame *, roq->current_frame, roq->last_frame);
943 21 FFSWAP(motion_vect *, enc->last_motion4, enc->this_motion4);
944 21 FFSWAP(motion_vect *, enc->last_motion8, enc->this_motion8);
945
946 21 enc->framesSinceKeyframe++;
947
948 21 return 0;
949 }
950
951 4 static av_cold int roq_encode_end(AVCodecContext *avctx)
952 {
953 4 RoqEncContext *const enc = avctx->priv_data;
954
955 4 av_frame_free(&enc->common.current_frame);
956 4 av_frame_free(&enc->common.last_frame);
957
958 4 av_freep(&enc->cel_evals);
959 4 av_freep(&enc->closest_cb);
960 4 av_freep(&enc->this_motion4);
961 4 av_freep(&enc->last_motion4);
962 4 av_freep(&enc->this_motion8);
963 4 av_freep(&enc->last_motion8);
964
965 4 avpriv_elbg_free(&enc->elbg);
966
967 4 return 0;
968 }
969
970 4 static av_cold int roq_encode_init(AVCodecContext *avctx)
971 {
972 4 RoqEncContext *const enc = avctx->priv_data;
973 4 RoqContext *const roq = &enc->common;
974
975 4 av_lfg_init(&enc->randctx, 1);
976
977 4 roq->avctx = avctx;
978
979 4 enc->framesSinceKeyframe = 0;
980
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if ((avctx->width & 0xf) || (avctx->height & 0xf)) {
981 av_log(avctx, AV_LOG_ERROR, "Dimensions must be divisible by 16\n");
982 return AVERROR(EINVAL);
983 }
984
985
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (avctx->width > 65535 || avctx->height > 65535) {
986 av_log(avctx, AV_LOG_ERROR, "Dimensions are max %d\n", enc->quake3_compat ? 32768 : 65535);
987 return AVERROR(EINVAL);
988 }
989
990
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
4 if (((avctx->width)&(avctx->width-1))||((avctx->height)&(avctx->height-1)))
991 3 av_log(avctx, AV_LOG_ERROR, "Warning: dimensions not power of two, this is not supported by quake\n");
992
993 4 roq->width = avctx->width;
994 4 roq->height = avctx->height;
995
996 4 enc->framesSinceKeyframe = 0;
997 4 enc->first_frame = 1;
998
999 4 roq->last_frame = av_frame_alloc();
1000 4 roq->current_frame = av_frame_alloc();
1001
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (!roq->last_frame || !roq->current_frame)
1002 return AVERROR(ENOMEM);
1003
1004 4 enc->this_motion4 =
1005 4 av_calloc(roq->width * roq->height / 16, sizeof(*enc->this_motion4));
1006
1007 4 enc->last_motion4 =
1008 4 av_malloc_array (roq->width * roq->height / 16, sizeof(motion_vect));
1009
1010 4 enc->this_motion8 =
1011 4 av_calloc(roq->width * roq->height / 64, sizeof(*enc->this_motion8));
1012
1013 4 enc->last_motion8 =
1014 4 av_malloc_array (roq->width * roq->height / 64, sizeof(motion_vect));
1015
1016 /* 4x4 codebook needs 6 * 4 * 4 / 4 * width * height / 16 * sizeof(int);
1017 * and so does the points buffer. */
1018 4 enc->closest_cb =
1019 4 av_malloc_array(roq->width * roq->height, 3 * sizeof(int));
1020
1021
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 if (!enc->this_motion4 || !enc->last_motion4 ||
1022
3/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
4 !enc->this_motion8 || !enc->last_motion8 || !enc->closest_cb)
1023 return AVERROR(ENOMEM);
1024
1025 4 enc->points = enc->closest_cb + roq->width * roq->height * 3 / 2;
1026
1027 4 return create_cel_evals(enc);
1028 }
1029
1030 4 static void roq_write_video_info_chunk(RoqEncContext *enc)
1031 {
1032 /* ROQ info chunk */
1033 4 bytestream_put_le16(&enc->out_buf, RoQ_INFO);
1034
1035 /* Size: 8 bytes */
1036 4 bytestream_put_le32(&enc->out_buf, 8);
1037
1038 /* Unused argument */
1039 4 bytestream_put_byte(&enc->out_buf, 0x00);
1040 4 bytestream_put_byte(&enc->out_buf, 0x00);
1041
1042 /* Width */
1043 4 bytestream_put_le16(&enc->out_buf, enc->common.width);
1044
1045 /* Height */
1046 4 bytestream_put_le16(&enc->out_buf, enc->common.height);
1047
1048 /* Unused in Quake 3, mimics the output of the real encoder */
1049 4 bytestream_put_byte(&enc->out_buf, 0x08);
1050 4 bytestream_put_byte(&enc->out_buf, 0x00);
1051 4 bytestream_put_byte(&enc->out_buf, 0x04);
1052 4 bytestream_put_byte(&enc->out_buf, 0x00);
1053 4 }
1054
1055 21 static int roq_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
1056 const AVFrame *frame, int *got_packet)
1057 {
1058 21 RoqEncContext *const enc = avctx->priv_data;
1059 21 RoqContext *const roq = &enc->common;
1060 int size, ret;
1061
1062 21 roq->avctx = avctx;
1063
1064 21 enc->frame_to_enc = frame;
1065
1066
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (frame->quality)
1067 enc->lambda = frame->quality - 1;
1068 else
1069 21 enc->lambda = 2*ROQ_LAMBDA_SCALE;
1070
1071 /* 138 bits max per 8x8 block +
1072 * 256 codebooks*(6 bytes 2x2 + 4 bytes 4x4) + 8 bytes frame header */
1073 21 size = ((roq->width * roq->height / 64) * 138 + 7) / 8 + 256 * (6 + 4) + 8;
1074
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
21 if ((ret = ff_alloc_packet(avctx, pkt, size)) < 0)
1075 return ret;
1076 21 enc->out_buf = pkt->data;
1077
1078 /* Check for I-frame */
1079
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (enc->framesSinceKeyframe == avctx->gop_size)
1080 enc->framesSinceKeyframe = 0;
1081
1082
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 17 times.
21 if (enc->first_frame) {
1083 /* Alloc memory for the reconstruction data (we must know the stride
1084 for that) */
1085
2/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
8 if ((ret = ff_get_buffer(avctx, roq->current_frame, 0)) < 0 ||
1086 4 (ret = ff_get_buffer(avctx, roq->last_frame, 0)) < 0)
1087 return ret;
1088
1089 /* Before the first video frame, write a "video info" chunk */
1090 4 roq_write_video_info_chunk(enc);
1091
1092 4 enc->first_frame = 0;
1093 }
1094
1095 /* Encode the actual frame */
1096 21 ret = roq_encode_video(enc);
1097
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (ret < 0)
1098 return ret;
1099
1100 21 pkt->size = enc->out_buf - pkt->data;
1101
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 17 times.
21 if (enc->framesSinceKeyframe == 1)
1102 4 pkt->flags |= AV_PKT_FLAG_KEY;
1103 21 *got_packet = 1;
1104
1105 21 return 0;
1106 }
1107
1108 #define OFFSET(x) offsetof(RoqEncContext, x)
1109 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1110 static const AVOption options[] = {
1111 { "quake3_compat", "Whether to respect known limitations in Quake 3 decoder", OFFSET(quake3_compat), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, VE },
1112 { NULL },
1113 };
1114
1115 static const AVClass roq_class = {
1116 .class_name = "RoQ",
1117 .item_name = av_default_item_name,
1118 .option = options,
1119 .version = LIBAVUTIL_VERSION_INT,
1120 };
1121
1122 const AVCodec ff_roq_encoder = {
1123 .name = "roqvideo",
1124 .long_name = NULL_IF_CONFIG_SMALL("id RoQ video"),
1125 .type = AVMEDIA_TYPE_VIDEO,
1126 .id = AV_CODEC_ID_ROQ,
1127 .priv_data_size = sizeof(RoqEncContext),
1128 .init = roq_encode_init,
1129 .encode2 = roq_encode_frame,
1130 .close = roq_encode_end,
1131 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUVJ444P,
1132 AV_PIX_FMT_NONE },
1133 .priv_class = &roq_class,
1134 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
1135 };
1136