Coverage Report

Created: 2026-02-14 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/cinepakenc.c
Line
Count
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/intreadwrite.h"
41
#include "libavutil/lfg.h"
42
#include "libavutil/mem.h"
43
#include "libavutil/opt.h"
44
45
#include "avcodec.h"
46
#include "codec_internal.h"
47
#include "elbg.h"
48
#include "encode.h"
49
50
49.4k
#define CVID_HEADER_SIZE 10
51
208k
#define STRIP_HEADER_SIZE 12
52
2.93M
#define CHUNK_HEADER_SIZE 4
53
54
2.50G
#define MB_SIZE 4           //4x4 MBs
55
2.86M
#define MB_AREA (MB_SIZE * MB_SIZE)
56
57
763
#define VECTOR_MAX     6    // six or four entries per vector depending on format
58
277k
#define CODEBOOK_MAX 256    // size of a codebook
59
60
#define MAX_STRIPS  32      // Note: having fewer choices regarding the number of strips speeds up encoding (obviously)
61
#define MIN_STRIPS   1      // Note: having more strips speeds up encoding the frame (this is less obvious)
62
// MAX_STRIPS limits the maximum quality you can reach
63
//            when you want high quality on high resolutions,
64
// MIN_STRIPS limits the minimum efficiently encodable bit rate
65
//            on low resolutions
66
// the numbers are only used for brute force optimization for the first frame,
67
// for the following frames they are adaptively readjusted
68
// NOTE the decoder in ffmpeg has its own arbitrary limitation on the number
69
// of strips, currently 32
70
71
typedef enum CinepakMode {
72
    MODE_V1_ONLY = 0,
73
    MODE_V1_V4,
74
    MODE_MC,
75
76
    MODE_COUNT,
77
} CinepakMode;
78
79
typedef enum mb_encoding {
80
    ENC_V1,
81
    ENC_V4,
82
    ENC_SKIP,
83
84
    ENC_UNCERTAIN
85
} mb_encoding;
86
87
typedef struct mb_info {
88
    int v1_vector;              // index into v1 codebook
89
    int v1_error;               // error when using V1 encoding
90
    int v4_vector[4];           // indices into v4 codebook
91
    int v4_error;               // error when using V4 encoding
92
    int skip_error;             // error when block is skipped (aka copied from last frame)
93
    mb_encoding best_encoding;  // last result from calculate_mode_score()
94
} mb_info;
95
96
typedef struct strip_info {
97
    int v1_codebook[CODEBOOK_MAX * VECTOR_MAX];
98
    int v4_codebook[CODEBOOK_MAX * VECTOR_MAX];
99
    int v1_size;
100
    int v4_size;
101
    CinepakMode mode;
102
} strip_info;
103
104
typedef struct CinepakEncContext {
105
    const AVClass *class;
106
    AVCodecContext *avctx;
107
    unsigned char *pict_bufs[4], *strip_buf, *frame_buf;
108
    AVFrame *last_frame;
109
    AVFrame *best_frame;
110
    AVFrame *scratch_frame;
111
    AVFrame *input_frame;
112
    enum AVPixelFormat pix_fmt;
113
    int w, h;
114
    int frame_buf_size;
115
    int curframe;
116
    AVLFG randctx;
117
    uint64_t lambda;
118
    int *codebook_input;
119
    int *codebook_closest;
120
    mb_info *mb;                // MB RD state
121
    int min_strips;             // the current limit
122
    int max_strips;             // the current limit
123
    // options
124
    int max_extra_cb_iterations;
125
    int skip_empty_cb;
126
    int min_min_strips;
127
    int max_max_strips;
128
    int strip_number_delta_range;
129
    struct ELBGContext *elbg;
130
} CinepakEncContext;
131
132
#define OFFSET(x) offsetof(CinepakEncContext, x)
133
#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
134
static const AVOption options[] = {
135
    { "max_extra_cb_iterations", "Max extra codebook recalculation passes, more is better and slower",
136
      OFFSET(max_extra_cb_iterations),  AV_OPT_TYPE_INT, { .i64 =          2 },          0, INT_MAX,                 VE },
137
    { "skip_empty_cb",           "Avoid wasting bytes, ignore vintage MacOS decoder",
138
      OFFSET(skip_empty_cb),            AV_OPT_TYPE_BOOL, { .i64 =         0 },          0, 1,                       VE },
139
    { "max_strips",              "Limit strips/frame, vintage compatible is 1..3, otherwise the more the better",
140
      OFFSET(max_max_strips),           AV_OPT_TYPE_INT, { .i64 =          3 }, MIN_STRIPS, MAX_STRIPS,              VE },
141
    { "min_strips",              "Enforce min strips/frame, more is worse and faster, must be <= max_strips",
142
      OFFSET(min_min_strips),           AV_OPT_TYPE_INT, { .i64 = MIN_STRIPS }, MIN_STRIPS, MAX_STRIPS,              VE },
143
    { "strip_number_adaptivity", "How fast the strip number adapts, more is slightly better, much slower",
144
      OFFSET(strip_number_delta_range), AV_OPT_TYPE_INT, { .i64 =          0 },          0, MAX_STRIPS - MIN_STRIPS, VE },
145
    { NULL },
146
};
147
148
static const AVClass cinepak_class = {
149
    .class_name = "cinepak",
150
    .item_name  = av_default_item_name,
151
    .option     = options,
152
    .version    = LIBAVUTIL_VERSION_INT,
153
};
154
155
static av_cold int cinepak_encode_init(AVCodecContext *avctx)
156
786
{
157
786
    CinepakEncContext *s = avctx->priv_data;
158
786
    int x, mb_count, strip_buf_size, frame_buf_size;
159
160
786
    if (avctx->width & 3 || avctx->height & 3) {
161
23
        av_log(avctx, AV_LOG_ERROR, "width and height must be multiples of four (got %ix%i)\n",
162
23
               avctx->width, avctx->height);
163
23
        return AVERROR(EINVAL);
164
23
    }
165
166
763
    if (s->min_min_strips > s->max_max_strips) {
167
0
        av_log(avctx, AV_LOG_ERROR, "minimum number of strips must not exceed maximum (got %i and %i)\n",
168
0
               s->min_min_strips, s->max_max_strips);
169
0
        return AVERROR(EINVAL);
170
0
    }
171
172
763
    if (!(s->last_frame = av_frame_alloc()))
173
0
        return AVERROR(ENOMEM);
174
763
    if (!(s->best_frame = av_frame_alloc()))
175
0
        return AVERROR(ENOMEM);
176
763
    if (!(s->scratch_frame = av_frame_alloc()))
177
0
        return AVERROR(ENOMEM);
178
763
    if (avctx->pix_fmt == AV_PIX_FMT_RGB24)
179
220
        if (!(s->input_frame = av_frame_alloc()))
180
0
            return AVERROR(ENOMEM);
181
182
763
    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))))
183
0
        return AVERROR(ENOMEM);
184
185
763
    if (!(s->codebook_closest = av_malloc_array((avctx->width * avctx->height) >> 2, sizeof(*s->codebook_closest))))
186
0
        return AVERROR(ENOMEM);
187
188
3.27k
    for (x = 0; x < (avctx->pix_fmt == AV_PIX_FMT_RGB24 ? 4 : 3); x++)
189
2.50k
        if (!(s->pict_bufs[x] = av_malloc((avctx->pix_fmt == AV_PIX_FMT_RGB24 ? 6 : 4) * (avctx->width * avctx->height) >> 2)))
190
0
            return AVERROR(ENOMEM);
191
192
763
    mb_count = avctx->width * avctx->height / MB_AREA;
193
194
    // the largest possible chunk is 0x31 with all MBs encoded in V4 mode
195
    // and full codebooks being replaced in INTER mode,
196
    // which is 34 bits per MB
197
    // and 2*256 extra flag bits per strip
198
763
    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;
199
200
763
    frame_buf_size = CVID_HEADER_SIZE + s->max_max_strips * strip_buf_size;
201
202
763
    if (!(s->strip_buf = av_malloc(strip_buf_size)))
203
0
        return AVERROR(ENOMEM);
204
205
763
    if (!(s->frame_buf = av_malloc(frame_buf_size)))
206
0
        return AVERROR(ENOMEM);
207
208
763
    if (!(s->mb = av_malloc_array(mb_count, sizeof(mb_info))))
209
0
        return AVERROR(ENOMEM);
210
211
763
    av_lfg_init(&s->randctx, 1);
212
763
    s->avctx          = avctx;
213
763
    s->w              = avctx->width;
214
763
    s->h              = avctx->height;
215
763
    s->frame_buf_size = frame_buf_size;
216
763
    s->curframe       = 0;
217
763
    s->pix_fmt        = avctx->pix_fmt;
218
219
    // set up AVFrames
220
763
    s->last_frame->data[0]        = s->pict_bufs[0];
221
763
    s->last_frame->linesize[0]    = s->w;
222
763
    s->best_frame->data[0]        = s->pict_bufs[1];
223
763
    s->best_frame->linesize[0]    = s->w;
224
763
    s->scratch_frame->data[0]     = s->pict_bufs[2];
225
763
    s->scratch_frame->linesize[0] = s->w;
226
227
763
    if (s->pix_fmt == AV_PIX_FMT_RGB24) {
228
220
        s->last_frame->data[1]     = s->last_frame->data[0] +   s->w * s->h;
229
220
        s->last_frame->data[2]     = s->last_frame->data[1] + ((s->w * s->h) >> 2);
230
220
        s->last_frame->linesize[1] =
231
220
        s->last_frame->linesize[2] = s->w >> 1;
232
233
220
        s->best_frame->data[1]     = s->best_frame->data[0] +   s->w * s->h;
234
220
        s->best_frame->data[2]     = s->best_frame->data[1] + ((s->w * s->h) >> 2);
235
220
        s->best_frame->linesize[1] =
236
220
        s->best_frame->linesize[2] = s->w >> 1;
237
238
220
        s->scratch_frame->data[1]     = s->scratch_frame->data[0] +   s->w * s->h;
239
220
        s->scratch_frame->data[2]     = s->scratch_frame->data[1] + ((s->w * s->h) >> 2);
240
220
        s->scratch_frame->linesize[1] =
241
220
        s->scratch_frame->linesize[2] = s->w >> 1;
242
243
220
        s->input_frame->data[0]     = s->pict_bufs[3];
244
220
        s->input_frame->linesize[0] = s->w;
245
220
        s->input_frame->data[1]     = s->input_frame->data[0] +   s->w * s->h;
246
220
        s->input_frame->data[2]     = s->input_frame->data[1] + ((s->w * s->h) >> 2);
247
220
        s->input_frame->linesize[1] =
248
220
        s->input_frame->linesize[2] = s->w >> 1;
249
220
    }
250
251
763
    s->min_strips = s->min_min_strips;
252
763
    s->max_strips = s->max_max_strips;
253
254
763
    return 0;
255
763
}
256
257
static int64_t calculate_mode_score(CinepakEncContext *s, int h,
258
                                    strip_info *info, int report,
259
                                    int *training_set_v1_shrunk,
260
                                    int *training_set_v4_shrunk)
261
893k
{
262
    // score = FF_LAMBDA_SCALE * error + lambda * bits
263
893k
    int x;
264
893k
    int entry_size = s->pix_fmt == AV_PIX_FMT_RGB24 ? 6 : 4;
265
893k
    int mb_count   = s->w * h / MB_AREA;
266
893k
    mb_info *mb;
267
893k
    int64_t score1, score2, score3;
268
893k
    int64_t ret = s->lambda * ((info->v1_size ? CHUNK_HEADER_SIZE + info->v1_size * entry_size : 0) +
269
893k
                               (info->v4_size ? CHUNK_HEADER_SIZE + info->v4_size * entry_size : 0) +
270
893k
                               CHUNK_HEADER_SIZE) << 3;
271
272
893k
    switch (info->mode) {
273
279k
    case MODE_V1_ONLY:
274
        // one byte per MB
275
279k
        ret += s->lambda * 8 * mb_count;
276
277
        // while calculating we assume all blocks are ENC_V1
278
27.4M
        for (x = 0; x < mb_count; x++) {
279
27.2M
            mb   = &s->mb[x];
280
27.2M
            ret += FF_LAMBDA_SCALE * mb->v1_error;
281
            // this function is never called for report in MODE_V1_ONLY
282
            // if (!report)
283
27.2M
            mb->best_encoding = ENC_V1;
284
27.2M
        }
285
286
279k
        break;
287
350k
    case MODE_V1_V4:
288
        // 9 or 33 bits per MB
289
350k
        if (report) {
290
            // no moves between the corresponding training sets are allowed
291
175k
            *training_set_v1_shrunk = *training_set_v4_shrunk = 0;
292
17.6M
            for (x = 0; x < mb_count; x++) {
293
17.4M
                int mberr;
294
17.4M
                mb = &s->mb[x];
295
17.4M
                if (mb->best_encoding == ENC_V1)
296
14.5M
                    score1 = s->lambda * 9 + FF_LAMBDA_SCALE * (mberr = mb->v1_error);
297
2.91M
                else
298
2.91M
                    score1 = s->lambda * 33 + FF_LAMBDA_SCALE * (mberr = mb->v4_error);
299
17.4M
                ret += score1;
300
17.4M
            }
301
175k
        } else { // find best mode per block
302
17.6M
            for (x = 0; x < mb_count; x++) {
303
17.4M
                mb     = &s->mb[x];
304
17.4M
                score1 = s->lambda * 9 + FF_LAMBDA_SCALE * mb->v1_error;
305
17.4M
                score2 = s->lambda * 33 + FF_LAMBDA_SCALE * mb->v4_error;
306
307
17.4M
                if (score1 <= score2) {
308
14.5M
                    ret += score1;
309
14.5M
                    mb->best_encoding = ENC_V1;
310
14.5M
                } else {
311
2.91M
                    ret += score2;
312
2.91M
                    mb->best_encoding = ENC_V4;
313
2.91M
                }
314
17.4M
            }
315
175k
        }
316
317
350k
        break;
318
262k
    case MODE_MC:
319
        // 1, 10 or 34 bits per MB
320
262k
        if (report) {
321
135k
            int v1_shrunk = 0, v4_shrunk = 0;
322
3.74M
            for (x = 0; x < mb_count; x++) {
323
3.61M
                mb = &s->mb[x];
324
                // it is OK to move blocks to ENC_SKIP here
325
                // but not to any codebook encoding!
326
3.61M
                score1 = s->lambda * 1 + FF_LAMBDA_SCALE * mb->skip_error;
327
3.61M
                if (mb->best_encoding == ENC_SKIP) {
328
1.28M
                    ret += score1;
329
2.33M
                } else if (mb->best_encoding == ENC_V1) {
330
1.22M
                    if ((score2 = s->lambda * 10 + FF_LAMBDA_SCALE * mb->v1_error) >= score1) {
331
15.0k
                        mb->best_encoding = ENC_SKIP;
332
15.0k
                        ++v1_shrunk;
333
15.0k
                        ret += score1;
334
1.20M
                    } else {
335
1.20M
                        ret += score2;
336
1.20M
                    }
337
1.22M
                } else {
338
1.11M
                    if ((score3 = s->lambda * 34 + FF_LAMBDA_SCALE * mb->v4_error) >= score1) {
339
11.3k
                        mb->best_encoding = ENC_SKIP;
340
11.3k
                        ++v4_shrunk;
341
11.3k
                        ret += score1;
342
1.09M
                    } else {
343
1.09M
                        ret += score3;
344
1.09M
                    }
345
1.11M
                }
346
3.61M
            }
347
135k
            *training_set_v1_shrunk = v1_shrunk;
348
135k
            *training_set_v4_shrunk = v4_shrunk;
349
135k
        } else { // find best mode per block
350
3.08M
            for (x = 0; x < mb_count; x++) {
351
2.96M
                mb     = &s->mb[x];
352
2.96M
                score1 = s->lambda * 1 + FF_LAMBDA_SCALE * mb->skip_error;
353
2.96M
                score2 = s->lambda * 10 + FF_LAMBDA_SCALE * mb->v1_error;
354
2.96M
                score3 = s->lambda * 34 + FF_LAMBDA_SCALE * mb->v4_error;
355
356
2.96M
                if (score1 <= score2 && score1 <= score3) {
357
1.10M
                    ret += score1;
358
1.10M
                    mb->best_encoding = ENC_SKIP;
359
1.85M
                } else if (score2 <= score3) {
360
937k
                    ret += score2;
361
937k
                    mb->best_encoding = ENC_V1;
362
937k
                } else {
363
915k
                    ret += score3;
364
915k
                    mb->best_encoding = ENC_V4;
365
915k
                }
366
2.96M
            }
367
126k
        }
368
369
262k
        break;
370
893k
    }
371
372
893k
    return ret;
373
893k
}
374
375
static int write_chunk_header(unsigned char *buf, int chunk_type, int chunk_size)
376
522k
{
377
522k
    buf[0] = chunk_type;
378
522k
    AV_WB24(&buf[1], chunk_size + CHUNK_HEADER_SIZE);
379
522k
    return CHUNK_HEADER_SIZE;
380
522k
}
381
382
static int encode_codebook(CinepakEncContext *s, int *codebook, int size,
383
                           int chunk_type_yuv, int chunk_type_gray,
384
                           unsigned char *buf)
385
348k
{
386
348k
    int x, y, ret, entry_size = s->pix_fmt == AV_PIX_FMT_RGB24 ? 6 : 4;
387
348k
    int incremental_codebook_replacement_mode = 0; // hardcoded here,
388
    // the compiler should notice that this is a constant -- rl
389
390
348k
    ret = write_chunk_header(buf,
391
348k
                             s->pix_fmt == AV_PIX_FMT_RGB24 ?
392
17.4k
                             chunk_type_yuv  + (incremental_codebook_replacement_mode ? 1 : 0) :
393
348k
                             chunk_type_gray + (incremental_codebook_replacement_mode ? 1 : 0),
394
348k
                             entry_size * size +
395
348k
                             (incremental_codebook_replacement_mode ? (size + 31) / 32 * 4 : 0));
396
397
    // we do codebook encoding according to the "intra" mode
398
    // but we keep the "dead" code for reference in case we will want
399
    // to use incremental codebook updates (which actually would give us
400
    // "kind of" motion compensation, especially in 1 strip/frame case) -- rl
401
    // (of course, the code will be not useful as-is)
402
348k
    if (incremental_codebook_replacement_mode) {
403
0
        int flags = 0;
404
0
        int flagsind;
405
0
        for (x = 0; x < size; x++) {
406
0
            if (flags == 0) {
407
0
                flagsind = ret;
408
0
                ret     += 4;
409
0
                flags    = 0x80000000;
410
0
            } else
411
0
                flags = ((flags >> 1) | 0x80000000);
412
0
            for (y = 0; y < entry_size; y++)
413
0
                buf[ret++] = codebook[y + x * entry_size] ^ (y >= 4 ? 0x80 : 0);
414
0
            if ((flags & 0xffffffff) == 0xffffffff) {
415
0
                AV_WB32(&buf[flagsind], flags);
416
0
                flags = 0;
417
0
            }
418
0
        }
419
0
        if (flags)
420
0
            AV_WB32(&buf[flagsind], flags);
421
0
    } else
422
2.36M
        for (x = 0; x < size; x++)
423
10.4M
            for (y = 0; y < entry_size; y++)
424
8.46M
                buf[ret++] = codebook[y + x * entry_size] ^ (y >= 4 ? 0x80 : 0);
425
426
348k
    return ret;
427
348k
}
428
429
// sets out to the sub picture starting at (x,y) in in
430
static void get_sub_picture(CinepakEncContext *s, int x, int y,
431
                            uint8_t *const in_data[4], const int in_linesize[4],
432
                            uint8_t *out_data[4], int out_linesize[4])
433
79.7M
{
434
79.7M
    out_data[0]     = in_data[0] + x + y * in_linesize[0];
435
79.7M
    out_linesize[0] = in_linesize[0];
436
437
79.7M
    if (s->pix_fmt == AV_PIX_FMT_RGB24) {
438
32.1M
        out_data[1]     = in_data[1] + (x >> 1) + (y >> 1) * in_linesize[1];
439
32.1M
        out_linesize[1] = in_linesize[1];
440
441
32.1M
        out_data[2]     = in_data[2] + (x >> 1) + (y >> 1) * in_linesize[2];
442
32.1M
        out_linesize[2] = in_linesize[2];
443
32.1M
    }
444
79.7M
}
445
446
// decodes the V1 vector in mb into the 4x4 MB pointed to by data
447
static void decode_v1_vector(CinepakEncContext *s, uint8_t *data[4],
448
                             int linesize[4], int v1_vector, strip_info *info)
449
52.4M
{
450
52.4M
    int entry_size = s->pix_fmt == AV_PIX_FMT_RGB24 ? 6 : 4;
451
452
52.4M
    data[0][0] =
453
52.4M
    data[0][1] =
454
52.4M
    data[0][    linesize[0]] =
455
52.4M
    data[0][1 + linesize[0]] = info->v1_codebook[v1_vector * entry_size];
456
457
52.4M
    data[0][2] =
458
52.4M
    data[0][3] =
459
52.4M
    data[0][2 + linesize[0]] =
460
52.4M
    data[0][3 + linesize[0]] = info->v1_codebook[v1_vector * entry_size + 1];
461
462
52.4M
    data[0][    2 * linesize[0]] =
463
52.4M
    data[0][1 + 2 * linesize[0]] =
464
52.4M
    data[0][    3 * linesize[0]] =
465
52.4M
    data[0][1 + 3 * linesize[0]] = info->v1_codebook[v1_vector * entry_size + 2];
466
467
52.4M
    data[0][2 + 2 * linesize[0]] =
468
52.4M
    data[0][3 + 2 * linesize[0]] =
469
52.4M
    data[0][2 + 3 * linesize[0]] =
470
52.4M
    data[0][3 + 3 * linesize[0]] = info->v1_codebook[v1_vector * entry_size + 3];
471
472
52.4M
    if (s->pix_fmt == AV_PIX_FMT_RGB24) {
473
22.5M
        data[1][0] =
474
22.5M
        data[1][1] =
475
22.5M
        data[1][    linesize[1]] =
476
22.5M
        data[1][1 + linesize[1]] = info->v1_codebook[v1_vector * entry_size + 4];
477
478
22.5M
        data[2][0] =
479
22.5M
        data[2][1] =
480
22.5M
        data[2][    linesize[2]] =
481
22.5M
        data[2][1 + linesize[2]] = info->v1_codebook[v1_vector * entry_size + 5];
482
22.5M
    }
483
52.4M
}
484
485
// decodes the V4 vectors in mb into the 4x4 MB pointed to by data
486
static void decode_v4_vector(CinepakEncContext *s, uint8_t *data[4],
487
                             int linesize[4], int *v4_vector, strip_info *info)
488
24.1M
{
489
24.1M
    int i, x, y, entry_size = s->pix_fmt == AV_PIX_FMT_RGB24 ? 6 : 4;
490
491
72.3M
    for (i = y = 0; y < 4; y += 2) {
492
144M
        for (x = 0; x < 4; x += 2, i++) {
493
96.4M
            data[0][x     +  y      * linesize[0]] = info->v4_codebook[v4_vector[i] * entry_size];
494
96.4M
            data[0][x + 1 +  y      * linesize[0]] = info->v4_codebook[v4_vector[i] * entry_size + 1];
495
96.4M
            data[0][x     + (y + 1) * linesize[0]] = info->v4_codebook[v4_vector[i] * entry_size + 2];
496
96.4M
            data[0][x + 1 + (y + 1) * linesize[0]] = info->v4_codebook[v4_vector[i] * entry_size + 3];
497
498
96.4M
            if (s->pix_fmt == AV_PIX_FMT_RGB24) {
499
31.5M
                data[1][(x >> 1) + (y >> 1) * linesize[1]] = info->v4_codebook[v4_vector[i] * entry_size + 4];
500
31.5M
                data[2][(x >> 1) + (y >> 1) * linesize[2]] = info->v4_codebook[v4_vector[i] * entry_size + 5];
501
31.5M
            }
502
96.4M
        }
503
48.2M
    }
504
24.1M
}
505
506
static void copy_mb(CinepakEncContext *s,
507
                    uint8_t *a_data[4], int a_linesize[4],
508
                    uint8_t *b_data[4], int b_linesize[4])
509
352k
{
510
352k
    int y, p;
511
512
1.76M
    for (y = 0; y < MB_SIZE; y++)
513
1.40M
        memcpy(a_data[0] + y * a_linesize[0], b_data[0] + y * b_linesize[0],
514
1.40M
               MB_SIZE);
515
516
352k
    if (s->pix_fmt == AV_PIX_FMT_RGB24) {
517
121k
        for (p = 1; p <= 2; p++)
518
243k
            for (y = 0; y < MB_SIZE / 2; y++)
519
162k
                memcpy(a_data[p] + y * a_linesize[p],
520
162k
                       b_data[p] + y * b_linesize[p],
521
162k
                       MB_SIZE / 2);
522
40.5k
    }
523
352k
}
524
525
static int encode_mode(CinepakEncContext *s, int h,
526
                       uint8_t *scratch_data[4], int scratch_linesize[4],
527
                       uint8_t *last_data[4], int last_linesize[4],
528
                       strip_info *info, unsigned char *buf)
529
174k
{
530
174k
    int x, y, z, bits, temp_size, header_ofs, ret = 0, mb_count = s->w * h / MB_AREA;
531
174k
    int needs_extra_bit, should_write_temp;
532
174k
    uint32_t flags;
533
174k
    unsigned char temp[64]; // 32/2 = 16 V4 blocks at 4 B each -> 64 B
534
174k
    mb_info *mb;
535
174k
    uint8_t *sub_scratch_data[4] = { 0 }, *sub_last_data[4] = { 0 };
536
174k
    int sub_scratch_linesize[4] = { 0 }, sub_last_linesize[4] = { 0 };
537
538
    // encode codebooks
539
    ////// MacOS vintage decoder compatibility dictates the presence of
540
    ////// the codebook chunk even when the codebook is empty - pretty dumb...
541
    ////// and also the certain order of the codebook chunks -- rl
542
174k
    if (info->v4_size || !s->skip_empty_cb)
543
174k
        ret += encode_codebook(s, info->v4_codebook, info->v4_size, 0x20, 0x24, buf + ret);
544
545
174k
    if (info->v1_size || !s->skip_empty_cb)
546
174k
        ret += encode_codebook(s, info->v1_codebook, info->v1_size, 0x22, 0x26, buf + ret);
547
548
    // update scratch picture
549
1.70M
    for (z = y = 0; y < h; y += MB_SIZE)
550
14.1M
        for (x = 0; x < s->w; x += MB_SIZE, z++) {
551
12.5M
            mb = &s->mb[z];
552
553
12.5M
            get_sub_picture(s, x, y, scratch_data, scratch_linesize,
554
12.5M
                            sub_scratch_data, sub_scratch_linesize);
555
556
12.5M
            if (info->mode == MODE_MC && mb->best_encoding == ENC_SKIP) {
557
352k
                get_sub_picture(s, x, y, last_data, last_linesize,
558
352k
                                sub_last_data, sub_last_linesize);
559
352k
                copy_mb(s, sub_scratch_data, sub_scratch_linesize,
560
352k
                        sub_last_data, sub_last_linesize);
561
12.2M
            } else if (info->mode == MODE_V1_ONLY || mb->best_encoding == ENC_V1)
562
9.54M
                decode_v1_vector(s, sub_scratch_data, sub_scratch_linesize,
563
9.54M
                                 mb->v1_vector, info);
564
2.69M
            else
565
2.69M
                decode_v4_vector(s, sub_scratch_data, sub_scratch_linesize,
566
2.69M
                                 mb->v4_vector, info);
567
12.5M
        }
568
569
174k
    switch (info->mode) {
570
51.6k
    case MODE_V1_ONLY:
571
51.6k
        ret += write_chunk_header(buf + ret, 0x32, mb_count);
572
573
3.76M
        for (x = 0; x < mb_count; x++)
574
3.71M
            buf[ret++] = s->mb[x].v1_vector;
575
576
51.6k
        break;
577
88.8k
    case MODE_V1_V4:
578
        // remember header position
579
88.8k
        header_ofs = ret;
580
88.8k
        ret       += CHUNK_HEADER_SIZE;
581
582
382k
        for (x = 0; x < mb_count; x += 32) {
583
294k
            flags = 0;
584
8.12M
            for (y = x; y < FFMIN(x + 32, mb_count); y++)
585
7.83M
                if (s->mb[y].best_encoding == ENC_V4)
586
2.33M
                    flags |= 1U << (31 - y + x);
587
588
294k
            AV_WB32(&buf[ret], flags);
589
294k
            ret += 4;
590
591
8.12M
            for (y = x; y < FFMIN(x + 32, mb_count); y++) {
592
7.83M
                mb = &s->mb[y];
593
594
7.83M
                if (mb->best_encoding == ENC_V1)
595
5.49M
                    buf[ret++] = mb->v1_vector;
596
2.33M
                else
597
11.6M
                    for (z = 0; z < 4; z++)
598
9.35M
                        buf[ret++] = mb->v4_vector[z];
599
7.83M
            }
600
294k
        }
601
602
88.8k
        write_chunk_header(buf + header_ofs, 0x30, ret - header_ofs - CHUNK_HEADER_SIZE);
603
604
88.8k
        break;
605
33.8k
    case MODE_MC:
606
        // remember header position
607
33.8k
        header_ofs = ret;
608
33.8k
        ret       += CHUNK_HEADER_SIZE;
609
33.8k
        flags      = bits = temp_size = 0;
610
611
1.07M
        for (x = 0; x < mb_count; x++) {
612
1.04M
            mb                = &s->mb[x];
613
1.04M
            flags            |= (uint32_t)(mb->best_encoding != ENC_SKIP) << (31 - bits++);
614
1.04M
            needs_extra_bit   = 0;
615
1.04M
            should_write_temp = 0;
616
617
1.04M
            if (mb->best_encoding != ENC_SKIP) {
618
689k
                if (bits < 32)
619
680k
                    flags |= (uint32_t)(mb->best_encoding == ENC_V4) << (31 - bits++);
620
9.73k
                else
621
9.73k
                    needs_extra_bit = 1;
622
689k
            }
623
624
1.04M
            if (bits == 32) {
625
37.9k
                AV_WB32(&buf[ret], flags);
626
37.9k
                ret  += 4;
627
37.9k
                flags = bits = 0;
628
629
37.9k
                if (mb->best_encoding == ENC_SKIP || needs_extra_bit) {
630
16.5k
                    memcpy(&buf[ret], temp, temp_size);
631
16.5k
                    ret      += temp_size;
632
16.5k
                    temp_size = 0;
633
16.5k
                } else
634
21.4k
                    should_write_temp = 1;
635
37.9k
            }
636
637
1.04M
            if (needs_extra_bit) {
638
9.73k
                flags = (uint32_t)(mb->best_encoding == ENC_V4) << 31;
639
9.73k
                bits  = 1;
640
9.73k
            }
641
642
1.04M
            if (mb->best_encoding == ENC_V1)
643
337k
                temp[temp_size++] = mb->v1_vector;
644
705k
            else if (mb->best_encoding == ENC_V4)
645
1.76M
                for (z = 0; z < 4; z++)
646
1.41M
                    temp[temp_size++] = mb->v4_vector[z];
647
648
1.04M
            if (should_write_temp) {
649
21.4k
                memcpy(&buf[ret], temp, temp_size);
650
21.4k
                ret      += temp_size;
651
21.4k
                temp_size = 0;
652
21.4k
            }
653
1.04M
        }
654
655
33.8k
        if (bits > 0) {
656
30.8k
            AV_WB32(&buf[ret], flags);
657
30.8k
            ret += 4;
658
30.8k
            memcpy(&buf[ret], temp, temp_size);
659
30.8k
            ret += temp_size;
660
30.8k
        }
661
662
33.8k
        write_chunk_header(buf + header_ofs, 0x31, ret - header_ofs - CHUNK_HEADER_SIZE);
663
664
33.8k
        break;
665
174k
    }
666
667
174k
    return ret;
668
174k
}
669
670
// computes distortion of 4x4 MB in b compared to a
671
static int compute_mb_distortion(CinepakEncContext *s,
672
                                 uint8_t *a_data[4], int a_linesize[4],
673
                                 uint8_t *b_data[4], int b_linesize[4])
674
64.7M
{
675
64.7M
    int x, y, p, d, ret = 0;
676
677
323M
    for (y = 0; y < MB_SIZE; y++)
678
1.29G
        for (x = 0; x < MB_SIZE; x++) {
679
1.03G
            d = a_data[0][x + y * a_linesize[0]] - b_data[0][x + y * b_linesize[0]];
680
1.03G
            ret += d * d;
681
1.03G
        }
682
683
64.7M
    if (s->pix_fmt == AV_PIX_FMT_RGB24) {
684
79.5M
        for (p = 1; p <= 2; p++) {
685
159M
            for (y = 0; y < MB_SIZE / 2; y++)
686
318M
                for (x = 0; x < MB_SIZE / 2; x++) {
687
212M
                    d = a_data[p][x + y * a_linesize[p]] - b_data[p][x + y * b_linesize[p]];
688
212M
                    ret += d * d;
689
212M
                }
690
53.0M
        }
691
26.5M
    }
692
693
64.7M
    return ret;
694
64.7M
}
695
696
// return the possibly adjusted size of the codebook
697
244M
#define CERTAIN(x) ((x) != ENC_UNCERTAIN)
698
static int quantize(CinepakEncContext *s, int h, uint8_t *data[4],
699
                    int linesize[4], int v1mode, strip_info *info,
700
                    mb_encoding encoding)
701
1.06M
{
702
1.06M
    int x, y, i, j, k, x2, y2, x3, y3, plane, shift, mbn;
703
1.06M
    int entry_size      = s->pix_fmt == AV_PIX_FMT_RGB24 ? 6 : 4;
704
1.06M
    int *codebook       = v1mode ? info->v1_codebook : info->v4_codebook;
705
1.06M
    int size            = v1mode ? info->v1_size : info->v4_size;
706
1.06M
    uint8_t vq_pict_buf[(MB_AREA * 3) / 2];
707
1.06M
    uint8_t     *sub_data[4],     *vq_data[4];
708
1.06M
    int      sub_linesize[4],  vq_linesize[4];
709
1.06M
    int ret;
710
711
15.5M
    for (mbn = i = y = 0; y < h; y += MB_SIZE) {
712
100M
        for (x = 0; x < s->w; x += MB_SIZE, ++mbn) {
713
86.3M
            int *base;
714
715
86.3M
            if (CERTAIN(encoding)) {
716
                // use for the training only the blocks known to be to be encoded [sic:-]
717
41.6M
                if (s->mb[mbn].best_encoding != encoding)
718
22.0M
                    continue;
719
41.6M
            }
720
721
64.3M
            base = s->codebook_input + i * entry_size;
722
64.3M
            if (v1mode) {
723
                // subsample
724
147M
                for (j = y2 = 0; y2 < entry_size; y2 += 2)
725
313M
                    for (x2 = 0; x2 < 4; x2 += 2, j++) {
726
209M
                        plane   = y2 < 4 ? 0 : 1 + (x2 >> 1);
727
209M
                        shift   = y2 < 4 ? 0 : 1;
728
209M
                        x3      = shift ? 0 : x2;
729
209M
                        y3      = shift ? 0 : y2;
730
209M
                        base[j] = (data[plane][((x + x3) >> shift) +      ((y + y3) >> shift)      * linesize[plane]] +
731
209M
                                   data[plane][((x + x3) >> shift) + 1 +  ((y + y3) >> shift)      * linesize[plane]] +
732
209M
                                   data[plane][((x + x3) >> shift) +     (((y + y3) >> shift) + 1) * linesize[plane]] +
733
209M
                                   data[plane][((x + x3) >> shift) + 1 + (((y + y3) >> shift) + 1) * linesize[plane]]) >> 2;
734
209M
                    }
735
42.8M
            } else {
736
                // copy
737
64.3M
                for (j = y2 = 0; y2 < MB_SIZE; y2 += 2) {
738
128M
                    for (x2 = 0; x2 < MB_SIZE; x2 += 2)
739
490M
                        for (k = 0; k < entry_size; k++, j++) {
740
404M
                            plane = k >= 4 ? k - 3 : 0;
741
742
404M
                            if (k >= 4) {
743
61.5M
                                x3 = (x + x2) >> 1;
744
61.5M
                                y3 = (y + y2) >> 1;
745
342M
                            } else {
746
342M
                                x3 = x + x2 + (k & 1);
747
342M
                                y3 = y + y2 + (k >> 1);
748
342M
                            }
749
750
404M
                            base[j] = data[plane][x3 + y3 * linesize[plane]];
751
404M
                        }
752
42.8M
                }
753
21.4M
            }
754
64.3M
            i += v1mode ? 1 : 4;
755
64.3M
        }
756
14.4M
    }
757
758
1.06M
    if (i == 0) // empty training set, nothing to do
759
170k
        return 0;
760
899k
    if (i < size)
761
213k
        size = i;
762
763
899k
    ret = avpriv_elbg_do(&s->elbg, s->codebook_input, entry_size, i, codebook,
764
899k
                         size, 1, s->codebook_closest, &s->randctx, 0);
765
899k
    if (ret < 0)
766
0
        return ret;
767
768
    // set up vq_data, which contains a single MB
769
899k
    vq_data[0]     = vq_pict_buf;
770
899k
    vq_linesize[0] = MB_SIZE;
771
899k
    vq_data[1]     = &vq_pict_buf[MB_AREA];
772
899k
    vq_data[2]     = vq_data[1] + (MB_AREA >> 2);
773
899k
    vq_linesize[1] =
774
899k
    vq_linesize[2] = MB_SIZE >> 1;
775
776
    // copy indices
777
13.6M
    for (i = j = y = 0; y < h; y += MB_SIZE)
778
91.7M
        for (x = 0; x < s->w; x += MB_SIZE, j++) {
779
78.9M
            mb_info *mb = &s->mb[j];
780
            // skip uninteresting blocks if we know their preferred encoding
781
78.9M
            if (CERTAIN(encoding) && mb->best_encoding != encoding)
782
14.6M
                continue;
783
784
            // point sub_data to current MB
785
64.3M
            get_sub_picture(s, x, y, data, linesize, sub_data, sub_linesize);
786
787
64.3M
            if (v1mode) {
788
42.8M
                mb->v1_vector = s->codebook_closest[i];
789
790
                // fill in vq_data with V1 data
791
42.8M
                decode_v1_vector(s, vq_data, vq_linesize, mb->v1_vector, info);
792
793
42.8M
                mb->v1_error = compute_mb_distortion(s, sub_data, sub_linesize,
794
42.8M
                                                     vq_data, vq_linesize);
795
42.8M
            } else {
796
107M
                for (k = 0; k < 4; k++)
797
85.7M
                    mb->v4_vector[k] = s->codebook_closest[i + k];
798
799
                // fill in vq_data with V4 data
800
21.4M
                decode_v4_vector(s, vq_data, vq_linesize, mb->v4_vector, info);
801
802
21.4M
                mb->v4_error = compute_mb_distortion(s, sub_data, sub_linesize,
803
21.4M
                                                     vq_data, vq_linesize);
804
21.4M
            }
805
64.3M
            i += v1mode ? 1 : 4;
806
64.3M
        }
807
    // check that we did it right in the beginning of the function
808
899k
    av_assert0(i >= size); // training set is no smaller than the codebook
809
810
899k
    return size;
811
899k
}
812
813
static void calculate_skip_errors(CinepakEncContext *s, int h,
814
                                  uint8_t *last_data[4], int last_linesize[4],
815
                                  uint8_t *data[4], int linesize[4],
816
                                  strip_info *info)
817
25.3k
{
818
25.3k
    int x, y, i;
819
25.3k
    uint8_t *sub_last_data    [4], *sub_pict_data    [4];
820
25.3k
    int      sub_last_linesize[4],  sub_pict_linesize[4];
821
822
94.4k
    for (i = y = 0; y < h; y += MB_SIZE)
823
474k
        for (x = 0; x < s->w; x += MB_SIZE, i++) {
824
405k
            get_sub_picture(s, x, y, last_data, last_linesize,
825
405k
                            sub_last_data, sub_last_linesize);
826
405k
            get_sub_picture(s, x, y, data, linesize,
827
405k
                            sub_pict_data, sub_pict_linesize);
828
829
405k
            s->mb[i].skip_error =
830
405k
                compute_mb_distortion(s,
831
405k
                                      sub_last_data, sub_last_linesize,
832
405k
                                      sub_pict_data, sub_pict_linesize);
833
405k
        }
834
25.3k
}
835
836
static void write_strip_keyframe(unsigned char *buf, int keyframe)
837
192k
{
838
    // actually we are exclusively using intra strip coding (how much can we win
839
    // otherwise? how to choose which part of a codebook to update?),
840
    // keyframes are different only because we disallow ENC_SKIP on them -- rl
841
    // (besides, the logic here used to be inverted: )
842
    //    buf[0] = keyframe ? 0x11: 0x10;
843
192k
    buf[0] = keyframe ? 0x10 : 0x11;
844
192k
}
845
846
static void write_strip_header(CinepakEncContext *s, int y, int h, int keyframe,
847
                               unsigned char *buf, int strip_size)
848
174k
{
849
174k
    write_strip_keyframe(buf, keyframe);
850
174k
    AV_WB24(&buf[1], strip_size + STRIP_HEADER_SIZE);
851
    // AV_WB16(&buf[4], y); /* using absolute y values works -- rl */
852
174k
    AV_WB16(&buf[4], 0); /* using relative values works as well -- rl */
853
174k
    AV_WB16(&buf[6], 0);
854
    // AV_WB16(&buf[8], y + h); /* using absolute y values works -- rl */
855
174k
    AV_WB16(&buf[8], h); /* using relative values works as well -- rl */
856
174k
    AV_WB16(&buf[10], s->w);
857
174k
}
858
859
static int rd_strip(CinepakEncContext *s, int y, int h, int keyframe,
860
                    uint8_t *last_data[4], int last_linesize[4],
861
                    uint8_t *data[4], int linesize[4],
862
                    uint8_t *scratch_data[4], int scratch_linesize[4],
863
                    unsigned char *buf, int64_t *best_score, int *no_skip)
864
33.6k
{
865
33.6k
    int64_t score = 0;
866
33.6k
    int best_size = 0;
867
33.6k
    strip_info info;
868
    // for codebook optimization:
869
33.6k
    int v1enough, v1_size, v4enough, v4_size;
870
33.6k
    int new_v1_size, new_v4_size;
871
33.6k
    int v1shrunk, v4shrunk;
872
873
33.6k
    if (!keyframe)
874
25.3k
        calculate_skip_errors(s, h, last_data, last_linesize, data, linesize,
875
25.3k
                              &info);
876
877
    // try some powers of 4 for the size of the codebooks
878
    // constraint the v4 codebook to be no bigger than v1 one,
879
    // (and no less than v1_size/4)
880
    // thus making v1 preferable and possibly losing small details? should be ok
881
171k
#define SMALLEST_CODEBOOK 1
882
138k
    for (v1enough = 0, v1_size = SMALLEST_CODEBOOK; v1_size <= CODEBOOK_MAX && !v1enough; v1_size <<= 2) {
883
384k
        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) {
884
279k
            CinepakMode mode;
885
            // try all modes
886
1.11M
            for (mode = 0; mode < MODE_COUNT; mode++) {
887
                // don't allow MODE_MC in intra frames
888
839k
                if (keyframe && mode == MODE_MC)
889
77.1k
                    continue;
890
891
762k
                if (mode == MODE_V1_ONLY) {
892
279k
                    info.v1_size = v1_size;
893
                    // the size may shrink even before optimizations if the input is short:
894
279k
                    if ((new_v1_size = quantize(s, h, data, linesize, 1,
895
279k
                                                &info, ENC_UNCERTAIN)) < 0)
896
0
                        return new_v1_size;
897
279k
                    info.v1_size = new_v1_size;
898
279k
                    if (info.v1_size < v1_size)
899
                        // too few eligible blocks, no sense in trying bigger sizes
900
98.1k
                        v1enough = 1;
901
902
279k
                    info.v4_size = 0;
903
482k
                } else { // mode != MODE_V1_ONLY
904
                    // if v4 codebook is empty then only allow V1-only mode
905
482k
                    if (!v4_size)
906
180k
                        continue;
907
908
302k
                    if (mode == MODE_V1_V4) {
909
175k
                        info.v4_size = v4_size;
910
175k
                        new_v4_size = quantize(s, h, data, linesize, 0,
911
175k
                                               &info, ENC_UNCERTAIN);
912
175k
                        if (new_v4_size < 0)
913
0
                            return new_v4_size;
914
175k
                        info.v4_size = new_v4_size;
915
175k
                        if (info.v4_size < v4_size)
916
                            // too few eligible blocks, no sense in trying bigger sizes
917
0
                            v4enough = 1;
918
175k
                    }
919
302k
                }
920
921
581k
                info.mode = mode;
922
                // choose the best encoding per block, based on current experience
923
581k
                score = calculate_mode_score(s, h, &info, 0,
924
581k
                                             &v1shrunk, &v4shrunk);
925
926
581k
                if (mode != MODE_V1_ONLY) {
927
302k
                    int extra_iterations_limit = s->max_extra_cb_iterations;
928
                    // recompute the codebooks, omitting the extra blocks
929
                    // we assume we _may_ come here with more blocks to encode than before
930
302k
                    info.v1_size = v1_size;
931
302k
                    new_v1_size = quantize(s, h, data, linesize, 1, &info, ENC_V1);
932
302k
                    if (new_v1_size < 0)
933
0
                        return new_v1_size;
934
302k
                    if (new_v1_size < info.v1_size)
935
174k
                        info.v1_size = new_v1_size;
936
                    // we assume we _may_ come here with more blocks to encode than before
937
302k
                    info.v4_size = v4_size;
938
302k
                    new_v4_size = quantize(s, h, data, linesize, 0, &info, ENC_V4);
939
302k
                    if (new_v4_size < 0)
940
0
                        return new_v4_size;
941
302k
                    if (new_v4_size < info.v4_size)
942
111k
                        info.v4_size = new_v4_size;
943
                    // calculate the resulting score
944
                    // (do not move blocks to codebook encodings now, as some blocks may have
945
                    // got bigger errors despite a smaller training set - but we do not
946
                    // ever grow the training sets back)
947
311k
                    for (;;) {
948
311k
                        score = calculate_mode_score(s, h, &info, 1,
949
311k
                                                     &v1shrunk, &v4shrunk);
950
                        // do we have a reason to reiterate? if so, have we reached the limit?
951
311k
                        if ((!v1shrunk && !v4shrunk) || !extra_iterations_limit--)
952
302k
                            break;
953
                        // recompute the codebooks, omitting the extra blocks
954
9.25k
                        if (v1shrunk) {
955
5.44k
                            info.v1_size = v1_size;
956
5.44k
                            new_v1_size = quantize(s, h, data, linesize, 1, &info, ENC_V1);
957
5.44k
                            if (new_v1_size < 0)
958
0
                                return new_v1_size;
959
5.44k
                            if (new_v1_size < info.v1_size)
960
95
                                info.v1_size = new_v1_size;
961
5.44k
                        }
962
9.25k
                        if (v4shrunk) {
963
5.17k
                            info.v4_size = v4_size;
964
5.17k
                            new_v4_size = quantize(s, h, data, linesize, 0, &info, ENC_V4);
965
5.17k
                            if (new_v4_size < 0)
966
0
                                return new_v4_size;
967
5.17k
                            if (new_v4_size < info.v4_size)
968
43
                                info.v4_size = new_v4_size;
969
5.17k
                        }
970
9.25k
                    }
971
302k
                }
972
973
581k
                if (best_size == 0 || score < *best_score) {
974
174k
                    *best_score = score;
975
174k
                    best_size = encode_mode(s, h,
976
174k
                                            scratch_data, scratch_linesize,
977
174k
                                            last_data, last_linesize, &info,
978
174k
                                            s->strip_buf + STRIP_HEADER_SIZE);
979
                    // in theory we could have MODE_MC without ENC_SKIP,
980
                    // but MODE_V1_V4 will always be more efficient
981
174k
                    *no_skip = info.mode != MODE_MC;
982
983
174k
                    write_strip_header(s, y, h, keyframe, s->strip_buf, best_size);
984
174k
                }
985
581k
            }
986
279k
        }
987
104k
    }
988
989
33.6k
    best_size += STRIP_HEADER_SIZE;
990
33.6k
    memcpy(buf, s->strip_buf, best_size);
991
992
33.6k
    return best_size;
993
33.6k
}
994
995
static int write_cvid_header(CinepakEncContext *s, unsigned char *buf,
996
                             int num_strips, int data_size, int isakeyframe)
997
14.9k
{
998
14.9k
    buf[0] = isakeyframe ? 0 : 1;
999
14.9k
    AV_WB24(&buf[1], data_size + CVID_HEADER_SIZE);
1000
14.9k
    AV_WB16(&buf[4], s->w);
1001
14.9k
    AV_WB16(&buf[6], s->h);
1002
14.9k
    AV_WB16(&buf[8], num_strips);
1003
1004
14.9k
    return CVID_HEADER_SIZE;
1005
14.9k
}
1006
1007
static int rd_frame(CinepakEncContext *s, const AVFrame *frame,
1008
                    int isakeyframe, unsigned char *buf, int buf_size, int *got_keyframe)
1009
11.0k
{
1010
11.0k
    int num_strips, strip, i, y, nexty, size, temp_size, best_size;
1011
11.0k
    uint8_t *last_data    [4], *data    [4], *scratch_data    [4];
1012
11.0k
    int      last_linesize[4],  linesize[4],  scratch_linesize[4];
1013
11.0k
    int64_t best_score = 0, score, score_temp;
1014
11.0k
    int best_nstrips, best_strip_offsets[MAX_STRIPS];
1015
1016
11.0k
    if (s->pix_fmt == AV_PIX_FMT_RGB24) {
1017
732
        int x;
1018
        // build a copy of the given frame in the correct colorspace
1019
157k
        for (y = 0; y < s->h; y += 2)
1020
1.75M
            for (x = 0; x < s->w; x += 2) {
1021
1.59M
                const uint8_t *ir[2];
1022
1.59M
                int32_t r, g, b, rr, gg, bb;
1023
1.59M
                ir[0] = frame->data[0] + x * 3 + y * frame->linesize[0];
1024
1.59M
                ir[1] = ir[0] + frame->linesize[0];
1025
1.59M
                get_sub_picture(s, x, y,
1026
1.59M
                                s->input_frame->data, s->input_frame->linesize,
1027
1.59M
                                scratch_data, scratch_linesize);
1028
1.59M
                r = g = b = 0;
1029
7.98M
                for (i = 0; i < 4; ++i) {
1030
6.38M
                    int i1, i2;
1031
6.38M
                    i1 = (i & 1);
1032
6.38M
                    i2 = (i >= 2);
1033
6.38M
                    rr = ir[i2][i1 * 3 + 0];
1034
6.38M
                    gg = ir[i2][i1 * 3 + 1];
1035
6.38M
                    bb = ir[i2][i1 * 3 + 2];
1036
6.38M
                    r += rr;
1037
6.38M
                    g += gg;
1038
6.38M
                    b += bb;
1039
                    // using fixed point arithmetic for portable repeatability, scaling by 2^23
1040
                    // "Y"
1041
                    // rr = 0.2857 * rr + 0.5714 * gg + 0.1429 * bb;
1042
6.38M
                    rr = (2396625 * rr + 4793251 * gg + 1198732 * bb) >> 23;
1043
6.38M
                    if (rr < 0)
1044
0
                        rr = 0;
1045
6.38M
                    else if (rr > 255)
1046
0
                        rr = 255;
1047
6.38M
                    scratch_data[0][i1 + i2 * scratch_linesize[0]] = rr;
1048
6.38M
                }
1049
                // let us scale down as late as possible
1050
                //                r /= 4; g /= 4; b /= 4;
1051
                // "U"
1052
                // rr = -0.1429 * r - 0.2857 * g + 0.4286 * b;
1053
1.59M
                rr = (-299683 * r - 599156 * g + 898839 * b) >> 23;
1054
1.59M
                if (rr < -128)
1055
0
                    rr = -128;
1056
1.59M
                else if (rr > 127)
1057
0
                    rr = 127;
1058
1.59M
                scratch_data[1][0] = rr + 128; // quantize needs unsigned
1059
                // "V"
1060
                // rr = 0.3571 * r - 0.2857 * g - 0.0714 * b;
1061
1.59M
                rr = (748893 * r - 599156 * g - 149737 * b) >> 23;
1062
1.59M
                if (rr < -128)
1063
0
                    rr = -128;
1064
1.59M
                else if (rr > 127)
1065
0
                    rr = 127;
1066
1.59M
                scratch_data[2][0] = rr + 128; // quantize needs unsigned
1067
1.59M
            }
1068
732
    }
1069
1070
    // would be nice but quite certainly incompatible with vintage players:
1071
    // support encoding zero strips (meaning skip the whole frame)
1072
29.9k
    for (num_strips = s->min_strips; num_strips <= s->max_strips && num_strips <= s->h / MB_SIZE; num_strips++) {
1073
18.8k
        int strip_offsets[MAX_STRIPS];
1074
18.8k
        int all_no_skip = 1;
1075
18.8k
        score = 0;
1076
18.8k
        size  = 0;
1077
1078
52.5k
        for (y = 0, strip = 1; y < s->h; strip++, y = nexty) {
1079
33.6k
            int strip_height, no_skip;
1080
1081
33.6k
            strip_offsets[strip-1] = size + CVID_HEADER_SIZE;
1082
33.6k
            nexty = strip * s->h / num_strips; // <= s->h
1083
            // make nexty the next multiple of 4 if not already there
1084
33.6k
            if (nexty & 3)
1085
6.69k
                nexty += 4 - (nexty & 3);
1086
1087
33.6k
            strip_height = nexty - y;
1088
33.6k
            if (strip_height <= 0) { // can this ever happen?
1089
0
                av_log(s->avctx, AV_LOG_INFO, "skipping zero height strip %i of %i\n", strip, num_strips);
1090
0
                continue;
1091
0
            }
1092
1093
33.6k
            if (s->pix_fmt == AV_PIX_FMT_RGB24)
1094
1.90k
                get_sub_picture(s, 0, y,
1095
1.90k
                                s->input_frame->data, s->input_frame->linesize,
1096
1.90k
                                data, linesize);
1097
31.7k
            else
1098
31.7k
                get_sub_picture(s, 0, y,
1099
31.7k
                                frame->data, frame->linesize,
1100
31.7k
                                data, linesize);
1101
33.6k
            get_sub_picture(s, 0, y,
1102
33.6k
                            s->last_frame->data, s->last_frame->linesize,
1103
33.6k
                            last_data, last_linesize);
1104
33.6k
            get_sub_picture(s, 0, y,
1105
33.6k
                            s->scratch_frame->data, s->scratch_frame->linesize,
1106
33.6k
                            scratch_data, scratch_linesize);
1107
1108
33.6k
            if ((temp_size = rd_strip(s, y, strip_height, isakeyframe,
1109
33.6k
                                      last_data, last_linesize, data, linesize,
1110
33.6k
                                      scratch_data, scratch_linesize,
1111
33.6k
                                      s->frame_buf + strip_offsets[strip-1],
1112
33.6k
                                      &score_temp, &no_skip)) < 0)
1113
0
                return temp_size;
1114
1115
33.6k
            score += score_temp;
1116
33.6k
            size += temp_size;
1117
33.6k
            all_no_skip &= no_skip;
1118
33.6k
        }
1119
1120
18.8k
        if (best_score == 0 || score < best_score) {
1121
14.9k
            best_score = score;
1122
14.9k
            best_size = size + write_cvid_header(s, s->frame_buf, num_strips, size, all_no_skip);
1123
1124
14.9k
            FFSWAP(AVFrame *, s->best_frame, s->scratch_frame);
1125
14.9k
            memcpy(buf, s->frame_buf, best_size);
1126
14.9k
            best_nstrips = num_strips;
1127
14.9k
            *got_keyframe = all_no_skip; // no skip MBs in any strip -> keyframe
1128
14.9k
            memcpy(best_strip_offsets, strip_offsets, sizeof(strip_offsets));
1129
14.9k
        }
1130
        // avoid trying too many strip numbers without a real reason
1131
        // (this makes the processing of the very first frame faster)
1132
18.8k
        if (num_strips - best_nstrips > 4)
1133
0
            break;
1134
18.8k
    }
1135
1136
    // update strip headers
1137
29.2k
    for (i = 0; i < best_nstrips; i++) {
1138
18.2k
        write_strip_keyframe(s->frame_buf + best_strip_offsets[i], *got_keyframe);
1139
18.2k
    }
1140
1141
    // let the number of strips slowly adapt to the changes in the contents,
1142
    // compared to full bruteforcing every time this will occasionally lead
1143
    // to some r/d performance loss but makes encoding up to several times faster
1144
11.0k
    if (!s->strip_number_delta_range) {
1145
11.0k
        if (best_nstrips == s->max_strips) { // let us try to step up
1146
6.26k
            s->max_strips = best_nstrips + 1;
1147
6.26k
            if (s->max_strips >= s->max_max_strips)
1148
3.49k
                s->max_strips = s->max_max_strips;
1149
6.26k
        } else { // try to step down
1150
4.77k
            s->max_strips = best_nstrips;
1151
4.77k
        }
1152
11.0k
        s->min_strips = s->max_strips - 1;
1153
11.0k
        if (s->min_strips < s->min_min_strips)
1154
2.96k
            s->min_strips = s->min_min_strips;
1155
11.0k
    } else {
1156
0
        s->max_strips = best_nstrips + s->strip_number_delta_range;
1157
0
        if (s->max_strips >= s->max_max_strips)
1158
0
            s->max_strips = s->max_max_strips;
1159
0
        s->min_strips = best_nstrips - s->strip_number_delta_range;
1160
0
        if (s->min_strips < s->min_min_strips)
1161
0
            s->min_strips = s->min_min_strips;
1162
0
    }
1163
1164
11.0k
    return best_size;
1165
11.0k
}
1166
1167
static int cinepak_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
1168
                                const AVFrame *frame, int *got_packet)
1169
11.0k
{
1170
11.0k
    CinepakEncContext *s = avctx->priv_data;
1171
11.0k
    int ret, got_keyframe;
1172
1173
11.0k
    s->lambda = frame->quality ? frame->quality - 1 : 2 * FF_LAMBDA_SCALE;
1174
1175
11.0k
    if ((ret = ff_alloc_packet(avctx, pkt, s->frame_buf_size)) < 0)
1176
0
        return ret;
1177
11.0k
    ret       = rd_frame(s, frame, (s->curframe == 0), pkt->data, s->frame_buf_size, &got_keyframe);
1178
11.0k
    pkt->size = ret;
1179
11.0k
    if (got_keyframe) {
1180
7.14k
        pkt->flags |= AV_PKT_FLAG_KEY;
1181
7.14k
        s->curframe = 0;
1182
7.14k
    }
1183
11.0k
    *got_packet = 1;
1184
1185
11.0k
    FFSWAP(AVFrame *, s->last_frame, s->best_frame);
1186
1187
11.0k
    if (++s->curframe >= avctx->gop_size)
1188
2.52k
        s->curframe = 0;
1189
1190
11.0k
    return 0;
1191
11.0k
}
1192
1193
static av_cold int cinepak_encode_end(AVCodecContext *avctx)
1194
786
{
1195
786
    CinepakEncContext *s = avctx->priv_data;
1196
786
    int x;
1197
1198
786
    avpriv_elbg_free(&s->elbg);
1199
786
    av_frame_free(&s->last_frame);
1200
786
    av_frame_free(&s->best_frame);
1201
786
    av_frame_free(&s->scratch_frame);
1202
786
    if (avctx->pix_fmt == AV_PIX_FMT_RGB24)
1203
237
        av_frame_free(&s->input_frame);
1204
786
    av_freep(&s->codebook_input);
1205
786
    av_freep(&s->codebook_closest);
1206
786
    av_freep(&s->strip_buf);
1207
786
    av_freep(&s->frame_buf);
1208
786
    av_freep(&s->mb);
1209
1210
3.38k
    for (x = 0; x < (avctx->pix_fmt == AV_PIX_FMT_RGB24 ? 4 : 3); x++)
1211
2.59k
        av_freep(&s->pict_bufs[x]);
1212
1213
786
    return 0;
1214
786
}
1215
1216
const FFCodec ff_cinepak_encoder = {
1217
    .p.name         = "cinepak",
1218
    CODEC_LONG_NAME("Cinepak"),
1219
    .p.type         = AVMEDIA_TYPE_VIDEO,
1220
    .p.id           = AV_CODEC_ID_CINEPAK,
1221
    .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
1222
    .priv_data_size = sizeof(CinepakEncContext),
1223
    .init           = cinepak_encode_init,
1224
    FF_CODEC_ENCODE_CB(cinepak_encode_frame),
1225
    .close          = cinepak_encode_end,
1226
    CODEC_PIXFMTS(AV_PIX_FMT_RGB24, AV_PIX_FMT_GRAY8),
1227
    .p.priv_class   = &cinepak_class,
1228
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
1229
};