GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/cinepakenc.c Lines: 529 583 90.7 %
Date: 2020-09-28 00:47:38 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 "internal.h"
49
50
#define CVID_HEADER_SIZE 10
51
#define STRIP_HEADER_SIZE 12
52
#define CHUNK_HEADER_SIZE 4
53
54
#define MB_SIZE 4           //4x4 MBs
55
#define MB_AREA (MB_SIZE * MB_SIZE)
56
57
#define VECTOR_MAX     6    // six or four entries per vector depending on format
58
#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, keyint;
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
} CinepakEncContext;
130
131
#define OFFSET(x) offsetof(CinepakEncContext, x)
132
#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
133
static const AVOption options[] = {
134
    { "max_extra_cb_iterations", "Max extra codebook recalculation passes, more is better and slower",
135
      OFFSET(max_extra_cb_iterations),  AV_OPT_TYPE_INT, { .i64 =          2 },          0, INT_MAX,                 VE },
136
    { "skip_empty_cb",           "Avoid wasting bytes, ignore vintage MacOS decoder",
137
      OFFSET(skip_empty_cb),            AV_OPT_TYPE_BOOL, { .i64 =         0 },          0, 1,                       VE },
138
    { "max_strips",              "Limit strips/frame, vintage compatible is 1..3, otherwise the more the better",
139
      OFFSET(max_max_strips),           AV_OPT_TYPE_INT, { .i64 =          3 }, MIN_STRIPS, MAX_STRIPS,              VE },
140
    { "min_strips",              "Enforce min strips/frame, more is worse and faster, must be <= max_strips",
141
      OFFSET(min_min_strips),           AV_OPT_TYPE_INT, { .i64 = MIN_STRIPS }, MIN_STRIPS, MAX_STRIPS,              VE },
142
    { "strip_number_adaptivity", "How fast the strip number adapts, more is slightly better, much slower",
143
      OFFSET(strip_number_delta_range), AV_OPT_TYPE_INT, { .i64 =          0 },          0, MAX_STRIPS - MIN_STRIPS, VE },
144
    { NULL },
145
};
146
147
static const AVClass cinepak_class = {
148
    .class_name = "cinepak",
149
    .item_name  = av_default_item_name,
150
    .option     = options,
151
    .version    = LIBAVUTIL_VERSION_INT,
152
};
153
154
3
static av_cold int cinepak_encode_init(AVCodecContext *avctx)
155
{
156
3
    CinepakEncContext *s = avctx->priv_data;
157
    int x, mb_count, strip_buf_size, frame_buf_size;
158
159

3
    if (avctx->width & 3 || avctx->height & 3) {
160
        av_log(avctx, AV_LOG_ERROR, "width and height must be multiples of four (got %ix%i)\n",
161
               avctx->width, avctx->height);
162
        return AVERROR(EINVAL);
163
    }
164
165
3
    if (s->min_min_strips > s->max_max_strips) {
166
        av_log(avctx, AV_LOG_ERROR, "minimum number of strips must not exceed maximum (got %i and %i)\n",
167
               s->min_min_strips, s->max_max_strips);
168
        return AVERROR(EINVAL);
169
    }
170
171
3
    if (!(s->last_frame = av_frame_alloc()))
172
        return AVERROR(ENOMEM);
173
3
    if (!(s->best_frame = av_frame_alloc()))
174
        return AVERROR(ENOMEM);
175
3
    if (!(s->scratch_frame = av_frame_alloc()))
176
        return AVERROR(ENOMEM);
177
3
    if (avctx->pix_fmt == AV_PIX_FMT_RGB24)
178
3
        if (!(s->input_frame = av_frame_alloc()))
179
            return AVERROR(ENOMEM);
180
181

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))))
182
        return AVERROR(ENOMEM);
183
184
3
    if (!(s->codebook_closest = av_malloc_array((avctx->width * avctx->height) >> 2, sizeof(*s->codebook_closest))))
185
        return AVERROR(ENOMEM);;
186
187

15
    for (x = 0; x < (avctx->pix_fmt == AV_PIX_FMT_RGB24 ? 4 : 3); x++)
188

12
        if (!(s->pict_bufs[x] = av_malloc((avctx->pix_fmt == AV_PIX_FMT_RGB24 ? 6 : 4) * (avctx->width * avctx->height) >> 2)))
189
            return AVERROR(ENOMEM);
190
191
3
    mb_count = avctx->width * avctx->height / MB_AREA;
192
193
    // the largest possible chunk is 0x31 with all MBs encoded in V4 mode
194
    // and full codebooks being replaced in INTER mode,
195
    // which is 34 bits per MB
196
    // and 2*256 extra flag bits per strip
197
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;
198
199
3
    frame_buf_size = CVID_HEADER_SIZE + s->max_max_strips * strip_buf_size;
200
201
3
    if (!(s->strip_buf = av_malloc(strip_buf_size)))
202
        return AVERROR(ENOMEM);
203
204
3
    if (!(s->frame_buf = av_malloc(frame_buf_size)))
205
        return AVERROR(ENOMEM);
206
207
3
    if (!(s->mb = av_malloc_array(mb_count, sizeof(mb_info))))
208
        return AVERROR(ENOMEM);
209
210
3
    av_lfg_init(&s->randctx, 1);
211
3
    s->avctx          = avctx;
212
3
    s->w              = avctx->width;
213
3
    s->h              = avctx->height;
214
3
    s->frame_buf_size = frame_buf_size;
215
3
    s->curframe       = 0;
216
3
    s->keyint         = avctx->keyint_min;
217
3
    s->pix_fmt        = avctx->pix_fmt;
218
219
    // set up AVFrames
220
3
    s->last_frame->data[0]        = s->pict_bufs[0];
221
3
    s->last_frame->linesize[0]    = s->w;
222
3
    s->best_frame->data[0]        = s->pict_bufs[1];
223
3
    s->best_frame->linesize[0]    = s->w;
224
3
    s->scratch_frame->data[0]     = s->pict_bufs[2];
225
3
    s->scratch_frame->linesize[0] = s->w;
226
227
3
    if (s->pix_fmt == AV_PIX_FMT_RGB24) {
228
3
        s->last_frame->data[1]     = s->last_frame->data[0] +   s->w * s->h;
229
3
        s->last_frame->data[2]     = s->last_frame->data[1] + ((s->w * s->h) >> 2);
230
3
        s->last_frame->linesize[1] =
231
3
        s->last_frame->linesize[2] = s->w >> 1;
232
233
3
        s->best_frame->data[1]     = s->best_frame->data[0] +   s->w * s->h;
234
3
        s->best_frame->data[2]     = s->best_frame->data[1] + ((s->w * s->h) >> 2);
235
3
        s->best_frame->linesize[1] =
236
3
        s->best_frame->linesize[2] = s->w >> 1;
237
238
3
        s->scratch_frame->data[1]     = s->scratch_frame->data[0] +   s->w * s->h;
239
3
        s->scratch_frame->data[2]     = s->scratch_frame->data[1] + ((s->w * s->h) >> 2);
240
3
        s->scratch_frame->linesize[1] =
241
3
        s->scratch_frame->linesize[2] = s->w >> 1;
242
243
3
        s->input_frame->data[0]     = s->pict_bufs[3];
244
3
        s->input_frame->linesize[0] = s->w;
245
3
        s->input_frame->data[1]     = s->input_frame->data[0] +   s->w * s->h;
246
3
        s->input_frame->data[2]     = s->input_frame->data[1] + ((s->w * s->h) >> 2);
247
3
        s->input_frame->linesize[1] =
248
3
        s->input_frame->linesize[2] = s->w >> 1;
249
    }
250
251
3
    s->min_strips = s->min_min_strips;
252
3
    s->max_strips = s->max_max_strips;
253
254
3
    return 0;
255
}
256
257
48249
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
{
262
    // score = FF_LAMBDA_SCALE * error + lambda * bits
263
    int x;
264
48249
    int entry_size = s->pix_fmt == AV_PIX_FMT_RGB24 ? 6 : 4;
265
48249
    int mb_count   = s->w * h / MB_AREA;
266
    mb_info *mb;
267
    int64_t score1, score2, score3;
268
48249
    int64_t ret = s->lambda * ((info->v1_size ? CHUNK_HEADER_SIZE + info->v1_size * entry_size : 0) +
269
48249
                               (info->v4_size ? CHUNK_HEADER_SIZE + info->v4_size * entry_size : 0) +
270
48249
                               CHUNK_HEADER_SIZE) << 3;
271
272

48249
    switch (info->mode) {
273
10542
    case MODE_V1_ONLY:
274
        // one byte per MB
275
10542
        ret += s->lambda * 8 * mb_count;
276
277
        // while calculating we assume all blocks are ENC_V1
278
3268398
        for (x = 0; x < mb_count; x++) {
279
3257856
            mb   = &s->mb[x];
280
3257856
            ret += FF_LAMBDA_SCALE * mb->v1_error;
281
            // this function is never called for report in MODE_V1_ONLY
282
            // if (!report)
283
3257856
            mb->best_encoding = ENC_V1;
284
        }
285
286
10542
        break;
287
13554
    case MODE_V1_V4:
288
        // 9 or 33 bits per MB
289
13554
        if (report) {
290
            // no moves between the corresponding training sets are allowed
291
6777
            *training_set_v1_shrunk = *training_set_v4_shrunk = 0;
292
2101113
            for (x = 0; x < mb_count; x++) {
293
                int mberr;
294
2094336
                mb = &s->mb[x];
295
2094336
                if (mb->best_encoding == ENC_V1)
296
664899
                    score1 = s->lambda * 9 + FF_LAMBDA_SCALE * (mberr = mb->v1_error);
297
                else
298
1429437
                    score1 = s->lambda * 33 + FF_LAMBDA_SCALE * (mberr = mb->v4_error);
299
2094336
                ret += score1;
300
            }
301
        } else { // find best mode per block
302
2101113
            for (x = 0; x < mb_count; x++) {
303
2094336
                mb     = &s->mb[x];
304
2094336
                score1 = s->lambda * 9 + FF_LAMBDA_SCALE * mb->v1_error;
305
2094336
                score2 = s->lambda * 33 + FF_LAMBDA_SCALE * mb->v4_error;
306
307
2094336
                if (score1 <= score2) {
308
664899
                    ret += score1;
309
664899
                    mb->best_encoding = ENC_V1;
310
                } else {
311
1429437
                    ret += score2;
312
1429437
                    mb->best_encoding = ENC_V4;
313
                }
314
            }
315
        }
316
317
13554
        break;
318
24153
    case MODE_MC:
319
        // 1, 10 or 34 bits per MB
320
24153
        if (report) {
321
17673
            int v1_shrunk = 0, v4_shrunk = 0;
322
5471753
            for (x = 0; x < mb_count; x++) {
323
5454080
                mb = &s->mb[x];
324
                // it is OK to move blocks to ENC_SKIP here
325
                // but not to any codebook encoding!
326
5454080
                score1 = s->lambda * 1 + FF_LAMBDA_SCALE * mb->skip_error;
327
5454080
                if (mb->best_encoding == ENC_SKIP) {
328
2516362
                    ret += score1;
329
2937718
                } else if (mb->best_encoding == ENC_V1) {
330
830323
                    if ((score2 = s->lambda * 10 + FF_LAMBDA_SCALE * mb->v1_error) >= score1) {
331
24137
                        mb->best_encoding = ENC_SKIP;
332
24137
                        ++v1_shrunk;
333
24137
                        ret += score1;
334
                    } else {
335
806186
                        ret += score2;
336
                    }
337
                } else {
338
2107395
                    if ((score3 = s->lambda * 34 + FF_LAMBDA_SCALE * mb->v4_error) >= score1) {
339
75551
                        mb->best_encoding = ENC_SKIP;
340
75551
                        ++v4_shrunk;
341
75551
                        ret += score1;
342
                    } else {
343
2031844
                        ret += score3;
344
                    }
345
                }
346
            }
347
17673
            *training_set_v1_shrunk = v1_shrunk;
348
17673
            *training_set_v4_shrunk = v4_shrunk;
349
        } else { // find best mode per block
350
1997136
            for (x = 0; x < mb_count; x++) {
351
1990656
                mb     = &s->mb[x];
352
1990656
                score1 = s->lambda * 1 + FF_LAMBDA_SCALE * mb->skip_error;
353
1990656
                score2 = s->lambda * 10 + FF_LAMBDA_SCALE * mb->v1_error;
354
1990656
                score3 = s->lambda * 34 + FF_LAMBDA_SCALE * mb->v4_error;
355
356

1990656
                if (score1 <= score2 && score1 <= score3) {
357
842199
                    ret += score1;
358
842199
                    mb->best_encoding = ENC_SKIP;
359
1148457
                } else if (score2 <= score3) {
360
314890
                    ret += score2;
361
314890
                    mb->best_encoding = ENC_V1;
362
                } else {
363
833567
                    ret += score3;
364
833567
                    mb->best_encoding = ENC_V4;
365
                }
366
            }
367
        }
368
369
24153
        break;
370
    }
371
372
48249
    return ret;
373
}
374
375
30759
static int write_chunk_header(unsigned char *buf, int chunk_type, int chunk_size)
376
{
377
30759
    buf[0] = chunk_type;
378
30759
    AV_WB24(&buf[1], chunk_size + CHUNK_HEADER_SIZE);
379
30759
    return CHUNK_HEADER_SIZE;
380
}
381
382
20506
static int encode_codebook(CinepakEncContext *s, int *codebook, int size,
383
                           int chunk_type_yuv, int chunk_type_gray,
384
                           unsigned char *buf)
385
{
386
20506
    int x, y, ret, entry_size = s->pix_fmt == AV_PIX_FMT_RGB24 ? 6 : 4;
387
20506
    int incremental_codebook_replacement_mode = 0; // hardcoded here,
388
    // the compiler should notice that this is a constant -- rl
389
390
20506
    ret = write_chunk_header(buf,
391
20506
                             s->pix_fmt == AV_PIX_FMT_RGB24 ?
392
20506
                             chunk_type_yuv  + (incremental_codebook_replacement_mode ? 1 : 0) :
393
                             chunk_type_gray + (incremental_codebook_replacement_mode ? 1 : 0),
394
20506
                             entry_size * size +
395
20506
                             (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
20506
    if (incremental_codebook_replacement_mode) {
403
        int flags = 0;
404
        int flagsind;
405
        for (x = 0; x < size; x++) {
406
            if (flags == 0) {
407
                flagsind = ret;
408
                ret     += 4;
409
                flags    = 0x80000000;
410
            } else
411
                flags = ((flags >> 1) | 0x80000000);
412
            for (y = 0; y < entry_size; y++)
413
                buf[ret++] = codebook[y + x * entry_size] ^ (y >= 4 ? 0x80 : 0);
414
            if ((flags & 0xffffffff) == 0xffffffff) {
415
                AV_WB32(&buf[flagsind], flags);
416
                flags = 0;
417
            }
418
        }
419
        if (flags)
420
            AV_WB32(&buf[flagsind], flags);
421
    } else
422
751672
        for (x = 0; x < size; x++)
423
5118162
            for (y = 0; y < entry_size; y++)
424
4386996
                buf[ret++] = codebook[y + x * entry_size] ^ (y >= 4 ? 0x80 : 0);
425
426
20506
    return ret;
427
}
428
429
// sets out to the sub picture starting at (x,y) in in
430
14989880
static void get_sub_picture(CinepakEncContext *s, int x, int y,
431
                            uint8_t * in_data[4], int  in_linesize[4],
432
                            uint8_t *out_data[4], int out_linesize[4])
433
{
434
14989880
    out_data[0]     = in_data[0] + x + y * in_linesize[0];
435
14989880
    out_linesize[0] = in_linesize[0];
436
437
14989880
    if (s->pix_fmt == AV_PIX_FMT_RGB24) {
438
14989880
        out_data[1]     = in_data[1] + (x >> 1) + (y >> 1) * in_linesize[1];
439
14989880
        out_linesize[1] = in_linesize[1];
440
441
14989880
        out_data[2]     = in_data[2] + (x >> 1) + (y >> 1) * in_linesize[2];
442
14989880
        out_linesize[2] = in_linesize[2];
443
    }
444
14989880
}
445
446
// decodes the V1 vector in mb into the 4x4 MB pointed to by data
447
5323426
static void decode_v1_vector(CinepakEncContext *s, uint8_t *data[4],
448
                             int linesize[4], int v1_vector, strip_info *info)
449
{
450
5323426
    int entry_size = s->pix_fmt == AV_PIX_FMT_RGB24 ? 6 : 4;
451
452
5323426
    data[0][0] =
453
5323426
    data[0][1] =
454
5323426
    data[0][    linesize[0]] =
455
5323426
    data[0][1 + linesize[0]] = info->v1_codebook[v1_vector * entry_size];
456
457
5323426
    data[0][2] =
458
5323426
    data[0][3] =
459
5323426
    data[0][2 + linesize[0]] =
460
5323426
    data[0][3 + linesize[0]] = info->v1_codebook[v1_vector * entry_size + 1];
461
462
5323426
    data[0][    2 * linesize[0]] =
463
5323426
    data[0][1 + 2 * linesize[0]] =
464
5323426
    data[0][    3 * linesize[0]] =
465
5323426
    data[0][1 + 3 * linesize[0]] = info->v1_codebook[v1_vector * entry_size + 2];
466
467
5323426
    data[0][2 + 2 * linesize[0]] =
468
5323426
    data[0][3 + 2 * linesize[0]] =
469
5323426
    data[0][2 + 3 * linesize[0]] =
470
5323426
    data[0][3 + 3 * linesize[0]] = info->v1_codebook[v1_vector * entry_size + 3];
471
472
5323426
    if (s->pix_fmt == AV_PIX_FMT_RGB24) {
473
5323426
        data[1][0] =
474
5323426
        data[1][1] =
475
5323426
        data[1][    linesize[1]] =
476
5323426
        data[1][1 + linesize[1]] = info->v1_codebook[v1_vector * entry_size + 4];
477
478
5323426
        data[2][0] =
479
5323426
        data[2][1] =
480
5323426
        data[2][    linesize[2]] =
481
5323426
        data[2][1 + linesize[2]] = info->v1_codebook[v1_vector * entry_size + 5];
482
    }
483
5323426
}
484
485
// decodes the V4 vectors in mb into the 4x4 MB pointed to by data
486
6993789
static void decode_v4_vector(CinepakEncContext *s, uint8_t *data[4],
487
                             int linesize[4], int *v4_vector, strip_info *info)
488
{
489
6993789
    int i, x, y, entry_size = s->pix_fmt == AV_PIX_FMT_RGB24 ? 6 : 4;
490
491
20981367
    for (i = y = 0; y < 4; y += 2) {
492
41962734
        for (x = 0; x < 4; x += 2, i++) {
493
27975156
            data[0][x     +  y      * linesize[0]] = info->v4_codebook[v4_vector[i] * entry_size];
494
27975156
            data[0][x + 1 +  y      * linesize[0]] = info->v4_codebook[v4_vector[i] * entry_size + 1];
495
27975156
            data[0][x     + (y + 1) * linesize[0]] = info->v4_codebook[v4_vector[i] * entry_size + 2];
496
27975156
            data[0][x + 1 + (y + 1) * linesize[0]] = info->v4_codebook[v4_vector[i] * entry_size + 3];
497
498
27975156
            if (s->pix_fmt == AV_PIX_FMT_RGB24) {
499
27975156
                data[1][(x >> 1) + (y >> 1) * linesize[1]] = info->v4_codebook[v4_vector[i] * entry_size + 4];
500
27975156
                data[2][(x >> 1) + (y >> 1) * linesize[2]] = info->v4_codebook[v4_vector[i] * entry_size + 5];
501
            }
502
        }
503
    }
504
6993789
}
505
506
883619
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
{
510
    int y, p;
511
512
4418095
    for (y = 0; y < MB_SIZE; y++)
513
3534476
        memcpy(a_data[0] + y * a_linesize[0], b_data[0] + y * b_linesize[0],
514
               MB_SIZE);
515
516
883619
    if (s->pix_fmt == AV_PIX_FMT_RGB24) {
517
2650857
        for (p = 1; p <= 2; p++)
518
5301714
            for (y = 0; y < MB_SIZE / 2; y++)
519
3534476
                memcpy(a_data[p] + y * a_linesize[p],
520
3534476
                       b_data[p] + y * b_linesize[p],
521
                       MB_SIZE / 2);
522
    }
523
883619
}
524
525
10253
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
{
530
10253
    int x, y, z, bits, temp_size, header_ofs, ret = 0, mb_count = s->w * h / MB_AREA;
531
    int needs_extra_bit, should_write_temp;
532
    uint32_t flags;
533
    unsigned char temp[64]; // 32/2 = 16 V4 blocks at 4 B each -> 64 B
534
    mb_info *mb;
535
10253
    uint8_t *sub_scratch_data[4] = { 0 }, *sub_last_data[4] = { 0 };
536
10253
    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

10253
    if (info->v4_size || !s->skip_empty_cb)
543
10253
        ret += encode_codebook(s, info->v4_codebook, info->v4_size, 0x20, 0x24, buf + ret);
544
545

10253
    if (info->v1_size || !s->skip_empty_cb)
546
10253
        ret += encode_codebook(s, info->v1_codebook, info->v1_size, 0x22, 0x26, buf + ret);
547
548
    // update scratch picture
549
109405
    for (z = y = 0; y < h; y += MB_SIZE)
550
3272016
        for (x = 0; x < s->w; x += MB_SIZE, z++) {
551
3172864
            mb = &s->mb[z];
552
553
3172864
            get_sub_picture(s, x, y, scratch_data, scratch_linesize,
554
                            sub_scratch_data, sub_scratch_linesize);
555
556

3172864
            if (info->mode == MODE_MC && mb->best_encoding == ENC_SKIP) {
557
883619
                get_sub_picture(s, x, y, last_data, last_linesize,
558
                                sub_last_data, sub_last_linesize);
559
883619
                copy_mb(s, sub_scratch_data, sub_scratch_linesize,
560
                        sub_last_data, sub_last_linesize);
561

2289245
            } else if (info->mode == MODE_V1_ONLY || mb->best_encoding == ENC_V1)
562
828313
                decode_v1_vector(s, sub_scratch_data, sub_scratch_linesize,
563
                                 mb->v1_vector, info);
564
            else
565
1460932
                decode_v4_vector(s, sub_scratch_data, sub_scratch_linesize,
566
1460932
                                 mb->v4_vector, info);
567
        }
568
569

10253
    switch (info->mode) {
570
1018
    case MODE_V1_ONLY:
571
1018
        ret += write_chunk_header(buf + ret, 0x32, mb_count);
572
573
316922
        for (x = 0; x < mb_count; x++)
574
315904
            buf[ret++] = s->mb[x].v1_vector;
575
576
1018
        break;
577
3665
    case MODE_V1_V4:
578
        // remember header position
579
3665
        header_ofs = ret;
580
3665
        ret       += CHUNK_HEADER_SIZE;
581
582
39285
        for (x = 0; x < mb_count; x += 32) {
583
35620
            flags = 0;
584

1175460
            for (y = x; y < FFMIN(x + 32, mb_count); y++)
585
1139840
                if (s->mb[y].best_encoding == ENC_V4)
586
861679
                    flags |= 1U << (31 - y + x);
587
588
35620
            AV_WB32(&buf[ret], flags);
589
35620
            ret += 4;
590
591

1175460
            for (y = x; y < FFMIN(x + 32, mb_count); y++) {
592
1139840
                mb = &s->mb[y];
593
594
1139840
                if (mb->best_encoding == ENC_V1)
595
278161
                    buf[ret++] = mb->v1_vector;
596
                else
597
4308395
                    for (z = 0; z < 4; z++)
598
3446716
                        buf[ret++] = mb->v4_vector[z];
599
            }
600
        }
601
602
3665
        write_chunk_header(buf + header_ofs, 0x30, ret - header_ofs - CHUNK_HEADER_SIZE);
603
604
3665
        break;
605
5570
    case MODE_MC:
606
        // remember header position
607
5570
        header_ofs = ret;
608
5570
        ret       += CHUNK_HEADER_SIZE;
609
5570
        flags      = bits = temp_size = 0;
610
611
1722690
        for (x = 0; x < mb_count; x++) {
612
1717120
            mb                = &s->mb[x];
613
1717120
            flags            |= (uint32_t)(mb->best_encoding != ENC_SKIP) << (31 - bits++);
614
1717120
            needs_extra_bit   = 0;
615
1717120
            should_write_temp = 0;
616
617
1717120
            if (mb->best_encoding != ENC_SKIP) {
618
833501
                if (bits < 32)
619
810194
                    flags |= (uint32_t)(mb->best_encoding == ENC_V4) << (31 - bits++);
620
                else
621
23307
                    needs_extra_bit = 1;
622
            }
623
624
1717120
            if (bits == 32) {
625
76945
                AV_WB32(&buf[ret], flags);
626
76945
                ret  += 4;
627
76945
                flags = bits = 0;
628
629

76945
                if (mb->best_encoding == ENC_SKIP || needs_extra_bit) {
630
50298
                    memcpy(&buf[ret], temp, temp_size);
631
50298
                    ret      += temp_size;
632
50298
                    temp_size = 0;
633
                } else
634
26647
                    should_write_temp = 1;
635
            }
636
637
1717120
            if (needs_extra_bit) {
638
23307
                flags = (uint32_t)(mb->best_encoding == ENC_V4) << 31;
639
23307
                bits  = 1;
640
            }
641
642
1717120
            if (mb->best_encoding == ENC_V1)
643
234248
                temp[temp_size++] = mb->v1_vector;
644
1482872
            else if (mb->best_encoding == ENC_V4)
645
2996265
                for (z = 0; z < 4; z++)
646
2397012
                    temp[temp_size++] = mb->v4_vector[z];
647
648
1717120
            if (should_write_temp) {
649
26647
                memcpy(&buf[ret], temp, temp_size);
650
26647
                ret      += temp_size;
651
26647
                temp_size = 0;
652
            }
653
        }
654
655
5570
        if (bits > 0) {
656
5386
            AV_WB32(&buf[ret], flags);
657
5386
            ret += 4;
658
5386
            memcpy(&buf[ret], temp, temp_size);
659
5386
            ret += temp_size;
660
        }
661
662
5570
        write_chunk_header(buf + header_ofs, 0x31, ret - header_ofs - CHUNK_HEADER_SIZE);
663
664
5570
        break;
665
    }
666
667
10253
    return ret;
668
}
669
670
// computes distortion of 4x4 MB in b compared to a
671
10249154
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
{
675
10249154
    int x, y, p, d, ret = 0;
676
677
51245770
    for (y = 0; y < MB_SIZE; y++)
678
204983080
        for (x = 0; x < MB_SIZE; x++) {
679
163986464
            d = a_data[0][x + y * a_linesize[0]] - b_data[0][x + y * b_linesize[0]];
680
163986464
            ret += d * d;
681
        }
682
683
10249154
    if (s->pix_fmt == AV_PIX_FMT_RGB24) {
684
30747462
        for (p = 1; p <= 2; p++) {
685
61494924
            for (y = 0; y < MB_SIZE / 2; y++)
686
122989848
                for (x = 0; x < MB_SIZE / 2; x++) {
687
81993232
                    d = a_data[p][x + y * a_linesize[p]] - b_data[p][x + y * b_linesize[p]];
688
81993232
                    ret += d * d;
689
                }
690
        }
691
    }
692
693
10249154
    return ret;
694
}
695
696
// return the possibly adjusted size of the codebook
697
#define CERTAIN(x) ((x) != ENC_UNCERTAIN)
698
58459
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
{
702
    int x, y, i, j, k, x2, y2, x3, y3, plane, shift, mbn;
703
58459
    int entry_size      = s->pix_fmt == AV_PIX_FMT_RGB24 ? 6 : 4;
704
58459
    int *codebook       = v1mode ? info->v1_codebook : info->v4_codebook;
705
58459
    int size            = v1mode ? info->v1_size : info->v4_size;
706
58459
    int64_t total_error = 0;
707
    uint8_t vq_pict_buf[(MB_AREA * 3) / 2];
708
    uint8_t     *sub_data[4],     *vq_data[4];
709
    int      sub_linesize[4],  vq_linesize[4];
710
711
623443
    for (mbn = i = y = 0; y < h; y += MB_SIZE) {
712
18644472
        for (x = 0; x < s->w; x += MB_SIZE, ++mbn) {
713
            int *base;
714
715
18079488
            if (CERTAIN(encoding)) {
716
                // use for the training only the blocks known to be to be encoded [sic:-]
717
12727296
                if (s->mb[mbn].best_encoding != encoding)
718
8051518
                    continue;
719
            }
720
721
10027970
            base = s->codebook_input + i * entry_size;
722
10027970
            if (v1mode) {
723
                // subsample
724
17980452
                for (j = y2 = 0; y2 < entry_size; y2 += 2)
725
40456017
                    for (x2 = 0; x2 < 4; x2 += 2, j++) {
726
26970678
                        plane   = y2 < 4 ? 0 : 1 + (x2 >> 1);
727
26970678
                        shift   = y2 < 4 ? 0 : 1;
728
26970678
                        x3      = shift ? 0 : x2;
729
26970678
                        y3      = shift ? 0 : y2;
730
26970678
                        base[j] = (data[plane][((x + x3) >> shift) +      ((y + y3) >> shift)      * linesize[plane]] +
731
26970678
                                   data[plane][((x + x3) >> shift) + 1 +  ((y + y3) >> shift)      * linesize[plane]] +
732
26970678
                                   data[plane][((x + x3) >> shift) +     (((y + y3) >> shift) + 1) * linesize[plane]] +
733
26970678
                                   data[plane][((x + x3) >> shift) + 1 + (((y + y3) >> shift) + 1) * linesize[plane]]) >> 2;
734
                    }
735
            } else {
736
                // copy
737
16598571
                for (j = y2 = 0; y2 < MB_SIZE; y2 += 2) {
738
33197142
                    for (x2 = 0; x2 < MB_SIZE; x2 += 2)
739
154919996
                        for (k = 0; k < entry_size; k++, j++) {
740
132788568
                            plane = k >= 4 ? k - 3 : 0;
741
742
132788568
                            if (k >= 4) {
743
44262856
                                x3 = (x + x2) >> 1;
744
44262856
                                y3 = (y + y2) >> 1;
745
                            } else {
746
88525712
                                x3 = x + x2 + (k & 1);
747
88525712
                                y3 = y + y2 + (k >> 1);
748
                            }
749
750
132788568
                            base[j] = data[plane][x3 + y3 * linesize[plane]];
751
                        }
752
                }
753
            }
754
10027970
            i += v1mode ? 1 : 4;
755
        }
756
    }
757
758
58459
    if (i == 0) // empty training set, nothing to do
759
34
        return 0;
760
58425
    if (i < size)
761
5040
        size = i;
762
763
58425
    avpriv_init_elbg(s->codebook_input, entry_size, i, codebook, size, 1, s->codebook_closest, &s->randctx);
764
58425
    avpriv_do_elbg(s->codebook_input, entry_size, i, codebook, size, 1, s->codebook_closest, &s->randctx);
765
766
    // set up vq_data, which contains a single MB
767
58425
    vq_data[0]     = vq_pict_buf;
768
58425
    vq_linesize[0] = MB_SIZE;
769
58425
    vq_data[1]     = &vq_pict_buf[MB_AREA];
770
58425
    vq_data[2]     = vq_data[1] + (MB_AREA >> 2);
771
58425
    vq_linesize[1] =
772
58425
    vq_linesize[2] = MB_SIZE >> 1;
773
774
    // copy indices
775
623121
    for (i = j = y = 0; y < h; y += MB_SIZE)
776
18634968
        for (x = 0; x < s->w; x += MB_SIZE, j++) {
777
18070272
            mb_info *mb = &s->mb[j];
778
            // skip uninteresting blocks if we know their preferred encoding
779

18070272
            if (CERTAIN(encoding) && mb->best_encoding != encoding)
780
8042302
                continue;
781
782
            // point sub_data to current MB
783
10027970
            get_sub_picture(s, x, y, data, linesize, sub_data, sub_linesize);
784
785
10027970
            if (v1mode) {
786
4495113
                mb->v1_vector = s->codebook_closest[i];
787
788
                // fill in vq_data with V1 data
789
4495113
                decode_v1_vector(s, vq_data, vq_linesize, mb->v1_vector, info);
790
791
4495113
                mb->v1_error = compute_mb_distortion(s, sub_data, sub_linesize,
792
                                                     vq_data, vq_linesize);
793
4495113
                total_error += mb->v1_error;
794
            } else {
795
27664285
                for (k = 0; k < 4; k++)
796
22131428
                    mb->v4_vector[k] = s->codebook_closest[i + k];
797
798
                // fill in vq_data with V4 data
799
5532857
                decode_v4_vector(s, vq_data, vq_linesize, mb->v4_vector, info);
800
801
5532857
                mb->v4_error = compute_mb_distortion(s, sub_data, sub_linesize,
802
                                                     vq_data, vq_linesize);
803
5532857
                total_error += mb->v4_error;
804
            }
805
10027970
            i += v1mode ? 1 : 4;
806
        }
807
    // check that we did it right in the beginning of the function
808
58425
    av_assert0(i >= size); // training set is no smaller than the codebook
809
810
58425
    return size;
811
}
812
813
720
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
{
818
    int x, y, i;
819
    uint8_t *sub_last_data    [4], *sub_pict_data    [4];
820
    int      sub_last_linesize[4],  sub_pict_linesize[4];
821
822
7632
    for (i = y = 0; y < h; y += MB_SIZE)
823
228096
        for (x = 0; x < s->w; x += MB_SIZE, i++) {
824
221184
            get_sub_picture(s, x, y, last_data, last_linesize,
825
                            sub_last_data, sub_last_linesize);
826
221184
            get_sub_picture(s, x, y, data, linesize,
827
                            sub_pict_data, sub_pict_linesize);
828
829
221184
            s->mb[i].skip_error =
830
221184
                compute_mb_distortion(s,
831
                                      sub_last_data, sub_last_linesize,
832
                                      sub_pict_data, sub_pict_linesize);
833
        }
834
720
}
835
836
10253
static void write_strip_header(CinepakEncContext *s, int y, int h, int keyframe,
837
                               unsigned char *buf, int strip_size)
838
{
839
    // actually we are exclusively using intra strip coding (how much can we win
840
    // otherwise? how to choose which part of a codebook to update?),
841
    // keyframes are different only because we disallow ENC_SKIP on them -- rl
842
    // (besides, the logic here used to be inverted: )
843
    //    buf[0] = keyframe ? 0x11: 0x10;
844
10253
    buf[0] = keyframe ? 0x10 : 0x11;
845
10253
    AV_WB24(&buf[1], strip_size + STRIP_HEADER_SIZE);
846
    // AV_WB16(&buf[4], y); /* using absolute y values works -- rl */
847
10253
    AV_WB16(&buf[4], 0); /* using relative values works as well -- rl */
848
10253
    AV_WB16(&buf[6], 0);
849
    // AV_WB16(&buf[8], y + h); /* using absolute y values works -- rl */
850
10253
    AV_WB16(&buf[8], h); /* using relative values works as well -- rl */
851
10253
    AV_WB16(&buf[10], s->w);
852
10253
}
853
854
753
static int rd_strip(CinepakEncContext *s, int y, int h, int keyframe,
855
                    uint8_t *last_data[4], int last_linesize[4],
856
                    uint8_t *data[4], int linesize[4],
857
                    uint8_t *scratch_data[4], int scratch_linesize[4],
858
                    unsigned char *buf, int64_t *best_score)
859
{
860
753
    int64_t score = 0;
861
753
    int best_size = 0;
862
    strip_info info;
863
    // for codebook optimization:
864
    int v1enough, v1_size, v4enough, v4_size;
865
    int new_v1_size, new_v4_size;
866
    int v1shrunk, v4shrunk;
867
868
753
    if (!keyframe)
869
720
        calculate_skip_errors(s, h, last_data, last_linesize, data, linesize,
870
                              &info);
871
872
    // try some powers of 4 for the size of the codebooks
873
    // constraint the v4 codebook to be no bigger than v1 one,
874
    // (and no less than v1_size/4)
875
    // thus making v1 preferable and possibly losing small details? should be ok
876
#define SMALLEST_CODEBOOK 1
877

4518
    for (v1enough = 0, v1_size = SMALLEST_CODEBOOK; v1_size <= CODEBOOK_MAX && !v1enough; v1_size <<= 2) {
878


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) {
879
            CinepakMode mode;
880
            // try all modes
881
42168
            for (mode = 0; mode < MODE_COUNT; mode++) {
882
                // don't allow MODE_MC in intra frames
883

31626
                if (keyframe && mode == MODE_MC)
884
462
                    continue;
885
886
31164
                if (mode == MODE_V1_ONLY) {
887
10542
                    info.v1_size = v1_size;
888
                    // the size may shrink even before optimizations if the input is short:
889
10542
                    info.v1_size = quantize(s, h, data, linesize, 1,
890
                                            &info, ENC_UNCERTAIN);
891
10542
                    if (info.v1_size < v1_size)
892
                        // too few eligible blocks, no sense in trying bigger sizes
893
                        v1enough = 1;
894
895
10542
                    info.v4_size = 0;
896
                } else { // mode != MODE_V1_ONLY
897
                    // if v4 codebook is empty then only allow V1-only mode
898
20622
                    if (!v4_size)
899
7365
                        continue;
900
901
13257
                    if (mode == MODE_V1_V4) {
902
6777
                        info.v4_size = v4_size;
903
6777
                        info.v4_size = quantize(s, h, data, linesize, 0,
904
                                                &info, ENC_UNCERTAIN);
905
6777
                        if (info.v4_size < v4_size)
906
                            // too few eligible blocks, no sense in trying bigger sizes
907
                            v4enough = 1;
908
                    }
909
                }
910
911
23799
                info.mode = mode;
912
                // choose the best encoding per block, based on current experience
913
23799
                score = calculate_mode_score(s, h, &info, 0,
914
                                             &v1shrunk, &v4shrunk);
915
916
23799
                if (mode != MODE_V1_ONLY) {
917
13257
                    int extra_iterations_limit = s->max_extra_cb_iterations;
918
                    // recompute the codebooks, omitting the extra blocks
919
                    // we assume we _may_ come here with more blocks to encode than before
920
13257
                    info.v1_size = v1_size;
921
13257
                    new_v1_size = quantize(s, h, data, linesize, 1, &info, ENC_V1);
922
13257
                    if (new_v1_size < info.v1_size)
923
5041
                        info.v1_size = new_v1_size;
924
                    // we assume we _may_ come here with more blocks to encode than before
925
13257
                    info.v4_size = v4_size;
926
13257
                    new_v4_size = quantize(s, h, data, linesize, 0, &info, ENC_V4);
927
13257
                    if (new_v4_size < info.v4_size)
928
19
                        info.v4_size = new_v4_size;
929
                    // calculate the resulting score
930
                    // (do not move blocks to codebook encodings now, as some blocks may have
931
                    // got bigger errors despite a smaller training set - but we do not
932
                    // ever grow the training sets back)
933
                    for (;;) {
934
24450
                        score = calculate_mode_score(s, h, &info, 1,
935
                                                     &v1shrunk, &v4shrunk);
936
                        // do we have a reason to reiterate? if so, have we reached the limit?
937

24450
                        if ((!v1shrunk && !v4shrunk) || !extra_iterations_limit--)
938
                            break;
939
                        // recompute the codebooks, omitting the extra blocks
940
11193
                        if (v1shrunk) {
941
4984
                            info.v1_size = v1_size;
942
4984
                            new_v1_size = quantize(s, h, data, linesize, 1, &info, ENC_V1);
943
4984
                            if (new_v1_size < info.v1_size)
944
12
                                info.v1_size = new_v1_size;
945
                        }
946
11193
                        if (v4shrunk) {
947
9642
                            info.v4_size = v4_size;
948
9642
                            new_v4_size = quantize(s, h, data, linesize, 0, &info, ENC_V4);
949
9642
                            if (new_v4_size < info.v4_size)
950
2
                                info.v4_size = new_v4_size;
951
                        }
952
                    }
953
                }
954
955

23799
                if (best_size == 0 || score < *best_score) {
956
10253
                    *best_score = score;
957
10253
                    best_size = encode_mode(s, h,
958
                                            scratch_data, scratch_linesize,
959
                                            last_data, last_linesize, &info,
960
10253
                                            s->strip_buf + STRIP_HEADER_SIZE);
961
962
10253
                    write_strip_header(s, y, h, keyframe, s->strip_buf, best_size);
963
                }
964
            }
965
        }
966
    }
967
968
753
    best_size += STRIP_HEADER_SIZE;
969
753
    memcpy(buf, s->strip_buf, best_size);
970
971
753
    return best_size;
972
}
973
974
303
static int write_cvid_header(CinepakEncContext *s, unsigned char *buf,
975
                             int num_strips, int data_size, int isakeyframe)
976
{
977
303
    buf[0] = isakeyframe ? 0 : 1;
978
303
    AV_WB24(&buf[1], data_size + CVID_HEADER_SIZE);
979
303
    AV_WB16(&buf[4], s->w);
980
303
    AV_WB16(&buf[6], s->h);
981
303
    AV_WB16(&buf[8], num_strips);
982
983
303
    return CVID_HEADER_SIZE;
984
}
985
986
150
static int rd_frame(CinepakEncContext *s, const AVFrame *frame,
987
                    int isakeyframe, unsigned char *buf, int buf_size)
988
{
989
    int num_strips, strip, i, y, nexty, size, temp_size, best_size;
990
    uint8_t *last_data    [4], *data    [4], *scratch_data    [4];
991
    int      last_linesize[4],  linesize[4],  scratch_linesize[4];
992
150
    int64_t best_score = 0, score, score_temp;
993
    int best_nstrips;
994
995
150
    if (s->pix_fmt == AV_PIX_FMT_RGB24) {
996
        int x;
997
        // build a copy of the given frame in the correct colorspace
998
7350
        for (y = 0; y < s->h; y += 2)
999
468000
            for (x = 0; x < s->w; x += 2) {
1000
                uint8_t *ir[2];
1001
                int32_t r, g, b, rr, gg, bb;
1002
460800
                ir[0] = frame->data[0] + x * 3 + y * frame->linesize[0];
1003
460800
                ir[1] = ir[0] + frame->linesize[0];
1004
460800
                get_sub_picture(s, x, y,
1005
460800
                                s->input_frame->data, s->input_frame->linesize,
1006
                                scratch_data, scratch_linesize);
1007
460800
                r = g = b = 0;
1008
2304000
                for (i = 0; i < 4; ++i) {
1009
                    int i1, i2;
1010
1843200
                    i1 = (i & 1);
1011
1843200
                    i2 = (i >= 2);
1012
1843200
                    rr = ir[i2][i1 * 3 + 0];
1013
1843200
                    gg = ir[i2][i1 * 3 + 1];
1014
1843200
                    bb = ir[i2][i1 * 3 + 2];
1015
1843200
                    r += rr;
1016
1843200
                    g += gg;
1017
1843200
                    b += bb;
1018
                    // using fixed point arithmetic for portable repeatability, scaling by 2^23
1019
                    // "Y"
1020
                    // rr = 0.2857 * rr + 0.5714 * gg + 0.1429 * bb;
1021
1843200
                    rr = (2396625 * rr + 4793251 * gg + 1198732 * bb) >> 23;
1022
1843200
                    if (rr < 0)
1023
                        rr = 0;
1024
1843200
                    else if (rr > 255)
1025
                        rr = 255;
1026
1843200
                    scratch_data[0][i1 + i2 * scratch_linesize[0]] = rr;
1027
                }
1028
                // let us scale down as late as possible
1029
                //                r /= 4; g /= 4; b /= 4;
1030
                // "U"
1031
                // rr = -0.1429 * r - 0.2857 * g + 0.4286 * b;
1032
460800
                rr = (-299683 * r - 599156 * g + 898839 * b) >> 23;
1033
460800
                if (rr < -128)
1034
                    rr = -128;
1035
460800
                else if (rr > 127)
1036
                    rr = 127;
1037
460800
                scratch_data[1][0] = rr + 128; // quantize needs unsigned
1038
                // "V"
1039
                // rr = 0.3571 * r - 0.2857 * g - 0.0714 * b;
1040
460800
                rr = (748893 * r - 599156 * g - 149737 * b) >> 23;
1041
460800
                if (rr < -128)
1042
                    rr = -128;
1043
460800
                else if (rr > 127)
1044
                    rr = 127;
1045
460800
                scratch_data[2][0] = rr + 128; // quantize needs unsigned
1046
            }
1047
    }
1048
1049
    // would be nice but quite certainly incompatible with vintage players:
1050
    // support encoding zero strips (meaning skip the whole frame)
1051

453
    for (num_strips = s->min_strips; num_strips <= s->max_strips && num_strips <= s->h / MB_SIZE; num_strips++) {
1052
303
        score = 0;
1053
303
        size  = 0;
1054
1055
1056
        for (y = 0, strip = 1; y < s->h; strip++, y = nexty) {
1056
            int strip_height;
1057
1058
753
            nexty = strip * s->h / num_strips; // <= s->h
1059
            // make nexty the next multiple of 4 if not already there
1060
753
            if (nexty & 3)
1061
                nexty += 4 - (nexty & 3);
1062
1063
753
            strip_height = nexty - y;
1064
753
            if (strip_height <= 0) { // can this ever happen?
1065
                av_log(s->avctx, AV_LOG_INFO, "skipping zero height strip %i of %i\n", strip, num_strips);
1066
                continue;
1067
            }
1068
1069
753
            if (s->pix_fmt == AV_PIX_FMT_RGB24)
1070
753
                get_sub_picture(s, 0, y,
1071
753
                                s->input_frame->data, s->input_frame->linesize,
1072
                                data, linesize);
1073
            else
1074
                get_sub_picture(s, 0, y,
1075
                                (uint8_t **)frame->data, (int *)frame->linesize,
1076
                                data, linesize);
1077
753
            get_sub_picture(s, 0, y,
1078
753
                            s->last_frame->data, s->last_frame->linesize,
1079
                            last_data, last_linesize);
1080
753
            get_sub_picture(s, 0, y,
1081
753
                            s->scratch_frame->data, s->scratch_frame->linesize,
1082
                            scratch_data, scratch_linesize);
1083
1084
753
            if ((temp_size = rd_strip(s, y, strip_height, isakeyframe,
1085
                                      last_data, last_linesize, data, linesize,
1086
                                      scratch_data, scratch_linesize,
1087
753
                                      s->frame_buf + size + CVID_HEADER_SIZE,
1088
                                      &score_temp)) < 0)
1089
                return temp_size;
1090
1091
753
            score += score_temp;
1092
753
            size += temp_size;
1093
        }
1094
1095

303
        if (best_score == 0 || score < best_score) {
1096
303
            best_score = score;
1097
303
            best_size = size + write_cvid_header(s, s->frame_buf, num_strips, size, isakeyframe);
1098
1099
303
            FFSWAP(AVFrame *, s->best_frame, s->scratch_frame);
1100
303
            memcpy(buf, s->frame_buf, best_size);
1101
303
            best_nstrips = num_strips;
1102
        }
1103
        // avoid trying too many strip numbers without a real reason
1104
        // (this makes the processing of the very first frame faster)
1105
303
        if (num_strips - best_nstrips > 4)
1106
            break;
1107
    }
1108
1109
    // let the number of strips slowly adapt to the changes in the contents,
1110
    // compared to full bruteforcing every time this will occasionally lead
1111
    // to some r/d performance loss but makes encoding up to several times faster
1112
150
    if (!s->strip_number_delta_range) {
1113
        if (best_nstrips == s->max_strips) { // let us try to step up
1114
            s->max_strips = best_nstrips + 1;
1115
            if (s->max_strips >= s->max_max_strips)
1116
                s->max_strips = s->max_max_strips;
1117
        } else { // try to step down
1118
            s->max_strips = best_nstrips;
1119
        }
1120
        s->min_strips = s->max_strips - 1;
1121
        if (s->min_strips < s->min_min_strips)
1122
            s->min_strips = s->min_min_strips;
1123
    } else {
1124
150
        s->max_strips = best_nstrips + s->strip_number_delta_range;
1125
150
        if (s->max_strips >= s->max_max_strips)
1126
150
            s->max_strips = s->max_max_strips;
1127
150
        s->min_strips = best_nstrips - s->strip_number_delta_range;
1128
150
        if (s->min_strips < s->min_min_strips)
1129
            s->min_strips = s->min_min_strips;
1130
    }
1131
1132
150
    return best_size;
1133
}
1134
1135
150
static int cinepak_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
1136
                                const AVFrame *frame, int *got_packet)
1137
{
1138
150
    CinepakEncContext *s = avctx->priv_data;
1139
    int ret;
1140
1141
150
    s->lambda = frame->quality ? frame->quality - 1 : 2 * FF_LAMBDA_SCALE;
1142
1143
150
    if ((ret = ff_alloc_packet2(avctx, pkt, s->frame_buf_size, 0)) < 0)
1144
        return ret;
1145
150
    ret       = rd_frame(s, frame, (s->curframe == 0), pkt->data, s->frame_buf_size);
1146
150
    pkt->size = ret;
1147
150
    if (s->curframe == 0)
1148
6
        pkt->flags |= AV_PKT_FLAG_KEY;
1149
150
    *got_packet = 1;
1150
1151
150
    FFSWAP(AVFrame *, s->last_frame, s->best_frame);
1152
1153
150
    if (++s->curframe >= s->keyint)
1154
6
        s->curframe = 0;
1155
1156
150
    return 0;
1157
}
1158
1159
3
static av_cold int cinepak_encode_end(AVCodecContext *avctx)
1160
{
1161
3
    CinepakEncContext *s = avctx->priv_data;
1162
    int x;
1163
1164
3
    av_frame_free(&s->last_frame);
1165
3
    av_frame_free(&s->best_frame);
1166
3
    av_frame_free(&s->scratch_frame);
1167
3
    if (avctx->pix_fmt == AV_PIX_FMT_RGB24)
1168
3
        av_frame_free(&s->input_frame);
1169
3
    av_freep(&s->codebook_input);
1170
3
    av_freep(&s->codebook_closest);
1171
3
    av_freep(&s->strip_buf);
1172
3
    av_freep(&s->frame_buf);
1173
3
    av_freep(&s->mb);
1174
1175

15
    for (x = 0; x < (avctx->pix_fmt == AV_PIX_FMT_RGB24 ? 4 : 3); x++)
1176
12
        av_freep(&s->pict_bufs[x]);
1177
1178
3
    return 0;
1179
}
1180
1181
AVCodec ff_cinepak_encoder = {
1182
    .name           = "cinepak",
1183
    .long_name      = NULL_IF_CONFIG_SMALL("Cinepak"),
1184
    .type           = AVMEDIA_TYPE_VIDEO,
1185
    .id             = AV_CODEC_ID_CINEPAK,
1186
    .priv_data_size = sizeof(CinepakEncContext),
1187
    .init           = cinepak_encode_init,
1188
    .encode2        = cinepak_encode_frame,
1189
    .close          = cinepak_encode_end,
1190
    .pix_fmts       = (const enum AVPixelFormat[]) { AV_PIX_FMT_RGB24, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE },
1191
    .priv_class     = &cinepak_class,
1192
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
1193
};