FFmpeg coverage


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