FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/cinepakenc.c
Date: 2021-09-22 23:55:11
Exec Total Coverage
Lines: 529 583 90.7%
Branches: 309 390 79.2%

Line Branch Exec Source
1 /*
2 * Cinepak encoder (c) 2011 Tomas Härdin
3 * http://titan.codemill.se/~tomhar/cinepakenc.patch
4 *
5 * Fixes and improvements, vintage decoders compatibility
6 * (c) 2013, 2014 Rl, Aetey Global Technologies AB
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27 /*
28 * TODO:
29 * - optimize: color space conversion (move conversion to libswscale), ...
30 * MAYBE:
31 * - "optimally" split the frame into several non-regular areas
32 * using a separate codebook pair for each area and approximating
33 * the area by several rectangular strips (generally not full width ones)
34 * (use quadtree splitting? a simple fixed-granularity grid?)
35 */
36
37 #include <string.h>
38
39 #include "libavutil/avassert.h"
40 #include "libavutil/common.h"
41 #include "libavutil/internal.h"
42 #include "libavutil/intreadwrite.h"
43 #include "libavutil/lfg.h"
44 #include "libavutil/opt.h"
45
46 #include "avcodec.h"
47 #include "elbg.h"
48 #include "encode.h"
49 #include "internal.h"
50
51 #define CVID_HEADER_SIZE 10
52 #define STRIP_HEADER_SIZE 12
53 #define CHUNK_HEADER_SIZE 4
54
55 #define MB_SIZE 4 //4x4 MBs
56 #define MB_AREA (MB_SIZE * MB_SIZE)
57
58 #define VECTOR_MAX 6 // six or four entries per vector depending on format
59 #define CODEBOOK_MAX 256 // size of a codebook
60
61 #define MAX_STRIPS 32 // Note: having fewer choices regarding the number of strips speeds up encoding (obviously)
62 #define MIN_STRIPS 1 // Note: having more strips speeds up encoding the frame (this is less obvious)
63 // MAX_STRIPS limits the maximum quality you can reach
64 // when you want high quality on high resolutions,
65 // MIN_STRIPS limits the minimum efficiently encodable bit rate
66 // on low resolutions
67 // the numbers are only used for brute force optimization for the first frame,
68 // for the following frames they are adaptively readjusted
69 // NOTE the decoder in ffmpeg has its own arbitrary limitation on the number
70 // of strips, currently 32
71
72 typedef enum CinepakMode {
73 MODE_V1_ONLY = 0,
74 MODE_V1_V4,
75 MODE_MC,
76
77 MODE_COUNT,
78 } CinepakMode;
79
80 typedef enum mb_encoding {
81 ENC_V1,
82 ENC_V4,
83 ENC_SKIP,
84
85 ENC_UNCERTAIN
86 } mb_encoding;
87
88 typedef struct mb_info {
89 int v1_vector; // index into v1 codebook
90 int v1_error; // error when using V1 encoding
91 int v4_vector[4]; // indices into v4 codebook
92 int v4_error; // error when using V4 encoding
93 int skip_error; // error when block is skipped (aka copied from last frame)
94 mb_encoding best_encoding; // last result from calculate_mode_score()
95 } mb_info;
96
97 typedef struct strip_info {
98 int v1_codebook[CODEBOOK_MAX * VECTOR_MAX];
99 int v4_codebook[CODEBOOK_MAX * VECTOR_MAX];
100 int v1_size;
101 int v4_size;
102 CinepakMode mode;
103 } strip_info;
104
105 typedef struct CinepakEncContext {
106 const AVClass *class;
107 AVCodecContext *avctx;
108 unsigned char *pict_bufs[4], *strip_buf, *frame_buf;
109 AVFrame *last_frame;
110 AVFrame *best_frame;
111 AVFrame *scratch_frame;
112 AVFrame *input_frame;
113 enum AVPixelFormat pix_fmt;
114 int w, h;
115 int frame_buf_size;
116 int curframe, keyint;
117 AVLFG randctx;
118 uint64_t lambda;
119 int *codebook_input;
120 int *codebook_closest;
121 mb_info *mb; // MB RD state
122 int min_strips; // the current limit
123 int max_strips; // the current limit
124 // options
125 int max_extra_cb_iterations;
126 int skip_empty_cb;
127 int min_min_strips;
128 int max_max_strips;
129 int strip_number_delta_range;
130 struct ELBGContext *elbg;
131 } CinepakEncContext;
132
133 #define OFFSET(x) offsetof(CinepakEncContext, x)
134 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
135 static const AVOption options[] = {
136 { "max_extra_cb_iterations", "Max extra codebook recalculation passes, more is better and slower",
137 OFFSET(max_extra_cb_iterations), AV_OPT_TYPE_INT, { .i64 = 2 }, 0, INT_MAX, VE },
138 { "skip_empty_cb", "Avoid wasting bytes, ignore vintage MacOS decoder",
139 OFFSET(skip_empty_cb), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
140 { "max_strips", "Limit strips/frame, vintage compatible is 1..3, otherwise the more the better",
141 OFFSET(max_max_strips), AV_OPT_TYPE_INT, { .i64 = 3 }, MIN_STRIPS, MAX_STRIPS, VE },
142 { "min_strips", "Enforce min strips/frame, more is worse and faster, must be <= max_strips",
143 OFFSET(min_min_strips), AV_OPT_TYPE_INT, { .i64 = MIN_STRIPS }, MIN_STRIPS, MAX_STRIPS, VE },
144 { "strip_number_adaptivity", "How fast the strip number adapts, more is slightly better, much slower",
145 OFFSET(strip_number_delta_range), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, MAX_STRIPS - MIN_STRIPS, VE },
146 { NULL },
147 };
148
149 static const AVClass cinepak_class = {
150 .class_name = "cinepak",
151 .item_name = av_default_item_name,
152 .option = options,
153 .version = LIBAVUTIL_VERSION_INT,
154 };
155
156 3 static av_cold int cinepak_encode_init(AVCodecContext *avctx)
157 {
158 3 CinepakEncContext *s = avctx->priv_data;
159 int x, mb_count, strip_buf_size, frame_buf_size;
160
161
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (avctx->width & 3 || avctx->height & 3) {
162 av_log(avctx, AV_LOG_ERROR, "width and height must be multiples of four (got %ix%i)\n",
163 avctx->width, avctx->height);
164 return AVERROR(EINVAL);
165 }
166
167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (s->min_min_strips > s->max_max_strips) {
168 av_log(avctx, AV_LOG_ERROR, "minimum number of strips must not exceed maximum (got %i and %i)\n",
169 s->min_min_strips, s->max_max_strips);
170 return AVERROR(EINVAL);
171 }
172
173
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (!(s->last_frame = av_frame_alloc()))
174 return AVERROR(ENOMEM);
175
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (!(s->best_frame = av_frame_alloc()))
176 return AVERROR(ENOMEM);
177
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (!(s->scratch_frame = av_frame_alloc()))
178 return AVERROR(ENOMEM);
179
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (avctx->pix_fmt == AV_PIX_FMT_RGB24)
180
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (!(s->input_frame = av_frame_alloc()))
181 return AVERROR(ENOMEM);
182
183
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
3 if (!(s->codebook_input = av_malloc_array((avctx->pix_fmt == AV_PIX_FMT_RGB24 ? 6 : 4) * (avctx->width * avctx->height) >> 2, sizeof(*s->codebook_input))))
184 return AVERROR(ENOMEM);
185
186
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (!(s->codebook_closest = av_malloc_array((avctx->width * avctx->height) >> 2, sizeof(*s->codebook_closest))))
187 return AVERROR(ENOMEM);
188
189
3/4
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 3 times.
15 for (x = 0; x < (avctx->pix_fmt == AV_PIX_FMT_RGB24 ? 4 : 3); x++)
190
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 12 times.
12 if (!(s->pict_bufs[x] = av_malloc((avctx->pix_fmt == AV_PIX_FMT_RGB24 ? 6 : 4) * (avctx->width * avctx->height) >> 2)))
191 return AVERROR(ENOMEM);
192
193 3 mb_count = avctx->width * avctx->height / MB_AREA;
194
195 // the largest possible chunk is 0x31 with all MBs encoded in V4 mode
196 // and full codebooks being replaced in INTER mode,
197 // which is 34 bits per MB
198 // and 2*256 extra flag bits per strip
199 3 strip_buf_size = STRIP_HEADER_SIZE + 3 * CHUNK_HEADER_SIZE + 2 * VECTOR_MAX * CODEBOOK_MAX + 4 * (mb_count + (mb_count + 15) / 16) + (2 * CODEBOOK_MAX) / 8;
200
201 3 frame_buf_size = CVID_HEADER_SIZE + s->max_max_strips * strip_buf_size;
202
203
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (!(s->strip_buf = av_malloc(strip_buf_size)))
204 return AVERROR(ENOMEM);
205
206
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (!(s->frame_buf = av_malloc(frame_buf_size)))
207 return AVERROR(ENOMEM);
208
209
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (!(s->mb = av_malloc_array(mb_count, sizeof(mb_info))))
210 return AVERROR(ENOMEM);
211
212 3 av_lfg_init(&s->randctx, 1);
213 3 s->avctx = avctx;
214 3 s->w = avctx->width;
215 3 s->h = avctx->height;
216 3 s->frame_buf_size = frame_buf_size;
217 3 s->curframe = 0;
218 3 s->keyint = avctx->keyint_min;
219 3 s->pix_fmt = avctx->pix_fmt;
220
221 // set up AVFrames
222 3 s->last_frame->data[0] = s->pict_bufs[0];
223 3 s->last_frame->linesize[0] = s->w;
224 3 s->best_frame->data[0] = s->pict_bufs[1];
225 3 s->best_frame->linesize[0] = s->w;
226 3 s->scratch_frame->data[0] = s->pict_bufs[2];
227 3 s->scratch_frame->linesize[0] = s->w;
228
229
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (s->pix_fmt == AV_PIX_FMT_RGB24) {
230 3 s->last_frame->data[1] = s->last_frame->data[0] + s->w * s->h;
231 3 s->last_frame->data[2] = s->last_frame->data[1] + ((s->w * s->h) >> 2);
232 3 s->last_frame->linesize[1] =
233 3 s->last_frame->linesize[2] = s->w >> 1;
234
235 3 s->best_frame->data[1] = s->best_frame->data[0] + s->w * s->h;
236 3 s->best_frame->data[2] = s->best_frame->data[1] + ((s->w * s->h) >> 2);
237 3 s->best_frame->linesize[1] =
238 3 s->best_frame->linesize[2] = s->w >> 1;
239
240 3 s->scratch_frame->data[1] = s->scratch_frame->data[0] + s->w * s->h;
241 3 s->scratch_frame->data[2] = s->scratch_frame->data[1] + ((s->w * s->h) >> 2);
242 3 s->scratch_frame->linesize[1] =
243 3 s->scratch_frame->linesize[2] = s->w >> 1;
244
245 3 s->input_frame->data[0] = s->pict_bufs[3];
246 3 s->input_frame->linesize[0] = s->w;
247 3 s->input_frame->data[1] = s->input_frame->data[0] + s->w * s->h;
248 3 s->input_frame->data[2] = s->input_frame->data[1] + ((s->w * s->h) >> 2);
249 3 s->input_frame->linesize[1] =
250 3 s->input_frame->linesize[2] = s->w >> 1;
251 }
252
253 3 s->min_strips = s->min_min_strips;
254 3 s->max_strips = s->max_max_strips;
255
256 3 return 0;
257 }
258
259 48249 static int64_t calculate_mode_score(CinepakEncContext *s, int h,
260 strip_info *info, int report,
261 int *training_set_v1_shrunk,
262 int *training_set_v4_shrunk)
263 {
264 // score = FF_LAMBDA_SCALE * error + lambda * bits
265 int x;
266
1/2
✓ Branch 0 taken 48249 times.
✗ Branch 1 not taken.
48249 int entry_size = s->pix_fmt == AV_PIX_FMT_RGB24 ? 6 : 4;
267 48249 int mb_count = s->w * h / MB_AREA;
268 mb_info *mb;
269 int64_t score1, score2, score3;
270
2/2
✓ Branch 0 taken 48222 times.
✓ Branch 1 taken 27 times.
48249 int64_t ret = s->lambda * ((info->v1_size ? CHUNK_HEADER_SIZE + info->v1_size * entry_size : 0) +
271
2/2
✓ Branch 0 taken 37668 times.
✓ Branch 1 taken 10581 times.
48249 (info->v4_size ? CHUNK_HEADER_SIZE + info->v4_size * entry_size : 0) +
272 48249 CHUNK_HEADER_SIZE) << 3;
273
274
3/4
✓ Branch 0 taken 10542 times.
✓ Branch 1 taken 13554 times.
✓ Branch 2 taken 24153 times.
✗ Branch 3 not taken.
48249 switch (info->mode) {
275 10542 case MODE_V1_ONLY:
276 // one byte per MB
277 10542 ret += s->lambda * 8 * mb_count;
278
279 // while calculating we assume all blocks are ENC_V1
280
2/2
✓ Branch 0 taken 3257856 times.
✓ Branch 1 taken 10542 times.
3268398 for (x = 0; x < mb_count; x++) {
281 3257856 mb = &s->mb[x];
282 3257856 ret += FF_LAMBDA_SCALE * mb->v1_error;
283 // this function is never called for report in MODE_V1_ONLY
284 // if (!report)
285 3257856 mb->best_encoding = ENC_V1;
286 }
287
288 10542 break;
289 13554 case MODE_V1_V4:
290 // 9 or 33 bits per MB
291
2/2
✓ Branch 0 taken 6777 times.
✓ Branch 1 taken 6777 times.
13554 if (report) {
292 // no moves between the corresponding training sets are allowed
293 6777 *training_set_v1_shrunk = *training_set_v4_shrunk = 0;
294
2/2
✓ Branch 0 taken 2094336 times.
✓ Branch 1 taken 6777 times.
2101113 for (x = 0; x < mb_count; x++) {
295 int mberr;
296 2094336 mb = &s->mb[x];
297
2/2
✓ Branch 0 taken 664899 times.
✓ Branch 1 taken 1429437 times.
2094336 if (mb->best_encoding == ENC_V1)
298 664899 score1 = s->lambda * 9 + FF_LAMBDA_SCALE * (mberr = mb->v1_error);
299 else
300 1429437 score1 = s->lambda * 33 + FF_LAMBDA_SCALE * (mberr = mb->v4_error);
301 2094336 ret += score1;
302 }
303 } else { // find best mode per block
304
2/2
✓ Branch 0 taken 2094336 times.
✓ Branch 1 taken 6777 times.
2101113 for (x = 0; x < mb_count; x++) {
305 2094336 mb = &s->mb[x];
306 2094336 score1 = s->lambda * 9 + FF_LAMBDA_SCALE * mb->v1_error;
307 2094336 score2 = s->lambda * 33 + FF_LAMBDA_SCALE * mb->v4_error;
308
309
2/2
✓ Branch 0 taken 664899 times.
✓ Branch 1 taken 1429437 times.
2094336 if (score1 <= score2) {
310 664899 ret += score1;
311 664899 mb->best_encoding = ENC_V1;
312 } else {
313 1429437 ret += score2;
314 1429437 mb->best_encoding = ENC_V4;
315 }
316 }
317 }
318
319 13554 break;
320 24153 case MODE_MC:
321 // 1, 10 or 34 bits per MB
322
2/2
✓ Branch 0 taken 17673 times.
✓ Branch 1 taken 6480 times.
24153 if (report) {
323 17673 int v1_shrunk = 0, v4_shrunk = 0;
324
2/2
✓ Branch 0 taken 5454080 times.
✓ Branch 1 taken 17673 times.
5471753 for (x = 0; x < mb_count; x++) {
325 5454080 mb = &s->mb[x];
326 // it is OK to move blocks to ENC_SKIP here
327 // but not to any codebook encoding!
328 5454080 score1 = s->lambda * 1 + FF_LAMBDA_SCALE * mb->skip_error;
329
2/2
✓ Branch 0 taken 2516362 times.
✓ Branch 1 taken 2937718 times.
5454080 if (mb->best_encoding == ENC_SKIP) {
330 2516362 ret += score1;
331
2/2
✓ Branch 0 taken 830323 times.
✓ Branch 1 taken 2107395 times.
2937718 } else if (mb->best_encoding == ENC_V1) {
332
2/2
✓ Branch 0 taken 24137 times.
✓ Branch 1 taken 806186 times.
830323 if ((score2 = s->lambda * 10 + FF_LAMBDA_SCALE * mb->v1_error) >= score1) {
333 24137 mb->best_encoding = ENC_SKIP;
334 24137 ++v1_shrunk;
335 24137 ret += score1;
336 } else {
337 806186 ret += score2;
338 }
339 } else {
340
2/2
✓ Branch 0 taken 75551 times.
✓ Branch 1 taken 2031844 times.
2107395 if ((score3 = s->lambda * 34 + FF_LAMBDA_SCALE * mb->v4_error) >= score1) {
341 75551 mb->best_encoding = ENC_SKIP;
342 75551 ++v4_shrunk;
343 75551 ret += score1;
344 } else {
345 2031844 ret += score3;
346 }
347 }
348 }
349 17673 *training_set_v1_shrunk = v1_shrunk;
350 17673 *training_set_v4_shrunk = v4_shrunk;
351 } else { // find best mode per block
352
2/2
✓ Branch 0 taken 1990656 times.
✓ Branch 1 taken 6480 times.
1997136 for (x = 0; x < mb_count; x++) {
353 1990656 mb = &s->mb[x];
354 1990656 score1 = s->lambda * 1 + FF_LAMBDA_SCALE * mb->skip_error;
355 1990656 score2 = s->lambda * 10 + FF_LAMBDA_SCALE * mb->v1_error;
356 1990656 score3 = s->lambda * 34 + FF_LAMBDA_SCALE * mb->v4_error;
357
358
4/4
✓ Branch 0 taken 1088699 times.
✓ Branch 1 taken 901957 times.
✓ Branch 2 taken 842199 times.
✓ Branch 3 taken 246500 times.
1990656 if (score1 <= score2 && score1 <= score3) {
359 842199 ret += score1;
360 842199 mb->best_encoding = ENC_SKIP;
361
2/2
✓ Branch 0 taken 314890 times.
✓ Branch 1 taken 833567 times.
1148457 } else if (score2 <= score3) {
362 314890 ret += score2;
363 314890 mb->best_encoding = ENC_V1;
364 } else {
365 833567 ret += score3;
366 833567 mb->best_encoding = ENC_V4;
367 }
368 }
369 }
370
371 24153 break;
372 }
373
374 48249 return ret;
375 }
376
377 30759 static int write_chunk_header(unsigned char *buf, int chunk_type, int chunk_size)
378 {
379 30759 buf[0] = chunk_type;
380 30759 AV_WB24(&buf[1], chunk_size + CHUNK_HEADER_SIZE);
381 30759 return CHUNK_HEADER_SIZE;
382 }
383
384 20506 static int encode_codebook(CinepakEncContext *s, int *codebook, int size,
385 int chunk_type_yuv, int chunk_type_gray,
386 unsigned char *buf)
387 {
388
1/2
✓ Branch 0 taken 20506 times.
✗ Branch 1 not taken.
20506 int x, y, ret, entry_size = s->pix_fmt == AV_PIX_FMT_RGB24 ? 6 : 4;
389 20506 int incremental_codebook_replacement_mode = 0; // hardcoded here,
390 // the compiler should notice that this is a constant -- rl
391
392 20506 ret = write_chunk_header(buf,
393
1/2
✓ Branch 0 taken 20506 times.
✗ Branch 1 not taken.
20506 s->pix_fmt == AV_PIX_FMT_RGB24 ?
394 20506 chunk_type_yuv + (incremental_codebook_replacement_mode ? 1 : 0) :
395 chunk_type_gray + (incremental_codebook_replacement_mode ? 1 : 0),
396 20506 entry_size * size +
397
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20506 times.
20506 (incremental_codebook_replacement_mode ? (size + 31) / 32 * 4 : 0));
398
399 // we do codebook encoding according to the "intra" mode
400 // but we keep the "dead" code for reference in case we will want
401 // to use incremental codebook updates (which actually would give us
402 // "kind of" motion compensation, especially in 1 strip/frame case) -- rl
403 // (of course, the code will be not useful as-is)
404
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20506 times.
20506 if (incremental_codebook_replacement_mode) {
405 int flags = 0;
406 int flagsind;
407 for (x = 0; x < size; x++) {
408 if (flags == 0) {
409 flagsind = ret;
410 ret += 4;
411 flags = 0x80000000;
412 } else
413 flags = ((flags >> 1) | 0x80000000);
414 for (y = 0; y < entry_size; y++)
415 buf[ret++] = codebook[y + x * entry_size] ^ (y >= 4 ? 0x80 : 0);
416 if ((flags & 0xffffffff) == 0xffffffff) {
417 AV_WB32(&buf[flagsind], flags);
418 flags = 0;
419 }
420 }
421 if (flags)
422 AV_WB32(&buf[flagsind], flags);
423 } else
424
2/2
✓ Branch 0 taken 731166 times.
✓ Branch 1 taken 20506 times.
751672 for (x = 0; x < size; x++)
425
2/2
✓ Branch 0 taken 4386996 times.
✓ Branch 1 taken 731166 times.
5118162 for (y = 0; y < entry_size; y++)
426
2/2
✓ Branch 0 taken 1462332 times.
✓ Branch 1 taken 2924664 times.
4386996 buf[ret++] = codebook[y + x * entry_size] ^ (y >= 4 ? 0x80 : 0);
427
428 20506 return ret;
429 }
430
431 // sets out to the sub picture starting at (x,y) in in
432 14989880 static void get_sub_picture(CinepakEncContext *s, int x, int y,
433 uint8_t * in_data[4], int in_linesize[4],
434 uint8_t *out_data[4], int out_linesize[4])
435 {
436 14989880 out_data[0] = in_data[0] + x + y * in_linesize[0];
437 14989880 out_linesize[0] = in_linesize[0];
438
439
1/2
✓ Branch 0 taken 14989880 times.
✗ Branch 1 not taken.
14989880 if (s->pix_fmt == AV_PIX_FMT_RGB24) {
440 14989880 out_data[1] = in_data[1] + (x >> 1) + (y >> 1) * in_linesize[1];
441 14989880 out_linesize[1] = in_linesize[1];
442
443 14989880 out_data[2] = in_data[2] + (x >> 1) + (y >> 1) * in_linesize[2];
444 14989880 out_linesize[2] = in_linesize[2];
445 }
446 14989880 }
447
448 // decodes the V1 vector in mb into the 4x4 MB pointed to by data
449 5323426 static void decode_v1_vector(CinepakEncContext *s, uint8_t *data[4],
450 int linesize[4], int v1_vector, strip_info *info)
451 {
452
1/2
✓ Branch 0 taken 5323426 times.
✗ Branch 1 not taken.
5323426 int entry_size = s->pix_fmt == AV_PIX_FMT_RGB24 ? 6 : 4;
453
454 5323426 data[0][0] =
455 5323426 data[0][1] =
456 5323426 data[0][ linesize[0]] =
457 5323426 data[0][1 + linesize[0]] = info->v1_codebook[v1_vector * entry_size];
458
459 5323426 data[0][2] =
460 5323426 data[0][3] =
461 5323426 data[0][2 + linesize[0]] =
462 5323426 data[0][3 + linesize[0]] = info->v1_codebook[v1_vector * entry_size + 1];
463
464 5323426 data[0][ 2 * linesize[0]] =
465 5323426 data[0][1 + 2 * linesize[0]] =
466 5323426 data[0][ 3 * linesize[0]] =
467 5323426 data[0][1 + 3 * linesize[0]] = info->v1_codebook[v1_vector * entry_size + 2];
468
469 5323426 data[0][2 + 2 * linesize[0]] =
470 5323426 data[0][3 + 2 * linesize[0]] =
471 5323426 data[0][2 + 3 * linesize[0]] =
472 5323426 data[0][3 + 3 * linesize[0]] = info->v1_codebook[v1_vector * entry_size + 3];
473
474
1/2
✓ Branch 0 taken 5323426 times.
✗ Branch 1 not taken.
5323426 if (s->pix_fmt == AV_PIX_FMT_RGB24) {
475 5323426 data[1][0] =
476 5323426 data[1][1] =
477 5323426 data[1][ linesize[1]] =
478 5323426 data[1][1 + linesize[1]] = info->v1_codebook[v1_vector * entry_size + 4];
479
480 5323426 data[2][0] =
481 5323426 data[2][1] =
482 5323426 data[2][ linesize[2]] =
483 5323426 data[2][1 + linesize[2]] = info->v1_codebook[v1_vector * entry_size + 5];
484 }
485 5323426 }
486
487 // decodes the V4 vectors in mb into the 4x4 MB pointed to by data
488 6993789 static void decode_v4_vector(CinepakEncContext *s, uint8_t *data[4],
489 int linesize[4], int *v4_vector, strip_info *info)
490 {
491
1/2
✓ Branch 0 taken 6993789 times.
✗ Branch 1 not taken.
6993789 int i, x, y, entry_size = s->pix_fmt == AV_PIX_FMT_RGB24 ? 6 : 4;
492
493
2/2
✓ Branch 0 taken 13987578 times.
✓ Branch 1 taken 6993789 times.
20981367 for (i = y = 0; y < 4; y += 2) {
494
2/2
✓ Branch 0 taken 27975156 times.
✓ Branch 1 taken 13987578 times.
41962734 for (x = 0; x < 4; x += 2, i++) {
495 27975156 data[0][x + y * linesize[0]] = info->v4_codebook[v4_vector[i] * entry_size];
496 27975156 data[0][x + 1 + y * linesize[0]] = info->v4_codebook[v4_vector[i] * entry_size + 1];
497 27975156 data[0][x + (y + 1) * linesize[0]] = info->v4_codebook[v4_vector[i] * entry_size + 2];
498 27975156 data[0][x + 1 + (y + 1) * linesize[0]] = info->v4_codebook[v4_vector[i] * entry_size + 3];
499
500
1/2
✓ Branch 0 taken 27975156 times.
✗ Branch 1 not taken.
27975156 if (s->pix_fmt == AV_PIX_FMT_RGB24) {
501 27975156 data[1][(x >> 1) + (y >> 1) * linesize[1]] = info->v4_codebook[v4_vector[i] * entry_size + 4];
502 27975156 data[2][(x >> 1) + (y >> 1) * linesize[2]] = info->v4_codebook[v4_vector[i] * entry_size + 5];
503 }
504 }
505 }
506 6993789 }
507
508 883619 static void copy_mb(CinepakEncContext *s,
509 uint8_t *a_data[4], int a_linesize[4],
510 uint8_t *b_data[4], int b_linesize[4])
511 {
512 int y, p;
513
514
2/2
✓ Branch 0 taken 3534476 times.
✓ Branch 1 taken 883619 times.
4418095 for (y = 0; y < MB_SIZE; y++)
515 3534476 memcpy(a_data[0] + y * a_linesize[0], b_data[0] + y * b_linesize[0],
516 MB_SIZE);
517
518
1/2
✓ Branch 0 taken 883619 times.
✗ Branch 1 not taken.
883619 if (s->pix_fmt == AV_PIX_FMT_RGB24) {
519
2/2
✓ Branch 0 taken 1767238 times.
✓ Branch 1 taken 883619 times.
2650857 for (p = 1; p <= 2; p++)
520
2/2
✓ Branch 0 taken 3534476 times.
✓ Branch 1 taken 1767238 times.
5301714 for (y = 0; y < MB_SIZE / 2; y++)
521 3534476 memcpy(a_data[p] + y * a_linesize[p],
522 3534476 b_data[p] + y * b_linesize[p],
523 MB_SIZE / 2);
524 }
525 883619 }
526
527 10253 static int encode_mode(CinepakEncContext *s, int h,
528 uint8_t *scratch_data[4], int scratch_linesize[4],
529 uint8_t *last_data[4], int last_linesize[4],
530 strip_info *info, unsigned char *buf)
531 {
532 10253 int x, y, z, bits, temp_size, header_ofs, ret = 0, mb_count = s->w * h / MB_AREA;
533 int needs_extra_bit, should_write_temp;
534 uint32_t flags;
535 unsigned char temp[64]; // 32/2 = 16 V4 blocks at 4 B each -> 64 B
536 mb_info *mb;
537 10253 uint8_t *sub_scratch_data[4] = { 0 }, *sub_last_data[4] = { 0 };
538 10253 int sub_scratch_linesize[4] = { 0 }, sub_last_linesize[4] = { 0 };
539
540 // encode codebooks
541 ////// MacOS vintage decoder compatibility dictates the presence of
542 ////// the codebook chunk even when the codebook is empty - pretty dumb...
543 ////// and also the certain order of the codebook chunks -- rl
544
3/4
✓ Branch 0 taken 1037 times.
✓ Branch 1 taken 9216 times.
✓ Branch 2 taken 1037 times.
✗ Branch 3 not taken.
10253 if (info->v4_size || !s->skip_empty_cb)
545 10253 ret += encode_codebook(s, info->v4_codebook, info->v4_size, 0x20, 0x24, buf + ret);
546
547
3/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 10244 times.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
10253 if (info->v1_size || !s->skip_empty_cb)
548 10253 ret += encode_codebook(s, info->v1_codebook, info->v1_size, 0x22, 0x26, buf + ret);
549
550 // update scratch picture
551
2/2
✓ Branch 0 taken 99152 times.
✓ Branch 1 taken 10253 times.
109405 for (z = y = 0; y < h; y += MB_SIZE)
552
2/2
✓ Branch 0 taken 3172864 times.
✓ Branch 1 taken 99152 times.
3272016 for (x = 0; x < s->w; x += MB_SIZE, z++) {
553 3172864 mb = &s->mb[z];
554
555 3172864 get_sub_picture(s, x, y, scratch_data, scratch_linesize,
556 sub_scratch_data, sub_scratch_linesize);
557
558
4/4
✓ Branch 0 taken 1717120 times.
✓ Branch 1 taken 1455744 times.
✓ Branch 2 taken 883619 times.
✓ Branch 3 taken 833501 times.
3172864 if (info->mode == MODE_MC && mb->best_encoding == ENC_SKIP) {
559 883619 get_sub_picture(s, x, y, last_data, last_linesize,
560 sub_last_data, sub_last_linesize);
561 883619 copy_mb(s, sub_scratch_data, sub_scratch_linesize,
562 sub_last_data, sub_last_linesize);
563
4/4
✓ Branch 0 taken 1973341 times.
✓ Branch 1 taken 315904 times.
✓ Branch 2 taken 512409 times.
✓ Branch 3 taken 1460932 times.
2289245 } else if (info->mode == MODE_V1_ONLY || mb->best_encoding == ENC_V1)
564 828313 decode_v1_vector(s, sub_scratch_data, sub_scratch_linesize,
565 mb->v1_vector, info);
566 else
567 1460932 decode_v4_vector(s, sub_scratch_data, sub_scratch_linesize,
568 1460932 mb->v4_vector, info);
569 }
570
571
3/4
✓ Branch 0 taken 1018 times.
✓ Branch 1 taken 3665 times.
✓ Branch 2 taken 5570 times.
✗ Branch 3 not taken.
10253 switch (info->mode) {
572 1018 case MODE_V1_ONLY:
573 1018 ret += write_chunk_header(buf + ret, 0x32, mb_count);
574
575
2/2
✓ Branch 0 taken 315904 times.
✓ Branch 1 taken 1018 times.
316922 for (x = 0; x < mb_count; x++)
576 315904 buf[ret++] = s->mb[x].v1_vector;
577
578 1018 break;
579 3665 case MODE_V1_V4:
580 // remember header position
581 3665 header_ofs = ret;
582 3665 ret += CHUNK_HEADER_SIZE;
583
584
2/2
✓ Branch 0 taken 35620 times.
✓ Branch 1 taken 3665 times.
39285 for (x = 0; x < mb_count; x += 32) {
585 35620 flags = 0;
586
3/4
✓ Branch 0 taken 1175460 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1139840 times.
✓ Branch 3 taken 35620 times.
1175460 for (y = x; y < FFMIN(x + 32, mb_count); y++)
587
2/2
✓ Branch 0 taken 861679 times.
✓ Branch 1 taken 278161 times.
1139840 if (s->mb[y].best_encoding == ENC_V4)
588 861679 flags |= 1U << (31 - y + x);
589
590 35620 AV_WB32(&buf[ret], flags);
591 35620 ret += 4;
592
593
3/4
✓ Branch 0 taken 1175460 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1139840 times.
✓ Branch 3 taken 35620 times.
1175460 for (y = x; y < FFMIN(x + 32, mb_count); y++) {
594 1139840 mb = &s->mb[y];
595
596
2/2
✓ Branch 0 taken 278161 times.
✓ Branch 1 taken 861679 times.
1139840 if (mb->best_encoding == ENC_V1)
597 278161 buf[ret++] = mb->v1_vector;
598 else
599
2/2
✓ Branch 0 taken 3446716 times.
✓ Branch 1 taken 861679 times.
4308395 for (z = 0; z < 4; z++)
600 3446716 buf[ret++] = mb->v4_vector[z];
601 }
602 }
603
604 3665 write_chunk_header(buf + header_ofs, 0x30, ret - header_ofs - CHUNK_HEADER_SIZE);
605
606 3665 break;
607 5570 case MODE_MC:
608 // remember header position
609 5570 header_ofs = ret;
610 5570 ret += CHUNK_HEADER_SIZE;
611 5570 flags = bits = temp_size = 0;
612
613
2/2
✓ Branch 0 taken 1717120 times.
✓ Branch 1 taken 5570 times.
1722690 for (x = 0; x < mb_count; x++) {
614 1717120 mb = &s->mb[x];
615 1717120 flags |= (uint32_t)(mb->best_encoding != ENC_SKIP) << (31 - bits++);
616 1717120 needs_extra_bit = 0;
617 1717120 should_write_temp = 0;
618
619
2/2
✓ Branch 0 taken 833501 times.
✓ Branch 1 taken 883619 times.
1717120 if (mb->best_encoding != ENC_SKIP) {
620
2/2
✓ Branch 0 taken 810194 times.
✓ Branch 1 taken 23307 times.
833501 if (bits < 32)
621 810194 flags |= (uint32_t)(mb->best_encoding == ENC_V4) << (31 - bits++);
622 else
623 23307 needs_extra_bit = 1;
624 }
625
626
2/2
✓ Branch 0 taken 76945 times.
✓ Branch 1 taken 1640175 times.
1717120 if (bits == 32) {
627 76945 AV_WB32(&buf[ret], flags);
628 76945 ret += 4;
629 76945 flags = bits = 0;
630
631
4/4
✓ Branch 0 taken 49954 times.
✓ Branch 1 taken 26991 times.
✓ Branch 2 taken 23307 times.
✓ Branch 3 taken 26647 times.
76945 if (mb->best_encoding == ENC_SKIP || needs_extra_bit) {
632 50298 memcpy(&buf[ret], temp, temp_size);
633 50298 ret += temp_size;
634 50298 temp_size = 0;
635 } else
636 26647 should_write_temp = 1;
637 }
638
639
2/2
✓ Branch 0 taken 23307 times.
✓ Branch 1 taken 1693813 times.
1717120 if (needs_extra_bit) {
640
2/2
✓ Branch 0 taken 16698 times.
✓ Branch 1 taken 6609 times.
23307 flags = (uint32_t)(mb->best_encoding == ENC_V4) << 31;
641 23307 bits = 1;
642 }
643
644
2/2
✓ Branch 0 taken 234248 times.
✓ Branch 1 taken 1482872 times.
1717120 if (mb->best_encoding == ENC_V1)
645 234248 temp[temp_size++] = mb->v1_vector;
646
2/2
✓ Branch 0 taken 599253 times.
✓ Branch 1 taken 883619 times.
1482872 else if (mb->best_encoding == ENC_V4)
647
2/2
✓ Branch 0 taken 2397012 times.
✓ Branch 1 taken 599253 times.
2996265 for (z = 0; z < 4; z++)
648 2397012 temp[temp_size++] = mb->v4_vector[z];
649
650
2/2
✓ Branch 0 taken 26647 times.
✓ Branch 1 taken 1690473 times.
1717120 if (should_write_temp) {
651 26647 memcpy(&buf[ret], temp, temp_size);
652 26647 ret += temp_size;
653 26647 temp_size = 0;
654 }
655 }
656
657
2/2
✓ Branch 0 taken 5386 times.
✓ Branch 1 taken 184 times.
5570 if (bits > 0) {
658 5386 AV_WB32(&buf[ret], flags);
659 5386 ret += 4;
660 5386 memcpy(&buf[ret], temp, temp_size);
661 5386 ret += temp_size;
662 }
663
664 5570 write_chunk_header(buf + header_ofs, 0x31, ret - header_ofs - CHUNK_HEADER_SIZE);
665
666 5570 break;
667 }
668
669 10253 return ret;
670 }
671
672 // computes distortion of 4x4 MB in b compared to a
673 10249154 static int compute_mb_distortion(CinepakEncContext *s,
674 uint8_t *a_data[4], int a_linesize[4],
675 uint8_t *b_data[4], int b_linesize[4])
676 {
677 10249154 int x, y, p, d, ret = 0;
678
679
2/2
✓ Branch 0 taken 40996616 times.
✓ Branch 1 taken 10249154 times.
51245770 for (y = 0; y < MB_SIZE; y++)
680
2/2
✓ Branch 0 taken 163986464 times.
✓ Branch 1 taken 40996616 times.
204983080 for (x = 0; x < MB_SIZE; x++) {
681 163986464 d = a_data[0][x + y * a_linesize[0]] - b_data[0][x + y * b_linesize[0]];
682 163986464 ret += d * d;
683 }
684
685
1/2
✓ Branch 0 taken 10249154 times.
✗ Branch 1 not taken.
10249154 if (s->pix_fmt == AV_PIX_FMT_RGB24) {
686
2/2
✓ Branch 0 taken 20498308 times.
✓ Branch 1 taken 10249154 times.
30747462 for (p = 1; p <= 2; p++) {
687
2/2
✓ Branch 0 taken 40996616 times.
✓ Branch 1 taken 20498308 times.
61494924 for (y = 0; y < MB_SIZE / 2; y++)
688
2/2
✓ Branch 0 taken 81993232 times.
✓ Branch 1 taken 40996616 times.
122989848 for (x = 0; x < MB_SIZE / 2; x++) {
689 81993232 d = a_data[p][x + y * a_linesize[p]] - b_data[p][x + y * b_linesize[p]];
690 81993232 ret += d * d;
691 }
692 }
693 }
694
695 10249154 return ret;
696 }
697
698 // return the possibly adjusted size of the codebook
699 #define CERTAIN(x) ((x) != ENC_UNCERTAIN)
700 58459 static int quantize(CinepakEncContext *s, int h, uint8_t *data[4],
701 int linesize[4], int v1mode, strip_info *info,
702 mb_encoding encoding)
703 {
704 int x, y, i, j, k, x2, y2, x3, y3, plane, shift, mbn;
705
1/2
✓ Branch 0 taken 58459 times.
✗ Branch 1 not taken.
58459 int entry_size = s->pix_fmt == AV_PIX_FMT_RGB24 ? 6 : 4;
706
2/2
✓ Branch 0 taken 28783 times.
✓ Branch 1 taken 29676 times.
58459 int *codebook = v1mode ? info->v1_codebook : info->v4_codebook;
707
2/2
✓ Branch 0 taken 28783 times.
✓ Branch 1 taken 29676 times.
58459 int size = v1mode ? info->v1_size : info->v4_size;
708 58459 int64_t total_error = 0;
709 uint8_t vq_pict_buf[(MB_AREA * 3) / 2];
710 uint8_t *sub_data[4], *vq_data[4];
711 int sub_linesize[4], vq_linesize[4];
712
713
2/2
✓ Branch 0 taken 564984 times.
✓ Branch 1 taken 58459 times.
623443 for (mbn = i = y = 0; y < h; y += MB_SIZE) {
714
2/2
✓ Branch 0 taken 18079488 times.
✓ Branch 1 taken 564984 times.
18644472 for (x = 0; x < s->w; x += MB_SIZE, ++mbn) {
715 int *base;
716
717
2/2
✓ Branch 0 taken 12727296 times.
✓ Branch 1 taken 5352192 times.
18079488 if (CERTAIN(encoding)) {
718 // use for the training only the blocks known to be to be encoded [sic:-]
719
2/2
✓ Branch 0 taken 8051518 times.
✓ Branch 1 taken 4675778 times.
12727296 if (s->mb[mbn].best_encoding != encoding)
720 8051518 continue;
721 }
722
723 10027970 base = s->codebook_input + i * entry_size;
724
2/2
✓ Branch 0 taken 4495113 times.
✓ Branch 1 taken 5532857 times.
10027970 if (v1mode) {
725 // subsample
726
2/2
✓ Branch 0 taken 13485339 times.
✓ Branch 1 taken 4495113 times.
17980452 for (j = y2 = 0; y2 < entry_size; y2 += 2)
727
2/2
✓ Branch 0 taken 26970678 times.
✓ Branch 1 taken 13485339 times.
40456017 for (x2 = 0; x2 < 4; x2 += 2, j++) {
728
2/2
✓ Branch 0 taken 8990226 times.
✓ Branch 1 taken 17980452 times.
26970678 plane = y2 < 4 ? 0 : 1 + (x2 >> 1);
729 26970678 shift = y2 < 4 ? 0 : 1;
730
2/2
✓ Branch 0 taken 17980452 times.
✓ Branch 1 taken 8990226 times.
26970678 x3 = shift ? 0 : x2;
731
2/2
✓ Branch 0 taken 17980452 times.
✓ Branch 1 taken 8990226 times.
26970678 y3 = shift ? 0 : y2;
732 26970678 base[j] = (data[plane][((x + x3) >> shift) + ((y + y3) >> shift) * linesize[plane]] +
733 26970678 data[plane][((x + x3) >> shift) + 1 + ((y + y3) >> shift) * linesize[plane]] +
734 26970678 data[plane][((x + x3) >> shift) + (((y + y3) >> shift) + 1) * linesize[plane]] +
735 26970678 data[plane][((x + x3) >> shift) + 1 + (((y + y3) >> shift) + 1) * linesize[plane]]) >> 2;
736 }
737 } else {
738 // copy
739
2/2
✓ Branch 0 taken 11065714 times.
✓ Branch 1 taken 5532857 times.
16598571 for (j = y2 = 0; y2 < MB_SIZE; y2 += 2) {
740
2/2
✓ Branch 0 taken 22131428 times.
✓ Branch 1 taken 11065714 times.
33197142 for (x2 = 0; x2 < MB_SIZE; x2 += 2)
741
2/2
✓ Branch 0 taken 132788568 times.
✓ Branch 1 taken 22131428 times.
154919996 for (k = 0; k < entry_size; k++, j++) {
742 132788568 plane = k >= 4 ? k - 3 : 0;
743
744
2/2
✓ Branch 0 taken 44262856 times.
✓ Branch 1 taken 88525712 times.
132788568 if (k >= 4) {
745 44262856 x3 = (x + x2) >> 1;
746 44262856 y3 = (y + y2) >> 1;
747 } else {
748 88525712 x3 = x + x2 + (k & 1);
749 88525712 y3 = y + y2 + (k >> 1);
750 }
751
752 132788568 base[j] = data[plane][x3 + y3 * linesize[plane]];
753 }
754 }
755 }
756
2/2
✓ Branch 0 taken 4495113 times.
✓ Branch 1 taken 5532857 times.
10027970 i += v1mode ? 1 : 4;
757 }
758 }
759
760
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 58425 times.
58459 if (i == 0) // empty training set, nothing to do
761 34 return 0;
762
2/2
✓ Branch 0 taken 5040 times.
✓ Branch 1 taken 53385 times.
58425 if (i < size)
763 5040 size = i;
764
765 58425 avpriv_elbg_do(&s->elbg, s->codebook_input, entry_size, i, codebook,
766 size, 1, s->codebook_closest, &s->randctx);
767
768 // set up vq_data, which contains a single MB
769 58425 vq_data[0] = vq_pict_buf;
770 58425 vq_linesize[0] = MB_SIZE;
771 58425 vq_data[1] = &vq_pict_buf[MB_AREA];
772 58425 vq_data[2] = vq_data[1] + (MB_AREA >> 2);
773 58425 vq_linesize[1] =
774 58425 vq_linesize[2] = MB_SIZE >> 1;
775
776 // copy indices
777
2/2
✓ Branch 0 taken 564696 times.
✓ Branch 1 taken 58425 times.
623121 for (i = j = y = 0; y < h; y += MB_SIZE)
778
2/2
✓ Branch 0 taken 18070272 times.
✓ Branch 1 taken 564696 times.
18634968 for (x = 0; x < s->w; x += MB_SIZE, j++) {
779 18070272 mb_info *mb = &s->mb[j];
780 // skip uninteresting blocks if we know their preferred encoding
781
4/4
✓ Branch 0 taken 12718080 times.
✓ Branch 1 taken 5352192 times.
✓ Branch 2 taken 8042302 times.
✓ Branch 3 taken 4675778 times.
18070272 if (CERTAIN(encoding) && mb->best_encoding != encoding)
782 8042302 continue;
783
784 // point sub_data to current MB
785 10027970 get_sub_picture(s, x, y, data, linesize, sub_data, sub_linesize);
786
787
2/2
✓ Branch 0 taken 4495113 times.
✓ Branch 1 taken 5532857 times.
10027970 if (v1mode) {
788 4495113 mb->v1_vector = s->codebook_closest[i];
789
790 // fill in vq_data with V1 data
791 4495113 decode_v1_vector(s, vq_data, vq_linesize, mb->v1_vector, info);
792
793 4495113 mb->v1_error = compute_mb_distortion(s, sub_data, sub_linesize,
794 vq_data, vq_linesize);
795 4495113 total_error += mb->v1_error;
796 } else {
797
2/2
✓ Branch 0 taken 22131428 times.
✓ Branch 1 taken 5532857 times.
27664285 for (k = 0; k < 4; k++)
798 22131428 mb->v4_vector[k] = s->codebook_closest[i + k];
799
800 // fill in vq_data with V4 data
801 5532857 decode_v4_vector(s, vq_data, vq_linesize, mb->v4_vector, info);
802
803 5532857 mb->v4_error = compute_mb_distortion(s, sub_data, sub_linesize,
804 vq_data, vq_linesize);
805 5532857 total_error += mb->v4_error;
806 }
807
2/2
✓ Branch 0 taken 4495113 times.
✓ Branch 1 taken 5532857 times.
10027970 i += v1mode ? 1 : 4;
808 }
809 // check that we did it right in the beginning of the function
810
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58425 times.
58425 av_assert0(i >= size); // training set is no smaller than the codebook
811
812 58425 return size;
813 }
814
815 720 static void calculate_skip_errors(CinepakEncContext *s, int h,
816 uint8_t *last_data[4], int last_linesize[4],
817 uint8_t *data[4], int linesize[4],
818 strip_info *info)
819 {
820 int x, y, i;
821 uint8_t *sub_last_data [4], *sub_pict_data [4];
822 int sub_last_linesize[4], sub_pict_linesize[4];
823
824
2/2
✓ Branch 0 taken 6912 times.
✓ Branch 1 taken 720 times.
7632 for (i = y = 0; y < h; y += MB_SIZE)
825
2/2
✓ Branch 0 taken 221184 times.
✓ Branch 1 taken 6912 times.
228096 for (x = 0; x < s->w; x += MB_SIZE, i++) {
826 221184 get_sub_picture(s, x, y, last_data, last_linesize,
827 sub_last_data, sub_last_linesize);
828 221184 get_sub_picture(s, x, y, data, linesize,
829 sub_pict_data, sub_pict_linesize);
830
831 221184 s->mb[i].skip_error =
832 221184 compute_mb_distortion(s,
833 sub_last_data, sub_last_linesize,
834 sub_pict_data, sub_pict_linesize);
835 }
836 720 }
837
838 10253 static void write_strip_header(CinepakEncContext *s, int y, int h, int keyframe,
839 unsigned char *buf, int strip_size)
840 {
841 // actually we are exclusively using intra strip coding (how much can we win
842 // otherwise? how to choose which part of a codebook to update?),
843 // keyframes are different only because we disallow ENC_SKIP on them -- rl
844 // (besides, the logic here used to be inverted: )
845 // buf[0] = keyframe ? 0x11: 0x10;
846
2/2
✓ Branch 0 taken 359 times.
✓ Branch 1 taken 9894 times.
10253 buf[0] = keyframe ? 0x10 : 0x11;
847 10253 AV_WB24(&buf[1], strip_size + STRIP_HEADER_SIZE);
848 // AV_WB16(&buf[4], y); /* using absolute y values works -- rl */
849 10253 AV_WB16(&buf[4], 0); /* using relative values works as well -- rl */
850 10253 AV_WB16(&buf[6], 0);
851 // AV_WB16(&buf[8], y + h); /* using absolute y values works -- rl */
852 10253 AV_WB16(&buf[8], h); /* using relative values works as well -- rl */
853 10253 AV_WB16(&buf[10], s->w);
854 10253 }
855
856 753 static int rd_strip(CinepakEncContext *s, int y, int h, int keyframe,
857 uint8_t *last_data[4], int last_linesize[4],
858 uint8_t *data[4], int linesize[4],
859 uint8_t *scratch_data[4], int scratch_linesize[4],
860 unsigned char *buf, int64_t *best_score)
861 {
862 753 int64_t score = 0;
863 753 int best_size = 0;
864 strip_info info;
865 // for codebook optimization:
866 int v1enough, v1_size, v4enough, v4_size;
867 int new_v1_size, new_v4_size;
868 int v1shrunk, v4shrunk;
869
870
2/2
✓ Branch 0 taken 720 times.
✓ Branch 1 taken 33 times.
753 if (!keyframe)
871 720 calculate_skip_errors(s, h, last_data, last_linesize, data, linesize,
872 &info);
873
874 // try some powers of 4 for the size of the codebooks
875 // constraint the v4 codebook to be no bigger than v1 one,
876 // (and no less than v1_size/4)
877 // thus making v1 preferable and possibly losing small details? should be ok
878 #define SMALLEST_CODEBOOK 1
879
3/4
✓ Branch 0 taken 3765 times.
✓ Branch 1 taken 753 times.
✓ Branch 2 taken 3765 times.
✗ Branch 3 not taken.
4518 for (v1enough = 0, v1_size = SMALLEST_CODEBOOK; v1_size <= CODEBOOK_MAX && !v1enough; v1_size <<= 2) {
880
7/8
✓ Branch 0 taken 6777 times.
✓ Branch 1 taken 3765 times.
✓ Branch 2 taken 3012 times.
✓ Branch 3 taken 753 times.
✓ Branch 4 taken 10542 times.
✓ Branch 5 taken 3765 times.
✓ Branch 6 taken 10542 times.
✗ Branch 7 not taken.
14307 for (v4enough = 0, v4_size = 0; v4_size <= v1_size && !v4enough; v4_size = v4_size ? v4_size << 2 : v1_size >= SMALLEST_CODEBOOK << 2 ? v1_size >> 2 : SMALLEST_CODEBOOK) {
881 CinepakMode mode;
882 // try all modes
883
2/2
✓ Branch 0 taken 31626 times.
✓ Branch 1 taken 10542 times.
42168 for (mode = 0; mode < MODE_COUNT; mode++) {
884 // don't allow MODE_MC in intra frames
885
4/4
✓ Branch 0 taken 1386 times.
✓ Branch 1 taken 30240 times.
✓ Branch 2 taken 462 times.
✓ Branch 3 taken 924 times.
31626 if (keyframe && mode == MODE_MC)
886 462 continue;
887
888
2/2
✓ Branch 0 taken 10542 times.
✓ Branch 1 taken 20622 times.
31164 if (mode == MODE_V1_ONLY) {
889 10542 info.v1_size = v1_size;
890 // the size may shrink even before optimizations if the input is short:
891 10542 info.v1_size = quantize(s, h, data, linesize, 1,
892 &info, ENC_UNCERTAIN);
893
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10542 times.
10542 if (info.v1_size < v1_size)
894 // too few eligible blocks, no sense in trying bigger sizes
895 v1enough = 1;
896
897 10542 info.v4_size = 0;
898 } else { // mode != MODE_V1_ONLY
899 // if v4 codebook is empty then only allow V1-only mode
900
2/2
✓ Branch 0 taken 7365 times.
✓ Branch 1 taken 13257 times.
20622 if (!v4_size)
901 7365 continue;
902
903
2/2
✓ Branch 0 taken 6777 times.
✓ Branch 1 taken 6480 times.
13257 if (mode == MODE_V1_V4) {
904 6777 info.v4_size = v4_size;
905 6777 info.v4_size = quantize(s, h, data, linesize, 0,
906 &info, ENC_UNCERTAIN);
907
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6777 times.
6777 if (info.v4_size < v4_size)
908 // too few eligible blocks, no sense in trying bigger sizes
909 v4enough = 1;
910 }
911 }
912
913 23799 info.mode = mode;
914 // choose the best encoding per block, based on current experience
915 23799 score = calculate_mode_score(s, h, &info, 0,
916 &v1shrunk, &v4shrunk);
917
918
2/2
✓ Branch 0 taken 13257 times.
✓ Branch 1 taken 10542 times.
23799 if (mode != MODE_V1_ONLY) {
919 13257 int extra_iterations_limit = s->max_extra_cb_iterations;
920 // recompute the codebooks, omitting the extra blocks
921 // we assume we _may_ come here with more blocks to encode than before
922 13257 info.v1_size = v1_size;
923 13257 new_v1_size = quantize(s, h, data, linesize, 1, &info, ENC_V1);
924
2/2
✓ Branch 0 taken 5041 times.
✓ Branch 1 taken 8216 times.
13257 if (new_v1_size < info.v1_size)
925 5041 info.v1_size = new_v1_size;
926 // we assume we _may_ come here with more blocks to encode than before
927 13257 info.v4_size = v4_size;
928 13257 new_v4_size = quantize(s, h, data, linesize, 0, &info, ENC_V4);
929
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 13238 times.
13257 if (new_v4_size < info.v4_size)
930 19 info.v4_size = new_v4_size;
931 // calculate the resulting score
932 // (do not move blocks to codebook encodings now, as some blocks may have
933 // got bigger errors despite a smaller training set - but we do not
934 // ever grow the training sets back)
935 for (;;) {
936 24450 score = calculate_mode_score(s, h, &info, 1,
937 &v1shrunk, &v4shrunk);
938 // do we have a reason to reiterate? if so, have we reached the limit?
939
6/6
✓ Branch 0 taken 18374 times.
✓ Branch 1 taken 6076 times.
✓ Branch 2 taken 9295 times.
✓ Branch 3 taken 9079 times.
✓ Branch 4 taken 11193 times.
✓ Branch 5 taken 4178 times.
24450 if ((!v1shrunk && !v4shrunk) || !extra_iterations_limit--)
940 break;
941 // recompute the codebooks, omitting the extra blocks
942
2/2
✓ Branch 0 taken 4984 times.
✓ Branch 1 taken 6209 times.
11193 if (v1shrunk) {
943 4984 info.v1_size = v1_size;
944 4984 new_v1_size = quantize(s, h, data, linesize, 1, &info, ENC_V1);
945
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4972 times.
4984 if (new_v1_size < info.v1_size)
946 12 info.v1_size = new_v1_size;
947 }
948
2/2
✓ Branch 0 taken 9642 times.
✓ Branch 1 taken 1551 times.
11193 if (v4shrunk) {
949 9642 info.v4_size = v4_size;
950 9642 new_v4_size = quantize(s, h, data, linesize, 0, &info, ENC_V4);
951
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 9640 times.
9642 if (new_v4_size < info.v4_size)
952 2 info.v4_size = new_v4_size;
953 }
954 }
955 }
956
957
4/4
✓ Branch 0 taken 23046 times.
✓ Branch 1 taken 753 times.
✓ Branch 2 taken 9500 times.
✓ Branch 3 taken 13546 times.
23799 if (best_size == 0 || score < *best_score) {
958 10253 *best_score = score;
959 10253 best_size = encode_mode(s, h,
960 scratch_data, scratch_linesize,
961 last_data, last_linesize, &info,
962 10253 s->strip_buf + STRIP_HEADER_SIZE);
963
964 10253 write_strip_header(s, y, h, keyframe, s->strip_buf, best_size);
965 }
966 }
967 }
968 }
969
970 753 best_size += STRIP_HEADER_SIZE;
971 753 memcpy(buf, s->strip_buf, best_size);
972
973 753 return best_size;
974 }
975
976 303 static int write_cvid_header(CinepakEncContext *s, unsigned char *buf,
977 int num_strips, int data_size, int isakeyframe)
978 {
979 303 buf[0] = isakeyframe ? 0 : 1;
980 303 AV_WB24(&buf[1], data_size + CVID_HEADER_SIZE);
981 303 AV_WB16(&buf[4], s->w);
982 303 AV_WB16(&buf[6], s->h);
983 303 AV_WB16(&buf[8], num_strips);
984
985 303 return CVID_HEADER_SIZE;
986 }
987
988 150 static int rd_frame(CinepakEncContext *s, const AVFrame *frame,
989 int isakeyframe, unsigned char *buf, int buf_size)
990 {
991 int num_strips, strip, i, y, nexty, size, temp_size, best_size;
992 uint8_t *last_data [4], *data [4], *scratch_data [4];
993 int last_linesize[4], linesize[4], scratch_linesize[4];
994 150 int64_t best_score = 0, score, score_temp;
995 int best_nstrips;
996
997
1/2
✓ Branch 0 taken 150 times.
✗ Branch 1 not taken.
150 if (s->pix_fmt == AV_PIX_FMT_RGB24) {
998 int x;
999 // build a copy of the given frame in the correct colorspace
1000
2/2
✓ Branch 0 taken 7200 times.
✓ Branch 1 taken 150 times.
7350 for (y = 0; y < s->h; y += 2)
1001
2/2
✓ Branch 0 taken 460800 times.
✓ Branch 1 taken 7200 times.
468000 for (x = 0; x < s->w; x += 2) {
1002 uint8_t *ir[2];
1003 int32_t r, g, b, rr, gg, bb;
1004 460800 ir[0] = frame->data[0] + x * 3 + y * frame->linesize[0];
1005 460800 ir[1] = ir[0] + frame->linesize[0];
1006 460800 get_sub_picture(s, x, y,
1007 460800 s->input_frame->data, s->input_frame->linesize,
1008 scratch_data, scratch_linesize);
1009 460800 r = g = b = 0;
1010
2/2
✓ Branch 0 taken 1843200 times.
✓ Branch 1 taken 460800 times.
2304000 for (i = 0; i < 4; ++i) {
1011 int i1, i2;
1012 1843200 i1 = (i & 1);
1013 1843200 i2 = (i >= 2);
1014 1843200 rr = ir[i2][i1 * 3 + 0];
1015 1843200 gg = ir[i2][i1 * 3 + 1];
1016 1843200 bb = ir[i2][i1 * 3 + 2];
1017 1843200 r += rr;
1018 1843200 g += gg;
1019 1843200 b += bb;
1020 // using fixed point arithmetic for portable repeatability, scaling by 2^23
1021 // "Y"
1022 // rr = 0.2857 * rr + 0.5714 * gg + 0.1429 * bb;
1023 1843200 rr = (2396625 * rr + 4793251 * gg + 1198732 * bb) >> 23;
1024
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1843200 times.
1843200 if (rr < 0)
1025 rr = 0;
1026
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1843200 times.
1843200 else if (rr > 255)
1027 rr = 255;
1028 1843200 scratch_data[0][i1 + i2 * scratch_linesize[0]] = rr;
1029 }
1030 // let us scale down as late as possible
1031 // r /= 4; g /= 4; b /= 4;
1032 // "U"
1033 // rr = -0.1429 * r - 0.2857 * g + 0.4286 * b;
1034 460800 rr = (-299683 * r - 599156 * g + 898839 * b) >> 23;
1035
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 460800 times.
460800 if (rr < -128)
1036 rr = -128;
1037
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 460800 times.
460800 else if (rr > 127)
1038 rr = 127;
1039 460800 scratch_data[1][0] = rr + 128; // quantize needs unsigned
1040 // "V"
1041 // rr = 0.3571 * r - 0.2857 * g - 0.0714 * b;
1042 460800 rr = (748893 * r - 599156 * g - 149737 * b) >> 23;
1043
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 460800 times.
460800 if (rr < -128)
1044 rr = -128;
1045
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 460800 times.
460800 else if (rr > 127)
1046 rr = 127;
1047 460800 scratch_data[2][0] = rr + 128; // quantize needs unsigned
1048 }
1049 }
1050
1051 // would be nice but quite certainly incompatible with vintage players:
1052 // support encoding zero strips (meaning skip the whole frame)
1053
3/4
✓ Branch 0 taken 303 times.
✓ Branch 1 taken 150 times.
✓ Branch 2 taken 303 times.
✗ Branch 3 not taken.
453 for (num_strips = s->min_strips; num_strips <= s->max_strips && num_strips <= s->h / MB_SIZE; num_strips++) {
1054 303 score = 0;
1055 303 size = 0;
1056
1057
2/2
✓ Branch 0 taken 753 times.
✓ Branch 1 taken 303 times.
1056 for (y = 0, strip = 1; y < s->h; strip++, y = nexty) {
1058 int strip_height;
1059
1060 753 nexty = strip * s->h / num_strips; // <= s->h
1061 // make nexty the next multiple of 4 if not already there
1062
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 753 times.
753 if (nexty & 3)
1063 nexty += 4 - (nexty & 3);
1064
1065 753 strip_height = nexty - y;
1066
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 753 times.
753 if (strip_height <= 0) { // can this ever happen?
1067 av_log(s->avctx, AV_LOG_INFO, "skipping zero height strip %i of %i\n", strip, num_strips);
1068 continue;
1069 }
1070
1071
1/2
✓ Branch 0 taken 753 times.
✗ Branch 1 not taken.
753 if (s->pix_fmt == AV_PIX_FMT_RGB24)
1072 753 get_sub_picture(s, 0, y,
1073 753 s->input_frame->data, s->input_frame->linesize,
1074 data, linesize);
1075 else
1076 get_sub_picture(s, 0, y,
1077 (uint8_t **)frame->data, (int *)frame->linesize,
1078 data, linesize);
1079 753 get_sub_picture(s, 0, y,
1080 753 s->last_frame->data, s->last_frame->linesize,
1081 last_data, last_linesize);
1082 753 get_sub_picture(s, 0, y,
1083 753 s->scratch_frame->data, s->scratch_frame->linesize,
1084 scratch_data, scratch_linesize);
1085
1086
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 753 times.
753 if ((temp_size = rd_strip(s, y, strip_height, isakeyframe,
1087 last_data, last_linesize, data, linesize,
1088 scratch_data, scratch_linesize,
1089 753 s->frame_buf + size + CVID_HEADER_SIZE,
1090 &score_temp)) < 0)
1091 return temp_size;
1092
1093 753 score += score_temp;
1094 753 size += temp_size;
1095 }
1096
1097
3/4
✓ Branch 0 taken 153 times.
✓ Branch 1 taken 150 times.
✓ Branch 2 taken 153 times.
✗ Branch 3 not taken.
303 if (best_score == 0 || score < best_score) {
1098 303 best_score = score;
1099 303 best_size = size + write_cvid_header(s, s->frame_buf, num_strips, size, isakeyframe);
1100
1101 303 FFSWAP(AVFrame *, s->best_frame, s->scratch_frame);
1102 303 memcpy(buf, s->frame_buf, best_size);
1103 303 best_nstrips = num_strips;
1104 }
1105 // avoid trying too many strip numbers without a real reason
1106 // (this makes the processing of the very first frame faster)
1107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 303 times.
303 if (num_strips - best_nstrips > 4)
1108 break;
1109 }
1110
1111 // let the number of strips slowly adapt to the changes in the contents,
1112 // compared to full bruteforcing every time this will occasionally lead
1113 // to some r/d performance loss but makes encoding up to several times faster
1114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 150 times.
150 if (!s->strip_number_delta_range) {
1115 if (best_nstrips == s->max_strips) { // let us try to step up
1116 s->max_strips = best_nstrips + 1;
1117 if (s->max_strips >= s->max_max_strips)
1118 s->max_strips = s->max_max_strips;
1119 } else { // try to step down
1120 s->max_strips = best_nstrips;
1121 }
1122 s->min_strips = s->max_strips - 1;
1123 if (s->min_strips < s->min_min_strips)
1124 s->min_strips = s->min_min_strips;
1125 } else {
1126 150 s->max_strips = best_nstrips + s->strip_number_delta_range;
1127
1/2
✓ Branch 0 taken 150 times.
✗ Branch 1 not taken.
150 if (s->max_strips >= s->max_max_strips)
1128 150 s->max_strips = s->max_max_strips;
1129 150 s->min_strips = best_nstrips - s->strip_number_delta_range;
1130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 150 times.
150 if (s->min_strips < s->min_min_strips)
1131 s->min_strips = s->min_min_strips;
1132 }
1133
1134 150 return best_size;
1135 }
1136
1137 150 static int cinepak_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
1138 const AVFrame *frame, int *got_packet)
1139 {
1140 150 CinepakEncContext *s = avctx->priv_data;
1141 int ret;
1142
1143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 150 times.
150 s->lambda = frame->quality ? frame->quality - 1 : 2 * FF_LAMBDA_SCALE;
1144
1145
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 150 times.
150 if ((ret = ff_alloc_packet(avctx, pkt, s->frame_buf_size)) < 0)
1146 return ret;
1147 150 ret = rd_frame(s, frame, (s->curframe == 0), pkt->data, s->frame_buf_size);
1148 150 pkt->size = ret;
1149
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 144 times.
150 if (s->curframe == 0)
1150 6 pkt->flags |= AV_PKT_FLAG_KEY;
1151 150 *got_packet = 1;
1152
1153 150 FFSWAP(AVFrame *, s->last_frame, s->best_frame);
1154
1155
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 144 times.
150 if (++s->curframe >= s->keyint)
1156 6 s->curframe = 0;
1157
1158 150 return 0;
1159 }
1160
1161 3 static av_cold int cinepak_encode_end(AVCodecContext *avctx)
1162 {
1163 3 CinepakEncContext *s = avctx->priv_data;
1164 int x;
1165
1166 3 avpriv_elbg_free(&s->elbg);
1167 3 av_frame_free(&s->last_frame);
1168 3 av_frame_free(&s->best_frame);
1169 3 av_frame_free(&s->scratch_frame);
1170
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (avctx->pix_fmt == AV_PIX_FMT_RGB24)
1171 3 av_frame_free(&s->input_frame);
1172 3 av_freep(&s->codebook_input);
1173 3 av_freep(&s->codebook_closest);
1174 3 av_freep(&s->strip_buf);
1175 3 av_freep(&s->frame_buf);
1176 3 av_freep(&s->mb);
1177
1178
3/4
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 3 times.
15 for (x = 0; x < (avctx->pix_fmt == AV_PIX_FMT_RGB24 ? 4 : 3); x++)
1179 12 av_freep(&s->pict_bufs[x]);
1180
1181 3 return 0;
1182 }
1183
1184 const AVCodec ff_cinepak_encoder = {
1185 .name = "cinepak",
1186 .long_name = NULL_IF_CONFIG_SMALL("Cinepak"),
1187 .type = AVMEDIA_TYPE_VIDEO,
1188 .id = AV_CODEC_ID_CINEPAK,
1189 .priv_data_size = sizeof(CinepakEncContext),
1190 .init = cinepak_encode_init,
1191 .encode2 = cinepak_encode_frame,
1192 .close = cinepak_encode_end,
1193 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_RGB24, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE },
1194 .priv_class = &cinepak_class,
1195 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
1196 };
1197